This is an automated email from the ASF dual-hosted git repository.

tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new f0267a0a3e [Fix][Relax] Skip vector weights in ReorderTakeAfterMatmul 
(#20815)
f0267a0a3e is described below

commit f0267a0a3e9e2d8d6841a0e46e7f275a638e433c
Author: Chen Yufan <[email protected]>
AuthorDate: Sun Nov 15 14:06:41 2026 +0800

    [Fix][Relax] Skip vector weights in ReorderTakeAfterMatmul (#20815)
    
    With rank-one weights, `take(..., axis=0)` selects elements on the
    MatMul reduction axis. Moving it after MatMul raises on vector
    activations and changes results on matrix activations, even when the
    output shape stays the same.
    
    Require weight rank >= 2 in the simple column-selection rewrite. Leave
    vector weights unchanged. Add two regressions and a positive control
    showing that vector activations with matrix weights still rewrite.
    
    Fixes #20595.
    
    Validation against main `30fd3017fd`, Python 3.11, LLVM 21.1.8 and
    source-built pinned TIRx/FFI dependencies:
    
    - `test_transform_reorder_take_after_matmul.py`,
    `test_transform_expand_matmul_of_sum.py`, and
    `test_op_linear_algebra.py`: 26 passed.
    - CPU C-backend VM execution against NumPy: 20/20 transformed cases
    match values, shapes and dtypes. Covers vector/matrix/batched
    activations, symbolic batch dimensions, and fast/clip/wrap take modes.
    All 20 original modules also match the reference.
    - File-scoped pre-commit checks passed.
    
    Prepared with OpenAI Codex assistance. The build and validation above
    were executed locally.
    
    Includes the shared packaging fix in #20980: the pinned TIRx source now
    carries the final `0.1.0` tag, which the old CMake/Python bounds
    rejected on macOS and Windows. The pass/operator implementation is
    unchanged by this CI follow-up. Fresh CI results are pending.
---
 CMakeLists.txt                                     |  4 +-
 pyproject.toml                                     |  4 +-
 src/relax/transform/reorder_take_after_matmul.cc   |  3 +-
 .../test_transform_reorder_take_after_matmul.py    | 60 ++++++++++++++++++++++
 4 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f146b6d7bf..0f5ebd5c73 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -158,8 +158,8 @@ if(TVM_USE_INSTALLED_TIRX)
       OUTPUT_VARIABLE TVM_TIRX_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE
       COMMAND_ERROR_IS_FATAL ANY)
   endif()
-  if(NOT TVM_TIRX_VERSION MATCHES "^0\\.1\\.0(-rc[23]|rc[23])$")
-    message(FATAL_ERROR "TVM requires apache-tvm-tirx 0.1.0rc2 or 0.1.0rc3")
+  if(NOT TVM_TIRX_VERSION MATCHES "^0\\.1\\.0(-rc[23]|rc[23])?$")
+    message(FATAL_ERROR "TVM requires apache-tvm-tirx 0.1.0rc2, 0.1.0rc3 or 
0.1.0")
   endif()
 else()
   # Keep source-built core libraries beside the parent libraries.
diff --git a/pyproject.toml b/pyproject.toml
index 1079ff18fe..ed82eb4634 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -20,7 +20,7 @@
 requires = [
   "scikit-build-core>=0.11",
   "setuptools-scm>=8",
-  "apache-tvm-tirx>=0.1.0rc2,<=0.1.0rc3",
+  "apache-tvm-tirx>=0.1.0rc2,<=0.1.0",
 ]
 build-backend = "scikit_build_core.build"
 
@@ -49,7 +49,7 @@ classifiers = [
   "Topic :: Scientific/Engineering :: Artificial Intelligence",
   "Topic :: Software Development :: Libraries :: Python Modules",
 ]
-dependencies = ["apache-tvm-tirx>=0.1.0rc2,<=0.1.0rc3"]
+dependencies = ["apache-tvm-tirx>=0.1.0rc2,<=0.1.0"]
 
 [project.scripts]
 tvm-config = "tvm.config:__main__"
diff --git a/src/relax/transform/reorder_take_after_matmul.cc 
b/src/relax/transform/reorder_take_after_matmul.cc
index 447d0e24a3..2326254bcb 100644
--- a/src/relax/transform/reorder_take_after_matmul.cc
+++ b/src/relax/transform/reorder_take_after_matmul.cc
@@ -83,7 +83,8 @@ std::tuple<DFPattern, ffi::TypedFunction<Expr(Expr, 
ffi::Map<DFPattern, Expr>)>>
         weights_ty->IsUnknownNdim())
       return expr;
 
-    if (indices_ty->ndim == 1 && axis + 1 == weights_ty->ndim) {
+    // The sole axis of vector weights is a reduction axis.
+    if (weights_ty->ndim >= 2 && indices_ty->ndim == 1 && axis + 1 == 
weights_ty->ndim) {
       // Simpler case.  The activations may have batch dimensions, but
       // the weights do not.
 
diff --git a/tests/python/relax/test_transform_reorder_take_after_matmul.py 
b/tests/python/relax/test_transform_reorder_take_after_matmul.py
index fccc8138ab..1e80ef2d80 100644
--- a/tests/python/relax/test_transform_reorder_take_after_matmul.py
+++ b/tests/python/relax/test_transform_reorder_take_after_matmul.py
@@ -74,6 +74,66 @@ class TestSimple(Base):
             return out
 
 
+class TestVectorActivations(Base):
+    @I.ir_module
+    class Before:
+        @R.function
+        def main(
+            x: R.Tensor([16], "float32"),
+            weight_table: R.Tensor([16, 64], "float32"),
+            routing_table: R.Tensor([32], "int64"),
+        ) -> R.Tensor([32], "float32"):
+            weight = R.take(weight_table, routing_table, axis=1, mode="clip")
+            out = R.matmul(x, weight)
+            return out
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(
+            x: R.Tensor([16], "float32"),
+            weight_table: R.Tensor([16, 64], "float32"),
+            routing_table: R.Tensor([32], "int64"),
+        ) -> R.Tensor([32], "float32"):
+            out_table = R.matmul(x, weight_table)
+            out = R.take(out_table, routing_table, axis=0, mode="clip")
+            return out
+
+
+class TestNoReorderVectorWeights(Base):
+    @I.ir_module
+    class Before:
+        @R.function
+        def main(
+            x: R.Tensor([3], "float32"),
+            weight_table: R.Tensor([3], "float32"),
+            routing_table: R.Tensor([3], "int64"),
+        ) -> R.Tensor([], "float32"):
+            weight = R.take(weight_table, routing_table, axis=0, mode="clip")
+            out = R.matmul(x, weight)
+            return out
+
+    Expected = Before
+
+
+class TestNoReorderVectorWeightsWithMatrixActivations(Base):
+    """Equal extents keep the invalid rewrite well typed, but change its 
result."""
+
+    @I.ir_module
+    class Before:
+        @R.function
+        def main(
+            x: R.Tensor([3, 3], "float32"),
+            weight_table: R.Tensor([3], "float32"),
+            routing_table: R.Tensor([3], "int64"),
+        ) -> R.Tensor([3], "float32"):
+            weight = R.take(weight_table, routing_table, axis=0, mode="clip")
+            out = R.matmul(x, weight)
+            return out
+
+    Expected = Before
+
+
 batch_size_before = T.dynamic("batch_size", "int64")
 weight_table_size_before = T.dynamic("weight_table_size", "int64")
 batch_size_expected = T.dynamic("batch_size", "int64")

Reply via email to