Skip to main content

qualia_core_db/wgsl_forge/
mod.rs

1//! Deterministic WGSL generation, validation, certification, and tuning.
2//!
3//! WGSL Forge treats shader semantics and hardware scheduling as separate typed
4//! inputs. Tuning is therefore allowed to change work distribution without
5//! changing the mathematical operation being certified.
6
7pub mod attention;
8pub mod audio;
9pub mod backend;
10// W10: the forge's calibration/adaptation pipeline (corpus→capture→learn→certify→package). Native-
11// only (drives the real inference stack for capture + the PPL oracle). The third produce-and-certify
12// entry point beside kernel certification and GGUF→p64 transcode.
13pub mod cache;
14#[cfg(not(target_arch = "wasm32"))]
15pub mod calibration;
16pub mod dispatch;
17pub mod emit;
18pub mod execute;
19pub mod graph_ops;
20pub mod ir;
21pub mod manifest;
22pub mod observer_kernels;
23pub mod oracle;
24pub mod physics;
25pub mod roofline;
26pub mod runtime;
27pub mod schedule;
28pub mod tune;
29pub mod validate;
30
31#[cfg(test)]
32mod etalon_iq_compat_tests;
33
34#[cfg(test)]
35/// `?::test_gpu_available`.
36pub(crate) fn test_gpu_available() -> bool {
37    use std::sync::OnceLock;
38    static AVAILABLE: OnceLock<bool> = OnceLock::new();
39    #[cfg(not(target_arch = "wasm32"))]
40    install_device_policy();
41    *AVAILABLE.get_or_init(|| execute::WgpuComputeContext::new(64 * 1024).is_ok())
42}
43
44#[cfg(all(test, feature = "cuda"))]
45/// `?::test_cuda_available`.
46pub(crate) fn test_cuda_available() -> bool {
47    use std::sync::OnceLock;
48    static AVAILABLE: OnceLock<bool> = OnceLock::new();
49    *AVAILABLE.get_or_init(|| execute::CudaComputeContext::new(64 * 1024).is_ok())
50}
51
52pub use etalon_wgsl_forge::numerical::*;
53pub use observer_kernels::*;
54pub use runtime::ForgeRuntime;
55
56#[cfg(not(target_arch = "wasm32"))]
57pub fn install_device_policy() {
58    use etalon_wgsl_forge::numerical::device_options::{self, DevicePolicy};
59    let _ = device_options::install(DevicePolicy {
60        backend: crate::gpu_context::qualia_backend_override,
61        experimental: crate::gpu_context::experimental_features_allowed,
62        shared: || {
63            crate::gpu_context::try_shared_gpu()
64                .map(|g| (g.device.clone(), g.queue.clone(), g.adapter_caps.clone()))
65        },
66    });
67}
68
69pub use dispatch::{
70    caps, coopmat_usable, fft_f32, gemm_cpu_f64, gemm_f32, gemm_f32_tc, gemm_f32_tc_coopmat,
71    gemm_f32_tc_reduced, gemm_f64, gemm_f64_df64, gemv_cpu_f64, gemv_f32, gemv_f64,
72    pairwise_sq_dist_cpu_f64, pairwise_sq_dist_f64,
73};
74
75#[cfg(all(test, not(target_arch = "wasm32")))]
76mod shared_device_tests;
77
78#[cfg(all(test, feature = "gpu-runtime"))]
79mod etalon_bridge_validation_tests;
80
81#[cfg(all(test, feature = "gpu-runtime"))]
82mod etalon_bridge_tests {
83    use super::*;
84
85    #[test]
86    fn test_target_backend_conversion() {
87        use etalon_bridge::*;
88        for target in [
89            TargetBackend::Wgsl,
90            TargetBackend::Msl,
91            TargetBackend::Hlsl,
92            TargetBackend::Ptx,
93            TargetBackend::CudaC,
94            TargetBackend::Spirv,
95        ] {
96            let etalon_target: EtalonTargetBackend = target.into();
97            let roundtrip: TargetBackend = etalon_target.into();
98            assert_eq!(target, roundtrip);
99            assert_eq!(to_etalon_target(target), etalon_target);
100            assert_eq!(from_etalon_target(etalon_target), target);
101        }
102    }
103
104    #[test]
105    fn test_builtin_kernel_conversion() {
106        use etalon_bridge::*;
107        for kernel in BuiltinKernel::ALL {
108            let etalon_kernel: EtalonBuiltinKernel = kernel.into();
109            let roundtrip: BuiltinKernel = etalon_kernel.into();
110            assert_eq!(kernel, roundtrip);
111            assert_eq!(to_etalon_builtin(kernel), etalon_kernel);
112            assert_eq!(from_etalon_builtin(etalon_kernel), kernel);
113        }
114    }
115
116    #[test]
117    fn test_schedule_conversion() {
118        use etalon_bridge::*;
119        let s = Schedule {
120            workgroup_size: 128,
121            items_per_invocation: 4,
122            vector_width: 2,
123        };
124        let etalon_s: EtalonSchedule = s.into();
125        let roundtrip: Schedule = etalon_s.into();
126        assert_eq!(s, roundtrip);
127        assert_eq!(to_etalon_schedule(s), etalon_s);
128        assert_eq!(from_etalon_schedule(etalon_s), s);
129    }
130
131    #[test]
132    fn test_generate_and_certify_affine_via_etalon() {
133        let result = generate_and_certify_via_etalon(
134            BuiltinKernel::AffineF32,
135            Schedule::default(),
136            TargetBackend::Wgsl,
137        );
138        assert!(
139            result.is_ok(),
140            "generate_and_certify_via_etalon failed: {:?}",
141            result.err()
142        );
143        let shader = result.unwrap();
144        assert!(!shader.source.is_empty());
145        assert_eq!(shader.kernel_id, "affine-f32");
146    }
147}