How to run notebooks for NLP Specialization C4W1 on Apple Silicon

It was difficult to create a conda environment on Apple Silicon that would run the notebooks provided in the NLP Specialization (Course 4, NLP with Attention Models, link), but I eventually succeeded, so I decided to post the procedure here for others to use in case they want to run their notebooks locally instead of on the Coursera platform. Several of the notebooks use the tensorflow_text module, and it is difficult to use with other modules needed on Apple Silicon. The working configuration included Python 3.9, as newer versions of Python did not work. Here is a summary of the procedure to create an environment called nlp_env. Note that to run the notebooks locally, you must download the utility functions that run in the notebook’s adjacent directories on the Coursera platform. Coursera provides instructions on how to download these files in a single zip file, but how to do that is not covered here.

Setting Up a Conda Environment for NLP on Apple Silicon (M1, M2, M3)

Prerequisites

  • Ensure you have Conda installed. If not, install it from Miniforge.

Steps to Set Up the Environment

  1. Download the pre-compiled tensorflow-text wheel:

    • Create a directory to store the wheel. (You can put the binaries, elsewhere, but you’ll have to reference the file paths consistently):
      mkdir -p ~/Documents/conda/myEnvs/tf-binaries
      cd ~/Documents/conda/myEnvs/tf-binaries
      
    • Download the wheel:
      wget https://github.com/sun1638650145/Libraries-and-Extensions-for-TensorFlow-for-Apple-Silicon/releases/download/v2.8/tensorflow_text-2.8.2-cp39-cp39-macosx_11_0_arm64.whl
      
  2. Create a requirements.txt file:

    • Create the file in the same directory where tf-binaries is located (e.g., ~/Documents/conda/myEnvs):
      trax==1.3.*
      
      # tensorflow-text==2.8.* from pre-compiled wheel
      ./tf-binaries/tensorflow_text-2.8.2-cp39-cp39-macosx_11_0_arm64.whl
      
      # Specify compatible versions of jax and jaxlib
      jax==0.2.25
      -f https://storage.googleapis.com/jax-releases/jax_releases.html
      jaxlib==0.1.75
      
      # seqio is required to prevent the following error when importing trax:
      # > ImportError: cannot import name 'multihost_utils' from 'jax.experimental'
      seqio<=0.0.16
      
      # use nltk<3.6, because nltk >= 3.6 has tokenizers with different behaviors
      nltk<3.6
      
      # needed to run jupyter notebook
      ipykernel
      
  3. Create and activate the new conda environment:

    conda create --name nlp_env python=3.9
    conda activate nlp_env
    
    

Next, install the requirements. Ensure you are in the directory where requirements.txt is located (and in the active environment):

cd ~/Documents/conda/myEnvs
pip install -r requirements.txt

Now, still in the active environment, install the packages you want, such as these. The scipy version here definitely works with the packages used above.

pip install numpy pandas scipy==1.7.3 scikit-learn seaborn pathlib jupyter jupyterlab

Next step (optional): register the kernel so you can see this new environment (nlp_env) in your notebook:

python -m ipykernel install --user --name=nlp_env --display-name "Python (nlp_env)"

Sometimes, a utility function in the notebook calls another one called dlai_grader that is not available locally, but you can get it using pip, so do this in the active environment:

pip install dlai_grader

Now you should be good to go! In your active environment, type this to launch your notebook:

jupyter notebook

(or)

jupyter lab

Good luck!

Update: I’ll add a few other useful conda commands people can use in Terminal to get running, as this sometimes requires a few attempts:

conda info --envs   # view existing conda environments and their paths
conda deactivate [env_name]             # leave environment before trying to remove it
conda remove -n [env_name] --all     # e.g., remove a faulty environment by name
conda remove -p /Users/[your_username]/miniforge3/envs/[env_name]  # remove by path

This should get you started…

3 Likes