blob: 4542c25bce09a306be3dd1855cac97a2024e5d80 [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.
16
17
18# Description:
19# Serialises and packs an NPU subgraph into tensors.
20
21from .nn_graph import PassPlacement
22from .tensor import MemArea, Tensor, TensorPurpose, TensorFormat
23from .operation import Operation
24from .data_type import DataType
25import numpy as np
26from . import driver_actions
27import struct
28
29
30def make_memory_tensor(name, mem_area, sz, want_values, arch):
31 tens = Tensor([sz], DataType.uint8, name)
32 tens.mem_area = mem_area
33 tens.purpose = TensorPurpose.FeatureMap
34 tens.set_format(TensorFormat.NHWC, arch)
35 if want_values:
36 tens.values = np.zeros(tens.shape, np.uint8)
37 return tens
38
39
40def copy_compressed_values_to_memory_tensor(memory_tensor, src_tensor):
41 start_addr = src_tensor.address
42 for compressed_values in src_tensor.compressed_values:
43 end_addr = start_addr + len(compressed_values)
44 memory_tensor.values[start_addr:end_addr] = compressed_values
45 start_addr = end_addr
46
47
48def serialise_npu_subgraph_into_tensors(nng, sg, arch, scratch_tens, flash_tens):
49 if sg.placement != PassPlacement.Npu:
50 return scratch_tens, flash_tens
51
52 flash_area = arch.permanent_storage_mem_area
53 scratch_area = MemArea.Sram
54
55 flash_size = sg.memory_used.get(flash_area, 0)
56 scratch_size = sg.memory_used.get(scratch_area, 0)
57
58 # Prepare driver actions for this command tensor
59 da_list = []
60 driver_actions.emit_fourcc(da_list, "COP1")
61 driver_actions.emit_config(da_list, 0, 1, arch)
62 driver_actions.emit_cmd_stream_header(da_list, len(sg.register_command_stream))
63
64 # Append command stream words
65 da_list.extend(sg.register_command_stream)
66
67 # Convert to bytes
68 payload_bytes = struct.pack("<{0}I".format(len(da_list)), *da_list)
69
70 command_stream_size_bytes = len(payload_bytes)
71
72 # Adjust the bits per element calculation to exclude metadata generated by Vela
73 nng.total_size[flash_area] = nng.total_size.get(flash_area, 0) - flash_size - command_stream_size_bytes
74 nng.total_elements[flash_area] = nng.total_elements.get(flash_area, 0) - flash_size - command_stream_size_bytes
75 nng.total_size[scratch_area] = nng.total_size.get(scratch_area, 0) - scratch_size
76 nng.total_elements[scratch_area] = nng.total_elements.get(scratch_area, 0) - scratch_size
77
78 if flash_tens == scratch_tens == None:
79 # First Npu subgraph, create scratch and flash tensors
80 sg.scratch_tensor = make_memory_tensor(sg.name + "_scratch", scratch_area, scratch_size, False, arch)
81 sg.scratch_tensor.purpose = TensorPurpose.Scratch
82 sg.flash_tensor = make_memory_tensor(sg.name + "_flash", flash_area, flash_size, True, arch)
83 else:
84 sg.scratch_tensor = scratch_tens
85 sg.scratch_tensor.shape[0] += scratch_size
86 sg.flash_tensor = flash_tens
87 sg.flash_tensor.shape[0] += flash_size
88
89 for cps in sg.cascaded_passes:
90 for ps in cps.passes:
91 if ps.placement == PassPlacement.Npu and ps.weight_tensor != None:
92 # For DMA ops, ps.weight_tensor is referring to the SRAM weight tensor and therefore the address
93 # is pointing at the destination address of where the weights should be placed in SRAM.
94 # This ensures that the Flash weight tensor is used instead and thus gets the correct address.
95 if ps.weight_tensor.ops[0].type == "DMA":
96 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.weight_tensor.ops[0].inputs[0])
97 else:
98 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.weight_tensor)
99
100 copy_compressed_values_to_memory_tensor(sg.flash_tensor, ps.scale_tensor)
101
102 sg.command_stream_tensor = make_memory_tensor(
103 sg.name + "_command_stream", flash_area, command_stream_size_bytes, True, arch
104 )
105 sg.command_stream_tensor.values = np.frombuffer(payload_bytes, dtype=np.uint8)
106
107 return sg.scratch_tensor, sg.flash_tensor
108
109
110def add_const_tens_to_startup_cascaded_pass(startup_cps, tens):
111 op = Operation("Const", tens.name + "_const")
112 op.outputs = [tens]
113 tens.ops = [op]
114 startup_cps.passes[0].ops.insert(0, op)
115 startup_cps.passes[0].outputs.insert(0, tens)
116 startup_cps.outputs.insert(0, tens)
117
118
119def rewrite_npu_call_ops(nng, sg, arch):
120 if sg.placement != PassPlacement.Cpu:
121 return
122
123 startup_cps = sg.cascaded_passes[0]
124
125 for idx, cps in enumerate(sg.cascaded_passes):
126 for ps in cps.passes:
127 for op in ps.ops:
128 if op.type == "NpuOp":
129 callee = op.attrs["subgraph"]
130 op.attrs["custom_options"] = {"type": op.type}
131
132 sz = 0
133 for tens in [callee.scratch_tensor, callee.flash_tensor, callee.command_stream_tensor]:
134 op.inputs.insert(0, tens)
135 ps.inputs.insert(0, tens)
136 cps.inputs.insert(0, tens)
137 if tens != callee.scratch_tensor:
138 add_const_tens_to_startup_cascaded_pass(startup_cps, tens)
139 sz += tens.storage_size()
140
141 for prev_cps in sg.cascaded_passes[: idx + 1]:
142 prev_cps.sram_used += sz
143
144 if callee.scratch_tensor is not None:
145 cps.sram_used += callee.scratch_tensor.storage_size()