gprx 0.1.0

Gaussian process regression in Rust: exact GPR, sparse GPR (VFE), and SVGP.
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
//! `model.safetensors` layout: original `x` / `y`, optional column-major `l`.

use std::fs::File;
use std::path::Path;

use faer::MatRef;
use memmap2::Mmap;
use safetensors::tensor::{Dtype, TensorView};
use safetensors::{SafeTensors, serialize};

use crate::error::GprError;
use crate::error::PersistErrorKind;

use super::{TENSOR_FILE, persist_err};

pub(super) const TENSOR_X: &str = "x";
pub(super) const TENSOR_Y: &str = "y";
pub(super) const TENSOR_L: &str = "l";
pub(super) const TENSOR_ALPHA: &str = "alpha";

/// Memory map of `model.safetensors` kept so [`Self::l_view`] can borrow `L`.
pub(crate) struct MappedTensors {
    mmap: Mmap,
    l_offset: usize,
    n: usize,
}

/// `model.safetensors` opened once for a load: read into memory, or memory
/// mapped when its `f64` factor stays mapped ([`Self::into_mapped_l`]).
/// Every tensor of the load comes from this one read.
pub(super) struct TensorFile {
    source: TensorSource,
}

enum TensorSource {
    Read(Vec<u8>),
    Mapped(Mmap),
}

impl TensorFile {
    /// Reads the whole file into memory.
    pub(super) fn read(dir: &Path) -> Result<Self, GprError> {
        let path = dir.join(TENSOR_FILE);
        let bytes = std::fs::read(&path)
            .map_err(|err| persist_err(PersistErrorKind::Io, format!("read {path:?}: {err}")))?;
        Ok(Self {
            source: TensorSource::Read(bytes),
        })
    }

    /// Maps the file; only the pages a load touches are read.
    pub(super) fn map(dir: &Path) -> Result<Self, GprError> {
        let path = dir.join(TENSOR_FILE);
        let file = File::open(&path)
            .map_err(|err| persist_err(PersistErrorKind::Io, format!("open {path:?}: {err}")))?;
        // SAFETY: this map stays alive on `MappedTensors` and is not written
        // through. gprx never writes into an existing `model.safetensors`:
        // a save renames a new file over the path (`atomic::write_atomic`),
        // so this mapping keeps the old file. Another program that truncates
        // or rewrites the file in place is outside what gprx can guard.
        let mmap = unsafe { Mmap::map(&file) }
            .map_err(|err| persist_err(PersistErrorKind::Io, format!("mmap {path:?}: {err}")))?;
        Ok(Self {
            source: TensorSource::Mapped(mmap),
        })
    }

    fn bytes(&self) -> &[u8] {
        match &self.source {
            TensorSource::Read(bytes) => bytes,
            TensorSource::Mapped(mmap) => mmap,
        }
    }

    /// The parsed header over the file's bytes.
    pub(super) fn tensors(&self) -> Result<SafeTensors<'_>, GprError> {
        SafeTensors::deserialize(self.bytes()).map_err(|err| {
            persist_err(
                PersistErrorKind::Tensor,
                format!("safetensors header: {err}"),
            )
        })
    }

    /// Keeps the map so the model can borrow its `n × n` `f64` factor `l`.
    pub(super) fn into_mapped_l(self, n: usize) -> Result<MappedTensors, GprError> {
        match self.source {
            TensorSource::Mapped(mmap) => MappedTensors::from_mmap(mmap, n),
            TensorSource::Read(_) => Err(persist_err(
                PersistErrorKind::Tensor,
                "the factor was read, not mapped",
            )),
        }
    }
}

