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
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
use crate::{
    compute::compute_manager::ComputeManager,
    model::layer_connection::{LayerConnection, LayerId},
    tensor::DeviceId,
    tensor_graph::TensorId,
    utils::error::VKMLError,
};
use onnx_extractor::DataType;

pub fn print_model_stats(cm: &ComputeManager) {
    let mut total_memory = 0u64;

    println!("\nModel Statistics");
    println!("================");
    println!("\nBatch Size: {}", cm.model.batch_size);
    println!("\nLayer Details:");
    println!("{:-<125}", "");
    println!(
        "{:<4} {:<12} {:<10} {:<18} {:<18} {:<12} {:<20} Config",
        "ID", "Type", "Memory", "Input Shape", "Output Shape", "Device", "Connections"
    );
    println!("{:-<125}", "");

    let execution_order = match &cm.model.verified {
        Some(verified) => &verified.execution_order,
        None => {
            println!("Warning: Model not verified, execution order may be incorrect");
            return;
        }
    };

    let mut ordered_layer_ids: Vec<LayerId> = execution_order.to_vec();
    ordered_layer_ids.sort();

    for &layer_id in &ordered_layer_ids {
        if let Some(layer) = cm.model.layers.get(&layer_id) {
            let layer_tensor_ids: Vec<TensorId> = (0..cm.tensors.len())
                .filter(|&id| cm.tensor_graph.tensor_to_layer.get(id) == Some(&Some(layer_id)))
                .collect();

            let is_output_layer = layer.output_connections.is_empty();

            let layer_output_tensors: Vec<TensorId> = if is_output_layer {
                cm.tensor_graph
                    .output_tensor_ids
                    .iter()
                    .filter(|&&id| cm.tensor_graph.tensor_to_layer.get(id) == Some(&Some(layer_id)))
                    .cloned()
                    .collect()
            } else {
                // For non-output layers, find tensors that other layers consume
                layer_tensor_ids
                    .iter()
                    .filter(|&&tensor_id| {
                        cm.tensor_graph
                            .get_tensor_consumers(tensor_id)
                            .iter()
                            .any(|&op_id| {
                                let op_inputs = cm.tensor_graph.get_operation_inputs(op_id);
                                let first_input = op_inputs.first().cloned();
                                let op_layer = first_input.and_then(|id| {
                                    cm.tensor_graph.tensor_to_layer.get(id).cloned().flatten()
                                });
                                op_layer.is_some() && op_layer != Some(layer_id)
                            })
                    })
                    .cloned()
                    .collect()
            };

            // Get representative output tensor for shape info
            let output_tensor = if !layer_output_tensors.is_empty() {
                layer_output_tensors[0]
            } else if !layer_tensor_ids.is_empty() {
                // No explicit outputs, use any tensor (preferably one produced by an operation)
                *layer_tensor_ids
                    .iter()
                    .find(|&&id| !cm.tensor_graph.get_tensor_producers(id).is_empty())
                    .unwrap_or(&layer_tensor_ids[0])
            } else {
                // No tensors found at all - should never happen
                continue;
            };

            let input_shapes_str = if layer.input_connections.is_empty() {
                "None".to_string()
            } else {
                layer
                    .input_connections
                    .iter()
                    .filter_map(|conn| {
                        let source_layer_id = conn.get_layerid();
                        let output_idx = conn.get_outputidx();

                        let connected_layer_outputs = get_layer_output_tensors(cm, source_layer_id);

                        if output_idx < connected_layer_outputs.len() {
                            let output_tensor_id = connected_layer_outputs[output_idx];
                            if output_tensor_id < cm.tensors.len() {
                                let tensor = cm.tensor_read(output_tensor_id);
                                let dims = tensor.desc().dims();
                                return Some(format_dimensions(dims));
                            }
                        }
                        None
                    })
                    .collect::<Vec<_>>()
                    .join(", ")
            };

            let output_shapes_str = if output_tensor < cm.tensors.len() {
                let tensor = cm.tensor_read(output_tensor);
                format_dimensions(tensor.desc().dims())
            } else {
                "Unknown".to_string()
            };

            let connections_str =
                format_layer_connections(&layer.input_connections, &layer.output_connections);

            let memory_bytes = layer_tensor_ids
                .iter()
                .map(|&id| cm.tensor_read(id).desc().size_in_bytes() as u64)
                .sum();

            let device_location = match cm.tensor_read(output_tensor).device() {
                DeviceId::Cpu => "CPU".to_string(),
                DeviceId::Gpu(gpu_idx) => format!("GPU {}", gpu_idx),
            };

            let layer_type = layer.layer.name();
            let layer_config = layer.layer.config_string().unwrap_or_default();

            println!(
                "{:<4} {:<12} {:<10} {:<18} {:<18} {:<12} {:<20} {}",
                layer_id,
                layer_type,
                cm.format_memory_mb(memory_bytes),
                input_shapes_str,
                output_shapes_str,
                device_location,
                connections_str,
                layer_config
            );

            total_memory += memory_bytes;
        }
    }

    println!("{:-<125}", "");

    let mut entry_points = cm
        .tensor_graph
        .input_tensor_ids
        .iter()
        .filter_map(|&id| cm.tensor_graph.tensor_to_layer.get(id).cloned().flatten())
        .collect::<Vec<_>>();

    let mut exit_points = cm
        .tensor_graph
        .output_tensor_ids
        .iter()
        .filter_map(|&id| cm.tensor_graph.tensor_to_layer.get(id).cloned().flatten())
        .collect::<Vec<_>>();

    entry_points.sort();
    exit_points.sort();
    entry_points.dedup();
    exit_points.dedup();

    println!("\nGraph Structure:");
    println!("Entry points: {:?}", entry_points);
    println!("Exit points: {:?}", exit_points);

    println!("\nModel Summary:");
    println!("Total Model Memory: {}", cm.format_memory_mb(total_memory));

    println!("\nMemory Allocation:");
    for (device, used, available) in cm.get_memory_usage_summary() {
        println!("{} Memory Used: {}", device, used);
        println!("{} Memory Available: {}", device, available);
    }
}

fn format_dimensions(dims: &[i64]) -> String {
    if dims.len() <= 4 {
        dims.iter()
            .map(|&d| d.to_string())
            .collect::<Vec<_>>()
            .join("×")
    } else {
        format!("{}d tensor", dims.len())
    }
}

fn get_layer_output_tensors(cm: &ComputeManager, layer_id: LayerId) -> Vec<TensorId> {
    // Get all tensors belonging to this layer
    let layer_tensors: Vec<TensorId> = (0..cm.tensors.len())
        .filter(|&id| cm.tensor_graph.tensor_to_layer.get(id) == Some(&Some(layer_id)))
        .collect();

    // First check explicit output tensors
    let explicit_outputs: Vec<TensorId> = cm
        .tensor_graph
        .output_tensor_ids
        .iter()
        .filter(|&&id| cm.tensor_graph.tensor_to_layer.get(id) == Some(&Some(layer_id)))
        .cloned()
        .collect();

    if !explicit_outputs.is_empty() {
        return explicit_outputs;
    }

    let mut outputs = Vec::new();

    if let Some(layer) = cm.model.layers.get(&layer_id) {
        if layer.output_connections.is_empty() {
            // This is a final layer, so all its tensors could be outputs
            outputs.extend(layer_tensors.clone());
        } else {
            for &tensor_id in &layer_tensors {
                let consumers = cm.tensor_graph.get_tensor_consumers(tensor_id);
                let is_consumed_by_other_layer = consumers.iter().any(|&op_id| {
                    let op_inputs = cm.tensor_graph.get_operation_inputs(op_id);
                    let first_input = op_inputs.first().cloned();
                    let op_layer = first_input
                        .and_then(|id| cm.tensor_graph.tensor_to_layer.get(id).cloned().flatten());
                    op_layer.is_some() && op_layer != Some(layer_id)
                });

                if is_consumed_by_other_layer {
                    outputs.push(tensor_id);
                }
            }
        }
    }

    // If we still haven't found outputs, include all tensors that have producers
    if outputs.is_empty() {
        for &tensor_id in &layer_tensors {
            if !cm.tensor_graph.get_tensor_producers(tensor_id).is_empty() {
                outputs.push(tensor_id);
            }
        }
    }

    outputs
}

