rustad 0.0.0

A rust Automatic Differentiation library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2025 Bradley M. Bell
// ---------------------------------------------------------------------------
//! This module implements AD atomic functions
//!
//! Then are called atomic functions because they are recorded as a
//! single operation in tapes and ADfn objects.
//!
//! Link to [parent module](super)
// ---------------------------------------------------------------------------
// use
//
use std::sync::RwLock;
use std::thread::LocalKey;
use std::cell::RefCell;
//
use crate::op::id::CALL_OP;
use crate::op::id::CALL_RES_OP;
use crate::tape::Tape;
use crate::tape::sealed::ThisThreadTape;
use crate::{
    IndexT,
    AD,
    ad_from_vector,
    AtomEvalVecPublic,
    ThisThreadTapePublic,
};
//
#[cfg(doc)]
use crate::{
        doc_generic_v,
        ADfn,
};
#[cfg(doc)]
use crate::adfn::{
    forward_zero::doc_forward_zero,
    forward_one::doc_forward_one,
    reverse_one::doc_reverse_one,
};
// ---------------------------------------------------------------------------
//
// AtomForwardZeroValue
/// Callback to atomic functions during [ADfn::forward_zero_value]
///
/// * Required :
/// This function is required for all atomic functions.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *range_one*
/// contains the value of the atomic function range variables.
///
pub type AtomForwardZeroValue<V> = fn(
    _domain_zero   : &Vec<&V>    ,
    _call_info     : IndexT      ,
    _trace         : bool        ,
) -> Vec<V> ;
//
// AtomForwardOneValue
/// Callback to atomic functions during [ADfn::forward_one_value]
///
/// * Required :
/// If you will not use this atomic function with
/// [ADfn::forward_one_value] ,
/// this function should panic if it gets used.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * domain_one :
/// this contains the direction for the directional derivative.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *range_one* is
/// ```text
///     range_one = f'(domain_zero) * domain_one
/// ```
pub type AtomForwardOneValue<V> = fn(
    _domain_zero   : &Vec<&V>    ,
    _domain_one    : Vec<&V>     ,
    _call_info     : IndexT      ,
    _trace         : bool        ,
) -> Vec<V> ;
//
// AtomReverseOneValue
/// Callback to atomic functions during [ADfn::reverse_one_value]
///
/// * Required :
/// If you will not use this atomic function with
/// [ADfn::reverse_one_value] ,
/// this function should panic if it gets used.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * range_one :
/// this contains the function weights for the partial derivatives.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *domain_one* is
/// ```text
///     domain_one = range_one * f'(domain_zero)
/// ```
pub type AtomReverseOneValue<V> = fn(
    _domain_zero   : &Vec<&V>    ,
    _range_one     : Vec<&V>     ,
    _call_info     : IndexT      ,
    _trace         : bool        ,
) -> Vec<V> ;
//
// AtomForwardDepend
/// Atomic function forward dependency type for value evaluations.
///
/// * Required :
/// This function is required for all atomic functions.
///
/// * is_var_domain :
/// This has the same length as *adomain* in the corresponding [call_atom].
/// The j-th component is true (false) if the j-th component
/// of *adomain* is a variable (constant).
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// This has the same length as *arange* in the corresponding [call_atom].
/// The i-th component is true (false) if the i-th component
/// of *arange* depends on a variable in *adomaion.
///
pub type AtomForwardDepend = fn(
    _is_var_domain  : &Vec<bool> ,
    _call_info      : IndexT     ,
    _trace          : bool       ,
)-> Vec<bool>;
//
// AtomForwardZeroAD
/// Callback to atomic functions during [ADfn::forward_zero_ad]
///
/// * Required :
/// If you will not use this atomic function with
/// [ADfn::forward_one_ad] ,
/// this function should panic if it gets used.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *arange_one*
/// contains the value of the atomic function range variables.
///
pub type AtomForwardZeroAD<V> = fn(
    _domain_zero   : &Vec<& AD<V> >     ,
    _call_info     : IndexT             ,
    _trace         : bool               ,
) -> Vec< AD<V> > ;
//
// AtomForwardOneAD
/// Callback to atomic functions during [ADfn::forward_one_ad]
///
/// * Required :
/// If you will not use this atomic function with
/// [ADfn::forward_one_ad] ,
/// this function should panic if it gets used.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * domain_one :
/// this contains the direction for the directional derivative.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *range_one* is
/// ```text
///     range_one = f'(domain_zero) * domain_one
/// ```
pub type AtomForwardOneAD<V> = fn(
    _domain_zero   : &Vec<& AD<V> >    ,
    _domain_one    : Vec<& AD<V> >     ,
    _call_info     : IndexT            ,
    _trace         : bool              ,
) -> Vec< AD<V> > ;
//
// AtomReverseOneAD
/// Callback to atomic functions during [ADfn::reverse_one_ad]
///
/// * Required :
/// If you will not use this atomic function with
/// [ADfn::reverse_one_ad] ,
/// this function should panic if it gets used.
///
/// * domain_zero :
/// this contains the value of the atomic function domain variables.
///
/// * range_one :
/// this contains the function weights for the partial derivatives.
///
/// * call_info :
/// is the *call_info* value used when the atomic function was called.
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
///
/// * return :
/// The return value *domain_one* is
/// ```text
///     domain_one = range_one * f'(domain_zero)
/// ```
pub type AtomReverseOneAD<V> = fn(
    _domain_zero   : &Vec<& AD<V> >    ,
    _range_one     : Vec<& AD<V> >     ,
    _call_info     : IndexT            ,
    _trace         : bool              ,
) -> Vec< AD<V> > ;
//
// AtomEval
/// Atomic function evaluation routines.
pub struct AtomEval<V> {
    //
    // required
    pub name                 : &'static str              ,
    pub forward_depend       : AtomForwardDepend         ,
    //
    pub forward_zero_value   : AtomForwardZeroValue::<V> ,
    pub forward_zero_ad      : Option< AtomForwardZeroAD::<V> >,
    //
    pub forward_one_value    : Option< AtomForwardOneValue::<V> > ,
    pub forward_one_ad       : Option< AtomForwardOneAD::<V> >    ,
    //
    pub reverse_one_value    : Option< AtomReverseOneValue::<V> > ,
    pub reverse_one_ad       : Option< AtomReverseOneAD::<V> >    ,
    //
}
// ----------------------------------------------------------------------------
pub (crate) mod sealed {
    //! The sub-module sealed is used to seal traits in this package.
    //
    use std::sync::RwLock;
    use super::AtomEval;
    //
    // AtomEvalVec
    pub trait AtomEvalVec
    where
        Self : Sized + 'static,
    {   fn get() -> &'static RwLock< Vec< AtomEval<Self> > >;
    }
}
//
// impl_atom_eval_vec!
/// Implement the atomic evaluation vector for value type V
///
/// * V : see [doc_generic_v]
///
/// This macro must be executed once for any type *V*  where
/// `AD<V>` is used. The rustad package automatically executes it
/// for the following types: `f32` , `f64` , `NumVec<f32>`, `NumVec<f64>`.
///
/// This macro can be invoked from anywhere given the following use statements:
/// ```text
///     use std::sync::RwLock;
/// ```
macro_rules! impl_atom_eval_vec{ ($V:ty) => {
    #[doc = concat!(
        "The atomic evaluation vector for value type `", stringify!($V), "`"
    ) ]
    impl crate::atom::sealed::AtomEvalVec for $V {
        fn get() -> &'static
        RwLock< Vec< crate::atom::AtomEval<$V> > > {
            pub(crate) static ATOM_EVAL_VEC :
            RwLock< Vec< crate::atom::AtomEval<$V> > > =
                RwLock::new( Vec::new() );
            &ATOM_EVAL_VEC
        }
    }
} }
pub(crate) use impl_atom_eval_vec;
// ----------------------------------------------------------------------------
// register_atom
/// Register an atomic function.
///
/// * Syntax :
/// ```text
///     atom_id = register_atom(atom_eval)
/// ```
///
/// * V : see [doc_generic_v]
///
/// ## atom_eval :
/// contains references to the callback functions that compute
/// values for this atomic function.
///
/// ## atom_id :
/// is the index that is used to identify this atomic function.
///
pub fn register_atom<V>( atom_eval : AtomEval<V> ) -> IndexT
where
    V : AtomEvalVecPublic ,
{   //
    // rwlock
    let rw_lock : &RwLock< Vec< AtomEval<V> > > = sealed::AtomEvalVec::get();
    //
    // atom_id
    let atom_id           : IndexT;
    let atom_id_too_large : bool;
    {   //
        // write_lock
        let write_lock = rw_lock.write();
        assert!( write_lock.is_ok() );
        //
        // Rest of this block has a lock, so it has to be fast and can't fail.
        let mut atom_eval_vec = write_lock.unwrap();
        let atom_id_usize     = atom_eval_vec.len();
        atom_id_too_large     = (IndexT::MAX as usize) < atom_id_usize;
        atom_id               = atom_eval_vec.len() as IndexT;
        atom_eval_vec.push( atom_eval );
    }
    assert!( ! atom_id_too_large );
    atom_id
}
// ----------------------------------------------------------------------------
// record_call_atom
fn record_call_atom<V>(
    tape                  : &mut Tape<V>                  ,
    forward_depend        : AtomForwardDepend             ,
    adomain               : Vec< AD<V> >                  ,
    range_zero            : Vec<V>                        ,
    atom_id               : IndexT                        ,
    call_info             : IndexT                        ,
    trace                 : bool                          ,
) -> Vec< AD<V> >
where
    V : Clone ,
{   //
    // tape.recordng
    debug_assert!( tape.recording );
    //
    // call_n_arg, call_n_res
    let call_n_arg = adomain.len();
    let call_n_res = range_zero.len();
    //
    // arange
    let mut arange : Vec< AD<V> > = ad_from_vector(range_zero);
    //
    // is_var_arg
    let is_var_arg : Vec<bool> = adomain.iter().map(
        |adomain_j| (*adomain_j).tape_id == tape.tape_id
    ).collect();
    //
    // is_var_res
    let is_var_res = forward_depend(&is_var_arg, call_info, trace);
    //
    // arange, n_var_res
    let mut n_var_res = 0;
    for i in 0 .. call_n_res {
        if is_var_res[i] {
            arange[i].tape_id   = tape.tape_id;
            arange[i].var_index = tape.n_var + n_var_res;
            n_var_res += 1;
        }
    }
    if n_var_res > 0 {
        //
        // tape.id_all, tape.op2arg
        tape.id_all.push( CALL_OP );
        tape.op2arg.push( tape.arg_all.len() as IndexT );
        //
        // tape.arg_all, tape.con_all
        tape.arg_all.push( atom_id );                        // arg[0]
        tape.arg_all.push( call_info );                      // arg[1]
        tape.arg_all.push( call_n_arg as IndexT );           // arg[2]
        tape.arg_all.push( call_n_res as IndexT );           // arg[3]
        tape.arg_all.push( tape.flag_all.len() as IndexT );  // arg[4]
        //
        // tape.arg_all
        for j in 0 .. call_n_arg {
            let index = if is_var_arg[j] {
                adomain[j].var_index
            } else {
                let con_index = tape.con_all.len();
                tape.con_all.push( adomain[j].value.clone() );
                con_index
            };
            tape.arg_all.push( index as IndexT );            // arg[5+j]
        }
        //
        // tape.flag_all
        tape.flag_all.push( trace );               // flag[ arg[4] ]
        for j in 0 .. call_n_arg {
            tape.flag_all.push( is_var_arg[j] );   // flag[ arg[4] + j + 1]
        }
        for i in 0 .. call_n_res {
            tape.flag_all.push( is_var_res[i] );   // flag[ arg[4] + n_res + i]
        }
        //
        // tape.n_var
        tape.n_var += n_var_res;
        //
        // tape.id_all, tape.op2arg
        for _i in 0 .. (n_var_res - 1) {
            tape.id_all.push( CALL_RES_OP );
            tape.op2arg.push( tape.arg_all.len() as IndexT );
        }
    }
    arange
}
// ----------------------------------------------------------------------------
// call_atom
/// Make an AD call to an atomic function.
///
/// Compute the result of an atomic function and,
/// if this thread is currently recording, include the call in its tape.
///
/// * V : see [doc_generic_v]
///
/// * adomain :
/// This is the value of the arguments to the atomic function.
/// `
/// * atom_id :
/// The [atom_id](register_atom#atom_id) returned by register_atom for this
/// atomic function.
///
/// * call_info :
/// This is information about this call that is be passed on to the
/// callback functions specified by [atom_eval](register_atom#atom_eval).
///
/// * trace :
/// if true, a trace of the calculations may be printed on stdout.
/// This may be useful for debugging atomic functions.
///
/// * return :
/// The return value *arange* is the range, as a function of the domain,
/// for this atomic function.
///
pub fn call_atom<V>(
    adomain     : Vec< AD<V> > ,
    atom_id     : IndexT       ,
    call_info   : IndexT       ,
    trace       : bool         ,
) -> Vec< AD<V> >
where
    V   : Clone + From<f32> + ThisThreadTapePublic + AtomEvalVecPublic ,
{
    //
    // local_key
    let local_key : &LocalKey< RefCell< Tape<V> > > = ThisThreadTape::get();
    //
    // recording
    let recording : bool = local_key.with_borrow( |tape| tape.recording );
    //
    // rwlock
    let rw_lock : &RwLock< Vec< AtomEval<V> > > = sealed::AtomEvalVec::get();
    //
    // forward_zero, forward_depend
    let forward_zero   : AtomForwardZeroValue<V>;
    let forward_depend : AtomForwardDepend;
    {   //
        // read_lock
        let read_lock = rw_lock.read();
        assert!( read_lock.is_ok() );
        //
        // Rest of this block has a lock, so it should be fast and not fail.
        let atom_eval_vec = read_lock.unwrap();
        let atom_eval     = &atom_eval_vec[atom_id as usize];
        forward_zero      = atom_eval.forward_zero_value.clone();
        forward_depend    = atom_eval.forward_depend.clone();
    }
    //
    // domain_zero
    let mut domain_zero : Vec<&V> = Vec::with_capacity( adomain.len() );
    for j in 0 .. adomain.len() {
        domain_zero.push( &adomain[j].value );
    }
    //
    // range_zero
    let range_zero  = forward_zero( &domain_zero, call_info, trace );
    //
    // arange
    let arange : Vec< AD<V> >;
    if ! recording {
        arange = ad_from_vector(range_zero);
    } else {
        arange = local_key.with_borrow_mut( |tape| record_call_atom::<V>(
            tape,
            forward_depend,
            adomain,
            range_zero,
            atom_id,
            call_info,
            trace,
        ) );
    }
    arange
}