goosedump 0.12.43

Browse, search, compact, and learn from coding-agent sessions
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) Jarkko Sakkinen 2026

//! Bounded process-owned cache for routed expert weights.
//!
//! Experts are streamed into leased slots rather than paged from the GGUF
//! mapping. Concurrent requests for the same expert share one load; active
//! leases block LRU eviction.

use std::cell::Cell;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fs::File;
#[cfg(any(target_os = "android", target_os = "linux"))]
use std::fs::OpenOptions;
use std::io;
use std::ops::Range;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::slice;
#[cfg(any(target_os = "android", target_os = "linux"))]
use std::sync::OnceLock;
#[cfg(feature = "bench")]
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread::{self, JoinHandle};

use anyhow::{Context as _, Result, anyhow, ensure};
use memmap2::{MmapMut, MmapOptions};

use super::gguf::ByteRange;

const IO_WORKERS: usize = 2;
const EXPERT_RANGE_COUNT: usize = 6;
const SLOT_CHARGE_ALIGNMENT: usize = 64 * 1024;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(super) struct ExpertKey {
    layer: usize,
    expert: usize,
}

impl ExpertKey {
    pub(super) const fn new(layer: usize, expert: usize) -> Self {
        Self { layer, expert }
    }
}

struct ExpertData {
    bytes: MmapMut,
    parts: [Range<usize>; 6],
}

/// Read-only view of a loaded mapping held alive by a matching cache pin.
struct ExpertView {
    ptr: *const u8,
    len: usize,
    parts: [Range<usize>; 6],
}

// SAFETY: a view is created only after its cache entry is pinned. The mapping
// is immutable while ready and cannot be reclaimed until all pins are dropped.
unsafe impl Send for ExpertView {}
// SAFETY: ready mapping bytes are immutable, so concurrent shared reads are safe.
unsafe impl Sync for ExpertView {}

impl ExpertData {
    fn view(&self) -> ExpertView {
        ExpertView {
            ptr: self.bytes.as_ptr(),
            len: self.bytes.len(),
            parts: self.parts.clone(),
        }
    }
}

impl ExpertView {
    fn part(&self, index: usize) -> Result<&[u8]> {
        let range = self
            .parts
            .get(index)
            .ok_or_else(|| anyhow!("expert part {index} is out of range"))?;
        ensure!(
            range.start <= range.end && range.end <= self.len,
            "expert part range {}..{} exceeds mapping length {}",
            range.start,
            range.end,
            self.len
        );
        // SAFETY: the checked range lies within the pinned mapping. The pin
        // prevents eviction, unmapping, or mutation for the returned lifetime.
        Ok(unsafe { slice::from_raw_parts(self.ptr.add(range.start), range.len()) })
    }
}

pub(super) struct ExpertLease {
    key: ExpertKey,
    generation: u64,
    data: Option<ExpertView>,
    shared: Arc<Shared>,
}

impl ExpertLease {
    pub(super) fn part(&self, index: usize) -> Result<&[u8]> {
        self.data
            .as_ref()
            .ok_or_else(|| anyhow!("expert lease has been released"))?
            .part(index)
    }
}

impl Drop for ExpertLease {
    fn drop(&mut self) {
        let mut state = lock(&self.shared.state);
        self.data.take();
        release_pin(&mut state, self.key, self.generation);
        self.shared.changed.notify_all();
    }
}

pub(super) struct ExpertRequest {
    key: ExpertKey,
    generation: u64,
    active: bool,
    shared: Arc<Shared>,
}

