[tvm][arithmetic] Should we use the empty_value as base for ComputeReduce?


Is there any reasons we only use the empty_value when values is empty?

Should we do it like the following?

  template<typename Op>
  inline Expr ComputeReduce(const Array<Expr>& values, Expr empty_value) {
    if (values.size() == 0U) {
      return empty_value;
    Expr res = empty_value;
    for (size_t i = 0; i < values.size(); ++i) {
      res = ComputeExpr<Op>(res, values[i]);
    return res;

The reason why I am asking is sometime we might need to cast the reduced value to empty_value. For example, we can have int64_t empty_value and cast the product of Array<int32>values to int64 when needed.

@tqchen @yzhliu @wweic


@tqchen Could you comment? I think it is more natural to make it have the same semantic as foldleft or std::accumulate


The main reason is to avoid additional expressions in the case of sum. Because otherwise sum([x]) will results in 0 + x, which is not very ideal


sorry, what’s the problem for 0 + x?


Just to keep the symbolic expression minimum


but we can do ir::simplify, right?


That is true, however in simple cases as such, being able to eagerly simplify things help us to avoid symbolic expression when possible.