Low-Rank Adaptation (LoRA) is a parameter-efficient fine-tuning technique. Instead of updating all weights of a pre-trained Transformer, LoRA freezes the original weights and injects a pair of small trainable low-rank matrices A (shape d x r) and B (shape r x d) into selected weight matrices of each Transformer layer. The effective update to a weight matrix W is then B @ A.
Given a Transformer specification, write a function that computes:
Inputs to the function:
num_layers (int): number of Transformer layers L.d_model (int): hidden dimension d.num_target_matrices (int): number of weight matrices per layer that LoRA is applied to (e.g. 2 if only W_q and W_v are adapted).rank (int): LoRA rank r.Assume every targeted weight matrix is square of shape (d_model, d_model). Return a dictionary with the integer keys lora_params and full_params, and a float key compression_ratio equal to full_params / lora_params.