To see a summary discussion on other ML @ Edge tooling, see edge-ml-options.md
This repository contains a Azure ML pipeline implementation for:
- Training a PyTorch model
- Exporting to ONNX format
- Converting ONNX to C code using onnx2c
- Compiling and testing the C code
- Building a minimal binary for deployment
.
├── README.md
├── diagrams
├── environments
│ ├── gcc
│ │ └── Dockerfile
│ ├── onnx2c
│ │ └── Dockerfile
│ └── pytorch
│ ├── Dockerfile
│ └── requirements.txt
├── requirements.txt
├── setup_pipeline.py
└── src
├── compile_test
│ ├── run.py
│ └── templates
│ ├── model_impl.c
│ ├── test_model.c
│ └── time_series_model.h
├── minimal_binary
│ ├── README.md
│ ├── binary-size-guide.md
│ ├── run.py
│ └── templates
│ ├── README.md
│ ├── compile_minimal.sh
│ ├── minimal_example.c
│ ├── model_impl.c
│ ├── nn_wrapper.h
│ └── time_series_model.h
├── onnx2c
│ └── run.py
└── pytorch_train
└── run.py
- Azure ML workspace
- Azure CLI installed
- Python environment with the following packages:
- azure-ai-ml
- azure-identity
- Clone this repository
- Update
setup_pipeline.pywith your Azure ML workspace details (current setup uses a.envfile) - Run the setup script:
# Set up the pipeline without running it
python setup_pipeline.py
# Or set up and immediately run the pipeline
python setup_pipeline.py --runThe --run flag will submit the pipeline job to Azure ML immediately. Without this flag, the setup script will only prepare the pipeline but not execute it.
- Trains a simple time series neural network using PyTorch
- Exports the model to ONNX format
- Saves test data for later validation
- Uses onnx2c to convert the ONNX model to C code
- Creates additional C files needed for compilation and testing
- Compiles the C code generated from the model
- Runs tests using the test data saved during training
- Saves test results for analysis
- Creates a minimal binary suitable for embedded deployment
- Optimizes for size using compiler flags
- Provides memory usage statistics
Contains PyTorch and all dependencies needed for training and ONNX export.
Contains the onnx2c tool built from source, which converts ONNX models to C code.
Contains GCC and tools needed to compile and test the C code.
The pipeline produces several artifacts:
-
Training Output
- ONNX model
- Test data (CSV files)
- Training metrics and visualizations
-
C Code Output
- Generated C code from the ONNX model
- Supporting C files for compilation
-
Test Results
- Test output showing prediction accuracy
- Compiled test binary
-
Minimal Binary
- Optimized binary for deployment
- Size and memory usage statistics
To adapt this pipeline for your own models:
- Modify
src/pytorch_train/run.pyto train your specific model - Adjust the C wrapper code in
src/onnx2c/run.pyif your model has a different interface - Update compilation settings in
src/minimal_binary/run.pyfor your target platform
- Check the logs for each pipeline step in the Azure ML Studio UI
- Ensure all environments are correctly built before running the pipeline
- Verify that the ONNX model is compatible with onnx2c (not all ONNX operations are supported)
