Skip to content

Instantly share code, notes, and snippets.

@nov05
Last active December 6, 2024 01:30
Show Gist options
  • Save nov05/d9c3be6c2ab9f6c050e3d988830db08b to your computer and use it in GitHub Desktop.
Save nov05/d9c3be6c2ab9f6c050e3d988830db08b to your computer and use it in GitHub Desktop.
  • Local Install Requirements
Python 3.7
MXNet 1.8
Pandas >= 1.2.4
AutoGluon 0.2.0
  • πŸ‘‰ create sagemaker base environment
conda create -n sagemaker_py310 python=3.10
conda activate sagemaker_py310
pip install boto3 awscli sagemaker
conda install -c conda-forge jupyterlab
python -m ipykernel install --user --name sagemaker_py310
aws configure
jupyter lab
  • πŸ‘‰ create an environment cloned from the base environment
conda create --name awsmle_py310 --clone sagemaker_py310
python -m ipykernel install --user --name awsmle_py310
conda install -c conda-forge mamba  (having unicode file names issue) 
mamba install -c conda-forge autogluon (failed)  
conda install -c conda-forge mamba==1.4.7  
conda update conda
mamba install -c conda-forge autogluon  
mamba install -c conda-forge "ray-tune >=2.6.3,<2.7" "ray-default >=2.6.3,<2.7" 
pip install bokeh==2.0.1  
pip install kaggle
pip install ipywidgets
pip install ydata-profiling  
pip install smdebug
pip install jinja2==2.11.3
pip install markupsafe==2.0.1
pip install 'sagemaker[local]'
pip install sagemaker_inference
  • To run SageMaker in the local environment, add the following 3 keys to ~/.aws/credentials.
notepad C:\Users\*\.aws\credentials
(awsmle_py310) PS D:\github\udacity-nd009t-C2-Developing-ML-Workflow> cat ~/.aws/credentials
[default]
aws_access_key_id = <?>
aws_secret_access_key = <?>
aws_session_token = *vQ== <?>
## reset the session after updating credentials
import boto3
boto3.DEFAULT_SESSION = None

  • πŸ‘‰ create an env with cuda enabled, torch and transformers installed.
conda create --name conda_py310 python=3.10  
conda activate conda_py310  
nvidia-smi
conda install cuda  --channel "nvidia/label/cuda-12.1.0"    
nvcc --version  
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia  
pip install transformers
pip install pandas
pip install scikit-learn
conda install -c conda-forge ipywidgets
pip install wandb

  • docker commands
docker images
docker ps
  • Windows Powershell commands
conda deactivate
conda env remove --name awsmle_py37
  • install wsl-ubuntu
wsl --list --online 
wsl --install -d Ubuntu-22.04

install ubuntu, restart Windows OS
install Docker Desktop, restart Windows


  • Tips:
    • Choose the torch version according to the cuda version at https://pytorch.org/
    • Choose the MXNet version according to the cuda version at mxnet.apache.org.
      MXNet 1.8 does not officially support CUDA 12.x, as the latest version (MXNet 1.8) was designed for compatibility with CUDA versions up to 11.x. However, you can try using a previous version of CUDA (such as CUDA 11.0 or 10.2) to install MXNet 1.8 with GPU support.

  • Some output in the terminal
(awsmle_py37) PS D:\github\udacity-cd0385-exercise-files> nvidia-smi                                                                                      
Tue Nov 19 14:19:10 2024                                                                                                                                  
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 555.99                 Driver Version: 555.99         CUDA Version: 12.5     |                                                               
|-----------------------------------------+------------------------+----------------------+                                                               
| GPU  Name                  Driver-Model | Bus-Id          Disp.A | Volatile Uncorr. ECC |                                                               
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |                                                               
|                                         |                        |               MIG M. |                                                               
|=========================================+========================+======================|                                                               
|   0  NVIDIA GeForce GTX 1650 Ti   WDDM  |   00000000:01:00.0 Off |                  N/A |                                                               
| N/A   46C    P0             14W /   50W |       0MiB /   4096MiB |      0%      Default |                                                               
|                                         |                        |                  N/A |                                                               
+-----------------------------------------+------------------------+----------------------+                                                               
                                                                                                                                                          
+-----------------------------------------------------------------------------------------+                                                               
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+
(awsmle_py37) PS D:\github\udacity-cd0385-exercise-files> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Wed_Feb__8_05:53:42_Coordinated_Universal_Time_2023
Cuda compilation tools, release 12.1, V12.1.66
Build cuda_12.1.r12.1/compiler.32415258_0
(awsmle_py37) PS D:\github\udacity-cd0385-exercise-files> python -m ipykernel install --user --name=awsmle_py37                         
Installed kernelspec awsmle_py37 in C:\Users\*\AppData\Roaming\jupyter\kernels\awsmle_py37  
(awsmle_py310) PS D:\github\udacity-cd0385-exercise-files> aws configure
AWS Access Key ID [****************HX33]: 
AWS Secret Access Key [****************HD5x]: 
Default region name [us-east-1]: us-east-1
Default output format [None]: json
@nov05
Copy link
Author

nov05 commented Nov 26, 2024

  • create SageMaker layer for Lambda functions

$ aws lambda publish-layer-version --layer-name sagemaker-layer --zip-file fileb://sagemaker-layer.zip