impl ExpertRequest {
    pub(super) fn wait(mut self) -> Result<ExpertLease> {
        let data = loop {
            let mut state = lock(&self.shared.state);
            match state.entries.get(&self.key) {
                Some(CacheEntry::Ready {
                    data, generation, ..
                }) if *generation == self.generation => break data.view(),
                Some(CacheEntry::Loading { generation, .. })
                    if *generation == self.generation && !state.shutdown =>
                {
                    drop(wait(&self.shared.changed, state));
                }
                Some(CacheEntry::Loading { generation, .. }) if *generation == self.generation => {
                    release_pin(&mut state, self.key, self.generation);
                    self.active = false;
                    self.shared.changed.notify_all();
                    return Err(anyhow!("expert cache is shut down"));
                }
                Some(CacheEntry::Failed {
                    generation, error, ..
                }) if *generation == self.generation => {
                    let error = Arc::clone(error);
                    release_pin(&mut state, self.key, self.generation);
                    self.active = false;
                    self.shared.changed.notify_all();
                    return Err(anyhow!(error.to_string()));
                }
                Some(
                    CacheEntry::Ready { .. }
                    | CacheEntry::Loading { .. }
                    | CacheEntry::Failed { .. },
                ) => return Err(anyhow!("expert cache entry changed while requested")),
                None => return Err(anyhow!("loaded expert is missing from the cache")),
            }
        };
        let lease = ExpertLease {
            key: self.key,
            generation: self.generation,
            data: Some(data),
            shared: Arc::clone(&self.shared),
        };
        self.active = false;
        Ok(lease)
    }
}

impl Drop for ExpertRequest {
    fn drop(&mut self) {
        if !self.active {
            return;
        }
        let mut state = lock(&self.shared.state);
        release_pin(&mut state, self.key, self.generation);
        self.shared.changed.notify_all();
    }
}

enum CacheEntry {
    Loading {
        generation: u64,
        pins: Cell<usize>,
    },
    Ready {
        generation: u64,
        data: ExpertData,
        last_used: Cell<u64>,
        pins: Cell<usize>,
    },
    Failed {
        generation: u64,
        error: Arc<str>,
        pins: Cell<usize>,
    },
}

struct LoadJob {
    key: ExpertKey,
    generation: u64,
    ranges: [ByteRange; 6],
    buffer: MmapMut,
}

struct StoreState {
    entries: HashMap<ExpertKey, CacheEntry>,
    queue: VecDeque<LoadJob>,
    free_buffers: Vec<MmapMut>,
    allocated_slots: usize,
    max_slots: usize,
    slot_bytes: usize,
    clock: u64,
    next_generation: u64,
    shutdown: bool,
}

struct Shared {
    state: Mutex<StoreState>,
    work_available: Condvar,
    changed: Condvar,
    #[cfg(feature = "bench")]
    queued_loads: AtomicUsize,
    #[cfg(feature = "bench")]
    slot_waits: AtomicUsize,
}

struct DirectFile {
    file: File,
    alignment: usize,
    file_len: u64,
}

enum DirectLoad {
    Complete([Range<usize>; 6]),
    Buffered,
    Unavailable,
}

struct ExpertSource {
    buffered: File,
    direct: Option<DirectFile>,
    direct_enabled: AtomicBool,
}

impl ExpertSource {
    fn new(buffered: File) -> Self {
        let direct = open_direct_file(&buffered);
        let direct_enabled = AtomicBool::new(direct.is_some());
        Self {
            buffered,
            direct,
            direct_enabled,
        }
    }

    fn slot_capacity(&self, data_bytes: usize) -> Result<usize> {
        let Some(direct) = &self.direct else {
            return Ok(data_bytes);
        };
        let padding = direct
            .alignment
            .checked_mul(2 * EXPERT_RANGE_COUNT)
            .context("expert direct-I/O padding overflow")?;
        data_bytes
            .checked_add(padding)
            .context("expert cache slot size overflow")
    }
}

pub(super) struct ExpertStore {
    shared: Arc<Shared>,
    request_gate: Mutex<()>,
    workers: Vec<JoinHandle<()>>,
}

