You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "Aharrypotter (via GitHub)" <gi...@apache.org> on 2026/06/24 08:26:55 UTC

[PR] [Relax][Frontend][TFLite] Support dynamic DYNAMIC_UPDATE_SLICE starts [tvm]

Aharrypotter opened a new pull request, #19881:
URL: https://github.com/apache/tvm/pull/19881

   ## Summary
   
   This PR adds Relax TFLite frontend support for runtime (dynamic) start indices
   in `STABLEHLO_DYNAMIC_UPDATE_SLICE`, addressing the `DYNAMIC_UPDATE_SLICE` item
   from #19412 section B.
   
   `_convert_stablehlo_dynamic_update_slice` (added in #19587) previously raised
   `OpNotImplemented` when the start-index scalars were runtime (non-constant)
   values, handling only compile-time-constant starts. Models that compute the
   update offset at runtime could therefore not be imported. This PR makes the
   dynamic-start path work, with StableHLO clamping semantics, without adding a new
   Relax op. The change is limited to this converter and its test.
   
   ## Design
   
   ### Dynamic start indices via scatter_nd
   
   The existing static path already lowers `STABLEHLO_DYNAMIC_UPDATE_SLICE` to
   `relax.op.scatter_nd`, building the scatter index grid at compile time with
   `numpy.indices`. `scatter_nd` accepts a general **runtime** `indices` tensor and
   returns the `data` (operand) shape unchanged, so the dynamic case needs no new
   op and introduces no symbolic dimensions — only the index grid is built
   in-graph instead of in NumPy.
   
   For runtime starts, the converter builds the index grid per axis `a` (rank is
   statically known from the operand/update shapes):
   
   - clamp the start to `[0, operand_dim - update_dim]` with `relax.op.maximum` /
     `relax.op.minimum` — StableHLO clamps out-of-range starts rather than erroring;
   - `idx = arange(update_dim) + clamped_start`;
   - reshape `idx` to broadcast on axis `a` and `broadcast_to` the update shape;
   - `expand_dims` a trailing index axis.
   
   `concat` over the axes produces an int64 index tensor of shape
   `(*update_shape, rank)`, which is fed to the same
   `relax.op.scatter_nd(operand, indices, update, "update")` call the static path
   uses.
   
   The static (constant-start) path is unchanged, including its compile-time
   out-of-bounds rejection.
   
   ## Operator Support
   
   | Operator | TFLite inputs | Relax lowering | Supported subset |
   |---|---|---|---|
   | `STABLEHLO_DYNAMIC_UPDATE_SLICE` | `operand`, `update`, N scalar `start` indices | `relax.op.scatter_nd` with a NumPy index grid (constant starts) or an in-graph `arange` + clamp index grid (runtime starts) | static operand/update shapes; constant or runtime start indices |
   
   ## Not Included
   
   - Dynamic (non-static) operand or update shapes — the index grid is built from
     the statically known update shape, so operand/update shapes must be static.
     Runtime *start indices* are supported; runtime *tensor shapes* are not.
   
   ## Tests
   
   The dynamic-start test compiles the imported module and runs it on the Relax VM,
   comparing the output against a NumPy reference; it includes an out-of-range start
   to exercise clamping. The static structural-equal and out-of-bounds tests are
   unchanged.
   
   | Test | Coverage |
   |---|---|
   | `test_stablehlo_dynamic_update_slice` | constant start indices, structural-equal (existing) |
   | `test_stablehlo_dynamic_update_slice_dynamic_starts` | runtime start indices, compile + run, including an out-of-range start that is clamped |
   | `test_stablehlo_dynamic_update_slice_out_of_bounds_unsupported` | constant-start path rejects out-of-bounds updates (existing) |
   
   Local validation:
   
   ```bash
   python -m ruff format --check \
     python/tvm/relax/frontend/tflite/tflite_frontend.py \
     tests/python/relax/test_frontend_tflite.py
   
   python -m ruff check \
     python/tvm/relax/frontend/tflite/tflite_frontend.py \
     tests/python/relax/test_frontend_tflite.py
   
   python -m pytest \
     tests/python/relax/test_frontend_tflite.py -k dynamic_update_slice -q
   
   python -m pytest \
     tests/python/relax/test_frontend_tflite.py -q
   ```
   
   Result:
   
   ```text
   ruff format --check: 2 files already formatted
   ruff check: All checks passed
   dynamic_update_slice tests: 3 passed, 555 deselected
   full TFLite pytest: 558 passed
   ```
   
   ## References
   
   - Issue #19412 section B: `DYNAMIC_UPDATE_SLICE`
   - PR #19587: introduced `STABLEHLO_DYNAMIC_UPDATE_SLICE` (constant starts) and
     multi-subgraph / StableHLO region support
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Frontend][TFLite] Support dynamic DYNAMIC_UPDATE_SLICE starts [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19881:
URL: https://github.com/apache/tvm/pull/19881#discussion_r3465643192


##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -3387,11 +3397,37 @@ def _convert_stablehlo_dynamic_update_slice(self, op):
             update_indices[axis] += start
         update_indices = np.moveaxis(update_indices, 0, -1)
 
-        operand = self.get_tensor_expr(operand_tensor)
-        update = self.get_tensor_expr(update_tensor)
         indices = self.bb.normalize(relax.const(update_indices, dtype="int64"))
         return self.bb.normalize(relax.op.scatter_nd(operand, indices, update, "update"))
 
+    def _build_dynamic_update_slice_indices(self, start_tensors, operand_shape, update_shape, rank):
+        """Build the scatter_nd index grid for runtime DYNAMIC_UPDATE_SLICE starts.
+
+        Returns an int64 tensor of shape ``(*update_shape, rank)`` where axis ``a``
+        holds ``arange(update_shape[a]) + clamp(start[a], 0, operand_dim - update_dim)``,
+        broadcast over the other axes (StableHLO clamps out-of-range starts).
+        """
+        axis_indices = []
+        for axis in range(rank):
+            start_expr = self.bb.normalize(
+                relax.op.astype(self.get_tensor_expr(start_tensors[axis]), "int64")
+            )
+            max_start = operand_shape[axis] - update_shape[axis]
+            start_expr = relax.op.maximum(start_expr, relax.const(0, "int64"))
+            start_expr = relax.op.minimum(start_expr, relax.const(max_start, "int64"))
+
+            base = relax.op.arange(update_shape[axis], dtype="int64")

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Calling `relax.op.arange` with only one positional argument (`update_shape[axis]`) and a keyword argument `dtype` can lead to type mismatches or signature errors in TVM Relax. Specifically, if `dtype` is `"int64"`, the implicitly converted `stop` argument (from Python `int`) may default to `"int32"`, causing a type mismatch with the `"int64"` `start` and `step` constants.
   
   To ensure robustness and prevent potential compilation or runtime errors, it is highly recommended to explicitly pass `start`, `stop`, and `step` as `relax.const` expressions with matching dtypes.
   
   ```suggestion
               base = relax.op.arange(
                   relax.const(0, "int64"),
                   relax.const(update_shape[axis], "int64"),
                   relax.const(1, "int64"),
               )
   ```



##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -3369,10 +3375,14 @@ def _convert_stablehlo_dynamic_update_slice(self, op):
                 "and start-index ranks to match"
             )
 
