use oa::data::{Dataset, Sample};
use oa::matrix::{self, Matrix};
use oa::{Engine, Error, Result};
use crate::core::decode::{Decoded, MatrixData};
use crate::core::pack::{self, FetchProgress};
#[path = "squad_v2.gen.rs"]
mod generated;
pub(crate) use generated::SQUAD_V2_PACK;
pub use generated::{SquadV2Config, SquadV2Split};
fn member(split: SquadV2Split) -> &'static str {
match split {
SquadV2Split::Train => "train-v2.0.json",
SquadV2Split::Validation => "dev-v2.0.json",
}
}
struct Qa {
question: String,
answer: String,
answer_start: f32,
impossible: f32,
}
pub struct SquadV2 {
question_bytes: Matrix,
question_offsets: Matrix,
question_lengths: Matrix,
context_bytes: Matrix,
context_offsets: Matrix,
context_lengths: Matrix,
context_index: Matrix,
answer_bytes: Matrix,
answer_offsets: Matrix,
answer_lengths: Matrix,
answer_start: Matrix,
is_impossible: Matrix,
question_span: Vec<(u32, u32)>,
context_span: Vec<(u32, u32)>,
context_lookup: Vec<u32>,
split: SquadV2Split,
}
impl SquadV2 {
pub fn decode(config: &SquadV2Config) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &SquadV2Config,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Decoded> {
let root = match &config.cache_dir {
Some(dir) => dir.clone(),
None => pack::data_root()?,
};
let dir = pack::ensure_pack_with_progress(&SQUAD_V2_PACK, &root, progress)?;
let body = std::fs::read(dir.join(member(config.split)))
.map_err(|error| Error::io("read squad_v2 member", error))?;
let (qas, contexts) = flatten(&body)?;
Ok(decoded(qas, contexts))
}
pub fn load_with_progress_on(
engine: &Engine,
config: &SquadV2Config,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let decoded = Self::decode_with_progress(config, progress)?;
let spans = |name: &str| -> Vec<(u32, u32)> {
let (starts, lens) = (format!("{name}_offsets"), format!("{name}_lengths"));
let find = |key: &str| -> Vec<u32> {
decoded
.matrices
.iter()
.find(|m| m.name == key)
.map(|m| m.values.iter().map(|&v| v as u32).collect())
.unwrap_or_default()
};
find(&starts).into_iter().zip(find(&lens)).collect()
};
let context_lookup = decoded
.matrices
.iter()
.find(|m| m.name == "context_index")
.map(|m| m.values.iter().map(|&v| v as u32).collect::<Vec<_>>())
.unwrap_or_default();
Ok(Self {
question_span: spans("question"),
context_span: spans("context"),
context_lookup,
question_bytes: decoded.upload("question_bytes", engine)?,
question_offsets: decoded.upload("question_offsets", engine)?,
question_lengths: decoded.upload("question_lengths", engine)?,
context_bytes: decoded.upload("context_bytes", engine)?,
context_offsets: decoded.upload("context_offsets", engine)?,
context_lengths: decoded.upload("context_lengths", engine)?,
context_index: decoded.upload("context_index", engine)?,
answer_bytes: decoded.upload("answer_bytes", engine)?,
answer_offsets: decoded.upload("answer_offsets", engine)?,
answer_lengths: decoded.upload("answer_lengths", engine)?,
answer_start: decoded.upload("answer_start", engine)?,
is_impossible: decoded.upload("is_impossible", engine)?,
split: config.split,
})
}
#[must_use]
pub const fn question_bytes(&self) -> &Matrix {
&self.question_bytes
}
#[must_use]
pub const fn question_offsets(&self) -> &Matrix {
&self.question_offsets
}
#[must_use]
pub const fn question_lengths(&self) -> &Matrix {
&self.question_lengths
}
#[must_use]
pub const fn context_bytes(&self) -> &Matrix {
&self.context_bytes
}
#[must_use]
pub const fn context_offsets(&self) -> &Matrix {
&self.context_offsets
}
#[must_use]
pub const fn context_lengths(&self) -> &Matrix {
&self.context_lengths
}
#[must_use]
pub const fn context_index(&self) -> &Matrix {
&self.context_index
}
#[must_use]
pub const fn answer_bytes(&self) -> &Matrix {
&self.answer_bytes
}
#[must_use]
pub const fn answer_offsets(&self) -> &Matrix {
&self.answer_offsets
}
#[must_use]
pub const fn answer_lengths(&self) -> &Matrix {
&self.answer_lengths
}
#[must_use]
pub const fn answer_start(&self) -> &Matrix {
&self.answer_start
}
#[must_use]
pub const fn is_impossible(&self) -> &Matrix {
&self.is_impossible
}
#[must_use]
pub const fn split(&self) -> SquadV2Split {
self.split
}
}
impl Dataset for SquadV2 {
fn len(&self) -> i64 {
i64::from(self.question_offsets.shape()[0] as i32)
}
fn get_item(&self, index: i64) -> Result<Matrix> {
let i = usize::try_from(index)
.map_err(|_| Error::invalid_argument("squad_v2 index must be nonnegative"))?;
let (qs, ql) = *self
.question_span
.get(i)
.ok_or_else(|| Error::invalid_argument("squad_v2 index is out of bounds"))?;
let row = *self
.context_lookup
.get(i)
.ok_or_else(|| Error::invalid_argument("squad_v2 index is out of bounds"))?;
let (cs, cl) = self.context_span[row as usize];
let q = matrix::slice(&self.question_bytes, 0, i64::from(qs), i64::from(qs + ql))?;
let c = matrix::slice(&self.context_bytes, 0, i64::from(cs), i64::from(cs + cl))?;
matrix::concat(&[q, c], 0)
}
fn get_sample(&self, index: i64) -> Result<Sample> {
let x = self.get_item(index)?;
let y = matrix::slice(&self.answer_start, 0, index, index + 1)?;
Ok(Sample::new_with_label(x, y))
}
}
struct QaRow {
qa: Qa,
context_index: u32,
}
fn flatten(body: &[u8]) -> Result<(Vec<QaRow>, Vec<String>)> {
let root: serde_json::Value = serde_json::from_slice(body)
.map_err(|error| Error::data_loss(format!("squad_v2 member is not valid JSON: {error}")))?;
let data = root["data"]
.as_array()
.ok_or_else(|| Error::data_loss("squad_v2 lacks data[]"))?;
let mut qas: Vec<QaRow> = Vec::new();
let mut contexts: Vec<String> = Vec::new();
let mut context_ids: std::collections::HashMap<&str, u32> = std::collections::HashMap::new();
for article in data {
let paragraphs = article["paragraphs"]
.as_array()
.ok_or_else(|| Error::data_loss("squad_v2 article lacks paragraphs[]"))?;
for paragraph in paragraphs {
let context = paragraph["context"].as_str().unwrap_or("");
let next = u32::try_from(contexts.len())
.map_err(|_| Error::data_loss("squad_v2 context table overflow"))?;
let context_index = *context_ids.entry(context).or_insert_with(|| {
contexts.push(context.to_owned());
next
});
for qa in paragraph["qas"].as_array().unwrap_or(&vec![]) {
let impossible = qa["is_impossible"].as_bool().unwrap_or(false);
let (answer, start) = qa["answers"]
.as_array()
.and_then(|answers| answers.first())
.map(|first| {
(
first["text"].as_str().unwrap_or("").to_owned(),
first["answer_start"].as_f64().unwrap_or(-1.0) as f32,
)
})
.unwrap_or((String::new(), -1.0));
qas.push(QaRow {
qa: Qa {
question: qa["question"].as_str().unwrap_or("").to_owned(),
answer,
answer_start: if impossible { -1.0 } else { start },
impossible: if impossible { 1.0 } else { 0.0 },
},
context_index,
});
}
}
}
Ok((qas, contexts))
}
fn blob(strings: impl Iterator<Item = String>) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
let mut bytes = Vec::new();
let mut offsets = Vec::new();
let mut lengths = Vec::new();
for text in strings {
offsets.push(bytes.len() as f32);
lengths.push(text.len() as f32);
bytes.extend(text.bytes().map(f32::from));
}
(bytes, offsets, lengths)
}
fn decoded(qas: Vec<QaRow>, contexts: Vec<String>) -> Decoded {
let (q_bytes, q_off, q_len) = blob(qas.iter().map(|row| row.qa.question.clone()));
let (c_bytes, c_off, c_len) = blob(contexts.into_iter());
let (a_bytes, a_off, a_len) = blob(qas.iter().map(|row| row.qa.answer.clone()));
let n = qas.len();
let p = c_off.len();
Decoded {
matrices: vec![
MatrixData {
name: "question_bytes",
shape: vec![q_bytes.len()],
values: q_bytes,
},
MatrixData {
name: "question_offsets",
shape: vec![n],
values: q_off,
},
MatrixData {
name: "question_lengths",
shape: vec![n],
values: q_len,
},
MatrixData {
name: "context_bytes",
shape: vec![c_bytes.len()],
values: c_bytes,
},
MatrixData {
name: "context_offsets",
shape: vec![p],
values: c_off,
},
MatrixData {
name: "context_lengths",
shape: vec![p],
values: c_len,
},
MatrixData {
name: "context_index",
shape: vec![n],
values: qas.iter().map(|row| row.context_index as f32).collect(),
},
MatrixData {
name: "answer_bytes",
shape: vec![a_bytes.len()],
values: a_bytes,
},
MatrixData {
name: "answer_offsets",
shape: vec![n],
values: a_off,
},
MatrixData {
name: "answer_lengths",
shape: vec![n],
values: a_len,
},
MatrixData {
name: "answer_start",
shape: vec![n],
values: qas.iter().map(|row| row.qa.answer_start).collect(),
},
MatrixData {
name: "is_impossible",
shape: vec![n],
values: qas.iter().map(|row| row.qa.impossible).collect(),
},
],
text: None,
seq_len: 0,
}
}