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