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 "javierdejesusda (via GitHub)" <gi...@apache.org> on 2026/06/03 12:15:55 UTC

[PR] [Relax][PyTorch] Decompose integer pow into repeated multiplication [tvm]

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

   `torch.pow` on an integer tensor returns an integer result, but the PyTorch frontend lowered it to `relax.op.power`, which fails `LegalizeOps` with `power only applies to float` (TOPI `power` / `tvm::pow` requires a floating-point input).
   
   This decomposes an integer base raised to a constant non-negative integer exponent into repeated multiplication, so the result stays integral and matches PyTorch. Float bases and non-constant or tensor exponents keep using `relax.op.power` unchanged. The ONNX frontend already uses the same decomposition (`x**3 = x*x*x`).
   
   Added structural tests covering both the FX and ExportedProgram import paths.
   
   Fixes #19550


-- 
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][PyTorch] Decompose integer pow into repeated multiplication [tvm]

Posted by "javierdejesusda (via GitHub)" <gi...@apache.org>.
javierdejesusda commented on code in PR #19660:
URL: https://github.com/apache/tvm/pull/19660#discussion_r3348513537


##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -523,6 +524,27 @@ def call_binary_op(op, lhs, rhs):
 
         return convert
 
+    def _pow(self, node: fx.Node) -> relax.Var:
+        lhs, rhs = self.retrieve_args(node)
+        # torch integer pow returns an integer tensor, but relax.op.power legalizes to
+        # TOPI power which requires floating-point inputs. Decompose an integer base with
+        # a constant non-negative integer exponent into repeated multiplication instead.
+        if (
+            isinstance(lhs, relax.Expr)
+            and isinstance(lhs.struct_info, relax.TensorStructInfo)
+            and "int" in lhs.struct_info.dtype
+            and isinstance(rhs, int)
+            and not isinstance(rhs, bool)
+            and rhs >= 0
+        ):
+            if rhs == 0:
+                return self.block_builder.emit(relax.op.ones_like(lhs))
+            result = lhs
+            for _ in range(rhs - 1):
+                result = self.block_builder.emit(relax.op.multiply(result, lhs))
+            return result

Review Comment:
   Keeping linear repeated multiplication intentionally: it matches the existing frontend idiom (onnx_frontend.py:1627, x^3 = x*x*x), and because each factor is emitted as a separate flat binding there's no recursive nesting to overflow. Realistic integer exponents are small, and large ones overflow the integer dtype itself, so O(log N) buys nothing here while complicating the expected-IR tests.



-- 
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][PyTorch] Decompose integer pow into repeated multiplication [tvm]

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


-- 
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][PyTorch] Decompose integer pow into repeated multiplication [tvm]

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


##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -523,6 +524,27 @@ def call_binary_op(op, lhs, rhs):
 
         return convert
 
+    def _pow(self, node: fx.Node) -> relax.Var:
+        lhs, rhs = self.retrieve_args(node)
+        # torch integer pow returns an integer tensor, but relax.op.power legalizes to
+        # TOPI power which requires floating-point inputs. Decompose an integer base with
+        # a constant non-negative integer exponent into repeated multiplication instead.
+        if (
+            isinstance(lhs, relax.Expr)
+            and isinstance(lhs.struct_info, relax.TensorStructInfo)
+            and "int" in lhs.struct_info.dtype
+            and isinstance(rhs, int)
+            and not isinstance(rhs, bool)
+            and rhs >= 0
+        ):
+            if rhs == 0:
+                return self.block_builder.emit(relax.op.ones_like(lhs))
+            result = lhs
+            for _ in range(rhs - 1):
+                result = self.block_builder.emit(relax.op.multiply(result, lhs))
+            return result

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Decomposing integer power using linear repeated multiplication takes $O(N)$ operations, which can lead to extremely bloated IR or compiler stack overflows for larger exponents (e.g., `x.pow(100)`).
   
   Using **exponentiation by squaring** (binary exponentiation) reduces the complexity to $O(\log N)$ multiplications, which is much more efficient and robust.
   
   ```suggestion
               if rhs == 0:
                   return self.block_builder.emit(relax.op.ones_like(lhs))
               result = None
               base = lhs
               exponent = rhs
               while exponent > 0:
                   if exponent % 2 == 1:
                       if result is None:
                           result = base
                       else:
                           result = self.block_builder.emit(relax.op.multiply(result, base))
                   if exponent > 1:
                       base = self.block_builder.emit(relax.op.multiply(base, base))
                   exponent //= 2
               return result
   ```



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