Skip to main content

tenferro_ad/
context.rs

1//! Explicit ownership for automatic-differentiation rule sets.
2
3use std::sync::Arc;
4
5use tenferro_runtime::program::FrozenProgram;
6use tenferro_runtime::{CacheStats, Result, TracedTensor};
7
8// SemanticCompatDispatcher removed in Unification 7.
9// Extension AD is handled exclusively by SemanticExtensionRuleSet.
10use crate::semantic_extension::{SemanticExtensionRegistryError, SemanticExtensionRuleSet};
11use crate::semantic_transform::{
12    semantic_jvp, semantic_vjp, SemanticAdProgram, SemanticAdTransformError,
13};
14use crate::transform_cache::{
15    AdTransformCache, AdTransformCacheLimits, SemanticAdTransformCacheKey,
16};
17
18/// Stats for caches owned by an [`AdContext`].
19///
20/// `retained_bytes` fields are logical payload estimates, not process RSS.
21///
22/// # Examples
23///
24/// ```rust
25/// use tenferro_ad::AdContext;
26///
27/// let ad = AdContext::builder().build().unwrap();
28/// assert_eq!(ad.cache_stats().unwrap().ad_transforms.entries, 0);
29/// ```
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
31pub struct AdContextCacheStats {
32    /// AD transform graph memoization cache.
33    pub ad_transforms: CacheStats,
34}
35
36/// Explicit automatic-differentiation context.
37///
38/// `AdContext` owns the extension AD rules used by traced AD transforms.
39/// It also owns the AD transform cache shared by context-driven traced AD and
40/// eager runtimes created from this context.
41///
42/// # Examples
43///
44/// ```rust
45/// use tenferro_ad::AdContext;
46///
47/// let ad = AdContext::builder().build().unwrap();
48/// assert!(ad
49///     .semantic_extension_rules()
50///     .lookup_linearize("example.missing.v1")
51///     .is_none());
52/// ```
53#[derive(Clone, Debug)]
54pub struct AdContext {
55    semantic_extension_rules: SemanticExtensionRuleSet,
56    ad_transform_cache: Arc<AdTransformCache>,
57}
58
59impl AdContext {
60    /// Start building an explicit AD context.
61    ///
62    /// # Examples
63    ///
64    /// ```rust
65    /// use tenferro_ad::AdContext;
66    ///
67    /// let _builder = AdContext::builder();
68    /// ```
69    pub fn builder() -> AdContextBuilder {
70        AdContextBuilder::default()
71    }
72
73    pub(crate) fn with_rules_and_transform_cache(
74        semantic_extension_rules: SemanticExtensionRuleSet,
75        ad_transform_cache: Arc<AdTransformCache>,
76    ) -> Self {
77        Self {
78            semantic_extension_rules,
79            ad_transform_cache,
80        }
81    }
82
83    /// Return semantic-program extension AD rules owned by this context.
84    ///
85    /// # Examples
86    ///
87    /// ```rust
88    /// use tenferro_ad::AdContext;
89    ///
90    /// let ad = AdContext::builder().build().unwrap();
91    /// assert!(ad
92    ///     .semantic_extension_rules()
93    ///     .lookup_linearize("example.missing.v1")
94    ///     .is_none());
95    /// ```
96    pub fn semantic_extension_rules(&self) -> &SemanticExtensionRuleSet {
97        &self.semantic_extension_rules
98    }
99
100    /// Transform a frozen semantic program into its forward-mode derivative.
101    ///
102    /// `active_inputs` follows source-program input order. Active tangent
103    /// seeds are appended after all primal inputs.
104    ///
105    /// # Errors
106    ///
107    /// Returns [`SemanticAdTransformError::ActivityArity`] when
108    /// `active_inputs` has the wrong length,
109    /// [`SemanticAdTransformError::Extension`] when an extension rule rejects
110    /// the transform, or the corresponding `Query`, `Build`, `Finish`, or
111    /// `Cache` variant when program import, construction, finalization, or
112    /// cache access fails.
113    pub fn jvp_program(
114        &self,
115        input: &FrozenProgram,
116        active_inputs: &[bool],
117    ) -> std::result::Result<SemanticAdProgram, SemanticAdTransformError> {
118        let key = SemanticAdTransformCacheKey::jvp(input, active_inputs);
119        if let Some(cached) = self
120            .ad_transform_cache
121            .get_semantic(&key, input)
122            .map_err(SemanticAdTransformError::Cache)?
123        {
124            return cached
125                .as_ref()
126                .with_input_prefix_bindings_from(input)
127                .map_err(SemanticAdTransformError::from);
128        }
129        let transformed = semantic_jvp(input, active_inputs, &self.semantic_extension_rules)?;
130        self.ad_transform_cache
131            .put_semantic(key, input, Arc::new(transformed.clone()))
132            .map_err(SemanticAdTransformError::Cache)?;
133        Ok(transformed)
134    }
135
136    /// Transform a frozen semantic program into its reverse-mode derivative.
137    ///
138    /// `active_inputs` selects requested primal-input cotangents and
139    /// `active_outputs` selects primal outputs that receive appended seeds.
140    ///
141    /// # Errors
142    ///
143    /// Returns [`SemanticAdTransformError::ActivityArity`] when either activity
144    /// mask has the wrong length,
145    /// [`SemanticAdTransformError::Extension`] when an extension rule rejects
146    /// the transform, or the corresponding `Query`, `Build`, `Finish`, or
147    /// `Cache` variant when program import, construction, finalization, or
148    /// cache access fails.
149    pub fn vjp_program(
150        &self,
151        input: &FrozenProgram,
152        active_inputs: &[bool],
153        active_outputs: &[bool],
154    ) -> std::result::Result<SemanticAdProgram, SemanticAdTransformError> {
155        let key = SemanticAdTransformCacheKey::vjp(input, active_inputs, active_outputs);
156        if let Some(cached) = self
157            .ad_transform_cache
158            .get_semantic(&key, input)
159            .map_err(SemanticAdTransformError::Cache)?
160        {
161            return cached
162                .as_ref()
163                .with_input_prefix_bindings_from(input)
164                .map_err(SemanticAdTransformError::from);
165        }
166        let transformed = semantic_vjp(
167            input,
168            active_inputs,
169            active_outputs,
170            &self.semantic_extension_rules,
171        )?;
172        self.ad_transform_cache
173            .put_semantic(key, input, Arc::new(transformed.clone()))
174            .map_err(SemanticAdTransformError::Cache)?;
175        Ok(transformed)
176    }
177
178    pub(crate) fn ad_transform_cache(&self) -> Arc<AdTransformCache> {
179        Arc::clone(&self.ad_transform_cache)
180    }
181
182    /// Return AD transform cache retention limits.
183    ///
184    /// # Examples
185    ///
186    /// ```rust
187    /// use tenferro_ad::AdContext;
188    ///
189    /// let ad = AdContext::builder().build().unwrap();
190    /// assert!(ad.ad_transform_cache_limits().unwrap().max_entries().get() > 0);
191    /// ```
192    ///
193    /// # Errors
194    ///
195    /// Returns [`tenferro_runtime::Error::RuntimeState`] if the cache lock is
196    /// poisoned or its state cannot be inspected.
197    pub fn ad_transform_cache_limits(&self) -> Result<AdTransformCacheLimits> {
198        self.ad_transform_cache.limits()
199    }
200
201    /// Replace AD transform cache retention limits.
202    ///
203    /// # Examples
204    ///
205    /// ```rust
206    /// use std::num::NonZeroUsize;
207    /// use tenferro_ad::{AdContext, AdTransformCacheLimits};
208    ///
209    /// let ad = AdContext::builder().build().unwrap();
210    /// let limits = AdTransformCacheLimits::new(NonZeroUsize::new(1).unwrap());
211    /// ad.set_ad_transform_cache_limits(limits).unwrap();
212    /// assert_eq!(ad.ad_transform_cache_limits().unwrap(), limits);
213    /// ```
214    ///
215    /// # Errors
216    ///
217    /// Returns [`tenferro_runtime::Error::RuntimeState`] if the cache lock is
218    /// poisoned while updating the limits.
219    pub fn set_ad_transform_cache_limits(&self, limits: AdTransformCacheLimits) -> Result<()> {
220        self.ad_transform_cache.set_limits(limits)
221    }
222
223    /// Clear AD transform cache entries owned by this context.
224    ///
225    /// # Examples
226    ///
227    /// ```rust
228    /// use tenferro_ad::AdContext;
229    ///
230    /// let ad = AdContext::builder().build().unwrap();
231    /// ad.clear_ad_transform_caches().unwrap();
232    /// assert_eq!(ad.ad_transform_cache_stats().unwrap().entries, 0);
233    /// ```
234    ///
235    /// # Errors
236    ///
237    /// Returns [`tenferro_runtime::Error::RuntimeState`] if the cache lock is
238    /// poisoned while clearing entries.
239    pub fn clear_ad_transform_caches(&self) -> Result<()> {
240        self.ad_transform_cache.clear()
241    }
242
243    /// Return AD transform cache-entry and retained-byte stats.
244    ///
245    /// # Examples
246    ///
247    /// ```rust
248    /// use tenferro_ad::AdContext;
249    ///
250    /// let ad = AdContext::builder().build().unwrap();
251    /// assert_eq!(ad.ad_transform_cache_stats().unwrap().entries, 0);
252    /// ```
253    ///
254    /// # Errors
255    ///
256    /// Returns [`tenferro_runtime::Error::RuntimeState`] if the cache lock is
257    /// poisoned while collecting statistics.
258    pub fn ad_transform_cache_stats(&self) -> Result<CacheStats> {
259        self.ad_transform_cache.stats()
260    }
261
262    /// Clear every cache owned by this AD context.
263    ///
264    /// # Examples
265    ///
266    /// ```rust
267    /// use tenferro_ad::AdContext;
268    ///
269    /// let ad = AdContext::builder().build().unwrap();
270    /// ad.clear_caches().unwrap();
271    /// assert_eq!(ad.cache_stats().unwrap().ad_transforms.entries, 0);
272    /// ```
273    ///
274    /// # Errors
275    ///
276    /// Returns [`tenferro_runtime::Error::RuntimeState`] if either owned cache
277    /// cannot be locked because its state is poisoned.
278    pub fn clear_caches(&self) -> Result<()> {
279        self.clear_ad_transform_caches()
280    }
281
282    /// Return aggregate cache-entry and retained-byte stats for this AD context.
283    ///
284    /// # Examples
285    ///
286    /// ```rust
287    /// use tenferro_ad::AdContext;
288    ///
289    /// let ad = AdContext::builder().build().unwrap();
290    /// assert_eq!(ad.cache_stats().unwrap().ad_transforms.retained_bytes, 0);
291    /// ```
292    ///
293    /// # Errors
294    ///
295    /// Returns [`tenferro_runtime::Error::RuntimeState`] if an owned cache lock
296    /// is poisoned while collecting statistics.
297    pub fn cache_stats(&self) -> Result<AdContextCacheStats> {
298        Ok(AdContextCacheStats {
299            ad_transforms: self.ad_transform_cache_stats()?,
300        })
301    }
302
303    /// Gradient of a scalar traced output with respect to a traced input.
304    ///
305    /// For complex scalar outputs, tenferro returns the Hermitian-adjoint
306    /// cotangent. To compare seed-`1` scalar gradients with JAX's public
307    /// `grad` values, use the complex conjugate of this result. See
308    /// <https://tensor4all.org/tenferro-rs/guides/complex-ad.html>.
309    ///
310    /// # Examples
311    ///
312    /// ```rust
313    /// use tenferro_ad::AdContext;
314    /// use tenferro_runtime::TracedTensor;
315    ///
316    /// let ad = AdContext::builder().build().unwrap();
317    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
318    /// let loss = (&x * &x).unwrap();
319    /// let grad = ad.grad(&loss, &x).unwrap();
320    /// assert_eq!(grad.rank, 0);
321    /// ```
322    ///
323    /// # Errors
324    ///
325    /// Returns [`tenferro_runtime::Error::NonScalarGrad`] when `output` is not
326    /// scalar, [`tenferro_runtime::Error::UnsupportedAdRule`] when a graph op
327    /// lacks a registered rule, or a typed [`tenferro_runtime::Error::Validation`]
328    /// / backend error when graph metadata or execution is invalid.
329    pub fn grad(&self, output: &TracedTensor, wrt: &TracedTensor) -> Result<TracedTensor> {
330        crate::traced::grad_with_rules_and_cache(
331            output,
332            wrt,
333            &self.semantic_extension_rules,
334            Some(self.ad_transform_cache.as_ref()),
335        )
336    }
337
338    /// Gradient that returns `None` when `wrt` is inactive.
339    ///
340    /// # Examples
341    ///
342    /// ```rust
343    /// use tenferro_ad::AdContext;
344    /// use tenferro_runtime::TracedTensor;
345    ///
346    /// let ad = AdContext::builder().build().unwrap();
347    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
348    /// let loss = (&x * &x).unwrap();
349    /// assert!(ad.grad_optional(&loss, &x).unwrap().is_some());
350    /// ```
351    ///
352    /// # Errors
353    ///
354    /// Returns [`tenferro_runtime::Error::NonScalarGrad`] for a non-scalar
355    /// output, [`tenferro_runtime::Error::UnsupportedAdRule`] for an
356    /// unregistered AD rule, or a typed [`tenferro_runtime::Error::Validation`]
357    /// / backend error from graph construction and
358    /// execution.
359    pub fn grad_optional(
360        &self,
361        output: &TracedTensor,
362        wrt: &TracedTensor,
363    ) -> Result<Option<TracedTensor>> {
364        crate::traced::grad_optional_with_rules_and_cache(
365            output,
366            wrt,
367            &self.semantic_extension_rules,
368            Some(self.ad_transform_cache.as_ref()),
369        )
370    }
371
372    /// Forward-mode Jacobian-vector product.
373    ///
374    /// # Examples
375    ///
376    /// ```rust
377    /// use tenferro_ad::AdContext;
378    /// use tenferro_runtime::TracedTensor;
379    ///
380    /// let ad = AdContext::builder().build().unwrap();
381    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
382    /// let dx = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
383    /// let y = (&x * &x).unwrap();
384    /// let dy = ad.jvp(&y, &x, &dx).unwrap();
385    /// assert_eq!(dy.rank, 0);
386    /// ```
387    ///
388    /// # Errors
389    ///
390    /// Returns [`tenferro_runtime::Error::UnsupportedAdRule`] when the graph
391    /// has no JVP rule, [`tenferro_runtime::Error::Validation`] for
392    /// inconsistent tangent metadata, or a typed backend/runtime-state error
393    /// during evaluation.
394    pub fn jvp(
395        &self,
396        output: &TracedTensor,
397        wrt: &TracedTensor,
398        tangent: &TracedTensor,
399    ) -> Result<TracedTensor> {
400        crate::traced::jvp_with_rules_and_cache(
401            output,
402            wrt,
403            tangent,
404            &self.semantic_extension_rules,
405            Some(self.ad_transform_cache.as_ref()),
406        )
407    }
408
409    /// Forward-mode Jacobian-vector product that returns `None` for inactive output.
410    ///
411    /// # Examples
412    ///
413    /// ```rust
414    /// use tenferro_ad::AdContext;
415    /// use tenferro_runtime::TracedTensor;
416    ///
417    /// let ad = AdContext::builder().build().unwrap();
418    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
419    /// let dx = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
420    /// let y = (&x * &x).unwrap();
421    /// assert!(ad.jvp_optional(&y, &x, &dx).unwrap().is_some());
422    /// ```
423    ///
424    /// # Errors
425    ///
426    /// Returns [`tenferro_runtime::Error::UnsupportedAdRule`] when the graph
427    /// has no JVP rule, [`tenferro_runtime::Error::Validation`] for
428    /// inconsistent tangent metadata, or a typed backend/runtime-state error
429    /// during evaluation.
430    pub fn jvp_optional(
431        &self,
432        output: &TracedTensor,
433        wrt: &TracedTensor,
434        tangent: &TracedTensor,
435    ) -> Result<Option<TracedTensor>> {
436        crate::traced::jvp_optional_with_rules_and_cache(
437            output,
438            wrt,
439            tangent,
440            &self.semantic_extension_rules,
441            Some(self.ad_transform_cache.as_ref()),
442        )
443    }
444
445    /// Forward-mode directional derivative for multiple distinct traced leaves.
446    ///
447    /// Reachable leaves are transformed together in one derivative graph.
448    /// Unreachable leaves contribute nothing; an empty or fully unreachable
449    /// request returns `None`. Duplicate `wrt` leaves are rejected before the
450    /// transform because one semantic seed slot cannot accept two tangents.
451    ///
452    /// # Examples
453    ///
454    /// ```rust
455    /// use tenferro_ad::AdContext;
456    /// use tenferro_runtime::TracedTensor;
457    ///
458    /// let ad = AdContext::builder().build().unwrap();
459    /// let x = TracedTensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap();
460    /// let y = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
461    /// let dx = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
462    /// let dy = TracedTensor::from_vec_col_major(vec![], vec![4.0_f64]).unwrap();
463    /// let output = (&x * &y).unwrap();
464    /// assert!(ad.jvp_many(&output, &[(&x, &dx), (&y, &dy)]).unwrap().is_some());
465    /// ```
466    ///
467    /// # Errors
468    ///
469    /// Returns [`tenferro_runtime::Error::Validation`] for duplicate leaves or
470    /// incompatible tangent metadata, [`tenferro_runtime::Error::UnsupportedAdRule`]
471    /// when a required rule is unavailable, or a typed runtime-state error when
472    /// derivative graph construction fails.
473    ///
474    /// # Deferred errors
475    ///
476    /// Symbolic shape constraints may fail during later compilation or execution.
477    pub fn jvp_many(
478        &self,
479        output: &TracedTensor,
480        wrt_tangents: &[(&TracedTensor, &TracedTensor)],
481    ) -> Result<Option<TracedTensor>> {
482        crate::traced::jvp_many_with_rules_and_cache(
483            output,
484            wrt_tangents,
485            &self.semantic_extension_rules,
486            Some(self.ad_transform_cache.as_ref()),
487        )
488    }
489
490    /// Reverse-mode vector-Jacobian product.
491    ///
492    /// Complex cotangents use tenferro's Hermitian real-inner-product
493    /// convention. Non-real complex cotangent seeds therefore need an explicit
494    /// seed-convention comparison when matching JAX. See
495    /// <https://tensor4all.org/tenferro-rs/guides/complex-ad.html>.
496    ///
497    /// # Examples
498    ///
499    /// ```rust
500    /// use tenferro_ad::AdContext;
501    /// use tenferro_runtime::TracedTensor;
502    ///
503    /// let ad = AdContext::builder().build().unwrap();
504    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
505    /// let dy = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
506    /// let y = (&x * &x).unwrap();
507    /// let dx = ad.vjp(&y, &x, &dy).unwrap();
508    /// assert_eq!(dx.rank, 0);
509    /// ```
510    ///
511    /// # Errors
512    ///
513    /// Returns [`tenferro_runtime::Error::Validation`] when the cotangent
514    /// metadata is incompatible, [`tenferro_runtime::Error::UnsupportedAdRule`]
515    /// when a VJP rule is unavailable, or a typed backend/runtime-state error
516    /// during execution.
517    pub fn vjp(
518        &self,
519        output: &TracedTensor,
520        wrt: &TracedTensor,
521        cotangent: &TracedTensor,
522    ) -> Result<TracedTensor> {
523        crate::traced::vjp_with_rules_and_cache(
524            output,
525            wrt,
526            cotangent,
527            &self.semantic_extension_rules,
528            Some(self.ad_transform_cache.as_ref()),
529        )
530    }
531
532    /// Reverse-mode vector-Jacobian product that returns `None` for inactive input.
533    ///
534    /// # Examples
535    ///
536    /// ```rust
537    /// use tenferro_ad::AdContext;
538    /// use tenferro_runtime::TracedTensor;
539    ///
540    /// let ad = AdContext::builder().build().unwrap();
541    /// let x = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
542    /// let dy = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
543    /// let y = (&x * &x).unwrap();
544    /// assert!(ad.vjp_optional(&y, &x, &dy).unwrap().is_some());
545    /// ```
546    ///
547    /// # Errors
548    ///
549    /// Returns [`tenferro_runtime::Error::Validation`] when the cotangent
550    /// metadata is incompatible, [`tenferro_runtime::Error::UnsupportedAdRule`]
551    /// when a VJP rule is unavailable, or a typed backend/runtime-state error
552    /// during execution.
553    pub fn vjp_optional(
554        &self,
555        output: &TracedTensor,
556        wrt: &TracedTensor,
557        cotangent: &TracedTensor,
558    ) -> Result<Option<TracedTensor>> {
559        crate::traced::vjp_optional_with_rules_and_cache(
560            output,
561            wrt,
562            cotangent,
563            &self.semantic_extension_rules,
564            Some(self.ad_transform_cache.as_ref()),
565        )
566    }
567
568    /// Reverse-mode products for multiple traced leaves in one derivative graph.
569    ///
570    /// Results align with `wrts`; unreachable leaves produce `None`. Duplicate
571    /// leaves are allowed and repeat the same traced derivative without
572    /// accumulating the cotangent twice. An empty request validates that the
573    /// cotangent has concrete data, then returns an empty vector.
574    ///
575    /// # Examples
576    ///
577    /// ```rust
578    /// use tenferro_ad::AdContext;
579    /// use tenferro_runtime::TracedTensor;
580    ///
581    /// let ad = AdContext::builder().build().unwrap();
582    /// let x = TracedTensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap();
583    /// let y = TracedTensor::from_vec_col_major(vec![], vec![3.0_f64]).unwrap();
584    /// let seed = TracedTensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
585    /// let output = (&x * &y).unwrap();
586    /// let products = ad.vjp_many(&output, &[&x, &y], &seed).unwrap();
587    /// assert!(products.iter().all(Option::is_some));
588    /// ```
589    ///
590    /// # Errors
591    ///
592    /// Returns [`tenferro_runtime::Error::Validation`] for invalid cotangent
593    /// metadata, [`tenferro_runtime::Error::UnsupportedAdRule`] when a required
594    /// rule is unavailable, or a typed runtime-state error when derivative graph
595    /// construction fails.
596    ///
597    /// # Deferred errors
598    ///
599    /// Symbolic shape constraints may fail during later compilation or execution.
600    pub fn vjp_many(
601        &self,
602        output: &TracedTensor,
603        wrts: &[&TracedTensor],
604        cotangent: &TracedTensor,
605    ) -> Result<Vec<Option<TracedTensor>>> {
606        crate::traced::vjp_many_with_rules_and_cache(
607            output,
608            wrts,
609            cotangent,
610            &self.semantic_extension_rules,
611            Some(self.ad_transform_cache.as_ref()),
612        )
613    }
614}
615
616/// Builder for [`AdContext`].
617///
618/// # Examples
619///
620/// ```rust
621/// use tenferro_ad::AdContextBuilder;
622///
623/// let ad = AdContextBuilder::new().build().unwrap();
624/// assert!(ad
625///     .semantic_extension_rules()
626///     .lookup_linearize("example.missing.v1")
627///     .is_none());
628/// ```
629#[derive(Clone, Debug, Default)]
630pub struct AdContextBuilder {
631    semantic_extension_rules: SemanticExtensionRuleSet,
632}
633
634impl AdContextBuilder {
635    /// Create an empty builder.
636    ///
637    /// # Examples
638    ///
639    /// ```rust
640    /// use tenferro_ad::AdContextBuilder;
641    ///
642    /// let _builder = AdContextBuilder::new();
643    /// ```
644    pub fn new() -> Self {
645        Self::default()
646    }
647
648    /// Include an owned semantic-program extension AD rule set.
649    ///
650    /// # Errors
651    ///
652    /// Returns [`SemanticExtensionRegistryError::MalformedFamilyId`] when a
653    /// family identifier is invalid, or
654    /// [`SemanticExtensionRegistryError::DuplicateRule`] when the same family
655    /// and role were already supplied.
656    pub fn with_semantic_extension_rules(
657        mut self,
658        rules: SemanticExtensionRuleSet,
659    ) -> std::result::Result<Self, SemanticExtensionRegistryError> {
660        self.semantic_extension_rules.merge(rules)?;
661        Ok(self)
662    }
663
664    /// Build the context.
665    ///
666    /// Semantic extension rules have already been validated and merged by
667    /// [`Self::with_semantic_extension_rules`].
668    ///
669    /// # Examples
670    ///
671    /// ```rust
672    /// use tenferro_ad::AdContext;
673    ///
674    /// let ad = AdContext::builder().build().unwrap();
675    /// assert!(ad
676    ///     .semantic_extension_rules()
677    ///     .lookup_linearize("example.missing.v1")
678    ///     .is_none());
679    /// ```
680    ///
681    /// # Errors
682    ///
683    /// The error type is [`std::convert::Infallible`], so this finalization step
684    /// never returns `Err` after semantic rule registration. It retains a
685    /// `Result` so callers can compose it with the fallible registration step.
686    pub fn build(self) -> std::result::Result<AdContext, std::convert::Infallible> {
687        Ok(AdContext {
688            semantic_extension_rules: self.semantic_extension_rules,
689            ad_transform_cache: Arc::new(AdTransformCache::new()),
690        })
691    }
692}