xad-rs 0.8.0

Exact automatic differentiation for Rust — forward-mode, reverse-mode, first- and second-order, with named variable support and a unified `Real` trait for mode-agnostic numerical code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! `NamedForwardTape<T>` — declare-and-scope lifecycle for named
//! forward-mode values.
//!
//! Build phase: declare named inputs against a mutable
//! [`NamedForwardTape<T>`]. Each `declare_*` returns an opaque handle.
//! Lock-and-use phase: consume the tape via
//! [`NamedForwardTape::into_scope`] to obtain a
//! [`NamedForwardScope<T>`]; the scope owns the constructed values and
//! exposes them via the handles.
//!
//! Both types are generic over a single `T: Passive` (defaulting to
//! `f64`). The TLS slot used for the cross-registry debug guard is
//! scalar-type-independent and is shared across all `T`.

use std::cell::Cell;
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicU64, Ordering};

use indexmap::IndexSet;

use crate::passive::Passive;
use crate::registry::VarRegistry;

// === TLS generation counter (debug-only) ===

#[cfg(debug_assertions)]
static NEXT_GEN: AtomicU64 = AtomicU64::new(1);

thread_local! {
    #[cfg(debug_assertions)]
    static ACTIVE_GEN: Cell<u64> = const { Cell::new(0) };

    static ACTIVE_REGISTRY: Cell<*const VarRegistry> = const { Cell::new(std::ptr::null()) };
}

pub(crate) fn with_active_registry<R>(f: impl FnOnce(Option<&VarRegistry>) -> R) -> R {
    ACTIVE_REGISTRY.with(|c| {
        let ptr = c.get();
        let reg_ref: Option<&VarRegistry> = if ptr.is_null() {
            None
        } else {
            Some(unsafe { &*ptr })
        };
        f(reg_ref)
    })
}

#[cfg(debug_assertions)]
#[inline(always)]
pub(crate) fn current_gen() -> u64 {
    ACTIVE_GEN.with(|c| c.get())
}

#[cfg(debug_assertions)]
#[inline(always)]
pub(crate) fn check_gen(lhs: u64, rhs: u64) {
    assert_eq!(
        lhs, rhs,
        "xad_rs::named: cross-registry forward-mode op detected (lhs tape generation = {lhs}, rhs tape generation = {rhs}). \
         Both operands must come from the same NamedForwardTape scope."
    );
}

#[cfg(not(debug_assertions))]
#[inline(always)]
#[allow(dead_code)]
pub(crate) fn check_gen(_lhs: (), _rhs: ()) {}

// === Handles ===
//
// Handles carry the scalar type `T` so the wrong scope cannot be
// indexed with a foreign-T handle. `PhantomData<fn() -> T>` is the
// covariant, drop-check-free encoding — `T: Passive` is `'static` so
// either form is sound, but `fn() -> T` is the conventional choice for
// "this handle stands in for a value of type T".

/// Handle to a `NamedJet1<T>` declared on a `NamedForwardTape<T>`.
pub struct Jet1Handle<T: Passive = f64> {
    idx: usize,
    _t: PhantomData<fn() -> T>,
}

impl<T: Passive> Clone for Jet1Handle<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T: Passive> Copy for Jet1Handle<T> {}
impl<T: Passive> fmt::Debug for Jet1Handle<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Jet1Handle").field("idx", &self.idx).finish()
    }
}
impl<T: Passive> PartialEq for Jet1Handle<T> {
    fn eq(&self, other: &Self) -> bool {
        self.idx == other.idx
    }
}
impl<T: Passive> Eq for Jet1Handle<T> {}

/// Handle to a `NamedJet1Vec<T>` declared on a `NamedForwardTape<T>`.
pub struct Jet1VecHandle<T: Passive = f64> {
    idx: usize,
    _t: PhantomData<fn() -> T>,
}

impl<T: Passive> Clone for Jet1VecHandle<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T: Passive> Copy for Jet1VecHandle<T> {}
impl<T: Passive> fmt::Debug for Jet1VecHandle<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Jet1VecHandle")
            .field("idx", &self.idx)
            .finish()
    }
}
impl<T: Passive> PartialEq for Jet1VecHandle<T> {
    fn eq(&self, other: &Self) -> bool {
        self.idx == other.idx
    }
}
impl<T: Passive> Eq for Jet1VecHandle<T> {}

