Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_cpp/TTL_tiles.h
Go to the documentation of this file.
1/*
2 * TTL_tiles.h
3 *
4 * Copyright (c) 2025 Mobileye
5 *
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#pragma once
20
21#include "TTL_types.h"
22
23/******************************************************
24 * AUGMENTATION
25 *****************************************************/
26
27/**
28 * @brief Augment an input tensor with logical padding
29 *
30 * When an input tensor is imported using a tiler the resulting tensors
31 * may have elements beyond the space of the original tensor. In terms of the
32 * origin tensor the subtensor may for example start at offset(-1, -1, -1) in
33 * this case the elements at (-1, -1, -1), (-1, 0, 0) etc. needed to be created
34 * from a process of augmentation.
35 *
36 * Currently the only dynamic part of the augmentation is the size of the
37 * augmentation the augmented values are simply hardcode.
38 *
39 * A TTL_augmented_dim is the number of elements to augment.
40 */
41
42typedef unsigned char TTL_augmented_dim; ///< A number of a "augmented" elements in the unit of elements
43
44/**
45 * @brief 3D description of the augmented margins
46 *
47 * TTL_augmentation represents the number of elements that a tensor will be expanded in each dimension.
48 *
49 * For example if left = 1 then one column of elements will be added to a tensor thereby increasing its width by 1.
50 *
51 * The type used to hold the augmentation of tiles along all dimensions
52 */
54 /**
55 * @brief Create a 3D Description of a Tile augmentation
56 *
57 * @see TTL_overlap for more information.
58 *
59 * @param left ///< Left hand augmentation in elements
60 * @param right ///< Right hand augmentation in elements
61 * @param top ///< Top augmentation in elements
62 * @param bottom ///< Bottom augmentation in elements
63 * @param front ///< Front augmentation in elements
64 * @param back ///< Back augmentation in elements
65 *
66 * @return A TTL_augmentation describing in 3D the overlap requested.
67 */
72
73 TTL_augmented_dim left; ///< Left hand augmentation in elements
74 TTL_augmented_dim right; ///< Right hand augmentation in elements
75 TTL_augmented_dim top; ///< Top augmentation in elements
76 TTL_augmented_dim bottom; ///< Bottom augmentation in elements
77 TTL_augmented_dim front; ///< Front augmentation in elements
78 TTL_augmented_dim back; ///< Back augmentation in elements
79};
80
81/***
82 * @brief A Tile is described by its Shape and the offset from the beginning of
83 * the Space
84 *
85 * The type used to hold a tile of a space having a given shape and offset from
86 * the beginning of the space
87 */
88struct TTL_tile {
89 /**
90 * @brief Create an empty tile. Empty means it has all dimensions set to zero
91 *
92 * Most operations on an empty tile should turn into no-ops and so an empty tile
93 * is the safest default state.
94 */
95 TTL_tile() : shape(), offset() {}
96
97 /**
98 * @brief Check if the tile passed is empty.
99 *
100 * Empty is defined as width of the shape being equal to 0.
101 *
102 * @param tile The tile to check the emptiness of.
103 *
104 * @return true if shape is empty
105 * @return false if shape is not empty
106 */
107 bool empty() const {
108 return shape.empty();
109 }
110
111 TTL_shape shape; ///< @see TTL_shape
112 TTL_offset offset; ///< @see TTL_offset
113};
114
115/**
116 * @brief TTL_tiler is the basic unit that describes how a tile is subdivided.
117 *
118 * The TTL_tiler type represents the tiling of a 3D space into 3D tiles with
119 * operational overlap
120 */
121struct TTL_tiler {
122 /**
123 * @brief Return a TTL_tiler based on a shape, a tile, and an overlap
124 *
125 * @param tensor_shape The shape to be tiled
126 * @param tile_shape The description of the tile that the shape will be sub-divided to.
127 * @param overlap The overlap between tiles
128 * @param augmentation The augomentation to apply at the edges durring import.
129 *
130 * Complete description of what not how here.
131 *
132 * @return A tiler that can produce a tile for any given index.
133 */
134 TTL_tiler(const TTL_shape tensor_shape, const TTL_shape tile_shape, const TTL_overlap overlap,
136 : space(tensor_shape), tile(tile_shape), overlap(overlap), augmentation(augmentation) {
137 const TTL_dim tiles_in_width =
138 TTL_ceil_of_a_div_b(tensor_shape.width + augmentation.left + augmentation.right - overlap.width,
139 tile_shape.width - overlap.width);
141 TTL_ceil_of_a_div_b(tensor_shape.height + augmentation.top + augmentation.bottom - overlap.height,
142 tile_shape.height - overlap.height);
143 const TTL_dim tiles_in_depth =
144 TTL_ceil_of_a_div_b(tensor_shape.depth + augmentation.front + augmentation.back - overlap.depth,
145 tile_shape.depth - overlap.depth);
146
149
151 }
152
153 // Simplify creation of non-overlap tiler
154 TTL_tiler(const TTL_shape shape, const TTL_shape tile)
155 : TTL_tiler(shape, tile, TTL_overlap(), TTL_augmentation()) {}
156
157 TTL_shape space; ///< Represents the space to be tiled such as an image
158 TTL_shape tile; ///< All tiles will be of this shape, except for clamping at
159 ///< the end of the space
160 TTL_overlap overlap; ///< When zeroes represent no overlap
161 TTL_augmentation augmentation; ///< The augmentation that the tile produces.
162
163 /**
164 * @brief Precomputed information to speed up later reuse
165 */
166 struct {
173
174 /**
175 * @brief Return the number of tiles that this tile can produce.
176 *
177 * @return int The number of tiles produced by the tiler.
178 */
179 int number_of_tiles() const {
180 return cache.number_of_tiles;
181 }
182
183 /**
184 * @brief Given a tile ID return true or false to indicate if the id is valid
185 *
186 * @param tile_id The ID to validate.
187 *
188 * @return int The number of tiles produced by the tiler.
189 */
190 inline int valid_tile_id(int tile_id) const {
191 return ((tile_id >= 0) && (tile_id < (int)cache.number_of_tiles));
192 }
193
194 /**
195 * @brief Return the ceil value of a/b i.e. ceil(a/b)
196 *
197 * Implementation of ceil(a/b) without requiring a library or floating-point.
198 *
199 * Internal TTL function not part of the API.
200 *
201 * @param a The dividend to use in the calculation
202 * @param b The divisor to use in the calculation
203 *
204 * @todo b is a shape dimension of a tensor, so it is not zero. Remove dynamic
205 * check for zero b, w/o crashing the compiler.
206 */
207 inline int TTL_ceil_of_a_div_b(const int a, const int b) const {
208 return b ? ((a + b - 1) / b) : 0;
209 }
210
212 return cache.tiles_in_width;
213 }
214
216 return cache.tiles_in_height;
217 }
218
220 return cache.tiles_in_depth;
221 }
222
223 /**
224 * @brief Returns a tile at a position from a tiler and respective coordinates.
225 *
226 * @param x The x position of the tile being created
227 * @param y The y position of the tile being created
228 * @param z The z position of the tile being created
229 * @param tiler The tiler from which the tiler can be calculated.
230 *
231 * @return The created TTL_tile type
232 */
234 TTL_tile result;
235
236 // Calculate the offset in 3D
237 result.offset = TTL_offset((x * (tile.width - overlap.width)) - augmentation.left,
238 (y * (tile.height - overlap.height)) - augmentation.top,
239 (z * (tile.depth - overlap.depth)) - augmentation.front);
240
241 // Set the tile shape, clamping at the end of each dimension
242 result.shape = tile;
243
244 if (x == cache.tiles_in_width - 1) result.shape.width = space.width - result.offset.x + augmentation.right;
245
246 if (y == cache.tiles_in_height - 1) result.shape.height = space.height - result.offset.y + augmentation.bottom;
247
248 if (z == cache.tiles_in_depth - 1) result.shape.depth = space.depth - result.offset.z + augmentation.back;
249
250 return result;
251 }
252
253 /**
254 * @brief Return the tile_id'th tile of a tile array in row-major order.
255 *
256 * Return the tile_id'th tile, starting from tile_id=0, in row-major order.
257 * Returns an invalid tile if tile_id is not valid (not from [0,
258 number_of_tiles))
259
260 * @param tile_id The tile id to return - if out of bounds then an invalid tile
261 is returned
262 * @param tiler The containing with the shape and tiling information
263 *
264 * @return The tile that is represented by tile_id when interpreted in row-major
265 order.
266 */
267 TTL_tile get_tile(const int tile_id) const {
268 if (valid_tile_id(tile_id) == false) {
269 TTL_tile invalid;
270 return invalid;
271 }
272
273 // Compute the 3D coordinates of the tile in order to compute its offset
274 const TTL_dim z = tile_id / cache.tiles_in_plane;
275 const TTL_dim tid_in_plane = tile_id % cache.tiles_in_plane;
276 const TTL_dim y = tid_in_plane / cache.tiles_in_width;
277 const TTL_dim x = tid_in_plane % cache.tiles_in_width;
278
279 return create_tile(x, y, z);
280 }
281
282 /**
283 * @brief Return the tile_id'th tile of a tile array in column-major order.
284 *
285 * Return the tile_id'th tile, starting from tile_id=0, in column-major order.
286 * Returns an invalid tile if tile_id is not valid (not from [0,
287 number_of_tiles))
288
289 * @param tile_id The tile id to return - if out of bounds then an invalid tile
290 is returned
291 * @param tiler The tiler containing the shape and tiling information
292 *
293 * @return The tile that is represented by tile_id when interpreted in
294 column-major order.
295 */
296 TTL_tile get_tile_column_major(const int tile_id) const {
297 // Compute the 3D coordinates of the tile in order to compute its offset
298 const TTL_dim z = tile_id / cache.tiles_in_plane;
299 const TTL_dim tid_in_plane = tile_id % cache.tiles_in_plane;
300 const TTL_dim y = tid_in_plane % cache.tiles_in_height;
301 const TTL_dim x = tid_in_plane / cache.tiles_in_height;
302
303 return create_tile(x, y, z);
304 }
305};
unsigned char TTL_augmented_dim
Augment an input tensor with logical padding.
uint32_t TTL_dim
The type used to hold the size of an object along any dimension.
3D description of the augmented margins
TTL_augmented_dim right
Right hand augmentation in elements.
TTL_augmented_dim bottom
Bottom augmentation in elements.
TTL_augmented_dim back
Back augmentation in elements.
TTL_augmentation(const TTL_augmented_dim left=0, const TTL_augmented_dim right=0, const TTL_augmented_dim top=0, const TTL_augmented_dim bottom=0, const TTL_augmented_dim front=0, const TTL_augmented_dim back=0)
Create a 3D Description of a Tile augmentation.
TTL_augmented_dim front
Front augmentation in elements.
TTL_augmented_dim top
Top augmentation in elements.
TTL_augmented_dim left
Left hand augmentation in elements.
Description of the 3D offset of an object.
TTL_offset_dim z
Offset in dimension z.
TTL_offset_dim y
Offset in dimension y.
TTL_offset_dim x
Offset in dimension x.
Description of the overlap in 3D space of adjacent tiles.
Description of a Shape.
TTL_dim height
Number of rows along dimension y.
TTL_dim depth
Number of planes along dimension z.
TTL_dim width
Number of elements along dimension x.
TTL_offset offset
TTL_shape shape
TTL_tile()
Create an empty tile. Empty means it has all dimensions set to zero.
bool empty() const
Check if the tile passed is empty.
TTL_tiler(const TTL_shape tensor_shape, const TTL_shape tile_shape, const TTL_overlap overlap, const TTL_augmentation augmentation)
Return a TTL_tiler based on a shape, a tile, and an overlap.
TTL_shape space
Represents the space to be tiled such as an image.
int TTL_ceil_of_a_div_b(const int a, const int b) const
Return the ceil value of a/b i.e. ceil(a/b)
TTL_overlap overlap
When zeroes represent no overlap.
int valid_tile_id(int tile_id) const
Given a tile ID return true or false to indicate if the id is valid.
TTL_dim tiles_in_height
TTL_dim tiles_in_width
TTL_dim tiles_in_depth() const
TTL_dim tiles_in_height() const
TTL_dim tiles_in_depth
TTL_augmentation augmentation
The augmentation that the tile produces.
struct TTL_tiler::@153017064222336267371240241203244373062067161267 cache
Precomputed information to speed up later reuse.
TTL_tile create_tile(TTL_dim x, TTL_dim y, TTL_dim z) const
Returns a tile at a position from a tiler and respective coordinates.
TTL_dim number_of_tiles
TTL_dim tiles_in_plane
TTL_tile get_tile(const int tile_id) const
Return the tile_id'th tile of a tile array in row-major order.
TTL_dim tiles_in_width() const
int number_of_tiles() const
Return the number of tiles that this tile can produce.
TTL_tiler(const TTL_shape shape, const TTL_shape tile)
TTL_tile get_tile_column_major(const int tile_id) const
Return the tile_id'th tile of a tile array in column-major order.