afe

The API for AFE as a library.

Functions in this file are re-exported from other modules. See those modules for documentation.

Submodules

Attributes

python_version

CALIBRATED_POSTFIX

QUANTIZED_POSTFIX

f

Functions

load_awesomenet(→ afe.ir.net.AwesomeNet)

Loads an AwesomeNet from yaml and npz files

save_awesomenet(→ None)

Saves an AwesomeNet to a yaml and npz file. Overwrites existing files.

load_tensorflow_model(→ afe.ir.net.AwesomeNet)

Loads a TensorFlow model into an _AwesomeNet format

load_pytorch_model(→ afe.ir.net.AwesomeNet)

Loads a PyTorch model into an _AwesomeNet format.

load_tflite_model(→ afe.ir.net.AwesomeNet)

Loads a TensorFlow Lite model into an _AwesomeNet format.

load_onnx_model(→ afe.ir.net.AwesomeNet)

Loads an ONNX model into an _AwesomeNet format.

load_keras_model(→ afe.ir.net.AwesomeNet)

Loads a Keras model into an _AwesomeNet format.

load_caffe_model(→ afe.ir.net.AwesomeNet)

Loads a caffe model into an _AwesomeNet format.

load_caffe2_model(→ afe.ir.net.AwesomeNet)

Loads a caffe2 model into an _AwesomeNet format.

quantize_net(net, input_generator, asymmetry, ...)

Quantizes a network.

execute_net(→ List[numpy.ndarray])

Executes a network.

compile_net(net, output_elf_path[, compress])

Compile a network using Product Compiler.

simulate_isim_module(elf_file, output_path[, ...])

Wrapper to take elf file and generate summary KPI file, and trace file.

Package Contents

afe.python_version[source]
afe.load_awesomenet(model_name: str, network_directory: str = '') afe.ir.net.AwesomeNet[source]

Loads an AwesomeNet from yaml and npz files

Parameters:
  • model_name – The name of the model, used in file names.

  • network_directory – The directory to load the files from.

afe.save_awesomenet(net: afe.ir.net.AwesomeNet, model_name: str, output_directory: str = '') None[source]

Saves an AwesomeNet to a yaml and npz file. Overwrites existing files.

Parameters:
  • net – AwesomeNet to save.

  • model_name – The name of the model, used in file names.

  • output_directory – The directory to save the files to.

afe.load_tensorflow_model(model_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], output_names: List[str], layout: str = 'NHWC', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a TensorFlow model into an _AwesomeNet format

