aicad 1.0.0-rc.1

A library to solve Constraint satisfaction problems with Multi-valued decision diagrams and, optionnaly, neural networks
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
//! The training loop of the neural networks.
//!
//! [`train_model`] fits a network on a set of problems with a [`Loss`]: each
//! epoch it goes through the training batches, takes an optimiser step per batch, and every
//! `validation_interval` epochs scores the validation set. The weights with the lowest validation
//! loss are saved in the output directory, so that a run can be loaded back (see
//! [`crate::neural::load_network`], which also reads the network configuration from there) or
//! continued. Training stops early if the validation loss
//! stops improving, when asked to.

use burn::config::Config;
use burn::data::dataloader::DataLoaderBuilder;
use burn::data::dataloader::batcher::Batcher;
use burn::data::dataset::Dataset;
use burn::module::{AutodiffModule, Module};
use burn::optim::GradientsParams;
use burn::prelude::ElementConversion;
use burn::record::CompactRecorder;
use burn::tensor::backend::AutodiffBackend;

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

use crate::learning::Loss;
use crate::learning::monitoring::SatisfactionReport;
use crate::learning::optimiser::OptimiserConfig;
use crate::modelling::Problem;
use crate::neural::{Batch, Network, NetworkConfig};

/// Configuration of the training loop
#[derive(Config, Debug)]
pub struct TrainingConfig {
    /// Number of epochs
    #[config(default = 10)]
    pub num_epochs: usize,
    /// Batch size
    #[config(default = 512)]
    pub batch_size: usize,
    /// How often (in epochs) to evaluate the loss on the validation set and update the best-so-far
    /// model, the one with the lowest validation loss.
    #[config(default = 10)]
    pub validation_interval: usize,
    /// The optimiser, with its learning rate and hyper-parameters (Adam by default).
    #[config(default = "OptimiserConfig::default()")]
    pub optimiser: OptimiserConfig,
    /// Stop training once the validation loss hasn't improved by more than
    /// `early_stopping_min_delta` for this many consecutive validation checks. `None` (the
    /// default) disables early stopping, matching the previous always-run-`num_epochs` behavior.
    #[config(default = "None")]
    pub early_stopping_patience: Option<usize>,
    /// How much the validation loss must drop below `best_score` at a validation check to
    /// count as progress rather than stagnation. Only used when `early_stopping_patience` is set.
    #[config(default = 1e-4)]
    pub early_stopping_min_delta: f64,
    /// A checkpoint directory (as written to `out_dir` by an earlier run) whose weights initialise
    /// the network before the first epoch, to continue or fine-tune a previous run. `None` (the
    /// default) starts from a fresh random initialisation. The network configuration must be the
    /// one the weights were trained with.
    #[config(default = "None")]
    pub initial_weights: Option<PathBuf>,
}

