24#ifndef TVM_TOPI_REDUCTION_H_
25#define TVM_TOPI_REDUCTION_H_
47 ffi::Array<PrimExpr> init,
Span span)>;
51 ffi::Array<PrimExpr>
exprs,
const ffi::Array<IterVar>& axis,
PrimExpr* condition)>;
65inline std::vector<int>
GetRealAxis(
int ndim,
const ffi::Optional<ffi::Array<int64_t>>& axis) {
67 if (!axis.has_value()) {
68 for (
int i = 0;
i < ndim; ++
i) {
92 std::string name =
"k" + std::to_string(
i);
100 const Tensor& data,
bool keepdims,
102 auto ndim = data->shape.size();
105 for (
size_t i = 0;
i < ndim; ++
i) {
114 for (
size_t i = 0;
i < ndim; ++
i) {
145 auto compute = [&](
const ffi::Array<PrimVar>& indices) {
151 for (
size_t i = 0;
i < data->shape.size(); ++
i) {
186 auto ndim = data->shape.size();
194 keepdims ? std::vector<int>() :
real_axis);
212 auto ndim = data->shape.size();
219 &data](
const ffi::Array<PrimVar>& indices) {
225 for (
size_t i = 0;
i < ndim; ++
i) {
260 std::function<ffi::Array<PrimExpr>(ffi::Array<PrimVar> lhs, ffi::Array<PrimVar> rhs)>;
263using FIdentity = std::function<ffi::Array<PrimExpr>(std::vector<PrimType>
types)>;
275 std::string name =
"reduce") {
278 ffi::Array<PrimVar> lhs, rhs;
280 std::vector<PrimType>
dtypes;
282 for (
size_t i = 0;
i <
exprs.size(); ++
i) {
295 auto cond = condition !=
nullptr ? *condition :
IntImm::Bool(
true);
298 ffi::Array<PrimExpr> outputs;
299 for (
size_t i = 0;
i <
exprs.size(); ++
i) {
309 return tvm::min(source, axis, init, span);
315 return tvm::max(source, axis, init, span);
321 return tvm::prod(source, axis, init, span);
338 bool keepdims =
false,
bool atleast1d =
false) {
340 if (data->dtype.code() == DLDataTypeCode::kDLBool) {
348 const auto&
ishape = data->shape;
350 int isize = data->shape.size();
354 <<
"Invalid collapse: input dimensionality smaller than output dimensionality.\ninput shape: "
355 << data->shape <<
"\nvs\noutput shape: " <<
target_shape;
396 bool keepdims =
false,
bool atleast1d =
false) {
415 bool keepdims =
false,
bool atleast1d =
false) {
434 bool keepdims =
false,
bool atleast1d =
false) {
453 bool keepdims =
false,
bool atleast1d =
false) {
459 auto fcombine = [=](ffi::Array<PrimVar> lhs, ffi::Array<PrimVar> rhs) {
460 ffi::Array<PrimExpr> result;
488 ffi::Array<PrimExpr> result;
513 bool keepdims =
false,
bool atleast1d =
false,
521 auto fcombine = [=](ffi::Array<PrimVar> lhs, ffi::Array<PrimVar> rhs) {
522 ffi::Array<PrimExpr> result;
550 ffi::Array<PrimExpr> result;
574 bool keepdims =
false,
bool atleast1d =
false,
594 bool keepdims =
false,
bool atleast1d =
false) {
602 auto fcombine = [](ffi::Array<PrimVar> lhs, ffi::Array<PrimVar> rhs) {
603 ffi::Array<PrimExpr> result;
605 result.reserve(lhs.size());
606 for (
size_t i = 0;
i < lhs.size(); ++
i) {
607 result.push_back(lhs[
i] + rhs[
i]);
612 ffi::Array<PrimExpr> result;
613 for (
size_t i = 0;
i <
types.size(); ++
i) {
Broadcast op constructions.
static IntImm Bool(bool value, Span span=Span())
Construct a scalar boolean constant.
Definition expr.h:519
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
Range container
Definition expr.h:610
Definition source_map.h:111
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to SelectNode.
Definition expr.h:525
Managed Tensor. The array is backed by reference counted blocks.
Definition tensor.h:49
Managed reference to CommReducerNode.
Definition operation.h:89
Managed reference to ReduceNode.
Definition operation.h:133
Checked scalar view over a VarNode.
Definition var.h:46
Utility functions for handling constants in TVM expressions.
Elementwise op constructions.
Tensor expression language DSL.
Definition extracted_task.h:33
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
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...
@ kCommReduce
Communicative reduction. Cannot be directly parallelized.
Definition var.h:104
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition op.h:1002
Tensor DoCommReduce(const Tensor &data, FReduce func, const ffi::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:140
Tensor collapse_sum(const Tensor &data, ffi::Array< PrimExpr > target_shape)
Definition reduction.h:347
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:99
std::function< ffi::Array< PrimExpr >(ffi::Array< PrimVar > lhs, ffi::Array< PrimVar > rhs)> FCombine
A combiner function for a reduction.
Definition reduction.h:260
ffi::Array< IterVar > MakeReduceAxes(const std::vector< int > &real_axis, const Tensor &data)
Enumerate the axes for a reduce op.
Definition reduction.h:89
FCommReduce MakeTupleSumReducer()
Create communitive reducer summing over tuples.
Definition reduction.h:601
std::vector< int > GetRealAxis(int ndim, const ffi::Optional< ffi::Array< int64_t > > &axis)
Convert a reduction axis which could be empty or have negative elements into a real axis with valid d...
Definition reduction.h:65
FCommReduce MakeCommReducer(FCombine fcombine, FIdentity fidentity, std::string name="reduce")
Create a commutative reducer for a reduction.
Definition reduction.h:274
std::function< ffi::Array< PrimExpr >(std::vector< PrimType > types)> FIdentity
An initializer function for a reduction.
Definition reduction.h:263
std::function< ffi::Array< PrimExpr >(ffi::Array< PrimExpr > exprs, const ffi::Array< IterVar > &axis, PrimExpr *condition)> FCommReduce
The operation to use for CommReduceIdx.
Definition reduction.h:51
FCommReduce MakeArgmaxReducer(bool select_last_index=false)
Definition reduction.h:519
PrimExpr MaxOp(PrimExpr source, ffi::Array< IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::max to ensure we get the correct overload.
Definition reduction.h:313
std::function< PrimExpr(PrimExpr source, const ffi::Array< IterVar > &axis, ffi::Array< PrimExpr > init, Span span)> FReduce
The operation to use for CommReduce.
Definition reduction.h:47
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:156
Tensor prod(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates product operation over given axis.
Definition reduction.h:593
Tensor any(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that computes the logical OR of elements over a given axis.
Definition reduction.h:414
Tensor argmin(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &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:512
constexpr auto kCommReduceIdx
Definition tags.h:35
PrimExpr MinOp(PrimExpr source, ffi::Array< IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::min to ensure we get the correct overload.
Definition reduction.h:307
FCommReduce MakeArgminReducer(bool select_last_index=false)
Definition reduction.h:457
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:154
PrimExpr ProdOp(PrimExpr source, ffi::Array< IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
Wrap tvm::prod to ensure we get the correct overload.
Definition reduction.h:319
Tensor CommReduceIdx(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, FCommReduce func, bool keepdims, bool atleast1d)
Create an index reduction operation.
Definition reduction.h:210
Tensor CommReduce(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, FReduce func, bool keepdims, bool atleast1d)
Create a reduction operation.
Definition reduction.h:184
Tensor argmax(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &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:573
Tensor all(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that computes the logical AND of elements over a given axis.
Definition reduction.h:395
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr max_value(PrimType dtype, Span span=Span())
PrimExpr any(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
logical Or of source expression over axis
PrimExpr min_value(PrimType dtype, Span span=Span())
PrimExpr all(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
logical And of source expression over axis
PrimExpr prod(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
product of source expression over axis
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
PrimExpr sum(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::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.