use fuel_tensor::Device;
use fuel_tensor::lazy::Tensor;
enum Outcome {
BuildErr(String),
Panic(String),
OkRealized { dims: Vec<usize>, elems: usize },
OkBuildRealizeErr { dims: Vec<usize> },
}
impl std::fmt::Debug for Outcome {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Outcome::BuildErr(s) => write!(f, "BuildErr({s:?})"),
Outcome::Panic(s) => write!(f, "Panic({s:?})"),
Outcome::OkRealized { dims, elems } => {
write!(f, "OkRealized {{ dims: {dims:?}, elems: {elems:?} }}")
}
Outcome::OkBuildRealizeErr { dims } => {
write!(f, "OkBuildRealizeErr {{ dims: {dims:?} }}")
}
}
}
}
fn cpu_f32(data: Vec<f32>, shape: &[usize]) -> Tensor {
Tensor::from_f32(data, shape.to_vec(), &Device::cpu()).unwrap()
}
fn probe<F>(f: F) -> Outcome
where
F: FnOnce() -> std::result::Result<Tensor, fuel_ir::Error> + std::panic::UnwindSafe,
{
let prev = std::panic::take_hook();
std::panic::set_hook(Box::new(|_| {})); let r = std::panic::catch_unwind(f);
std::panic::set_hook(prev);
match r {
Err(p) => {
let msg = p
.downcast_ref::<String>()
.cloned()
.or_else(|| p.downcast_ref::<&str>().map(|s| (*s).to_string()))
.unwrap_or_else(|| "<non-string panic>".to_string());
Outcome::Panic(msg.lines().next().unwrap_or("").to_string())
}
Ok(Err(e)) => Outcome::BuildErr(format!("{e}").lines().next().unwrap_or("").to_string()),
Ok(Ok(t)) => {
let dims = t.shape().dims().to_vec();
let realized =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| t.realize_f32()));
match realized {
Ok(v) => Outcome::OkRealized {
dims,
elems: v.len(),
},
Err(_) => Outcome::OkBuildRealizeErr { dims },
}
}
}
}
fn arm_label(guard_present: bool) -> &'static str {
if guard_present {
"ARM A - guard PRESENT (positive control: the probe must reach it)"
} else {
"ARM B - guard ABSENT (this is what the shipped artifact does)"
}
}
fn main() {
let guard_present = cfg!(debug_assertions);
println!(
"\n=== GAP-315 probe: Tensor::split_heads (pub fn, caller SHAPE, lazy) ===\n\
debug_assertions = {guard_present} ({})",
arm_label(guard_present)
);
let valid =
probe(|| cpu_f32((0..12).map(|i| i as f32).collect(), &[1, 2, 6]).split_heads(2, 3));
println!(" [control] VALID [1,2,6].split_heads(2,3) -> {valid:?}");
let i = probe(|| cpu_f32((0..12).map(|x| x as f32).collect(), &[1, 2, 6]).split_heads(2, 4));
println!(" [i] dim violated, COUNT DIFFERS -> {i:?}");
let ii = probe(|| cpu_f32(vec![], &[0, 2, 6]).split_heads(2, 4));
println!(" [ii] dim violated, COUNT PRESERVED (b=0) -> {ii:?}");
let z1 = probe(|| cpu_f32(vec![], &[0, 2, 6]).split_heads(2, 3));
println!(" [z1] CONTROL zero-size, VALID split(2,3) -> {z1:?}");
let z2 = probe(|| Ok(cpu_f32(vec![], &[0, 2, 6])));
println!(" [z2] CONTROL zero-size, NO split_heads -> {z2:?}");
let iii = probe(|| cpu_f32(vec![0.0; 6], &[6]).split_heads(2, 3));
println!(" [iii] rank violated (rank 1) -> {iii:?}");
let iv = probe(|| cpu_f32(vec![0.0; 12], &[2, 6]).split_heads(2, 3));
println!(" [iv] rank violated (rank 2) -> {iv:?}");
println!(" (measurement only - this test asserts nothing about the verdict)");
}