Repository navigation
[dynamo] Build the guard filter entries once in CheckFunctionManager - #198085
bobrenjc93 wants to merge 2 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/198085
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ⏳ 6 Pending, 2 Unrelated FailuresAs of commit ee9f978 with merge base 618de55 ( 🟡 GREEN LIGHT - PR requires human review:
reason: UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Claude finished @bobrenjc93's task in 4m 2s —— View job PR #198085 reviewReviewed the 35/-16 refactor in Full reviewPR Review: #198085SummaryHoists the inline guard-filter application in Code Quality
Performance
Notes on claims I verified
RecommendationApprove — the refactor is correct and the stated invariants check out. The unreachable assertion and the stale |
[ghstack-poisoned]
|
Abandoning this stack for now; it may be reopened later. |
Stack from ghstack (oldest at bottom):
Problem. CheckFunctionManager applied the user's guard filter inline: when a filter was set it ran an extra unsaved build over the sorted guards, turned each guard into a GuardFilterEntry from that builder, called the filter, validated the result, and rebound the same local to the surviving list, which the runtime build, compile_check_fn and serialize_guards then all consumed. That shape supports exactly one filter. The next commit in this stack needs to apply a second filter (the package's serialization filter) to the same guards and keep both results, and inlining the build-entries-call-filter-validate sequence a second time would duplicate the extra build and the checks.
Fix. The constructor now sorts the guards into all_guards and defines two closures over them. build_filter_entries runs the inspection build at most once and caches the resulting GuardFilterEntry list; apply_filter takes a filter callable, forces the entries to exist, calls it, keeps the length and bool-type assertions from the old inline code, and returns the guards the filter kept. The list that the runtime build, compile_check_fn and serialize_guards receive is renamed runtime_guards and is either apply_filter(guard_filter_fn) or all_guards when no filter is set. No other function changes.
Summary. This is a pure refactor with no behaviour change: a filter is still called exactly once with the same entries, the inspection build still runs only when some filter exists, and a filter-less compile still does one build as before. Caching the entries rather than rebuilding per filter is sound because every build_guards call resets and repopulates code_list and guard_types on the guards it is given, so a single snapshot taken from the unsaved inspection build serves any number of filters over these guards; the comment in build_filter_entries records that. The alternative of hoisting this into a method on CheckFunctionManager was rejected because the closures only make sense over the constructor's locals (existing_diff_guard_sources, f_code, output_graph) and nothing outside the constructor needs them. The caching_precompile wrapper that rewrites guard_filter_fn is untouched and still applies to the runtime filter only. The commit above this one adds serialization_guard_filter_fn and calls apply_filter a second time for the guards that get pickled, which is why the runtime list gets its own name here. Carved out of #195913.
The filter still runs once over one set of entries
Run from the worktree root; the guard filter passed through compile options is the guard_filter_fn that CheckFunctionManager receives.
Inside the test helper the same path is exercised by constructing the manager directly, which is the shape the next commit extends:
Pinned by test_global_state_guard_filter and test_torch_function_state_filter in test/dynamo/test_guard_serialization.py, and by every _test_serialization case in that file, which selects one guard type through guard_filter_fn and checks that the saved and rebuilt guard managers agree with the runtime one.
Test Plan:
The file passes on the parent and passes here; no test changes because behaviour is unchanged.
Authored with an AI assistant.
Co-Authored-By: Claude Fable 5.1 [email protected]
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98