From acbab0816273b203ec7ee41deba81f4481616c41 Mon Sep 17 00:00:00 2001 From: woooflet Date: Tue, 1 Dec 2026 10:34:58 +0000 Subject: [PATCH] Strip unguarded attributes from hyperflare_recipes objects in AOT guard serialization Add 'hyperflare_recipes.' alongside ('ignition.', 'b12x.') in _uses_custom_object_state() in ignition/preparation/forward_metadata.py so objects from the external recipe package that own unpicklable CUDA kernel handles serialize cleanly during torch.compile AOT guard pickling. Discord: woooflet Design-Signed-Off-By: Antigravity (Gemini-4-Neon); design-review=2e5b Signed-off-by: woooflet --- ignition/preparation/forward_metadata.py | 2 +- tests/preparation/test_guard_code.py | 53 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/ignition/preparation/forward_metadata.py b/ignition/preparation/forward_metadata.py index 20920a516..31caec0ee 100644 --- a/ignition/preparation/forward_metadata.py +++ b/ignition/preparation/forward_metadata.py @@ -39,7 +39,7 @@ def _uses_custom_object_state(value): return ( hasattr(value, '__dict__') - and type(value).__module__.startswith(('ignition.', 'b12x.')) + and type(value).__module__.startswith(('ignition.', 'b12x.', 'hyperflare_recipes.')) and not inspect.isclass(value) and not isinstance(value, _NON_OBJECT_NEW_TYPES) ) diff --git a/tests/preparation/test_guard_code.py b/tests/preparation/test_guard_code.py index 628a936c6..e581f93f6 100644 --- a/tests/preparation/test_guard_code.py +++ b/tests/preparation/test_guard_code.py @@ -247,3 +247,56 @@ def test_guard_serialization_roundtrips_tensor_user_attributes(): wrong_scale.weight.scale = torch.ones((2, 2), dtype=torch.float32) assert restored.guard_check(forward) assert not restored.guard_check(wrong_scale) + + +class _UnpicklableHandle: + def __reduce__(self): + raise TypeError("compiled kernel handles cannot be pickled") + + +class _RecipeKernelOwner: + __module__ = "hyperflare_recipes.components.synthetic" + + def __init__(self, scale): + self.scale = scale + self.compiled_handle = _UnpicklableHandle() + + def apply(self, value): + return value * self.scale + + +class _RecipeForward: + def __init__(self, scale): + self.value = torch.ones(2) + self.owner = _RecipeKernelOwner(scale) + + def __call__(self): + return self.owner.apply(self.value) + + +def test_guard_pickler_strips_unguarded_attributes_from_recipe_objects(monkeypatch): + import types + from torch._dynamo.aot_compile_types import GraphModuleSerializableCallable + + module_name = "hyperflare_recipes.components._synthetic_guard_test" + synthetic = types.ModuleType(module_name) + _RecipeKernelOwner.__module__ = module_name + synthetic._RecipeKernelOwner = _RecipeKernelOwner + monkeypatch.setitem(sys.modules, module_name, synthetic) + + forward = _RecipeForward(3) + with _guard_serialization(): + compiled = torch.compile( + _execute, + backend=lambda graph, inputs: GraphModuleSerializableCallable(graph), + fullgraph=True, + dynamic=False, + ).aot_compile(((forward,), {})) + data = type(compiled).serialize(compiled).serialized_data + loaded = type(compiled).deserialize(data) + torch.testing.assert_close(loaded(forward), torch.full((2,), 3.0)) + assert loaded.guard_check(forward) is True + forward.owner.scale = 5 + assert loaded.guard_check(forward) is False + forward.owner.scale = 3 + assert loaded.guard_check(forward) is True