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


#1

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) {
      CHECK(empty_value.defined());
      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


#2

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


#3

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


#4

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


#5

Just to keep the symbolic expression minimum


#6

but we can do ir::simplify, right?


#7

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