Skip to content
Open
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
32 changes: 20 additions & 12 deletions checkpoint/orbax/checkpoint/_src/arrays/fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,26 @@ def slice(
stop = out.stop[:] = np.minimum(out.stop, slice_shape)
if not (start < stop).all():
return None
if (value := self.value) is None:
return out
else:
value_fragment = Fragment(
np_index=np.stack([
np.maximum(self.start, np_index[:, 0]),
np.minimum(self.stop, np_index[:, 1]),
np_index[:, 2],
], axis=1)
).offset_by(-self.start)
out_value = value[value_fragment.index or ...]
return dataclasses.replace(out, value=out_value)
return dataclasses.replace(
out, value=self.slice_of_value(np_index)
) if self.value is not None else out

def slice_of_value(
self,
new_np_idx: NpIndex,
) -> np.ndarray:
"""Returns a slice of `value`."""
start = self.start
stop = self.stop
# This is just a convenient way to construct the required tuple of slices.
f = Fragment(
np_index=np.stack([
np.maximum(start, new_np_idx[:, 0]),
np.minimum(stop, new_np_idx[:, 1]),
new_np_idx[:, 2],
], axis=1)
).offset_by(-start)
return self.value[f.index or ...]


@dataclasses.dataclass(frozen=True)
Expand Down