24#ifndef TVM_TOPI_NN_SOFTMAX_H_
25#define TVM_TOPI_NN_SOFTMAX_H_
51 std::string tag =
"softmax_output") {
53 auto input_shape = x->shape;
54 auto ndim = input_shape.size();
58 TVM_FFI_ICHECK_LT(axis, ndim) <<
"axis parameter should be less than input dim";
64 tvm::ffi::Map<ffi::String, ffi::Any> attrs;
67 auto insert_reduce_index = [axis, ndim](
const ffi::Array<PrimVar>& indices,
69 ffi::Array<PrimExpr> eval_range;
71 for (
size_t i = 0; i < ndim; ++i) {
72 if (
static_cast<int>(i) == axis) {
73 eval_range.push_back(reduce_index);
75 eval_range.push_back(indices[arg_counter++]);
81 auto get_non_reduce_indices = [axis, ndim](
const ffi::Array<PrimVar>& indices) {
82 ffi::Array<PrimExpr> non_reduce_indices;
83 for (
size_t i = 0; i < ndim; ++i) {
84 if (
static_cast<int>(i) != axis) non_reduce_indices.push_back(indices[i]);
86 return non_reduce_indices;
89 auto _compute_max = [&](
const ffi::Array<PrimVar>& indices) {
90 auto eval_range = insert_reduce_index(indices, k1);
94 auto _compute_exp = [&](
const Tensor& max_elem,
const ffi::Array<PrimVar>& indices) {
95 auto non_reduce_indices = get_non_reduce_indices(indices);
99 auto _compute_expsum = [&](
const Tensor&
exp,
const ffi::Array<PrimVar>& indices) {
100 auto eval_range = insert_reduce_index(indices, k2);
105 const ffi::Array<PrimVar>& indices) {
106 auto non_reduce_indices = get_non_reduce_indices(indices);
107 return exp(indices) / expsum(non_reduce_indices);
112 return _compute_exp(max_elem, indices);
114 auto expsum =
tvm::te::compute(reduced_shape, [&](
const ffi::Array<PrimVar>& indices) {
115 return _compute_expsum(
exp, indices);
119 [&](
const ffi::Array<PrimVar>& indices) {
return _normalize(
exp, expsum, indices); }, name,
133 std::string tag =
"log_softmax_output") {
134 TVM_FFI_ICHECK_EQ(x->shape.size(), 2) <<
"Log softmax requires 2-D input";
149 [&](
PrimVar i,
PrimVar j) { return x(i, j) - max_elem(i) - tvm::prim::log(expsum(i)); }, name,
static IntImm Int32(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int32 constant.
Definition expr.h:181
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Checked scalar view over a VarNode.
Definition expr.h:742
Range container
Definition expr.h:1025
Iteration Variable, represents an iteration over an integer interval.
Definition iter_var.h:147
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:111
PrimExpr sum(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
sum of source expression over axis
PrimExpr exp(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1003
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 const char * kAxis
Definition tags.h:65
Tensor softmax(const Tensor &x, int axis=-1, std::string name="tensor", std::string tag="softmax_output")
Softmax activation.
Definition softmax.h:50
Tensor log_softmax(const Tensor &x, std::string name="tensor", std::string tag="log_softmax_output")
Log softmax activation.
Definition softmax.h:132
ffi::Array< PrimExpr > MakeReduceTargetShape(const std::vector< int > &real_axis, const Tensor &data, bool keepdims, bool atleast1d)
Calculate the target shape for a reduce op.
Definition reduction.h:98
Tensor exp(const Tensor &x, std::string name="T_" "exp", std::string tag=kElementWise)
Definition elemwise.h:50
PrimExpr MaxOp(PrimExpr source, ffi::Array< IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
Wrap tvm::max to ensure we get the correct overload.
Definition reduction.h:312
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
TVM_TIRX_DLL PrimExpr max(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
take maximum of two values
Operation node can generate one or multiple Tensors.
Reduction op constructors.