kizzasi-model 0.2.1

Model architectures for Kizzasi AGSP - Mamba, RWKV, S4, Transformer
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! ONNX model export for kizzasi-model
//!
//! Serializes kizzasi model weights and architecture to ONNX protobuf format
//! using manual byte encoding (Pure Rust approach — no prost, no build.rs).
//!
//! ## ONNX Protobuf Reference
//! <https://github.com/onnx/onnx/blob/main/onnx/onnx.proto3>
//!
//! Wire types used:
//! - 0: varint (int32, int64, bool, enum)
//! - 2: length-delimited (string, bytes, embedded messages, repeated fields)
//! - 5: 32-bit (float)

use crate::{ModelError, ModelResult};
use std::collections::HashMap;

// ============================================================
// Minimal protobuf encoder
// ============================================================

/// Minimal protobuf wire-format encoder.
///
/// All functions produce raw bytes that can be concatenated to form
/// a valid protobuf message. This module intentionally exposes its
/// functions as `pub` so the tests can unit-test individual primitives.
pub mod proto {
    /// Encode an unsigned 64-bit integer as a varint.
    pub fn encode_varint(value: u64) -> Vec<u8> {
        let mut buf = Vec::with_capacity(10);
        let mut v = value;
        loop {
            if v < 0x80 {
                buf.push(v as u8);
                break;
            }
            buf.push((v as u8 & 0x7F) | 0x80);
            v >>= 7;
        }
        buf
    }

    /// Build a field tag byte sequence: `(field << 3) | wire_type`.
    pub fn field_tag(field: u32, wire_type: u32) -> Vec<u8> {
        encode_varint(((field as u64) << 3) | wire_type as u64)
    }

    /// Encode a `string` field (wire type 2).
    pub fn encode_string(field: u32, s: &str) -> Vec<u8> {
        let bytes = s.as_bytes();
        let mut out = field_tag(field, 2);
        out.extend(encode_varint(bytes.len() as u64));
        out.extend_from_slice(bytes);
        out
    }

    /// Encode a `bytes` field (wire type 2).
    pub fn encode_bytes(field: u32, b: &[u8]) -> Vec<u8> {
        let mut out = field_tag(field, 2);
        out.extend(encode_varint(b.len() as u64));
        out.extend_from_slice(b);
        out
    }

    /// Encode an `int32` field (wire type 0).
    ///
    /// Returns empty bytes when the value is zero (default protobuf behaviour).
    pub fn encode_i32(field: u32, v: i32) -> Vec<u8> {
        if v == 0 {
            return vec![];
        }
        let mut out = field_tag(field, 0);
        out.extend(encode_varint(v as i64 as u64));
        out
    }

    /// Encode an `int64` field (wire type 0).
    ///
    /// Returns empty bytes when the value is zero (default protobuf behaviour).
    pub fn encode_i64(field: u32, v: i64) -> Vec<u8> {
        if v == 0 {
            return vec![];
        }
        let mut out = field_tag(field, 0);
        out.extend(encode_varint(v as u64));
        out
    }

    /// Encode a `float` field (wire type 5 = 32-bit little-endian).
    pub fn encode_f32(field: u32, v: f32) -> Vec<u8> {
        let mut out = field_tag(field, 5);
        out.extend_from_slice(&v.to_le_bytes());
        out
    }

    /// Encode an embedded sub-message (wire type 2).
    ///
    /// Returns empty bytes when `msg` is empty.
    pub fn encode_submessage(field: u32, msg: &[u8]) -> Vec<u8> {
        if msg.is_empty() {
            return vec![];
        }
        let mut out = field_tag(field, 2);
        out.extend(encode_varint(msg.len() as u64));
        out.extend_from_slice(msg);
        out
    }

    /// Encode a `[]f32` slice as `raw_data` bytes (field, wire type 2).
    ///
    /// Uses the efficient `raw_data` approach (field 9 in `TensorProto`)
    /// rather than per-element `float_data`.
    pub fn encode_float_slice_as_raw(field: u32, floats: &[f32]) -> Vec<u8> {
        let bytes: Vec<u8> = floats.iter().flat_map(|f| f.to_le_bytes()).collect();
        encode_bytes(field, &bytes)
    }

