Skip to content

NameError in Unsloth-generated Linear_peft_forward.py: VARIANT_KWARG_KEYS not defined #128

@Nyovelt

Description

@Nyovelt

Bug Report

  • Summary: The Unsloth-generated Linear_peft_forward.py references VARIANT_KWARG_KEYS when constructing variant_kwargs, but the constant is never imported or defined, so every LoRA Linear forward immediately crashes with NameError.
  • Environment: Python 3.10, torch ≥2.8.0, transformers 4.56.2, latest unsloth/unsloth_zoo from Git (ROCm workstation, but this is a pure-Python failure).
  • GPU: AMD RYZEN AI MAX+ 395 w/ Radeon 8060S, rocm 7.0+,
  • Steps to Reproduce:
    1. Generate any LoRA-wrapped model with Unsloth (e.g., run examples/gpt_oss_(20B)_Reinforcement_Learning_2048_Game_BF16.py).
    2. When the decoder calls a LoRA projection, unsloth_forward in Linear_peft_forward.py executes.
    3. The function evaluates variant_kwargs = {k: kwargs.pop(k, None) for k in VARIANT_KWARG_KEYS} and Python raises NameError, halting the run.
  • Minimal Reproduction (standalone):
import textwrap, types, torch

LINEAR_SOURCE = textwrap.dedent(
    """
    import torch
    def unsloth_forward(self, x, *args, **kwargs):
        variant_kwargs = {k: kwargs.pop(k, None) for k in VARIANT_KWARG_KEYS}
        return self.base_layer(x, *args, **kwargs)
    """
)

mod = types.ModuleType("broken_linear")
exec(LINEAR_SOURCE, mod.__dict__)

class Dummy:
    disable_adapters = False
    merged = False
    active_adapters = []
    lora_A = lora_B = lora_dropout = {}
    scaling = {}
    lora_variant = {}
    def base_layer(self, x, *args, **kwargs):
        return x

mod.unsloth_forward(Dummy(), torch.zeros(1, 1))

Running this script reproduces the NameError.

  • Expected Result: variant_kwargs should be built successfully so the base layer forward completes.
  • Actual Result: NameError prevents any LoRA projection from running.
  • Proposed Fix: Have the generator import VARIANT_KWARG_KEYS from peft.tuners.lora.layer, fallback to peft.tuners.lora.bnb, or default to ["alora_offsets"] if neither import is available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions