Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2020-2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 16 | # |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 17 | # Description: |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 18 | # Mark purpose and select formats for Tensors. |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 19 | from .graph_optimiser_util import memory_only_ops |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 20 | from .operation import CustomType |
| 21 | from .operation import Op |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 22 | from .rewrite_graph import visit_graph_post_order |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 23 | from .tensor import MemType |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 24 | from .tensor import TensorFormat |
| 25 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 26 | |
| 27 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 28 | def get_format(purpose, arch): |
Fredrik Svedberg | e22ba8c | 2021-01-27 16:53:41 +0100 | [diff] [blame] | 29 | if purpose in (TensorPurpose.FeatureMap, TensorPurpose.LUT, TensorPurpose.Scratch, TensorPurpose.ScratchFast): |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 30 | fmt = arch.default_feature_map_format |
| 31 | elif purpose == TensorPurpose.Weights: |
| 32 | fmt = arch.default_weight_format |
| 33 | elif purpose == TensorPurpose.Unknown: |
| 34 | fmt = TensorFormat.Unknown |
| 35 | else: |
| 36 | assert 0, "unknown tensor purpose {}".format(purpose) |
| 37 | return fmt |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 38 | |
| 39 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 40 | def mark_purpose(tens, arch, purpose): |
| 41 | # Sets tensor's purpose, format, mem_area and mem_type |
| 42 | if tens.purpose == TensorPurpose.Unknown: |
| 43 | tens.purpose = purpose |
| 44 | elif tens.purpose not in (purpose, TensorPurpose.LUT): |
| 45 | assert 0, "Cannot resolve tensor purpose {} and {} for tensor {}".format(tens.purpose, purpose, tens) |
Patrik Gustavsson | ee99bb1 | 2021-04-08 09:04:00 +0200 | [diff] [blame] | 46 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 47 | fmt = get_format(purpose, arch) |
| 48 | tens.set_format(fmt, arch) |
| 49 | tens.mem_area = arch.tensor_storage_mem_area[tens.purpose] |
| 50 | tens.mem_type = arch.tensor_storage_mem_type[tens.purpose] |
| 51 | |
Fredrik Svedberg | e22ba8c | 2021-01-27 16:53:41 +0100 | [diff] [blame] | 52 | if ( |
| 53 | len(tens.ops) == 1 |
| 54 | and tens.ops[0].type == Op.Const |
| 55 | and purpose not in (TensorPurpose.Scratch, TensorPurpose.ScratchFast) |
| 56 | ): |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 57 | tens.mem_area = arch.permanent_storage_mem_area # special case constants, as they must be in permanent storage |
| 58 | tens.mem_type = MemType.Permanent_NPU |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 59 | |
| 60 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 61 | def rewrite_mark_tensor_purpose(op, arch): |
| 62 | # find disconnected outputs and mark as feature maps |
| 63 | for tens in op.outputs: |
| 64 | if not tens.consumers(): |
| 65 | mark_purpose(tens, arch, TensorPurpose.FeatureMap) |
| 66 | weight_tensors = op.get_weight_tensors() |
| 67 | for tens in op.inputs: |
Louis Verhaard | 0411edb | 2020-11-16 16:37:11 +0100 | [diff] [blame] | 68 | if tens is None: |
| 69 | continue |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 70 | if tens.purpose != TensorPurpose.Unknown: |
| 71 | purpose = tens.purpose |
| 72 | elif tens in weight_tensors: |
| 73 | purpose = TensorPurpose.Weights |
| 74 | else: |
| 75 | purpose = TensorPurpose.FeatureMap |
| 76 | mark_purpose(tens, arch, purpose) |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 77 | if op.type in memory_only_ops: |
| 78 | # Memory only operator input and output point to same data |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 79 | op.ofm.mem_area = op.ifm.mem_area |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 80 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 81 | if op.type == Op.Custom and op.attrs.get("custom_type") == CustomType.ExistingNpuOp: |
| 82 | scratch_tensor = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 83 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 84 | if len(op.inputs) >= 3: |
| 85 | scratch_tensor = op.inputs[2] # should be existing scratch tensor |
| 86 | if scratch_tensor.name.endswith("_scratch"): |
| 87 | scratch_tensor.purpose = TensorPurpose.Scratch |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 88 | |
Fredrik Svedberg | e22ba8c | 2021-01-27 16:53:41 +0100 | [diff] [blame] | 89 | if len(op.inputs) >= 4: |
| 90 | scratch_fast_tensor = op.inputs[3] # should be existing scratch fast tensor |
| 91 | if scratch_fast_tensor.name.endswith("_scratch_fast"): |
| 92 | scratch_fast_tensor.purpose = TensorPurpose.ScratchFast |
| 93 | |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 94 | if scratch_tensor is None: |
Michael McGeagh | 528a56d | 2020-12-16 11:33:21 +0000 | [diff] [blame] | 95 | op.error("Scratch tensor not found.") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def mark_tensor_purpose(nng, arch, verbose_tensor_purpose=False): |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 99 | # Sets purpose, format, mem_area and mem_type for all tensors in the graph |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 100 | for sg in nng.subgraphs: |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 101 | visit_graph_post_order(sg.output_tensors, arch, [], [rewrite_mark_tensor_purpose]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 102 | for tens in sg.output_tensors: |
Louis Verhaard | 17afa28 | 2020-10-14 08:32:41 +0200 | [diff] [blame] | 103 | mark_purpose(tens, arch, TensorPurpose.FeatureMap) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 104 | |
| 105 | if verbose_tensor_purpose: |
| 106 | nng.print_graph_with_tensors() |
| 107 | |
| 108 | return nng |