overbrainer 0.10.0

Distill a large LLM into a smaller open-weights model: generate questions, collect answers, fine-tune, visualize.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Progress events published by pipeline stages and training runs. The core never
//! renders: the CLI turns events into log lines, the TUI (later) into views.

use std::fmt;
use std::sync::Arc;

use tokio::sync::broadcast;

use crate::exec::JobStatus;
use crate::llm::Usage;
use crate::pricing::Price;
use crate::runpod::PodStatus;
use crate::system::SystemSample;
use crate::train::{Mark, TrainMetric};

/// A pipeline stage.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Stage {
    /// Subtopics of each topic.
    Subtopics,
    /// Questions of each subtopic.
    Questions,
    /// Parent answers to each question.
    Answers,
    /// Train and eval split.
    Split,
}

impl Stage {
    /// Lowercase name, as used by the CLI command.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Subtopics => "subtopics",
            Self::Questions => "questions",
            Self::Answers => "answers",
            Self::Split => "split",
        }
    }
}

impl fmt::Display for Stage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// Counters of a stage run.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct StageStats {
    /// Items produced.
    pub done: usize,
    /// Items already present and left untouched.
    pub skipped: usize,
    /// Items that failed after retries.
    pub failed: usize,
    /// Items produced but marked as not usable for training.
    pub excluded: usize,
    /// Tokens used by every request of the stage, retries included.
    pub usage: Usage,
    /// Cost in USD, when the price of the model is known.
    pub cost: Option<f64>,
}

impl StageStats {
    /// Adds `usage` to the totals, and its cost when `price` is known.
    pub fn add_usage(&mut self, usage: Usage, price: Option<&Price>) {
        self.usage += usage;
        if let Some(price) = price {
            *self.cost.get_or_insert(0.0) += price.cost(usage);
        }
    }

    /// Adds the counts, tokens and cost of `other`.
    pub(crate) fn merge(&mut self, other: &Self) {
        self.done += other.done;
        self.skipped += other.skipped;
        self.failed += other.failed;
        self.excluded += other.excluded;
        self.usage += other.usage;
        if let Some(cost) = other.cost {
            *self.cost.get_or_insert(0.0) += cost;
        }
    }
}

/// Something that happened in a stage, a training run or its Runpod pod.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Event {
    /// A stage started with `total` items to process.
    StageStarted {
        /// The stage.
        stage: Stage,
        /// Items to process in this run.
        total: usize,
    },
    /// One item was produced.
    ItemDone {
        /// The stage.
        stage: Stage,
        /// ID of the item, or the topic name for the subtopics stage.
        id: String,
        /// Tokens used for this item, when known.
        usage: Option<Usage>,
        /// Cost of this item in USD, when the model price is known.
        cost: Option<f64>,
        /// True for an item produced but not usable for training (an excluded
        /// answer): counted as excluded, not done.
        excluded: bool,
    },
    /// One attempt or one item failed.
    ItemFailed {
        /// The stage.
        stage: Stage,
        /// ID of the item, or the topic name for the subtopics stage.
        id: String,
        /// Error message. Never contains secrets.
        error: String,
        /// True when the item will be tried again in this run.
        retryable: bool,
    },
    /// The model a stage asks, published right after its [`Event::StageStarted`];
    /// never for `split`.
    StageModel {
        /// The stage.
        stage: Stage,
        /// The model of the stage's role.
        model: String,
    },
    /// A stage finished.
    StageFinished {
        /// The stage.
        stage: Stage,
        /// Final counters.
        stats: StageStats,
    },
    /// The flow follows the job of run `run_id`: the [`Event::Metric`]s that come
    /// next on this bus are its own.
    RunWatched {
        /// ID of the run.
        run_id: String,
        /// The kind of job when it is not a training job (`run_id` is then
        /// the job's ID): an export or a compare.
        job: Option<crate::runs::JobKind>,
    },
    /// A training or evaluation log of the running job.
    Metric(TrainMetric),
    /// A stage, evaluation or end line of the running job: what it does
    /// besides training steps (see [`Phases`](crate::train::Phases)).
    Mark(Mark),
    /// The training job is in a new state.
    JobStatus(JobStatus),
    /// The Runpod pod of a run changed.
    PodStatus(PodStatus),
    /// What the machine running the job looks like now: disk, CPU, memory and
    /// GPUs. Like [`Event::Metric`], it belongs to the run of the last
    /// [`Event::RunWatched`] on this bus; one comes every
    /// [`PROBE_EVERY`](crate::runs::PROBE_EVERY) while the job is followed.
    System(SystemSample),
    /// Bytes of run `run_id` uploaded to the Hugging Face Hub so far.
    Push {
        /// ID of the run pushed.
        run_id: String,
        /// Bytes uploaded.
        done: u64,
        /// Bytes to upload.
        total: u64,
    },
    /// Questions of compare `compare_id` judged so far, of `total`.
    Judged {
        /// ID of the compare.
        compare_id: String,
        /// Questions with a verdict.
        done: u64,
        /// Questions to judge.
        total: u64,
    },
}

