maheshambule commented on a change in pull request #5082: [Relay, Topi] [TF,
MXNet] Unravel Index operator
URL: https://github.com/apache/incubator-tvm/pull/5082#discussion_r394577194
##########
File path: topi/include/topi/transform.h
##########
@@ -232,6 +232,52 @@ inline Tensor reshape(const Tensor& x,
}
}
+/*!
+ * \brief Converts a flat index or array of flat indices into a tuple of
coordinate arrays
+ *
+ * \param x The input tensor having indices.
+ * \param shape The shape tensor
+ * \param name The name of the operation
+ * \param tag The tag to mark the operation
+ *
+ * \return A Tensor of coordinate arrays.
+ */
+
+inline Tensor unravel_index(const Tensor& x, const Tensor& shape, std::string
name = "T_unravel",
+ std::string tag = kInjective) {
+ auto x_shape = x->shape;
+ auto shape_shape = shape->shape;
+
+ Array<PrimExpr> oshape;
+ oshape.push_back(shape_shape[0]);
+ if (x_shape.size() != 0) {
+ oshape.push_back(x_shape[0]);
+ }
+
+ return compute(oshape,
+ [&](const Array<Var>& indices) {
+ auto i = indices[0];
+ std::vector<PrimExpr> indices_divs;
+ PrimExpr ret = 0;
+ PrimExpr cur_val = 0;
+ PrimExpr index_val = 0;
+
+ if (x_shape.size() != 0) {
+ index_val = x[indices[1]];
+ } else {
+ index_val = x();
+ }
+ indices_divs.push_back(index_val);
+ for (int v = GetConstInt(shape_shape[0]) - 1; v >= 0; --v) {
+ ret = tvm::if_then_else(i == v,
indexmod(indices_divs.back(), shape[v]), ret);
+ cur_val = indexdiv(indices_divs.back(), shape[v]);
+ indices_divs.push_back(cur_val);
Review comment:
The function in this file returns all the coordinates for a given index. In
compute definition we just want a coordinate for the current compute index and
not for all of them. I was facing issue while extracting the current coordinate
because compute index which is a Var can not be directly used to extract Expr
from an array of Exprs. I had to use if_then_else construct for that. Please
let me know if I am missing something here and if there is an easier way to
achieve this. I could have modified the existing function to meet my purposes
for example pass in the coordinate index I want to extract and return just that
coordinate. Please let me know if I should implement this.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services