|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
19 | 19 | import abc |
| 20 | +import copy |
20 | 21 | import dataclasses |
21 | 22 | from typing import Any, Callable, Optional, Protocol, Sequence, Tuple |
22 | 23 |
|
|
30 | 31 | from orbax.checkpoint._src.metadata import empty_values |
31 | 32 | from orbax.checkpoint._src.metadata import pytree_metadata_options as pytree_metadata_options_lib |
32 | 33 | from orbax.checkpoint._src.metadata import value as value_metadata |
| 34 | +from orbax.checkpoint._src.path import types as path_types |
33 | 35 | from orbax.checkpoint._src.serialization import limits |
34 | 36 | import tensorstore as ts |
35 | 37 |
|
@@ -60,81 +62,123 @@ def check_input_arguments(*args): |
60 | 62 | raise ValueError('Found input args with mismatched lengths.') |
61 | 63 |
|
62 | 64 |
|
63 | | -@dataclasses.dataclass(kw_only=True) |
64 | 65 | class ParamInfo: |
65 | 66 | """Information describing a parameter in a PyTree. |
66 | 67 |
|
67 | 68 | Note that ParamInfo is distinct from SaveArgs and RestoreArgs in that in |
68 | 69 | represents information not provided by a user, and should be computed |
69 | 70 | internally. |
70 | 71 |
|
71 | | - name: |
72 | | - Name of the parameter. |
73 | | - parent_dir: |
74 | | - A path providing location where all files under the same checkpoint should |
75 | | - be saved under. All `ParamInfo` provided to a given TypeHandler should have |
76 | | - the same `parent_dir`. The parent_dir is assumed to be a directory. |
77 | | - path: |
78 | | - Do not provide directly. Automatically set to `parent_dir / name`. |
79 | | - skip_deserialize: |
80 | | - If specified, skips deserialization of the given parameter using the |
81 | | - TypeHandler. This may be for multiple different reasons, including that the |
82 | | - parameter may have been aggregated, or it will be unneeded after |
83 | | - transformations. Note: this parameter is handled by PyTreeCheckpointHandler, |
84 | | - so it is unnecessary for TypeHandler implementations to deal with it. |
85 | | - byte_limiter: |
86 | | - Object to limit the number of bytes that can be read or written in |
87 | | - parallel. |
88 | | - device_host_byte_limiter: |
89 | | - Object to limit the number of bytes that can be transferred from device to |
90 | | - host memory in parallel. |
91 | | - is_ocdbt_checkpoint: |
92 | | - Indicates whether the checkpoint path uses OCDBT format |
93 | | - or not. Only used for restoration. |
94 | | - use_compression: |
95 | | - When True, turn on zstd compression. Default is True. |
96 | | - use_zarr3: |
97 | | - If True, use Zarr ver3 otherwise ver2. |
98 | | - ocdbt_target_data_file_size: |
99 | | - Specifies the target size (in bytes) of each OCDBT data file. If set to 0, |
100 | | - data file size is not limited. If omitted (None), the TensorStore default |
101 | | - is used. |
102 | | - ts_context: |
103 | | - Tensorstore context to use for reading/writing. |
104 | | - value_typestr: stores the original value's typestr (from TypeHandler). |
105 | | - Only required when saving. |
106 | | - enable_pinned_host_transfer: |
107 | | - True by default. If False, disables transfer to pinned host when copying |
108 | | - from device to host, regardless of the presence of pinned host memory. |
109 | | - raise_array_data_missing_error: |
110 | | - Only used for restoring. See documentation in `tensorstore_utils.py`. Comes |
111 | | - from tree metadata and should be the same across all parameters. |
112 | | - write_shape: |
113 | | - Shape of the array shard. Used in the subchunking context. |
114 | | - is_prioritized_key_fn: See `IsPrioritizedKeyFn` definition. |
| 72 | + Attributes: |
| 73 | + name: Name of the parameter. |
| 74 | + parent_dir: A path providing location where all files under the same |
| 75 | + checkpoint should be saved under. All ParamInfo provided to a given |
| 76 | + TypeHandler should have the same parent_dir. The parent_dir is assumed to |
| 77 | + be a directory. Accessing this property raises ValueError if the |
| 78 | + underlying path is a PathAwaitingCreation that has not been resolved via |
| 79 | + await_path_creation(). |
| 80 | + path: Automatically set to parent_dir / name. Accessing this property raises |
| 81 | + ValueError if the underlying path is a PathAwaitingCreation that has not |
| 82 | + been resolved via await_path_creation(). |
| 83 | + skip_deserialize: If specified, skips deserialization of the given parameter |
| 84 | + using the TypeHandler. |
| 85 | + byte_limiter: Object to limit the number of bytes that can be read or |
| 86 | + written in parallel. |
| 87 | + device_host_byte_limiter: Object to limit the number of bytes that can be |
| 88 | + transferred from device to host memory in parallel. |
| 89 | + is_ocdbt_checkpoint: Indicates whether the checkpoint path uses OCDBT format |
| 90 | + or not. Only used for restoration. |
| 91 | + use_compression: When True, turn on zstd compression. Default is True. |
| 92 | + use_zarr3: If True, use Zarr ver3 otherwise ver2. |
| 93 | + ocdbt_target_data_file_size: Specifies the target size (in bytes) of each |
| 94 | + OCDBT data file. |
| 95 | + ts_context: Tensorstore context to use for reading/writing. |
| 96 | + value_typestr: Stores the original value's typestr (from TypeHandler). |
| 97 | + enable_pinned_host_transfer: If False, disables transfer to pinned host. |
| 98 | + raise_array_data_missing_error: Only used for restoring. |
| 99 | + write_shape: Shape of the array shard. Used in the subchunking context. |
| 100 | + is_prioritized_key_fn: See ``IsPrioritizedKeyFn`` definition. |
| 101 | + keypath: Tuple of keys identifying the parameter's position in the PyTree. |
115 | 102 | """ |
116 | 103 |
|
117 | | - name: str |
118 | | - parent_dir: epath.Path |
119 | | - path: Optional[epath.Path] = None |
120 | | - keypath: Optional[Tuple[Any, ...]] = None |
121 | | - skip_deserialize: Optional[bool] = None |
122 | | - byte_limiter: Optional[limits.ByteLimiter] = None |
123 | | - device_host_byte_limiter: Optional[limits.ByteLimiter] = None |
124 | | - is_ocdbt_checkpoint: Optional[bool] = None |
125 | | - use_compression: bool | None = True |
126 | | - use_zarr3: Optional[bool] = False |
127 | | - ocdbt_target_data_file_size: Optional[int] = None |
128 | | - ts_context: Optional[ts.Context] = None |
129 | | - value_typestr: Optional[str] = None |
130 | | - enable_pinned_host_transfer: bool = False |
131 | | - raise_array_data_missing_error: bool = True |
132 | | - write_shape: arrays_types.Shape | None = None |
133 | | - is_prioritized_key_fn: Optional[IsPrioritizedKeyFn] = None |
134 | | - |
135 | | - def __post_init__(self): |
136 | | - if self.path is None: |
137 | | - self.path = self.parent_dir / self.name |
| 104 | + def __init__( |
| 105 | + self, |
| 106 | + *, |
| 107 | + name: str, |
| 108 | + parent_dir: epath.Path | path_types.PathAwaitingCreation, |
| 109 | + path: epath.Path | path_types.PathAwaitingCreation | None = None, |
| 110 | + keypath: Optional[Tuple[Any, ...]] = None, |
| 111 | + skip_deserialize: Optional[bool] = None, |
| 112 | + byte_limiter: Optional[limits.ByteLimiter] = None, |
| 113 | + device_host_byte_limiter: Optional[limits.ByteLimiter] = None, |
| 114 | + is_ocdbt_checkpoint: Optional[bool] = None, |
| 115 | + use_compression: bool | None = True, |
| 116 | + use_zarr3: Optional[bool] = False, |
| 117 | + ocdbt_target_data_file_size: Optional[int] = None, |
| 118 | + ts_context: Optional[ts.Context] = None, |
| 119 | + value_typestr: Optional[str] = None, |
| 120 | + enable_pinned_host_transfer: bool = False, |
| 121 | + raise_array_data_missing_error: bool = True, |
| 122 | + write_shape: arrays_types.Shape | None = None, |
| 123 | + is_prioritized_key_fn: Optional[IsPrioritizedKeyFn] = None, |
| 124 | + ): |
| 125 | + self.name = name |
| 126 | + self._parent_dir = parent_dir |
| 127 | + self._path = path if path is not None else parent_dir / name |
| 128 | + self.keypath = keypath |
| 129 | + self.skip_deserialize = skip_deserialize |
| 130 | + self.byte_limiter = byte_limiter |
| 131 | + self.device_host_byte_limiter = device_host_byte_limiter |
| 132 | + self.is_ocdbt_checkpoint = is_ocdbt_checkpoint |
| 133 | + self.use_compression = use_compression |
| 134 | + self.use_zarr3 = use_zarr3 |
| 135 | + self.ocdbt_target_data_file_size = ocdbt_target_data_file_size |
| 136 | + self.ts_context = ts_context |
| 137 | + self.value_typestr = value_typestr |
| 138 | + self.enable_pinned_host_transfer = enable_pinned_host_transfer |
| 139 | + self.raise_array_data_missing_error = raise_array_data_missing_error |
| 140 | + self.write_shape = write_shape |
| 141 | + self.is_prioritized_key_fn = is_prioritized_key_fn |
| 142 | + |
| 143 | + @property |
| 144 | + def parent_dir(self) -> epath.Path: |
| 145 | + if isinstance(self._parent_dir, path_types.PathAwaitingCreation): |
| 146 | + raise ValueError( |
| 147 | + 'parent_dir is a PathAwaitingCreation and has not been resolved yet. ' |
| 148 | + 'Call `await info.await_path_creation()` first.' |
| 149 | + ) |
| 150 | + return self._parent_dir |
| 151 | + |
| 152 | + @parent_dir.setter |
| 153 | + def parent_dir(self, value: epath.Path | path_types.PathAwaitingCreation): |
| 154 | + self._parent_dir = value |
| 155 | + |
| 156 | + @property |
| 157 | + def path(self) -> epath.Path: |
| 158 | + if isinstance(self._path, path_types.PathAwaitingCreation): |
| 159 | + raise ValueError( |
| 160 | + 'path is a PathAwaitingCreation and has not been resolved yet. ' |
| 161 | + 'Call `await info.await_path_creation()` first.' |
| 162 | + ) |
| 163 | + if self._path is None: |
| 164 | + raise ValueError('path is None.') |
| 165 | + return self._path |
| 166 | + |
| 167 | + @path.setter |
| 168 | + def path(self, value: epath.Path | path_types.PathAwaitingCreation | None): |
| 169 | + self._path = value |
| 170 | + |
| 171 | + def replace(self, **kwargs) -> ParamInfo: |
| 172 | + new = copy.copy(self) |
| 173 | + for k, v in kwargs.items(): |
| 174 | + setattr(new, k, v) |
| 175 | + return new |
| 176 | + |
| 177 | + async def await_path_creation(self) -> None: |
| 178 | + if isinstance(self._parent_dir, path_types.PathAwaitingCreation): |
| 179 | + resolved_path = await self._parent_dir.await_creation() |
| 180 | + self._parent_dir = resolved_path |
| 181 | + self._path = resolved_path / self.name |
138 | 182 |
|
139 | 183 |
|
140 | 184 | @dataclasses.dataclass |
|
0 commit comments