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 "guan404ming (via GitHub)" <gi...@apache.org> on 2026/02/09 03:10:01 UTC

[PR] [Relax] Support constant folding for call_tir with tuple outputs [tvm]

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

   ## Why
   
   Constant folding skipped call_tir nodes with tuple (multi-tensor) outputs, leaving foldable operations unoptimized.
   
   ## How
   
   - Add ConstEvaluateCallTIRTuple to handle call_tir with TupleStructInfo output by allocating and packing multiple output tensors
   - Route VisitCallTIR through tuple vs single-tensor paths based on sinfo_args type
   - Add test for folding a split-like prim_func with two output tensors


-- 
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] Support constant folding for call_tir with tuple outputs [tvm]

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

   Thanks!


-- 
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] Support constant folding for call_tir with tuple outputs [tvm]

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


##########
src/relax/transform/fold_constant.cc:
##########
@@ -175,25 +174,73 @@ class ConstantFolder : public ExprMutator {
     return Constant(ret_tensor);
   }
 
+  // Try constant evaluate a call_tir with tuple outputs (multiple output tensors).
+  // Returns std::nullopt on failure.
+  ffi::Optional<Expr> ConstEvaluateCallTIRTuple(tir::PrimFunc tir_func,
+                                                ffi::Array<runtime::Tensor> arr_args,
+                                                const TupleStructInfoNode* tuple_sinfo) {
+    ffi::Optional<ffi::Function> func = GetCachedBuild(tir_func);
+    if (!func) return std::nullopt;
+
+    DLDevice cpu_dev = {DLDeviceType::kDLCPU, 0};
+    size_t num_outputs = tuple_sinfo->fields.size();
+
+    // Match shapes and dtypes for all output fields.
+    std::vector<runtime::Tensor> ret_tensors;
+    for (size_t i = 0; i < num_outputs; ++i) {
+      ffi::Optional<ffi::Shape> shape = MatchConstShape(tuple_sinfo->fields[i]);
+      if (!shape) return std::nullopt;
+      auto tensor_sinfo = tuple_sinfo->fields[i].as<TensorStructInfoNode>();
+      if (!tensor_sinfo || tensor_sinfo->IsUnknownDtype()) return std::nullopt;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Since `MatchConstShape` is called on line 191 and it succeeds, we know that `tuple_sinfo->fields[i]` is a `TensorStructInfo`. Therefore, you can use `Downcast<TensorStructInfo>` here instead of `as<TensorStructInfoNode>()` to make the assumption explicit and avoid a redundant null check. `Downcast` will perform a checked cast.
   
   ```suggestion
         auto tensor_sinfo = Downcast<TensorStructInfo>(tuple_sinfo->fields[i]);
         if (tensor_sinfo->IsUnknownDtype()) return std::nullopt;
   ```



##########
src/relax/transform/fold_constant.cc:
##########
@@ -175,25 +174,73 @@ class ConstantFolder : public ExprMutator {
     return Constant(ret_tensor);
   }
 
+  // Try constant evaluate a call_tir with tuple outputs (multiple output tensors).
+  // Returns std::nullopt on failure.
+  ffi::Optional<Expr> ConstEvaluateCallTIRTuple(tir::PrimFunc tir_func,
+                                                ffi::Array<runtime::Tensor> arr_args,
+                                                const TupleStructInfoNode* tuple_sinfo) {
+    ffi::Optional<ffi::Function> func = GetCachedBuild(tir_func);
+    if (!func) return std::nullopt;
+
+    DLDevice cpu_dev = {DLDeviceType::kDLCPU, 0};
+    size_t num_outputs = tuple_sinfo->fields.size();
+
+    // Match shapes and dtypes for all output fields.
+    std::vector<runtime::Tensor> ret_tensors;
+    for (size_t i = 0; i < num_outputs; ++i) {
+      ffi::Optional<ffi::Shape> shape = MatchConstShape(tuple_sinfo->fields[i]);
+      if (!shape) return std::nullopt;
+      auto tensor_sinfo = tuple_sinfo->fields[i].as<TensorStructInfoNode>();
+      if (!tensor_sinfo || tensor_sinfo->IsUnknownDtype()) return std::nullopt;
+      ret_tensors.push_back(runtime::Tensor::Empty(shape.value(), tensor_sinfo->dtype, cpu_dev));
+    }
+
+    // Pack input args + all output tensors.
+    std::vector<AnyView> packed_args(arr_args.size() + num_outputs);
+    std::vector<runtime::Tensor> temp_args(arr_args.begin(), arr_args.end());
+
+    size_t arg_offset = 0;
+    for (; arg_offset < arr_args.size(); ++arg_offset) {
+      packed_args[arg_offset] = temp_args[arg_offset];
+    }
+    for (size_t i = 0; i < num_outputs; ++i) {
+      packed_args[arg_offset++] = ret_tensors[i];
+    }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The argument packing logic can be simplified for better readability and to be more idiomatic C++. Instead of using a C-style loop with an index, you can use range-based for loops to populate `packed_args`.
   
   ```c
       std::vector<runtime::Tensor> temp_args(arr_args.begin(), arr_args.end());
       std::vector<AnyView> packed_args;
       packed_args.reserve(temp_args.size() + num_outputs);
       for (const auto& arg : temp_args) {
         packed_args.push_back(arg);
       }
       for (const auto& out_tensor : ret_tensors) {
         packed_args.push_back(out_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] Support constant folding for call_tir with tuple outputs [tvm]

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


-- 
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] Support constant folding for call_tir with tuple outputs [tvm]

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

   cc @tlopex @mshr-h 


-- 
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] Support constant folding for call_tir with tuple outputs [tvm]

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

   ## Summary of Changes
   
   Hello @guan404ming, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
   
   This pull request enhances the Relax constant folding pass by enabling it to optimize `call_tir` operations that return multiple tensors as a tuple. This addresses a limitation where such operations were previously unoptimized, allowing for more aggressive constant propagation and potentially improving the performance of compiled models.
   
   ### Highlights
   
   * **Expanded Constant Folding**: Constant folding now supports `call_tir` nodes that produce multiple output tensors (tuples), which were previously skipped.
   * **New Evaluation Function**: Introduced `ConstEvaluateCallTIRTuple` to specifically handle the evaluation and packing of multiple output tensors for `call_tir` calls.
   * **Improved VisitCallTIR Logic**: The `VisitCallTIR` method has been updated to correctly route `call_tir` nodes to either the single-tensor or tuple-output evaluation paths based on their `sinfo_args` type.
   * **New Test Case**: A dedicated test `test_fold_tuple_output` was added to validate the constant folding of a `split`-like `prim_func` with two output tensors.
   
   🧠 **New Feature in Public Preview:** You can now enable **Memory** to help **Gemini Code Assist** learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. **Click [here](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   <details>
   <summary><b>Changelog</b></summary>
   
   * **src/relax/transform/fold_constant.cc**
       * Removed a restrictive comment in `MatchConstShape` regarding single output support.
       * Clarified the purpose of `ConstEvaluateCallTIR` to handle single tensor outputs.
       * Implemented `ConstEvaluateCallTIRTuple` to manage constant evaluation for `call_tir` with tuple outputs, including shape matching, tensor allocation, and argument packing.
       * Refactored `VisitCallTIR` to conditionally call `ConstEvaluateCallTIRTuple` for tuple outputs or `ConstEvaluateCallTIR` for single tensor outputs.
       * Removed a `TODO` comment related to tuple output support.
   * **tests/python/relax/test_transform_fold_constant.py**
       * Added `test_fold_tuple_output`, which defines a `prim_func` that splits a tensor into two and verifies that `FoldConstant` correctly folds the `call_tir` into a `Tuple` of `Constant` expressions.
   </details>
   
   
   
   <details>
   <summary><b>Activity</b></summary>
   
   * The author, guan404ming, has submitted this pull request to introduce new functionality and a corresponding test. No further review comments or activity are available in the provided context.
   </details>
   
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
   
   Feature | Command | Description
   --- | --- | ---
   Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
   Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
   Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
   Help | `/gemini help` | Displays a list of available commands.
   
   <b>Customization</b>
   
   To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up [here](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the [Gemini Code Assist IDE Extension](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


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