impl MappedTensors {
    fn from_mmap(mmap: Mmap, n: usize) -> Result<Self, GprError> {
        let tensors = SafeTensors::deserialize(&mmap).map_err(|err| {
            persist_err(
                PersistErrorKind::Tensor,
                format!("safetensors header: {err}"),
            )
        })?;
        let tensor = tensors.tensor(TENSOR_L).map_err(|err| {
            persist_err(
                PersistErrorKind::Tensor,
                format!("missing tensor {TENSOR_L}: {err}"),
            )
        })?;
        validate_f64_shape(&tensor, &[n, n], TENSOR_L)?;
        let data = tensor.data();
        if data.as_ptr() as usize % align_of::<f64>() != 0 {
            return Err(persist_err(
                PersistErrorKind::Tensor,
                format!("tensor {TENSOR_L} is not aligned for f64"),
            ));
        }
        let l_offset = offset_in_mmap(&mmap, data)?;
        let nbytes = n
            .checked_mul(n)
            .and_then(|cells| cells.checked_mul(size_of::<f64>()))
            .ok_or_else(|| persist_err(PersistErrorKind::Tensor, "L byte length overflowed"))?;
        if l_offset.checked_add(nbytes).is_none() || l_offset + nbytes > mmap.len() {
            return Err(persist_err(
                PersistErrorKind::Tensor,
                "L tensor is outside the mapped file",
            ));
        }
        let mapped = Self { mmap, l_offset, n };
        // SAFETY: the bounds and alignment are checked above.
        let values = unsafe { f64_slice_unchecked(&mapped.mmap[l_offset..l_offset + nbytes]) };
        require_finite_tensor(values, TENSOR_L)?;
        Ok(mapped)
    }

    pub(crate) fn l_view(&self) -> MatRef<'_, f64> {
        let nbytes = self.n * self.n * size_of::<f64>();
        let bytes = &self.mmap[self.l_offset..self.l_offset + nbytes];
        // SAFETY: `from_mmap` checked this range for length and alignment, and the
        // mapping stays live for `'self`.
        let data = unsafe { f64_slice_unchecked(bytes) };
        MatRef::from_column_major_slice(data, self.n, self.n)
    }
}

pub(super) struct FactorBytes<'a> {
    pub l_dtype: Dtype,
    pub l: &'a [u8],
    pub alpha_dtype: Dtype,
    pub alpha: &'a [u8],
}

pub(super) fn scalar_bytes<T>(values: &[T]) -> &[u8] {
    // SAFETY: any `T` slice is a valid byte slice of `size_of_val` bytes.
    unsafe {
        std::slice::from_raw_parts(values.as_ptr().cast::<u8>(), std::mem::size_of_val(values))
    }
}

pub(super) fn pack_lower<T: crate::kernel::KernelScalar>(l: MatRef<'_, T>, out: &mut [T]) {
    let n = l.nrows();
    debug_assert_eq!(l.ncols(), n);
    debug_assert_eq!(out.len(), n * n);
    let zero = T::from_f64(0.0);
    out.fill(zero);
    for col in 0..n {
        for row in col..n {
            out[col * n + row] = l[(row, col)];
        }
    }
}

