lift-export 0.4.4

LIFT compiler backends: LLVM IR, ONNX (opset 21), OpenQASM 3 exporters; CUDA PTX and XLA planned
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
// ============================================================================
// LIFT → ONNX Export
// ============================================================================
//
// Generates ONNX-compatible protobuf text format (.onnx.pbtxt) and binary-
// equivalent JSON representation (.onnx.json) from the LIFT IR.
//
// ONNX (Open Neural Network Exchange) is the industry-standard interchange
// format for neural network models, supported by PyTorch, TensorFlow,
// TensorRT, ONNX Runtime, and many others.
//
// The exporter maps LIFT tensor operations to ONNX operator set opset 21.
// ============================================================================

use lift_core::context::Context;
use lift_core::types::{CoreType, DataType, Dimension, TypeData};
use std::fmt::Write;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum OnnxExportError {
    #[error("Unsupported operation for ONNX export: {0}")]
    UnsupportedOp(String),
    #[error("Export error: {0}")]
    General(String),
}

/// ONNX data type enum values (from onnx.proto TensorProto.DataType)
#[derive(Debug, Clone, Copy)]
#[repr(i32)]
enum OnnxDataType {
    Float = 1,
    // Uint8 = 2,
    Int8 = 3,
    // Uint16 = 4,
    Int16 = 5,
    Int32 = 6,
    Int64 = 7,
    // String = 8,
    Bool = 9,
    Float16 = 10,
    Double = 11,
    // Uint32 = 12,
    // Uint64 = 13,
    // Complex64 = 14,
    // Complex128 = 15,
    BFloat16 = 16,
    // Float8E4M3FN = 17,
    // Float8E5M2 = 19,
}

fn lift_dtype_to_onnx(dt: &DataType) -> OnnxDataType {
    match dt {
        DataType::FP64 => OnnxDataType::Double,
        DataType::FP32 => OnnxDataType::Float,
        DataType::FP16 => OnnxDataType::Float16,
        DataType::BF16 => OnnxDataType::BFloat16,
        DataType::FP8E4M3 | DataType::FP8E5M2 => OnnxDataType::Float16,
        DataType::INT64 => OnnxDataType::Int64,
        DataType::INT32 => OnnxDataType::Int32,
        DataType::INT16 => OnnxDataType::Int16,
        DataType::INT8 | DataType::INT4 | DataType::INT2 => OnnxDataType::Int8,
        DataType::UINT8 => OnnxDataType::Int8,
        DataType::Bool => OnnxDataType::Bool,
        DataType::Index => OnnxDataType::Int64,
    }
}