impl ExpertStore {
    pub(super) fn new(
        file: File,
        slot_bytes: usize,
        byte_budget: usize,
        minimum_slots: usize,
        maximum_slots: usize,
    ) -> Result<Self> {
        ensure!(slot_bytes != 0, "expert cache slot must not be empty");
        ensure!(
            cfg!(any(unix, windows)),
            "expert cache requires positional file reads"
        );
        ensure!(
            minimum_slots != 0,
            "expert cache requires at least one slot"
        );
        ensure!(
            minimum_slots <= maximum_slots,
            "expert cache slot bounds are invalid"
        );
        let source = Arc::new(ExpertSource::new(file));
        let slot_bytes = source.slot_capacity(slot_bytes)?;
        let slot_charge = align_up(slot_bytes, SLOT_CHARGE_ALIGNMENT)?;
        let max_slots = (byte_budget / slot_charge).min(maximum_slots);
        ensure!(
            max_slots >= minimum_slots,
            "expert cache budget provides {max_slots} slots, but inference requires at least {minimum_slots}"
        );
        let shared = Arc::new(Shared {
            state: Mutex::new(StoreState {
                entries: HashMap::new(),
                queue: VecDeque::new(),
                free_buffers: Vec::new(),
                allocated_slots: 0,
                max_slots,
                slot_bytes,
                clock: 0,
                next_generation: 1,
                shutdown: false,
            }),
            work_available: Condvar::new(),
            changed: Condvar::new(),
            #[cfg(feature = "bench")]
            queued_loads: AtomicUsize::new(0),
            #[cfg(feature = "bench")]
            slot_waits: AtomicUsize::new(0),
        });
        let mut store = Self {
            shared,
            request_gate: Mutex::new(()),
            workers: Vec::new(),
        };
        for index in 0..IO_WORKERS.min(max_slots) {
            let source = Arc::clone(&source);
            let shared = Arc::clone(&store.shared);
            let worker = thread::Builder::new()
                .name(format!("expert-io-{index}"))
                .spawn(move || worker_loop(&shared, &source))
                .context("start expert I/O worker")?;
            store.workers.push(worker);
        }
        Ok(store)
    }

    #[cfg(feature = "bench")]
    pub(super) fn queued_load_count(&self) -> usize {
        self.shared.queued_loads.load(Ordering::Relaxed)
    }

    #[cfg(feature = "bench")]
    pub(super) fn slot_wait_count(&self) -> usize {
        self.shared.slot_waits.load(Ordering::Relaxed)
    }

    pub(super) fn request_many(
        &self,
        requests: impl IntoIterator<Item = (ExpertKey, [ByteRange; 6])>,
    ) -> Result<Vec<ExpertRequest>> {
        let requests = requests.into_iter().collect::<Vec<_>>();
        // Serialize multi-key admission so concurrent batches cannot each pin
        // a partial slot set while waiting for the other batch to release it.
        let _request_guard = lock(&self.request_gate);
        let unique_keys = requests.iter().map(|(key, _)| *key).collect::<HashSet<_>>();
        let state = lock(&self.shared.state);
        ensure!(!state.shutdown, "expert cache is shut down");
        ensure!(
            unique_keys.len() <= state.max_slots,
            "expert request needs {} slots, but the cache has {}",
            unique_keys.len(),
            state.max_slots
        );
        drop(state);
        requests
            .into_iter()
            .map(|(key, ranges)| self.request(key, ranges))
            .collect()
    }

