I am deploying a deep learning model using Keras and Tensorflow into AWS Lambda. To get around Lambda's size restriction of 250MB uncompressed, I used this to reduce the package size to under the limit:
cd /home/ec2-user/build_dnn_nlp/env/lib64/python3.6/site-packages
find . -name "*.so" | xargs strip
find . -type f -name '*.pyc' -delete
find . -type d -name '__pycache__' -delete
Now, when I try to import keras, I see this error in the Lambda console:
module initialization error: [Errno 38] Function not implemented
I believe error 38 is related to multiprocessing, and because lambda instances do not have shared memory (no /dev/shm/), multiprocessing isn't possible.
To get around any multiprocessing in keras, I set tensorflow into single core mode before importing Keras, with print statements to help with debugging on Lambda. While 'set the CPU to 1 core' prints, it always stops at importing keras.
def orchestrate(text):
import tensorflow as tf
print('imported tf')
config = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=num_cores,
allow_soft_placement=True,
device_count={'CPU': 1})
session = tf.Session(config=config)
print('set cpu to 1 core')
from keras import backend as K
print('import keras backend')
K.set_session(session)
print('set session')
from keras.layers import Activation, Dense, Dropout
from keras.models import Sequential, load_model
from keras.preprocessing.text import Tokenizer
print('imported keras libs ')
model = define_model()
What about importing keras requires multiprocessing (if that is the problem)? If others have deployed keras on AWS Lambda, what did I do to break it?