use crate::{
DType, Error, Matrix, OpAttribute, Result,
runtime::{BufferBinding, ComputeDispatch, KernelId, PushConstant, SemanticDispatch},
};
use crate::ml::{Parameter, autograd};
use super::super::validation::{shader_u32, validate_f32_same_engine};
use super::recurrent::{MAX_HIDDEN_SIZE, optional_bias};
pub fn lstm_scan(gates_i: &Matrix, weight_hh: &Matrix, bias_hh: Option<&Matrix>) -> Result<Matrix> {
let bias_value = optional_bias(weight_hh, bias_hh, "oa::ml::matrix::lstm_scan")?;
let has_bias = bias_hh.is_some();
let (output, hidden_previous, cell_previous, cell_state) =
lstm_scan_dispatch(gates_i, weight_hh, &bias_value, has_bias)?;
autograd::record_lstm_scan(
gates_i,
&output,
hidden_previous,
cell_previous,
cell_state,
None,
weight_hh.clone(),
None,
bias_value,
has_bias,
)?;
Ok(output)
}
pub(in crate::ml) fn lstm_scan_parameterized(
gates_i: &Matrix,
weight: (Parameter, Matrix, u64),
bias: Option<(Parameter, u64)>,
bias_value: &Matrix,
) -> Result<Matrix> {
let (weight_parameter, weight_value, weight_version) = weight;
let has_bias = bias.is_some();
let (output, hidden_previous, cell_previous, cell_state) =
lstm_scan_dispatch(gates_i, &weight_value, bias_value, has_bias)?;
autograd::record_lstm_scan(
gates_i,
&output,
hidden_previous,
cell_previous,
cell_state,
Some((weight_parameter, weight_version)),
weight_value,
bias,
bias_value.clone(),
has_bias,
)?;
Ok(output)
}
#[allow(
clippy::too_many_arguments,
reason = "the recurrent adjoint consumes every saved scan value"
)]
pub(in crate::ml) fn lstm_scan_backward(
output_gradient: &Matrix,
gates_i: &Matrix,
hidden_previous: &Matrix,
cell_previous: &Matrix,
cell_state: &Matrix,
weight_hh: &Matrix,
bias_hh: &Matrix,
has_bias: bool,
) -> Result<(Matrix, Matrix, Matrix)> {
const OPERATION: &str = crate::core::operation::ml::LSTM_SCAN_BACKWARD.name();
let [batch, sequence_length, gate_count] = gates_i.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} input gates must have shape [B, S, 4H]"
)));
};
let [weight_gate_count, hidden_size] = weight_hh.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} recurrent weight must have shape [4H, H]"
)));
};
let expected_gate_count = hidden_size
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} gate size overflows usize")))?;
let output_shape = [*batch, *sequence_length, *hidden_size];
if *batch == 0
|| *sequence_length == 0
|| *hidden_size == 0
|| *hidden_size > MAX_HIDDEN_SIZE
|| *gate_count != expected_gate_count
|| *weight_gate_count != expected_gate_count
|| bias_hh.shape() != [expected_gate_count]
|| hidden_previous.shape() != output_shape
|| cell_previous.shape() != output_shape
|| cell_state.shape() != output_shape
|| output_gradient.shape() != output_shape
{
return Err(Error::invalid_argument(format!(
"{OPERATION} saved values and output gradient do not match the LSTM scan contract"
)));
}
validate_f32_same_engine(
OPERATION,
&[
output_gradient,
gates_i,
hidden_previous,
cell_previous,
cell_state,
weight_hh,
bias_hh,
],
)?;
let gates_i_gradient = Matrix::allocate(
gates_i.engine_handle(),
gates_i.shape().to_vec(),
gates_i.num_elements(),
DType::F32,
)?;
let gates_h_gradient = Matrix::allocate(
gates_i.engine_handle(),
gates_i.shape().to_vec(),
gates_i.num_elements(),
DType::F32,
)?;
let weight_gradient = Matrix::allocate(
gates_i.engine_handle(),
weight_hh.shape().to_vec(),
weight_hh.num_elements(),
DType::F32,
)?;
let bias_gradient = Matrix::allocate(
gates_i.engine_handle(),
bias_hh.shape().to_vec(),
bias_hh.num_elements(),
DType::F32,
)?;
let rows = batch
.checked_mul(*sequence_length)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} row count overflows usize")))?;
let batch = shader_u32(*batch, "batch", OPERATION)?;
let sequence_length = shader_u32(*sequence_length, "sequence length", OPERATION)?;
let hidden_size = shader_u32(*hidden_size, "hidden size", OPERATION)?;
let gate_count = shader_u32(expected_gate_count, "gate count", OPERATION)?;
let rows = shader_u32(rows, "flattened row count", OPERATION)?;
let scan_buffers = [
BufferBinding::read(output_gradient.storage()),
BufferBinding::read(gates_i.storage()),
BufferBinding::read(hidden_previous.storage()),
BufferBinding::read(cell_previous.storage()),
BufferBinding::read(cell_state.storage()),
BufferBinding::read(weight_hh.storage()),
BufferBinding::read(bias_hh.storage()),
BufferBinding::write(gates_i_gradient.storage()),
BufferBinding::write(gates_h_gradient.storage()),
];
let scan_push_constants = [
PushConstant::U32(hidden_size),
PushConstant::U32(sequence_length),
PushConstant::U32(batch),
PushConstant::U32(u32::from(has_bias)),
];
let parameter_buffers = [
BufferBinding::read(hidden_previous.storage()),
BufferBinding::read(gates_h_gradient.storage()),
BufferBinding::write(weight_gradient.storage()),
BufferBinding::write(bias_gradient.storage()),
];
let parameter_push_constants = [
PushConstant::U32(rows),
PushConstant::U32(hidden_size),
PushConstant::U32(gate_count),
];
let dispatches = [
ComputeDispatch {
kernel: KernelId::MlLstmScanBackwardF32,
buffers: &scan_buffers,
push_constants: &scan_push_constants,
workgroups: [batch, 1, 1],
indirect: None,
},
ComputeDispatch {
kernel: KernelId::MlLinearParameterBackwardF32,
buffers: ¶meter_buffers,
push_constants: ¶meter_push_constants,
workgroups: [gate_count, hidden_size.div_ceil(32), 1],
indirect: None,
},
];
let inputs = [
output_gradient,
gates_i,
hidden_previous,
cell_previous,
cell_state,
weight_hh,
bias_hh,
];
let outputs = [&gates_i_gradient, &weight_gradient, &bias_gradient];
let attributes = [OpAttribute::Boolean {
name: "has_bias".into(),
value: has_bias,
}];
gates_i.engine_handle().record_split_semantic(
&dispatches,
SemanticDispatch {
contract: crate::core::operation::ml::LSTM_SCAN_BACKWARD,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
Ok((gates_i_gradient, weight_gradient, bias_gradient))
}
pub fn lstm_cell(
gates_i: &Matrix,
hidden: &Matrix,
cell: &Matrix,
weight_hh: &Matrix,
bias_hh: Option<&Matrix>,
) -> Result<(Matrix, Matrix)> {
let bias_value = optional_bias(weight_hh, bias_hh, "oa::ml::matrix::lstm_cell")?;
let has_bias = bias_hh.is_some();
let (hidden_next, cell_next, gates_h) =
lstm_cell_dispatch(gates_i, hidden, cell, weight_hh, &bias_value, has_bias)?;
autograd::record_lstm_cell(
gates_i,
hidden,
cell,
&hidden_next,
&cell_next,
gates_h,
None,
weight_hh.clone(),
None,
bias_value,
has_bias,
)?;
Ok((hidden_next, cell_next))
}
#[allow(
clippy::too_many_arguments,
reason = "module forwarding preserves optional parameter identity separately from Matrix values"
)]
pub(in crate::ml) fn lstm_cell_parameterized(
gates_i: &Matrix,
hidden: &Matrix,
cell: &Matrix,
weight: (Parameter, Matrix, u64),
bias: Option<(Parameter, u64)>,
bias_value: &Matrix,
) -> Result<(Matrix, Matrix)> {
let (weight_parameter, weight_value, weight_version) = weight;
let has_bias = bias.is_some();
let (hidden_next, cell_next, gates_h) =
lstm_cell_dispatch(gates_i, hidden, cell, &weight_value, bias_value, has_bias)?;
autograd::record_lstm_cell(
gates_i,
hidden,
cell,
&hidden_next,
&cell_next,
gates_h,
Some((weight_parameter, weight_version)),
weight_value,
bias,
bias_value.clone(),
has_bias,
)?;
Ok((hidden_next, cell_next))
}
#[allow(
clippy::too_many_arguments,
reason = "the cell adjoint consumes every saved value and both carry gradients"
)]
pub(in crate::ml) fn lstm_cell_backward(
gates_i: &Matrix,
gates_h: &Matrix,
hidden: &Matrix,
cell: &Matrix,
hidden_gradient: &Matrix,
cell_gradient: &Matrix,
weight_hh: &Matrix,
bias_hh: &Matrix,
has_bias: bool,
) -> Result<(Matrix, Matrix, Matrix, Matrix, Matrix)> {
const OPERATION: &str = crate::core::operation::ml::LSTM_CELL_BACKWARD.name();
let [batch, gate_count] = gates_i.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} input gates must have shape [B, 4H]"
)));
};
let [hidden_batch, hidden_size] = hidden.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} hidden state must have shape [B, H]"
)));
};
let expected_gate_count = hidden_size
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} gate size overflows usize")))?;
if *batch == 0
|| *hidden_size == 0
|| *hidden_size > MAX_HIDDEN_SIZE
|| batch != hidden_batch
|| *gate_count != expected_gate_count
|| gates_h.shape() != gates_i.shape()
|| cell.shape() != hidden.shape()
|| hidden_gradient.shape() != hidden.shape()
|| cell_gradient.shape() != hidden.shape()
|| weight_hh.shape() != [expected_gate_count, *hidden_size]
|| bias_hh.shape() != [expected_gate_count]
{
return Err(Error::invalid_argument(format!(
"{OPERATION} saved values and carry gradients do not match the LSTM cell contract"
)));
}
validate_f32_same_engine(
OPERATION,
&[
gates_i,
gates_h,
hidden,
cell,
hidden_gradient,
cell_gradient,
weight_hh,
bias_hh,
],
)?;
let gates_i_gradient = Matrix::allocate(
gates_i.engine_handle(),
gates_i.shape().to_vec(),
gates_i.num_elements(),
DType::F32,
)?;
let gates_h_gradient = Matrix::allocate(
gates_i.engine_handle(),
gates_i.shape().to_vec(),
gates_i.num_elements(),
DType::F32,
)?;
let direct_hidden_gradient = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let direct_cell_gradient = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let recurrent_hidden_gradient = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let hidden_state_gradient = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let weight_gradient = Matrix::allocate(
gates_i.engine_handle(),
weight_hh.shape().to_vec(),
weight_hh.num_elements(),
DType::F32,
)?;
let bias_gradient = Matrix::allocate(
gates_i.engine_handle(),
bias_hh.shape().to_vec(),
bias_hh.num_elements(),
DType::F32,
)?;
let batch = shader_u32(*batch, "batch", OPERATION)?;
let hidden_size = shader_u32(*hidden_size, "hidden size", OPERATION)?;
let gate_count = shader_u32(expected_gate_count, "gate count", OPERATION)?;
let element_count = shader_u32(hidden.num_elements(), "hidden element count", OPERATION)?;
let pointwise_buffers = [
BufferBinding::read(gates_i.storage()),
BufferBinding::read(gates_h.storage()),
BufferBinding::read(hidden.storage()),
BufferBinding::read(cell.storage()),
BufferBinding::read(hidden_gradient.storage()),
BufferBinding::read(cell_gradient.storage()),
BufferBinding::write(gates_i_gradient.storage()),
BufferBinding::write(gates_h_gradient.storage()),
BufferBinding::write(direct_hidden_gradient.storage()),
BufferBinding::write(direct_cell_gradient.storage()),
];
let pointwise_push_constants = [
PushConstant::U32(hidden_size),
PushConstant::U32(element_count),
];
let input_buffers = [
BufferBinding::read(weight_hh.storage()),
BufferBinding::read(gates_h_gradient.storage()),
BufferBinding::write(recurrent_hidden_gradient.storage()),
];
let linear_push_constants = [
PushConstant::U32(batch),
PushConstant::U32(hidden_size),
PushConstant::U32(gate_count),
];
let parameter_buffers = [
BufferBinding::read(hidden.storage()),
BufferBinding::read(gates_h_gradient.storage()),
BufferBinding::write(weight_gradient.storage()),
BufferBinding::write(bias_gradient.storage()),
];
let add_buffers = [
BufferBinding::read(direct_hidden_gradient.storage()),
BufferBinding::read(recurrent_hidden_gradient.storage()),
BufferBinding::write(hidden_state_gradient.storage()),
];
let add_push_constants = [PushConstant::U32(element_count)];
let dispatches = [
ComputeDispatch {
kernel: KernelId::MlLstmCellBackwardF32,
buffers: &pointwise_buffers,
push_constants: &pointwise_push_constants,
workgroups: KernelId::MlLstmCellBackwardF32.linear_workgroups(element_count),
indirect: None,
},
ComputeDispatch {
kernel: KernelId::MlLinearBackwardF32,
buffers: &input_buffers,
push_constants: &linear_push_constants,
workgroups: KernelId::MlLinearBackwardF32.output_workgroups(batch, hidden_size),
indirect: None,
},
ComputeDispatch {
kernel: KernelId::MlLinearParameterBackwardF32,
buffers: ¶meter_buffers,
push_constants: &linear_push_constants,
workgroups: [gate_count, hidden_size.div_ceil(32), 1],
indirect: None,
},
ComputeDispatch {
kernel: KernelId::MatrixAddF32,
buffers: &add_buffers,
push_constants: &add_push_constants,
workgroups: KernelId::MatrixAddF32.linear_workgroups(element_count),
indirect: None,
},
];
let inputs = [
gates_i,
gates_h,
hidden,
cell,
hidden_gradient,
cell_gradient,
weight_hh,
bias_hh,
];
let outputs = [
&gates_i_gradient,
&hidden_state_gradient,
&direct_cell_gradient,
&weight_gradient,
&bias_gradient,
];
let attributes = [OpAttribute::Boolean {
name: "has_bias".into(),
value: has_bias,
}];
gates_i.engine_handle().record_split_semantic(
&dispatches,
SemanticDispatch {
contract: crate::core::operation::ml::LSTM_CELL_BACKWARD,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
Ok((
gates_i_gradient,
hidden_state_gradient,
direct_cell_gradient,
weight_gradient,
bias_gradient,
))
}
fn lstm_scan_dispatch(
gates_i: &Matrix,
weight_hh: &Matrix,
bias_hh: &Matrix,
has_bias: bool,
) -> Result<(Matrix, Matrix, Matrix, Matrix)> {
const OPERATION: &str = crate::core::operation::ml::LSTM_SCAN.name();
let [batch, sequence_length, gate_count] = gates_i.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} input gates must have shape [B, S, 4H]"
)));
};
let [weight_gate_count, hidden_size] = weight_hh.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} recurrent weight must have shape [4H, H]"
)));
};
let expected_gate_count = hidden_size
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} gate size overflows usize")))?;
if *batch == 0
|| *sequence_length == 0
|| *hidden_size == 0
|| *hidden_size > MAX_HIDDEN_SIZE
|| *gate_count != expected_gate_count
|| *weight_gate_count != expected_gate_count
|| bias_hh.shape() != [expected_gate_count]
{
return Err(Error::invalid_argument(format!(
"{OPERATION} requires nonempty gates [B, S, 4H], recurrent weight [4H, H], bias storage [4H], and H <= {MAX_HIDDEN_SIZE}"
)));
}
validate_f32_same_engine(OPERATION, &[gates_i, weight_hh, bias_hh])?;
let output_count = batch
.checked_mul(*sequence_length)
.and_then(|count| count.checked_mul(*hidden_size))
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
let output_shape = vec![*batch, *sequence_length, *hidden_size];
let output = Matrix::allocate(
gates_i.engine_handle(),
output_shape.clone(),
output_count,
DType::F32,
)?;
let hidden_previous = Matrix::allocate(
gates_i.engine_handle(),
output_shape.clone(),
output_count,
DType::F32,
)?;
let cell_previous = Matrix::allocate(
gates_i.engine_handle(),
output_shape.clone(),
output_count,
DType::F32,
)?;
let cell_state = Matrix::allocate(
gates_i.engine_handle(),
output_shape,
output_count,
DType::F32,
)?;
let batch = shader_u32(*batch, "batch", OPERATION)?;
let sequence_length = shader_u32(*sequence_length, "sequence length", OPERATION)?;
let hidden_size = shader_u32(*hidden_size, "hidden size", OPERATION)?;
let buffers = [
BufferBinding::read(gates_i.storage()),
BufferBinding::read(weight_hh.storage()),
BufferBinding::read(bias_hh.storage()),
BufferBinding::write(output.storage()),
BufferBinding::write(hidden_previous.storage()),
BufferBinding::write(cell_previous.storage()),
BufferBinding::write(cell_state.storage()),
];
let push_constants = [
PushConstant::U32(hidden_size),
PushConstant::U32(sequence_length),
PushConstant::U32(batch),
PushConstant::U32(u32::from(has_bias)),
];
let attributes = [
OpAttribute::SignedInteger {
name: "hiddenSize".into(),
value: i64::from(hidden_size),
},
OpAttribute::SignedInteger {
name: "sequenceLength".into(),
value: i64::from(sequence_length),
},
OpAttribute::SignedInteger {
name: "batch".into(),
value: i64::from(batch),
},
];
{
let inputs: &[&Matrix] = &[gates_i, weight_hh, bias_hh];
let outputs: &[&Matrix] = &[&output, &hidden_previous, &cell_previous, &cell_state];
let attributes: &[OpAttribute] = &attributes;
let dispatch = ComputeDispatch {
kernel: KernelId::MlLstmScanF32,
buffers: &buffers,
push_constants: &push_constants,
workgroups: [batch, 1, 1],
indirect: None,
};
let contract = dispatch.kernel.semantic_contract().ok_or_else(|| {
Error::internal(format!(
"lowering-only kernel {} cannot own one semantic ML operation",
dispatch.kernel.report_name()
))
})?;
let engine = inputs
.first()
.ok_or_else(|| Error::internal("semantic ML dispatch requires an input"))?
.engine_handle();
engine.record_semantic(
dispatch,
SemanticDispatch {
contract,
inputs,
outputs,
attributes,
},
)
}?;
Ok((output, hidden_previous, cell_previous, cell_state))
}
fn lstm_cell_dispatch(
gates_i: &Matrix,
hidden: &Matrix,
cell: &Matrix,
weight_hh: &Matrix,
bias_hh: &Matrix,
has_bias: bool,
) -> Result<(Matrix, Matrix, Matrix)> {
const OPERATION: &str = crate::core::operation::ml::LSTM_CELL.name();
let [batch, gate_count] = gates_i.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} input gates must have shape [B, 4H]"
)));
};
let [hidden_batch, hidden_size] = hidden.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} hidden state must have shape [B, H]"
)));
};
let [weight_gate_count, weight_hidden_size] = weight_hh.shape() else {
return Err(Error::invalid_argument(format!(
"{OPERATION} recurrent weight must have shape [4H, H]"
)));
};
let expected_gate_count = hidden_size
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} gate size overflows usize")))?;
if *batch == 0
|| *hidden_size == 0
|| *hidden_size > MAX_HIDDEN_SIZE
|| batch != hidden_batch
|| *gate_count != expected_gate_count
|| *weight_gate_count != expected_gate_count
|| weight_hidden_size != hidden_size
|| cell.shape() != hidden.shape()
|| bias_hh.shape() != [expected_gate_count]
{
return Err(Error::invalid_argument(format!(
"{OPERATION} requires gates [B, 4H], hidden/cell [B, H], recurrent weight [4H, H], bias storage [4H], and H <= {MAX_HIDDEN_SIZE}"
)));
}
validate_f32_same_engine(OPERATION, &[gates_i, hidden, cell, weight_hh, bias_hh])?;
let hidden_output = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let cell_output = Matrix::allocate(
gates_i.engine_handle(),
hidden.shape().to_vec(),
hidden.num_elements(),
DType::F32,
)?;
let gates_h = Matrix::allocate(
gates_i.engine_handle(),
gates_i.shape().to_vec(),
gates_i.num_elements(),
DType::F32,
)?;
let batch = shader_u32(*batch, "batch", OPERATION)?;
let hidden_size = shader_u32(*hidden_size, "hidden size", OPERATION)?;
let buffers = [
BufferBinding::read(hidden.storage()),
BufferBinding::read(cell.storage()),
BufferBinding::read(weight_hh.storage()),
BufferBinding::read(bias_hh.storage()),
BufferBinding::read(gates_i.storage()),
BufferBinding::write(hidden_output.storage()),
BufferBinding::write(cell_output.storage()),
BufferBinding::write(gates_h.storage()),
];
let push_constants = [
PushConstant::U32(hidden_size),
PushConstant::U32(batch),
PushConstant::U32(u32::from(has_bias)),
];
let attributes = [OpAttribute::Boolean {
name: "has_bias".into(),
value: has_bias,
}];
{
let inputs: &[&Matrix] = &[gates_i, hidden, cell, weight_hh, bias_hh];
let outputs: &[&Matrix] = &[&hidden_output, &cell_output, &gates_h];
let attributes: &[OpAttribute] = &attributes;
let dispatch = ComputeDispatch {
kernel: KernelId::MlLstmCellF32,
buffers: &buffers,
push_constants: &push_constants,
workgroups: [batch, 1, 1],
indirect: None,
};
let contract = dispatch.kernel.semantic_contract().ok_or_else(|| {
Error::internal(format!(
"lowering-only kernel {} cannot own one semantic ML operation",
dispatch.kernel.report_name()
))
})?;
let engine = inputs
.first()
.ok_or_else(|| Error::internal("semantic ML dispatch requires an input"))?
.engine_handle();
engine.record_semantic(
dispatch,
SemanticDispatch {
contract,
inputs,
outputs,
attributes,
},
)
}?;
Ok((hidden_output, cell_output, gates_h))
}