8000 numpy_pytorch_interop/Readme.md at mandelbrot_compile · Quansight-Labs/numpy_pytorch_interop · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":"autogen","path":"autogen","contentType":"directory"},{"name":"e2e","path":"e2e","contentType":"directory"},{"name":"torch_np","path":"torch_np","contentType":"directory"},{"name":".git-blame-ignore-revs","path":".git-blame-ignore-revs","contentType":"file"},{"name":".gitattributes","path":".gitattributes","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":".pre-commit-config.yaml","path":".pre-commit-config.yaml","contentType":"file"},{"name":"RFC.md","path":"RFC.md","contentType":"file"},{"name":"Readme.md","path":"Readme.md","contentType":"file"},{"name":"pyproject.toml","path":"pyproject.toml","contentType":"file"},{"name":"setup.py","path":"setup.py","contentType":"file"}],"totalCount":12}},"fileTreeProcessingTime":2.451234,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":577305728,"defaultBranch":"main","name":"numpy_pytorch_interop","ownerLogin":"Quansight-Labs","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2022-12-12T12:55:18.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/42948208?v=4","public":true,"private":false,"isOrgOwned":true},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"mandelbrot_compile","listCacheKey":"v0:1694703568.0","canEdit":false,"refType":"branch","currentOid":"db1710a95d8adee00121840fa483603b3ac69067"},"path":"Readme.md","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/Quansight-Labs/numpy_pytorch_interop/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"Readme.md","displayUrl":"https://github.com/Quansight-Labs/numpy_pytorch_interop/blob/mandelbrot_compile/Readme.md?raw=true","headerInfo":{"blobSize":"4.25 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"1de1870","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2FQuansight-Labs%2Fnumpy_pytorch_interop%2Fblob%2Fmandelbrot_compile%2FReadme.md","isCSV":false,"isRichtext":true,"toc":[{"level":1,"text":"JIT compiled mode","anchor":"jit-compiled-mode","htmlText":"JIT compiled mode"},{"level":2,"text":"Maze path-finding","anchor":"maze-path-finding","htmlText":"Maze path-finding"},{"level":2,"text":"CFD diffusion/advecton process","anchor":"cfd-diffusionadvecton-process","htmlText":"CFD diffusion/advecton process"},{"level":2,"text":"Mandelbrot fractal","anchor":"mandelbrot-fractal","htmlText":"Mandelbrot fractal"},{"level":2,"text":"K-means clustering","anchor":"k-means-clustering","htmlText":"K-means clustering"},{"level":1,"text":"Eager mode","anchor":"eager-mode","htmlText":"Eager mode"},{"level":2,"text":"A toy NN from scratch using numpy","anchor":"a-toy-nn-from-scratch-using-numpy","htmlText":"A toy NN from scratch using numpy"},{"level":3,"text":"Tweaks","anchor":"tweaks","htmlText":"Tweaks"},{"level":3,"text":"Results","anchor":"results","htmlText":"Results"},{"level":2,"text":"Build a maze and find a path in it","anchor":"build-a-maze-and-find-a-path-in-it","htmlText":"Build a maze and find a path in it"},{"level":3,"text":"Tweaks","anchor":"tweaks-1","htmlText":"Tweaks"},{"level":2,"text":"Diffusion/advection","anchor":"diffusionadvection","htmlText":"Diffusion/advection"},{"level":3,"text":"Tweaks","anchor":"tweaks-2","htmlText":"Tweaks"},{"level":2,"text":"Mandelbrot fractal","anchor":"mandelbrot-fractal-1","htmlText":"Mandelbrot fractal"},{"level":3,"text":"Tweaks","anchor":"tweaks-3","htmlText":"Tweaks"}],"lineInfo":{"truncatedLoc":"135","truncatedSloc":"73"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"Markdown","languageID":222,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/Quansight-Labs/numpy_pytorch_interop/blob/mandelbrot_compile/Readme.md","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/Quansight-Labs/numpy_pytorch_interop/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/Quansight-Labs/numpy_pytorch_interop/raw/refs/heads/mandelbrot_compile/Readme.md","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cp dir=\"auto\"\u003eTo test our wrapper, we use two strategies:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eport parts of the numpy test suite\u003c/li\u003e\n\u003cli\u003erun several small examples which use NumPy and check that the results are identical to original NumPy.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp dir=\"auto\"\u003eWe only run tests in the eager mode by replacing \u003ccode\u003eimport numpy as np\u003c/code\u003e by \u003ccode\u003eimport torch_np as np\u003c/code\u003e.\nExamples we run in both eager and JIT modes.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFor numpy tests, see \u003ccode\u003etorch_np/testing/numpy_tests\u003c/code\u003e folder.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003e\u003ccode\u003ee2e\u003c/code\u003e folder contains examples we run our wrapper on:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eA toy NN from scratch using numpy\u003c/li\u003e\n\u003cli\u003eBuild a random maze and find a path in it\u003c/li\u003e\n\u003cli\u003eSimulate a diffusion/advection process\u003c/li\u003e\n\u003cli\u003eConstruct and visualize the Mandelbrot fractal\u003c/li\u003e\n\u003cli\u003eInner operation of the k-means clustering\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eJIT compiled mode\u003c/h1\u003e\u003ca id=\"user-content-jit-compiled-mode\" class=\"anchor\" aria-label=\"Permalink: JIT compiled mode\" href=\"#jit-compiled-mode\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe main observation is that \u003ccode\u003etorch.dynamo\u003c/code\u003e unrolls python-level loops. For\niterative algorithms this leads to very long compile times. We therefore\noften only compile the inner loop.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eMaze path-finding\u003c/h2\u003e\u003ca id=\"user-content-maze-path-finding\" class=\"anchor\" aria-label=\"Permalink: Maze path-finding\" href=\"#maze-path-finding\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe Bellman-Ford algorithm simply does not compile because it contains a\ndata-dependent loop \u003ccode\u003ewhile point != start\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eCFD diffusion/advecton process\u003c/h2\u003e\u003ca id=\"user-content-cfd-diffusionadvecton-process\" class=\"anchor\" aria-label=\"Permalink: CFD diffusion/advecton process\" href=\"#cfd-diffusionadvecton-process\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eWe compile the inner loop of the diffusion-advection simulation. While the code\ncompiles, the performance is on par or slightly worse than the original NumPy.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eMandelbrot fractal\u003c/h2\u003e\u003ca id=\"user-content-mandelbrot-fractal\" class=\"anchor\" aria-label=\"Permalink: Mandelbrot fractal\" href=\"#mandelbrot-fractal\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eResults strongly depend on an implementation: a straighforward NumPy implementation\nuses complex-valued arrays which are not supported by triton.\nWorking around this and several other dynamo issues, leads to speedups of about x3 to x5.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe implementation based on the \u003ca href=\"https://shashankprasanna.com/benchmarking-modular-mojo-and-pytorch-torch.compile-on-mandelbrot-function/index.html#benchmarking-pytorch-cpu-with-torchcompile\" rel=\"nofollow\"\u003eMojo benchmark\u003c/a\u003e allows to compile the inner loop. The performance\nincrease relative to numpy is substantial and strongly data size and machine\ndependent: x8 for smaller inputs and up to x50 for unputs larger than the cache size of the machine.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eK-means clustering\u003c/h2\u003e\u003ca id=\"user-content-k-means-clustering\" class=\"anchor\" aria-label=\"Permalink: K-means clustering\" href=\"#k-means-clustering\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe internal loop of the k-means algorithm compiles into a straighforward\nC++ loop and offers up to x30 speedups versus NumPy.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eEager mode\u003c/h1\u003e\u003ca id=\"user-content-eager-mode\" class=\"anchor\" aria-label=\"Permalink: Eager mode\" href=\"#eager-mode\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIn short, the main changes to examples are:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eWith random number generators, our \u003ccode\u003erandom\u003c/code\u003e module is a drop-in replacement to NumPy's, but exact streams of random variates is different. Therefore, to preserve exact bit-to-bit identity, one needs to use NumPy's \u003ccode\u003erandom\u003c/code\u003e numbers.\u003c/li\u003e\n\u003cli\u003eInteraction with matplotlib: for plotting, we need to convert our wrapper ndarrays to PyTorch tensors or original NumPy arrays. In practice, this will be done automatically by TorchDynamo.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp dir=\"auto\"\u003eWe checked that our examples run on both CPU and GPU by setting the PyTorch global state\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"torch.set_default_device(\u0026quot;cuda\u0026quot;) # or \u0026quot;cpu\u0026quot;\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003etorch.set_default_device(\"cuda\") # or \"cpu\"\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eMore specifics for the examples we run:\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eA toy NN from scratch using numpy\u003c/h2\u003e\u003ca id=\"user-content-a-toy-nn-from-scratch-using-numpy\" class=\"anchor\" aria-label=\"Permalink: A toy NN from scratch using numpy\" href=\"#a-toy-nn-from-scratch-using-numpy\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOrigin: \u003ca href=\"https://www.geeksforgeeks.org/implementation-of-neural-network-from-scratch-using-numpy/\" rel=\"nofollow\"\u003ehttps://www.geeksforgeeks.org/implementation-of-neural-network-from-scratch-using-numpy/\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSource: \u003ccode\u003ee2e/nn_from_scratch\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTweaks\u003c/h3\u003e\u003ca id=\"user-content-tweaks\" class=\"anchor\" aria-label=\"Permalink: Tweaks\" href=\"#tweaks\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eUse the original numpy random stream in both cases to initialize the NN weights\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eResults\u003c/h3\u003e\u003ca id=\"user-content-results\" class=\"anchor\" aria-label=\"Permalink: Results\" href=\"#results\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eResults with numpy and torch_np are identical modulo different scalar vs 0D array reprs:\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"`epochs: 100 ======== acc: 98.64865709132037` with NumPy vs \n`epochs: 100 ======== acc: array_w(98.6487, dtype=float64)` with torch_np\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003e`epochs: 100 ======== acc: 98.64865709132037` with NumPy vs \n`epochs: 100 ======== acc: array_w(98.6487, dtype=float64)` with torch_np\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eBuild a maze and find a path in it\u003c/h2\u003e\u003ca id=\"user-content-build-a-maze-and-find-a-path-in-it\" class=\"anchor\" aria-label=\"Permalink: Build a maze and find a path in it\" href=\"#build-a-maze-and-find-a-path-in-it\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOrigin: N. Rougier, From python to numpy,\n\u003ca href=\"https://github.com/rougier/from-python-to-numpy/blob/master/code/maze_numpy.py\"\u003ehttps://github.com/rougier/from-python-to-numpy/blob/master/code/maze_numpy.py\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSource: \u003ccode\u003ee2e/maze\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTweaks\u003c/h3\u003e\u003ca id=\"user-content-tweaks-1\" class=\"anchor\" aria-label=\"Permalink: Tweaks\" href=\"#tweaks-1\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSeed the numpy random generator, use the same random stream.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFor plotting with matplotlib, convert torch_numpy arrays to numpy via\n\u003ccode\u003eZ = Z.tensor.numpy()\u003c/code\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eDiffusion/advection\u003c/h2\u003e\u003ca id=\"user-content-diffusionadvection\" class=\"anchor\" aria-label=\"Permalink: Diffusion/advection\" href=\"#diffusionadvection\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOrigin: N. Rougier, From python to numpy,\n\u003ca href=\"https://github.com/rougier/from-python-to-numpy/blob/master/code/%7Bsmoke_solver,smoke_1,smoke_2%7D.py\"\u003ehttps://github.com/rougier/from-python-to-numpy/blob/master/code/{smoke_solver,smoke_1,smoke_2}.py\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSource: \u003ccode\u003ee2e/smoke\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTweaks\u003c/h3\u003e\u003ca id=\"user-content-tweaks-2\" class=\"anchor\" aria-label=\"Permalink: Tweaks\" href=\"#tweaks-2\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003efix a bug in bool array minus bool array (fails on numpy 1.24)\u003c/li\u003e\n\u003cli\u003einline np.fromfunction into a call to np.indices\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eMandelbrot fractal\u003c/h2\u003e\u003ca id=\"user-content-mandelbrot-fractal-1\" class=\"anchor\" aria-label=\"Permalink: Mandelbrot fractal\" href=\"#mandelbrot-fractal-1\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOrigin: N. Rougier, From python to numpy,\n\u003ca href=\"https://github.com/rougier/from-python-to-numpy/blob/master/code/mandelbrot.py\"\u003ehttps://github.com/rougier/from-python-to-numpy/blob/master/code/mandelbrot.py\u003c/a\u003e\n\u003ca href=\"https://github.com/rougier/from-python-to-numpy/blob/master/code/mandelbrot_numpy_1.py\"\u003ehttps://github.com/rougier/from-python-to-numpy/blob/master/code/mandelbrot_numpy_1.py\u003c/a\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003esource: \u003ccode\u003ee2e/mandelbrot.py\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTweaks\u003c/h3\u003e\u003ca id=\"user-content-tweaks-3\" class=\"anchor\" aria-label=\"Permalink: Tweaks\" href=\"#tweaks-3\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003euse \u003ccode\u003emandelbrot_numpy_1.py\u003c/code\u003e version (slightly slower, but no mgrid)\u003c/li\u003e\n\u003cli\u003ecomplex abs in float32 runs into \u003ca class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1675363658\" data-permission-text=\"Title is private\" data-url=\"https://github.com/pytorch/pytorch/issues/99550\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/pytorch/pytorch/pull/99550/hovercard\" href=\"https://github.com/pytorch/pytorch/pull/99550\"\u003epytorch/pytorch#99550\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":null,"showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":{"timed_out":false,"not_analyzed":false,"symbols":[{"name":"JIT compiled mode","kind":"section_1","ident_start":667,"ident_end":684,"extent_start":665,"extent_end":2057,"fully_qualified_name":"JIT compiled mode","ident_utf16":{"start":{"line_number":19,"utf16_col":2},"end":{"line_number":19,"utf16_col":19}},"extent_utf16":{"start":{"line_number":19,"utf16_col":0},"end":{"line_number":53,"utf16_col":0}}},{"name":"Maze path-finding","kind":"section_2","ident_start":874,"ident_end":891,"extent_start":871,"extent_end":1012,"fully_qualified_name":"Maze path-finding","ident_utf16":{"start":{"line_number":25,"utf16_col":3},"end":{"line_number":25,"utf16_col":20}},"extent_utf16":{"start":{"line_number":25,"utf16_col":0},"end":{"line_number":31,"utf16_col":0}}},{"name":"CFD diffusion/advecton process","kind":"section_2","ident_start":1015,"ident_end":1045,"extent_start":1012,"extent_end":1207,"fully_qualified_name":"CFD diffusion/advecton process","ident_utf16":{"start":{"line_number":31,"utf16_col":3},"end":{"line_number":31,"utf16_col":33}},"extent_utf16":{"start":{"line_number":31,"utf16_col":0},"end":{"line_number":36,"utf16_col":0}}},{"name":"Mandelbrot fractal","kind":"section_2","ident_start":1210,"ident_end":1228,"extent_start":1207,"extent_end":1905,"fully_qualified_name":"Mandelbrot fractal","ident_utf16":{"start":{"line_number":36,"utf16_col":3},"end":{"line_number":36,"utf16_col":21}},"extent_utf16":{"start":{"line_number":36,"utf16_col":0},"end":{"line_number":47,"utf16_col":0}}},{"name":"K-means clustering","kind":"section_2","ident_start":1908,"ident_end":1926,"extent_start":1905,"extent_end":2057,"fully_qualified_name":"K-means clustering","ident_utf16":{"start":{"line_number":47,"utf16_col":3},"end":{"line_number":47,"utf16_col":21}},"extent_utf16":{"start":{"line_number":47,"utf16_col":0},"end":{"line_number":53,"utf16_col":0}}},{"name":"Eager mode","kind":"section_1","ident_start":2059,"ident_end":2069,"extent_start":2057,"extent_end":4356,"fully_qualified_name":"Eager mode","ident_utf16":{"start":{"line_number":53,"utf16_col":2},"end":{"line_number":53,"utf16_col":12}},"extent_utf16":{"start":{"line_number":53,"utf16_col":0},"end":{"line_number":135,"utf16_col":0}}},{"name":"A toy NN from scratch using numpy","kind":"section_2","ident_start":2734,"ident_end":2767,"extent_start":2731,"extent_end":3252,"fully_qualified_name":"A toy NN from scratch using numpy","ident_utf16":{"start":{"line_number":70,"utf16_col":3},"end":{"line_number":70,"utf16_col":36}},"extent_utf16":{"start":{"line_number":70,"utf16_col":0},"end":{"line_number":91,"utf16_col":0}}},{"name":"Tweaks","kind":"section_3","ident_start":2904,"ident_end":2910,"extent_start":2900,"extent_end":2998,"fully_qualified_name":"Tweaks","ident_utf16":{"start":{"line_number":77,"utf16_col":4},"end":{"line_number":77,"utf16_col":10}},"extent_utf16":{"start":{"line_number":77,"utf16_col":0},"end":{"line_number":82,"utf16_col":0}}},{"name":"Results","kind":"section_3","ident_start":3002,"ident_end":3009,"extent_start":2998,"extent_end":3252,"fully_qualified_name":"Results","ident_utf16":{"start":{"line_number":82,"utf16_col":4},"end":{"line_number":82,"utf16_col":11}},"extent_utf16":{"start":{"line_number":82,"utf16_col":0},"end":{"line_number":91,"utf16_col":0}}},{"name":"Build a maze and find a path in it","kind":"section_2","ident_start":3255,"ident_end":3289,"extent_start":3252,"extent_end":3607,"fully_qualified_name":"Build a maze and find a path in it","ident_utf16":{"start":{"line_number":91,"utf16_col":3},"end":{"line_number":91,"utf16_col":37}},"extent_utf16":{"start":{"line_number":91,"utf16_col":0},"end":{"line_number":109,"utf16_col":0}}},{"name":"Tweaks","kind":"section_3","ident_start":3439,"ident_end":3445,"extent_start":3435,"extent_end":3607,"fully_qualified_name":"Tweaks","ident_utf16":{"start":{"line_number":99,"utf16_col":4},"end":{"line_number":99,"utf16_col":10}},"extent_utf16":{"start":{"line_number":99,"utf16_col":0},"end":{"line_number":109,"utf16_col":0}}},{"name":"Diffusion/advection","kind":"section_2","ident_start":3610,"ident_end":3629,"extent_start":3607,"extent_end":3926,"fully_qualified_name":"Diffusion/advection","ident_utf16":{"start":{"line_number":109,"utf16_col":3},"end":{"line_number":109,"utf16_col":22}},"extent_utf16":{"start":{"line_number":109,"utf16_col":0},"end":{"line_number":122,"utf16_col":0}}},{"name":"Tweaks","kind":"section_3","ident_start":3798,"ident_end":3804,"extent_start":3794,"extent_end":3926,"fully_qualified_name":"Tweaks","ident_utf16":{"start":{"line_number":116,"utf16_col":4},"end":{"line_number":116,"utf16_col":10}},"extent_utf16":{"start":{"line_number":116,"utf16_col":0},"end":{"line_number":122,"utf16_col":0}}},{"name":"Mandelbrot fractal","kind":"section_2","ident_start":3929,"ident_end":3947,"extent_start":3926,"extent_end":4356,"fully_qualified_name":"Mandelbrot fractal","ident_utf16":{"start":{"line_number":122,"utf16_col":3},"end":{"line_number":122,"utf16_col":21}},"extent_utf16":{"start":{"line_number":122,"utf16_col":0},"end":{"line_number":135,"utf16_col":0}}},{"name":"Tweaks","kind":"section_3","ident_start":4192,"ident_end":4198,"extent_start":4188,"extent_end":4356,"fully_qualified_name":"Tweaks","ident_utf16":{"start":{"line_number":130,"utf16_col":4},"end":{"line_number":130,"utf16_col":10}},"extent_utf16":{"start":{"line_number":130,"utf16_col":0},"end":{"line_number":135,"utf16_col":0}}}]}},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/Quansight-Labs/numpy_pytorch_interop/branches":{"post":"5wsALpiRUHFNdXxQ_DrVuutVMCKMdwZ9wQ1Jn8dFzAy_NTsNNhC02JRuNTTomNblouGbG34T87EQzPh8vjEyFQ"},"/repos/preferences":{"post":"S1kj6FuyXClxQWceNlDxyLfHJ0lAwlUSTXGHxZQg_MoxfQ-iVCKOaq7BlNMBY6rsvhSEqk-qZ7fcYU6ycnUdNQ"}}},"title":"numpy_pytorch_interop/Readme.md at mandelbrot_compile · Quansight-Labs/numpy_pytorch_interop","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-7d7eb7c71814.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-1ae9fa256942.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true,"github_models_repo_integration":false}}}
0