STRA-338 size-fn: serving-policy: parse_embeddings header and positions finish within limits

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 18:12:06 +01:00
parent 142de61f2f
commit 953ba7df1c
+34 -26
View File
@@ -43,17 +43,7 @@ pub fn parse_embeddings(bytes: &[u8], n_embd: i64) -> Result<Embeddings, String>
let Some(header) = bytes.get(at..at + 20) else {
return Err(bad());
};
let word = |i: usize| i32::from_le_bytes(header[4 * i..4 * i + 4].try_into().expect("4 bytes"));
let (magic, n, nx, ny, width) = (word(0), word(1), word(2), word(3), word(4));
if magic != SVE1_MAGIC
|| n < 1
|| nx < 1
|| ny < 1
|| i64::from(nx) * i64::from(ny) != i64::from(n)
|| i64::from(width) != n_embd
{
return Err(bad());
}
let (n, nx, ny, width) = sve1_header(header, n_embd).ok_or_else(bad)?;
at += 20;
let count = usize::try_from(i64::from(n) * i64::from(width)).map_err(|_| bad())?;
let Some(data) = bytes.get(at..at + count * 4) else {
@@ -73,6 +63,19 @@ pub fn parse_embeddings(bytes: &[u8], n_embd: i64) -> Result<Embeddings, String>
Ok(out)
}
/// `(n, nx, ny, width)` of a valid strata-vision record header.
fn sve1_header(header: &[u8], n_embd: i64) -> Option<(i32, i32, i32, i32)> {
let word = |i: usize| i32::from_le_bytes(header[4 * i..4 * i + 4].try_into().expect("4 bytes"));
let (magic, n, nx, ny, width) = (word(0), word(1), word(2), word(3), word(4));
let valid = magic == SVE1_MAGIC
&& n >= 1
&& nx >= 1
&& ny >= 1
&& i64::from(nx) * i64::from(ny) == i64::from(n)
&& i64::from(width) == n_embd;
valid.then_some((n, nx, ny, width))
}
/// FNV-1a (`fnv1a` in generate.cpp), continuing from `h`.
pub fn fnv1a(data: &[u8], mut h: u64) -> u64 {
for byte in data {
@@ -137,21 +140,7 @@ pub fn build_positions(
while walk.i < n {
walk.step()?;
}
if walk.k != embeddings.images.len() {
return Err("the embeddings file has more images than the prompt".into());
}
if n > 0 && ids[(n - 1) as usize] == image_pad {
return Err("the prompt cannot end in an image".into());
}
let p = walk.p;
for c in n..cells as i64 {
put_position(&mut walk.table, c, p + (c - n), p + (c - n), p + (c - n))?;
}
Ok(Positions {
table: walk.table,
row_offset: walk.row_offset,
images: walk.keys,
})
walk.finish(cells)
}
/// The walk over the prompt: the cell `i`, the next text position `p` and the number of images seen `k`.
@@ -169,6 +158,25 @@ struct Walk<'a> {
}
impl Walk<'_> {
/// The end checks, then the positions of the cells past the prompt.
fn finish(mut self, cells: usize) -> Result<Positions, String> {
let (ids, n, p) = (self.ids, self.ids.len() as i64, self.p);
if self.k != self.embeddings.images.len() {
return Err("the embeddings file has more images than the prompt".into());
}
if n > 0 && ids[(n - 1) as usize] == self.image_pad {
return Err("the prompt cannot end in an image".into());
}
for c in n..cells as i64 {
put_position(&mut self.table, c, p + (c - n), p + (c - n), p + (c - n))?;
}
Ok(Positions {
table: self.table,
row_offset: self.row_offset,
images: self.keys,
})
}
/// Places the text token or the whole image at the current cell.
fn step(&mut self) -> Result<(), String> {
let (i, p) = (self.i, self.p);