Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bench_cmsis_dsp_nn_kernels' into…
Browse files Browse the repository at this point in the history
… isaac-demo
  • Loading branch information
PhilippvK committed Jan 28, 2025
2 parents 1f929db + 351bd8c commit 6253077
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mlonmcu/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
MibenchFrontend,
LayerGenFrontend,
OpenASIPFrontend,
CmsisDSPFrontend,
CmsisNNFrontend,
)

SUPPORTED_FRONTENDS = {
Expand All @@ -53,6 +55,8 @@
"mibench": MibenchFrontend,
"layergen": LayerGenFrontend,
"openasip": OpenASIPFrontend,
"cmsis_dsp": CmsisDSPFrontend,
"cmsis_nn": CmsisNNFrontend,
} # TODO: use registry instead

__all__ = [
Expand Down
86 changes: 86 additions & 0 deletions mlonmcu/models/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
MathisProgram,
MibenchProgram,
OpenASIPProgram,
CmsisDSPProgram,
CmsisNNProgram,
)
from mlonmcu.models.lookup import lookup_models
from mlonmcu.feature.type import FeatureType
Expand Down Expand Up @@ -2142,3 +2144,87 @@ def get_platform_config(self, platform):
if platform == "mlif":
ret["template"] = "openasip"
return ret


class CmsisDSPFrontend(SimpleFrontend):

def __init__(self, features=None, config=None):
super().__init__(
"cmsis_dsp",
ModelFormats.NONE,
features=features,
config=config,
)

@property
def supported_names(self):
return [
"arm_abs_q15",
"arm_abs_q31",
]

def lookup_models(self, names, config=None, context=None):
ret = []
for name in names:
name = name.replace("cmsis_dsp/", "")
if name in self.supported_names:
hint = CmsisDSPProgram(
name,
alt=f"cmsis_dsp/{name}",
config=config,
)
ret.append(hint)
return ret

def generate(self, model) -> Tuple[dict, dict]:
artifacts = [Artifact("dummy_model", raw=bytes(), fmt=ArtifactFormat.RAW, flags=["model", "dummy"])]

return {"default": artifacts}, {}

def get_platform_config(self, platform):
ret = {}
if platform == "mlif":
ret["template"] = "cmsis_dsp_bench"
return ret


class CmsisNNFrontend(SimpleFrontend):

def __init__(self, features=None, config=None):
super().__init__(
"cmsis_nn",
ModelFormats.NONE,
features=features,
config=config,
)

@property
def supported_names(self):
return [
"arm_nn_activation_s16_tanh",
"arm_nn_activation_s16_sigmoid",
]

def lookup_models(self, names, config=None, context=None):
ret = []
for name in names:
name = name.replace("cmsis_nn/", "")
if name in self.supported_names:
hint = CmsisNNProgram(
name,
alt=f"cmsis_nn/{name}",
config=config,
)
ret.append(hint)
return ret

def generate(self, model) -> Tuple[dict, dict]:
artifacts = [Artifact("dummy_model", raw=bytes(), fmt=ArtifactFormat.RAW, flags=["model", "dummy"])]

return {"default": artifacts}, {}

def get_platform_config(self, platform):
ret = {}
if platform == "mlif":
ret["template"] = "cmsis_nn_bench"
return ret
88 changes: 88 additions & 0 deletions mlonmcu/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,91 @@ def get_platform_defs(self, platform):
if self.name == "crc":
ret["OPENASIP_CRC_MODE"] = self.crc_mode
return ret


class CmsisDSPProgram(Program):
DEFAULTS = {
"size": 16, # hoch much data to operate on
"batch_size": 16, # num_batches=ceil(size/batch_size)
"number": 10, # how often to repeat (TODO: use existing bench feature)
}

@property
def size(self):
value = self.config["size"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return value

@property
def batch_size(self):
value = self.config["batch_size"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return min(value, self.size) # batch size should not exceed size

@property
def number(self):
value = self.config["number"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return value

def get_platform_defs(self, platform):
ret = {}
if platform == "mlif":
ret["CMSIS_DSP_BENCHMARK"] = self.name
ret["CMSIS_DSP_SIZE"] = self.size
ret["CMSIS_DSP_BATCH_SIZE"] = self.batch_size
ret["CMSIS_DSP_NUMBER"] = self.number
return ret


class CmsisNNProgram(Program):
DEFAULTS = {
"size": 16, # hoch much data to operate on
"batch_size": 16, # num_batches=ceil(size/batch_size)
"number": 10, # how often to repeat (TODO: use existing bench feature)
}

@property
def size(self):
value = self.config["size"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return value

@property
def batch_size(self):
value = self.config["batch_size"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return min(value, self.size) # batch size should not exceed size

@property
def number(self):
value = self.config["number"]
if isinstance(value, str):
value = int(value)
assert isinstance(value, int)
assert value > 0
return value

def get_platform_defs(self, platform):
ret = {}
if platform == "mlif":
ret["CMSIS_NN_BENCHMARK"] = self.name
ret["CMSIS_NN_SIZE"] = self.size
ret["CMSIS_NN_BATCH_SIZE"] = self.batch_size
ret["CMSIS_NN_NUMBER"] = self.number
return ret

0 comments on commit 6253077

Please sign in to comment.