From b6e02cbbd280114ffcdffd5f7d4d26aee281d3d6 Mon Sep 17 00:00:00 2001 From: Jesse Gross Date: Tue, 18 Nov 2025 20:24:09 -0800 Subject: [PATCH] ggml: Automatically make tensors contiguous on reshape GGML requires tensors to be contiguous for reshape and if this is not the case, it will assert fail. Contiguous is an expensive operation, so it's best to do it lazily when it is actually required rather than ahead of time when it may not be needed. --- ml/backend/ggml/ggml.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ml/backend/ggml/ggml.go b/ml/backend/ggml/ggml.go index 1d457fc4f..8d0415cfa 100644 --- a/ml/backend/ggml/ggml.go +++ b/ml/backend/ggml/ggml.go @@ -1378,6 +1378,10 @@ func inferShape(t *Tensor, shape []int) { } func (t *Tensor) Reshape(ctx ml.Context, shape ...int) ml.Tensor { + if !C.ggml_is_contiguous(t.t) { + return t.Contiguous(ctx, shape...) + } + if slices.Contains(shape, -1) { inferShape(t, shape) }