    /// Encode repeated `int64` values as a packed field (wire type 2).
    ///
    /// Returns empty bytes when `vals` is empty.
    pub fn encode_packed_i64(field: u32, vals: &[i64]) -> Vec<u8> {
        if vals.is_empty() {
            return vec![];
        }
        let mut inner = Vec::new();
        for &v in vals {
            inner.extend(encode_varint(v as u64));
        }
        encode_bytes(field, &inner)
    }

    /// Encode repeated `float` values as a packed field (wire type 2).
    ///
    /// Returns empty bytes when `vals` is empty.
    pub fn encode_packed_f32(field: u32, vals: &[f32]) -> Vec<u8> {
        if vals.is_empty() {
            return vec![];
        }
        // packed floats: concatenate 4-byte LE representations
        let mut inner = Vec::with_capacity(vals.len() * 4);
        for &v in vals {
            inner.extend_from_slice(&v.to_le_bytes());
        }
        encode_bytes(field, &inner)
    }
}

// ============================================================
// ONNX data type constants (TensorProto.DataType)
// ============================================================

/// ONNX data type constants (from `onnx.proto` `TensorProto.DataType`).
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(i32)]
pub enum OnnxDataType {
    /// 32-bit IEEE floating point
    Float = 1,
    /// 8-bit signed integer
    Int8 = 3,
    /// 32-bit signed integer
    Int32 = 6,
    /// 64-bit signed integer
    Int64 = 7,
    /// 16-bit IEEE floating point (half)
    Float16 = 10,
    /// 64-bit IEEE floating point (double)
    Double = 11,
}

impl OnnxDataType {
    /// Return the protobuf enum integer value.
    pub fn as_i32(self) -> i32 {
        self as i32
    }
}

// ============================================================
// ONNX graph element types
// ============================================================

/// A single ONNX tensor initializer (weight blob).
#[derive(Debug, Clone)]
pub struct OnnxTensor {
    /// Tensor name (used to reference this weight in the graph).
    pub name: String,
    /// Shape dimensions.
    pub dims: Vec<i64>,
    /// Element data type.
    pub data_type: OnnxDataType,
    /// Float values (only populated when `data_type == Float`).
    pub float_data: Vec<f32>,
}

impl OnnxTensor {
    /// Serialize to `TensorProto` bytes.
    ///
    /// Field numbers (onnx.proto3 `TensorProto`):
    /// - dims:      1 (int64 repeated, packed)
    /// - data_type: 2 (int32)
    /// - name:      8 (string)
    /// - raw_data:  9 (bytes)
    fn to_proto_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // field 1: dims (packed int64)
        buf.extend(proto::encode_packed_i64(1, &self.dims));
        // field 2: data_type
        buf.extend(proto::encode_i32(2, self.data_type.as_i32()));
        // field 8: name
        buf.extend(proto::encode_string(8, &self.name));
        // field 9: raw_data (f32 little-endian blob)
        if !self.float_data.is_empty() {
            buf.extend(proto::encode_float_slice_as_raw(9, &self.float_data));
        }
        buf
    }
}

// ============================================================
// ONNX value info
// ============================================================

/// ONNX value info (input/output declaration).
#[derive(Debug, Clone)]
pub struct OnnxValueInfo {
    /// Tensor name.
    pub name: String,
    /// Element data type.
    pub data_type: OnnxDataType,
    /// Shape dimensions. `None` means a dynamic (symbolic) dimension.
    pub shape: Vec<Option<i64>>,
}

