-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[TVMC][microNPU] tvmc option for printing which operators are offloaded to Ethos-U #13212
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
Merged
lhutton1
merged 2 commits into
apache:main
from
sergio-grovety:tvmc-ethosu-dump-npu-functions-coverage-option
Mar 27, 2023
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,102 @@ | ||
# 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. | ||
"""Utilities that enable analyze Relay and get mappings for | ||
the unique identifier of the Relay line to the tuple of | ||
compiler name, composite name and composite/function identifier.""" | ||
import tvm | ||
from tvm import relay | ||
from tvm.relay.expr_functor import ExprVisitor | ||
|
||
|
||
class AnalyzeOperationsDistribution(ExprVisitor): | ||
"""A visitor pass that maintains the dictionary unique_op_ids where | ||
the tuple (compiler name, composite name, composite/function identifier) | ||
corresponds to the unique identifier of the Relay line. | ||
TVMC compiler adds a unique Relay line identifier as a suffix | ||
to the call span field using the tag_suffixes pass | ||
if the --dump-offloads option is specified. | ||
|
||
Attributes | ||
---------- | ||
unique_op_ids : Dict[str, str, int] | ||
Mapping the unique identifier of the Relay line obtained from | ||
the "span" field of the Call and the tuple of compiler name, | ||
composite name and internal composite/function identifier. | ||
func_name : str | ||
The name of the composite name in the partitioned Relay or | ||
'generic' in case the Call has not been included in any composite. | ||
func_id : int | ||
Internal(inside unique_op_ids) composite/function identifier. | ||
compiler_name : str | ||
A name of the compiler (e.g. 'ethos-u' or 'cmsis-nn') or 'generic' | ||
in case the Call has not been included in any composite. | ||
""" | ||
|
||
def __init__(self): | ||
self.unique_op_ids = {} | ||
self.func_name = "" | ||
self.func_id = 1 | ||
self.compiler_name = "" | ||
super().__init__() | ||
|
||
def extract(self, call: relay.Call): | ||
self.compiler_name = "generic" | ||
self.func_name = "generic" | ||
if "Compiler" in call.attrs: | ||
self.compiler_name = call.attrs["Compiler"] | ||
self.visit(call) | ||
|
||
def visit_call(self, call: relay.Call): | ||
if isinstance(call.op, tvm.ir.Op): | ||
if call.span: | ||
src = call.span.source_name.name | ||
self.unique_op_ids[src] = [self.compiler_name, self.func_name, self.func_id] | ||
if self.func_name == "generic": | ||
self.func_id += 1 | ||
if isinstance(call.op, relay.Function): | ||
self.func_name = call.op.attrs["Composite"] | ||
self.func_id += 1 | ||
super().visit_call(call) | ||
|
||
|
||
def analyze_operations_distribution(mod): | ||
"""Traverses the partitioned graph to get the unique identifier | ||
of the Relay line from the Call's span field. | ||
The result is maintained in the dictionary unique_op_ids where | ||
the unique indicator obtained from the op's span corresponds to | ||
the tuple (compiler name, composite name, composite/function identifier). | ||
With this information we can annotate the textual representation | ||
of the initial Relay by indicating into which target composite | ||
and function the operators are converted | ||
|
||
Parameters | ||
---------- | ||
mod : tvm.ir.IRModule | ||
The partitioned Relay graph usually obtained with | ||
partition_for_<target> function | ||
|
||
Returns | ||
------- | ||
unique_op_ids : Dict[str, str, int] | ||
Mapping from the unique identifier of the Relay line to the tuple of | ||
compiler name, composite name, internal composite/function | ||
identifier. | ||
""" | ||
analyze = AnalyzeOperationsDistribution() | ||
for _, func in mod.functions.items(): | ||
analyze.extract(func) | ||
return analyze.unique_op_ids |
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.
Uh oh!
There was an error while loading. Please reload this page.