diff --git a/CI/check_model.py b/CI/check_model.py index c935b9b..3d340db 100644 --- a/CI/check_model.py +++ b/CI/check_model.py @@ -1,9 +1,9 @@ import argparse -import os +import akida from cnn2snn import convert from quantizeml import load_model -from compute_device import compute_min_device + def process_model(file_path): try: @@ -18,7 +18,7 @@ def process_model(file_path): raise RuntimeError(f"❌ Error converting {file_path}: {e}") from e try: - device = compute_min_device(model_ak, enable_hwpr=True) + device = akida.compute_min_device(model_ak, enable_hwpr=True) result = len(device.mesh.nps) // 4 print(f"✅ {file_path}: needs {result} Akida nodes") except Exception as e: @@ -28,10 +28,9 @@ def process_model(file_path): if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--models", nargs="+", required=True, - help="Path to model files (.h5 or .onnx)") + help="Path to model files (.h5 or .onnx)") args = parser.parse_args() # Process each model for model_file in args.models: - if os.path.exists(model_file): - process_model(model_file) + process_model(model_file) diff --git a/CI/compute_device.py b/CI/compute_device.py deleted file mode 100644 index ed8ee0f..0000000 --- a/CI/compute_device.py +++ /dev/null @@ -1,257 +0,0 @@ -#!/usr/bin/env python -# ****************************************************************************** -# Copyright 2025 Brainchip Holdings Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ****************************************************************************** -__all__ = ["compute_min_device", "compute_common_device"] - -import warnings -from collections import namedtuple - -import akida - - -LayerSequence = namedtuple('LayerSequence', ['layers']) - - -def _get_outbounds(layer, layers): - return [ly for ly in layers if layer in ly.inbounds] - - -def _model_generator(layers): - # Scroll through a list of layers, returning a pair of consecutive layers. Notes: - # - one of the two branches must not have nodes (not implemented yet) - # - merge layer is performed in the following NP, so we take their inbounds - queue = [layers[-1]] - while len(queue) > 0: - t_layer = queue.pop(0) - inbounds = t_layer.inbounds - # Skip a layer if it is a merge one. - if len(inbounds) == 1 and len(inbounds[0].inbounds) > 1: - inbounds = inbounds[0].inbounds - # Check inbounds constraints. - if len(inbounds) > 1: - # In case of multiple branches, one of them must not contain layers. - # This translates to some inbound having multiple outbounds. - new_inbounds = [] - for ly in inbounds: - # Remove the branch with empty layers - if len(_get_outbounds(ly, layers)) == 1: - new_inbounds.append(ly) - if len(new_inbounds) != 1: - raise NotImplementedError(f"{t_layer} has multiple inbounds, " - "but there is no empty branch.") - # Remove the inbounds that are not empty branches. - inbounds = new_inbounds - - # Yield the pair (inbound, target_layer) if both have been mapped - # Or if taget_layer is mapped and the inbound is an InputData layer. - if len(inbounds) == 1 and t_layer.mapping is not None: - if inbounds[0].parameters.layer_type == akida.LayerType.InputData or \ - inbounds[0].mapping is not None: - yield LayerSequence((inbounds[0], t_layer)) - # Then, update the queue with the inbound layer. - queue.append(inbounds[0]) - - -def _get_initial_skip_dma_channels(model): - # The initial number of skip DMAs is len(btc) + len(skips) - SKIP_LAYER_TYPES = [akida.LayerType.Add, akida.LayerType.Concatenate] - BTC_LAYER_TYPES = [akida.LayerType.BufferTempConv, akida.LayerType.DepthwiseBufferTempConv] - skip_dma_channels = 0 - for ly in model.layers: - if ly.parameters.layer_type in SKIP_LAYER_TYPES + BTC_LAYER_TYPES: - skip_dma_channels += 1 - return skip_dma_channels - - -def _get_initial_number_of_fnp(model): - # The initial number of FNP is len(dense), since they are not split - FNP_LAYER_TYPES = [akida.LayerType.Dense1D] - nb_fnp = 0 - for ly in model.layers: - if ly.parameters.layer_type in FNP_LAYER_TYPES: - nb_fnp += 1 - return nb_fnp - - -def _get_np_components(model_or_pass, np_types=None): - total_nps = [] - for layer in model_or_pass.layers: - if hasattr(layer.mapping, 'nps'): - for np in layer.mapping.nps: - if np_types is None or np.type in np_types: - total_nps.append(np) - if hasattr(layer.mapping, 'skipdma_loads'): - for np in layer.mapping.skipdma_loads: - if np_types is None or np.type in np_types: - total_nps.append(np) - if hasattr(layer.mapping, 'skipdma_stores'): - for np in layer.mapping.skipdma_stores: - if np_types is None or np.type in np_types: - total_nps.append(np) - return total_nps - - -def compute_skip_dma_channels(model_or_pass): - # Compute the number of skip DMA channels as max(len(SKIP_DMA_STORE), len(SKIP_DMA_LOAD)) - skip_dma_load = _get_np_components(model_or_pass, (akida.NP.SKIP_DMA_LOAD,)) - skip_dma_store = _get_np_components(model_or_pass, (akida.NP.SKIP_DMA_STORE,)) - return max(len(skip_dma_load), len(skip_dma_store)) - - -def compute_number_of_cnp_tnp(model_or_pass): - # Compute the number of CNP/TNP-B. - CNP_TNP_B_TYPES = (akida.NP.CNP1, akida.NP.CNP2, akida.NP.TNP_B) - total_cnps = _get_np_components(model_or_pass, CNP_TNP_B_TYPES) - return len(total_cnps) - - -def compute_number_of_fnp(model_or_pass): - # Compute the number of FNP. - FNP_TYPES = (akida.NP.FNP2, akida.NP.FNP3) - total_fnps = _get_np_components(model_or_pass, FNP_TYPES) - return len(total_fnps) - - -def compute_min_device(model, - enable_hwpr=False, - sram_size=None, - minimal_memory=False, - initial_num_nodes=36): - """Builds the Akida virtual device that can fit the model entirely - with or without reconfiguration. - - Args: - model (akida.Model): the model used to determine the device. - enable_hwpr (bool, optional): if True, the device is computed assuming - partial reconfiguration. Defaults to False. - sram_size (akida.NP.SramSize, optional): Size of shared SRAM available inside the mesh. - Ignored when `minimal_memory` is True. Defaults to None. - minimal_memory (bool, optional): if True, computes and sets the minimal required - inputs and weights memory for the device. Defaults to False. - initial_num_nodes (int, optional): the initial number of nodes with which to compute - the base device. Defaults to 36. - - Returns: - akida.Device: the computed device - """ - NUM_NPS_PER_NODE = 4 - if model.ip_version != akida.IpVersion.v2: - raise ValueError("Only IpVersion.v2 models are supported. " - f"Current model version={model.ip_version}") - - # Create a copy of the model to avoid modifying the original one. - model = akida.Model(layers=model.layers) - - # Compute a base device with which to compute the next parameters. - params = {"num_skip_dma_channel": _get_initial_skip_dma_channels(model), - "num_fnp": _get_initial_number_of_fnp(model), - "sram_size": sram_size} - - params["num_cnp_tnp"] = NUM_NPS_PER_NODE * initial_num_nodes - params["num_fnp"] - if params["num_cnp_tnp"] < 0: - raise ValueError("Impossible to compute base device: " - f"the number of initial nodes ({initial_num_nodes}) is not enough.") - device = akida.create_device(**params) - - # Map model with the default parameters. - model.map(device, mode=akida.mapping.MapMode.Minimal, hw_only=True) - - # Now that the model has been mapped onto the base device, - # we can compute the parameters to build the required device. - if enable_hwpr: - params["num_cnp_tnp"] = params["num_fnp"] = 0 - for layer_seq in _model_generator(model.layers): - # Compute the number of CNP/FNP needed to map the model in multiple passes, - # as the larger sum of 2 consecutive layers. - params["num_cnp_tnp"] = max(params["num_cnp_tnp"], compute_number_of_cnp_tnp(layer_seq)) - params["num_fnp"] = max(params["num_fnp"], compute_number_of_fnp(layer_seq)) - # To compute the minimum number of skip DMA channels needed when partial reconfiguration - # is allowed, we iterate the device until we find a valid one. - for num_skip_dma_channel in range(1, params.pop("num_skip_dma_channel") + 1): - try: - device = akida.create_device(num_skip_dma_channel=num_skip_dma_channel, **params) - model.map(device, mode=akida.mapping.MapMode.Minimal, hw_only=True) - params["num_skip_dma_channel"] = num_skip_dma_channel - break - except Exception: - continue - else: - params["num_cnp_tnp"] = compute_number_of_cnp_tnp(model) - params["num_fnp"] = compute_number_of_fnp(model) - params["num_skip_dma_channel"] = compute_skip_dma_channels(model) - - if minimal_memory: - if sram_size is not None: - warnings.warn( - "The 'sram_size' argument will be ignored because 'minimal_memory' is set to True. " - "The required memory will be computed automatically." - ) - params["sram_size"] = akida.NP.SramSize(*akida.compute_minimal_memory(model)) - - # Create a virtual device with the requirements. - device = akida.create_device(**params) - - # Sanity check: map model on device. - try: - model.map(device, mode=akida.mapping.MapMode.Minimal, hw_only=True) - except Exception as e: - raise RuntimeError("It was not possible to find a device for this model. " - f"Reason:\n{str(e)}") - return device - - -def compute_common_device(ak_models): - """Computes a common Akida device that can run all the given models. - Ensures all models were mapped. - - Args: - ak_models (List[akida.Model]): A list of Akida models whose hardware - requirements will be combined. - - Returns: - akida.Device: A new device that can map all the given models. - """ - if not ak_models: - raise ValueError("The list of Akida models cannot be empty.") - - if any(model.device is None for model in ak_models): - raise ValueError("All models must be mapped on a device.") - - # For safety, check that all models devices have the same version - assert all(model.device.version == ak_models[0].device.version for model in ak_models), \ - "Models devices have different versions." - - include_hrc = any(model.device.mesh.has_hrc for model in ak_models) - max_num_cnp_tnp = 0 - max_num_fnp = 0 - max_num_skip_dma_channel = 0 - sram_size = akida.NP.SramSize(0, 0) - - for model in ak_models: - # Update params - max_num_cnp_tnp = max(max_num_cnp_tnp, compute_number_of_cnp_tnp(model)) - max_num_fnp = max(max_num_fnp, compute_number_of_fnp(model)) - max_num_skip_dma_channel = max(max_num_skip_dma_channel, compute_skip_dma_channels(model)) - - # Update Sram size - sram_size = akida.NP.SramSize(max(sram_size.input_bytes, - model.device.mesh.np_sram_size.input_bytes), - max(sram_size.weight_bytes, - model.device.mesh.np_sram_size.weight_bytes)) - - return akida.create_device(max_num_cnp_tnp, max_num_fnp, - max_num_skip_dma_channel, include_hrc, - sram_size, ak_models[0].device.version) diff --git a/README.md b/README.md index d00f3ad..1ec49d2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ In addition, some models can be evaluated directly through [Akida Cloud](https:/ | Vision | Classification | [MobileNetV2 1.0](vision/classification/mobilenetv2/imagenet) | 224 | ImageNet | 3.5M | 8 | 70.35% | | | 7 | | Vision | Classification | [MobileNetV2 0.75](vision/classification/mobilenetv2/imagenet) | 160 | ImageNet | 2.6M | 8 | 62.85% | | | 4 ☁️ | | Vision | Classification | [MobileNetV2 0.35](vision/classification/mobilenetv2/imagenet) | 96 | ImageNet | 1.2M | 8 | 43.47% | | | 2 ☁️ | +| Vision | Classification | [MobileNetV4 1.0](vision/classification/mobilenetv4/imagenet) | 224 | ImageNet | 3.77M | 8 | 71.86% | | | 8 | | Vision | Classification | [MobileNetV2_1.0](vision/classification/mobilenetv2/cifar10) | 128 | CIFAR-10 | 2.25M | 8 | 93.96% | | | 5 ☁️ | | Vision | Classification | [MobileNetV2_1.0](vision/classification/mobilenetv2/oxford_flowers) | 224 | Oxford_Flower | 2.4M | 8 | 91.97% | | | 7 | | Vision | Classification | [MobileNetV4_1.0](vision/classification/mobilenetv4/cifar10) | 128 | CIFAR-10 | 2.5M | 8 | 94.72% | | | 7 | diff --git a/vision/classification/mobilenetv4/imagenet/README.md b/vision/classification/mobilenetv4/imagenet/README.md new file mode 100644 index 0000000..7e1968a --- /dev/null +++ b/vision/classification/mobilenetv4/imagenet/README.md @@ -0,0 +1,19 @@ +# MobileNetV4-Conv-Small + +## Source +https://huggingface.co/timm/mobilenetv4_conv_small.e2400_r224_in1k + +## Environment +``` +torch: 2.0 +torchvision +timm +quantizeml: 1.1.1 +``` + +## References +- **MobileNet-v4** Model from the paper [Exploring the MobileNetV4 Architecture](https://arxiv.org/abs/2404.10518) +- Model Card [MobileNetV4-Conv-Small](https://huggingface.co/timm/mobilenetv4_conv_small.e2400_r224_in1k) + +## License +Please refer to [https://huggingface.co/timm/mobilenetv4_conv_small.e2400_r224_in1k](https://huggingface.co/timm/mobilenetv4_conv_small.e2400_r224_in1k) diff --git a/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224.onnx b/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224.onnx new file mode 100644 index 0000000..5d58144 --- /dev/null +++ b/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d4ed1e2a376df61176462a27e8cbaf041412c35b12675e3a90a64804a6a1b8c +size 15069033 diff --git a/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224_i8_w8_a8.onnx b/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224_i8_w8_a8.onnx new file mode 100644 index 0000000..b19f3ed --- /dev/null +++ b/vision/classification/mobilenetv4/imagenet/mobilenet_v4_small_224_i8_w8_a8.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5661433b49c9f2cf6cbb175b0c2c74c72866cc485e59d142bf76178027a95785 +size 4061699