/// How an item ended, as its event says.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemResult {
    /// Produced: an [`Event::ItemDone`] with its usage.
    Done,
    /// Already present: an [`Event::ItemDone`] without usage.
    Skipped,
    /// Produced but not usable for training: an excluded [`Event::ItemDone`].
    Excluded,
    /// Failed for good: an [`Event::ItemFailed`] not retried.
    Failed,
}

impl ItemResult {
    /// Lowercase name, as the history counts it.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Done => "done",
            Self::Skipped => "skipped",
            Self::Excluded => "excluded",
            Self::Failed => "failed",
        }
    }
}

impl Event {
    /// The stage and result of the item this event ends; `None` for any other
    /// event, a failed attempt that is retried included.
    #[must_use]
    pub fn item_result(&self) -> Option<(Stage, ItemResult)> {
        match self {
            Self::ItemDone {
                stage,
                usage: Some(_),
                excluded: true,
                ..
            } => Some((*stage, ItemResult::Excluded)),
            Self::ItemDone {
                stage,
                usage: Some(_),
                ..
            } => Some((*stage, ItemResult::Done)),
            Self::ItemDone {
                stage, usage: None, ..
            } => Some((*stage, ItemResult::Skipped)),
            Self::ItemFailed {
                stage,
                retryable: false,
                ..
            } => Some((*stage, ItemResult::Failed)),
            _ => None,
        }
    }
}

/// Sees every event of the buses built with it, as they are published: nothing is
/// ever skipped, however many come at once.
///
/// A swap point: the events sit below everything that counts them, so they
/// cannot name the Prometheus metrics (`crate::metrics::Metrics`, the one
/// implementation), and tests put their own recorder in its place.
pub trait Observer: Send + Sync {
    /// `event` was published on bus `bus`, an ID unique among the open buses.
    fn event(&self, bus: usize, event: &Event);
    /// Every handle of bus `bus` was dropped: nothing more comes from it, and a
    /// later bus may get its ID.
    fn closed(&self, bus: usize);
}

/// Broadcast channel of [`Event`]s. Publishing never blocks and never fails: events
/// without subscribers are dropped, and a slow subscriber skips old events. An
/// [`Observer`] given at creation sees each event before the subscribers.
#[derive(Debug, Clone)]
pub struct EventBus {
    sender: broadcast::Sender<Event>,
    /// Shared by the clones: the last one dropped tells the observer.
    id: Arc<BusId>,
}

impl EventBus {
    /// Events kept for a subscriber that falls behind.
    pub const CAPACITY: usize = 1024;

    /// A bus with no subscriber yet, keeping [`Self::CAPACITY`] events.
    #[must_use]
    pub fn new() -> Self {
        Self::with_capacity(Self::CAPACITY)
    }

