oarust 0.8.6

GPU-first semantic computing engine written in Rust
// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=ml_training.json schema_version=1 generator_version=64
// schema_sha256=6678961ee57f24310158676c7b4fe0821e21ca48db7edcb20d2a749a164976ae
// Saved-state and backward recipes; lifecycle remains tape-owned.
use crate::ml::autograd::node::operation::BackwardStatus;
use crate::ml::autograd::tape::backward::accumulate_value_gradient;
use crate::ml::matrix as ml_matrix;
use crate::{Matrix, Result};
use std::collections::HashMap;

pub(in crate::ml::autograd) struct GradNodeRope {
	pub(in crate::ml::autograd) input: Matrix,
	pub(in crate::ml::autograd) num_heads: usize,
	pub(in crate::ml::autograd) head_dim: usize,
	pub(in crate::ml::autograd) theta_base: f32,
	pub(in crate::ml::autograd) position_offset: u32,
	pub(in crate::ml::autograd) output_id: u64,
}

impl GradNodeRope {
	pub(in crate::ml::autograd) fn backward(
		&self,
		gradients: &mut HashMap<u64, Matrix>,
	) -> Result<BackwardStatus> {
		let Self {
			input,
			output_id,
			num_heads,
			head_dim,
			theta_base,
			position_offset,
		} = self;
		let Some(output_gradient) = gradients.remove(output_id) else {
			return Ok(BackwardStatus::Skipped);
		};
		let input_gradient = ml_matrix::rope_backward(
			&output_gradient,
			*num_heads,
			*head_dim,
			*theta_base,
			*position_offset,
		)?;
		accumulate_value_gradient(gradients, input.value_id(), input_gradient)?;
		Ok(BackwardStatus::Recorded)
	}
}