    fn request(&self, key: ExpertKey, ranges: [ByteRange; 6]) -> Result<ExpertRequest> {
        loop {
            let mut state = lock(&self.shared.state);
            ensure!(!state.shutdown, "expert cache is shut down");
            state.clock = state.clock.saturating_add(1);
            let last_used = state.clock;
            if let Some(entry) = state.entries.get(&key) {
                let generation = match entry {
                    CacheEntry::Loading { generation, pins }
                    | CacheEntry::Failed {
                        generation, pins, ..
                    } => {
                        pins.set(
                            pins.get()
                                .checked_add(1)
                                .context("expert pin count overflow")?,
                        );
                        *generation
                    }
                    CacheEntry::Ready {
                        generation,
                        last_used: entry_last_used,
                        pins,
                        ..
                    } => {
                        pins.set(
                            pins.get()
                                .checked_add(1)
                                .context("expert pin count overflow")?,
                        );
                        entry_last_used.set(last_used);
                        *generation
                    }
                };
                return Ok(ExpertRequest {
                    key,
                    generation,
                    active: true,
                    shared: Arc::clone(&self.shared),
                });
            }

            let next_generation = state
                .next_generation
                .checked_add(1)
                .context("expert cache generation overflow")?;
            if let Some(buffer) = take_buffer(&mut state)? {
                let generation = state.next_generation;
                state.next_generation = next_generation;
                state.entries.insert(
                    key,
                    CacheEntry::Loading {
                        generation,
                        pins: Cell::new(1),
                    },
                );
                state.queue.push_back(LoadJob {
                    key,
                    generation,
                    ranges,
                    buffer,
                });
                #[cfg(feature = "bench")]
                self.shared.queued_loads.fetch_add(1, Ordering::Relaxed);
                self.shared.work_available.notify_one();
                return Ok(ExpertRequest {
                    key,
                    generation,
                    active: true,
                    shared: Arc::clone(&self.shared),
                });
            }

            #[cfg(feature = "bench")]
            self.shared.slot_waits.fetch_add(1, Ordering::Relaxed);
            drop(wait(&self.shared.changed, state));
        }
    }
}

impl Drop for ExpertStore {
    fn drop(&mut self) {
        {
            let mut state = lock(&self.shared.state);
            state.shutdown = true;
            self.shared.work_available.notify_all();
            self.shared.changed.notify_all();
        }
        for worker in self.workers.drain(..) {
            let _result = worker.join();
        }
        fail_incomplete_loads(&self.shared);
    }
}

fn release_pin(state: &mut StoreState, key: ExpertKey, generation: u64) {
    let Some(entry) = state.entries.get(&key) else {
        return;
    };
    let remove = match entry {
        CacheEntry::Loading {
            generation: entry_generation,
            pins,
        }
        | CacheEntry::Ready {
            generation: entry_generation,
            pins,
            ..
        } => {
            if *entry_generation == generation {
                pins.set(pins.get().saturating_sub(1));
            }
            false
        }
        CacheEntry::Failed {
            generation: entry_generation,
            pins,
            ..
        } => {
            if *entry_generation == generation {
                pins.set(pins.get().saturating_sub(1));
                pins.get() == 0
            } else {
                false
            }
        }
    };
    if remove {
        state.entries.remove(&key);
    }
}

fn take_buffer(state: &mut StoreState) -> Result<Option<MmapMut>> {
    if let Some(buffer) = state.free_buffers.pop() {
        return Ok(Some(buffer));
    }
    if state.allocated_slots < state.max_slots {
        let buffer = MmapOptions::new()
            .len(state.slot_bytes)
            .map_anon()
            .context("allocate expert cache slot")?;
        state.allocated_slots += 1;
        return Ok(Some(buffer));
    }

    let victim = state
        .entries
        .iter()
        .filter_map(|(key, entry)| match entry {
            CacheEntry::Ready {
                last_used, pins, ..
            } if pins.get() == 0 => Some((*key, last_used.get())),
            CacheEntry::Loading { .. } | CacheEntry::Ready { .. } | CacheEntry::Failed { .. } => {
                None
            }
        })
        .min_by_key(|(_, last_used)| *last_used)
        .map(|(key, _)| key);
    let Some(victim) = victim else {
        return Ok(None);
    };
    let Some(CacheEntry::Ready { data, .. }) = state.entries.remove(&victim) else {
        return Ok(None);
    };
    Ok(Some(data.bytes))
}

