Skip to content

Conversation

@justinchuby
Copy link
Collaborator

Create and use ir.Value in loop subgraph inputs and bodies to avoid unintentional duplication of ir.Values (which led to invalid IR graphs).

Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.45%. Comparing base (7ec5d25) to head (d696f19).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnxscript/_internal/converter.py 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2777      +/-   ##
==========================================
- Coverage   70.45%   70.45%   -0.01%     
==========================================
  Files         228      228              
  Lines       27260    27257       -3     
  Branches     2760     2760              
==========================================
- Hits        19206    19203       -3     
  Misses       7102     7102              
  Partials      952      952              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical issue in loop statement translation where ir.Value objects were being duplicated, leading to invalid IR graphs. The fix ensures that ir.Value objects are created properly using make_value() and reused rather than duplicated.

Changes:

  • Replace bare ir.Value() construction with make_value() calls to ensure proper metadata and type information
  • Use generate_unique_name() for condition variables to avoid name collisions
  • Reuse existing ir.Value objects in loop subgraph parameters and outputs instead of creating duplicates

@justinchuby
Copy link
Collaborator Author

onnxscript/_framework_apis/torch_2_5.py:113: in get_torchlib_ops
    from onnxscript.function_libs.torch_lib import (  # pylint: disable=import-outside-toplevel
onnxscript/function_libs/torch_lib/ops/__init__.py:16: in <module>
    from . import (
onnxscript/function_libs/torch_lib/ops/core.py:3197: in <module>
    @torch_op("aten::embedding_bag", private=True)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/function_libs/torch_lib/registration.py:138: in wrapper
    processed_func = onnxscript.script(opset=custom_opset)(func)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/_internal/main.py:94: in transform
    result = script_check(f_ast, opset, env, src, default_opset=default_opset)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/_internal/main.py:38: in script_check
    return convert.translate_function_def(f)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/_internal/converter.py:1491: in translate_function_def
    fn_ir = self._translate_function_def_common(stmt)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/_internal/converter.py:1478: in _translate_function_def_common
    self._translate_stmt(s, index_of_stmt=i)
onnxscript/_internal/converter.py:1023: in _translate_stmt
    return self._translate_loop_stmt(node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/_internal/converter.py:1316: in _translate_loop_stmt
    self.fail(
onnxscript/_internal/converter.py:263: in fail
    fail(self._message(node, error_msg))
onnxscript/_internal/converter.py:53: in fail
    raise TranslationError(msg)
E   onnxscript._internal.converter.TranslationError: ERROR: Unable to find condition variable cond_11 in known variables ['infinite_loop', 'index_tensor', 'result', 'slice_index', 'start', 'end', 'row_result', 'cond'].
E   at: Function 'loop_body', line 36
E       while cond:
E       ^

@justinchuby justinchuby marked this pull request as draft January 22, 2026 03:30
@justinchuby
Copy link
Collaborator Author

justinchuby commented Jan 22, 2026

[Claude]

Looking at the error, the issue is that cond_while.name is cond_11 but the actual variable in scope is cond. Let me trace through the code to find the bug.

The Problem
In converter.py:1243-1380, for a while loop, cond_while is created at converter.py:1270-1275:

But later at converter.py:1354-1361, the code tries to look up cond_while.name in the current scope:

The problem is that cond_while.name is "cond_11" (a uniquified name), but the Python variable bound in scope is still "cond" (the original test.id).

The Fix
The code should look up the original Python variable name (test.id), not the uniquified ONNX name. Here's the fix:

And update the lookup code:

Summary
The bug is that cond_while was being set to a newly created ir.Value with a uniquified name (cond_11), when it should just store the original Python variable name (cond) to look up in the scope later.

The original code (before your file) had cond_while = ir.Value(name=test.id) which preserved the original name. Your updated version uses make_value(self.generate_unique_name(test.id), ...) which generates a new unique name, breaking the lookup.

Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@justinchuby justinchuby marked this pull request as ready for review January 22, 2026 03:34
@justinchuby justinchuby requested a review from Copilot January 22, 2026 03:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@justinchuby justinchuby enabled auto-merge (squash) January 22, 2026 20:26
@justinchuby justinchuby merged commit 1080d6a into main Jan 22, 2026
32 of 33 checks passed
@justinchuby justinchuby deleted the justinchu/cond-fix branch January 22, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants