From 124fdca973529bf69e5e03ebd6e04df9389d6e13 Mon Sep 17 00:00:00 2001 From: xaedes Date: Fri, 28 Apr 2023 18:36:07 +0200 Subject: [PATCH] successfully test view backward --- tests/test-grad0.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test-grad0.c b/tests/test-grad0.c index 222e68592..2bfde960f 100644 --- a/tests/test-grad0.c +++ b/tests/test-grad0.c @@ -7,6 +7,10 @@ #define MAX_NARGS 2 +#undef MIN +#undef MAX +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define GGML_SILU_FP16 @@ -517,6 +521,30 @@ int main(int argc, const char ** argv) { } } + // view + { + const int nargs = 1; + for (int ndims = 1; ndims <= 3; ++ndims) { + + x[0] = get_random_tensor(ctx0, ndims, ne, -1.0f, 1.0f); + + ggml_set_param(ctx0, x[0]); + + const int k0 = irand(ggml_nelements(x[0])); + const int k1 = irand(ggml_nelements(x[0])); + const int i0 = MIN(k0, k1); + const int i1 = MAX(k0, k1); + + const int offset = i0 * sizeof(float); + const int nelem = i1 - i0; + + if (nelem == 0) continue; + struct ggml_tensor * f = ggml_sum(ctx0, ggml_view_1d(ctx0, x[0], nelem, offset)); + + check_gradient("view", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY); + } + } + // softmax { const int nargs = 1;