blob: 120cf8b168261769468aa6692fc7d5f9c3cb1b3e [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# Register level (low-level) command stream generation for Ethos-U55. Takes a high-level command stream and generates
20# all the register settings. Calculates dependencies between commands and inserts wait operations. And generates a bit
21# stream suitable for interpretation by the Ethos-U55 processor.
22
23from collections import defaultdict
24from enum import Enum, IntEnum
25from .high_level_command_stream import CommandType
26from .ethos_u55_regs.ethos_u55_regs import *
27from .tensor import MemArea, TensorBlockTraversal
28from .operation import NpuBlockType
29from .numeric_util import quantise_float32, round_up, round_away_zero, round_up_to_int, clamp_sigmoid, clamp_tanh
Fredrik Svedbergd67c0aa2020-03-30 13:15:28 +020030from .data_type import BaseType, DataType
Tim Hall79d07d22020-04-27 18:20:16 +010031import numpy as np
32from .shared_buffer_allocation import SharedBufferAllocation
33from .architecture_features import SharedBufferArea, SHRAMElements, ArchitectureFeatures
34from .nn_graph import TensorFormat, SchedulingStrategy
35from .range_set import (
36 MemoryAccessSet,
37 AccessDirection,
38)
39from .mark_tensors import (
40 reshape_operations,
41)
42from .architecture_features import Block, Kernel, Rect
43from . import scaling
44
45
46class RegisterMachine:
47 def __init__(self):
48 self.n_banks = 1
49 self.registers = [defaultdict(lambda: None) for _ in range(self.n_banks)]
50 self.bank_idx = 0
51
52 def set_register(self, reg, value):
53 is_changed = self.registers[self.bank_idx][reg] != value
54 self.registers[self.bank_idx][reg] = value
55 # is_changed = True # force command
56 return is_changed
57
58 def switch_bank(self):
59 self.bank_idx = (self.bank_idx + 1) % self.n_banks
60
61
62class CmdMode(IntEnum):
63 NoPayload = 0x0000
64 Payload32 = 0x4000
65 Mask = 0xC000
66 CmdOpMask = 0x03FF
67
68
69class BasePointerIndex(IntEnum):
70 ReadOnly = 0 # base address slot index for weights and scaling
71 Scratch = 1 # base address slot index for scratch memory area
72
73
74# TODO: Replace with definitions from ethos_u55_regs
75class IFM2Broadcast(IntEnum):
76 BroadcastHdim = 1 << 0
77 BroadcastWdim = 1 << 1
78 BroadcastCdim = 1 << 2
79 ReverseOperandOrder = 1 << 6
80 UseIFM2Scalar = 1 << 7
81
82
83class CommandStreamEmitter:
84 def __init__(self):
85 self.cmd_stream = []
86 self.reg_machine = [RegisterMachine(), RegisterMachine()]
87 self.last_absolute_wait = defaultdict(int)
88
89 def get_reg_machine(self, cmd):
90 if "DMA" in cmd.name:
91 return self.reg_machine[1]
92 else:
93 return self.reg_machine[0]
94
95 def size_in_bytes(self):
96 sz = 0
97 for cmd in self.cmd_stream:
98 sz += len(cmd) * 4
99 return sz
100
101 def to_list(self):
102 return [elem for cmd in self.cmd_stream for elem in cmd]
103
104 def print_cmds(self):
105 print("Code: Command: Param: Payload:")
106 for words_for_one_command in self.cmd_stream:
107 code = words_for_one_command[0] & 0x0000FFFF # lower 16 bits
108 param = words_for_one_command[0] >> 16 # higher 16 bits
109
110 payload_mode = CmdMode(code & CmdMode.Mask)
111
112 # code and command
113 s = " 0x%04x " % code
114 if payload_mode == CmdMode.NoPayload:
115 s += str(cmd0(code & CmdMode.CmdOpMask))
116 else:
117 s += str(cmd1(code & CmdMode.CmdOpMask))
118
119 s = s.ljust(40)
120 s += "%5d" % param
121
122 # payload
123 if payload_mode == CmdMode.Payload32:
124 s += " 0x%08x (%d)" % (words_for_one_command[1], words_for_one_command[1])
125 else:
126 s += " -"
127
128 print(s)
129
130 def cmd0_with_param(self, cmd, param):
131 if isinstance(param, Enum):
132 param = int(param.value)
133 else:
134 param = int(param)
135 param = param & 0xFFFF
136 command = cmd.value | (param << 16)
137 if not self.get_reg_machine(cmd).set_register(cmd, (command, param)):
138 return
139
140 # This is not a redundant command, actually write it
141 self.cmd_stream.append((command,))
142
143 def cmd1_with_offset(self, cmd, offset, param=0x0):
144 offset = int(offset) & 0xFFFFFFFFF
145 command = cmd.value | CmdMode.Payload32.value | (param << 16)
146
147 if not self.get_reg_machine(cmd).set_register(cmd, (command, offset)):
148 return
149
150 # This is not a redundant command, actually write it
151 self.cmd_stream.append((command, offset))
152
153 def cmd_wait(self, cmd, param, absolute_wait_time):
154 if absolute_wait_time <= self.last_absolute_wait[cmd]:
155 return
156
157 self.last_absolute_wait[cmd] = absolute_wait_time
158 param = int(param)
159 command = ((param & 0xFFFF) << 16) | cmd.value
160 self.cmd_stream.append((command,))
161
162 def cmd_do_operation(self, cmd, param=0):
163 param = int(param)
164 command = ((param & 0xFFFF) << 16) | cmd.value
165
166 self.cmd_stream.append((command,))
167 self.get_reg_machine(cmd).switch_bank()
168
169
170def calc_command_dependencies(cmd_stream, arch):
171 cmd_starts = {}
172 cmd_ends = {}
173 memory_accesses = {}
174
175 # Keep track of accumulated number of commands in command stream.
176 # First element kernel ops: (# of blocks, # of commands)
177 # Second element DMA ops: (# of commands)
178 pos = np.array((np.array((0, 0)), np.array([0])))
179
180 dependencies = {}
181
182 for cmd in cmd_stream:
183 cmd_starts[cmd] = pos
184 op_count = cmd.get_operation_count()
185 # Keep track of both num blocks and commands
186 cmd_add = 0 if (op_count[0] == 0) else 1
187 pos = np.array((pos[0] + np.array((op_count[0], cmd_add)), pos[1] + np.array([op_count[1]])))
188 cmd_ends[cmd] = np.array((pos[0], pos[1]))
189 memory_accesses[cmd] = cmd.get_memory_accesses()
190
191 for idx, cmd in enumerate(cmd_stream):
192 curr_accesses = memory_accesses[cmd]
193 # Keep track of command dependency.
194 # First element kernel ops: (# of blocks, # of commands)
195 # Second element DMA ops: (# of commands)
196 dep_offsets = np.array((np.array((-1, -1)), np.array([-1])))
197 dep_cmds = [None] * CommandType.Size.value
198 if idx > 0:
199 # Look at the previous commands in backwards order
200 for prev_cmd in cmd_stream[idx - 1 :: -1]:
201 assert prev_cmd is not cmd
202 if dep_cmds[prev_cmd.cmdtype] is None:
203 is_dependency = False
204 if cmd.cmdtype == CommandType.NpuStripe and prev_cmd.cmdtype == CommandType.NpuStripe:
205 # Special handling here, as dpu -> dpu operations require additional care
206 if not SharedBufferAllocation.is_compatible(prev_cmd.ps.shared_buffer, cmd.ps.shared_buffer):
207 is_dependency = True
208 elif memory_accesses[prev_cmd].conflicts(curr_accesses):
209 is_dependency = True
210 else:
211 if memory_accesses[prev_cmd].conflicts(curr_accesses):
212 is_dependency = True
213
214 if is_dependency:
215 new_offset = cmd_ends[prev_cmd][prev_cmd.cmdtype]
216 if new_offset[0] > dep_offsets[prev_cmd.cmdtype][0]:
217 dep_cmds[prev_cmd.cmdtype] = prev_cmd
218 dep_offsets[prev_cmd.cmdtype] = new_offset
219
220 # Check if we've got dependencies for all commands, in which case we can early out
221 for dep in dep_cmds:
222 if dep is None:
223 break
224 else:
225 break # all handled
226
227 # Convert absolute to relative dependencies, using None to signal the special case of no
228 # dependency of this kind
229 res = [None] * CommandType.Size.value
230 for i in range(CommandType.Size.value):
231 if dep_cmds[i] is not None:
232 res[i] = cmd_starts[cmd][i] - dep_offsets[i]
233
234 dependencies[cmd] = cmd_starts[cmd], res
235
236 return dependencies
237
238
239def get_op_kernel(ps):
240 if ps.primary_op is None:
241 return None
242
243 strides = ps.primary_op.attrs.get("strides", (1, 1, 1, 1))
244 dilation = ps.primary_op.attrs.get("dilation", (1, 1, 1, 1))
245 if ps.weight_tensor:
246 if ps.npu_block_type in set((NpuBlockType.VectorProduct, NpuBlockType.ElementWise)):
247 k_h = 1
248 k_w = 1
249 else:
250 k_h = ps.weight_tensor.shape[0]
251 k_w = ps.weight_tensor.shape[1]
252 else:
253 k_h = ps.primary_op.attrs.get("filter_height", 1)
254 k_w = ps.primary_op.attrs.get("filter_width", 1)
255
256 return Kernel(k_w, k_h, strides[2], strides[1], dilation[2], dilation[1])
257
258
259def full_shape(shape, fill):
260 return ([fill] * (4 - len(shape))) + shape
261
262
263def has_prev_op_dependency(prev_cmd, cmd):
264 if prev_cmd is None:
265 return False
266 if (prev_cmd.cmdtype == cmd.cmdtype == CommandType.NpuStripe) and (prev_cmd.ps != cmd.ps):
267 if prev_cmd.ofm_tensor == cmd.ifm_tensor:
268 return True
269 else:
270 return prev_cmd.ofm_tensor.equivalence_id == cmd.ifm_tensor.equivalence_id
271 return False
272
273
274def get_op_ofm_rect(cmd):
275 start = full_shape(cmd.ofm_box.start_coord, 0)
276 end = full_shape(cmd.ofm_box.end_coord, 1)
277 return Rect(start[-2], start[-3], start[-1], end[-2] - 1, end[-3] - 1, end[-1] - 1)
278
279
280def get_op_ifm_rect(cmd):
281 start = full_shape(cmd.ifm_box.start_coord, 0)
282 end = full_shape(cmd.ifm_box.end_coord, 1)
283 return Rect(start[-2], start[-3], start[-1], end[-2] - 1, end[-3] - 1, end[-1] - 1)
284
285
286def get_op_ifmofm_block_depth(arch, cmd):
287 # Note: NOT equivalent to the normal ifm block depth calculation since
288 # it takes into account 'depthless' block operations by returning full
289 # depth
290 if cmd.ps.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling, NpuBlockType.ElementWise):
291 return cmd.ofm_box.get_size_shape()[-1]
292
293 return arch.calc_ifm_block_depth(cmd.ifm_box.get_size_shape()[-1], cmd.ifm_tensor.dtype.bits)
294
295
296def get_op_padding_lt(cmd):
297 if cmd.ps.npu_block_type not in (
298 NpuBlockType.ConvolutionDepthWise,
299 NpuBlockType.Pooling,
300 NpuBlockType.ConvolutionMxN,
301 ):
302 return (0, 0)
303
304 explicit_padding = list(cmd.ps.primary_op.attrs["explicit_padding"]) # (top, left, bottom, right)
305
306 # Check if this is for horizontal ifm streaming
307 if not (cmd.is_first_h_stripe and cmd.is_last_h_stripe):
308 explicit_padding[0] = cmd.pad_top
309 explicit_padding[2] = cmd.pad_bottom
310
311 return (explicit_padding[1], explicit_padding[0])
312
313
314def generate_register_command_stream(nng, sg, arch, verbose=False):
315 emit = CommandStreamEmitter()
316
317 base_ptr_idx_map = {
318 MemArea.Sram: BasePointerIndex.Scratch,
319 MemArea.OnChipFlash: BasePointerIndex.ReadOnly,
320 MemArea.OffChipFlash: BasePointerIndex.ReadOnly,
321 MemArea.Dram: BasePointerIndex.ReadOnly,
322 }
323
324 # Maps an AccumulatorType enum to the corresponding acc_format value
325 acc_format_map = {
326 SHRAMElements.Acc16: acc_format.FP_S5_10.value,
327 SHRAMElements.Acc32: acc_format.INT_32BIT.value,
328 SHRAMElements.Acc40: acc_format.INT_40BIT.value,
329 }
330
331 # Maps an elementwise op type to an elementwise_mode enum value used by NPU_OP_ELEMENTWISE
332 elementwise_mode_map = {
333 "MulAct": elementwise_mode.MUL.value,
334 "AddAct": elementwise_mode.ADD.value,
335 "SubAct": elementwise_mode.SUB.value,
336 "Minimum": elementwise_mode.MIN.value,
337 "Maximum": elementwise_mode.MAX.value,
338 "LeakyRelu": elementwise_mode.LRELU.value,
339 "Abs": elementwise_mode.ABS.value,
340 }
341
342 cmd_stream = []
343 for cmd in sg.high_level_command_stream:
344 if cmd.cmdtype == CommandType.NpuStripe and cmd.ps.npu_block_type == NpuBlockType.Default:
345 print("Warning: Skipping register command stream generation for", cmd.ps)
346 else:
347 cmd_stream.append(cmd)
348
349 dependencies = calc_command_dependencies(cmd_stream, arch)
350
351 # Initialise operator dependency state
352 prev_ifm_rect = cur_ifm_rect = None
353 prev_ifm_block_depth = cur_ifm_block_depth = None
354 prev_ofm_rect = cur_ofm_rect = None
355 prev_ofm_block = cur_ofm_block = None
356 prev_kernel = cur_kernel = None
357 prev_cmd = None
358
359 def emit_wait_commands(cmd):
360 # The command is fully set up, emit whatever wait commands we need
361 absolute_dep, relative_dep = dependencies[cmd]
362 if relative_dep[CommandType.NpuStripe] is not None:
363 if cmd.cmdtype == CommandType.DMA:
364 param = relative_dep[CommandType.NpuStripe][1]
365 if param <= 3:
366 emit.cmd_wait(cmd0.NPU_OP_KERNEL_WAIT, param, absolute_dep[CommandType.NpuStripe][1])
367 else:
368 param = relative_dep[CommandType.NpuStripe][0]
369 param = min(param, 0xFFFF) # Clamp to allowable wait amount
370
371 if relative_dep[CommandType.DMA] is not None:
372 param = relative_dep[CommandType.DMA][0]
373 param = min(param, 0xF) # Clamp to allowable wait amount
374 emit.cmd_wait(cmd0.NPU_OP_DMA_WAIT, param, absolute_dep[CommandType.DMA][0])
375 prev_cmd = None # Clear any dependency
376
Tim Hall79d07d22020-04-27 18:20:16 +0100377 for cmd in cmd_stream:
378 if cmd.cmdtype == CommandType.DMA:
379 start_coord = cmd.box.start_coord
380
381 src_addr = cmd.in_tensor.address_for_coordinate(start_coord)
382 dst_addr = cmd.out_tensor.address_for_coordinate(start_coord)
383
384 if cmd.in_tensor.compressed_values is not None:
385 stream_index = cmd.in_tensor.compressed_stream_index_from_coord(start_coord)
386 sz = cmd.in_tensor.size_of_compressed_stream(stream_index)
387 else:
388 sz = cmd.in_tensor.address_for_coordinate(cmd.box.end_coord, is_top_box=True) - src_addr
389
390 # TODO: Yoda support needs to use feature_maps_not_in_fast_storage and force_outputs_to_fast_storage
391 emit.cmd0_with_param(cmd0.NPU_SET_DMA0_SRC_REGION, base_ptr_idx_map[cmd.in_tensor.mem_area])
392 emit.cmd1_with_offset(cmd1.NPU_SET_DMA0_SRC, src_addr)
393 emit.cmd0_with_param(cmd0.NPU_SET_DMA0_DST_REGION, base_ptr_idx_map[cmd.out_tensor.mem_area])
394 emit.cmd1_with_offset(cmd1.NPU_SET_DMA0_DST, dst_addr)
395 emit.cmd1_with_offset(cmd1.NPU_SET_DMA0_LEN, sz)
396 dma_channel = 0
397 mode = 0 # From external to external
398
399 emit_wait_commands(cmd)
400 emit.cmd_do_operation(cmd0.NPU_OP_DMA_START, dma_channel * 16 + mode)
401
402 elif cmd.cmdtype == CommandType.NpuStripe:
403
404 ps = cmd.ps
405 primary_op = ps.primary_op
406 npu_block_type = ps.npu_block_type
407 # Specifies if global scale from the NPU_SET_OFM_SCALE register should be used instead of per-channel scale
408 use_global_scale = False
409 # Specifies type of rounding to be used.
410 rounding_mode = rounding.TFL
411 fmf = primary_op.attrs.get("fused_memory_function", None)
412 faf = primary_op.attrs.get("fused_activation_function", None)
413
414 # Specifies which operand to apply scaling to in bitexact elementwise ADD/SUB
415 op_to_scale = 0
416
417 # Update state history
418 prev_ifm_rect = cur_ifm_rect
419 prev_ifm_block_depth = cur_ifm_block_depth
420 prev_ofm_rect = cur_ofm_rect
421 prev_ofm_block = cur_ofm_block
422 prev_kernel = cur_kernel
423
424 block_config = ps.block_config
425 emit.cmd0_with_param(cmd0.NPU_SET_OFM_BLK_HEIGHT_M1, block_config[0] - 1)
426 emit.cmd0_with_param(cmd0.NPU_SET_OFM_BLK_WIDTH_M1, block_config[1] - 1)
427 emit.cmd0_with_param(cmd0.NPU_SET_OFM_BLK_DEPTH_M1, block_config[3] - 1)
428
429 shared_buffer = ps.shared_buffer
430
431 if npu_block_type == NpuBlockType.ElementWise:
432 ifm2_broadcast = 0
433
434 if cmd.ifm_tensor.shape == []:
435 # The scalar has to be the ifm2 tensor so switch the ifms
436 cmd.ifm_tensor, cmd.ifm2_tensor = cmd.ifm2_tensor, cmd.ifm_tensor
437 cmd.ifm_box, cmd.ifm2_box = cmd.ifm2_box, cmd.ifm_box
438
439 # Set ReverseOperandOrder bit to IFM2_BROADCAST
440 ifm2_broadcast |= IFM2Broadcast.ReverseOperandOrder
441
442 # Calculate scales needed for arithmetic elementwise operators
443 if primary_op.type in set(("AddAct", "MulAct", "SubAct",)):
444 input_scale = cmd.ifm_tensor.quantization.scale_f32
445 input2_scale = cmd.ifm2_tensor.quantization.scale_f32
446 output_scale = cmd.ofm_tensor.quantization.scale_f32
447 use_global_scale = True
448
449 if primary_op.type == "MulAct":
450 if (faf == "Sigmoid") or (faf == "Tanh"):
451 output_scale = 1 / 0x3000
452
453 ofm_scale, shift = scaling.elementwise_mul_scale(input_scale, input2_scale, output_scale)
454 emit.cmd1_with_offset(cmd1.NPU_SET_OFM_SCALE, ofm_scale, shift)
455 else: # AddAct/SubAct
456 if (faf == "Sigmoid") or (faf == "Tanh"):
457 output_scale = 1 / 0x3000
458
459 if input_scale == input2_scale:
460 opa_scale, opb_scale, ofm_scale, shift = scaling.simplified_elementwise_add_sub_scale(
461 input_scale, input2_scale, output_scale
462 )
463 opa_shift = 0 # Unused for this case
464 else:
465 # Use advanced implementation only when input scales differ
466 bitdepth = cmd.ifm_tensor.dtype.bits
467 (
468 opa_scale,
469 opa_shift,
470 ofm_scale,
471 shift,
472 op_to_scale,
473 ) = scaling.advanced_elementwise_add_sub_scale(
474 input_scale, input2_scale, output_scale, bitdepth
475 )
476 opb_scale = 0 # Unused for this case
477 if ifm2_broadcast & IFM2Broadcast.ReverseOperandOrder:
478 # If the operand order is reversed we also have to swap which operand is scaled
479 if op_to_scale == scaling.OperandToScale.OPa:
480 op_to_scale = scaling.OperandToScale.OPb
481 else:
482 op_to_scale = scaling.OperandToScale.OPa
483
484 emit.cmd1_with_offset(cmd1.NPU_SET_OPA_SCALE, opa_scale, opa_shift)
485 emit.cmd1_with_offset(cmd1.NPU_SET_OPB_SCALE, opb_scale)
486 emit.cmd1_with_offset(cmd1.NPU_SET_OFM_SCALE, ofm_scale, shift)
487
488 if primary_op.type in set(("LeakyRelu", "Abs",)):
489 output_scale = cmd.ofm_tensor.quantization.scale_f32
490 use_global_scale = True
491
492 if primary_op.type == "LeakyRelu":
493 output_scale *= primary_op.attrs["alpha"]
494
495 ofm_scale, shift = scaling.quantise_scale(output_scale)
496 emit.cmd1_with_offset(cmd1.NPU_SET_OFM_SCALE, ofm_scale, shift)
497
498 # For elementwise set the required SHRAM to be equal to the total size of SHRAM
499 shram_required = arch.shram_total_banks
500 emit.cmd0_with_param(cmd0.NPU_SET_IFM_IB_END, shram_required)
501
502 # Acc buffers not needed so set AB_START to size of SHRAM
503 emit.cmd0_with_param(cmd0.NPU_SET_AB_START, arch.shram_total_banks)
504
505 # Is not a unary operator
506 if cmd.ifm2_tensor is not None:
507 if cmd.ifm2_tensor.shape == []:
508 # IFM2 is a constant, set UseIFM2Scalar bit to IFM2_BROADCAST
509 ifm2_broadcast |= IFM2Broadcast.UseIFM2Scalar
510 else:
511 ifm_box_shape = cmd.ifm_box.get_size_shape()
512 ifm2_box_shape = cmd.ifm2_box.get_size_shape()
513
514 if len(cmd.ifm_tensor.shape) > 1 and ifm_box_shape[1] != ifm2_box_shape[1]:
515 # Broadcast in 'H' dimension
516 assert cmd.ifm2_tensor.shape[1] == 1
517 ifm2_broadcast |= IFM2Broadcast.BroadcastHdim
518
519 if len(cmd.ifm_tensor.shape) > 2 and ifm_box_shape[2] != ifm2_box_shape[2]:
520 # Broadcast in 'W' dimension
521 assert cmd.ifm2_tensor.shape[2] == 1
522 ifm2_broadcast |= IFM2Broadcast.BroadcastWdim
523
524 if len(cmd.ifm_tensor.shape) > 3 and ifm_box_shape[3] != ifm2_box_shape[3]:
525 # Broadcast in 'C' dimension
526 assert cmd.ifm2_tensor.shape[3] == 1
527 ifm2_broadcast |= IFM2Broadcast.BroadcastCdim
528
529 # Set IFM2_IB_START to the latter half of the IB space
530 ifm_ib_start = shared_buffer.bank_locations[SharedBufferArea.IFM]
531 emit.cmd0_with_param(
532 cmd0.NPU_SET_IFM2_IB_START, (shram_required - ifm_ib_start) / 2 + ifm_ib_start
533 )
534
535 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_BROADCAST, ifm2_broadcast)
536
537 else:
538 emit.cmd0_with_param(
539 cmd0.NPU_SET_IFM_IB_END,
540 shared_buffer.bank_locations[SharedBufferArea.IFM]
541 + shared_buffer.banks_required[SharedBufferArea.IFM],
542 )
543 emit.cmd0_with_param(cmd0.NPU_SET_AB_START, shared_buffer.bank_locations[SharedBufferArea.Accumulators])
544
545 emit.cmd0_with_param(cmd0.NPU_SET_ACC_FORMAT, acc_format_map[shared_buffer.use_accumulator_element])
546
547 emit.cmd0_with_param(cmd0.NPU_SET_IFM_UPSCALE, 0)
548
549 if npu_block_type in set(
550 (NpuBlockType.ConvolutionMxN, NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling)
551 ):
552 # Set up padding
553 explicit_padding = list(primary_op.attrs["explicit_padding"]) # (top, left, bottom, right)
554
555 # Check if this is for horizontal ifm streaming
556 if not (cmd.is_first_h_stripe and cmd.is_last_h_stripe):
557 explicit_padding[0] = cmd.pad_top
558 explicit_padding[2] = cmd.pad_bottom
559
560 # Indexing from end since a 1x1 Avgpool might have been added with non 4-dimensional input/output,
561 # because of activation function needed to be fused.
562 if cmd.ifm_box.start_coord[-2] > 0:
563 explicit_padding[1] = 0
564 if cmd.ifm_box.end_coord[-2] < cmd.ifm_tensor.shape[-2]:
565 explicit_padding[3] = 0
566
567 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_TOP, explicit_padding[0])
568 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_LEFT, explicit_padding[1])
569 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_BOTTOM, explicit_padding[2])
570 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_RIGHT, explicit_padding[3])
571
Dwight Lidman0538a772020-05-06 14:09:17 +0200572 # set kernel x stride low bit
573 stride = primary_op.attrs["strides"][2] - 1 & 1
574 # set kernel y stride low bit
575 stride |= (primary_op.attrs["strides"][1] - 1 & 1) << 1
576 # set kernel x stride extension bits
577 stride |= (primary_op.attrs["strides"][2] - 1 >> 1) << 6
578 # set kernel y stride extension bits
579 stride |= (primary_op.attrs["strides"][1] - 1 >> 1) << 9
580
Tim Hall79d07d22020-04-27 18:20:16 +0100581
582 if npu_block_type == NpuBlockType.Pooling:
583 k_height, k_width = primary_op.attrs["ksize"][1:3]
584 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_HEIGHT_M1, k_height - 1)
585 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_WIDTH_M1, k_width - 1)
586
587 valid_padding = sum(explicit_padding) == 0
588
589 if primary_op.type in set(("AvgPool", "AvgPoolAct")) and valid_padding:
590 # For valid padding vela has to output scaling values
591 if faf == "Sigmoid" or faf == "Tanh":
592 rescale = 0x3000 * cmd.ifm_tensor.quantization.scale_f32
593 rescale_bits = len(bin(round_up_to_int(rescale))) - 2 + 1
594
595 scale, shift = scaling.quantise_pooling_scale(k_height * k_width, rescale_bits)
596 scale = int(round_away_zero(scale * rescale))
597 else:
598 # In case avg pool fused with concat or other memory operation, rescaling might be needed.
599 # k_height == k_width == 1 is allways true in this case
600 # Normally the scale is maximised, to get maximum precision, which means that
601 # if rescale != 1, scale need to consider the number of bits needed for rescaling
602 rescale = cmd.ifm_tensor.quantization.scale_f32 / cmd.ofm_tensor.quantization.scale_f32
603 rescale_bits = 0
604 if k_height == k_width == 1:
605 if fmf == "ConcatSliceWrite":
606 rounding_mode = rounding.NATURAL
607 if rescale > 1:
608 rescale_bits = len(bin(round_up_to_int(rescale))) - 2 + 1
609 elif rescale < 1:
610 rescale_bits = -(len(bin(round_up_to_int(1 / rescale))) - 2 - 1)
611 scale, shift = scaling.quantise_pooling_scale(k_height * k_width, rescale_bits)
612 scale = int(round_away_zero(scale * rescale))
613
614 emit.cmd1_with_offset(cmd1.NPU_SET_OFM_SCALE, scale, shift)
615 # Valid-padded average pool should use the global scale from
616 # NPU_SET_OFM_SCALE register, which is set above.
617 use_global_scale = True
618
619 else: # Convolution
620 assert cmd.weight_tensor.block_traversal != TensorBlockTraversal.Default
Fredrik Svedbergd67c0aa2020-03-30 13:15:28 +0200621 # Reduced precision quantization and natural rounding used for int16
622 if cmd.ifm_tensor.dtype == DataType.int16:
623 rounding_mode = rounding.NATURAL
Tim Hall79d07d22020-04-27 18:20:16 +0100624 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_HEIGHT_M1, cmd.weight_tensor.shape[0] - 1)
625 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_WIDTH_M1, cmd.weight_tensor.shape[1] - 1)
626 if cmd.weight_tensor.block_traversal == TensorBlockTraversal.PartKernelFirst:
627 # Part-kernel-first weight ordering
628 assert npu_block_type == NpuBlockType.ConvolutionMxN
629 stride |= 1 << 2
630
631 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_STRIDE, stride)
632
633 elif npu_block_type in set((NpuBlockType.VectorProduct,)):
634 # Vector product is implemented using a 1x1 convolution so need
635 # to setup the appropriate padding and kernel info
636 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_TOP, 0)
637 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_LEFT, 0)
638 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_BOTTOM, 0)
639 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PAD_RIGHT, 0)
640
641 # kernel stride reg = 0 means stride(1,1) + depth first weight
642 # order + dilation(0,0) + kernel_split_size=8
643 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_STRIDE, 0)
644
645 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_HEIGHT_M1, 0)
646 emit.cmd0_with_param(cmd0.NPU_SET_KERNEL_WIDTH_M1, 0)
647
648 if npu_block_type in set(
649 (NpuBlockType.ConvolutionMxN, NpuBlockType.ConvolutionDepthWise, NpuBlockType.VectorProduct)
650 ):
651 # Emit Weight base address commands, only maps the area required for
652 # this command's weights from the larger tensor.
653 stream_index = cmd.weight_tensor.compressed_stream_index_from_coord(cmd.weight_box.start_coord)
654 weight_addr = cmd.weight_tensor.address_for_coordinate(cmd.weight_box.start_coord)
655 weight_len = cmd.weight_tensor.size_of_compressed_stream(stream_index)
656 # Select weight/scale region depending on where permanent storage was defined
657 weight_region = base_ptr_idx_map[cmd.weight_tensor.mem_area]
658 if arch.permanent_storage_mem_area == MemArea.Sram:
659 weight_region = BasePointerIndex.ReadOnly
660 emit.cmd0_with_param(cmd0.NPU_SET_WEIGHT_REGION, weight_region)
661 emit.cmd1_with_offset(cmd1.NPU_SET_WEIGHT_BASE, weight_addr)
662 emit.cmd1_with_offset(cmd1.NPU_SET_WEIGHT_LENGTH, weight_len)
663
664 # Emit Scale & Bias base address commands, with length matching the amount required by
665 # the weight tensors.
666 if cmd.scale_tensor is not None:
667 # Get address and size of the scale/bias data area
668 scale_addr = cmd.scale_tensor.address_for_coordinate(cmd.weight_box.start_coord[-1:])
669 scale_len = (
670 cmd.scale_tensor.address_for_coordinate(cmd.weight_box.end_coord[-1:], True) - scale_addr
671 )
672 # Emit base address for NPU to access scale & bias data
673 scale_region = base_ptr_idx_map[cmd.scale_tensor.mem_area]
674 if arch.permanent_storage_mem_area == MemArea.Sram:
675 scale_region = BasePointerIndex.ReadOnly
676 emit.cmd0_with_param(cmd0.NPU_SET_SCALE_REGION, scale_region)
677 emit.cmd1_with_offset(cmd1.NPU_SET_SCALE_BASE, scale_addr)
678 emit.cmd1_with_offset(cmd1.NPU_SET_SCALE_LENGTH, round_up(scale_len, 16))
679
680 ofm_quant = cmd.ofm_tensor.quantization
681 ofm_quant_qmin = cmd.ofm_tensor.quantization.quant_min
682 ofm_quant_qmax = cmd.ofm_tensor.quantization.quant_max
683 ifm_min = cmd.ifm_tensor.quantization.min
684 ifm_max = cmd.ifm_tensor.quantization.max
685
686 # Emit commands for any fused activation function
687 if faf == None:
688 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.NONE)
689 # Even if no activation function, values need to be set to override previous values
690 faf_min = ofm_quant_qmin
691 faf_max = ofm_quant_qmax
692 elif faf == "Relu":
693 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.NONE)
694 faf_min = quantise_float32(0.0, ofm_quant.scale_f32, ofm_quant.zero_point)
695 faf_max = ofm_quant_qmax
696 elif faf == "Relu6":
697 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.NONE)
698 faf_min = quantise_float32(0.0, ofm_quant.scale_f32, ofm_quant.zero_point)
699 faf_max = quantise_float32(6.0, ofm_quant.scale_f32, ofm_quant.zero_point)
700 elif faf == "ReluN1To1":
701 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.NONE)
702 faf_min = quantise_float32(-1.0, ofm_quant.scale_f32, ofm_quant.zero_point)
703 faf_max = quantise_float32(1.0, ofm_quant.scale_f32, ofm_quant.zero_point)
704 elif faf == "Tanh":
705 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.TANH)
706 faf_min = quantise_float32(clamp_tanh(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point)
707 faf_max = quantise_float32(clamp_tanh(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point)
708 elif faf == "Sigmoid":
709 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.SIGMOID)
710 faf_min = quantise_float32(clamp_sigmoid(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point)
711 faf_max = quantise_float32(clamp_sigmoid(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point)
712 else:
713 raise Exception("Unsupported fused_activation_function = " + faf)
714
715 # Activation range needs to be set based upon the quantisation range and the fused activation range
716 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION_MIN, max(ofm_quant_qmin, faf_min))
717 emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION_MAX, min(ofm_quant_qmax, faf_max))
718
719 out_shape = cmd.ofm_box.get_size_shape()
720 if len(out_shape) >= 4:
721 emit.cmd0_with_param(cmd0.NPU_SET_OFM_HEIGHT_M1, out_shape[-3] - 1)
722 else:
723 emit.cmd0_with_param(cmd0.NPU_SET_OFM_HEIGHT_M1, 0)
724 if len(out_shape) >= 2:
725 emit.cmd0_with_param(cmd0.NPU_SET_OFM_WIDTH_M1, out_shape[-2] - 1)
726 else:
727 emit.cmd0_with_param(cmd0.NPU_SET_OFM_WIDTH_M1, 0)
728 emit.cmd0_with_param(cmd0.NPU_SET_OFM_DEPTH_M1, out_shape[-1] - 1)
729
730 if npu_block_type in set((NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct)):
731 in_shape = cmd.ifm_box.get_size_shape()
732 emit.cmd0_with_param(cmd0.NPU_SET_IFM_DEPTH_M1, in_shape[-1] - 1)
733 else:
734 emit.cmd0_with_param(cmd0.NPU_SET_IFM_DEPTH_M1, out_shape[-1] - 1)
735
Jacob Bohlin3c678292020-04-27 10:27:25 +0200736 for tens, box, region_op, ptr_ops, stride_ops, zero_point_op in (
Tim Hall79d07d22020-04-27 18:20:16 +0100737 (
738 cmd.ifm_tensor,
739 cmd.ifm_box,
Jacob Bohlin3c678292020-04-27 10:27:25 +0200740 cmd0.NPU_SET_IFM_REGION,
Tim Hall79d07d22020-04-27 18:20:16 +0100741 (cmd1.NPU_SET_IFM_BASE0, cmd1.NPU_SET_IFM_BASE1, cmd1.NPU_SET_IFM_BASE2, cmd1.NPU_SET_IFM_BASE3),
742 (cmd1.NPU_SET_IFM_STRIDE_C, cmd1.NPU_SET_IFM_STRIDE_Y, cmd1.NPU_SET_IFM_STRIDE_X),
743 cmd0.NPU_SET_IFM_ZERO_POINT,
744 ),
745 (
746 cmd.ifm2_tensor,
747 cmd.ifm2_box,
Jacob Bohlin3c678292020-04-27 10:27:25 +0200748 cmd0.NPU_SET_IFM2_REGION,
Tim Hall79d07d22020-04-27 18:20:16 +0100749 (
750 cmd1.NPU_SET_IFM2_BASE0,
751 cmd1.NPU_SET_IFM2_BASE1,
752 cmd1.NPU_SET_IFM2_BASE2,
753 cmd1.NPU_SET_IFM2_BASE3,
754 ),
755 (cmd1.NPU_SET_IFM2_STRIDE_C, cmd1.NPU_SET_IFM2_STRIDE_Y, cmd1.NPU_SET_IFM2_STRIDE_X),
756 cmd0.NPU_SET_IFM2_ZERO_POINT,
757 ),
758 (
759 cmd.ofm_tensor,
760 cmd.ofm_box,
Jacob Bohlin3c678292020-04-27 10:27:25 +0200761 cmd0.NPU_SET_OFM_REGION,
Tim Hall79d07d22020-04-27 18:20:16 +0100762 (cmd1.NPU_SET_OFM_BASE0, cmd1.NPU_SET_OFM_BASE1, cmd1.NPU_SET_OFM_BASE2, cmd1.NPU_SET_OFM_BASE3),
763 (cmd1.NPU_SET_OFM_STRIDE_C, cmd1.NPU_SET_OFM_STRIDE_Y, cmd1.NPU_SET_OFM_STRIDE_X),
764 cmd0.NPU_SET_OFM_ZERO_POINT,
765 ),
766 ):
767
768 if tens == None:
769 continue
770
771 need_zero_point = (faf != None) or (fmf == "ConcatSliceWrite")
772 if (
773 primary_op.type in set(("AvgPool", "AvgPoolAct")) and not need_zero_point
774 ) or tens.quantization == None:
775 # Actual integer operation, just set scale to 1 and zero point to 0
776 emit.cmd0_with_param(zero_point_op, 0)
777 else:
778 assert tens.quantization.zero_point is not None, "need an actual zero point set"
779 emit.cmd0_with_param(zero_point_op, int(tens.quantization.zero_point))
780
781 if tens.shape == []:
782 # Empty shape, elementwise constant
783 ifm2_scalar = tens.quant_values.astype(np.uint8)
784 assert ifm2_scalar.size == 1
785 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_SCALAR, ifm2_scalar.item(0))
786 continue
787
788 height_0, height_1, width_0, addresses = tens.addresses_for_rolling_buffer(
789 box.start_coord, box.end_coord
790 )
791 if npu_block_type != NpuBlockType.VectorProduct:
792 if tens == cmd.ifm_tensor:
793 emit.cmd0_with_param(cmd0.NPU_SET_IFM_HEIGHT0_M1, height_0 - 1)
794 emit.cmd0_with_param(cmd0.NPU_SET_IFM_HEIGHT1_M1, height_1 - 1)
795 emit.cmd0_with_param(cmd0.NPU_SET_IFM_WIDTH0_M1, width_0 - 1)
796 elif tens == cmd.ofm_tensor:
797 emit.cmd0_with_param(cmd0.NPU_SET_OFM_HEIGHT0_M1, height_0 - 1)
798 emit.cmd0_with_param(cmd0.NPU_SET_OFM_HEIGHT1_M1, height_1 - 1)
799 emit.cmd0_with_param(cmd0.NPU_SET_OFM_WIDTH0_M1, width_0 - 1)
800 elif tens == cmd.ifm2_tensor:
801 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_HEIGHT0_M1, height_0 - 1)
802 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_HEIGHT1_M1, height_1 - 1)
803 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_WIDTH0_M1, width_0 - 1)
804 else:
805 if len(out_shape) == 2:
806 # TODO: N is put in W-dimension for now
807 # Should be spread over H and W, but then block size selectetion,
808 # and stride calculation should be changed
809 if tens == cmd.ifm_tensor:
810 emit.cmd0_with_param(cmd0.NPU_SET_IFM_WIDTH0_M1, out_shape[-2] - 1)
811 elif tens == cmd.ofm_tensor:
812 emit.cmd0_with_param(cmd0.NPU_SET_OFM_WIDTH0_M1, out_shape[-2] - 1)
813 else:
814 assert False
815
Jacob Bohlin3c678292020-04-27 10:27:25 +0200816 if tens.mem_area == MemArea.Sram:
817 emit.cmd0_with_param(region_op, BasePointerIndex.Scratch)
818 else:
819 emit.cmd0_with_param(region_op, BasePointerIndex.ReadOnly)
820
Tim Hall79d07d22020-04-27 18:20:16 +0100821 for idx, addr in enumerate(addresses):
822 if addr is None:
823 addresses[idx] = 0
824
825 emit.cmd1_with_offset(ptr_ops[0], addresses[0])
826 emit.cmd1_with_offset(ptr_ops[1], addresses[1])
827 emit.cmd1_with_offset(ptr_ops[2], addresses[2])
828 emit.cmd1_with_offset(ptr_ops[3], addresses[3])
829
830 strides = tens.get_strides()
831 emit.cmd1_with_offset(stride_ops[0], strides[1]) # stride between 16-byte channel blocks (C)
832 emit.cmd1_with_offset(stride_ops[2], strides[3]) # stride between horisontal values (W)
833 emit.cmd1_with_offset(stride_ops[1], strides[2]) # stride between vertical values (H)
834
835 if tens.format == TensorFormat.NHCWB16:
836 # Check that all BasePointer addresses are aligned to 16 bytes
837 assert (int(addresses[0]) % 16) == 0
838 assert (int(addresses[1]) % 16) == 0
839 assert (int(addresses[2]) % 16) == 0
840 assert (int(addresses[3]) % 16) == 0
841
842 ofm_dtype = cmd.ofm_tensor.dtype
843 assert ofm_dtype.type & BaseType.Int
844 prec = 0
845 if ofm_dtype.size_in_bits() == 8:
846 prec = 0
847 elif ofm_dtype.size_in_bits() == 16:
848 prec = 2
849 else:
850 assert 0
851
852 if ofm_dtype.type & BaseType.Signed:
853 prec += 1
854
855 if use_global_scale:
856 # Set global scale bit, as opposed to using per channel scale
857 prec |= 1 << 8
858
859 if cmd.ofm_tensor.format == TensorFormat.NHCWB16:
860 prec |= 1 << 6
861
862 prec |= rounding_mode.value << 14
863
864 emit.cmd0_with_param(cmd0.NPU_SET_OFM_PRECISION, prec)
865
866 prec = None
867 weight_bits = 8
868 if cmd.weight_tensor is not None:
869 weight_bits = cmd.weight_tensor.dtype.size_in_bits()
870
871 ifm_dtype = cmd.ifm_tensor.dtype
872
873 assert weight_bits == 8, "Unsupported weight bit depth"
874 assert ifm_dtype.size_in_bits() in {8, 16}
875
876 if ifm_dtype.size_in_bits() == 8:
877 if ifm_dtype.type & BaseType.Signed:
Diqing Zhongfed918b2020-04-27 10:27:34 +0200878 prec = ifm_precision.S8
Tim Hall79d07d22020-04-27 18:20:16 +0100879 else:
Diqing Zhongfed918b2020-04-27 10:27:34 +0200880 prec = ifm_precision.U8
Tim Hall79d07d22020-04-27 18:20:16 +0100881 elif ifm_dtype.size_in_bits() == 16:
882 if ifm_dtype.type & BaseType.Signed:
Diqing Zhongfed918b2020-04-27 10:27:34 +0200883 prec = ifm_precision.S16
Tim Hall79d07d22020-04-27 18:20:16 +0100884 else:
Diqing Zhongfed918b2020-04-27 10:27:34 +0200885 prec = ifm_precision.U16
Tim Hall79d07d22020-04-27 18:20:16 +0100886
887 ifm_prec = prec.value
888 ifm2_prec = ifm_prec
889
890 if cmd.ifm_tensor.format == TensorFormat.NHCWB16:
891 ifm_prec |= 1 << 6
892
893 ifm_prec |= op_to_scale << 8
894
895 emit.cmd0_with_param(cmd0.NPU_SET_IFM_PRECISION, ifm_prec)
896
897 if cmd.ifm2_tensor is not None:
898 if cmd.ifm2_tensor.format == TensorFormat.NHCWB16:
899 ifm2_prec |= 1 << 6
900 emit.cmd0_with_param(cmd0.NPU_SET_IFM2_PRECISION, ifm2_prec)
901
902 emit_wait_commands(cmd)
903
904 # Get op parameters
905 cur_ifm_block_depth = get_op_ifmofm_block_depth(arch, cmd)
906 cur_ofm_block = Block(ps.block_config[1], ps.block_config[0], ps.block_config[3])
907 cur_ofm_rect = get_op_ofm_rect(cmd)
908 cur_ifm_rect = get_op_ifm_rect(cmd)
909 cur_kernel = get_op_kernel(cmd.ps)
910 cur_padLT = get_op_padding_lt(cmd)
911 if (prev_kernel is not None) and (cur_kernel is not None) and has_prev_op_dependency(prev_cmd, cmd):
912 if cmd.ifm_tensor.shape == prev_cmd.ofm_tensor.shape:
913 blockdep = arch.calc_block_dep(
914 prev_ifm_rect,
915 prev_ofm_rect,
916 prev_ifm_block_depth,
917 prev_ofm_block,
918 prev_kernel,
919 cur_ifm_rect,
920 cur_ofm_rect,
921 cur_ifm_block_depth,
922 cur_ofm_block,
923 cur_kernel,
924 cur_padLT,
925 )
926 else:
927 blockdep = 0
928 else:
929 blockdep = ArchitectureFeatures.MAX_BLOCKDEP
930
931 # Set between every op (dependent or not)
932 blockdep = min(blockdep, arch.max_blockdep)
933 emit.cmd0_with_param(cmd0.NPU_SET_BLOCKDEP, blockdep)
934 prev_cmd = cmd
935
936 if npu_block_type == NpuBlockType.ConvolutionMxN:
937 emit.cmd_do_operation(cmd0.NPU_OP_CONV)
938 elif npu_block_type == NpuBlockType.ConvolutionDepthWise:
939 emit.cmd_do_operation(cmd0.NPU_OP_DEPTHWISE)
940 elif npu_block_type == NpuBlockType.VectorProduct:
941 # Vector product is implemented using a 1x1 convolution
942 emit.cmd_do_operation(cmd0.NPU_OP_CONV)
943 elif npu_block_type == NpuBlockType.Pooling:
944 param = "Max" not in primary_op.type
945 emit.cmd_do_operation(cmd0.NPU_OP_POOL, param=param)
946 elif npu_block_type == NpuBlockType.ElementWise:
947 param = elementwise_mode_map[primary_op.type]
948 emit.cmd_do_operation(cmd0.NPU_OP_ELEMENTWISE, param)
949 else:
950 print("Warning: Skipping register command stream generation for", ps)
951
952 # Fill in final part of command stream:
953 emit.cmd_do_operation(cmd0.NPU_OP_STOP, param=0xFFFF)
954
955 sg.register_command_stream = emit.to_list()
956 if verbose:
957 emit.print_cmds()
958 print("number of commands", len(emit.cmd_stream))
959 print("command stream length in words", len(sg.register_command_stream))