tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
transform.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_TOPI_TRANSFORM_H_
25 #define TVM_TOPI_TRANSFORM_H_
26 
27 #include <tvm/te/operation.h>
28 #include <tvm/tir/data_layout.h>
29 #include <tvm/tir/index_map.h>
30 #include <tvm/topi/broadcast.h>
36 #include <tvm/topi/tags.h>
37 
38 #include <algorithm>
39 #include <iterator>
40 #include <limits>
41 #include <string>
42 #include <unordered_set>
43 #include <vector>
44 
45 namespace tvm {
46 namespace topi {
47 
48 using namespace tvm::te;
49 using namespace topi::detail;
50 
68 inline Tensor sliding_window(const Tensor& x, int axis, Array<Integer> window_shape,
69  Array<Integer> strides, std::string name = "T_sliding_window",
70  std::string tag = "") {
71  CHECK_GE(axis, 0);
72  auto _axis = size_t(axis);
73  CHECK_LT(_axis, x->shape.size()) << "axis must be a valid dimension index of x.";
74  CHECK_EQ(x->shape.size() - _axis, window_shape.size())
75  << "There must be a window shape for every dimension of x "
76  << "over which we are sliding the window.";
77  CHECK_EQ(strides.size(), window_shape.size()) << "Windows and strides should be the same length.";
78 
79  // Compute the new shape.
80  Array<PrimExpr> new_shape;
81  // Dimensions up until `axis` remain the same.
82  for (size_t i = 0; i < _axis; ++i) {
83  new_shape.push_back(x->shape[i]);
84  }
85 
86  // New dimensions which result from sliding the window in each dimension. One new dimension per
87  // window dimension.
88  for (size_t i = 0; i < window_shape.size(); ++i) {
89  // Length of the shape along this dimension.
90  auto dim_len = x->shape[_axis + i];
91  // Length of the window along this dimension.
92  auto window_len = window_shape[i];
93  // Strides along this dimension.
94  auto stride = strides[i];
95 
96  new_shape.push_back(floordiv(dim_len - (window_len - 1) + stride - 1, stride));
97  }
98 
99  // Dimensions comprising the window.
100  for (size_t i = 0; i < window_shape.size(); ++i) {
101  new_shape.push_back(window_shape[i]);
102  }
103 
104  ICHECK(new_shape.size() == _axis + 2 * window_shape.size());
105 
106  return compute(
107  new_shape,
108  [&](const Array<Var>& indices) {
109  // The index at which to index the old tensor x.
110  Array<PrimExpr> idx;
111 
112  // Dimensions up until `axis` remain the same.
113  for (size_t i = 0; i < _axis; ++i) {
114  idx.push_back(indices[i]);
115  }
116 
117  for (size_t i = 0; i < window_shape.size(); ++i) {
118  // Which window in this dimension we are indexing.
119  auto window_idx = indices[_axis + i];
120  // Which index within the window we are indexing.
121  auto idx_within_window = indices[_axis + window_shape.size() + i];
122  // Stride value for this dimension.
123  auto stride = strides[i];
124 
125  idx.push_back(window_idx * stride + idx_within_window);
126  }
127 
128  ICHECK(idx.size() == x->shape.size());
129 
130  return x(idx);
131  },
132  name, tag);
133 }
134 
147 inline Tensor expand_dims(const Tensor& x, int axis, int num_newaxis = 1,
148  std::string name = "T_expand_dims", std::string tag = kBroadcast) {
149  int ndim = static_cast<int>(x->shape.size());
150  ICHECK(-ndim - 1 <= axis && axis <= ndim)
151  << "expand_dims only accepts `axis` in [-data.ndim - 1, data.ndim]"
152  << ", but got axis = " << axis << ", and data.ndim = " << ndim;
153  ICHECK(num_newaxis >= 0) << "expand_dims only accepts `num_newaxis >= 0`"
154  << ", but got num_newaxis = " << num_newaxis;
155  if (axis < 0) {
156  // Calculate offset from last dimension
157  axis = ndim + axis + 1;
158  }
159  Array<PrimExpr> new_shape;
160  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
161  new_shape.push_back(x->shape[i]);
162  }
163  for (size_t i = 0; i < static_cast<size_t>(num_newaxis); ++i) {
164  new_shape.push_back(1);
165  }
166  for (size_t i = axis; i < x->shape.size(); ++i) {
167  new_shape.push_back(x->shape[i]);
168  }
169 
170  return compute(
171  new_shape,
172  [&](const Array<Var>& indices) {
173  Array<PrimExpr> idx;
174  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
175  idx.push_back(indices[i]);
176  }
177  for (size_t i = axis + num_newaxis; i < indices.size(); ++i) {
178  idx.push_back(indices[i]);
179  }
180  return x(idx);
181  },
182  name, tag);
183 }
184 
196 inline Tensor transpose(const Tensor& x, Array<Integer> axes, std::string name = "T_transpose",
197  std::string tag = kInjective) {
198  if (!axes.defined() || axes.size() == 0) {
199  axes = Array<Integer>();
200  for (int i = static_cast<int>(x->shape.size()) - 1; i >= 0; --i) {
201  axes.push_back(i);
202  }
203  }
204 
205  Array<PrimExpr> new_shape;
206  for (size_t i = 0; i < axes.size(); ++i) {
207  int axis = static_cast<int>(axes[i]->value);
208  int new_axis = axis;
209  if (axis < 0) {
210  new_axis = static_cast<int>(x->shape.size()) + axis;
211  axes.Set(i, new_axis);
212  }
213  ICHECK((new_axis >= 0) && (new_axis < static_cast<int>(x->shape.size())))
214  << "axis=" << axis << " is invalid for the " << static_cast<int>(x->shape.size())
215  << "-dimensional input tensor";
216 
217  for (size_t j = 0; j < axes.size(); ++j) {
218  if (i != j) {
219  ICHECK(new_axis != static_cast<int>(axes[j]->value)) << "repeated axis in transpose";
220  }
221  }
222  new_shape.push_back(x->shape[new_axis]);
223  }
224 
225  return compute(
226  new_shape,
227  [&](const Array<Var>& indices) {
228  std::vector<PrimExpr> idx;
229  for (size_t i = 0; i < axes.size(); ++i) {
230  idx.push_back(1);
231  }
232  for (size_t i = 0; i < axes.size(); ++i) {
233  int axis = static_cast<int>(axes[i]->value);
234  idx[axis] = indices[i];
235  }
236  return x(idx);
237  },
238  name, tag);
239 }
240 
255 inline Tensor reverse_sequence(const Tensor& x, const Tensor& seq_lengths, int seq_axis = 1,
256  int batch_axis = 0, std::string name = "T_reverse_sequence",
257  std::string tag = kInjective) {
258  size_t src_tensor_dim = x->shape.size();
259  int seq_axis_inp = seq_axis;
260 
261  if (seq_lengths.defined()) {
262  size_t seq_lengths_dim = seq_lengths->shape.size();
263  int batch_axis_inp = batch_axis;
264  if (batch_axis < 0) {
265  batch_axis = static_cast<int>(x->shape.size()) + batch_axis;
266  }
267 
268  ICHECK(seq_lengths_dim == 1) << "seq_lengths should be 1D vector";
269 
270  ICHECK(GetConstInt(seq_lengths->shape[0]) == GetConstInt(x->shape[batch_axis]))
271  << "For reverse_sequnece seq_lengths size should match with dimension of batch axis"
272  << ", but got dimension of batch_axis = " << GetConstInt(x->shape[batch_axis])
273  << ", and seq_length size = " << GetConstInt(seq_lengths->shape[0]);
274 
275  ICHECK((0 <= batch_axis) && (batch_axis < static_cast<int>(x->shape.size())))
276  << "batch_axis=" << batch_axis_inp << " is invalid for the "
277  << static_cast<int>(x->shape.size()) << "-dimensional input tensor";
278  }
279 
280  if (seq_axis < 0) {
281  seq_axis = static_cast<int>(x->shape.size()) + seq_axis;
282  }
283  ICHECK((0 <= seq_axis) && (seq_axis < static_cast<int>(x->shape.size())))
284  << "seq_axis=" << seq_axis_inp << " is invalid for the " << static_cast<int>(x->shape.size())
285  << "-dimensional input tensor";
286 
287  auto func = [&](const Array<Var>& indices) {
288  Array<PrimExpr> real_indices;
289  for (size_t i = 0; i < src_tensor_dim; ++i) {
290  if (i == static_cast<size_t>(seq_axis)) {
291  if (seq_lengths.defined()) {
292  auto len = seq_lengths(indices[batch_axis]);
293  auto idx = if_then_else(
294  len <= 1 || len <= indices[i], indices[i],
295  if_then_else(len > x->shape[i], x->shape[i] - 1 - indices[i], len - 1 - indices[i]));
296  real_indices.push_back(idx);
297  } else {
298  real_indices.push_back(x->shape[i] - 1 - indices[i]);
299  }
300  } else {
301  real_indices.push_back(indices[i]);
302  }
303  }
304  return x(real_indices);
305  };
306 
307  return compute(x->shape, func, name, tag);
308 }
309 
320 inline Tensor reshape(const Tensor& x, Array<PrimExpr> newshape, std::string name = "T_reshape",
321  std::string tag = kInjective) {
322  auto x_shape = x->shape;
323  Array<PrimExpr> target_shape;
324 
325  for (const auto& ele : newshape) {
326  if (ele.as<IntImmNode>()) {
327  target_shape.push_back(cast(DataType::Int(32), ele));
328  } else {
329  target_shape.push_back(ele);
330  }
331  }
332 
333  // If either the input shape or the target shape contains a zero, return an empty tensor.
334  if (is_empty_shape(target_shape) || is_empty_shape(x->shape)) {
335  return compute(
336  target_shape, [&](const Array<Var>& indices) { return tvm::cast(x->dtype, 0); }, name, tag);
337  } else {
338  return compute(
339  target_shape,
340  [&](const Array<Var>& indices) {
341  return x(UnravelIndex(
342  RavelIndex(Array<PrimExpr>{indices.begin(), indices.end()}, target_shape), x_shape));
343  },
344  name, tag);
345  }
346 }
347 
359 inline Tensor unravel_index(const Tensor& x, const Tensor& shape, std::string name = "T_unravel",
360  std::string tag = kInjective) {
361  auto x_shape = x->shape;
362  auto shape_shape = shape->shape;
363 
364  Array<PrimExpr> oshape;
365  oshape.push_back(shape_shape[0]);
366  if (x_shape.size() != 0) {
367  oshape.push_back(x_shape[0]);
368  }
369 
370  auto func = [&](const Array<Var>& indices) {
371  auto i = indices[0];
372  std::vector<PrimExpr> indices_divs;
373  PrimExpr ret = 0;
374  PrimExpr cur_val = 0;
375  PrimExpr index_val = 0;
376 
377  if (x_shape.size() != 0) {
378  index_val = x[indices[1]];
379  } else {
380  index_val = x();
381  }
382  indices_divs.push_back(index_val);
383  for (int v = GetConstInt(shape_shape[0]) - 1; v >= 0; --v) {
384  ret = tvm::if_then_else(i == v, indexmod(indices_divs.back(), shape[v]), ret);
385  cur_val = indexdiv(indices_divs.back(), shape[v]);
386  indices_divs.push_back(cur_val);
387  }
388  return ret;
389  };
390 
391  return compute(oshape, func, name, tag);
392 }
393 
407 inline Tensor squeeze(const Tensor& x, Array<Integer> axis, bool atleast1d = false,
408  std::string name = "T_squeeze", std::string tag = kInjective) {
409  auto ndim = x->shape.size();
410  std::vector<int> axis_val;
411  if (!axis.defined()) {
412  for (size_t i = 0; i < ndim; ++i) {
413  if (IsConstInt(x->shape[i]) && GetConstInt(x->shape[i]) == 1) {
414  axis_val.push_back(static_cast<int>(i));
415  }
416  }
417  } else {
418  for (size_t i = 0; i < axis.size(); ++i) {
419  int64_t val = axis[i]->value;
420  if (val < 0) {
421  val += static_cast<int>(x->shape.size());
422  }
423  if (IsConstInt(x->shape[val])) {
424  ICHECK_EQ(GetConstInt(x->shape[val]), 1) << "Dimension " << val << " must have size 1";
425  }
426  axis_val.push_back(val);
427  }
428  }
429 
430  std::unordered_set<int> axis_set(axis_val.begin(), axis_val.end());
431 
432  Array<PrimExpr> out_shape;
433  for (size_t i = 0; i < ndim; ++i) {
434  if (axis_set.count(static_cast<int>(i)) == 0) {
435  out_shape.push_back(x->shape[i]);
436  }
437  }
438  if (out_shape.size() == 0 && atleast1d) {
439  out_shape.push_back(1);
440  }
441 
442  return compute(
443  out_shape,
444  [&](const Array<Var>& indices) {
445  Array<PrimExpr> real_indices;
446  int flag = 0;
447  for (size_t i = 0; i < ndim; ++i) {
448  if (axis_set.count(static_cast<int>(i)) == 0) {
449  real_indices.push_back(indices[i - flag]);
450  } else {
451  real_indices.push_back(0);
452  flag += 1;
453  }
454  }
455  return x(real_indices);
456  },
457  name, tag);
458 }
459 
470 inline Tensor concatenate(const Array<Tensor>& inputs, int axis = 0, std::string name = "T_concat",
471  std::string tag = kInjective) {
472  int ndim = static_cast<int>(inputs[0]->shape.size());
473  ICHECK(-ndim <= axis && axis < ndim) << "concatenate only accepts `axis` in [-ndim, ndim)"
474  << ", but got axis = " << axis << ", and ndim = " << ndim;
475  if (axis < 0) {
476  axis += ndim;
477  }
478  ICHECK_LT(axis, inputs[0]->shape.size()) << "axis out of bounds";
479 
480  Array<PrimExpr> axis_sizes;
481  for (auto t : inputs) {
482  axis_sizes.push_back(t->shape[axis]);
483  }
484  arith::Analyzer analyzer;
485  PrimExpr join_size = axis_sizes[0];
486  for (size_t i = 1; i < axis_sizes.size(); ++i) {
487  join_size += axis_sizes[i];
488  }
489  join_size = analyzer.Simplify(join_size);
490  Array<PrimExpr> out_shape;
491  for (size_t i = 0; i < inputs[0]->shape.size(); ++i) {
492  out_shape.push_back(i == static_cast<size_t>(axis) ? join_size : inputs[0]->shape[i]);
493  }
494 
495  return compute(
496  out_shape,
497  [&](const Array<Var>& indices) {
498  auto ret = inputs[0](indices);
499  auto ind = indices[axis];
500  for (size_t i = 0; i < inputs.size() - 1; ++i) {
501  ind -= axis_sizes[i];
502 
503  Array<PrimExpr> idx;
504  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
505  idx.push_back(indices[i]);
506  }
507  idx.push_back(ind);
508  for (size_t i = axis + 1; i < indices.size(); ++i) {
509  idx.push_back(indices[i]);
510  }
511 
512  ret = tvm::if_then_else(ind >= 0, inputs[i + 1](idx), ret);
513  }
514  return ret;
515  },
516  name, tag);
517 }
518 
529 inline Tensor stack(const Array<Tensor>& inputs, int axis = 0, std::string name = "T_stack",
530  std::string tag = kInjective) {
531  int ndim = static_cast<int>(inputs[0]->shape.size());
532  ICHECK(-ndim - 1 <= axis && axis <= ndim)
533  << "stack only accepts `axis` in [-ndim, ndim)"
534  << ", but got axis = " << axis << ", and ndim = " << ndim;
535  if (axis < 0) {
536  axis += ndim + 1;
537  }
538  ICHECK_LT(axis, inputs[0]->shape.size() + 1) << "axis out of bounds";
539 
540  const int stack_size = static_cast<int>(inputs.size());
541  Array<PrimExpr> out_shape;
542  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) out_shape.push_back(inputs[0]->shape[i]);
543  out_shape.push_back(stack_size);
544  for (size_t i = static_cast<size_t>(axis); i < static_cast<size_t>(ndim); ++i)
545  out_shape.push_back(inputs[0]->shape[i]);
546 
547  return compute(
548  out_shape,
549  [&](const Array<Var>& indices) {
550  Array<PrimExpr> idx;
551  for (size_t i = 0; i < indices.size(); ++i)
552  if (i != static_cast<size_t>(axis)) idx.push_back(indices[i]);
553  auto ind = indices[axis];
554  auto ret = inputs[0](idx);
555  for (int i = 0; i < static_cast<int>(inputs.size() - 1); ++i) {
556  ret = tvm::if_then_else(ind == i + 1, inputs[i + 1](idx), ret);
557  }
558  return ret;
559  },
560  name, tag);
561 }
562 
575 inline Array<Tensor> split(const Tensor& x, Array<PrimExpr> split_indices, int axis,
576  std::string name = "T_split", std::string tag = kInjective) {
577  if (axis < 0) {
578  axis += static_cast<int>(x->shape.size());
579  }
580  ICHECK_LT(axis, x->shape.size()) << "axis out of bounds";
581 
582  auto src_axis_size = x->shape[axis];
583  std::vector<PrimExpr> begin_ids;
584  begin_ids.push_back(0);
585 
586  for (auto idx : split_indices) {
587  auto idx_node = idx.as<IntImmNode>();
588  auto back_node = begin_ids.back().as<IntImmNode>();
589  if (idx_node && back_node) {
590  ICHECK_GT(idx_node->value, back_node->value) << "split_indices must be sorted";
591  }
592  begin_ids.push_back(idx);
593  }
594 
595  Array<Array<PrimExpr>> out_shapes;
596  for (size_t i = 0; i < begin_ids.size(); ++i) {
597  PrimExpr out_axis_size;
598  if (i == begin_ids.size() - 1) {
599  out_axis_size = src_axis_size - begin_ids[i];
600  } else {
601  out_axis_size = begin_ids[i + 1] - begin_ids[i];
602  }
603 
605  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
606  shape.push_back(x->shape[i]);
607  }
608  shape.push_back(out_axis_size);
609  for (size_t i = axis + 1; i < x->shape.size(); ++i) {
610  shape.push_back(x->shape[i]);
611  }
612 
613  out_shapes.push_back(shape);
614  }
615 
616  Array<Tensor> result;
617  for (size_t i = 0; i < begin_ids.size(); ++i) {
618  result.push_back(compute(
619  out_shapes[i],
620  [&](const Array<Var>& indices) {
621  auto begin = begin_ids[i];
622  Array<PrimExpr> real_indices;
623  for (size_t j = 0; j < static_cast<size_t>(axis); ++j) {
624  real_indices.push_back(indices[j]);
625  }
626  real_indices.push_back(indices[axis] + begin);
627  for (size_t j = axis + 1; j < indices.size(); ++j) {
628  real_indices.push_back(indices[j]);
629  }
630 
631  return x(real_indices);
632  },
633  name, tag));
634  }
635 
636  return result;
637 }
638 
652 inline Tensor dynamic_strided_slice(const Tensor& x, const Array<PrimExpr>& begin,
653  const Array<PrimExpr>& end, const Array<PrimExpr>& strides,
654  std::string name = "T_dynamic_strided_slice",
655  std::string tag = kInjective) {
656  const size_t src_tensor_dim = x->shape.size();
657  ICHECK_LE(begin.size(), src_tensor_dim);
658  ICHECK_LE(end.size(), src_tensor_dim);
659  ICHECK_LE(strides.size(), src_tensor_dim);
660  ICHECK_EQ(begin.size(), end.size());
661  ICHECK_EQ(begin.size(), strides.size());
662 
663  const size_t num_slice_axes = begin.size();
664  Array<PrimExpr> out_shape;
665 
666  for (size_t i = 0; i < num_slice_axes; ++i) {
667  auto d = indexdiv(end[i] - begin[i], strides[i]);
668  if (d->IsInstance<tvm::IntImmNode>()) {
669  // Preserve static dimension if possible
670  out_shape.push_back(d);
671  } else {
672  out_shape.push_back(tvm::tir::Var("dim"));
673  }
674  }
675 
676  for (size_t i = num_slice_axes; i < src_tensor_dim; ++i) {
677  out_shape.push_back(x->shape[i]);
678  }
679 
680  return te::compute(
681  out_shape,
682  [&](const Array<tvm::tir::Var>& indices) {
683  Array<PrimExpr> real_indices;
684  for (size_t i = 0; i < num_slice_axes; ++i) {
685  real_indices.push_back(indices[i] * strides[i] + tvm::min(begin[i], x->shape[i] - 1));
686  }
687  // keep input dim
688  for (size_t i = num_slice_axes; i < src_tensor_dim; ++i) {
689  real_indices.push_back(indices[i]);
690  }
691  return x(real_indices);
692  },
693  name, tag);
694 }
695 
710  const te::Tensor& end, const te::Tensor& strides,
711  std::string name = "T_strided_slice_dynamic",
712  std::string tag = topi::kInjective) {
713  DataType index_dtype = begin->shape[0]->dtype;
714  const int64_t num_dynamic_axes = begin->shape[0].as<IntImmNode>()->value;
715  ICHECK_EQ(end->shape[0].as<IntImmNode>()->value, num_dynamic_axes);
716  ICHECK_EQ(strides->shape[0].as<IntImmNode>()->value, num_dynamic_axes);
717 
718  Array<PrimExpr> begin_expr, end_expr, strides_expr;
719  for (int64_t i = 0; i < num_dynamic_axes; ++i) {
720  auto ind = make_const(index_dtype, i);
721  begin_expr.push_back(begin(ind));
722  end_expr.push_back(end(ind));
723  strides_expr.push_back(strides(ind));
724  }
725  return dynamic_strided_slice(x, begin_expr, end_expr, strides_expr, name, tag);
726 }
727 
743  const Array<PrimExpr>& ishape, const Array<Integer>& begin, const Array<Integer>& end,
744  const Array<Integer>& strides, const Array<Integer>& axes, const std::string& slice_mode) {
745  ICHECK(axes.size() == begin.size() && axes.size() == end.size() && axes.size() == strides.size());
746  std::vector<int64_t> begin_vec, end_vec, strides_vec;
747  std::tie(begin_vec, end_vec, strides_vec) = ConvertToVec(begin, end, strides, slice_mode);
748  auto begin_canonicalized = StridedSliceCanonicalizeBegin(ishape, begin_vec, strides_vec, axes,
749  begin[0]->dtype, slice_mode);
750  return StridedSliceOutputShape(ishape, begin_vec, end_vec, strides_vec, axes, slice_mode,
751  begin_canonicalized, true);
752 }
753 
770 inline Tensor strided_slice_with_axes(const Tensor& x, const Array<Integer>& begin,
771  const Array<Integer>& end, const Array<Integer>& strides,
772  const Array<Integer>& axes, std::string slice_mode = "end",
773  std::string name = "T_strided_slice_with_axes",
774  std::string tag = kInjective) {
775  const size_t src_tensor_dim = x->shape.size();
776  ICHECK(axes.size() <= src_tensor_dim);
777  ICHECK(axes.size() == begin.size() && axes.size() == end.size() && axes.size() == strides.size());
778 
779  std::vector<int64_t> begin_vec, end_vec, strides_vec;
780  std::tie(begin_vec, end_vec, strides_vec) = ConvertToVec(begin, end, strides, slice_mode);
781 
782  auto begin_expr = StridedSliceCanonicalizeBegin(x->shape, begin_vec, strides_vec, axes,
783  begin[0]->dtype, slice_mode);
784  auto out_shape = StridedSliceOutputShape(x->shape, begin_vec, end_vec, strides_vec, axes,
785  slice_mode, begin_expr);
786 
787  return te::compute(
788  out_shape,
789  [&](const Array<tir::Var>& indices) {
790  Array<PrimExpr> real_indices;
791  for (size_t i = 0; i < out_shape.size(); ++i) real_indices.push_back(indices[i]);
792  for (size_t i = 0; i < axes.size(); ++i) {
793  auto stride = make_const(strides[i].dtype(), strides_vec[i]);
794  PrimExpr ind = indices[axes[i].IntValue()] * stride + begin_expr[i];
795  real_indices.Set(axes[i].IntValue(), ind);
796  }
797  return x(real_indices);
798  },
799  name, tag);
800 }
801 
816 inline Tensor strided_slice(const Tensor& x, const Array<Integer>& begin, const Array<Integer>& end,
817  const Array<Integer>& strides, std::string slice_mode = "end",
818  std::string name = "T_strided_slice", std::string tag = kInjective) {
819  size_t src_tensor_dim = static_cast<size_t>(x->shape.size());
820  Array<Integer> axes;
821  for (size_t i = 0; i < src_tensor_dim; ++i) axes.push_back(i);
822  Array<Integer> begin_full(begin);
823  Array<Integer> end_full(end);
824  Array<Integer> strides_full(strides);
825 
826  DataType index_dtype = begin.size() > 0 ? begin[0]->dtype : DataType::Int(64);
827  const IntImm one = IntImm(index_dtype, 1);
828  const IntImm zero = IntImm(index_dtype, 0);
829  const IntImm max_range = Downcast<IntImm>(max_value(index_dtype));
830 
831  for (size_t i = strides.size(); i < src_tensor_dim; ++i) {
832  strides_full.push_back(one);
833  }
834  for (size_t i = begin.size(); i < src_tensor_dim; ++i) {
835  begin_full.push_back(GetConstInt(strides_full[i]) > 0 ? zero : max_range);
836  }
837  for (size_t i = end.size(); i < src_tensor_dim; ++i) {
838  end_full.push_back(GetConstInt(strides_full[i]) < 0 ? zero : max_range);
839  }
840 
841  return strided_slice_with_axes(x, begin_full, end_full, strides_full, axes, slice_mode, name,
842  tag);
843 }
844 
857 inline Array<Tensor> split_sections(const Tensor& x, int num_sections, int axis,
858  std::string name = "T_split_sections",
859  std::string tag = kInjective) {
860  if (axis < 0) {
861  axis += static_cast<int>(x->shape.size());
862  }
863  ICHECK_LT(axis, x->shape.size()) << "axis out of bounds";
864 
865  auto src_axis_size = x->shape[axis];
866 
867  ICHECK_GT(num_sections, 0) << "Slice count must be > 0";
868 
869  if (auto node = src_axis_size.as<IntImmNode>()) {
870  ICHECK_EQ(node->value % num_sections, 0)
871  << "num_sections must be an integer factor of the size of axis " << axis << " ("
872  << node->value << ")";
873  }
874 
875  Array<PrimExpr> split_indices;
876  auto seg_size = indexdiv(src_axis_size, num_sections);
877  for (int i = 0; i < num_sections; ++i) {
878  // region at index 0 is added by split()
879  if (i != 0) {
880  split_indices.push_back(seg_size * i);
881  }
882  }
883 
884  return split(x, split_indices, axis, name, tag);
885 }
886 
900 inline Tensor take(const Tensor& a, const Tensor& indices, int batch_dims,
901  std::string mode = "clip", std::string name = "T_take",
902  std::string tag = kInjective) {
903  Array<PrimExpr> a_shape = a->shape;
904  Array<PrimExpr> out_shape = indices->shape;
905  PrimExpr a_size = 1;
906  for (size_t i = 0; i < a_shape.size(); ++i) {
907  a_size = a_size * a_shape[i];
908  }
909 
910  if (mode == "clip") {
911  return compute(
912  out_shape,
913  [&](const Array<Var>& out_index) {
914  auto idx = tvm::min(tvm::max(0, indices(out_index)), a_size - 1);
915  return a(UnravelIndex(idx, a_shape));
916  },
917  name, tag);
918  } else if (mode == "fast") {
919  LOG(WARNING) << "Fast mode segfaults when there are out-of-bounds indices. "
920  "Make sure input indices are in bound";
921  return compute(
922  out_shape,
923  [&](const Array<Var>& out_index) { return a(UnravelIndex(indices(out_index), a_shape)); },
924  name, tag);
925  } else { // mode == "wrap"
926  return compute(
927  out_shape,
928  [&](const Array<Var>& out_index) {
929  auto idx = truncmod(truncmod(indices(out_index), a_size) + a_size, a_size);
930  return a(UnravelIndex(idx, a_shape));
931  },
932  name, tag);
933  }
934 }
935 
948 inline Tensor sequence_mask(const Tensor& data, const Tensor& valid_length, double mask_value,
949  int axis, std::string name = "T_sequence_mask",
950  std::string tag = kInjective) {
951  ICHECK(axis == 0 || axis == 1) << "axis must be either 0 or 1";
952  ICHECK_EQ(valid_length->shape.size(), 1) << "valid_length must have ndim=1, i.e., (batch_size,).";
953  auto length_dim = data->shape[axis];
954  auto batch_dim = data->shape[1 - axis];
955  Array<PrimExpr> out_shape = data->shape;
956  Tensor out = compute(
957  out_shape,
958  [&](const Array<Var>& out_index) {
959  Array<PrimExpr> len_index;
960  auto tid = out_index[axis];
961  auto bid = out_index[1 - axis];
962  len_index.push_back(bid);
963  PrimExpr ret =
964  tvm::if_then_else(tvm::cast(valid_length->dtype, tid) >= valid_length(len_index),
965  tvm::tir::make_const(data->dtype, mask_value), data(out_index));
966  return ret;
967  },
968  name, tag);
969  return out;
970 }
971 
986 inline Tensor take(const Tensor& a, const Tensor& indices, int batch_dims, int axis,
987  std::string mode = "clip", std::string name = "T_take",
988  std::string tag = kInjective) {
989  if (axis < 0) {
990  axis += static_cast<int>(a->shape.size());
991  }
992  ICHECK_GE(axis, 0) << "axis out of bounds";
993  ICHECK_LT(axis, a->shape.size()) << "axis out of bounds";
994  auto axis_dim = a->shape[axis];
995  int indices_len = static_cast<int>(indices->shape.size());
996 
997  int batch_dims_ = batch_dims;
998  if (batch_dims_ != 0) {
999  ICHECK_GE(batch_dims_, -static_cast<int>(indices->shape.size())) << "batch_dims out of bounds";
1000  ICHECK_LE(batch_dims_, indices->shape.size()) << "batch_dims out of bounds";
1001 
1002  if (batch_dims_ < 0) {
1003  batch_dims_ = indices->shape.size() + batch_dims_;
1004  }
1005 
1006  ICHECK_LT(batch_dims_, a->shape.size()) << "batch_dims out of bounds";
1007  ICHECK_LE(batch_dims_, axis) << "batch_dims must be less than or equal to axis";
1008  for (int i = 0; i < batch_dims_; ++i) {
1009  auto addr1 = a->shape[i];
1010  auto addr2 = indices->shape[i];
1011  auto v1 = static_cast<IntImm*>(&addr1)->get()->value;
1012  auto v2 = static_cast<IntImm*>(&addr2)->get()->value;
1013  ICHECK_EQ(v1, v2) << "a.shape[" << i << "] should be equal to indices.shape[" << i << "]";
1014  }
1015  }
1016 
1017  // The result shape is a.shape[:axis] + indices.shape[batch_dims:] +
1018  // a.shape[axis + 1:].
1019 
1020  Array<PrimExpr> out_shape;
1021  for (int i = 0; i < batch_dims_; ++i) {
1022  out_shape.push_back(a->shape[i]);
1023  }
1024  for (int i = batch_dims_; i < axis; ++i) {
1025  out_shape.push_back(a->shape[i]);
1026  }
1027  for (size_t i = static_cast<size_t>(batch_dims_); i < indices->shape.size(); ++i) {
1028  out_shape.push_back(indices->shape[i]);
1029  }
1030  for (size_t i = axis + 1; i < a->shape.size(); ++i) {
1031  out_shape.push_back(a->shape[i]);
1032  }
1033 
1034  if (mode == "clip") {
1035  if (batch_dims_ == 0) {
1036  return compute(
1037  out_shape,
1038  [&](const Array<Var>& out_index) {
1039  Array<PrimExpr> indices_position;
1040  for (size_t j = axis; j < static_cast<size_t>(axis + indices_len); ++j) {
1041  indices_position.push_back(out_index[j]);
1042  }
1043  Array<PrimExpr> real_indices;
1044  for (size_t j = 0; j < static_cast<size_t>(axis); ++j) {
1045  real_indices.push_back(out_index[j]);
1046  }
1047  auto idx = tvm::min(tvm::max(0, indices(indices_position)), axis_dim - 1);
1048  real_indices.push_back(idx);
1049  for (size_t j = axis + indices_len; j < out_index.size(); ++j) {
1050  real_indices.push_back(out_index[j]);
1051  }
1052  return a(real_indices);
1053  },
1054  name, tag);
1055  } else {
1056  return compute(
1057  out_shape,
1058  [&](const Array<Var>& out_index) {
1059  Array<PrimExpr> indices_position;
1060  for (size_t j = 0; j < static_cast<size_t>(batch_dims_); ++j) {
1061  indices_position.push_back(out_index[j]);
1062  }
1063  for (size_t j = axis; j < static_cast<size_t>(axis + indices_len - batch_dims_); ++j) {
1064  indices_position.push_back(out_index[j]);
1065  }
1066  Array<PrimExpr> real_indices;
1067  for (size_t j = 0; j < static_cast<size_t>(axis); ++j) {
1068  real_indices.push_back(out_index[j]);
1069  }
1070  auto idx = tvm::min(tvm::max(0, indices(indices_position)), axis_dim - 1);
1071  real_indices.push_back(idx);
1072  for (size_t j = axis + indices_len - batch_dims_; j < out_index.size(); ++j) {
1073  real_indices.push_back(out_index[j]);
1074  }
1075  return a(real_indices);
1076  },
1077  name, tag);
1078  }
1079  } else if (mode == "fast") {
1080  LOG(WARNING) << "Fast mode segfaults when there are out-of-bounds indices. "
1081  "Make sure input indices are in bound";
1082  return compute(
1083  out_shape,
1084  [&](const Array<Var>& out_index) {
1085  Array<PrimExpr> indices_position;
1086  for (size_t j = axis; j < static_cast<size_t>(axis + indices_len); ++j) {
1087  indices_position.push_back(out_index[j]);
1088  }
1089  Array<PrimExpr> real_indices;
1090  for (size_t j = 0; j < static_cast<size_t>(axis); ++j) {
1091  real_indices.push_back(out_index[j]);
1092  }
1093  real_indices.push_back(indices(indices_position));
1094  for (size_t j = axis + indices_len; j < out_index.size(); ++j) {
1095  real_indices.push_back(out_index[j]);
1096  }
1097  return a(real_indices);
1098  },
1099  name, tag);
1100  } else { // mode == "wrap"
1101  return compute(
1102  out_shape,
1103  [&](const Array<Var>& out_index) {
1104  Array<PrimExpr> indices_position;
1105  for (size_t j = axis; j < static_cast<size_t>(axis + indices_len); ++j) {
1106  indices_position.push_back(out_index[j]);
1107  }
1108  Array<PrimExpr> real_indices;
1109  for (size_t j = 0; j < static_cast<size_t>(axis); ++j) {
1110  real_indices.push_back(out_index[j]);
1111  }
1112  auto idx = truncmod(truncmod(indices(indices_position), axis_dim) + axis_dim, axis_dim);
1113  real_indices.push_back(idx);
1114  for (size_t j = axis + indices_len; j < out_index.size(); ++j) {
1115  real_indices.push_back(out_index[j]);
1116  }
1117  return a(real_indices);
1118  },
1119  name, tag);
1120  }
1121 }
1122 
1134 inline Tensor where(const Tensor& condition, const Tensor& x, const Tensor& y,
1135  std::string name = "T_where", std::string tag = kBroadcast) {
1136  ICHECK_EQ(x->dtype, y->dtype) << "x and y must have the same dtype: " << x->dtype << " vs "
1137  << y->dtype;
1138  auto get_out_shape = [&]() {
1139  auto bh1 = detail::BroadcastShape(x->shape, y->shape);
1140  Array<PrimExpr> common_shape1(bh1.common_shape.begin(), bh1.common_shape.end());
1141  auto bh2 = detail::BroadcastShape(condition->shape, common_shape1);
1142  Array<PrimExpr> common_shape2(bh2.common_shape.begin(), bh2.common_shape.end());
1143  return common_shape2;
1144  };
1145 
1146  auto oshape = get_out_shape();
1147 
1148  auto c_bh = detail::BroadcastShape(condition->shape, oshape);
1149  auto x_bh = detail::BroadcastShape(x->shape, oshape);
1150  auto y_bh = detail::BroadcastShape(y->shape, oshape);
1151 
1152  auto select = [&](tvm::Array<tvm::tir::Var> ovars) {
1153  auto c = condition(InputIndexFromBroadcast(ovars, condition, c_bh.vars1, c_bh.all_vars));
1154  auto true_val = x(InputIndexFromBroadcast(ovars, x, x_bh.vars1, x_bh.all_vars));
1155  auto false_val = y(InputIndexFromBroadcast(ovars, y, y_bh.vars1, y_bh.all_vars));
1156  return tvm::tir::Select(c != 0, true_val, false_val);
1157  };
1158 
1159  return compute(oshape, select, name, tag);
1160 }
1161 
1174 inline Tensor repeat(const Tensor& x, int repeats, int axis, std::string name = "T_repeat",
1175  std::string tag = kBroadcast) {
1176  int ndim = static_cast<int>(x->shape.size());
1177  ICHECK(-ndim - 1 <= axis && axis <= ndim)
1178  << "repeat only accepts `axis` in [-data.ndim - 1, data.ndim]"
1179  << ", but got axis = " << axis << ", and data.ndim = " << ndim;
1180  ICHECK(repeats >= 1) << "repeat only accepts `repeats >= 1`"
1181  << ", but got repeats = " << repeats;
1182  if (axis < 0) {
1183  // Calculate offset from last dimension
1184  axis += ndim;
1185  }
1186  Array<PrimExpr> new_shape;
1187  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
1188  new_shape.push_back(x->shape[i]);
1189  }
1190  new_shape.push_back(repeats * x->shape[axis]);
1191  for (size_t i = axis + 1; i < x->shape.size(); ++i) {
1192  new_shape.push_back(x->shape[i]);
1193  }
1194 
1195  return compute(
1196  new_shape,
1197  [&](const Array<Var>& indices) {
1198  Array<PrimExpr> idx;
1199  for (size_t i = 0; i < static_cast<size_t>(axis); ++i) {
1200  idx.push_back(indices[i]);
1201  }
1202  idx.push_back(indexdiv(indices[axis], repeats));
1203  for (size_t i = axis + 1; i < indices.size(); ++i) {
1204  idx.push_back(indices[i]);
1205  }
1206  return x(idx);
1207  },
1208  name, tag);
1209 }
1210 
1221 inline Tensor tile(const Tensor& x, Array<Integer> reps, std::string name = "T_tile",
1222  std::string tag = kBroadcast) {
1223  size_t ndim = x->shape.size();
1224  size_t rdim = reps.size();
1225  size_t tdim = (ndim > rdim) ? ndim : rdim;
1226  Array<PrimExpr> data_shape;
1227  Array<PrimExpr> reps_shape;
1228  Array<PrimExpr> new_shape;
1229  if (ndim == rdim) {
1230  for (size_t i = 0; i < ndim; ++i) {
1231  data_shape.push_back(x->shape[i]);
1232  reps_shape.push_back(reps[i]);
1233  }
1234  } else if (ndim > rdim) {
1235  for (size_t i = 0; i < ndim; ++i) data_shape.push_back(x->shape[i]);
1236  for (size_t i = 0; i < (ndim - rdim); ++i) reps_shape.push_back(1);
1237  for (size_t i = 0; i < rdim; ++i) reps_shape.push_back(reps[i]);
1238  } else {
1239  for (size_t i = 0; i < (rdim - ndim); ++i) data_shape.push_back(1);
1240  for (size_t i = 0; i < ndim; ++i) data_shape.push_back(x->shape[i]);
1241  for (size_t i = 0; i < rdim; ++i) reps_shape.push_back(reps[i]);
1242  }
1243  for (size_t i = 0; i < tdim; ++i) new_shape.push_back(data_shape[i] * reps_shape[i]);
1244 
1245  if (is_empty_shape(new_shape)) {
1246  return compute(
1247  new_shape, [&](const Array<Var>& indices) { return tvm::cast(x->dtype, 0); }, name, tag);
1248  } else {
1249  return compute(
1250  new_shape,
1251  [&](const Array<Var>& indices) {
1252  Array<PrimExpr> idx;
1253  if (ndim >= rdim) {
1254  for (size_t i = 0; i < ndim; ++i) idx.push_back(indexmod(indices[i], x->shape[i]));
1255  } else {
1256  for (size_t i = 0; i < ndim; ++i)
1257  idx.push_back(indexmod(indices[rdim - ndim + i], x->shape[i]));
1258  }
1259  return x(idx);
1260  },
1261  name, tag);
1262  }
1263 }
1264 
1276 inline Tensor dyn_tile(const Tensor& x, Array<PrimExpr> new_shape, size_t rdim,
1277  std::string name = "T_tile", std::string tag = kBroadcast) {
1278  size_t ndim = x->shape.size();
1279  if (is_empty_shape(new_shape)) {
1280  return compute(
1281  new_shape, [&](const Array<Var>& indices) { return tvm::cast(x->dtype, 0); }, name, tag);
1282  } else {
1283  return compute(
1284  new_shape,
1285  [&](const Array<Var>& indices) {
1286  Array<PrimExpr> idx;
1287  if (ndim >= rdim) {
1288  for (size_t i = 0; i < ndim; ++i) {
1289  idx.push_back(indexmod(indices[i], x->shape[i]));
1290  }
1291  } else {
1292  for (size_t i = 0; i < ndim; ++i) {
1293  idx.push_back(indexmod(indices[rdim - ndim + i], x->shape[i]));
1294  }
1295  }
1296  return x(idx);
1297  },
1298  name, tag);
1299  }
1300 }
1301 
1313 inline Tensor gather(const Tensor& data, int axis, const Tensor& indices,
1314  std::string name = "T_gather", std::string tag = kInjective) {
1315  size_t ndim_d = data->shape.size();
1316  size_t ndim_i = indices->shape.size();
1317  ICHECK_GE(ndim_d, 1) << "Cannot gather from a scalar.";
1318  ICHECK_EQ(ndim_d, ndim_i);
1319  if (axis < 0) {
1320  axis += ndim_d;
1321  }
1322  ICHECK_GE(axis, 0);
1323  ICHECK_LT(axis, ndim_d);
1324  if (indices->shape[axis].as<IntImmNode>()) {
1325  size_t indices_dim_i = static_cast<size_t>(GetConstInt(indices->shape[axis]));
1326  ICHECK_GE(indices_dim_i, 1);
1327  }
1328  ICHECK(indices->dtype.is_int() || indices->dtype.is_uint());
1329 
1330  Array<PrimExpr> out_shape;
1331  for (size_t i = 0; i < ndim_i; ++i) {
1332  out_shape.push_back(indices->shape[i]);
1333  }
1334 
1335  return compute(
1336  out_shape,
1337  [&](const Array<Var>& out_index) {
1338  Array<PrimExpr> indices_position;
1339  for (size_t i = 0; i < ndim_i; ++i) {
1340  indices_position.push_back(out_index[i]);
1341  }
1342  Array<PrimExpr> real_indices;
1343  for (size_t i = 0; i < ndim_i; ++i) {
1344  if (i == static_cast<size_t>(axis)) {
1345  real_indices.push_back(indices(indices_position));
1346  } else {
1347  real_indices.push_back(indices_position[i]);
1348  }
1349  }
1350  return data(real_indices);
1351  },
1352  name, tag);
1353 }
1354 
1366 inline Tensor gather_nd(const Tensor& data, const Tensor& indices, int batch_dims = 0,
1367  std::string name = "T_gather_nd", std::string tag = kInjective) {
1368  size_t ndim_d = data->shape.size();
1369  size_t ndim_i = indices->shape.size();
1370  ICHECK_GE(ndim_i, 1) << "indices tensor must have at least 1 dimensions";
1371  size_t indices_dim0 = static_cast<size_t>(GetConstInt(indices->shape[0]));
1372  ICHECK_LE(indices_dim0, ndim_d) << "dim 0 of indices tensor must be no more "
1373  << "than dimensions of data tensor";
1374  Array<PrimExpr> out_shape;
1375  for (size_t i = 1; i < ndim_i; ++i) {
1376  out_shape.push_back(indices->shape[i]);
1377  }
1378  for (size_t i = indices_dim0 + batch_dims; i < ndim_d; ++i) {
1379  out_shape.push_back(data->shape[i]);
1380  }
1381  return compute(
1382  out_shape,
1383  [&](const Array<Var>& out_index) {
1384  Array<PrimExpr> indices_position;
1385  indices_position.push_back(0);
1386  for (size_t i = 0; i < ndim_i - 1; ++i) {
1387  indices_position.push_back(out_index[i]);
1388  }
1389  Array<PrimExpr> real_indices;
1390  for (size_t i = 0; i < static_cast<size_t>(batch_dims); ++i) {
1391  real_indices.push_back(out_index[i]);
1392  }
1393  for (size_t i = 0; i < indices_dim0; ++i) {
1394  indices_position.Set(0, make_const(DataType::Int(32), i));
1395  if (indices->dtype.is_int() || indices->dtype.is_uint()) {
1396  real_indices.push_back(indices(indices_position));
1397  } else {
1398  real_indices.push_back(tvm::cast(tvm::DataType::Int(32), indices(indices_position)));
1399  }
1400  }
1401  if (real_indices.size() == ndim_d) {
1402  return data(real_indices);
1403  }
1404  for (size_t i = ndim_i - 1; i < out_index.size(); ++i) {
1405  real_indices.push_back(out_index[i]);
1406  }
1407  return data(real_indices);
1408  },
1409  name, tag);
1410 }
1411 
1428  bool trans_a = false, bool trans_b = false,
1429  std::string name = "T_matmul", std::string tag = kMatMul) {
1430  tvm::Array<tvm::PrimExpr> output_shape{A->shape[trans_a ? 1 : 0], B->shape[trans_b ? 0 : 1]};
1431  auto k = tvm::te::reduce_axis(tvm::Range{0, A->shape[trans_a ? 0 : 1]}, "k");
1432  auto l = [&](tvm::tir::Var i, tvm::tir::Var j) {
1433  return tvm::sum((trans_a ? A[k][i] : A[i][k]) * (trans_b ? B[j][k] : B[k][j]), {k});
1434  };
1435  return tvm::te::compute(output_shape, l, name, tag);
1436 }
1437 
1449 inline Tensor tensordot(const Tensor& A, const tvm::te::Tensor& B, int axes = 2,
1450  std::string name = "T_tensordot", std::string tag = kMatMul) {
1451  ICHECK_GE(A->shape.size(), axes);
1452  ICHECK_GE(B->shape.size(), axes);
1453 
1454  Array<PrimExpr> output_shape(A->shape.begin(), A->shape.end() + (-axes));
1455  for (auto it = B->shape.begin() + axes; it != B->shape.end(); ++it) output_shape.push_back(*it);
1456 
1457  Array<IterVar> iter_vars;
1458  for (int i = 0; i < axes; ++i)
1459  iter_vars.push_back(reduce_axis(Range(0, B->shape[i]), "k" + std::to_string(i)));
1460 
1461  auto func = [&A, &B, &iter_vars, axes](const Array<Var>& input_indices) {
1462  Array<PrimExpr> A_indices(input_indices.begin(),
1463  input_indices.begin() + (A->shape.size() - axes));
1464  for (auto& v : iter_vars) A_indices.push_back(v);
1465 
1466  Array<PrimExpr> B_indices;
1467  for (auto& v : iter_vars) B_indices.push_back(v);
1468 
1469  auto it = input_indices.begin() + (A->shape.size() - axes);
1470  for (; it != input_indices.end(); ++it) B_indices.push_back(*it);
1471 
1472  // Some passes don't like reductions with empty axis, so avoid it here
1473  if (iter_vars.empty()) {
1474  return A(A_indices) * B(B_indices);
1475  } else {
1476  return sum(A(A_indices) * B(B_indices), iter_vars);
1477  }
1478  };
1479 
1480  return compute(output_shape, func, name, tag);
1481 }
1482 
1495 inline Tensor tensordot(const Tensor& A, const tvm::te::Tensor& B, Array<PrimExpr> A_axes,
1496  Array<PrimExpr> B_axes, std::string name = "T_tensordot",
1497  std::string tag = kMatMul) {
1498  ICHECK_EQ(A_axes.size(), B_axes.size());
1499 
1500  auto A_axes_val = GetConstIntValues(A_axes, "A_axes");
1501  auto B_axes_val = GetConstIntValues(B_axes, "B_axes");
1502 
1503  Array<PrimExpr> output_shape;
1504  for (unsigned i = 0; i < A->shape.size(); ++i)
1505  if (std::find(A_axes_val.begin(), A_axes_val.end(), i) == A_axes_val.end())
1506  output_shape.push_back(A->shape[i]);
1507  for (unsigned i = 0; i < B->shape.size(); ++i)
1508  if (std::find(B_axes_val.begin(), B_axes_val.end(), i) == B_axes_val.end())
1509  output_shape.push_back(B->shape[i]);
1510 
1511  Array<IterVar> iter_vars;
1512  for (unsigned i = 0; i < B_axes_val.size(); ++i)
1513  iter_vars.push_back(reduce_axis(Range(0, B->shape[B_axes_val[i]]), "k" + std::to_string(i)));
1514 
1515  auto func = [&A, &B, &iter_vars, A_axes_val, B_axes_val](const Array<Var>& input_indices) {
1516  int idx_input = 0;
1517  Array<PrimExpr> A_indices;
1518  for (unsigned i = 0; i < A->shape.size(); ++i) {
1519  auto axes_pos = std::find(A_axes_val.begin(), A_axes_val.end(), i);
1520  if (axes_pos == A_axes_val.end()) {
1521  A_indices.push_back(input_indices[idx_input++]);
1522  } else {
1523  A_indices.push_back(iter_vars[axes_pos - A_axes_val.begin()]);
1524  }
1525  }
1526 
1527  Array<PrimExpr> B_indices;
1528  for (unsigned i = 0; i < B->shape.size(); ++i) {
1529  auto axes_pos = std::find(B_axes_val.begin(), B_axes_val.end(), i);
1530  if (axes_pos == B_axes_val.end()) {
1531  B_indices.push_back(input_indices[idx_input++]);
1532  } else {
1533  B_indices.push_back(iter_vars[axes_pos - B_axes_val.begin()]);
1534  }
1535  }
1536  return sum(A(A_indices) * B(B_indices), iter_vars);
1537  };
1538  return compute(output_shape, func, name, tag);
1539 }
1540 
1541 inline Tensor arange(const PrimExpr& start, const PrimExpr& stop, const PrimExpr& step,
1542  DataType dtype, std::string name = "T_arange", std::string tag = kInjective) {
1543  PrimExpr num_elem = tvm::cast(
1544  tvm::DataType::Int(32), tvm::ceil(tvm::cast(tvm::DataType::Float(32), stop - start) / step));
1546  return compute(
1547  {num_elem},
1548  [&](const Array<Var>& indices) { return tvm::cast(dtype, start + step * indices[0]); }, name,
1549  tag);
1550 }
1551 
1562 inline Array<Tensor> meshgrid(const Array<Tensor>& inputs, const std::string& indexing,
1563  std::string name = "T_meshgrid", std::string tag = kInjective) {
1564  const bool cartesian_indexing = indexing == "xy" && inputs.size() >= 2;
1565  Array<PrimExpr> out_shape;
1566  for (size_t i = 0; i < inputs.size(); ++i) {
1567  const int src_index = (cartesian_indexing && i < 2) ? 1 - i : i;
1568  out_shape.push_back(inputs[src_index]->shape.size() == 0 ? 1 : inputs[src_index]->shape[0]);
1569  }
1570  Array<Tensor> result;
1571  for (size_t i = 0; i < inputs.size(); ++i) {
1572  result.push_back(compute(
1573  out_shape,
1574  [&](const Array<Var>& indices) {
1575  const int src_index = (cartesian_indexing && i < 2) ? 1 - i : i;
1576  auto ndim = inputs[i]->GetShape().size();
1577  Array<PrimExpr> real_indices = {};
1578  if (ndim > 0) {
1579  real_indices = {indices[src_index]};
1580  }
1581  return inputs[i](real_indices);
1582  },
1583  name, tag));
1584  }
1585  return result;
1586 }
1587 
1598 inline Tensor layout_transform(const Tensor& src, const std::string& src_layout,
1599  const std::string& dst_layout,
1600  const std::string schedule_rule = "None",
1601  const std::string name = "T_layout_trans",
1602  const std::string tag = kInjective) {
1603  Layout src_layout_struct(src_layout);
1604  Layout dst_layout_struct(dst_layout);
1605 
1606  if (src_layout_struct.Equals(dst_layout_struct)) {
1607  return src;
1608  }
1609 
1610  ICHECK(src_layout_struct.defined() && dst_layout_struct.defined())
1611  << "cannot convert from/to undefined layout";
1612 
1613  auto layout_converter = tir::BijectiveLayout(src_layout_struct, dst_layout_struct);
1614  ICHECK(layout_converter.defined())
1615  << "cannot convert from " << src_layout << " to " << dst_layout;
1616 
1617  Array<PrimExpr> dst_shape = layout_converter.ForwardShape(src->shape);
1618 
1619  Map<String, ObjectRef> attrs = {{"schedule_rule", String(schedule_rule)},
1620  // Information about layouts needed for the schedule rule
1621  {"src_layout", String(src_layout)},
1622  {"dst_layout", String(dst_layout)},
1623  {"input_shape", src->shape}};
1624 
1625  return compute(
1626  dst_shape,
1627  [&](const Array<Var>& dst_indices) {
1628  Array<PrimExpr> dst_indices_expr(dst_indices.begin(), dst_indices.end());
1629  Array<PrimExpr> src_indices = layout_converter.BackwardIndex(dst_indices_expr);
1630  PrimExpr in_range = PrimExpr(1) > PrimExpr(0); // init with dtype=bool and value=true
1631  for (size_t i = 0; i < src.ndim(); ++i) {
1632  in_range = in_range && (src_indices[i] < src->shape[i]);
1633  }
1634  return if_then_else(in_range, src(src_indices), tvm::cast(src->dtype, PrimExpr(0)));
1635  },
1636  name, tag, attrs);
1637 }
1638 
1641  std::vector<std::string>* axes) {
1642  int32_t factor = 0;
1643  std::string axis = "";
1644  for (char c : std::string(layout)) {
1645  if (c >= 'A' && c <= 'z') {
1646  axis += c;
1647  if (factor != 0) {
1648  shape->push_back(factor);
1649  factor = 0;
1650  }
1651  } else if (c >= '0' && c <= '9') {
1652  factor = factor * 10 + c - '0';
1653  if (!axis.empty()) {
1654  axes->push_back(axis);
1655  axis = "";
1656  }
1657  } else {
1658  LOG(FATAL) << "Invalid layout " << layout;
1659  }
1660  }
1661  if (!axis.empty()) {
1662  axes->push_back(axis);
1663  }
1664 }
1665 
1676 inline Tensor auto_scheduler_layout_transform(const Tensor& src, const String& src_layout,
1677  const String& dst_layout,
1678  const String name = "T_auto_scheduler_layout_trans",
1679  const String tag = kInjective) {
1680  Array<PrimExpr> src_shape;
1681  std::vector<std::string> src_axes;
1682  Array<PrimExpr> dst_shape;
1683  std::vector<std::string> dst_axes;
1684 
1685  parse_auto_scheduler_layout(src_layout, &src_shape, &src_axes);
1686  parse_auto_scheduler_layout(dst_layout, &dst_shape, &dst_axes);
1687  return compute(
1688  dst_shape,
1689  [&](const Array<Var>& dst_indices) {
1690  Array<PrimExpr> dst_indices_expr(dst_indices.begin(), dst_indices.end());
1691  Array<PrimExpr> src_indices;
1692  for (const std::string& src_axis : src_axes) {
1693  PrimExpr src_index = 0;
1694  CHECK_EQ(dst_indices_expr.size(), dst_axes.size());
1695  for (size_t i = 0; i < dst_axes.size(); ++i) {
1696  if (dst_axes[i] == src_axis) {
1697  src_index = src_index * dst_shape[i] + dst_indices_expr[i];
1698  }
1699  }
1700  src_indices.push_back(src_index);
1701  }
1702  return src(src_indices);
1703  },
1704  name, tag);
1705 }
1706 
1743 inline Tensor meta_schedule_layout_transform(const Tensor& src, const tir::IndexMap& index_map,
1744  const String name = "T_meta_schedule_layout_trans",
1745  const String tag = kInjective) {
1746  Array<Range> iter_domain;
1747  iter_domain.reserve(src->shape.size());
1748  for (const PrimExpr& e : src->shape) {
1749  iter_domain.push_back(Range::FromMinExtent(make_zero(e->dtype), e));
1750  }
1751  Array<PrimExpr> post_transform_shape = index_map->MapShape(src->shape);
1752  return compute(
1753  post_transform_shape,
1754  [src, inv = index_map.Inverse(iter_domain)](const Array<Var>& indices) -> PrimExpr {
1755  return src(inv->MapIndices(Array<PrimExpr>{indices.begin(), indices.end()}));
1756  },
1757  name, tag);
1758 }
1759 
1768 inline Tensor shape(const Tensor& src, DataType dtype, const std::string name = "T_shape",
1769  const std::string tag = kInjective) {
1770  int ndim = static_cast<int>(src->shape.size());
1771  Array<PrimExpr> out_shape{ndim};
1772  return compute(
1773  out_shape,
1774  [&](const Array<Var>& indices) {
1775  auto idx = indices[0];
1776  PrimExpr ret = 0;
1777  for (int i = 0; i < ndim; ++i) {
1778  ret = tvm::if_then_else(idx == i, src->shape[i], ret);
1779  }
1780  return tvm::cast(dtype, ret);
1781  },
1782  name, tag);
1783 }
1784 
1793 inline Tensor ndarray_size(const Tensor& src, const DataType& dtype,
1794  const std::string& name = "ndarray_size",
1795  const std::string& tag = kInjective) {
1796  int ndim = static_cast<int>(src->shape.size());
1797  Array<PrimExpr> out_ndarray_size = {};
1798  return compute(
1799  out_ndarray_size,
1800  [&](const Array<Var>& indices) {
1801  PrimExpr ret = 1;
1802  for (int i = 0; i < ndim; ++i) {
1803  ret *= src->shape[i];
1804  }
1805  return tvm::cast(dtype, ret);
1806  },
1807  name, tag);
1808 }
1809 
1824 inline Tensor one_hot(const Tensor& indices, const PrimExpr on_value, const PrimExpr off_value,
1825  int depth, int axis, const DataType& dtype,
1826  Array<PrimExpr> oshape = Array<PrimExpr>(),
1827  const std::string name = "T_one_hot", const std::string tag = kInjective) {
1828  int true_axis = (axis == -1) ? indices->shape.size() : axis;
1829  if (oshape.size() == 0) {
1830  int ndim = indices->shape.size() + 1;
1831  int indices_index = 0;
1832  for (int i = 0; i < ndim; i++) {
1833  if (i == true_axis) {
1834  oshape.push_back(Integer(depth));
1835  } else {
1836  oshape.push_back(indices->shape[indices_index++]);
1837  }
1838  }
1839  }
1840 
1841  PrimExpr on_value_cast = cast(dtype, on_value);
1842  PrimExpr off_value_cast = cast(dtype, off_value);
1843  return compute(
1844  oshape,
1845  [&](const Array<Var>& iter_vars) {
1846  Array<Var> indices_indices;
1847  for (size_t i = 0; i < iter_vars.size(); i++) {
1848  if (static_cast<int>(i) == true_axis) {
1849  continue;
1850  }
1851 
1852  indices_indices.push_back(iter_vars[i]);
1853  }
1854 
1855  auto idx = iter_vars[true_axis];
1856  return tir::Select(indices(indices_indices) == idx, on_value_cast, off_value_cast);
1857  },
1858  name, tag);
1859 }
1860 
1871 inline Tensor sparse_to_dense(const Tensor& sparse_indices, const Array<PrimExpr>& output_shape,
1872  const Tensor& sparse_values, const PrimExpr& default_value,
1873  const std::string name = "T_sparse_to_dense",
1874  const std::string tag = kInjective) {
1875  ICHECK(sparse_indices->dtype.is_int()) << "sparse_indices only accepts integer values";
1876  ICHECK_LE(sparse_indices->shape.size(), 3)
1877  << "sparse_indices tensor should be 0D, 1D, or 2D only";
1878  ICHECK_LE(sparse_values->shape.size(), 2) << "sparse_values tensor should be 0D or 1D only";
1879 
1880  const auto rank_sparse_indices = static_cast<int>(sparse_indices->shape.size());
1881  Array<PrimExpr> oshape;
1882  for (auto l : output_shape) {
1883  oshape.push_back(l);
1884  }
1885  return compute(
1886  oshape,
1887  [&](const Array<Var>& indices) {
1888  PrimExpr ret = default_value;
1889  if (0 == rank_sparse_indices) {
1890  ret = if_then_else(indices[0] == sparse_indices(), sparse_values(), ret);
1891  } else if (1 == rank_sparse_indices) {
1892  for (int j = 0; j < GetConstInt(sparse_indices->shape[0]); j++) {
1893  ret = if_then_else(indices[0] == sparse_indices[j], sparse_values[j], ret);
1894  }
1895  } else {
1896  for (int j = 0; j < GetConstInt(sparse_indices->shape[0]); j++) {
1897  PrimExpr aggregate_condition;
1898  for (int k = 0; k < GetConstInt(sparse_indices->shape[1]); k++) {
1899  PrimExpr comparision = indices[k] == sparse_indices[j][k];
1900  aggregate_condition = 0 == k ? comparision : aggregate_condition && comparision;
1901  }
1902  ret = if_then_else(aggregate_condition, sparse_values[j], ret);
1903  }
1904  }
1905  return ret;
1906  },
1907  name, tag);
1908 }
1909 
1922 inline Tensor matrix_set_diag(const Tensor& input, const Tensor& diagonal, int k1, int k2,
1923  bool super_diag_right_align, bool sub_diag_right_align,
1924  const std::string name = "T_matrix_set_diag",
1925  const std::string tag = kInjective) {
1926  size_t ndim = input->shape.size() - 1;
1927 
1928  bool only_one_diagonal = k1 == k2;
1929 
1930  return compute(
1931  input->shape,
1932  [&](const Array<Var>& iter_vars) {
1933  auto get_diag = [&]() {
1934  Array<PrimExpr> diagonal_indices;
1935  PrimExpr k, offset = 0;
1936  for (size_t i = 0; i < ndim - 1; i++) {
1937  diagonal_indices.push_back(iter_vars[i]);
1938  }
1939  if (only_one_diagonal) {
1940  k = k1;
1941  } else {
1942  // Determining which diagonal/sub-diagonal/super-diagonal it is
1943  k = iter_vars[ndim] - iter_vars[ndim - 1];
1944  diagonal_indices.push_back(k2 - k);
1945 
1946  // Calculating the offset in diagonal tensor for this diagonal
1947  auto get_offset = [&](PrimExpr M, PrimExpr N) {
1948  // offset = max_diagonal_length - diagonal_length
1949  return diagonal->shape[diagonal->shape.size() - 1] - if_then_else(M < N, M, N);
1950  };
1951  offset = if_then_else(
1952  k >= 0,
1953  super_diag_right_align ? get_offset(input->shape[ndim] - k, input->shape[ndim - 1])
1954  : 0,
1955  sub_diag_right_align ? get_offset(input->shape[ndim], input->shape[ndim - 1] + k)
1956  : 0);
1957  }
1958  diagonal_indices.push_back(if_then_else(k >= 0, iter_vars[ndim - 1], iter_vars[ndim]) +
1959  offset);
1960  return diagonal(diagonal_indices);
1961  };
1962  return if_then_else((PrimExpr)iter_vars[ndim] - iter_vars[ndim - 1] >= k1,
1963  if_then_else((PrimExpr)iter_vars[ndim] - iter_vars[ndim - 1] <= k2,
1964  get_diag(), input(iter_vars)),
1965  input(iter_vars));
1966  },
1967  name, tag);
1968 }
1969 
1978 inline Tensor adv_index(const Tensor& data, const Array<Tensor>& indices,
1979  const std::string name = "advanced_index",
1980  const std::string tag = kInjective) {
1981  ICHECK_LE(indices.size(), data->shape.size()) << "too many indices for data!";
1982  Array<PrimExpr> oshape;
1983  Array<PrimExpr> broadcast_shape;
1984  Array<Tensor> bindices;
1985 
1986  broadcast_shape = indices[0]->shape;
1987  for (size_t i = 1; i < indices.size(); ++i) {
1988  auto bh = detail::BroadcastShape(broadcast_shape, indices[i]->shape);
1989  broadcast_shape = Array<PrimExpr>(bh.common_shape.begin(), bh.common_shape.end());
1990  }
1991  if (indices.size() == 1) {
1992  // quick path
1993  bindices = indices;
1994  } else {
1995  // Do broadcast for indices
1996  for (size_t i = 0; i < indices.size(); ++i) {
1997  bindices.push_back(broadcast_to(indices[i], broadcast_shape));
1998  }
1999  }
2000 
2001  for (const auto& dim : broadcast_shape) {
2002  oshape.push_back(dim);
2003  }
2004  for (size_t i = indices.size(); i < data->shape.size(); ++i) {
2005  oshape.push_back(data->shape[i]);
2006  }
2007 
2008  return compute(
2009  oshape,
2010  [&](const Array<Var>& iter_var) {
2011  Array<PrimExpr> tensor_indices;
2012  for (size_t i = 0; i < broadcast_shape.size(); ++i) {
2013  tensor_indices.push_back(iter_var[i]);
2014  }
2015 
2016  Array<PrimExpr> real_indices;
2017  for (size_t i = 0; i < bindices.size(); ++i) {
2018  real_indices.push_back(bindices[i](tensor_indices));
2019  }
2020  for (size_t i = broadcast_shape.size(); i < iter_var.size(); ++i) {
2021  real_indices.push_back(iter_var[i]);
2022  }
2023 
2024  return data(real_indices);
2025  },
2026  name, tag);
2027 }
2028 
2029 } // namespace topi
2030 } // namespace tvm
2031 #endif // TVM_TOPI_TRANSFORM_H_
void reserve(int64_t n)
Make sure the list has the capacity of at least n.
Definition: array.h:569
Managed reference to LayoutNode.
Definition: data_layout.h:123
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
bool Equals(const Layout &rhs) const
Whether the two layouts are equal.
Definition: data_layout.h:278
Tensor strided_slice_with_axes(const Tensor &x, const Array< Integer > &begin, const Array< Integer > &end, const Array< Integer > &strides, const Array< Integer > &axes, std::string slice_mode="end", std::string name="T_strided_slice_with_axes", std::string tag=kInjective)
strided_slice of a tensor
Definition: transform.h:770
Tensor sparse_to_dense(const Tensor &sparse_indices, const Array< PrimExpr > &output_shape, const Tensor &sparse_values, const PrimExpr &default_value, const std::string name="T_sparse_to_dense", const std::string tag=kInjective)
Get a dense tensor.
Definition: transform.h:1871
PrimExpr indexmod(PrimExpr a, PrimExpr b, Span span=Span())
compute the remainder floor(a / b) where a and b are non-negative.
Array< PrimExpr > StridedSliceOutputShape(const Array< PrimExpr > &ishape, const Array< Integer > &begin, const Array< Integer > &end, const Array< Integer > &strides, const Array< Integer > &axes, const std::string &slice_mode)
Calcluate the output shape of strided_slice, the entry point for Relay type relation.
Definition: transform.h:742
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:954
static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span=Span())
construct a new range with min and extent The corresponding constructor is removed, because that is counter convention of tradition meaning of range(begin, end)
Tensor sliding_window(const Tensor &x, int axis, Array< Integer > window_shape, Array< Integer > strides, std::string name="T_sliding_window", std::string tag="")
Creates an operation to slide a window over the input x.
Definition: transform.h:68
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Tensor layout_transform(const Tensor &src, const std::string &src_layout, const std::string &dst_layout, const std::string schedule_rule="None", const std::string name="T_layout_trans", const std::string tag=kInjective)
Transform the layout according to src_layout and dst_layout.
Definition: transform.h:1598
Tensor expression language DSL.
Definition: extracted_task.h:33
Tensor tensordot(const Tensor &A, const tvm::te::Tensor &B, int axes=2, std::string name="T_tensordot", std::string tag=kMatMul)
A generalization of matrix multiplication to tensors.
Definition: transform.h:1449
Tensor dynamic_strided_slice(const Tensor &x, const Array< PrimExpr > &begin, const Array< PrimExpr > &end, const Array< PrimExpr > &strides, std::string name="T_dynamic_strided_slice", std::string tag=kInjective)
strided_slice of a tensor where begin/end/stride can be mixed static and dynamic
Definition: transform.h:652
PrimExpr ceil(PrimExpr x, Span span=Span())
Calculate ceil(x)
a named variable in TIR
Definition: var.h:88
Tensor where(const Tensor &condition, const Tensor &x, const Tensor &y, std::string name="T_where", std::string tag=kBroadcast)
Return the elements, either from x or y, depending on the condition.
Definition: transform.h:1134
Tensor one_hot(const Tensor &indices, const PrimExpr on_value, const PrimExpr off_value, int depth, int axis, const DataType &dtype, Array< PrimExpr > oshape=Array< PrimExpr >(), const std::string name="T_one_hot", const std::string tag=kInjective)
Returns a one-hot tensor where the locations repsented by indices take value on_value, other locations take value off_value.
Definition: transform.h:1824
PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Span span=Span())
Conditional expression.
Tensor matrix_set_diag(const Tensor &input, const Tensor &diagonal, int k1, int k2, bool super_diag_right_align, bool sub_diag_right_align, const std::string name="T_matrix_set_diag", const std::string tag=kInjective)
Returns a tensor with the diagonal of input tensor replaced with the provided diagonals.
Definition: transform.h:1922
constexpr auto kMatMul
Definition: tags.h:37
constexpr auto kInjective
Definition: tags.h:33
PrimExpr Simplify(const PrimExpr &expr, int steps=2)
Simplify expr.
Utility functions for strided_slice op.
Tensor unravel_index(const Tensor &x, const Tensor &shape, std::string name="T_unravel", std::string tag=kInjective)
Converts a flat index or array of flat indices into a tuple of coordinate arrays. ...
Definition: transform.h:359
Array< Tensor > split(const Tensor &x, Array< PrimExpr > split_indices, int axis, std::string name="T_split", std::string tag=kInjective)
Split a tensor into multiple sub-tensors.
Definition: transform.h:575
DataType dtype() const
Definition: expr.h:128
void parse_auto_scheduler_layout(const String &layout, Array< PrimExpr > *shape, std::vector< std::string > *axes)
Utility function for auto_scheduler_layout_transform.
Definition: transform.h:1640
Tensor gather(const Tensor &data, int axis, const Tensor &indices, std::string name="T_gather", std::string tag=kInjective)
Gather values along given axis from given indices.
Definition: transform.h:1313
size_t ndim() const
Definition: tensor.h:214
Tensor dyn_tile(const Tensor &x, Array< PrimExpr > new_shape, size_t rdim, std::string name="T_tile", std::string tag=kBroadcast)
Creates an operation to tile elements of an array.
Definition: transform.h:1276
Tensor auto_scheduler_layout_transform(const Tensor &src, const String &src_layout, const String &dst_layout, const String name="T_auto_scheduler_layout_trans", const String tag=kInjective)
Transform the auto-scheduler generated layout according to src_layout and dst_layout.
Definition: transform.h:1676
PrimExpr cast(const DataType &t, PrimExpr value, Span span=Span())
cast value to type.
Tensor squeeze(const Tensor &x, Array< Integer > axis, bool atleast1d=false, std::string name="T_squeeze", std::string tag=kInjective)
Remove size 1 dimensions from the shape of a tensor. The removed dimensions must have a constant size...
Definition: transform.h:407
void Set(int64_t i, T value)
set i-th element of the array.
Definition: array.h:621
Constant integer literals in the program.
Definition: expr.h:491
void push_back(const T &item)
push a new item to the back of the list
Definition: array.h:457
Defines a remapping of buffer indices.
Tensor expand_dims(const Tensor &x, int axis, int num_newaxis=1, std::string name="T_expand_dims", std::string tag=kBroadcast)
Creates an operation to insert new dimensions of length 1.
Definition: transform.h:147
Tensor tile(const Tensor &x, Array< Integer > reps, std::string name="T_tile", std::string tag=kBroadcast)
Creates an operation to tile elements of an array.
Definition: transform.h:1221
Tensor sum(const Tensor &data, const Array< Integer > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that sums array elements over a given axis.
Definition: reduction.h:326
Utility functions for handling constants in TVM expressions.
constexpr auto kBroadcast
Definition: tags.h:36
Range constainer.
Definition: expr.h:715
Tensor arange(const PrimExpr &start, const PrimExpr &stop, const PrimExpr &step, DataType dtype, std::string name="T_arange", std::string tag=kInjective)
Definition: transform.h:1541
size_t size() const
Definition: array.h:420
Runtime primitive data type.
Definition: data_type.h:41
bool defined() const
Definition: object.h:544
Tensor stack(const Array< Tensor > &inputs, int axis=0, std::string name="T_stack", std::string tag=kInjective)
Join a sequence of tensors along a new axis.
Definition: transform.h:529
Utility functions for handling tensor.
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:178
PrimExpr sum(PrimExpr source, Array< tir::IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
sum of source expression over axis
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Definition: index_map.h:177
PrimExpr indexdiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b) where a and b are non-negative.
Tensor concatenate(const Array< Tensor > &inputs, int axis=0, std::string name="T_concat", std::string tag=kInjective)
Join a sequence of tensors along an existing axis.
Definition: transform.h:470
Tensor take(const Tensor &a, const Tensor &indices, int batch_dims, std::string mode="clip", std::string name="T_take", std::string tag=kInjective)
Take elements from an flattened input array when axis is None.
Definition: transform.h:900
Managed reference class to IntImmNode.
Definition: expr.h:520
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
int64_t value
the Internal value.
Definition: expr.h:494
Reference to string objects.
Definition: string.h:98
PrimExpr make_zero(DataType t, Span span=Span())
Make a const zero expr.
Definition: op.h:962
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1768
Array< Tensor > meshgrid(const Array< Tensor > &inputs, const std::string &indexing, std::string name="T_meshgrid", std::string tag=kInjective)
Produce grids by expanding input over dimensions defined by other inputs.
Definition: transform.h:1562
IterVar reduce_axis(Range dom, std::string name="rv")
Create a new IterVar for reduction operations.
PrimExpr truncmod(PrimExpr a, PrimExpr b, Span span=Span())
compute the remainder of truncdiv
iterator end() const
Definition: array.h:390
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:102
iterator begin() const
Definition: array.h:387
Operation node can generate one or multiple Tensors.
Tensor meta_schedule_layout_transform(const Tensor &src, const tir::IndexMap &index_map, const String name="T_meta_schedule_layout_trans", const String tag=kInjective)
Transform the meta-schedule generated layout according to TIR&#39;s IndexMap.
Definition: transform.h:1743
Managed reference to SelectNode.
Definition: expr.h:609
Bijective function mapping for data layout transformation. Given two Layout, BijectiveLayout build an...
Definition: data_layout.h:332
Tensor transpose(const Tensor &x, Array< Integer > axes, std::string name="T_transpose", std::string tag=kInjective)
Permute the dimensions of an array.
Definition: transform.h:196
PrimExpr max_value(const DataType &dtype, Span span=Span())
Tensor ndarray_size(const Tensor &src, const DataType &dtype, const std::string &name="ndarray_size", const std::string &tag=kInjective)
Get the size of input tensor.
Definition: transform.h:1793
Tensor sequence_mask(const Tensor &data, const Tensor &valid_length, double mask_value, int axis, std::string name="T_sequence_mask", std::string tag=kInjective)
Mask the out-of-boundary elements of each sequence.
Definition: transform.h:948
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Tensor adv_index(const Tensor &data, const Array< Tensor > &indices, const std::string name="advanced_index", const std::string tag=kInjective)
Numpy style advanced indexing with tensor.
Definition: transform.h:1978
External function interface to rocBLAS libraries.
Tensor compute(Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", Map< String, ObjectRef > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
Tensor reverse_sequence(const Tensor &x, const Tensor &seq_lengths, int seq_axis=1, int batch_axis=0, std::string name="T_reverse_sequence", std::string tag=kInjective)
Reverse the tensor for variable length slices. Input is first sliced along batch axis and then elemen...
Definition: transform.h:255
Tensor cast(const Tensor &x, DataType type, std::string name="T_cast", std::string tag=kElementWise)
Cast each element of x to the given type. If expr is scalar and type is a corresponding vector type...
Definition: elemwise.h:281
Tensor reshape(const Tensor &x, Array< PrimExpr > newshape, std::string name="T_reshape", std::string tag=kInjective)
Reshape a tensor.
Definition: transform.h:320
tvm::te::Tensor broadcast_to(const tvm::te::Tensor &t, const tvm::Array< tvm::PrimExpr > &output_shape, std::string name="T_broadcast_to", std::string tag=kBroadcast)
Creates an operation that broadcasts a tensor into a compatible shape according to numpy&#39;s rules...
Definition: broadcast.h:48
Tensor repeat(const Tensor &x, int repeats, int axis, std::string name="T_repeat", std::string tag=kBroadcast)
Creates an operation to repeat elements of an array.
Definition: transform.h:1174
Tensor strided_slice(const Tensor &x, const Array< Integer > &begin, const Array< Integer > &end, const Array< Integer > &strides, std::string slice_mode="end", std::string name="T_strided_slice", std::string tag=kInjective)
strided_slice of a tensor
Definition: transform.h:816
Broadcast op constructions.
Reference to PrimExprNode.
Definition: expr.h:114
Layout expression to describe the data organization of a tensor. And BijectiveLayout to mapping two d...
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:865
Tensor gather_nd(const Tensor &data, const Tensor &indices, int batch_dims=0, std::string name="T_gather_nd", std::string tag=kInjective)
Gather elements from a n-dimension array.
Definition: transform.h:1366
Array< Tensor > split_sections(const Tensor &x, int num_sections, int axis, std::string name="T_split_sections", std::string tag=kInjective)
Split a tensor into a number of sub-tensors.
Definition: transform.h:857
Detail broadcast.
Index ravel and unraval operations.
IndexMap Inverse(Array< Range > initial_ranges) const
Generate the inverse mapping.
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:579
tvm::te::Tensor matmul(const tvm::te::Tensor &A, const tvm::te::Tensor &B, bool trans_a=false, bool trans_b=false, std::string name="T_matmul", std::string tag=kMatMul)
Creates an operation that calculates a matrix multiplication (row-major notation): A(i...
Definition: transform.h:1427
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:164
Container of constant int that adds more constructors.
Definition: expr.h:622