Lunderberg commented on code in PR #17219:
URL: https://github.com/apache/tvm/pull/17219#discussion_r1698515074


##########
src/tir/ir/buffer.cc:
##########
@@ -334,24 +334,38 @@ inline Array<PrimExpr> BufferOffset(const BufferNode* n, 
Array<PrimExpr> index,
   return offsets;
 }
 
-Buffer Buffer::GetFlattenedBuffer() const {
-  auto self = operator->();
-
+static void ValidateAxisSeparators(const Array<IntImm>& axis_separators, 
size_t buffer_dim) {
   // These checks ensure that all output axes contain at least one
   // input axis.
-  for (size_t i = 0; (i + 1) < self->axis_separators.size(); i++) {
-    auto sep = self->axis_separators[i]->value;
-    auto next_sep = self->axis_separators[i + 1]->value;
-    ICHECK_LT(sep, next_sep) << "Axis separators must be in strictly 
increasing order.";
-  }
-  if (self->axis_separators.size()) {
-    auto first_sep = self->axis_separators[0]->value;
-    ICHECK_GT(first_sep, 0) << "First axis separator must be strictly greater 
than 0, "
-                            << "so that first output axis contains at least 
one input axis";
-    auto last_sep = self->axis_separators[self->axis_separators.size() - 
1]->value;
-    ICHECK_LT(last_sep, self->shape.size())
-        << "Last output axis must contain at least one input axis.";
+  for (size_t i = 0; (i + 1) < axis_separators.size(); i++) {
+    auto sep = axis_separators[i]->value;
+    auto next_sep = axis_separators[i + 1]->value;
+    CHECK_LT(sep, next_sep) << "ValueError: "
+                            << "Axis separators must be in strictly increasing 
order, "
+                            << "but axis_separators[" << i << "] = " << sep
+                            << " is greater than or equal to axis_separators[" 
<< (i + 1)
+                            << "] = " << next_sep << ".";
+  }
+  if (axis_separators.size()) {
+    auto first_sep = axis_separators[0]->value;
+    CHECK_GT(first_sep, 0) << "ValueError: "
+                           << "First axis separator must be strictly greater 
than 0, "
+                           << "so that first output axis contains at least one 
input axis.  "
+                           << "However, the axis_separators[0] = " << 
first_sep;
+    auto last_sep = axis_separators[axis_separators.size() - 1]->value;
+    CHECK_LT(last_sep, buffer_dim)
+        << "ValueError: "
+        << "Last output axis must contain at least one input axis.  "
+        << "However, the axis_separators[" << (axis_separators.size() - 1) << 
"] = " << last_sep
+        << " does not leave any input axes between it and the buffer's 
dimensionality "
+        << buffer_dim;

Review Comment:
   I'm not sure the best way forward on it, as buffer views have always had a 
bit of this problem.  They have enough information to show/implement element 
access of a buffer, but some of the fields (e.g. `strides`) aren't used when 
lowering.  But at least for `strides`, the field can be set to a value that is 
consistent with the parent buffer.  For `axis_separators`, the view may not 
have sufficient information to determine the flattened shape.
   
   That said, being able to determine the physical dimensionality of a 
`tir::Buffer`, regardless of whether it is a view or the original, is useful.  
I like your idea of relaxing the `CHECK_GT` and `CHECK_LT` conditions to 
`CHECK_GE` and `CHECK_LE`, as it would let these cases be better expressed.  
That would allow a physical axis with extent 1 to be determined from an empty 
set of logical axes.
   
   (This change all came up from [this failing unit 
test](https://github.com/apache/tvm/pull/17219/files#diff-6c21ac3bae4a648708cb2b84c1e975dd9b80b25dada4fc964ed6a2e688b2913cR99).
  The `B_subregion0` buffer couldn't satisfy the constraints 
`len(view.axis_separators) < len(view.shape)` and `len(view.axis_separators) == 
len(backing_alloc_buf.axis_separators)` at the same time.)



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to