Skip to content

Commit a9bfea7

Browse files
tqchenWei Chen
authored andcommitted
[TOPI] Fix atlest1d for reduce and squeeze (apache#2147)
1 parent 681f780 commit a9bfea7

13 files changed

Lines changed: 125 additions & 97 deletions

File tree

nnvm/include/nnvm/compiler/util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ inline tvm::Array<tvm::Expr> ShapeToArray(TShape shape) {
2828
return result;
2929
}
3030

31+
/*
32+
* \brief Helper function to convert TShape to TVM array. Useful for
33+
* passing data from NNVM param structures to TOPI ops.
34+
*
35+
* \param shape The shape to convert
36+
*
37+
* \return An Array of Expr, where each element is a constant int32
38+
*/
39+
inline tvm::Array<tvm::Integer> ShapeToIntArray(TShape shape) {
40+
return tvm::Array<tvm::Integer>(ShapeToArray(shape).node_);
41+
}
3142
} // namespace compiler
3243
} // namespace nnvm
3344
#endif // NNVM_COMPILER_UTIL_H_

nnvm/src/top/tensor/reduce.cc

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
* \file reduce.cc
44
* \brief reduce operator.
55
*/
6-
// Enforce TOPI to use old behavior that reduces to at least 1d
7-
#define TOPI_REDUCE_ATLEAST1D 1
8-
96
#include <nnvm/op.h>
107
#include <nnvm/node.h>
118
#include <nnvm/op_attr_types.h>
@@ -20,13 +17,12 @@
2017
#include "topi/reduction.h"
2118
#include "topi/transform.h"
2219

23-
static_assert(TOPI_REDUCE_ATLEAST1D, "need to use legacy reduce behavior");
24-
2520
namespace nnvm {
2621
namespace top {
2722
using namespace tvm;
2823
using namespace nnvm::compiler;
2924

25+
3026
// reduce
3127
DMLC_REGISTER_PARAMETER(ReduceParam);
3228

@@ -168,9 +164,9 @@ Example::
168164
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
169165
param.axis, param.exclude);
170166
if (!r_axes.ndim()) return Array<Tensor> { topi::identity(inputs[0]) };
171-
auto axis = ShapeToArray(r_axes);
167+
auto axis = ShapeToIntArray(r_axes);
172168
return Array<Tensor>{
173-
topi::sum(inputs[0], axis, param.keepdims) };
169+
topi::sum(inputs[0], axis, param.keepdims, true) };
174170
})
175171
.set_attr<FGradient>(
176172
"FGradient", [](const NodePtr& n,
@@ -202,9 +198,9 @@ NNVM_REGISTER_REDUCE_OP(max)
202198
const ReduceParam& param = nnvm::get<ReduceParam>(attrs.parsed);
203199
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
204200
param.axis, param.exclude);
205-
auto axis = ShapeToArray(r_axes);
201+
auto axis = ShapeToIntArray(r_axes);
206202
return Array<Tensor>{
207-
topi::max(inputs[0], axis, param.keepdims) };
203+
topi::max(inputs[0], axis, param.keepdims, true) };
208204
})
209205
.set_attr<FGradient>(
210206
"FGradient", [](const NodePtr& n,
@@ -235,9 +231,9 @@ NNVM_REGISTER_REDUCE_OP(min)
235231
const ReduceParam& param = nnvm::get<ReduceParam>(attrs.parsed);
236232
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
237233
param.axis, param.exclude);
238-
auto axis = ShapeToArray(r_axes);
234+
auto axis = ShapeToIntArray(r_axes);
239235
return Array<Tensor>{
240-
topi::min(inputs[0], axis, param.keepdims) };
236+
topi::min(inputs[0], axis, param.keepdims, true) };
241237
})
242238
.set_attr<FGradient>(
243239
"FGradient", [](const NodePtr& n,
@@ -299,8 +295,8 @@ values over a given axis.
299295
const ReduceParam& param = nnvm::get<ReduceParam>(attrs.parsed);
300296
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
301297
param.axis, param.exclude);
302-
auto axis = ShapeToArray(r_axes);
303-
Tensor out = topi::argmax(inputs[0], axis, param.keepdims);
298+
auto axis = ShapeToIntArray(r_axes);
299+
Tensor out = topi::argmax(inputs[0], axis, param.keepdims, true);
304300
if (param.dtype == kFloat32) out = topi::cast(out, out_info[0]->dtype);
305301
return Array<Tensor>{out};
306302
});
@@ -322,8 +318,8 @@ values over a given axis.
322318
const ReduceParam& param = nnvm::get<ReduceParam>(attrs.parsed);
323319
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
324320
param.axis, param.exclude);
325-
auto axis = ShapeToArray(r_axes);
326-
Tensor out = topi::argmin(inputs[0], axis, param.keepdims);
321+
auto axis = ShapeToIntArray(r_axes);
322+
Tensor out = topi::argmin(inputs[0], axis, param.keepdims, true);
327323
if (param.dtype == kFloat32) out = topi::cast(out, out_info[0]->dtype);
328324
return Array<Tensor>{out};
329325
});
@@ -352,15 +348,15 @@ Example::
352348
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
353349
param.axis, param.exclude);
354350
if (!r_axes.ndim()) return Array<Tensor> { topi::identity(inputs[0]) };
355-
auto axis = ShapeToArray(r_axes);
351+
auto axis = ShapeToIntArray(r_axes);
356352

