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>
2017#include " topi/reduction.h"
2118#include " topi/transform.h"
2219
23- static_assert (TOPI_REDUCE_ATLEAST1D , " need to use legacy reduce behavior" );
24-
2520namespace nnvm {
2621namespace top {
2722using namespace tvm ;
2823using namespace nnvm ::compiler;
2924
25+
3026// reduce
3127DMLC_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
366362NNVM_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
0 commit comments