ggml : implement the backward pass for TRI (#30712)

* ggml: implement the backward pass for TRI

GGML_OP_TRI had no case in the backward switch, so a trainable graph containing it aborted with "unsupported ggml op for backward pass: TRI". The op is linear, so its gradient is masked the same way. The incoming gradient is not necessarily contiguous (the backward of a transpose hands over a view) and ggml_tri requires contiguity, so it is made contiguous first.

test_tri gains a post_transpose variant (out = transpose(tri(a))) that aborts as above without this change.

* ggml : simplify the TRI backward pass

Pass the gradient directly to ggml_tri and drop the transpose test. The existing TRI cases cover the backward pass for contiguous F32 gradients.
This commit is contained in:
losewayy authored and GitHub committed 2026-11-09 15:03:00 +02:00
1 parent d628a98f09
commit 0eebb6eddc
1 file changed
+5
+5
View File
@@ -7237,6 +7237,11 @@ static void ggml_compute_backward(
ggml_add_or_set(ctx, cgraph, isrc0, ggml_diag_mask_zero_impl(ctx, grad, n_past, false));
}
} break;
case GGML_OP_TRI: {
if (src0_needs_grads) {
ggml_add_or_set(ctx, cgraph, isrc0, ggml_tri(ctx, grad, (enum ggml_tri_type) ggml_get_op_params_i32(tensor, 0)));
}
} break;
case GGML_OP_SOFT_MAX: {
if (src0_needs_grads) {
float scale = 1.0f;