Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/tensor_reduce_multi_tests.inc # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / tensor_reduce_multi_tests.inc` 308 lines ยท 15505 bytes Raw Blame History 1 /* 2 * CR-5 tests: omega_tensor_reduce_axes and omega_tensor_sum_to_shape 3 * (src/tensor/omega_tensor.h, docs/tensor/M20_OMEGA_TENSOR.md). Included by 4 * tests/test_omega_tensor.c after its helpers (g, mk, rd, CHECK, ref_tree, 5 * rand_mod, mismatches, prod). The reference is independent of the library: 6 * plain row-major index loops plus the test's own copy of the frozen E1 7 * reduction tree (ref_tree), applied in descending axis order. 8 */ 9 10 /* One axis of a dense row-major array: out has shape with that axis removed 11 * (keepdims does not change the dense layout). */ 12 static void rm_ref_axis(OmegaTensorReduceOp op, const float *x, uint32_t rank, const uint64_t *shape, 13 uint32_t axis, float *out) { 14 uint64_t outer = prod(axis, shape), n = shape[axis]; 15 uint64_t inner = prod(rank - axis - 1, shape + axis + 1); 16 float *row = malloc((size_t)n * sizeof(float)); 17 for (uint64_t o = 0; o < outer; o++) 18 for (uint64_t i = 0; i < inner; i++) { 19 for (uint64_t r = 0; r < n; r++) row[r] = x[(o * n + r) * inner + i]; 20 out[o * inner + i] = ref_tree(op, row, (size_t)n); 21 } 22 free(row); 23 } 24 25 /* Reference multi-axis reduce in the given axis sequence. Returns a malloc'd 26 * array; *orank / oshape get the result shape. */ 27 static float *rm_ref_multi(OmegaTensorReduceOp op, const float *x, uint32_t rank, const uint64_t *shape, 28 uint32_t n, const uint32_t *seq, bool keepdims, uint32_t *orank, uint64_t *oshape) { 29 uint64_t cur_shape[OMEGA_TENSOR_MAX_RANK]; 30 uint32_t cur_rank = rank; 31 memcpy(cur_shape, shape, rank * sizeof(uint64_t)); 32 float *cur = malloc((size_t)prod(rank, shape) * sizeof(float)); 33 memcpy(cur, x, (size_t)prod(rank, shape) * sizeof(float)); 34 for (uint32_t k = 0; k < n; k++) { 35 uint32_t a = seq[k]; 36 float *nx = malloc((size_t)(prod(cur_rank, cur_shape) / cur_shape[a]) * sizeof(float)); 37 rm_ref_axis(op, cur, cur_rank, cur_shape, a, nx); 38 free(cur); 39 cur = nx; 40 if (keepdims) cur_shape[a] = 1; 41 else { 42 for (uint32_t d = a; d + 1 < cur_rank; d++) cur_shape[d] = cur_shape[d + 1]; 43 cur_rank--; 44 } 45 } 46 *orank = cur_rank; 47 memcpy(oshape, cur_shape, cur_rank * sizeof(uint64_t)); 48 return cur; 49 } 50 51 static bool rm_shape_is(OmegaTensor t, uint32_t rank, const uint64_t *shape) { 52 OmegaTensorInfo in; 53 if (omega_tensor_info(g, t, &in) || in.rank != rank) return false; 54 for (uint32_t d = 0; d < rank; d++) if (in.shape[d] != shape[d]) return false; 55 return true; 56 } 57 58 static void test_reduce_axes(void) { 59 uint64_t s[4] = {3, 5, 4, 6}; 60 float x[360]; 61 fill(x, 360, rand_mod); 62 OmegaTensor X = mk(4, s, x); 63 int order_matters = 0; 64 for (uint32_t mask = 1; mask < 16; mask++) { 65 uint32_t asc[4], desc[4], n = 0; 66 for (uint32_t d = 0; d < 4; d++) if (mask & (1u << d)) asc[n++] = d; 67 for (uint32_t i = 0; i < n; i++) desc[i] = asc[n - 1 - i]; 68 for (int op = 0; op < OMEGA_TR_COUNT; op++) { 69 if (op == OMEGA_TR_MEAN && n > 1) continue; 70 for (int kd = 0; kd < 2; kd++) { 71 uint32_t er; 72 uint64_t es[4]; 73 float *ex = rm_ref_multi((OmegaTensorReduceOp)op, x, 4, s, n, desc, kd, &er, es); 74 uint64_t cnt = prod(er, es); 75 /* axes passed in ascending order: the op must sort them */ 76 OmegaTensor O; 77 int rc = omega_tensor_reduce_axes(g, (OmegaTensorReduceOp)op, X, n, asc, kd, &O); 78 float *got = rc ? NULL : rd(O); 79 CHECK(!rc && got && rm_shape_is(O, er, es) && mismatches(ex, got, (size_t)cnt) == 0, 80 "reduce_axes mask %u op %d keepdims %d vs reference (rc %d)", mask, op, kd, rc); 81 /* equality with the explicit chain of single-axis reduce calls */ 82 OmegaTensor cur = X, nx; 83 int crc = 0; 84 for (uint32_t i = 0; i < n && !crc; i++) { 85 crc = omega_tensor_reduce(g, (OmegaTensorReduceOp)op, cur, desc[i], kd, &nx); 86 if (i > 0) omega_tensor_release(g, cur); 87 if (!crc) cur = nx; 88 } 89 bool eq = false; 90 if (!crc && !rc) omega_tensor_value_equal(g, cur, O, &eq); 91 CHECK(!crc && eq, "reduce_axes mask %u op %d keepdims %d == descending reduce chain", mask, op, kd); 92 if (!crc) omega_tensor_release(g, cur); 93 /* the declared order is observable: ascending gives other bits */ 94 if (op == OMEGA_TR_SUM && kd && n > 1) { 95 uint32_t ar; 96 uint64_t as[4]; 97 float *alt = rm_ref_multi(OMEGA_TR_SUM, x, 4, s, n, asc, true, &ar, as); 98 if (mismatches(ex, alt, (size_t)cnt)) order_matters++; 99 free(alt); 100 } 101 free(got); 102 free(ex); 103 if (!rc) omega_tensor_release(g, O); 104 } 105 } 106 } 107 CHECK(order_matters > 0, "test data distinguishes descending from ascending order (%d subsets)", order_matters); 108 /* full reduction to rank 0 */ 109 { 110 uint32_t all[4] = {2, 0, 3, 1}; 111 OmegaTensor O; 112 int rc = omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 4, all, false, &O); 113 CHECK(!rc && rm_shape_is(O, 0, s), "reduce_axes over every axis gives rank 0"); 114 if (!rc) omega_tensor_release(g, O); 115 } 116 /* MEAN over one axis equals omega_tensor_reduce; over two it is refused */ 117 { 118 uint32_t one[1] = {2}, two[2] = {1, 2}; 119 OmegaTensor O1, O2; 120 bool eq = false; 121 int r1 = omega_tensor_reduce_axes(g, OMEGA_TR_MEAN, X, 1, one, true, &O1); 122 int r2 = omega_tensor_reduce(g, OMEGA_TR_MEAN, X, 2, true, &O2); 123 if (!r1 && !r2) omega_tensor_value_equal(g, O1, O2, &eq); 124 CHECK(!r1 && !r2 && eq, "MEAN one axis == omega_tensor_reduce MEAN"); 125 if (!r1) omega_tensor_release(g, O1); 126 if (!r2) omega_tensor_release(g, O2); 127 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_MEAN, X, 2, two, false, &O1) == OMEGA_TENSOR_ERR_BAD_ARGS, 128 "MEAN over two axes refused (no mean of means)"); 129 } 130 /* naxes 0: dense copy, same value */ 131 { 132 OmegaTensor O; 133 bool eq = false; 134 int rc = omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 0, NULL, false, &O); 135 if (!rc) omega_tensor_value_equal(g, X, O, &eq); 136 CHECK(!rc && eq, "naxes 0 is a copy"); 137 if (!rc) omega_tensor_release(g, O); 138 } 139 /* strided view input equals its contiguous copy */ 140 { 141 uint32_t perm[4] = {3, 1, 0, 2}, ax2[2] = {0, 3}; 142 uint64_t st[4] = {0, 1, 0, 0}, step[4] = {1, 2, 1, 2}; 143 OmegaTensor P, V, C, O1, O2; 144 int rc = omega_tensor_permute(g, X, perm, &P); 145 rc = rc ? rc : omega_tensor_slice(g, P, st, (uint64_t[4]){6, 5, 3, 4}, step, &V); 146 rc = rc ? rc : omega_tensor_contiguous(g, V, &C); 147 bool eq = false; 148 if (!rc) { 149 int a = omega_tensor_reduce_axes(g, OMEGA_TR_SUM, V, 2, ax2, true, &O1); 150 int b = omega_tensor_reduce_axes(g, OMEGA_TR_SUM, C, 2, ax2, true, &O2); 151 if (!a && !b) omega_tensor_value_equal(g, O1, O2, &eq); 152 CHECK(!a && !b && eq, "reduce_axes of strided view == of its copy"); 153 if (!a) omega_tensor_release(g, O1); 154 if (!b) omega_tensor_release(g, O2); 155 omega_tensor_release(g, C); 156 omega_tensor_release(g, V); 157 omega_tensor_release(g, P); 158 } else CHECK(0, "view setup rc %d", rc); 159 } 160 /* refusals: no tensor is made */ 161 { 162 uint32_t lt0, ls0, lt1, ls1; 163 omega_tensor_live_counts(g, <0, &ls0); 164 uint32_t dup[2] = {1, 1}, oor[1] = {4}, five[5] = {0, 1, 2, 3, 0}; 165 OmegaTensor O; 166 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 2, dup, false, &O) == OMEGA_TENSOR_ERR_AXIS, 167 "duplicate axis refused"); 168 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 1, oor, false, &O) == OMEGA_TENSOR_ERR_AXIS, 169 "out-of-range axis refused"); 170 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 5, five, false, &O) == OMEGA_TENSOR_ERR_AXIS, 171 "more axes than rank refused"); 172 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 1, NULL, false, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, 173 "NULL axes refused"); 174 CHECK(omega_tensor_reduce_axes(g, (OmegaTensorReduceOp)9, X, 1, oor, false, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, 175 "unknown reduce op refused"); 176 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, X, 1, dup, false, NULL) == OMEGA_TENSOR_ERR_BAD_ARGS, 177 "NULL out refused"); 178 float sc = 1.0f; 179 OmegaTensor S = mk(0, NULL, &sc); 180 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, S, 0, NULL, false, &O) == OMEGA_TENSOR_ERR_RANK, 181 "rank-0 input refused"); 182 omega_tensor_release(g, S); 183 uint16_t h[6] = {0}; 184 uint64_t s23[2] = {2, 3}; 185 OmegaTensor F; 186 if (!omega_tensor_from_data(g, OMEGA_DT_F16, 2, s23, h, &F)) { 187 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, F, 1, dup, false, &O) == OMEGA_TENSOR_ERR_DTYPE, 188 "f16 reduce_axes refused"); 189 omega_tensor_release(g, F); 190 } else CHECK(0, "f16 create"); 191 OmegaTensor Y = mk(4, s, x); 192 omega_tensor_release(g, Y); 193 CHECK(omega_tensor_reduce_axes(g, OMEGA_TR_SUM, Y, 1, dup, false, &O) == OMEGA_TENSOR_ERR_STALE, 194 "stale handle refused"); 195 omega_tensor_live_counts(g, <1, &ls1); 196 CHECK(lt0 == lt1 && ls0 == ls1, "refusals made no tensor"); 197 } 198 omega_tensor_release(g, X); 199 } 200 201 /* x (rank rx, shape sx) full of exact small integers, broadcast to S, summed 202 * back: every element must be exactly x * (prod(S) / prod(sx)). */ 203 static void rm_unbroadcast_case(uint32_t rx, const uint64_t *sx, uint32_t rs, const uint64_t *S) { 204 uint64_t nx = prod(rx, sx), ns = prod(rs, S); 205 float *x = malloc((size_t)nx * sizeof(float)); 206 for (uint64_t i = 0; i < nx; i++) x[i] = (float)((int)(i % 7) - 3); 207 OmegaTensor X = mk(rx, sx, x), B, O; 208 int rc = omega_tensor_broadcast_to(g, X, rs, S, &B); 209 int rc2 = rc ? rc : omega_tensor_sum_to_shape(g, B, rx, sx, &O); 210 float *got = rc2 ? NULL : rd(O); 211 size_t bad = got ? 0 : 1; 212 for (uint64_t i = 0; got && i < nx; i++) 213 bad += !same(got[i], (float)(((int)(i % 7) - 3) * (int)(ns / nx))); 214 CHECK(!rc && !rc2 && got && rm_shape_is(O, rx, sx) && bad == 0, 215 "sum_to_shape(broadcast_to(x, S), shape(x)) exact, rank %u -> %u (rc %d/%d)", rs, rx, rc, rc2); 216 free(got); 217 if (!rc2) omega_tensor_release(g, O); 218 if (!rc) omega_tensor_release(g, B); 219 omega_tensor_release(g, X); 220 free(x); 221 } 222 223 static void test_sum_to_shape(void) { 224 { uint64_t sx[3] = {3, 1, 4}, S[4] = {2, 3, 5, 4}; rm_unbroadcast_case(3, sx, 4, S); } 225 { uint64_t sx[1] = {4}, S[3] = {2, 3, 4}; rm_unbroadcast_case(1, sx, 3, S); } 226 { uint64_t S[2] = {3, 2}; rm_unbroadcast_case(0, NULL, 2, S); } 227 { uint64_t sx[2] = {1, 4}, S[2] = {3, 4}; rm_unbroadcast_case(2, sx, 2, S); } 228 { uint64_t sx[3] = {2, 1, 3}, S[5] = {1, 2, 2, 5, 3}; rm_unbroadcast_case(3, sx, 5, S); } 229 { uint64_t sx[2] = {1, 1}, S[3] = {4, 3, 2}; rm_unbroadcast_case(2, sx, 3, S); } 230 /* random floats: equals the declared chain (axis 2 keepdims, then axis 0) */ 231 uint64_t s[4] = {2, 3, 5, 4}, tgt[3] = {3, 1, 4}; 232 float x[120]; 233 fill(x, 120, rand_mod); 234 OmegaTensor T = mk(4, s, x), O, A, Bc; 235 int rc = omega_tensor_sum_to_shape(g, T, 3, tgt, &O); 236 int ra = omega_tensor_reduce(g, OMEGA_TR_SUM, T, 2, true, &A); 237 int rb = ra ? ra : omega_tensor_reduce(g, OMEGA_TR_SUM, A, 0, false, &Bc); 238 bool eq = false; 239 if (!rc && !rb) omega_tensor_value_equal(g, O, Bc, &eq); 240 CHECK(!rc && !rb && eq && rm_shape_is(O, 3, tgt), "sum_to_shape == descending single-axis SUM chain"); 241 { 242 uint32_t er; 243 uint64_t es[4]; 244 uint32_t seq[2] = {2, 0}; 245 bool kd[2] = {true, false}; 246 /* reference: axis 2 with keepdims, then axis 0 removed */ 247 float *e1 = rm_ref_multi(OMEGA_TR_SUM, x, 4, s, 1, seq, kd[0], &er, es); 248 uint64_t s2[4] = {2, 3, 1, 4}; 249 float *e2 = rm_ref_multi(OMEGA_TR_SUM, e1, 4, s2, 1, seq + 1, kd[1], &er, es); 250 float *got = rc ? NULL : rd(O); 251 CHECK(got && mismatches(e2, got, 12) == 0, "sum_to_shape vs independent reference"); 252 free(got); 253 free(e1); 254 free(e2); 255 } 256 if (!rc) omega_tensor_release(g, O); 257 if (!rb) omega_tensor_release(g, Bc); 258 if (!ra) omega_tensor_release(g, A); 259 /* same shape: a copy; size-1 leading axes of t are dropped, bits kept */ 260 { 261 bool e = false; 262 rc = omega_tensor_sum_to_shape(g, T, 4, s, &O); 263 if (!rc) omega_tensor_value_equal(g, T, O, &e); 264 CHECK(!rc && e, "sum_to_shape to own shape is a copy"); 265 if (!rc) omega_tensor_release(g, O); 266 float y[6] = {omega_bits_to_float(0x80000000U), 1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; 267 uint64_t s1[3] = {1, 2, 3}, s23[2] = {2, 3}; 268 OmegaTensor Y = mk(3, s1, y); 269 float *gy = NULL; 270 rc = omega_tensor_sum_to_shape(g, Y, 2, s23, &O); 271 if (!rc) gy = rd(O); 272 CHECK(!rc && gy && rm_shape_is(O, 2, s23) && mismatches(gy, y, 6) == 0, 273 "size-1 leading axis dropped, bits (incl. -0) kept"); 274 free(gy); 275 if (!rc) omega_tensor_release(g, O); 276 omega_tensor_release(g, Y); 277 } 278 /* refusals: no tensor is made */ 279 { 280 uint32_t lt0, ls0, lt1, ls1; 281 omega_tensor_live_counts(g, <0, &ls0); 282 uint64_t bad1[3] = {3, 2, 4}, bad2[2] = {4, 5}, r5[5] = {1, 2, 3, 5, 4}, z[3] = {3, 0, 4}, 283 r9[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; 284 CHECK(omega_tensor_sum_to_shape(g, T, 3, bad1, &O) == OMEGA_TENSOR_ERR_SHAPE, 285 "incompatible size (2 vs 5) refused"); 286 CHECK(omega_tensor_sum_to_shape(g, T, 2, bad2, &O) == OMEGA_TENSOR_ERR_SHAPE, 287 "incompatible trailing shape refused"); 288 CHECK(omega_tensor_sum_to_shape(g, T, 5, r5, &O) == OMEGA_TENSOR_ERR_SHAPE, 289 "target rank above t's rank refused"); 290 CHECK(omega_tensor_sum_to_shape(g, T, 3, z, &O) == OMEGA_TENSOR_ERR_SHAPE, "zero dim refused"); 291 CHECK(omega_tensor_sum_to_shape(g, T, 9, r9, &O) == OMEGA_TENSOR_ERR_RANK, "rank 9 refused"); 292 CHECK(omega_tensor_sum_to_shape(g, T, 3, NULL, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, "NULL shape refused"); 293 CHECK(omega_tensor_sum_to_shape(g, T, 3, tgt, NULL) == OMEGA_TENSOR_ERR_BAD_ARGS, "NULL out refused"); 294 uint16_t h[6] = {0}; 295 uint64_t s23[2] = {2, 3}, s3[1] = {3}; 296 OmegaTensor F; 297 if (!omega_tensor_from_data(g, OMEGA_DT_F16, 2, s23, h, &F)) { 298 CHECK(omega_tensor_sum_to_shape(g, F, 1, s3, &O) == OMEGA_TENSOR_ERR_DTYPE, "f16 sum_to_shape refused"); 299 omega_tensor_release(g, F); 300 } else CHECK(0, "f16 create"); 301 OmegaTensor Y = mk(4, s, x); 302 omega_tensor_release(g, Y); 303 CHECK(omega_tensor_sum_to_shape(g, Y, 3, tgt, &O) == OMEGA_TENSOR_ERR_STALE, "stale handle refused"); 304 omega_tensor_live_counts(g, <1, &ls1); 305 CHECK(lt0 == lt1 && ls0 == ls1, "sum_to_shape refusals made no tensor"); 306 } 307 omega_tensor_release(g, T); 308 }