Tensor Tiling Library
 
Loading...
Searching...
No Matches
TTL_tensors_common.h
Go to the documentation of this file.
1/*
2 * TTL_tensors_common.h
3 *
4 * Copyright (c) 2023 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/************************************************************************************************************
20 * Allow for sizeof anything - deals with sizeof(void)
21 ************************************************************************************************************/
22
23/**
24 * @def TTL_SIZEOF
25 *
26 * @brief opencl doesn't like sizeof(void) so for it to be 1 like normal c
27 *
28 * OpenCl will produce error: invalid application of 'sizeof' to a void type
29 *
30 * This will need to be expanded with more types - or a different language used!
31 */
32
33/************************************************************************************************************
34 * Define Layout
35 ***********************************************************************************************************/
36
37/**
38 * @brief Description of a Tensor layout in memory
39 *
40 * Each logical tensor is embedded in both global and local memories within some
41 * enclosing physical tensors.
42 *
43 * This embedding is referred to as 'layout', which specifies the actual distance in elements
44 * between the start of consecutive data elements in each dimension.
45 *
46 * For the first axis the distance is always 1 element and and so this value is not stored.
47 *
48 * line_length and plane_area in memory, in units of an element.
49 */
50typedef struct {
51 TTL_dim_t row_spacing; ///< The distance between the start of consequtive rows in units of elements.
52 TTL_dim_t plane_spacing; ///< The distance between the start of consequtive planes in units of elements.
54
55/**
56 * @brief Create a 3D Description of a Tensor layout in memory
57 *
58 * @see TTL_layout_t for more information.
59 *
60 * @param row_spacing; The distance between the start of consequtive rows in units of elements.
61 * @param plane_spacing; The distance between the start of consequtive planes in units of elements.
62 *
63 * @return A TTL_layout_t describing in 3D the layout requested.
64 */
65static inline TTL_layout_t __attribute__((overloadable)) TTL_create_layout(const TTL_dim_t row_spacing,
66 const TTL_dim_t plane_spacing) {
67 const TTL_layout_t res = { row_spacing, plane_spacing };
68 return res;
69}
70
71/**
72 * @brief Create a 2D Description of a Tensor layout in memory
73 *
74 * @see TTL_layout_t for more information.
75 *
76 * @param row_spacing The distance between the start of consequtive rows in units of elements.
77 *
78 * The plane spacing is set to 0 - as no planes exist to be spaced.
79 *
80 * @return A TTL_layout_t describing in 3D the layout requested.
81 */
82static inline TTL_layout_t __attribute__((overloadable)) TTL_create_layout(const TTL_dim_t row_spacing) {
83 return TTL_create_layout(row_spacing, 0);
84}
85
86/**
87 * @brief Create a 1D Description of a Tensor layout in memory
88 *
89 * @see TTL_layout_t for more information.
90 *
91 * The row spacing is set to 0 - as no rows exist to be spaced.
92 * The plane spacing is set to 0 - as no planes exist to be spaced.
93 **
94 * @return A TTL_layout_t describing in 3D the layout requested.
95 */
96static inline TTL_layout_t __attribute__((overloadable)) TTL_create_layout(void) {
97 return TTL_create_layout(0, 0);
98}
99
100/**
101 * @brief Calculate the absolute linear offset in elements, based on a given
102 * tensor offset and layout
103 *
104 * @param offset The 3D offset of the required linear offset.
105 * @param layout The layout of the offset being calculated
106 *
107 * @return The offset in linear address space of the 3D offset
108 */
109static inline TTL_offset_dim_t TTL_linearize(const TTL_offset_t offset, const TTL_layout_t layout) {
110 return ((offset.z * layout.plane_spacing) + (offset.y * layout.row_spacing) + offset.x);
111}
112
113/************************************************************************************************************
114 * Structure of a tensor
115 ***********************************************************************************************************/
116
117/**
118 * @def __TTL_typedef_tensor_t
119 *
120 * @brief A poor mans base class for an a tensor in the passed address space
121 *
122 * TTL_int_tensor_base_t contains both the logical dimensions of a tile as well as
123 * its physical mapping to memory.
124 *
125 * @param TTL_scope The scope of the creation can be TTL_global or TTL_local
126 * @param const_1 The const name to place after the prefix - should be empty or const_
127 * @param location The location of the tensor - should be ext or int
128 * @param type The type of the tensor - should be any valid c type
129 * @param const_2 The const type to create - should be empty or const
130 */
131/**
132 * @def __TTL_typedef_sub_tensor_t
133 *
134 * @brief A tensor plus its reference to its parent tensor
135 *
136 * __TTL_sub_tensor_t contains both the logical dimensions of a tile as well as
137 * its physical mapping to memory.
138 *
139 * @param TTL_scope The scope of the creation can be TTL_global or TTL_local
140 * @param const_1 The const name to place after the prefix - should be empty or const_
141 * @param location The location of the tensor - should be ext or int
142 * @param type The type of the tensor - should be any valid c type
143 * @param const_2 The const type to create - should be empty or const
144 */
145/************************************************************************************************************
146 * Creation of tensors
147 ************************************************************************************************************/
148
149/**
150 * @brief Implementation of TTL_create_int_tensor
151 *
152 * @see TTL_create_int_tensor for full API and parameter information
153 *
154 * @param TTL_scope The scope of the creation can be TTL_global or TTL_local
155 * @param const_1 The const name to place after the prefix - should be empty or const_
156 * @param location The location of the tensor - should be ext or int
157 * @param type The type of the tensor - should be any valid c type
158 * @param const_2 The const type to create - should be empty or const
159 */
160/**
161 * @brief Implementation of TTL_create_int_sub_tensor
162 *
163 * @see TTL_create_int_sub_tensor for full API and parameter information
164 */
165/************************************************************************************************************
166 * Create the create tensor functions for different overloads
167 ************************************************************************************************************/
static TTL_layout_t TTL_create_layout(void)
Create a 1D Description of a Tensor layout in memory.
static TTL_offset_dim_t TTL_linearize(const TTL_offset_t offset, const TTL_layout_t layout)
Calculate the absolute linear offset in elements, based on a given tensor offset and layout.
unsigned int TTL_dim_t
The type used to hold the size of an object along any dimension.
int TTL_offset_dim_t
The type used to hold offsets and origins.
Description of a Tensor layout in memory.
TTL_dim_t row_spacing
The distance between the start of consequtive rows in units of elements.
TTL_dim_t plane_spacing
The distance between the start of consequtive planes in units of elements.
Description of the 3D offset of an object.
TTL_offset_dim_t z
Offset in dimension z.
TTL_offset_dim_t y
Offset in dimension y.
TTL_offset_dim_t x
Offset in dimension x.