/// Trains a model. Generic over the backend (B), the network configuration (NC), the training
/// sample type (S), the training batch type (TBatch), the Batcher (Ba), the loss function (L),
/// the validation sample type (SValid) and the validation batch type (VBatch). This is designed
/// so each neural network can be learned with this method against several different training
/// recipes sharing the same architecture. For example, the classical per-constraint-penalty
/// ConsFormer loss and the MDD-WMC ConsFormer loss both drive the same `ConsFormer` network
/// just by instantiating this function with a different `(S, TBatch, L, SValid, VBatch)` tuple)
///
/// network_config: Neural network configuration (type and hyper-parameters)
/// train_dataset: training dataset, used for gradient updates
/// valid_dataset: held-out dataset, used only for model selection (built on `B::InnerBackend`
///                since evaluating it never needs autodiff bookkeeping)
/// batcher: shared by both dataloaders; needs to implement `Batcher` for both `B` and
///          `B::InnerBackend`, which is automatic for any batcher generic over `Backend`
/// loss_fn: The loss function
/// training: Configuration of the training loop
/// device: Device to launch the training on (cpu or gpu)
///
/// Returns the best model found, the one with the lowest validation loss, which is also saved in
/// `out_dir/weights`. When no validation check ran (for instance `num_epochs` is smaller than
/// `validation_interval`), nothing is saved and the network as it is at the end is returned.
#[allow(clippy::too_many_arguments)]
pub fn train_model<B, NC, S, TBatch, Ba, L, SValid, VBatch>(
    network_config: NC,
    problems: &[Arc<Problem>],
    train_dataset: impl Dataset<S> + 'static,
    valid_dataset: impl Dataset<SValid> + 'static,
    batcher: Ba,
    mut loss_fn: L,
    training: TrainingConfig,
    out_dir: &Path,
    device: &B::Device,
) -> Result<NC::N, String>
where
    B: AutodiffBackend,
    NC: NetworkConfig<B>,
    NC::N: AutodiffModule<B> + Clone + Network<B, TBatch>,
    <NC::N as AutodiffModule<B>>::InnerModule: Network<B::InnerBackend, VBatch>,
    S: Send + Sync + Clone + std::fmt::Debug + 'static,
    SValid: Send + Sync + Clone + std::fmt::Debug + 'static,
    TBatch: Batch<B>,
    VBatch: Batch<B::InnerBackend>,
    Ba: Batcher<B, S, TBatch>
        + Batcher<B::InnerBackend, SValid, VBatch>
        + Clone
        + Send
        + Sync
        + 'static,
    L: Loss<B, TBatch> + Loss<B::InnerBackend, VBatch>,
{
    if training.num_epochs > 0
        && (training.validation_interval == 0 || training.validation_interval > training.num_epochs)
    {
        panic!(
            "Training over {} epochs and validation interval is {} (should be in [1, {}]",
            training.num_epochs, training.validation_interval, training.num_epochs
        );
    }
    // Let the loss prepare whatever it needs about the problems (e.g. compile their MDDs).
    <L as Loss<B, TBatch>>::prepare(&mut loss_fn, problems);

    // Initialise the network architecture with the given parameters.
    let mut network = network_config.init(problems, device);
    // If some initial weights are given as parameters, we load them. This allows restarting the
    // learning from a previous checkpoint (e.g., for adaptative learning, fine-tuning, etc.)
    if let Some(checkpoint) = &training.initial_weights {
        network = network
            .load_file(checkpoint.join("weights"), &CompactRecorder::new(), device)
            .map_err(|e| {
                format!(
                    "cannot load the initial weights from {}: {e} (expected a checkpoint \
                     directory holding `weights.mpk`, trained with this network configuration)",
                    checkpoint.display()
                )
            })?;
        log::info!("initial weights loaded from {}", checkpoint.display());
    }

    // Load the training and validation datasets
    let train_dataloader = DataLoaderBuilder::new(batcher.clone())
        .batch_size(training.batch_size)
        .build(train_dataset);

    let valid_dataloader = DataLoaderBuilder::new(batcher)
        .batch_size(training.batch_size)
        .build(valid_dataset);

    // Initialise the optimiser.
    let mut optim = training.optimiser.init::<B, NC::N>();

    let mut best_score = f64::INFINITY;
    // Whether `out_dir/weights` holds a best-so-far model (no validation may ever have run)
    let mut best_saved = false;
    let mut stagnant_checks = 0usize;

    // Actual training loop over the number of epochs
    for epoch in 0..training.num_epochs {
        let mut epoch_loss_sum = 0.0;
        let mut epoch_batches = 0usize;
        // Some logging for learning monitoring. Doesn't imply that a "better" satisfiaction report
        // means better learning, it's just an heuristical way of checking how learning behaves.
        let mut epoch_report = SatisfactionReport::default();
        // Some runtime statistics for logging. The statistics are over the batches; it is the sum
        // of each batch runtime, so it can be used as a comparison if the batch sizes are equal.
        // Total time passed in forward pass of the neural network.
        let epoch_start = Instant::now();

        let mut t_forward = 0.0f64;
        let mut t_loss = 0.0f64;
        let mut t_backward = 0.0f64;

        for batch in train_dataloader.iter() {
            let t = Instant::now();
            let logits = network.forward(&batch);
            B::sync(device).ok();
            t_forward += t.elapsed().as_secs_f64();

            epoch_report.update(batch.problems(), &batch.decode(logits.clone()));

            let t = Instant::now();
            let loss = loss_fn.loss(logits.clone(), &batch);
            B::sync(device).ok();
            t_loss += t.elapsed().as_secs_f64();
            let loss_scalar = loss.clone().into_scalar().elem::<f32>();
            if !loss_scalar.is_finite() {
                let logits_data: Vec<f32> = logits
                    .into_data()
                    .to_vec::<f32>()
                    .unwrap_or_else(|_| Vec::new());
                let nan_count = logits_data.iter().filter(|v| v.is_nan()).count();
                let inf_count = logits_data.iter().filter(|v| v.is_infinite()).count();
                let (finite_min, finite_max) = logits_data
                    .iter()
                    .filter(|v| v.is_finite())
                    .fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), &v| {
                        (lo.min(v), hi.max(v))
                    });
                panic!(
                    "epoch {epoch}: loss went non-finite ({loss_scalar}) -- stopping before this \
                     batch's optimizer step so the network/optimizer state isn't poisoned any \
                     further. logits: {nan_count} NaN, {inf_count} Inf out of {} values, finite \
                     range [{finite_min}, {finite_max}]. If validation checkpointing is enabled, \
                     the file saved at the best score before this epoch is your recovery point.",
                    logits_data.len(),
                );
            }

            let t = Instant::now();
            let grads = GradientsParams::from_grads(loss.backward(), &network);
            network = optim.step(network, grads);
            B::sync(device).ok();
            t_backward += t.elapsed().as_secs_f64();

            epoch_loss_sum += loss_scalar;
            epoch_batches += 1;
        }
        let epoch_rt = epoch_start.elapsed().as_secs();
        log::info!(
            "epoch {epoch}: forward={t_forward:.3}s loss={t_loss:.3}s backward+step={t_backward:.3}s"
        );
        let epoch_loss = epoch_loss_sum / epoch_batches as f32;
        if !epoch_loss.is_finite() {
            log::warn!("Non-finite train loss at epoch {epoch}");
        }
        log::info!("epoch {epoch}: loss = {epoch_loss} ({epoch_rt} seconds)");
        crate::diagnostics::data_log!(
            "train_epoch",
            epoch = epoch,
            t_forward = t_forward,
            t_loss = t_loss,
            t_backward = t_backward,
            epoch_rt_s = epoch_start.elapsed().as_secs_f64(),
            epoch_loss = epoch_loss,
        );
        epoch_report.print(40);

        if (epoch + 1) % training.validation_interval == 0 {
            // Pass the network in validation mode
            let valid_network = network.valid();
            let mut valid_loss_sum = 0.0f64;
            let mut valid_batches = 0usize;

            for batch in valid_dataloader.iter() {
                let logits = valid_network.forward(&batch);
                let loss = loss_fn.loss(logits, &batch);
                valid_loss_sum += loss.into_scalar().elem::<f32>() as f64;
                valid_batches += 1;
            }

            let score = valid_loss_sum / valid_batches as f64;
            if !score.is_finite() {
                log::warn!("Non-finite validation score at epoch {epoch}");
            }
            log::info!("epoch {epoch}: validation loss = {score:.4}");
            crate::diagnostics::data_log!("valid_epoch", epoch = epoch, score = score,);

            let improved = score < best_score - training.early_stopping_min_delta;

            if score < best_score {
                best_score = score;
                match network
                    .clone()
                    .save_file(out_dir.join("weights"), &CompactRecorder::new())
                {
                    Ok(()) => best_saved = true,
                    Err(e) => {
                        log::warn!("warning: failed to save checkpoint at epoch {epoch}: {e}")
                    }
                }
            }

            if let Some(patience) = training.early_stopping_patience {
                if improved {
                    stagnant_checks = 0;
                } else {
                    stagnant_checks += 1;
                    log::info!(
                        "epoch {epoch}: no improvement > {} for {stagnant_checks}/{patience} \
                         validation checks",
                        training.early_stopping_min_delta,
                    );
                    if stagnant_checks >= patience {
                        log::info!(
                            "epoch {epoch}: early stopping -- {stagnant_checks} validation \
                             checks without improvement > {}",
                            training.early_stopping_min_delta,
                        );
                        break;
                    }
                }
            }
        }

        log::info!("");
    }

    // Return the best model rather than the last one. Reading it back from the checkpoint avoids
    // keeping a second copy of the network in memory during training.
    if best_saved {
        network = network
            .load_file(out_dir.join("weights"), &CompactRecorder::new(), device)
            .map_err(|e| {
                format!(
                    "cannot reload the best model from {}: {e}",
                    out_dir.display()
                )
            })?;
    }
    Ok(network)
}