/// Handle to a `NamedJet2<T>` declared on a `NamedForwardTape<T>`.
pub struct Jet2Handle<T: Passive = f64> {
    idx: usize,
    _t: PhantomData<fn() -> T>,
}

impl<T: Passive> Clone for Jet2Handle<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T: Passive> Copy for Jet2Handle<T> {}
impl<T: Passive> fmt::Debug for Jet2Handle<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Jet2Handle").field("idx", &self.idx).finish()
    }
}
impl<T: Passive> PartialEq for Jet2Handle<T> {
    fn eq(&self, other: &Self) -> bool {
        self.idx == other.idx
    }
}
impl<T: Passive> Eq for Jet2Handle<T> {}

// === NamedForwardTape ===

/// Builder used to declare named inputs of scalar type `T`. Convert to
/// a [`NamedForwardScope<T>`] via [`NamedForwardTape::into_scope`] to
/// access the constructed values.
pub struct NamedForwardTape<T: Passive = f64> {
    builder: IndexSet<String>,
    pending_jet1: Vec<(String, T)>,
    pending_jet1vec: Vec<(String, T)>,
    pending_jet2: Vec<(String, T)>,
    #[cfg(debug_assertions)]
    #[allow(dead_code)]
    gen_id: u64,
    #[cfg(debug_assertions)]
    prev_gen: u64,
    /// `true` iff `into_scope` consumed this tape — suppresses the
    /// generation-restore in `Drop` (the scope owns that responsibility).
    consumed: bool,
    _not_send: PhantomData<*const ()>,
}

impl<T: Passive> NamedForwardTape<T> {
    /// Create a new, empty tape. In debug builds this also bumps the
    /// global TLS generation so cross-tape ops can be detected.
    pub fn new() -> Self {
        #[cfg(debug_assertions)]
        let new_gen = NEXT_GEN.fetch_add(1, Ordering::Relaxed);
        #[cfg(debug_assertions)]
        let prev_gen = ACTIVE_GEN.with(|c| {
            let p = c.get();
            c.set(new_gen);
            p
        });
        Self {
            builder: IndexSet::new(),
            pending_jet1: Vec::new(),
            pending_jet1vec: Vec::new(),
            pending_jet2: Vec::new(),
            #[cfg(debug_assertions)]
            gen_id: new_gen,
            #[cfg(debug_assertions)]
            prev_gen,
            consumed: false,
            _not_send: PhantomData,
        }
    }

    /// Declare a named [`crate::forward::jet1::NamedJet1`] input.
    pub fn declare_jet1(&mut self, name: &str, value: T) -> Jet1Handle<T> {
        assert!(
            !self.consumed,
            "NamedForwardTape::declare_jet1({:?}) called after into_scope",
            name
        );
        if !self.builder.contains(name) {
            self.builder.insert(name.to_string());
        }
        let idx = self.pending_jet1.len();
        self.pending_jet1.push((name.to_string(), value));
        Jet1Handle {
            idx,
            _t: PhantomData,
        }
    }

    /// Declare a named [`crate::forward::jet1vec::NamedJet1Vec`] input.
    pub fn declare_jet1vec(&mut self, name: &str, value: T) -> Jet1VecHandle<T> {
        assert!(
            !self.consumed,
            "NamedForwardTape::declare_jet1vec({:?}) called after into_scope",
            name
        );
        if !self.builder.contains(name) {
            self.builder.insert(name.to_string());
        }
        let idx = self.pending_jet1vec.len();
        self.pending_jet1vec.push((name.to_string(), value));
        Jet1VecHandle {
            idx,
            _t: PhantomData,
        }
    }

    /// Declare a named [`crate::forward::jet2::NamedJet2`] input.
    pub fn declare_jet2(&mut self, name: &str, value: T) -> Jet2Handle<T> {
        assert!(
            !self.consumed,
            "NamedForwardTape::declare_jet2({:?}) called after into_scope",
            name
        );
        if !self.builder.contains(name) {
            self.builder.insert(name.to_string());
        }
        let idx = self.pending_jet2.len();
        self.pending_jet2.push((name.to_string(), value));
        Jet2Handle {
            idx,
            _t: PhantomData,
        }
    }

