oa::ml::loss
Module published by the oa::ml module.
MdRust public API
Public Types
Public Functions
pub fn cosine_embedding( input1: &Matrix, input2: &Matrix, target: &Matrix, margin: f32, ) -> Result<Matrix>pub fn cross_entropy_masked( logits: &Matrix, targets: &Matrix, mask: &Matrix, valid_count: usize, ) -> Result<Matrix>pub fn ctc( log_probs: &Matrix, targets: &Matrix, input_lengths: &Matrix, target_lengths: &Matrix, blank: usize, ) -> Result<Matrix>pub fn dqn( q: &Matrix, action: &Matrix, reward: &Matrix, next_q: &Matrix, terminated: &Matrix, truncated: &Matrix, config: DqnLossConfig, ) -> Result<DqnLossResult>pub fn gaussian_nll( prediction: &Matrix, target: &Matrix, variance: &Matrix, eps: f32, full: bool, ) -> Result<Matrix>pub fn margin_ranking( input1: &Matrix, input2: &Matrix, target: &Matrix, margin: f32, ) -> Result<Matrix>pub fn multi_label_soft_margin( input: &Matrix, target: &Matrix, weight: Option<&Matrix>, ) -> Result<Matrix>pub fn ppo( new_log_probability: &Matrix, old_log_probability: &Matrix, advantage: &Matrix, value: &Matrix, target_return: &Matrix, entropy: &Matrix, config: PpoLossConfig, ) -> Result<PpoLossResult>pub fn ppo_clipped_policy( new_log_probability: &Matrix, old_log_probability: &Matrix, advantage: &Matrix, clip_epsilon: f32, ) -> Result<Matrix>pub fn ppo_clipped_policy_backward( new_log_probability: &Matrix, old_log_probability: &Matrix, advantage: &Matrix, clip_epsilon: f32, ) -> Result<Matrix>pub fn sac_actor( q1: &Matrix, q2: &Matrix, log_probability: &Matrix, entropy_coefficient: f32, ) -> Result<Matrix>Function Documentation
Compute mean binary cross entropy over matching FP32 probability matrices. Predictions use the donor’s [1e-7, 1 - 1e-7] numerical clamp. Targets are treated as detached; reverse mode differentiates only the prediction. Errors Returns an error for empty, mismatched, non-FP32, or cross-runtime inputs, or when allocation or runtime recording fails.
Compute the cosine embedding loss over [N, D] pairs. Rows with target == 1 contribute 1 - cos(x1, x2); rows with target == -1 contribute max(0, cos(x1, x2) - margin). target is an [N, 1] ±1 mask. Errors Returns an error for mismatched, empty, non-rank-two, or non-FP32 inputs, or when runtime recording fails.
Compute mean cross-entropy over rank-two FP32 logits and U32 or I32 class targets. logits has shape [N, C], targets has shape [N], and the returned matrix is an FP32 scalar. I32 targets must be non-negative; any negative or out-of-range target produces NaN without an out-of-bounds access. Errors Returns an error when ranks, shapes, dtypes, ownership, allocation, or runtime recording are invalid.
Compute mean cross-entropy over the rows selected by a floating FP32 mask. logits has shape [N, C]; targets and mask have shape [N]. A zero mask value excludes its row and produces an exact-zero logits adjoint. valid_count is the caller-provided normalization denominator and must be in 1..=N; it is not read back or recomputed from device storage. Reverse mode differentiates only logits. Errors Returns an error when ranks, shapes, dtypes, ownership, valid_count, allocation, or runtime recording are invalid.
Compute the mean connectionist temporal classification loss over a batch. log_probs has shape [T, N, C] and holds log-probabilities over C classes including the blank class. targets has shape [N, S] and holds the label sequences, input_lengths and target_lengths have shape [N] and hold each item’s frame and label counts. blank is the blank class index and must be below C. The returned matrix is an FP32 scalar: the mean of the per-item negative log-likelihoods. Label entries shorter than S and frames beyond an item’s input_lengths entry are ignored. Target classes outside 0..C produce a NaN item loss without an out-of-bounds read, matching the cross-entropy invalid-target contract. Errors Returns an error when ranks, shapes, dtypes, blank, target_lengths width, ownership, allocation, or runtime recording are invalid.
Compose the donor DQN selected-action and detached Bellman-target loss. Termination suppresses next-state bootstrap. Truncation deliberately keeps bootstrap because it ends only the current trace, not the underlying task. Reverse mode therefore connects loss and selected_q only to q. Errors Returns an error unless Q and next-Q are same-runtime nonempty F32 [B,A] matrices with A > 1, action is I32 [B], reward is F32 [B], boundary masks are U8 [B], all fields share one runtime, extents fit the shader ABI, and discount is finite in [0, 1].
Compute the Gaussian negative log likelihood mean(0.5 * (log var + (x - t)^2 / var) + full * 0.5 * log(2 pi)). variance is clamped below at eps, matching the donor’s stability rule. full adds the constant 0.5 * log(2 pi) term. Errors Returns an error for mismatched, empty, or non-FP32 inputs, nonpositive eps, or when runtime recording fails.
Compute the KL divergence loss mean(target * (log target - input)). input holds log-probabilities and target holds probabilities, matching torch.nn.functional.kl_div with log-target input. The target == 0 contribution is exact zero; targets below 1e-12 clamp before the log so the product stays finite. Errors Returns an error for mismatched, empty, or non-FP32 inputs, or when runtime recording fails.
Compute mean absolute error for matching FP32 matrices. The target is treated as detached; reverse mode differentiates only the prediction. Equal prediction and target values use the zero subgradient. Errors Returns an error for empty, mismatched, non-FP32, or cross-runtime inputs, or when allocation or runtime recording fails.
Compute the margin ranking loss mean(max(0, -y * (x1 - x2) + margin)). target is the ±1 direction mask matching the input shapes. Errors Returns an error for mismatched, empty, or non-FP32 inputs, or when runtime recording fails.
Compute mean squared error for matching FP32 matrices. The target is treated as detached; reverse mode differentiates only the prediction. Errors Returns an error for empty, mismatched, non-FP32, or cross-runtime inputs, or when allocation or runtime recording fails.
Compute the multi-label soft margin loss (binary cross-entropy with logits, averaged over classes then batch). input is an [N, C] logit matrix and target an [N, C] 0/1 mask. weight optionally scales each class term by an [N, C]-broadcastable weight matrix. Uses the stable softplus form so large logits do not overflow. Errors Returns an error for mismatched, empty, non-rank-two, or non-FP32 inputs, or when runtime recording fails.
Compute the multiclass hinge loss mean_i((1/C) * sum_{j != y_i} max(0, margin - x[i,y_i] + x[i,j])). input is an [N, C] score matrix and target an I32 [N, 1] class-index matrix. The j == y_i hinge term equals margin, so it is subtracted exactly after the full row sum. Errors Returns an error for non-rank-two, empty, or non-FP32 inputs, mismatched targets, or when runtime recording fails.
Compute the negative log likelihood loss -mean(x[i, target_i]). input is an [N, C] score matrix (usually log-probabilities) and target an I32 [N, 1] class-index matrix. Errors Returns an error for non-rank-two, empty, or non-FP32 inputs, mismatched or out-of-range targets, or when runtime recording fails.
Compute the Poisson negative log likelihood mean(exp(input) - target * input) for log-rate input. Matches the donor’s log_input contract: input is the log-rate, so the rate is exp(input). The Stirling correction (full) is not part of this contract. Errors Returns an error for mismatched, empty, or non-FP32 inputs, or when runtime recording fails.
Compose PPO policy, value, entropy, and total losses. All four outputs are FP32 scalars under one semantic PPO operation. Old log probability, advantage, and target return remain detached by their child operation contracts. Errors Returns an error unless all rollout fields are matching nonempty same-runtime FP32 matrices and every coefficient is valid.
Compute the mean negative clipped PPO surrogate objective. Errors Returns an error unless all inputs are matching nonempty same-runtime FP32 matrices and clip_epsilon is finite in (0, 1).
Compute the explicit clipped-policy adjoint for new log probability. Errors Returns the same validation and runtime errors as ppo_clipped_policy.
Compute the donor entropy-regularized SAC actor objective. The scalar result is mean(alpha * log_probability - min(q1, q2)) and is differentiable through all three inputs. Errors Returns an error unless inputs are matching nonempty same-runtime F32 vectors, entropy coefficient is finite and non-negative, and lowering succeeds.
Compose the donor twin-critic SAC objective. The target is reward + discount * (min(next_q1, next_q2) - alpha * next_log_probability) outside terminal states. Truncation retains that bootstrap. All next-state target fields are detached from reverse mode. Errors Returns an error unless all numerical fields are matching nonempty same-runtime F32 vectors, both boundary masks are matching U8 vectors, discount is finite in [0, 1], entropy coefficient is non-negative and finite, the batch fits the shader ABI, and lowering succeeds.
Compute mean unit-beta Smooth L1 loss for matching FP32 matrices. The target is treated as detached; reverse mode differentiates only the prediction. Errors Returns an error for empty, mismatched, non-FP32, or cross-runtime inputs, or when allocation or runtime recording fails.
Compute the soft margin loss mean(log(1 + exp(-y * x))). Uses the stable softplus form max(z, 0) + log(1 + exp(-|z|)) with z = -y * x, so large margins saturate without overflow. Errors Returns an error for mismatched, empty, or non-FP32 inputs, or when runtime recording fails.
Compute the triplet margin loss mean(max(0, d(a, p) - d(a, n) + margin)) with L2 distances over the last axis. All three inputs share one [N, D] shape; swap (the donor’s distance exchange) is not part of this contract. Errors Returns an error for mismatched, empty, non-rank-two, or non-FP32 inputs, or when runtime recording fails.