vkml 0.0.2

High-level Vulkan-based machine learning library
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
use crate::{
    instruction::{self, Instruction},
    tensor::TensorDesc,
    tensor_graph::{TensorGraph, TensorId},
    utils::{OnnxAutoPad, error::VKMLError},
    weight_initialiser::Initialiser,
};
use onnx_extractor::{AttributeValue, OnnxModel, OnnxOperation, TensorData};
use std::collections::HashMap;

/// Convert ONNX model to TensorGraph
pub fn parse_onnx_model(
    mut onnx_model: OnnxModel,
    batch_size: i64,
) -> Result<(TensorGraph, Vec<Initialiser>), VKMLError> {
    let mut tensor_descs = Vec::new();
    let mut initialisers = Vec::new();
    let mut operations: Vec<Box<dyn Instruction>> = Vec::new();
    let mut tensor_name_to_id: HashMap<String, TensorId> = HashMap::new();

    let mut memory_requirements = 0;

    // Create tensors from ONNX model
    for (name, onnx_tensor) in onnx_model.drain_tensors() {
        let mut dims = onnx_tensor.shape().to_vec();

        // Replace -1 in first dimension with batch_size
        if let Some(first) = dims.first_mut()
            && *first == -1
        {
            *first = batch_size;
        }

        let onnx_tensor_desc = TensorDesc::new(dims, onnx_tensor.data_type());
        memory_requirements += onnx_tensor_desc.size_in_bytes();

        tensor_descs.push(onnx_tensor_desc.clone());

        // Extract tensor data using into_data() for zero-copy
        // Since we are the only holders of OnnxModel, Cow into_owned will not copy
        let initialiser = onnx_tensor
            .into_data()
            .ok()
            .map(|data| match data {
                TensorData::Raw(bytes) => Initialiser::Bytes(bytes),
                TensorData::Strings(parts) => Initialiser::VecBytes(parts),
                TensorData::F32(v) => Initialiser::VecF32(v),
                TensorData::F64(v) => Initialiser::VecF64(v),
                TensorData::I32(v) => Initialiser::VecI32(v),
                TensorData::I64(v) => Initialiser::VecI64(v),
                TensorData::U64(v) => Initialiser::VecU64(v),
            })
            .unwrap_or(Initialiser::None);

        initialisers.push(initialiser);

        tensor_name_to_id.insert(name.clone(), tensor_descs.len() - 1);
    }

    // Create operations from ONNX nodes; fail fast if an op isn't supported
    for onnx_op in onnx_model.operations() {
        let instruction = convert_onnx_operation_to_instruction(
            onnx_op,
            &tensor_name_to_id,
            &initialisers,
            &tensor_descs,
        )?;
        operations.push(instruction);
    }

    // Map input/output tensor names to IDs
    let input_tensor_ids: Vec<TensorId> = onnx_model
        .inputs()
        .iter()
        .filter_map(|name| tensor_name_to_id.get(name).copied())
        .collect();

    let output_tensor_ids: Vec<TensorId> = onnx_model
        .outputs()
        .iter()
        .filter_map(|name| tensor_name_to_id.get(name).copied())
        .collect();

    let tensor_to_layer = vec![None; tensor_descs.len()];
    let operation_to_layer = vec![0; operations.len()];

    Ok((
        TensorGraph {
            tensor_descs,
            operations,
            input_tensor_ids,
            output_tensor_ids,
            tensor_to_layer,
            operation_to_layer,
            memory_requirements,
        },
        initialisers,
    ))
}