(awsmle_py310) PS D:\github\udacity-nd009t-C2-Developing-ML-Workflow> aws lambda publish-layer-version --layer-name sagemaker-layer --zip-file fileb://sagemaker-layer.zip
{
    "Content": {
        "Location": "https://prod-04-2014-layers.s3.us-east-1.amazonaws.com/snapshots/807711953667/sagemaker-layer-916c77e6-f7a3-4dc7-bd1e-6dd9ab5a7321?versionId=UK7HYuxaBN5A8VPVcGeaeQxV2Oq7L_l2&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEIH%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJGMEQCICbHuLXtZzox%2FDcD4W4FHt%2Bw5qg9VGSqfVC%2F3GTAL3d1AiAXZvwluvZCcP8iBvHoBGcQ1cbNpBJUZok%2Fw5967G38USqyBQgqEAAaDDc0OTY3ODkwMjgzOSIMdkhZWaLZtPPbTpC%2BKo8F0sefAeW7pzE%2B5asbi38Bi2Pt4oJtbFUDAxNVpRyuUqlvjj1w6VTHseFnkkERCAZZ%2BHyCN1vQtJ9fVYvmlM%2FMKibMA6MGMKYn7XTSaNRzQu21MdLaGyNFW84Jx0yO2wUe2cumqDP3aJJpCMsdyt2M5is8YQPjaYZqzYw9Fyd42zll3bpps0F%2BtG592AjRX%2FFg9vuGfCqYe6gr1JtH1A3Jqa%2BLsPuKeA0xnVzw5H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%2Fs3%2Faws4_request&X-Amz-Signature=c023271972d4fb4ca92c53c8b2b4e81a865183c3f1cc75487ae073993cb2357e",
        "CodeSha256": "ofdERd8h+eJmzGuSAI3OalGpKE8OI5wqUF8HEBCy8Pc=",
        "CodeSize": 4406191
    },
    "LayerArn": "arn:aws:lambda:us-east-1:807711953667:layer:sagemaker-layer",
    "LayerVersionArn": "arn:aws:lambda:us-east-1:807711953667:layer:sagemaker-layer:1",
    "Description": "",
    "CreatedDate": "2024-11-26T09:33:42.626+0000",
    "Version": 1
}

@nov05
Copy link
Author

nov05 commented Nov 26, 2024

  • create a layer for lambda
  • lambda is using python 3.13
  • create a new local env. install sagemaker.
  • remove win32 files. rolled back to jsonschema==4.17.3.
  • go to the site-packages folder, to package all the files there into a ZIP file.
  • upload the ZIP file with aws cli command to create a layer (the ZIP file has a size of 99MB)
  • include the layer in the lambda function
conda create --name sagemaker_py313 python=3.13
conda activate sagemaker_py313
pip install sagemaker  
pip uninstall jsonschema
pip install jsonschema==4.17.3
aws lambda publish-layer-version --layer-name sagemaker-layer --zip-file fileb://python.zip    <- for small zip files
  • for large zip files, upload them to s3 first, then add or update layers from s3 files.
aws lambda publish-layer-version \
  --layer-name sagemaker-layer \
  --content S3Bucket=sagemaker-studio-807711953667-mmx0am1bt28,S3Key=python/sagemaker-layer.zip \
  --compatible-runtimes python3.13 \
  --description "SageMaker Layer"
  • exclude if possible
The following NEW packages will be INSTALLED:

  bzip2              pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_6 
  ca-certificates    pkgs/main/win-64::ca-certificates-2024.9.24-haa95532_0 
  expat              pkgs/main/win-64::expat-2.6.3-h5da7b33_0 
  libffi             pkgs/main/win-64::libffi-3.4.4-hd77b12b_1 
  libmpdec           pkgs/main/win-64::libmpdec-4.0.0-h827c3e9_0 
  openssl            pkgs/main/win-64::openssl-3.0.15-h827c3e9_0 
  pip                pkgs/main/win-64::pip-24.2-py313haa95532_0 
  python             pkgs/main/win-64::python-3.13.0-hadb2040_100_cp313 
  python_abi         pkgs/main/win-64::python_abi-3.13-0_cp313 
  setuptools         pkgs/main/win-64::setuptools-75.1.0-py313haa95532_0 
  sqlite             pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0
  tk                 pkgs/main/win-64::tk-8.6.14-h0416ee5_0
  tzdata             pkgs/main/noarch::tzdata-2024b-h04d1e81_0
  vc                 pkgs/main/win-64::vc-14.40-h2eaa2aa_1
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.40.33807-h98bb1dd_1
  wheel              pkgs/main/win-64::wheel-0.44.0-py313haa95532_0
  xz                 pkgs/main/win-64::xz-5.4.6-h8cc25b3_1
  zlib               pkgs/main/win-64::zlib-1.2.13-h8cc25b3_1
(sagemaker_py313) PS D:\github\udacity-nd009t-C2-Developing-ML-Workflow> pip uninstall jsonschema
Found existing installation: jsonschema 4.23.0
Uninstalling jsonschema-4.23.0:
  Would remove:
    d:\users\guido\miniconda3\envs\sagemaker_py313\lib\site-packages\jsonschema-4.23.0.dist-info\*
    d:\users\guido\miniconda3\envs\sagemaker_py313\lib\site-packages\jsonschema\*
    d:\users\guido\miniconda3\envs\sagemaker_py313\scripts\jsonschema.exe
Proceed (Y/n)? y
  Successfully uninstalled jsonschema-4.23.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment