Skip to content
Open
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 packages/experimental/kumo-pytorch/docs/api/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract is self-contained; saved declarations are checked on rebinding.
DataLoaderSpec.to_json(*, indent: int | None = 2) -> str
```

Sort named definitions while retaining explicit output/operand order.
Serialize in declaration order; `fingerprint` hashes a key-sorted copy.

#### `DataLoaderSpec.from_dict`

Expand Down
4 changes: 2 additions & 2 deletions packages/experimental/kumo-pytorch/src/kumo/pytorch/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ def resolve(self, *, dataset: Dataset | None = None) -> DataLoaderSpec:
)

def to_json(self, *, indent: int | None = 2) -> str:
"""Sort named definitions while retaining explicit output/operand order."""
"""Serialize in declaration order; ``fingerprint`` hashes a key-sorted copy."""

return json.dumps(self.to_dict(), indent=indent, sort_keys=True, allow_nan=False)
return json.dumps(self.to_dict(), indent=indent, allow_nan=False)

@classmethod
def from_dict(cls, value: Mapping[str, Any]) -> DataLoaderSpec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,38 @@ def test_artifact_contract_preserves_executable_meaning(tmp_path):
assert task.preview(requests).batch.inputs["values"]["b_total"]["values"].tolist() == [
6
]


def _unsorted_inputs_artifact() -> tuple[api.Artifact, api.Dataset, list[api.SampleRequest]]:
names = ("z_input", "a_input", "m_input")
contract, requests = _contract(
values={name: api.Constant(value=1.0, dtype=api.DType("float64")) for name in names}
)
inputs = {
name: api.Input(
api.EntitySource(),
api.Fields(fields=(api.Encode(field=name, encoding=api.Raw()),), row=api.Columns()),
api.Separate(),
)
for name in names
}
contract = replace(contract, batch=api.BatchSpec(inputs))
dataset = api.Dataset({"users": api.InMemoryRows([{"id": 1}])})
with api.prepare(contract, dataset, api.InMemoryEngine()) as plan:
with plan.materialize(requests) as task:
return task.to_artifact(), dataset, list(requests)


def test_saved_artifact_keeps_input_declaration_order(tmp_path):
artifact, dataset, requests = _unsorted_inputs_artifact()
declared = ["z_input", "a_input", "m_input"]
artifact.save(tmp_path)
loaded = api.Artifact.load(tmp_path)
assert list(loaded.contract.batch.inputs) == declared
assert loaded.contract.fingerprint == artifact.contract.fingerprint
with api.prepare(loaded.contract, dataset, api.InMemoryEngine()) as plan:
with plan.materialize(requests, fitted=loaded.fitted_state, inference=True) as task:
batch = next(iter(task.batches(batch_size=1, seed=0)))
assert list(batch.inputs) == declared
served = api.ServingAssembler(loaded).assemble({"users": [{"id": 1}]}, requests)
assert list(served[0].inputs) == declared
Loading