[BugFix][Relax] Add structural_equal verification to subroutine cache lookup#18962
Open
3em0 wants to merge 3 commits intoapache:mainfrom
Open
[BugFix][Relax] Add structural_equal verification to subroutine cache lookup#189623em0 wants to merge 3 commits intoapache:mainfrom
3em0 wants to merge 3 commits intoapache:mainfrom
Conversation
… lookup SubroutineMixin._get_subroutine() used structural_hash as the sole cache key without structural_equal verification. If two different arg_sinfo values produced the same 64-bit hash, the cache would return a previously compiled function with mismatched parameter shapes, leading to silently incorrect compiled output. The fix changes the cache to store a list of (arg_sinfo, result) pairs per hash bucket and verifies each candidate with structural_equal before returning. This follows the same pattern used in block_builder.cc (StructuralHash + StructuralEqual). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Code Review
This pull request addresses a correctness issue in the subroutine cache of SubroutineMixin._get_subroutine() where relying solely on structural_hash could lead to incorrect function reuse. The fix implements a bucketed cache that verifies hits using structural_equal. A security advisory and a regression test are also included. Feedback suggests further refining the cache lookup_key by including the function object to prevent collisions between different implementations with identical input signatures.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
The previous test used nn.op.add on tensors with incompatible shapes (batch_size, 32) + (batch_size, 64). Changed to use Linear layers that project different input dimensions to the same output dimension, making the add valid while still testing distinct subroutine generation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SubroutineMixin._get_subroutine()usedstructural_hashas the sole cache key withoutstructural_equalverification. If two differentarg_sinfovalues produced the same 64-bit hash (collision), the cache would return a previously compiled function with mismatched parameter shapes, leading to silently incorrect compiled output.(arg_sinfo, result)pairs per hash bucket and verify withstructural_equalon lookup, consistent with the pattern inblock_builder.cc.Root Cause
The subroutine cache (
cls._gvar) was keyed by(structural_hash(arg_sinfo), is_dataflow). A hash match was treated as proof of structural equality, skipping the necessarystructural_equalcheck. This is a hash-only lookup anti-pattern — hash determines the bucket, but equality must confirm the match.For comparison,
block_builder.cccorrectly usesStructuralHash+StructuralEqualtogether as the hash and equality functions forstd::unordered_map.Test plan
test_linearpasses (no regression)test_different_shapes_produce_distinct_subroutinespasses — verifies that the same Module class with different input shapes generates distinct subroutines🤖 Generated with Claude Code