pub(super) fn write_tensors(
    dir: &Path,
    x: &[f64],
    y: &[f64],
    n: usize,
    d: usize,
    factor: Option<FactorBytes<'_>>,
) -> Result<(), GprError> {
    if x.len() != n * d {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            format!("x has {} values, expected n*d = {}", x.len(), n * d),
        ));
    }
    if y.len() != n {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            format!("y has {} values, expected n = {n}", y.len()),
        ));
    }
    let x_bytes = f64_as_bytes(x);
    let y_bytes = f64_as_bytes(y);
    let x_view = TensorView::new(Dtype::F64, vec![n, d], x_bytes)
        .map_err(|err| persist_err(PersistErrorKind::Tensor, format!("x tensor: {err}")))?;
    let y_view = TensorView::new(Dtype::F64, vec![n], y_bytes)
        .map_err(|err| persist_err(PersistErrorKind::Tensor, format!("y tensor: {err}")))?;
    let bytes = if let Some(factor) = factor {
        let l_cells = match factor.l_dtype {
            Dtype::F32 => factor.l.len() / size_of::<f32>(),
            Dtype::F64 => factor.l.len() / size_of::<f64>(),
            other => {
                return Err(persist_err(
                    PersistErrorKind::Tensor,
                    format!("L dtype {other:?} is not f32 or f64"),
                ));
            }
        };
        let alpha_cells = match factor.alpha_dtype {
            Dtype::F32 => factor.alpha.len() / size_of::<f32>(),
            Dtype::F64 => factor.alpha.len() / size_of::<f64>(),
            other => {
                return Err(persist_err(
                    PersistErrorKind::Tensor,
                    format!("alpha dtype {other:?} is not f32 or f64"),
                ));
            }
        };
        if l_cells != n * n {
            return Err(persist_err(
                PersistErrorKind::Tensor,
                format!("L has {l_cells} values, expected n*n = {}", n * n),
            ));
        }
        if alpha_cells != n {
            return Err(persist_err(
                PersistErrorKind::Tensor,
                format!("alpha has {alpha_cells} values, expected n = {n}"),
            ));
        }
        let l_view = TensorView::new(factor.l_dtype, vec![n, n], factor.l)
            .map_err(|err| persist_err(PersistErrorKind::Tensor, format!("L tensor: {err}")))?;
        let alpha_view = TensorView::new(factor.alpha_dtype, vec![n], factor.alpha)
            .map_err(|err| persist_err(PersistErrorKind::Tensor, format!("alpha tensor: {err}")))?;
        serialize(
            [
                (TENSOR_X, x_view),
                (TENSOR_Y, y_view),
                (TENSOR_L, l_view),
                (TENSOR_ALPHA, alpha_view),
            ],
            None,
        )
    } else {
        serialize([(TENSOR_X, x_view), (TENSOR_Y, y_view)], None)
    }
    .map_err(|err| {
        persist_err(
            PersistErrorKind::Tensor,
            format!("serialize safetensors: {err}"),
        )
    })?;
    let path = dir.join(TENSOR_FILE);
    super::atomic::write_atomic(&path, &bytes)
}

pub(super) fn read_xy(
    tensors: &SafeTensors<'_>,
    n: usize,
    d: usize,
) -> Result<(Vec<f64>, Vec<f64>), GprError> {
    let x = copy_f64_tensor(tensors, TENSOR_X, &[n, d])?;
    let y = copy_f64_tensor(tensors, TENSOR_Y, &[n])?;
    Ok((x, y))
}

pub(super) fn read_scalars<T: crate::kernel::KernelScalar>(
    tensors: &SafeTensors<'_>,
    name: &str,
    shape: &[usize],
    dtype: Dtype,
) -> Result<Vec<T>, GprError> {
    let tensor = tensors.tensor(name).map_err(|err| {
        persist_err(
            PersistErrorKind::Tensor,
            format!("missing tensor {name}: {err}"),
        )
    })?;
    validate_shape(&tensor, shape, name, dtype)?;
    let data = scalar_slice::<T>(tensor.data())?;
    require_finite_tensor(data, name)?;
    Ok(data.to_vec())
}

pub(super) fn read_matrix<T: crate::kernel::KernelScalar>(
    tensors: &SafeTensors<'_>,
    n: usize,
    dtype: Dtype,
) -> Result<faer::Mat<T>, GprError> {
    let values = read_scalars::<T>(tensors, TENSOR_L, &[n, n], dtype)?;
    Ok(faer::Mat::from_fn(n, n, |row, col| values[col * n + row]))
}

fn copy_f64_tensor(
    tensors: &SafeTensors<'_>,
    name: &str,
    shape: &[usize],
) -> Result<Vec<f64>, GprError> {
    let tensor = tensors.tensor(name).map_err(|err| {
        persist_err(
            PersistErrorKind::Tensor,
            format!("missing tensor {name}: {err}"),
        )
    })?;
    validate_f64_shape(&tensor, shape, name)?;
    let data = f64_slice(tensor.data())?;
    require_finite_tensor(data, name)?;
    Ok(data.to_vec())
}

/// Rejects a stored tensor holding `NaN` or `±∞`. gprx never writes one, so
/// such a file was damaged or edited.
fn require_finite_tensor<T: crate::kernel::KernelScalar>(
    values: &[T],
    name: &str,
) -> Result<(), GprError> {
    match values.iter().position(|value| !value.to_f64().is_finite()) {
        None => Ok(()),
        Some(index) => Err(persist_err(
            PersistErrorKind::Tensor,
            format!("tensor {name} holds a non-finite value at index {index}"),
        )),
    }
}

