lora

Lora layer and utility code adapted from https://github.com/rasbt/LLMs-from-scratch/blob/main/appendix-E/01_main-chapter-code/appendix-E.ipynb

class mfai.pytorch.models.llms.lora.LinearWithLoRA(linear, rank, alpha)[source]

Bases: Module

Parameters:
forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:

x (Tensor)

Return type:

Tensor

class mfai.pytorch.models.llms.lora.LoRALayer(in_dim, out_dim, rank, alpha)[source]

Bases: Module

Parameters:
forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:

x (Tensor)

Return type:

Tensor

mfai.pytorch.models.llms.lora.replace_linear_with_lora(model, rank, alpha)[source]
Return type:

None

Parameters:
mfai.pytorch.models.llms.lora.setup_model_for_lora(model, rank, alpha)[source]
Return type:

None

Parameters:
  1. Print total number of trainable params before lora replacement

  2. Freeze all model parameters

  3. replace Linear Layers by LinearWithLora layers (not frozen)

  4. Print total number of trainable params after lora replacement