-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Introduce Apple BNNS backend #7299
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
Conversation
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.
Reviewed mostly the Python sources.
python/tvm/relay/op/contrib/bnns.py
Outdated
if len(b_shape) == 4: | ||
return bool(b_shape[0] == 1 and b_shape[2] == 1 and b_shape[3] == 1) | ||
elif len(b_shape) == 3: | ||
return bool(b_shape[1] == 1 and b_shape[2] == 1) |
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.
Why do we need these bool()
calls here after the expression?
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.
b_shape[0] == 1
is an instance of tvm.tir.expr.IntImm
. External consumer of this function expect bool value and further "bool like" processing lead to exception.
This explicit cast was a shorter way to avoid this.
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.
Understood, please mark this one a closed.
Parameters | ||
---------- | ||
space: List[List[Any]] | ||
A list of different options with varying values to test. | ||
r_factor: (optional) int | ||
The repeat factor. | ||
|
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.
There is a mix of numpy-style docstrings and other formats (in other Python sources in this same PR as well). I think it would be better to make it consistent, as this is quite a big chunk of work.
Once one is picked, please check the other source files in this PR.
Hi @apeskov Thanks for your contribution. Could you add more introduction for Apple BNNS backend in your description? I think this will help others know more about it. Thanks! |
19a75c1
to
47dd249
Compare
cf50a8f
to
89a4bee
Compare
This is simple JSON based runtime which offload execution of some operation into Accelerate frameworks via BNNS api. Works only for: * macOS 11.0 and later * iOS 14.0 and later Supported primitives: * conv2d and fusing with bias and relu * dense and fusing with bias and relu/gelu * batch_matmul Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Also fix some pylint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Signed-off-by: Alexander Peskov <peskovnn@gmail.com>
Supports `nn.max_pool2d`, `nn.avg_pool2d`, `nn.global_max_pool2d` and `nn.global_avg_pool2d` operations
89a4bee
to
d444846
Compare
@FrozenGene @leandron can you guys take another look at this PR? It seems to have come a long way in the last month. |
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.
In general it looks OK to me. A few comments on the example snippets, plus kindly asking for TVMC support.
docs/deploy/bnns.rst
Outdated
with tvm.transform.PassContext(opt_level=3): | ||
model = partition_for_bnns(model, params=params) |
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.
Does this need to be inside the PassContext
? Other targets don't required that.
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.
No, it is not required, thank you.
if skip_codegen_test(): | ||
return |
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.
the use of this idiom here, will make the test be marked as "passed" when BNNS is not available. Please check how to use the @pytest.mark.skipif
decorator.
This applies to all tests using this if
statement.
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.
Done
docs/deploy/bnns.rst
Outdated
target = "llvm -mtriple=x86_64-apple-darwin20.2.0" | ||
|
||
with tvm.transform.PassContext(opt_level=3): | ||
model = partition_for_bnns(model, params=params) # to markup operations to be offloaded to BNNS |
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.
This probably doesn't need to be nested into the PassContext
, can you check?
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.
You are right, thanks
from ...dataflow_pattern import wildcard, is_op, is_expr | ||
|
||
|
||
def partition_for_bnns(mod, params=None): |
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.
Do you mind also checking how to integrate this in TVMC, by binding this function into:
tvm/python/tvm/driver/tvmc/composite_target.py
Lines 34 to 43 in 51dc332
REGISTERED_CODEGEN = { | |
"compute-library": { | |
"config_key": None, | |
"pass_pipeline": partition_for_arm_compute_lib, | |
}, | |
"ethos-n77": { | |
"config_key": "relay.ext.ethos-n.options", | |
"pass_pipeline": partition_for_ethosn, | |
}, | |
} |
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.
Done
@FrozenGene @leandron could you please take a look on this PR one again? |
docs/deploy/bnns.rst
Outdated
---------------------------------------- | ||
|
||
Operations to be offloaded on BNNS execution must be annotated before passing of module for compilation. | ||
All opps annotated by `partition_for_bnns` will be offloaded for BNNS execution. The rest of the ops |
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.
opps -> ops
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.
Thank you, I fixed it.
docs/deploy/bnns.rst
Outdated
|
||
Important note: BNNS support primitives only with constant weights. To satisfy this requirements we have | ||
to map constants to related tensor abstraction in relay representation. To freeze tensors and operate | ||
with them as with constants you may need to call ONNX importer with special flag "freeze_params=True" |
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.
as with -> as
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.
Done
docs/deploy/bnns.rst
Outdated
|
||
|
||
Input data layout for operations to be offloaded to BNNS execution | ||
---------------------------------------- |
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.
I wonder whether we have problem if we don't align --------
with words, other places have same problem
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.
Done
transform.AnnotateTarget("bnns"), | ||
# If you no need in per layer performance statistic you can | ||
# uncomment next line | ||
# transform.MergeCompilerRegions(), |
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.
Does it have effect on end to end network performance?
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.
No, it doesn't have effect on performance. But if we use this transformation, then it will be more difficult to compare per layer statistic with initial topology due to some kernels will be merged.
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.
One minor nit. Apart from that LGTM.
Common device configuration for python tests. | ||
|
||
Check tests/python/contrib/arm_compute_lib/ for the presence of an test_config.json file. | ||
This file can be used to override the default configuration here which will attempt to run the Arm | ||
Compute Library runtime tests locally if the runtime is available. Changing the configuration | ||
will allow these runtime tests to be offloaded to a remote Arm device via a tracker for example. |
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.
Does this need some updating to refer to BNNS instead?
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.
Thank you. I updated this comment and double checked that we don't have any refers to ARC in BNNS tests.
Thanks @apeskov, @FrozenGene, @leandron, and @echuraev. This is now merged and will be an awesome addition to TVM! |
* Introduce Apple BNNS backend This is simple JSON based runtime which offload execution of some operation into Accelerate frameworks via BNNS api. Works only for: * macOS 11.0 and later * iOS 14.0 and later Supported primitives: * conv2d and fusing with bias and relu * dense and fusing with bias and relu/gelu * batch_matmul Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add conv2d DW test Also fix some pylint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix clang-format issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Refactoring. Add TView abstraction Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add several more onnx topologies into tests Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Avoid redundant tensor allocation Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix conv_splitter issue Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix isse with bias {1,1,1,1} Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Min. Rename file Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix review comments. Initial Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] test refactoring Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix cpplint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix clang-format issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix python format Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix pylint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix pylint. Second attempt Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add integration documentation * Check onnx import before use Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add instance normalization operator * Add fusing sigmoid activation after conv2d * min changes Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Add pooling operations to BNNS runtime Supports `nn.max_pool2d`, `nn.avg_pool2d`, `nn.global_max_pool2d` and `nn.global_avg_pool2d` operations * Fix lint * Fix lint * Apply comments * Fix documentation * Fix comment to refer to BNNS Co-authored-by: dlexplorer <elvin.nnov@gmail.com> Co-authored-by: Egor Churaev <egor.churaev@gmail.com>
* Introduce Apple BNNS backend This is simple JSON based runtime which offload execution of some operation into Accelerate frameworks via BNNS api. Works only for: * macOS 11.0 and later * iOS 14.0 and later Supported primitives: * conv2d and fusing with bias and relu * dense and fusing with bias and relu/gelu * batch_matmul Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add conv2d DW test Also fix some pylint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix clang-format issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Refactoring. Add TView abstraction Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add several more onnx topologies into tests Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Avoid redundant tensor allocation Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix conv_splitter issue Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix isse with bias {1,1,1,1} Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Min. Rename file Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix review comments. Initial Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] test refactoring Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix cpplint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix clang-format issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix python format Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Fix pylint issues Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Fix pylint. Second attempt Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add integration documentation * Check onnx import before use Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * [BNNS] Add instance normalization operator * Add fusing sigmoid activation after conv2d * min changes Signed-off-by: Alexander Peskov <peskovnn@gmail.com> * Add pooling operations to BNNS runtime Supports `nn.max_pool2d`, `nn.avg_pool2d`, `nn.global_max_pool2d` and `nn.global_avg_pool2d` operations * Fix lint * Fix lint * Apply comments * Fix documentation * Fix comment to refer to BNNS Co-authored-by: dlexplorer <elvin.nnov@gmail.com> Co-authored-by: Egor Churaev <egor.churaev@gmail.com>
This is simple JSON based runtime which offload execution of some operation into Accelerate frameworks via BNNS api.
Works only for next Apple platforms:
List of supported primitives:
Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from Reviewers by @ them in the pull request thread.