etils.easystate
EasyDeLState
Bases: PyTreeNode
Source code in src/python/easydel/etils/easystate.py
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
|
__repr__()
The repr function is the "official" string representation of an object. It's what you get when you type the object name at the Python prompt, or pass it to str(). The goal of repr is to be unambiguous: if eval(repr(x)) == x, then repr should return a string that looks like a valid Python expression that could be used to recreate an object with the same value ( given an appropriate environment). If this is not possible, a string formatted using %s formatting is also acceptable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
Type | Description |
---|---|
A string that is a valid python expression |
Source code in src/python/easydel/etils/easystate.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
|
__str__()
The str function is called when you call str(object) or print(object). The repr function is called when you type the object name in the interpreter. If no str method exists, Python will use repr as a fallback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the object itself |
required |
Returns:
Type | Description |
---|---|
string |
Source code in src/python/easydel/etils/easystate.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
|
apply_gradients(*, grads, **kwargs)
The apply_gradients function is the core of the optimizer. It takes in a dictionary of gradients, and returns an updated version of itself with new parameters and state. The function also updates the step count.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the current instance of the class |
required | |
* |
Unpack the grads dictionary into positional arguments |
required | |
grads |
Pass in the gradients of the loss function with respect to each parameter |
required | |
kwargs |
Pass in additional arguments to the function |
{}
|
Returns:
Type | Description |
---|---|
A new State with the updated parameters and params |
Source code in src/python/easydel/etils/easystate.py
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 |
|
create(*, apply_fn, params, tx, tx_init=None, hyperparameters=None, module=None, module_config=None, module_config_args=None, **kwargs)
classmethod
The create function is used to create a new instance of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Create a new instance of the class |
required | |
* |
Pass a list of parameters to the function |
required | |
apply_fn |
Callable
|
Callable: Apply the model to a batch of data |
required |
params |
Union[FrozenDict[str, Any], Mapping[str, Any]]
|
core.FrozenDict[str,Any] | Mapping[str,Any]: Pass in the parameters of the model |
required |
tx |
GradientTransformation
|
optax.GradientTransformation: Initialize the optimizer |
required |
tx_init |
Optional[dict]
|
Optional[dict]: Initialize the optimizer |
None
|
hyperparameters |
Optional[dict]
|
Optional[dict]: Pass hyperparameters to the state for init |
None
|
module |
Optional[EasyDeLFlaxPretrainedModel]
|
Optional[EasyDeLFlaxPretrainedModel]: Pass the module to be used int state |
None
|
module_config |
Optional[EasyDeLPretrainedConfig]
|
Optional[EasyDeLPretrainedConfig]: Pass in the module config |
None
|
module_config_args |
Optional[dict]
|
Optional[dict]: Store the config args of the model |
None
|
kwargs |
Pass in additional parameters to the |
{}
|
Returns:
Type | Description |
---|---|
A EasyDeLState object |
Source code in src/python/easydel/etils/easystate.py
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 |
|
create_hyperparameters(model_type)
staticmethod
it's the only way we can dump xla compiler
Source code in src/python/easydel/etils/easystate.py
587 588 589 590 591 592 593 594 595 596 597 598 |
|
free_opt_state()
The free_opt_state function is used to free the memory allocated by a previous call to setopt. It should be called after all the options have been set, and before you perform any of the transfers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
Type | Description |
---|---|
EasyDeLState
|
A new state with the opt_state field set to none |
Source code in src/python/easydel/etils/easystate.py
386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
from_pretrained(pretrained_model_name_or_path, filename=None, optimizer='adamw', scheduler='none', tx_init=None, device=jax.devices('cpu')[0], dtype=jax.numpy.float32, param_dtype=jax.numpy.float32, precision=jax.lax.Precision('fastest'), sharding_axis_dims=(1, -1, 1, 1), sharding_axis_names=('dp', 'fsdp', 'tp', 'sp'), query_partition_spec=PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None), generation_query_partition_spec=PartitionSpec(('dp', 'fsdp'), 'tp', None, None), key_partition_spec=PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None), value_partition_spec=PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None), bias_partition_spec=PartitionSpec(('dp', 'fsdp'), None, None, None), generation_bias_partition_spec=PartitionSpec(('dp', 'fsdp'), None, None, None), attention_partition_spec=PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None), shard_attention_computation=True, input_shape=(1, 1), backend=None, init_optimizer_state=False, free_optimizer_state=True, verbose=True, state_shard_fns=None, config_kwargs=None, **kwargs)
classmethod
The from_pretrained function is a helper function to quickly load a pretrained model and its associated configuration.
This method takes care of returning the correct model class instance based on the model_type
property in the
config object, or when it's missing, falling back to using pattern matching on the
pretrained_model_name_or_path
string:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Refer to the class that is being defined |
required | |
pretrained_model_name_or_path |
str
|
str: Load the pretrained model |
required |
filename |
Optional[str]
|
Optional[str]: Specify the name of the file to download from huggingface hub |
None
|
optimizer |
AVAILABLE_OPTIMIZERS
|
AVAILABLE_OPTIMIZERS: Specify the optimizer used for training |
'adamw'
|
scheduler |
AVAILABLE_SCHEDULERS
|
AVAILABLE_SCHEDULERS: Specify the name of the scheduler to use |
'none'
|
tx_init |
Optional[dict]
|
Optional[dict]: Pass the hyperparameters of the optimizer |
None
|
device |
Specify the device on which to run the model |
devices('cpu')[0]
|
|
dtype |
dtype
|
jax.numpy.dtype: Specify the dtype of the model parameters |
float32
|
param_dtype |
dtype
|
jax.numpy.dtype: Specify the data type of the parameters |
float32
|
precision |
Optional[Precision]
|
jax.lax.Precision: Control the precision of the calculation |
Precision('fastest')
|
sharding_axis_dims |
Sequence[int]
|
Sequence[int]: Specify the dimension of each axis |
(1, -1, 1, 1)
|
sharding_axis_names |
Sequence[str]
|
Sequence[str]: Specify the names of the axes in each shard |
('dp', 'fsdp', 'tp', 'sp')
|
query_partition_spec |
PartitionSpec
|
PartitionSpec: Specify the partitioning of the query matrix |
PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None)
|
generation_query_partition_spec |
PartitionSpec
|
PartitionSpec: Specify the partitioning of the query tensor in generation process:param key_partition_spec: PartitionSpec: Specify the partitioning of the key matrix |
PartitionSpec(('dp', 'fsdp'), 'tp', None, None)
|
value_partition_spec |
PartitionSpec
|
PartitionSpec: Specify the partitioning of the value tensor |
PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None)
|
bias_partition_spec |
PartitionSpec
|
PartitionSpec: Specify the partitioning of the bias |
PartitionSpec(('dp', 'fsdp'), None, None, None)
|
attention_partition_spec |
PartitionSpec
|
PartitionSpec: Partition the attention weights |
PartitionSpec(('dp', 'fsdp'), 'sp', 'tp', None)
|
shard_attention_computation |
bool
|
bool: Determine whether to use shard_map or not |
True
|
input_shape |
Sequence[int]
|
Sequence[int]: Specify the shape of the input to be used for training |
(1, 1)
|
backend |
Optional[str]
|
Optional[str]: Specify the backend used for the model |
None
|
init_optimizer_state |
bool
|
bool: Initialize the optimizer state |
False
|
free_optimizer_state |
bool
|
bool: Free the optimizer state from memory |
True
|
verbose |
bool
|
bool: Print the progress of loading the model |
True
|
state_shard_fns |
Optional[Mapping[str, Callable]]
|
Optional[Mapping[str,Callable]]: Specify the function to use for sharding the state |
None
|
kwargs |
Pass keyword arguments to the function |
{}
|
|
config_kwargs |
Optional[Mapping[str, Any]]
|
Optional[Mapping[str, Any]]: Config kwargs to be added to config before creating module |
None
|
Returns:
Type | Description |
---|---|
EasyDeLState
|
An |
Source code in src/python/easydel/etils/easystate.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
|
init_opt_state()
The init_opt_state function initializes the optimizer state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Make the object callable, and params is used to pass in a dictionary of parameters |
required |
Returns:
Type | Description |
---|---|
EasyDeLState
|
A new instance of the class with opt_state initialized |
Source code in src/python/easydel/etils/easystate.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
load(*, apply_fn, params, step=0, opt_state=None, tx_init=None, hyperparameters=None, module=None, module_config=None, module_config_args=None, **kwargs)
classmethod
The load function is used to load a saved state of the Model and optimizer or Model Only.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Make the function a class method |
required | |
* |
Pass in a variable number of arguments |
required | |
step |
int
|
int: Keep track of the number of steps that have been taken |
0
|
apply_fn |
Callable
|
Callable: Apply the optimizer to the model |
required |
params |
Union[FrozenDict[str, Any], Mapping[str, Any]]
|
core.FrozenDict[str,Any] | Mapping[str,Any]: Pass in the parameters of the model |
required |
opt_state |
Optional[OptState]
|
Optional[optax.OptState]: optimizer state |
None
|
tx_init |
Optional[dict]
|
Optional[dict]: Pass the hyperparameters to the optimizer |
None
|
hyperparameters |
Optional[dict]
|
Optional[dict]: Load hyperparameters from the state dict |
None
|
module |
Optional[EasyDeLFlaxPretrainedModel]
|
Optional[EasyDeLFlaxPretrainedModel]: Pass in the module |
None
|
module_config |
Optional[EasyDeLPretrainedConfig]
|
Optional[EasyDeLPretrainedConfig]: Pass the module config |
None
|
module_config_args |
Optional[dict]
|
Optional[dict]: Pass the config_args to the model |
None
|
kwargs |
Pass in any additional parameters that may be needed for the model |
{}
|
Returns:
Type | Description |
---|---|
A new instance of the class |
Source code in src/python/easydel/etils/easystate.py
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 |
|
load_state(checkpoint_path, dtype=jnp.float32, param_dtype=jnp.float32, precision=None, init_optimizer_state=False, state_shard_fns=None, verbose=False, input_shape=(1, 1), config_kwargs=None)
classmethod
The load_state function is a class method that loads the state of an EasyDeLModel from a checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Create an instance of the class |
required | |
checkpoint_path |
Union[str, PathLike]
|
str | os.PathLike: Specify the path to the checkpoint file |
required |
dtype |
dtype
|
jnp.dtype: The dtype of the model |
float32
|
param_dtype |
dtype
|
jnp.dtype: The dtype of the model parameters |
float32
|
precision |
Optional[Union[str, Precision]]
|
Optional[Union[str, jax.lax.Precision]]: precision of the model |
None
|
init_optimizer_state |
bool
|
bool: Initialize the optimizer if it's not Initialized yet (if it Initialized the option will be ignored ) |
False
|
state_shard_fns |
Optional[Mapping[str, Callable]]
|
Optional[Mapping[str,Callable]]: Specify the function that will be used to shard the loaded state |
None
|
verbose |
bool
|
bool: Print out the progress of loading |
False
|
input_shape |
Tuple
|
Tuple: input_shape to init module |
(1, 1)
|
config_kwargs |
Optional[dict]
|
Optional[dict] : config kwargs to be passed to model config |
None
|
Returns:
Type | Description |
---|---|
A state object |
Source code in src/python/easydel/etils/easystate.py
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 |
|
save_state(filename, save_optimizer=False, checkpoint_dir=None, verbose=False, gather_fns=None, float_dtype=None)
The save_state function saves the state of a model to disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Pass the object itself to the function |
required | |
filename |
Union[str, PathLike]
|
str | os.PathLike: Specify the name of the file to save |
required |
save_optimizer |
bool
|
bool: Determine whether to save the optimizer state or not |
False
|
checkpoint_dir |
Optional[Union[str, PathLike]]
|
Optional[str | os.PathLike]: Specify the directory where the checkpoint is saved |
None
|
verbose |
bool
|
bool: Print out the path of the saved file |
False
|
gather_fns |
dict[Callable]
|
dict[Callable]: Specify a dictionary of functions that can be used to gather |
None
|
float_dtype |
Union[str, dtype]
|
str | jax.numpy.dtype: Specify the precision of the saved model |
None
|
|
Save the optimizer state |
required |
Returns:
Type | Description |
---|---|
None |
Source code in src/python/easydel/etils/easystate.py
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|