<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mustafa Mujahid on Medium]]></title>
        <description><![CDATA[Stories by Mustafa Mujahid on Medium]]></description>
        <link>https://medium.com/@mustafamujahid01?source=rss-58d0ad12ea32------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IWA9op3JSyqvp_h1WgYZXg.jpeg</url>
            <title>Stories by Mustafa Mujahid on Medium</title>
            <link>https://medium.com/@mustafamujahid01?source=rss-58d0ad12ea32------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:06:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mustafamujahid01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Pytorch for Mac M1/M2 with GPU acceleration 2023. Jupyter and VS Code setup for PyTorch included.]]></title>
            <link>https://medium.com/@mustafamujahid01/pytorch-for-mac-m1-m2-with-gpu-acceleration-2023-jupyter-and-vs-code-setup-for-pytorch-included-100c0d0acfe2?source=rss-58d0ad12ea32------2</link>
            <guid isPermaLink="false">https://medium.com/p/100c0d0acfe2</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[mac]]></category>
            <dc:creator><![CDATA[Mustafa Mujahid]]></dc:creator>
            <pubDate>Sun, 06 Aug 2023 13:39:28 GMT</pubDate>
            <atom:updated>2023-08-06T13:55:41.721Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*CrrU0uH159aGD3bj3Of9gg.png" /><figcaption>Image Source: <a href="https://wandb.ai/capecape/pytorch-M1Pro/reports/PyTorch-Runs-On-the-GPU-of-Apple-M1-Macs-Now-Announcement-With-Code-Samples---VmlldzoyMDMyNzMz">https://wandb.ai/</a></figcaption></figure><h3><strong>Introduction</strong></h3><p>Among the numerous deep learning frameworks available, PyTorch stands tall as a powerful and versatile platform for building cutting-edge machine learning models. Its ability to leverage GPU acceleration has undoubtedly been a game-changer, unleashing the potential of modern hardware and drastically speeding up computation.</p><p>For Mac users wielding the mighty M1 or M2 chips, tapping into the full potential of PyTorch with GPU acceleration can be a transformative experience. These ARM-based Apple chips have set new benchmarks in performance and energy efficiency, making them a favourite among developers and AI enthusiasts alike.</p><p>In this comprehensive guide, we embark on an exciting journey to unravel the mysteries of installing PyTorch with GPU acceleration on Mac M1/M2 along with using it in Jupyter notebooks and VS Code.</p><p>Before we begin, make sure you have your seatbelts fastened, your Mac powered up, and your enthusiasm fueled. Let’s embark on this exhilarating adventure, where we’ll pave the way for unleashing the full potential of PyTorch with GPU acceleration on your Mac M1/M2 machine!</p><p><strong>Pytorch Metal Performance Shader(MPS)</strong></p><p>Every Apple silicon Mac has a unified memory architecture, providing the GPU with direct access to the full memory store. This makes Mac a great platform for machine learning, enabling users to train larger networks or batch sizes locally. This reduces costs associated with cloud-based development or the need for additional local GPUs. The Unified Memory architecture also reduces data retrieval latency, improving end-to-end performance.</p><p>MPS basically allows you to get access to Apple GPU while training which allows faster training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jBSM7n_pxAfEOFkffyKBMQ.gif" /><figcaption>Accelerated GPU training and evaluation speedups over CPU-only (times faster)</figcaption></figure><p>The above chart shows the comparison of training and evaluation between the CPU and GPU . Source: <a href="https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/">https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/</a></p><p>Note: It only works with Apple silicon macs and not with intel powered macs.</p><h3><strong>Implementation</strong></h3><p>Steps for installing PyTorch within conda environment</p><p>Step 1: Create a new conda environment</p><pre>conda create --name ENV_NAME python=3.9</pre><p>Step 2: Activate the conda enviroment</p><pre>conda activate ENV_NAME</pre><p>Step 3: Install PyTorch</p><p>Paste the below code in the terminal (make sure you’re in the env)</p><pre>conda install pytorch::pytorch torchvision torchaudio -c pytorch</pre><p>If you’re using pip instead of conda or you would like to use C++/Java as a language i encourage you to go to pytorch website and according to your preferences copy the generated command and paste into the terminal.</p><p><a href="https://pytorch.org/get-started/locally/">Get Started</a></p><p>Congratulations P<strong>yTorch</strong> is now installed!</p><blockquote>Running PyTorch in Jupyter Notebook</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*2gW1UjgLvVzikKn3i3J8zA.png" /></figure><p>Step 1: Install Jupyter Notebook</p><pre>conda install -c conda-forge notebook<br>conda install -c conda-forge nb_conda_kernels</pre><p>Step 2: Install Jupyter Lab</p><pre>conda install -c conda-forge jupyterlab<br>conda install -c conda-forge nb_conda_kernels</pre><p>Step 3: Start Jupyter Notebook</p><pre>jupyter notebook</pre><p>Jupyter notebook will be opened. Now create a new file or open an exisiting one and type the following command to check whether the pytorch is correctly installed and the version of it.</p><pre>import torch<br><br>print(torch.__version__)</pre><p>It should output something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*3g5a3p7ueOXWVzAFjdB8Bg.png" /></figure><p>So the 2.0.1 here states the version.</p><p>Check for the MPS(Metal Performance Shader) i.e Apple Metal GPU</p><pre># Is MPS even available? macOS 12.3+<br>print(torch.backends.mps.is_available())<br><br># Was the current version of PyTorch built with MPS activated?<br>print(torch.backends.mps.is_built())</pre><p>If the above code outputs “true” &amp; “true” then it’s time to celebrate because you now have access to Apple Metal GPU.</p><blockquote>Running PyTorch in VS Code</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*_MATIVwMR0-A5KlRiBnO1g.jpeg" /></figure><p>Step 1: Activate your conda environment and then type following command in the terminal(make sure you have vs code installed) this will open VS code .</p><pre>code . </pre><p>Now make a new file with .ipynb extension.</p><p>As soon as you create a new file you may need to select a kernel. I will be attaching photos which will guide you how to do so.</p><p>Step 2: Select kernel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tjIkT71yPPXxRPq8yK_w0A.png" /></figure><p>After selecting kernel chances are your conda environment will be displayed click the environment and you’re ready to go but if not follow the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d7x0mMkrbjJ-mn6ah6uHHQ.png" /></figure><p>Step 3: Click on select another kernel which will display two options</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7sA8BetR9I67gg-9fBdYaQ.png" /></figure><ol><li>Python Environments: Display all the python environment including virtual environments.</li><li>Existing jupyter server: By providing the URL of running jupyter notebook you can access the notebook in the vs code.</li></ol><p>Step 4: Select Python Environments. It would display list of environments</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21AH6a-Yu09FqaTuFcjyFw.png" /></figure><p>Choose your environment and tadaaa!!! All the libraries and frameworks will be loaded present in that specific environment in our case pytorch.</p><p>Now check for the PyTorch version.</p><pre>import torch<br><br>print(torch.__version)</pre><p>It may output pytorch version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0soBzHOpaEOLcI4vYZCwfA.png" /></figure><p>Check for the MPS(Metal Performance Shader) i.e Apple Metal GPU</p><pre># Is MPS even available? macOS 12.3+<br>print(torch.backends.mps.is_available())<br><br># Was the current version of PyTorch built with MPS activated?<br>print(torch.backends.mps.is_built())</pre><p>If the above code outputs “true” &amp; “true” then it’s time to celebrate because you now have access to Apple Metal GPU.</p><p><em>I hope you find this article helpful. Support my work by following me.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=100c0d0acfe2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>