/// Map a LIFT tensor op name to ONNX op type and domain.
fn lift_op_to_onnx(op_name: &str) -> Option<(&str, &str)> {
    // Returns (onnx_op_type, domain)
    // domain "" = standard ONNX domain
    // domain "com.microsoft" = Microsoft extensions
    let mapping = match op_name {
        // Arithmetic
        "tensor.add" => ("Add", ""),
        "tensor.sub" => ("Sub", ""),
        "tensor.mul" => ("Mul", ""),
        "tensor.div" => ("Div", ""),
        "tensor.neg" => ("Neg", ""),
        "tensor.abs" => ("Abs", ""),
        "tensor.sqrt" => ("Sqrt", ""),
        "tensor.exp" => ("Exp", ""),
        "tensor.log" => ("Log", ""),
        "tensor.pow" => ("Pow", ""),
        "tensor.floor" => ("Floor", ""),
        "tensor.ceil" => ("Ceil", ""),
        "tensor.clip" => ("Clip", ""),
        "tensor.sin" => ("Sin", ""),
        "tensor.cos" => ("Cos", ""),

        // MatMul / Gemm
        "tensor.matmul" => ("MatMul", ""),
        "tensor.linear" => ("Gemm", ""),

        // Activations
        "tensor.relu" => ("Relu", ""),
        "tensor.gelu" => ("Gelu", ""),
        "tensor.silu" => ("Sigmoid", ""), // SiLU = x * sigmoid(x), approximated
        "tensor.sigmoid" => ("Sigmoid", ""),
        "tensor.tanh" => ("Tanh", ""),
        "tensor.softmax" => ("Softmax", ""),
        "tensor.leaky_relu" => ("LeakyRelu", ""),
        "tensor.elu" => ("Elu", ""),

        // Normalisation
        "tensor.layernorm" => ("LayerNormalization", ""),
        "tensor.rmsnorm" => ("SimplifiedLayerNormalization", "com.microsoft"),
        "tensor.batchnorm" => ("BatchNormalization", ""),
        "tensor.groupnorm" => ("GroupNormalization", ""),

        // Convolution
        "tensor.conv1d" => ("Conv", ""),
        "tensor.conv2d" => ("Conv", ""),
        "tensor.conv3d" => ("Conv", ""),

        // Pooling
        "tensor.maxpool2d" => ("MaxPool", ""),
        "tensor.avgpool2d" => ("AveragePool", ""),
        "tensor.global_avgpool" => ("GlobalAveragePool", ""),
        "tensor.global_maxpool" => ("GlobalMaxPool", ""),

        // Shape manipulation
        "tensor.reshape" => ("Reshape", ""),
        "tensor.transpose" => ("Transpose", ""),
        "tensor.concat" => ("Concat", ""),
        "tensor.flatten" => ("Flatten", ""),
        "tensor.squeeze" => ("Squeeze", ""),
        "tensor.unsqueeze" => ("Unsqueeze", ""),
        "tensor.gather" => ("Gather", ""),
        "tensor.scatter" => ("ScatterElements", ""),
        "tensor.split" => ("Split", ""),
        "tensor.slice" => ("Slice", ""),
        "tensor.pad" => ("Pad", ""),

        // Embedding / Reduce
        "tensor.embedding" => ("Gather", ""),
        "tensor.reduce_sum" => ("ReduceSum", ""),
        "tensor.reduce_mean" => ("ReduceMean", ""),
        "tensor.reduce_max" => ("ReduceMax", ""),

        // Attention (use Microsoft extensions)
        "tensor.attention" => ("Attention", "com.microsoft"),
        "tensor.multi_head_attention" => ("MultiHeadAttention", "com.microsoft"),
        "tensor.grouped_query_attention" => ("GroupQueryAttention", "com.microsoft"),
        "tensor.flash_attention" => ("MultiHeadAttention", "com.microsoft"),
        "tensor.cross_attention" => ("MultiHeadAttention", "com.microsoft"),
        "tensor.sliding_window_attention" => ("MultiHeadAttention", "com.microsoft"),
        "tensor.paged_attention" => ("Attention", "com.microsoft"),

        // Quantization
        "tensor.quantize" => ("QuantizeLinear", ""),
        "tensor.dequantize" => ("DequantizeLinear", ""),

        // MoE
        "tensor.moe_dispatch" => ("MoE", "com.microsoft"),
        "tensor.moe_combine" => ("MoE", "com.microsoft"),

        // Fused (map to subgraph or ms domain)
        "tensor.fused_matmul_bias_relu" => ("FusedMatMul", "com.microsoft"),
        "tensor.fused_matmul_bias" => ("FusedMatMul", "com.microsoft"),
        "tensor.fused_linear_gelu" => ("FusedMatMul", "com.microsoft"),
        "tensor.fused_linear_silu" => ("FusedMatMul", "com.microsoft"),

        // Dropout
        "tensor.dropout" => ("Dropout", ""),

        _ => return None,
    };
    Some(mapping)
}

#[derive(Debug)]
pub struct OnnxExporter {
    opset_version: i64,
    ir_version: i64,
    producer: String,
}

impl OnnxExporter {
    pub fn new() -> Self {
        Self {
            opset_version: 21,
            ir_version: 9,
            producer: "LIFT Compiler Framework".to_string(),
        }
    }

    pub fn with_opset(mut self, version: i64) -> Self {
        self.opset_version = version;
        self
    }