fn worker_loop(shared: &Shared, source: &ExpertSource) {
    loop {
        let job = {
            let mut state = lock(&shared.state);
            while state.queue.is_empty() && !state.shutdown {
                state = wait(&shared.work_available, state);
            }
            match state.queue.pop_front() {
                Some(job) => Some(job),
                None if state.shutdown => None,
                None => continue,
            }
        };
        let Some(job) = job else {
            return;
        };
        complete_job(shared, source, job);
    }
}

fn complete_job(shared: &Shared, source: &ExpertSource, mut job: LoadJob) {
    let loaded = catch_unwind(AssertUnwindSafe(|| {
        load_ranges(source, &job.ranges, &mut job.buffer)
    }));
    let result = match loaded {
        Ok(result) => result
            .with_context(|| format!("load layer {} expert {}", job.key.layer, job.key.expert)),
        Err(_) => Err(anyhow!(
            "expert I/O worker panicked while loading layer {} expert {}",
            job.key.layer,
            job.key.expert
        )),
    };
    match result {
        Ok(parts) => finish_load(shared, job, parts),
        Err(error) => fail_load(shared, job, Arc::from(error.to_string())),
    }
}

fn finish_load(shared: &Shared, job: LoadJob, parts: [Range<usize>; 6]) {
    let mut state = lock(&shared.state);
    let pins = match state.entries.get(&job.key) {
        Some(CacheEntry::Loading { generation, pins }) if *generation == job.generation => {
            pins.get()
        }
        Some(CacheEntry::Loading { .. } | CacheEntry::Ready { .. } | CacheEntry::Failed { .. })
        | None => {
            state.free_buffers.push(job.buffer);
            shared.changed.notify_all();
            return;
        }
    };
    state.entries.remove(&job.key);
    let data = ExpertData {
        bytes: job.buffer,
        parts,
    };
    state.clock = state.clock.saturating_add(1);
    let last_used = state.clock;
    state.entries.insert(
        job.key,
        CacheEntry::Ready {
            generation: job.generation,
            data,
            last_used: Cell::new(last_used),
            pins: Cell::new(pins),
        },
    );
    shared.changed.notify_all();
}

fn fail_load(shared: &Shared, job: LoadJob, error: Arc<str>) {
    let mut state = lock(&shared.state);
    let pins = match state.entries.get(&job.key) {
        Some(CacheEntry::Loading { generation, pins }) if *generation == job.generation => {
            Some(pins.get())
        }
        Some(CacheEntry::Loading { .. } | CacheEntry::Ready { .. } | CacheEntry::Failed { .. })
        | None => None,
    };
    state.free_buffers.push(job.buffer);
    if let Some(pins) = pins {
        state.entries.remove(&job.key);
        if pins != 0 {
            state.entries.insert(
                job.key,
                CacheEntry::Failed {
                    generation: job.generation,
                    error,
                    pins: Cell::new(pins),
                },
            );
        }
    }
    shared.changed.notify_all();
}

fn fail_incomplete_loads(shared: &Shared) {
    let mut state = lock(&shared.state);
    while let Some(job) = state.queue.pop_front() {
        state.free_buffers.push(job.buffer);
    }
    let error = Arc::<str>::from("expert cache shut down");
    let loading = state
        .entries
        .iter()
        .filter_map(|(key, entry)| match entry {
            CacheEntry::Loading { .. } => Some(*key),
            CacheEntry::Ready { .. } | CacheEntry::Failed { .. } => None,
        })
        .collect::<Vec<_>>();
    for key in loading {
        let Some(CacheEntry::Loading { generation, pins }) = state.entries.remove(&key) else {
            continue;
        };
        state.entries.insert(
            key,
            CacheEntry::Failed {
                generation,
                error: Arc::clone(&error),
                pins,
            },
        );
    }
    shared.changed.notify_all();
}