pub fn print_layer_values(cm: &ComputeManager, layer_id: LayerId) -> Result<(), VKMLError> {
    let layer = cm
        .model
        .layers
        .get(&layer_id)
        .ok_or(VKMLError::ComputeManager(format!(
            "Layer ID {} not found",
            layer_id
        )))?;

    println!("\nLayer {} Values ({})", layer_id, layer.layer.name());
    println!("{:-<120}", "");
    println!("Input connections: {:?}", layer.input_connections);
    println!("Output connections: {:?}", layer.output_connections);

    let format_array = |arr: &[f32], max_items: usize| {
        let mut s = String::from("[");
        for (i, val) in arr.iter().take(max_items).enumerate() {
            if i > 0 {
                s.push_str(", ");
            }
            s.push_str(&format!("{:.6}", val));
        }
        if arr.len() > max_items {
            s.push_str(", ...")
        }
        s.push(']');
        s
    };

    let print_tensor_stats = |data: &[f32]| {
        if !data.is_empty() {
            let min_val = data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
            let max_val = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
            let mean = data.iter().sum::<f32>() / data.len() as f32;
            let variance =
                data.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / data.len() as f32;
            let std_dev = variance.sqrt();

            println!("  Stats:");
            println!("    Min: {:.6}", min_val);
            println!("    Max: {:.6}", max_val);
            println!("    Mean: {:.6}", mean);
            println!("    Std Dev: {:.6}", std_dev);

            let non_zero = data.iter().filter(|&&x| x != 0.0).count();
            println!(
                "    Non-zero elements: {} ({:.2}%)",
                non_zero,
                (non_zero as f32 / data.len() as f32) * 100.0
            );
        }
    };

    // Helper: convert raw bytes to f32 vector based on DataType
    fn bytes_to_f32_vec(raw: &[u8], dtype: DataType) -> Option<Vec<f32>> {
        match dtype {
            DataType::Uint8 => Some(raw.iter().map(|&b| b as f32).collect()),
            DataType::Int8 => Some(raw.iter().map(|&b| (b as i8) as f32).collect()),
            DataType::Uint16 => {
                if !raw.len().is_multiple_of(2) {
                    return None;
                }
                Some(
                    raw.chunks_exact(2)
                        .map(|c| u16::from_le_bytes([c[0], c[1]]) as f32)
                        .collect(),
                )
            }
            DataType::Int16 => {
                if !raw.len().is_multiple_of(2) {
                    return None;
                }
                Some(
                    raw.chunks_exact(2)
                        .map(|c| i16::from_le_bytes([c[0], c[1]]) as f32)
                        .collect(),
                )
            }
            DataType::Uint32 => {
                if !raw.len().is_multiple_of(4) {
                    return None;
                }
                Some(
                    raw.chunks_exact(4)
                        .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32)
                        .collect(),
                )
            }
            DataType::Int32 => {
                if !raw.len().is_multiple_of(4) {
                    return None;
                }
                Some(
                    raw.chunks_exact(4)
                        .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32)
                        .collect(),
                )
            }
            DataType::Uint64 => {
                if !raw.len().is_multiple_of(8) {
                    return None;
                }
                Some(
                    raw.chunks_exact(8)
                        .map(|c| {
                            u64::from_le_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]])
                                as f32
                        })
                        .collect(),
                )
            }
            DataType::Int64 => {
                if !raw.len().is_multiple_of(8) {
                    return None;
                }
                Some(
                    raw.chunks_exact(8)
                        .map(|c| {
                            i64::from_le_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]])
                                as f32
                        })
                        .collect(),
                )
            }
            DataType::Float => {
                if !raw.len().is_multiple_of(4) {
                    return None;
                }
                Some(
                    raw.chunks_exact(4)
                        .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
                        .collect(),
                )
            }
            DataType::Double => {
                if !raw.len().is_multiple_of(8) {
                    return None;
                }
                Some(
                    raw.chunks_exact(8)
                        .map(|c| {
                            f64::from_le_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]])
                                as f32
                        })
                        .collect(),
                )
            }
            _ => None,
        }
    }

    // Get all tensors belonging to this layer
    let tensor_ids: Vec<TensorId> = (0..cm.tensors.len())
        .filter(|&id| cm.tensor_graph.tensor_to_layer.get(id) == Some(&Some(layer_id)))
        .collect();

    for &tensor_id in &tensor_ids {
        let tensor = cm.tensor_read(tensor_id);
        let raw = tensor.read();
        let gpu_idx = match tensor.device() {
            DeviceId::Gpu(idx) => Some(idx),
            _ => None,
        };
        let dtype = tensor.desc().data_type();

        println!("\nTensor {}:", tensor_id);
        println!(
            "  Location: {}",
            match gpu_idx {
                Some(idx) => format!("GPU {}", idx),
                None => "CPU".to_string(),
            }
        );
        println!("  Shape: {:?}", tensor.desc().dims());
        println!(
            "  Size in memory: {}",
            cm.format_memory_mb(tensor.desc().size_in_bytes() as u64)
        );

        if let Some(values) = bytes_to_f32_vec(&raw, dtype) {
            println!("  Values: {}", format_array(&values, 10));
            print_tensor_stats(&values);
        } else {
            // Fallback: show raw bytes preview in hex
            let preview_len = raw.len().min(32);
            let preview = raw
                .iter()
                .take(preview_len)
                .map(|b| format!("{:02x}", b))
                .collect::<Vec<_>>()
                .join(" ");
            println!(
                "  Values: <raw bytes, dtype={:?}, len={}> {}",
                dtype,
                raw.len(),
                preview
            );
        }
    }

    println!("\nLayer Output Tensors:");

    let output_tensors: Vec<_> = tensor_ids
        .iter()
        .filter(|&&id| {
            cm.tensor_graph.output_tensor_ids.contains(&id)
                || cm
                    .tensor_graph
                    .get_tensor_consumers(id)
                    .iter()
                    .any(|&op_id| {
                        let op_inputs = cm.tensor_graph.get_operation_inputs(op_id);
                        let first_input = op_inputs.first().cloned();
                        let op_layer = first_input.and_then(|id| {
                            cm.tensor_graph.tensor_to_layer.get(id).cloned().flatten()
                        });
                        op_layer.is_some() && op_layer != Some(layer_id)
                    })
        })
        .cloned()
        .collect();

    if output_tensors.is_empty() {
        println!("  No explicit output tensors found");
    } else {
        for &tensor_id in &output_tensors {
            let tensor = cm.tensor_read(tensor_id);
            println!("  Tensor {} Shape: {:?}", tensor_id, tensor.desc().dims());
        }
    }

    Ok(())
}

fn format_layer_connections(inputs: &[LayerConnection], outputs: &[LayerConnection]) -> String {
    // Maintain original order of connections
    let in_ids: Vec<String> = inputs
        .iter()
        .map(|conn| match conn {
            LayerConnection::DefaultOutput(id) => format!("{}:0", id),
            LayerConnection::SpecificOutput(id, idx) => format!("{}:{}", id, idx),
        })
        .collect();

    let out_ids: Vec<String> = outputs
        .iter()
        .map(|conn| match conn {
            LayerConnection::DefaultOutput(id) => format!("{}:0", id),
            LayerConnection::SpecificOutput(id, idx) => format!("{}:{}", id, idx),
        })
        .collect();

    if in_ids.is_empty() && out_ids.is_empty() {
        return "None".to_string();
    }

    let mut result = String::new();

    if !in_ids.is_empty() {
        result.push_str(&format!("in:[{}]", in_ids.join(",")));
    }

    if !out_ids.is_empty() {
        if !result.is_empty() {
            result.push(' ');
        }
        result.push_str(&format!("out:[{}]", out_ids.join(",")));
    }

    result
}