    /// A bus with no subscriber yet, keeping `capacity` events (at least 1) for a
    /// subscriber that falls behind.
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self::observed(capacity, None)
    }

    /// [`EventBus::with_capacity`], whose events `observer` also sees.
    #[must_use]
    pub fn observed(capacity: usize, observer: Option<Arc<dyn Observer>>) -> Self {
        let (sender, _) = broadcast::channel(capacity.max(1));
        Self {
            sender,
            id: Arc::new(BusId { observer }),
        }
    }

    /// Sends `event` to the observer, then to every current subscriber.
    pub fn publish(&self, event: Event) {
        if let Some(observer) = &self.id.observer {
            observer.event(self.id.key(), &event);
        }
        self.sender.send(event).ok();
    }

    /// Receives every event published from now on.
    #[must_use]
    pub fn subscribe(&self) -> broadcast::Receiver<Event> {
        self.sender.subscribe()
    }

    /// How many subscribers are listening.
    #[cfg(test)]
    pub(crate) fn receiver_count(&self) -> usize {
        self.sender.receiver_count()
    }
}

/// The identity of a bus and its clones: its address, while it lives.
struct BusId {
    observer: Option<Arc<dyn Observer>>,
}

impl BusId {
    fn key(&self) -> usize {
        std::ptr::from_ref(self).addr()
    }
}

impl fmt::Debug for BusId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BusId")
            .field("observed", &self.observer.is_some())
            .finish()
    }
}

impl Drop for BusId {
    fn drop(&mut self) {
        if let Some(observer) = &self.observer {
            observer.closed(self.key());
        }
    }
}

impl Default for EventBus {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[tokio::test]
    async fn subscribers_receive_published_events() -> Result<(), broadcast::error::RecvError> {
        let bus = EventBus::new();
        bus.publish(Event::StageStarted {
            stage: Stage::Split,
            total: 0,
        });
        let mut receiver = bus.subscribe();
        bus.publish(Event::StageStarted {
            stage: Stage::Answers,
            total: 3,
        });
        assert_eq!(
            receiver.recv().await?,
            Event::StageStarted {
                stage: Stage::Answers,
                total: 3
            }
        );
        Ok(())
    }

    /// What an observer saw: `Ok` events and `Err` ends, by bus.
    #[derive(Default)]
    struct Seen(std::sync::Mutex<Vec<Result<(usize, Event), usize>>>);

    impl Observer for Seen {
        fn event(&self, bus: usize, event: &Event) {
            if let Ok(mut seen) = self.0.lock() {
                seen.push(Ok((bus, event.clone())));
            }
        }

        fn closed(&self, bus: usize) {
            if let Ok(mut seen) = self.0.lock() {
                seen.push(Err(bus));
            }
        }
    }

    #[test]
    fn an_observer_sees_every_event_of_its_buses_and_their_end() -> Result<(), String> {
        let seen = Arc::new(Seen::default());
        let observer: Arc<dyn Observer> = seen.clone();
        let first = EventBus::observed(1, Some(Arc::clone(&observer)));
        let second = EventBus::observed(1, Some(observer));
        let clone = first.clone();
        let (a, b) = (first.id.key(), second.id.key());
        assert_ne!(a, b);
        let started = |total| Event::StageStarted {
            stage: Stage::Answers,
            total,
        };
        // Far more than the channel keeps, with no subscriber at all.
        for total in 0..3 {
            clone.publish(started(total));
        }
        second.publish(started(9));
        EventBus::new().publish(started(5));
        drop(first);
        drop(clone);
        let seen = seen.0.lock().map_err(|e| e.to_string())?;
        assert_eq!(
            *seen,
            [
                Ok((a, started(0))),
                Ok((a, started(1))),
                Ok((a, started(2))),
                Ok((b, started(9))),
                Err(a),
            ]
        );
        Ok(())
    }