impl OnnxValueInfo {
    /// Serialize to `ValueInfoProto` bytes.
    ///
    /// Field numbers:
    /// - name: 1 (string)
    /// - type: 2 (TypeProto)
    ///
    /// `TypeProto`:
    /// - tensor_type: 1 (TypeProto.Tensor)
    ///
    /// `TypeProto.Tensor`:
    /// - elem_type: 1 (int32)
    /// - shape:     2 (TensorShapeProto)
    ///
    /// `TensorShapeProto`:
    /// - dim: 1 (Dimension repeated)
    ///
    /// `TensorShapeProto.Dimension`:
    /// - dim_value: 1 (int64)  — static
    /// - dim_param: 2 (string) — dynamic (symbolic)
    fn to_proto_bytes(&self) -> Vec<u8> {
        // Build TensorShapeProto
        let mut shape_buf = Vec::new();
        for dim_opt in &self.shape {
            let dim_bytes = match dim_opt {
                Some(v) => {
                    // dim_value: field 1 int64
                    proto::encode_i64(1, *v)
                }
                None => {
                    // dim_param: field 2 string (use "?" as placeholder)
                    proto::encode_string(2, "?")
                }
            };
            shape_buf.extend(proto::encode_submessage(1, &dim_bytes));
        }

        // Build TypeProto.Tensor
        let mut tensor_type_buf = Vec::new();
        // elem_type: field 1 int32
        tensor_type_buf.extend(proto::encode_i32(1, self.data_type.as_i32()));
        // shape: field 2 TensorShapeProto
        if !shape_buf.is_empty() {
            tensor_type_buf.extend(proto::encode_submessage(2, &shape_buf));
        }

        // Build TypeProto (tensor_type is field 1)
        let type_proto_buf = proto::encode_submessage(1, &tensor_type_buf);

        // Build ValueInfoProto
        let mut buf = Vec::new();
        buf.extend(proto::encode_string(1, &self.name));
        buf.extend(proto::encode_submessage(2, &type_proto_buf));
        buf
    }
}

// ============================================================
// ONNX attribute
// ============================================================

/// Attribute type enum values (AttributeProto.AttributeType).
#[repr(i32)]
enum AttributeType {
    Int = 1,
    Float = 4,
    String = 3,
    Ints = 7,
    Floats = 6,
}

/// An ONNX operator attribute.
#[derive(Debug, Clone)]
pub enum OnnxAttribute {
    /// Integer attribute.
    Int(String, i64),
    /// Float attribute.
    Float(String, f32),
    /// String/bytes attribute.
    String(String, Vec<u8>),
    /// Repeated integer attribute.
    Ints(String, Vec<i64>),
    /// Repeated float attribute.
    Floats(String, Vec<f32>),
}

impl OnnxAttribute {
    /// Serialize to `AttributeProto` bytes.
    ///
    /// Field numbers:
    /// - name:   1 (string)
    /// - i:      3 (int64)
    /// - f:      4 (float)
    /// - s:      8 (bytes)
    /// - floats: 6 (repeated float, packed)
    /// - ints:   7 (repeated int64, packed)
    /// - type:  20 (int32, AttributeType enum)
    fn to_proto_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        match self {
            OnnxAttribute::Int(name, v) => {
                buf.extend(proto::encode_string(1, name));
                buf.extend(proto::encode_i64(3, *v));
                buf.extend(proto::encode_i32(20, AttributeType::Int as i32));
            }
            OnnxAttribute::Float(name, v) => {
                buf.extend(proto::encode_string(1, name));
                buf.extend(proto::encode_f32(4, *v));
                buf.extend(proto::encode_i32(20, AttributeType::Float as i32));
            }
            OnnxAttribute::String(name, v) => {
                buf.extend(proto::encode_string(1, name));
                buf.extend(proto::encode_bytes(8, v));
                buf.extend(proto::encode_i32(20, AttributeType::String as i32));
            }
            OnnxAttribute::Ints(name, vals) => {
                buf.extend(proto::encode_string(1, name));
                buf.extend(proto::encode_packed_i64(7, vals));
                buf.extend(proto::encode_i32(20, AttributeType::Ints as i32));
            }
            OnnxAttribute::Floats(name, vals) => {
                buf.extend(proto::encode_string(1, name));
                buf.extend(proto::encode_packed_f32(6, vals));
                buf.extend(proto::encode_i32(20, AttributeType::Floats as i32));
            }
        }
        buf
    }
}

// ============================================================
// ONNX node (operator)
// ============================================================

/// An ONNX computation node.
#[derive(Debug, Clone)]
pub struct OnnxNode {
    /// Operator type (e.g. `"MatMul"`, `"Add"`).
    pub op_type: String,
    /// Node name (for debugging).
    pub name: String,
    /// Names of input tensors/values.
    pub inputs: Vec<String>,
    /// Names of output tensors/values.
    pub outputs: Vec<String>,
    /// Operator attributes.
    pub attributes: Vec<OnnxAttribute>,
}

