wuyii8941 opened a new issue, #19517:
URL: https://github.com/apache/tvm/issues/19517

   
   ## Expected behavior
   
   `dlight.gpu.Reduction()` should successfully schedule a 4D tensor reduction 
along any axis (0, 1, 2, or 3).
   
   ## Actual behavior
   
   The `Reduction` schedule rule crashes with `ScheduleError` in the `bind` 
primitive when applied to a 4D tensor reduction along axis 0, 1, or 2 (any 
non-last axis). Reduction along the last axis (3 or -1) works correctly. 
Lower-dimensional tensors (2D, 3D) work correctly for all axes.
   
   ## Reproduction
   
   ```python
   import tvm
   from tvm import relax
   import tvm.relax.op as R
   from tvm.s_tir import dlight
   
   bb = relax.BlockBuilder()
   x = relax.Var("x", relax.TensorStructInfo((4, 8, 16, 32), "float32"))
   with bb.function("main", [x]):
       with bb.dataflow():
           out = bb.emit(R.sum(x, axis=0, keepdims=True))
           gv = bb.emit_output(out)
       bb.emit_func_output(gv)
   mod = bb.get()
   
   pipeline = tvm.ir.transform.Sequential([
       relax.transform.LegalizeOps(),
       dlight.ApplyDefaultSchedule(
           dlight.gpu.Reduction(),
           dlight.gpu.Fallback(),
       ),
   ])
   
   with tvm.target.Target("cuda"):
       mod = pipeline(mod)  # ScheduleError here
   ```
   
   ## Error
   
   ```
   ScheduleError: An error occurred in the schedule primitive 'bind'.
   ```
   
   ## Trigger conditions
   
   | Shape | Axis | Result |
   |-------|------|--------|
   | (4, 8, 16, 32) | 0 | **CRASH** |
   | (4, 8, 16, 32) | 1 | **CRASH** |
   | (4, 8, 16, 32) | 2 | **CRASH** |
   | (4, 8, 16, 32) | 3 / -1 | OK |
   | (4, 8, 32) | any | OK |
   | (4, 32) | any | OK |
   
   Both `keepdims=True` and `keepdims=False` crash. `R.max`, `R.min`, `R.mean` 
also crash under the same conditions — not specific to `R.sum`.
   
   The `Fallback()` rule handles these cases correctly, so the issue is 
specifically in `Reduction`.
   
   ## Root cause
   
   In `python/tvm/s_tir/dlight/gpu/reduction.py`, after `_normalize` fuses the 
spatial and reduction loops, both `_sch_inner_reduction` and 
`_sch_inner_spatial` assume that the write-back block (after 
`reverse_compute_at`) has a specific loop structure. For 4D tensors with 
non-last-axis reduction, the extra spatial dimensions cause the 
`sch.get_loops(block)` unpacking to yield more loops than expected, producing 
an invalid configuration that the `bind` primitive rejects.
   
   ## Environment
   
   - TVM: main branch (also reproducible on v0.23.0)
   - Target: `cuda`
   - Python: 3.11
   - OS: Ubuntu Linux
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to