fn load_ranges(
    source: &ExpertSource,
    ranges: &[ByteRange; 6],
    buffer: &mut MmapMut,
) -> Result<[Range<usize>; 6]> {
    if source.direct_enabled.load(Ordering::Relaxed)
        && let Some(direct) = &source.direct
    {
        match load_ranges_direct(direct, ranges, buffer)? {
            DirectLoad::Complete(parts) => return Ok(parts),
            DirectLoad::Buffered => {}
            DirectLoad::Unavailable => {
                source.direct_enabled.store(false, Ordering::Relaxed);
            }
        }
    }
    load_ranges_buffered(&source.buffered, ranges, buffer)
}

fn load_ranges_direct(
    direct: &DirectFile,
    ranges: &[ByteRange; 6],
    buffer: &mut MmapMut,
) -> Result<DirectLoad> {
    if !(buffer.as_ptr() as usize).is_multiple_of(direct.alignment) {
        return Ok(DirectLoad::Unavailable);
    }
    let mut parts = std::array::from_fn(|_| 0..0);
    let mut offset = 0usize;
    for (index, range) in ranges.iter().copied().enumerate() {
        let file_start = range.start() / direct.alignment * direct.alignment;
        let prefix = range.start() - file_start;
        let required = prefix
            .checked_add(range.len())
            .context("expert direct-I/O range overflow")?;
        let read_len = align_up(required, direct.alignment)?;
        let end = offset
            .checked_add(read_len)
            .context("expert direct-I/O slot offset overflow")?;
        ensure!(end <= buffer.len(), "expert data exceeds its cache slot");
        let file_start = u64::try_from(file_start).context("expert file offset exceeds u64")?;
        let file_end = file_start
            .checked_add(u64::try_from(read_len).context("expert read length exceeds u64")?)
            .context("expert direct-I/O file range overflow")?;
        if file_end > direct.file_len {
            return Ok(DirectLoad::Buffered);
        }
        if let Err(error) = read_exact_at(&direct.file, &mut buffer[offset..end], file_start) {
            if direct_io_unavailable(&error) {
                return Ok(DirectLoad::Unavailable);
            }
            return Err(error).context("read expert range with O_DIRECT");
        }
        let part_start = offset
            .checked_add(prefix)
            .context("expert cache part offset overflow")?;
        let part_end = part_start
            .checked_add(range.len())
            .context("expert cache part offset overflow")?;
        ensure!(
            part_start.is_multiple_of(std::mem::align_of::<f32>()),
            "expert cache part {index} is not F32-aligned"
        );
        parts[index] = part_start..part_end;
        offset = end;
    }
    Ok(DirectLoad::Complete(parts))
}

fn load_ranges_buffered(
    file: &File,
    ranges: &[ByteRange; 6],
    buffer: &mut MmapMut,
) -> Result<[Range<usize>; 6]> {
    let mut parts = std::array::from_fn(|_| 0..0);
    let mut offset = 0usize;
    for (index, range) in ranges.iter().copied().enumerate() {
        ensure!(
            offset.is_multiple_of(std::mem::align_of::<f32>()),
            "expert cache part {index} is not F32-aligned"
        );
        let end = offset
            .checked_add(range.len())
            .context("expert cache part offset overflow")?;
        ensure!(end <= buffer.len(), "expert data exceeds its cache slot");
        read_exact_at(
            file,
            &mut buffer[offset..end],
            u64::try_from(range.start()).context("expert file offset exceeds u64")?,
        )?;
        discard_file_pages(file, range);
        parts[index] = offset..end;
        offset = end;
    }
    Ok(parts)
}

#[cfg(any(target_os = "android", target_os = "linux"))]
fn direct_io_unavailable(error: &io::Error) -> bool {
    matches!(
        error.raw_os_error(),
        Some(libc::EINVAL | libc::EOPNOTSUPP | libc::ENOSYS)
    ) || error.kind() == io::ErrorKind::InvalidInput
}

