blob: 8e28b953dd796047f15d195f779292de23df526e [file] [log] [blame]
Louis Verhaard0b8268a2020-08-05 16:11:29 +02001# 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# Description:
17# Functionality for lookup table support.
18import uuid
Louis Verhaard0b8268a2020-08-05 16:11:29 +020019
Louis Verhaardb9fc33c2020-08-13 11:47:36 +020020import numpy as np
21
Louis Verhaard0b8268a2020-08-05 16:11:29 +020022from . import numeric_util
23from .high_level_command_stream import CommandType
Louis Verhaardb9fc33c2020-08-13 11:47:36 +020024from .tensor import create_const_tensor
Louis Verhaard9db529a2020-09-23 10:27:11 +020025from .tensor import create_equivalence_id
Louis Verhaard0b8268a2020-08-05 16:11:29 +020026from .tensor import TensorPurpose
27
28
Louis Verhaard0b8268a2020-08-05 16:11:29 +020029class LUTState:
30 # Tracks which LUT-s are located in SHRAM.
31 def __init__(self):
32 self.tensors = []
33
34 def get_equivalent(self, lut_tens):
Jacob Bohlin1a666972020-09-11 10:04:15 +020035 # Returns existing lut with the same values, None if not found
Louis Verhaard0b8268a2020-08-05 16:11:29 +020036 for t in self.tensors:
Jacob Bohlin1a666972020-09-11 10:04:15 +020037 if np.array_equal(t.values, lut_tens.values):
Louis Verhaard0b8268a2020-08-05 16:11:29 +020038 return t
39 return None
40
41 def put(self, lut_tens):
42 # Returns new LUT state containing given tensor + all tensors in this state
43 # that do not overlap with the given tensor
44 new_state = LUTState()
45 new_state.tensors.append(lut_tens)
46 start = lut_tens.address
47 end = start + lut_tens.storage_size()
48 for tens in self.tensors:
49 start2 = tens.address
50 end2 = start2 + tens.storage_size()
51 if not numeric_util.overlaps(start, end, start2, end2):
52 new_state.tensors.append(tens)
Jacob Bohlin1a666972020-09-11 10:04:15 +020053
Louis Verhaard0b8268a2020-08-05 16:11:29 +020054 return new_state
55
56 def find_best_address(self, start, stop, step):
57 # Finds the address in the given range that overlaps with the minimum number of
58 # currently present LUT-s.
59 # An improvement would be to also take future LUT usage into account
60 best_addr = start
61 best_nr_overlaps = stop
62 for addr in range(start, stop, step):
63 nr_overlaps = 0
64 for tens in self.tensors:
65 start2 = tens.address
66 end2 = start2 + tens.storage_size()
67 if numeric_util.overlaps(addr, addr + step, start2, end2):
68 nr_overlaps += 1
69 if nr_overlaps < best_nr_overlaps:
70 best_nr_overlaps = nr_overlaps
71 best_addr = addr
72 return best_addr
73
74
75def get_lut_index(arch, lut_tensor):
76 # Returns the index in SHRAM where the given LUT is stored, a value between 0 and 8
77 slot = (lut_tensor.address - arch.shram_lut_address) // lut_tensor.storage_size()
78 assert 0 <= slot < 8
79 return slot
80
81
Louis Verhaardb9fc33c2020-08-13 11:47:36 +020082def create_lut_tensor(name, values, dtype):
83 # Creates constant LUT tensor with the given values as lookup table.
84 # The tensor's equivalence_id is based on these values, so if multiple
85 # LUT tensors are created with identical values, they will get the same
86 # address in constant memory, and unnecessary DMA operations can be avoided.
87 sz = len(values)
88 assert sz in (256, 512)
89 ntype = np.uint8 if dtype.size_in_bytes() == 1 else np.uint32
90 tens = create_const_tensor(name, [1, 1, 1, sz], dtype, values, ntype, TensorPurpose.LUT)
91 tens.equivalence_id = create_equivalence_id(tuple(values))
92 return tens
93
94
Louis Verhaard0b8268a2020-08-05 16:11:29 +020095def optimize_high_level_cmd_stream(sg, arch):
96 # - Allocates SHRAM address/lut index to LUT tensors
97 # - Removes unnecessary DMA operations of LUT-s that are already present in SHRAM from sg's command stream
98 cmd_stream = [] # will contain existing command stream minus unneeded DMA operations
99 lut_state = LUTState()
100 slot_size = 256
101 lut_start = arch.shram_lut_address
102 lut_end = lut_start + arch.shram_lut_size
103 for cmd in sg.high_level_command_stream:
104 if cmd.cmdtype == CommandType.NpuStripe and cmd.ps.lut_tensor is None and arch.shram_reserved_unused_banks == 0:
105 # The command overwrites the last 2 banks containing the LUT; next LUT operation will require DMA
106 # TODO: check the command's SHRAM usage in more detail to determine if the LUT is overwritten or not
107 lut_state = LUTState()
108 if cmd.cmdtype != CommandType.DMA or cmd.out_tensor.purpose != TensorPurpose.LUT:
109 # Non-LUT operation; leave untouched
110 cmd_stream.append(cmd)
111 continue
112 # LUT DMA operation
113 lut_tens = cmd.out_tensor
114 existing_tens = lut_state.get_equivalent(lut_tens)
115 if existing_tens is not None:
116 # LUT is already in SHRAM, no need to perform DMA
117 lut_tens.address = existing_tens.address
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100118 cmd.ps.primary_op.activation.lut_index = get_lut_index(arch, existing_tens)
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200119 continue
120 # Place the LUT in the last 2 blocks of SHRAM
121 # Alignment is always on the size of the LUT, 256 for 256-byte LUT, 1K for 1K LUT, etc
122 address = lut_state.find_best_address(lut_start, lut_end, lut_tens.storage_size())
Jacob Bohlin1a666972020-09-11 10:04:15 +0200123 lut_tens.equivalence_id = uuid.uuid4()
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200124 lut_tens.address = address
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100125 cmd.ps.primary_op.activation.lut_index = (address - lut_start) // slot_size
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200126 lut_state = lut_state.put(lut_tens)
127 cmd_stream.append(cmd)
128 sg.high_level_command_stream = cmd_stream