pystan2 icon indicating copy to clipboard operation
pystan2 copied to clipboard

Minimizing the post-install package size for AWS Lambda (Serverless)

Open CarstVaartjes opened this issue 9 years ago • 10 comments

Summary:

For AWS Lambda, I want to make serverless functions using pystan but the packaging size exceeds the maximum for Lambda.

Description:

AWS Lambda has a total maximum of 50mb zipped / 250mb unzipped for a virtual environment to run functions in it. There are examples on the internet on how to remove all unused code (non-compiled c/cython etc) to shrink a virtual environment but I'm trying to see how to apply this to pystan.

Scikit-learn example: https://github.com/ryansb/sklearn-build-lambda/blob/master/build.sh Lambda limits: http://docs.aws.amazon.com/lambda/latest/dg/limits.html

So pystan has boost (180mb) and the source that seem to take up most of the space in the site-packages. What can and can't I delete in a smart way to create a package that is as small as possible.

I will share the code like the scikit-learn example but I need to get it running first.

Expected Output:

Could you perhaps point me in a direction as to how I could minimize the pystan site-package size post-install? What can I delete, what not? Any directional pointing would be really great!

PyStan Version:

2.17

Python Version:

2.7

Operating System:

Docker with amazon_linux:2017.03

CarstVaartjes avatar Nov 17 '17 12:11 CarstVaartjes

Are you planning on doing Stan coding on amazon (interactively) or run the compiled model (e.g. pickle the model)?

After the model is compiled, it should work without any source files. (I have not tested this)

ahartikainen avatar Nov 17 '17 15:11 ahartikainen

Hi,

sorry, I completely missed your response! So we basically have a situation where we use fb-prophet to generate models on the fly; we have built an optimizer on top of fb-prophet, so it goes through a few hundred possible scenarios normally until it finds its optimum. We do this for over 100k combinations, each of which we forecast separately, so the ability to run in parallel is quite important and we do not do this all the time but sometimes as a resource usage spike (which is why serverless is so attractive in this scenario) So I was looking at ways to shrink the size of the used libraries by only keeping compiled code, and no more source & tests. But i'm not sure how fb-prophet uses stan and how this would work directly.

BR Carst

CarstVaartjes avatar Dec 05 '17 10:12 CarstVaartjes

My guess is that fb-prophet will cache compiled models so removing source files from PyStan should not be a problem (at least after the first run).

In this case you could manually edit the fb-prophet codebase to read specific models (load from the pickle, add compression if needed).

ahartikainen avatar Dec 05 '17 13:12 ahartikainen

I will try some stuff this weekend and will let you know!

CarstVaartjes avatar Dec 13 '17 02:12 CarstVaartjes

@CarstVaartjes facing the same problem with prophet & lambda. Have you already found a solution? Cheers Patrick

patrick-michelberger avatar Apr 07 '18 23:04 patrick-michelberger

Hi!

Well almost, I need to test some last things! @ahartikainen was right that after the initial model creation by fb-prophet (which makes a normal + a logistics variant), less is needed. So I based some ideas om https://github.com/ryansb/sklearn-build-lambda/blob/master/build.sh Pip install everything (which does the model creation), then I remove things such as boost, strip all .so files, test files and all .py files (after compiling .pyc) And that seems to work, but I need to comprehensively test it

CarstVaartjes avatar Apr 08 '18 11:04 CarstVaartjes

See https://gist.github.com/CarstVaartjes/77dbe8249d171e592bd17847f7009272

CarstVaartjes avatar Apr 08 '18 19:04 CarstVaartjes

Hi, were you able to test this?

Everything is fixed?

ahartikainen avatar Jul 18 '18 12:07 ahartikainen

I still have to get it to lambda, but in general it works locally (also the latest fb-prophet is smaller as it makes a single compiled model now) Thanks for the help & ideas!!

CarstVaartjes avatar Jul 19 '18 19:07 CarstVaartjes

Just in case people are looking here for how to get the code running on lambda after reducing the size, here's my experience: https://medium.com/@amacenov/how-to-get-fbprophet-work-on-aws-lambda-c3a33a081aaf @CarstVaartjes - thanks a lot for taking the crack on it!

matse004 avatar Sep 15 '18 12:09 matse004