impl OnnxNode {
    /// Serialize to `NodeProto` bytes.
    ///
    /// Field numbers:
    /// - input:     1 (string repeated)
    /// - output:    2 (string repeated)
    /// - name:      3 (string)
    /// - op_type:   4 (string)
    /// - attribute: 5 (AttributeProto repeated)
    fn to_proto_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        for inp in &self.inputs {
            buf.extend(proto::encode_string(1, inp));
        }
        for out in &self.outputs {
            buf.extend(proto::encode_string(2, out));
        }
        buf.extend(proto::encode_string(3, &self.name));
        buf.extend(proto::encode_string(4, &self.op_type));
        for attr in &self.attributes {
            let attr_bytes = attr.to_proto_bytes();
            buf.extend(proto::encode_submessage(5, &attr_bytes));
        }
        buf
    }
}

// ============================================================
// ONNX graph
// ============================================================

/// A complete ONNX computational graph.
#[derive(Debug, Clone)]
pub struct OnnxGraph {
    /// Graph name.
    pub name: String,
    /// Computation nodes in topological order.
    pub nodes: Vec<OnnxNode>,
    /// Graph-level inputs (dynamic activations, not weights).
    pub inputs: Vec<OnnxValueInfo>,
    /// Graph-level outputs.
    pub outputs: Vec<OnnxValueInfo>,
    /// Weight tensors (initializers).
    pub initializers: Vec<OnnxTensor>,
}

impl OnnxGraph {
    /// Serialize to `GraphProto` bytes.
    ///
    /// Field numbers:
    /// - node:        1 (NodeProto repeated)
    /// - name:        2 (string)
    /// - initializer: 6 (TensorProto repeated)
    /// - input:      11 (ValueInfoProto repeated)
    /// - output:     12 (ValueInfoProto repeated)
    fn to_proto_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // field 1: nodes
        for node in &self.nodes {
            let node_bytes = node.to_proto_bytes();
            buf.extend(proto::encode_submessage(1, &node_bytes));
        }
        // field 2: name
        buf.extend(proto::encode_string(2, &self.name));
        // field 6: initializers (weight tensors)
        for init in &self.initializers {
            let init_bytes = init.to_proto_bytes();
            buf.extend(proto::encode_submessage(6, &init_bytes));
        }
        // field 11: inputs
        for inp in &self.inputs {
            let inp_bytes = inp.to_proto_bytes();
            buf.extend(proto::encode_submessage(11, &inp_bytes));
        }
        // field 12: outputs
        for out in &self.outputs {
            let out_bytes = out.to_proto_bytes();
            buf.extend(proto::encode_submessage(12, &out_bytes));
        }
        buf
    }
}

// ============================================================
// ONNX model
// ============================================================

/// A complete ONNX model ready for serialization.
#[derive(Debug, Clone)]
pub struct OnnxModel {
    /// ONNX IR version (typically 8).
    pub ir_version: i64,
    /// Default opset version (typically 17).
    pub opset_version: i64,
    /// Operator domain (empty string for the default ONNX domain).
    pub domain: String,
    /// The computation graph.
    pub graph: OnnxGraph,
    /// Human-readable description.
    pub doc_string: String,
}

impl OnnxModel {
    /// Create a new model with sane ONNX defaults.
    pub fn new(graph: OnnxGraph) -> Self {
        Self {
            ir_version: 8,
            opset_version: 17,
            domain: String::new(),
            graph,
            doc_string: "Generated by Kizzasi".to_string(),
        }
    }

