ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
luci::sinf Namespace Reference

Data Structures

class  Algorithm
 
struct  Rule
 
struct  StridedSliceContext
 
struct  StridedSliceParams
 
class  TensorShapeExpander
 Create a higher-rank TensorShape following NumPy broadcasting semantics. More...
 

Functions

loco::TensorShape circle_shape (const luci::CircleNode *node)
 
loco::TensorShape broadcast_shape (const loco::TensorShape &x, const loco::TensorShape &y)
 
loco::TensorShape pad_shape (const loco::TensorShape &input_shape, const luci::CircleNode *paddings)
 
int Clamp (const int32_t v, const int32_t lo, const int32_t hi)
 
int64_t StartForAxis (const StridedSliceParams &params, const loco::TensorShape &input_shape, int64_t axis)
 
int64_t StopForAxis (const StridedSliceParams &params, const loco::TensorShape &input_shape, int64_t axis, int64_t start_for_axis)
 
StridedSliceParams BuildStridedSliceParams (StridedSliceContext *op_context)
 

Variables

const int kMaxDim = 5
 
const loco::DataType S32 = loco::DataType::S32
 

Function Documentation

◆ broadcast_shape()

loco::TensorShape luci::sinf::broadcast_shape ( const loco::TensorShape x,
const loco::TensorShape y 
)

Definition at line 152 of file CircleShapeInferenceHelper.cpp.

153{
154 auto x_match = x;
155 auto y_match = y;
156
157 expand_rank(x_match, y_match);
158
159 auto output_shape = expand_dimension(x_match, y_match);
160
161 return output_shape;
162}
const luci_interpreter::RuntimeShape output_shape

References output_shape.

Referenced by luci::sinf::Algorithm::visit(), luci::sinf::Algorithm::visit(), luci::sinf::Algorithm::visit(), and luci::sinf::Algorithm::visit().

◆ BuildStridedSliceParams()

StridedSliceParams luci::sinf::BuildStridedSliceParams ( StridedSliceContext op_context)

Definition at line 250 of file CircleStridedSlice.cpp.