    /// Consume the tape and produce a [`NamedForwardScope<T>`] that
    /// owns all declared values. Activates the registry on the TLS slot
    /// so named-derivative accessors find it.
    pub fn into_scope(mut self) -> NamedForwardScope<T> {
        assert!(
            !self.consumed,
            "NamedForwardTape::into_scope called twice"
        );

        let reg = Arc::new(VarRegistry::from_names(self.builder.iter().cloned()));

        // Activate the registry on the TLS slot, saving the previous pointer
        // so the scope's Drop can restore it.
        let new_ptr: *const VarRegistry = Arc::as_ptr(&reg);
        let prev_registry = ACTIVE_REGISTRY.with(|c| {
            let prev = c.get();
            c.set(new_ptr);
            prev
        });

        // Mark consumed so the tape's Drop is a no-op for the gen restore
        // (the scope now owns that responsibility).
        self.consumed = true;
        #[cfg(debug_assertions)]
        let prev_gen = self.prev_gen;

        // Drain pending lists into owned scope storage.
        let pending_jet1 = std::mem::take(&mut self.pending_jet1);
        let n_jet1 = pending_jet1.len();
        let mut jet1s: Vec<crate::forward::jet1::NamedJet1<T>> = Vec::with_capacity(n_jet1);
        for (_name, value) in pending_jet1.into_iter() {
            // `Jet1<T>` carries one tangent direction; seeding with `1.0`
            // matches the original `input_jet1` semantics.
            let inner = crate::forward::jet1::Jet1::<T>::new(value, T::one());
            jet1s.push(crate::forward::jet1::NamedJet1::<T>::__from_inner(inner));
        }

        let pending_jet1vec = std::mem::take(&mut self.pending_jet1vec);
        let n_jet1vec = pending_jet1vec.len();
        let mut jet1vecs: Vec<crate::forward::jet1vec::NamedJet1Vec<T>> =
            Vec::with_capacity(n_jet1vec);
        for (name, value) in pending_jet1vec.into_iter() {
            let reg_idx = reg
                .index_of(&name)
                .expect("declared name missing from frozen registry");
            let inner = crate::forward::jet1vec::Jet1Vec::<T>::variable(value, reg_idx, n_jet1vec);
            jet1vecs.push(crate::forward::jet1vec::NamedJet1Vec::<T>::__from_inner(
                inner,
            ));
        }

        let pending_jet2 = std::mem::take(&mut self.pending_jet2);
        let mut jet2s: Vec<crate::forward::jet2::NamedJet2<T>> =
            Vec::with_capacity(pending_jet2.len());
        for (name, value) in pending_jet2.into_iter() {
            let reg_idx = reg
                .index_of(&name)
                .expect("declared name missing from frozen registry");
            let inner = crate::forward::jet2::Jet2::<T>::variable(value);
            jet2s.push(crate::forward::jet2::NamedJet2::<T>::__from_parts(
                inner,
                Some(reg_idx),
            ));
        }

        NamedForwardScope {
            registry: reg,
            jet1s,
            jet1vecs,
            jet2s,
            prev_registry,
            #[cfg(debug_assertions)]
            prev_gen,
            _not_send: PhantomData,
        }
    }

    /// Recovery hook for the `std::mem::forget` path. Resets the active
    /// registry pointer and (in debug) the active generation. Static so
    /// it can be called without a live tape instance.
    pub fn deactivate_all() {
        ACTIVE_REGISTRY.with(|c| c.set(std::ptr::null()));
        #[cfg(debug_assertions)]
        ACTIVE_GEN.with(|c| c.set(0));
    }
}

impl<T: Passive> Default for NamedForwardTape<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Passive> fmt::Debug for NamedForwardTape<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NamedForwardTape")
            .field("inputs", &self.builder.len())
            .field("pending_jet1", &self.pending_jet1.len())
            .field("pending_jet1vec", &self.pending_jet1vec.len())
            .field("pending_jet2", &self.pending_jet2.len())
            .field("consumed", &self.consumed)
            .finish()
    }
}

