tensor-compiler / array-programming-benchmarks

MIT License
3 stars 0 forks source link

taco: bug when lowering a right inclusive iteration algebra #28

Closed weiya711 closed 3 years ago

weiya711 commented 3 years ago

When running a right inclusive iteration algebra for the power(a, b) with a 1-value compression user-defined function, the generated code is incorrect. The given power function is:

// Power op and algebra
struct Power {
  ir::Expr operator()(const std::vector<ir::Expr> &v) {
    if (v.size() == 1)
      return v[0];

    ir::Expr pow = ir::BinOp::make(v[0], v[1], "pow(", ", ", ")");
    for (size_t idx = 2; idx < v.size(); ++idx) {
      pow = ir::BinOp::make(pow, v[idx], "pow(", ", ", ")");
    }
    return pow;
  }
};

struct rightIncAlgebra {
  IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
    return regions[1];
  }
};

Func pow1Comp("power_1compression", Power(), leftIncAlgebra());

Tensor<int64_t> matrix = castToType<int64_t>("A", loadRandomTensor("A", {dim, dim}, sparsity, f));
Tensor<int64_t> matrix1 = castToType<int64_t>("B", loadRandomTensor("B", {dim, dim}, sparsity, f, 1 /* variant */));
Tensor<int64_t> result("result", {dim, dim}, f, 1);
IndexVar i("i"), j("j");
result(i, j) = pow1Comp(matrix(i, j), matrix1(i, j));

The generated code (below) does not properly emit the definition of jA.

169   for (int32_t i = 0; i < B1_dimension; i++) {
170     for (int32_t jB = B2_pos[i]; jB < B2_pos[(i + 1)]; jB++) {
171       result_vals[jresult] = pow(A_vals[jA], B_vals[jB]);
172       jresult++;
173     }
174   }
weiya711 commented 3 years ago

Not a bug, there is a difference in how the iteration algebra is defined.

struct rightIncAlgebra {
  IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
    return Union(Intersect(regions[0], regions[1]), regions[1]);
  }
};