use crate::runtime::dna::ir::elementwise::{Program, Source};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum Binding {
Input(usize),
Output(usize),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum Scalar {
Loaded(usize),
Value(usize),
Constant(u32),
Mapped {
input: usize,
rank: u32,
dimensions: [u32; 8],
strides: [u32; 8],
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum TileOp {
LoadLinear { slot: usize },
GatherRow {
value: usize,
table: usize,
indices: usize,
rows: u32,
width: u32,
dtype: u32,
},
Pointwise {
value: usize,
opcode: u32,
args: Vec<Scalar>,
parameter: u32,
},
Store { slot: usize, value: usize },
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) struct TileProgram {
pub element_count: u32,
pub bindings: Vec<Binding>,
pub ops: Vec<TileOp>,
}
impl TileProgram {
pub(in crate::runtime) fn input_count(&self) -> usize {
self
.bindings
.iter()
.position(|binding| matches!(binding, Binding::Output(_)))
.unwrap_or(self.bindings.len())
}
pub(in crate::runtime) fn from_elementwise(program: &Program) -> Self {
let bindings = (0..program.input_bytes.len())
.map(Binding::Input)
.chain((0..program.outputs.len()).map(Binding::Output))
.collect();
let mut ops = Vec::new();
let mut linear = std::collections::BTreeSet::new();
for op in &program.instructions {
if op.opcode != 4 {
for source in &op.sources {
if let Source::Input(slot) = source {
linear.insert(*slot);
}
}
}
}
ops.extend(linear.into_iter().map(|slot| TileOp::LoadLinear { slot }));
for (index, op) in program.instructions.iter().enumerate() {
if op.opcode == 4 {
let [Source::Input(table), Source::Input(indices)] = op.sources[..] else {
continue;
};
let [rows, width, dtype] = op.parameters[..] else {
continue;
};
ops.push(TileOp::GatherRow {
value: index,
table,
indices,
rows,
width,
dtype,
});
} else {
ops.push(TileOp::Pointwise {
value: index,
opcode: op.opcode,
args: op
.sources
.iter()
.map(|source| match source {
Source::Input(slot) => Scalar::Loaded(*slot),
Source::Value(value) => Scalar::Value(*value),
Source::Constant(bits) => Scalar::Constant(*bits),
Source::Broadcast {
input,
rank,
dimensions,
strides,
} => Scalar::Mapped {
input: *input,
rank: *rank,
dimensions: *dimensions,
strides: *strides,
},
})
.collect(),
parameter: op.parameters.first().copied().unwrap_or(0),
});
}
for (slot, &value) in program.outputs.iter().enumerate() {
if value == index {
ops.push(TileOp::Store { slot, value: index });
}
}
}
Self {
element_count: program.element_count,
bindings,
ops,
}
}
}
#[cfg(test)]
#[path = "../../../../../../test/rs/runtime/ir_tile_unit.rs"]
mod tests;