-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[microNPU] Add various options to the cascader #10509
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,15 @@ | |
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name | ||
# pylint: disable=too-many-nested-blocks | ||
"""Device config class to hold information about the target hardware""" | ||
from typing import Tuple, List, Dict, Optional | ||
from functools import reduce | ||
|
||
import math | ||
import numpy as np | ||
|
||
import tvm | ||
from . import BlockConfig | ||
from . import StripeConfig | ||
from . import Propagator | ||
|
@@ -64,13 +67,14 @@ def as_list(self): | |
class EthosuDeviceConfig: | ||
"""Arm(R) Ethos(TM)-U NPU config class""" | ||
|
||
def __init__(self, device: str): | ||
def __init__(self, device: str, disable_block_bulling: bool = False): | ||
self._device = device | ||
self._subkernel_limits = (8, 8) | ||
self._output_cycles = (1, 2, 3, 4, 6) | ||
self._split_depth = 16 | ||
self._max_block_shape = _Shape([1, 32, 64, 128]) | ||
self._bank_size_bytes = 1024 | ||
self._disable_block_culling = disable_block_bulling | ||
if self._device == "ethos-u55-256": | ||
self._micro_block = _Shape([1, 2, 2, 8]) | ||
self._input_micro_block = _Shape([1, 2, 2, 8]) | ||
|
@@ -508,6 +512,28 @@ def get_elementwise_block_config( | |
if activation == "LUT" and not self._lut_reserved: | ||
banks_available -= 2 | ||
|
||
# Handle user-forced block config | ||
options = tvm.transform.PassContext.current().config.get("relay.ext.ethos-u.options", None) | ||
if options and options.dev_force_block_config: | ||
block_config = [int(v) for v in options.dev_force_block_config.split("x")] | ||
assert len(block_config) == 3 | ||
if output_layout == "NHWC": | ||
block_shape = [output_shape[0], block_config[0], block_config[1], block_config[2]] | ||
else: | ||
block_shape = [ | ||
output_shape[0], | ||
block_config[0], | ||
1 + ((block_config[2] - 1) // 16), | ||
block_config[1], | ||
16, | ||
] | ||
output_cycles = self._get_output_cycles( | ||
op_type, op_str, ifm_dtype, ofm_dtype, activation | ||
) | ||
output_cycles *= reduce(lambda a, b: a * b, block_shape, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's probably simpler, I will change it. |
||
output_cycles = int(math.ceil(output_cycles)) | ||
return [BlockConfig(block_shape, block_shape, 0, output_cycles)] | ||
|
||
# Split the block in half until it fits into SHRAM | ||
max_height, max_width, max_depth = self._max_block_shape.as_list()[1:] | ||
if output_layout == "NHCWB16": | ||
|
@@ -666,6 +692,21 @@ def get_valid_block_configs( | |
max_depth = min(ofm_channels, self._max_block_shape.depth) | ||
min_depth = max(self._micro_block.depth, upscaling_factor) | ||
|
||
heights = range(min_height, max_height + min_height, min_height) | ||
widths = range(min_width, max_width + min_width, min_width) | ||
depths = range(min_depth, max_depth + min_depth, min_depth) | ||
|
||
# Handle user-forced block config | ||
options = tvm.transform.PassContext.current().config.get("relay.ext.ethos-u.options", None) | ||
forced = False | ||
if options and options.dev_force_block_config: | ||
block_config = [int(v) for v in options.dev_force_block_config.split("x")] | ||
assert len(block_config) == 3 | ||
heights = [block_config[0]] | ||
widths = [block_config[1]] | ||
depths = [block_config[2]] | ||
forced = True | ||
|
||
input_bytewidth = 1 if ifm_dtype == "int8" else 2 | ||
acc_bytewidth = self._get_accumulator_width(op_type, ifm_dtype) | ||
banks_available = self._total_banks - self._reserved_banks | ||
|
@@ -681,26 +722,24 @@ def get_valid_block_configs( | |
else: | ||
input_block_depth = min(ifm_channels, 32) | ||
|
||
for depth in range(min_depth, max_depth + min_depth, min_depth): | ||
if (depth < output_shape.depth) and (depth % self._split_depth != 0): | ||
for depth in reversed(depths): | ||
if (depth < output_shape.depth) and (depth % self._split_depth != 0) and not forced: | ||
# Block depth has to be less than full depth or a multiple of the split depth | ||
continue | ||
|
||
subkernel_propagator = self._get_subkernel_propagator( | ||
op_attrs, ifm_propagator, input_layout, output_layout, depth | ||
) | ||
|
||
for width in range(min_width, max_width + min_width, min_width): | ||
for height in range(min_height, max_height + min_height, min_height): | ||
for width in reversed(widths): | ||
for height in reversed(heights): | ||
if output_layout == "NHCWB16": | ||
output_block = ( | ||
1, | ||
height, | ||
1 + ((depth - 1) // 16), | ||
width, | ||
_round_up( | ||
min(16, max(ofm_channels, min_depth)), self._micro_block.depth | ||
), | ||
min(16, _round_up(ofm_channels, self._micro_block.depth)), | ||
) | ||
order = [1, 2, 4, 3, 0] | ||
else: | ||
|
@@ -740,7 +779,7 @@ def get_valid_block_configs( | |
output_cycles = self._get_output_cycles( | ||
op_type, op_str, ifm_dtype, ofm_dtype, activation | ||
) | ||
output_cycles *= reduce(lambda a, b: a * b, output_block, 1) | ||
output_cycles *= np.prod(output_block).tolist() | ||
output_cycles = int(math.ceil(output_cycles)) | ||
compute_cycles = self._estimate_compute_cycles_per_block( | ||
op_type, | ||
|
@@ -755,11 +794,27 @@ def get_valid_block_configs( | |
block_config = BlockConfig( | ||
input_block_shape.as_list(), output_block, compute_cycles, output_cycles | ||
) | ||
valid_block_configs.append(block_config) | ||
else: | ||
# Block config does not fit into SHRAM | ||
# Any Block config that is strictly larger than this one will also fail | ||
break | ||
|
||
if self._disable_block_culling: | ||
# Block culling disabled - add all block configs that fit | ||
valid_block_configs.append(block_config) | ||
else: | ||
# Add block config only if it's not dominated by an existing block. | ||
# A block config is dominated by another if its output_shape is greater | ||
# or equal in every dimension and strictly greater in at least one | ||
# dimension. | ||
dominated = False | ||
for valid_block in valid_block_configs: | ||
if block_config < valid_block: | ||
dominated = True | ||
break | ||
|
||
if not dominated: | ||
valid_block_configs.append(block_config) | ||
|
||
# Every consecutive block in the innermost loop will be dominated by | ||
# this one so break | ||
break | ||
|
||
return valid_block_configs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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. | ||
"""A class to hold logging information about the cascader""" | ||
from typing import Tuple | ||
import datetime | ||
import json | ||
import os | ||
import math | ||
|
||
|
||
class Logging: | ||
"""Cascader logging class""" | ||
|
||
def __init__(self): | ||
self.min_memory_usage = 0 | ||
self.max_memory_usage = 0 | ||
self.min_cycles = 0 | ||
self.max_cycles = 0 | ||
|
||
self.selected_proposal_idx = -1 | ||
self.proposals = {} | ||
self.cascader_runtime = 0 | ||
|
||
def add_proposal(self, idx: int, memory_usage: int, cycles: int): | ||
self.proposals[idx] = {"memory_usage": memory_usage, "cycles": cycles} | ||
|
||
def get_extreme_points(self) -> Tuple[int, int, int, int]: | ||
min_cycles, min_mem_usage = math.inf, math.inf | ||
max_cycles, max_mem_usage = 0, 0 | ||
for proposal in self.proposals.values(): | ||
min_mem_usage = min(proposal["memory_usage"], min_mem_usage) | ||
max_mem_usage = max(proposal["memory_usage"], max_mem_usage) | ||
min_cycles = min(proposal["cycles"], min_cycles) | ||
max_cycles = max(proposal["cycles"], max_cycles) | ||
|
||
return min_mem_usage, max_mem_usage, min_cycles, max_cycles | ||
|
||
def dump_json(self): | ||
min_mem_usage, max_mem_usage, min_cycles, max_cycles = self.get_extreme_points() | ||
with open(os.getcwd() + "/cascader_log.json", "w") as json_file: | ||
print( | ||
json.dumps( | ||
{ | ||
"date": f"{datetime.datetime.now()}", | ||
"cascader_runtime": self.cascader_runtime, | ||
"min_cycles": min_cycles, | ||
"max_cycles": max_cycles, | ||
"min_memory_usage": min_mem_usage, | ||
"max_memory_usage": max_mem_usage, | ||
"selected_proposal": self.selected_proposal_idx, | ||
"proposals": self.proposals, | ||
}, | ||
indent=2, | ||
), | ||
file=json_file, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it supposed to be similar to the comparison in
__ge__
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I thought the easiest way to define strict less than in this case was as the inverse of greater or equal, i.e.
__ge__
.