blob: 08dc0d38d6fa7d91a74fd4e038ae8588fcf83e8d [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Serialises and packs an NPU subgraph into tensors.
Diego Russoea6111a2020-04-14 18:41:58 +010018import struct
19
20import numpy as np
21
22from . import driver_actions
Tim Hall79d07d22020-04-27 18:20:16 +010023from .data_type import DataType
Diego Russoe8a10452020-04-21 17:39:10 +010024from .nn_graph import PassPlacement
25from .operation import Operation
26from .tensor import MemArea
27from .tensor import Tensor
28from .tensor import TensorFormat
29from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010030
31
32def make_memory_tensor(name, mem_area, sz, want_values, arch):
33 tens = Tensor([sz], DataType.uint8, name)
34 tens.mem_area = mem_area
35 tens.purpose = TensorPurpose.FeatureMap
36 tens.set_format(TensorFormat.NHWC, arch)
37 if want_values:
38 tens.values = np.zeros(tens.shape, np.uint8)
39 return tens
40
41
42def copy_compressed_values_to_memory_tensor(memory_tensor, src_tensor):
43 start_addr = src_tensor.address
44 for compressed_values in src_tensor.compressed_values:
45 end_addr = start_addr + len(compressed_values)
46 memory_tensor.values[start_addr:end_addr] = compressed_values
47 start_addr = end_addr
48
Tim Hallc30f4952020-06-15 20:47:35 +010049
Charles Xu78792222020-05-13 10:15:26 +020050def copy_ifm_values_to_memory_tensor(memory_tensor, src_tensor):
51 start_addr = src_tensor.address
52 end_addr = start_addr + src_tensor.quant_values.size
53 memory_tensor.values[start_addr:end_addr] = src_tensor.quant_values
Tim Hall79d07d22020-04-27 18:20:16 +010054
Tim Hallc30f4952020-06-15 20:47:35 +010055
Tim Hall79d07d22020-04-27 18:20:16 +010056def serialise_npu_subgraph_into_tensors(nng, sg, arch, scratch_tens, flash_tens):
57 if sg.placement != PassPlacement.Npu:
58 return scratch_tens, flash_tens
59
60 flash_area = arch.permanent_storage_mem_area
61 scratch_area = MemArea.Sram
62
63 flash_size = sg.memory_used.get(flash_area, 0)
64 scratch_size = sg.memory_used.get(scratch_area, 0)
65
66 # Prepare driver actions for this command tensor
67 da_list = []
68 driver_actions.emit_fourcc(da_list, "COP1")
69 driver_actions.emit_config(da_list, 0, 1, arch)
70 driver_actions.emit_cmd_stream_header(da_list, len(sg.register_command_stream))
71
72 # Append command stream words
73 da_list.extend(sg.register_command_stream)
74
75 # Convert to bytes
76 payload_bytes = struct.pack("<{0}I".format(len(da_list)), *da_list)
77
78 command_stream_size_bytes = len(payload_bytes)
79
80 # Adjust the bits per element calculation to exclude metadata generated by Vela
81 nng.total_size[flash_area] = nng.total_size.get(flash_area, 0) - flash_size - command_stream_size_bytes
82 nng.total_elements[flash_area] = nng.total_elements.get(flash_area, 0) - flash_size - command_stream_size_bytes
83 nng.total_size[scratch_area] = nng.total_size.get(scratch_area, 0) - scratch_size
84 nng.total_elements[scratch_area] = nng.total_elements.get(scratch_area, 0) - scratch_size
85
Diego Russoea6111a2020-04-14 18:41:58 +010086 if flash_tens == scratch_tens is None:
Tim Hall79d07d22020-04-27 18:20:16 +010087 # First Npu subgraph, create scratch and flash tensors
88 sg.scratch_tensor = make_memory_tensor(sg.name + "_scratch", scratch_area, scratch_size, False, arch)
89 sg.scratch_tensor.purpose = TensorPurpose.Scratch
90 sg.flash_tensor = make_memory_tensor(sg.name + "_flash", flash_area, flash_size, True, arch)
91 else:
92 sg.scratch_tensor = scratch_tens
93 sg.scratch_tensor.shape[0] += scratch_size
94 sg.flash_tensor = flash_tens
95 sg.flash_tensor.shape[0] += flash_size
96
97 for cps in sg.cascaded_passes:
98 for ps in cps.passes:
Charles Xu78792222020-05-13 10:15:26 +020099 if ps.placement == PassPlacement.Npu:
Tim Hallc30f4952020-06-15 20:47:35 +0100100 if ps.weight_tensor is not None:
Charles Xu78792222020-05-13 10:15:26 +0200101 # For DMA ops, ps.weight_tensor is referring to the SRAM weight tensor and therefore the address
102 # is pointing at the destination address of where the weights should be placed in SRAM.
103 # This ensures that the Flash weight tensor is used instead and thus gets the correct address.
104 if ps.weight_tensor.ops[0].type == "DMA":
105 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.weight_tensor.ops[0].inputs[0])
106 else:
107 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.weight_tensor)
Tim Hall79d07d22020-04-27 18:20:16 +0100108
Charles Xu78792222020-05-13 10:15:26 +0200109 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.scale_tensor)
110
Tim Hallc30f4952020-06-15 20:47:35 +0100111 if ps.ifm_tensor is not None and ps.ifm_tensor.mem_area != MemArea.Sram:
Charles Xu78792222020-05-13 10:15:26 +0200112 copy_ifm_values_to_memory_tensor(sg.flash_tensor, ps.ifm_tensor)
Tim Hallc30f4952020-06-15 20:47:35 +0100113 if ps.ifm2_tensor is not None and ps.ifm2_tensor.mem_area != MemArea.Sram:
Charles Xu78792222020-05-13 10:15:26 +0200114 copy_ifm_values_to_memory_tensor(sg.flash_tensor, ps.ifm2_tensor)
Tim Hall79d07d22020-04-27 18:20:16 +0100115
116 sg.command_stream_tensor = make_memory_tensor(
117 sg.name + "_command_stream", flash_area, command_stream_size_bytes, True, arch
118 )
119 sg.command_stream_tensor.values = np.frombuffer(payload_bytes, dtype=np.uint8)
120
121 return sg.scratch_tensor, sg.flash_tensor
122
123
124def add_const_tens_to_startup_cascaded_pass(startup_cps, tens):
125 op = Operation("Const", tens.name + "_const")
126 op.outputs = [tens]
127 tens.ops = [op]
128 startup_cps.passes[0].ops.insert(0, op)
129 startup_cps.passes[0].outputs.insert(0, tens)
130 startup_cps.outputs.insert(0, tens)
131
132
133def rewrite_npu_call_ops(nng, sg, arch):
134 if sg.placement != PassPlacement.Cpu:
135 return
136
137 startup_cps = sg.cascaded_passes[0]
138
139 for idx, cps in enumerate(sg.cascaded_passes):
140 for ps in cps.passes:
141 for op in ps.ops:
142 if op.type == "NpuOp":
143 callee = op.attrs["subgraph"]
144 op.attrs["custom_options"] = {"type": op.type}
145
146 sz = 0
147 for tens in [callee.scratch_tensor, callee.flash_tensor, callee.command_stream_tensor]:
148 op.inputs.insert(0, tens)
149 ps.inputs.insert(0, tens)
150 cps.inputs.insert(0, tens)
151 if tens != callee.scratch_tensor:
152 add_const_tens_to_startup_cascaded_pass(startup_cps, tens)
153 sz += tens.storage_size()
154
155 for prev_cps in sg.cascaded_passes[: idx + 1]:
156 prev_cps.sram_used += sz
157
158 if callee.scratch_tensor is not None:
159 cps.sram_used += callee.scratch_tensor.storage_size()