+        operand = self.get_tensor_expr(operand_tensor)
+        update = self.get_tensor_expr(update_tensor)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Since both `operand_shape` and `update_shape` are statically known, we should validate at compile time that the update slice dimensions do not exceed the operand dimensions (i.e., `update_shape[axis] <= operand_shape[axis]` for all axes). This prevents invalid models from being imported and provides a clear, early error message instead of failing later during compilation or runtime.
   
   ```suggestion
           for dim, size in zip(operand_shape, update_shape):
               if size > dim:
                   raise ValueError(
                       "STABLEHLO_DYNAMIC_UPDATE_SLICE update shape must be smaller than "
                       "or equal to operand shape for all dimensions."
                   )
   
           operand = self.get_tensor_expr(operand_tensor)
           update = self.get_tensor_expr(update_tensor)
   ```



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Frontend][TFLite] Support dynamic DYNAMIC_UPDATE_SLICE starts [tvm]

Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on PR #19881:
URL: https://github.com/apache/tvm/pull/19881#issuecomment-4796215237

   Thanks, agreed. I’ve updated this test to use tvm.ir.assert_structural_equal instead of an end-to-end execution check. I’ll also keep preferring structural checks for frontend tests whenever possible going forward.


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Frontend][TFLite] Support dynamic DYNAMIC_UPDATE_SLICE starts [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19881:
URL: https://github.com/apache/tvm/pull/19881


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org