251{
252 StridedSliceParams op_params;
253
254 // The ellipsis_mask and new_axis_mask in op_params are not used. Those masks
255 // are processed here to update begin_mask, end_mask and the index range.
256 op_params.begin_mask = 0;
257 op_params.ellipsis_mask = 0;
258 op_params.end_mask = 0;
259 op_params.new_axis_mask = 0;
260 op_params.shrink_axis_mask = 0;
261
262 // Count indexes where the new_axis_mask is set but the ellipsis_mask is not.
263 loco::TensorShape begin_shape = circle_shape(op_context->begin);
264 const int64_t begin_count = static_cast<int64_t>(begin_shape.dim(0).value());
265 int64_t num_add_axis = 0;
266 for (int64_t i = 0; i < begin_count; ++i)
267 {
268 if (!((1LL << i) & op_context->params.ellipsis_mask) &&
269 ((1LL << i) & op_context->params.new_axis_mask))
270 {
271 num_add_axis++;
272 }
273 }
274
275 // Calculate the dims of input after adding new axises.
276 const int64_t effective_dims = op_context->input_dims + num_add_axis;
277
278 // If begin, end and strides are not fully provided, it means Ellipsis should
279 // be expanded to multiple dimensions (Ex: for spec [Ellipsis, 2] on a 3D
280 // input, the Ellipsis should be applied for the first 2 dimensions). Besides,
281 // If the new_axis_mask and the ellipsis_mask are set at the same index, the
282 // new_axis_mask will have no effect.
283 int64_t effective_ellipsis_mask = 0, effective_new_axis_mask = 0;
284 int64_t ellipsis_start_idx = effective_dims, expanded_ellipsis = 0;
285 for (int64_t i = 0; i < effective_dims;)
286 {
287 if ((1LL << i) & op_context->params.ellipsis_mask)
288 {
289 ellipsis_start_idx = i;
290 int64_t ellipsis_end_idx =
291 std::max(i + 1, std::min(i + 1 + num_add_axis + op_context->input_dims - begin_count,
292 effective_dims));
293 expanded_ellipsis = ellipsis_end_idx - ellipsis_start_idx - 1;
294
295 // Set bit for effective_ellipsis_mask.
296 for (; i < ellipsis_end_idx; ++i)
297 {
298 effective_ellipsis_mask |= (1LL << i);
299 }
300 continue;
301 }
302
303 if ((1LL << (i - expanded_ellipsis)) & op_context->params.new_axis_mask)
304 {
305 effective_new_axis_mask |= (1LL << i);
306 }
307 ++i;
308 }
309
310 // Calculate effective_input_shape and its corresponding begin, end, strides.
311 loco::TensorShape input_shape = circle_shape(op_context->input);
312 int64_t added_ellipsis = 0, added_axises = 0;
313 // make sure no overflow
314 assert(static_cast<uint32_t>(effective_dims) == effective_dims);
315 op_context->effective_input_shape.rank(effective_dims);
316
317 for (int64_t i = 0; i < effective_dims; ++i)
318 {
319 if ((1LL << i) & effective_ellipsis_mask)
320 {
321 // If ellipsis_mask, set the begin_mask and end_mask at that index.
322 added_ellipsis = std::max(int64_t(0), i - ellipsis_start_idx);
323 assert(i < 16);
324 op_params.begin_mask |= (1LL << i);
325 op_params.end_mask |= (1LL << i);
326 op_params.strides[i] = 1;
327 op_context->effective_input_shape.dim(i) = input_shape.dim(i - added_axises);
328 }
329 else if ((1LL << i) & effective_new_axis_mask)
330 {
331 // If new_axis_mask is set, it is equivalent to adding a new dim of 1 to
332 // input tensor. Store added shape to effective_input_shape.
333 op_params.start_indices[i] = 0;
334 op_params.stop_indices[i] = 1;
335 op_params.strides[i] = 1;
336 op_context->effective_input_shape.dim(i) = loco::Dimension(1);
337 added_axises++;
338 }
339 else if (i >= begin_count + expanded_ellipsis)
340 {
341 op_params.start_indices[i] = 0;
342 op_params.stop_indices[i] = 0;
343 op_params.strides[i] = 1;
344 assert(i < 16);
345 op_params.begin_mask |= (1LL << i);
346 op_params.end_mask |= (1LL << i);
347 op_context->effective_input_shape.dim(i) = input_shape.dim(i - added_axises);
348 }
349 else
350 {
351 const int64_t orig_idx = i - added_ellipsis;
352 op_params.start_indices[i] = op_context->begin->at<S32>(orig_idx);
353 op_params.stop_indices[i] = op_context->end->at<S32>(orig_idx);
354 op_params.strides[i] = op_context->strides->at<S32>(orig_idx);
355 if (op_context->params.begin_mask & (1LL << orig_idx))
356 {
357 assert(i < 16);
358 op_params.begin_mask |= (1LL << i);
359 }
360 if (op_context->params.end_mask & (1LL << orig_idx))
361 {
362 assert(i < 16);
363 op_params.end_mask |= (1LL << i);
364 }
365 if (op_context->params.shrink_axis_mask & (1LL << orig_idx))
366 {
367 assert(i < 16);
368 op_params.shrink_axis_mask |= (1LL << i);
369 }
370 op_context->effective_input_shape.dim(i) = input_shape.dim(i - added_axises);
371 }
372 }
373
374 // make sure no overflow
375 assert(static_cast<int8_t>(effective_dims) == static_cast<int32_t>(effective_dims));
376
377 op_params.start_indices_count = effective_dims;
378 op_params.stop_indices_count = effective_dims;
379 op_params.strides_count = effective_dims;
380
381 return op_params;
382}
The value of one dimension in a tensor shape.
Definition Dimension.h:30
uint32_t value(void) const
Return the value.
Definition Dimension.h:51
const Dimension & dim(uint32_t axis) const
Definition TensorShape.h:38
uint32_t rank(void) const
Definition TensorShape.h:35
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
const loco::DataType S32
loco::TensorShape circle_shape(const luci::CircleNode *node)
int8_t begin_count
Definition Slice.cpp:32

References luci::CircleConst::at(), luci::sinf::StridedSliceContext::begin, begin_count, luci::sinf::StridedSliceParams::begin_mask, circle_shape(), loco::TensorShape::dim(), luci::sinf::StridedSliceContext::effective_input_shape, luci::sinf::StridedSliceParams::ellipsis_mask, luci::sinf::StridedSliceContext::end, luci::sinf::StridedSliceParams::end_mask, luci::sinf::StridedSliceContext::input, luci::sinf::StridedSliceContext::input_dims, luci::sinf::StridedSliceParams::new_axis_mask, luci::sinf::StridedSliceContext::params, loco::TensorShape::rank(), S32, luci::sinf::StridedSliceParams::shrink_axis_mask, luci::sinf::StridedSliceParams::start_indices, luci::sinf::StridedSliceParams::start_indices_count, luci::sinf::StridedSliceParams::stop_indices, luci::sinf::StridedSliceParams::stop_indices_count, luci::sinf::StridedSliceParams::strides, luci::sinf::StridedSliceContext::strides, luci::sinf::StridedSliceParams::strides_count, and loco::Dimension::value().

