Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ignition/preparation/forward_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
53 changes: 53 additions & 0 deletions tests/preparation/test_guard_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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