    /// Serialize to ONNX protobuf bytes.
    ///
    /// `ModelProto` field numbers:
    /// - ir_version:    1 (int64)
    /// - opset_import:  8 (OperatorSetIdProto repeated)
    /// - graph:         7 (GraphProto)
    /// - doc_string:   12 (string)
    ///
    /// `OperatorSetIdProto`:
    /// - domain:  1 (string)
    /// - version: 2 (int64)
    pub fn to_bytes(&self) -> ModelResult<Vec<u8>> {
        let mut buf = Vec::new();

        // field 1: ir_version
        buf.extend(proto::encode_i64(1, self.ir_version));

        // field 7: graph (GraphProto)
        let graph_bytes = self.graph.to_proto_bytes();
        buf.extend(proto::encode_submessage(7, &graph_bytes));

        // field 8: opset_import (OperatorSetIdProto)
        //   domain:  field 1 (string)
        //   version: field 2 (int64)
        let mut opset_buf = Vec::new();
        opset_buf.extend(proto::encode_string(1, &self.domain));
        opset_buf.extend(proto::encode_i64(2, self.opset_version));
        buf.extend(proto::encode_submessage(8, &opset_buf));

        // field 12: doc_string
        if !self.doc_string.is_empty() {
            buf.extend(proto::encode_string(12, &self.doc_string));
        }

        Ok(buf)
    }

    /// Write the serialized model to a file at `path`.
    pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> ModelResult<()> {
        let bytes = self.to_bytes()?;
        std::fs::write(path, bytes).map_err(ModelError::IoError)
    }
}

// ============================================================
// High-level export helpers
// ============================================================

/// Build a weight-only ONNX model from a flat weight map.
///
/// This creates a graph with no computation nodes — only initializer
/// tensors. This is useful for checkpoint export and weight inspection
/// by tools such as Netron.
///
/// # Arguments
/// * `weights` — map from tensor name to flat `f32` data.
/// * `shapes`  — map from tensor name to `[d0, d1, …]` shape (same keys).
/// * `model_name` — used as the graph name.
///
/// # Errors
/// Returns [`ModelError::InvalidConfig`] if a name appears in `weights`
/// but not in `shapes`, or if the shape volume does not match the data length.
pub fn export_weights_to_onnx(
    weights: &HashMap<String, Vec<f32>>,
    shapes: &HashMap<String, Vec<usize>>,
    model_name: &str,
) -> ModelResult<OnnxModel> {
    let mut initializers = Vec::with_capacity(weights.len());

    // Iterate in a deterministic order for reproducibility.
    let mut names: Vec<&String> = weights.keys().collect();
    names.sort();

    for name in names {
        let data = &weights[name];
        let shape = shapes.get(name).ok_or_else(|| {
            ModelError::invalid_config(format!(
                "export_weights_to_onnx: shape missing for tensor '{name}'"
            ))
        })?;

        // Validate shape volume vs data length.
        let volume: usize = shape.iter().product();
        if volume != data.len() {
            return Err(ModelError::InvalidConfig {
                message: format!(
                    "export_weights_to_onnx: tensor '{name}' shape {:?} has volume {volume} \
                     but data length is {}",
                    shape,
                    data.len()
                ),
            });
        }

        let dims: Vec<i64> = shape.iter().map(|&d| d as i64).collect();
        initializers.push(OnnxTensor {
            name: name.clone(),
            dims,
            data_type: OnnxDataType::Float,
            float_data: data.clone(),
        });
    }

    let graph = OnnxGraph {
        name: model_name.to_string(),
        nodes: vec![],
        inputs: vec![],
        outputs: vec![],
        initializers,
    };

    Ok(OnnxModel::new(graph))
}

