File size: 3,507 Bytes
3922a1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Inject LoRA into the vendor's REAL training entrypoint.

Why: the vendor's own recipe (tasks/vla/train_lingbotvla.py) is full-parameter
fine-tune only - grep finds zero LoRA/PEFT wiring anywhere in it. LoRA support
DOES exist in the codebase (lingbotvla/utils/lora_utils.py, peft==0.15.2 is
even a dependency) but is only called by openroboto CLI's own bundled
single-process runner - which uses a DIFFERENT, simplified data pipeline (a
decoded JSON episode list) rather than the vendor's real LeRobot dataset
loader. Converting our real 1693-episode dataset into that JSON format would
mean re-deriving a data pipeline that already works. Patching LoRA into the
real entrypoint instead keeps the already-verified real data loading, real
model construction, and real depth/video alignment losses, changing only
which parameters are trainable.

Insertion point: right after the model is built and its param stats are
logged, before FSDP wrapping and before the optimizer is constructed. At that
point `model` is a plain nn.Module (not yet parallelized), which is exactly
what add_lora_to_model expects.

Target modules and the r=32/alpha=64 pair are not invented here - they come
from openroboto-cli's own train_runner.py, which documents having verified on
a real GPU that q_proj/k_proj/v_proj/o_proj matches 396 real tensors (38.9M
trainable params), and that the lora_utils.add_lora_to_model signature
default ("q,k,v,o,ffn.0,ffn.2") does NOT match this architecture and raises
ValueError if used as-is.

The optimizer needs no change: split_muon_adamw_params already skips any
param with requires_grad=False, so freezing everything but the LoRA adapters
automatically means only the adapters get optimized.
"""
import ast

TRAIN = "/app/lingbot-vla-v2/tasks/vla/train_lingbotvla.py"

LORA_RANK = 32
LORA_ALPHA = 64
LORA_TARGET_MODULES = "q_proj,k_proj,v_proj,o_proj"

anchor = "    log_model_param_stats(model)\n\n    model_config = model.config"
if anchor not in open(TRAIN).read():
    raise SystemExit("anchor not found - vendor code changed, patch needs updating")

injection = """    log_model_param_stats(model)

    from lingbotvla.utils.lora_utils import add_lora_to_model, freeze_parameters
    _lora_targets = "{targets}".split(",")
    freeze_parameters(model)
    add_lora_to_model(
        model,
        lora_rank={rank},
        lora_alpha={alpha},
        lora_target_modules="{targets}",
        lora_target_modules_support=_lora_targets,
    )
    # add_lora_to_model has no return statement (it mutates `model` in place
    # via inject_adapter_in_model, which itself modifies its argument
    # in-place) - capturing a return value here overwrites `model` with None.
    _n_trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    _n_total = sum(p.numel() for p in model.parameters())
    logger.info_rank0(
        f"LoRA r={rank} alpha={alpha} targets={{_lora_targets}}: "
        f"trainable {{_n_trainable/1e6:.1f}}M / {{_n_total/1e9:.2f}}B "
        f"({{100*_n_trainable/_n_total:.3f}}%)"
    )
    if _n_trainable == 0:
        raise RuntimeError("LoRA injection left nothing trainable - target modules did not match")

    model_config = model.config""".format(rank=LORA_RANK, alpha=LORA_ALPHA, targets=LORA_TARGET_MODULES)

src = open(TRAIN).read()
src = src.replace(anchor, injection, 1)
open(TRAIN, "w").write(src)

ast.parse(open(TRAIN).read())
print(f"OK LoRA injection patched in: r={LORA_RANK} alpha={LORA_ALPHA} targets={LORA_TARGET_MODULES}")