checkpoint.streamer
CheckpointManager
Bases: object
Custom msgpack checkpointer that saves large train states by serializing and saving tensors one by one in a streaming fashion. Avoids running out of memory or local TPU disk with default flax checkpointer.
Source code in src/fjformer/checkpoint/streamer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
load_checkpoint(path, target=None, shard_fns=None, remove_dict_prefix=None, verbose=False, mismatch_allowed=True)
staticmethod
The load_checkpoint function is used to checkpoint a checkpoint from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
Union[str, PathLike]
|
Specify the path to the checkpoint file |
required |
target |
Specify the model to checkpoint the checkpoint into |
None
|
|
shard_fns |
dict[Callable]
|
Specify a function that will be applied to each tensor in the checkpoint |
None
|
remove_dict_prefix |
Remove the prefix of a dictionary |
None
|
|
verbose |
bool
|
print state and other stuff |
False
|
mismatch_allowed |
bool
|
when ever to allow shard_fns to be passed even if their None |
True
|
Returns:
| Type | Description |
|---|---|
|
of the form {key: value}, where key is a tuple and value is a tensor |
Source code in src/fjformer/checkpoint/streamer.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
load_flax_checkpoint(path, target=None, shard_fns=None)
staticmethod
Load a standard flax checkpoint that"s not saved with the msgpack streaming format.
Source code in src/fjformer/checkpoint/streamer.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
load_state_checkpoint(load_type, load_path, state_target=None, state_shard_fns=None, disallow_state=False, mismatch_allowed=True)
classmethod
The load_state_checkpoint function is used to checkpoint a checkpoint from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls |
Call the load_checkpoint function |
required | |
load_type |
Literal['state', 'state_params', 'params', 'flax_params']
|
Specify which part of state to checkpoint |
required |
load_path |
Union[str, PathLike]
|
Specify where to checkpoint the model from |
required |
state_target |
Specify the target for the train state |
None
|
|
state_shard_fns |
Specify the sharding function |
None
|
|
disallow_state |
Prevent loading the entire state |
False
|
|
mismatch_allowed |
bool
|
when ever to allow shard func to be None |
True
|
Returns:
| Type | Description |
|---|---|
|
A tuple of two objects, the state and restored_params |
Source code in src/fjformer/checkpoint/streamer.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
save_all(state, gather_fns, metadata=None, dataset=None, milestone=False)
The save_all function saves the following: - metadata.pkl (a pickle file containing a dictionary of metadata) - dataset.pkl (a pickle file containing the training data) - streaming_params_{step}.pkl or streaming_state_{step}.pkl (depending on whether we want to save optimizer state or not, this is a checkpoint that will not be overwritten by future checkpoints)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Access the attributes and methods of the class |
required | |
state |
PyTreeNode
|
struct.PyTreeNode: Save the current state of the model |
required |
gather_fns |
Gather the state of the optimizer |
required | |
metadata |
Save the metadata of the training |
None
|
|
dataset |
Save the dataset to disk |
None
|
|
milestone |
Determine whether the checkpoint is a milestone or not |
False
|
Returns:
| Type | Description |
|---|---|
|
Nothing |
Source code in src/fjformer/checkpoint/streamer.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
save_pickle(obj, filename)
The save_pickle function saves a Python object to disk using the pickle module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Represent the instance of the class |
required | |
obj |
Pass the object that is to be pickled |
required | |
filename |
Union[str, PathLike]
|
Specify the name of the file to be saved |
required |
Returns:
| Type | Description |
|---|---|
|
A pickle object |
Source code in src/fjformer/checkpoint/streamer.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |