Skip to content

Commit e4c3b84

Browse files
committed
Added xgboost-inference-arm64-docker-lambda sample code
1 parent 0c2bd50 commit e4c3b84

File tree

11 files changed

+486
-0
lines changed

11 files changed

+486
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,5 @@ hebert-sentiment-analysis-inference-docker-lambda/.aws-sam/build.toml
339339
hebert-sentiment-analysis-inference-docker-lambda/samconfig.toml
340340
fasttext-inference-docker-lambda/.aws-sam/build.toml
341341
fasttext-inference-docker-lambda/samconfig.toml
342+
xgboost-inference-arm64-docker-lambda/.aws-sam/build.toml
343+
xgboost-inference-arm64-docker-lambda/samconfig.toml

img/xgboost_arm_64_arch_view.png

102 KB
Loading
105 KB
Loading
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
2+
# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
3+
4+
### Linux ###
5+
*~
6+
7+
# temporary files which can be created if a process still has a handle open of a deleted file
8+
.fuse_hidden*
9+
10+
# KDE directory preferences
11+
.directory
12+
13+
# Linux trash folder which might appear on any partition or disk
14+
.Trash-*
15+
16+
# .nfs files are created when an open file is removed but is still being accessed
17+
.nfs*
18+
19+
### OSX ###
20+
*.DS_Store
21+
.AppleDouble
22+
.LSOverride
23+
24+
# Icon must end with two \r
25+
Icon
26+
27+
# Thumbnails
28+
._*
29+
30+
# Files that might appear in the root of a volume
31+
.DocumentRevisions-V100
32+
.fseventsd
33+
.Spotlight-V100
34+
.TemporaryItems
35+
.Trashes
36+
.VolumeIcon.icns
37+
.com.apple.timemachine.donotpresent
38+
39+
# Directories potentially created on remote AFP share
40+
.AppleDB
41+
.AppleDesktop
42+
Network Trash Folder
43+
Temporary Items
44+
.apdisk
45+
46+
### PyCharm ###
47+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
48+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
49+
50+
# User-specific stuff:
51+
.idea/**/workspace.xml
52+
.idea/**/tasks.xml
53+
.idea/dictionaries
54+
55+
# Sensitive or high-churn files:
56+
.idea/**/dataSources/
57+
.idea/**/dataSources.ids
58+
.idea/**/dataSources.xml
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
64+
# Gradle:
65+
.idea/**/gradle.xml
66+
.idea/**/libraries
67+
68+
# CMake
69+
cmake-build-debug/
70+
71+
# Mongo Explorer plugin:
72+
.idea/**/mongoSettings.xml
73+
74+
## File-based project format:
75+
*.iws
76+
77+
## Plugin-specific files:
78+
79+
# IntelliJ
80+
/out/
81+
82+
# mpeltonen/sbt-idea plugin
83+
.idea_modules/
84+
85+
# JIRA plugin
86+
atlassian-ide-plugin.xml
87+
88+
# Cursive Clojure plugin
89+
.idea/replstate.xml
90+
91+
# Ruby plugin and RubyMine
92+
/.rakeTasks
93+
94+
# Crashlytics plugin (for Android Studio and IntelliJ)
95+
com_crashlytics_export_strings.xml
96+
crashlytics.properties
97+
crashlytics-build.properties
98+
fabric.properties
99+
100+
### PyCharm Patch ###
101+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
102+
103+
# *.iml
104+
# modules.xml
105+
# .idea/misc.xml
106+
# *.ipr
107+
108+
# Sonarlint plugin
109+
.idea/sonarlint
110+
111+
### Python ###
112+
# Byte-compiled / optimized / DLL files
113+
__pycache__/
114+
*.py[cod]
115+
*$py.class
116+
117+
# C extensions
118+
*.so
119+
120+
# Distribution / packaging
121+
.Python
122+
build/
123+
develop-eggs/
124+
dist/
125+
downloads/
126+
eggs/
127+
.eggs/
128+
lib/
129+
lib64/
130+
parts/
131+
sdist/
132+
var/
133+
wheels/
134+
*.egg-info/
135+
.installed.cfg
136+
*.egg
137+
138+
# PyInstaller
139+
# Usually these files are written by a python script from a template
140+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
141+
*.manifest
142+
*.spec
143+
144+
# Installer logs
145+
pip-log.txt
146+
pip-delete-this-directory.txt
147+
148+
# Unit test / coverage reports
149+
htmlcov/
150+
.tox/
151+
.coverage
152+
.coverage.*
153+
.cache
154+
.pytest_cache/
155+
nosetests.xml
156+
coverage.xml
157+
*.cover
158+
.hypothesis/
159+
160+
# Translations
161+
*.mo
162+
*.pot
163+
164+
# Flask stuff:
165+
instance/
166+
.webassets-cache
167+
168+
# Scrapy stuff:
169+
.scrapy
170+
171+
# Sphinx documentation
172+
docs/_build/
173+
174+
# PyBuilder
175+
target/
176+
177+
# Jupyter Notebook
178+
.ipynb_checkpoints
179+
180+
# pyenv
181+
.python-version
182+
183+
# celery beat schedule file
184+
celerybeat-schedule.*
185+
186+
# SageMath parsed files
187+
*.sage.py
188+
189+
# Environments
190+
.env
191+
.venv
192+
env/
193+
venv/
194+
ENV/
195+
env.bak/
196+
venv.bak/
197+
198+
# Spyder project settings
199+
.spyderproject
200+
.spyproject
201+
202+
# Rope project settings
203+
.ropeproject
204+
205+
# mkdocs documentation
206+
/site
207+
208+
# mypy
209+
.mypy_cache/
210+
211+
### VisualStudioCode ###
212+
.vscode/*
213+
!.vscode/settings.json
214+
!.vscode/tasks.json
215+
!.vscode/launch.json
216+
!.vscode/extensions.json
217+
.history
218+
219+
### Windows ###
220+
# Windows thumbnail cache files
221+
Thumbs.db
222+
ehthumbs.db
223+
ehthumbs_vista.db
224+
225+
# Folder config file
226+
Desktop.ini
227+
228+
# Recycle Bin used on file shares
229+
$RECYCLE.BIN/
230+
231+
# Windows Installer files
232+
*.cab
233+
*.msi
234+
*.msm
235+
*.msp
236+
237+
# Windows shortcuts
238+
*.lnk
239+
240+
# Build folder
241+
242+
*/build/*
243+
244+
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM public.ecr.aws/lambda/python:3.8-arm64
2+
3+
COPY requirements.txt ./
4+
RUN python3.8 -m pip install -r requirements.txt -t .
5+
6+
COPY ./train-code/bc_xgboost_train.py ./
7+
RUN python bc_xgboost_train.py
8+
9+
COPY ./app/app.py ./
10+
11+
CMD ["app.handler"]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## Serverless XGBoost Model Serving on Graviton2 architecture
2+
3+
This examples illustrates how to serve XGBoost model on Lambda Function on Graviton2 architecture to predict breast cancer.
4+
5+
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.
6+
7+
- app - Code for the application's Lambda function.
8+
- train-code - Code for training XGBoost model based on breast cancer dataset.
9+
- events - Invocation events that you can use to invoke the function.
10+
- template.yaml - A template that defines the application's AWS resources.
11+
12+
The application uses several AWS resources, including Lambda functions. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
13+
14+
## Deploy the sample application
15+
16+
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
17+
18+
To use the SAM CLI, you need the following tools.
19+
20+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
21+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
22+
23+
You may need the following for local testing.
24+
* [Python 3 installed](https://www.python.org/downloads/)
25+
26+
To build and deploy your application for the first time, run the following in your shell:
27+
28+
```bash
29+
sam build
30+
sam deploy --guided
31+
```
32+
33+
The first command will build a docker image from a Dockerfile and then copy the source of your application inside the Docker image. The second command will package and deploy your application to AWS, with a series of prompts:
34+
35+
* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
36+
* **AWS Region**: The AWS region you want to deploy your app to.
37+
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
38+
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
39+
* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application.
40+
41+
## Use the SAM CLI to build and test locally
42+
43+
Build your application with the `sam build` command.
44+
45+
```bash
46+
xgboost-inference-arm64-docker-lambda$ sam build
47+
```
48+
49+
The SAM CLI builds a docker image from a Dockerfile and then installs dependencies defined in `requirements.txt` inside the docker image. The processed template file is saved in the `.aws-sam/build` folder.
50+
51+
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project.
52+
53+
Run functions locally and invoke them with the `sam local invoke` command.
54+
55+
```bash
56+
xgboost-inference-arm64-docker-lambda$ sam local invoke XGBoostInferenceArm64Function --event events/event.json
57+
```
58+
59+
## View your Lambda Architecture
60+
61+
1. In the [Lambda Console](https://console.aws.amazon.com/lambda/), select the `Image` tab and scrll down to the `Image` section.
62+
2. For `Architecture` you should see `arm64`
63+
64+
![arm64 Architecture](../img/xgboost_arm_64_arch_view.png)
65+
66+
67+
## Testing your Lambda function in the Cloud
68+
69+
1. In the [Lambda Console](https://console.aws.amazon.com/lambda/), select Configure test events from the Test events dropdown.
70+
2. For Event Name, enter InferenceTestEvent.
71+
3. Copy the event JSON from [here](./events/event.json) and paste in the dialog box.
72+
4. Choose _**Create**_.
73+
74+
![Configure test event](../img/xgboost_configure_test_event.png)
75+
76+
After saving, you see InferenceTestEvent in the Test list. Now choose _**Test**_.
77+
78+
You see the Lambda function inference result, log output, and duration:
79+
80+
![Lambda execution result](../img/xgboost_arm_64_execution_result.png)
81+
82+
## Fetch, tail, and filter Lambda function logs
83+
84+
To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
85+
86+
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
87+
88+
```bash
89+
xgboost-inference-arm64-docker-lambda$ sam logs -n XGBoostInferenceArm64Function --stack-name xgboost-inference-arm64-docker-lambda --tail
90+
```
91+
92+
You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html).
93+
94+
## Cleanup
95+
96+
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
97+
98+
```bash
99+
aws cloudformation delete-stack --stack-name xgboost-inference-arm64-docker-lambda
100+
```
101+
102+
## Resources
103+
104+
See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts.
105+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
import os
3+
import json
4+
import joblib
5+
import xgboost as xgb
6+
import pandas as pd
7+
8+
loaded_model = joblib.load('./bc-xgboost-model')
9+
10+
def handler(event, context):
11+
print('Received event: ' + json.dumps(event, indent=2))
12+
13+
payload_df = pd.json_normalize([event])
14+
result = loaded_model.predict(payload_df)
15+
16+
print("Returning: {}".format(result[0]))
17+
return(json.dumps({"result": result[0]}))
18+
19+

0 commit comments

Comments
 (0)