-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonnx_export_example.rs
More file actions
118 lines (101 loc) · 3.7 KB
/
Copy pathonnx_export_example.rs
File metadata and controls
118 lines (101 loc) · 3.7 KB
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
use candlelight::{DType, Device, Tensor};
use mlmf::Error;
use std::collections::HashMap;
use std::path::Path;
fn main() -> Result<(), Error> {
println!("ONNX Export Example");
// Create a device (CPU for this example)
let device = Device::Cpu;
// Create a simple transformer-like model structure
let mut tensors = HashMap::new();
// Model dimensions
let vocab_size = 32000;
let hidden_size = 4096;
let intermediate_size = 11008;
let num_heads = 32;
let head_dim = hidden_size / num_heads;
let num_layers = 32;
// Embedding layers
tensors.insert(
"model.embed_tokens.weight".to_string(),
Tensor::randn(0f32, 1f32, (vocab_size, hidden_size), &device)?,
);
// Transformer layers
for layer_idx in 0..num_layers {
let prefix = format!("model.layers.{}", layer_idx);
// Attention weights
tensors.insert(
format!("{}.self_attn.q_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (hidden_size, hidden_size), &device)?,
);
tensors.insert(
format!("{}.self_attn.k_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (hidden_size, hidden_size), &device)?,
);
tensors.insert(
format!("{}.self_attn.v_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (hidden_size, hidden_size), &device)?,
);
tensors.insert(
format!("{}.self_attn.o_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (hidden_size, hidden_size), &device)?,
);
// Feed-forward weights
tensors.insert(
format!("{}.mlp.gate_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (intermediate_size, hidden_size), &device)?,
);
tensors.insert(
format!("{}.mlp.up_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (intermediate_size, hidden_size), &device)?,
);
tensors.insert(
format!("{}.mlp.down_proj.weight", prefix),
Tensor::randn(0f32, 1f32, (hidden_size, intermediate_size), &device)?,
);
// Layer normalization
tensors.insert(
format!("{}.input_layernorm.weight", prefix),
Tensor::ones((hidden_size,), DType::F32, &device)?,
);
tensors.insert(
format!("{}.post_attention_layernorm.weight", prefix),
Tensor::ones((hidden_size,), DType::F32, &device)?,
);
}
// Final layer norm and output projection
tensors.insert(
"model.norm.weight".to_string(),
Tensor::ones((hidden_size,), DType::F32, &device)?,
);
tensors.insert(
"lm_head.weight".to_string(),
Tensor::randn(0f32, 1f32, (vocab_size, hidden_size), &device)?,
);
println!("Created model with {} tensors", tensors.len());
// Export to ONNX
let output_path = Path::new("test_transformer.onnx");
println!("Exporting to ONNX format...");
// Use high-level save function
use mlmf::{save_model, SaveOptions};
let save_options = SaveOptions::default();
match save_model(&tensors, output_path, &save_options) {
Ok(_) => {
println!(
"✅ Successfully exported model to: {}",
output_path.display()
);
// Print file size
if let Ok(metadata) = std::fs::metadata(output_path) {
let size_mb = metadata.len() as f64 / (1024.0 * 1024.0);
println!("📁 File size: {:.2} MB", size_mb);
}
}
Err(e) => {
eprintln!("❌ Failed to export ONNX model: {}", e);
return Err(e);
}
}
println!("\nONNX Export completed successfully!");
Ok(())
}