Last active
April 9, 2020 13:49
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
I'm seeing the same thing. My zip is about 700 MB.
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 ?
@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.
ah amazing! thanks!
… Le 18 juin 2019 à 15:23, Shane ***@***.***> a écrit :
@simonguertin <https://github.com/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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <https://gist.github.com/854bcb3761e6cb8568574611b42f8c92?email_source=notifications&email_token=ABIVLN4QM3M53AQ3LV5DGD3P3EY4NA5CNFSM4HMEUIW2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFT4UA#gistcomment-2947392>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ABIVLN33OMCOVUYBMK73VEDP3EY4NANCNFSM4HMEUIWQ>.
my package is now 1.4 GB!!
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
Is this still working? I am not getting the same results (My package is >250MB)