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::UpsampleMode;
use crate::{Matrix, Result};
use std::collections::HashMap;

pub(in crate::ml::autograd) struct GradNodeUpsample2d {
	pub(in crate::ml::autograd) input: Matrix,
	pub(in crate::ml::autograd) scale_factor: usize,
	pub(in crate::ml::autograd) mode: UpsampleMode,
	pub(in crate::ml::autograd) output_id: u64,
}

impl GradNodeUpsample2d {
	pub(in crate::ml::autograd) fn backward(
		&self,
		gradients: &mut HashMap<u64, Matrix>,
	) -> Result<BackwardStatus> {
		let Self {
			input,
			output_id,
			scale_factor,
			mode,
		} = self;
		let Some(output_gradient) = gradients.remove(output_id) else {
			return Ok(BackwardStatus::Skipped);
		};
		let input_gradient =
			crate::ml::matrix::upsample_2d_backward(input, &output_gradient, *scale_factor, *mode)?;
		accumulate_value_gradient(gradients, input.value_id(), input_gradient)?;
		Ok(BackwardStatus::Recorded)
	}
}