{"id":15626118,"url":"https://github.com/mrdbourke/m1-machine-learning-test","last_synced_at":"2025-04-04T15:08:11.921Z","repository":{"id":38354909,"uuid":"427852909","full_name":"mrdbourke/m1-machine-learning-test","owner":"mrdbourke","description":"Code for testing various M1 Chip benchmarks with TensorFlow.","archived":false,"fork":false,"pushed_at":"2024-04-04T12:28:22.000Z","size":546,"stargazers_count":532,"open_issues_count":9,"forks_count":153,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-03-28T14:05:41.160Z","etag":null,"topics":["machine-learning","metal","tensorflow","tensorflow-macos"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrdbourke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-14T06:15:05.000Z","updated_at":"2025-03-19T11:58:56.000Z","dependencies_parsed_at":"2024-10-22T09:26:25.430Z","dependency_job_id":null,"html_url":"https://github.com/mrdbourke/m1-machine-learning-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdbourke%2Fm1-machine-learning-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdbourke%2Fm1-machine-learning-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdbourke%2Fm1-machine-learning-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrdbourke%2Fm1-machine-learning-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrdbourke","download_url":"https://codeload.github.com/mrdbourke/m1-machine-learning-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198445,"owners_count":20900079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["machine-learning","metal","tensorflow","tensorflow-macos"],"created_at":"2024-10-03T10:10:44.733Z","updated_at":"2025-04-04T15:08:11.892Z","avatar_url":"https://github.com/mrdbourke.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# M1, M1 Pro, M1 Max Machine Learning Speed Test Comparison\n\nThis repo contains some sample code to benchmark the new M1 MacBooks (M1 Pro and M1 Max) against various other pieces of hardware.\n\nIt also has steps below to setup your M1, M1 Pro, M1 Max, M1 Ultra or M2 Mac to run the code.\n\n## Who is this repo for?\n\n**You:** have a new M1, M1 Pro, M1 Max, M1 Ultra or M2 Mac and would like to get started doing machine learning and data science on it.\n\n**This repo:** teaches you how to install the most common machine learning and data science packages (software) on your machine and make sure they run using sample code.\n\n## Machine Learning Experiments Conducted\n\nAll experiments were run with the same code. For Apple devices, TensorFlow environments were created with the steps below.\n\n| Notebook Number | Experiment |\n| ----- | ----- |\n| [00](https://github.com/mrdbourke/m1-machine-learning-test/blob/main/00_cifar10_tinyvgg_benchmark.ipynb) | TinyVGG model trained on CIFAR10 dataset with TensorFlow code. |\n| [01](https://github.com/mrdbourke/m1-machine-learning-test/blob/main/01_food101_effnet_benchmark.ipynb) | EfficientNetB0 Feature Extractor on Food101 dataset with TensorFlow code.\n| [02](https://github.com/mrdbourke/m1-machine-learning-test/blob/main/02_random_forest_benchmark.ipynb) | `RandomForestClassifier` from Scikit-Learn trained with random search cross-validation on California Housing dataset. |\n\n## Results\n\nSee the [results directory](https://github.com/mrdbourke/m1-machine-learning-test/tree/main/results).\n\n## Steps (how to test your Apple Silicon machine)\n1. Create an environment and install dependencies ([see below](https://github.com/mrdbourke/m1-machine-learning-test#how-to-setup-a-tensorflow-environment-on-m1-m1-pro-m1-max-using-miniforge-shorter-version))\n2. Clone this repo\n3. Run various notebooks (results come at the end of the notebooks)\n\n## How to setup a TensorFlow environment on M1, M1 Pro, M1 Max, M1 Ultra, M2 using Miniforge (shorter version)\n\nIf you're experienced with making environments and using the command line, follow this version. If not, see the longer version below. \n\n1. Download and install Homebrew from https://brew.sh. Follow the steps it prompts you to go through after installation.\n2. [Download Miniforge3](https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh) (Conda installer) for macOS arm64 chips (M1, M1 Pro, M1 Max).\n3. Install Miniforge3 into home directory.\n```bash\nchmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh\nsh ~/Downloads/Miniforge3-MacOSX-arm64.sh\nsource ~/miniforge3/bin/activate\n```\n4. Restart terminal.\n5. Create a directory to setup TensorFlow environment.\n```bash\nmkdir tensorflow-test\ncd tensorflow-test\n```\n6. Make and activate Conda environment. **Note:** Python 3.8 is the most stable for using the following setup.\n```bash\nconda create --prefix ./env python=3.8\nconda activate ./env\n```\n7. Install TensorFlow dependencies from Apple Conda channel.\n```bash\nconda install -c apple tensorflow-deps\n```\n8. Install base TensorFlow (Apple's fork of TensorFlow is called `tensorflow-macos`).\n```bash\npython -m pip install tensorflow-macos\n```\n9. Install Apple's `tensorflow-metal` to leverage Apple Metal (Apple's GPU framework) for M1, M1 Pro, M1 Max GPU acceleration.\n```bash\npython -m pip install tensorflow-metal\n```\n10. (Optional) Install TensorFlow Datasets to run benchmarks included in this repo.\n```bash\npython -m pip install tensorflow-datasets\n```\n11. Install common data science packages.\n```bash\nconda install jupyter pandas numpy matplotlib scikit-learn\n```\n12. Start Jupyter Notebook.\n```bash\njupyter notebook\n```\n13. Import dependencies and check TensorFlow version/GPU access.\n```python\nimport numpy as np\nimport pandas as pd\nimport sklearn\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\n\n# Check for TensorFlow GPU access\nprint(f\"TensorFlow has access to the following devices:\\n{tf.config.list_physical_devices()}\")\n\n# See TensorFlow version\nprint(f\"TensorFlow version: {tf.__version__}\")\n```\n\nIf it all worked, you should see something like: \n\n```bash\nTensorFlow has access to the following devices:\n[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),\nPhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\nTensorFlow version: 2.8.0\n```\n\n## How to setup a TensorFlow environment on M1, M1 Pro, M1 Max, M1 Ultra, M2 using Miniforge (longer version)\n\nIf you're new to creating environments, using a new M1, M1 Pro, M1 Max machine and would like to get started running TensorFlow and other data science libraries, follow the below steps.\n\n\u003e **Note:** You're going to see the term \"package manager\" a lot below. Think of it like this: a **package manager** is a piece of software that helps you install other pieces (packages) of software.\n\n### Installing package managers (Homebrew and Miniforge)\n\n1. Download and install Homebrew from https://brew.sh. Homebrew is a package manager that sets up a lot of useful things on your machine, including Command Line Tools for Xcode, you'll need this to run things like `git`. The command to install Homebrew will look something like:\n\n```bash\n/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\n```\n\nIt will explain what it's doing and what you need to do as you go.\n\n2. [Download the most compatible version of Miniforge](https://github.com/conda-forge/miniforge#download) (minimal installer for Conda specific to conda-forge, Conda is another package manager and conda-forge is a Conda channel) from GitHub.\n\nIf you're using an M1 variant Mac, it's \"[Miniforge3-MacOSX-arm64](https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh)\" \u003c- click for direct download. \n\nClicking the link above will download a shell file called `Miniforge3-MacOSX-arm64.sh` to your `Downloads` folder (unless otherwise specified). \n\n3. Open Terminal.\n\n4. We've now got a shell file capable of installing Miniforge, but to do so we'll have to modify it's permissions to [make it executable](https://askubuntu.com/tags/chmod/info).\n\nTo do so, we'll run the command `chmod -x FILE_NAME` which stands for \"change mode of FILE_NAME to -executable\".\n\nWe'll then execute (run) the program using `sh`.\n\n```bash\nchmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh\nsh ~/Downloads/Miniforge3-MacOSX-arm64.sh\n```\n\n5. This should install Miniforge3 into your home directory (`~/` stands for \"Home\" on Mac).\n\nTo check this, we can try to activate the `(base)` environment, we can do so using the `source` command.\n\n```bash\nsource ~/miniforge3/bin/activate\n```\n\nIf it worked, you should see something like the following in your terminal window.\n\n```bash\n(base) daniel@Daniels-MBP ~ %\n```\n\n6. We've just installed some new software and for it to fully work, we'll need to **restart terminal**. \n\n### Creating a TensorFlow environment\n\nNow we've got the package managers we need, it's time to install TensorFlow.\n\nLet's setup a folder called `tensorflow-test` (you can call this anything you want) and install everything in there to make sure it's working.\n\n\u003e **Note:** An **environment** is like a virtual room on your computer. For example, you use the kitchen in your house for cooking because it's got all the tools you need. It would be strange to have an oven in your bedroom. The same thing on your computer. If you're going to be working on specific software, you'll want it all in one place and not scattered everywhere else. \n\n7. Make a directory called `tensorflow-test`. This is the directory we're going to be storing our environment. And inside the environment will be the software tools we need to run TensorFlow.\n\nWe can do so with the `mkdir` command which stands for \"make directory\".\n\n```bash\nmkdir tensorflow-test\n```\n\n8. Change into `tensorflow-test`. For the rest of the commands we'll be running them inside the directory `tensorflow-test` so we need to change into it.\n\nWe can do this with the `cd` command which stands for \"change directory\".\n\n```bash\ncd tensorflow-test\n```\n\n9. Now we're inside the `tensorflow-test` directory, let's create a new Conda environment using the `conda` command (this command was installed when we installed Miniforge above).\n\nWe do so using `conda create --prefix ./env` which stands for \"conda create an environment with the name `file/path/to/this/folder/env`\". The `.` stands for \"everything before\".\n\nFor example, if I didn't use the `./env`, my filepath looks like: `/Users/daniel/tensorflow-test/env`\n\n```bash\nconda create --prefix ./env\n```\n\n10. Activate the environment. If `conda` created the environment correctly, you should be able to activate it using `conda activate path/to/environment`.\n\nShort version: \n\n```bash\nconda activate ./env\n```\n\nLong version:\n\n```bash\nconda activate /Users/daniel/tensorflow-test/env\n```\n\n\u003e **Note:** It's important to activate your environment every time you'd like to work on projects that use the software you install into that environment. For example, you might have one environment for every different project you work on. And all of the different tools for that specific project are stored in its specific environment.\n\nIf activating your environment went correctly, your terminal window prompt should look something like: \n\n```bash\n(/Users/daniel/tensorflow-test/env) daniel@Daniels-MBP tensorflow-test %\n```\n\n11. Now we've got a Conda environment setup, it's time to install the software we need.\n\nLet's start by installing various TensorFlow dependencies (TensorFlow is a large piece of software and *depends* on many other pieces of software).\n\nRather than list these all out, Apple have setup a quick command so you can install almost everything TensorFlow needs in one line.\n\n```bash\nconda install -c apple tensorflow-deps\n```\n\nThe above stands for \"hey conda install all of the TensorFlow dependencies from the Apple Conda channel\" (`-c` stands for channel).\n\nIf it worked, you should see a bunch of stuff being downloaded and installed for you. \n\n12. Now all of the TensorFlow dependencies have been installed, it's time install base TensorFlow.\n\nApple have created a fork (copy) of TensorFlow specifically for Apple Macs. It has all the features of TensorFlow with some extra functionality to make it work on Apple hardware.\n\nThis Apple fork of TensorFlow is called `tensorflow-macos` and is the version we'll be installing:\n\n```bash\npython -m pip install tensorflow-macos\n```\n\nDepending on your internet connection the above may take a few minutes since TensorFlow is quite a large piece of software.\n\n13. Now we've got base TensorFlow installed, it's time to install `tensorflow-metal`.\n\nWhy?\n\nMachine learning models often benefit from GPU acceleration. And the M1, M1 Pro and M1 Max chips have quite powerful GPUs.\n\nTensorFlow allows for automatic GPU acceleration if the right software is installed.\n\nAnd Metal is Apple's framework for GPU computing.\n\nSo Apple have created a plugin for TensorFlow (also referred to as a TensorFlow PluggableDevice) called `tensorflow-metal` to run TensorFlow on Mac GPUs.\n\nWe can install it using:\n\n```bash\npython -m pip install tensorflow-metal\n```\n\nIf the above works, we should now be able to leverage our Mac's GPU cores to speed up model training with TensorFlow.\n\n14. (Optional) Install TensorFlow Datasets. Doing the above is enough to run TensorFlow on your machine. But if you'd like to run the benchmarks included in this repo, you'll need TensorFlow Datasets.\n\nTensorFlow Datasets provides a collection of common machine learning datasets to test out various machine learning code.\n\n```bash\npython -m pip install tensorflow-datasets\n```\n\n15. Install common data science packages. If you'd like to run the benchmarks above or work on other various data science and machine learning projects, you're likely going to need Jupyter Notebooks, pandas for data manipulation, NumPy for numeric computing, matplotlib for plotting and Scikit-Learn for traditional machine learning algorithms and processing functions.\n\nTo install those in the current environment run:\n\n```bash\nconda install jupyter pandas numpy matplotlib scikit-learn\n```\n\n16. Test it out. To see if everything worked, try starting a Jupyter Notebook and importing the installed packages.\n\n```bash\n# Start a Jupyter notebook\njupyter notebook\n```\n\nOnce the notebook is started, in the first cell:\n\n```python\nimport numpy as np\nimport pandas as pd\nimport sklearn\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\n\n# Check for TensorFlow GPU access\nprint(tf.config.list_physical_devices())\n\n# See TensorFlow version\nprint(tf.__version__)\n```\n\nIf it all worked, you should see something like:\n\n```bash\nTensorFlow has access to the following devices:\n[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),\nPhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\nTensorFlow version: 2.5.0\n```\n\n17. To see if it really worked, try running one of the notebooks above end to end!\n\nAnd then compare your results to the benchmarks above.\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdbourke%2Fm1-machine-learning-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrdbourke%2Fm1-machine-learning-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrdbourke%2Fm1-machine-learning-test/lists"}