-
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
[5/6] Arm(R) Ethos(TM)-U NPU codegen integration #8849
Merged
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 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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# 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. | ||
"""Codegen for Arm(R) Ethos(TM)-U""" | ||
import tvm | ||
from tvm import relay | ||
from tvm.relay.backend.contrib.ethosu.tir.compiler import lower_to_tir | ||
from tvm.relay.backend.contrib.ethosu.tir.scheduler import copy_constants | ||
from tvm.relay.backend.contrib.ethosu.legalize import LegalizeEthosU | ||
from tvm.relay.backend.contrib.ethosu import tir_to_cs_translator | ||
from tvm.relay.backend.contrib.ethosu import util | ||
|
||
|
||
@tvm._ffi.register_func("relay.ext.ethosu.constant_updater") | ||
def constant_updater(expr, symbol): # pylint: disable=unused-argument | ||
""" | ||
We dont want the build process to extract constants to be loaded in | ||
the runtime as we are embedding them inside the C runtime.Module. | ||
""" | ||
return dict() | ||
|
||
|
||
@tvm._ffi.register_func("relay.ext.ethosu") | ||
def ethosu_compiler(ref): | ||
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. Update argument name/improve doc string here? |
||
"""Main function to a compile a given relay function of | ||
NPU compatible operators to generated command stream. | ||
Such generated command stream would be loaded to the runtime | ||
module that interfaces with NPU driver. | ||
""" | ||
assert isinstance(ref, tvm.ir.function.BaseFunc) | ||
func_name = ref.attrs["global_symbol"] | ||
# There should only be a single input | ||
assert len(ref.params) == 1 | ||
input_size = util.calculate_size_bytes(ref.params[0]) | ||
output_size = util.calculate_size_bytes(ref.body) | ||
cmms, encoded_constants, scratch_size = _compile(ref) | ||
ethosu_runtime = tvm._ffi.get_global_func("runtime.module.ethosu.create") | ||
return ethosu_runtime(func_name, cmms, encoded_constants, scratch_size, input_size, output_size) | ||
|
||
|
||
def _compile(ext_func): | ||
""" | ||
This is the main wrapper that accepts an external | ||
relay function and runs all the passes to lower it down | ||
to command stream | ||
Parameters | ||
---------- | ||
ext_func : tvm.relay.function.Function | ||
The partitioned relay function | ||
Returns | ||
------- | ||
cs : str | ||
An hex string of the bytes of command stream | ||
encoded_constants : str | ||
An hex string of the bytes that includes concat'd | ||
encoded weights, encoded biases and scales. | ||
scratch_size : int | ||
The size of the scratch buffer needed. | ||
""" | ||
mod = tvm.IRModule() | ||
mod["main"] = ext_func | ||
mod = LegalizeEthosU()(mod) | ||
mod = relay.transform.InferType()(mod) | ||
# We are currently using copy_constants scheduler In the long run, | ||
# this should be a single intelligent and a composite scheduler | ||
# that can perform scheduling based on user inputs such as | ||
# scratch memory size. | ||
tir_mod, params = lower_to_tir(mod["main"], copy_constants()) | ||
cmms, encoded_constants, scratch_size = tir_to_cs_translator.translate(tir_mod, params) | ||
return cmms, encoded_constants, scratch_size |
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
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.
nit: move below main entry point and comment it is called back from utils.h UpdateConstants after lowering.