blob: 3dda1793da5d845a7d2654fe70cc1a3ba7037d73 [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# Unit tests for LUT support
18import numpy as np
19
20from ethosu.vela import insert_dma
21from ethosu.vela import lut
22from ethosu.vela import mark_tensors
23from ethosu.vela import pass_packing
24from ethosu.vela.data_type import DataType
25from ethosu.vela.high_level_command_stream import DMA
26from ethosu.vela.nn_graph import Graph
27from ethosu.vela.rewrite_graph import verify_graph_health
28from ethosu.vela.tensor import create_const_tensor
29from ethosu.vela.tensor import TensorPurpose
30from ethosu.vela.test import testutil
31
32
33def set_256_lut(op, key):
34 values = list(range(256))
35 lut_tensor = create_const_tensor(
36 op.name + "_lut", [1, 1, 1, 256], DataType.int8, values, np.uint8, TensorPurpose.LUT
37 )
38 lut_tensor.equivalence_id = lut.create_equivalence_id(key)
39 op.set_activation_lut(lut_tensor)
40
41
42def set_1K_lut(op, key):
43 values = list(range(256))
44 lut_tensor = create_const_tensor(
45 op.name + "_lut", [1, 1, 1, 256], DataType.int32, values, np.uint32, TensorPurpose.LUT
46 )
47 lut_tensor.equivalence_id = lut.create_equivalence_id(key)
48 op.set_activation_lut(lut_tensor)
49
50
51def set_2K_lut(op, key):
52 values = list(range(512))
53 lut_tensor = create_const_tensor(
54 op.name + "_lut", [1, 1, 1, 512], DataType.int32, values, np.uint32, TensorPurpose.LUT
55 )
56 lut_tensor.equivalence_id = lut.create_equivalence_id(key)
57 op.set_activation_lut(lut_tensor)
58
59
60def process(arch, op_list):
61 # Returns subgraph with given operations
62 nng = Graph()
63 sg = testutil.create_subgraph(op_list)
64 nng.subgraphs.append(sg)
65 assert verify_graph_health(nng)
66 nng = mark_tensors.mark_tensor_purpose(nng, arch, False)
67 assert verify_graph_health(nng)
68 nng = insert_dma.insert_dma_commands(nng, arch, False)
69 assert verify_graph_health(nng)
70 pass_packing.pack_into_passes(nng, arch, False)
71 assert verify_graph_health(nng)
72 # Create a DMA instruction for every op
73 cmd_list = []
74 for ps in sg.passes:
75 for intermediate in ps.intermediates:
76 if intermediate.needs_dma():
77 cmd_list.append(DMA(ps, intermediate.get_dma_src_tensor(), intermediate, None))
78 sg.high_level_command_stream = cmd_list
79 return sg
80
81
Patrik Gustavssone5cf95b2020-09-03 16:39:52 +020082def filter_lut_cmds(cmd_list):
83 lut_cmd_list = []
84 for cmd in cmd_list:
85 if "lut" in cmd.in_tensor.name:
86 lut_cmd_list.append(cmd)
87 return lut_cmd_list
88
89
Louis Verhaard0b8268a2020-08-05 16:11:29 +020090def test_optimize_high_level_cmd_stream_2K():
91 # Tests lut.optimize_high_level_cmd_stream, blending 256 byte and 2K luts
92 arch = testutil.create_arch()
93 shape = [1, 1, 1, 1]
94 # u8 LUT op, should lead to DMA
95 op0 = testutil.create_elemwise_op("AddAct", "op0", shape, shape, shape)
96 set_256_lut(op0, "lut0")
97 # u8 LUT op, should lead to DMA
98 op1 = testutil.create_elemwise_op("AddAct", "op1", shape, shape, shape)
99 set_256_lut(op1, "lut1")
100 # u8 LUT op with different LUT, should lead to DMA
101 op2 = testutil.create_elemwise_op("AddAct", "op2", shape, shape, shape)
102 set_256_lut(op2, "lut2")
103 # u8 LUT op with same LUT as in op1, should not lead to DMA
104 op3 = testutil.create_elemwise_op("AddAct", "op3", shape, shape, shape)
105 set_256_lut(op3, "lut1")
106 # u8 LUT op with same LUT as in op2, should not lead to DMA
107 op4 = testutil.create_elemwise_op("AddAct", "op4", shape, shape, shape)
108 set_256_lut(op4, "lut2")
109 # 2K LUT op, should lead to DMA, and will overwrite all previous LUTs in SHRAM
110 op5_2K = testutil.create_elemwise_op("AddAct", "op5", shape, shape, shape)
111 set_2K_lut(op5_2K, "lut5")
112 # Another 2K LUT op, should lead to DMA, and will overwrite the previous LUT in SHRAM
113 op6_2K = testutil.create_elemwise_op("AddAct", "op6", shape, shape, shape)
114 set_2K_lut(op6_2K, "lut6")
115 # u8 LUT op with same LUT as in op1, should lead to DMA
116 op7 = testutil.create_elemwise_op("AddAct", "op7", shape, shape, shape)
117 set_256_lut(op7, "lut1")
118
119 op_list = [op0, op1, op2, op3, op4, op5_2K, op6_2K, op7]
120 sg = process(arch, op_list)
121 orig_cmd_list = sg.high_level_command_stream
122 sg.high_level_command_stream = orig_cmd_list
123 lut.optimize_high_level_cmd_stream(sg, arch)
124 cmd_list = sg.high_level_command_stream
125 # Check that only the needed DMA commands are left
126 expected_dma_ops = [op0, op1, op2, op5_2K, op6_2K, op7]
Patrik Gustavssone5cf95b2020-09-03 16:39:52 +0200127
128 cmd_list = filter_lut_cmds(cmd_list)
129 orig_cmd_list = filter_lut_cmds(orig_cmd_list)
130
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200131 for (cmd, op) in zip(cmd_list, expected_dma_ops):
132 assert cmd.in_tensor == op.activation_lut
133 # Check that lut0, lut1 and lut2 in op0, op1, op2 are stored on different addresses
134 assert orig_cmd_list[0].out_tensor.address != orig_cmd_list[1].out_tensor.address
135 assert orig_cmd_list[0].out_tensor.address != orig_cmd_list[2].out_tensor.address
136 assert orig_cmd_list[1].out_tensor.address != orig_cmd_list[2].out_tensor.address
137 # Check that lut1 in op1 and op3 have same address
138 assert orig_cmd_list[1].out_tensor.address == orig_cmd_list[3].out_tensor.address
139 # Check that lut2 in op2 and op4 have same address
140 assert orig_cmd_list[2].out_tensor.address == orig_cmd_list[4].out_tensor.address
141 # Check that lut-s for 16 bit (op5 and op6) are stored on same address
142 assert orig_cmd_list[5].out_tensor.address == orig_cmd_list[6].out_tensor.address
143
144
145def test_optimize_high_level_cmd_stream_1K():
146 # Tests lut.optimize_high_level_cmd_stream, blending 256 and 1K luts
147 arch = testutil.create_arch()
148 shape = [1, 1, 1, 1]
149 # u8 LUT op, should lead to DMA
150 op0 = testutil.create_elemwise_op("AddAct", "op0", shape, shape, shape)
151 set_256_lut(op0, "lut0")
152 # u8 LUT op, should lead to DMA
153 op1 = testutil.create_elemwise_op("AddAct", "op1", shape, shape, shape)
154 set_256_lut(op1, "lut1")
155 # 1K LUT op with different LUT, should lead to DMA
156 op2_1K = testutil.create_elemwise_op("AddAct", "op2", shape, shape, shape)
157 set_1K_lut(op2_1K, "lut2")
158 # u8 LUT op with same LUT as in op1, should not lead to DMA
159 op3 = testutil.create_elemwise_op("AddAct", "op3", shape, shape, shape)
160 set_256_lut(op3, "lut1")
161 # 1K LUT op with same LUT as in op2, should not lead to DMA
162 op4_1K = testutil.create_elemwise_op("AddAct", "op4", shape, shape, shape)
163 set_1K_lut(op4_1K, "lut2")
164 # 1K LUT op, should lead to DMA, and will overwrite lut2
165 op5_2K = testutil.create_elemwise_op("AddAct", "op5", shape, shape, shape)
166 set_1K_lut(op5_2K, "lut5")
167 # u8 LUT op, lut0 should still be present, should not lead to DMA
168 op6 = testutil.create_elemwise_op("AddAct", "op6", shape, shape, shape)
169 set_256_lut(op6, "lut0")
170 # 1K LUT op with same LUT as in op2, should lead to DMA
171 op7 = testutil.create_elemwise_op("AddAct", "op7", shape, shape, shape)
172 set_1K_lut(op7, "lut2")
173
174 op_list = [op0, op1, op2_1K, op3, op4_1K, op5_2K, op6, op7]
175 sg = process(arch, op_list)
176 orig_cmd_list = sg.high_level_command_stream
177 sg.high_level_command_stream = orig_cmd_list
178 lut.optimize_high_level_cmd_stream(sg, arch)
179 cmd_list = sg.high_level_command_stream
Patrik Gustavssone5cf95b2020-09-03 16:39:52 +0200180
181 cmd_list = filter_lut_cmds(cmd_list)
182 orig_cmd_list = filter_lut_cmds(orig_cmd_list)
183
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200184 # Check that only the needed DMA commands are left
185 expected_dma_ops = [op0, op1, op2_1K, op5_2K, op7]
186 for (cmd, op) in zip(cmd_list, expected_dma_ops):
187 assert cmd.in_tensor == op.activation_lut
188 # Check that lut0, lut1 and lut2 in op0, op1, op2 are stored on different addresses
189 assert orig_cmd_list[0].out_tensor.address != orig_cmd_list[1].out_tensor.address
190 assert orig_cmd_list[0].out_tensor.address != orig_cmd_list[2].out_tensor.address
191 assert orig_cmd_list[1].out_tensor.address != orig_cmd_list[2].out_tensor.address
192 # Check that lut1 in op1 and op3 have same address
193 assert orig_cmd_list[1].out_tensor.address == orig_cmd_list[3].out_tensor.address
194 # Check that lut2 in op2 and op4 and op7 have same address
195 assert orig_cmd_list[2].out_tensor.address == orig_cmd_list[4].out_tensor.address
196 assert orig_cmd_list[2].out_tensor.address == orig_cmd_list[7].out_tensor.address