Referenced by luci::sinf::Algorithm::visit().

◆ circle_shape()

◆ Clamp()

int luci::sinf::Clamp ( const int32_t  v,
const int32_t  lo,
const int32_t  hi 
)
inline

Definition at line 115 of file CircleStridedSlice.cpp.

116{
117 LUCI_ASSERT(!(hi < lo), "Clamp hi < lo");
118 if (hi < v)
119 return hi;
120 if (v < lo)
121 return lo;
122 return v;
123}
#define LUCI_ASSERT(condition, msg)
Definition Check.h:26

References LUCI_ASSERT.

Referenced by StartForAxis(), and StopForAxis().

◆ pad_shape()

loco::TensorShape luci::sinf::pad_shape ( const loco::TensorShape input_shape,
const luci::CircleNode paddings 
)

Definition at line 164 of file CircleShapeInferenceHelper.cpp.

165{
166 const loco::DataType S32 = loco::DataType::S32;
167 const loco::DataType S64 = loco::DataType::S64;
168
169 // TODO support other data type
170 LUCI_ASSERT(paddings->dtype() == S32 || paddings->dtype() == S64, "Support int 32/64 for now");
171 if (paddings->rank() != 2)
172 INTERNAL_EXN("paddings should be rank 2");
173
174 int32_t n = paddings->dim(0).value();
175 int32_t v = paddings->dim(1).value();
176
177 if (v != 2)
178 INTERNAL_EXN("paddings should be [n, 2]");
179
180 if (n != int32_t(input_shape.rank()))
181 INTERNAL_EXN("paddings [n, 2] should have same value of input rank");
182
184
185 output_shape.rank(input_shape.rank());
186
187 auto const_padding = dynamic_cast<const luci::CircleConst *>(paddings);
188 if (const_padding == nullptr)
189 return output_shape;
190
191 for (int32_t ni = 0; ni < n; ++ni)
192 {
193 if (not input_shape.dim(ni).known())
194 {
195 output_shape.dim(ni).unset();
196 continue;
197 }
198 int32_t idx = ni * 2;
199 int value = input_shape.dim(ni).value();
200 if (const_padding->dtype() == S32)
201 {
202 value += const_padding->at<S32>(idx + 0); // left
203 value += const_padding->at<S32>(idx + 1); // right
204 }
205 else
206 {
207 auto pl = const_padding->at<S64>(idx + 0);
208 auto pr = const_padding->at<S64>(idx + 1);
209 auto max = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
210 auto low = static_cast<int64_t>(std::numeric_limits<int32_t>::lowest());
211 LUCI_ASSERT(pl <= max, "paddings is over 32 bit limit");
212 LUCI_ASSERT(pl >= low, "paddings is over 32 bit limit");
213 LUCI_ASSERT(pr <= max, "paddings is over 32 bit limit");
214 LUCI_ASSERT(pr >= low, "paddings is over 32 bit limit");
215 value += static_cast<int32_t>(pl); // left
216 value += static_cast<int32_t>(pr); // right
217 }
218 output_shape.dim(ni) = value;
219 }
220
221 return output_shape;
222}
#define INTERNAL_EXN(msg)
@ brief throw internal exception with message
Definition InternalExn.h:25
bool known(void) const
Return whether the value is known (or not)
Definition Dimension.h:47
Class to build tensor data.
Definition CircleConst.h:35
DataType
"scalar" value type
Definition DataType.h:27

References loco::TensorShape::dim(), INTERNAL_EXN, loco::Dimension::known(), LUCI_ASSERT, output_shape, loco::TensorShape::rank(), S32, and loco::Dimension::value().

Referenced by luci::sinf::Algorithm::visit().

◆ StartForAxis()

int64_t luci::sinf::StartForAxis ( const StridedSliceParams params,
const loco::TensorShape input_shape,
int64_t  axis 
)
inline

