Neural Compiler V2 (Beta)#
Beta
Neural Compiler V2 is in beta. Its command line flags and Python API may still change between releases. The existing Neural Compiler (mx_nc) remains the default and is what the rest of this documentation refers to.
Neural Compiler V2 is the next version of the Neural Compiler interface. It compiles the same models into the same Dataflow Programs (DFP), with two user-facing changes:
a new command,
memryx compile, with a smaller and more consistent set of flags, anda new Python class,
NeuralCompilerV2, which can run the whole compilation in one call or one stage at a time.
See also
Neural Compiler for the compiler flow and supported frameworks, which are unchanged.
Usage#
After installing the SDK, the compiler is available as the compile subcommand of the memryx command. mx is a shorter alias for memryx.
memryx compile --help
Compile a single model for four chips, which is the default:
memryx compile mobilenet.h5
This writes mobilenet.dfp to the current directory.
Shell completion for flags and subcommands can be installed with:
memryx --install-completion
Required Argument#
Argument |
Description |
|---|---|
|
Space separated list of model files to compile. The file extension indicates the model framework, according to the following table. |
Framework |
Extensions |
|---|---|
Keras |
.h5, .json, .keras |
TensorFlow |
.pb, .meta |
TensorFlow-Lite |
.tflite |
ONNX |
.onnx |
System Selection#
Option |
Description |
|---|---|
-c, --num-chips |
Target number of chips (default: 4). |
Model Arguments#
Option |
Description |
|---|---|
--input-shapes |
Input shape(s) for the model(s), as a JSON string. Only necessary if the input shapes cannot be inferred from the model itself. Please see Input Shapes. |
--autocrop |
Automatically crop the pre/post-processing layers from the input model(s). NOTE: This feature is considered experimental, as it may sometimes crop layers that could have run on the MXA. |
--inputs |
The names of the input layers of the model(s). Comma separated within a model, |
--outputs |
The names of the output layers of the model(s). Same format as |
--io-file |
JSON config file that contains the names of the input and output layers. Use this instead of |
Mapping Arguments#
Option |
Description |
|---|---|
-e, --effort |
Set the compiler’s optimization effort. Select from [lazy, normal, hard, closed_loop]. Please see Effort. |
-j, --num-processes |
Number of worker processes (default: 1). Only has an effect when |
--target-fps |
Sets the target FPS for the mapper’s optimizer. By default the optimizer maximizes performance. |
--show-optimization |
Animates mapper optimization steps in the terminal. |
--split-number |
Compile only the selected split of a |
--save-all-splits |
Save every successful candidate DFP of a |
--device_id |
ID of the connected MXA device used to benchmark candidates when |
Advanced Precision Options#
Option |
Description |
|---|---|
--wbtable |
Optional JSON file with per-layer weight quantization information. Valid precision values are 4, 8, and 16-bit. Please see Mixed-Precision Weights. |
--exp-auto-dp |
Optional experimental flag that will attempt to identify layers that could be sensitive to weight quantization and automatically set them to 16-bit. If |
--hpoc |
Optional JSON list of integers that specify the final layer output channels to increase precision, e.g. |
--hpoc-file |
Optional JSON file that specifies the output channels to increase precision on a per-output basis. Cannot be used together with |
Advanced General Options#
Option |
Description |
|---|---|
--extensions |
Path to a .nce file that extends / modifies the compiler functionality. Repeat the flag to load multiple extensions. |
--allow-unsigned-extensions |
Allows execution of unsigned extensions for the Neural Compiler. An extension may execute arbitrary code, only run extensions from sources you trust! |
Other Arguments#
Option |
Description |
|---|---|
--help |
Print the help information. |
-v, --verbose |
Controls how verbose the compiler will be. Repeat this option for more information (e.g. -vv). |
--dfp-fname |
Output dataflow program filename (defaults to ModelName.dfp, or models.dfp when compiling multiple models). The |
--sim-dfp / --no-sim-dfp |
|
Be sure to check the troubleshooting page if you have issues running the Neural Compiler.
Changes from mx_nc#
The following table lists the mx_nc arguments that were renamed or changed format. Arguments that only changed from underscores to hyphens (e.g. --num_chips to --num-chips) are not listed.
mx_nc |
memryx compile |
Notes |
|---|---|---|
|
|
Models are now positional arguments. |
|
|
Now a single JSON string. Please see Input Shapes. |
|
|
Same file format. |
|
|
Same file format. |
|
|
Now a JSON list ( |
|
|
Same file format. |
|
|
The |
|
|
|
|
|
The flag is repeated instead of taking a space separated list. |
The following mx_nc arguments are not available in memryx compile: -g / --chip_gen, --input_format, and -so_aux / --show_optimization_aux.
New in memryx compile: the closed_loop effort, -j / --num-processes, --split-number, --save-all-splits, and --device_id.
Effort#
The --effort option controls how much time the compiler spends searching for an efficient mapping.
Effort |
Description |
|---|---|
|
Compile very quickly, but with low inference performance (FPS). |
|
Default. Strikes a good balance between compile time and inference performance. |
|
Will get the best inference performance without hardware, but will greatly increase time to compile. |
|
Like |
The hard and closed_loop efforts compile the model with 30 different sets of compiler options, called splits, and keep the best result. Splits are not related to model cropping and do not modify the layers being compiled. Use -j to run the splits in parallel:
memryx compile yolov8n.onnx --autocrop -e hard -j 8
With hard, the best split is selected using the compiler’s cost function. With closed_loop, each candidate DFP is benchmarked on the MXA and the one with the highest measured FPS is selected, so an MXA must be connected to the machine running the compiler. Use --device_id to select which device runs the benchmark:
memryx compile yolov8n.onnx --autocrop -e closed_loop -j 8 --device_id 0
To keep every candidate DFP rather than only the best one, add --save-all-splits. The candidates are saved to a <dfp name>_splits directory next to the output DFP, as <dfp name>_split_<N>.dfp. Any existing contents of that directory are replaced. There can be fewer than 30 files: splits that produce identical results are only compiled once, and splits that fail are skipped.
If benchmarking fails during a closed_loop compile, the compile stops without producing a DFP. Using --save-all-splits keeps the candidate DFPs that were assembled before the failure.
The number of the split that produced a DFP is stored in the DFP. Valid split numbers are 0 to 29 for hard and closed_loop (and only 0 for lazy and normal); an out-of-range number stops the compile with an error. To reproduce that result without running every split again, pass the number to --split-number:
memryx compile yolov8n.onnx --autocrop -e hard --split-number 7
See also
Closed Loop Compilation Tutorial for background on splits and closed-loop compilation.
Multi-Model and Multi-Chip#
Multiple models are compiled into a single DFP by listing them. Models are allowed to be from different frameworks.
memryx compile model_0.h5 model_1.onnx model_2.pb
The number of chips is selected with -c:
memryx compile model_0.h5 model_1.h5 -c 2
Please see Neural Compiler for how models and input streams are distributed over the chips.
Input Shapes#
The Neural Compiler will attempt to infer the shape of each layer of each model. If this information is not present in a model, the shapes of its input layers must be passed in with --input-shapes.
--input-shapes takes a JSON object that maps the index of a model, in the order the models were given, to its input shape. The shape includes the batch size:
memryx compile frozen_graph.pb --input-shapes '{"0": [1, 300, 300, 3]}'
For a model with more than one input, provide a list of shapes:
memryx compile model.onnx --input-shapes '{"0": [[1, 224, 224, 3], [1, 128]]}'
In the multi-model scenario, only the models that need a shape have to be listed. Here the first model’s shapes are inferred and the second model’s shape is given:
memryx compile classifier.h5 detector.pb --input-shapes '{"1": [1, 300, 300, 3]}'
Important
Input shapes must always be provided in channel-last order (i.e NHWC), even if ONNX models provided are channel-first order (NCHW). The Neural compiler will internally handle assigning the correct shape for each layer.
Model Cropping#
Cropping works the same way as in mx_nc. Please see Model Cropping for a full description.
The cropped model and the cropped-out sections are saved next to the output DFP as <name>_crop.<ext>, <name>_pre.<ext> and <name>_post.<ext>, where <name> is the --dfp-fname name (or the model’s name if --dfp-fname is not given) and <ext> matches the model’s framework. The _pre and _post files are only written when that section exists. When compiling multiple models, the file names are prefixed with model_<index>_.
memryx compile ssd_mobilenet.pb --input-shapes '{"0": [1, 300, 300, 3]}' --autocrop -vv
Using the --inputs and --outputs arguments:
memryx compile model_0.pb --inputs "input_0" --outputs "output_0,output_1"
In the case of multi-models, the inputs and outputs of different models are separated using a vertical separator (|):
memryx compile model_0.pb model_1.onnx --inputs "model_0_input_0|model_1_input_0" \
--outputs "model_0_output_0,model_0_output_1|model_1_output_0"
Using a config file, in the in_out file format:
memryx compile model_0.pb --io-file model_in_out.json
If --inputs or --outputs is given together with --io-file, the names given on the command line are used.
Mixed-Precision Weights#
The weight precision can be specified per layer as 4-bit, 8-bit, or 16-bit using a JSON file passed to --wbtable:
memryx compile path/to/model --wbtable path/to/wbits.json
High-Precision Output Channels#
The channels of the final output-feature-map that need more precision are given to --hpoc as a JSON list:
memryx compile yolov4.h5 --hpoc "[0, 1, 2, 3, 4, 85, 86, 87, 88, 89, 170, 171, 172, 173, 174]"
For more flexibility, especially with multiple outputs, provide a JSON file to --hpoc-file to designate the channels on a per-output basis:
memryx compile yolov7.onnx --hpoc-file hpoc_file.json
See also
Neural Compiler Extensions#
Supplied .nce files are passed with --extensions. Repeat the flag to load more than one:
memryx compile <MODEL> --extensions first.nce --extensions second.nce
Only extensions signed by MemryX are loaded by default. Use --allow-unsigned-extensions to load unsigned extensions, and only for extensions from sources you trust.
See also
