24 #ifndef TVM_TOPI_REDUCTION_H_
25 #define TVM_TOPI_REDUCTION_H_
47 Array<PrimExpr> init,
Span span)>;
50 using FCommReduce = std::function<Array<PrimExpr>(Array<PrimExpr> exprs,
const Array<IterVar>& axis,
65 inline std::vector<int>
GetRealAxis(
int ndim,
const Optional<Array<Integer>>& axis) {
66 std::vector<int> real_axis;
67 if (!axis.has_value()) {
68 for (
int i = 0; i < ndim; ++i) {
69 real_axis.push_back(i);
73 for (
auto elem : axis.value()) {
74 int64_t val = elem->value;
78 ICHECK_LT(val, ndim) <<
" exceeds the maximum dimension " << ndim;
80 real_axis.push_back(
static_cast<int>(val));
82 std::sort(real_axis.begin(), real_axis.end());
83 real_axis.resize(std::unique(real_axis.begin(), real_axis.end()) - real_axis.begin());
90 Array<IterVar> reduce_axes;
91 for (
auto i : real_axis) {
92 std::string name =
"k" + std::to_string(i);
100 bool keepdims,
bool atleast1d) {
101 auto ndim = data->shape.size();
102 Array<PrimExpr> target_shape;
104 for (
size_t i = 0; i < ndim; ++i) {
105 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
107 target_shape.push_back(1);
109 target_shape.push_back(data->shape[i]);
113 for (
size_t i = 0; i < ndim; ++i) {
114 if (std::find(real_axis.begin(), real_axis.end(), i) == real_axis.end()) {
116 target_shape.push_back(data->shape[i]);
120 if (target_shape.size() == 0 && atleast1d) {
121 target_shape.push_back(1);
140 const std::vector<int>& reduce_axes,
141 const std::vector<int>& squeeze_axes,
Span span =
Span()) {
143 auto compute = [&](
const Array<Var>& indices) {
144 Array<PrimExpr> eval_range;
145 Array<Var> eval_indices;
149 for (
size_t i = 0; i < data->shape.size(); ++i) {
150 bool squeeze_i = std::find(squeeze_axes.begin(), squeeze_axes.end(), i) != squeeze_axes.end();
151 if (std::find(reduce_axes.begin(), reduce_axes.end(), i) != reduce_axes.end()) {
153 eval_range.push_back(r_axes[red_counter]);
154 eval_indices.push_back(r_axes[red_counter]->
var);
156 arg_counter += !squeeze_i;
159 eval_range.push_back(indices[arg_counter]);
163 return func(data(eval_range), r_axes, {}, span);
183 bool keepdims,
bool atleast1d) {
184 auto ndim = data->shape.size();
185 ICHECK_NE(ndim, 0) <<
"Cannot reduce a 0 dim Tensor";
186 auto real_axis =
GetRealAxis(
static_cast<int>(ndim), axis);
188 return DoCommReduce(data, func, target_shape, real_axis,
189 keepdims ? std::vector<int>() : real_axis);
207 auto ndim = data->shape.size();
208 ICHECK_NE(ndim, 0) <<
"Cannot reduce a 0 dim Tensor";
209 auto real_axis =
GetRealAxis(
static_cast<int>(ndim), axis);
213 auto compute = [ndim, keepdims, &real_axis, &reduce_axes, &func,
214 &data](
const Array<Var>& indices) {
215 Array<PrimExpr> eval_range;
216 Array<PrimExpr> eval_indices;
220 for (
size_t i = 0; i < ndim; ++i) {
221 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
223 eval_range.push_back(reduce_axes[red_counter]);
224 eval_indices.push_back(reduce_axes[red_counter]->
var);
228 eval_range.push_back(indices[arg_counter]);
231 eval_range.push_back(indices[i]);
236 Array<PrimExpr> ravel_shape;
237 for (
auto i : real_axis) {
238 ravel_shape.push_back(data->shape[i]);
240 auto idx = detail::RavelIndex(eval_indices, ravel_shape);
241 return func({idx, data(eval_range)}, reduce_axes,
nullptr);
246 auto temp_idx = temp_idx_val[0];
247 auto temp_val = temp_idx_val[1];
249 target_shape, [&temp_idx](
const Array<Var>& indices) {
return temp_idx(indices); },
254 using FCombine = std::function<Array<PrimExpr>(Array<Var> lhs, Array<Var> rhs)>;
257 using FIdentity = std::function<Array<PrimExpr>(std::vector<DataType> types)>;
269 std::string name =
"reduce") {
270 return [fcombine, fidentity, name](Array<PrimExpr> exprs,
const Array<IterVar>& axis,
273 std::vector<DataType> dtypes;
275 for (
size_t i = 0; i < exprs.size(); ++i) {
276 auto dtype = exprs[i].dtype();
277 dtypes.push_back(dtype);
278 lhs.push_back(
var(name +
"_lhs_" + std::to_string(i), dtype));
279 rhs.push_back(
var(name +
"_rhs_" + std::to_string(i), dtype));
282 auto result = fcombine(lhs, rhs);
283 auto id_elem = fidentity(dtypes);
287 Array<PrimExpr> outputs;
288 for (
size_t i = 0; i < exprs.size(); ++i) {
289 outputs.push_back(
tvm::tir::Reduce(combiner, exprs, axis, cond,
static_cast<int>(i), {}));
298 return tvm::min(source, axis, init, span);
304 return tvm::max(source, axis, init, span);
310 return tvm::prod(source, axis, init, span);
326 inline Tensor sum(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
327 bool atleast1d =
false) {
328 if (data->dtype.is_bool()) {
336 const auto& ishape = data->shape;
337 const auto& oshape = target_shape;
338 int isize = data->shape.size();
339 int osize = target_shape.size();
341 ICHECK_GE(isize, osize)
342 <<
"Invalid collapse: input dimensionality smaller than output dimensionality.\ninput shape: "
343 << data->shape <<
"\nvs\noutput shape: " << target_shape;
345 std::vector<int> reduce_axes;
346 std::vector<int> squeeze_axes;
349 for (
int i_ax = isize - 1, o_ax = osize - 1; i_ax >= 0; --i_ax) {
350 if (o_ax >= 0 && topi::detail::EqualCheck(ishape[i_ax], oshape[o_ax])) {
354 reduce_axes.push_back(i_ax);
356 squeeze_axes.push_back(i_ax);
357 }
else if (topi::detail::EqualCheck(one, oshape[o_ax])) {
364 std::reverse(reduce_axes.begin(), reduce_axes.end());
365 std::reverse(squeeze_axes.begin(), squeeze_axes.end());
383 inline Tensor all(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
384 bool atleast1d =
false) {
402 inline Tensor any(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
403 bool atleast1d =
false) {
421 inline Tensor min(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
422 bool atleast1d =
false) {
440 inline Tensor max(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
441 bool atleast1d =
false) {
447 auto fcombine = [=](Array<Var> lhs, Array<Var> rhs) {
448 Array<PrimExpr> result;
457 auto is_smaller = lhs_val < rhs_val;
458 auto is_same = lhs_val == rhs_val;
464 if (select_last_index) {
465 proper_index = lhs_idx > rhs_idx;
467 proper_index = lhs_idx < rhs_idx;
470 PrimExpr update_index = is_smaller || (is_same && proper_index);
475 auto fidentity = [&](std::vector<DataType> types) {
476 Array<PrimExpr> result;
501 bool keepdims =
false,
bool atleast1d =
false,
502 bool select_last_index =
false) {
504 return CommReduceIdx(data, axis, reducer, keepdims, atleast1d);
509 auto fcombine = [=](Array<Var> lhs, Array<Var> rhs) {
510 Array<PrimExpr> result;
519 auto is_bigger = lhs_val > rhs_val;
520 auto is_same = lhs_val == rhs_val;
526 if (select_last_index) {
527 proper_index = lhs_idx > rhs_idx;
529 proper_index = lhs_idx < rhs_idx;
532 PrimExpr update_index = is_bigger || (is_same && proper_index);
537 auto fidentity = [&](std::vector<DataType> types) {
538 Array<PrimExpr> result;
562 bool keepdims =
false,
bool atleast1d =
false,
563 bool select_last_index =
false) {
565 return CommReduceIdx(data, axis, reducer, keepdims, atleast1d);
581 inline Tensor prod(
const Tensor& data,
const Optional<Array<Integer>>& axis,
bool keepdims =
false,
582 bool atleast1d =
false) {
590 auto fcombine = [](Array<Var> lhs, Array<Var> rhs) {
591 Array<PrimExpr> result;
592 ICHECK_EQ(lhs.size(), rhs.size());
593 result.reserve(lhs.size());
594 for (
size_t i = 0; i < lhs.size(); ++i) {
595 result.push_back(lhs[i] + rhs[i]);
599 auto fidentity = [](std::vector<DataType> types) {
600 Array<PrimExpr> result;
601 for (
size_t i = 0; i < types.size(); ++i) {
Broadcast op constructions.
Reference to PrimExprNode.
Definition: expr.h:129
Range container
Definition: expr.h:698
Definition: source_map.h:113
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:100
Managed reference to CommReducerNode.
Definition: expr.h:854
Managed reference to ReduceNode.
Definition: expr.h:900
Managed reference to SelectNode.
Definition: expr.h:523
Utility functions for handling constants in TVM expressions.
Elementwise op constructions.
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.
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Tensor compute(Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", Map< String, ffi::Any > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:980
PrimExpr const_true(int lanes=1, Span span=Span())
Make a constant true expression.
Definition: op.h:804
Tensor collapse_sum(const Tensor &data, Array< PrimExpr > target_shape)
Definition: reduction.h:335
std::function< Array< PrimExpr >(Array< PrimExpr > exprs, const Array< IterVar > &axis, PrimExpr *condition)> FCommReduce
The operation to use for CommReduceIdx.
Definition: reduction.h:51
FCommReduce MakeTupleSumReducer()
Create communitive reducer summing over tuples.
Definition: reduction.h:589
Tensor sum(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that sums array elements over a given axis.
Definition: reduction.h:326
FCommReduce MakeCommReducer(FCombine fcombine, FIdentity fidentity, std::string name="reduce")
Create a commutative reducer for a reduction.
Definition: reduction.h:268
Tensor any(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that computes the logical OR of elements over a given axis.
Definition: reduction.h:402
PrimExpr MaxOp(PrimExpr source, Array< IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::max to ensure we get the correct overload.
Definition: reduction.h:302
FCommReduce MakeArgmaxReducer(bool select_last_index=false)
Definition: reduction.h:507
Tensor argmin(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false, bool select_last_index=false)
Creates an operation that finds the indices of the minimum values over a given axis.
Definition: reduction.h:500
Tensor CommReduce(const Tensor &data, const Optional< Array< Integer >> &axis, FReduce func, bool keepdims, bool atleast1d)
Create a reduction operation.
Definition: reduction.h:182
std::function< Array< PrimExpr >(Array< Var > lhs, Array< Var > rhs)> FCombine
A combiner function for a reduction.
Definition: reduction.h:254
constexpr auto kCommReduce
Definition: tags.h:34
std::function< Array< PrimExpr >(std::vector< DataType > types)> FIdentity
An initializer function for a reduction.
Definition: reduction.h:257
Tensor all(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that computes the logical AND of elements over a given axis.
Definition: reduction.h:383
Array< IterVar > MakeReduceAxes(const std::vector< int > &real_axis, const Tensor &data)
Enumerate the axes for a reduce op.
Definition: reduction.h:89
constexpr auto kCommReduceIdx
Definition: tags.h:35
FCommReduce MakeArgminReducer(bool select_last_index=false)
Definition: reduction.h:445
Tensor identity(const Tensor &x, std::string name="T_identity", std::string tag=kElementWise)
Creates an operation that returns identity of a given tensor.
Definition: elemwise.h:152
Tensor CommReduceIdx(const Tensor &data, const Optional< Array< Integer >> &axis, FCommReduce func, bool keepdims, bool atleast1d)
Create an index reduction operation.
Definition: reduction.h:205
Tensor DoCommReduce(const Tensor &data, FReduce func, const Array< PrimExpr > &target_shape, const std::vector< int > &reduce_axes, const std::vector< int > &squeeze_axes, Span span=Span())
Create a reduction operation.
Definition: reduction.h:139
Tensor argmax(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false, bool select_last_index=false)
Creates an operation that finds the indices of the maximum values over a given axis.
Definition: reduction.h:561
Tensor min(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that finds the minimum of elements over a given axis.
Definition: reduction.h:421
Tensor max(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that finds the maximum of elements over a given axis.
Definition: reduction.h:440
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:99
PrimExpr MinOp(PrimExpr source, Array< IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::min to ensure we get the correct overload.
Definition: reduction.h:296
std::function< PrimExpr(PrimExpr source, const Array< IterVar > &axis, Array< PrimExpr > init, Span span)> FReduce
The operation to use for CommReduce.
Definition: reduction.h:47
std::vector< int > GetRealAxis(int ndim, const Optional< Array< Integer >> &axis)
Convert a reduction axis which could be empty or have negative elements into a real axis with valid d...
Definition: reduction.h:65
PrimExpr ProdOp(PrimExpr source, Array< IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::prod to ensure we get the correct overload.
Definition: reduction.h:308
Tensor prod(const Tensor &data, const Optional< Array< Integer >> &axis, bool keepdims=false, bool atleast1d=false)
Creates product operation over given axis.
Definition: reduction.h:581
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr prod(PrimExpr source, Array< tir::IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
product of source expression over axis
PrimExpr min_value(const DataType &dtype, Span span=Span())
PrimExpr max_value(const DataType &dtype, Span span=Span())
PrimExpr any(PrimExpr source, Array< tir::IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
logical Or of source expression over axis
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
PrimExpr all(PrimExpr source, Array< tir::IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
logical And of source expression over axis
PrimExpr sum(PrimExpr source, Array< tir::IterVar > axis, Array< PrimExpr > init={}, Span span=Span())
sum of source expression over axis
Operation node can generate one or multiple Tensors.
Index ravel and unraval operations.