Definition at line 128 of file CircleStridedSlice.cpp.

130{
131 const auto begin_mask = params.begin_mask;
132 const auto *start_indices = params.start_indices;
133 const auto *strides = params.strides;
134 const int64_t axis_size = static_cast<int64_t>(input_shape.dim(axis).value());
135 if (axis_size == 0)
136 {
137 return 0;
138 }
139 // Begin with the specified index.
140 int64_t start = start_indices[axis];
141
142 // begin_mask override
143 if (begin_mask & (1LL << axis))
144 {
145 if (strides[axis] > 0)
146 {
147 // Forward iteration - use the first element. These values will get
148 // clamped below (Note: We could have set them to 0 and axis_size-1, but
149 // use lowest() and max() to maintain symmetry with StopForAxis())
150 start = std::numeric_limits<int32_t>::lowest();
151 }
152 else
153 {
154 // Backward iteration - use the last element.
155 start = std::numeric_limits<int32_t>::max();
156 }
157 }
158
159 // Handle negative indices
160 if (start < 0)
161 {
162 start += axis_size;
163 }
164
165 // Clamping
166 if (strides[axis] > 0)
167 {
168 // Forward iteration
169 start = Clamp(start, 0, axis_size);
170 }
171 else
172 {
173 // Backward iteration
174 start = Clamp(start, -1, axis_size - 1);
175 }
176
177 return start;
178}
int Clamp(const int32_t v, const int32_t lo, const int32_t hi)

References luci::sinf::StridedSliceParams::begin_mask, Clamp(), loco::TensorShape::dim(), luci::sinf::StridedSliceParams::start_indices, luci::sinf::StridedSliceParams::strides, and loco::Dimension::value().

Referenced by luci::sinf::Algorithm::visit().

◆ StopForAxis()

int64_t luci::sinf::StopForAxis ( const StridedSliceParams params,
const loco::TensorShape input_shape,
int64_t  axis,
int64_t  start_for_axis 
)
inline

Definition at line 185 of file CircleStridedSlice.cpp.

187{
188 const auto end_mask = params.end_mask;
189 const auto shrink_axis_mask = params.shrink_axis_mask;
190 const auto *stop_indices = params.stop_indices;
191 const auto *strides = params.strides;
192 const int64_t axis_size = static_cast<int64_t>(input_shape.dim(axis).value());
193 if (axis_size == 0)
194 {
195 return 0;
196 }
197
198 // Begin with the specified index
199 const bool shrink_axis = shrink_axis_mask & (1LL << axis);
200 int64_t stop = stop_indices[axis];
201
202 // When shrinking an axis, the end position does not matter (and can be
203 // incorrect when negative indexing is used, see Issue #19260). Always use
204 // start_for_axis + 1 to generate a length 1 slice, since start_for_axis has
205 // already been adjusted for negative indices.
206 if (shrink_axis)
207 {
208 return start_for_axis + 1;
209 }
210
211 // end_mask override
212 if (end_mask & (1LL << axis))
213 {
214 if (strides[axis] > 0)
215 {
216 // Forward iteration - use the last element. These values will get
217 // clamped below
218 stop = std::numeric_limits<int32_t>::max();
219 }
220 else
221 {
222 // Backward iteration - use the first element.
223 stop = std::numeric_limits<int32_t>::lowest();
224 }
225 }
226
227 // Handle negative indices
228 if (stop < 0)
229 {
230 stop += axis_size;
231 }
232
233 // Clamping
234 // Because the end index points one past the last element, we need slightly
235 // different clamping ranges depending on the direction.
236 if (strides[axis] > 0)
237 {
238 // Forward iteration
239 stop = Clamp(stop, 0, axis_size);
240 }
241 else
242 {
243 // Backward iteration
244 stop = Clamp(stop, -1, axis_size - 1);
245 }
246
247 return stop;
248}

References Clamp(), loco::TensorShape::dim(), luci::sinf::StridedSliceParams::end_mask, luci::sinf::StridedSliceParams::shrink_axis_mask, luci::sinf::StridedSliceParams::stop_indices, luci::sinf::StridedSliceParams::strides, and loco::Dimension::value().

Referenced by luci::sinf::Algorithm::visit().

Variable Documentation

◆ kMaxDim

const int luci::sinf::kMaxDim = 5

Definition at line 58 of file CircleStridedSlice.cpp.

◆ S32

const loco::DataType luci::sinf::S32 = loco::DataType::S32