File size: 11,910 Bytes
8b187bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
MiniMind Export Utilities
Export models to ONNX, GGUF (llama.cpp), and other formats.
"""

import json
import struct
from typing import Optional, Dict, Any, List
from pathlib import Path
from dataclasses import dataclass, asdict

import torch
import torch.nn as nn


@dataclass
class ExportConfig:
    """Configuration for model export."""
    # ONNX settings
    opset_version: int = 17
    use_external_data: bool = False
    optimize_for_mobile: bool = True

    # GGUF settings
    gguf_quant_type: str = "Q4_K_M"  # Q4_0, Q4_K_M, Q5_K_M, Q8_0, F16
    gguf_use_mmap: bool = True

    # General
    max_seq_len: int = 2048
    batch_size: int = 1


def export_to_onnx(
    model: nn.Module,
    output_path: str,
    config: Optional[ExportConfig] = None,
    sample_input: Optional[torch.Tensor] = None,
) -> str:
    """
    Export model to ONNX format.

    Args:
        model: PyTorch model to export
        output_path: Path to save ONNX model
        config: Export configuration
        sample_input: Sample input tensor for tracing

    Returns:
        Path to exported model
    """
    config = config or ExportConfig()
    output_path = Path(output_path)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    model.eval()
    device = next(model.parameters()).device

    # Create sample input if not provided
    if sample_input is None:
        sample_input = torch.randint(
            0, 1000,
            (config.batch_size, config.max_seq_len),
            dtype=torch.long,
            device=device,
        )

    # Dynamic axes for variable sequence length
    dynamic_axes = {
        "input_ids": {0: "batch_size", 1: "sequence_length"},
        "logits": {0: "batch_size", 1: "sequence_length"},
    }

    # Wrapper to simplify output
    class ONNXWrapper(nn.Module):
        def __init__(self, model):
            super().__init__()
            self.model = model

        def forward(self, input_ids):
            _, logits, _, _ = self.model(input_ids)
            return logits

    wrapped_model = ONNXWrapper(model)

    # Export
    torch.onnx.export(
        wrapped_model,
        (sample_input,),
        str(output_path),
        opset_version=config.opset_version,
        input_names=["input_ids"],
        output_names=["logits"],
        dynamic_axes=dynamic_axes,
        do_constant_folding=True,
    )

    print(f"ONNX model exported to {output_path}")

    # Optimize for mobile if requested
    if config.optimize_for_mobile:
        try:
            import onnx
            from onnxruntime.transformers import optimizer

            optimized_path = output_path.with_suffix(".optimized.onnx")
            onnx_model = onnx.load(str(output_path))

            # Basic optimization
            from onnx import optimizer as onnx_optimizer
            passes = ["fuse_bn_into_conv", "fuse_consecutive_transposes"]
            optimized_model = onnx_optimizer.optimize(onnx_model, passes)
            onnx.save(optimized_model, str(optimized_path))

            print(f"Optimized ONNX model saved to {optimized_path}")
        except ImportError:
            print("Note: Install onnx and onnxruntime for optimization")

    return str(output_path)


# GGUF format constants
GGUF_MAGIC = 0x46554747  # "GGUF" in little endian
GGUF_VERSION = 3

GGUF_TYPE_UINT8 = 0
GGUF_TYPE_INT8 = 1
GGUF_TYPE_UINT16 = 2
GGUF_TYPE_INT16 = 3
GGUF_TYPE_UINT32 = 4
GGUF_TYPE_INT32 = 5
GGUF_TYPE_FLOAT32 = 6
GGUF_TYPE_BOOL = 7
GGUF_TYPE_STRING = 8
GGUF_TYPE_ARRAY = 9
GGUF_TYPE_UINT64 = 10
GGUF_TYPE_INT64 = 11
GGUF_TYPE_FLOAT64 = 12


class GGUFWriter:
    """Writer for GGUF format (llama.cpp compatible)."""

    def __init__(self, output_path: str):
        self.output_path = Path(output_path)
        self.metadata: Dict[str, Any] = {}
        self.tensors: List[Dict[str, Any]] = []

    def add_metadata(self, key: str, value: Any, value_type: int = None):
        """Add metadata key-value pair."""
        self.metadata[key] = {"value": value, "type": value_type}

    def add_tensor(self, name: str, tensor: torch.Tensor, quant_type: str = "F32"):
        """Add a tensor to be written."""
        self.tensors.append({
            "name": name,
            "data": tensor.cpu().numpy(),
            "quant_type": quant_type,
        })

    def _write_string(self, f, s: str):
        """Write a string in GGUF format."""
        encoded = s.encode("utf-8")
        f.write(struct.pack("<Q", len(encoded)))
        f.write(encoded)

    def _write_metadata_value(self, f, value: Any, value_type: int):
        """Write a metadata value."""
        f.write(struct.pack("<I", value_type))

        if value_type == GGUF_TYPE_UINT32:
            f.write(struct.pack("<I", value))
        elif value_type == GGUF_TYPE_INT32:
            f.write(struct.pack("<i", value))
        elif value_type == GGUF_TYPE_FLOAT32:
            f.write(struct.pack("<f", value))
        elif value_type == GGUF_TYPE_STRING:
            self._write_string(f, value)
        elif value_type == GGUF_TYPE_BOOL:
            f.write(struct.pack("<?", value))

    def write(self):
        """Write the GGUF file."""
        self.output_path.parent.mkdir(parents=True, exist_ok=True)

        with open(self.output_path, "wb") as f:
            # Header
            f.write(struct.pack("<I", GGUF_MAGIC))
            f.write(struct.pack("<I", GGUF_VERSION))
            f.write(struct.pack("<Q", len(self.tensors)))
            f.write(struct.pack("<Q", len(self.metadata)))

            # Metadata
            for key, meta in self.metadata.items():
                self._write_string(f, key)
                self._write_metadata_value(f, meta["value"], meta["type"])

            # Tensor info (headers)
            tensor_data_offset = f.tell()
            for tensor_info in self.tensors:
                self._write_string(f, tensor_info["name"])
                data = tensor_info["data"]

                # Number of dimensions
                f.write(struct.pack("<I", len(data.shape)))

                # Dimensions
                for dim in data.shape:
                    f.write(struct.pack("<Q", dim))

                # Data type (simplified - using F32 for now)
                f.write(struct.pack("<I", GGUF_TYPE_FLOAT32))

                # Offset (to be updated)
                f.write(struct.pack("<Q", 0))

            # Alignment padding
            alignment = 32
            current_pos = f.tell()
            padding = (alignment - (current_pos % alignment)) % alignment
            f.write(b"\x00" * padding)

            # Tensor data
            for tensor_info in self.tensors:
                data = tensor_info["data"].astype("float32")
                f.write(data.tobytes())

        print(f"GGUF model written to {self.output_path}")


def export_to_gguf(
    model: nn.Module,
    output_path: str,
    model_config: Any,
    config: Optional[ExportConfig] = None,
) -> str:
    """
    Export model to GGUF format for llama.cpp.

    Args:
        model: PyTorch model to export
        output_path: Path to save GGUF model
        model_config: Model configuration
        config: Export configuration

    Returns:
        Path to exported model
    """
    config = config or ExportConfig()
    writer = GGUFWriter(output_path)

    # Add model metadata
    writer.add_metadata("general.architecture", "mind2", GGUF_TYPE_STRING)
    writer.add_metadata("general.name", model_config.model_name, GGUF_TYPE_STRING)
    writer.add_metadata("mind2.context_length", model_config.max_position_embeddings, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.embedding_length", model_config.hidden_size, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.block_count", model_config.num_hidden_layers, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.attention.head_count", model_config.num_attention_heads, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.attention.head_count_kv", model_config.num_key_value_heads, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.rope.freq_base", model_config.rope_theta, GGUF_TYPE_FLOAT32)
    writer.add_metadata("mind2.expert_count", model_config.num_experts, GGUF_TYPE_UINT32)
    writer.add_metadata("mind2.expert_used_count", model_config.num_experts_per_tok, GGUF_TYPE_UINT32)

    # Add tokenizer metadata (placeholder)
    writer.add_metadata("tokenizer.ggml.model", "gpt2", GGUF_TYPE_STRING)

    # Export tensors
    state_dict = model.state_dict()
    tensor_name_map = {
        "model.embed_tokens.weight": "token_embd.weight",
        "model.norm.weight": "output_norm.weight",
        "lm_head.weight": "output.weight",
    }

    for name, tensor in state_dict.items():
        # Map tensor names to GGUF convention
        gguf_name = tensor_name_map.get(name, name)

        # Layer-specific mappings
        if "layers." in name:
            parts = name.split(".")
            layer_idx = parts[2]

            if "self_attn.q_proj" in name:
                gguf_name = f"blk.{layer_idx}.attn_q.weight"
            elif "self_attn.k_proj" in name:
                gguf_name = f"blk.{layer_idx}.attn_k.weight"
            elif "self_attn.v_proj" in name:
                gguf_name = f"blk.{layer_idx}.attn_v.weight"
            elif "self_attn.o_proj" in name:
                gguf_name = f"blk.{layer_idx}.attn_output.weight"
            elif "input_layernorm" in name:
                gguf_name = f"blk.{layer_idx}.attn_norm.weight"
            elif "post_attention_layernorm" in name:
                gguf_name = f"blk.{layer_idx}.ffn_norm.weight"
            elif "mlp.gate" in name:
                gguf_name = f"blk.{layer_idx}.ffn_gate.weight"
            elif "experts" in name:
                expert_idx = parts[4]
                if "gate_proj" in name:
                    gguf_name = f"blk.{layer_idx}.ffn_gate_exps.{expert_idx}.weight"
                elif "up_proj" in name:
                    gguf_name = f"blk.{layer_idx}.ffn_up_exps.{expert_idx}.weight"
                elif "down_proj" in name:
                    gguf_name = f"blk.{layer_idx}.ffn_down_exps.{expert_idx}.weight"

        writer.add_tensor(gguf_name, tensor)

    writer.write()
    return str(output_path)


def export_for_android(
    model: nn.Module,
    output_dir: str,
    model_config: Any,
    export_formats: List[str] = ["onnx", "gguf"],
) -> Dict[str, str]:
    """
    Export model in formats suitable for Android deployment.

    Args:
        model: PyTorch model
        output_dir: Output directory
        model_config: Model configuration
        export_formats: List of formats to export

    Returns:
        Dictionary mapping format to output path
    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    outputs = {}

    config = ExportConfig(
        optimize_for_mobile=True,
        max_seq_len=512,  # Shorter for mobile
    )

    if "onnx" in export_formats:
        onnx_path = output_dir / f"{model_config.model_name}.onnx"
        outputs["onnx"] = export_to_onnx(model, str(onnx_path), config)

    if "gguf" in export_formats:
        gguf_path = output_dir / f"{model_config.model_name}.gguf"
        outputs["gguf"] = export_to_gguf(model, str(gguf_path), model_config, config)

    # Create model info JSON for Android app
    model_info = {
        "model_name": model_config.model_name,
        "vocab_size": model_config.vocab_size,
        "hidden_size": model_config.hidden_size,
        "num_layers": model_config.num_hidden_layers,
        "num_heads": model_config.num_attention_heads,
        "max_seq_len": config.max_seq_len,
        "exports": {k: str(v) for k, v in outputs.items()},
    }

    info_path = output_dir / "model_info.json"
    with open(info_path, "w") as f:
        json.dump(model_info, f, indent=2)

    print(f"Model info saved to {info_path}")
    outputs["info"] = str(info_path)

    return outputs