From 0eebb6eddcd5dc70e5bd3ee440a47f36f6fb1ba8 Mon Sep 17 00:00:00 2001 From: losewayy <1134285612@qq.com> Date: Mon, 9 Nov 2026 21:03:00 +0800 Subject: [PATCH] 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. --- ggml/src/ggml.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index f8cf75b3a..c6a080d22 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -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;