/// Export a single fully-connected (linear) layer as an ONNX sub-graph.
///
/// Produces:
/// - One `MatMul` node: `input × weight^T → matmul_out`
/// - Optionally one `Add` node: `matmul_out + bias → output`
///
/// The weight tensor is stored transposed relative to PyTorch convention
/// so that standard `MatMul` (`[batch, in_features] × [in_features, out_features]`)
/// produces the correct result.
///
/// # Arguments
/// * `weight`       — flat row-major weight data, shape `[out, in]`.
/// * `weight_shape` — `[out_features, in_features]` (2 elements).
/// * `bias`         — optional bias of length `out_features`.
/// * `input_name`   — name of the graph-level input value.
/// * `output_name`  — name of the graph-level output value.
/// * `layer_name`   — prefix used to name internal tensors and nodes.
///
/// # Errors
/// Returns [`ModelError::InvalidConfig`] if `weight_shape` does not have
/// exactly 2 elements or if the bias length is inconsistent.
pub fn export_linear_layer(
    weight: &[f32],
    weight_shape: &[usize],
    bias: Option<&[f32]>,
    input_name: &str,
    output_name: &str,
    layer_name: &str,
) -> ModelResult<OnnxGraph> {
    if weight_shape.len() != 2 {
        return Err(ModelError::invalid_config(format!(
            "export_linear_layer: weight_shape must have exactly 2 elements, \
             got {}",
            weight_shape.len()
        )));
    }

    let out_features = weight_shape[0];
    let in_features = weight_shape[1];
    let expected_len = out_features * in_features;

    if weight.len() != expected_len {
        return Err(ModelError::invalid_config(format!(
            "export_linear_layer: weight length {} does not match shape {:?} (volume {})",
            weight.len(),
            weight_shape,
            expected_len
        )));
    }

    // Transpose weight from [out, in] to [in, out] for ONNX MatMul.
    // ONNX MatMul: [batch, in] × [in, out] → [batch, out]
    let mut weight_transposed = vec![0.0f32; expected_len];
    for o in 0..out_features {
        for i in 0..in_features {
            weight_transposed[i * out_features + o] = weight[o * in_features + i];
        }
    }

    let weight_name = format!("{layer_name}.weight");
    let matmul_out_name = format!("{layer_name}.matmul_out");

    let weight_tensor = OnnxTensor {
        name: weight_name.clone(),
        dims: vec![in_features as i64, out_features as i64],
        data_type: OnnxDataType::Float,
        float_data: weight_transposed,
    };

    let mut nodes = Vec::new();
    let mut initializers = vec![weight_tensor];

    // Decide whether the MatMul feeds directly into the output or into Add.
    let matmul_output = if bias.is_some() {
        matmul_out_name.clone()
    } else {
        output_name.to_string()
    };

    let matmul_node = OnnxNode {
        op_type: "MatMul".to_string(),
        name: format!("{layer_name}/MatMul"),
        inputs: vec![input_name.to_string(), weight_name],
        outputs: vec![matmul_output],
        attributes: vec![],
    };
    nodes.push(matmul_node);

    if let Some(bias_data) = bias {
        if bias_data.len() != out_features {
            return Err(ModelError::invalid_config(format!(
                "export_linear_layer: bias length {} does not match out_features {}",
                bias_data.len(),
                out_features
            )));
        }

        let bias_name = format!("{layer_name}.bias");
        let bias_tensor = OnnxTensor {
            name: bias_name.clone(),
            dims: vec![out_features as i64],
            data_type: OnnxDataType::Float,
            float_data: bias_data.to_vec(),
        };
        initializers.push(bias_tensor);

        let add_node = OnnxNode {
            op_type: "Add".to_string(),
            name: format!("{layer_name}/Add"),
            inputs: vec![matmul_out_name, bias_name],
            outputs: vec![output_name.to_string()],
            attributes: vec![],
        };
        nodes.push(add_node);
    }

    // Build graph-level I/O declarations.
    let graph_input = OnnxValueInfo {
        name: input_name.to_string(),
        data_type: OnnxDataType::Float,
        // batch dimension is dynamic
        shape: vec![None, Some(in_features as i64)],
    };
    let graph_output = OnnxValueInfo {
        name: output_name.to_string(),
        data_type: OnnxDataType::Float,
        shape: vec![None, Some(out_features as i64)],
    };

    Ok(OnnxGraph {
        name: layer_name.to_string(),
        nodes,
        inputs: vec![graph_input],
        outputs: vec![graph_output],
        initializers,
    })
}

// ============================================================
// Additional builder helpers
// ============================================================

/// Builder for constructing `OnnxGraph`s incrementally.
///
/// Useful when programmatically assembling multi-layer graphs.
#[derive(Debug, Default)]
pub struct OnnxGraphBuilder {
    name: String,
    nodes: Vec<OnnxNode>,
    inputs: Vec<OnnxValueInfo>,
    outputs: Vec<OnnxValueInfo>,
    initializers: Vec<OnnxTensor>,
}

impl OnnxGraphBuilder {
    /// Create a new builder with the given graph name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Add a computation node.
    pub fn add_node(mut self, node: OnnxNode) -> Self {
        self.nodes.push(node);
        self
    }