#[cfg(not(any(target_os = "android", target_os = "linux")))]
fn direct_io_unavailable(_error: &io::Error) -> bool {
    false
}

#[cfg(any(target_os = "android", target_os = "linux"))]
fn open_direct_file(source: &File) -> Option<DirectFile> {
    use std::os::fd::AsRawFd as _;
    use std::os::unix::fs::OpenOptionsExt as _;

    let alignment = system_page_size()?;
    let path = format!("/proc/self/fd/{}", source.as_raw_fd());
    let file = OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_DIRECT)
        .open(path)
        .ok()?;
    let file_len = file.metadata().ok()?.len();
    Some(DirectFile {
        file,
        alignment,
        file_len,
    })
}

#[cfg(not(any(target_os = "android", target_os = "linux")))]
fn open_direct_file(_source: &File) -> Option<DirectFile> {
    None
}

#[cfg(unix)]
fn read_exact_at(file: &File, buffer: &mut [u8], offset: u64) -> io::Result<()> {
    use std::os::unix::fs::FileExt as _;

    file.read_exact_at(buffer, offset)
}

#[cfg(windows)]
fn read_exact_at(file: &File, mut buffer: &mut [u8], mut offset: u64) -> io::Result<()> {
    use std::os::windows::fs::FileExt as _;

    while !buffer.is_empty() {
        let read = file.seek_read(buffer, offset)?;
        if read == 0 {
            return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
        }
        offset = offset
            .checked_add(read as u64)
            .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?;
        buffer = &mut buffer[read..];
    }
    Ok(())
}

#[cfg(not(any(unix, windows)))]
fn read_exact_at(_file: &File, _buffer: &mut [u8], _offset: u64) -> io::Result<()> {
    Err(io::Error::from(io::ErrorKind::Unsupported))
}

#[cfg(any(target_os = "android", target_os = "linux"))]
fn discard_file_pages(file: &File, range: ByteRange) {
    use std::os::fd::AsRawFd as _;

    let Some(page_size) = system_page_size() else {
        return;
    };
    let Some(start) = range
        .start()
        .checked_add(page_size - 1)
        .map(|start| start / page_size * page_size)
    else {
        return;
    };
    let end = range.end() / page_size * page_size;
    if start >= end {
        return;
    }
    let (Ok(offset), Ok(length)) = (
        libc::off_t::try_from(start),
        libc::off_t::try_from(end - start),
    ) else {
        return;
    };
    // Only complete source pages are discarded, so advice cannot evict a
    // neighboring tensor which shares a boundary page.
    // SAFETY: posix_fadvise does not access Rust memory, and the descriptor
    // remains open for the duration of the call.
    let _result =
        unsafe { libc::posix_fadvise(file.as_raw_fd(), offset, length, libc::POSIX_FADV_DONTNEED) };
}

#[cfg(any(target_os = "android", target_os = "linux"))]
fn system_page_size() -> Option<usize> {
    static PAGE_SIZE: OnceLock<Option<usize>> = OnceLock::new();

    *PAGE_SIZE.get_or_init(|| {
        // SAFETY: sysconf reads process configuration and has no pointer arguments.
        let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
        usize::try_from(page_size)
            .ok()
            .filter(|size| size.is_power_of_two())
    })
}

#[cfg(not(any(target_os = "android", target_os = "linux")))]
fn discard_file_pages(_file: &File, _range: ByteRange) {}

fn align_up(value: usize, alignment: usize) -> Result<usize> {
    let remainder = value % alignment;
    if remainder == 0 {
        return Ok(value);
    }
    value
        .checked_add(alignment - remainder)
        .context("expert cache slot size overflow")
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    match mutex.lock() {
        Ok(guard) => guard,
        Err(poisoned) => poisoned.into_inner(),
    }
}

fn wait<'a, T>(condition: &Condvar, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
    match condition.wait(guard) {
        Ok(guard) => guard,
        Err(poisoned) => poisoned.into_inner(),
    }
}