Skip to main content

ferrum_interfaces/vnext/execution/
weight.rs

1use std::collections::{BTreeMap, BTreeSet};
2use std::fmt;
3use std::sync::Arc;
4
5use sha2::{Digest, Sha256};
6
7use crate::vnext::{
8    CanonicalRational, QuantizationFormatId, WeightComponentPayload, WeightComponentSource,
9    WeightComponentSpec, WeightFormatId, WeightLayoutId,
10};
11
12use super::{
13    canonical_fingerprint, canonical_json, invalid_plan, is_canonical_sha256, CapabilityCatalog,
14    CapabilityId, ContractVersion, Deserialize, DeviceDescriptor, ModelFamilyId,
15    PreparedModelFamily, ResourceId, Serialize, VNextError, WeightId, WeightMaterializerId,
16    WeightSchema,
17};
18
19pub const IDENTITY_WEIGHT_MATERIALIZER_ID: &str = "weight-materializer.identity";
20const IDENTITY_MATERIALIZER_VERSION: ContractVersion = ContractVersion::new(2, 0);
21pub const MAX_WEIGHT_MATERIALIZERS: usize = 64;
22pub const NUMERIC_WEIGHT_QUALITY_ARTIFACT_SCHEMA_ID: &str =
23    "quality-approval.weight-materializer.numeric.v1";
24pub const NUMERIC_WEIGHT_QUALITY_AUTHORITY_ID: &str = "quality-approval-authority.ferrum.numeric";
25pub const MAX_APPROXIMATE_WEIGHT_QUALITY_ARTIFACT_BYTES: usize = 64 * 1024;
26pub const STATIC_WEIGHT_TRANSFORM_SCRATCH_ALIGNMENT_BYTES: u64 = 256;
27
28const NUMERIC_WEIGHT_QUALITY_AUTHORITY_VERSION: ContractVersion = ContractVersion::new(1, 0);
29const REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES: usize = 4;
30const MAX_NUMERIC_WEIGHT_QUALITY_VALUES_PER_CASE: usize = 8 * 1024;
31const MAX_NUMERIC_WEIGHT_QUALITY_VALUES: usize = 16 * 1024;
32
33#[derive(Serialize)]
34struct NumericWeightQualityAuthorityFingerprint<'a> {
35    id: &'a str,
36    version: ContractVersion,
37    artifact_schema_id: &'a str,
38    actual_encoding: &'a str,
39    reference_encoding: &'a str,
40    metric: &'a str,
41    verification_contract: &'a str,
42    artifact_max_bytes: usize,
43    required_cases: usize,
44    maximum_values_per_case: usize,
45    maximum_total_values: usize,
46}
47
48/// Canonical identity of the crate-owned numeric-artifact verification
49/// contract. It changes only when the parser, encodings, metric, or containment
50/// limits change; it is not a digest of a compiler binary or candidate SHA.
51pub fn numeric_weight_quality_authority_implementation_fingerprint() -> Result<String, VNextError> {
52    canonical_fingerprint(
53        &NumericWeightQualityAuthorityFingerprint {
54            id: NUMERIC_WEIGHT_QUALITY_AUTHORITY_ID,
55            version: NUMERIC_WEIGHT_QUALITY_AUTHORITY_VERSION,
56            artifact_schema_id: NUMERIC_WEIGHT_QUALITY_ARTIFACT_SCHEMA_ID,
57            actual_encoding: "ieee754-binary16-little-endian-bits",
58            reference_encoding: "ieee754-binary32-little-endian-bits",
59            metric: "norm(actual-reference)_2/max(norm(reference)_2,1e-6)",
60            verification_contract: "strict-canonical-json+locked-vector-payload-sha256+case-reference-sha256+raw-vector-sha256+recomputed-nonfinite-and-relative-l2+live-schema-binding",
61            artifact_max_bytes: MAX_APPROXIMATE_WEIGHT_QUALITY_ARTIFACT_BYTES,
62            required_cases: REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES,
63            maximum_values_per_case: MAX_NUMERIC_WEIGHT_QUALITY_VALUES_PER_CASE,
64            maximum_total_values: MAX_NUMERIC_WEIGHT_QUALITY_VALUES,
65        },
66        "fingerprint approximate weight numeric quality authority",
67    )
68}
69
70/// Typed compiler selection for one weight materializer.
71///
72/// Artifact bytes remain inert until the process-local registry verifies them
73/// against the selected implementation, its checked-in quality contract, and
74/// the live source and execution schemas. The bytes therefore cannot act as a
75/// global precision override.
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub struct WeightMaterializerSelection {
78    materializer_id: WeightMaterializerId,
79    numeric_quality_artifact: Option<Arc<[u8]>>,
80}
81
82impl WeightMaterializerSelection {
83    pub fn exact(materializer_id: WeightMaterializerId) -> Self {
84        Self {
85            materializer_id,
86            numeric_quality_artifact: None,
87        }
88    }
89
90    pub fn numeric_quality_artifact(
91        materializer_id: WeightMaterializerId,
92        artifact_bytes: impl Into<Vec<u8>>,
93    ) -> Result<Self, VNextError> {
94        let artifact_bytes = artifact_bytes.into();
95        decode_numeric_weight_quality_artifact(&artifact_bytes)?;
96        Ok(Self {
97            materializer_id,
98            numeric_quality_artifact: Some(Arc::from(artifact_bytes)),
99        })
100    }
101
102    pub fn materializer_id(&self) -> &WeightMaterializerId {
103        &self.materializer_id
104    }
105
106    pub fn has_numeric_quality_artifact(&self) -> bool {
107        self.numeric_quality_artifact.is_some()
108    }
109
110    fn numeric_quality_artifact_bytes(&self) -> Option<&[u8]> {
111        self.numeric_quality_artifact.as_deref()
112    }
113}
114
115/// Whether a physical weight transformation preserves source values.
116///
117/// Kernel availability is not authorization to change model precision.
118/// Approximate materializers may be registered for capability discovery, but
119/// require a separate numerical-quality approval path before plan selection.
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(rename_all = "snake_case")]
122pub enum WeightMaterializationFidelity {
123    Exact,
124    Approximate,
125}
126
127/// Stable physical ABI of cached or device-produced execution weights.
128///
129/// This identity deliberately excludes the materializer implementation
130/// fingerprint, compiler SHA, device model, and worker count. Those values are
131/// useful runtime evidence, but none of them changes the bytes accepted by an
132/// execution provider. Cache compatibility is therefore tied to this typed
133/// ABI plus the source/component identity rather than to one producer build.
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(deny_unknown_fields)]
136pub struct WeightArtifactAbi {
137    version: ContractVersion,
138    weight_format_id: WeightFormatId,
139    weight_layout_id: WeightLayoutId,
140    quantization_format_ids: BTreeSet<QuantizationFormatId>,
141}
142
143impl WeightArtifactAbi {
144    const VERSION: ContractVersion = ContractVersion::new(1, 0);
145
146    fn from_schema(schema: &WeightSchema) -> Result<Self, VNextError> {
147        let abi = Self {
148            version: Self::VERSION,
149            weight_format_id: schema.format_id.clone(),
150            weight_layout_id: schema.layout_id.clone(),
151            quantization_format_ids: schema.quantization_formats(),
152        };
153        abi.validate()?;
154        Ok(abi)
155    }
156
157    pub const fn version(&self) -> ContractVersion {
158        self.version
159    }
160
161    pub fn weight_format_id(&self) -> &WeightFormatId {
162        &self.weight_format_id
163    }
164
165    pub fn weight_layout_id(&self) -> &WeightLayoutId {
166        &self.weight_layout_id
167    }
168
169    pub fn quantization_format_ids(&self) -> &BTreeSet<QuantizationFormatId> {
170        &self.quantization_format_ids
171    }
172
173    pub fn fingerprint(&self) -> Result<String, VNextError> {
174        canonical_fingerprint(self, "fingerprint weight artifact ABI")
175    }
176
177    fn validate(&self) -> Result<(), VNextError> {
178        if self.version != Self::VERSION {
179            return Err(invalid_plan(
180                "weight artifact ABI has an unsupported contract version",
181            ));
182        }
183        Ok(())
184    }
185}
186
187/// One required cold-path device transform from source checkpoint components
188/// into final execution-weight components.
189///
190/// The first supported producer preserves official E4M3 bytes, converts the
191/// BF16 inverse-scale grid into Marlin's grouped F16 scale ABI, and repacks one
192/// logical matrix at a time. `matrices_per_output == 2` represents the routed
193/// gate/up fusion `[E, 2, N, K] -> [E, 2N, K]`; all other projections use one.
194#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
195#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
196pub enum StaticWeightTransformPlan {
197    BlockFp8ToMarlinFp8Group128 {
198        source_values_id: WeightId,
199        source_scales_id: WeightId,
200        packed_values_id: WeightId,
201        scales_id: WeightId,
202        logical_dimensions: Vec<u64>,
203        matrices_per_output: u32,
204    },
205    GptOssMxfp4ToMarlin {
206        source_blocks_id: WeightId,
207        source_scales_id: WeightId,
208        packed_values_id: WeightId,
209        scales_id: WeightId,
210        logical_dimensions: Vec<u64>,
211        execution_dimensions: Vec<u64>,
212    },
213}
214
215impl StaticWeightTransformPlan {
216    pub fn source_component_ids(&self) -> [&WeightId; 2] {
217        match self {
218            Self::BlockFp8ToMarlinFp8Group128 {
219                source_values_id,
220                source_scales_id,
221                ..
222            } => [source_values_id, source_scales_id],
223            Self::GptOssMxfp4ToMarlin {
224                source_blocks_id,
225                source_scales_id,
226                ..
227            } => [source_blocks_id, source_scales_id],
228        }
229    }
230
231    pub fn execution_component_ids(&self) -> [&WeightId; 2] {
232        match self {
233            Self::BlockFp8ToMarlinFp8Group128 {
234                packed_values_id,
235                scales_id,
236                ..
237            } => [packed_values_id, scales_id],
238            Self::GptOssMxfp4ToMarlin {
239                packed_values_id,
240                scales_id,
241                ..
242            } => [packed_values_id, scales_id],
243        }
244    }
245
246    pub fn logical_dimensions(&self) -> &[u64] {
247        match self {
248            Self::BlockFp8ToMarlinFp8Group128 {
249                logical_dimensions, ..
250            } => logical_dimensions,
251            Self::GptOssMxfp4ToMarlin {
252                logical_dimensions, ..
253            } => logical_dimensions,
254        }
255    }
256
257    /// Matrix dimensions consumed by the native execution kernel after any
258    /// explicitly typed cold-path padding. Block-FP8 transforms are exact;
259    /// GPT-OSS MXFP4 may zero-extend only K to a supported Marlin tile.
260    pub fn execution_dimensions(&self) -> &[u64] {
261        match self {
262            Self::BlockFp8ToMarlinFp8Group128 {
263                logical_dimensions, ..
264            } => logical_dimensions,
265            Self::GptOssMxfp4ToMarlin {
266                execution_dimensions,
267                ..
268            } => execution_dimensions,
269        }
270    }
271
272    pub const fn matrices_per_output(&self) -> u32 {
273        match self {
274            Self::BlockFp8ToMarlinFp8Group128 {
275                matrices_per_output,
276                ..
277            } => *matrices_per_output,
278            Self::GptOssMxfp4ToMarlin { .. } => 1,
279        }
280    }
281
282    /// Device scratch is shared across transforms and sized for the largest
283    /// single fused output matrix, never for the model or expert count.
284    pub fn scratch_bytes(&self) -> Result<u64, VNextError> {
285        match self {
286            Self::BlockFp8ToMarlinFp8Group128 {
287                logical_dimensions,
288                matrices_per_output,
289                ..
290            } => {
291                if logical_dimensions.len() < 2 {
292                    return Err(invalid_plan(
293                        "static block-FP8 transform requires at least two dimensions",
294                    ));
295                }
296                let [n, k] = logical_dimensions[logical_dimensions.len() - 2..] else {
297                    unreachable!("two-axis slice has exact length")
298                };
299                n.checked_mul(u64::from(*matrices_per_output))
300                    .and_then(|rows| rows.checked_mul(k))
301                    .ok_or_else(|| {
302                        invalid_plan("static block-FP8 transform scratch size overflows u64")
303                    })
304            }
305            Self::GptOssMxfp4ToMarlin {
306                execution_dimensions,
307                ..
308            } => {
309                let [_, n, k] = execution_dimensions.as_slice() else {
310                    return Err(invalid_plan(
311                        "static GPT-OSS MXFP4 transform requires [E,N,K] execution dimensions",
312                    ));
313                };
314                n.checked_mul(*k)
315                    .and_then(|weights| weights.checked_div(2))
316                    .ok_or_else(|| {
317                        invalid_plan("static GPT-OSS MXFP4 transform scratch size overflows u64")
318                    })
319            }
320        }
321    }
322
323    fn validate(&self) -> Result<(), VNextError> {
324        let source_ids = self.source_component_ids();
325        let execution_ids = self.execution_component_ids();
326        if source_ids[0] == source_ids[1] || execution_ids[0] == execution_ids[1] {
327            return Err(invalid_plan(
328                "static weight transform has repeated source or execution component identities",
329            ));
330        }
331        match self {
332            Self::BlockFp8ToMarlinFp8Group128 {
333                logical_dimensions,
334                matrices_per_output,
335                ..
336            } => {
337                if logical_dimensions.len() < 2 {
338                    return Err(invalid_plan(
339                        "static block-FP8 transform requires matrix dimensions",
340                    ));
341                }
342                let n = logical_dimensions[logical_dimensions.len() - 2];
343                let k = logical_dimensions[logical_dimensions.len() - 1];
344                let source_matrix_count = logical_dimensions[..logical_dimensions.len() - 2]
345                    .iter()
346                    .try_fold(1_u64, |count, extent| count.checked_mul(*extent))
347                    .ok_or_else(|| invalid_plan("static block-FP8 matrix count overflows u64"))?;
348                let fused_prefix_is_typed = *matrices_per_output == 1
349                    || (*matrices_per_output == 2
350                        && logical_dimensions.len() >= 4
351                        && logical_dimensions[logical_dimensions.len() - 3] == 2);
352                if n == 0
353                    || k == 0
354                    || !n.is_multiple_of(128)
355                    || !k.is_multiple_of(128)
356                    || !matches!(*matrices_per_output, 1 | 2)
357                    || !source_matrix_count.is_multiple_of(u64::from(*matrices_per_output))
358                    || !fused_prefix_is_typed
359                    || self.scratch_bytes()? == 0
360                {
361                    return Err(invalid_plan(
362                        "static block-FP8 group-128 transform has invalid shape, fusion, or scratch demand",
363                    ));
364                }
365            }
366            Self::GptOssMxfp4ToMarlin {
367                logical_dimensions,
368                execution_dimensions,
369                ..
370            } => {
371                let [experts, n, k] = logical_dimensions.as_slice() else {
372                    return Err(invalid_plan(
373                        "static GPT-OSS MXFP4 transform requires [E,N,K] dimensions",
374                    ));
375                };
376                let [execution_experts, execution_n, execution_k] = execution_dimensions.as_slice()
377                else {
378                    return Err(invalid_plan(
379                        "static GPT-OSS MXFP4 transform requires [E,N,K] execution dimensions",
380                    ));
381                };
382                let expected_execution_k = if n.is_multiple_of(128) || k.is_multiple_of(128) {
383                    Some(*k)
384                } else {
385                    k.checked_add(64)
386                };
387                if *experts == 0
388                    || *n == 0
389                    || *k == 0
390                    || !n.is_multiple_of(64)
391                    || !k.is_multiple_of(64)
392                    || execution_experts != experts
393                    || execution_n != n
394                    || Some(*execution_k) != expected_execution_k
395                    || !execution_k.is_multiple_of(64)
396                    || self.scratch_bytes()? == 0
397                {
398                    return Err(invalid_plan(
399                        "static GPT-OSS MXFP4 transform requires positive 64-aligned logical E/N/K and the exact safe Marlin execution K",
400                    ));
401                }
402            }
403        }
404        Ok(())
405    }
406}
407
408/// Checked-in numerical policy for an approximate materializer.
409///
410/// This is not an approval record. The public compiler remains exact-only;
411/// M3 must add a crate-owned verifier that consumes real numeric artifact
412/// bytes before an approximate materializer can be selected.
413#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
414#[serde(deny_unknown_fields)]
415pub struct ApproximateWeightQualityContract {
416    execution_contract_fingerprint: String,
417    quality_vector_digest: String,
418    required_case_count: u32,
419    relative_l2_max: CanonicalRational,
420    nan_count_max: u64,
421    inf_count_max: u64,
422}
423
424impl ApproximateWeightQualityContract {
425    pub fn new(
426        execution_contract_fingerprint: impl Into<String>,
427        quality_vector_digest: impl Into<String>,
428        required_case_count: u32,
429        relative_l2_max: CanonicalRational,
430        nan_count_max: u64,
431        inf_count_max: u64,
432    ) -> Result<Self, VNextError> {
433        let contract = Self {
434            execution_contract_fingerprint: execution_contract_fingerprint.into(),
435            quality_vector_digest: quality_vector_digest.into(),
436            required_case_count,
437            relative_l2_max,
438            nan_count_max,
439            inf_count_max,
440        };
441        contract.validate()?;
442        Ok(contract)
443    }
444
445    pub fn execution_contract_fingerprint(&self) -> &str {
446        &self.execution_contract_fingerprint
447    }
448
449    pub fn quality_vector_digest(&self) -> &str {
450        &self.quality_vector_digest
451    }
452
453    pub const fn required_case_count(&self) -> u32 {
454        self.required_case_count
455    }
456
457    pub const fn relative_l2_max(&self) -> CanonicalRational {
458        self.relative_l2_max
459    }
460
461    pub const fn nan_count_max(&self) -> u64 {
462        self.nan_count_max
463    }
464
465    pub const fn inf_count_max(&self) -> u64 {
466        self.inf_count_max
467    }
468
469    fn validate(&self) -> Result<(), VNextError> {
470        if !is_canonical_sha256(&self.execution_contract_fingerprint)
471            || !is_canonical_sha256(&self.quality_vector_digest)
472            || self.required_case_count == 0
473            || self.relative_l2_max.numerator() <= 0
474        {
475            return Err(invalid_plan(
476                "approximate weight quality contract has invalid digests, case count, or threshold",
477            ));
478        }
479        Ok(())
480    }
481}
482
483#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
484#[serde(deny_unknown_fields)]
485struct NumericWeightQualityArtifact {
486    schema_id: String,
487    authority: NumericWeightQualityArtifactAuthority,
488    checkpoint: NumericWeightQualityArtifactCheckpoint,
489    materializer: NumericWeightQualityArtifactMaterializer,
490    source: NumericWeightQualityArtifactSource,
491    execution: NumericWeightQualityArtifactExecution,
492    contract: NumericWeightQualityArtifactContract,
493    quality_vector_payload: serde_json::Value,
494    cases: Vec<NumericWeightQualityArtifactCase>,
495}
496
497#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
498#[serde(deny_unknown_fields)]
499struct NumericWeightQualityArtifactAuthority {
500    id: String,
501    version: ContractVersion,
502    implementation_fingerprint: String,
503}
504
505#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
506#[serde(deny_unknown_fields)]
507struct NumericWeightQualityArtifactCheckpoint {
508    id: String,
509    repository: String,
510    revision: String,
511}
512
513#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
514#[serde(deny_unknown_fields)]
515struct NumericWeightQualityArtifactMaterializer {
516    id: WeightMaterializerId,
517    version: ContractVersion,
518    implementation_fingerprint: String,
519    fidelity: WeightMaterializationFidelity,
520}
521
522#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
523#[serde(deny_unknown_fields)]
524struct NumericWeightQualityArtifactSource {
525    weight_format_id: WeightFormatId,
526}
527
528#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
529#[serde(deny_unknown_fields)]
530struct NumericWeightQualityArtifactExecution {
531    weight_format_id: WeightFormatId,
532    weight_layout_id: WeightLayoutId,
533    quantization_format_ids: BTreeSet<QuantizationFormatId>,
534}
535
536#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
537#[serde(deny_unknown_fields)]
538struct NumericWeightQualityArtifactContract {
539    execution_contract_fingerprint: String,
540    quality_vector_digest: String,
541}
542
543#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
544#[serde(deny_unknown_fields)]
545struct NumericWeightQualityArtifactCase {
546    case_id: String,
547    actual_f16le_sha256: String,
548    actual_f16_bits: Vec<u16>,
549    reference_f32le_sha256: String,
550    reference_f32_bits: Vec<u32>,
551    relative_l2_upper_bound: CanonicalRational,
552    nan_count: u64,
553    inf_count: u64,
554}
555
556/// Serializable receipt embedded into an approximate execution-weight plan.
557///
558/// This record is evidence, not authority: only the non-serializable
559/// [`TrustedExecutionWeightPlan`] produced by the process-local registry can
560/// carry it into an executable plan. It binds reusable numeric evidence to the
561/// exact live source and execution schema fingerprints selected for this plan.
562#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
563#[serde(deny_unknown_fields)]
564pub struct ApproximateWeightQualityApprovalRecord {
565    authority_id: String,
566    authority_version: ContractVersion,
567    authority_implementation_fingerprint: String,
568    artifact_sha256: String,
569    source_schema_fingerprint: String,
570    execution_schema_fingerprint: String,
571    execution_contract_fingerprint: String,
572    quality_vector_digest: String,
573    completed_case_count: u32,
574    relative_l2_max_observed: CanonicalRational,
575    nan_count: u64,
576    inf_count: u64,
577}
578
579impl ApproximateWeightQualityApprovalRecord {
580    pub fn authority_id(&self) -> &str {
581        &self.authority_id
582    }
583
584    pub const fn authority_version(&self) -> ContractVersion {
585        self.authority_version
586    }
587
588    pub fn authority_implementation_fingerprint(&self) -> &str {
589        &self.authority_implementation_fingerprint
590    }
591
592    pub fn artifact_sha256(&self) -> &str {
593        &self.artifact_sha256
594    }
595
596    pub fn source_schema_fingerprint(&self) -> &str {
597        &self.source_schema_fingerprint
598    }
599
600    pub fn execution_schema_fingerprint(&self) -> &str {
601        &self.execution_schema_fingerprint
602    }
603
604    pub fn execution_contract_fingerprint(&self) -> &str {
605        &self.execution_contract_fingerprint
606    }
607
608    pub fn quality_vector_digest(&self) -> &str {
609        &self.quality_vector_digest
610    }
611
612    pub const fn completed_case_count(&self) -> u32 {
613        self.completed_case_count
614    }
615
616    pub const fn relative_l2_max_observed(&self) -> CanonicalRational {
617        self.relative_l2_max_observed
618    }
619
620    pub const fn nan_count(&self) -> u64 {
621        self.nan_count
622    }
623
624    pub const fn inf_count(&self) -> u64 {
625        self.inf_count
626    }
627
628    fn validate_structure(&self) -> Result<(), VNextError> {
629        if self.authority_id != NUMERIC_WEIGHT_QUALITY_AUTHORITY_ID
630            || self.authority_version != NUMERIC_WEIGHT_QUALITY_AUTHORITY_VERSION
631            || self.authority_implementation_fingerprint
632                != numeric_weight_quality_authority_implementation_fingerprint()?
633            || !is_canonical_sha256(&self.artifact_sha256)
634            || !is_canonical_sha256(&self.source_schema_fingerprint)
635            || !is_canonical_sha256(&self.execution_schema_fingerprint)
636            || !is_canonical_sha256(&self.execution_contract_fingerprint)
637            || !is_canonical_sha256(&self.quality_vector_digest)
638            || self.completed_case_count != REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES as u32
639            || self.relative_l2_max_observed.numerator() < 0
640        {
641            return Err(invalid_plan(
642                "approximate weight quality approval record is structurally invalid",
643            ));
644        }
645        Ok(())
646    }
647
648    fn validate_against(
649        &self,
650        source_schema_fingerprint: &str,
651        execution_schema_fingerprint: &str,
652        quality_contract: &ApproximateWeightQualityContract,
653    ) -> Result<(), VNextError> {
654        self.validate_structure()?;
655        if self.source_schema_fingerprint != source_schema_fingerprint
656            || self.execution_schema_fingerprint != execution_schema_fingerprint
657            || self.execution_contract_fingerprint
658                != quality_contract.execution_contract_fingerprint()
659            || self.quality_vector_digest != quality_contract.quality_vector_digest()
660            || self.completed_case_count != quality_contract.required_case_count()
661            || !nonnegative_rational_le(
662                self.relative_l2_max_observed,
663                quality_contract.relative_l2_max(),
664            )
665            || self.nan_count > quality_contract.nan_count_max()
666            || self.inf_count > quality_contract.inf_count_max()
667        {
668            return Err(invalid_plan(
669                "approximate weight quality approval differs from the live materializer or schema contract",
670            ));
671        }
672        Ok(())
673    }
674}
675
676fn decode_numeric_weight_quality_artifact(
677    artifact_bytes: &[u8],
678) -> Result<NumericWeightQualityArtifact, VNextError> {
679    if artifact_bytes.is_empty()
680        || artifact_bytes.len() > MAX_APPROXIMATE_WEIGHT_QUALITY_ARTIFACT_BYTES
681    {
682        return Err(invalid_plan(format!(
683            "approximate weight quality artifact must contain 1..={MAX_APPROXIMATE_WEIGHT_QUALITY_ARTIFACT_BYTES} bytes"
684        )));
685    }
686    let artifact: NumericWeightQualityArtifact =
687        serde_json::from_slice(artifact_bytes).map_err(|error| {
688            invalid_plan(format!(
689                "approximate weight quality artifact is not strict schema-valid JSON: {error}"
690            ))
691        })?;
692    let canonical_bytes = serde_json::to_value(&artifact)
693        .map(canonical_json)
694        .and_then(|value| serde_json::to_vec(&value))
695        .map_err(|error| VNextError::Serialization {
696            context: "canonicalize approximate weight quality artifact",
697            message: error.to_string(),
698        })?;
699    if canonical_bytes != artifact_bytes {
700        return Err(invalid_plan(
701            "approximate weight quality artifact is not canonical compact JSON",
702        ));
703    }
704    if artifact.schema_id != NUMERIC_WEIGHT_QUALITY_ARTIFACT_SCHEMA_ID
705        || artifact.authority.id != NUMERIC_WEIGHT_QUALITY_AUTHORITY_ID
706        || artifact.authority.version != NUMERIC_WEIGHT_QUALITY_AUTHORITY_VERSION
707        || artifact.authority.implementation_fingerprint
708            != numeric_weight_quality_authority_implementation_fingerprint()?
709    {
710        return Err(invalid_plan(
711            "approximate weight quality artifact names a different verification authority",
712        ));
713    }
714    if !portable_artifact_text(&artifact.checkpoint.id)
715        || !portable_artifact_text(&artifact.checkpoint.repository)
716        || !canonical_revision(&artifact.checkpoint.revision)
717    {
718        return Err(invalid_plan(
719            "approximate weight quality artifact checkpoint identity is invalid",
720        ));
721    }
722    if artifact.materializer.version.major == 0
723        || !is_canonical_sha256(&artifact.materializer.implementation_fingerprint)
724        || artifact.materializer.fidelity != WeightMaterializationFidelity::Approximate
725        || !is_canonical_sha256(&artifact.contract.execution_contract_fingerprint)
726        || !is_canonical_sha256(&artifact.contract.quality_vector_digest)
727        || artifact.cases.len() != REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES
728    {
729        return Err(invalid_plan(
730            "approximate weight quality artifact has invalid materializer, contract, or case structure",
731        ));
732    }
733    let quality_vector_bytes = serde_json::to_vec(&canonical_json(
734        artifact.quality_vector_payload.clone(),
735    ))
736    .map_err(|error| VNextError::Serialization {
737        context: "canonicalize approximate weight quality vector payload",
738        message: error.to_string(),
739    })?;
740    if format!("{:x}", Sha256::digest(&quality_vector_bytes))
741        != artifact.contract.quality_vector_digest
742    {
743        return Err(invalid_plan(
744            "approximate weight quality artifact does not contain the locked quality vector payload",
745        ));
746    }
747    let vector_references =
748        quality_vector_references(&artifact.quality_vector_payload, &artifact.checkpoint)?;
749    let mut case_ids = BTreeSet::new();
750    let mut total_values = 0_usize;
751    for case in &artifact.cases {
752        let value_count = case.actual_f16_bits.len();
753        if !portable_artifact_text(&case.case_id)
754            || !case_ids.insert(case.case_id.clone())
755            || value_count == 0
756            || value_count != case.reference_f32_bits.len()
757            || value_count > MAX_NUMERIC_WEIGHT_QUALITY_VALUES_PER_CASE
758            || !is_canonical_sha256(&case.actual_f16le_sha256)
759            || !is_canonical_sha256(&case.reference_f32le_sha256)
760            || case.relative_l2_upper_bound.numerator() < 0
761        {
762            return Err(invalid_plan(
763                "approximate weight quality artifact has an invalid case identity, vector, digest, or metric structure",
764            ));
765        }
766        if vector_references.get(&case.case_id) != Some(&case.reference_f32le_sha256) {
767            return Err(invalid_plan(format!(
768                "approximate weight quality artifact case `{}` reference differs from the locked quality vector",
769                case.case_id
770            )));
771        }
772        total_values = total_values.checked_add(value_count).ok_or_else(|| {
773            invalid_plan("approximate weight quality artifact value count overflows usize")
774        })?;
775        if total_values > MAX_NUMERIC_WEIGHT_QUALITY_VALUES {
776            return Err(invalid_plan(format!(
777                "approximate weight quality artifact exceeds {MAX_NUMERIC_WEIGHT_QUALITY_VALUES} total values"
778            )));
779        }
780    }
781    if case_ids != vector_references.keys().cloned().collect() {
782        return Err(invalid_plan(
783            "approximate weight quality artifact cases differ from the locked quality vector",
784        ));
785    }
786    Ok(artifact)
787}
788
789fn verify_numeric_weight_quality_artifact(
790    artifact_bytes: &[u8],
791    descriptor: &WeightMaterializerDescriptor,
792    family: &PreparedModelFamily,
793    execution_schema: &WeightSchema,
794) -> Result<ApproximateWeightQualityApprovalRecord, VNextError> {
795    let artifact = decode_numeric_weight_quality_artifact(artifact_bytes)?;
796    let quality_contract = descriptor.approximate_quality_contract().ok_or_else(|| {
797        invalid_plan(format!(
798            "approximate weight materializer `{}` has no numerical quality contract",
799            descriptor.id()
800        ))
801    })?;
802    if artifact.materializer.id != *descriptor.id()
803        || artifact.materializer.version != descriptor.version()
804        || artifact.materializer.implementation_fingerprint
805            != descriptor.implementation_fingerprint()
806        || artifact.materializer.fidelity != descriptor.fidelity()
807        || descriptor.fidelity() != WeightMaterializationFidelity::Approximate
808    {
809        return Err(invalid_plan(
810            "approximate weight quality artifact differs from the selected materializer",
811        ));
812    }
813    if artifact.source.weight_format_id != family.weight_schema().format_id
814        || artifact.execution.weight_format_id != execution_schema.format_id
815        || artifact.execution.weight_layout_id != execution_schema.layout_id
816        || artifact.execution.quantization_format_ids != execution_schema.quantization_formats()
817    {
818        return Err(invalid_plan(
819            "approximate weight quality artifact differs from the live source or execution format contract",
820        ));
821    }
822    if artifact.contract.execution_contract_fingerprint
823        != quality_contract.execution_contract_fingerprint()
824        || artifact.contract.quality_vector_digest != quality_contract.quality_vector_digest()
825        || quality_contract.required_case_count() as usize != REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES
826        || artifact.cases.len() != REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES
827    {
828        return Err(invalid_plan(
829            "approximate weight quality artifact differs from the checked-in quality contract",
830        ));
831    }
832    let quality_vector_bytes = serde_json::to_vec(&canonical_json(
833        artifact.quality_vector_payload.clone(),
834    ))
835    .map_err(|error| VNextError::Serialization {
836        context: "canonicalize approximate weight quality vector payload",
837        message: error.to_string(),
838    })?;
839    if format!("{:x}", Sha256::digest(&quality_vector_bytes))
840        != quality_contract.quality_vector_digest()
841    {
842        return Err(invalid_plan(
843            "approximate weight quality artifact does not contain the locked quality vector payload",
844        ));
845    }
846    let vector_references =
847        quality_vector_references(&artifact.quality_vector_payload, &artifact.checkpoint)?;
848
849    let mut case_ids = BTreeSet::new();
850    let mut total_values = 0_usize;
851    let mut total_nan_count = 0_u64;
852    let mut total_inf_count = 0_u64;
853    let mut maximum_upper_bound = CanonicalRational::new(0, 1)?;
854    for case in &artifact.cases {
855        let value_count = case.actual_f16_bits.len();
856        if !portable_artifact_text(&case.case_id)
857            || !case_ids.insert(case.case_id.clone())
858            || value_count == 0
859            || value_count != case.reference_f32_bits.len()
860            || value_count > MAX_NUMERIC_WEIGHT_QUALITY_VALUES_PER_CASE
861        {
862            return Err(invalid_plan(
863                "approximate weight quality artifact has an invalid case identity or vector size",
864            ));
865        }
866        if vector_references.get(&case.case_id) != Some(&case.reference_f32le_sha256) {
867            return Err(invalid_plan(format!(
868                "approximate weight quality artifact case `{}` reference differs from the locked quality vector",
869                case.case_id
870            )));
871        }
872        total_values = total_values.checked_add(value_count).ok_or_else(|| {
873            invalid_plan("approximate weight quality artifact value count overflows usize")
874        })?;
875        if total_values > MAX_NUMERIC_WEIGHT_QUALITY_VALUES {
876            return Err(invalid_plan(format!(
877                "approximate weight quality artifact exceeds {MAX_NUMERIC_WEIGHT_QUALITY_VALUES} total values"
878            )));
879        }
880        if digest_little_endian_u16(&case.actual_f16_bits) != case.actual_f16le_sha256
881            || digest_little_endian_u32(&case.reference_f32_bits) != case.reference_f32le_sha256
882        {
883            return Err(invalid_plan(format!(
884                "approximate weight quality artifact case `{}` raw-vector digest differs",
885                case.case_id
886            )));
887        }
888
889        let (relative_l2, nan_count, inf_count) = recompute_relative_l2(case)?;
890        if nan_count != case.nan_count || inf_count != case.inf_count {
891            return Err(invalid_plan(format!(
892                "approximate weight quality artifact case `{}` reports incorrect NaN or Inf counts",
893                case.case_id
894            )));
895        }
896        total_nan_count = total_nan_count.checked_add(nan_count).ok_or_else(|| {
897            invalid_plan("approximate weight quality artifact NaN count overflows u64")
898        })?;
899        total_inf_count = total_inf_count.checked_add(inf_count).ok_or_else(|| {
900            invalid_plan("approximate weight quality artifact Inf count overflows u64")
901        })?;
902        if case.relative_l2_upper_bound.numerator() < 0
903            || relative_l2 > rational_as_f64(case.relative_l2_upper_bound)
904            || !nonnegative_rational_le(
905                case.relative_l2_upper_bound,
906                quality_contract.relative_l2_max(),
907            )
908        {
909            return Err(invalid_plan(format!(
910                "approximate weight quality artifact case `{}` exceeds or understates its relative-L2 contract",
911                case.case_id
912            )));
913        }
914        if nonnegative_rational_le(maximum_upper_bound, case.relative_l2_upper_bound) {
915            maximum_upper_bound = case.relative_l2_upper_bound;
916        }
917    }
918    if case_ids != vector_references.keys().cloned().collect() {
919        return Err(invalid_plan(
920            "approximate weight quality artifact cases differ from the locked quality vector",
921        ));
922    }
923    if total_nan_count > quality_contract.nan_count_max()
924        || total_inf_count > quality_contract.inf_count_max()
925    {
926        return Err(invalid_plan(
927            "approximate weight quality artifact exceeds its non-finite output contract",
928        ));
929    }
930
931    let record = ApproximateWeightQualityApprovalRecord {
932        authority_id: NUMERIC_WEIGHT_QUALITY_AUTHORITY_ID.to_owned(),
933        authority_version: NUMERIC_WEIGHT_QUALITY_AUTHORITY_VERSION,
934        authority_implementation_fingerprint:
935            numeric_weight_quality_authority_implementation_fingerprint()?,
936        artifact_sha256: format!("{:x}", Sha256::digest(artifact_bytes)),
937        source_schema_fingerprint: family.weight_schema().fingerprint()?,
938        execution_schema_fingerprint: execution_schema.fingerprint()?,
939        execution_contract_fingerprint: quality_contract
940            .execution_contract_fingerprint()
941            .to_owned(),
942        quality_vector_digest: quality_contract.quality_vector_digest().to_owned(),
943        completed_case_count: u32::try_from(artifact.cases.len()).map_err(|_| {
944            invalid_plan("approximate weight quality artifact case count exceeds u32")
945        })?,
946        relative_l2_max_observed: maximum_upper_bound,
947        nan_count: total_nan_count,
948        inf_count: total_inf_count,
949    };
950    record.validate_against(
951        &family.weight_schema().fingerprint()?,
952        &execution_schema.fingerprint()?,
953        quality_contract,
954    )?;
955    Ok(record)
956}
957
958fn quality_vector_references(
959    payload: &serde_json::Value,
960    checkpoint: &NumericWeightQualityArtifactCheckpoint,
961) -> Result<BTreeMap<String, String>, VNextError> {
962    const ROOT_KEYS: [&str; 10] = [
963        "activation_batches",
964        "activation_contract",
965        "cases",
966        "checkpoint",
967        "fixture_id",
968        "generator",
969        "reference_contract",
970        "schema_version",
971        "source_contract",
972        "weight_shapes",
973    ];
974    let root = payload.as_object().ok_or_else(|| {
975        invalid_plan("approximate weight quality vector payload must be an object")
976    })?;
977    if root.keys().map(String::as_str).collect::<BTreeSet<_>>() != ROOT_KEYS.into_iter().collect() {
978        return Err(invalid_plan(
979            "approximate weight quality vector payload has unexpected root fields",
980        ));
981    }
982    let payload_checkpoint = root
983        .get("checkpoint")
984        .and_then(serde_json::Value::as_object)
985        .ok_or_else(|| {
986            invalid_plan("approximate weight quality vector checkpoint must be an object")
987        })?;
988    if payload_checkpoint
989        .get("id")
990        .and_then(serde_json::Value::as_str)
991        != Some(checkpoint.id.as_str())
992        || payload_checkpoint
993            .get("repository")
994            .and_then(serde_json::Value::as_str)
995            != Some(checkpoint.repository.as_str())
996        || payload_checkpoint
997            .get("revision")
998            .and_then(serde_json::Value::as_str)
999            != Some(checkpoint.revision.as_str())
1000    {
1001        return Err(invalid_plan(
1002            "approximate weight quality artifact checkpoint differs from its locked vector",
1003        ));
1004    }
1005    let cases = root
1006        .get("cases")
1007        .and_then(serde_json::Value::as_array)
1008        .ok_or_else(|| invalid_plan("approximate weight quality vector cases must be an array"))?;
1009    if cases.len() != REQUIRED_NUMERIC_WEIGHT_QUALITY_CASES {
1010        return Err(invalid_plan(
1011            "approximate weight quality vector must contain exactly four cases",
1012        ));
1013    }
1014    let mut references = BTreeMap::new();
1015    for case in cases {
1016        let case = case.as_object().ok_or_else(|| {
1017            invalid_plan("approximate weight quality vector case must be an object")
1018        })?;
1019        let case_id = case
1020            .get("case_id")
1021            .and_then(serde_json::Value::as_str)
1022            .filter(|value| portable_artifact_text(value))
1023            .ok_or_else(|| {
1024                invalid_plan("approximate weight quality vector case identity is invalid")
1025            })?;
1026        let reference = case
1027            .get("reference_f32le_sha256")
1028            .and_then(serde_json::Value::as_str)
1029            .filter(|value| is_canonical_sha256(value))
1030            .ok_or_else(|| {
1031                invalid_plan("approximate weight quality vector reference digest is invalid")
1032            })?;
1033        if references
1034            .insert(case_id.to_owned(), reference.to_owned())
1035            .is_some()
1036        {
1037            return Err(invalid_plan(
1038                "approximate weight quality vector has duplicate case identities",
1039            ));
1040        }
1041    }
1042    Ok(references)
1043}
1044
1045fn portable_artifact_text(value: &str) -> bool {
1046    !value.is_empty()
1047        && value.len() <= 160
1048        && value.bytes().all(|byte| {
1049            byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-' | b':' | b'/')
1050        })
1051}
1052
1053fn canonical_revision(value: &str) -> bool {
1054    matches!(value.len(), 40 | 64)
1055        && value
1056            .bytes()
1057            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
1058}
1059
1060fn digest_little_endian_u16(values: &[u16]) -> String {
1061    let mut digest = Sha256::new();
1062    for value in values {
1063        digest.update(value.to_le_bytes());
1064    }
1065    format!("{:x}", digest.finalize())
1066}
1067
1068fn digest_little_endian_u32(values: &[u32]) -> String {
1069    let mut digest = Sha256::new();
1070    for value in values {
1071        digest.update(value.to_le_bytes());
1072    }
1073    format!("{:x}", digest.finalize())
1074}
1075
1076fn recompute_relative_l2(
1077    case: &NumericWeightQualityArtifactCase,
1078) -> Result<(f64, u64, u64), VNextError> {
1079    let mut error_squared = 0_f64;
1080    let mut reference_squared = 0_f64;
1081    let mut nan_count = 0_u64;
1082    let mut inf_count = 0_u64;
1083    for (&actual_bits, &reference_bits) in case.actual_f16_bits.iter().zip(&case.reference_f32_bits)
1084    {
1085        let actual = binary16_as_f32(actual_bits);
1086        if actual.is_nan() {
1087            nan_count += 1;
1088            continue;
1089        }
1090        if actual.is_infinite() {
1091            inf_count += 1;
1092            continue;
1093        }
1094        let reference = f32::from_bits(reference_bits);
1095        if !reference.is_finite() {
1096            return Err(invalid_plan(format!(
1097                "approximate weight quality artifact case `{}` has a non-finite reference",
1098                case.case_id
1099            )));
1100        }
1101        let error = f64::from(actual - reference);
1102        error_squared += error * error;
1103        let reference = f64::from(reference);
1104        reference_squared += reference * reference;
1105    }
1106    if nan_count != 0 || inf_count != 0 {
1107        return Ok((f64::INFINITY, nan_count, inf_count));
1108    }
1109    let relative_l2 = error_squared.sqrt() / reference_squared.sqrt().max(1.0e-6);
1110    if !relative_l2.is_finite() {
1111        return Err(invalid_plan(format!(
1112            "approximate weight quality artifact case `{}` produced a non-finite relative L2",
1113            case.case_id
1114        )));
1115    }
1116    Ok((relative_l2, nan_count, inf_count))
1117}
1118
1119fn binary16_as_f32(bits: u16) -> f32 {
1120    let sign = if bits & 0x8000 == 0 {
1121        1.0_f32
1122    } else {
1123        -1.0_f32
1124    };
1125    let exponent = u32::from((bits >> 10) & 0x1f);
1126    let fraction = u32::from(bits & 0x03ff);
1127    match (exponent, fraction) {
1128        (0, 0) => sign * 0.0,
1129        (0, fraction) => sign * fraction as f32 * 2_f32.powi(-24),
1130        (0x1f, 0) => sign * f32::INFINITY,
1131        (0x1f, _) => f32::NAN,
1132        (exponent, fraction) => {
1133            sign * (1.0 + fraction as f32 / 1024.0) * 2_f32.powi(exponent as i32 - 15)
1134        }
1135    }
1136}
1137
1138fn rational_as_f64(value: CanonicalRational) -> f64 {
1139    value.numerator() as f64 / value.denominator() as f64
1140}
1141
1142fn nonnegative_rational_le(left: CanonicalRational, right: CanonicalRational) -> bool {
1143    left.numerator() >= 0
1144        && right.numerator() >= 0
1145        && i128::from(left.numerator()) * i128::from(right.denominator())
1146            <= i128::from(right.numerator()) * i128::from(left.denominator())
1147}
1148
1149#[derive(Serialize)]
1150struct IdentityMaterializerFingerprint<'a> {
1151    id: &'a str,
1152    version: ContractVersion,
1153    contract: &'a str,
1154}
1155
1156fn identity_materializer_fingerprint() -> Result<String, VNextError> {
1157    canonical_fingerprint(
1158        &IdentityMaterializerFingerprint {
1159            id: IDENTITY_WEIGHT_MATERIALIZER_ID,
1160            version: IDENTITY_MATERIALIZER_VERSION,
1161            contract: "execution-weight-plan.identity.v2",
1162        },
1163        "fingerprint identity weight materializer",
1164    )
1165}
1166
1167/// Serializable capability identity for one trusted weight transformation.
1168///
1169/// The descriptor advertises availability, but cannot construct an execution
1170/// schema. Only the process-local [`WeightMaterializerRegistry`] retains the
1171/// implementation object that may produce a trusted plan.
1172#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1173#[serde(deny_unknown_fields)]
1174pub struct WeightMaterializerDescriptor {
1175    id: WeightMaterializerId,
1176    version: ContractVersion,
1177    implementation_fingerprint: String,
1178    fidelity: WeightMaterializationFidelity,
1179    required_capabilities: BTreeSet<CapabilityId>,
1180    #[serde(default, skip_serializing_if = "Option::is_none")]
1181    approximate_quality_contract: Option<ApproximateWeightQualityContract>,
1182}
1183
1184impl WeightMaterializerDescriptor {
1185    pub fn new(
1186        id: WeightMaterializerId,
1187        version: ContractVersion,
1188        implementation_fingerprint: impl Into<String>,
1189        fidelity: WeightMaterializationFidelity,
1190        required_capabilities: BTreeSet<CapabilityId>,
1191    ) -> Result<Self, VNextError> {
1192        let descriptor = Self {
1193            id,
1194            version,
1195            implementation_fingerprint: implementation_fingerprint.into(),
1196            fidelity,
1197            required_capabilities,
1198            approximate_quality_contract: None,
1199        };
1200        descriptor.validate_structure()?;
1201        Ok(descriptor)
1202    }
1203
1204    pub(crate) fn identity() -> Result<Self, VNextError> {
1205        Self::new(
1206            WeightMaterializerId::new(IDENTITY_WEIGHT_MATERIALIZER_ID)?,
1207            IDENTITY_MATERIALIZER_VERSION,
1208            identity_materializer_fingerprint()?,
1209            WeightMaterializationFidelity::Exact,
1210            BTreeSet::new(),
1211        )
1212    }
1213
1214    pub fn id(&self) -> &WeightMaterializerId {
1215        &self.id
1216    }
1217
1218    pub const fn version(&self) -> ContractVersion {
1219        self.version
1220    }
1221
1222    pub fn implementation_fingerprint(&self) -> &str {
1223        &self.implementation_fingerprint
1224    }
1225
1226    pub const fn fidelity(&self) -> WeightMaterializationFidelity {
1227        self.fidelity
1228    }
1229
1230    pub fn required_capabilities(&self) -> &BTreeSet<CapabilityId> {
1231        &self.required_capabilities
1232    }
1233
1234    pub fn with_approximate_quality_contract(
1235        mut self,
1236        contract: ApproximateWeightQualityContract,
1237    ) -> Result<Self, VNextError> {
1238        if self.fidelity != WeightMaterializationFidelity::Approximate {
1239            return Err(invalid_plan(format!(
1240                "exact weight materializer `{}` cannot carry an approximate quality contract",
1241                self.id
1242            )));
1243        }
1244        contract.validate()?;
1245        self.approximate_quality_contract = Some(contract);
1246        Ok(self)
1247    }
1248
1249    pub fn approximate_quality_contract(&self) -> Option<&ApproximateWeightQualityContract> {
1250        self.approximate_quality_contract.as_ref()
1251    }
1252
1253    pub fn fingerprint(&self) -> Result<String, VNextError> {
1254        canonical_fingerprint(self, "fingerprint weight materializer descriptor")
1255    }
1256
1257    pub(crate) fn validate_for_device(&self, device: &DeviceDescriptor) -> Result<(), VNextError> {
1258        self.validate_structure()?;
1259        if !self.required_capabilities.is_subset(&device.capabilities) {
1260            return Err(invalid_plan(format!(
1261                "weight materializer `{}` requires capabilities absent from device `{}`",
1262                self.id, device.id
1263            )));
1264        }
1265        Ok(())
1266    }
1267
1268    fn validate_structure(&self) -> Result<(), VNextError> {
1269        if self.version.major == 0 || !is_canonical_sha256(&self.implementation_fingerprint) {
1270            return Err(invalid_plan(format!(
1271                "weight materializer descriptor `{}` has invalid version or implementation identity",
1272                self.id
1273            )));
1274        }
1275        if let Some(contract) = &self.approximate_quality_contract {
1276            contract.validate()?;
1277            if self.fidelity != WeightMaterializationFidelity::Approximate {
1278                return Err(invalid_plan(format!(
1279                    "exact weight materializer `{}` cannot carry an approximate quality contract",
1280                    self.id
1281                )));
1282            }
1283        }
1284        Ok(())
1285    }
1286}
1287
1288/// Trusted authority for one physical weight transformation.
1289///
1290/// Implementations are invoked once while compiling an immutable plan. They
1291/// must derive the complete execution schema from the prepared family and
1292/// device descriptor without allocating device memory or reading request
1293/// state. During static initialization the same retained implementation turns
1294/// source components into the exact execution components recorded in that
1295/// plan; no provider or weight source may bypass this authority.
1296pub trait WeightMaterializer: Send + Sync {
1297    fn descriptor(&self) -> &WeightMaterializerDescriptor;
1298
1299    fn execution_schema(
1300        &self,
1301        family: &PreparedModelFamily,
1302        device: &DeviceDescriptor,
1303    ) -> Result<WeightSchema, VNextError>;
1304
1305    /// Ordered source-component identities for every execution component.
1306    ///
1307    /// The default only permits a same-id mapping. Materializers that create
1308    /// derived component identities or combine multiple source components must
1309    /// declare that provenance explicitly.
1310    fn component_sources(
1311        &self,
1312        family: &PreparedModelFamily,
1313        execution_schema: &WeightSchema,
1314    ) -> Result<BTreeMap<WeightId, Vec<WeightId>>, VNextError> {
1315        identity_component_sources(family, execution_schema)
1316    }
1317
1318    /// Required device-side transforms for final execution artifacts.
1319    ///
1320    /// An empty set means every execution component is produced as a host
1321    /// payload through [`Self::materialize_components`]. A non-empty set is a
1322    /// fail-closed contract: static initialization must use a runtime that
1323    /// implements the exact transform and must never call the host
1324    /// materializer for those outputs.
1325    fn static_weight_transforms(
1326        &self,
1327        _family: &PreparedModelFamily,
1328        _execution_schema: &WeightSchema,
1329    ) -> Result<Vec<StaticWeightTransformPlan>, VNextError> {
1330        Ok(Vec::new())
1331    }
1332
1333    /// Materialize one execution component on the cold initialization path.
1334    ///
1335    /// `source_components` is resolved from the immutable plan's ordered source
1336    /// map. Implementations may borrow source bytes for identity layouts or
1337    /// return owned bytes for repacked/quantized layouts.
1338    fn materialize_component<'source>(
1339        &self,
1340        source: &'source dyn WeightComponentSource,
1341        source_components: &[&WeightComponentSpec],
1342        execution_component: &WeightComponentSpec,
1343    ) -> Result<WeightComponentPayload<'source>, VNextError>;
1344
1345    /// Materialize execution components that share one exact ordered source set.
1346    ///
1347    /// The default preserves existing one-component implementations. Expensive
1348    /// transforms may override this method to decode, quantize, or repack their
1349    /// common source once and return one payload per requested component in the
1350    /// same order.
1351    fn materialize_components<'source>(
1352        &self,
1353        source: &'source dyn WeightComponentSource,
1354        source_components: &[&WeightComponentSpec],
1355        execution_components: &[&WeightComponentSpec],
1356    ) -> Result<Vec<WeightComponentPayload<'source>>, VNextError> {
1357        execution_components
1358            .iter()
1359            .map(|component| self.materialize_component(source, source_components, component))
1360            .collect()
1361    }
1362}
1363
1364struct IdentityWeightMaterializer {
1365    descriptor: WeightMaterializerDescriptor,
1366}
1367
1368impl IdentityWeightMaterializer {
1369    fn new() -> Result<Self, VNextError> {
1370        Ok(Self {
1371            descriptor: WeightMaterializerDescriptor::identity()?,
1372        })
1373    }
1374}
1375
1376impl WeightMaterializer for IdentityWeightMaterializer {
1377    fn descriptor(&self) -> &WeightMaterializerDescriptor {
1378        &self.descriptor
1379    }
1380
1381    fn execution_schema(
1382        &self,
1383        family: &PreparedModelFamily,
1384        _device: &DeviceDescriptor,
1385    ) -> Result<WeightSchema, VNextError> {
1386        Ok(family.weight_schema().clone())
1387    }
1388
1389    fn materialize_component<'source>(
1390        &self,
1391        source: &'source dyn WeightComponentSource,
1392        source_components: &[&WeightComponentSpec],
1393        execution_component: &WeightComponentSpec,
1394    ) -> Result<WeightComponentPayload<'source>, VNextError> {
1395        let [source_component] = source_components else {
1396            return Err(invalid_plan(
1397                "identity weight materializer requires exactly one source component",
1398            ));
1399        };
1400        if *source_component != execution_component {
1401            return Err(invalid_plan(format!(
1402                "identity weight materializer cannot transform component `{}`",
1403                execution_component.id
1404            )));
1405        }
1406        source.component(source_component)
1407    }
1408}
1409
1410fn identity_component_sources(
1411    family: &PreparedModelFamily,
1412    execution_schema: &WeightSchema,
1413) -> Result<BTreeMap<WeightId, Vec<WeightId>>, VNextError> {
1414    let source_ids = family
1415        .weight_schema()
1416        .components
1417        .iter()
1418        .map(|component| component.id.clone())
1419        .collect::<BTreeSet<_>>();
1420    execution_schema
1421        .components
1422        .iter()
1423        .map(|component| {
1424            if !source_ids.contains(&component.id) {
1425                return Err(invalid_plan(format!(
1426                    "weight materializer must declare sources for derived component `{}`",
1427                    component.id
1428                )));
1429            }
1430            Ok((component.id.clone(), vec![component.id.clone()]))
1431        })
1432        .collect()
1433}
1434
1435/// Process-local registry retaining the exact implementations authorized to
1436/// transform checkpoint schemas. It is deliberately neither serializable nor
1437/// reconstructible from a capability catalog.
1438pub struct WeightMaterializerRegistry {
1439    materializers: BTreeMap<WeightMaterializerId, Arc<dyn WeightMaterializer>>,
1440}
1441
1442impl WeightMaterializerRegistry {
1443    pub fn new(materializers: Vec<Box<dyn WeightMaterializer>>) -> Result<Self, VNextError> {
1444        if materializers.len() >= MAX_WEIGHT_MATERIALIZERS {
1445            return Err(invalid_plan(format!(
1446                "weight materializer registry exceeds {} non-identity entries",
1447                MAX_WEIGHT_MATERIALIZERS - 1
1448            )));
1449        }
1450        let identity: Arc<dyn WeightMaterializer> = Arc::new(IdentityWeightMaterializer::new()?);
1451        let mut entries = BTreeMap::from([(identity.descriptor().id().clone(), identity)]);
1452        for materializer in materializers {
1453            materializer.descriptor().validate_structure()?;
1454            let id = materializer.descriptor().id().clone();
1455            if entries
1456                .insert(id.clone(), Arc::from(materializer))
1457                .is_some()
1458            {
1459                return Err(invalid_plan(format!(
1460                    "duplicate weight materializer `{id}`"
1461                )));
1462            }
1463        }
1464        Ok(Self {
1465            materializers: entries,
1466        })
1467    }
1468
1469    pub fn identity_only() -> Result<Self, VNextError> {
1470        Self::new(Vec::new())
1471    }
1472
1473    /// Extends a capability catalog with descriptors from these exact
1474    /// process-local implementation objects.
1475    pub fn augment_catalog(
1476        &self,
1477        catalog: CapabilityCatalog,
1478    ) -> Result<CapabilityCatalog, VNextError> {
1479        catalog.with_weight_materializer_descriptors(self.descriptors())
1480    }
1481
1482    /// Invokes the selected trusted implementation and returns a witness that
1483    /// cannot be forged by deserializing an [`ExecutionWeightPlan`].
1484    /// Selects a materializer that is value-preserving by contract.
1485    ///
1486    /// Approximate transforms require a future typed quality approval carrying
1487    /// checked-in numerical-tolerance evidence. Keeping that authority out of
1488    /// this entrypoint prevents a build feature or backend capability from
1489    /// silently changing checkpoint precision.
1490    pub fn select_exact(
1491        &self,
1492        family: &PreparedModelFamily,
1493        catalog: &CapabilityCatalog,
1494        materializer_id: &WeightMaterializerId,
1495    ) -> Result<TrustedExecutionWeightPlan, VNextError> {
1496        let materializer = self.registered_materializer(catalog, materializer_id)?;
1497        let descriptor = materializer.descriptor();
1498        if descriptor.fidelity() != WeightMaterializationFidelity::Exact {
1499            return Err(VNextError::WeightMaterializerQualityApprovalRequired {
1500                materializer_id: materializer_id.to_string(),
1501            });
1502        }
1503        descriptor.validate_for_device(catalog.device())?;
1504        let mut schema = materializer.execution_schema(family, catalog.device())?;
1505        schema.normalize();
1506        let component_sources = materializer.component_sources(family, &schema)?;
1507        let static_weight_transforms = materializer.static_weight_transforms(family, &schema)?;
1508        let plan = ExecutionWeightPlan::from_materializer(
1509            family,
1510            descriptor,
1511            schema,
1512            component_sources,
1513            static_weight_transforms,
1514        )?;
1515        Ok(TrustedExecutionWeightPlan {
1516            plan,
1517            descriptor: descriptor.clone(),
1518            materializer: Arc::clone(materializer),
1519        })
1520    }
1521
1522    /// Selects either an exact materializer or an approximate materializer
1523    /// carrying strict, crate-verified numeric artifact bytes.
1524    pub fn select(
1525        &self,
1526        family: &PreparedModelFamily,
1527        catalog: &CapabilityCatalog,
1528        selection: &WeightMaterializerSelection,
1529    ) -> Result<TrustedExecutionWeightPlan, VNextError> {
1530        let Some(artifact_bytes) = selection.numeric_quality_artifact_bytes() else {
1531            return self.select_exact(family, catalog, selection.materializer_id());
1532        };
1533        self.select_with_numeric_quality_artifact(
1534            family,
1535            catalog,
1536            selection.materializer_id(),
1537            artifact_bytes,
1538        )
1539    }
1540
1541    pub fn select_with_numeric_quality_artifact(
1542        &self,
1543        family: &PreparedModelFamily,
1544        catalog: &CapabilityCatalog,
1545        materializer_id: &WeightMaterializerId,
1546        artifact_bytes: &[u8],
1547    ) -> Result<TrustedExecutionWeightPlan, VNextError> {
1548        let materializer = self.registered_materializer(catalog, materializer_id)?;
1549        let descriptor = materializer.descriptor();
1550        if descriptor.fidelity() != WeightMaterializationFidelity::Approximate {
1551            return Err(invalid_plan(format!(
1552                "exact weight materializer `{materializer_id}` cannot consume an approximate quality artifact"
1553            )));
1554        }
1555        descriptor.validate_for_device(catalog.device())?;
1556        let mut schema = materializer.execution_schema(family, catalog.device())?;
1557        schema.normalize();
1558        let approval =
1559            verify_numeric_weight_quality_artifact(artifact_bytes, descriptor, family, &schema)?;
1560        let component_sources = materializer.component_sources(family, &schema)?;
1561        let static_weight_transforms = materializer.static_weight_transforms(family, &schema)?;
1562        let plan = ExecutionWeightPlan::from_materializer_with_approval(
1563            family,
1564            descriptor,
1565            schema,
1566            component_sources,
1567            static_weight_transforms,
1568            Some(approval),
1569        )?;
1570        Ok(TrustedExecutionWeightPlan {
1571            plan,
1572            descriptor: descriptor.clone(),
1573            materializer: Arc::clone(materializer),
1574        })
1575    }
1576
1577    fn registered_materializer<'registry>(
1578        &'registry self,
1579        catalog: &CapabilityCatalog,
1580        materializer_id: &WeightMaterializerId,
1581    ) -> Result<&'registry Arc<dyn WeightMaterializer>, VNextError> {
1582        let materializer = self.materializers.get(materializer_id).ok_or_else(|| {
1583            invalid_plan(format!(
1584                "weight materializer `{materializer_id}` is not registered"
1585            ))
1586        })?;
1587        if materializer.descriptor() != catalog.weight_materializer(materializer_id)? {
1588            return Err(invalid_plan(format!(
1589                "weight materializer `{materializer_id}` differs from its capability catalog descriptor"
1590            )));
1591        }
1592        Ok(materializer)
1593    }
1594
1595    pub fn descriptors(&self) -> BTreeMap<WeightMaterializerId, WeightMaterializerDescriptor> {
1596        self.materializers
1597            .iter()
1598            .map(|(id, materializer)| (id.clone(), materializer.descriptor().clone()))
1599            .collect()
1600    }
1601}
1602
1603/// Non-serializable proof that a process-local registry implementation
1604/// produced and validated this physical execution schema.
1605#[derive(Clone)]
1606pub struct TrustedExecutionWeightPlan {
1607    plan: ExecutionWeightPlan,
1608    descriptor: WeightMaterializerDescriptor,
1609    materializer: Arc<dyn WeightMaterializer>,
1610}
1611
1612impl fmt::Debug for TrustedExecutionWeightPlan {
1613    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1614        formatter
1615            .debug_struct("TrustedExecutionWeightPlan")
1616            .field("plan", &self.plan)
1617            .field("descriptor", &self.descriptor)
1618            .finish_non_exhaustive()
1619    }
1620}
1621
1622impl PartialEq for TrustedExecutionWeightPlan {
1623    fn eq(&self, other: &Self) -> bool {
1624        self.plan == other.plan && self.descriptor == other.descriptor
1625    }
1626}
1627
1628impl Eq for TrustedExecutionWeightPlan {}
1629
1630impl TrustedExecutionWeightPlan {
1631    pub(crate) fn identity(family: &PreparedModelFamily) -> Result<Self, VNextError> {
1632        let materializer: Arc<dyn WeightMaterializer> =
1633            Arc::new(IdentityWeightMaterializer::new()?);
1634        let descriptor = materializer.descriptor().clone();
1635        let schema = family.weight_schema().clone();
1636        let component_sources = materializer.component_sources(family, &schema)?;
1637        Ok(Self {
1638            plan: ExecutionWeightPlan::from_materializer(
1639                family,
1640                &descriptor,
1641                schema,
1642                component_sources,
1643                Vec::new(),
1644            )?,
1645            descriptor,
1646            materializer,
1647        })
1648    }
1649
1650    pub fn plan(&self) -> &ExecutionWeightPlan {
1651        &self.plan
1652    }
1653
1654    pub(crate) fn validate_against_catalog(
1655        &self,
1656        family: &PreparedModelFamily,
1657        catalog: &CapabilityCatalog,
1658    ) -> Result<(), VNextError> {
1659        self.validate_runtime_authority()?;
1660        let catalog_descriptor = catalog.weight_materializer(self.descriptor.id())?;
1661        if &self.descriptor != catalog_descriptor {
1662            return Err(invalid_plan(format!(
1663                "weight materializer `{}` differs from its capability catalog descriptor",
1664                self.descriptor.id()
1665            )));
1666        }
1667        self.plan
1668            .validate_against_materializer(family, &self.descriptor)?;
1669        let mut expected_transforms = self
1670            .materializer
1671            .static_weight_transforms(family, self.plan.schema())?;
1672        expected_transforms.sort();
1673        if expected_transforms != self.plan.static_weight_transforms {
1674            return Err(invalid_plan(format!(
1675                "weight materializer `{}` static transform authority differs from the trusted plan",
1676                self.descriptor.id()
1677            )));
1678        }
1679        Ok(())
1680    }
1681
1682    pub(crate) fn materialize_components<'source>(
1683        &self,
1684        family: &PreparedModelFamily,
1685        source: &'source dyn WeightComponentSource,
1686        execution_components: &[&WeightComponentSpec],
1687    ) -> Result<Vec<WeightComponentPayload<'source>>, VNextError> {
1688        self.validate_runtime_authority()?;
1689        let Some(first_execution_component) = execution_components.first() else {
1690            return Err(invalid_plan(
1691                "weight materializer received an empty execution component group",
1692            ));
1693        };
1694        let mut planned_components = Vec::with_capacity(execution_components.len());
1695        for execution_component in execution_components {
1696            let planned_component_index = self
1697                .plan
1698                .schema
1699                .components
1700                .binary_search_by(|component| component.id.cmp(&execution_component.id))
1701                .map_err(|_| {
1702                    invalid_plan(format!(
1703                        "execution component `{}` is absent from the trusted weight plan",
1704                        execution_component.id
1705                    ))
1706                })?;
1707            let planned_component = &self.plan.schema.components[planned_component_index];
1708            if planned_component != *execution_component {
1709                return Err(invalid_plan(format!(
1710                    "execution component `{}` differs from the trusted weight plan",
1711                    execution_component.id
1712                )));
1713            }
1714            planned_components.push(planned_component);
1715        }
1716        let source_ids = self
1717            .plan
1718            .component_sources
1719            .get(&first_execution_component.id)
1720            .ok_or_else(|| {
1721                invalid_plan(format!(
1722                    "execution component `{}` has no source mapping",
1723                    first_execution_component.id
1724                ))
1725            })?;
1726        if execution_components
1727            .iter()
1728            .skip(1)
1729            .any(|component| self.plan.component_sources.get(&component.id) != Some(source_ids))
1730        {
1731            return Err(invalid_plan(
1732                "grouped execution components do not share one ordered source mapping",
1733            ));
1734        }
1735        let source_components = source_ids
1736            .iter()
1737            .map(|source_id| {
1738                let source_component_index = family
1739                    .weight_schema()
1740                    .components
1741                    .binary_search_by(|component| component.id.cmp(source_id))
1742                    .map_err(|_| {
1743                        invalid_plan(format!(
1744                            "execution component `{}` references unknown source component `{source_id}`",
1745                            first_execution_component.id
1746                        ))
1747                    })?;
1748                Ok(&family.weight_schema().components[source_component_index])
1749            })
1750            .collect::<Result<Vec<_>, _>>()?;
1751        let payloads = self.materializer.materialize_components(
1752            source,
1753            &source_components,
1754            &planned_components,
1755        )?;
1756        if payloads.len() != planned_components.len() {
1757            return Err(invalid_plan(format!(
1758                "weight materializer `{}` returned {} payloads for {} execution components",
1759                self.descriptor.id,
1760                payloads.len(),
1761                planned_components.len()
1762            )));
1763        }
1764        for (payload, execution_component) in payloads.iter().zip(&planned_components) {
1765            if payload.component_id() != &execution_component.id
1766                || payload.external_names() != execution_component.external_names.as_slice()
1767                || payload.dimensions() != execution_component.dimensions.as_slice()
1768                || payload.element_type() != execution_component.physical_element_type()
1769                || u64::try_from(payload.bytes().len()).ok()
1770                    != Some(execution_component.physical_bytes()?)
1771            {
1772                return Err(invalid_plan(format!(
1773                    "weight materializer `{}` returned invalid or reordered payload for execution component `{}`",
1774                    self.descriptor.id, execution_component.id
1775                )));
1776            }
1777        }
1778        Ok(payloads)
1779    }
1780
1781    pub(crate) fn static_weight_transform_for_components(
1782        &self,
1783        execution_components: &[&WeightComponentSpec],
1784    ) -> Result<Option<&StaticWeightTransformPlan>, VNextError> {
1785        self.validate_runtime_authority()?;
1786        if execution_components.is_empty() {
1787            return Err(invalid_plan(
1788                "static weight transform lookup received an empty component group",
1789            ));
1790        }
1791        let requested = execution_components
1792            .iter()
1793            .map(|component| component.id.clone())
1794            .collect::<BTreeSet<_>>();
1795        let matching = self
1796            .plan
1797            .static_weight_transforms
1798            .iter()
1799            .filter(|transform| {
1800                transform
1801                    .execution_component_ids()
1802                    .into_iter()
1803                    .any(|component_id| requested.contains(component_id))
1804            })
1805            .collect::<Vec<_>>();
1806        if matching.is_empty() {
1807            return Ok(None);
1808        }
1809        let [transform] = matching.as_slice() else {
1810            return Err(invalid_plan(
1811                "execution component group spans multiple static weight transforms",
1812            ));
1813        };
1814        let expected = transform
1815            .execution_component_ids()
1816            .into_iter()
1817            .cloned()
1818            .collect::<BTreeSet<_>>();
1819        if requested != expected {
1820            return Err(invalid_plan(
1821                "execution component group contains a partial or mixed static weight transform",
1822            ));
1823        }
1824        Ok(Some(transform))
1825    }
1826
1827    fn validate_runtime_authority(&self) -> Result<(), VNextError> {
1828        if self.materializer.descriptor() != &self.descriptor {
1829            return Err(invalid_plan(format!(
1830                "weight materializer `{}` runtime authority differs from its trusted descriptor",
1831                self.descriptor.id
1832            )));
1833        }
1834        Ok(())
1835    }
1836}
1837
1838/// Trusted physical weight contract selected for one immutable execution plan.
1839///
1840/// The prepared family remains the source/checkpoint contract. This plan owns
1841/// the physical schema consumed by providers, static memory planning, and
1842/// initialization. Keeping those contracts separate allows a backend to
1843/// select a prepared layout without changing model semantics or hiding an
1844/// allocation behind a provider.
1845#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1846#[serde(deny_unknown_fields)]
1847pub struct ExecutionWeightPlan {
1848    source_schema_fingerprint: String,
1849    materializer_id: WeightMaterializerId,
1850    materializer_version: ContractVersion,
1851    materializer_implementation_fingerprint: String,
1852    #[serde(default, skip_serializing_if = "Option::is_none")]
1853    approximate_quality_approval: Option<ApproximateWeightQualityApprovalRecord>,
1854    component_sources: BTreeMap<WeightId, Vec<WeightId>>,
1855    #[serde(default, skip_serializing_if = "Vec::is_empty")]
1856    static_weight_transforms: Vec<StaticWeightTransformPlan>,
1857    schema: WeightSchema,
1858}
1859
1860impl ExecutionWeightPlan {
1861    pub fn identity(family: &PreparedModelFamily) -> Result<Self, VNextError> {
1862        let descriptor = WeightMaterializerDescriptor::identity()?;
1863        let schema = family.weight_schema().clone();
1864        let component_sources = identity_component_sources(family, &schema)?;
1865        Self::from_materializer(family, &descriptor, schema, component_sources, Vec::new())
1866    }
1867
1868    fn from_materializer(
1869        family: &PreparedModelFamily,
1870        descriptor: &WeightMaterializerDescriptor,
1871        schema: WeightSchema,
1872        component_sources: BTreeMap<WeightId, Vec<WeightId>>,
1873        static_weight_transforms: Vec<StaticWeightTransformPlan>,
1874    ) -> Result<Self, VNextError> {
1875        Self::from_materializer_with_approval(
1876            family,
1877            descriptor,
1878            schema,
1879            component_sources,
1880            static_weight_transforms,
1881            None,
1882        )
1883    }
1884
1885    fn from_materializer_with_approval(
1886        family: &PreparedModelFamily,
1887        descriptor: &WeightMaterializerDescriptor,
1888        schema: WeightSchema,
1889        component_sources: BTreeMap<WeightId, Vec<WeightId>>,
1890        mut static_weight_transforms: Vec<StaticWeightTransformPlan>,
1891        approximate_quality_approval: Option<ApproximateWeightQualityApprovalRecord>,
1892    ) -> Result<Self, VNextError> {
1893        static_weight_transforms.sort();
1894        let plan = Self {
1895            source_schema_fingerprint: family.weight_schema().fingerprint()?,
1896            materializer_id: descriptor.id().clone(),
1897            materializer_version: descriptor.version(),
1898            materializer_implementation_fingerprint: descriptor
1899                .implementation_fingerprint()
1900                .to_owned(),
1901            approximate_quality_approval,
1902            component_sources,
1903            static_weight_transforms,
1904            schema,
1905        };
1906        plan.validate_against_materializer(family, descriptor)?;
1907        Ok(plan)
1908    }
1909
1910    pub fn source_schema_fingerprint(&self) -> &str {
1911        &self.source_schema_fingerprint
1912    }
1913
1914    pub fn materializer_id(&self) -> &WeightMaterializerId {
1915        &self.materializer_id
1916    }
1917
1918    pub const fn materializer_version(&self) -> ContractVersion {
1919        self.materializer_version
1920    }
1921
1922    pub fn materializer_implementation_fingerprint(&self) -> &str {
1923        &self.materializer_implementation_fingerprint
1924    }
1925
1926    pub fn artifact_abi(&self) -> Result<WeightArtifactAbi, VNextError> {
1927        WeightArtifactAbi::from_schema(&self.schema)
1928    }
1929
1930    pub fn approximate_quality_approval(&self) -> Option<&ApproximateWeightQualityApprovalRecord> {
1931        self.approximate_quality_approval.as_ref()
1932    }
1933
1934    pub fn schema(&self) -> &WeightSchema {
1935        &self.schema
1936    }
1937
1938    pub fn component_sources(&self) -> &BTreeMap<WeightId, Vec<WeightId>> {
1939        &self.component_sources
1940    }
1941
1942    pub fn static_weight_transforms(&self) -> &[StaticWeightTransformPlan] {
1943        &self.static_weight_transforms
1944    }
1945
1946    pub fn maximum_static_weight_transform_scratch_bytes(&self) -> Result<u64, VNextError> {
1947        self.static_weight_transforms
1948            .iter()
1949            .map(StaticWeightTransformPlan::scratch_bytes)
1950            .try_fold(0_u64, |maximum, bytes| {
1951                bytes.map(|bytes| maximum.max(bytes))
1952            })
1953    }
1954
1955    pub fn static_weight_transform_scratch_resource_id(
1956        &self,
1957    ) -> Result<Option<ResourceId>, VNextError> {
1958        if self.static_weight_transforms.is_empty() {
1959            return Ok(None);
1960        }
1961        let artifact_abi = self.artifact_abi()?;
1962        let digest = canonical_fingerprint(
1963            &(&artifact_abi, &self.static_weight_transforms),
1964            "fingerprint static weight transform scratch identity",
1965        )?;
1966        ResourceId::new(format!(
1967            "resource/static-weight-transform-scratch/sha256/{digest}"
1968        ))
1969        .map(Some)
1970    }
1971
1972    pub fn fingerprint(&self) -> Result<String, VNextError> {
1973        canonical_fingerprint(self, "fingerprint execution weight plan")
1974    }
1975
1976    pub(super) fn validate_structure(&self, family_id: &ModelFamilyId) -> Result<(), VNextError> {
1977        if !is_canonical_sha256(&self.source_schema_fingerprint)
1978            || !is_canonical_sha256(&self.materializer_implementation_fingerprint)
1979            || self.materializer_version.major == 0
1980        {
1981            return Err(VNextError::InvalidExecutionPlan {
1982                reason: "execution weight plan provenance is invalid".to_owned(),
1983            });
1984        }
1985        if let Some(approval) = &self.approximate_quality_approval {
1986            approval.validate_structure()?;
1987        }
1988        self.schema.validate(family_id)?;
1989        self.artifact_abi()?.validate()?;
1990        let execution_component_ids = self
1991            .schema
1992            .components
1993            .iter()
1994            .map(|component| component.id.clone())
1995            .collect::<BTreeSet<_>>();
1996        let mapped_component_ids = self
1997            .component_sources
1998            .keys()
1999            .cloned()
2000            .collect::<BTreeSet<_>>();
2001        if execution_component_ids != mapped_component_ids
2002            || self.component_sources.values().any(|source_ids| {
2003                source_ids.is_empty()
2004                    || source_ids.iter().collect::<BTreeSet<_>>().len() != source_ids.len()
2005            })
2006        {
2007            return Err(invalid_plan(
2008                "execution weight component source map is incomplete or contains duplicate sources",
2009            ));
2010        }
2011        if self
2012            .static_weight_transforms
2013            .windows(2)
2014            .any(|pair| pair[0] >= pair[1])
2015        {
2016            return Err(invalid_plan(
2017                "static weight transforms are duplicate or non-canonical",
2018            ));
2019        }
2020        let mut transformed_components = BTreeSet::new();
2021        for transform in &self.static_weight_transforms {
2022            transform.validate()?;
2023            let source_ids = transform
2024                .source_component_ids()
2025                .into_iter()
2026                .cloned()
2027                .collect::<Vec<_>>();
2028            for execution_id in transform.execution_component_ids() {
2029                if !execution_component_ids.contains(execution_id)
2030                    || self.component_sources.get(execution_id) != Some(&source_ids)
2031                    || !transformed_components.insert(execution_id.clone())
2032                {
2033                    return Err(invalid_plan(
2034                        "static weight transform outputs differ from the execution schema source map",
2035                    ));
2036                }
2037            }
2038        }
2039        Ok(())
2040    }
2041
2042    pub(crate) fn validate_against_family(
2043        &self,
2044        family: &PreparedModelFamily,
2045    ) -> Result<(), VNextError> {
2046        self.validate_structure(family.family_id())?;
2047        if self.source_schema_fingerprint != family.weight_schema().fingerprint()? {
2048            return Err(invalid_plan(
2049                "execution weight plan source schema differs from its prepared family",
2050            ));
2051        }
2052        let source_components = family
2053            .weight_schema()
2054            .components
2055            .iter()
2056            .map(|component| (&component.id, component))
2057            .collect::<BTreeMap<_, _>>();
2058        let mut referenced_source_components = BTreeSet::new();
2059        for (execution_component_id, source_ids) in &self.component_sources {
2060            for source_id in source_ids {
2061                if !source_components.contains_key(source_id) {
2062                    return Err(invalid_plan(format!(
2063                        "execution component `{execution_component_id}` references unknown source component `{source_id}`"
2064                    )));
2065                }
2066                referenced_source_components.insert(source_id.clone());
2067            }
2068        }
2069        if let Some(component) = source_components.values().find(|component| {
2070            component.required && !referenced_source_components.contains(&component.id)
2071        }) {
2072            return Err(invalid_plan(format!(
2073                "required source component `{}` is not represented in the execution weight plan",
2074                component.id
2075            )));
2076        }
2077        let source_tensors = family
2078            .weight_schema()
2079            .tensors
2080            .iter()
2081            .map(|tensor| (&tensor.id, tensor))
2082            .collect::<BTreeMap<_, _>>();
2083        let execution_tensors = self
2084            .schema
2085            .tensors
2086            .iter()
2087            .map(|tensor| (&tensor.id, tensor))
2088            .collect::<BTreeMap<_, _>>();
2089        if source_tensors.len() != execution_tensors.len()
2090            || source_tensors.iter().any(|(id, source)| {
2091                execution_tensors.get(id).is_none_or(|execution| {
2092                    source.dimensions != execution.dimensions
2093                        || source.logical_element_type != execution.logical_element_type
2094                        || source.required != execution.required
2095                })
2096            })
2097        {
2098            return Err(invalid_plan(
2099                "execution weight schema changes the prepared family's logical tensor contract",
2100            ));
2101        }
2102        Ok(())
2103    }
2104
2105    fn validate_against_materializer(
2106        &self,
2107        family: &PreparedModelFamily,
2108        descriptor: &WeightMaterializerDescriptor,
2109    ) -> Result<(), VNextError> {
2110        self.validate_against_family(family)?;
2111        if &self.materializer_id != descriptor.id()
2112            || self.materializer_version != descriptor.version()
2113            || self.materializer_implementation_fingerprint
2114                != descriptor.implementation_fingerprint()
2115        {
2116            return Err(invalid_plan(
2117                "execution weight plan differs from its trusted materializer descriptor",
2118            ));
2119        }
2120        match (
2121            descriptor.fidelity(),
2122            descriptor.approximate_quality_contract(),
2123            &self.approximate_quality_approval,
2124        ) {
2125            (WeightMaterializationFidelity::Exact, None, None) => {}
2126            (
2127                WeightMaterializationFidelity::Approximate,
2128                Some(quality_contract),
2129                Some(approval),
2130            ) => approval.validate_against(
2131                &self.source_schema_fingerprint,
2132                &self.schema.fingerprint()?,
2133                quality_contract,
2134            )?,
2135            _ => {
2136                return Err(invalid_plan(
2137                    "execution weight plan fidelity differs from its numerical quality approval",
2138                ));
2139            }
2140        }
2141        Ok(())
2142    }
2143}