#[cfg(feature = "cpu")]
fn main() {
use rlx_ir::*;
use rlx_runtime::backend::Backend;
use rlx_runtime::{CompileOptions, Device, Session, SubgraphCache, run_if};
let const4 = |v: f32| -> Vec<u8> {
let mut out = Vec::with_capacity(16);
for _ in 0..4 {
out.extend_from_slice(&v.to_le_bytes());
}
out
};
let then_branch = {
let mut g = Graph::new("then");
let x = g.input("x", Shape::new(&[4], DType::F32));
let c = g.add_node(
Op::Constant { data: const4(10.0) },
vec![],
Shape::new(&[4], DType::F32),
);
let y = g.binary(op::BinaryOp::Add, x, c, Shape::new(&[4], DType::F32));
g.set_outputs(vec![y]);
g
};
let else_branch = {
let mut g = Graph::new("else");
let x = g.input("x", Shape::new(&[4], DType::F32));
let c = g.add_node(
Op::Constant { data: const4(2.0) },
vec![],
Shape::new(&[4], DType::F32),
);
let y = g.binary(op::BinaryOp::Mul, x, c, Shape::new(&[4], DType::F32));
g.set_outputs(vec![y]);
g
};
let _session = Session::new(Device::Cpu);
let backend: Box<dyn Backend> = Box::new(rlx_runtime::backend::cpu_backend::CpuBackend);
let mut cache = SubgraphCache::new(CompileOptions::new());
let x_data = vec![1.0, 2.0, 3.0, 4.0];
let then_result = run_if(
&mut cache,
backend.as_ref(),
1.0,
&then_branch,
&else_branch,
&[("x", &x_data)],
);
let else_result = run_if(
&mut cache,
backend.as_ref(),
0.0,
&then_branch,
&else_branch,
&[("x", &x_data)],
);
println!("then (predicate=1): x + 10 = {:?}", then_result[0]);
println!("else (predicate=0): x * 2 = {:?}", else_result[0]);
assert_eq!(then_result[0], vec![11.0, 12.0, 13.0, 14.0]);
assert_eq!(else_result[0], vec![2.0, 4.0, 6.0, 8.0]);
println!("✓ sub-graph execution works through SubgraphCache");
let r2 = run_if(
&mut cache,
backend.as_ref(),
1.0,
&then_branch,
&else_branch,
&[("x", &x_data)],
);
assert_eq!(r2, then_result);
println!("✓ cached sub-graph reuse works (second invocation)");
}
#[cfg(not(feature = "cpu"))]
fn main() {
eprintln!("requires --features cpu");
}