#[cfg(test)]
mod tests {
    use super::*;

    use burn::backend::ndarray::NdArrayDevice;
    use burn::backend::{Autodiff, NdArray};

    use crate::constraints::not_equals;
    use crate::learning::consformer::ConsFormerLoss;
    use crate::modelling::Problem;
    use crate::neural::consformer::{ConsFormerBatcher, ConsFormerConfig, ConsFormerDataset};
    use crate::neural::load_network;

    #[test]
    fn early_stopping_stops_before_num_epochs_once_the_score_stops_improving() {
        type ADBackend = Autodiff<NdArray>;
        let device = NdArrayDevice::default();

        let out_dir =
            std::env::temp_dir().join(format!("aicad_test_early_stopping_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&out_dir);

        let mut problem = Problem::default();
        let x = problem.add_variable(vec![0, 1, 2]);
        let y = problem.add_variable(vec![0, 1, 2]);
        not_equals(&mut problem, x, y);
        let problems = vec![Arc::new(problem)];

        let network_config = ConsFormerConfig::new(3, 4, 4, 1, 4, 0, 0.5, 1.0).with_num_layers(1);

        std::fs::create_dir_all(&out_dir).unwrap();
        network_config.save(out_dir.join("config.json")).unwrap();

        let train_dataset = ConsFormerDataset::<ADBackend>::new(problems.clone(), &device);
        let valid_dataset = ConsFormerDataset::<NdArray>::new(problems.clone(), &device);
        let batcher = ConsFormerBatcher {
            mask_fraction: 0.5,
            destroy: None,
        };

        // `early_stopping_min_delta` set unreasonably high so the very first improvement (from
        // `best_score = INFINITY`) is the only one that will ever count -- every validation check
        // after the first must register as stagnant, so training should stop at
        // epoch `early_stopping_patience + 1`, well short of `num_epochs`.
        let training = TrainingConfig::new()
            .with_num_epochs(10)
            .with_validation_interval(1)
            .with_batch_size(1)
            .with_early_stopping_patience(Some(1))
            .with_early_stopping_min_delta(1000.0);

        train_model::<ADBackend, ConsFormerConfig, _, _, _, ConsFormerLoss, _, _>(
            network_config,
            &problems,
            train_dataset,
            valid_dataset,
            batcher,
            ConsFormerLoss,
            training,
            &out_dir,
            &device,
        )
        .unwrap();

        // The first validation check is the only improvement, so training stops after the second
        // one and the best model is the one saved at the root.
        assert!(out_dir.join("weights.mpk").is_file());
        let _ = load_network::<NdArray, ConsFormerConfig>(&out_dir, &problems, &device)
            .expect("the best checkpoint should be loadable directly");

        std::fs::remove_dir_all(&out_dir).ok();
    }

    /// A small ConsFormer problem set plus its checkpoint directory, with `config.json` written.
    fn setup(name: &str) -> (Vec<Arc<Problem>>, ConsFormerConfig, std::path::PathBuf) {
        let out_dir =
            std::env::temp_dir().join(format!("aicad_test_{name}_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&out_dir);
        let mut problem = Problem::default();
        let x = problem.add_variable(vec![0, 1, 2]);
        let y = problem.add_variable(vec![0, 1, 2]);
        not_equals(&mut problem, x, y);
        let network_config = ConsFormerConfig::new(3, 4, 4, 1, 4, 0, 0.5, 1.0).with_num_layers(1);
        std::fs::create_dir_all(&out_dir).unwrap();
        network_config.save(out_dir.join("config.json")).unwrap();
        (vec![Arc::new(problem)], network_config, out_dir)
    }

    fn train(
        problems: &[Arc<Problem>],
        network_config: ConsFormerConfig,
        training: TrainingConfig,
        out_dir: &Path,
    ) -> Result<<ConsFormerConfig as NetworkConfig<Autodiff<NdArray>>>::N, String> {
        type ADBackend = Autodiff<NdArray>;
        let device = NdArrayDevice::default();
        train_model::<ADBackend, ConsFormerConfig, _, _, _, ConsFormerLoss, _, _>(
            network_config,
            problems,
            ConsFormerDataset::<ADBackend>::new(problems.to_vec(), &device),
            ConsFormerDataset::<NdArray>::new(problems.to_vec(), &device),
            ConsFormerBatcher {
                mask_fraction: 0.5,
                destroy: None,
            },
            ConsFormerLoss,
            training,
            out_dir,
            &device,
        )
    }

    #[test]
    fn initial_weights_initialise_the_network_before_training() {
        use crate::neural::consformer::ConsFormerBatch;
        use burn::tensor::{Int, Tensor};

        let device = NdArrayDevice::default();
        let (problems, network_config, first_dir) = setup("initial_weights_first");
        let training = TrainingConfig::new()
            .with_num_epochs(2)
            .with_validation_interval(1)
            .with_batch_size(1);
        train(&problems, network_config.clone(), training, &first_dir).unwrap();

        // Zero epochs: the returned network is exactly the one the weights initialised.
        let (_, _, second_dir) = setup("initial_weights_second");
        let training = TrainingConfig::new()
            .with_num_epochs(0)
            .with_batch_size(1)
            .with_initial_weights(Some(first_dir.clone()));
        let resumed = train(&problems, network_config, training, &second_dir).unwrap();
        let (_, saved) =
            load_network::<Autodiff<NdArray>, ConsFormerConfig>(&first_dir, &problems, &device)
                .unwrap();

        let batch = ConsFormerBatch::<Autodiff<NdArray>>::for_assignments(
            &problems,
            Tensor::<Autodiff<NdArray>, 2, Int>::from_ints([[0, 1]], &device),
            Tensor::<Autodiff<NdArray>, 2, Int>::from_ints([[1, 1]], &device),
            &device,
        );
        let a = resumed.forward(&batch).into_data();
        let b = saved.forward(&batch).into_data();
        assert_eq!(a, b);

        std::fs::remove_dir_all(&first_dir).ok();
        std::fs::remove_dir_all(&second_dir).ok();
    }

    #[test]
    fn the_returned_network_is_the_best_one_saved_in_the_checkpoint() {
        use crate::neural::consformer::ConsFormerBatch;
        use burn::tensor::{Int, Tensor};

        let device = NdArrayDevice::default();
        let (problems, network_config, out_dir) = setup("returns_best");
        let training = TrainingConfig::new()
            .with_num_epochs(3)
            .with_validation_interval(1)
            .with_batch_size(1);
        let returned = train(&problems, network_config, training, &out_dir).unwrap();
        let (_, saved) =
            load_network::<Autodiff<NdArray>, ConsFormerConfig>(&out_dir, &problems, &device)
                .unwrap();

        let batch = ConsFormerBatch::<Autodiff<NdArray>>::for_assignments(
            &problems,
            Tensor::<Autodiff<NdArray>, 2, Int>::from_ints([[0, 1]], &device),
            Tensor::<Autodiff<NdArray>, 2, Int>::from_ints([[1, 1]], &device),
            &device,
        );
        assert_eq!(
            returned.forward(&batch).into_data(),
            saved.forward(&batch).into_data()
        );
        std::fs::remove_dir_all(&out_dir).ok();
    }

    #[test]
    fn missing_initial_weights_are_reported_with_their_path() {
        let (problems, network_config, out_dir) = setup("missing_initial_weights");
        let training = TrainingConfig::new()
            .with_num_epochs(10)
            .with_initial_weights(Some(out_dir.join("nowhere")));
        let error = train(&problems, network_config, training, &out_dir).unwrap_err();
        assert!(error.contains("nowhere"), "{error}");
        std::fs::remove_dir_all(&out_dir).ok();
    }

    #[test]
    #[should_panic(expected = "validation interval is 0")]
    fn panic_on_zero_validation_interval() {
        let (problems, network_config, out_dir) = setup("tmp");
        let training = TrainingConfig::new()
            .with_num_epochs(3)
            .with_validation_interval(0)
            .with_batch_size(1);
        std::fs::remove_dir_all(&out_dir).ok();
        train(&problems, network_config, training, &out_dir).unwrap();
    }

    #[test]
    #[should_panic(expected = "validation interval is 4")]
    fn panic_on_out_of_bound_validation_interval() {
        let (problems, network_config, out_dir) = setup("tmp");
        let training = TrainingConfig::new()
            .with_num_epochs(3)
            .with_validation_interval(4)
            .with_batch_size(1);
        std::fs::remove_dir_all(&out_dir).ok();
        train(&problems, network_config, training, &out_dir).unwrap();
    }
}