Parameters:
  • model_path – Path to a .pb TensorFlow model

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • output_names – List of output names of the network eg. [‘output1’, ‘output2’]

  • layout – any variation of the characters NHWC representing Batch Size, Height, Width, and Channels

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_pytorch_model(model_path: str, input_names: List[str], input_shapes: List[Tuple[int, Ellipsis]], input_dtypes: List[afe.ir.tensor_type.ScalarType] | None = None, layout: str = 'NCHW', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a PyTorch model into an _AwesomeNet format.

Parameters:
  • model_path – Path to a PyTorch file (.pt) that contains the entire model

  • input_names – List of input names. eg [‘input’]

  • input_shapes – List of input shapes corresponding to input names. eg. [(1, 224, 224, 3)]

  • input_dtypes – List of input datatypes corresponding to input names. eg [‘float32’]

  • layout – Input data layout. Default is ‘NCHW’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_tflite_model(model_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], dtype_dict: Dict[str, afe.ir.tensor_type.ScalarType], layout: str = 'NHWC', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a TensorFlow Lite model into an _AwesomeNet format.

Parameters:
  • model_path – Path to a .tflite file containing the TensorFlow Lite model

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • dtype_dict – Dictionary of input names to input types eg. {‘input’, ‘float32’}

  • layout – Input data layout. Default is ‘NHWC’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_onnx_model(model_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], dtype_dict: Dict[str, afe.ir.tensor_type.ScalarType], layout: str = 'NCHW', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads an ONNX model into an _AwesomeNet format.

Parameters:
  • model_path – Path to a .onnx file containing the onnx model

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • dtype_dict – Dictionary of input names to input types eg. {‘input’, ‘float32’}

  • layout – Input data layout. Default is ‘NCHW’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_keras_model(model_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], layout: str = 'NCHW', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a Keras model into an _AwesomeNet format.

Parameters:
  • model_path – Path to a .h5 file containing the keras model

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • layout – Input data layout. Default is ‘NCHW’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_caffe_model(prototxt_file_path: str, caffemodel_file_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], dtype_dict: Dict[str, afe.ir.tensor_type.ScalarType], layout: str = 'NCHW', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a caffe model into an _AwesomeNet format.

Parameters:
  • prototxt_file_path – filepath to the caffe .prototxt file

  • caffemodel_file_path – filepath to the caffe .caffemodel file

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • dtype_dict – Dictionary of input names to input types eg. {‘input’, ‘float32’}

  • layout – Input data layout. Default is ‘NCHW’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.load_caffe2_model(init_net_file_path: str, predict_net_file_path: str, shape_dict: Dict[str, Tuple[int, Ellipsis]], dtype_dict: Dict[str, afe.ir.tensor_type.ScalarType], layout: str = 'NCHW', is_quantized: bool = False) afe.ir.net.AwesomeNet[source]

Loads a caffe2 model into an _AwesomeNet format.

Parameters:
  • init_net_file_path – filepath to the caffe2 .pb init_net file

  • predict_net_file_path – filepath to the caffe2 .pb predict_net file

  • shape_dict – Dictionary of input names to input shapes eg. {‘input’, (1,224,224,3)}

  • dtype_dict – Dictionary of input names to input types eg. {‘input’, ‘float32’}

  • layout – Input data layout. Default is ‘NCHW’

  • is_quantized – Whether the model is pre-quantized. Default is False

Returns:

An _AwesomeNet

afe.quantize_net(net: afe.ir.net.AwesomeNet, input_generator: sima_utils.data.data_generator.DataGenerator, asymmetry: bool, per_channel: bool, max_calibration_samples: int)[source]

Quantizes a network.

Parameters:
  • net – an _AwesomeNet

  • input_generator – a DataGenerator we use to feed inputs into the _AwesomeNet during calibration

  • asymmetry – If True this function performs asymmetric quantization. Otherwise, it performs symmetric quantization

  • per_channel – If True this function performs per_channel quantization.

  • max_calibration_samples – Maximum number of samples we use for calibration

afe.execute_net(net: afe.ir.net.AwesomeNet, inputs: Dict[str, numpy.ndarray], dequantize_outputs: bool = True) List[numpy.ndarray][source]

Executes a network.

Parameters:
  • net – an _AwesomeNet

  • inputs – Dictionary of placeholder node names (str) to the input data

  • dequantize_outputs – Whether to dequantize the output data to floating point

:return The result of executing the output node

afe.compile_net(net: afe.ir.net.AwesomeNet, output_elf_path: str, compress: bool = True)[source]

Compile a network using Product Compiler.

Parameters:
  • net – an _AwesomeNet.

  • output_elf_path – Path in which elf files should be created.

  • compress – If True mlc file is compressed before generating .elf file.

afe.simulate_isim_module(elf_file: str, output_path: str, output_kpi_file_name: str | None = None, output_trace_file_name: str | None = None, generate_csv_kpi: bool | None = True, override_existing: bool | None = False, target: sima_utils.common.Platform | None = None)[source]

Wrapper to take elf file and generate summary KPI file, and trace file.

Parameters:
  • elf_file – Path to the location of the elf file to simulate in ISIM.

  • output_path – Location of where to store all output files. If location does not exist, it will be created.

  • output_kpi_file_name – Name for output kpi file. Default: elf_file name without ‘.elf’ extension and finishing with ‘_kpis.txt’ postfix.

  • output_trace_file_name – Name for output trace file. Default: elf_file name without ‘.elf’ extension and finishing with ‘_trace.mpack’ postfix.

  • generate_csv_kpi – Whether to generate a CSV file with the KPIs in addition to the txt.

  • override_existing – Whether to override existing kpis and trace files from a previous run.

  • target – [Optional] platform target.

afe.CALIBRATED_POSTFIX = '_calibrated'[source]
afe.QUANTIZED_POSTFIX = '_quantized'[source]
afe.f[source]