impl<T: Passive> Drop for NamedForwardTape<T> {
    fn drop(&mut self) {
        // If `into_scope` ran, the scope owns the TLS-restore. If the
        // tape was dropped without calling `into_scope` (no scope was
        // constructed), restore the previous generation here so nested
        // tapes don't leak their generation upward.
        if !self.consumed {
            #[cfg(debug_assertions)]
            ACTIVE_GEN.with(|c| c.set(self.prev_gen));
        }
    }
}

// === NamedForwardScope ===

/// Owns the values declared on a [`NamedForwardTape<T>`] and the active
/// registry. The scope is `!Send` and restores the previous TLS state
/// on drop.
pub struct NamedForwardScope<T: Passive = f64> {
    registry: Arc<VarRegistry>,
    jet1s: Vec<crate::forward::jet1::NamedJet1<T>>,
    jet1vecs: Vec<crate::forward::jet1vec::NamedJet1Vec<T>>,
    jet2s: Vec<crate::forward::jet2::NamedJet2<T>>,
    prev_registry: *const VarRegistry,
    #[cfg(debug_assertions)]
    prev_gen: u64,
    _not_send: PhantomData<*const ()>,
}

impl<T: Passive> NamedForwardScope<T> {
    /// Look up a declared `NamedJet1<T>` by handle.
    #[inline]
    pub fn jet1(&self, handle: Jet1Handle<T>) -> &crate::forward::jet1::NamedJet1<T> {
        &self.jet1s[handle.idx]
    }

    /// Look up a declared `NamedJet1Vec<T>` by handle.
    #[inline]
    pub fn jet1vec(&self, handle: Jet1VecHandle<T>) -> &crate::forward::jet1vec::NamedJet1Vec<T> {
        &self.jet1vecs[handle.idx]
    }

    /// Look up a declared `NamedJet2<T>` by handle.
    #[inline]
    pub fn jet2(&self, handle: Jet2Handle<T>) -> &crate::forward::jet2::NamedJet2<T> {
        &self.jet2s[handle.idx]
    }

    /// Construct a scope-stamped `NamedJet1<T>` constant (zero tangent).
    #[inline]
    pub fn constant_jet1(&self, value: T) -> crate::forward::jet1::NamedJet1<T> {
        let inner = crate::forward::jet1::Jet1::<T>::constant(value);
        crate::forward::jet1::NamedJet1::<T>::__from_inner(inner)
    }

    /// Construct a scope-stamped `NamedJet1Vec<T>` constant (zero gradient,
    /// length matching the active registry).
    #[inline]
    pub fn constant_jet1vec(&self, value: T) -> crate::forward::jet1vec::NamedJet1Vec<T> {
        let inner = crate::forward::jet1vec::Jet1Vec::<T>::constant(value, self.registry.len());
        crate::forward::jet1vec::NamedJet1Vec::<T>::__from_inner(inner)
    }

    /// Construct a scope-stamped `NamedJet2<T>` constant (no seeded direction).
    #[inline]
    pub fn constant_jet2(&self, value: T) -> crate::forward::jet2::NamedJet2<T> {
        let inner = crate::forward::jet2::Jet2::<T>::constant(value);
        crate::forward::jet2::NamedJet2::<T>::__from_parts(inner, None)
    }

    /// Borrow the scope's frozen registry.
    #[inline]
    pub fn registry(&self) -> &Arc<VarRegistry> {
        &self.registry
    }
}

impl<T: Passive> fmt::Debug for NamedForwardScope<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NamedForwardScope")
            .field("registry_len", &self.registry.len())
            .field("jet1s", &self.jet1s.len())
            .field("jet1vecs", &self.jet1vecs.len())
            .field("jet2s", &self.jet2s.len())
            .finish()
    }
}

impl<T: Passive> Drop for NamedForwardScope<T> {
    fn drop(&mut self) {
        ACTIVE_REGISTRY.with(|c| c.set(self.prev_registry));
        #[cfg(debug_assertions)]
        ACTIVE_GEN.with(|c| c.set(self.prev_gen));
    }
}