{"record":{"id":"920daa6ce87ca1ad","repo":"unslothai/unsloth","slug":"activation-rotation-target-fqn-r-is-already-rota","errorCode":null,"errorMessage":"activation rotation target {fqn!r} is already rotated","messagePattern":"activation rotation target (.+?) is already rotated","errorType":"exception","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"studio/backend/core/inference/diffusion_convrot.py","lineNumber":362,"sourceCode":"        raise ValueError(problem)\n\n    from torch import nn\n\n    group_size = int(metadata[ROTATION_GROUP_KEY])\n    fqns = list(metadata[ROTATION_FQNS_KEY])\n    modules = dict(transformer.named_modules())\n    missing = [fqn for fqn in fqns if fqn not in modules]\n    if missing:\n        raise ValueError(\n            f\"activation rotation names {len(missing)} fqn(s) this model does not have \"\n            f\"(e.g. {missing[0]!r}); the checkpoint and this build disagree about the model\"\n        )\n    for fqn in fqns:\n        module = modules[fqn]\n        if not isinstance(module, nn.Linear):\n            raise ValueError(f\"activation rotation target {fqn!r} is not an nn.Linear\")\n        if is_rotated_linear(module):\n            raise ValueError(f\"activation rotation target {fqn!r} is already rotated\")\n        if module.in_features % group_size:\n            raise ValueError(\n                f\"activation rotation target {fqn!r} has in_features {module.in_features}, \"\n                f\"which the recorded group {group_size} does not divide\"\n            )\n    # Every target is validated before ANY is swapped. A partial install is the one outcome worse\n    # than either end state: the rotated half still renders, just wrongly, so there is nothing to\n    # notice and nothing to fall back from.\n    for fqn in fqns:\n        _install_rotation(modules[fqn], group_size)\n    try:\n        setattr(\n            transformer,\n            CONVROT_ATTR,\n            {\"kind\": CONVROT_KIND, \"group\": group_size, \"linears\": len(fqns)},\n        )\n    except Exception:  # noqa: BLE001 -- the marker is a diagnostic, never the mechanism\n        pass","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/core/inference/diffusion_convrot.py#L344-L380","documentation":"At load time, a target Linear is already a rotated Linear (is_rotated_linear returned true), meaning the online rotation has been installed twice or the module came pre-rotated. Double rotation would compute x @ H @ H instead of x @ H and silently corrupt outputs, so it is refused.","triggerScenarios":"Calling apply_activation_rotation twice on the same transformer (e.g. a retry loop or a second load path re-applying metadata); or loading weights into a transformer that already had _install_rotation run — including the meta-device retry path that re-applies after a rebuild.","commonSituations":"Retrying a failed load without rebuilding the model; a code change that moved apply_activation_rotation into a helper invoked from two places; checkpoint contains already-rotated modules plus metadata asking to rotate again.","solutions":["Apply the rotation exactly once per model instance: guard with is_rotated_linear(module) before calling","Rebuild the transformer from scratch (fresh from_pretrained/config) before retrying a failed rotated load","Audit for duplicate call sites of apply_activation_rotation in the load path"],"exampleFix":"# before\napply_activation_rotation(transformer, metadata)  # called again on retry\n\n# after\nif any(is_rotated_linear(m) for m in transformer.modules()):\n    transformer = rebuild_fresh_model(config)  # or skip re-applying\napply_activation_rotation(transformer, metadata)","handlingStrategy":"type-guard","validationCode":"from studio.backend.core.inference.diffusion_convrot import is_rotated_linear\n\ndef needs_rotation(transformer, fqns) -> bool:\n    mods = dict(transformer.named_modules())\n    return any(not is_rotated_linear(mods[fqn]) for fqn in fqns)","typeGuard":"from studio.backend.core.inference.diffusion_convrot import is_rotated_linear\n\ndef is_rotated(module) -> bool:\n    \"\"\"True if the online rotation is already installed on this module.\"\"\"\n    return is_rotated_linear(module)","tryCatchPattern":"try:\n    apply_activation_rotation(transformer, metadata)\nexcept ValueError as e:\n    if \"already rotated\" in str(e):\n        rebuild_model_and_retry()  # fresh instance, then apply once\n    raise","preventionTips":["Apply rotation exactly once per model instance; keep one call site in the load path","On any load failure, rebuild the transformer from scratch before retrying — never reuse a half-loaded one","Remember x @ H twice is not identity; double rotation corrupts output silently"],"tags":["convrot","double-apply","state-mismatch","quantization"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}