IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python class

LoRA

LoRA​

class max.experimental.nn.LoRA(module, *, max_num_loras, max_lora_rank, max_lora_seq_len)

source

Bases: TransparentModule[(<class ‘max.experimental.tensor.Tensor’>,), Tensor]

Wraps a projection module with an additive multi-adapter LoRA term.

forward(x) keeps the wrapped module’s signature and returns module(x) plus, per ragged token group, the SGMV delta x @ lora_a[id].T @ lora_b[id].T. A fused q/k/v module runs the delta as one fused shrink + one fused expand (2 adapter slots) over a fused A/B weight that stacks the per-projection q/k/v adapters; a plain Linear uses the single-projection shrink/expand (also 2 slots). Adapters arrive through lora_a/lora_b (per-call inputs) and routing through set_lora_batch_info(); neither is a module parameter. Adapters are expected pre-scaled (lora_alpha / r).

It holds no Tensor parameters of its own, so as a TransparentModule it keeps the wrapped module’s native checkpoint paths.

Wrap a projection in place; the enclosing model drives routing and adapters each step via lora_layers() / lora_parameters():

from max.experimental.nn import Linear, LoRA

proj = LoRA(
    Linear(1024, 1024),
    max_num_loras=4,
    max_lora_rank=16,
    max_lora_seq_len=512,
)
#   proj.set_lora_batch_info(lora_ids, grouped_offsets, end_idx)
#   proj.lora_a.set(a_stack); proj.lora_b.set(b_stack)

Parameters:

  • module (Module[..., Any])
  • max_num_loras (int)
  • max_lora_rank (int)
  • max_lora_seq_len (int)

forward()​

forward(x)

source

Returns the base projection plus the per-request LoRA delta.

Parameters:

x (Tensor)

Return type:

Tensor

module​

module: Module[..., Any]

source

set_lora_batch_info()​

set_lora_batch_info(lora_ids, grouped_offsets, end_idx)

source

Wires this batch’s ragged routing in for the next forward.

Parameters:

Return type:

None