Skip to content

Instantly share code, notes, and snippets.

@mjdietzx
Last active April 9, 2020 13:49
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mjdietzx/854bcb3761e6cb8568574611b42f8c92 to your computer and use it in GitHub Desktop.
Save mjdietzx/854bcb3761e6cb8568574611b42f8c92 to your computer and use it in GitHub Desktop.
AWS Lambda pytorch deep learning deployment package (building pytorch and numpy from source on EC2 Amazon Linux AMI)
#
# written for Amazon Linux AMI
# creates an AWS Lambda deployment package for pytorch deep learning models (Python 3.6.1)
# assumes lambda function defined in ~/main.py
# deployment package created at ~/waya-ai-lambda.zip
#
#
# install python 3.6.1
#
sudo yum update
sudo yum install -y gcc zlib zlib-devel openssl openssl-devel
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
tar -xzvf Python-3.6.1.tgz
cd Python-3.6.1 && ./configure && make
sudo make install
#
# setup a minimal virtual environment for our lambda function's dependencies
#
sudo /usr/local/bin/pip3 install virtualenv
/usr/local/bin/virtualenv ~/shrink_venv
source ~/shrink_venv/bin/activate
ls $VIRTUAL_ENV/lib/python3.6/site-packages
du -sh $VIRTUAL_ENV/lib/python3.6/site-packages
#
# it's fine to install smaller python modules with pip (note `boto3` comes pre-installed in AWS Lambda environment)
#
pip install Pillow
pip install cython # numpy dependency
pip install pyyaml # pytorch dependency
#
# install numpy and pytorch from source to reduce package size
#
sudo yum install git
cd
git clone --recursive https://github.com/numpy/numpy.git
cd numpy
git checkout 31465473c491829d636c9104c390062cba005681 # latest release
python setup.py install
#
# and pytorch...
#
cd
sudo yum install cmake make automake gcc gcc-c++ kernel-devel # pytorch build dependencies
git clone --recursive https://github.com/pytorch/pytorch.git
cd pytorch
git checkout af3964a8725236c78ce969b827fdeee1c5c54110
export NO_CUDA=1 # reduce package size (pointless b/c AWS Lambda does not have these capabilities anyways)
export NO_CUDNN=1
python setup.py install
pip install torchvision
#
# ensure the total size of our dependencies is under 250 MB (should be ~210 MB)
#
du -sh $VIRTUAL_ENV/lib/python3.6/site-packages
#
# create the deployment package
#
cd $VIRTUAL_ENV/lib/python3.6/site-packages
zip -r9 ~/waya-ai-lambda.zip *
cd
zip -g waya-ai-lambda.zip main.py
@DavidBegert
Copy link

Is this still working? I am not getting the same results (My package is >250MB)

@ShaneRyan1977
Copy link

I'm seeing the same thing. My zip is about 700 MB.

@simonguertin
Copy link

Hi, I am trying with EC2 AMI Amazon Linux AMI 2018.03.0 (HVM) and Amazon Linux AMI 2017.09.1 and I get the error
at the command python setup.py install:
gcc: error trying to exec 'cc1plus': execvp:

with EC2 image are you using ?

@ShaneRyan1977
Copy link

@simonguertin
I am running this script in a Sagemaker JupyterLab console
I ran it the first time by pasting each line into the console. A couple of the lines needed "-y" flags to force them to say "yes" to some questions.

@simonguertin
Copy link

simonguertin commented Jun 18, 2019 via email

@simonguertin
Copy link

my package is now 1.4 GB!!

@simonguertin
Copy link

I get 224M after installing pytorch as per this script, but after installing torch vision, I am up to 1.4 GB! Is there an older version of torch vision we can install ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment