scirs2-neural 0.6.4

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
//! Tests for [`super::packager::ModelPackager`] and its host code generation pipeline.

use crate::models::sequential::Sequential;
use std::collections::HashMap;
use std::fs;
use std::process::Command;

use super::codegen::{
    binary_project_cargo_toml, binary_project_main_rs, cdylib_project_cargo_toml,
    cdylib_project_lib_rs, ensure_host_platform, unique_temp_dir,
};
use super::packager::ModelPackager;
use super::types::{
    CBindingConfig, CallingConvention, CpuRequirements, FrameworkConfig, MobileArchitecture,
    MobileConfig, MobileOptimization, MobilePlatform, ModelServer, OptimizationLevel,
    PackageFormat, PackageMetadata, RuntimeRequirements, ServerConfig, TargetPlatform, TensorSpec,
    WasmConfig, WasmImport, WasmMemoryConfig, WasmVersion,
};

use crate::layers::Dense;
use scirs2_core::random::SeedableRng;
use tempfile::TempDir;

fn make_test_model() -> Sequential<f32> {
    let mut rng = scirs2_core::random::rngs::SmallRng::from_seed([42; 32]);
    let mut model: Sequential<f32> = Sequential::new();
    let dense = Dense::new(10, 1, Some("relu"), &mut rng).expect("failed to build Dense layer");
    model.add_layer(dense);
    model
}
#[test]
fn test_package_metadata_creation() {
    let metadata = PackageMetadata {
        name: "test_model".to_string(),
        version: "1.0.0".to_string(),
        description: "Test model".to_string(),
        author: "Test".to_string(),
        license: "Apache-2.0".to_string(),
        platforms: vec!["linux".to_string()],
        dependencies: HashMap::new(),
        input_specs: vec![TensorSpec {
            name: "input".to_string(),
            shape: vec![Some(1), Some(10)],
            dtype: "float32".to_string(),
            description: None,
            range: None,
        }],
        output_specs: vec![TensorSpec {
            name: "output".to_string(),
            shape: vec![Some(1), Some(1)],
            dtype: "float32".to_string(),
            description: None,
            range: None,
        }],
        runtime_requirements: RuntimeRequirements {
            min_memory_mb: 256,
            cpu_requirements: CpuRequirements {
                min_cores: 1,
                instruction_sets: vec!["sse2".to_string()],
                min_frequency_mhz: None,
            },
            gpu_requirements: None,
            system_dependencies: Vec::new(),
        },
        timestamp: "2024-01-01T00:00:00Z".to_string(),
        checksum: "abc123".to_string(),
    };
    assert_eq!(metadata.name, "test_model");
    assert_eq!(metadata.input_specs.len(), 1);
    assert_eq!(metadata.output_specs.len(), 1);
}
#[test]
fn test_model_packager_creation() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    assert_eq!(packager.metadata.name, "scirs2_model");
    assert_eq!(packager.optimization, OptimizationLevel::Basic);
}
#[test]
fn test_tensor_spec() {
    let spec = TensorSpec {
        name: "test_tensor".to_string(),
        shape: vec![Some(32), None, Some(10)],
        dtype: "float32".to_string(),
        description: Some("Test tensor".to_string()),
        range: Some((-1.0, 1.0)),
    };
    assert_eq!(spec.name, "test_tensor");
    assert_eq!(spec.shape.len(), 3);
    assert_eq!(spec.shape[1], None);
    assert!(spec.range.is_some());
}
#[test]
fn test_c_binding_config() {
    let mut type_mappings = HashMap::new();
    type_mappings.insert("f32".to_string(), "float".to_string());
    type_mappings.insert("f64".to_string(), "double".to_string());
    let config = CBindingConfig {
        library_name: "test_lib".to_string(),
        header_guard: "TEST_LIB_H".to_string(),
        namespace: Some("testlib".to_string()),
        calling_convention: CallingConvention::CDecl,
        additional_headers: vec!["math.h".to_string()],
        type_mappings,
    };
    assert_eq!(config.library_name, "test_lib");
    assert_eq!(config.calling_convention, CallingConvention::CDecl);
    assert!(config.namespace.is_some());
    assert_eq!(config.type_mappings.len(), 2);
}
#[test]
fn test_wasm_config() {
    let config = WasmConfig {
        wasm_version: WasmVersion::V1_0,
        enable_simd: true,
        enable_threads: false,
        memory_config: WasmMemoryConfig {
            initial_pages: 256,
            max_pages: Some(1024),
            allow_growth: true,
        },
        imports: vec![WasmImport {
            module: "env".to_string(),
            name: "memory".to_string(),
            signature: "memory".to_string(),
        }],
        exports: vec!["predict".to_string()],
    };
    assert_eq!(config.wasm_version, WasmVersion::V1_0);
    assert!(config.enable_simd);
    assert!(!config.enable_threads);
    assert_eq!(config.memory_config.initial_pages, 256);
    assert_eq!(config.imports.len(), 1);
    assert_eq!(config.exports.len(), 1);
}
#[test]
fn test_mobile_config() {
    let config = MobileConfig {
        platform: MobilePlatform::IOS,
        min_os_version: "12.0".to_string(),
        architecture: MobileArchitecture::ARM64,
        optimization: MobileOptimization {
            enable_quantization: true,
            pruning_level: 0.05,
            memory_optimization: true,
            battery_optimization: true,
        },
        framework_config: FrameworkConfig {
            use_metal: true,
            use_nnapi: false,
            use_gpu: true,
            thread_pool_size: Some(2),
        },
    };
    assert_eq!(config.platform, MobilePlatform::IOS);
    assert_eq!(config.architecture, MobileArchitecture::ARM64);
    assert!(config.optimization.enable_quantization);
    assert!(config.framework_config.use_metal);
    assert!(!config.framework_config.use_nnapi);
}
#[test]
fn test_mobile_architecture_universal_variant_exists() {
    let arch = MobileArchitecture::Universal;
    assert_ne!(arch, MobileArchitecture::ARM64);
}
#[test]
fn test_server_config() {
    let config = ServerConfig {
        port: 8080,
        max_batch_size: 32,
        timeout_seconds: 30,
        enable_logging: true,
        max_concurrent_requests: 100,
    };
    assert_eq!(config.port, 8080);
    assert_eq!(config.max_batch_size, 32);
    assert_eq!(config.timeout_seconds, 30);
    assert!(config.enable_logging);
    assert_eq!(config.max_concurrent_requests, 100);
}
#[test]
fn test_model_server_stats() {
    let model = make_test_model();
    let config = ServerConfig {
        port: 8080,
        max_batch_size: 1,
        timeout_seconds: 30,
        enable_logging: false,
        max_concurrent_requests: 10,
    };
    let server = ModelServer::new(model, config);
    let stats = server.get_stats();
    assert_eq!(stats.total_requests, 0);
    assert_eq!(stats.successful_predictions, 0);
    assert_eq!(stats.total_errors, 0);
    assert_eq!(stats.avg_response_time_ms, 0.0);
    assert_eq!(stats.active_requests, 0);
}
#[test]
fn test_target_platform_host_detection_is_consistent() {
    let variants = [
        TargetPlatform::LinuxX64,
        TargetPlatform::LinuxArm64,
        TargetPlatform::WindowsX64,
        TargetPlatform::MacOSX64,
        TargetPlatform::MacOSArm64,
        TargetPlatform::AndroidArm64,
        TargetPlatform::AndroidX64,
        TargetPlatform::IOSArm64,
        TargetPlatform::IOSX64,
        TargetPlatform::WASM,
    ];
    let host = TargetPlatform::host();
    for variant in &variants {
        assert_eq!(variant.is_host(), host.as_ref() == Some(variant));
    }
    assert!(!TargetPlatform::WASM.is_host());
}
#[test]
fn test_binary_project_cargo_toml_has_path_dependency_and_no_cdylib() {
    let manifest_dir = "/some/workspace/scirs2-neural";
    let toml = binary_project_cargo_toml("scirs2_model_runtime", manifest_dir);
    assert!(toml.contains("name = \"scirs2_model_runtime\""));
    assert!(toml.contains(&format!("path = \"{manifest_dir}\"")));
    assert!(toml.contains("features = [\"legacy_serialization\"]"));
    assert!(toml.contains("[[bin]]"));
    assert!(!toml.contains("crate-type"));
}
#[test]
fn test_binary_project_main_rs_loads_model_and_forwards() {
    let main_rs = binary_project_main_rs();
    assert!(main_rs.contains("load_model"));
    assert!(main_rs.contains("SerializationFormat::JSON"));
    assert!(main_rs.contains(".forward("));
    assert!(main_rs.contains("fn main()"));
}
#[test]
fn test_cdylib_project_cargo_toml_has_cdylib_crate_type() {
    let manifest_dir = "/some/workspace/scirs2-neural";
    let toml = cdylib_project_cargo_toml("scirs2_model_cdylib", manifest_dir);
    assert!(toml.contains("crate-type = [\"cdylib\"]"));
    assert!(toml.contains(&format!("path = \"{manifest_dir}\"")));
    assert!(toml.contains("features = [\"legacy_serialization\"]"));
}
#[test]
fn test_cdylib_project_lib_rs_matches_c_header_abi_symbols() {
    let lib_rs = cdylib_project_lib_rs();
    for symbol in [
        "scirs2_model_load",
        "scirs2_model_predict",
        "scirs2_model_free",
        "scirs2_tensor_free",
    ] {
        assert!(
            lib_rs.contains(symbol),
            "generated cdylib source is missing expected ABI symbol `{symbol}`"
        );
    }
    assert!(lib_rs.contains("#[no_mangle]"));
    assert!(lib_rs.contains("extern \"C\" fn"));
}
#[test]
fn test_generate_c_header_declares_same_abi_symbols_as_cdylib() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let header_path = temp_dir.path().join("scirs2_model.h");
    let config = CBindingConfig {
        library_name: "scirs2_model".to_string(),
        header_guard: "SCIRS2_MODEL_H".to_string(),
        namespace: None,
        calling_convention: CallingConvention::CDecl,
        additional_headers: Vec::new(),
        type_mappings: HashMap::new(),
    };
    packager
        .generate_c_header(&header_path, &config)
        .expect("header generation should succeed");
    let header = fs::read_to_string(&header_path).expect("failed to read generated header");
    for symbol in [
        "scirs2_model_load",
        "scirs2_model_predict",
        "scirs2_model_free",
        "scirs2_tensor_free",
    ] {
        assert!(header.contains(symbol));
    }
}
#[test]
fn test_codegen_pipeline_end_to_end_std_only_program() {
    if Command::new("rustc").arg("--version").output().is_err() {
        println!("rustc not found on PATH; skipping end-to-end codegen smoke test");
        return;
    }
    let dir = unique_temp_dir("scirs2_neural_serving_rustc_smoke");
    fs::create_dir_all(&dir).expect("failed to create smoke-test temp dir");
    let src_path = dir.join("main.rs");
    fs::write(
        &src_path,
        r#"fn main() { println!("scirs2-neural-codegen-smoke-ok"); }"#,
    )
    .expect("failed to write smoke-test source");
    let exe_path = dir.join(if cfg!(windows) { "smoke.exe" } else { "smoke" });
    let compile_output = Command::new("rustc")
        .arg(&src_path)
        .arg("-o")
        .arg(&exe_path)
        .output()
        .expect("failed to invoke rustc");
    assert!(
        compile_output.status.success(),
        "rustc failed: {}",
        String::from_utf8_lossy(&compile_output.stderr)
    );
    let run_output = Command::new(&exe_path)
        .output()
        .expect("failed to run compiled smoke-test binary");
    assert_eq!(run_output.status.code(), Some(0));
    assert_eq!(
        String::from_utf8_lossy(&run_output.stdout).trim(),
        "scirs2-neural-codegen-smoke-ok"
    );
    let _ = fs::remove_dir_all(&dir);
}
#[test]
fn test_generate_runtime_binary_rejects_non_host_platform() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let out_path = temp_dir.path().join("runtime_out");
    let result = packager.generate_runtime_binary(&out_path, &TargetPlatform::WASM);
    assert!(
        result.is_err(),
        "a non-host TargetPlatform must be rejected, not silently faked"
    );
    assert!(
        !out_path.exists(),
        "no artifact should be written for a rejected non-host target"
    );
}
#[test]
fn test_generate_shared_library_rejects_non_host_platform() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let config = CBindingConfig {
        library_name: "scirs2_model".to_string(),
        header_guard: "SCIRS2_MODEL_H".to_string(),
        namespace: None,
        calling_convention: CallingConvention::CDecl,
        additional_headers: Vec::new(),
        type_mappings: HashMap::new(),
    };
    let out_path = temp_dir.path().join("libscirs2_model.out");
    let result = packager.generate_shared_library(&out_path, &config, &TargetPlatform::WASM);
    assert!(
        result.is_err(),
        "a non-host TargetPlatform must be rejected, not silently faked"
    );
    assert!(
        !out_path.exists(),
        "no artifact should be written for a rejected non-host target"
    );
}
#[test]
fn test_ensure_host_platform_accepts_actual_host() {
    if let Some(host) = TargetPlatform::host() {
        assert!(ensure_host_platform(&host).is_ok());
    }
}
#[test]
fn test_generate_wasm_module_returns_honest_error_not_fake_bytes() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let path = temp_dir.path().join("model.wasm");
    let config = WasmConfig {
        wasm_version: WasmVersion::V1_0,
        enable_simd: false,
        enable_threads: false,
        memory_config: WasmMemoryConfig {
            initial_pages: 1,
            max_pages: None,
            allow_growth: false,
        },
        imports: Vec::new(),
        exports: Vec::new(),
    };
    let result = packager.generate_wasm_module(&path, &config);
    assert!(result.is_err());
    assert!(!path.exists(), "no fake .wasm artifact should be written");
}
#[test]
fn test_generate_android_aar_returns_honest_error_not_fake_bytes() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let path = temp_dir.path().join("model.aar");
    let config = MobileConfig {
        platform: MobilePlatform::Android,
        min_os_version: "21".to_string(),
        architecture: MobileArchitecture::ARM64,
        optimization: MobileOptimization {
            enable_quantization: false,
            pruning_level: 0.0,
            memory_optimization: false,
            battery_optimization: false,
        },
        framework_config: FrameworkConfig {
            use_metal: false,
            use_nnapi: false,
            use_gpu: false,
            thread_pool_size: None,
        },
    };
    let result = packager.generate_android_aar(&path, &config);
    assert!(result.is_err());
    assert!(!path.exists(), "no fake .aar artifact should be written");
}
#[test]
fn test_generate_ios_framework_returns_honest_error_not_fake_bytes() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let path = temp_dir.path().join("SciRS2Model.framework");
    let config = MobileConfig {
        platform: MobilePlatform::IOS,
        min_os_version: "12.0".to_string(),
        architecture: MobileArchitecture::ARM64,
        optimization: MobileOptimization {
            enable_quantization: false,
            pruning_level: 0.0,
            memory_optimization: false,
            battery_optimization: false,
        },
        framework_config: FrameworkConfig {
            use_metal: false,
            use_nnapi: false,
            use_gpu: false,
            thread_pool_size: None,
        },
    };
    let result = packager.generate_ios_framework(&path, &config);
    assert!(result.is_err());
    assert!(!path.exists(), "no fake framework binary should be written");
}
#[test]
fn test_generate_python_wheel_returns_honest_error_not_fake_bytes() {
    let temp_dir = TempDir::new().expect("failed to create temp dir");
    let model = make_test_model();
    let packager = ModelPackager::new(model, temp_dir.path().to_path_buf());
    let path = temp_dir.path().join("model-1.0.0-py3-none-any.whl");
    let result = packager.generate_python_wheel(&path);
    assert!(result.is_err());
    assert!(!path.exists(), "no fake .whl artifact should be written");
}

// Note: deliberately no test here that drives `package_native`/`package_c_library`
// (or `generate_runtime_binary`/`generate_shared_library` directly on the host
// platform) end-to-end: doing so triggers a real `cargo build` of a
// scirs2-neural-*dependent* scaffolded project, which is too slow/fragile for
// the standard test suite. `test_codegen_pipeline_end_to_end_std_only_program`
// above proves the compile-and-run pipeline mechanics with a std-only program
// instead; the scirs2-neural-dependent path is exercised structurally via the
// `test_binary_project_*`/`test_cdylib_project_*` template-content assertions.