use crate::runtime::dna::ir::elementwise::{Program, Source};
use crate::{Error, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum Binding {
Input(usize),
Output(usize),
ReadWrite(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, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum PlaneScale {
Luma,
Chroma,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum LaneReduce {
Sum,
Max,
Min,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum BiasAct {
Relu,
Gelu,
Silu,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ConvTailStore {
Plain,
Relu,
ResidualRelu,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum LaneExpr {
GroupX,
GroupY,
GroupZ,
Lane,
Push(usize),
Induction,
U32FromF32(usize),
ConstU32(u32),
ConstF32(u32),
AddU32(usize, usize),
SubU32(usize, usize),
MulU32(usize, usize),
MinU32(usize, usize),
MaxU32(usize, usize),
LoopVar(u32),
LtU32(usize, usize),
GeU32(usize, usize),
NeqU32(usize, usize),
EqU32(usize, usize),
BufferU32 {
binding: usize,
index: usize,
},
BufferU8 {
binding: usize,
index: usize,
},
OrBool(usize, usize),
AndBool(usize, usize),
GtF32(usize, usize),
EqF32(usize, usize),
WaveMax(usize),
WaveSum(usize),
WaveMinU32(usize),
WaveLaneCount,
SelectU32 {
cond: usize,
if_true: usize,
if_false: usize,
},
SelectF32 {
cond: usize,
if_true: usize,
if_false: usize,
},
SharedU32 {
tile: usize,
offset: usize,
},
RegisterU32 {
tile: usize,
index: u32,
},
DivU32(usize, usize),
ModU32(usize, usize),
AddF32(usize, usize),
SubF32(usize, usize),
MulF32(usize, usize),
DivF32(usize, usize),
MaxF32(usize, usize),
MinF32(usize, usize),
ExpF32(usize),
LogF32(usize),
SqrtF32(usize),
RsqrtF32(usize),
PowF32(usize, usize),
FloorF32(usize),
PushF32(usize),
PushBitsU32(usize),
ShrU32 {
value: usize,
bits: u32,
},
LtF32(usize, usize),
GeF32(usize, usize),
PlaneSample {
binding: usize,
x: usize,
y: usize,
width: usize,
height: usize,
components: u32,
component: u32,
interp: usize,
sample_bits: u32,
},
ScalePlane {
kind: PlaneScale,
code: usize,
color_mode: usize,
sample_bits: u32,
},
YuvToRgb {
luma: usize,
cb: usize,
cr: usize,
space: usize,
component: usize,
},
BilinearF32 {
binding: usize,
base: usize,
xf: usize,
yf: usize,
width: usize,
height: usize,
},
PadFetchF32 {
binding: usize,
base: usize,
x: usize,
y: usize,
pad_left: usize,
pad_top: usize,
width: usize,
height: usize,
mode: usize,
value: usize,
},
WarpF32 {
input: usize,
transform: usize,
base: usize,
x: usize,
y: usize,
width: usize,
height: usize,
interp: usize,
mode: usize,
value: usize,
perspective: bool,
},
RemapF32 {
input: usize,
map: usize,
base: usize,
x: usize,
y: usize,
batch: usize,
width: usize,
height: usize,
out_width: usize,
out_height: usize,
interp: usize,
mode: usize,
map_batch: usize,
value: usize,
},
BorderedFetchF32 {
binding: usize,
base: usize,
x: usize,
y: usize,
width: usize,
height: usize,
mode: usize,
value: usize,
},
BorderCoordU32 {
coordinate: usize,
extent: usize,
mode: usize,
},
F32FromU32(usize),
LoadF32 {
binding: usize,
address: usize,
},
LoadU32 {
binding: usize,
address: usize,
},
Alias {
source: usize,
},
SharedF32 {
tile: usize,
offset: usize,
},
RegisterF32 {
tile: usize,
index: u32,
},
}
#[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 },
Loop {
extent: Extent,
step: u32,
start: Option<Extent>,
body: Vec<TileOp>,
},
ClearTile { tile: usize },
LoadShared {
tile: usize,
binding: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
vec: u32,
row_offset: Option<Extent>,
mask_origin: Option<Extent>,
z: bool,
column_offset: Option<Extent>,
transposed: bool,
},
LoadSharedConcat {
tile: usize,
bindings: [usize; 3],
widths: [usize; 3],
materialize: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
Barrier,
DotShared { acc: usize, lhs: usize, rhs: usize },
DotSharedParam {
acc: usize,
lhs: usize,
rhs: usize,
bias_acc: usize,
bias_gate: usize,
},
StoreTile {
acc: usize,
binding: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
mask_origin: Option<Extent>,
},
LoadSharedPrologue {
tile: usize,
upstream: usize,
saved: usize,
dy: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
gate: usize,
},
LoadSharedNorm {
tile: usize,
normalized: usize,
inv_stddev: usize,
norm_weight: usize,
upstream: usize,
addend: Option<usize>,
norm_input_gradient: usize,
weight_contribution: usize,
stage1: Option<usize>,
stats: usize,
inverse_columns: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
gate: usize,
},
DotSharedWide {
acc: usize,
lhs: usize,
rhs: usize,
column_tiles: u32,
},
StoreTileSegment {
acc: usize,
binding: usize,
row_origin: Extent,
row_lo: Extent,
row_hi: Extent,
column_origin: Extent,
column_extent: Extent,
column_tiles: u32,
},
StoreTileBiasParam {
acc: usize,
binding: usize,
origin: Extent,
row_origin: Extent,
row_extent: Extent,
gate: usize,
},
StoreTileBias {
acc: usize,
bias_binding: usize,
binding: usize,
z: bool,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
bias_origin: Option<usize>,
act: Option<BiasAct>,
},
StoreTileAct {
acc: usize,
bias_binding: usize,
linear_binding: usize,
act_binding: usize,
kind: usize,
alpha: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
StoreTileResidual {
acc: usize,
bias_binding: usize,
residual_binding: usize,
linear_binding: usize,
sum_binding: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
StoreTileResidualAct {
acc: usize,
bias_binding: usize,
residual_binding: usize,
linear_binding: usize,
sum_binding: usize,
act_binding: usize,
kind: usize,
alpha: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
StoreTileResidualNorm {
acc: usize,
bias_binding: usize,
residual_binding: usize,
linear_binding: usize,
sum_binding: usize,
tile: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
StoreTileGated {
gate_acc: usize,
up_acc: usize,
bias_binding: usize,
linear_binding: usize,
act_binding: usize,
linear_stride: Extent,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
gate_width: Extent,
bias_origin: Option<usize>,
},
StoreTileGateUp {
gate_acc: usize,
up_acc: usize,
gate_bias_binding: usize,
up_bias_binding: usize,
binding: usize,
row_origin: Extent,
column_origin: Extent,
row_extent: Extent,
column_extent: Extent,
},
Guard { bound: usize },
GuardIf { cond: usize },
Return,
ExpertScan {
offsets: usize,
experts: Extent,
row: usize,
acc: usize,
},
BoundaryGateUp {
acc: usize,
offsets: usize,
input: usize,
weight: usize,
bias: usize,
output: usize,
act: usize,
experts: Extent,
rows: Extent,
outputs: Extent,
inner: Extent,
gate_width: Extent,
row_origin: Extent,
column_origin: Extent,
},
BoundaryLinear {
acc: usize,
offsets: usize,
input: usize,
weight: usize,
bias: Option<usize>,
output: usize,
experts: Extent,
rows: Extent,
outputs: Extent,
inner: Extent,
column_extent: Extent,
row_origin: Extent,
column_origin: Extent,
transposed: bool,
},
LaneLoop { extent: Extent, body: Vec<TileOp> },
LaneFold {
acc: usize,
init: usize,
extent: Extent,
op: LaneReduce,
element: usize,
serial: bool,
},
LaneFoldDual {
acc: usize,
acc2: usize,
init: usize,
extent: Extent,
element: usize,
element2: usize,
},
LaneLoopRange {
start: usize,
step: u32,
extent: Extent,
body: Vec<TileOp>,
},
AssignScalar {
tile: usize,
index: u32,
value: usize,
},
StoreShared {
tile: usize,
offset: usize,
value: usize,
},
StoreSharedU32 {
tile: usize,
offset: usize,
value: usize,
},
AssignScalarU32 {
tile: usize,
index: u32,
value: usize,
},
StoreScalarU32 {
binding: usize,
address: usize,
value: usize,
},
TreeReduceArgmax {
value_tile: usize,
index_tile: usize,
},
TreeReduce { tile: usize, op: LaneReduce },
StoreScalar {
binding: usize,
address: usize,
value: usize,
},
LaneGuard { lane: usize, body: Vec<TileOp> },
LaneIf { cond: usize, body: Vec<TileOp> },
}
#[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,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum Extent {
Push(usize),
GroupX(u32),
GroupY(u32),
Induction,
HalfPush(usize),
Expr(usize),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum TileDecl {
Shared {
rows: u32,
columns: u32,
stride: u32,
},
Register { rows: u32, columns: u32 },
SharedU32 { elements: u32 },
RegisterU32 { elements: u32 },
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) struct BlockShape {
pub lanes: u32,
pub thread_columns: u32,
pub fragment_rows: u32,
pub fragment_columns: u32,
pub tile_rows: u32,
pub tile_columns: u32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(in crate::runtime) enum ScalarTail {
Add(f32),
Sub(f32),
Mul(f32),
Div(f32),
Max(f32),
Min(f32),
Neg,
Abs,
Relu,
Log,
Exp,
Sqrt,
Recip,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::runtime) struct BlockProgram {
pub bindings: Vec<Binding>,
pub push_scalars: Vec<String>,
pub push_f32_scalars: Vec<String>,
pub shape: BlockShape,
pub tiles: Vec<TileDecl>,
pub lane_exprs: Vec<LaneExpr>,
pub ops: Vec<TileOp>,
}
struct FilterPrelude {
out_of_bounds: usize,
zero_u32: usize,
plane: usize,
plane_base: usize,
out_x: usize,
out_y: usize,
out_index: usize,
height: usize,
width: usize,
mode: usize,
kernel_width: usize,
kernel_extent: usize,
size: usize,
size_radius: usize,
size_extent: usize,
radius_x: usize,
radius_y: usize,
border_value: usize,
zero: usize,
flt_max: usize,
neg_flt_max: usize,
}
struct ImageGatherFrame {
out_of_bounds: usize,
plane: usize,
plane_base: usize,
out_x: usize,
out_y: usize,
out_index: usize,
channels: usize,
in_height: usize,
in_width: usize,
out_height: usize,
out_width: usize,
}
impl ImageGatherFrame {
fn new(lane_exprs: &mut Vec<LaneExpr>) -> Self {
Self::new_out_at(lane_exprs, 4, 5)
}
fn new_out_at(lane_exprs: &mut Vec<LaneExpr>, out_h_push: usize, out_w_push: usize) -> Self {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let plane = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(0));
let channels = expr!(LaneExpr::Push(1));
let in_height = expr!(LaneExpr::Push(2));
let in_width = expr!(LaneExpr::Push(3));
let out_height = expr!(LaneExpr::Push(out_h_push));
let out_width = expr!(LaneExpr::Push(out_w_push));
let batch_channels = expr!(LaneExpr::MulU32(batch, channels));
let x_over = expr!(LaneExpr::GeU32(out_x, out_width));
let y_over = expr!(LaneExpr::GeU32(out_y, out_height));
let z_over = expr!(LaneExpr::GeU32(plane, batch_channels));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let in_plane = expr!(LaneExpr::MulU32(in_height, in_width));
let plane_base = expr!(LaneExpr::MulU32(plane, in_plane));
let out_rows = expr!(LaneExpr::MulU32(plane, out_height));
let out_row = expr!(LaneExpr::AddU32(out_rows, out_y));
let out_line = expr!(LaneExpr::MulU32(out_row, out_width));
let out_index = expr!(LaneExpr::AddU32(out_line, out_x));
Self {
out_of_bounds,
plane,
plane_base,
out_x,
out_y,
out_index,
channels,
in_height,
in_width,
out_height,
out_width,
}
}
fn program(
self,
bindings: Vec<Binding>,
push_scalars: Vec<String>,
value: usize,
lane_exprs: Vec<LaneExpr>,
) -> BlockProgram {
let store_binding = bindings.len() - 1;
BlockProgram {
bindings,
push_scalars,
push_f32_scalars: vec!["border_value".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: self.out_of_bounds,
},
TileOp::StoreScalar {
binding: store_binding,
address: self.out_index,
value,
},
],
}
}
}
fn lane(lane_exprs: &mut Vec<LaneExpr>, node: LaneExpr) -> usize {
lane_exprs.push(node);
lane_exprs.len() - 1
}
fn apply_scalar_tail(
lane_exprs: &mut Vec<LaneExpr>,
mut value: usize,
tail: &[ScalarTail],
) -> usize {
for op in tail {
value = match *op {
ScalarTail::Add(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::AddF32(value, c))
}
ScalarTail::Sub(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::SubF32(value, c))
}
ScalarTail::Mul(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::MulF32(value, c))
}
ScalarTail::Div(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::DivF32(value, c))
}
ScalarTail::Max(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::MaxF32(value, c))
}
ScalarTail::Min(scalar) => {
let c = lane(lane_exprs, LaneExpr::ConstF32(scalar.to_bits()));
lane(lane_exprs, LaneExpr::MinF32(value, c))
}
ScalarTail::Neg => {
let zero = lane(lane_exprs, LaneExpr::ConstF32(0.0f32.to_bits()));
lane(lane_exprs, LaneExpr::SubF32(zero, value))
}
ScalarTail::Abs => {
let zero = lane(lane_exprs, LaneExpr::ConstF32(0.0f32.to_bits()));
let negated = lane(lane_exprs, LaneExpr::SubF32(zero, value));
lane(lane_exprs, LaneExpr::MaxF32(value, negated))
}
ScalarTail::Relu => {
let zero = lane(lane_exprs, LaneExpr::ConstF32(0.0f32.to_bits()));
lane(lane_exprs, LaneExpr::MaxF32(value, zero))
}
ScalarTail::Log => lane(lane_exprs, LaneExpr::LogF32(value)),
ScalarTail::Exp => lane(lane_exprs, LaneExpr::ExpF32(value)),
ScalarTail::Sqrt => lane(lane_exprs, LaneExpr::SqrtF32(value)),
ScalarTail::Recip => {
let one = lane(lane_exprs, LaneExpr::ConstF32(1.0f32.to_bits()));
lane(lane_exprs, LaneExpr::DivF32(one, value))
}
};
}
value
}
fn channel_f32_sel(
lane_exprs: &mut Vec<LaneExpr>,
zero_c: usize,
one_c: usize,
base: usize,
) -> usize {
let f0 = lane(lane_exprs, LaneExpr::PushF32(base));
let f1 = lane(lane_exprs, LaneExpr::PushF32(base + 1));
let f2 = lane(lane_exprs, LaneExpr::PushF32(base + 2));
let tail = lane(
lane_exprs,
LaneExpr::SelectF32 {
cond: one_c,
if_true: f1,
if_false: f2,
},
);
lane(
lane_exprs,
LaneExpr::SelectF32 {
cond: zero_c,
if_true: f0,
if_false: tail,
},
)
}
impl BlockProgram {
pub(in crate::runtime) fn with_scalar_tail(mut self, tail: &[ScalarTail]) -> Self {
if tail.is_empty() {
return self;
}
for op in &mut self.ops {
if let TileOp::StoreScalar { value, .. } = op {
*value = apply_scalar_tail(&mut self.lane_exprs, *value, tail);
}
}
self
}
pub(in crate::runtime) fn with_scalar_prologue(
mut self,
binding: usize,
prologue: &[ScalarTail],
) -> Self {
if prologue.is_empty() {
return self;
}
let loads = self
.lane_exprs
.iter()
.enumerate()
.filter_map(|(index, expr)| {
matches!(expr, LaneExpr::LoadF32 { binding: b, .. } if *b == binding).then_some(index)
})
.collect::<Vec<_>>();
for index in loads {
let cloned = self.lane_exprs[index].clone();
let load = lane(&mut self.lane_exprs, cloned);
let folded = apply_scalar_tail(&mut self.lane_exprs, load, prologue);
self.lane_exprs[index] = LaneExpr::Alias { source: folded };
}
self
}
pub(in crate::runtime) fn workgroups(&self, rows: u32, columns: u32) -> [u32; 3] {
[
rows.div_ceil(self.shape.tile_rows),
columns.div_ceil(self.shape.tile_columns),
1,
]
}
pub(in crate::runtime) fn video_to_matrix(planar: bool, sample_bits: u32) -> Result<Self> {
if sample_bits != 8 && sample_bits != 10 {
return Err(Error::invalid_argument(
"video plane loads admit 8- or 10-bit samples",
));
}
if planar && sample_bits != 8 {
return Err(Error::invalid_argument(
"planar video conversion admits 8-bit samples",
));
}
let mut lane_exprs: Vec<LaneExpr> = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
const P_OUT_WIDTH: usize = 0;
const P_OUT_HEIGHT: usize = 1;
const P_OUTPUT_BASE: usize = 2;
const P_CHANNELS: usize = 3;
const P_LAYOUT: usize = 4;
const P_INTERP: usize = 5;
const P_LUMA_W: usize = 6;
const P_LUMA_H: usize = 7;
const P_CHROMA_W: usize = 8;
const P_CHROMA_H: usize = 9;
const P_COLOR: usize = 10;
const F_OFF_X: usize = 0;
const F_OFF_Y: usize = 1;
const F_SCALE_X: usize = 2;
const F_SCALE_Y: usize = 3;
const F_VALID_MIN_X: usize = 4;
const F_VALID_MIN_Y: usize = 5;
const F_VALID_MAX_X: usize = 6;
const F_VALID_MAX_Y: usize = 7;
const F_FILL: usize = 8;
const F_MEAN: usize = 11;
const F_STD: usize = 14;
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let channel = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let half = expr!(LaneExpr::ConstF32(0.5f32.to_bits()));
let zero = expr!(LaneExpr::ConstU32(0));
let one = expr!(LaneExpr::ConstU32(1));
let two = expr!(LaneExpr::ConstU32(2));
let three = expr!(LaneExpr::ConstU32(3));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let out_width = expr!(LaneExpr::Push(P_OUT_WIDTH));
let out_height = expr!(LaneExpr::Push(P_OUT_HEIGHT));
let channels_mode = expr!(LaneExpr::Push(P_CHANNELS));
let luma_only = expr!(LaneExpr::EqU32(channels_mode, zero));
let channel_count = expr!(LaneExpr::SelectU32 {
cond: luma_only,
if_true: one,
if_false: three,
});
let fit_x = expr!(LaneExpr::LtU32(out_x, out_width));
let fit_y = expr!(LaneExpr::LtU32(out_y, out_height));
let fit_c = expr!(LaneExpr::LtU32(channel, channel_count));
let fit_xy = expr!(LaneExpr::AndBool(fit_x, fit_y));
let in_bounds = expr!(LaneExpr::AndBool(fit_xy, fit_c));
let out_x_f = expr!(LaneExpr::F32FromU32(out_x));
let out_y_f = expr!(LaneExpr::F32FromU32(out_y));
let px = expr!(LaneExpr::AddF32(out_x_f, half));
let py = expr!(LaneExpr::AddF32(out_y_f, half));
let valid_min_x = expr!(LaneExpr::PushF32(F_VALID_MIN_X));
let valid_max_x = expr!(LaneExpr::PushF32(F_VALID_MAX_X));
let valid_min_y = expr!(LaneExpr::PushF32(F_VALID_MIN_Y));
let valid_max_y = expr!(LaneExpr::PushF32(F_VALID_MAX_Y));
let inside_x_lo = expr!(LaneExpr::GeF32(px, valid_min_x));
let inside_x_hi = expr!(LaneExpr::LtF32(px, valid_max_x));
let inside_y_lo = expr!(LaneExpr::GeF32(py, valid_min_y));
let inside_y_hi = expr!(LaneExpr::LtF32(py, valid_max_y));
let inside_x = expr!(LaneExpr::AndBool(inside_x_lo, inside_x_hi));
let inside_y = expr!(LaneExpr::AndBool(inside_y_lo, inside_y_hi));
let inside = expr!(LaneExpr::AndBool(inside_x, inside_y));
let scale_x = expr!(LaneExpr::PushF32(F_SCALE_X));
let scale_y = expr!(LaneExpr::PushF32(F_SCALE_Y));
let off_x = expr!(LaneExpr::PushF32(F_OFF_X));
let off_y = expr!(LaneExpr::PushF32(F_OFF_Y));
let px_scaled = expr!(LaneExpr::MulF32(px, scale_x));
let py_scaled = expr!(LaneExpr::MulF32(py, scale_y));
let sx = expr!(LaneExpr::AddF32(px_scaled, off_x));
let sy = expr!(LaneExpr::AddF32(py_scaled, off_y));
let interp = expr!(LaneExpr::Push(P_INTERP));
let color_mode = expr!(LaneExpr::Push(P_COLOR));
let luma_width = expr!(LaneExpr::Push(P_LUMA_W));
let luma_height = expr!(LaneExpr::Push(P_LUMA_H));
let chroma_width = expr!(LaneExpr::Push(P_CHROMA_W));
let chroma_height = expr!(LaneExpr::Push(P_CHROMA_H));
let luma_code = expr!(LaneExpr::PlaneSample {
binding: 0,
x: sx,
y: sy,
width: luma_width,
height: luma_height,
components: 1,
component: 0,
interp,
sample_bits,
});
let luma = expr!(LaneExpr::ScalePlane {
kind: PlaneScale::Luma,
code: luma_code,
color_mode,
sample_bits,
});
let sx_half = expr!(LaneExpr::SubF32(sx, half));
let sy_half = expr!(LaneExpr::SubF32(sy, half));
let chroma_x = expr!(LaneExpr::MulF32(sx_half, half));
let chroma_y = expr!(LaneExpr::MulF32(sy_half, half));
let (cb_binding, cr_binding, components) = if planar { (1, 2, 1) } else { (1, 1, 2) };
let cb_code = expr!(LaneExpr::PlaneSample {
binding: cb_binding,
x: chroma_x,
y: chroma_y,
width: chroma_width,
height: chroma_height,
components,
component: 0,
interp,
sample_bits,
});
let cr_code = expr!(LaneExpr::PlaneSample {
binding: cr_binding,
x: chroma_x,
y: chroma_y,
width: chroma_width,
height: chroma_height,
components,
component: u32::from(!planar),
interp,
sample_bits,
});
let cb = expr!(LaneExpr::ScalePlane {
kind: PlaneScale::Chroma,
code: cb_code,
color_mode,
sample_bits,
});
let cr = expr!(LaneExpr::ScalePlane {
kind: PlaneScale::Chroma,
code: cr_code,
color_mode,
sample_bits,
});
let zero_c = expr!(LaneExpr::EqU32(channel, zero));
let one_c = expr!(LaneExpr::EqU32(channel, one));
let cb_half = expr!(LaneExpr::AddF32(cb, half));
let cr_half = expr!(LaneExpr::AddF32(cr, half));
let yuv_tail = expr!(LaneExpr::SelectF32 {
cond: one_c,
if_true: cb_half,
if_false: cr_half,
});
let yuv_value = expr!(LaneExpr::SelectF32 {
cond: zero_c,
if_true: luma,
if_false: yuv_tail,
});
let bgr_mode = expr!(LaneExpr::EqU32(channels_mode, two));
let bgr_component = expr!(LaneExpr::SubU32(two, channel));
let component = expr!(LaneExpr::SelectU32 {
cond: bgr_mode,
if_true: bgr_component,
if_false: channel,
});
let space = expr!(LaneExpr::ShrU32 {
value: color_mode,
bits: 1
});
let rgb_value = expr!(LaneExpr::YuvToRgb {
luma,
cb,
cr,
space,
component,
});
let yuv_mode = expr!(LaneExpr::EqU32(channels_mode, three));
let tri_value = expr!(LaneExpr::SelectF32 {
cond: yuv_mode,
if_true: yuv_value,
if_false: rgb_value,
});
let converted = expr!(LaneExpr::SelectF32 {
cond: luma_only,
if_true: luma,
if_false: tri_value,
});
let fill = channel_f32_sel(&mut lane_exprs, zero_c, one_c, F_FILL);
let mean = channel_f32_sel(&mut lane_exprs, zero_c, one_c, F_MEAN);
let std_sel = channel_f32_sel(&mut lane_exprs, zero_c, one_c, F_STD);
let value = expr!(LaneExpr::SelectF32 {
cond: inside,
if_true: converted,
if_false: fill,
});
let centered = expr!(LaneExpr::SubF32(value, mean));
let result = expr!(LaneExpr::DivF32(centered, std_sel));
let output_base = expr!(LaneExpr::Push(P_OUTPUT_BASE));
let layout_mode = expr!(LaneExpr::Push(P_LAYOUT));
let ch_span = expr!(LaneExpr::MulU32(channel, out_height));
let ch_row = expr!(LaneExpr::AddU32(ch_span, out_y));
let chw_row = expr!(LaneExpr::MulU32(ch_row, out_width));
let chw_flat = expr!(LaneExpr::AddU32(chw_row, out_x));
let chw_index = expr!(LaneExpr::AddU32(output_base, chw_flat));
let hw_row = expr!(LaneExpr::MulU32(out_y, out_width));
let hw_px = expr!(LaneExpr::AddU32(hw_row, out_x));
let hwc_px = expr!(LaneExpr::MulU32(hw_px, channel_count));
let hwc_flat = expr!(LaneExpr::AddU32(hwc_px, channel));
let hwc_index = expr!(LaneExpr::AddU32(output_base, hwc_flat));
let planar_layout = expr!(LaneExpr::EqU32(layout_mode, zero));
let index = expr!(LaneExpr::SelectU32 {
cond: planar_layout,
if_true: chw_index,
if_false: hwc_index,
});
let output_binding = if planar { 3 } else { 2 };
let mut bindings: Vec<Binding> = Vec::new();
if planar {
bindings.extend([Binding::Input(0), Binding::Input(1), Binding::Input(2)]);
} else {
bindings.extend([Binding::Input(0), Binding::Input(1)]);
}
bindings.push(Binding::Output(0));
Ok(Self {
bindings,
push_scalars: [
"out_width",
"out_height",
"output_base",
"channels_mode",
"layout_mode",
"interp_mode",
"luma_width",
"luma_height",
"chroma_width",
"chroma_height",
"color_mode",
]
.iter()
.map(|name| name.to_string())
.collect(),
push_f32_scalars: [
"src_off_x",
"src_off_y",
"src_scale_x",
"src_scale_y",
"valid_min_x",
"valid_min_y",
"valid_max_x",
"valid_max_y",
"fill_0",
"fill_1",
"fill_2",
"mean_0",
"mean_1",
"mean_2",
"std_0",
"std_1",
"std_2",
]
.iter()
.map(|name| name.to_string())
.collect(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 16,
tile_columns: 16,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![TileOp::LaneIf {
cond: in_bounds,
body: vec![TileOp::StoreScalar {
binding: output_binding,
address: index,
value: result,
}],
}],
})
}
pub(in crate::runtime) fn mat_mul_nt(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
if vec == 0 || vec > 4 || !tile_k.is_multiple_of(vec) {
return Err(Error::invalid_argument(
"tile_k must be a positive multiple of the staging width (≤4)",
));
}
if !tile_rows.is_multiple_of(fragment_rows) || !tile_columns.is_multiple_of(fragment_columns) {
return Err(Error::invalid_argument(
"output tile must be an exact multiple of the fragment tile",
));
}
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(2, 0, 1),
)?;
ops.push(TileOp::StoreTile {
acc: 2,
binding: 2,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
mask_origin: None,
});
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec!["m".into(), "n".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn mat_mul_nt_bias(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
act: Option<BiasAct>,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(2, 0, 1),
)?;
ops.push(TileOp::StoreTileBias {
acc: 2,
bias_binding: 2,
z: false,
binding: 3,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
bias_origin: None,
act,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec!["m".into(), "n".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
ops.push(TileOp::StoreTileBias {
acc: 2,
bias_binding: 2,
z: false,
binding: 3,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
bias_origin: None,
act: None,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_act(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
ops.push(TileOp::StoreTileAct {
acc: 2,
bias_binding: 2,
linear_binding: 3,
act_binding: 4,
kind: 3,
alpha: 0,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
"act_kind".into(),
],
push_f32_scalars: vec!["alpha".into()],
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_residual(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
ops.push(TileOp::StoreTileResidual {
acc: 2,
bias_binding: 2,
residual_binding: 3,
linear_binding: 4,
sum_binding: 5,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_residual_act(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
ops.push(TileOp::StoreTileResidualAct {
acc: 2,
bias_binding: 2,
residual_binding: 3,
linear_binding: 4,
sum_binding: 5,
act_binding: 6,
kind: 3,
alpha: 0,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
"act_kind".into(),
],
push_f32_scalars: vec!["alpha".into()],
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn sum(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"sum lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
},
E::ConstF32(0), E::RegisterF32 { tile: 1, index: 0 },
E::Lane,
E::ConstU32(0),
E::SharedF32 { tile: 0, offset: 6 },
];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::LaneFold {
acc: 1,
init: 3,
extent: Extent::Push(0),
op: LaneReduce::Sum,
element: 2,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 5,
value: 4,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::LaneGuard {
lane: 6,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 6,
value: 7,
}],
},
],
})
}
pub(in crate::runtime) fn sum_axis(lanes: u32) -> Result<Self> {
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX,
E::ConstU32(lanes),
E::MulU32(3, 4), E::Lane,
E::AddU32(5, 6), E::MulU32(0, 2), E::LtU32(7, 8), E::ModU32(7, 2), E::DivU32(7, 2), E::MulU32(11, 1), E::MulU32(12, 2), E::AddU32(13, 10), E::Induction, E::MulU32(15, 2), E::AddU32(14, 16), E::LoadF32 {
binding: 0,
address: 17,
},
E::ConstF32(0),
E::RegisterF32 { tile: 0, index: 0 },
];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
ops: vec![TileOp::LaneIf {
cond: 9,
body: vec![
TileOp::LaneFold {
acc: 0,
init: 19,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 18,
serial: true,
},
TileOp::StoreScalar {
binding: 1,
address: 7,
value: 20,
},
],
}],
})
}
pub(in crate::runtime) fn sum_axis_cooperative(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"sum_axis_cooperative lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX, E::ModU32(3, 2), E::DivU32(3, 2), E::MulU32(5, 1), E::MulU32(6, 2), E::AddU32(7, 4), E::Induction, E::MulU32(9, 2), E::AddU32(8, 10), E::LoadF32 {
binding: 0,
address: 11,
},
E::ConstF32(0),
E::RegisterF32 { tile: 1, index: 0 },
E::Lane,
E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 16,
},
];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::LaneFold {
acc: 1,
init: 13,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 12,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 14,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::LaneGuard {
lane: 16,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 3,
value: 17,
}],
},
],
})
}
pub(in crate::runtime) fn conv_1d_im2_col() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::MulU32(6, 8), E::MulU32(5, 12), E::MulU32(14, 13), E::GeU32(4, 15), E::ModU32(4, 13), E::DivU32(4, 13), E::ModU32(17, 8), E::DivU32(17, 8), E::ModU32(18, 12), E::DivU32(18, 12), E::MulU32(21, 9), E::MulU32(19, 11), E::AddU32(23, 24), E::GeU32(25, 10), E::SubU32(25, 10), E::LtU32(27, 7), E::AndBool(26, 28), E::MulU32(22, 6), E::AddU32(30, 20), E::MulU32(31, 7), E::AddU32(32, 27), E::LoadF32 {
binding: 0,
address: 33,
},
E::ConstF32(0), E::RegisterF32 { tile: 0, index: 0 },
];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"input_length".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"dilation".into(),
"output_length".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::GuardIf { cond: 16 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 35,
},
TileOp::LaneIf {
cond: 29,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 34,
}],
},
TileOp::StoreScalar {
binding: 1,
address: 4,
value: 36,
},
],
)
}
pub(in crate::runtime) fn conv_1d_bias_add() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::MulU32(5, 6), E::GeU32(4, 7), E::ModU32(4, 6), E::LoadF32 {
binding: 0,
address: 4,
},
E::LoadF32 {
binding: 1,
address: 9,
},
E::AddF32(10, 11),
];
Self::flat_program(
vec![Binding::ReadWrite(0), Binding::Input(0)],
vec!["rows".into(), "columns".into()],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::StoreScalar {
binding: 0,
address: 4,
value: 12,
},
],
)
}
pub(in crate::runtime) fn conv_1d_transpose() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::Lane,
E::ConstU32(32),
E::DivU32(0, 1), E::ModU32(0, 1), E::GroupX,
E::GroupY,
E::GroupZ, E::Push(0), E::Push(1), E::Push(2), E::MulU32(5, 1), E::AddU32(10, 2), E::MulU32(4, 1), E::AddU32(12, 3), E::LtU32(11, 8), E::LtU32(13, 9), E::AndBool(14, 15), E::MulU32(8, 9), E::MulU32(6, 17), E::MulU32(11, 9), E::AddU32(19, 13), E::AddU32(18, 20), E::LoadF32 {
binding: 0,
address: 21,
},
E::ConstF32(0), E::ConstU32(33), E::MulU32(2, 24), E::AddU32(25, 3), E::AddU32(12, 2), E::AddU32(10, 3), E::LtU32(27, 9), E::LtU32(28, 8), E::AndBool(29, 30), E::MulU32(27, 8), E::AddU32(32, 28), E::AddU32(18, 33), E::MulU32(3, 24), E::AddU32(35, 2), E::SharedF32 {
tile: 0,
offset: 36,
},
];
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["batch_size".into(), "rows".into(), "columns".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 1024,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![TileDecl::Shared {
rows: 32,
columns: 32,
stride: 33,
}],
lane_exprs,
ops: vec![
TileOp::StoreShared {
tile: 0,
offset: 26,
value: 23,
},
TileOp::LaneIf {
cond: 16,
body: vec![TileOp::StoreShared {
tile: 0,
offset: 26,
value: 22,
}],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 31,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 34,
value: 37,
}],
},
],
}
}
pub(in crate::runtime) fn conv_2d() -> Self {
Self::conv_2d_program(false)
}
pub(in crate::runtime) fn conv_2d_relu() -> Self {
Self::conv_2d_program(true)
}
fn conv_2d_program(relu: bool) -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::Push(10), E::MulU32(13, 14), E::MulU32(7, 16), E::MulU32(5, 17), E::GeU32(4, 18), E::ModU32(4, 14), E::DivU32(4, 14), E::ModU32(21, 13), E::DivU32(21, 13), E::ModU32(23, 7), E::DivU32(23, 7), E::DivU32(6, 15), E::DivU32(7, 15), E::DivU32(24, 27), E::MulU32(28, 26), E::LoadF32 {
binding: 2,
address: 24,
}, E::MulU32(22, 11), E::MulU32(20, 11), E::LoopVar(3), E::AddU32(70, 33), E::GeU32(34, 12), E::SubU32(34, 12), E::LtU32(36, 8), E::LoopVar(5), E::AddU32(71, 38), E::GeU32(39, 12), E::SubU32(39, 12), E::LtU32(41, 9), E::LoopVar(2), E::AddU32(72, 43), E::MulU32(25, 6), E::MulU32(44, 8), E::AddU32(46, 36), E::MulU32(47, 9), E::AddU32(48, 41), E::AddU32(73, 49), E::MulU32(24, 26), E::AddU32(74, 43), E::MulU32(52, 10), E::AddU32(53, 33), E::MulU32(54, 10), E::AddU32(55, 38), E::LoadF32 {
binding: 0,
address: 50,
},
E::LoadF32 {
binding: 1,
address: 56,
},
E::MulF32(57, 58), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(60, 59), E::AndBool(35, 37), E::AndBool(40, 42), E::AndBool(62, 63), E::ConstU32(0), E::ConstF32(0), E::MaxF32(66, 60), E::MulU32(45, 8), E::MulU32(68, 9), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, E::RegisterU32 { tile: 1, index: 5 }, ];
let store_value = if relu { 67 } else { 60 };
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
"groups".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 6 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 19 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 30,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 31,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 32,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 29,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 69,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 51,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 5,
value: 4,
},
TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Expr(26),
body: vec![TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 62,
body: vec![TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 63,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 61,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 3,
address: 75,
value: store_value,
},
],
)
}
pub(in crate::runtime) fn conv_1d_parameter_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::GeU32(4, 14), E::MulU32(7, 6), E::MulU32(16, 9), E::GeU32(4, 17), E::LtU32(4, 17), E::SubU32(4, 17), E::LoopVar(3), E::LoopVar(4), E::MulU32(21, 7), E::AddU32(23, 58), E::MulU32(24, 13), E::AddU32(25, 22), E::LoadF32 {
binding: 1,
address: 26,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(28, 27), E::ModU32(4, 9), E::DivU32(4, 9), E::ModU32(31, 6), E::DivU32(31, 6), E::MulU32(22, 10), E::MulU32(30, 12), E::AddU32(34, 57), E::GeU32(36, 11), E::SubU32(36, 11), E::LtU32(38, 8), E::AndBool(37, 39), E::MulU32(21, 6), E::AddU32(41, 55), E::MulU32(42, 8), E::AddU32(43, 38), E::MulU32(21, 7), E::AddU32(45, 56), E::MulU32(46, 13), E::AddU32(47, 22), E::LoadF32 {
binding: 0,
address: 44,
},
E::LoadF32 {
binding: 1,
address: 48,
},
E::MulF32(49, 50), E::AddF32(28, 51), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, ];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Output(0),
Binding::Output(1),
],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_length".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"dilation".into(),
"output_length".into(),
"parameter_count".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 5 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 15 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 54,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 32,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 33,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 35,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 20,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 4,
},
TileOp::LaneIf {
cond: 18,
body: vec![
TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(8),
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 29,
}],
}],
},
TileOp::StoreScalar {
binding: 3,
address: 58,
value: 28,
},
],
},
TileOp::LaneIf {
cond: 19,
body: vec![
TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(8),
body: vec![TileOp::LaneIf {
cond: 40,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 52,
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 59,
value: 28,
},
],
},
],
)
}
pub(in crate::runtime) fn conv_1d_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(5, 6), E::MulU32(14, 8), E::GeU32(4, 15), E::ModU32(4, 8), E::DivU32(4, 8), E::ModU32(18, 6), E::DivU32(18, 6), E::LoopVar(2), E::LoopVar(3), E::AddU32(17, 11), E::MulU32(22, 12), E::GeU32(49, 24), E::SubU32(49, 24), E::ModU32(26, 10), E::ConstU32(0),
E::EqU32(27, 28), E::DivU32(26, 10), E::LtU32(30, 13), E::AndBool(25, 29), E::AndBool(32, 31), E::MulU32(20, 7), E::AddU32(51, 21), E::MulU32(35, 13), E::AddU32(36, 30), E::MulU32(21, 6), E::AddU32(38, 50), E::MulU32(39, 9), E::AddU32(40, 22), E::LoadF32 {
binding: 0,
address: 37,
},
E::LoadF32 {
binding: 1,
address: 41,
},
E::MulF32(42, 43), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(45, 44), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_length".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"dilation".into(),
"output_length".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 16 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 48,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 23,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 19,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 34,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 4,
},
TileOp::LaneLoopRange {
start: 47,
step: 1,
extent: Extent::Push(2),
body: vec![TileOp::LaneLoopRange {
start: 47,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 33,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 46,
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 52,
value: 45,
},
],
)
}
pub(in crate::runtime) fn conv_transpose_1d() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(5, 7), E::MulU32(14, 13), E::GeU32(4, 15), E::ModU32(4, 13), E::DivU32(4, 13), E::ModU32(18, 7), E::DivU32(18, 7), E::LoopVar(2), E::LoopVar(3), E::AddU32(17, 11), E::MulU32(22, 12), E::GeU32(49, 24), E::SubU32(49, 24), E::ModU32(26, 10), E::ConstU32(0),
E::EqU32(27, 28), E::DivU32(26, 10), E::LtU32(30, 8), E::AndBool(25, 29), E::AndBool(32, 31), E::MulU32(20, 6), E::AddU32(51, 21), E::MulU32(35, 8), E::AddU32(36, 30), E::MulU32(21, 7), E::AddU32(38, 50), E::MulU32(39, 9), E::AddU32(40, 22), E::LoadF32 {
binding: 0,
address: 37,
},
E::LoadF32 {
binding: 1,
address: 41,
},
E::MulF32(42, 43), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(45, 44), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_length".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"dilation".into(),
"output_length".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 16 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 48,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 23,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 19,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 34,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 4,
},
TileOp::LaneLoopRange {
start: 47,
step: 1,
extent: Extent::Push(1),
body: vec![TileOp::LaneLoopRange {
start: 47,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 33,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 46,
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 45,
},
],
)
}
pub(in crate::runtime) fn conv_2d_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::Push(10), E::MulU32(5, 6), E::MulU32(16, 8), E::MulU32(17, 9), E::GeU32(4, 18), E::ModU32(4, 9), E::DivU32(4, 9), E::ModU32(21, 8), E::DivU32(21, 8), E::ModU32(23, 6), E::DivU32(23, 6), E::DivU32(6, 15), E::DivU32(7, 15), E::DivU32(24, 26), E::MulU32(28, 26), E::SubU32(24, 29), E::MulU32(28, 27), E::LoopVar(2), E::LoopVar(3), E::LoopVar(5), E::AddU32(79, 32), E::AddU32(76, 12), E::GeU32(36, 33), E::SubU32(36, 33), E::ModU32(38, 11), E::ConstU32(0),
E::EqU32(39, 40), E::DivU32(38, 11), E::LtU32(42, 13), E::AddU32(75, 12), E::GeU32(44, 34), E::SubU32(44, 34), E::ModU32(46, 11), E::EqU32(47, 40), E::DivU32(46, 11), E::LtU32(49, 14), E::AndBool(37, 41), E::AndBool(51, 43), E::AndBool(45, 48), E::AndBool(53, 50), E::AndBool(52, 54), E::MulU32(77, 7), E::AddU32(56, 35), E::MulU32(57, 13), E::AddU32(58, 42), E::MulU32(59, 14), E::AddU32(60, 49), E::MulU32(35, 26), E::AddU32(62, 78), E::MulU32(63, 10), E::AddU32(64, 33), E::MulU32(65, 10), E::AddU32(66, 34), E::LoadF32 {
binding: 0,
address: 61,
},
E::LoadF32 {
binding: 1,
address: 67,
},
E::MulF32(68, 69), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(71, 70), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, E::RegisterU32 { tile: 1, index: 5 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
"groups".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 6 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 19 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 74,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 20,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 22,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 25,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 30,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 31,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 5,
value: 4,
},
TileOp::LaneLoopRange {
start: 73,
step: 1,
extent: Extent::Expr(27),
body: vec![TileOp::LaneLoopRange {
start: 73,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 52,
body: vec![TileOp::LaneLoopRange {
start: 73,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 54,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 72,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 80,
value: 71,
},
],
)
}
pub(in crate::runtime) fn conv_2d_parameter_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::Push(10), E::Push(11), E::GeU32(4, 16), E::DivU32(6, 15), E::DivU32(7, 15), E::MulU32(7, 18), E::MulU32(20, 10), E::MulU32(21, 10), E::GeU32(4, 22), E::LtU32(4, 22), E::SubU32(4, 22), E::LoopVar(3), E::LoopVar(4), E::LoopVar(5), E::MulU32(26, 7), E::AddU32(29, 83), E::MulU32(30, 13), E::AddU32(31, 27), E::MulU32(32, 14), E::AddU32(33, 28), E::LoadF32 {
binding: 1,
address: 34,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(36, 35), E::ModU32(4, 10), E::DivU32(4, 10), E::ModU32(39, 10), E::DivU32(39, 10), E::ModU32(41, 18), E::DivU32(41, 18), E::DivU32(43, 19), E::MulU32(44, 18), E::AddU32(45, 42), E::MulU32(27, 11), E::AddU32(47, 80), E::GeU32(48, 12), E::SubU32(48, 12), E::LtU32(50, 8), E::MulU32(84, 11), E::AddU32(52, 79), E::GeU32(53, 12), E::SubU32(53, 12), E::LtU32(55, 9), E::AndBool(49, 51), E::AndBool(54, 56), E::AndBool(57, 58), E::MulU32(26, 6), E::AddU32(60, 81), E::MulU32(61, 8), E::AddU32(62, 50), E::MulU32(63, 9), E::AddU32(64, 55), E::MulU32(26, 7), E::AddU32(66, 82), E::MulU32(67, 13), E::AddU32(68, 27), E::MulU32(69, 14), E::AddU32(70, 84), E::LoadF32 {
binding: 0,
address: 65,
},
E::LoadF32 {
binding: 1,
address: 71,
},
E::MulF32(72, 73), E::AddF32(36, 74), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, E::RegisterU32 { tile: 1, index: 5 }, E::LoopVar(6), ];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Output(0),
Binding::Output(1),
],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
"groups".into(),
"parameter_count".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 6 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 17 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 77,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 4,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 38,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 40,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 46,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 43,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 5,
value: 25,
},
TileOp::LaneIf {
cond: 23,
body: vec![
TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(8),
body: vec![TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(9),
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 37,
}],
}],
}],
},
TileOp::StoreScalar {
binding: 3,
address: 83,
value: 36,
},
],
},
TileOp::LaneIf {
cond: 24,
body: vec![
TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(8),
body: vec![TileOp::LaneIf {
cond: 57,
body: vec![TileOp::LaneLoopRange {
start: 76,
step: 1,
extent: Extent::Push(9),
body: vec![TileOp::LaneIf {
cond: 58,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 75,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 78,
value: 36,
},
],
},
],
)
}
pub(in crate::runtime) fn conv_transpose_2d() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::MulU32(5, 7), E::MulU32(15, 13), E::MulU32(16, 14), E::GeU32(4, 17), E::ModU32(4, 14), E::DivU32(4, 14), E::ModU32(20, 13), E::DivU32(20, 13), E::ModU32(22, 7), E::DivU32(22, 7), E::LoopVar(2), E::LoopVar(3), E::LoopVar(5), E::AddU32(21, 12), E::GeU32(67, 26), E::SubU32(67, 26), E::ModU32(30, 11), E::ConstU32(0),
E::EqU32(31, 32), E::DivU32(30, 11), E::LtU32(34, 8), E::AddU32(19, 12), E::GeU32(68, 27), E::SubU32(68, 27), E::ModU32(38, 11), E::EqU32(39, 32), E::DivU32(38, 11), E::LtU32(41, 9), E::AndBool(29, 33), E::AndBool(43, 35), E::AndBool(37, 40), E::AndBool(45, 42), E::AndBool(44, 46), E::MulU32(24, 6), E::AddU32(70, 25), E::MulU32(49, 8), E::AddU32(50, 34), E::MulU32(51, 9), E::AddU32(52, 41), E::MulU32(25, 7), E::AddU32(54, 69), E::MulU32(55, 10), E::AddU32(56, 26), E::MulU32(57, 10), E::AddU32(58, 27), E::LoadF32 {
binding: 0,
address: 53,
},
E::LoadF32 {
binding: 1,
address: 59,
},
E::MulF32(60, 61), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(63, 62), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 5 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 18 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 66,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 36,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 23,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 48,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 4,
},
TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Push(1),
body: vec![TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 44,
body: vec![TileOp::LaneLoopRange {
start: 65,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 46,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 64,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 63,
},
],
)
}
pub(in crate::runtime) fn conv_transpose_2d_bias_add() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::MulU32(7, 8), E::MulU32(5, 6), E::MulU32(10, 9), E::GeU32(4, 11), E::DivU32(4, 9), E::ModU32(13, 6), E::LoadF32 {
binding: 1,
address: 14,
},
E::LoadF32 {
binding: 0,
address: 4,
},
E::AddF32(16, 15), ];
Self::flat_program(
vec![Binding::ReadWrite(0), Binding::Input(1)],
vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 12 },
TileOp::StoreScalar {
binding: 0,
address: 4,
value: 17,
},
],
)
}
pub(in crate::runtime) fn conv_transpose_2d_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::MulU32(5, 6), E::MulU32(15, 8), E::MulU32(16, 9), E::GeU32(4, 17), E::ModU32(4, 9), E::DivU32(4, 9), E::ModU32(20, 8), E::DivU32(20, 8), E::ModU32(22, 6), E::DivU32(22, 6), E::LoopVar(2), E::LoopVar(3), E::LoopVar(5), E::MulU32(21, 11), E::AddU32(60, 26), E::GeU32(29, 12), E::SubU32(29, 12), E::LtU32(31, 13), E::MulU32(19, 11), E::AddU32(61, 27), E::GeU32(34, 12), E::SubU32(34, 12), E::LtU32(36, 14), E::AndBool(30, 32), E::AndBool(35, 37), E::AndBool(38, 39), E::MulU32(24, 7), E::AddU32(62, 25), E::MulU32(42, 13), E::AddU32(43, 31), E::MulU32(44, 14), E::AddU32(45, 36), E::MulU32(23, 7), E::AddU32(63, 25), E::MulU32(48, 10), E::AddU32(49, 26), E::MulU32(50, 10), E::AddU32(51, 27), E::LoadF32 {
binding: 0,
address: 46,
},
E::LoadF32 {
binding: 1,
address: 52,
},
E::MulF32(53, 54), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(56, 55), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 5 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 18 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 59,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 33,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 41,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 47,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 4,
},
TileOp::LaneLoopRange {
start: 58,
step: 1,
extent: Extent::Push(2),
body: vec![TileOp::LaneLoopRange {
start: 58,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 38,
body: vec![TileOp::LaneLoopRange {
start: 58,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 39,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 57,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 56,
},
],
)
}
pub(in crate::runtime) fn conv_transpose_2d_parameter_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::Push(9), E::Push(10), E::GeU32(4, 15), E::MulU32(6, 7), E::MulU32(17, 10), E::MulU32(18, 10), E::GeU32(4, 19), E::LtU32(4, 19), E::SubU32(4, 19), E::LoopVar(3), E::LoopVar(4), E::LoopVar(5), E::MulU32(23, 7), E::AddU32(26, 72), E::MulU32(27, 13), E::AddU32(28, 24), E::MulU32(29, 14), E::AddU32(30, 25), E::LoadF32 {
binding: 1,
address: 31,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(33, 32), E::ModU32(4, 10), E::DivU32(4, 10), E::ModU32(36, 10), E::DivU32(36, 10), E::ModU32(38, 7), E::DivU32(38, 7), E::MulU32(24, 11), E::AddU32(41, 74), E::GeU32(42, 12), E::SubU32(42, 12), E::LtU32(44, 13), E::MulU32(78, 11), E::AddU32(46, 73), E::GeU32(47, 12), E::SubU32(47, 12), E::LtU32(49, 14), E::AndBool(43, 45), E::AndBool(48, 50), E::AndBool(51, 52), E::MulU32(23, 6), E::AddU32(54, 76), E::MulU32(55, 8), E::AddU32(56, 24), E::MulU32(57, 9), E::AddU32(58, 78), E::MulU32(23, 7), E::AddU32(60, 75), E::MulU32(61, 13), E::AddU32(62, 44), E::MulU32(63, 14), E::AddU32(64, 49), E::LoadF32 {
binding: 0,
address: 59,
},
E::LoadF32 {
binding: 1,
address: 65,
},
E::MulF32(66, 67), E::AddF32(33, 68), E::ConstU32(0), E::ConstF32(0), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, E::RegisterU32 { tile: 1, index: 5 }, E::LoopVar(6), ];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Output(0),
Binding::Output(1),
],
vec![
"batch_size".into(),
"input_channels".into(),
"output_channels".into(),
"input_height".into(),
"input_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
"output_height".into(),
"output_width".into(),
"parameter_count".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 6 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 16 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 71,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 22,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 35,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 37,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 39,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 40,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 5,
value: 4,
},
TileOp::LaneIf {
cond: 20,
body: vec![
TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(8),
body: vec![TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(9),
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 34,
}],
}],
}],
},
TileOp::StoreScalar {
binding: 3,
address: 72,
value: 33,
},
],
},
TileOp::LaneIf {
cond: 21,
body: vec![
TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(0),
body: vec![TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(3),
body: vec![TileOp::LaneIf {
cond: 51,
body: vec![TileOp::LaneLoopRange {
start: 70,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 52,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 69,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 77,
value: 33,
},
],
},
],
)
}
pub(in crate::runtime) fn conv_1d_bias_transpose() -> Self {
Self::conv_1d_bias_transpose_program(ConvTailStore::Plain)
}
pub(in crate::runtime) fn conv_1d_bias_transpose_relu() -> Self {
Self::conv_1d_bias_transpose_program(ConvTailStore::Relu)
}
pub(in crate::runtime) fn conv_1d_bias_transpose_residual_relu() -> Self {
Self::conv_1d_bias_transpose_program(ConvTailStore::ResidualRelu)
}
fn conv_1d_bias_transpose_program(store: ConvTailStore) -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::Lane,
E::ConstU32(32),
E::DivU32(0, 1), E::ModU32(0, 1), E::GroupX,
E::GroupY,
E::GroupZ, E::Push(0), E::Push(1), E::Push(2), E::MulU32(5, 1), E::AddU32(10, 2), E::MulU32(4, 1), E::AddU32(12, 3), E::LtU32(11, 8), E::LtU32(13, 9), E::AndBool(14, 15), E::MulU32(8, 9), E::MulU32(6, 17), E::MulU32(11, 9), E::AddU32(19, 13), E::AddU32(18, 20), E::LoadF32 {
binding: 0,
address: 21,
},
E::ConstF32(0), E::ConstU32(33), E::MulU32(2, 24), E::AddU32(25, 3), E::AddU32(12, 2), E::AddU32(10, 3), E::LtU32(27, 9), E::LtU32(28, 8), E::AndBool(29, 30), E::MulU32(27, 8), E::AddU32(32, 28), E::AddU32(18, 33), E::MulU32(3, 24), E::AddU32(35, 2), E::SharedF32 {
tile: 0,
offset: 36,
},
E::LoadF32 {
binding: 1,
address: 13,
}, E::AddF32(22, 38), E::MaxF32(37, 23),
E::LoadF32 {
binding: 3,
address: 34,
}, E::AddF32(40, 41), ];
let mut bindings = vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)];
if store == ConvTailStore::ResidualRelu {
bindings.push(Binding::Input(2));
bindings.push(Binding::Output(1));
}
let epilogue = match store {
ConvTailStore::Plain => vec![TileOp::StoreScalar {
binding: 2,
address: 34,
value: 37,
}],
ConvTailStore::Relu => vec![TileOp::StoreScalar {
binding: 2,
address: 34,
value: 40,
}],
ConvTailStore::ResidualRelu => vec![
TileOp::StoreScalar {
binding: 2,
address: 34,
value: 40,
},
TileOp::StoreScalar {
binding: 4,
address: 34,
value: 42,
},
],
};
Self {
bindings,
push_scalars: vec!["batch_size".into(), "rows".into(), "columns".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 1024,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![TileDecl::Shared {
rows: 32,
columns: 32,
stride: 33,
}],
lane_exprs,
ops: vec![
TileOp::StoreShared {
tile: 0,
offset: 26,
value: 23,
},
TileOp::LaneIf {
cond: 16,
body: vec![TileOp::StoreShared {
tile: 0,
offset: 26,
value: 39,
}],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 31,
body: epilogue,
},
],
}
}
pub(in crate::runtime) fn conv_1d_bias_copy() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::MulU32(5, 6), E::GeU32(4, 7), E::ModU32(4, 6), E::LoadF32 {
binding: 1,
address: 9,
},
E::LoadF32 {
binding: 0,
address: 4,
},
E::AddF32(11, 10), ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec!["rows".into(), "columns".into()],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 12,
},
],
)
}
pub(in crate::runtime) fn conv_1d_bias_transpose_norm(relu: bool) -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX, E::MulU32(0, 2), E::MulU32(3, 1), E::Induction, E::AddU32(5, 6), E::LoadF32 {
binding: 0,
address: 7,
},
E::LoadF32 {
binding: 1,
address: 6,
}, E::AddF32(8, 9), E::ConstF32(0), E::Lane,
E::RegisterF32 { tile: 1, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 14,
}, E::F32FromU32(1), E::DivF32(15, 16), E::RegisterF32 { tile: 2, index: 0 }, E::SubF32(10, 18), E::MulF32(19, 19), E::DivF32(15, 16), E::PushF32(0), E::AddF32(21, 22), E::RsqrtF32(23), E::RegisterF32 { tile: 3, index: 0 }, E::LoopVar(2), E::DivU32(3, 2), E::MulU32(27, 1), E::MulU32(28, 2), E::ModU32(3, 2), E::AddU32(29, 30), E::MulU32(26, 2), E::AddU32(31, 32), E::AddU32(5, 26), E::LoadF32 {
binding: 0,
address: 34,
},
E::LoadF32 {
binding: 1,
address: 26,
},
E::AddF32(35, 36), E::SubF32(37, 18), E::MulF32(38, 25), E::LoadF32 {
binding: 2,
address: 26,
}, E::MulF32(39, 40), E::LoadF32 {
binding: 3,
address: 26,
}, E::AddF32(41, 42), E::MaxF32(43, 11), ];
let output_value = if relu { 44 } else { 43 };
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec!["batch".into(), "channels".into(), "sequence_length".into()],
push_f32_scalars: vec!["epsilon".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 256,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 256,
stride: 256,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 4 },
TileOp::LaneFold {
acc: 1,
init: 11,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 10,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 12,
value: 13,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 17,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 1,
init: 11,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 20,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 12,
value: 13,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 24,
},
TileOp::LaneLoopRange {
start: 12,
step: 256,
extent: Extent::Push(1),
body: vec![
TileOp::StoreScalar {
binding: 4,
address: 33,
value: output_value,
},
TileOp::StoreScalar {
binding: 5,
address: 33,
value: 37,
},
],
},
],
}
}
pub(in crate::runtime) fn avg_pool_2d() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(11, 12), E::GeU32(2, 20),
E::GeU32(7, 15),
E::GeU32(10, 16),
E::OrBool(21, 22),
E::OrBool(24, 23), E::DivU32(2, 12), E::ModU32(2, 12), E::MulU32(7, 18), E::MulU32(10, 18), E::LoopVar(2), E::AddU32(67, 30), E::GeU32(31, 19), E::SubU32(31, 19), E::LtU32(33, 13), E::AndBool(32, 34), E::LoopVar(4), E::AddU32(68, 36), E::GeU32(37, 19), E::SubU32(37, 19), E::LtU32(39, 14), E::AndBool(38, 40), E::AndBool(35, 41), E::MulU32(26, 12), E::AddU32(43, 27), E::MulU32(33, 14), E::AddU32(45, 39), E::AddU32(69, 46), E::AddU32(47, 55), E::LoadF32 {
binding: 0,
address: 48,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(50, 49), E::RegisterU32 { tile: 1, index: 0 }, E::ConstU32(1),
E::AddU32(52, 53), E::ConstU32(0),
E::LtU32(55, 52), E::F32FromU32(52), E::DivF32(50, 57), E::ConstF32(0),
E::SelectF32 {
cond: 56,
if_true: 58,
if_false: 59,
}, E::MulU32(44, 15), E::AddU32(61, 7), E::MulU32(62, 16), E::AddU32(63, 10), E::MulU32(44, 13), E::MulU32(65, 14), E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 25 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 59,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 55,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 29,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 66,
},
TileOp::LaneLoopRange {
start: 55,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneIf {
cond: 35,
body: vec![TileOp::LaneLoopRange {
start: 55,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneIf {
cond: 41,
body: vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 51,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 54,
},
],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 1,
address: 64,
value: 60,
},
],
)
}
pub(in crate::runtime) fn max_pool_2d() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(11, 12), E::GeU32(2, 20),
E::GeU32(7, 15),
E::GeU32(10, 16),
E::OrBool(21, 22),
E::OrBool(24, 23), E::DivU32(2, 12), E::ModU32(2, 12), E::MulU32(7, 18), E::MulU32(10, 18), E::LoopVar(2), E::AddU32(61, 30), E::GeU32(31, 19), E::SubU32(31, 19), E::LtU32(33, 13), E::AndBool(32, 34), E::LoopVar(4), E::AddU32(62, 36), E::GeU32(37, 19), E::SubU32(37, 19), E::LtU32(39, 14), E::AndBool(38, 40), E::AndBool(35, 41), E::MulU32(26, 12), E::AddU32(43, 27), E::MulU32(33, 14), E::AddU32(45, 39), E::AddU32(63, 46), E::AddU32(47, 53), E::LoadF32 {
binding: 0,
address: 48,
},
E::RegisterF32 { tile: 0, index: 0 }, E::GtF32(49, 50), E::RegisterU32 { tile: 1, index: 0 }, E::ConstU32(0),
E::ConstF32(0xff7fffff), E::MulU32(44, 15), E::AddU32(55, 7), E::MulU32(56, 16), E::AddU32(57, 10), E::MulU32(44, 13), E::MulU32(59, 14), E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0), Binding::Output(1)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 25 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 54,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 53,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 29,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 60,
},
TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneIf {
cond: 35,
body: vec![TileOp::LaneLoopRange {
start: 53,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneIf {
cond: 41,
body: vec![TileOp::LaneIf {
cond: 51,
body: vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 49,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 48,
},
],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 1,
address: 58,
value: 50,
},
TileOp::StoreScalarU32 {
binding: 2,
address: 58,
value: 52,
},
],
)
}
pub(in crate::runtime) fn adaptive_avg_pool_2d() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::MulU32(11, 12), E::GeU32(2, 17),
E::GeU32(7, 15),
E::GeU32(10, 16),
E::OrBool(18, 19),
E::OrBool(21, 20), E::DivU32(2, 12), E::ModU32(2, 12), E::ConstU32(1),
E::MulU32(7, 13), E::DivU32(26, 15), E::MulU32(10, 14), E::DivU32(28, 16), E::AddU32(7, 25), E::MulU32(30, 13), E::AddU32(31, 15), E::SubU32(32, 25), E::DivU32(33, 15), E::AddU32(10, 25), E::MulU32(35, 14), E::AddU32(36, 16), E::SubU32(37, 25), E::DivU32(38, 16), E::LoopVar(2), E::LoopVar(3), E::MulU32(23, 12), E::AddU32(42, 24), E::MulU32(40, 14), E::AddU32(44, 41), E::AddU32(63, 45), E::AddU32(63, 45), E::LoadF32 {
binding: 0,
address: 47,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(49, 48), E::SubU32(34, 27), E::SubU32(39, 29), E::MulU32(51, 52), E::F32FromU32(53), E::DivF32(49, 54), E::MulU32(43, 15), E::AddU32(56, 7), E::MulU32(57, 16), E::AddU32(58, 10), E::ConstF32(0), E::MulU32(43, 13), E::MulU32(61, 14), E::RegisterU32 { tile: 1, index: 0 }, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 22 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 60,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 62,
},
TileOp::LaneLoopRange {
start: 27,
step: 1,
extent: Extent::Expr(34),
body: vec![TileOp::LaneLoopRange {
start: 29,
step: 1,
extent: Extent::Expr(39),
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 50,
}],
}],
},
TileOp::StoreScalar {
binding: 1,
address: 59,
value: 55,
},
],
)
}
pub(in crate::runtime) fn avg_pool_2d_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(11, 12), E::GeU32(2, 20),
E::GeU32(7, 13),
E::GeU32(10, 14),
E::OrBool(21, 22),
E::OrBool(24, 23), E::DivU32(2, 12), E::ModU32(2, 12), E::AddU32(7, 19), E::AddU32(10, 19), E::LoopVar(2), E::MulU32(30, 18), E::GeU32(77, 31), E::AddU32(31, 17), E::LtU32(77, 33), E::AndBool(32, 34), E::LoopVar(4), E::MulU32(36, 18), E::GeU32(78, 37), E::AddU32(37, 17), E::LtU32(78, 39), E::AndBool(38, 40), E::AndBool(35, 41), E::LoopVar(6), E::AddU32(31, 43), E::GeU32(44, 19), E::SubU32(44, 19), E::LtU32(46, 13), E::AndBool(45, 47), E::LoopVar(7), E::AddU32(37, 49), E::GeU32(50, 19), E::SubU32(50, 19), E::LtU32(52, 14), E::AndBool(51, 53), E::AndBool(48, 54), E::RegisterU32 { tile: 1, index: 0 }, E::ConstU32(1),
E::AddU32(56, 57), E::MulU32(26, 12), E::AddU32(59, 27), E::MulU32(60, 15), E::AddU32(61, 30), E::MulU32(62, 16), E::AddU32(81, 36), E::LoadF32 {
binding: 0,
address: 64,
},
E::F32FromU32(56), E::DivF32(65, 66), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(68, 67), E::ConstF32(0), E::MulU32(60, 13), E::AddU32(71, 7), E::MulU32(72, 14), E::AddU32(73, 10), E::ConstU32(0),
E::MulU32(30, 16), E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::RegisterU32 { tile: 1, index: 4 }, E::AddU32(80, 76), E::MulU32(61, 16), ];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 5 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 25 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 70,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 29,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 74,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 4,
value: 82,
},
TileOp::LaneLoopRange {
start: 75,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 35,
body: vec![TileOp::LaneLoopRange {
start: 75,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 41,
body: vec![
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 75,
},
TileOp::LaneLoopRange {
start: 75,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneLoopRange {
start: 75,
step: 1,
extent: Extent::Push(6),
body: vec![TileOp::LaneIf {
cond: 55,
body: vec![TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 58,
}],
}],
}],
},
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 69,
},
],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 1,
address: 79,
value: 68,
},
],
)
}
pub(in crate::runtime) fn max_pool_2d_backward() -> Self {
Self::max_pool_2d_backward_program(false)
}
pub(in crate::runtime) fn max_pool_2d_relu_backward() -> Self {
Self::max_pool_2d_backward_program(true)
}
fn max_pool_2d_backward_program(relu: bool) -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::Push(6), E::Push(7), E::Push(8), E::MulU32(11, 12), E::GeU32(2, 20),
E::GeU32(7, 13),
E::GeU32(10, 14),
E::OrBool(21, 22),
E::OrBool(24, 23), E::DivU32(2, 12), E::ModU32(2, 12), E::AddU32(7, 19), E::AddU32(10, 19), E::LoopVar(2), E::MulU32(30, 18), E::GeU32(64, 31), E::AddU32(31, 17), E::LtU32(64, 33), E::AndBool(32, 34), E::LoopVar(4), E::MulU32(36, 18), E::GeU32(65, 37), E::AddU32(37, 17), E::LtU32(65, 39), E::AndBool(38, 40), E::AndBool(35, 41), E::MulU32(26, 12), E::AddU32(43, 27), E::MulU32(44, 15), E::AddU32(45, 30), E::MulU32(46, 16), E::AddU32(68, 36),
E::BufferU32 {
binding: 0,
index: 48,
}, E::MulU32(44, 13), E::AddU32(50, 7), E::MulU32(51, 14), E::AddU32(52, 10), E::EqU32(49, 66), E::LoadF32 {
binding: 1,
address: 48,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(56, 55), E::ConstF32(0), E::ConstU32(0),
E::LoadF32 {
binding: 2,
address: 66,
}, E::GtF32(60, 58),
E::SelectF32 {
cond: 61,
if_true: 56,
if_false: 58,
}, E::MulU32(30, 16), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::AddU32(67, 63), E::MulU32(45, 16), ];
let bindings = if relu {
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
]
} else {
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)]
};
let store = if relu { 3 } else { 2 };
let value = if relu { 62 } else { 56 };
Self::flat_program(
bindings,
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"kernel_size".into(),
"stride".into(),
"padding".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 25 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 58,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 29,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 53,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 69,
},
TileOp::LaneLoopRange {
start: 59,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 35,
body: vec![TileOp::LaneLoopRange {
start: 59,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 41,
body: vec![TileOp::LaneIf {
cond: 54,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 57,
}],
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: store,
address: 66,
value,
},
],
)
}
pub(in crate::runtime) fn adaptive_avg_pool_2d_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::GroupY,
E::GroupZ, E::Lane,
E::ConstU32(16),
E::ModU32(3, 4), E::MulU32(0, 4),
E::AddU32(6, 5), E::DivU32(3, 4), E::MulU32(1, 4),
E::AddU32(9, 8), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::Push(4), E::Push(5), E::MulU32(11, 12), E::GeU32(2, 17),
E::GeU32(7, 13),
E::GeU32(10, 14),
E::OrBool(18, 19),
E::OrBool(21, 20), E::DivU32(2, 12), E::ModU32(2, 12), E::LoopVar(2), E::MulU32(25, 13), E::DivU32(26, 15), E::GeU32(70, 27), E::ConstU32(1),
E::AddU32(25, 29), E::MulU32(30, 13), E::AddU32(31, 15), E::SubU32(32, 29), E::DivU32(33, 15), E::LtU32(70, 34), E::AndBool(28, 35), E::LoopVar(4), E::MulU32(37, 14), E::DivU32(38, 16), E::GeU32(71, 39), E::AddU32(37, 29), E::MulU32(41, 14), E::AddU32(42, 16), E::SubU32(43, 29), E::DivU32(44, 16), E::LtU32(71, 45), E::AndBool(40, 46), E::AndBool(36, 47), E::SubU32(34, 27), E::SubU32(45, 39), E::MulU32(49, 50), E::F32FromU32(51), E::MulU32(23, 12), E::AddU32(53, 24), E::MulU32(54, 15), E::AddU32(55, 25), E::MulU32(56, 16), E::AddU32(74, 37), E::LoadF32 {
binding: 0,
address: 58,
},
E::DivF32(59, 52), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(61, 60), E::ConstF32(0), E::MulU32(54, 13), E::AddU32(64, 7), E::MulU32(65, 14), E::AddU32(66, 10), E::ConstU32(0),
E::MulU32(25, 16), E::RegisterU32 { tile: 1, index: 0 }, E::RegisterU32 { tile: 1, index: 1 }, E::RegisterU32 { tile: 1, index: 2 }, E::RegisterU32 { tile: 1, index: 3 }, E::AddU32(73, 69), E::MulU32(55, 16), ];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 22 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 63,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 0,
value: 7,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 1,
value: 10,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 2,
value: 67,
},
TileOp::AssignScalarU32 {
tile: 1,
index: 3,
value: 75,
},
TileOp::LaneLoopRange {
start: 68,
step: 1,
extent: Extent::Push(4),
body: vec![TileOp::LaneIf {
cond: 36,
body: vec![TileOp::LaneLoopRange {
start: 68,
step: 1,
extent: Extent::Push(5),
body: vec![TileOp::LaneIf {
cond: 47,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 62,
}],
}],
}],
}],
},
TileOp::StoreScalar {
binding: 1,
address: 72,
value: 61,
},
],
)
}
pub(in crate::runtime) fn split_heads() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::MulU32(5, 6),
E::MulU32(9, 7),
E::MulU32(10, 8), E::GeU32(4, 11), E::ModU32(4, 8), E::DivU32(4, 8), E::ModU32(14, 6), E::DivU32(14, 6), E::ModU32(16, 7), E::DivU32(16, 7), E::MulU32(18, 6), E::AddU32(19, 15), E::MulU32(20, 7), E::AddU32(21, 17), E::MulU32(22, 8), E::AddU32(23, 13), E::LoadF32 {
binding: 0,
address: 24,
},
];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch".into(),
"sequence_length".into(),
"num_heads".into(),
"head_dim".into(),
],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 12 },
TileOp::StoreScalar {
binding: 1,
address: 4,
value: 25,
},
],
)
}
pub(in crate::runtime) fn merge_heads() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::MulU32(5, 6),
E::MulU32(9, 7),
E::MulU32(10, 8), E::GeU32(4, 11), E::ModU32(4, 8), E::DivU32(4, 8), E::ModU32(14, 7), E::DivU32(14, 7), E::ModU32(16, 6), E::DivU32(16, 6), E::MulU32(18, 7), E::AddU32(19, 15), E::MulU32(20, 6), E::AddU32(21, 17), E::MulU32(22, 8), E::AddU32(23, 13), E::LoadF32 {
binding: 0,
address: 24,
},
];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch".into(),
"sequence_length".into(),
"num_heads".into(),
"head_dim".into(),
],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 12 },
TileOp::StoreScalar {
binding: 1,
address: 4,
value: 25,
},
],
)
}
pub(in crate::runtime) fn sdpa_softmax(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"sdpa_softmax lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::PushF32(0), E::GroupX, E::MulU32(5, 1), E::ModU32(5, 1), E::Induction, E::AddU32(33, 8), E::LoadF32 {
binding: 0,
address: 9,
}, E::MulF32(10, 4), E::LoadF32 {
binding: 1,
address: 9,
}, E::AddF32(11, 12), E::ConstU32(0),
E::Lane,
E::NeqU32(2, 14), E::AddF32(11, 35), E::NeqU32(3, 14), E::LtU32(34, 8), E::AndBool(18, 19), E::ConstF32(0xff7f_ffff),
E::SelectF32 {
cond: 20,
if_true: 21,
if_false: 17,
}, E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::SubF32(22, 24), E::ExpF32(25),
E::SharedF32 {
tile: 0,
offset: 14,
}, E::RegisterF32 { tile: 3, index: 0 }, E::MulF32(26, 28), E::ConstF32(0x0000_0000), E::ConstF32(0x3f80_0000), E::DivF32(31, 27), E::RegisterU32 { tile: 4, index: 0 }, E::RegisterU32 { tile: 4, index: 1 },
E::SelectF32 {
cond: 16,
if_true: 12,
if_false: 30,
}, ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec![
"rows".into(),
"columns".into(),
"has_mask".into(),
"causal".into(),
],
push_f32_scalars: vec!["scale".into()],
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 2 },
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::AssignScalarU32 {
tile: 4,
index: 0,
value: 6,
},
TileOp::AssignScalarU32 {
tile: 4,
index: 1,
value: 7,
},
TileOp::LaneFold {
acc: 1,
init: 21,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 22,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 23,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 27,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 1,
init: 30,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 26,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 23,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 32,
},
TileOp::Barrier,
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 2,
address: 9,
value: 29,
}],
},
],
})
}
pub(in crate::runtime) fn small_causal_sdpa() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX, E::Push(0), E::Push(1), E::Push(2), E::Push(3), E::PushF32(0), E::MulU32(1, 3), E::DivU32(0, 3), E::ModU32(0, 3), E::MulU32(2, 4), E::ConstU32(128),
E::MinU32(2, 10), E::ConstU32(256),
E::DivU32(12, 11), E::Lane,
E::DivU32(14, 11), E::ModU32(14, 11), E::MulU32(15, 11), E::MulU32(0, 9), E::MulU32(0, 2), E::Induction,
E::ModU32(20, 4), E::DivU32(20, 4), E::MulU32(111, 2), E::AddU32(23, 22),
E::MulU32(24, 3),
E::AddU32(25, 112),
E::MulU32(26, 4),
E::AddU32(27, 21), E::LoadF32 {
binding: 0,
address: 28,
},
E::LoadF32 {
binding: 1,
address: 28,
},
E::LoadF32 {
binding: 2,
address: 28,
},
E::AddU32(18, 20), E::ConstU32(0),
E::ConstU32(1),
E::ConstF32(0x0000_0000),
E::ConstF32(0x3f80_0000),
E::ConstF32(0xff7f_ffff), E::MulU32(20, 114), E::AddU32(38, 115), E::LtU32(39, 2), E::LtU32(58, 2), E::AndBool(40, 41), E::MulU32(39, 4), E::MulU32(58, 4), E::AddU32(19, 39), E::MulU32(45, 2), E::MulU32(45, 4), E::AddU32(23, 39),
E::MulU32(48, 3),
E::AddU32(49, 8),
E::MulU32(50, 4), E::AddU32(2, 114),
E::SubU32(52, 34),
E::DivU32(53, 114), E::AddU32(4, 113),
E::SubU32(55, 34),
E::DivU32(56, 113), E::RegisterU32 { tile: 10, index: 6 }, E::RegisterU32 { tile: 10, index: 7 }, E::RegisterU32 {
tile: 10,
index: 10,
}, E::RegisterU32 {
tile: 10,
index: 11,
}, E::RegisterU32 {
tile: 10,
index: 12,
}, E::RegisterU32 {
tile: 10,
index: 13,
}, E::RegisterU32 {
tile: 10,
index: 14,
}, E::RegisterU32 {
tile: 10,
index: 15,
}, E::RegisterU32 {
tile: 10,
index: 16,
}, E::AddU32(59, 58), E::AddU32(59, 20), E::SharedF32 {
tile: 3,
offset: 68,
},
E::AddU32(61, 20), E::SharedF32 {
tile: 0,
offset: 70,
},
E::AddU32(62, 20), E::SharedF32 {
tile: 1,
offset: 72,
},
E::MulF32(71, 73), E::AddU32(63, 58), E::RegisterF32 { tile: 4, index: 0 }, E::MulF32(76, 5), E::LtU32(60, 58),
E::SelectF32 {
cond: 78,
if_true: 37,
if_false: 77,
}, E::SharedF32 {
tile: 3,
offset: 59,
}, E::RegisterF32 { tile: 6, index: 0 }, E::MaxF32(81, 69),
E::SubF32(69, 81),
E::ExpF32(83),
E::RegisterF32 { tile: 7, index: 0 }, E::AddF32(85, 84),
E::DivF32(36, 85), E::RegisterF32 { tile: 8, index: 0 }, E::MulF32(69, 88), E::AddU32(63, 20), E::MulU32(20, 113), E::AddU32(58, 91), E::MulU32(20, 4), E::AddU32(93, 66), E::SharedF32 {
tile: 2,
offset: 94,
},
E::MulF32(69, 95), E::RegisterF32 { tile: 9, index: 0 }, E::AddU32(64, 66), E::AddU32(65, 66), E::RegisterF32 { tile: 5, index: 0 }, E::EqU32(58, 33), E::AndBool(101, 40), E::LtU32(66, 4), E::AddF32(85, 100), E::RegisterU32 {
tile: 10,
index: 17,
}, E::RegisterU32 {
tile: 10,
index: 18,
}, E::RegisterF32 { tile: 11, index: 0 }, E::LoadF32 {
binding: 0,
address: 105,
},
E::LoadF32 {
binding: 1,
address: 105,
},
E::LoadF32 {
binding: 2,
address: 105,
},
E::RegisterU32 { tile: 10, index: 0 }, E::RegisterU32 { tile: 10, index: 1 }, E::RegisterU32 { tile: 10, index: 3 }, E::RegisterU32 { tile: 10, index: 4 }, E::RegisterU32 { tile: 10, index: 5 }, ];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
Binding::Output(3),
Binding::Output(4),
Binding::Output(5),
Binding::Output(6),
],
push_scalars: vec![
"batch".into(),
"sequence_length".into(),
"num_heads".into(),
"head_dim".into(),
],
push_f32_scalars: vec!["scale".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 256,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 2048,
stride: 2048,
},
TileDecl::Shared {
rows: 1,
columns: 2048,
stride: 2048,
},
TileDecl::Shared {
rows: 1,
columns: 2048,
stride: 2048,
},
TileDecl::Shared {
rows: 1,
columns: 1024,
stride: 1024,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 19 },
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 6 },
TileOp::AssignScalarU32 {
tile: 10,
index: 0,
value: 7,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 1,
value: 8,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 2,
value: 9,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 3,
value: 11,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 4,
value: 13,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 5,
value: 15,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 6,
value: 16,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 7,
value: 17,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 8,
value: 18,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 9,
value: 19,
},
TileOp::LaneLoop {
extent: Extent::Expr(9),
body: vec![
TileOp::AssignScalarU32 {
tile: 10,
index: 17,
value: 28,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 18,
value: 32,
},
TileOp::AssignScalar {
tile: 11,
index: 0,
value: 108,
},
TileOp::StoreShared {
tile: 0,
offset: 20,
value: 107,
},
TileOp::StoreScalar {
binding: 3,
address: 106,
value: 107,
},
TileOp::AssignScalar {
tile: 11,
index: 0,
value: 109,
},
TileOp::StoreShared {
tile: 1,
offset: 20,
value: 107,
},
TileOp::StoreScalar {
binding: 4,
address: 106,
value: 107,
},
TileOp::AssignScalar {
tile: 11,
index: 0,
value: 110,
},
TileOp::StoreShared {
tile: 2,
offset: 20,
value: 107,
},
TileOp::StoreScalar {
binding: 5,
address: 106,
value: 107,
},
],
},
TileOp::Barrier,
TileOp::LaneLoopRange {
start: 33,
step: 1,
extent: Extent::Expr(54),
body: vec![
TileOp::AssignScalarU32 {
tile: 10,
index: 10,
value: 39,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 11,
value: 43,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 12,
value: 44,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 13,
value: 46,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 14,
value: 47,
},
TileOp::AssignScalarU32 {
tile: 10,
index: 15,
value: 51,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 37,
},
TileOp::LaneIf {
cond: 42,
body: vec![
TileOp::LaneFold {
acc: 4,
init: 35,
extent: Extent::Push(3),
op: LaneReduce::Sum,
element: 74,
serial: true,
},
TileOp::StoreScalar {
binding: 6,
address: 75,
value: 76,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 79,
},
],
},
TileOp::StoreShared {
tile: 3,
offset: 67,
value: 100,
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 102,
body: vec![
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 80,
},
TileOp::LaneLoopRange {
start: 34,
step: 1,
extent: Extent::Push(1),
body: vec![TileOp::AssignScalar {
tile: 6,
index: 0,
value: 82,
}],
},
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 35,
},
TileOp::LaneLoopRange {
start: 33,
step: 1,
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 84,
},
TileOp::StoreShared {
tile: 3,
offset: 68,
value: 100,
},
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 104,
},
],
},
TileOp::AssignScalar {
tile: 8,
index: 0,
value: 87,
},
TileOp::LaneLoopRange {
start: 33,
step: 1,
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 89,
},
TileOp::StoreShared {
tile: 3,
offset: 68,
value: 100,
},
TileOp::StoreScalar {
binding: 7,
address: 90,
value: 100,
},
],
},
],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 40,
body: vec![TileOp::LaneLoopRange {
start: 33,
step: 1,
extent: Extent::Expr(57),
body: vec![
TileOp::AssignScalarU32 {
tile: 10,
index: 16,
value: 92,
},
TileOp::LaneIf {
cond: 103,
body: vec![
TileOp::LaneFold {
acc: 9,
init: 35,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 96,
serial: true,
},
TileOp::StoreScalar {
binding: 8,
address: 98,
value: 97,
},
TileOp::StoreScalar {
binding: 9,
address: 99,
value: 97,
},
],
},
],
}],
},
TileOp::Barrier,
],
},
],
}
}
pub(in crate::runtime) fn flash_causal() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX, E::Push(0), E::Push(1), E::Push(2), E::PushF32(0), E::MulU32(1, 2), E::DivU32(0, 2), E::MulU32(6, 2), E::SubU32(0, 7), E::MulU32(0, 3), E::MulU32(7, 3), E::Lane,
E::ConstU32(0),
E::ConstU32(1),
E::AddU32(8, 13), E::ConstF32(0xff7f_ffff), E::ConstF32(0x0000_0000), E::Induction,
E::RegisterU32 { tile: 6, index: 0 }, E::MulU32(18, 3), E::AddU32(49, 19), E::AddU32(48, 17), E::AddU32(20, 17), E::LoadF32 {
binding: 0,
address: 21,
},
E::LoadF32 {
binding: 1,
address: 22,
},
E::MulF32(23, 24), E::RegisterF32 { tile: 2, index: 0 }, E::MulF32(26, 4), E::RegisterF32 { tile: 3, index: 0 }, E::MaxF32(28, 27),
E::SharedF32 {
tile: 0,
offset: 17,
}, E::SharedF32 {
tile: 1,
offset: 12,
}, E::RegisterF32 { tile: 5, index: 0 }, E::SubF32(30, 32), E::ExpF32(33), E::RegisterF32 { tile: 4, index: 0 }, E::AddF32(35, 26), E::EqU32(11, 12), E::LogF32(31), E::AddF32(32, 38), E::MulU32(17, 3), E::AddU32(40, 18), E::AddU32(49, 41), E::LoadF32 {
binding: 2,
address: 42,
},
E::SharedF32 {
tile: 0,
offset: 17,
}, E::DivF32(44, 31), E::MulF32(45, 43), E::AddU32(48, 18), E::RegisterU32 { tile: 6, index: 1 }, E::RegisterU32 { tile: 6, index: 2 }, ];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch_heads".into(),
"sequence_length".into(),
"head_dim".into(),
],
push_f32_scalars: vec!["scale".into()],
shape: BlockShape {
lanes: 64,
thread_columns: 64,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 1024,
stride: 1024,
},
TileDecl::Shared {
rows: 1,
columns: 64,
stride: 64,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 3 },
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 5 },
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 16,
},
TileOp::AssignScalarU32 {
tile: 6,
index: 0,
value: 12,
},
TileOp::AssignScalarU32 {
tile: 6,
index: 1,
value: 9,
},
TileOp::AssignScalarU32 {
tile: 6,
index: 2,
value: 10,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 15,
},
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Expr(14),
body: vec![
TileOp::AssignScalarU32 {
tile: 6,
index: 0,
value: 17,
},
TileOp::LaneFold {
acc: 2,
init: 16,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: 25,
serial: true,
},
TileOp::StoreShared {
tile: 0,
offset: 18,
value: 27,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 29,
},
],
},
TileOp::StoreShared {
tile: 1,
offset: 11,
value: 28,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 31,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 16,
},
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Expr(14),
body: vec![
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 34,
},
TileOp::StoreShared {
tile: 0,
offset: 17,
value: 26,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 36,
},
],
},
TileOp::Barrier,
TileOp::StoreShared {
tile: 1,
offset: 11,
value: 35,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Sum,
},
TileOp::LaneIf {
cond: 37,
body: vec![TileOp::StoreScalar {
binding: 4,
address: 0,
value: 39,
}],
},
TileOp::Barrier,
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalarU32 {
tile: 6,
index: 0,
value: 17,
},
TileOp::LaneFold {
acc: 2,
init: 16,
extent: Extent::Expr(14),
op: LaneReduce::Sum,
element: 46,
serial: true,
},
TileOp::StoreScalar {
binding: 3,
address: 47,
value: 26,
},
],
},
],
}
}
pub(in crate::runtime) fn flash_causal_backward_q() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX, E::Push(0), E::Push(1), E::Push(2), E::PushF32(0), E::MulU32(1, 2), E::DivU32(0, 2), E::MulU32(6, 2), E::SubU32(0, 7), E::MulU32(0, 3), E::MulU32(7, 3), E::Lane,
E::ConstU32(0),
E::ConstU32(1),
E::AddU32(8, 13), E::ConstF32(0x0000_0000), E::Induction,
E::AddU32(53, 16), E::LoadF32 {
binding: 4,
address: 17,
}, E::LoadF32 {
binding: 3,
address: 17,
}, E::MulF32(18, 19), E::RegisterU32 { tile: 7, index: 0 }, E::MulU32(21, 3), E::AddU32(54, 22), E::AddU32(23, 16), E::LoadF32 {
binding: 0,
address: 17,
}, E::LoadF32 {
binding: 1,
address: 24,
}, E::MulF32(25, 26), E::LoadF32 {
binding: 2,
address: 24,
}, E::MulF32(18, 28), E::RegisterF32 { tile: 2, index: 0 }, E::MulF32(30, 4), E::RegisterF32 { tile: 4, index: 0 }, E::SubF32(31, 32),
E::ExpF32(33), E::RegisterF32 { tile: 3, index: 0 }, E::RegisterF32 { tile: 5, index: 0 }, E::SubF32(35, 36), E::RegisterF32 { tile: 6, index: 0 }, E::MulF32(38, 37), E::MulF32(39, 4), E::MulU32(16, 3), E::RegisterU32 { tile: 7, index: 1 }, E::AddU32(41, 42), E::AddU32(54, 43), E::LoadF32 {
binding: 1,
address: 44,
},
E::SharedF32 {
tile: 0,
offset: 16,
}, E::MulF32(46, 45), E::SharedF32 {
tile: 1,
offset: 12,
}, E::LoadF32 {
binding: 5,
address: 0,
}, E::AddU32(53, 42), E::AddF32(30, 27), E::AddF32(35, 29), E::RegisterU32 { tile: 7, index: 2 }, E::RegisterU32 { tile: 7, index: 3 }, ];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Output(0),
],
push_scalars: vec![
"batch_heads".into(),
"sequence_length".into(),
"head_dim".into(),
],
push_f32_scalars: vec!["scale".into()],
shape: BlockShape {
lanes: 64,
thread_columns: 64,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 1024,
stride: 1024,
},
TileDecl::Shared {
rows: 1,
columns: 64,
stride: 64,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 4 },
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 5 },
TileOp::AssignScalarU32 {
tile: 7,
index: 0,
value: 12,
},
TileOp::AssignScalarU32 {
tile: 7,
index: 2,
value: 9,
},
TileOp::AssignScalarU32 {
tile: 7,
index: 3,
value: 10,
},
TileOp::LaneFold {
acc: 2,
init: 15,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: 20,
serial: false,
},
TileOp::StoreShared {
tile: 1,
offset: 11,
value: 30,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 48,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 49,
},
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Expr(14),
body: vec![
TileOp::AssignScalarU32 {
tile: 7,
index: 0,
value: 16,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 15,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 15,
},
TileOp::LaneLoopRange {
start: 12,
step: 1,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 51,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 52,
},
],
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 34,
},
TileOp::StoreShared {
tile: 0,
offset: 21,
value: 40,
},
],
},
TileOp::Barrier,
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalarU32 {
tile: 7,
index: 1,
value: 16,
},
TileOp::LaneFold {
acc: 2,
init: 15,
extent: Extent::Expr(14),
op: LaneReduce::Sum,
element: 47,
serial: true,
},
TileOp::StoreScalar {
binding: 6,
address: 50,
value: 30,
},
],
},
],
}
}
pub(in crate::runtime) fn flash_causal_backward_kv() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX, E::Push(0), E::Push(1), E::Push(2), E::PushF32(0), E::MulU32(1, 2), E::DivU32(0, 2), E::MulU32(6, 2), E::SubU32(0, 7), E::MulU32(0, 3), E::MulU32(7, 3), E::Lane,
E::AddU32(8, 11), E::ConstU32(0),
E::ConstF32(0x0000_0000), E::Induction,
E::RegisterU32 { tile: 8, index: 0 }, E::MulU32(16, 3), E::AddU32(59, 17), E::AddU32(18, 15), E::LoadF32 {
binding: 0,
address: 19,
}, E::AddU32(60, 15), E::LoadF32 {
binding: 1,
address: 21,
}, E::MulF32(20, 22), E::LoadF32 {
binding: 4,
address: 19,
}, E::LoadF32 {
binding: 2,
address: 21,
}, E::MulF32(24, 25), E::LoadF32 {
binding: 3,
address: 19,
}, E::MulF32(24, 27), E::RegisterF32 { tile: 2, index: 0 }, E::MulF32(29, 4), E::AddU32(61, 16), E::LoadF32 {
binding: 5,
address: 31,
}, E::SubF32(30, 32),
E::ExpF32(33), E::RegisterF32 { tile: 5, index: 0 }, E::RegisterF32 { tile: 3, index: 0 }, E::RegisterF32 { tile: 4, index: 0 }, E::SubF32(36, 37), E::MulF32(35, 38), E::MulF32(39, 4), E::RegisterU32 { tile: 8, index: 1 }, E::MulU32(15, 3), E::AddU32(59, 42), E::AddU32(43, 41), E::LoadF32 {
binding: 0,
address: 44,
},
E::SharedF32 {
tile: 1,
offset: 15,
}, E::MulF32(46, 45), E::LoadF32 {
binding: 4,
address: 44,
}, E::SharedF32 {
tile: 0,
offset: 15,
}, E::MulF32(49, 48), E::RegisterF32 { tile: 6, index: 0 }, E::AddF32(51, 47), E::RegisterF32 { tile: 7, index: 0 }, E::AddF32(53, 50), E::AddU32(60, 41), E::AddF32(29, 23), E::AddF32(36, 26), E::AddF32(37, 28), E::RegisterU32 { tile: 8, index: 2 }, E::RegisterU32 { tile: 8, index: 3 }, E::RegisterU32 { tile: 8, index: 4 }, ];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch_heads".into(),
"sequence_length".into(),
"head_dim".into(),
],
push_f32_scalars: vec!["scale".into()],
shape: BlockShape {
lanes: 64,
thread_columns: 64,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 1024,
stride: 1024,
},
TileDecl::Shared {
rows: 1,
columns: 1024,
stride: 1024,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 5 },
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 5 },
TileOp::AssignScalarU32 {
tile: 8,
index: 0,
value: 13,
},
TileOp::AssignScalarU32 {
tile: 8,
index: 2,
value: 10,
},
TileOp::AssignScalarU32 {
tile: 8,
index: 3,
value: 9,
},
TileOp::AssignScalarU32 {
tile: 8,
index: 4,
value: 7,
},
TileOp::LaneLoopRange {
start: 12,
step: 64,
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalarU32 {
tile: 8,
index: 0,
value: 15,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 14,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 14,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 14,
},
TileOp::LaneLoopRange {
start: 13,
step: 1,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 56,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 57,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 58,
},
],
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 34,
},
TileOp::StoreShared {
tile: 0,
offset: 16,
value: 35,
},
TileOp::StoreShared {
tile: 1,
offset: 16,
value: 40,
},
],
},
TileOp::Barrier,
TileOp::LaneLoopRange {
start: 11,
step: 64,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalarU32 {
tile: 8,
index: 1,
value: 15,
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 14,
},
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 14,
},
TileOp::LaneLoopRange {
start: 8,
step: 1,
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 52,
},
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 54,
},
],
},
TileOp::StoreScalar {
binding: 6,
address: 55,
value: 51,
},
TileOp::StoreScalar {
binding: 7,
address: 55,
value: 53,
},
],
},
],
}
}
pub(in crate::runtime) fn linear_triple(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body_z(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
ops.push(TileOp::StoreTileBias {
acc: 2,
bias_binding: 4,
binding: 7,
z: true,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
bias_origin: None,
act: None,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Input(6),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_gated_act(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, mut tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
tiles.push(TileDecl::Shared {
rows: tile_columns,
columns: tile_k,
stride: tile_k + 1,
});
tiles.push(TileDecl::Register {
rows: fragment_rows,
columns: fragment_columns,
});
ops.insert(1, TileOp::ClearTile { tile: 4 });
let TileOp::Loop { body, .. } = &mut ops[2] else {
return Err(Error::internal("gemm body did not produce the K loop"));
};
body.insert(
2,
TileOp::LoadShared {
tile: 3,
binding: 1,
row_origin: Extent::GroupY(tile_columns),
column_origin: Extent::Induction,
row_extent: Extent::Push(2),
column_extent: Extent::Push(1),
vec,
row_offset: Some(Extent::HalfPush(2)),
mask_origin: None,
z: false,
column_offset: None,
transposed: false,
},
);
body.insert(
5,
TileOp::DotShared {
acc: 4,
lhs: 0,
rhs: 3,
},
);
ops.push(TileOp::StoreTileGated {
gate_acc: 2,
up_acc: 4,
bias_binding: 2,
linear_binding: 3,
act_binding: 4,
linear_stride: Extent::Push(2),
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
gate_width: Extent::HalfPush(2),
bias_origin: None,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
"act_kind".into(),
],
push_f32_scalars: vec!["alpha".into()],
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_concat(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
let TileOp::Loop { body, .. } = &mut ops[1] else {
return Err(Error::internal("gemm body did not produce the K loop"));
};
body[0] = TileOp::LoadSharedConcat {
tile: 0,
bindings: [0, 1, 2],
widths: [3, 4, 5],
materialize: 5,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::Induction,
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
};
if let TileOp::LoadShared { binding, .. } = &mut body[1] {
*binding = 3;
}
ops.push(TileOp::StoreTileBias {
acc: 2,
bias_binding: 4,
binding: 6,
z: false,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
bias_origin: None,
act: None,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
"first_width".into(),
"second_width".into(),
"third_width".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn linear_residual_norm(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, mut tiles, mut ops) = gemm_tile_body(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(1, 0, 2),
)?;
if !shape.lanes.is_multiple_of(tile_rows) {
return Err(Error::invalid_argument(
"residual_norm needs a whole number of lanes per tile row",
));
}
let lanes_per_row = shape.lanes / tile_rows;
tiles.push(TileDecl::Shared {
rows: tile_rows,
columns: tile_columns,
stride: tile_columns,
});
tiles.push(TileDecl::Shared {
rows: 1,
columns: tile_rows,
stride: tile_rows,
});
tiles.push(TileDecl::Shared {
rows: 1,
columns: tile_rows,
stride: tile_rows,
});
for _ in 0..4 {
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
});
}
let mut e = Vec::new();
macro_rules! x {
($v:expr) => {{
e.push($v);
e.len() - 1
}};
}
let lane = x!(LaneExpr::Lane);
let gx = x!(LaneExpr::GroupX);
let tr = x!(LaneExpr::ConstU32(tile_rows));
let tc = x!(LaneExpr::ConstU32(tile_columns));
let batch = x!(LaneExpr::Push(0));
let out = x!(LaneExpr::Push(2));
let row_base = x!(LaneExpr::MulU32(gx, tr));
let remain = x!(LaneExpr::SubU32(batch, row_base));
let valid = x!(LaneExpr::MinU32(tr, remain));
let stat_cond = x!(LaneExpr::LtU32(lane, valid));
let zero = x!(LaneExpr::ConstF32(0));
let ind = x!(LaneExpr::Induction);
let row_off = x!(LaneExpr::MulU32(lane, tc));
let stat_idx = x!(LaneExpr::AddU32(row_off, ind));
let stat_val = x!(LaneExpr::SharedF32 {
tile: 3,
offset: stat_idx
});
let out_f = x!(LaneExpr::F32FromU32(out));
let rsum = x!(LaneExpr::RegisterF32 { tile: 6, index: 0 });
let mean = x!(LaneExpr::DivF32(rsum, out_f));
let rmean = x!(LaneExpr::RegisterF32 { tile: 8, index: 0 });
let centered = x!(LaneExpr::SubF32(stat_val, rmean));
let sq = x!(LaneExpr::MulF32(centered, centered));
let rsq = x!(LaneExpr::RegisterF32 { tile: 7, index: 0 });
let var = x!(LaneExpr::DivF32(rsq, out_f));
let eps = x!(LaneExpr::PushF32(0));
let veps = x!(LaneExpr::AddF32(var, eps));
let inv = x!(LaneExpr::RsqrtF32(veps));
let rinv = x!(LaneExpr::RegisterF32 { tile: 9, index: 0 });
let stat_addr = x!(LaneExpr::AddU32(row_base, lane));
let lpr = x!(LaneExpr::ConstU32(lanes_per_row));
let nrow = x!(LaneExpr::DivU32(lane, lpr));
let orow = x!(LaneExpr::AddU32(row_base, nrow));
let ncond = x!(LaneExpr::LtU32(orow, batch));
let nrow_off = x!(LaneExpr::MulU32(nrow, tc));
let nidx = x!(LaneExpr::AddU32(nrow_off, ind));
let nval = x!(LaneExpr::SharedF32 {
tile: 3,
offset: nidx
});
let nmean = x!(LaneExpr::SharedF32 {
tile: 4,
offset: nrow
});
let ninv = x!(LaneExpr::SharedF32 {
tile: 5,
offset: nrow
});
let dnorm = x!(LaneExpr::SubF32(nval, nmean));
let normed = x!(LaneExpr::MulF32(dnorm, ninv));
let elrow = x!(LaneExpr::MulU32(orow, out));
let el = x!(LaneExpr::AddU32(elrow, ind));
let nw = x!(LaneExpr::LoadF32 {
binding: 4,
address: ind,
});
let nb = x!(LaneExpr::LoadF32 {
binding: 5,
address: ind,
});
let nwmul = x!(LaneExpr::MulF32(normed, nw));
let outv = x!(LaneExpr::AddF32(nwmul, nb));
let start = x!(LaneExpr::ModU32(lane, lpr));
ops.push(TileOp::StoreTileResidualNorm {
acc: 2,
bias_binding: 2,
residual_binding: 3,
linear_binding: 6,
sum_binding: 7,
tile: 3,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
});
ops.push(TileOp::Barrier);
ops.push(TileOp::LaneIf {
cond: stat_cond,
body: vec![
TileOp::LaneFold {
acc: 6,
init: zero,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: stat_val,
serial: true,
},
TileOp::AssignScalar {
tile: 8,
index: 0,
value: mean,
},
TileOp::LaneFold {
acc: 7,
init: zero,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: sq,
serial: true,
},
TileOp::AssignScalar {
tile: 9,
index: 0,
value: inv,
},
TileOp::StoreShared {
tile: 4,
offset: lane,
value: rmean,
},
TileOp::StoreShared {
tile: 5,
offset: lane,
value: rinv,
},
TileOp::StoreScalar {
binding: 10,
address: stat_addr,
value: rinv,
},
],
});
ops.push(TileOp::Barrier);
ops.push(TileOp::LaneLoopRange {
start,
step: lanes_per_row,
extent: Extent::Push(2),
body: vec![TileOp::LaneIf {
cond: ncond,
body: vec![
TileOp::StoreScalar {
binding: 9,
address: el,
value: normed,
},
TileOp::StoreScalar {
binding: 8,
address: el,
value: outv,
},
],
}],
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
Binding::Output(3),
Binding::Output(4),
],
push_scalars: vec![
"batch".into(),
"input_features".into(),
"output_features".into(),
],
push_f32_scalars: vec!["epsilon".into()],
shape,
tiles,
lane_exprs: e,
ops,
})
}
pub(in crate::runtime) fn qkv_projection_bias(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body_multi(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(2, 0, 1),
&[1, 2, 3],
)?;
for (acc, bias_binding, binding) in [(4usize, 4usize, 7usize), (5, 5, 8), (6, 6, 9)] {
ops.push(TileOp::StoreTileBias {
acc,
bias_binding,
binding,
z: false,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
bias_origin: None,
act: None,
});
}
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Input(6),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
],
push_scalars: vec!["m".into(), "n".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn gate_up_swiglu_bias(
tile_rows: u32,
tile_columns: u32,
tile_k: u32,
fragment_rows: u32,
fragment_columns: u32,
vec: u32,
) -> Result<Self> {
let (shape, tiles, mut ops) = gemm_tile_body_multi(
(
tile_rows,
tile_columns,
tile_k,
fragment_rows,
fragment_columns,
vec,
),
(2, 0, 1),
&[1, 2],
)?;
ops.push(TileOp::StoreTileGateUp {
gate_acc: 3,
up_acc: 4,
gate_bias_binding: 3,
up_bias_binding: 4,
binding: 5,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::GroupY(tile_columns),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Output(0),
],
push_scalars: vec!["m".into(), "n".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs: Vec::new(),
ops,
})
}
pub(in crate::runtime) fn softmax(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"softmax lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX, E::MulU32(0, 2), E::DivU32(3, 2), E::ModU32(3, 2), E::MulU32(5, 1), E::MulU32(7, 2), E::AddU32(8, 6), E::Induction, E::MulU32(10, 2), E::AddU32(9, 11), E::LoadF32 {
binding: 0,
address: 12,
}, E::ConstF32(0xff7f_ffff), E::Lane, E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::SubF32(13, 17), E::ExpF32(18), E::ConstF32(0x0000_0000), E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 21,
}, E::RegisterF32 { tile: 3, index: 0 }, E::MulF32(19, 23), E::ConstF32(0x3f80_0000), E::DivF32(25, 22), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 4 },
TileOp::LaneFold {
acc: 1,
init: 14,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 13,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 16,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 22,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 1,
init: 20,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 19,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 16,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 26,
},
TileOp::Barrier,
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 1,
address: 12,
value: 24,
}],
},
],
})
}
pub(in crate::runtime) fn log_softmax(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"log_softmax lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX, E::MulU32(0, 2), E::DivU32(3, 2), E::ModU32(3, 2), E::MulU32(5, 1), E::MulU32(7, 2), E::AddU32(8, 6), E::Induction, E::MulU32(10, 2), E::AddU32(9, 11), E::LoadF32 {
binding: 0,
address: 12,
}, E::ConstF32(0xff7f_ffff), E::Lane, E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::SubF32(13, 17), E::ExpF32(18), E::ConstF32(0x0000_0000), E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 21,
}, E::RegisterF32 { tile: 3, index: 0 }, E::LogF32(22), E::AddF32(17, 24), E::SubF32(13, 23), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 4 },
TileOp::LaneFold {
acc: 1,
init: 14,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 13,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 16,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 22,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 1,
init: 20,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 19,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 16,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 25,
},
TileOp::Barrier,
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 1,
address: 12,
value: 26,
}],
},
],
})
}
pub(in crate::runtime) fn softmax_backward(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"softmax_backward lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0),
E::Push(1),
E::Push(2),
E::GroupX,
E::MulU32(0, 2),
E::DivU32(3, 2),
E::ModU32(3, 2),
E::MulU32(5, 1),
E::MulU32(7, 2),
E::AddU32(8, 6),
E::Induction,
E::MulU32(10, 2),
E::AddU32(9, 11), E::LoadF32 {
binding: 0,
address: 12,
}, E::LoadF32 {
binding: 1,
address: 12,
}, E::MulF32(14, 13), E::ConstF32(0x0000_0000), E::Lane,
E::RegisterF32 { tile: 1, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 19,
}, E::RegisterF32 { tile: 2, index: 0 }, E::SubF32(14, 21), E::MulF32(13, 22), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 4 },
TileOp::LaneFold {
acc: 1,
init: 16,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 15,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 17,
value: 18,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 20,
},
TileOp::Barrier,
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 2,
address: 12,
value: 23,
}],
},
],
})
}
pub(in crate::runtime) fn log_softmax_backward(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"log_softmax_backward lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0),
E::Push(1),
E::Push(2),
E::GroupX,
E::MulU32(0, 2),
E::DivU32(3, 2),
E::ModU32(3, 2),
E::MulU32(5, 1),
E::MulU32(7, 2),
E::AddU32(8, 6),
E::Induction,
E::MulU32(10, 2),
E::AddU32(9, 11), E::LoadF32 {
binding: 1,
address: 12,
}, E::ConstF32(0x0000_0000), E::Lane,
E::RegisterF32 { tile: 1, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 17,
}, E::RegisterF32 { tile: 2, index: 0 }, E::LoadF32 {
binding: 0,
address: 12,
}, E::ExpF32(20), E::MulF32(21, 19), E::SubF32(13, 22), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 4 },
TileOp::LaneFold {
acc: 1,
init: 14,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 13,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 15,
value: 16,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 18,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 2,
address: 12,
value: 23,
}],
},
],
})
}
pub(in crate::runtime) fn rms_norm(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"rms_norm lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 0,
address: 5,
}, E::MulF32(6, 6), E::ConstF32(0x0000_0000), E::Lane, E::RegisterF32 { tile: 1, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 11,
}, E::F32FromU32(1), E::DivF32(12, 13), E::PushF32(0), E::AddF32(14, 15), E::SqrtF32(16), E::ConstF32(0x3f80_0000), E::DivF32(18, 17), E::RegisterF32 { tile: 2, index: 0 }, E::LoadF32 {
binding: 1,
address: 4,
}, E::MulF32(6, 20), E::MulF32(22, 21), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec!["rows".into(), "columns".into()],
push_f32_scalars: vec!["epsilon".into()],
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneFold {
acc: 1,
init: 8,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 7,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 9,
value: 10,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 19,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 2,
address: 5,
value: 23,
}],
},
],
})
}
pub(in crate::runtime) fn layer_norm(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"layer_norm lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 0,
address: 5,
}, E::ConstF32(0x0000_0000), E::Lane, E::RegisterF32 { tile: 2, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 10,
}, E::F32FromU32(1), E::DivF32(11, 12), E::RegisterF32 { tile: 3, index: 0 }, E::SubF32(6, 14), E::MulF32(15, 15), E::SharedF32 {
tile: 1,
offset: 10,
}, E::DivF32(17, 12), E::PushF32(0), E::AddF32(18, 19), E::RsqrtF32(20), E::RegisterF32 { tile: 4, index: 0 }, E::MulF32(15, 22), E::LoadF32 {
binding: 1,
address: 4,
}, E::MulF32(23, 24), E::LoadF32 {
binding: 2,
address: 4,
}, E::AddF32(25, 26), ];
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
],
push_scalars: vec!["rows".into(), "columns".into()],
push_f32_scalars: vec!["epsilon".into()],
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneFold {
acc: 2,
init: 7,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 6,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 8,
value: 9,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 13,
},
TileOp::LaneFold {
acc: 2,
init: 7,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 16,
serial: false,
},
TileOp::StoreShared {
tile: 1,
offset: 8,
value: 9,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 21,
},
TileOp::LaneGuard {
lane: 10,
body: vec![TileOp::StoreScalar {
binding: 5,
address: 2,
value: 22,
}],
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::StoreScalar {
binding: 4,
address: 5,
value: 23,
},
TileOp::StoreScalar {
binding: 3,
address: 5,
value: 27,
},
],
},
],
})
}
pub(in crate::runtime) fn layer_norm_backward(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"layer_norm_backward lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 3,
address: 5,
}, E::LoadF32 {
binding: 2,
address: 4,
}, E::MulF32(6, 7), E::LoadF32 {
binding: 0,
address: 5,
}, E::MulF32(8, 9), E::ConstF32(0x0000_0000), E::Lane, E::ConstU32(0),
E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::SharedF32 {
tile: 0,
offset: 13,
}, E::RegisterF32 { tile: 3, index: 0 }, E::RegisterF32 { tile: 4, index: 0 }, E::LoadF32 {
binding: 1,
address: 2,
}, E::RegisterF32 { tile: 5, index: 0 }, E::F32FromU32(1), E::ConstF32(0x3f80_0000), E::DivF32(22, 21), E::MulF32(17, 23), E::MulF32(9, 18), E::MulF32(25, 23), E::SubF32(8, 24), E::SubF32(27, 26), E::MulF32(20, 28), E::MulF32(6, 9), ];
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec!["rows".into(), "columns".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneFoldDual {
acc: 1,
acc2: 2,
init: 11,
extent: Extent::Push(1),
element: 8,
element2: 10,
},
TileOp::StoreShared {
tile: 0,
offset: 12,
value: 14,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 16,
},
TileOp::Barrier,
TileOp::StoreShared {
tile: 0,
offset: 12,
value: 15,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 16,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 19,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::StoreScalar {
binding: 4,
address: 5,
value: 29,
},
TileOp::StoreScalar {
binding: 5,
address: 5,
value: 30,
},
],
},
],
})
}
pub(in crate::runtime) fn rms_norm_backward(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"rms_norm_backward lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 0,
address: 5,
}, E::MulF32(6, 6), E::ConstF32(0x0000_0000), E::Lane, E::ConstU32(0),
E::RegisterF32 { tile: 1, index: 0 }, E::SharedF32 {
tile: 0,
offset: 10,
}, E::RegisterF32 { tile: 2, index: 0 }, E::F32FromU32(1), E::DivF32(13, 14), E::PushF32(0), E::AddF32(15, 16), E::SqrtF32(17), E::ConstF32(0x3f80_0000), E::DivF32(19, 18), E::RegisterF32 { tile: 3, index: 0 }, E::LoadF32 {
binding: 2,
address: 5,
}, E::LoadF32 {
binding: 1,
address: 4,
}, E::MulF32(22, 23), E::MulF32(24, 6), E::RegisterF32 { tile: 4, index: 0 }, E::RegisterF32 { tile: 5, index: 0 }, E::DivF32(19, 14), E::MulF32(6, 27), E::MulF32(29, 21), E::MulF32(30, 21), E::MulF32(31, 28), E::SubF32(24, 32), E::MulF32(21, 33), E::MulF32(22, 6), E::MulF32(35, 21), ];
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec!["rows".into(), "columns".into()],
push_f32_scalars: vec!["epsilon".into()],
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneFold {
acc: 1,
init: 8,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 7,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 9,
value: 11,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 12,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 20,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 4,
init: 8,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 25,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 9,
value: 26,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 12,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::StoreScalar {
binding: 3,
address: 5,
value: 34,
},
TileOp::StoreScalar {
binding: 4,
address: 5,
value: 36,
},
],
},
],
})
}
pub(in crate::runtime) fn grouped_linear_m_act() -> Result<Self> {
Self::grouped_body(true, true)
}
pub(in crate::runtime) fn grouped_linear_m() -> Result<Self> {
Self::grouped_body(true, false)
}
pub(in crate::runtime) fn grouped_gemm_m() -> Result<Self> {
Self::grouped_body(false, false)
}
fn grouped_body(has_bias: bool, has_act: bool) -> Result<Self> {
let weights: &[usize] = if has_act { &[1, 1] } else { &[1] };
let (shape, mut tiles, mut ops) =
gemm_tile_body_multi((32, 32, 16, 4, 4, 1), (2, 0, 1), weights)?;
let acc = 1 + weights.len();
let up_acc = acc + 1;
let scan_first = tiles.len();
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
});
let scan_last = tiles.len();
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
});
let mut lane_exprs = vec![
LaneExpr::GroupX, LaneExpr::ConstU32(32), LaneExpr::MulU32(0, 1), LaneExpr::Push(0), LaneExpr::GeU32(2, 3), LaneExpr::GroupY, LaneExpr::MulU32(5, 1), LaneExpr::Push(1), ];
let column_bound = if has_act {
lane_exprs.push(LaneExpr::ConstU32(2)); lane_exprs.push(LaneExpr::DivU32(7, 8)); 9
} else {
7
};
let column_oor = lane_exprs.len();
lane_exprs.push(LaneExpr::GeU32(6, column_bound));
let guard = lane_exprs.len();
lane_exprs.push(LaneExpr::OrBool(4, column_oor));
let base_plus = lane_exprs.len();
lane_exprs.push(LaneExpr::AddU32(2, 1));
let row_end = lane_exprs.len();
lane_exprs.push(LaneExpr::MinU32(base_plus, 3));
let one = lane_exprs.len();
lane_exprs.push(LaneExpr::ConstU32(1));
let last_row = lane_exprs.len();
lane_exprs.push(LaneExpr::SubU32(row_end, one));
let first_reg = lane_exprs.len();
lane_exprs.push(LaneExpr::RegisterF32 {
tile: scan_first,
index: 0,
});
let first = lane_exprs.len();
lane_exprs.push(LaneExpr::U32FromF32(first_reg));
let last_reg = lane_exprs.len();
lane_exprs.push(LaneExpr::RegisterF32 {
tile: scan_last,
index: 0,
});
let last = lane_exprs.len();
lane_exprs.push(LaneExpr::U32FromF32(last_reg));
let boundary_cond = lane_exprs.len();
lane_exprs.push(LaneExpr::NeqU32(first, last));
let expert_row = lane_exprs.len();
lane_exprs.push(LaneExpr::MulU32(first, 7));
let gate_origin = lane_exprs.len();
lane_exprs.push(LaneExpr::AddU32(expert_row, 6));
let up_origin = if has_act {
lane_exprs.push(LaneExpr::AddU32(gate_origin, column_bound));
Some(lane_exprs.len() - 1)
} else {
None
};
let TileOp::Loop { body, .. } = ops.last_mut().unwrap() else {
return Err(Error::internal("gemm body did not produce the K loop"));
};
for (index, origin) in [Some(gate_origin), up_origin].iter().flatten().enumerate() {
let TileOp::LoadShared {
row_origin,
row_extent,
mask_origin,
..
} = &mut body[1 + index]
else {
return Err(Error::internal("gemm body weight staging missing"));
};
*row_origin = Extent::Expr(*origin);
*row_extent = Extent::Expr(column_bound);
*mask_origin = Some(Extent::GroupY(32));
}
let boundary = if has_act {
TileOp::BoundaryGateUp {
acc,
offsets: 3,
input: 0,
weight: 1,
bias: 2,
output: 4,
act: 5,
experts: Extent::Push(3),
rows: Extent::Push(0),
outputs: Extent::Push(1),
inner: Extent::Push(2),
gate_width: Extent::HalfPush(1),
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
}
} else {
TileOp::BoundaryLinear {
acc,
offsets: if has_bias { 3 } else { 2 },
input: 0,
weight: 1,
bias: has_bias.then_some(2),
output: if has_bias { 4 } else { 3 },
experts: Extent::Push(3),
rows: Extent::Push(0),
outputs: Extent::Push(1),
inner: Extent::Push(2),
column_extent: Extent::Expr(column_bound),
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
transposed: false,
}
};
let mut prefix = vec![
TileOp::GuardIf { cond: guard },
TileOp::ExpertScan {
offsets: if has_bias { 3 } else { 2 },
experts: Extent::Push(3),
row: 2,
acc: scan_first,
},
TileOp::ExpertScan {
offsets: if has_bias { 3 } else { 2 },
experts: Extent::Push(3),
row: last_row,
acc: scan_last,
},
TileOp::LaneIf {
cond: boundary_cond,
body: vec![boundary, TileOp::Return],
},
];
prefix.append(&mut ops);
ops = prefix;
if has_act {
ops.push(TileOp::StoreTileGated {
gate_acc: acc,
up_acc,
bias_binding: 2,
linear_binding: 4,
act_binding: 5,
linear_stride: Extent::Push(1),
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(0),
gate_width: Extent::HalfPush(1),
bias_origin: Some(expert_row),
});
} else if has_bias {
ops.push(TileOp::StoreTileBias {
acc,
bias_binding: 2,
binding: 4,
z: false,
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
bias_origin: Some(expert_row),
act: None,
});
} else {
ops.push(TileOp::StoreTile {
acc,
binding: 3,
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
mask_origin: None,
});
}
let mut bindings = vec![Binding::Input(0), Binding::Input(1)];
if has_bias {
bindings.push(Binding::Input(2));
}
bindings.push(Binding::Input(if has_bias { 3 } else { 2 }));
bindings.push(Binding::Output(0));
if has_act {
bindings.push(Binding::Output(1));
}
let mut push_scalars = vec![
"rows".into(),
"output_features".into(),
"input_features".into(),
"experts".into(),
];
if has_act {
push_scalars.push("act_kind".into());
}
Ok(Self {
bindings,
push_scalars,
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn grouped_linear_m_data_backward() -> Result<Self> {
let (shape, mut tiles, mut ops) = gemm_tile_body_multi((32, 32, 16, 4, 4, 1), (1, 0, 2), &[1])?;
let acc = 2;
let scan_first = tiles.len();
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
});
let scan_last = tiles.len();
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
});
let mut lane_exprs = vec![
LaneExpr::GroupX, LaneExpr::ConstU32(32), LaneExpr::MulU32(0, 1), LaneExpr::Push(0), LaneExpr::GeU32(2, 3), LaneExpr::GroupY, LaneExpr::MulU32(5, 1), LaneExpr::Push(2), ];
let column_oor = lane_exprs.len();
lane_exprs.push(LaneExpr::GeU32(6, 7)); let guard = lane_exprs.len();
lane_exprs.push(LaneExpr::OrBool(4, column_oor)); let base_plus = lane_exprs.len();
lane_exprs.push(LaneExpr::AddU32(2, 1)); let row_end = lane_exprs.len();
lane_exprs.push(LaneExpr::MinU32(base_plus, 3)); let one = lane_exprs.len();
lane_exprs.push(LaneExpr::ConstU32(1)); let last_row = lane_exprs.len();
lane_exprs.push(LaneExpr::SubU32(row_end, one)); let first_reg = lane_exprs.len();
lane_exprs.push(LaneExpr::RegisterF32 {
tile: scan_first,
index: 0,
});
let first = lane_exprs.len();
lane_exprs.push(LaneExpr::U32FromF32(first_reg));
let last_reg = lane_exprs.len();
lane_exprs.push(LaneExpr::RegisterF32 {
tile: scan_last,
index: 0,
});
let last = lane_exprs.len();
lane_exprs.push(LaneExpr::U32FromF32(last_reg));
let boundary_cond = lane_exprs.len();
lane_exprs.push(LaneExpr::NeqU32(first, last));
let outputs = lane_exprs.len();
lane_exprs.push(LaneExpr::Push(1));
let expert_col = lane_exprs.len();
lane_exprs.push(LaneExpr::MulU32(first, outputs));
let TileOp::Loop { body, .. } = ops.last_mut().unwrap() else {
return Err(Error::internal("gemm body did not produce the K loop"));
};
let TileOp::LoadShared {
column_offset,
transposed,
..
} = &mut body[1]
else {
return Err(Error::internal("gemm body weight staging missing"));
};
*column_offset = Some(Extent::Expr(expert_col));
*transposed = true;
let mut prefix = vec![
TileOp::GuardIf { cond: guard },
TileOp::ExpertScan {
offsets: 2,
experts: Extent::Push(3),
row: 2,
acc: scan_first,
},
TileOp::ExpertScan {
offsets: 2,
experts: Extent::Push(3),
row: last_row,
acc: scan_last,
},
TileOp::LaneIf {
cond: boundary_cond,
body: vec![
TileOp::BoundaryLinear {
acc,
offsets: 2,
input: 0,
weight: 1,
bias: None,
output: 3,
experts: Extent::Push(3),
rows: Extent::Push(0),
outputs: Extent::Push(1),
inner: Extent::Push(2),
column_extent: Extent::Push(2),
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
transposed: true,
},
TileOp::Return,
],
},
];
prefix.append(&mut ops);
ops = prefix;
ops.push(TileOp::StoreTile {
acc,
binding: 3,
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
mask_origin: None,
});
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec![
"rows".into(),
"output_features".into(),
"input_features".into(),
"experts".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn linear_backward() -> Self {
let (shape, tiles, mut ops) = linear_backward_prelude();
ops.extend(linear_backward_ops(
TileOp::LoadShared {
tile: 0,
z: false,
binding: 1,
row_origin: Extent::GroupX(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
row_offset: None,
mask_origin: None,
column_offset: None,
transposed: false,
vec: 1,
},
2,
2,
));
Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec![
String::from("batch"),
String::from("input_features"),
String::from("output_features"),
],
push_f32_scalars: vec![],
shape,
tiles,
lane_exprs: vec![],
ops,
}
}
pub(in crate::runtime) fn linear_backward_prologue() -> Self {
use LaneExpr as E;
let (shape, tiles, mut ops) = linear_backward_prelude();
ops.extend(linear_backward_ops(
TileOp::LoadSharedPrologue {
tile: 0,
upstream: 1,
saved: 2,
dy: 3,
row_origin: Extent::GroupX(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
gate: 1,
},
2,
4,
));
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
String::from("batch"),
String::from("input_features"),
String::from("output_features"),
String::from("prologue_kind"),
],
push_f32_scalars: vec![String::from("alpha")],
shape,
tiles,
lane_exprs: vec![
E::ConstU32(0),
E::EqU32(2, 0), E::GroupY,
],
ops,
}
}
fn linear_backward_norm_program(add: bool) -> Self {
use LaneExpr as E;
let (shape, mut tiles, mut ops) = linear_backward_prelude();
tiles.insert(
2,
TileDecl::Shared {
rows: 1,
columns: 128,
stride: 128,
},
);
tiles.insert(
3,
TileDecl::Shared {
rows: 1,
columns: 64,
stride: 64,
},
);
tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
}); tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
}); tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
}); tiles.push(TileDecl::Register {
rows: 1,
columns: 1,
}); let lane_exprs = vec![
E::Lane,
E::Push(0), E::Push(2), E::ConstU32(0),
E::ConstU32(1),
E::ConstU32(2),
E::ConstU32(32),
E::GroupX,
E::GroupY,
E::MulU32(7, 6), E::DivU32(0, 5), E::AddU32(9, 10), E::MulU32(11, 2), E::ModU32(0, 5), E::LtU32(11, 1), E::Induction, E::AddU32(12, 15), E::LoadF32 {
binding: 4,
address: 16,
}, E::LoadF32 {
binding: 3,
address: 15,
}, E::MulF32(17, 18), E::LoadF32 {
binding: 1,
address: 16,
}, E::RegisterF32 { tile: 5, index: 0 }, E::RegisterF32 { tile: 6, index: 0 }, E::RegisterF32 { tile: 7, index: 0 }, E::AddF32(21, 23), E::MulF32(23, 20), E::AddF32(22, 25), E::MulU32(0, 5), E::AddU32(27, 4), E::AddU32(27, 5), E::AddU32(29, 4), E::EqU32(13, 3), E::MulU32(10, 5), E::AddU32(32, 4), E::SharedF32 {
tile: 2,
offset: 27,
}, E::SharedF32 {
tile: 2,
offset: 29,
}, E::SharedF32 {
tile: 2,
offset: 28,
}, E::SharedF32 {
tile: 2,
offset: 30,
}, E::AddF32(34, 35), E::AddF32(36, 37), E::ConstF32(0x3f80_0000), E::F32FromU32(2), E::DivF32(40, 41), E::EqU32(8, 3), E::RegisterF32 { tile: 8, index: 0 }, E::ConstF32(0x0000_0000), ];
ops.extend([
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 45,
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 45,
},
TileOp::LaneIf {
cond: 14,
body: vec![TileOp::LaneLoopRange {
start: 13,
step: 2,
extent: Extent::Push(2),
body: vec![
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 19,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 24,
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 26,
},
],
}],
},
TileOp::StoreShared {
tile: 2,
offset: 27,
value: 21,
},
TileOp::StoreShared {
tile: 2,
offset: 28,
value: 22,
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 31,
body: vec![
TileOp::StoreShared {
tile: 3,
offset: 32,
value: 38,
},
TileOp::StoreShared {
tile: 3,
offset: 33,
value: 39,
},
],
},
TileOp::Barrier,
TileOp::AssignScalar {
tile: 8,
index: 0,
value: 42,
},
]);
let (norm_in, contrib, stage1, dx, addend) = if add {
(6usize, 7usize, Some(8usize), 9usize, Some(5usize))
} else {
(5usize, 6usize, None, 7usize, None)
};
ops.extend(linear_backward_ops(
TileOp::LoadSharedNorm {
tile: 0,
normalized: 1,
inv_stddev: 2,
norm_weight: 3,
upstream: 4,
addend,
norm_input_gradient: norm_in,
weight_contribution: contrib,
stage1,
stats: 3,
inverse_columns: 44,
row_origin: Extent::GroupX(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(0),
column_extent: Extent::Push(2),
gate: 43,
},
4,
dx,
));
let mut bindings = vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
];
if add {
bindings.push(Binding::Input(5));
}
bindings.push(Binding::Output(0));
bindings.push(Binding::Output(1));
if add {
bindings.push(Binding::Output(2));
}
bindings.push(Binding::Output(if add { 3 } else { 2 }));
Self {
bindings,
push_scalars: vec![
String::from("batch"),
String::from("input_features"),
String::from("output_features"),
],
push_f32_scalars: vec![],
shape,
tiles,
lane_exprs,
ops,
}
}
pub(in crate::runtime) fn linear_backward_norm() -> Self {
Self::linear_backward_norm_program(false)
}
pub(in crate::runtime) fn linear_backward_norm_add() -> Self {
Self::linear_backward_norm_program(true)
}
pub(in crate::runtime) fn linear_backward_bmm_chain() -> Self {
use LaneExpr as E;
let tiles = vec![
TileDecl::Shared {
rows: 16,
columns: 16,
stride: 17,
},
TileDecl::Shared {
rows: 16,
columns: 32,
stride: 33,
},
TileDecl::Shared {
rows: 16,
columns: 32,
stride: 33,
},
TileDecl::Shared {
rows: 32,
columns: 32,
stride: 33,
},
TileDecl::Register {
rows: 1,
columns: 2,
},
TileDecl::Register {
rows: 1,
columns: 2,
},
];
let ops = vec![
TileOp::ClearTile { tile: 4 },
TileOp::Loop {
extent: Extent::Push(2),
step: 16,
start: None,
body: vec![
TileOp::StoreShared {
tile: 0,
offset: 36,
value: 33,
},
TileOp::StoreShared {
tile: 1,
offset: 39,
value: 53,
},
TileOp::StoreShared {
tile: 1,
offset: 40,
value: 54,
},
TileOp::Barrier,
TileOp::Loop {
extent: Extent::Expr(1),
step: 1,
start: None,
body: vec![
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 66,
},
TileOp::AssignScalar {
tile: 4,
index: 1,
value: 67,
},
],
},
TileOp::Barrier,
],
},
TileOp::StoreShared {
tile: 2,
offset: 39,
value: 64,
},
TileOp::StoreShared {
tile: 2,
offset: 40,
value: 65,
},
TileOp::LaneIf {
cond: 11,
body: vec![
TileOp::LaneIf {
cond: 43,
body: vec![TileOp::StoreScalar {
binding: 3,
address: 69,
value: 64,
}],
},
TileOp::LaneIf {
cond: 45,
body: vec![TileOp::StoreScalar {
binding: 3,
address: 70,
value: 65,
}],
},
],
},
TileOp::LaneLoopRange {
start: 3,
step: 256,
extent: Extent::Expr(71),
body: vec![TileOp::StoreShared {
tile: 3,
offset: 82,
value: 80,
}],
},
TileOp::Barrier,
TileOp::ClearTile { tile: 5 },
TileOp::Loop {
extent: Extent::Push(3),
step: 1,
start: None,
body: vec![
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 91,
},
TileOp::AssignScalar {
tile: 5,
index: 1,
value: 92,
},
],
},
TileOp::LaneIf {
cond: 11,
body: vec![
TileOp::LaneIf {
cond: 96,
body: vec![TileOp::StoreScalar {
binding: 4,
address: 94,
value: 89,
}],
},
TileOp::LaneIf {
cond: 97,
body: vec![TileOp::StoreScalar {
binding: 4,
address: 95,
value: 90,
}],
},
],
},
];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
Binding::Output(1),
],
push_scalars: vec![
String::from("batch"),
String::from("rows"),
String::from("inner"),
String::from("columns"),
String::from("input_features"),
String::from("kind"),
],
push_f32_scalars: vec![],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 16,
fragment_columns: 2,
tile_rows: 16,
tile_columns: 32,
},
tiles,
lane_exprs: vec![
E::ConstU32(0),
E::ConstU32(16),
E::ConstU32(32),
E::Lane,
E::ModU32(3, 1),
E::DivU32(3, 1),
E::GroupZ,
E::GroupY,
E::MulU32(7, 1),
E::AddU32(8, 5),
E::Push(1),
E::LtU32(9, 10),
E::Push(2),
E::Push(3),
E::Push(4),
E::Push(5),
E::AddU32(4, 1),
E::Induction,
E::AddU32(17, 4),
E::LtU32(18, 12),
E::AndBool(11, 19),
E::MulU32(6, 10),
E::AddU32(21, 9),
E::MulU32(22, 12),
E::AddU32(23, 18),
E::MulU32(6, 12),
E::AddU32(25, 18),
E::MulU32(26, 10),
E::AddU32(27, 9),
E::EqU32(15, 0),
E::SelectU32 {
cond: 29,
if_true: 24,
if_false: 28,
},
E::LoadF32 {
binding: 0,
address: 30,
},
E::ConstF32(0),
E::SelectF32 {
cond: 20,
if_true: 31,
if_false: 32,
},
E::ConstU32(17),
E::MulU32(5, 34),
E::AddU32(35, 4),
E::ConstU32(33),
E::MulU32(5, 37),
E::AddU32(38, 4),
E::AddU32(39, 1),
E::AddU32(17, 5),
E::LtU32(41, 12),
E::LtU32(4, 13),
E::AndBool(42, 43),
E::LtU32(16, 13),
E::AndBool(42, 45),
E::AddU32(25, 41),
E::MulU32(47, 13),
E::AddU32(48, 4),
E::AddU32(48, 16),
E::LoadF32 {
binding: 1,
address: 49,
},
E::LoadF32 {
binding: 1,
address: 50,
},
E::SelectF32 {
cond: 44,
if_true: 51,
if_false: 32,
},
E::SelectF32 {
cond: 46,
if_true: 52,
if_false: 32,
},
E::AddU32(35, 17),
E::SharedF32 {
tile: 0,
offset: 55,
},
E::MulU32(17, 37),
E::AddU32(57, 4),
E::SharedF32 {
tile: 1,
offset: 58,
},
E::AddU32(58, 1),
E::SharedF32 {
tile: 1,
offset: 60,
},
E::MulF32(56, 59),
E::MulF32(56, 61),
E::RegisterF32 { tile: 4, index: 0 },
E::RegisterF32 { tile: 4, index: 1 },
E::AddF32(64, 62),
E::AddF32(65, 63),
E::MulU32(22, 13),
E::AddU32(68, 4),
E::AddU32(68, 16),
E::ConstU32(1024),
E::DivU32(17, 2),
E::ModU32(17, 2),
E::LtU32(72, 13),
E::LtU32(73, 14),
E::AndBool(74, 75),
E::MulU32(72, 14),
E::AddU32(77, 73),
E::LoadF32 {
binding: 2,
address: 78,
},
E::SelectF32 {
cond: 76,
if_true: 79,
if_false: 32,
},
E::MulU32(72, 37),
E::AddU32(81, 73),
E::AddU32(38, 17),
E::SharedF32 {
tile: 2,
offset: 83,
},
E::SharedF32 {
tile: 3,
offset: 58,
},
E::SharedF32 {
tile: 3,
offset: 60,
},
E::MulF32(84, 85),
E::MulF32(84, 86),
E::RegisterF32 { tile: 5, index: 0 },
E::RegisterF32 { tile: 5, index: 1 },
E::AddF32(89, 87),
E::AddF32(90, 88),
E::MulU32(22, 14),
E::AddU32(93, 4),
E::AddU32(93, 16),
E::LtU32(4, 14),
E::LtU32(16, 14),
],
ops,
}
}
pub(in crate::runtime) fn grouped_backward_prologue() -> Result<Self> {
let shape = BlockShape {
lanes: 64,
thread_columns: 8,
fragment_rows: 4,
fragment_columns: 4,
tile_rows: 32,
tile_columns: 32,
};
let tiles = vec![
TileDecl::Shared {
rows: 32,
columns: 16,
stride: 17,
},
TileDecl::Shared {
rows: 64,
columns: 16,
stride: 17,
},
TileDecl::Register {
rows: 8,
columns: 4,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
];
let (gradient, weight, acc, scan_first, scan_last) = (0usize, 1, 2, 3, 4);
let mut lane_exprs = vec![
LaneExpr::GroupX, LaneExpr::ConstU32(32), LaneExpr::MulU32(0, 1), LaneExpr::Push(0), LaneExpr::GeU32(2, 3), LaneExpr::AddU32(2, 1), LaneExpr::MinU32(5, 3), LaneExpr::ConstU32(1), LaneExpr::SubU32(6, 7), LaneExpr::RegisterF32 {
tile: scan_first,
index: 0,
}, LaneExpr::U32FromF32(9), LaneExpr::RegisterF32 {
tile: scan_last,
index: 0,
}, LaneExpr::U32FromF32(11), LaneExpr::AddU32(12, 7), LaneExpr::LoopVar(2), LaneExpr::LoopVar(3), LaneExpr::LoopVar(4), LaneExpr::Push(1), LaneExpr::MulU32(14, 17), LaneExpr::ConstU32(0), LaneExpr::EqU32(14, 10), LaneExpr::EqU32(15, 19), LaneExpr::AndBool(20, 21), LaneExpr::AddU32(14, 7), ];
let segment_lo = lane_exprs.len();
lane_exprs.push(LaneExpr::MaxU32(
2,
lane_exprs.len() + 1, ));
lane_exprs.push(LaneExpr::BufferU32 {
binding: 4,
index: 14,
});
let segment_hi = lane_exprs.len();
lane_exprs.push(LaneExpr::MinU32(6, lane_exprs.len() + 1));
lane_exprs.push(LaneExpr::BufferU32 {
binding: 4,
index: 23,
});
let ops = vec![
TileOp::GuardIf { cond: 4 },
TileOp::ExpertScan {
offsets: 4,
experts: Extent::Push(3),
row: 2,
acc: scan_first,
},
TileOp::ExpertScan {
offsets: 4,
experts: Extent::Push(3),
row: 8,
acc: scan_last,
},
TileOp::Loop {
extent: Extent::Expr(13),
step: 1,
start: Some(Extent::Expr(10)),
body: vec![TileOp::Loop {
extent: Extent::Push(2),
step: 64,
start: None,
body: vec![
TileOp::ClearTile { tile: acc },
TileOp::Loop {
extent: Extent::Push(1),
step: 16,
start: None,
body: vec![
TileOp::LoadSharedPrologue {
tile: gradient,
upstream: 0,
saved: 1,
dy: 2,
row_origin: Extent::Expr(2),
column_origin: Extent::Expr(16),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
gate: 22,
},
TileOp::LoadShared {
tile: weight,
z: false,
binding: 3,
row_origin: Extent::Expr(15),
column_origin: Extent::Expr(16),
row_extent: Extent::Push(2),
column_extent: Extent::Push(1),
row_offset: None,
mask_origin: None,
column_offset: Some(Extent::Expr(18)),
transposed: true,
vec: 1,
},
TileOp::Barrier,
TileOp::DotSharedWide {
acc,
lhs: gradient,
rhs: weight,
column_tiles: 2,
},
TileOp::Barrier,
],
},
TileOp::StoreTileSegment {
acc,
binding: 5,
row_origin: Extent::Expr(2),
row_lo: Extent::Expr(segment_lo),
row_hi: Extent::Expr(segment_hi),
column_origin: Extent::Expr(15),
column_extent: Extent::Push(2),
column_tiles: 2,
},
],
}],
},
];
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Output(0),
Binding::Input(2),
Binding::Input(3),
Binding::Output(1),
],
push_scalars: vec![
String::from("rows"),
String::from("output_features"),
String::from("input_features"),
String::from("experts"),
String::from("prologue_kind"),
],
push_f32_scalars: vec![String::from("alpha")],
shape,
tiles,
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn grouped_linear_m_parameter_backward() -> Result<Self> {
Self::grouped_parameter_body(true)
}
pub(in crate::runtime) fn grouped_gemm_m_parameter_backward() -> Result<Self> {
Self::grouped_parameter_body(false)
}
fn grouped_parameter_body(has_bias: bool) -> Result<Self> {
let shape = BlockShape {
lanes: 64,
thread_columns: 8,
fragment_rows: 4,
fragment_columns: 4,
tile_rows: 32,
tile_columns: 32,
};
let tiles = vec![
TileDecl::Shared {
rows: 32,
columns: 16,
stride: 17,
},
TileDecl::Shared {
rows: 32,
columns: 16,
stride: 17,
},
TileDecl::Register {
rows: 4,
columns: 4,
},
TileDecl::Register {
rows: 4,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
];
let lane_exprs = vec![
LaneExpr::GroupZ, LaneExpr::Push(3), LaneExpr::GeU32(0, 1), LaneExpr::GroupX, LaneExpr::ConstU32(32), LaneExpr::MulU32(3, 4), LaneExpr::Push(1), LaneExpr::GeU32(5, 6), LaneExpr::GroupY, LaneExpr::MulU32(8, 4), LaneExpr::Push(2), LaneExpr::GeU32(9, 10), LaneExpr::OrBool(2, 7), LaneExpr::OrBool(12, 11), LaneExpr::BufferU32 {
binding: 2,
index: 0,
}, LaneExpr::ConstU32(1), LaneExpr::AddU32(0, 15), LaneExpr::BufferU32 {
binding: 2,
index: 16,
}, LaneExpr::F32FromU32(14), LaneExpr::F32FromU32(17), LaneExpr::Lane, LaneExpr::ConstU32(8), LaneExpr::ModU32(20, 21), LaneExpr::ConstU32(0), LaneExpr::EqU32(22, 23), LaneExpr::EqU32(8, 23), LaneExpr::AndBool(24, 25), LaneExpr::MulU32(0, 6), LaneExpr::AddU32(27, 5), LaneExpr::RegisterF32 { tile: 4, index: 0 }, LaneExpr::U32FromF32(29), LaneExpr::RegisterF32 { tile: 5, index: 0 }, LaneExpr::U32FromF32(31), ];
let mut ops = vec![
TileOp::GuardIf { cond: 13 },
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 18,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 19,
},
TileOp::ClearTile { tile: 2 },
];
if has_bias {
ops.push(TileOp::ClearTile { tile: 3 });
}
let dot = if has_bias {
TileOp::DotSharedParam {
acc: 2,
lhs: 0,
rhs: 1,
bias_acc: 3,
bias_gate: 26,
}
} else {
TileOp::DotShared {
acc: 2,
lhs: 0,
rhs: 1,
}
};
ops.push(TileOp::Loop {
extent: Extent::Expr(32),
step: 16,
start: Some(Extent::Expr(30)),
body: vec![
TileOp::LoadShared {
tile: 0,
binding: 0,
row_origin: Extent::GroupX(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(1),
column_extent: Extent::Expr(32),
vec: 1,
row_offset: None,
mask_origin: None,
z: false,
column_offset: None,
transposed: true,
},
TileOp::LoadShared {
tile: 1,
binding: 1,
row_origin: Extent::GroupY(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(2),
column_extent: Extent::Expr(32),
vec: 1,
row_offset: None,
mask_origin: None,
z: false,
column_offset: None,
transposed: true,
},
TileOp::Barrier,
dot,
TileOp::Barrier,
],
});
ops.push(TileOp::StoreTile {
acc: 2,
binding: 3,
row_origin: Extent::Expr(28),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(1),
column_extent: Extent::Push(2),
mask_origin: Some(Extent::GroupX(32)),
});
if has_bias {
ops.push(TileOp::StoreTileBiasParam {
acc: 3,
binding: 4,
origin: Extent::Expr(27),
row_origin: Extent::GroupX(32),
row_extent: Extent::Push(1),
gate: 26,
});
}
let mut bindings = vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
];
if has_bias {
bindings.push(Binding::Output(1));
}
Ok(Self {
bindings,
push_scalars: vec![
"rows".into(),
"output_features".into(),
"input_features".into(),
"experts".into(),
],
push_f32_scalars: Vec::new(),
shape,
tiles,
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn mat_mul_nt_gemv(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"gemv lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX, E::GroupY, E::Push(0), E::Push(1), E::Push(2), E::LtU32(0, 2), E::LtU32(1, 3), E::AndBool(5, 6), E::Induction, E::MulU32(0, 4), E::AddU32(9, 8), E::LoadF32 {
binding: 0,
address: 10,
},
E::MulU32(1, 4), E::AddU32(12, 8), E::LoadF32 {
binding: 1,
address: 13,
},
E::MulF32(11, 14),
E::ConstF32(0), E::RegisterF32 { tile: 1, index: 0 }, E::Lane,
E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 19,
}, E::MulU32(0, 3), E::AddU32(21, 1), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec!["m".into(), "n".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 16,
},
TileOp::LaneIf {
cond: 7,
body: vec![TileOp::LaneFold {
acc: 1,
init: 16,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: 15,
serial: false,
}],
},
TileOp::StoreShared {
tile: 0,
offset: 18,
value: 17,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::LaneGuard {
lane: 19,
body: vec![TileOp::LaneIf {
cond: 7,
body: vec![TileOp::StoreScalar {
binding: 2,
address: 22,
value: 20,
}],
}],
},
],
})
}
}
#[cfg(test)]
#[path = "../../../../../../test/rs/runtime/ir_tile_unit.rs"]
mod tests;
fn gemm_tile_body(
geometry: (u32, u32, u32, u32, u32, u32),
pushes: (usize, usize, usize),
) -> Result<(BlockShape, Vec<TileDecl>, Vec<TileOp>)> {
gemm_tile_body_multi(geometry, pushes, &[1])
}
fn gemm_tile_body_multi(
geometry: (u32, u32, u32, u32, u32, u32),
pushes: (usize, usize, usize),
weights: &[usize],
) -> Result<(BlockShape, Vec<TileDecl>, Vec<TileOp>)> {
let (tile_rows, tile_columns, tile_k, fragment_rows, fragment_columns, vec) = geometry;
let (k_push, row_push, column_push) = pushes;
if weights.is_empty() {
return Err(Error::invalid_argument(
"multi-weight GEMM body needs at least one weight binding",
));
}
if vec == 0 || vec > 4 || !tile_k.is_multiple_of(vec) {
return Err(Error::invalid_argument(
"tile_k must be a positive multiple of the staging width (≤4)",
));
}
if !tile_rows.is_multiple_of(fragment_rows) || !tile_columns.is_multiple_of(fragment_columns) {
return Err(Error::invalid_argument(
"output tile must be an exact multiple of the fragment tile",
));
}
let thread_columns = tile_columns / fragment_columns;
let lanes = (tile_rows / fragment_rows) * thread_columns;
if lanes > 1024 {
return Err(Error::invalid_argument(
"fragment grid exceeds the Vulkan workgroup floor",
));
}
let shape = BlockShape {
lanes,
thread_columns,
fragment_rows,
fragment_columns,
tile_rows,
tile_columns,
};
let count = weights.len();
let mut tiles = vec![TileDecl::Shared {
rows: tile_rows,
columns: tile_k,
stride: tile_k + 1,
}];
for _ in weights {
tiles.push(TileDecl::Shared {
rows: tile_columns,
columns: tile_k,
stride: tile_k + 1,
});
}
for _ in weights {
tiles.push(TileDecl::Register {
rows: fragment_rows,
columns: fragment_columns,
});
}
let mut ops = Vec::new();
for acc in 0..count {
ops.push(TileOp::ClearTile {
tile: count + 1 + acc,
});
}
let mut body = vec![TileOp::LoadShared {
tile: 0,
binding: 0,
row_origin: Extent::GroupX(tile_rows),
column_origin: Extent::Induction,
row_extent: Extent::Push(row_push),
column_extent: Extent::Push(k_push),
vec,
row_offset: None,
mask_origin: None,
z: false,
column_offset: None,
transposed: false,
}];
for (index, binding) in weights.iter().enumerate() {
body.push(TileOp::LoadShared {
tile: 1 + index,
binding: *binding,
row_origin: Extent::GroupY(tile_columns),
column_origin: Extent::Induction,
row_extent: Extent::Push(column_push),
column_extent: Extent::Push(k_push),
vec,
row_offset: None,
mask_origin: None,
z: false,
column_offset: None,
transposed: false,
});
}
body.push(TileOp::Barrier);
for index in 0..count {
body.push(TileOp::DotShared {
acc: count + 1 + index,
lhs: 0,
rhs: 1 + index,
});
}
body.push(TileOp::Barrier);
ops.push(TileOp::Loop {
extent: Extent::Push(k_push),
step: tile_k,
start: None,
body,
});
Ok((shape, tiles, ops))
}
fn linear_backward_prelude() -> (BlockShape, Vec<TileDecl>, Vec<TileOp>) {
(
BlockShape {
lanes: 64,
thread_columns: 8,
fragment_rows: 4,
fragment_columns: 4,
tile_rows: 32,
tile_columns: 32,
},
vec![
TileDecl::Shared {
rows: 32,
columns: 16,
stride: 17,
},
TileDecl::Shared {
rows: 32,
columns: 16,
stride: 17,
},
TileDecl::Register {
rows: 4,
columns: 4,
},
],
Vec::new(),
)
}
fn linear_backward_ops(lhs: TileOp, acc: usize, dx_binding: usize) -> Vec<TileOp> {
vec![
TileOp::ClearTile { tile: acc },
TileOp::Loop {
extent: Extent::Push(2),
step: 16,
start: None,
body: vec![
lhs,
TileOp::LoadShared {
tile: 1,
z: false,
binding: 0,
row_origin: Extent::GroupY(32),
column_origin: Extent::Induction,
row_extent: Extent::Push(1),
column_extent: Extent::Push(2),
row_offset: None,
mask_origin: None,
column_offset: None,
transposed: true,
vec: 1,
},
TileOp::Barrier,
TileOp::DotShared {
acc,
lhs: 0,
rhs: 1,
},
TileOp::Barrier,
],
},
TileOp::StoreTile {
acc,
binding: dx_binding,
row_origin: Extent::GroupX(32),
column_origin: Extent::GroupY(32),
row_extent: Extent::Push(0),
column_extent: Extent::Push(1),
mask_origin: None,
},
]
}
fn gemm_tile_body_z(
geometry: (u32, u32, u32, u32, u32, u32),
pushes: (usize, usize, usize),
) -> Result<(BlockShape, Vec<TileDecl>, Vec<TileOp>)> {
let (shape, tiles, mut ops) = gemm_tile_body(geometry, pushes)?;
if let TileOp::Loop { body, .. } = &mut ops[1]
&& let TileOp::LoadShared { z, .. } = &mut body[1]
{
*z = true;
}
Ok((shape, tiles, ops))
}
impl BlockProgram {
pub(in crate::runtime) fn moe_router_select() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::Push(2), E::GroupX, E::MulU32(3, 1), E::Lane,
E::Induction, E::AddU32(4, 6), E::LoadF32 {
binding: 0,
address: 7,
}, E::ConstF32(0xff7fffff), E::RegisterF32 { tile: 3, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 11,
}, E::RegisterF32 { tile: 4, index: 0 }, E::SubF32(8, 13), E::ExpF32(14),
E::ConstF32(0), E::RegisterF32 { tile: 5, index: 0 }, E::ConstF32(0x3f800000), E::DivF32(18, 12), E::RegisterF32 { tile: 6, index: 0 }, E::MulF32(15, 20), E::LoopVar(2), E::LoopVar(3), E::LoopVar(4), E::SharedU32 {
tile: 2,
offset: 24,
}, E::EqU32(25, 23),
E::ConstU32(1),
E::SelectU32 {
cond: 26,
if_true: 27,
if_false: 11,
},
E::RegisterU32 { tile: 9, index: 0 }, E::AddU32(29, 28),
E::EqU32(29, 11), E::AddU32(4, 23), E::LoadF32 {
binding: 1,
address: 32,
}, E::RegisterF32 { tile: 7, index: 0 }, E::GtF32(33, 34),
E::EqF32(33, 34),
E::RegisterU32 { tile: 8, index: 0 }, E::LtU32(23, 37), E::AndBool(36, 38),
E::OrBool(35, 39), E::AndBool(31, 40), E::MulU32(3, 2), E::AddU32(42, 22), E::SharedU32 {
tile: 1,
offset: 11,
}, E::SharedU32 {
tile: 2,
offset: 23,
}, E::EqU32(45, 22), E::SelectU32 {
cond: 46,
if_true: 27,
if_false: 11,
},
E::AddU32(29, 47), E::AddU32(4, 22), E::MinU32(29, 27), E::F32FromU32(50), E::ConstU32(u32::MAX), ];
Self {
bindings: vec![
Binding::Input(0), Binding::Input(1), Binding::Output(0), Binding::Output(1), Binding::Output(2), Binding::Output(3), ],
push_scalars: vec!["rows".into(), "experts".into(), "k".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: 256,
stride: 256,
}, TileDecl::SharedU32 { elements: 256 }, TileDecl::SharedU32 { elements: 1024 }, TileDecl::Register {
rows: 1,
columns: 1,
}, TileDecl::Register {
rows: 1,
columns: 1,
}, TileDecl::Register {
rows: 1,
columns: 1,
}, TileDecl::Register {
rows: 1,
columns: 1,
}, TileDecl::Register {
rows: 1,
columns: 1,
}, TileDecl::RegisterU32 { elements: 1 }, TileDecl::RegisterU32 { elements: 1 }, ],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 11,
},
TileOp::LaneFold {
acc: 3,
init: 9,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 8,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 5,
value: 10,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 12,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 5,
init: 16,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 15,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 5,
value: 17,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 19,
},
TileOp::Barrier,
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 2,
address: 7,
value: 21,
}],
},
TileOp::Barrier,
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 9,
},
TileOp::AssignScalarU32 {
tile: 8,
index: 0,
value: 52,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 11,
},
TileOp::Loop {
extent: Extent::Expr(22),
step: 1,
start: None,
body: vec![TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 30,
}],
},
TileOp::LaneIf {
cond: 41,
body: vec![
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 33,
},
TileOp::AssignScalarU32 {
tile: 8,
index: 0,
value: 23,
},
],
},
],
},
TileOp::StoreShared {
tile: 0,
offset: 5,
value: 34,
},
TileOp::StoreSharedU32 {
tile: 1,
offset: 5,
value: 37,
},
TileOp::Barrier,
TileOp::TreeReduceArgmax {
value_tile: 0,
index_tile: 1,
},
TileOp::LaneGuard {
lane: 11,
body: vec![
TileOp::StoreSharedU32 {
tile: 2,
offset: 22,
value: 44,
},
TileOp::StoreScalar {
binding: 3,
address: 43,
value: 12,
},
TileOp::StoreScalarU32 {
binding: 4,
address: 43,
value: 44,
},
],
},
TileOp::Barrier,
],
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 11,
},
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 48,
}],
},
TileOp::StoreScalar {
binding: 5,
address: 49,
value: 51,
},
],
},
],
}
}
pub(in crate::runtime) fn moe_gather() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::MulU32(5, 6), E::GeU32(4, 7), E::DivU32(4, 6), E::ModU32(4, 6), E::BufferU32 {
binding: 1,
index: 9,
}, E::MulU32(11, 6), E::AddU32(12, 10), E::LoadF32 {
binding: 0,
address: 13,
},
];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec!["routes".into(), "row_width".into()],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 14,
},
],
)
}
pub(in crate::runtime) fn moe_combine() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(2), E::MulU32(5, 6), E::GeU32(4, 7),
E::DivU32(4, 6), E::ModU32(4, 6), E::Push(1), E::MulU32(9, 11), E::Induction, E::AddU32(12, 13), E::BufferU32 {
binding: 2,
index: 14,
}, E::Push(3), E::LtU32(15, 16), E::MulU32(15, 6), E::AddU32(18, 10), E::LoadF32 {
binding: 0,
address: 19,
},
E::LoadF32 {
binding: 1,
address: 14,
}, E::MulF32(20, 21),
E::ConstF32(0), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(24, 22), ];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
vec![
"tokens".into(),
"routes_per_token".into(),
"row_width".into(),
"routes".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 23,
},
TileOp::Loop {
extent: Extent::Push(1),
step: 1,
start: None,
body: vec![TileOp::LaneIf {
cond: 17,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 25,
}],
}],
},
TileOp::StoreScalar {
binding: 3,
address: 4,
value: 24,
},
],
)
}
pub(in crate::runtime) fn moe_gather_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3),
E::Push(2), E::Push(1), E::MulU32(5, 6), E::GeU32(4, 7),
E::DivU32(4, 6), E::ModU32(4, 6), E::Push(3), E::MulU32(9, 11), E::Induction,
E::AddU32(12, 13), E::BufferU32 {
binding: 1,
index: 14,
}, E::Push(0), E::LtU32(15, 16),
E::MulU32(15, 6),
E::AddU32(18, 10),
E::LoadF32 {
binding: 0,
address: 19,
},
E::ConstF32(0),
E::RegisterF32 { tile: 0, index: 0 },
E::AddF32(22, 20),
];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"routes".into(),
"row_width".into(),
"tokens".into(),
"routes_per_token".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 21,
},
TileOp::Loop {
extent: Extent::Push(3),
step: 1,
start: None,
body: vec![TileOp::LaneIf {
cond: 17,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 23,
}],
}],
},
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 22,
},
],
)
}
pub(in crate::runtime) fn moe_combine_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(3), E::Push(2), E::MulU32(5, 6), E::LtU32(4, 7), E::DivU32(4, 6), E::ModU32(4, 6), E::BufferU32 {
binding: 4,
index: 9,
}, E::Push(1), E::DivU32(11, 12), E::MulU32(13, 6),
E::AddU32(14, 10), E::LoadF32 {
binding: 0,
address: 15,
},
E::LoadF32 {
binding: 2,
address: 11,
}, E::MulF32(16, 17),
E::LtU32(4, 5), E::DivU32(4, 12), E::BufferU32 {
binding: 3,
index: 4,
}, E::Induction, E::MulU32(20, 6),
E::AddU32(23, 22), E::LoadF32 {
binding: 0,
address: 24,
},
E::MulU32(21, 6),
E::AddU32(26, 22), E::LoadF32 {
binding: 1,
address: 27,
},
E::MulF32(25, 28),
E::ConstF32(0),
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(31, 29),
];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Output(0),
Binding::Output(1),
],
vec![
"tokens".into(),
"routes_per_token".into(),
"row_width".into(),
"routes".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::LaneIf {
cond: 8,
body: vec![TileOp::StoreScalar {
binding: 5,
address: 4,
value: 18,
}],
},
TileOp::LaneIf {
cond: 19,
body: vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 30,
},
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 32,
}],
},
TileOp::StoreScalar {
binding: 6,
address: 4,
value: 31,
},
],
},
],
)
}
pub(in crate::runtime) fn moe_route_weights() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::GeU32(4, 5),
E::Push(2), E::MulU32(4, 7), E::Induction, E::AddU32(8, 9), E::BufferU32 {
binding: 1,
index: 10,
}, E::Push(1), E::LtU32(11, 12), E::MulU32(4, 12), E::AddU32(14, 11), E::LoadF32 {
binding: 0,
address: 15,
},
E::ConstF32(0), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(18, 16), E::GtF32(18, 17), E::ConstF32(0x3f800000), E::DivF32(21, 18),
E::SelectF32 {
cond: 20,
if_true: 22,
if_false: 17,
},
E::RegisterF32 { tile: 1, index: 0 }, E::MulF32(16, 24),
E::SelectF32 {
cond: 13,
if_true: 25,
if_false: 17,
},
];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec!["tokens".into(), "experts".into(), "routes_per_token".into()],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 6 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 17,
},
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![TileOp::LaneIf {
cond: 13,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 19,
}],
}],
},
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 23,
},
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![TileOp::StoreScalar {
binding: 2,
address: 10,
value: 26,
}],
},
],
)
}
pub(in crate::runtime) fn moe_route_weights_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::MulU32(5, 6), E::GeU32(4, 7),
E::DivU32(4, 6), E::ModU32(4, 6), E::Push(2), E::MulU32(9, 11), E::Induction, E::AddU32(12, 13), E::BufferU32 {
binding: 2,
index: 14,
}, E::LtU32(15, 6), E::MulU32(9, 6), E::AddU32(17, 15), E::LoadF32 {
binding: 1,
address: 18,
},
E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(20, 19),
E::LoadF32 {
binding: 0,
address: 14,
}, E::LoadF32 {
binding: 3,
address: 14,
}, E::MulF32(22, 23),
E::RegisterF32 { tile: 1, index: 0 }, E::AddF32(25, 24),
E::EqU32(15, 10), E::RegisterU32 { tile: 2, index: 0 }, E::ConstU32(u32::MAX), E::NeqU32(28, 29), E::ConstF32(0), E::GtF32(20, 31), E::AndBool(30, 32),
E::AddU32(12, 28), E::LoadF32 {
binding: 0,
address: 34,
},
E::SubF32(35, 25),
E::DivF32(36, 20),
E::SelectF32 {
cond: 33,
if_true: 37,
if_false: 31,
},
];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Output(0),
],
vec!["tokens".into(), "experts".into(), "routes_per_token".into()],
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
],
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 31,
},
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 31,
},
TileOp::AssignScalarU32 {
tile: 2,
index: 0,
value: 29,
},
TileOp::Loop {
extent: Extent::Push(2),
step: 1,
start: None,
body: vec![TileOp::LaneIf {
cond: 16,
body: vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 21,
},
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 26,
},
TileOp::LaneIf {
cond: 27,
body: vec![TileOp::AssignScalarU32 {
tile: 2,
index: 0,
value: 13,
}],
},
],
}],
},
TileOp::StoreScalar {
binding: 4,
address: 4,
value: 38,
},
],
)
}
pub(in crate::runtime) fn cross_entropy(lanes: u32, masked: bool) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"cross_entropy lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let mut lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX,
E::GroupY,
E::ConstU32(65535),
E::MulU32(3, 4),
E::AddU32(2, 5), E::GeU32(6, 0), E::MulU32(6, 1), E::Induction, E::AddU32(8, 9), E::LoadF32 {
binding: 0,
address: 10,
}, E::ConstF32(0xff7f_ffff), E::ConstF32(0x0000_0000), E::Lane,
E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 15,
}, E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::RegisterF32 { tile: 3, index: 0 }, E::SubF32(11, 19), E::ExpF32(20),
E::BufferU32 {
binding: 1,
index: 6,
}, E::AddU32(8, 22), E::LoadF32 {
binding: 0,
address: 23,
}, E::LogF32(16), E::AddF32(25, 19), E::SubF32(26, 24), E::Push(2), E::ConstU32(1),
E::EqU32(28, 29),
E::LtU32(22, 1), E::AndBool(30, 31), E::RegisterF32 { tile: 4, index: 0 }, E::ConstF32(0xffc0_0000), ];
let mut ops = vec![TileOp::GuardIf { cond: 7 }];
if masked {
lane_exprs.extend([
E::LoadF32 {
binding: 2,
address: 6,
}, E::EqF32(35, 13), ]);
ops.push(TileOp::LaneIf {
cond: 36,
body: vec![
TileOp::LaneGuard {
lane: 15,
body: vec![TileOp::StoreScalar {
binding: 3,
address: 6,
value: 13,
}],
},
TileOp::Return,
],
});
}
ops.extend([
TileOp::LaneFold {
acc: 1,
init: 12,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 11,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 14,
value: 17,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 16,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 2,
init: 13,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 21,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 14,
value: 18,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::LaneGuard {
lane: 15,
body: vec![
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 34,
},
TileOp::LaneIf {
cond: 32,
body: vec![TileOp::AssignScalar {
tile: 4,
index: 0,
value: 27,
}],
},
TileOp::StoreScalar {
binding: if masked { 3 } else { 2 },
address: 6,
value: 33,
},
],
},
]);
let mut bindings = vec![Binding::Input(0), Binding::Input(1)];
if masked {
bindings.push(Binding::Input(2));
}
bindings.push(Binding::Output(0));
Ok(Self {
bindings,
push_scalars: vec!["rows".into(), "classes".into(), "target_dtype".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn cross_entropy_backward(lanes: u32, masked: bool) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"cross_entropy_backward lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let count_push = if masked { 3 } else { 0 };
let mut lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX,
E::GroupY,
E::ConstU32(65535),
E::MulU32(3, 4),
E::AddU32(2, 5), E::GeU32(6, 0), E::MulU32(6, 1), E::Induction, E::AddU32(8, 9), E::LoadF32 {
binding: 0,
address: 10,
}, E::ConstF32(0xff7f_ffff), E::ConstF32(0x0000_0000), E::Lane,
E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 15,
}, E::BufferU32 {
binding: 1,
index: 6,
}, E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::RegisterF32 { tile: 3, index: 0 }, E::SubF32(11, 20), E::ExpF32(21),
E::RegisterF32 { tile: 4, index: 0 }, E::RegisterF32 { tile: 5, index: 0 }, E::ConstF32(0x3f80_0000), E::DivF32(25, 16), E::Push(count_push), E::F32FromU32(27), E::DivF32(25, 28), E::ConstU32(1),
E::EqU32(9, 17),
E::SelectF32 {
cond: 31,
if_true: 25,
if_false: 13,
}, E::MulF32(22, 23), E::SubF32(33, 32), E::MulF32(34, 24), E::Push(2), E::EqU32(36, 30),
E::LtU32(17, 1), E::AndBool(37, 38), E::ConstF32(0xffc0_0000),
E::SelectF32 {
cond: 39,
if_true: 35,
if_false: 40,
}, ];
let mut ops = vec![TileOp::GuardIf { cond: 7 }];
if masked {
lane_exprs.extend([
E::LoadF32 {
binding: 2,
address: 6,
}, E::EqF32(42, 13), ]);
ops.push(TileOp::LaneIf {
cond: 43,
body: vec![
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 3,
address: 10,
value: 13,
}],
},
TileOp::Return,
],
});
}
ops.extend([
TileOp::LaneFold {
acc: 1,
init: 12,
extent: Extent::Push(1),
op: LaneReduce::Max,
element: 11,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 14,
value: 18,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 16,
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 2,
init: 13,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 22,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 14,
value: 19,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 26,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 29,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: if masked { 3 } else { 2 },
address: 10,
value: 41,
}],
},
]);
let mut bindings = vec![Binding::Input(0), Binding::Input(1)];
if masked {
bindings.push(Binding::Input(2));
}
bindings.push(Binding::Output(0));
let mut push_scalars = vec!["rows".into(), "classes".into(), "target_dtype".into()];
if masked {
push_scalars.push("valid_count".into());
}
Ok(Self {
bindings,
push_scalars,
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn layer_norm_backward_sum(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"layer_norm_backward_sum lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 3,
address: 5,
}, E::LoadF32 {
binding: 4,
address: 5,
}, E::AddF32(6, 7), E::LoadF32 {
binding: 5,
address: 5,
}, E::AddF32(8, 9), E::Push(2), E::ConstU32(3),
E::EqU32(11, 12),
E::SelectF32 {
cond: 13,
if_true: 10,
if_false: 8,
}, E::LoadF32 {
binding: 2,
address: 4,
}, E::MulF32(14, 15), E::LoadF32 {
binding: 0,
address: 5,
}, E::MulF32(16, 17), E::ConstF32(0x0000_0000), E::Lane, E::ConstU32(0),
E::RegisterF32 { tile: 1, index: 0 }, E::RegisterF32 { tile: 2, index: 0 }, E::SharedF32 {
tile: 0,
offset: 21,
}, E::RegisterF32 { tile: 3, index: 0 }, E::RegisterF32 { tile: 4, index: 0 }, E::LoadF32 {
binding: 1,
address: 2,
}, E::RegisterF32 { tile: 5, index: 0 }, E::F32FromU32(1), E::ConstF32(0x3f80_0000), E::DivF32(30, 29), E::RegisterF32 { tile: 10, index: 0 }, E::RegisterF32 { tile: 6, index: 0 }, E::MulF32(33, 17), E::AddF32(22, 33), E::AddF32(23, 34), E::RegisterF32 { tile: 7, index: 0 }, E::RegisterF32 { tile: 8, index: 0 }, E::RegisterF32 { tile: 9, index: 0 }, E::MulF32(37, 38), E::MulF32(25, 32), E::MulF32(39, 26), E::MulF32(42, 32), E::SubF32(40, 41), E::SubF32(44, 43), E::MulF32(28, 45), E::MulF32(37, 39), ];
Ok(Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Input(3),
Binding::Input(4),
Binding::Input(5),
Binding::Output(0),
Binding::Output(1),
Binding::Output(2),
],
push_scalars: vec!["rows".into(), "columns".into(), "addend_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 19,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 19,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 16,
},
TileOp::AssignScalar {
tile: 1,
index: 0,
value: 35,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 36,
},
],
},
TileOp::StoreShared {
tile: 0,
offset: 20,
value: 22,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 24,
},
TileOp::Barrier,
TileOp::StoreShared {
tile: 0,
offset: 20,
value: 23,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 24,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: 27,
},
TileOp::AssignScalar {
tile: 10,
index: 0,
value: 31,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![
TileOp::AssignScalar {
tile: 7,
index: 0,
value: 14,
},
TileOp::AssignScalar {
tile: 8,
index: 0,
value: 15,
},
TileOp::AssignScalar {
tile: 9,
index: 0,
value: 17,
},
TileOp::StoreScalar {
binding: 6,
address: 5,
value: 46,
},
TileOp::StoreScalar {
binding: 7,
address: 5,
value: 47,
},
TileOp::StoreScalar {
binding: 8,
address: 5,
value: 37,
},
],
},
],
})
}
pub(in crate::runtime) fn embedding_backward_sum() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(1), E::Push(2), E::MulU32(5, 6), E::GeU32(4, 7), E::DivU32(4, 6), E::ModU32(4, 6), E::ConstF32(0x0000_0000), E::LoopVar(2), E::BufferU32 {
binding: 0,
index: 12,
}, E::EqU32(13, 9), E::MulU32(12, 6),
E::AddU32(15, 10), E::LoadF32 {
binding: 1,
address: 16,
}, E::LoadF32 {
binding: 2,
address: 16,
}, E::AddF32(17, 18), E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(20, 19),
E::SelectF32 {
cond: 14,
if_true: 21,
if_false: 20,
}, ];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
vec![
"index_count".into(),
"num_embeddings".into(),
"embedding_dim".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 11,
},
TileOp::Loop {
extent: Extent::Push(0),
step: 1,
start: None,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 22,
}],
},
TileOp::StoreScalar {
binding: 3,
address: 4,
value: 20,
},
],
)
}
pub(in crate::runtime) fn embedding_backward() -> Self {
Self::embedding_backward_program(false)
}
pub(in crate::runtime) fn embedding_backward_u8() -> Self {
Self::embedding_backward_program(true)
}
fn embedding_backward_program(packed: bool) -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(1), E::Push(2), E::MulU32(5, 6), E::GeU32(4, 7), E::DivU32(4, 6), E::ModU32(4, 6), E::ConstF32(0x0000_0000), E::LoopVar(2),
if packed {
E::BufferU8 {
binding: 0,
index: 12,
}
} else {
E::BufferU32 {
binding: 0,
index: 12,
}
}, E::EqU32(13, 9), E::MulU32(12, 6),
E::AddU32(15, 10),
E::LoadF32 {
binding: 1,
address: 16,
}, E::RegisterF32 { tile: 0, index: 0 }, E::AddF32(18, 17),
E::SelectF32 {
cond: 14,
if_true: 19,
if_false: 18,
}, ];
Self::flat_program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"index_count".into(),
"num_embeddings".into(),
"embedding_dim".into(),
],
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
vec![
TileOp::GuardIf { cond: 8 },
TileOp::AssignScalar {
tile: 0,
index: 0,
value: 11,
},
TileOp::Loop {
extent: Extent::Push(0),
step: 1,
start: None,
body: vec![TileOp::AssignScalar {
tile: 0,
index: 0,
value: 20,
}],
},
TileOp::StoreScalar {
binding: 2,
address: 4,
value: 18,
},
],
)
}
pub(in crate::runtime) fn sum_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::Push(1), E::Push(2), E::MulU32(6, 7), E::MulU32(5, 8), E::GeU32(4, 9), E::DivU32(4, 8), E::ModU32(4, 7), E::MulU32(11, 7),
E::AddU32(13, 12), E::LoadF32 {
binding: 0,
address: 14,
},
];
Self::flat_program(
vec![Binding::Input(0), Binding::Output(0)],
vec!["outer_size".into(), "dim_size".into(), "inner_size".into()],
vec![],
lane_exprs,
vec![
TileOp::GuardIf { cond: 10 },
TileOp::StoreScalar {
binding: 1,
address: 4,
value: 15,
},
],
)
}
pub(in crate::runtime) fn max(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"max lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
},
E::ConstF32(0xff7f_ffff), E::RegisterF32 { tile: 1, index: 0 },
E::Lane,
E::ConstU32(0),
E::SharedF32 { tile: 0, offset: 6 },
];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::LaneFold {
acc: 1,
init: 3,
extent: Extent::Push(0),
op: LaneReduce::Max,
element: 2,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 5,
value: 4,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Max,
},
TileOp::LaneGuard {
lane: 6,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 6,
value: 7,
}],
},
],
})
}
pub(in crate::runtime) fn max_wave(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"max_wave lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
},
E::ConstF32(0xff7f_ffff), E::RegisterF32 { tile: 1, index: 0 }, E::Lane,
E::WaveMax(4), E::WaveLaneCount, E::ModU32(5, 7), E::ConstU32(0),
E::EqU32(8, 9), E::DivU32(5, 7), E::ConstU32(lanes),
E::DivU32(12, 7), E::LtU32(5, 13), E::SharedF32 { tile: 0, offset: 5 },
E::SelectF32 {
cond: 14,
if_true: 15,
if_false: 3,
}, E::WaveMax(16), E::LtU32(5, 7), E::RegisterF32 { tile: 2, index: 0 }, E::RegisterF32 { tile: 3, index: 0 }, ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::LaneFold {
acc: 1,
init: 3,
extent: Extent::Push(0),
op: LaneReduce::Max,
element: 2,
serial: false,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 6,
},
TileOp::LaneIf {
cond: 10,
body: vec![TileOp::StoreShared {
tile: 0,
offset: 11,
value: 19,
}],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 18,
body: vec![
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 17,
},
TileOp::LaneGuard {
lane: 9,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 9,
value: 20,
}],
},
],
},
],
})
}
pub(in crate::runtime) fn sum_wave(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"sum_wave lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
},
E::ConstF32(0), E::RegisterF32 { tile: 1, index: 0 }, E::Lane,
E::WaveSum(4), E::WaveLaneCount, E::ModU32(5, 7), E::ConstU32(0),
E::EqU32(8, 9), E::DivU32(5, 7), E::ConstU32(lanes),
E::DivU32(12, 7), E::LtU32(5, 13), E::SharedF32 { tile: 0, offset: 5 },
E::SelectF32 {
cond: 14,
if_true: 15,
if_false: 3,
}, E::WaveSum(16), E::LtU32(5, 7), E::RegisterF32 { tile: 2, index: 0 }, E::RegisterF32 { tile: 3, index: 0 }, ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::LaneFold {
acc: 1,
init: 3,
extent: Extent::Push(0),
op: LaneReduce::Sum,
element: 2,
serial: false,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 6,
},
TileOp::LaneIf {
cond: 10,
body: vec![TileOp::StoreShared {
tile: 0,
offset: 11,
value: 19,
}],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 18,
body: vec![
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 17,
},
TileOp::LaneGuard {
lane: 9,
body: vec![TileOp::StoreScalar {
binding: 1,
address: 9,
value: 20,
}],
},
],
},
],
})
}
pub(in crate::runtime) fn argmax(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"argmax lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
}, E::RegisterF32 { tile: 2, index: 0 }, E::RegisterU32 { tile: 3, index: 0 }, E::GtF32(2, 3),
E::SelectF32 {
cond: 5,
if_true: 2,
if_false: 3,
},
E::SelectU32 {
cond: 5,
if_true: 1,
if_false: 4,
}, E::ConstF32(0xff7f_ffff), E::ConstU32(0),
E::Lane,
E::SharedU32 { tile: 1, offset: 9 }, ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::SharedU32 { elements: lanes },
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
],
lane_exprs,
ops: vec![
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 8,
},
TileOp::AssignScalarU32 {
tile: 3,
index: 0,
value: 9,
},
TileOp::LaneLoop {
extent: Extent::Push(0),
body: vec![
TileOp::AssignScalarU32 {
tile: 3,
index: 0,
value: 7,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 6,
},
],
},
TileOp::StoreShared {
tile: 0,
offset: 10,
value: 3,
},
TileOp::StoreSharedU32 {
tile: 1,
offset: 10,
value: 4,
},
TileOp::Barrier,
TileOp::TreeReduceArgmax {
value_tile: 0,
index_tile: 1,
},
TileOp::LaneGuard {
lane: 9,
body: vec![TileOp::StoreScalarU32 {
binding: 1,
address: 9,
value: 11,
}],
},
],
})
}
pub(in crate::runtime) fn argmax_wave(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"argmax_wave lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Induction, E::LoadF32 {
binding: 0,
address: 1,
}, E::RegisterF32 { tile: 2, index: 0 }, E::RegisterU32 { tile: 3, index: 0 }, E::GtF32(2, 3),
E::SelectF32 {
cond: 5,
if_true: 2,
if_false: 3,
},
E::SelectU32 {
cond: 5,
if_true: 1,
if_false: 4,
},
E::ConstF32(0xff7f_ffff), E::ConstU32(0),
E::Lane,
E::WaveMax(3), E::RegisterF32 { tile: 4, index: 0 }, E::EqF32(3, 12), E::ConstU32(0xffff_ffff),
E::SelectU32 {
cond: 13,
if_true: 4,
if_false: 14,
}, E::WaveMinU32(15), E::WaveLaneCount, E::ModU32(10, 17), E::EqU32(18, 9), E::DivU32(10, 17), E::ConstU32(lanes),
E::DivU32(21, 17), E::LtU32(10, 22), E::SharedF32 {
tile: 0,
offset: 10,
}, E::SharedU32 {
tile: 1,
offset: 10,
},
E::SelectF32 {
cond: 23,
if_true: 24,
if_false: 8,
},
E::SelectU32 {
cond: 23,
if_true: 25,
if_false: 14,
}, E::LtU32(10, 17), E::RegisterF32 { tile: 6, index: 0 }, E::WaveMax(29), E::RegisterF32 { tile: 8, index: 0 }, E::EqF32(29, 31), E::RegisterU32 { tile: 7, index: 0 },
E::SelectU32 {
cond: 32,
if_true: 33,
if_false: 14,
}, E::WaveMinU32(34), E::RegisterU32 { tile: 9, index: 0 }, E::RegisterU32 { tile: 5, index: 0 }, ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::SharedU32 { elements: lanes },
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::RegisterU32 { elements: 1 },
],
lane_exprs,
ops: vec![
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 8,
},
TileOp::AssignScalarU32 {
tile: 3,
index: 0,
value: 9,
},
TileOp::LaneLoop {
extent: Extent::Push(0),
body: vec![
TileOp::AssignScalarU32 {
tile: 3,
index: 0,
value: 7,
},
TileOp::AssignScalar {
tile: 2,
index: 0,
value: 6,
},
],
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 11,
},
TileOp::AssignScalarU32 {
tile: 5,
index: 0,
value: 16,
},
TileOp::LaneIf {
cond: 19,
body: vec![
TileOp::StoreShared {
tile: 0,
offset: 20,
value: 12,
},
TileOp::StoreSharedU32 {
tile: 1,
offset: 20,
value: 37,
},
],
},
TileOp::Barrier,
TileOp::LaneIf {
cond: 28,
body: vec![
TileOp::AssignScalar {
tile: 6,
index: 0,
value: 26,
},
TileOp::AssignScalarU32 {
tile: 7,
index: 0,
value: 27,
},
TileOp::AssignScalar {
tile: 8,
index: 0,
value: 30,
},
TileOp::AssignScalarU32 {
tile: 9,
index: 0,
value: 35,
},
TileOp::LaneGuard {
lane: 9,
body: vec![TileOp::StoreScalarU32 {
binding: 1,
address: 9,
value: 36,
}],
},
],
},
],
})
}
pub(in crate::runtime) fn max_backward() -> Self {
use LaneExpr as E;
let lane_exprs = vec![
E::GroupX,
E::ConstU32(256),
E::MulU32(0, 1),
E::Lane,
E::AddU32(2, 3), E::Push(0), E::GeU32(4, 5), E::LoadF32 {
binding: 0,
address: 4,
}, E::ConstU32(0),
E::LoadF32 {
binding: 1,
address: 8,
}, E::LoadF32 {
binding: 2,
address: 8,
}, E::EqF32(7, 9), E::ConstF32(0x0000_0000), E::SelectF32 {
cond: 11,
if_true: 10,
if_false: 12,
},
];
Self::flat_program(
vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
vec!["element_count".into()],
Vec::new(),
lane_exprs,
vec![
TileOp::GuardIf { cond: 6 },
TileOp::StoreScalar {
binding: 3,
address: 4,
value: 13,
},
],
)
}
pub(in crate::runtime) fn channel_normalize(lanes: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 {
return Err(Error::invalid_argument(
"channel_normalize lanes must be a power of two within the workgroup floor",
));
}
use LaneExpr as E;
let lane_exprs = vec![
E::Push(0), E::Push(1), E::GroupX, E::MulU32(2, 1), E::Induction, E::AddU32(3, 4), E::LoadF32 {
binding: 0,
address: 5,
}, E::ConstF32(0x0000_0000), E::Lane, E::RegisterF32 { tile: 2, index: 0 }, E::ConstU32(0),
E::SharedF32 {
tile: 0,
offset: 10,
}, E::F32FromU32(1), E::DivF32(11, 12), E::RegisterF32 { tile: 3, index: 0 }, E::SubF32(6, 14), E::MulF32(15, 15), E::SharedF32 {
tile: 1,
offset: 10,
}, E::DivF32(17, 12), E::ConstF32(0x3727_c5ac), E::AddF32(18, 19), E::RsqrtF32(20), E::RegisterF32 { tile: 4, index: 0 }, E::MulF32(15, 22), ];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["channels".into(), "values_per_channel".into()],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneFold {
acc: 2,
init: 7,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 6,
serial: false,
},
TileOp::StoreShared {
tile: 0,
offset: 8,
value: 9,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 0,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 3,
index: 0,
value: 13,
},
TileOp::LaneFold {
acc: 2,
init: 7,
extent: Extent::Push(1),
op: LaneReduce::Sum,
element: 16,
serial: false,
},
TileOp::StoreShared {
tile: 1,
offset: 8,
value: 9,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 4,
index: 0,
value: 21,
},
TileOp::LaneLoop {
extent: Extent::Push(1),
body: vec![TileOp::StoreScalar {
binding: 1,
address: 5,
value: 23,
}],
},
],
})
}
pub(in crate::runtime) fn image_resize(nearest: bool) -> Result<Self> {
let mut lane_exprs: Vec<LaneExpr> = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
const P_BATCH: usize = 0;
const P_CHANNELS: usize = 1;
const P_IN_H: usize = 2;
const P_IN_W: usize = 3;
const P_OUT_H: usize = 4;
const P_OUT_W: usize = 5;
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let batch_channel = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let half = expr!(LaneExpr::ConstF32(0.5f32.to_bits()));
let one = expr!(LaneExpr::ConstU32(1));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(P_BATCH));
let channels = expr!(LaneExpr::Push(P_CHANNELS));
let in_height = expr!(LaneExpr::Push(P_IN_H));
let in_width = expr!(LaneExpr::Push(P_IN_W));
let out_height = expr!(LaneExpr::Push(P_OUT_H));
let out_width = expr!(LaneExpr::Push(P_OUT_W));
let batch_channels = expr!(LaneExpr::MulU32(batch, channels));
let x_over = expr!(LaneExpr::GeU32(out_x, out_width));
let y_over = expr!(LaneExpr::GeU32(out_y, out_height));
let z_over = expr!(LaneExpr::GeU32(batch_channel, batch_channels));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let out_x_f = expr!(LaneExpr::F32FromU32(out_x));
let out_y_f = expr!(LaneExpr::F32FromU32(out_y));
let in_width_f = expr!(LaneExpr::F32FromU32(in_width));
let in_height_f = expr!(LaneExpr::F32FromU32(in_height));
let out_width_f = expr!(LaneExpr::F32FromU32(out_width));
let out_height_f = expr!(LaneExpr::F32FromU32(out_height));
let in_plane = expr!(LaneExpr::MulU32(in_height, in_width));
let plane_base = expr!(LaneExpr::MulU32(batch_channel, in_plane));
let out_rows = expr!(LaneExpr::MulU32(batch_channel, out_height));
let out_row = expr!(LaneExpr::AddU32(out_rows, out_y));
let out_line = expr!(LaneExpr::MulU32(out_row, out_width));
let out_index = expr!(LaneExpr::AddU32(out_line, out_x));
let value = if nearest {
let x_scale = expr!(LaneExpr::MulF32(out_x_f, in_width_f));
let x_raw = expr!(LaneExpr::DivF32(x_scale, out_width_f));
let x_trunc = expr!(LaneExpr::U32FromF32(x_raw));
let x_last = expr!(LaneExpr::SubU32(in_width, one));
let in_x = expr!(LaneExpr::MinU32(x_trunc, x_last));
let y_scale = expr!(LaneExpr::MulF32(out_y_f, in_height_f));
let y_raw = expr!(LaneExpr::DivF32(y_scale, out_height_f));
let y_trunc = expr!(LaneExpr::U32FromF32(y_raw));
let y_last = expr!(LaneExpr::SubU32(in_height, one));
let in_y = expr!(LaneExpr::MinU32(y_trunc, y_last));
let row = expr!(LaneExpr::MulU32(in_y, in_width));
let offset = expr!(LaneExpr::AddU32(row, in_x));
let address = expr!(LaneExpr::AddU32(plane_base, offset));
expr!(LaneExpr::LoadF32 {
binding: 0,
address,
})
} else {
let x_centered = expr!(LaneExpr::AddF32(out_x_f, half));
let x_scaled = expr!(LaneExpr::MulF32(x_centered, in_width_f));
let x_ratio = expr!(LaneExpr::DivF32(x_scaled, out_width_f));
let in_x_f = expr!(LaneExpr::SubF32(x_ratio, half));
let y_centered = expr!(LaneExpr::AddF32(out_y_f, half));
let y_scaled = expr!(LaneExpr::MulF32(y_centered, in_height_f));
let y_ratio = expr!(LaneExpr::DivF32(y_scaled, out_height_f));
let in_y_f = expr!(LaneExpr::SubF32(y_ratio, half));
expr!(LaneExpr::BilinearF32 {
binding: 0,
base: plane_base,
xf: in_x_f,
yf: in_y_f,
width: in_width,
height: in_height,
})
};
let ops = vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: out_index,
value,
},
];
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops,
})
}
pub(in crate::runtime) fn image_flip() -> Self {
let mut lane_exprs: Vec<LaneExpr> = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
const P_BATCH: usize = 0;
const P_CHANNELS: usize = 1;
const P_HEIGHT: usize = 2;
const P_WIDTH: usize = 3;
const P_HORIZONTAL: usize = 4;
const P_VERTICAL: usize = 5;
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let plane = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let zero = expr!(LaneExpr::ConstU32(0));
let one = expr!(LaneExpr::ConstU32(1));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(P_BATCH));
let channels = expr!(LaneExpr::Push(P_CHANNELS));
let height = expr!(LaneExpr::Push(P_HEIGHT));
let width = expr!(LaneExpr::Push(P_WIDTH));
let horizontal = expr!(LaneExpr::Push(P_HORIZONTAL));
let vertical = expr!(LaneExpr::Push(P_VERTICAL));
let batch_channels = expr!(LaneExpr::MulU32(batch, channels));
let x_over = expr!(LaneExpr::GeU32(out_x, width));
let y_over = expr!(LaneExpr::GeU32(out_y, height));
let z_over = expr!(LaneExpr::GeU32(plane, batch_channels));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let flip_x = expr!(LaneExpr::NeqU32(horizontal, zero));
let width_last = expr!(LaneExpr::SubU32(width, one));
let mirrored_x = expr!(LaneExpr::SubU32(width_last, out_x));
let source_x = expr!(LaneExpr::SelectU32 {
cond: flip_x,
if_true: mirrored_x,
if_false: out_x,
});
let flip_y = expr!(LaneExpr::NeqU32(vertical, zero));
let height_last = expr!(LaneExpr::SubU32(height, one));
let mirrored_y = expr!(LaneExpr::SubU32(height_last, out_y));
let source_y = expr!(LaneExpr::SelectU32 {
cond: flip_y,
if_true: mirrored_y,
if_false: out_y,
});
let plane_base = expr!(LaneExpr::MulU32(plane, height));
let source_row = expr!(LaneExpr::AddU32(plane_base, source_y));
let source_line = expr!(LaneExpr::MulU32(source_row, width));
let source_index = expr!(LaneExpr::AddU32(source_line, source_x));
let out_row = expr!(LaneExpr::AddU32(plane_base, out_y));
let out_line = expr!(LaneExpr::MulU32(out_row, width));
let out_index = expr!(LaneExpr::AddU32(out_line, out_x));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: source_index,
});
let ops = vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: out_index,
value,
},
];
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
"horizontal".into(),
"vertical".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops,
}
}
pub(in crate::runtime) fn image_pad() -> Self {
let mut lane_exprs = Vec::new();
let frame = ImageGatherFrame::new(&mut lane_exprs);
let pad_top = lane(&mut lane_exprs, LaneExpr::Push(6));
let pad_left = lane(&mut lane_exprs, LaneExpr::Push(7));
let border_mode = lane(&mut lane_exprs, LaneExpr::Push(8));
let border_value = lane(&mut lane_exprs, LaneExpr::PushF32(0));
let value = lane(
&mut lane_exprs,
LaneExpr::PadFetchF32 {
binding: 0,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
pad_left,
pad_top,
width: frame.in_width,
height: frame.in_height,
mode: border_mode,
value: border_value,
},
);
frame.program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"pad_top".into(),
"pad_left".into(),
"border_mode".into(),
],
value,
lane_exprs,
)
}
pub(in crate::runtime) fn image_warp(perspective: bool) -> Self {
let mut lane_exprs = Vec::new();
let frame = ImageGatherFrame::new(&mut lane_exprs);
let interp = lane(&mut lane_exprs, LaneExpr::Push(6));
let border_mode = lane(&mut lane_exprs, LaneExpr::Push(7));
let border_value = lane(&mut lane_exprs, LaneExpr::PushF32(0));
let value = lane(
&mut lane_exprs,
LaneExpr::WarpF32 {
input: 0,
transform: 1,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
width: frame.in_width,
height: frame.in_height,
interp,
mode: border_mode,
value: border_value,
perspective,
},
);
frame.program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"interpolation".into(),
"border_mode".into(),
],
value,
lane_exprs,
)
}
pub(in crate::runtime) fn image_remap() -> Self {
let mut lane_exprs = Vec::new();
let frame = ImageGatherFrame::new(&mut lane_exprs);
let interp = lane(&mut lane_exprs, LaneExpr::Push(6));
let border_mode = lane(&mut lane_exprs, LaneExpr::Push(7));
let map_batch = lane(&mut lane_exprs, LaneExpr::Push(8));
let border_value = lane(&mut lane_exprs, LaneExpr::PushF32(0));
let batch = lane(
&mut lane_exprs,
LaneExpr::DivU32(frame.plane, frame.channels),
);
let value = lane(
&mut lane_exprs,
LaneExpr::RemapF32 {
input: 0,
map: 1,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
batch,
width: frame.in_width,
height: frame.in_height,
out_width: frame.out_width,
out_height: frame.out_height,
interp,
mode: border_mode,
map_batch,
value: border_value,
},
);
frame.program(
vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"interpolation".into(),
"border_mode".into(),
"map_batch".into(),
],
value,
lane_exprs,
)
}
pub(in crate::runtime) fn image_crop() -> Self {
let mut lane_exprs = Vec::new();
let frame = ImageGatherFrame::new_out_at(&mut lane_exprs, 6, 7);
let crop_x = lane(&mut lane_exprs, LaneExpr::Push(4));
let crop_y = lane(&mut lane_exprs, LaneExpr::Push(5));
let source_x = lane(&mut lane_exprs, LaneExpr::AddU32(crop_x, frame.out_x));
let source_y = lane(&mut lane_exprs, LaneExpr::AddU32(crop_y, frame.out_y));
let source_line = lane(&mut lane_exprs, LaneExpr::MulU32(source_y, frame.in_width));
let source_row = lane(
&mut lane_exprs,
LaneExpr::AddU32(frame.plane_base, source_line),
);
let source_index = lane(&mut lane_exprs, LaneExpr::AddU32(source_row, source_x));
let value = lane(
&mut lane_exprs,
LaneExpr::LoadF32 {
binding: 0,
address: source_index,
},
);
let mut program = frame.program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"crop_x".into(),
"crop_y".into(),
"output_height".into(),
"output_width".into(),
],
value,
lane_exprs,
);
program.push_f32_scalars.clear();
program
}
pub(in crate::runtime) fn image_rotate() -> Self {
let mut lane_exprs = Vec::new();
let frame = ImageGatherFrame::new(&mut lane_exprs);
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let degrees = expr!(LaneExpr::Push(6));
let one = expr!(LaneExpr::ConstU32(1));
let ninety = expr!(LaneExpr::ConstU32(90));
let one_eighty = expr!(LaneExpr::ConstU32(180));
let two_seventy = expr!(LaneExpr::ConstU32(270));
let w_last = expr!(LaneExpr::SubU32(frame.in_width, one));
let h_last = expr!(LaneExpr::SubU32(frame.in_height, one));
let flip_x = expr!(LaneExpr::SubU32(w_last, frame.out_x));
let flip_y = expr!(LaneExpr::SubU32(h_last, frame.out_y));
let mirror_y_on_x = expr!(LaneExpr::SubU32(h_last, frame.out_x));
let mirror_x_on_y = expr!(LaneExpr::SubU32(w_last, frame.out_y));
let d90 = expr!(LaneExpr::EqU32(degrees, ninety));
let d180 = expr!(LaneExpr::EqU32(degrees, one_eighty));
let d270 = expr!(LaneExpr::EqU32(degrees, two_seventy));
let sx_180_270_0 = expr!(LaneExpr::SelectU32 {
cond: d270,
if_true: mirror_x_on_y,
if_false: frame.out_x,
});
let sx_180 = expr!(LaneExpr::SelectU32 {
cond: d180,
if_true: flip_x,
if_false: sx_180_270_0,
});
let source_x = expr!(LaneExpr::SelectU32 {
cond: d90,
if_true: frame.out_y,
if_false: sx_180,
});
let sy_180_270_0 = expr!(LaneExpr::SelectU32 {
cond: d270,
if_true: frame.out_x,
if_false: frame.out_y,
});
let sy_180 = expr!(LaneExpr::SelectU32 {
cond: d180,
if_true: flip_y,
if_false: sy_180_270_0,
});
let source_y = expr!(LaneExpr::SelectU32 {
cond: d90,
if_true: mirror_y_on_x,
if_false: sy_180,
});
let source_line = expr!(LaneExpr::MulU32(source_y, frame.in_width));
let source_row = expr!(LaneExpr::AddU32(frame.plane_base, source_line));
let source_index = expr!(LaneExpr::AddU32(source_row, source_x));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: source_index,
});
let mut program = frame.program(
vec![Binding::Input(0), Binding::Output(0)],
vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
"degrees".into(),
],
value,
lane_exprs,
);
program.push_f32_scalars.clear();
program
}
fn filter_prelude(lane_exprs: &mut Vec<LaneExpr>) -> FilterPrelude {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let plane = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let two = expr!(LaneExpr::ConstU32(2));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(0));
let channels = expr!(LaneExpr::Push(1));
let height = expr!(LaneExpr::Push(2));
let width = expr!(LaneExpr::Push(3));
let mode = expr!(LaneExpr::Push(5));
let kernel_width = expr!(LaneExpr::Push(6));
let kernel_height = expr!(LaneExpr::Push(7));
let size = expr!(LaneExpr::Push(8));
let border_value = expr!(LaneExpr::PushF32(4));
let batch_channels = expr!(LaneExpr::MulU32(batch, channels));
let x_over = expr!(LaneExpr::GeU32(out_x, width));
let y_over = expr!(LaneExpr::GeU32(out_y, height));
let z_over = expr!(LaneExpr::GeU32(plane, batch_channels));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let plane_rows = expr!(LaneExpr::MulU32(plane, height));
let plane_base = expr!(LaneExpr::MulU32(plane_rows, width));
let radius_x = expr!(LaneExpr::DivU32(kernel_width, two));
let radius_y = expr!(LaneExpr::DivU32(kernel_height, two));
let kernel_extent = expr!(LaneExpr::MulU32(kernel_width, kernel_height));
let size_radius = expr!(LaneExpr::DivU32(size, two));
let size_extent = expr!(LaneExpr::MulU32(size, size));
let out_row = expr!(LaneExpr::MulU32(out_y, width));
let out_line = expr!(LaneExpr::AddU32(plane_base, out_row));
let out_index = expr!(LaneExpr::AddU32(out_line, out_x));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let zero = expr!(LaneExpr::ConstF32(0));
let flt_max = expr!(LaneExpr::ConstF32(0x7F7F_FFFF));
let neg_flt_max = expr!(LaneExpr::ConstF32(0xFF7F_FFFF));
FilterPrelude {
out_of_bounds,
zero_u32,
plane,
plane_base,
out_x,
out_y,
out_index,
height,
width,
mode,
kernel_width,
kernel_extent,
size,
size_radius,
size_extent,
radius_x,
radius_y,
border_value,
zero,
flt_max,
neg_flt_max,
}
}
const FILTER_PUSH_SCALARS: &[&str] = &[
"batch_size",
"channels",
"height",
"width",
"operation",
"border_mode",
"kernel_width",
"kernel_height",
"kernel_size",
"dx",
"dy",
];
pub(in crate::runtime) fn image_convolve() -> Self {
const P_BATCH: usize = 0;
const P_CHANNELS: usize = 1;
const P_HEIGHT: usize = 2;
const P_WIDTH: usize = 3;
const P_BORDER_MODE: usize = 5;
const P_KERNEL_WIDTH: usize = 6;
const P_KERNEL_HEIGHT: usize = 7;
const P_BORDER_VALUE: usize = 4;
use LaneExpr as E;
let lane_exprs = vec![
E::Push(P_BATCH),
E::Push(P_CHANNELS),
E::Push(P_HEIGHT),
E::Push(P_WIDTH),
E::Push(P_BORDER_MODE),
E::Push(P_KERNEL_WIDTH),
E::Push(P_KERNEL_HEIGHT),
E::PushF32(P_BORDER_VALUE),
E::GroupX,
E::GroupY,
E::GroupZ,
E::Lane,
E::ConstU32(16),
E::DivU32(11, 12), E::ModU32(11, 12), E::MulU32(8, 12), E::MulU32(9, 12), E::AddU32(15, 14), E::AddU32(16, 13), E::MulU32(0, 1), E::GeU32(17, 3), E::GeU32(18, 2), E::GeU32(10, 19), E::OrBool(20, 21),
E::OrBool(23, 22), E::MulU32(10, 2), E::MulU32(25, 3), E::ConstU32(2),
E::DivU32(5, 27), E::DivU32(6, 27), E::Induction, E::DivU32(30, 5), E::ModU32(30, 5), E::AddU32(17, 32), E::SubU32(33, 28), E::AddU32(18, 31), E::SubU32(35, 29),
E::BorderedFetchF32 {
binding: 0,
base: 26,
x: 34,
y: 36,
width: 3,
height: 2,
mode: 4,
value: 7,
}, E::LoadF32 {
binding: 1,
address: 30,
}, E::MulF32(37, 38), E::ConstF32(0), E::MulU32(5, 6), E::MulU32(18, 3), E::AddU32(26, 42), E::AddU32(43, 17), E::RegisterF32 { tile: 0, index: 0 }, ];
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
"operation".into(),
"border_mode".into(),
"kernel_width".into(),
"kernel_height".into(),
"kernel_size".into(),
"dx".into(),
"dy".into(),
],
push_f32_scalars: vec![
"p0".into(),
"p1".into(),
"p2".into(),
"p3".into(),
"border_value".into(),
],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
ops: vec![
TileOp::GuardIf { cond: 24 },
TileOp::LaneFold {
acc: 0,
init: 40,
extent: Extent::Expr(41),
op: LaneReduce::Sum,
element: 39,
serial: true,
},
TileOp::StoreScalar {
binding: 3,
address: 44,
value: 45,
},
],
}
}
pub(in crate::runtime) fn image_average_blur() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let i = expr!(LaneExpr::Induction);
let ky = expr!(LaneExpr::DivU32(i, frame.kernel_width));
let kx = expr!(LaneExpr::ModU32(i, frame.kernel_width));
let tap_xp = expr!(LaneExpr::AddU32(frame.out_x, kx));
let tap_x = expr!(LaneExpr::SubU32(tap_xp, frame.radius_x));
let tap_yp = expr!(LaneExpr::AddU32(frame.out_y, ky));
let tap_y = expr!(LaneExpr::SubU32(tap_yp, frame.radius_y));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: tap_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let count = expr!(LaneExpr::F32FromU32(frame.kernel_extent));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
let result = expr!(LaneExpr::DivF32(acc, count));
let mut program = Self::filter_program(
lane_exprs,
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
vec![TileOp::LaneFold {
acc: 0,
init: frame.zero,
extent: Extent::Expr(frame.kernel_extent),
op: LaneReduce::Sum,
element: tap,
serial: true,
}],
frame.out_of_bounds,
frame.out_index,
result,
);
program.bindings[1] = Binding::Input(1);
program.bindings[2] = Binding::Input(2);
program
}
pub(in crate::runtime) fn image_morphology_once(dilate: bool) -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let i = expr!(LaneExpr::Induction);
let ky = expr!(LaneExpr::DivU32(i, frame.kernel_width));
let kx = expr!(LaneExpr::ModU32(i, frame.kernel_width));
let tap_xp = expr!(LaneExpr::AddU32(frame.out_x, kx));
let tap_x = expr!(LaneExpr::SubU32(tap_xp, frame.radius_x));
let tap_yp = expr!(LaneExpr::AddU32(frame.out_y, ky));
let tap_y = expr!(LaneExpr::SubU32(tap_yp, frame.radius_y));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: tap_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
Self::filter_program(
lane_exprs,
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
vec![TileOp::LaneFold {
acc: 0,
init: if dilate {
frame.neg_flt_max
} else {
frame.flt_max
},
extent: Extent::Expr(frame.kernel_extent),
op: if dilate {
LaneReduce::Max
} else {
LaneReduce::Min
},
element: tap,
serial: true,
}],
frame.out_of_bounds,
frame.out_index,
acc,
)
}
pub(in crate::runtime) fn image_morphology_gradient() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let i = expr!(LaneExpr::Induction);
let ky = expr!(LaneExpr::DivU32(i, frame.kernel_width));
let kx = expr!(LaneExpr::ModU32(i, frame.kernel_width));
let tap_xp = expr!(LaneExpr::AddU32(frame.out_x, kx));
let tap_x = expr!(LaneExpr::SubU32(tap_xp, frame.radius_x));
let tap_yp = expr!(LaneExpr::AddU32(frame.out_y, ky));
let tap_y = expr!(LaneExpr::SubU32(tap_yp, frame.radius_y));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: tap_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let dilated = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
let eroded = expr!(LaneExpr::RegisterF32 { tile: 1, index: 0 });
let result = expr!(LaneExpr::SubF32(dilated, eroded));
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
vec![
TileOp::LaneFold {
acc: 0,
init: frame.neg_flt_max,
extent: Extent::Expr(frame.kernel_extent),
op: LaneReduce::Max,
element: tap,
serial: true,
},
TileOp::LaneFold {
acc: 1,
init: frame.flt_max,
extent: Extent::Expr(frame.kernel_extent),
op: LaneReduce::Min,
element: tap,
serial: true,
},
],
frame.out_of_bounds,
frame.out_index,
result,
)
}
pub(in crate::runtime) fn image_separable_convolve() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let k = expr!(LaneExpr::LoopVar(2));
let source_y = expr!(LaneExpr::AddU32(frame.out_y, k));
let shifted_y = expr!(LaneExpr::SubU32(source_y, frame.radius_y));
let mapped_y = expr!(LaneExpr::BorderCoordU32 {
coordinate: shifted_y,
extent: frame.height,
mode: frame.mode,
});
let neg_threshold = expr!(LaneExpr::ConstU32(0x8000_0000));
let mapped_neg = expr!(LaneExpr::GeU32(mapped_y, neg_threshold));
let i = expr!(LaneExpr::Induction);
let tap_xp = expr!(LaneExpr::AddU32(frame.out_x, i));
let tap_x = expr!(LaneExpr::SubU32(tap_xp, frame.radius_x));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: mapped_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let coeff_x = expr!(LaneExpr::LoadF32 {
binding: 1,
address: i,
});
let inner_term = expr!(LaneExpr::MulF32(tap, coeff_x));
let inner_acc = expr!(LaneExpr::RegisterF32 { tile: 1, index: 0 });
let intermediate = expr!(LaneExpr::SelectF32 {
cond: mapped_neg,
if_true: frame.border_value,
if_false: inner_acc,
});
let coeff_y = expr!(LaneExpr::LoadF32 {
binding: 2,
address: k,
});
let term = expr!(LaneExpr::MulF32(intermediate, coeff_y));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
let updated = expr!(LaneExpr::AddF32(acc, term));
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: frame.zero,
},
TileOp::LaneLoopRange {
start: frame.zero_u32,
step: 1,
extent: Extent::Push(7),
body: vec![
TileOp::LaneFold {
acc: 1,
init: frame.zero,
extent: Extent::Push(6),
op: LaneReduce::Sum,
element: inner_term,
serial: true,
},
TileOp::AssignScalar {
tile: 0,
index: 0,
value: updated,
},
],
},
],
frame.out_of_bounds,
frame.out_index,
acc,
)
}
fn morphology_twice_body(
lane_exprs: &mut Vec<LaneExpr>,
frame: &FilterPrelude,
first_dilate: bool,
) -> Vec<TileOp> {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let outer = expr!(LaneExpr::LoopVar(2));
let ky = expr!(LaneExpr::DivU32(outer, frame.kernel_width));
let kx = expr!(LaneExpr::ModU32(outer, frame.kernel_width));
let source_x = expr!(LaneExpr::AddU32(frame.out_x, kx));
let shifted_x = expr!(LaneExpr::SubU32(source_x, frame.radius_x));
let source_y = expr!(LaneExpr::AddU32(frame.out_y, ky));
let shifted_y = expr!(LaneExpr::SubU32(source_y, frame.radius_y));
let mapped_x = expr!(LaneExpr::BorderCoordU32 {
coordinate: shifted_x,
extent: frame.width,
mode: frame.mode,
});
let mapped_y = expr!(LaneExpr::BorderCoordU32 {
coordinate: shifted_y,
extent: frame.height,
mode: frame.mode,
});
let neg_threshold = expr!(LaneExpr::ConstU32(0x8000_0000));
let neg_x = expr!(LaneExpr::GeU32(mapped_x, neg_threshold));
let neg_y = expr!(LaneExpr::GeU32(mapped_y, neg_threshold));
let mapped_neg = expr!(LaneExpr::OrBool(neg_x, neg_y));
let i = expr!(LaneExpr::Induction);
let inner_ky = expr!(LaneExpr::DivU32(i, frame.kernel_width));
let inner_kx = expr!(LaneExpr::ModU32(i, frame.kernel_width));
let inner_xp = expr!(LaneExpr::AddU32(mapped_x, inner_kx));
let inner_x = expr!(LaneExpr::SubU32(inner_xp, frame.radius_x));
let inner_yp = expr!(LaneExpr::AddU32(mapped_y, inner_ky));
let inner_y = expr!(LaneExpr::SubU32(inner_yp, frame.radius_y));
let inner_tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: inner_x,
y: inner_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let inner_acc = expr!(LaneExpr::RegisterF32 { tile: 1, index: 0 });
let value = expr!(LaneExpr::SelectF32 {
cond: mapped_neg,
if_true: frame.border_value,
if_false: inner_acc,
});
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
let second_dilate = !first_dilate;
let updated = if second_dilate {
expr!(LaneExpr::MaxF32(acc, value))
} else {
expr!(LaneExpr::MinF32(acc, value))
};
vec![
TileOp::AssignScalar {
tile: 0,
index: 0,
value: if second_dilate {
frame.neg_flt_max
} else {
frame.flt_max
},
},
TileOp::LaneLoopRange {
start: frame.zero_u32,
step: 1,
extent: Extent::Expr(frame.kernel_extent),
body: vec![
TileOp::LaneFold {
acc: 1,
init: if first_dilate {
frame.neg_flt_max
} else {
frame.flt_max
},
extent: Extent::Expr(frame.kernel_extent),
op: if first_dilate {
LaneReduce::Max
} else {
LaneReduce::Min
},
element: inner_tap,
serial: true,
},
TileOp::AssignScalar {
tile: 0,
index: 0,
value: updated,
},
],
},
]
}
pub(in crate::runtime) fn image_morphology_twice(first_dilate: bool) -> Self {
let mut lane_exprs = Vec::new();
let frame = Self::filter_prelude(&mut lane_exprs);
let body = Self::morphology_twice_body(&mut lane_exprs, &frame, first_dilate);
let acc = lane(&mut lane_exprs, LaneExpr::RegisterF32 { tile: 0, index: 0 });
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
body,
frame.out_of_bounds,
frame.out_index,
acc,
)
}
pub(in crate::runtime) fn image_morphology_hat(top: bool) -> Self {
let mut lane_exprs = Vec::new();
let frame = Self::filter_prelude(&mut lane_exprs);
let body = Self::morphology_twice_body(&mut lane_exprs, &frame, !top);
let center = lane(
&mut lane_exprs,
LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
},
);
let twice = lane(&mut lane_exprs, LaneExpr::RegisterF32 { tile: 0, index: 0 });
let result = if top {
lane(&mut lane_exprs, LaneExpr::SubF32(center, twice))
} else {
lane(&mut lane_exprs, LaneExpr::SubF32(twice, center))
};
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
body,
frame.out_of_bounds,
frame.out_index,
result,
)
}
fn fixed_linear_tap(
lane_exprs: &mut Vec<LaneExpr>,
frame: &FilterPrelude,
) -> (usize, usize, usize, usize) {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let i = expr!(LaneExpr::Induction);
let three = expr!(LaneExpr::ConstU32(3));
let one = expr!(LaneExpr::ConstU32(1));
let id3 = expr!(LaneExpr::DivU32(i, three));
let im3 = expr!(LaneExpr::ModU32(i, three));
let tap_xp = expr!(LaneExpr::AddU32(frame.out_x, im3));
let tap_x = expr!(LaneExpr::SubU32(tap_xp, one));
let tap_yp = expr!(LaneExpr::AddU32(frame.out_y, id3));
let tap_y = expr!(LaneExpr::SubU32(tap_yp, one));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: tap_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let nine = expr!(LaneExpr::ConstU32(9));
(tap, im3, id3, nine)
}
fn signed_axis(lane_exprs: &mut Vec<LaneExpr>, axis: usize) -> usize {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let two_u32 = expr!(LaneExpr::ConstU32(2));
let neg_one = expr!(LaneExpr::ConstF32(0xBF80_0000));
let pos_one = expr!(LaneExpr::ConstF32(0x3F80_0000));
let zero = expr!(LaneExpr::ConstF32(0));
let is_neg = expr!(LaneExpr::EqU32(axis, zero_u32));
let is_pos = expr!(LaneExpr::EqU32(axis, two_u32));
let pos_or_zero = expr!(LaneExpr::SelectF32 {
cond: is_pos,
if_true: pos_one,
if_false: zero,
});
expr!(LaneExpr::SelectF32 {
cond: is_neg,
if_true: neg_one,
if_false: pos_or_zero,
})
}
fn centered_axis(
lane_exprs: &mut Vec<LaneExpr>,
axis: usize,
center_weight: usize,
edge_weight: usize,
) -> usize {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let one_u32 = expr!(LaneExpr::ConstU32(1));
let centered = expr!(LaneExpr::EqU32(axis, one_u32));
expr!(LaneExpr::SelectF32 {
cond: centered,
if_true: center_weight,
if_false: edge_weight,
})
}
pub(in crate::runtime) fn image_directional_linear(center: u32, edge: u32) -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, im3, id3, nine) = Self::fixed_linear_tap(&mut lane_exprs, &frame);
let center_w = expr!(LaneExpr::ConstF32(center));
let edge_w = expr!(LaneExpr::ConstF32(edge));
let sign_kx = Self::signed_axis(&mut lane_exprs, im3);
let sign_ky = Self::signed_axis(&mut lane_exprs, id3);
let w_ky = Self::centered_axis(&mut lane_exprs, id3, center_w, edge_w);
let w_kx = Self::centered_axis(&mut lane_exprs, im3, center_w, edge_w);
let one_u32 = expr!(LaneExpr::ConstU32(1));
let dx = expr!(LaneExpr::Push(9));
let is_dx = expr!(LaneExpr::EqU32(dx, one_u32));
let x_coeff = expr!(LaneExpr::MulF32(sign_kx, w_ky));
let y_coeff = expr!(LaneExpr::MulF32(sign_ky, w_kx));
let coeff = expr!(LaneExpr::SelectF32 {
cond: is_dx,
if_true: x_coeff,
if_false: y_coeff,
});
let element = expr!(LaneExpr::MulF32(tap, coeff));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
Self::filter_program(
lane_exprs,
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
vec![TileOp::LaneFold {
acc: 0,
init: frame.zero,
extent: Extent::Expr(nine),
op: LaneReduce::Sum,
element,
serial: true,
}],
frame.out_of_bounds,
frame.out_index,
acc,
)
}
pub(in crate::runtime) fn image_laplacian() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, im3, id3, nine) = Self::fixed_linear_tap(&mut lane_exprs, &frame);
let one_u32 = expr!(LaneExpr::ConstU32(1));
let x_center = expr!(LaneExpr::EqU32(im3, one_u32));
let y_center = expr!(LaneExpr::EqU32(id3, one_u32));
let centered = expr!(LaneExpr::AndBool(x_center, y_center));
let edge = expr!(LaneExpr::OrBool(x_center, y_center));
let neg_four = expr!(LaneExpr::ConstF32(0xC080_0000));
let pos_one = expr!(LaneExpr::ConstF32(0x3F80_0000));
let zero = expr!(LaneExpr::ConstF32(0));
let edge_or_zero = expr!(LaneExpr::SelectF32 {
cond: edge,
if_true: pos_one,
if_false: zero,
});
let coeff = expr!(LaneExpr::SelectF32 {
cond: centered,
if_true: neg_four,
if_false: edge_or_zero,
});
let element = expr!(LaneExpr::MulF32(tap, coeff));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
Self::filter_program(
lane_exprs,
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
vec![TileOp::LaneFold {
acc: 0,
init: frame.zero,
extent: Extent::Expr(nine),
op: LaneReduce::Sum,
element,
serial: true,
}],
frame.out_of_bounds,
frame.out_index,
acc,
)
}
pub(in crate::runtime) fn image_sharpen() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, im3, id3, nine) = Self::fixed_linear_tap(&mut lane_exprs, &frame);
let one_u32 = expr!(LaneExpr::ConstU32(1));
let x_center = expr!(LaneExpr::EqU32(im3, one_u32));
let y_center = expr!(LaneExpr::EqU32(id3, one_u32));
let centered = expr!(LaneExpr::AndBool(x_center, y_center));
let edge = expr!(LaneExpr::OrBool(x_center, y_center));
let amount = expr!(LaneExpr::PushF32(0));
let four = expr!(LaneExpr::ConstF32(0x4080_0000));
let one = expr!(LaneExpr::ConstF32(0x3F80_0000));
let scaled = expr!(LaneExpr::MulF32(four, amount));
let center_w = expr!(LaneExpr::AddF32(one, scaled));
let neg_amount = expr!(LaneExpr::SubF32(frame.zero, amount));
let zero = expr!(LaneExpr::ConstF32(0));
let edge_or_zero = expr!(LaneExpr::SelectF32 {
cond: edge,
if_true: neg_amount,
if_false: zero,
});
let coeff = expr!(LaneExpr::SelectF32 {
cond: centered,
if_true: center_w,
if_false: edge_or_zero,
});
let element = expr!(LaneExpr::MulF32(tap, coeff));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
Self::filter_program(
lane_exprs,
vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
vec![TileOp::LaneFold {
acc: 0,
init: frame.zero,
extent: Extent::Expr(nine),
op: LaneReduce::Sum,
element,
serial: true,
}],
frame.out_of_bounds,
frame.out_index,
acc,
)
}
fn neighborhood_tap(lane_exprs: &mut Vec<LaneExpr>, frame: &FilterPrelude) -> (usize, usize) {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let i = expr!(LaneExpr::Induction);
let ky = expr!(LaneExpr::DivU32(i, frame.size));
let kx = expr!(LaneExpr::ModU32(i, frame.size));
let oy = expr!(LaneExpr::SubU32(ky, frame.size_radius));
let ox = expr!(LaneExpr::SubU32(kx, frame.size_radius));
let tap_x = expr!(LaneExpr::AddU32(frame.out_x, ox));
let tap_y = expr!(LaneExpr::AddU32(frame.out_y, oy));
let tap = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: tap_x,
y: tap_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let ox_sq = expr!(LaneExpr::MulU32(ox, ox));
let oy_sq = expr!(LaneExpr::MulU32(oy, oy));
let dist_u32 = expr!(LaneExpr::AddU32(ox_sq, oy_sq));
let dist = expr!(LaneExpr::F32FromU32(dist_u32));
(tap, dist)
}
fn weighted_folds(
lane_exprs: &mut Vec<LaneExpr>,
frame: &FilterPrelude,
element: usize,
weight: usize,
) -> (usize, usize, Vec<TileOp>) {
let weighted = lane(lane_exprs, LaneExpr::RegisterF32 { tile: 0, index: 0 });
let weight_sum = lane(lane_exprs, LaneExpr::RegisterF32 { tile: 1, index: 0 });
let ops = vec![
TileOp::LaneFold {
acc: 0,
init: frame.zero,
extent: Extent::Expr(frame.size_extent),
op: LaneReduce::Sum,
element,
serial: true,
},
TileOp::LaneFold {
acc: 1,
init: frame.zero,
extent: Extent::Expr(frame.size_extent),
op: LaneReduce::Sum,
element: weight,
serial: true,
},
];
(weighted, weight_sum, ops)
}
fn gaussian_weight(
lane_exprs: &mut Vec<LaneExpr>,
frame: &FilterPrelude,
dist: usize,
sigma_index: usize,
) -> usize {
macro_rules! expr {
($e:expr) => {
lane(lane_exprs, $e)
};
}
let two = expr!(LaneExpr::ConstF32(0x4000_0000));
let sigma = expr!(LaneExpr::PushF32(sigma_index));
let two_sigma = expr!(LaneExpr::MulF32(two, sigma));
let denom = expr!(LaneExpr::MulF32(two_sigma, sigma));
let ratio = expr!(LaneExpr::DivF32(dist, denom));
let neg = expr!(LaneExpr::SubF32(frame.zero, ratio));
expr!(LaneExpr::ExpF32(neg))
}
pub(in crate::runtime) fn image_gaussian() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, dist) = Self::neighborhood_tap(&mut lane_exprs, &frame);
let weight = Self::gaussian_weight(&mut lane_exprs, &frame, dist, 0);
let element = expr!(LaneExpr::MulF32(tap, weight));
let (weighted, weight_sum, ops) =
Self::weighted_folds(&mut lane_exprs, &frame, element, weight);
let eps = expr!(LaneExpr::ConstF32(0x3586_37BD));
let denom = expr!(LaneExpr::MaxF32(weight_sum, eps));
let result = expr!(LaneExpr::DivF32(weighted, denom));
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
ops,
frame.out_of_bounds,
frame.out_index,
result,
)
}
pub(in crate::runtime) fn image_bilateral() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, dist) = Self::neighborhood_tap(&mut lane_exprs, &frame);
let center = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let color = expr!(LaneExpr::SubF32(tap, center));
let color_sq = expr!(LaneExpr::MulF32(color, color));
let two = expr!(LaneExpr::ConstF32(0x4000_0000));
let sigma_space = expr!(LaneExpr::PushF32(1));
let two_space = expr!(LaneExpr::MulF32(two, sigma_space));
let space_denom = expr!(LaneExpr::MulF32(two_space, sigma_space));
let space_ratio = expr!(LaneExpr::DivF32(dist, space_denom));
let sigma_color = expr!(LaneExpr::PushF32(0));
let two_color = expr!(LaneExpr::MulF32(two, sigma_color));
let color_denom = expr!(LaneExpr::MulF32(two_color, sigma_color));
let color_ratio = expr!(LaneExpr::DivF32(color_sq, color_denom));
let neg_space = expr!(LaneExpr::SubF32(frame.zero, space_ratio));
let exponent = expr!(LaneExpr::SubF32(neg_space, color_ratio));
let weight = expr!(LaneExpr::ExpF32(exponent));
let element = expr!(LaneExpr::MulF32(tap, weight));
let (weighted, weight_sum, ops) =
Self::weighted_folds(&mut lane_exprs, &frame, element, weight);
let eps = expr!(LaneExpr::ConstF32(0x3586_37BD));
let denom = expr!(LaneExpr::MaxF32(weight_sum, eps));
let result = expr!(LaneExpr::DivF32(weighted, denom));
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
ops,
frame.out_of_bounds,
frame.out_index,
result,
)
}
pub(in crate::runtime) fn image_unsharp() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, dist) = Self::neighborhood_tap(&mut lane_exprs, &frame);
let weight = Self::gaussian_weight(&mut lane_exprs, &frame, dist, 0);
let element = expr!(LaneExpr::MulF32(tap, weight));
let (weighted, weight_sum, ops) =
Self::weighted_folds(&mut lane_exprs, &frame, element, weight);
let eps = expr!(LaneExpr::ConstF32(0x3586_37BD));
let denom = expr!(LaneExpr::MaxF32(weight_sum, eps));
let blurred = expr!(LaneExpr::DivF32(weighted, denom));
let center = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let amount = expr!(LaneExpr::PushF32(1));
let diff = expr!(LaneExpr::SubF32(center, blurred));
let scaled = expr!(LaneExpr::MulF32(amount, diff));
let result = expr!(LaneExpr::AddF32(center, scaled));
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
ops,
frame.out_of_bounds,
frame.out_index,
result,
)
}
pub(in crate::runtime) fn image_adaptive_threshold(gaussian: bool) -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let (tap, dist) = Self::neighborhood_tap(&mut lane_exprs, &frame);
let weight = if gaussian {
Self::gaussian_weight(&mut lane_exprs, &frame, dist, 2)
} else {
expr!(LaneExpr::ConstF32(0x3F80_0000))
};
let element = expr!(LaneExpr::MulF32(tap, weight));
let (weighted, weight_sum, ops) =
Self::weighted_folds(&mut lane_exprs, &frame, element, weight);
let eps = expr!(LaneExpr::ConstF32(0x3586_37BD));
let denom = expr!(LaneExpr::MaxF32(weight_sum, eps));
let filtered = expr!(LaneExpr::DivF32(weighted, denom));
let center = expr!(LaneExpr::BorderedFetchF32 {
binding: 0,
base: frame.plane_base,
x: frame.out_x,
y: frame.out_y,
width: frame.width,
height: frame.height,
mode: frame.mode,
value: frame.border_value,
});
let c = expr!(LaneExpr::PushF32(0));
let bound = expr!(LaneExpr::SubF32(filtered, c));
let above = expr!(LaneExpr::GtF32(center, bound));
let max_value = expr!(LaneExpr::PushF32(1));
let result = expr!(LaneExpr::SelectF32 {
cond: above,
if_true: max_value,
if_false: frame.zero,
});
Self::filter_program(
lane_exprs,
vec![
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
ops,
frame.out_of_bounds,
frame.out_index,
result,
)
}
pub(in crate::runtime) fn image_pointwise(operation: u32) -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let wide = expr!(LaneExpr::ConstU32(256));
let tile_base = expr!(LaneExpr::MulU32(group_x, wide));
let lane_id = expr!(LaneExpr::Lane);
let index = expr!(LaneExpr::AddU32(tile_base, lane_id));
let elements = expr!(LaneExpr::Push(0));
let out_of_bounds = expr!(LaneExpr::GeU32(index, elements));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: index,
});
let p0 = expr!(LaneExpr::PushF32(0));
let p1 = expr!(LaneExpr::PushF32(1));
let p2 = expr!(LaneExpr::PushF32(2));
let zero = expr!(LaneExpr::ConstF32(0));
let result = match operation {
0 => {
let above = expr!(LaneExpr::GtF32(value, p0));
expr!(LaneExpr::SelectF32 {
cond: above,
if_true: p1,
if_false: zero,
})
}
1 => {
let above = expr!(LaneExpr::GtF32(value, p0));
expr!(LaneExpr::SelectF32 {
cond: above,
if_true: zero,
if_false: p1,
})
}
2 => expr!(LaneExpr::MinF32(value, p0)),
3 => {
let above = expr!(LaneExpr::GtF32(value, p0));
expr!(LaneExpr::SelectF32 {
cond: above,
if_true: value,
if_false: zero,
})
}
4 => {
let above = expr!(LaneExpr::GtF32(value, p0));
expr!(LaneExpr::SelectF32 {
cond: above,
if_true: zero,
if_false: value,
})
}
5 => {
let lower = expr!(LaneExpr::GeF32(value, p0));
let upper = expr!(LaneExpr::GeF32(p1, value));
let inside = expr!(LaneExpr::AndBool(lower, upper));
expr!(LaneExpr::SelectF32 {
cond: inside,
if_true: p2,
if_false: zero,
})
}
6 => {
let lo = expr!(LaneExpr::MaxF32(value, p0));
expr!(LaneExpr::MinF32(lo, p1))
}
7 => expr!(LaneExpr::SubF32(p0, value)),
8 => {
let scaled = expr!(LaneExpr::MulF32(value, p0));
expr!(LaneExpr::AddF32(scaled, p1))
}
9 => {
let base = expr!(LaneExpr::MaxF32(value, zero));
let raised = expr!(LaneExpr::PowF32(base, p0));
expr!(LaneExpr::MulF32(raised, p1))
}
10 => {
let above = expr!(LaneExpr::GeF32(value, p0));
let mirrored = expr!(LaneExpr::SubF32(p1, value));
expr!(LaneExpr::SelectF32 {
cond: above,
if_true: mirrored,
if_false: value,
})
}
11 => {
let two = expr!(LaneExpr::ConstF32(0x4000_0000));
let levels = expr!(LaneExpr::MaxF32(p0, two));
let one = expr!(LaneExpr::ConstF32(0x3F80_0000));
let half = expr!(LaneExpr::ConstF32(0x3F00_0000));
let range = expr!(LaneExpr::SubF32(p2, p1));
let shifted = expr!(LaneExpr::SubF32(value, p1));
let normalized = expr!(LaneExpr::DivF32(shifted, range));
let clamp_lo = expr!(LaneExpr::MaxF32(normalized, zero));
let unit = expr!(LaneExpr::MinF32(clamp_lo, one));
let last = expr!(LaneExpr::SubF32(levels, one));
let scaled = expr!(LaneExpr::MulF32(unit, last));
let rounded = expr!(LaneExpr::AddF32(scaled, half));
let bucket = expr!(LaneExpr::FloorF32(rounded));
let fraction = expr!(LaneExpr::DivF32(bucket, last));
let rescaled = expr!(LaneExpr::MulF32(fraction, range));
expr!(LaneExpr::AddF32(rescaled, p1))
}
_ => value,
};
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec!["element_count".into(), "operation".into()],
push_f32_scalars: vec!["p0".into(), "p1".into(), "p2".into(), "p3".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 1,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: index,
value: result,
},
],
}
}
pub(in crate::runtime) fn image_segmentation_overlay() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let channels = expr!(LaneExpr::Push(1));
let class_count = expr!(LaneExpr::Push(4));
let alpha = expr!(LaneExpr::PushF32(0));
let batch = expr!(LaneExpr::DivU32(frame.plane, channels));
let channel = expr!(LaneExpr::ModU32(frame.plane, channels));
let batch_rows = expr!(LaneExpr::MulU32(batch, frame.height));
let batch_row = expr!(LaneExpr::AddU32(batch_rows, frame.out_y));
let pixel_row = expr!(LaneExpr::MulU32(batch_row, frame.width));
let pixel = expr!(LaneExpr::AddU32(pixel_row, frame.out_x));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: frame.out_index,
});
let label = expr!(LaneExpr::LoadU32 {
binding: 1,
address: pixel,
});
let label_ok = expr!(LaneExpr::LtU32(label, class_count));
let three = expr!(LaneExpr::ConstU32(3));
let chan_ok = expr!(LaneExpr::LtU32(channel, three));
let blend_ok = expr!(LaneExpr::AndBool(label_ok, chan_ok));
let label_rgb = expr!(LaneExpr::MulU32(label, three));
let palette_addr = expr!(LaneExpr::AddU32(label_rgb, channel));
let color = expr!(LaneExpr::LoadF32 {
binding: 2,
address: palette_addr,
});
let one = expr!(LaneExpr::ConstF32(0x3F80_0000));
let keep = expr!(LaneExpr::SubF32(one, alpha));
let kept = expr!(LaneExpr::MulF32(value, keep));
let tinted = expr!(LaneExpr::MulF32(color, alpha));
let blended = expr!(LaneExpr::AddF32(kept, tinted));
let result = expr!(LaneExpr::SelectF32 {
cond: blend_ok,
if_true: blended,
if_false: value,
});
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
"class_count".into(),
],
push_f32_scalars: vec!["alpha".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: frame.out_of_bounds,
},
TileOp::StoreScalar {
binding: 3,
address: frame.out_index,
value: result,
},
],
}
}
pub(in crate::runtime) fn image_normalize() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let channels = expr!(LaneExpr::Push(1));
let channel = expr!(LaneExpr::ModU32(frame.plane, channels));
let one = expr!(LaneExpr::ConstU32(1));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let is_r = expr!(LaneExpr::EqU32(channel, zero_u32));
let is_g = expr!(LaneExpr::EqU32(channel, one));
let mean_0 = expr!(LaneExpr::PushF32(0));
let mean_1 = expr!(LaneExpr::PushF32(1));
let mean_2 = expr!(LaneExpr::PushF32(2));
let std_0 = expr!(LaneExpr::PushF32(3));
let std_1 = expr!(LaneExpr::PushF32(4));
let std_2 = expr!(LaneExpr::PushF32(5));
let mean_gb = expr!(LaneExpr::SelectF32 {
cond: is_g,
if_true: mean_1,
if_false: mean_2,
});
let mean = expr!(LaneExpr::SelectF32 {
cond: is_r,
if_true: mean_0,
if_false: mean_gb,
});
let std_gb = expr!(LaneExpr::SelectF32 {
cond: is_g,
if_true: std_1,
if_false: std_2,
});
let stddev = expr!(LaneExpr::SelectF32 {
cond: is_r,
if_true: std_0,
if_false: std_gb,
});
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: frame.out_index,
});
let shifted = expr!(LaneExpr::SubF32(value, mean));
let result = expr!(LaneExpr::DivF32(shifted, stddev));
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
],
push_f32_scalars: vec![
"mean_0".into(),
"mean_1".into(),
"mean_2".into(),
"std_0".into(),
"std_1".into(),
"std_2".into(),
],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: frame.out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: frame.out_index,
value: result,
},
],
}
}
pub(in crate::runtime) fn image_convert_color() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let plane = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let three = expr!(LaneExpr::ConstU32(3));
let two = expr!(LaneExpr::ConstU32(2));
let one = expr!(LaneExpr::ConstU32(1));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(0));
let height = expr!(LaneExpr::Push(1));
let width = expr!(LaneExpr::Push(2));
let swap = expr!(LaneExpr::Push(3));
let batch_planes = expr!(LaneExpr::MulU32(batch, three));
let x_over = expr!(LaneExpr::GeU32(out_x, width));
let y_over = expr!(LaneExpr::GeU32(out_y, height));
let z_over = expr!(LaneExpr::GeU32(plane, batch_planes));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let batch_i = expr!(LaneExpr::DivU32(plane, three));
let channel = expr!(LaneExpr::ModU32(plane, three));
let swapping = expr!(LaneExpr::NeqU32(swap, zero_u32));
let not_green = expr!(LaneExpr::NeqU32(channel, one));
let swap_this = expr!(LaneExpr::AndBool(swapping, not_green));
let mirrored = expr!(LaneExpr::SubU32(two, channel));
let source_channel = expr!(LaneExpr::SelectU32 {
cond: swap_this,
if_true: mirrored,
if_false: channel,
});
let plane_elements = expr!(LaneExpr::MulU32(height, width));
let pixel_row = expr!(LaneExpr::MulU32(out_y, width));
let pixel = expr!(LaneExpr::AddU32(pixel_row, out_x));
let batch_base = expr!(LaneExpr::MulU32(batch_i, three));
let source_plane = expr!(LaneExpr::AddU32(batch_base, source_channel));
let source_base = expr!(LaneExpr::MulU32(source_plane, plane_elements));
let source = expr!(LaneExpr::AddU32(source_base, pixel));
let target_base = expr!(LaneExpr::MulU32(plane, plane_elements));
let target = expr!(LaneExpr::AddU32(target_base, pixel));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: source,
});
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"height".into(),
"width".into(),
"swap_red_blue".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: target,
value,
},
],
}
}
pub(in crate::runtime) fn image_resize_normalize() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let plane = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let three = expr!(LaneExpr::ConstU32(3));
let two = expr!(LaneExpr::ConstU32(2));
let one = expr!(LaneExpr::ConstU32(1));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let half = expr!(LaneExpr::ConstF32(0x3F00_0000));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch = expr!(LaneExpr::Push(0));
let channels = expr!(LaneExpr::Push(1));
let in_height = expr!(LaneExpr::Push(2));
let in_width = expr!(LaneExpr::Push(3));
let out_height = expr!(LaneExpr::Push(4));
let out_width = expr!(LaneExpr::Push(5));
let batch_channels = expr!(LaneExpr::MulU32(batch, channels));
let x_over = expr!(LaneExpr::GeU32(out_x, out_width));
let y_over = expr!(LaneExpr::GeU32(out_y, out_height));
let z_over = expr!(LaneExpr::GeU32(plane, batch_channels));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let channel = expr!(LaneExpr::ModU32(plane, channels));
let batch_i = expr!(LaneExpr::DivU32(plane, channels));
let swap = expr!(LaneExpr::PushBitsU32(6));
let swapping = expr!(LaneExpr::NeqU32(swap, zero_u32));
let rgb = expr!(LaneExpr::LtU32(channel, three));
let swap_this = expr!(LaneExpr::AndBool(swapping, rgb));
let mirrored = expr!(LaneExpr::SubU32(two, channel));
let source_channel = expr!(LaneExpr::SelectU32 {
cond: swap_this,
if_true: mirrored,
if_false: channel,
});
let batch_base = expr!(LaneExpr::MulU32(batch_i, channels));
let source_plane = expr!(LaneExpr::AddU32(batch_base, source_channel));
let in_plane = expr!(LaneExpr::MulU32(in_height, in_width));
let base = expr!(LaneExpr::MulU32(source_plane, in_plane));
let out_x_f = expr!(LaneExpr::F32FromU32(out_x));
let out_y_f = expr!(LaneExpr::F32FromU32(out_y));
let in_width_f = expr!(LaneExpr::F32FromU32(in_width));
let in_height_f = expr!(LaneExpr::F32FromU32(in_height));
let out_width_f = expr!(LaneExpr::F32FromU32(out_width));
let out_height_f = expr!(LaneExpr::F32FromU32(out_height));
let x_centered = expr!(LaneExpr::AddF32(out_x_f, half));
let x_scaled = expr!(LaneExpr::MulF32(x_centered, in_width_f));
let x_ratio = expr!(LaneExpr::DivF32(x_scaled, out_width_f));
let xf = expr!(LaneExpr::SubF32(x_ratio, half));
let y_centered = expr!(LaneExpr::AddF32(out_y_f, half));
let y_scaled = expr!(LaneExpr::MulF32(y_centered, in_height_f));
let y_ratio = expr!(LaneExpr::DivF32(y_scaled, out_height_f));
let yf = expr!(LaneExpr::SubF32(y_ratio, half));
let value = expr!(LaneExpr::BilinearF32 {
binding: 0,
base,
xf,
yf,
width: in_width,
height: in_height,
});
let is_r = expr!(LaneExpr::EqU32(channel, zero_u32));
let is_g = expr!(LaneExpr::EqU32(channel, one));
let mean_0 = expr!(LaneExpr::PushF32(0));
let mean_1 = expr!(LaneExpr::PushF32(1));
let mean_2 = expr!(LaneExpr::PushF32(2));
let std_0 = expr!(LaneExpr::PushF32(3));
let std_1 = expr!(LaneExpr::PushF32(4));
let std_2 = expr!(LaneExpr::PushF32(5));
let mean_gb = expr!(LaneExpr::SelectF32 {
cond: is_g,
if_true: mean_1,
if_false: mean_2,
});
let mean = expr!(LaneExpr::SelectF32 {
cond: is_r,
if_true: mean_0,
if_false: mean_gb,
});
let std_gb = expr!(LaneExpr::SelectF32 {
cond: is_g,
if_true: std_1,
if_false: std_2,
});
let stddev = expr!(LaneExpr::SelectF32 {
cond: is_r,
if_true: std_0,
if_false: std_gb,
});
let shifted = expr!(LaneExpr::SubF32(value, mean));
let result = expr!(LaneExpr::DivF32(shifted, stddev));
let out_rows = expr!(LaneExpr::MulU32(plane, out_height));
let out_row = expr!(LaneExpr::AddU32(out_rows, out_y));
let out_line = expr!(LaneExpr::MulU32(out_row, out_width));
let out_index = expr!(LaneExpr::AddU32(out_line, out_x));
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"input_height".into(),
"input_width".into(),
"output_height".into(),
"output_width".into(),
],
push_f32_scalars: vec![
"mean_0".into(),
"mean_1".into(),
"mean_2".into(),
"std_0".into(),
"std_1".into(),
"std_2".into(),
"swap_rb".into(),
],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: out_index,
value: result,
},
],
}
}
pub(in crate::runtime) fn audio_mel_filterbank() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let frame = expr!(LaneExpr::GroupY);
let channel = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let thirty_two = expr!(LaneExpr::ConstU32(32));
let tile_mel = expr!(LaneExpr::MulU32(group_x, thirty_two));
let mel = expr!(LaneExpr::AddU32(tile_mel, lane_id));
let channels = expr!(LaneExpr::Push(0));
let frames = expr!(LaneExpr::Push(1));
let freq_bins = expr!(LaneExpr::Push(2));
let num_mels = expr!(LaneExpr::Push(3));
let log_scale = expr!(LaneExpr::Push(4));
let mel_over = expr!(LaneExpr::GeU32(mel, num_mels));
let frame_over = expr!(LaneExpr::GeU32(frame, frames));
let channel_over = expr!(LaneExpr::GeU32(channel, channels));
let xy_over = expr!(LaneExpr::OrBool(mel_over, frame_over));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, channel_over));
let channel_frames = expr!(LaneExpr::MulU32(channel, frames));
let input_row = expr!(LaneExpr::AddU32(channel_frames, frame));
let input_base = expr!(LaneExpr::MulU32(input_row, freq_bins));
let filter_base = expr!(LaneExpr::MulU32(mel, freq_bins));
let i = expr!(LaneExpr::Induction);
let input_index = expr!(LaneExpr::AddU32(input_base, i));
let filter_index = expr!(LaneExpr::AddU32(filter_base, i));
let sample = expr!(LaneExpr::LoadF32 {
binding: 0,
address: input_index,
});
let weight = expr!(LaneExpr::LoadF32 {
binding: 2,
address: filter_index,
});
let element = expr!(LaneExpr::MulF32(sample, weight));
let zero = expr!(LaneExpr::ConstF32(0));
let acc = expr!(LaneExpr::RegisterF32 { tile: 0, index: 0 });
let epsilon = expr!(LaneExpr::ConstF32(0x3089_705F));
let shifted = expr!(LaneExpr::AddF32(acc, epsilon));
let logged = expr!(LaneExpr::LogF32(shifted));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let scaled = expr!(LaneExpr::NeqU32(log_scale, zero_u32));
let value = expr!(LaneExpr::SelectF32 {
cond: scaled,
if_true: logged,
if_false: acc,
});
let channel_mels = expr!(LaneExpr::MulU32(channel, num_mels));
let out_row = expr!(LaneExpr::AddU32(channel_mels, mel));
let out_base = expr!(LaneExpr::MulU32(out_row, frames));
let out_index = expr!(LaneExpr::AddU32(out_base, frame));
Self {
bindings: vec![Binding::Input(0), Binding::Output(0), Binding::Input(1)],
push_scalars: vec![
"channels".into(),
"frames".into(),
"freq_bins".into(),
"num_mels".into(),
"log_scale".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 32,
thread_columns: 32,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: vec![TileDecl::Register {
rows: 1,
columns: 1,
}],
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::LaneFold {
acc: 0,
init: zero,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element,
serial: true,
},
TileOp::StoreScalar {
binding: 1,
address: out_index,
value,
},
],
}
}
pub(in crate::runtime) fn audio_mel_chain(lanes: u32, capacity: u32) -> Result<Self> {
if !lanes.is_power_of_two() || lanes > 1024 || capacity < lanes {
return Err(Error::invalid_argument(
"audio_mel_chain lanes must be a power of two within the workgroup floor and capacity must cover the lanes",
));
}
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frames = expr!(LaneExpr::Push(1));
let freq_bins = expr!(LaneExpr::Push(2));
let num_mels = expr!(LaneExpr::Push(3));
let log_scale = expr!(LaneExpr::Push(4));
let channel = expr!(LaneExpr::GroupX);
let values = expr!(LaneExpr::MulU32(num_mels, frames));
let e_stage = expr!(LaneExpr::LoopVar(2));
let mel = expr!(LaneExpr::DivU32(e_stage, frames));
let frame = expr!(LaneExpr::ModU32(e_stage, frames));
let channel_frames = expr!(LaneExpr::MulU32(channel, frames));
let input_row = expr!(LaneExpr::AddU32(channel_frames, frame));
let input_base = expr!(LaneExpr::MulU32(input_row, freq_bins));
let filter_base = expr!(LaneExpr::MulU32(mel, freq_bins));
let k = expr!(LaneExpr::Induction);
let input_index = expr!(LaneExpr::AddU32(input_base, k));
let filter_index = expr!(LaneExpr::AddU32(filter_base, k));
let sample = expr!(LaneExpr::LoadF32 {
binding: 0,
address: input_index,
});
let weight = expr!(LaneExpr::LoadF32 {
binding: 2,
address: filter_index,
});
let product = expr!(LaneExpr::MulF32(sample, weight));
let zero_f32 = expr!(LaneExpr::ConstF32(0));
let acc = expr!(LaneExpr::RegisterF32 { tile: 3, index: 0 });
let epsilon = expr!(LaneExpr::ConstF32(0x3089_705F));
let shifted = expr!(LaneExpr::AddF32(acc, epsilon));
let logged = expr!(LaneExpr::LogF32(shifted));
let zero_u32 = expr!(LaneExpr::ConstU32(0));
let do_log = expr!(LaneExpr::NeqU32(log_scale, zero_u32));
let staged_value = expr!(LaneExpr::SelectF32 {
cond: do_log,
if_true: logged,
if_false: acc,
});
let e = expr!(LaneExpr::Induction);
let staged = expr!(LaneExpr::SharedF32 { tile: 0, offset: e });
let reduce_acc = expr!(LaneExpr::RegisterF32 { tile: 4, index: 0 });
let lane_id = expr!(LaneExpr::Lane);
let partial = expr!(LaneExpr::SharedF32 {
tile: 1,
offset: zero_u32,
});
let count_f32 = expr!(LaneExpr::F32FromU32(values));
let mean = expr!(LaneExpr::DivF32(partial, count_f32));
let mean_reg = expr!(LaneExpr::RegisterF32 { tile: 5, index: 0 });
let centered = expr!(LaneExpr::SubF32(staged, mean_reg));
let squared = expr!(LaneExpr::MulF32(centered, centered));
let partial_sq = expr!(LaneExpr::SharedF32 {
tile: 2,
offset: zero_u32,
});
let variance = expr!(LaneExpr::DivF32(partial_sq, count_f32));
let eps = expr!(LaneExpr::ConstF32(0x3727_c5ac));
let shifted_variance = expr!(LaneExpr::AddF32(variance, eps));
let inverse_stddev = expr!(LaneExpr::RsqrtF32(shifted_variance));
let inverse_reg = expr!(LaneExpr::RegisterF32 { tile: 6, index: 0 });
let normalized = expr!(LaneExpr::MulF32(centered, inverse_reg));
let channel_base = expr!(LaneExpr::MulU32(channel, values));
let out_index = expr!(LaneExpr::AddU32(channel_base, e));
Ok(Self {
bindings: vec![Binding::Input(0), Binding::Output(0), Binding::Input(1)],
push_scalars: vec![
"channels".into(),
"frames".into(),
"freq_bins".into(),
"num_mels".into(),
"log_scale".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes,
thread_columns: lanes,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles: vec![
TileDecl::Shared {
rows: 1,
columns: capacity,
stride: capacity,
},
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Shared {
rows: 1,
columns: lanes,
stride: lanes,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
TileDecl::Register {
rows: 1,
columns: 1,
},
],
lane_exprs,
ops: vec![
TileOp::Guard { bound: 0 },
TileOp::LaneLoop {
extent: Extent::Expr(values),
body: vec![
TileOp::LaneFold {
acc: 3,
init: zero_f32,
extent: Extent::Push(2),
op: LaneReduce::Sum,
element: product,
serial: true,
},
TileOp::StoreShared {
tile: 0,
offset: e_stage,
value: staged_value,
},
],
},
TileOp::Barrier,
TileOp::LaneFold {
acc: 4,
init: zero_f32,
extent: Extent::Expr(values),
op: LaneReduce::Sum,
element: staged,
serial: false,
},
TileOp::StoreShared {
tile: 1,
offset: lane_id,
value: reduce_acc,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 1,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 5,
index: 0,
value: mean,
},
TileOp::LaneFold {
acc: 4,
init: zero_f32,
extent: Extent::Expr(values),
op: LaneReduce::Sum,
element: squared,
serial: false,
},
TileOp::StoreShared {
tile: 2,
offset: lane_id,
value: reduce_acc,
},
TileOp::Barrier,
TileOp::TreeReduce {
tile: 2,
op: LaneReduce::Sum,
},
TileOp::AssignScalar {
tile: 6,
index: 0,
value: inverse_stddev,
},
TileOp::LaneLoop {
extent: Extent::Expr(values),
body: vec![TileOp::StoreScalar {
binding: 1,
address: out_index,
value: normalized,
}],
},
],
})
}
pub(in crate::runtime) fn image_grayscale() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let group_x = expr!(LaneExpr::GroupX);
let group_y = expr!(LaneExpr::GroupY);
let batch = expr!(LaneExpr::GroupZ);
let lane_id = expr!(LaneExpr::Lane);
let sixteen = expr!(LaneExpr::ConstU32(16));
let two = expr!(LaneExpr::ConstU32(2));
let thread_row = expr!(LaneExpr::DivU32(lane_id, sixteen));
let thread_column = expr!(LaneExpr::ModU32(lane_id, sixteen));
let tile_x = expr!(LaneExpr::MulU32(group_x, sixteen));
let tile_y = expr!(LaneExpr::MulU32(group_y, sixteen));
let out_x = expr!(LaneExpr::AddU32(tile_x, thread_column));
let out_y = expr!(LaneExpr::AddU32(tile_y, thread_row));
let batch_count = expr!(LaneExpr::Push(0));
let channels = expr!(LaneExpr::Push(1));
let height = expr!(LaneExpr::Push(2));
let width = expr!(LaneExpr::Push(3));
let x_over = expr!(LaneExpr::GeU32(out_x, width));
let y_over = expr!(LaneExpr::GeU32(out_y, height));
let xy_over = expr!(LaneExpr::OrBool(x_over, y_over));
let z_over = expr!(LaneExpr::GeU32(batch, batch_count));
let out_of_bounds = expr!(LaneExpr::OrBool(xy_over, z_over));
let plane = expr!(LaneExpr::MulU32(height, width));
let row = expr!(LaneExpr::MulU32(out_y, width));
let pixel = expr!(LaneExpr::AddU32(row, out_x));
let batch_planes = expr!(LaneExpr::MulU32(batch, channels));
let batch_base = expr!(LaneExpr::MulU32(batch_planes, plane));
let base = expr!(LaneExpr::AddU32(batch_base, pixel));
let green_base = expr!(LaneExpr::AddU32(base, plane));
let two_plane = expr!(LaneExpr::MulU32(two, plane));
let blue_base = expr!(LaneExpr::AddU32(base, two_plane));
let red = expr!(LaneExpr::LoadF32 {
binding: 0,
address: base,
});
let green = expr!(LaneExpr::LoadF32 {
binding: 0,
address: green_base,
});
let blue = expr!(LaneExpr::LoadF32 {
binding: 0,
address: blue_base,
});
let w_r = expr!(LaneExpr::ConstF32(0x3E59_B3D0));
let w_g = expr!(LaneExpr::ConstF32(0x3F37_1759));
let w_b = expr!(LaneExpr::ConstF32(0x3D93_DD98));
let wr = expr!(LaneExpr::MulF32(w_r, red));
let wg = expr!(LaneExpr::MulF32(w_g, green));
let wb = expr!(LaneExpr::MulF32(w_b, blue));
let rg = expr!(LaneExpr::AddF32(wr, wg));
let luma = expr!(LaneExpr::AddF32(rg, wb));
let out_plane = expr!(LaneExpr::MulU32(batch, plane));
let out_index = expr!(LaneExpr::AddU32(out_plane, pixel));
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"input_channels".into(),
"height".into(),
"width".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: out_index,
value: luma,
},
],
}
}
pub(in crate::runtime) fn image_channel_reorder() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let two = expr!(LaneExpr::ConstU32(2));
let one = expr!(LaneExpr::ConstU32(1));
let zero = expr!(LaneExpr::ConstU32(0));
let channels = expr!(LaneExpr::Push(1));
let channel = expr!(LaneExpr::ModU32(frame.plane, channels));
let batch_i = expr!(LaneExpr::DivU32(frame.plane, channels));
let plane_elements = expr!(LaneExpr::MulU32(frame.height, frame.width));
let batch_planes = expr!(LaneExpr::MulU32(batch_i, channels));
let batch_base = expr!(LaneExpr::MulU32(batch_planes, plane_elements));
let pixel_row = expr!(LaneExpr::MulU32(frame.out_y, frame.width));
let pixel = expr!(LaneExpr::AddU32(pixel_row, frame.out_x));
let order_0 = expr!(LaneExpr::Push(4));
let order_1 = expr!(LaneExpr::Push(5));
let order_2 = expr!(LaneExpr::Push(6));
let order_3 = expr!(LaneExpr::Push(7));
let is_0 = expr!(LaneExpr::EqU32(channel, zero));
let is_1 = expr!(LaneExpr::EqU32(channel, one));
let is_2 = expr!(LaneExpr::EqU32(channel, two));
let tail_23 = expr!(LaneExpr::SelectU32 {
cond: is_2,
if_true: order_2,
if_false: order_3,
});
let tail_123 = expr!(LaneExpr::SelectU32 {
cond: is_1,
if_true: order_1,
if_false: tail_23,
});
let order = expr!(LaneExpr::SelectU32 {
cond: is_0,
if_true: order_0,
if_false: tail_123,
});
let order_offset = expr!(LaneExpr::MulU32(order, plane_elements));
let source = expr!(LaneExpr::AddU32(batch_base, order_offset));
let source_index = expr!(LaneExpr::AddU32(source, pixel));
let value = expr!(LaneExpr::LoadF32 {
binding: 0,
address: source_index,
});
Self {
bindings: vec![Binding::Input(0), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
"channel_0".into(),
"channel_1".into(),
"channel_2".into(),
"channel_3".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: frame.out_of_bounds,
},
TileOp::StoreScalar {
binding: 1,
address: frame.out_index,
value,
},
],
}
}
pub(in crate::runtime) fn image_color_twist() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let four = expr!(LaneExpr::ConstU32(4));
let three = expr!(LaneExpr::ConstU32(3));
let channels = expr!(LaneExpr::Push(1));
let channel = expr!(LaneExpr::ModU32(frame.plane, channels));
let batch_i = expr!(LaneExpr::DivU32(frame.plane, channels));
let plane_elements = expr!(LaneExpr::MulU32(frame.height, frame.width));
let batch_planes = expr!(LaneExpr::MulU32(batch_i, channels));
let batch_base = expr!(LaneExpr::MulU32(batch_planes, plane_elements));
let pixel_row = expr!(LaneExpr::MulU32(frame.out_y, frame.width));
let pixel = expr!(LaneExpr::AddU32(pixel_row, frame.out_x));
let value_base = expr!(LaneExpr::AddU32(batch_base, pixel));
let channel_plane = expr!(LaneExpr::MulU32(channel, plane_elements));
let target = expr!(LaneExpr::AddU32(value_base, channel_plane));
let own = expr!(LaneExpr::LoadF32 {
binding: 0,
address: target,
});
let in_rgb = expr!(LaneExpr::LtU32(channel, three));
let row = expr!(LaneExpr::MulU32(channel, four));
let mut products = Vec::new();
for tap in [0_u32, 1, 2] {
let row_offset = expr!(LaneExpr::ConstU32(tap));
let coefficient_row = expr!(LaneExpr::AddU32(row, row_offset));
let coefficient = expr!(LaneExpr::LoadF32 {
binding: 1,
address: coefficient_row,
});
let plane_offset = expr!(LaneExpr::ConstU32(tap));
let plane_delta = expr!(LaneExpr::MulU32(plane_offset, plane_elements));
let sample_index = expr!(LaneExpr::AddU32(value_base, plane_delta));
let sample = expr!(LaneExpr::LoadF32 {
binding: 0,
address: sample_index,
});
products.push(expr!(LaneExpr::MulF32(coefficient, sample)));
}
let head = expr!(LaneExpr::AddF32(products[0], products[1]));
let tail = expr!(LaneExpr::AddF32(head, products[2]));
let bias_row = expr!(LaneExpr::AddU32(row, three));
let bias = expr!(LaneExpr::LoadF32 {
binding: 1,
address: bias_row,
});
let accumulate = expr!(LaneExpr::AddF32(tail, bias));
let result = expr!(LaneExpr::SelectF32 {
cond: in_rgb,
if_true: accumulate,
if_false: own,
});
Self {
bindings: vec![Binding::Input(0), Binding::Input(1), Binding::Output(0)],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
],
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: frame.out_of_bounds,
},
TileOp::StoreScalar {
binding: 2,
address: target,
value: result,
},
],
}
}
pub(in crate::runtime) fn image_composite() -> Self {
let mut lane_exprs = Vec::new();
macro_rules! expr {
($e:expr) => {
lane(&mut lane_exprs, $e)
};
}
let frame = Self::filter_prelude(&mut lane_exprs);
let zero = expr!(LaneExpr::ConstU32(0));
let one = expr!(LaneExpr::ConstU32(1));
let zero_f = expr!(LaneExpr::ConstF32(0));
let one_f = expr!(LaneExpr::ConstF32(0x3F80_0000));
let operation = expr!(LaneExpr::Push(4));
let mask_channels = expr!(LaneExpr::Push(5));
let rect_x = expr!(LaneExpr::Push(6));
let rect_y = expr!(LaneExpr::Push(7));
let rect_width = expr!(LaneExpr::Push(8));
let rect_height = expr!(LaneExpr::Push(9));
let alpha = expr!(LaneExpr::PushF32(0));
let value_f = expr!(LaneExpr::PushF32(1));
let a = expr!(LaneExpr::LoadF32 {
binding: 0,
address: frame.out_index,
});
let b = expr!(LaneExpr::LoadF32 {
binding: 1,
address: frame.out_index,
});
let one_minus_alpha = expr!(LaneExpr::SubF32(one_f, alpha));
let a_alpha = expr!(LaneExpr::MulF32(a, one_minus_alpha));
let b_alpha = expr!(LaneExpr::MulF32(b, alpha));
let alpha_result = expr!(LaneExpr::AddF32(a_alpha, b_alpha));
let channels = expr!(LaneExpr::Push(1));
let channel = expr!(LaneExpr::ModU32(frame.plane, channels));
let single = expr!(LaneExpr::EqU32(mask_channels, one));
let mask_channel = expr!(LaneExpr::SelectU32 {
cond: single,
if_true: zero,
if_false: channel,
});
let batch_i = expr!(LaneExpr::DivU32(frame.plane, channels));
let mask_planes = expr!(LaneExpr::MulU32(batch_i, mask_channels));
let mask_plane = expr!(LaneExpr::AddU32(mask_planes, mask_channel));
let mask_rows = expr!(LaneExpr::MulU32(mask_plane, frame.height));
let mask_row = expr!(LaneExpr::AddU32(mask_rows, frame.out_y));
let mask_line = expr!(LaneExpr::MulU32(mask_row, frame.width));
let mask_index = expr!(LaneExpr::AddU32(mask_line, frame.out_x));
let mask_raw = expr!(LaneExpr::LoadF32 {
binding: 2,
address: mask_index,
});
let mask_lo = expr!(LaneExpr::MaxF32(mask_raw, zero_f));
let mask = expr!(LaneExpr::MinF32(mask_lo, one_f));
let one_minus_mask = expr!(LaneExpr::SubF32(one_f, mask));
let a_mask = expr!(LaneExpr::MulF32(a, one_minus_mask));
let b_mask = expr!(LaneExpr::MulF32(b, mask));
let mask_result = expr!(LaneExpr::AddF32(a_mask, b_mask));
let x_inside = expr!(LaneExpr::GeU32(frame.out_x, rect_x));
let x_delta = expr!(LaneExpr::SubU32(frame.out_x, rect_x));
let x_fits = expr!(LaneExpr::LtU32(x_delta, rect_width));
let x_in = expr!(LaneExpr::AndBool(x_inside, x_fits));
let y_inside = expr!(LaneExpr::GeU32(frame.out_y, rect_y));
let y_delta = expr!(LaneExpr::SubU32(frame.out_y, rect_y));
let y_fits = expr!(LaneExpr::LtU32(y_delta, rect_height));
let y_in = expr!(LaneExpr::AndBool(y_inside, y_fits));
let inside = expr!(LaneExpr::AndBool(x_in, y_in));
let rect_result = expr!(LaneExpr::SelectF32 {
cond: inside,
if_true: value_f,
if_false: a,
});
let is_alpha = expr!(LaneExpr::EqU32(operation, zero));
let is_mask = expr!(LaneExpr::EqU32(operation, one));
let non_alpha = expr!(LaneExpr::SelectF32 {
cond: is_mask,
if_true: mask_result,
if_false: rect_result,
});
let result = expr!(LaneExpr::SelectF32 {
cond: is_alpha,
if_true: alpha_result,
if_false: non_alpha,
});
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: vec![
"batch_size".into(),
"channels".into(),
"height".into(),
"width".into(),
"operation".into(),
"mask_channels".into(),
"rect_x".into(),
"rect_y".into(),
"rect_width".into(),
"rect_height".into(),
],
push_f32_scalars: vec!["alpha".into(), "value".into()],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles: Vec::new(),
lane_exprs,
ops: vec![
TileOp::GuardIf {
cond: frame.out_of_bounds,
},
TileOp::StoreScalar {
binding: 3,
address: frame.out_index,
value: result,
},
],
}
}
fn filter_program(
lane_exprs: Vec<LaneExpr>,
tiles: Vec<TileDecl>,
mut body: Vec<TileOp>,
out_of_bounds: usize,
out_index: usize,
result: usize,
) -> Self {
let mut ops = Vec::with_capacity(body.len() + 2);
ops.push(TileOp::GuardIf {
cond: out_of_bounds,
});
ops.append(&mut body);
ops.push(TileOp::StoreScalar {
binding: 3,
address: out_index,
value: result,
});
Self {
bindings: vec![
Binding::Input(0),
Binding::Input(1),
Binding::Input(2),
Binding::Output(0),
],
push_scalars: Self::FILTER_PUSH_SCALARS
.iter()
.map(|name| (*name).to_string())
.collect(),
push_f32_scalars: vec![
"p0".into(),
"p1".into(),
"p2".into(),
"p3".into(),
"border_value".into(),
],
shape: BlockShape {
lanes: 256,
thread_columns: 16,
fragment_rows: 1,
tile_rows: 1,
fragment_columns: 1,
tile_columns: 1,
},
tiles,
lane_exprs,
ops,
}
}
fn flat_program(
bindings: Vec<Binding>,
push_scalars: Vec<String>,
tiles: Vec<TileDecl>,
lane_exprs: Vec<LaneExpr>,
ops: Vec<TileOp>,
) -> Self {
Self {
bindings,
push_scalars,
push_f32_scalars: Vec::new(),
shape: BlockShape {
lanes: 256,
thread_columns: 1,
fragment_rows: 1,
fragment_columns: 1,
tile_rows: 1,
tile_columns: 1,
},
tiles,
lane_exprs,
ops,
}
}
}