357353
Expr count = make_const(inputs[0]->dtype, 1);
358354
for (auto& i : r_axes) {
359355
count *= inputs[0]->shape[i];
360356
}
361357

362358
return Array<Tensor>{
363-
topi::divide(topi::sum(inputs[0], axis, param.keepdims), count) };
359+
topi::divide(topi::sum(inputs[0], axis, param.keepdims, true), count) };
364360
});
365361

366362
NNVM_REGISTER_REDUCE_OP(prod)
@@ -387,9 +383,9 @@ Example::
387383
TShape r_axes = GetReduceAxes(inputs[0]->shape.size(),
388384
param.axis, param.exclude);
389385
if (!r_axes.ndim()) return Array<Tensor> { topi::identity(inputs[0]) };
390-
auto axis = ShapeToArray(r_axes);
386+
auto axis = ShapeToIntArray(r_axes);
391387
return Array<Tensor>{
392-
topi::prod(inputs[0], axis, param.keepdims) };
388+
topi::prod(inputs[0], axis, param.keepdims, true) };
393389
});
394390

395391

nnvm/src/top/tensor/transform.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,8 @@ Examples::
756756
const Array<Tensor>& inputs,
757757
const Array<Tensor>& out_info) {
758758
const SqueezeParam& param = nnvm::get<SqueezeParam>(attrs.parsed);
759-
auto axis = ShapeToArray(param.axis);
760-
return Array<Tensor>{ topi::squeeze(inputs[0], axis) };
759+
auto axis = ShapeToIntArray(param.axis);
760+
return Array<Tensor>{ topi::squeeze(inputs[0], axis, true) };
761761
})
762762
.set_attr<FGradient>(
763763
"FGradient", [](const NodePtr& n,

topi/include/topi/detail/fuse.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,16 @@ using namespace tvm;
1414

1515
/*!
1616
* \brief Fuse all of the given args
17-
*
17+
*
1818
* \param stage The stage in which to apply the fuse
1919
* \param args The iteration variables to be fused
2020
*
2121
* \return The fused iteration variable
2222
*/
2323
inline IterVar Fuse(Stage stage, const Array<IterVar>& args) {
24-
CHECK_GE(args.size(), 1) << "Fuse requires at least 1 arg";
25-
26-
auto fused = args[0];
27-
for (size_t i = 1; i < args.size(); ++i) {
28-
IterVar out;
29-
stage.fuse(fused, args[i], &out);
30-
fused = out;
31-
}
32-
return fused;
24+
IterVar res;
25+
stage.fuse(args, &res);
26+
return res;
3327
}
3428

3529
} // namespace detail

topi/include/topi/nn/l2_normalize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace tvm;
2727
*/
2828
inline Tensor l2_normalize(const Tensor& data,
2929
float eps,
30-
const Array<Expr>& axis,
30+
const Array<Integer>& axis,
3131
std::string name = "tensor",
3232
std::string tag = "l2_normalize") {
3333
CHECK_EQ(data->shape.size(), 4) << "L2 normalization requires 4-D input";

topi/include/topi/nn/softmax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ inline Tensor softmax(const Tensor &x,
4040

4141
auto k1 = tvm::reduce_axis(Range(0, input_shape[axis]), "k1");
4242
auto k2 = tvm::reduce_axis(Range(0, input_shape[axis]), "k2");
43-
auto reduced_shape = MakeReduceTargetShape({axis}, x, false);
43+
auto reduced_shape = MakeReduceTargetShape({axis}, x, false, false);
4444

4545
auto insert_reduce_index = [axis, ndim](const Array<Var> &indices,
4646
const IterVar &reduce_index) {

0 commit comments

Comments
 (0)