fn convert_onnx_operation_to_instruction(
    onnx_op: &OnnxOperation,
    tensor_map: &HashMap<String, TensorId>,
    initialisers: &[Initialiser],
    tensor_descs: &[TensorDesc],
) -> Result<Box<dyn Instruction>, VKMLError> {
    // Resolve tensor names to IDs
    let input_ids = onnx_op
        .inputs()
        .iter()
        .map(|name| {
            tensor_map.get(name).copied().ok_or_else(|| {
                VKMLError::OnnxImporter(format!(
                    "Input tensor '{}' not found for operation '{}'",
                    name,
                    onnx_op.name()
                ))
            })
        })
        .collect::<Result<Vec<TensorId>, VKMLError>>()?;

    let output_ids = onnx_op
        .outputs()
        .iter()
        .map(|name| {
            tensor_map.get(name).copied().ok_or_else(|| {
                VKMLError::OnnxImporter(format!(
                    "Output tensor '{}' not found for operation '{}'",
                    name,
                    onnx_op.name()
                ))
            })
        })
        .collect::<Result<Vec<TensorId>, VKMLError>>()?;

    match &*onnx_op.op_type() {
        "MatMul" => Ok(instruction::matmul(
            input_ids[0],
            input_ids[1],
            output_ids[0],
        )),
        "Gemm" => {
            // GEMM: General Matrix Multiplication
            // Y = alpha * A' * B' + beta * C
            // Required inputs: A, B
            // Optional input: C (can be empty string in ONNX)
            // Attributes: alpha (default 1.0), beta (default 1.0), transA (default 0), transB (default 0)

            let alpha = onnx_op
                .attributes()
                .get("alpha")
                .and_then(attr_to_float)
                .unwrap_or(1.0);

            let beta = onnx_op
                .attributes()
                .get("beta")
                .and_then(attr_to_float)
                .unwrap_or(1.0);

            let trans_a = onnx_op
                .attributes()
                .get("transA")
                .and_then(attr_to_int)
                .unwrap_or(0)
                != 0;

            let trans_b = onnx_op
                .attributes()
                .get("transB")
                .and_then(attr_to_int)
                .unwrap_or(0)
                != 0;

            // C is optional - check if we have 3 inputs
            let c_id = if input_ids.len() >= 3 {
                Some(input_ids[2])
            } else {
                None
            };

            Ok(instruction::gemm(
                input_ids[0],  // A
                input_ids[1],  // B
                c_id,          // C (optional)
                output_ids[0], // Y
                alpha,
                beta,
                trans_a,
                trans_b,
            ))
        }
        "Concat" => {
            let axis = if let Some(a) = onnx_op.attributes().get("axis") {
                attr_to_int(a).ok_or_else(|| {
                    VKMLError::OnnxImporter("Concat: 'axis' attribute must be an int".to_string())
                })? as usize
            } else {
                0usize
            };

            Ok(instruction::concat(input_ids, output_ids[0], axis))
        }
        "Reshape" => {
            let shape_id = input_ids[1];
            let raw = initialisers[shape_id].as_slice();

            if !raw.len().is_multiple_of(8) {
                return Err(VKMLError::OnnxImporter(format!(
                    "Reshape: shape initializer has invalid raw byte length {}",
                    raw.len()
                )));
            }

            let mut shape_vec: Vec<i64> = Vec::with_capacity(raw.len() / 8);
            for chunk in raw.chunks_exact(8) {
                let mut a = [0u8; 8];
                a.copy_from_slice(chunk);
                shape_vec.push(i64::from_le_bytes(a));
            }

            let allowzero = onnx_op.attributes().get("allowzero").and_then(attr_to_int);
            Ok(instruction::reshape(
                input_ids[0],
                output_ids[0],
                shape_vec,
                allowzero,
            ))
        }
        "Expand" => {
            let shape_id = input_ids[1];
            let raw = initialisers[shape_id].as_slice();

            if !raw.len().is_multiple_of(8) {
                return Err(VKMLError::OnnxImporter(format!(
                    "Expand: shape initializer has invalid raw byte length {}",
                    raw.len()
                )));
            }

            let mut shape_vec: Vec<i64> = Vec::with_capacity(raw.len() / 8);
            for chunk in raw.chunks_exact(8) {
                let mut a = [0u8; 8];
                a.copy_from_slice(chunk);
                shape_vec.push(i64::from_le_bytes(a));
            }

            Ok(instruction::expand(input_ids[0], output_ids[0], shape_vec))
        }
        "Shape" => {
            // optional attributes 'start' and 'end'
            let start = onnx_op.attributes().get("start").and_then(attr_to_int);
            let end = onnx_op.attributes().get("end").and_then(attr_to_int);

            Ok(instruction::shape(input_ids[0], output_ids[0], start, end))
        }
        "Sigmoid" => Ok(instruction::sigmoid(input_ids[0], output_ids[0])),
        "Softmax" => {
            let axis = onnx_op.attributes().get("axis").and_then(attr_to_int);
            Ok(instruction::softmax(input_ids[0], output_ids[0], axis))
        }
        "Identity" => Ok(instruction::identity(input_ids[0], output_ids[0])),
        "MaxPool" => {
            // Parse attributes similar to Conv: kernel_shape, pads, strides, dilations, auto_pad, ceil_mode
            let strides = onnx_op
                .attributes()
                .get("strides")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let dilations = onnx_op
                .attributes()
                .get("dilations")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let kernel_shape = onnx_op
                .attributes()
                .get("kernel_shape")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let pads = onnx_op
                .attributes()
                .get("pads")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let auto_pad = onnx_op
                .attributes()
                .get("auto_pad")
                .and_then(attr_to_string)
                .map(|s| match s.as_str() {
                    "VALID" => OnnxAutoPad::Valid,
                    "SAME_UPPER" => OnnxAutoPad::SameUpper,
                    "SAME_LOWER" => OnnxAutoPad::SameLower,
                    _ => OnnxAutoPad::NotSet,
                })
                .unwrap_or(OnnxAutoPad::NotSet);
            let ceil_mode = onnx_op
                .attributes()
                .get("ceil_mode")
                .and_then(attr_to_int)
                .map(|i| i != 0)
                .unwrap_or(false);

            Ok(instruction::maxpool(
                input_ids[0],
                output_ids[0],
                auto_pad,
                dilations,
                kernel_shape,
                pads,
                strides,
                ceil_mode,
            ))
        }
        "ReduceMean" => {
            let keepdims = onnx_op
                .attributes()
                .get("keepdims")
                .and_then(attr_to_int)
                .unwrap_or(1);
            let noop_with_empty_axes = onnx_op
                .attributes()
                .get("noop_with_empty_axes")
                .and_then(attr_to_int)
                .unwrap_or(0);

            // axes may be provided as second input (initializer). If present and has bytes, parse i64s
            let axes = if input_ids.len() >= 2 {
                let axes_id = input_ids[1];
                let raw = initialisers[axes_id].as_slice();
                if raw.len().is_multiple_of(8) {
                    let mut v = Vec::new();
                    for chunk in raw.chunks_exact(8) {
                        let mut a = [0u8; 8];
                        a.copy_from_slice(chunk);
                        v.push(i64::from_le_bytes(a));
                    }
                    Some(v)
                } else {
                    return Err(VKMLError::OnnxImporter(
                        "ReduceMean: axes initializer has invalid length".to_string(),
                    ));
                }
            } else {
                None
            };

            Ok(instruction::reducemean(
                input_ids[0],
                axes,
                keepdims,
                noop_with_empty_axes,
                output_ids[0],
            ))
        }
        "Add" => Ok(instruction::add(input_ids[0], input_ids[1], output_ids[0])),
        "Sub" => Ok(instruction::sub(input_ids[0], input_ids[1], output_ids[0])),
        "Mul" => Ok(instruction::mul(input_ids[0], input_ids[1], output_ids[0])),
        "Div" => Ok(instruction::div(input_ids[0], input_ids[1], output_ids[0])),
        "Max" => Ok(instruction::max(input_ids[0], input_ids[1], output_ids[0])),
        "Min" => Ok(instruction::min(input_ids[0], input_ids[1], output_ids[0])),
        "Relu" => Ok(instruction::relu(input_ids[0], output_ids[0])),
        "Conv" => {
            let weights = input_ids[1];

            let mut kernel_shape: Vec<i64> = Vec::new();
            let mut pads: Vec<i64> = Vec::new();

            let strides = onnx_op
                .attributes()
                .get("strides")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let dilations = onnx_op
                .attributes()
                .get("dilations")
                .and_then(attr_to_vec)
                .unwrap_or_default();
            let groups = onnx_op
                .attributes()
                .get("groups")
                .and_then(attr_to_int)
                .unwrap_or(1);

            if let Some(val) = onnx_op.attributes().get("kernel_shape")
                && let Some(v) = attr_to_vec(val)
            {
                kernel_shape = v;
            } else {
                // If kernel_shape is not in attributes, infer from weight tensor shape
                // Weight tensor shape is typically [M, C/group, k_h, k_w] for 2D conv
                let weight_desc = &tensor_descs[weights];
                let weight_dims = weight_desc.dims();
                if weight_dims.len() >= 3 {
                    // For 2D/3D conv: weight is [M, C/group, k_h, k_w] or [M, C/group, k_d, k_h, k_w]
                    // kernel_shape should be the spatial dimensions
                    kernel_shape = weight_dims[2..].to_vec();
                }
            }

            // Parse auto_pad per ONNX (default NOTSET)
            let mut auto_pad: Option<OnnxAutoPad> = None;
            if let Some(val) = onnx_op.attributes().get("auto_pad")
                && let AttributeValue::String(s) = val
            {
                auto_pad = match s.as_str() {
                    "VALID" => Some(OnnxAutoPad::Valid),
                    "SAME_UPPER" => Some(OnnxAutoPad::SameUpper),
                    "SAME_LOWER" => Some(OnnxAutoPad::SameLower),
                    "NOTSET" | "" => Some(OnnxAutoPad::NotSet),
                    _ => None,
                };
            }
            let auto_pad_val = auto_pad.unwrap_or(OnnxAutoPad::NotSet);

            // pads: only allowed when auto_pad == NOTSET
            if let Some(val) = onnx_op.attributes().get("pads") {
                if auto_pad_val != OnnxAutoPad::NotSet {
                    return Err(VKMLError::OnnxImporter(
                        "Conv: 'pads' and 'auto_pad' cannot be used together".to_string(),
                    ));
                }
                if let Some(pv) = attr_to_vec(val) {
                    if pv.iter().any(|x| *x < 0) {
                        return Err(VKMLError::OnnxImporter(
                            "Pads must be non-negative for Conv operation".to_string(),
                        ));
                    }
                    if pv.len() % 2 != 0 {
                        return Err(VKMLError::OnnxImporter(
                            "Invalid 'pads' attribute length for Conv operation".to_string(),
                        ));
                    }
                    pads = pv;
                }
            }

            Ok(instruction::conv(
                input_ids[0],
                weights,
                input_ids.get(2).copied(),
                output_ids[0],
                auto_pad_val,
                dilations,
                groups,
                kernel_shape,
                pads,
                strides,
            ))
        }
        unsupported => Err(VKMLError::OnnxImporter(format!(
            "Operation '{}' is not implemented",
            unsupported
        ))),
    }
}

// Helper functions to extract ONNX attribute values
fn attr_to_vec(a: &AttributeValue) -> Option<Vec<i64>> {
    match a {
        AttributeValue::Ints(v) => Some(v.clone()),
        AttributeValue::Int(i) => Some(vec![*i]),
        _ => None,
    }
}

fn attr_to_int(a: &AttributeValue) -> Option<i64> {
    match a {
        AttributeValue::Int(i) => Some(*i),
        _ => None,
    }
}

fn attr_to_string(a: &AttributeValue) -> Option<String> {
    match a {
        AttributeValue::String(s) => Some(s.clone()),
        _ => None,
    }
}

fn attr_to_float(a: &AttributeValue) -> Option<f32> {
    match a {
        AttributeValue::Float(f) => Some(*f),
        _ => None,
    }
}