instmodel_inference 0.8.0

High-performance neural network inference library with instruction-based execution
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
//! Performance benchmark binary comparing framework vs manual neural network inference.
//!
//! This binary measures the performance of the instruction-based neural network framework
//! against manual implementations to understand the overhead and efficiency characteristics.

use instmodel_inference::benchmarks::instructions::dot_product::manual::DotProductTestData;
use instmodel_inference::instruction_model_info::*;
use instmodel_inference::{Activation, InstructionModel};
use log::{error, info};
use std::time::Instant;

/// Manual implementation of the same neural network for comparison
struct ManualNeuralNetwork {
    weights_layer1: Vec<Vec<f32>>,
    bias_layer1: Vec<f32>,
    weights_layer2: Vec<Vec<f32>>,
    bias_layer2: Vec<f32>,
}

impl ManualNeuralNetwork {
    fn new(
        weights_layer1: Vec<Vec<f32>>,
        bias_layer1: Vec<f32>,
        weights_layer2: Vec<Vec<f32>>,
        bias_layer2: Vec<f32>,
    ) -> Self {
        Self {
            weights_layer1,
            bias_layer1,
            weights_layer2,
            bias_layer2,
        }
    }

    /// Manual forward pass: input -> hidden (ReLU) -> output (Sigmoid)
    fn predict(&self, input: &[f32]) -> Vec<f32> {
        // Layer 1: Dense + ReLU
        let mut hidden = vec![0.0f32; self.weights_layer1.len()];
        for (i, weights_row) in self.weights_layer1.iter().enumerate() {
            let mut sum = self.bias_layer1[i];
            for (j, &weight) in weights_row.iter().enumerate() {
                sum += weight * input[j];
            }
            // ReLU activation
            hidden[i] = if sum > 0.0 { sum } else { 0.0 };
        }

        // Layer 2: Dense + Sigmoid
        let mut output = vec![0.0f32; self.weights_layer2.len()];
        for (i, weights_row) in self.weights_layer2.iter().enumerate() {
            let mut sum = self.bias_layer2[i];
            for (j, &weight) in weights_row.iter().enumerate() {
                sum += weight * hidden[j];
            }
            // Sigmoid activation
            output[i] = 1.0 / (1.0 + (-sum).exp());
        }

        output
    }

    /// Manual forward pass with pre-allocated buffers for fair comparison
    fn predict_with_buffers(
        &self,
        input: &[f32],
        hidden_buffer: &mut [f32],
        output_buffer: &mut [f32],
    ) {
        // Layer 1: Dense + ReLU
        for (i, weights_row) in self.weights_layer1.iter().enumerate() {
            let mut sum = self.bias_layer1[i];
            for (j, &weight) in weights_row.iter().enumerate() {
                sum += weight * input[j];
            }
            // ReLU activation
            hidden_buffer[i] = if sum > 0.0 { sum } else { 0.0 };
        }

        // Layer 2: Dense + Sigmoid
        for (i, weights_row) in self.weights_layer2.iter().enumerate() {
            let mut sum = self.bias_layer2[i];
            for (j, &weight) in weights_row.iter().enumerate() {
                sum += weight * hidden_buffer[j];
            }
            // Sigmoid activation
            output_buffer[i] = 1.0 / (1.0 + (-sum).exp());
        }
    }
}

/// Performance measurement structure
#[derive(Debug)]
struct PerformanceResults {
    method: String,
    total_time_ns: u128,
    average_time_ns: u128,
    average_time_ms: f64,
    num_executions: u32,
}

impl PerformanceResults {
    fn new(method: String, total_time_ns: u128, num_executions: u32) -> Self {
        let average_time_ns = total_time_ns / num_executions as u128;
        let average_time_ms = average_time_ns as f64 / 1_000_000.0;

        Self {
            method,
            total_time_ns,
            average_time_ns,
            average_time_ms,
            num_executions,
        }
    }

    fn overhead_ratio(&self, baseline: &PerformanceResults) -> f64 {
        self.average_time_ns as f64 / baseline.average_time_ns as f64
    }

    fn overhead_percentage(&self, baseline: &PerformanceResults) -> f64 {
        (self.overhead_ratio(baseline) - 1.0) * 100.0
    }
}

fn create_test_data() -> DotProductTestData {
    // Create weights for layer 1: 10000 outputs x 2 inputs
    let weights_layer1: Vec<Vec<f32>> = (0..10000)
        .map(|i| vec![0.1 + (i as f32) * 0.0001, 0.2 + (i as f32) * 0.0001])
        .collect();

    // Create bias for layer 1: 10000 values
    let bias_layer1: Vec<f32> = (0..10000).map(|i| 0.01 + (i as f32) * 0.00001).collect();

    // Create weights for layer 2: 10 outputs x 10000 inputs
    let weights_layer2: Vec<Vec<f32>> = (0..10)
        .map(|i| {
            (0..10000)
                .map(|j| 0.001 + (i as f32 + j as f32) * 0.000001)
                .collect()
        })
        .collect();

    // Create bias for layer 2: 10 values
    let bias_layer2: Vec<f32> = (0..10).map(|i| 0.1 + (i as f32) * 0.01).collect();

    // Test inputs
    let inputs = vec![0.5, -0.3];

    (
        weights_layer1,
        bias_layer1,
        weights_layer2,
        bias_layer2,
        inputs,
    )
}

fn create_framework_model(
    weights_layer1: &[Vec<f32>],
    bias_layer1: &[f32],
    weights_layer2: &[Vec<f32>],
    bias_layer2: &[f32],
) -> InstructionModel {
    let computation_buffer_sizes = vec![2, 10000, 10];

    let instructions = vec![
        InstructionInfo::Dot(DotInstructionInfo {
            input: 0,
            output: 1,
            weights: 0,
            activation: Some(Activation::Relu),
        }),
        InstructionInfo::Dot(DotInstructionInfo {
            input: 1,
            output: 2,
            weights: 1,
            activation: Some(Activation::Sigmoid),
        }),
    ];

    let model_info = InstructionModelInfo {
        features: Some(vec!["input1".to_string(), "input2".to_string()]),
        feature_size: None,
        computation_buffer_sizes,
        instructions,
        weights: vec![weights_layer1.to_vec(), weights_layer2.to_vec()],
        bias: vec![bias_layer1.to_vec(), bias_layer2.to_vec()],
        parameters: None,
        maps: None,
        validation_data: None,
    };

    InstructionModel::new(model_info).expect("Model creation should succeed")
}

fn benchmark_method<F>(name: &str, num_executions: u32, mut benchmark_fn: F) -> PerformanceResults
where
    F: FnMut(),
{
    println!("Benchmarking {} ({} executions)...", name, num_executions);

    // Warm-up
    for _ in 0..5 {
        benchmark_fn();
    }

    let start = Instant::now();
    for i in 0..num_executions {
        benchmark_fn();
        if (i + 1) % (num_executions / 10) == 0 {
            println!("  Progress: {}/{}", i + 1, num_executions);
        }
    }
    let duration = start.elapsed();

    PerformanceResults::new(name.to_string(), duration.as_nanos(), num_executions)
}

fn verify_outputs_match(manual_output: &[f32], framework_output: &[f32]) -> bool {
    const EPSILON: f32 = 1e-6;
    if manual_output.len() != framework_output.len() {
        return false;
    }
    for (manual, framework) in manual_output.iter().zip(framework_output.iter()) {
        if (manual - framework).abs() > EPSILON {
            println!(
                "Output mismatch: manual={}, framework={}, diff={}",
                manual,
                framework,
                (manual - framework).abs()
            );
            return false;
        }
    }
    true
}

