File size: 6,901 Bytes
d3cd5c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import math
from contextlib import contextmanager
from dataclasses import dataclass
from typing import List, Callable

import safetensors
import torch

from .layers import AttentionWeights, LayerNormWeights, LinearWeights, MLPWeights


@dataclass
class VisionBlock:
    ln1: LayerNormWeights
    attn: AttentionWeights
    ln2: LayerNormWeights
    mlp: MLPWeights


@dataclass
class VisionModel:
    patch_size: int
    patch_emb: LinearWeights
    pos_emb: torch.Tensor
    blocks: List[VisionBlock]
    post_ln: LayerNormWeights
    proj_mlp: MLPWeights


@dataclass
class TextBlock:
    ln: LayerNormWeights
    attn: AttentionWeights
    mlp: MLPWeights


@dataclass
class TextModel:
    wte: torch.Tensor
    blocks: List[TextBlock]
    post_ln: LayerNormWeights
    lm_head: LinearWeights


@dataclass
class MoondreamModel:
    vision: VisionModel
    text: TextModel


@contextmanager
def safetensors_open(safetensors_file: str):
    """
    Simplify interfacing with safetensors files. Eliminates the need to ignore
    type errors when using the `safe_open` function.
    """
    with safetensors.safe_open(
        safetensors_file, framework="pt"
    ) as st:  # pyright: ignore

        def get_tensor(name: str) -> torch.Tensor:
            return st.get_tensor(name)

        yield get_tensor


def load_model(
    get_tensor: Callable[[str], torch.Tensor],
    vision_blocks: int = 27,
    text_blocks: int = 24,
    vision_n_heads: int = 16,
    text_n_heads: int = 32,
) -> MoondreamModel:
    ## Vision encoder
    prefix = "vision_encoder.encoder.model.visual.patch_embed.linear"
    patch_emb = LinearWeights(
        weight=get_tensor(f"{prefix}.weight"), bias=get_tensor(f"{prefix}.bias")
    )
    patch_size = int(math.sqrt(patch_emb.weight.shape[1] // 3))
    pos_emb = get_tensor("vision_encoder.encoder.model.visual.pos_embed")
    post_ln = LayerNormWeights(
        weight=get_tensor("vision_encoder.encoder.model.visual.norm.weight"),
        bias=get_tensor("vision_encoder.encoder.model.visual.norm.bias"),
    )
    blocks = []
    for i in range(vision_blocks):
        prefix = f"vision_encoder.encoder.model.visual.blocks.{i}"
        blocks.append(
            VisionBlock(
                ln1=LayerNormWeights(
                    weight=get_tensor(f"{prefix}.norm1.weight"),
                    bias=get_tensor(f"{prefix}.norm1.bias"),
                ),
                attn=AttentionWeights(
                    qkv=LinearWeights(
                        weight=get_tensor(f"{prefix}.attn.qkv.weight"),
                        bias=get_tensor(f"{prefix}.attn.qkv.bias"),
                    ),
                    proj=LinearWeights(
                        weight=get_tensor(f"{prefix}.attn.proj.weight"),
                        bias=get_tensor(f"{prefix}.attn.proj.bias"),
                    ),
                    n_heads=vision_n_heads,
                ),
                ln2=LayerNormWeights(
                    weight=get_tensor(f"{prefix}.norm2.weight"),
                    bias=get_tensor(f"{prefix}.norm2.bias"),
                ),
                mlp=MLPWeights(
                    fc1=LinearWeights(
                        weight=get_tensor(f"{prefix}.mlp.fc1.weight"),
                        bias=get_tensor(f"{prefix}.mlp.fc1.bias"),
                    ),
                    fc2=LinearWeights(
                        weight=get_tensor(f"{prefix}.mlp.fc2.weight"),
                        bias=get_tensor(f"{prefix}.mlp.fc2.bias"),
                    ),
                ),
            )
        )
    proj_mlp = MLPWeights(
        fc1=LinearWeights(
            weight=get_tensor("vision_encoder.projection.mlp.fc1.weight"),
            bias=get_tensor("vision_encoder.projection.mlp.fc1.bias"),
        ),
        fc2=LinearWeights(
            weight=get_tensor("vision_encoder.projection.mlp.fc2.weight"),
            bias=get_tensor("vision_encoder.projection.mlp.fc2.bias"),
        ),
        act="gelu_approx",
    )
    vision = VisionModel(
        patch_size=patch_size,
        patch_emb=patch_emb,
        pos_emb=pos_emb,
        blocks=blocks,
        post_ln=post_ln,
        proj_mlp=proj_mlp,
    )

    ## Text decoder model
    wte = get_tensor("text_model.transformer.embd.wte.weight")
    post_ln = LayerNormWeights(
        weight=get_tensor("text_model.lm_head.ln.weight"),
        bias=get_tensor("text_model.lm_head.ln.bias"),
    )
    lm_head = LinearWeights(
        weight=get_tensor("text_model.lm_head.linear.weight"),
        bias=get_tensor("text_model.lm_head.linear.bias"),
    )
    blocks = []
    for i in range(text_blocks):
        prefix = f"text_model.transformer.h.{i}"
        blocks.append(
            TextBlock(
                ln=LayerNormWeights(
                    weight=get_tensor(f"{prefix}.ln.weight"),
                    bias=get_tensor(f"{prefix}.ln.bias"),
                ),
                attn=AttentionWeights(
                    qkv=LinearWeights(
                        weight=get_tensor(f"{prefix}.mixer.Wqkv.weight"),
                        bias=get_tensor(f"{prefix}.mixer.Wqkv.bias"),
                    ),
                    proj=LinearWeights(
                        weight=get_tensor(f"{prefix}.mixer.out_proj.weight"),
                        bias=get_tensor(f"{prefix}.mixer.out_proj.bias"),
                    ),
                    n_heads=text_n_heads,
                ),
                mlp=MLPWeights(
                    fc1=LinearWeights(
                        weight=get_tensor(f"{prefix}.mlp.fc1.weight"),
                        bias=get_tensor(f"{prefix}.mlp.fc1.bias"),
                    ),
                    fc2=LinearWeights(
                        weight=get_tensor(f"{prefix}.mlp.fc2.weight"),
                        bias=get_tensor(f"{prefix}.mlp.fc2.bias"),
                    ),
                    act="gelu_approx",
                ),
            )
        )
    text = TextModel(wte=wte, blocks=blocks, post_ln=post_ln, lm_head=lm_head)

    return MoondreamModel(vision=vision, text=text)


def load_from_safetensors(
    safetensors_file: str,
    vision_blocks: int = 27,
    text_blocks: int = 24,
    **kwargs,
) -> MoondreamModel:
    with safetensors_open(safetensors_file) as get_tensor:
        return load_model(get_tensor, vision_blocks, text_blocks, **kwargs)


def load_from_pt(
    pt_file: str,
    vision_blocks: int = 27,
    text_blocks: int = 24,
    **kwargs,
) -> MoondreamModel:
    device = str(torch.empty(0).device)
    tensors = torch.load(pt_file, map_location=device, weights_only=True)
    tensors = {
        k.replace("._orig_mod", ""): v.to(dtype=torch.float16)
        for k, v in tensors.items()
    }
    return load_model(lambda x: tensors[x], vision_blocks, text_blocks, **kwargs)


if __name__ == "__main__":
    weights = load_from_safetensors("model.safetensors")
    print(weights)