8000 Introduce Apple BNNS backend by apeskov · Pull Request #7299 · apache/tvm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 27 commits into from
Mar 12, 2021
Merged

Introduce Apple BNNS backend #7299

merged 27 commits into from
Mar 12, 2021

Conversation

apeskov
Copy link
Contributor
@apeskov apeskov commented Jan 16, 2021

This is simple JSON based runtime which offload execution of some operation into Accelerate frameworks via BNNS api.

Works only for next Apple platforms:

  • macOS 11.0 and later
  • iOS 14.0 and later

List of supported primitives:

  • conv2d and fusing with bias and relu
  • dense and fusing with bias and relu/gelu
  • batch_matmul

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.

Copy link
Contributor
@leandron leandron left a 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.

Comment on lines 150 to 189
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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Comment on lines 281 to 307
Parameters
----------
space: List[List[Any]]
A list of different options with varying values to test.
r_factor: (optional) int
The repeat factor.

Copy link
Contributor

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.

@FrozenGene
Copy link
Member

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!

@echuraev echuraev force-pushed the ap/bnns-runtime branch 4 times, most recently from cf50a8f to 89a4bee Compare February 25, 2021 09:18
apeskov and others added 22 commits February 26, 2021 00:30
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
@apeskov apeskov marked this pull request as ready for review February 26, 2021 19:07
@jwfromm
Copy link
Contributor
jwfromm commented Mar 1, 2021

@FrozenGene @leandron can you guys take another look at this PR? It seems to have come a long way in the last month.

Copy link
Contributor
@leandron leandron left a 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.

Comment on lines 72 to 73
with tvm.transform.PassContext(opt_level=3):
model = partition_for_bnns(model, params=params)
Copy link
Contributor

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.

Copy link
Contributor

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.

Comment on lines 238 to 239
if skip_codegen_test():
return
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
Copy link
Contributor

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?

Copy link
Contributor

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):
Copy link
Contributor

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:

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,
},
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@echuraev
Copy link
Contributor
echuraev commented Mar 5, 2021

@FrozenGene @leandron could you please take a look on this PR one again?

----------------------------------------

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opps -> ops

Copy link
Contributor

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.


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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as with -> as

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



Input data layout for operations to be offloaded to BNNS execution
----------------------------------------
Copy link
Member

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

Copy link
Contributor

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(),
Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Contributor
@leandron leandron left a 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.

Comment on lines 37 to 42
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.
Copy link
Contributor

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?

Copy link
Contributor

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.

@jwfromm jwfromm merged commit 68b81ad into apache:main Mar 12, 2021
@jwfromm
Copy link
Contributor
jwfromm commented Mar 12, 2021

Thanks @apeskov, @FrozenGene, @leandron, and @echuraev. This is now merged and will be an awesome addition to TVM!

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request May 6, 2021
* 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>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request May 11, 2021
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0