fn main() {
    // Initialize logger
    env_logger::init();

    info!("{}", "=".repeat(80));
    info!("Neural Network Performance Benchmark");
    info!("Network Architecture: 2 inputs -> 10000 hidden (ReLU) -> 10 outputs (Sigmoid)");
    info!("{}", "=".repeat(80));

    let num_executions = 1000; // More executions for better statistical accuracy
    let (weights_layer1, bias_layer1, weights_layer2, bias_layer2, inputs) = create_test_data();

    // Create models
    let manual_model = ManualNeuralNetwork::new(
        weights_layer1.clone(),
        bias_layer1.clone(),
        weights_layer2.clone(),
        bias_layer2.clone(),
    );
    let framework_model =
        create_framework_model(&weights_layer1, &bias_layer1, &weights_layer2, &bias_layer2);

    // Verify outputs match
    info!("Verifying output consistency between manual and framework implementations...");
    let manual_result = manual_model.predict(&inputs);
    let framework_result = framework_model
        .predict(&inputs)
        .expect("Framework prediction failed");

    if verify_outputs_match(&manual_result, &framework_result) {
        info!("✅ Outputs match - implementations are consistent");
        info!(
            "   Sample values: {:?}",
            &manual_result[..5.min(manual_result.len())]
        );
    } else {
        error!("❌ Outputs do not match - there may be an implementation bug");
        return;
    }

    info!("{}", "=".repeat(80));
    info!("Performance Benchmarks");
    info!("{}", "=".repeat(80));

    // 1. Manual implementation (baseline)
    let manual_results =
        benchmark_method("Manual Implementation (Baseline)", num_executions, || {
            let _result = manual_model.predict(&inputs);
        });

    // 2. Manual implementation with pre-allocated buffers
    let mut hidden_buffer = vec![0.0f32; 10000];
    let mut output_buffer = vec![0.0f32; 10];
    let manual_buffered_results = benchmark_method(
        "Manual Implementation (Pre-allocated buffers)",
        num_executions,
        || {
            manual_model.predict_with_buffers(&inputs, &mut hidden_buffer, &mut output_buffer);
        },
    );

    // 3. Framework with buffer allocation
    let framework_alloc_results = benchmark_method(
        "Framework Implementation (Buffer allocation)",
        num_executions,
        || {
            let _result = framework_model.predict(&inputs).expect("Prediction failed");
        },
    );

    // 4. Framework with pre-allocated buffer
    let required_memory = framework_model.required_memory();
    let mut computation_buffer = vec![0.0f32; required_memory];
    let framework_buffer_results = benchmark_method(
        "Framework Implementation (Pre-allocated buffer)",
        num_executions,
        || {
            // Copy input to buffer
            for (i, &value) in inputs.iter().enumerate() {
                computation_buffer[i] = value;
            }
            framework_model
                .predict_with_buffer(&mut computation_buffer)
                .expect("Prediction failed");
        },
    );

    // Print detailed results
    println!("\n{}", "=".repeat(80));
    println!("Detailed Results");
    println!("{}", "=".repeat(80));

    let results = vec![
        &manual_results,
        &manual_buffered_results,
        &framework_alloc_results,
        &framework_buffer_results,
    ];

    for result in &results {
        println!("\n📊 {}", result.method);
        println!(
            "   Average time: {:.3} ms ({} ns)",
            result.average_time_ms, result.average_time_ns
        );
        println!(
            "   Total time: {:.3} ms",
            result.total_time_ns as f64 / 1_000_000.0
        );
        println!("   Executions: {}", result.num_executions);

        if result.method != manual_results.method {
            println!(
                "   Overhead vs baseline: {:.2}x ({:.1}%)",
                result.overhead_ratio(&manual_results),
                result.overhead_percentage(&manual_results)
            );
        }
    }

    println!("\n{}", "=".repeat(80));
    println!("Performance Analysis");
    println!("{}", "=".repeat(80));

    println!("\n🚀 Speed Rankings (fastest to slowest):");
    let mut sorted_results = results.clone();
    sorted_results.sort_by_key(|r| r.average_time_ns);

    for (i, result) in sorted_results.iter().enumerate() {
        let rank_emoji = match i {
            0 => "🥇",
            1 => "🥈",
            2 => "🥉",
            _ => "  ",
        };
        println!(
            "   {} {}: {:.3} ms",
            rank_emoji, result.method, result.average_time_ms
        );
    }

    println!("\n📈 Framework Overhead Analysis:");
    println!(
        "   Framework (alloc) vs Manual (baseline): {:.2}x overhead ({:.1}%)",
        framework_alloc_results.overhead_ratio(&manual_results),
        framework_alloc_results.overhead_percentage(&manual_results)
    );

    println!(
        "   Framework (buffer) vs Manual (baseline): {:.2}x overhead ({:.1}%)",
        framework_buffer_results.overhead_ratio(&manual_results),
        framework_buffer_results.overhead_percentage(&manual_results)
    );

    println!(
        "   Framework (buffer) vs Manual (buffer): {:.2}x overhead ({:.1}%)",
        framework_buffer_results.overhead_ratio(&manual_buffered_results),
        framework_buffer_results.overhead_percentage(&manual_buffered_results)
    );

    println!("\n💾 Memory Requirements:");
    println!(
        "   Framework buffer size: {} floats ({} KB)",
        required_memory,
        (required_memory * 4) / 1024
    );
    println!(
        "   Manual buffer size: {} floats ({} KB)",
        10000 + 10,
        ((10000 + 10) * 4) / 1024
    );

    println!("\n{}", "=".repeat(80));
    println!("Benchmark Complete");
    println!("{}", "=".repeat(80));
}