    /// Export LIFT IR to ONNX protobuf text format.
    pub fn export(&self, ctx: &Context) -> Result<String, OnnxExportError> {
        let mut out = String::new();

        // ONNX ModelProto header
        let _ = writeln!(out, "# ONNX Model — Generated by LIFT Compiler Framework");
        let _ = writeln!(out, "# Opset version: {}", self.opset_version);
        let _ = writeln!(out, "# IR version: {}", self.ir_version);
        let _ = writeln!(out);

        for module in &ctx.modules {
            let mod_name = ctx.strings.resolve(module.name).to_string();

            let _ = writeln!(out, "ir_version: {}", self.ir_version);
            let _ = writeln!(out, "producer_name: \"{}\"", self.producer);
            let _ = writeln!(out, "producer_version: \"0.3.0\"");
            let _ = writeln!(out, "domain: \"\"");
            let _ = writeln!(out, "model_version: 1");
            let _ = writeln!(out, "doc_string: \"LIFT model: {}\"", mod_name);
            let _ = writeln!(out);

            // Opset imports
            let _ = writeln!(out, "opset_import {{");
            let _ = writeln!(out, "  domain: \"\"");
            let _ = writeln!(out, "  version: {}", self.opset_version);
            let _ = writeln!(out, "}}");
            let _ = writeln!(out, "opset_import {{");
            let _ = writeln!(out, "  domain: \"com.microsoft\"");
            let _ = writeln!(out, "  version: 1");
            let _ = writeln!(out, "}}");
            let _ = writeln!(out);

            // Graph
            let _ = writeln!(out, "graph {{");
            let _ = writeln!(out, "  name: \"{}\"", mod_name);
            let _ = writeln!(out);

            let mut node_counter = 0usize;
            let mut graph_inputs: Vec<String> = Vec::new();
            let mut graph_outputs: Vec<String> = Vec::new();
            let mut value_info_entries: Vec<String> = Vec::new();

            for func in &module.functions {
                let fname = ctx.strings.resolve(func.name).to_string();

                // Collect function params as graph inputs
                for (pi, param_ty) in func.params.iter().enumerate() {
                    let input_name = format!("{}_input{}", fname, pi);
                    graph_inputs.push(self.format_value_info(&input_name, ctx, *param_ty));
                }

                // Collect return types as graph outputs
                for (ri, ret_ty) in func.returns.iter().enumerate() {
                    let output_name = format!("{}_output{}", fname, ri);
                    graph_outputs.push(self.format_value_info(&output_name, ctx, *ret_ty));
                }

                // Walk the ops
                if let Some(region_key) = func.body {
                    if let Some(region) = ctx.get_region(region_key) {
                        for &block_key in &region.blocks {
                            if let Some(block) = ctx.get_block(block_key) {
                                // Map block args to named values
                                let mut val_names = std::collections::HashMap::new();
                                for (ai, &arg_val) in block.args.iter().enumerate() {
                                    val_names.insert(arg_val, format!("{}_input{}", fname, ai));
                                }

                                for &op_key in &block.ops {
                                    if let Some(op) = ctx.get_op(op_key) {
                                        let op_name = ctx.strings.resolve(op.name).to_string();

                                        // Skip core.return — it just identifies output
                                        if op_name == "core.return" {
                                            // Map the inputs of return as graph outputs
                                            for (ri, &inp) in op.inputs.iter().enumerate() {
                                                let out_name = format!("{}_output{}", fname, ri);
                                                if let Some(src_name) = val_names.get(&inp) {
                                                    // Alias
                                                    let _ = writeln!(
                                                        out,
                                                        "  # output {} -> {}",
                                                        out_name, src_name
                                                    );
                                                }
                                            }
                                            continue;
                                        }

                                        // Map LIFT op to ONNX
                                        let (onnx_op, domain) = match lift_op_to_onnx(&op_name) {
                                            Some(pair) => pair,
                                            None => {
                                                // Emit as custom op
                                                let _ = writeln!(out, "  # WARNING: unmapped op {} — emitted as custom", op_name);
                                                (&op_name as &str, "ai.lift")
                                            }
                                        };

                                        let node_name = format!("{}_{}", fname, node_counter);

                                        // Input names
                                        let input_names: Vec<String> = op
                                            .inputs
                                            .iter()
                                            .map(|&v| {
                                                val_names
                                                    .get(&v)
                                                    .cloned()
                                                    .unwrap_or_else(|| format!("v_{:?}", v))
                                            })
                                            .collect();

                                        // Result names
                                        let result_names: Vec<String> = op
                                            .results
                                            .iter()
                                            .enumerate()
                                            .map(|(i, &v)| {
                                                let name = format!("{}_r{}", node_name, i);
                                                val_names.insert(v, name.clone());

                                                // Collect intermediate value type info
                                                if let Some(val) = ctx.get_value(v) {
                                                    value_info_entries.push(
                                                        self.format_value_info(&name, ctx, val.ty),
                                                    );
                                                }

                                                name
                                            })
                                            .collect();

                                        // Emit node
                                        let _ = writeln!(out, "  node {{");
                                        for iname in &input_names {
                                            let _ = writeln!(out, "    input: \"{}\"", iname);
                                        }
                                        for rname in &result_names {
                                            let _ = writeln!(out, "    output: \"{}\"", rname);
                                        }
                                        let _ = writeln!(out, "    name: \"{}\"", node_name);
                                        let _ = writeln!(out, "    op_type: \"{}\"", onnx_op);
                                        if !domain.is_empty() {
                                            let _ = writeln!(out, "    domain: \"{}\"", domain);
                                        }
                                        let _ = writeln!(out, "  }}");
                                        let _ = writeln!(out);

                                        node_counter += 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Graph inputs
            let _ = writeln!(out, "  # === Graph Inputs ===");
            for gi in &graph_inputs {
                let _ = writeln!(out, "{}", gi);
            }
            let _ = writeln!(out);

            // Graph outputs
            let _ = writeln!(out, "  # === Graph Outputs ===");
            for go in &graph_outputs {
                let _ = writeln!(out, "{}", go);
            }
            let _ = writeln!(out);

            // Intermediate value info
            if !value_info_entries.is_empty() {
                let _ = writeln!(out, "  # === Value Info ===");
                for vi in &value_info_entries {
                    let _ = writeln!(out, "{}", vi);
                }
                let _ = writeln!(out);
            }

            let _ = writeln!(out, "}}");
        }

        Ok(out)
    }

    /// Export to ONNX-compatible JSON (for tools that consume JSON ONNX).
    pub fn export_json(&self, ctx: &Context) -> Result<String, OnnxExportError> {
        let mut out = String::new();

        let _ = writeln!(out, "{{");
        let _ = writeln!(out, "  \"irVersion\": {},", self.ir_version);
        let _ = writeln!(out, "  \"producerName\": \"{}\",", self.producer);
        let _ = writeln!(out, "  \"producerVersion\": \"0.3.0\",");
        let _ = writeln!(out, "  \"opsetImport\": [");
        let _ = writeln!(
            out,
            "    {{ \"domain\": \"\", \"version\": {} }},",
            self.opset_version
        );
        let _ = writeln!(
            out,
            "    {{ \"domain\": \"com.microsoft\", \"version\": 1 }}"
        );
        let _ = writeln!(out, "  ],");

        for module in &ctx.modules {
            let mod_name = ctx.strings.resolve(module.name).to_string();

            let _ = writeln!(out, "  \"graph\": {{");
            let _ = writeln!(out, "    \"name\": \"{}\",", mod_name);

            // Nodes
            let _ = writeln!(out, "    \"node\": [");
            let mut nodes: Vec<String> = Vec::new();

            for func in &module.functions {
                let fname = ctx.strings.resolve(func.name).to_string();
                let mut val_names = std::collections::HashMap::new();
                let mut node_counter = 0usize;

                if let Some(region_key) = func.body {
                    if let Some(region) = ctx.get_region(region_key) {
                        for &block_key in &region.blocks {
                            if let Some(block) = ctx.get_block(block_key) {
                                for (ai, &arg_val) in block.args.iter().enumerate() {
                                    val_names.insert(arg_val, format!("{}_input{}", fname, ai));
                                }

                                for &op_key in &block.ops {
                                    if let Some(op) = ctx.get_op(op_key) {
                                        let op_name = ctx.strings.resolve(op.name).to_string();
                                        if op_name == "core.return" {
                                            continue;
                                        }

                                        let (onnx_op, domain) = lift_op_to_onnx(&op_name)
                                            .unwrap_or((&op_name, "ai.lift"));

                                        let node_name = format!("{}_{}", fname, node_counter);

                                        let input_names: Vec<String> = op
                                            .inputs
                                            .iter()
                                            .map(|&v| {
                                                val_names
                                                    .get(&v)
                                                    .cloned()
                                                    .unwrap_or_else(|| format!("v_{:?}", v))
                                            })
                                            .collect();

                                        let result_names: Vec<String> = op
                                            .results
                                            .iter()
                                            .enumerate()
                                            .map(|(i, &v)| {
                                                let name = format!("{}_r{}", node_name, i);
                                                val_names.insert(v, name.clone());
                                                name
                                            })
                                            .collect();

                                        let mut node_json = String::new();
                                        let _ = write!(node_json, "      {{");
                                        let _ = write!(node_json, " \"name\": \"{}\",", node_name);
                                        let _ = write!(node_json, " \"opType\": \"{}\",", onnx_op);
                                        if !domain.is_empty() {
                                            let _ =
                                                write!(node_json, " \"domain\": \"{}\",", domain);
                                        }
                                        let inputs_json: Vec<String> = input_names
                                            .iter()
                                            .map(|n| format!("\"{}\"", n))
                                            .collect();
                                        let outputs_json: Vec<String> = result_names
                                            .iter()
                                            .map(|n| format!("\"{}\"", n))
                                            .collect();
                                        let _ = write!(
                                            node_json,
                                            " \"input\": [{}],",
                                            inputs_json.join(", ")
                                        );
                                        let _ = write!(
                                            node_json,
                                            " \"output\": [{}]",
                                            outputs_json.join(", ")
                                        );
                                        let _ = write!(node_json, " }}");
                                        nodes.push(node_json);

                                        node_counter += 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            let _ = writeln!(out, "{}", nodes.join(",\n"));
            let _ = writeln!(out, "    ],");

            // Graph inputs
            let _ = writeln!(out, "    \"input\": [");
            let mut inputs: Vec<String> = Vec::new();
            for func in &module.functions {
                let fname = ctx.strings.resolve(func.name).to_string();
                for (pi, param_ty) in func.params.iter().enumerate() {
                    let name = format!("{}_input{}", fname, pi);
                    inputs.push(self.format_json_value_info(&name, ctx, *param_ty));
                }
            }
            let _ = writeln!(out, "{}", inputs.join(",\n"));
            let _ = writeln!(out, "    ],");

            // Graph outputs
            let _ = writeln!(out, "    \"output\": [");
            let mut outputs: Vec<String> = Vec::new();
            for func in &module.functions {
                let fname = ctx.strings.resolve(func.name).to_string();
                for (ri, ret_ty) in func.returns.iter().enumerate() {
                    let name = format!("{}_output{}", fname, ri);
                    outputs.push(self.format_json_value_info(&name, ctx, *ret_ty));
                }
            }
            let _ = writeln!(out, "{}", outputs.join(",\n"));
            let _ = writeln!(out, "    ]");

            let _ = writeln!(out, "  }}");
        }

        let _ = writeln!(out, "}}");
        Ok(out)
    }

    fn format_value_info(
        &self,
        name: &str,
        ctx: &Context,
        type_key: lift_core::types::TypeId,
    ) -> String {
        let mut info = String::new();
        let _ = writeln!(info, "  input {{");
        let _ = writeln!(info, "    name: \"{}\"", name);
        let _ = writeln!(info, "    type {{");
        let _ = writeln!(info, "      tensor_type {{");

        match ctx.resolve_type(type_key) {
            CoreType::Opaque {
                data: TypeData::Tensor(tensor_info),
                ..
            } => {
                let onnx_dt = lift_dtype_to_onnx(&tensor_info.dtype);
                let _ = writeln!(info, "        elem_type: {}", onnx_dt as i32);
                let _ = writeln!(info, "        shape {{");
                for dim in &tensor_info.shape {
                    match dim {
                        Dimension::Constant(s) => {
                            let _ = writeln!(info, "          dim {{ dim_value: {} }}", s);
                        }
                        _ => {
                            let _ = writeln!(info, "          dim {{ dim_param: \"?\" }}");
                        }
                    }
                }
                let _ = writeln!(info, "        }}");
            }
            _ => {
                let _ = writeln!(info, "        elem_type: 1"); // default float
            }
        }

        let _ = writeln!(info, "      }}");
        let _ = writeln!(info, "    }}");
        let _ = write!(info, "  }}");
        info
    }

    fn format_json_value_info(
        &self,
        name: &str,
        ctx: &Context,
        type_key: lift_core::types::TypeId,
    ) -> String {
        let mut info = String::new();
        let _ = write!(info, "      {{ \"name\": \"{}\"", name);

        match ctx.resolve_type(type_key) {
            CoreType::Opaque {
                data: TypeData::Tensor(tensor_info),
                ..
            } => {
                let onnx_dt = lift_dtype_to_onnx(&tensor_info.dtype);
                let dims: Vec<String> = tensor_info
                    .shape
                    .iter()
                    .map(|d| match d {
                        Dimension::Constant(s) => s.to_string(),
                        _ => "\"?\"".to_string(),
                    })
                    .collect();
                let _ = write!(
                    info,
                    ", \"type\": {{ \"tensorType\": {{ \"elemType\": {}, \"shape\": [{}] }} }}",
                    onnx_dt as i32,
                    dims.join(", ")
                );
            }
            _ => {
                let _ = write!(
                    info,
                    ", \"type\": {{ \"tensorType\": {{ \"elemType\": 1 }} }}"
                );
            }
        }

        let _ = write!(info, " }}");
        info
    }
}

impl Default for OnnxExporter {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_onnx_export_empty() {
        let ctx = Context::new();
        let exporter = OnnxExporter::new();
        let result = exporter.export(&ctx);
        assert!(result.is_ok());
        let text = result.unwrap();
        assert!(text.contains("ONNX Model") || text.is_empty());
    }

    #[test]
    fn test_onnx_json_export_empty() {
        let ctx = Context::new();
        let exporter = OnnxExporter::new();
        let result = exporter.export_json(&ctx);
        assert!(result.is_ok());
        let text = result.unwrap();
        assert!(text.contains("irVersion"));
    }

    #[test]
    fn test_lift_op_mapping() {
        assert_eq!(lift_op_to_onnx("tensor.matmul"), Some(("MatMul", "")));
        assert_eq!(lift_op_to_onnx("tensor.relu"), Some(("Relu", "")));
        assert_eq!(
            lift_op_to_onnx("tensor.layernorm"),
            Some(("LayerNormalization", ""))
        );
        assert_eq!(
            lift_op_to_onnx("tensor.grouped_query_attention"),
            Some(("GroupQueryAttention", "com.microsoft"))
        );
        assert!(lift_op_to_onnx("unknown.op").is_none());
    }

    #[test]
    fn test_onnx_with_context() {
        let model = lift_core::model_builder::ModelBuilder::new("test_onnx")
            .function("forward")
            .param(
                "x",
                lift_core::model_builder::tensor(&[1, 784], DataType::FP32),
            )
            .param(
                "w",
                lift_core::model_builder::tensor_2d(784, 10, DataType::FP32),
            )
            .op(
                "tensor.matmul",
                &["x", "w"],
                "out",
                lift_core::model_builder::tensor(&[1, 10], DataType::FP32),
            )
            .returns("out")
            .done();

        let ctx = model.build_context();
        let exporter = OnnxExporter::new();

        let pbtxt = exporter.export(&ctx).unwrap();
        assert!(pbtxt.contains("MatMul"));
        assert!(pbtxt.contains("test_onnx"));
        assert!(pbtxt.contains("opset_import"));

        let json = exporter.export_json(&ctx).unwrap();
        assert!(json.contains("MatMul"));
        assert!(json.contains("test_onnx"));
    }
}