Package: baklava
BaklavaThis is a package for building python based Machine Learning models into docker images, that can be deployed directly into AWS SageMaker.This is an extension to the standard python packaging utilitysetuptools. The
officialpython packaging guideexplains the basics of building python distributions in detail.This extends the existing behavior of building a setuptools source
distribution (sdist) by installing the built package artifact
(*.tar.gz) into a Docker image. After the python distribution has been
installed to the Docker image, it allows the user to configure the image
for the purposes of model training and prediction.The name was chosen becausebaklavaconsists of small pieces and layers, like we put technologies together in form of many layers to create Docker images.InstallationInstalldockerand then install the package:pip install baklavaFeaturesInstalling thebaklavapackage automatically registers
extensions tosetuptools. New features are added to build python
distributions into docker images.When installed, this package allows you to use two newsetuptools
commands(similar tosdistorbdist_wheel):train: Builds a training docker image for your package. A training
image (python setup.py train) executes a user-provided function
just once in order to produce a model artifact. This image conforms to the AWS
SageMaker training image API.predict: Builds a prediction docker image for your package. A prediction
image (python setup.py predict) hosts the user-provided function in a web
application to be able to produce many decisions over time using a RESTful
service conforming to the AWS SageMaker prediction API.execute: Builds a batch execution docker image for your package. A batch execution
image (python setup.py execute) executes a user-provided batch function for prediction
on large amount of records.Production-grade Machine Learning API using Flask, Gunicorn, Nginx, and DockerNewsetup keywordsare also registered with setuptools (similar toinstall_requiresorentry_points). These include:python_version: Specify the version of python to build the docker image fordockerlines: Add docker commands to your resultingDockerfileThis package also defines aPython APIto perform
the same actions as the setuptools extension.UsageTrainTo create a training image, your package must define a function that
takes no arguments and returns nothing. It can be named anything as long
as it is correctly referenced in thesetup.pyfile.defmy_training_function():"""A training function takes no arguments and returns no results"""passThesetup.pymust include abaklava.trainentrypoint which
points to this function. The entrypoint is the full module path to the
defined python function. An example of asetup.pyscript with a valid
training entrypoint would  look like the following:fromsetuptoolsimportsetup,find_packagessetup(name='example',version='0.0.1',packages=find_packages(),include_package_data=True,entry_points={'baklava.train':['my_entrypoint = example.main:my_training_function',],})With thissetup.py, a training docker image can be built:python setup.py trainSee theexamplesfor full sample projects.PredictTo create a prediction image, your package must define a function
that takes one argument and returns one value. It can be named anything
as long as it is correctly referenced in thesetup.pyfile.defmy_hosted_function(payload):"""A hosted function takes a dictionary input and returns a dictionaryoutput.Arguments:payload (dict[str, object]): This is the payload was sent tothe SageMaker server using a POST request to the`invocations` route.Returns:result (dict[str, object]): The output of the function isexpected to be either a dictionary (like the function input)or a JSON string."""return{}Thesetup.pymust include abaklava.predictentrypoint
which points to this function. The entrypoint is the full module path to
the defined python function. An example of asetup.pyscript with a
valid prediction entrypoint would  look like the following:fromsetuptoolsimportsetup,find_packagessetup(name='example',version='0.0.1',packages=find_packages(),include_package_data=True,entry_points={'baklava.predict':['my_entrypoint = example.main:my_hosted_function',]})With thissetup.py, a prediction docker image can be built:python setup.py predictSee theexamplesfor full sample projects.Predict InitializationThere are often cases when python code needs to execute prior to running
predictions. For example, it may take a long time to load a model
artifact into memory.To add a prediction initializer, your package must define a function
that takes no arguments and may return anything. It can be named
anything as long as it is correctly referenced in thesetup.pyfile.
The function is responsible for it's own caching, but it is recommended
to use caching function similar tofunctools.lru_cacheto save the
function results in memory.importfunctools@functools.lru_cache()defmy_init_function():"""An initialization function takes no arguments and may return aresult.Returns:data (object): Data necessary for prediction. Could be any type."""return1,2,3Thesetup.pymust include abaklava.initializeentrypoint
which points to this function. The entrypoint is the full module path to
the defined python function. An example of asetup.pyscript with a
valid prediction initialization entrypoint would  look like the
following:fromsetuptoolsimportsetup,find_packagessetup(name='example',version='0.0.1',packages=find_packages(),include_package_data=True,# Notice that we have an initializer AND a predict functionentry_points={'baklava.predict':['my_entrypoint = example.main:my_hosted_function',]'baklava.initialize':['my_initializer = example.main:my_init_function',]})With thissetup.py, a prediction docker image can be built that will
initialize using themy_init_functioninitializer:python setup.py predictSee theexamplesfor full sample projects.Multiple OptionsA package may include all of the previous entrypoints in a single image
if that package is responsible for both training and prediction. Like
the previous examples, all that is required is to add a set of
entrypoints to an existingsetup.pyscript.In addition, we can also fix thepython_versionand add customdockerlinesto the final imagefromsetuptoolsimportsetup,find_packagessetup(name='example',version='0.0.1',packages=find_packages(),include_package_data=True,# This will force the python version for the resulting imagepython_version='3.6.6',# This will run during the docker build stagedockerlines=['RUN echo Hello, World!','RUN echo Hello, Sailor!',],# The predict and train entrypoints create distinct imagesentry_points={'baklava.train':['my_train_entrypoint = example.main:my_training_function',],'baklava.predict':['my_predict_entrypoint = example.main:my_hosted_function',]'baklava.initialize':['my_initializer = example.main:my_init_function',]})With thissetup.py, both a prediction and a training docker image can
be built:python setup.py predict
python setup.py trainCommunityEngage with the Baklava + MLCTL community on Slack at:https://mlctl.slack.com/ContributingFor information on how to contribute tobaklava, please read through thecontributing guidelines.