    /// Declare a graph-level input.
    pub fn add_input(mut self, vi: OnnxValueInfo) -> Self {
        self.inputs.push(vi);
        self
    }

    /// Declare a graph-level output.
    pub fn add_output(mut self, vi: OnnxValueInfo) -> Self {
        self.outputs.push(vi);
        self
    }

    /// Add a weight initializer tensor.
    pub fn add_initializer(mut self, tensor: OnnxTensor) -> Self {
        self.initializers.push(tensor);
        self
    }

    /// Consume the builder and produce an [`OnnxGraph`].
    pub fn build(self) -> OnnxGraph {
        OnnxGraph {
            name: self.name,
            nodes: self.nodes,
            inputs: self.inputs,
            outputs: self.outputs,
            initializers: self.initializers,
        }
    }
}

// ============================================================
// Unit tests
// ============================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_proto_varint_encoding() {
        use super::proto::encode_varint;
        assert_eq!(encode_varint(0), vec![0]);
        assert_eq!(encode_varint(1), vec![1]);
        assert_eq!(encode_varint(127), vec![127]);
        assert_eq!(encode_varint(128), vec![0x80, 0x01]);
        assert_eq!(encode_varint(300), vec![0xAC, 0x02]);
    }

    #[test]
    fn test_onnx_model_to_bytes_nonempty() {
        let graph = OnnxGraph {
            name: "test".to_string(),
            nodes: vec![],
            inputs: vec![],
            outputs: vec![],
            initializers: vec![OnnxTensor {
                name: "w".to_string(),
                dims: vec![2, 3],
                data_type: OnnxDataType::Float,
                float_data: vec![1.0f32; 6],
            }],
        };
        let model = OnnxModel::new(graph);
        let bytes = model.to_bytes().expect("serialization must succeed");
        assert!(!bytes.is_empty());
    }

    #[test]
    fn test_onnx_save_and_file_size() {
        let graph = OnnxGraph {
            name: "weight_export".to_string(),
            nodes: vec![],
            inputs: vec![],
            outputs: vec![],
            initializers: vec![OnnxTensor {
                name: "embed.weight".to_string(),
                dims: vec![4, 8],
                data_type: OnnxDataType::Float,
                float_data: (0..32).map(|i| i as f32 * 0.01).collect(),
            }],
        };
        let model = OnnxModel::new(graph);
        let path = std::env::temp_dir().join("test_kizzasi_export.onnx");
        model.save(&path).expect("save must succeed");
        let metadata = std::fs::metadata(&path).expect("file must exist after save");
        assert!(metadata.len() > 10, "exported file must be non-trivial");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn test_export_weights_to_onnx() {
        let mut weights = HashMap::new();
        weights.insert("layer.weight".to_string(), vec![1.0f32; 12]);
        let mut shapes = HashMap::new();
        shapes.insert("layer.weight".to_string(), vec![3, 4]);
        let model =
            export_weights_to_onnx(&weights, &shapes, "test_model").expect("export must succeed");
        assert_eq!(model.graph.initializers.len(), 1);
        assert_eq!(model.graph.initializers[0].dims, vec![3i64, 4]);
    }

    #[test]
    fn test_export_linear_layer() {
        let weight = vec![1.0f32; 6]; // [2, 3]
        let bias = vec![0.1f32; 2];
        let graph =
            export_linear_layer(&weight, &[2, 3], Some(&bias), "input", "output", "linear0")
                .expect("export_linear_layer must succeed");
        // Should have MatMul and Add nodes
        assert!(!graph.nodes.is_empty(), "must have at least one node");
        assert!(
            !graph.initializers.is_empty(),
            "must have at least one initializer"
        );
        // With bias we expect exactly two nodes: MatMul and Add
        assert_eq!(graph.nodes.len(), 2);
        assert_eq!(graph.nodes[0].op_type, "MatMul");
        assert_eq!(graph.nodes[1].op_type, "Add");
    }

    #[test]
    fn test_export_linear_layer_no_bias() {
        let weight = vec![0.5f32; 12]; // [3, 4]
        let graph = export_linear_layer(&weight, &[3, 4], None, "x", "y", "fc")
            .expect("export must succeed");
        assert_eq!(graph.nodes.len(), 1);
        assert_eq!(graph.nodes[0].op_type, "MatMul");
        assert_eq!(graph.initializers.len(), 1);
    }

    #[test]
    fn test_onnx_tensor_raw_data() {
        // Verify that raw_data encoding stores f32 as little-endian bytes.
        let floats = vec![1.0f32, 2.0, 3.0];
        let raw: Vec<u8> = floats.iter().flat_map(|f| f.to_le_bytes()).collect();
        assert_eq!(raw.len(), 12);
        let recovered: Vec<f32> = raw
            .chunks_exact(4)
            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
            .collect();
        assert_eq!(recovered, floats);
    }

    #[test]
    fn test_export_weights_shape_mismatch() {
        let mut weights = HashMap::new();
        weights.insert("w".to_string(), vec![1.0f32; 6]);
        let mut shapes = HashMap::new();
        // Wrong volume: 3×3 = 9 ≠ 6
        shapes.insert("w".to_string(), vec![3, 3]);
        let result = export_weights_to_onnx(&weights, &shapes, "bad");
        assert!(result.is_err(), "must fail on volume mismatch");
    }

    #[test]
    fn test_export_weights_missing_shape() {
        let mut weights = HashMap::new();
        weights.insert("w".to_string(), vec![1.0f32; 6]);
        let shapes: HashMap<String, Vec<usize>> = HashMap::new();
        let result = export_weights_to_onnx(&weights, &shapes, "bad");
        assert!(result.is_err(), "must fail when shape is absent");
    }

    #[test]
    fn test_onnx_graph_builder() {
        let graph = OnnxGraphBuilder::new("built_graph")
            .add_initializer(OnnxTensor {
                name: "param".to_string(),
                dims: vec![4, 4],
                data_type: OnnxDataType::Float,
                float_data: vec![0.0f32; 16],
            })
            .add_input(OnnxValueInfo {
                name: "x".to_string(),
                data_type: OnnxDataType::Float,
                shape: vec![None, Some(4)],
            })
            .add_output(OnnxValueInfo {
                name: "y".to_string(),
                data_type: OnnxDataType::Float,
                shape: vec![None, Some(4)],
            })
            .build();
        assert_eq!(graph.name, "built_graph");
        assert_eq!(graph.initializers.len(), 1);
        assert_eq!(graph.inputs.len(), 1);
        assert_eq!(graph.outputs.len(), 1);
    }

    #[test]
    fn test_attribute_int_encoding() {
        let attr = OnnxAttribute::Int("axis".to_string(), 1);
        let bytes = attr.to_proto_bytes();
        assert!(!bytes.is_empty());
    }

    #[test]
    fn test_attribute_floats_encoding() {
        let attr = OnnxAttribute::Floats("scales".to_string(), vec![1.0, 2.0, 3.0]);
        let bytes = attr.to_proto_bytes();
        assert!(!bytes.is_empty());
    }

    #[test]
    fn test_multi_tensor_export_round_trip() {
        // Create a model with multiple initializers and write/check it.
        let mut weights = HashMap::new();
        let mut shapes = HashMap::new();
        for layer in 0..4 {
            let key = format!("layer{layer}.weight");
            weights.insert(key.clone(), vec![0.1f32; 8]);
            shapes.insert(key, vec![2, 4]);
            let bkey = format!("layer{layer}.bias");
            weights.insert(bkey.clone(), vec![0.0f32; 2]);
            shapes.insert(bkey, vec![2]);
        }

        let model =
            export_weights_to_onnx(&weights, &shapes, "multi_layer").expect("export must succeed");
        assert_eq!(model.graph.initializers.len(), 8);

        let bytes = model.to_bytes().expect("to_bytes must succeed");
        assert!(!bytes.is_empty());

        // Write and read back.
        let path = std::env::temp_dir().join("test_kizzasi_multi_layer.onnx");
        model.save(&path).expect("save must succeed");
        let on_disk = std::fs::read(&path).expect("must read back file");
        assert_eq!(on_disk, bytes);
        let _ = std::fs::remove_file(&path);
    }
}