xrapture.xrapture
LoraWeight
dataclass
Bases: ImplicitArray
Source code in src/fjformer/xrapture/xrapture.py
33 34 35 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 | |
__post_init__()
The post_init function is called after the init function. It allows us to check that the shapes of our parameters are correct, and if not, raise an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
| Type | Description |
|---|---|
|
The output of the super() |
Source code in src/fjformer/xrapture/xrapture.py
41 42 43 44 45 46 47 48 49 50 51 52 53 | |
get_scale()
The get_scale function returns the scale of the model. The scale is defined as alpha / number of columns in b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Represent the instance of the class |
required |
Returns:
| Type | Description |
|---|---|
|
The scale of the model |
Source code in src/fjformer/xrapture/xrapture.py
65 66 67 68 69 70 71 72 73 74 75 | |
materialize()
The materialize function is used to create a new matrix from the parameters of the factorization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Access the attributes and methods of a class |
required |
Returns:
| Type | Description |
|---|---|
|
The materialized vector |
Source code in src/fjformer/xrapture/xrapture.py
55 56 57 58 59 60 61 62 63 | |
XRapTure
Source code in src/fjformer/xrapture/xrapture.py
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 | |
apply_lora(module, parameters, tx, decision_fn=None, tune_vectors=False, rng=jax.random.PRNGKey(0), stddev=0.01, alpha=1.0, is_leaf=None)
The apply_lora function is a wrapper for the XRapTureModule class. It takes in a module, parameters, and an optimizer (tx) and returns an instance of the XRapTureModule class. The apply_lora function also allows you to specify whether you want to tune vectors as well as whether you want to use a decision function when tuning your parameters. The default behavior is that vectors are tuned using LORA while scalars are tuned using SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Access the attributes of the class |
required | |
module |
Union[Any, Module]
|
Any | flax.linen.Module: Specify the model that is being trained |
required |
parameters |
Union[dict, FrozenDict]
|
dict | flax.core.FrozenDict: Define the parameters of the model |
required |
tx |
GradientTransformation
|
optax.GradientTransformation: Specify the optimizer |
required |
decision_fn |
Optional[Callable]
|
Optional[Callable]: Decide whether to apply lora to a parameter or not |
None
|
tune_vectors |
bool
|
bool: Determine whether to tune the vectors or not |
False
|
rng |
PRNGKey
|
jax.random.PRNGKey: Set the random seed for the initialisation of parameters |
PRNGKey(0)
|
stddev |
float
|
float: Set the standard deviation of the initial weights |
0.01
|
alpha |
float
|
float: Control the variance of the gaussian distribution used to initialize |
1.0
|
is_leaf |
bool
|
bool: Determine if the node is a leaf or not |
None
|
Returns:
| Type | Description |
|---|---|
XRapTureModule
|
A XRaptureModule object |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
base_decision_function(path, params=None)
The base_decision_function function is used to determine which parameters are frozen, which are fine-tuned with LoRA, and which are fully fine-tuned. The function takes in a path to the parameter (e.g., "model/dense_layer/kernel") and returns an integer indicating how the parameter should be treated:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Refer to the object itself |
required | |
path |
list[DictKey]
|
list[jax.tree_util.DictKey]: Determine the path of the parameter in question |
required |
params |
Optional[Union[dict, PyTreeDef]]
|
dict | jax.tree_util.PyTreeDef | None: Specify the parameters of the model |
None
|
Returns:
| Type | Description |
|---|---|
|
The following: |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
init_lora_parameters(param_tree, lora_spec, dtype=jnp.float32, rng=jax.random.PRNGKey(0), stddev=0.01, alpha=1.0, is_leaf=None)
staticmethod
The init_lora_parameters function takes in a parameter tree, the lora_spec, and some other parameters. It then iterates through the parameter tree using jax.tree_util.tree_map_with_path to get each path and value of the parameter tree (which is just a nested dictionary). It then checks if that value is either LORA_FREEZE or LORA_FULL (these are constants defined above). If it's one of those two values, it returns the original parameter as-is; otherwise it creates a new LoraWeight object with random values for b
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_tree |
Specify the parameters of a neural network |
required | |
lora_spec |
Determine how many parameters to tune |
required | |
dtype |
dtype
|
jnp.dtype: Specify the data type of the parameters |
float32
|
rng |
PRNGKey
|
jax.random.PRNGKey: Generate random numbers |
PRNGKey(0)
|
stddev |
float
|
float: Initialize the weights of the network |
0.01
|
alpha |
float
|
float: Control the amount of regularization |
1.0
|
is_leaf |
bool
|
bool: Specify whether a node is a leaf or not |
None
|
Returns:
| Type | Description |
|---|---|
|
A tree of loraweight objects |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
make_lora_specs(parameters, decision_fn=None, tune_vectors=False)
The make_lora_specs function is used to create a dictionary of LORA specs for the parameters of a model. The function takes in two arguments:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self |
Allow the function to access other attributes and methods of the class |
required | |
parameters |
Union[dict, FrozenDict]
|
dict | flax.core.FrozenDict: Specify the parameters to be tuned |
required |
decision_fn |
Optional[Callable]
|
Optional[Callable]: Decide whether to freeze or unfreeze a parameter |
None
|
tune_vectors |
bool
|
bool: Determine if the vectors should be tuned or not |
False
|
|
Decide whether to freeze the parameter or not |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary of the same shape as the input parameters, |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
merge_parameters(lora_parameters, destructive=True)
staticmethod
The merge_parameters function is used to convert a LoraWeight into an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_parameters |
Pass in the parameters of the model |
required | |
destructive |
Determine whether to delete the original parameters or not |
True
|
|
|
Determine if the function is destructive or not |
required |
Returns:
| Type | Description |
|---|---|
|
The parameters of the model |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
wrap_tx(tx, lora_spec, scalar_frozen_grads=False)
staticmethod
The wrap_tx function takes a gradient transformation and wraps it in two freeze transformations. The first freezes all parameters that are marked as LORA_FREEZE, which is the default for LoraWeight objects. The second freezes the weights of all LoraWeight objects, regardless of their freeze status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tx |
GradientTransformation
|
optax.GradientTransformation: Pass in the optimizer |
required |
lora_spec |
Specify which parameters we want to freeze |
required | |
scalar_frozen_grads |
Determine whether to use scalar zeros or array zeros |
False
|
Returns:
| Type | Description |
|---|---|
|
A transformed version of the optimizer |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
eval_lora_transpose(primitive, arg, *, permutation)
The eval_lora_transpose function is used to transpose a LoraWeight object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primitive |
Determine which function to use |
required | |
arg |
LoraWeight
|
LoraWeight: Specify the type of input that is expected |
required |
* |
Indicate that the permutation parameter is a keyword-only argument |
required | |
permutation |
Specify the permutation of the weights |
required |
Returns:
| Type | Description |
|---|---|
|
A loraweight object with the same |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
handle_conv(primitive, inp, lora, *, dimension_numbers, **params)
The handle_conv function is a helper function that allows us to use LoraWeight objects as inputs to convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primitive |
Identify the function that is being called |
required | |
inp |
ArrayValue
|
ArrayValue: Specify the input to the convolution |
required |
lora |
LoraWeight
|
LoraWeight: Pass the loraweight object into the function |
required |
* |
Pass in the dimension_numbers parameter |
required | |
dimension_numbers |
Specify the convolution |
required | |
params |
Pass in the dimension_numbers parameter |
{}
|
Returns:
| Type | Description |
|---|---|
|
The result of the convolution |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
handle_dot_lhs(primitive, lora, rhs, *, dimension_numbers, **kwargs)
The handle_dot_lhs function is a JAX primitive that allows us to perform matrix multiplication on LoraWeights. It does this by first performing the matrix multiplication on the underlying weight tensor, and then adding in a second term that accounts for the fact that we are multiplying two low-rank matrices together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primitive |
Determine which function to use |
required | |
lora |
LoraWeight
|
LoraWeight: Pass the loraweight object to the function |
required |
rhs |
ArrayValue
|
ArrayValue: Pass the right hand side of the dot product |
required |
dimension_numbers |
Determine which dimensions are being contracted |
required | |
kwargs |
Pass the dimension_numbers to handle_dot_lhs |
{}
|
Returns:
| Type | Description |
|---|---|
|
The result of the dot product between |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
handle_dot_rhs(primitive, lhs, lora, *, dimension_numbers, **kwargs)
The handle_dot_rhs function is a partial application of the jax.lax.dot_general function, which takes in two arrays and returns their dot product (or matrix multiplication). The handle_dot_rhs function is used to handle the case where a LoraWeight object appears on the right-hand side of an equation. The handle_dot_rhs function takes in three arguments: primitive, lhs, and lora (the LoraWeight object). It then checks that the dimension numbers are correct for this operation using _check_dimension numbers(). If they are not correct it will return NotIm
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primitive |
Identify the function that is being called |
required | |
lhs |
Array
|
jax.Array: Store the left hand side of the dot product |
required |
lora |
LoraWeight
|
LoraWeight: Pass the loraweight object to the function |
required |
dimension_numbers |
Specify the dimensions of the input arrays |
required | |
kwargs |
Pass the dimension_numbers argument to handle_dot_rhs |
{}
|
Returns:
| Type | Description |
|---|---|
|
The output of the dot product with a loraweight |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
handle_gather(primitive, lora, indices, *, dimension_numbers, slice_sizes, **params)
The handle_gather function is a JAX primitive handler that allows us to perform the gather operation on LoraWeight objects. This function is called by JAX when it encounters a gather operation in the computation graph. The function takes as input:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primitive |
Identify the operation |
required | |
lora |
LoraWeight
|
LoraWeight: Pass the loraweight object to the function |
required |
indices |
Array
|
jax.Array: Select the rows of the weight matrix |
required |
dimension_numbers |
Specify the dimension numbers of |
required | |
slice_sizes |
Specify the size of each slice |
required | |
params |
Pass the dimension_numbers parameter to the gather function |
{}
|
Returns:
| Type | Description |
|---|---|
|
A new loraweight |
Source code in src/fjformer/xrapture/xrapture.py
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 | |
split_lora_params(params, lora_spec)
Map params to a pytree in which all LoraWeight.w values and all params marked with
LORA_FREEZE are replaced with EmptyNode. This is useful for checkpointing just
the trainable params.
Source code in src/fjformer/xrapture/xrapture.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |