-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Do not Review] AlgoSpec + IOType serialization compatibility test #3119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
talsperre
wants to merge
9
commits into
master
Choose a base branch
from
feature/algo-spec-iotype
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3607545
Add pluggable artifact serializer framework
a337f93
Wire artifact serializers into plugin system
329059f
Replace hardcoded pickle in TaskDataStore with serializer dispatch
db72465
Address Greptile review feedback
ac16511
Add AlgoSpec and @step(start/end) kwargs
talsperre 90dcf81
Add IOType ABC and 11 concrete generic types
21fa792
Add Tensor type with optional numpy dependency
33b6608
Add IOTypeSerializer bridge and wire into plugin system
de5cad9
Merge branch 'pr-3118' into feature/algo-spec-iotype
talsperre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,4 @@ stubs/version.py | |
| # claude code | ||
| .claude/ | ||
|
|
||
| workflow.yaml | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """ | ||
| AlgoSpec -- a single-computation unit within Metaflow. | ||
|
|
||
| AlgoSpec is a FlowSpec subclass with a single step: the call method, | ||
| marked as @step(start=True, end=True). FlowGraph._identify_start_end | ||
| picks it up via the is_start/is_end attributes — no special-casing | ||
| needed in graph traversal, lint, runtime, or Maestro. | ||
|
|
||
| Lifecycle: | ||
| init() -- called once per worker (model loading) | ||
| call() -- called per row or batch (computation) | ||
| """ | ||
|
|
||
| import atexit | ||
|
|
||
| from .flowspec import FlowSpec, FlowSpecMeta | ||
|
|
||
|
|
||
| class AlgoSpecMeta(FlowSpecMeta): | ||
| """Metaclass for AlgoSpec. | ||
|
|
||
| Marks call() as @step(start=True, end=True) before FlowSpecMeta | ||
| builds the graph. The step name is the class name lowercased. | ||
| """ | ||
|
|
||
| _registry = [] | ||
| _atexit_registered = False | ||
|
|
||
| def __init__(cls, name, bases, attrs): | ||
| if name == "AlgoSpec": | ||
| super().__init__(name, bases, attrs) | ||
| return | ||
|
|
||
| from .decorators import step | ||
|
|
||
| call_fn = attrs.get("call") | ||
| if call_fn is not None and callable(call_fn): | ||
| attrs["call"] = step(call_fn, start=True, end=True) | ||
| attrs["call"].name = name.lower() | ||
| attrs["call"].__name__ = name.lower() | ||
| cls.call = attrs["call"] | ||
| cls._algo_step_name = name.lower() | ||
|
|
||
| super().__init__(name, bases, attrs) | ||
|
|
||
| if not hasattr(cls.call, "is_step"): | ||
| from .exception import MetaflowException | ||
|
|
||
| raise MetaflowException( | ||
| "%s must implement call(). " | ||
| "AlgoSpec subclasses require a call() method." % name | ||
| ) | ||
|
|
||
| AlgoSpecMeta._registry.append(cls) | ||
|
|
||
| if not AlgoSpecMeta._atexit_registered: | ||
| atexit.register(AlgoSpecMeta._on_exit) | ||
| AlgoSpecMeta._atexit_registered = True | ||
|
|
||
| def _init_graph(cls): | ||
| from .graph import FlowGraph | ||
|
|
||
| cls._graph = FlowGraph(cls) | ||
| # The method is cls.call but node.name is the class-derived name. | ||
| if cls._graph.is_algo_spec: | ||
| cls._steps = [cls.call] | ||
| else: | ||
| cls._steps = [getattr(cls, node.name) for node in cls._graph] | ||
|
|
||
| @staticmethod | ||
| def _on_exit(): | ||
| AlgoSpecMeta._registry.clear() | ||
|
|
||
|
|
||
| class AlgoSpec(FlowSpec, metaclass=AlgoSpecMeta): | ||
| """Base class for single-computation algo specifications.""" | ||
|
|
||
| is_algo_spec = True | ||
|
|
||
| _EPHEMERAL = FlowSpec._EPHEMERAL | {"is_algo_spec"} | ||
|
|
||
| _NON_PARAMETERS = FlowSpec._NON_PARAMETERS | { | ||
| "init", | ||
| "call", | ||
| "is_algo_spec", | ||
| } | ||
|
|
||
| def init(self): | ||
| """Called once per worker before any call() invocations.""" | ||
| pass | ||
|
|
||
| def call(self): | ||
| """Main computation. Must be overridden.""" | ||
| raise NotImplementedError("Subclasses must implement call()") | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| return self.call(*args, **kwargs) | ||
|
|
||
| def __getattr__(self, name): | ||
| # Resolve the class-derived step name to the call method | ||
| if name == getattr(self.__class__, "_algo_step_name", None): | ||
| return self.call | ||
| return super().__getattr__(name) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from .serializer import ( | ||
| ArtifactSerializer, | ||
| SerializationMetadata, | ||
| SerializedBlob, | ||
| SerializerStore, | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| from abc import ABCMeta, abstractmethod | ||
| from collections import namedtuple | ||
|
|
||
|
|
||
| SerializationMetadata = namedtuple( | ||
| "SerializationMetadata", ["type", "size", "encoding", "serializer_info"] | ||
| ) | ||
|
|
||
|
|
||
| class SerializedBlob(object): | ||
| """ | ||
| Represents a single blob produced by a serializer. | ||
|
|
||
| A serializer may produce multiple blobs per artifact. Each blob is either: | ||
| - New bytes to be stored (is_reference=False, value is bytes) | ||
| - A reference to already-stored data (is_reference=True, value is a string key) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| value : Union[str, bytes] | ||
| The blob data (bytes) or a reference key (str). | ||
| is_reference : bool, optional | ||
| If None, auto-detected from value type: str -> reference, bytes -> new data. | ||
| compress_method : str | ||
| Compression method for new blobs. Ignored for references. Default "gzip". | ||
| NOTE: Not yet wired into the save path — ContentAddressedStore currently | ||
| always applies gzip. This field is forward-looking for when per-blob | ||
| compression control is needed (e.g., multi-blob IOType support). | ||
| """ | ||
|
|
||
| def __init__(self, value, is_reference=None, compress_method="gzip"): | ||
| if not isinstance(value, (str, bytes)): | ||
| raise TypeError( | ||
| "SerializedBlob value must be str or bytes, got %s" % type(value).__name__ | ||
| ) | ||
| self.value = value | ||
| self.compress_method = compress_method | ||
| if is_reference is None: | ||
| self.is_reference = isinstance(value, str) | ||
| else: | ||
| self.is_reference = is_reference | ||
|
|
||
| @property | ||
| def needs_save(self): | ||
| """True if this blob contains new bytes that need to be stored.""" | ||
| return not self.is_reference | ||
|
|
||
|
|
||
| class SerializerStore(ABCMeta): | ||
| """ | ||
| Metaclass for ArtifactSerializer that auto-registers subclasses by TYPE. | ||
|
|
||
| Provides deterministic ordering: serializers are sorted by (PRIORITY, registration_order). | ||
| Lower PRIORITY values are tried first. Registration order breaks ties. | ||
| """ | ||
|
|
||
| _all_serializers = {} | ||
| _registration_order = [] | ||
|
|
||
| def __init__(cls, name, bases, namespace): | ||
| super().__init__(name, bases, namespace) | ||
| if cls.TYPE is not None: | ||
| if cls.TYPE not in SerializerStore._all_serializers: | ||
| SerializerStore._registration_order.append(cls.TYPE) | ||
| SerializerStore._all_serializers[cls.TYPE] = cls | ||
|
|
||
| @staticmethod | ||
| def get_ordered_serializers(): | ||
| """ | ||
| Return serializer classes sorted by (PRIORITY, registration_order). | ||
|
|
||
| This ordering is deterministic for a given set of loaded serializers. | ||
| """ | ||
| order = SerializerStore._registration_order | ||
| return sorted( | ||
| SerializerStore._all_serializers.values(), | ||
| key=lambda s: (s.PRIORITY, order.index(s.TYPE)), | ||
| ) | ||
|
|
||
|
|
||
| class ArtifactSerializer(object, metaclass=SerializerStore): | ||
| """ | ||
| Abstract base class for artifact serializers. | ||
|
|
||
| Subclasses must set TYPE to a unique string identifier and implement | ||
| all four class methods. Subclasses are auto-registered by the SerializerStore | ||
| metaclass on class definition. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| TYPE : str or None | ||
| Unique identifier for this serializer (e.g., "pickle", "iotype"). | ||
| Set to None in the base class to prevent registration. | ||
| PRIORITY : int | ||
| Dispatch priority. Lower values are tried first. Default 100. | ||
| PickleSerializer uses 9999 as the universal fallback. | ||
| """ | ||
|
|
||
| TYPE = None | ||
| PRIORITY = 100 | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def can_serialize(cls, obj): | ||
| """ | ||
| Return True if this serializer can handle the given object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Any | ||
| The Python object to serialize. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def can_deserialize(cls, metadata): | ||
| """ | ||
| Return True if this serializer can deserialize given the metadata. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| metadata : SerializationMetadata | ||
| Metadata stored alongside the artifact. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def serialize(cls, obj): | ||
| """ | ||
| Serialize obj to blobs and metadata. Must be side-effect-free. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Any | ||
| The Python object to serialize. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple | ||
| (List[SerializedBlob], SerializationMetadata) | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def deserialize(cls, blobs, metadata, context): | ||
| """ | ||
| Deserialize blobs back to a Python object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| blobs : List[bytes] | ||
| The raw blob data. | ||
| metadata : SerializationMetadata | ||
| Metadata stored alongside the artifact. | ||
| context : Any | ||
| Optional context for deserialization (e.g., task vs client loading). | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| """ | ||
| raise NotImplementedError |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except Exceptionsilently swallows real failuresThe
except Exception: passblock treats every failure — includingAttributeError, missing_parameterstask, network errors, and data corruption — as "just an old run" and falls back to("start", "end"). This means a corrupted metadata store or a genuine programming mistake silently produces incorrect graph endpoints rather than a visible error.Consider at minimum logging a warning on the exception, or narrowing the except to
KeyErrorfor the missing-step case.