    #[test]
    fn item_events_say_how_their_item_ended() {
        let done = |usage| Event::ItemDone {
            stage: Stage::Answers,
            id: "a".into(),
            usage,
            cost: None,
            excluded: false,
        };
        let failed = |retryable| Event::ItemFailed {
            stage: Stage::Questions,
            id: "q".into(),
            error: "no".into(),
            retryable,
        };
        assert_eq!(
            done(Some(Usage::default())).item_result(),
            Some((Stage::Answers, ItemResult::Done))
        );
        assert_eq!(
            done(None).item_result(),
            Some((Stage::Answers, ItemResult::Skipped))
        );
        assert_eq!(
            failed(false).item_result(),
            Some((Stage::Questions, ItemResult::Failed))
        );
        assert_eq!(
            failed(true).item_result(),
            None,
            "a retried attempt ends nothing"
        );
        assert_eq!(
            Event::StageStarted {
                stage: Stage::Split,
                total: 1
            }
            .item_result(),
            None
        );
        assert_eq!(
            [
                ItemResult::Done,
                ItemResult::Skipped,
                ItemResult::Excluded,
                ItemResult::Failed
            ]
            .map(ItemResult::name),
            ["done", "skipped", "excluded", "failed"]
        );
    }

    #[test]
    fn stage_serializes_as_its_name() -> Result<(), serde_json::Error> {
        for stage in [
            Stage::Subtopics,
            Stage::Questions,
            Stage::Answers,
            Stage::Split,
        ] {
            assert_eq!(
                serde_json::to_string(&stage)?,
                format!("\"{}\"", stage.name())
            );
            assert_eq!(
                serde_json::from_str::<Stage>(&format!("\"{}\"", stage.name()))?,
                stage
            );
        }
        Ok(())
    }

    #[test]
    fn cost_is_known_only_with_a_price() {
        let usage = Usage {
            input_tokens: 1_000_000,
            output_tokens: 0,
        };
        let mut stats = StageStats::default();
        stats.add_usage(usage, None);
        assert_eq!(stats.cost, None);
        let price = Price {
            input_per_million: 3.0,
            output_per_million: 15.0,
        };
        stats.add_usage(usage, Some(&price));
        assert_eq!(stats.usage.input_tokens, 2_000_000);
        assert!(stats.cost.is_some_and(|cost| (cost - 3.0).abs() < 1e-9));
    }

    #[test]
    fn merge_adds_counts_tokens_and_cost() {
        let mut total = StageStats {
            done: 1,
            cost: None,
            ..StageStats::default()
        };
        let other = StageStats {
            done: 2,
            skipped: 1,
            failed: 1,
            excluded: 1,
            usage: Usage {
                input_tokens: 3,
                output_tokens: 4,
            },
            cost: Some(0.5),
        };
        total.merge(&other);
        total.merge(&other);
        assert_eq!(
            (total.done, total.skipped, total.failed, total.excluded),
            (5, 2, 2, 2)
        );
        assert_eq!(
            total.usage,
            Usage {
                input_tokens: 6,
                output_tokens: 8
            }
        );
        assert_eq!(total.cost, Some(1.0));
    }

    #[tokio::test]
    async fn a_larger_bus_keeps_more_events_for_a_slow_subscriber()
    -> Result<(), broadcast::error::RecvError> {
        let small = EventBus::new();
        let large = EventBus::with_capacity(EventBus::CAPACITY * 2);
        let (mut behind, mut kept) = (small.subscribe(), large.subscribe());
        for total in 0..EventBus::CAPACITY * 2 {
            let event = Event::StageStarted {
                stage: Stage::Answers,
                total,
            };
            small.publish(event.clone());
            large.publish(event);
        }
        assert!(matches!(
            behind.recv().await,
            Err(broadcast::error::RecvError::Lagged(1024))
        ));
        assert_eq!(
            kept.recv().await?,
            Event::StageStarted {
                stage: Stage::Answers,
                total: 0
            }
        );
        Ok(())
    }
}