55 std::string name =
"T_relu", std::string tag =
kElementWise) {
58 [&](
const tvm::ffi::Array<tvm::PrimVar>& i) {
59 auto threshold_const = tvm::prim::MakeConst(tvm::PrimType(t->dtype), threshold);
60 return tvm::max(t(i), threshold_const);
76 std::string name =
"T_leaky_relu",
80 [&](
const tvm::ffi::Array<tvm::PrimVar>& i) {
82 auto calpha = tvm::prim::MakeConst(value.ty(), alpha);
83 return tvm::prim::Select(value > 0, value, value * calpha);
100 const int axis = 1, std::string name =
"T_prelu",
102 TVM_FFI_ICHECK((
size_t)axis < x->
shape.size()) <<
"Wrong axis (" << axis <<
")value. ";
103 TVM_FFI_ICHECK(topi::detail::GetConstInt(slope->shape[0]) ==
104 topi::detail::GetConstInt(x->shape[axis]))
105 <<
"Wrong slope shape received.";
109 [&](
const tvm::ffi::Array<tvm::PrimVar>& indices) {
110 auto xval = x(indices);
111 return tvm::prim::Select(xval > 0, xval, xval * slope(indices[axis]));
156 const tvm::te::Tensor& t,
const tvm::ffi::Array<tvm::PrimExpr>& pad_before,
157 tvm::ffi::Array<tvm::PrimExpr> pad_after = tvm::ffi::Array<tvm::PrimExpr>(),
158 ffi::Optional<PrimExpr> pad_value = std::nullopt, std::string name =
"T_pad",
159 std::string tag =
kElementWise, std::string pad_mode =
"constant",
160 const ffi::Array<PrimExpr>* dyn_output_shape =
nullptr) {
162 if (pad_after.size() < pad_before.size()) {
163 for (
size_t i = pad_after.size(); i < pad_before.size(); ++i) {
164 pad_after.push_back(pad_before[i]);
169 TVM_FFI_ICHECK_GE(pad_before.size(), 1);
170 TVM_FFI_ICHECK_EQ(pad_before.size(), pad_after.size());
171 tvm::ffi::Array<tvm::PrimExpr> pad_before_int32;
172 tvm::ffi::Array<tvm::PrimExpr> pad_after_int32;
174 for (
const auto& ele : pad_before) {
177 for (
const auto& ele : pad_after) {
181 tvm::ffi::Array<tvm::PrimExpr> output_shape;
182 if (dyn_output_shape ==
nullptr) {
183 for (
size_t i = 0; i < t->shape.size(); ++i) {
184 if (i >= pad_before.size()) {
185 output_shape.push_back(t->shape[i]);
187 output_shape.push_back(
188 analyzer->Simplify(t->shape[i] + pad_before_int32[i] + pad_after_int32[i]));
192 for (
size_t i = 0; i < dyn_output_shape->size(); i++) {
193 output_shape.push_back((*dyn_output_shape)[i]);
197 if (!pad_value.has_value()) {
201 auto l = [&](tvm::ffi::Array<tvm::PrimVar> ovars) {
202 tvm::ffi::Array<tvm::PrimExpr> indices;
203 tvm::ffi::Array<tvm::PrimExpr> sel;
204 tvm::ffi::Array<tvm::PrimExpr> pad_idx;
205 for (
size_t i = 0; i < t->shape.size(); ++i) {
206 if (i >= pad_before_int32.size()) {
207 indices.push_back(ovars[i]);
210 if (!topi::detail::EqualCheck(pad_before_int32[i], 0)) {
211 sel.push_back(ovars[i] >= pad_before_int32[i]);
212 indices.push_back(ovars[i] - pad_before_int32[i]);
214 indices.push_back(ovars[i]);
216 if (!topi::detail::EqualCheck(pad_after_int32[i], 0)) {
217 sel.push_back(analyzer->Simplify(ovars[i].as_or_throw<
PrimExpr>() <
218 pad_before_int32[i] + t->shape[i]));
220 if (pad_mode ==
"edge") {
224 t->shape[i] - 1, ovars[i] - pad_before[i])));
225 }
else if (pad_mode ==
"reflect") {
227 ovars[i].as_or_throw<PrimExpr>() < pad_before[i], pad_before[i] - ovars[i],
229 t->shape[i] * 2 - ovars[i] + pad_before[i] - 2,
230 ovars[i] - pad_before[i])));
233 if (sel.size() != 0) {
234 if (pad_mode ==
"constant") {
238 t(indices), pad_value.value());
239 }
else if (pad_mode ==
"edge" || pad_mode ==
"reflect") {
243 t(indices), t(pad_idx));
272 int pad_h = 0,
int pad_w = 0,
int stride_h = 1,
int stride_w = 1,
273 std::string name =
"T_conv2d_nchw",
275 TVM_FFI_ICHECK_EQ(4, I->shape.size());
276 TVM_FFI_ICHECK_EQ(4, W->shape.size());
277 auto pH = I->shape[2];
278 auto pW = I->shape[3];
279 tvm::ffi::Array<tvm::PrimExpr> output_shape{
282 indexdiv(I->shape[2] - W->shape[2] + 2 * pad_h, stride_h) + 1,
283 indexdiv(I->shape[3] - W->shape[3] + 2 * pad_w, stride_w) + 1
291 return tvm::prim::sum(T(b, i, stride_h * h + kh, stride_w * w + kw) * W(o, i, kh, kw),
317 int pad_h = 0,
int pad_w = 0,
int stride_h = 1,
int stride_w = 1,
318 std::string name =
"T_conv2d_hwcn",
320 TVM_FFI_ICHECK_EQ(4, I->shape.size());
321 TVM_FFI_ICHECK_EQ(4, W->shape.size());
322 auto pH = I->shape[2];
323 auto pW = I->shape[3];
324 tvm::ffi::Array<tvm::PrimExpr> output_shape{
325 indexdiv(I->shape[2] - W->shape[2] + 2 * pad_h, stride_h) + 1,
326 indexdiv(I->shape[3] - W->shape[3] + 2 * pad_w, stride_w) + 1,
333 auto T = (pad_h == 0 && pad_w == 0) ? I :
pad(I, {pad_h, pad_w});
335 return tvm::prim::sum(T(stride_h * h + kh, stride_w * w + kw, i, b) * W(kh, kw, i, o),
362 int pad_h = 0,
int pad_w = 0,
int stride_h = 1,
364 std::string name =
"T_depthwise_conv2d_nchw",
366 TVM_FFI_ICHECK_EQ(4, I->shape.size());
367 TVM_FFI_ICHECK_EQ(4, W->shape.size());
368 auto pH = I->shape[2];
369 auto pW = I->shape[3];
370 auto pCM = W->shape[1];
371 tvm::ffi::Array<tvm::PrimExpr> output_shape{
374 indexdiv(I->shape[2] - W->shape[2] + 2 * pad_h, stride_h) + 1,
375 indexdiv(I->shape[3] - W->shape[3] + 2 * pad_w, stride_w) + 1
391 int pad_h = 0,
int pad_w = 0,
int stride_h = 1,
393 std::string name =
"T_depthwise_conv2d_nhwc",
395 TVM_FFI_ICHECK_EQ(4, I->shape.size());
396 TVM_FFI_ICHECK_EQ(4, W->shape.size());
397 auto pH = I->shape[1];
398 auto pW = I->shape[2];
399 auto pCM = W->shape[1];
400 tvm::ffi::Array<tvm::PrimExpr> output_shape{
402 indexdiv(I->shape[1] - W->shape[1] + 2 * pad_h, stride_h) + 1,
403 indexdiv(I->shape[2] - W->shape[2] + 2 * pad_w, stride_w) + 1,
440 int pad_h = 0,
int pad_w = 0,
int stride_h = 1,
442 std::string name =
"T_group_conv2d_ngchw",
444 TVM_FFI_ICHECK_EQ(5, I->shape.size());
445 TVM_FFI_ICHECK_EQ(5, W->shape.size());
446 auto pH = I->shape[2];
447 auto pW = I->shape[3];
448 tvm::ffi::Array<tvm::PrimExpr> output_shape{
452 indexdiv(I->shape[3] - W->shape[3] + 2 * pad_h, stride_h) + 1,
453 indexdiv(I->shape[4] - W->shape[4] + 2 * pad_w, stride_w) + 1
459 auto T = (pad_h == 0 && pad_w == 0)
462 auto l = [&](tvm::ffi::Array<tvm::PrimVar> args) {
468 return tvm::prim::sum(I(b, g, i, stride_h * h + kh, stride_w * w + kw) * W(g, i, o, kh, kw),
488 const tvm::ffi::Array<int64_t>& block_shape,
489 const tvm::ffi::Array<tvm::PrimExpr>& pad_before,
490 const tvm::ffi::Array<tvm::PrimExpr>& pad_after,
491 ffi::Optional<PrimExpr> pad_value = std::nullopt,
492 std::string name =
"space_to_batch_nd",
494 TVM_FFI_ICHECK_EQ(pad_before.size(), pad_after.size());
495 TVM_FFI_ICHECK_EQ(block_shape.size(), pad_before.size())
496 <<
"Paddings must be provided for each spatial dimension";
497 tvm::ffi::Array<tvm::PrimExpr> pad_before_int32;
498 tvm::ffi::Array<tvm::PrimExpr> pad_after_int32;
504 for (
const auto& ele : pad_before) {
507 for (
const auto& ele : pad_after) {
512 if (!pad_value.has_value()) {
517 auto input_shape = data->shape;
518 auto padded_shape = padded_t->shape;
521 tvm::ffi::Array<PrimExpr> r_shape;
522 tvm::ffi::Array<int64_t> axis;
523 tvm::ffi::Array<PrimExpr> o_shape;
525 size_t num_block_dims = block_shape.size();
526 int batch =
static_cast<int>(GetConstInt(input_shape[0]));
528 r_shape.push_back(batch);
530 for (
size_t i = 1; i <= num_block_dims; i++) {
531 int padded_input =
static_cast<int>(GetConstInt(padded_shape[i]));
532 int block_size =
static_cast<int>(block_shape[i - 1]);
533 TVM_FFI_ICHECK_EQ((padded_input % block_size), 0)
536 "Input dimension after padding ("
537 << padded_input <<
")"
538 <<
" must be divisible by its block size (" << block_size <<
")";
541 r_shape.push_back(
div(padded_shape[i], bs));
542 r_shape.push_back(bs);
543 block_shape_prod *= bs;
544 axis.push_back(
static_cast<int64_t
>(r_shape.size() - 1));
547 size_t n = axis.size();
550 for (
size_t i = 0; i < n; i++) {
551 axis.push_back(axis[i] - 1);
554 for (
size_t i = 1; i <= num_block_dims; i++) {
556 o_shape.push_back(
div(padded_shape[i], bs));
559 for (
size_t i = num_block_dims + 1; i < input_shape.size(); i++) {
560 r_shape.push_back(input_shape[i]);
562 static_cast<int64_t
>(r_shape.size() - 1));
563 o_shape.push_back(input_shape[i]);
568 output =
reshape(output, o_shape);
586 const tvm::ffi::Array<int64_t>& block_shape,
587 const tvm::ffi::Array<tvm::PrimExpr>& crop_begin_list,
588 const tvm::ffi::Array<tvm::PrimExpr>& crop_end_list,
589 std::string name =
"batch_to_space_nd",
592 ffi::Array<PrimExpr> in_shape = data->shape;
593 ffi::Array<PrimExpr> r_shape;
594 ffi::Array<int64_t> axis;
595 size_t num_block_dims = block_shape.size();
596 size_t num_input_dims = in_shape.size();
598 int batch =
static_cast<int>(GetConstInt(in_shape[0]));
600 for (
size_t i = 0; i < num_block_dims; i++) {
602 r_shape.push_back(bs);
603 block_shape_prod *= bs;
605 axis.push_back(
static_cast<int64_t
>(r_shape.size()));
606 r_shape.push_back(batch / block_shape_prod);
608 for (
size_t i = 1; i < num_input_dims; i++) {
609 axis.push_back(
static_cast<int64_t
>(r_shape.size()));
610 if (axis.size() < (num_block_dims + num_input_dims)) {
612 static_cast<int64_t
>(r_shape.size() - (num_block_dims + 1)));
614 r_shape.push_back(in_shape[i]);
617 ffi::Array<PrimExpr> r_p_shape;
618 r_p_shape.push_back(batch / block_shape_prod);
619 for (
size_t i = 1; i <= num_block_dims; i++) {
621 r_p_shape.push_back(in_shape[i] * bs);
623 for (
size_t i = num_block_dims + 1; i < num_input_dims; i++) {
624 r_p_shape.push_back(in_shape[i]);
632 ffi::Array<ffi::Optional<IntImm>> begin_idx, end_idx;
633 ffi::Array<IntImm> strides;
635 for (
size_t i = 0; i < r_p_shape.size(); ++i) {
636 strides.push_back(
IntImm(index_ty, 1));
637 if (i > 0 && i <= num_block_dims) {
639 const auto* begin_i_imm = crop_begin_list[i - 1].as<
IntImmNode>();
640 ffi::BigInt begin_i = begin_i_imm ? begin_i_imm->
value : GetConstInt(crop_begin_list[i - 1]);
641 const auto* end_i_imm = crop_end_list[i - 1].as<
IntImmNode>();
642 ffi::BigInt end_i = end_i_imm ? end_i_imm->
value : GetConstInt(crop_end_list[i - 1]);
643 const auto* out_i_imm = r_p_shape[i].as<
IntImmNode>();
644 ffi::BigInt out_i = out_i_imm ? out_i_imm->
value : GetConstInt(r_p_shape[i]);
645 TVM_FFI_ICHECK_GT(out_i, (begin_i + end_i))
646 <<
"Incorrect crop sizes for (" << i <<
")th dim, can not crop more than"
647 <<
" output size" << out_i <<
" vs " << (begin_i + end_i);
648 begin_idx.push_back(
IntImm(index_ty, begin_i));
649 end_idx.push_back(
IntImm(index_ty, out_i - end_i));
652 begin_idx.push_back(
IntImm(index_ty, 0));
653 const auto* extent = r_p_shape[i].as<
IntImmNode>();
654 end_idx.push_back(
IntImm(index_ty, extent ? extent->value : GetConstInt(r_p_shape[i])));
676 std::string reduction =
"mean",
int ignore_index = -100,
677 const std::string name =
"nll_loss",
const std::string tag =
kBroadcast) {
678 if (predictions.
ndim() == 1) {
683 [&](
const tvm::ffi::Array<tvm::PrimVar>& target_indices) {
689 if (reduction ==
"mean") {
692 [&](
const tvm::ffi::Array<tvm::PrimVar>& target_indices) {
705 [&](
const tvm::ffi::Array<tvm::PrimVar>& target_indices) {
706 auto c = targets(target_indices);
707 tvm::ffi::Array<tvm::PrimExpr> pred_indices;
708 pred_indices.push_back(target_indices[0]);
709 pred_indices.push_back(c);
710 for (size_t i = 1; i < target_indices.size(); i++) {
711 pred_indices.push_back(target_indices[i]);
713 return tvm::prim::Select(c != ignore_index, -predictions(pred_indices) * weights(c),
717 TVM_FFI_ICHECK(T->shape.size() != 0);
718 if (reduction ==
"mean") {
721 [&](
const tvm::ffi::Array<tvm::PrimVar>& target_indices) {
722 auto c = targets(target_indices);
723 return tvm::prim::Select(c != ignore_index, weights(c),
724 tvm::prim::MakeConst(tvm::PrimType(predictions->dtype), 0));
728 }
else if (reduction ==
"sum") {
Algebra expression simplifications.
Constant integer literals in the program.
Definition expr.h:135
ffi::BigInt value
the Internal value.
Definition expr.h:138
Managed reference class to IntImmNode.
Definition expr.h:152
static IntImm Int64(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int64 constant.
Definition expr.h:195
static IntImm Bool(bool value, Location loc=UnknownLoc())
Construct a scalar boolean constant.
Definition expr.h:172
Non-nullable source-location metadata.
Definition location.h:91
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Definition base_expr.h:161
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Checked scalar view over a VarNode.
Definition expr.h:742
Range container
Definition expr.h:1025
Managed reference to SelectNode.
Definition expr.h:712
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:111
size_t ndim() const
Definition tensor.h:236
Utility functions for handling constants in TVM expressions.
Shared primitive construction and constant helpers.
PrimExpr sum(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
sum of source expression over axis
PrimExpr MakeConst(PrimType dtype, ValueType value, Location loc=UnknownLoc())
Make a const value with certain data type.
Definition op.h:553
TVM_TIRX_DLL PrimExpr cast(PrimType t, PrimExpr value, Location loc=UnknownLoc())
cast value to type.
Tensor expression language DSL.
Definition extracted_task.h:33
IterVar reduce_axis(Range dom, std::string name="rv")
Create a new IterVar for reduction operations.
Tensor compute(ffi::Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", ffi::Map< ffi::String, ffi::Any > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
constexpr auto kElementWise
Definition tags.h:32
tvm::te::Tensor pad(const tvm::te::Tensor &t, const tvm::ffi::Array< tvm::PrimExpr > &pad_before, tvm::ffi::Array< tvm::PrimExpr > pad_after=tvm::ffi::Array< tvm::PrimExpr >(), ffi::Optional< PrimExpr > pad_value=std::nullopt, std::string name="T_pad", std::string tag=kElementWise, std::string pad_mode="constant", const ffi::Array< PrimExpr > *dyn_output_shape=nullptr)
Creates an operation that performs padding.
Definition nn.h:155
Tensor reshape(const Tensor &x, ffi::Array< PrimExpr > newshape, std::string name="T_reshape", std::string tag=kInjective)
Reshape a tensor.
Definition transform.h:329
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition transform.h:2016
tvm::te::Tensor batch_to_space_nd(const tvm::te::Tensor &data, const tvm::ffi::Array< int64_t > &block_shape, const tvm::ffi::Array< tvm::PrimExpr > &crop_begin_list, const tvm::ffi::Array< tvm::PrimExpr > &crop_end_list, std::string name="batch_to_space_nd", std::string tag=kInjective)
Reshape the batch dimension into spatial dimensions.
Definition nn.h:585
constexpr auto kBroadcast
Definition tags.h:36
constexpr auto kInjective
Definition tags.h:33
Tensor sum(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that sums array elements over a given axis.
Definition reduction.h:336
constexpr auto kConv2dNCHW
Definition tags.h:38
tvm::te::Tensor prelu(const tvm::te::Tensor &x, const tvm::te::Tensor &slope, const int axis=1, std::string name="T_prelu", std::string tag=kBroadcast)
Creates an operation that performs a parametric rectified linear unit.
Definition nn.h:99
Tensor strided_slice(const Tensor &x, const ffi::Array< ffi::Optional< IntImm > > &begin, const ffi::Array< ffi::Optional< IntImm > > &end, const ffi::Array< IntImm > &strides, std::string slice_mode="end", std::string name="T_strided_slice", std::string tag=kInjective)
strided_slice of a tensor
Definition transform.h:965
tvm::te::Tensor group_conv2d_ngchw(const tvm::te::Tensor &I, const tvm::te::Tensor &W, int pad_h=0, int pad_w=0, int stride_h=1, int stride_w=1, std::string name="T_group_conv2d_ngchw", std::string tag=kGroupConv2d)
Creates an operation that performs a 2-D group convolution with an NGCHW-layout.
Definition nn.h:439
tvm::te::Tensor leaky_relu(const tvm::te::Tensor &t, double alpha=0.1, std::string name="T_leaky_relu", std::string tag=kElementWise)
Creates an operation that performs a leaky rectified linear unit.
Definition nn.h:75
tvm::PrimExpr divide(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition broadcast.h:241
constexpr auto kDepthwiseConv2dNCHW
Definition tags.h:40
tvm::te::Tensor depthwise_conv2d_nchw(const tvm::te::Tensor &I, const tvm::te::Tensor &W, int pad_h=0, int pad_w=0, int stride_h=1, int stride_w=1, std::string name="T_depthwise_conv2d_nchw", std::string tag=kDepthwiseConv2dNCHW)
Creates an operation that performs a 2-D depthwise convolution with an NCHW-layout.
Definition nn.h:361
constexpr auto kGroupConv2d
Definition tags.h:45
constexpr auto kConv2dHWCN
Definition tags.h:39
constexpr auto kDepthwiseConv2dNHWC
Definition tags.h:41
tvm::te::Tensor conv2d_nchw(const tvm::te::Tensor &I, const tvm::te::Tensor &W, int pad_h=0, int pad_w=0, int stride_h=1, int stride_w=1, std::string name="T_conv2d_nchw", std::string tag=kConv2dNCHW)
Creates an operation that performs a 2-D convolution with an NCHW-layout.
Definition nn.h:271
Tensor transpose(const Tensor &x, ffi::Optional< ffi::Array< int64_t > > opt_axes, std::string name="T_transpose", std::string tag=kInjective)
Permute the dimensions of an array.
Definition transform.h:204
tvm::te::Tensor conv2d_hwcn(const tvm::te::Tensor &I, const tvm::te::Tensor &W, int pad_h=0, int pad_w=0, int stride_h=1, int stride_w=1, std::string name="T_conv2d_hwcn", std::string tag=kConv2dHWCN)
Creates an operation for 2-D convolution layer with an HWCN-layout.
Definition nn.h:316
tvm::te::Tensor space_to_batch_nd(const tvm::te::Tensor &data, const tvm::ffi::Array< int64_t > &block_shape, const tvm::ffi::Array< tvm::PrimExpr > &pad_before, const tvm::ffi::Array< tvm::PrimExpr > &pad_after, ffi::Optional< PrimExpr > pad_value=std::nullopt, std::string name="space_to_batch_nd", std::string tag=kInjective)
Divide spatial dimensions of the input into a grid of blocks.
Definition nn.h:487
tvm::te::Tensor relu(const tvm::te::Tensor &t, T threshold=static_cast< T >(0), std::string name="T_relu", std::string tag=kElementWise)
Creates an operation that performs a rectified linear unit.
Definition nn.h:54
Tensor nll_loss(const Tensor &predictions, const Tensor &targets, const Tensor &weights, std::string reduction="mean", int ignore_index=-100, const std::string name="nll_loss", const std::string tag=kBroadcast)
Negative log likelihood loss.
Definition nn.h:675
tvm::te::Tensor depthwise_conv2d_nhwc(const tvm::te::Tensor &I, const tvm::te::Tensor &W, int pad_h=0, int pad_w=0, int stride_h=1, int stride_w=1, std::string name="T_depthwise_conv2d_nhwc", std::string tag=kDepthwiseConv2dNHWC)
Definition nn.h:390
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
TVM_TIRX_DLL PrimExpr logical_and(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
and
TVM_TIRX_DLL PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Location loc=UnknownLoc())
Conditional expression.
TVM_TIRX_DLL PrimExpr indexdiv(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute floor(a / b) where a and b are non-negative.
TVM_TIRX_DLL PrimExpr div(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute division in C semantics.
TVM_TIRX_DLL PrimExpr indexmod(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute the remainder floor(a / b) where a and b are non-negative.
Operation node can generate one or multiple Tensors.
Reduction op constructors.