fn validate_shape(
    tensor: &TensorView<'_>,
    shape: &[usize],
    name: &str,
    dtype: Dtype,
) -> Result<(), GprError> {
    if tensor.dtype() != dtype {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            format!(
                "tensor {name} dtype is {:?}, expected {dtype:?}",
                tensor.dtype()
            ),
        ));
    }
    if tensor.shape() != shape {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            format!(
                "tensor {name} shape is {:?}, expected {shape:?}",
                tensor.shape()
            ),
        ));
    }
    Ok(())
}

fn validate_f64_shape(
    tensor: &TensorView<'_>,
    shape: &[usize],
    name: &str,
) -> Result<(), GprError> {
    validate_shape(tensor, shape, name, Dtype::F64)?;
    Ok(())
}

fn offset_in_mmap(mmap: &Mmap, data: &[u8]) -> Result<usize, GprError> {
    let base = mmap.as_ptr() as usize;
    let ptr = data.as_ptr() as usize;
    if ptr < base {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            "tensor pointer is before the mapped file",
        ));
    }
    Ok(ptr - base)
}

fn f64_as_bytes(values: &[f64]) -> &[u8] {
    // SAFETY: an `f64` slice is a valid byte slice of `size_of_val` bytes.
    unsafe {
        std::slice::from_raw_parts(values.as_ptr().cast::<u8>(), std::mem::size_of_val(values))
    }
}

fn scalar_slice<T: Copy>(bytes: &[u8]) -> Result<&[T], GprError> {
    if bytes.as_ptr() as usize % align_of::<T>() != 0 {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            "tensor is not aligned",
        ));
    }
    if bytes.len() % size_of::<T>() != 0 {
        return Err(persist_err(
            PersistErrorKind::Tensor,
            "tensor length is not a multiple of the scalar size",
        ));
    }
    // SAFETY: alignment and the length multiple were checked above.
    Ok(unsafe {
        std::slice::from_raw_parts(bytes.as_ptr().cast::<T>(), bytes.len() / size_of::<T>())
    })
}

fn f64_slice(bytes: &[u8]) -> Result<&[f64], GprError> {
    scalar_slice::<f64>(bytes)
}

/// Reinterprets `bytes` as `f64` values.
///
/// # Safety
///
/// `bytes` must be aligned for `f64` and its length must be a multiple of
/// `size_of::<f64>()`. The caller keeps the underlying allocation alive for
/// the returned slice.
unsafe fn f64_slice_unchecked(bytes: &[u8]) -> &[f64] {
    // SAFETY: the caller upholds alignment, length, and lifetime.
    unsafe {
        std::slice::from_raw_parts(bytes.as_ptr().cast::<f64>(), bytes.len() / size_of::<f64>())
    }
}

/// Writes `model.safetensors` with the named `f64` tensors `(name, shape,
/// values)`, each column-major for a matrix.
pub(super) fn write_f64_tensors(
    dir: &Path,
    tensors: &[(&str, Vec<usize>, &[f64])],
) -> Result<(), GprError> {
    let mut views = Vec::with_capacity(tensors.len());
    for (name, shape, values) in tensors {
        let cells: usize = shape.iter().product();
        if values.len() != cells {
            return Err(persist_err(
                PersistErrorKind::Tensor,
                format!("{name} has {} values, expected {cells}", values.len()),
            ));
        }
        let view =
            TensorView::new(Dtype::F64, shape.clone(), f64_as_bytes(values)).map_err(|err| {
                persist_err(PersistErrorKind::Tensor, format!("{name} tensor: {err}"))
            })?;
        views.push((*name, view));
    }
    let bytes = serialize(views, None).map_err(|err| {
        persist_err(
            PersistErrorKind::Tensor,
            format!("serialize safetensors: {err}"),
        )
    })?;
    let path = dir.join(TENSOR_FILE);
    super::atomic::write_atomic(&path, &bytes)
}

/// Reads the `f64` tensor `name` of shape `shape` from `model.safetensors`.
pub(super) fn read_f64(
    tensors: &SafeTensors<'_>,
    name: &str,
    shape: &[usize],
) -> Result<Vec<f64>, GprError> {
    read_scalars::<f64>(tensors, name, shape, Dtype::F64)
}