File size: 2,534 Bytes
dea9139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Convert the PRATIBHA-2 release (MannasAI/pratibha-2, revision 863edc9) to
braindecode's ``Pratibha2``.

usage: python convert.py <model.safetensors> <out_dir>

Renames the encoder keys to braindecode's, splits each fused feed-forward input
projection ``ffn.in_proj`` (value rows, then gate rows) into ``mlp.fc1`` and
``mlp.fc_gate``, and drops ``pos_enc.freq_matrix``, which the model rebuilds.
``<out_dir>`` gets ``config.json`` and ``model.safetensors`` from
``Pratibha2.save_pretrained`` for the 10-20 montage below, 5 s windows and
``n_outputs=2``. The head is not part of the release: it is PyTorch's default
init under ``torch.manual_seed(0)``; ``from_pretrained(..., n_outputs=k)``
replaces it.
"""

import sys

import torch
from safetensors.torch import load_file

from braindecode.models import Pratibha2

RENAMES = [
    ("patch_embed.linear.", "encoder.patch_embed."),
    ("pos_enc.learned_linear.0.", "pos_enc.learned_linear."),
    ("pos_enc.learned_linear.2.", "pos_enc.learned_norm."),
    ("pos_enc.", "encoder.pos_enc."),
    ("encoder.layers.", "encoder.blocks."),
    (".pre_attn_norm.", ".norm1."),
    (".attn.qkv_proj.", ".attn.qkv."),
    (".attn.out_proj.", ".attn.proj."),
    (".pre_ffn_norm.", ".norm2."),
    (".ffn.out_proj.", ".mlp.fc2."),
]
MONTAGE = "Fp1 Fp2 F7 F3 Fz F4 F8 T7 C3 Cz C4 T8 P7 P3 Pz P4 P8 O1 O2".split()


def convert_state_dict(state_dict):
    out = {}
    for key, value in state_dict.items():
        if key == "pos_enc.freq_matrix":
            continue
        for old, new in RENAMES:
            key = key.replace(old, new)
        if ".ffn.in_proj." in key:
            value_rows, gate_rows = value.chunk(2)
            out[key.replace(".ffn.in_proj.", ".mlp.fc1.")] = value_rows.contiguous()
            out[key.replace(".ffn.in_proj.", ".mlp.fc_gate.")] = gate_rows.contiguous()
        else:
            out[key] = value
    return out


if __name__ == "__main__":
    src, out_dir = sys.argv[1:]
    original = load_file(src)
    torch.manual_seed(0)
    model = Pratibha2(
        chs_info=[{"ch_name": name} for name in MONTAGE],
        n_outputs=2,
        n_times=1000,
        sfreq=200.0,
    )
    torch.testing.assert_close(
        original["pos_enc.freq_matrix"], model.encoder.pos_enc.freq_matrix
    )
    missing, unexpected = model.load_state_dict(
        convert_state_dict(original), strict=False
    )
    assert not unexpected, unexpected
    assert all(key.startswith("final_layer.") for key in missing), missing
    model.save_pretrained(out_dir)