diff --git a/docs/tensor/M20_OMEGA_TENSOR.md b/docs/tensor/M20_OMEGA_TENSOR.md index 841a70c2b..dab3ddde9 100644 --- a/docs/tensor/M20_OMEGA_TENSOR.md +++ b/docs/tensor/M20_OMEGA_TENSOR.md @@ -125,6 +125,8 @@ removed. Order and padding mutations are caught by the E1 WP-D suite. | Crash-safe storage lifetime | PASS (CPU) | host PASS, forge log HIVE-M20-lifetime-light-182200 (`test-tensor-store` 314/0 plain + ASan/UBSan, 6/6 store mutants caught, hook absent from default build). `src/tensor/omega_tensor_store.{c,h}`: content-addressed payloads + checksummed journal, temp/fsync/rename/fsync-dir commit; recovery replays the journal, re-hashes every payload, refuses torn/short/corrupt records (typed errors), keeps generations monotonic; `test-tensor-store` (round trip, fork crash at all 8 commit phases, journal/payload truncated at every byte and bit-flipped, crafted journals) + 6 store mutants in `test-tensor-mutations` | | Reproducible receipt | PASS (CPU) | writer implemented and host-tested: `tools/m20_receipt.sh` writes `/.json` (0444, exclusive, never overwritten), QUALIFIED only with GB10 parity GB10 PASS + matching chip evidence; `test-m20-receipt` + 11 receipt mutants in `test-tensor-mutations`, forge log HIVE-M20-receipt-175835. No real M20 receipt has been written yet because the GB10 parity evidence does not exist. | | E1 numeric closure | PASS (on main) | E1 PASSED on main 4863803; receipt merged in omega#177 | +| Placement: embed + concat (CR-4, answers LT-M21 CR-4) | PASS (CPU) | Host PASS, CPU tier: forge log `HIVE-M20-scatter-light-193431` at pre-stack head d99af69; re-run at stacked head queued (job HIVE-M20-scatter). `omega_tensor_embed(ctx, src, rank, out_shape, start, step, out)` is the exact inverse placement of `omega_tensor_slice` (`src/tensor/omega_tensor.h` slice: per axis start < stop <= dim, step >= 1, out dim = ceil((stop-start)/step)). Declared bits: a new dense tensor, all-zero bits (+0.0) everywhere except src element i copied bit for bit (NaN payload and -0 kept, any dtype) to start + i*step per axis; so slice(embed(x), start, start+(n-1)*step+1, step) == x. `omega_tensor_concat(ctx, n, tensors, axis, out)`: parts joined in array order along axis, bit for bit. Both are pure semantic-layer copies (gather + scatter in `src/tensor/omega_tensor.c`), no realization call, no arithmetic. Typed refusals, nothing created: rank mismatch / rank 0 `ERR_RANK`, step 0 `ERR_BAD_ARGS` (as slice), a placed position outside out_shape or start/step overflow `ERR_BOUNDS`, zero dim `ERR_SHAPE`, > MAX_ELEMS `ERR_CAPACITY`, concat dtype mismatch `ERR_DTYPE`, other-axis mismatch `ERR_SHAPE`, axis >= rank `ERR_AXIS`, n = 0 / NULL `ERR_BAD_ARGS`, stale handle `ERR_STALE`. `test-tensor` test_placement: 60 random embed trials vs an independent index-arithmetic reference, slice(embed(x)) == x, embed(slice(y)) zero off the sliced set, transposed / broadcast / F16 sources, 40 random concat trials vs reference and slice recovery, view parts, every refusal. Mutants `EMBED_START`, `EMBED_STEP`, `CONCAT_AXIS_OFFSET` in `test-tensor-mutations`. Gather is OUT OF SCOPE: M20 has no index tensor op, so gather / gather-backward is not provided | +| GB10 placement (embed, concat) | NOT_RUN | No GB10 realization table exists (row "GB10 parity"); embed and concat call no E1 op, so there is no E1 GB10 kernel to cite. CPU tier only in this work | **M20 verdict: not qualified.** GB10 parity NOT_RUN; receipt writer host PASS, no M20 receipt written yet. diff --git a/src/tensor/omega_tensor.c b/src/tensor/omega_tensor.c index 534cd836a..30d73e429 100644 --- a/src/tensor/omega_tensor.c +++ b/src/tensor/omega_tensor.c @@ -878,3 +878,125 @@ void omega_tensor_live_counts(const OmegaTensorCtx *ctx, uint32_t *tensors, uint if (tensors) *tensors = t; if (storages) *storages = s; } + +/* ---- placement (CR-4): embed and concat ---------------------------------- */ +/* Pure bit copies: gather the source in logical row-major order, then write + * it through a destination descriptor into fresh zero-filled dense storage + * (storage_alloc uses calloc: all-zero bits = +0.0 in F32/F16/BF16). */ + +/* Inverse of gather: element e of src (row-major) goes to the storage index + * the descriptor `in` gives for logical index e. */ +static void scatter(StorageSlot *s, const OmegaTensorInfo *in, const void *src) { + size_t es = omega_dtype_size(in->dtype); + uint64_t idx[OMEGA_TENSOR_MAX_RANK] = {0}; + uint64_t pos = in->offset; + uint8_t *dst = s->data; + const uint8_t *sp = src; + for (uint64_t e = 0; e < in->elements; e++) { + memcpy(dst + pos * es, sp + e * es, es); + for (uint32_t ax = in->rank; ax-- > 0;) { + if (++idx[ax] < in->shape[ax]) { pos += in->strides[ax]; break; } + pos -= (in->shape[ax] - 1) * in->strides[ax]; + idx[ax] = 0; + } + } +} + +/* Gather part (s, in) and scatter it through dst_desc into out's storage. */ +static int place(OmegaTensorCtx *ctx, const StorageSlot *s, const OmegaTensorInfo *in, OmegaTensor out, + const OmegaTensorInfo *dst_desc) { + StorageSlot *ds = &ctx->st[ctx->ts[out.slot].info.storage.slot]; + if (bounds_check(dst_desc, ds->elems)) return OMEGA_TENSOR_ERR_BOUNDS; /* defence in depth */ + void *tmp = malloc((size_t)in->elements * omega_dtype_size(in->dtype)); + if (!tmp) return OMEGA_TENSOR_ERR_CAPACITY; + gather(s, in, tmp); + scatter(ds, dst_desc, tmp); + free(tmp); + return OMEGA_TENSOR_OK; +} + +int omega_tensor_embed(OmegaTensorCtx *ctx, OmegaTensor src, uint32_t rank, const uint64_t *out_shape, + const uint64_t *start, const uint64_t *step, OmegaTensor *out) { + TensorSlot *x; + StorageSlot *s; + if (!out) return OMEGA_TENSOR_ERR_BAD_ARGS; + int rc = tensor_get(ctx, src, &x, &s); + if (rc) return rc; + OmegaTensorInfo in = x->info; /* copy: slot table may be reused below */ + if (rank == 0 || rank != in.rank) return OMEGA_TENSOR_ERR_RANK; + if (!out_shape || !start) return OMEGA_TENSOR_ERR_BAD_ARGS; + uint64_t n; + rc = shape_check(rank, out_shape, &n); + if (rc) return rc; + uint64_t ds[OMEGA_TENSOR_MAX_RANK]; + dense_strides(rank, out_shape, ds); + OmegaTensorInfo v = in; /* destination descriptor over out's storage */ + v.offset = 0; + for (uint32_t d = 0; d < rank; d++) { + uint64_t st = step ? step[d] : 1; + if (st == 0) return OMEGA_TENSOR_ERR_BAD_ARGS; + uint64_t last, so; + if (__builtin_mul_overflow(in.shape[d] - 1, st, &last) || + __builtin_add_overflow(last, start[d], &last) || last >= out_shape[d]) + return OMEGA_TENSOR_ERR_BOUNDS; + if (__builtin_mul_overflow(start[d], ds[d], &so) || /* MUT:EMBED_START */ + __builtin_add_overflow(v.offset, so, &v.offset)) + return OMEGA_TENSOR_ERR_BOUNDS; + if (in.shape[d] == 1) v.strides[d] = 0; /* never used; avoids overflow */ + else if (__builtin_mul_overflow(ds[d], st, &v.strides[d])) /* MUT:EMBED_STEP */ + return OMEGA_TENSOR_ERR_BOUNDS; + } + void *buf; + rc = new_dense(ctx, in.dtype, rank, out_shape, out, &buf); + if (rc) return rc; + rc = place(ctx, s, &in, *out, &v); + if (rc) omega_tensor_release(ctx, *out); + return rc; +} + +int omega_tensor_concat(OmegaTensorCtx *ctx, uint32_t n, const OmegaTensor *tensors, uint32_t axis, + OmegaTensor *out) { + if (!ctx || !out || !tensors || n == 0) return OMEGA_TENSOR_ERR_BAD_ARGS; + TensorSlot *x0; + int rc = tensor_get(ctx, tensors[0], &x0, NULL); + if (rc) return rc; + OmegaTensorInfo first = x0->info; + if (first.rank == 0) return OMEGA_TENSOR_ERR_RANK; + if (axis >= first.rank) return OMEGA_TENSOR_ERR_AXIS; + uint64_t shape[OMEGA_TENSOR_MAX_RANK]; + memcpy(shape, first.shape, sizeof(shape)); + shape[axis] = 0; + for (uint32_t k = 0; k < n; k++) { /* validate everything before allocating */ + TensorSlot *xk; + rc = tensor_get(ctx, tensors[k], &xk, NULL); + if (rc) return rc; + const OmegaTensorInfo *ik = &xk->info; + if (ik->dtype != first.dtype) return OMEGA_TENSOR_ERR_DTYPE; + if (ik->rank != first.rank) return OMEGA_TENSOR_ERR_RANK; + for (uint32_t d = 0; d < first.rank; d++) + if (d != axis && ik->shape[d] != first.shape[d]) return OMEGA_TENSOR_ERR_SHAPE; + if (__builtin_add_overflow(shape[axis], ik->shape[axis], &shape[axis]) || + shape[axis] > OMEGA_TENSOR_MAX_ELEMS) + return OMEGA_TENSOR_ERR_CAPACITY; + } + void *buf; + rc = new_dense(ctx, first.dtype, first.rank, shape, out, &buf); /* MAX_ELEMS -> ERR_CAPACITY */ + if (rc) return rc; + uint64_t ds[OMEGA_TENSOR_MAX_RANK]; + dense_strides(first.rank, shape, ds); + uint64_t aoff = 0; + for (uint32_t k = 0; k < n && !rc; k++) { + TensorSlot *xk; + StorageSlot *sk; + rc = tensor_get(ctx, tensors[k], &xk, &sk); + if (rc) break; + OmegaTensorInfo ik = xk->info; + OmegaTensorInfo v = ik; + v.offset = aoff * ds[axis]; /* MUT:CONCAT_AXIS_OFFSET */ + for (uint32_t d = 0; d < ik.rank; d++) v.strides[d] = ds[d]; + rc = place(ctx, sk, &ik, *out, &v); + aoff += ik.shape[axis]; + } + if (rc) omega_tensor_release(ctx, *out); + return rc; +} diff --git a/src/tensor/omega_tensor.h b/src/tensor/omega_tensor.h index 092976c8c..b25c8ad02 100644 --- a/src/tensor/omega_tensor.h +++ b/src/tensor/omega_tensor.h @@ -303,6 +303,35 @@ int omega_tensor_reduce(OmegaTensorCtx *ctx, OmegaTensorReduceOp op, OmegaTensor */ int omega_tensor_matmul(OmegaTensorCtx *ctx, OmegaTensor a, OmegaTensor b, OmegaTensor *out); +/* ---- placement (CR-4): pure bit copies, no arithmetic -------------------- */ +/* + * Embed: the exact inverse placement of omega_tensor_slice. Returns a new + * dense tensor of (rank, out_shape), dtype of src, every element all-zero + * bits (+0.0 for F32/F16/BF16), except that src element i (logical row-major + * multi-index) is copied bit for bit to out position start + i * step, per + * axis. step may be NULL (all 1). For y = embed(x, start, step) and + * stop[d] = start[d] + (x.shape[d] - 1) * step[d] + 1, + * slice(y, start, stop, step) is x bit for bit. + * Refused (typed, nothing created): rank != src rank or rank 0 -> ERR_RANK; + * out_shape with a 0 dim -> ERR_SHAPE; result > OMEGA_TENSOR_MAX_ELEMS -> + * ERR_CAPACITY; a step of 0 (steps are unsigned, so "<= 0" is exactly 0) -> + * ERR_BAD_ARGS (as in slice); any placed position outside out_shape -> + * ERR_BOUNDS. Works for every dtype (no arithmetic). + */ +int omega_tensor_embed(OmegaTensorCtx *ctx, OmegaTensor src, uint32_t rank, const uint64_t *out_shape, + const uint64_t *start, const uint64_t *step, OmegaTensor *out); +/* + * Concat: n >= 1 tensors joined along axis, in array order, bit for bit. + * All parts must have the same dtype (else ERR_DTYPE), the same rank >= 1 + * (else ERR_RANK) and equal sizes on every other axis (else ERR_SHAPE); + * axis >= rank -> ERR_AXIS; total > OMEGA_TENSOR_MAX_ELEMS -> ERR_CAPACITY; + * n == 0 or NULL arrays -> ERR_BAD_ARGS. Parts may be views (strided, + * broadcast). Part k occupies [off_k, off_k + part_k.shape[axis]) on axis, + * off_k = sum of earlier parts' sizes on axis. + */ +int omega_tensor_concat(OmegaTensorCtx *ctx, uint32_t n, const OmegaTensor *tensors, uint32_t axis, + OmegaTensor *out); + /* Live tensor descriptors and live storage slots (lifetime audits). */ void omega_tensor_live_counts(const OmegaTensorCtx *ctx, uint32_t *tensors, uint32_t *storages); diff --git a/tests/test_omega_tensor.c b/tests/test_omega_tensor.c index e2cb97fc4..b7cb0a35e 100644 --- a/tests/test_omega_tensor.c +++ b/tests/test_omega_tensor.c @@ -1411,6 +1411,304 @@ static void test_const_neg(void) { for (size_t i = 0; i < sizeof(all) / sizeof(all[0]); i++) omega_tensor_release(g, all[i]); } +/* ---- placement (CR-4): embed + concat ------------------------------------- * + * Reference: zero-filled row-major array; each source element's position is + * computed from shapes, start and step only (never from tensor strides). + * Every comparison is on raw bits (NaN payloads included: pure copies). */ +static uint32_t *rdbits(OmegaTensor t, uint64_t n) { + uint32_t *o = malloc((size_t)n * 4 + 4); + if (o && omega_tensor_read(g, t, o, (size_t)n * 4)) { free(o); return NULL; } + return o; +} +static void row_strides(uint32_t r, const uint64_t *s, uint64_t *st) { + uint64_t m = 1; + for (uint32_t d = r; d-- > 0;) { st[d] = m; m *= s[d]; } +} +static void ref_embed(uint32_t r, const uint64_t *xs, const uint32_t *x, const uint64_t *os, + const uint64_t *start, const uint64_t *step, uint32_t *o) { + uint64_t ost[OMEGA_TENSOR_MAX_RANK]; + row_strides(r, os, ost); + memset(o, 0, (size_t)prod(r, os) * 4); + for (uint64_t e = 0; e < prod(r, xs); e++) { + uint64_t rem = e, pos = 0; + for (uint32_t d = r; d-- > 0;) { + uint64_t i = rem % xs[d]; + rem /= xs[d]; + pos += (start[d] + i * (step ? step[d] : 1)) * ost[d]; + } + o[pos] = x[e]; + } +} +static size_t bits_diff(const uint32_t *a, const uint32_t *b, uint64_t n) { + size_t m = 0; + for (uint64_t i = 0; i < n; i++) m += a[i] != b[i]; + return m; +} +static uint32_t live_tensors(void) { uint32_t t, s; omega_tensor_live_counts(g, &t, &s); return t; } + +static void test_placement(void) { + /* 1. slice(embed(x)) == x and embed == reference, random small shapes */ + size_t bad_ref = 0, bad_rt = 0, bad_inv = 0; + int rcs = 0; + for (int trial = 0; trial < 60; trial++) { + uint32_t r = 1 + (uint32_t)(rnd() % 4); + uint64_t xs[4], os[4], st[4], sp[4], stop[4]; + for (uint32_t d = 0; d < r; d++) { + xs[d] = 1 + rnd() % 4; + st[d] = rnd() % 3; + sp[d] = 1 + rnd() % 3; + stop[d] = st[d] + (xs[d] - 1) * sp[d] + 1; + os[d] = stop[d] + rnd() % 3; + } + uint64_t n = prod(r, xs), on = prod(r, os); + float *x = malloc((size_t)n * 4); + fill(x, (size_t)n, rand_f32); + OmegaTensor X = mk(r, xs, x), Y = {0, 0}, Z = {0, 0}, W = {0, 0}; + uint32_t *want = malloc((size_t)on * 4), *xb = malloc((size_t)n * 4); + memcpy(xb, x, (size_t)n * 4); + ref_embed(r, xs, xb, os, st, sp, want); + int rc = omega_tensor_embed(g, X, r, os, st, sp, &Y); + rcs |= rc; + if (!rc) { + uint32_t *y = rdbits(Y, on); + bad_ref += y ? bits_diff(y, want, on) : 1; + if (!omega_tensor_slice(g, Y, st, stop, sp, &Z)) { + uint32_t *z = rdbits(Z, n); + bad_rt += z ? bits_diff(z, xb, n) : 1; + free(z); + /* embed(slice(y)) is y again here (y is zero off the placed set) */ + if (!omega_tensor_embed(g, Z, r, os, st, sp, &W)) { + uint32_t *w = rdbits(W, on); + bad_inv += w ? bits_diff(w, want, on) : 1; + free(w); + omega_tensor_release(g, W); + } else bad_inv++; + omega_tensor_release(g, Z); + } else bad_rt++; + free(y); + omega_tensor_release(g, Y); + } + omega_tensor_release(g, X); + free(x); free(want); free(xb); + } + CHECK(rcs == 0, "embed: random trials all accepted"); + CHECK(bad_ref == 0, "embed == independent reference (%zu bad elements)", bad_ref); + CHECK(bad_rt == 0, "slice(embed(x)) == x bit exact (%zu bad)", bad_rt); + CHECK(bad_inv == 0, "embed(slice(embed(x))) == embed(x) (%zu bad)", bad_inv); + + /* 2. embed(slice(y)) keeps only the sliced positions, y full of non-zero bits */ + { + uint64_t ys[3] = {5, 6, 7}, st[3] = {1, 0, 2}, stop[3] = {5, 6, 7}, sp[3] = {2, 3, 2}; + uint64_t n = prod(3, ys); + uint32_t yb[210]; + for (uint64_t i = 0; i < n; i++) yb[i] = 0x3f800000U + (uint32_t)i; /* never zero bits */ + OmegaTensor Yt = mk(3, ys, (const float *)(const void *)yb), S, E; + CHECK(omega_tensor_slice(g, Yt, st, stop, sp, &S) == 0, "embed: slice of y"); + CHECK(omega_tensor_embed(g, S, 3, ys, st, sp, &E) == 0, "embed(slice(y))"); + uint32_t *e = rdbits(E, n); + size_t wrong = e ? 0 : 1, placed = 0; + for (uint64_t i = 0; e && i < n; i++) { + uint64_t a = i / 42, b = (i / 7) % 6, c = i % 7; + bool in = a >= 1 && (a - 1) % 2 == 0 && b % 3 == 0 && c >= 2 && (c - 2) % 2 == 0; + placed += in; + wrong += in ? e[i] != yb[i] : e[i] != 0; + } + CHECK(wrong == 0 && placed == 2 * 2 * 3, "embed(slice(y)): sliced positions = y, rest +0.0 (%zu wrong, %zu placed)", + wrong, placed); + free(e); + omega_tensor_release(g, E); omega_tensor_release(g, S); omega_tensor_release(g, Yt); + } + + /* 3. strided and broadcast sources: expected from index arithmetic on the parent */ + { + float d[12]; + for (int i = 0; i < 12; i++) d[i] = (float)(i + 1); + OmegaTensor T = mk(2, (uint64_t[]){3, 4}, d), TT, E1, R, RB, E2; + CHECK(omega_tensor_transpose(g, T, &TT) == 0, "embed: transpose view"); /* [4,3], (i,j) = d[j*4+i] */ + uint64_t os[2] = {9, 7}, st[2] = {1, 2}, sp[2] = {2, 2}; + CHECK(omega_tensor_embed(g, TT, 2, os, st, sp, &E1) == 0, "embed of strided view"); + uint32_t *e = rdbits(E1, 63); + size_t wrong = e ? 0 : 1; + for (uint64_t p = 0; e && p < 63; p++) { + uint64_t a = p / 7, b = p % 7; + float want = 0.0f; + if (a >= 1 && (a - 1) % 2 == 0 && (a - 1) / 2 < 4 && b >= 2 && (b - 2) % 2 == 0 && (b - 2) / 2 < 3) + want = d[((b - 2) / 2) * 4 + (a - 1) / 2]; + uint32_t wb; memcpy(&wb, &want, 4); + wrong += e[p] != wb; + } + CHECK(wrong == 0, "embed of transposed view (%zu wrong)", wrong); + free(e); + R = mk(2, (uint64_t[]){1, 3}, (float[]){-0.0f, 2.5f, -7.0f}); + CHECK(omega_tensor_broadcast_to(g, R, 2, (uint64_t[]){2, 3}, &RB) == 0, "embed: broadcast view"); + CHECK(omega_tensor_embed(g, RB, 2, (uint64_t[]){3, 4}, (uint64_t[]){1, 1}, NULL, &E2) == 0, + "embed of broadcast view, step NULL"); + uint32_t *f = rdbits(E2, 12); + static const float want2[12] = {0, 0, 0, 0, 0, -0.0f, 2.5f, -7.0f, 0, -0.0f, 2.5f, -7.0f}; + uint32_t wb2[12]; + memcpy(wb2, want2, sizeof(wb2)); + CHECK(f && bits_diff(f, wb2, 12) == 0, "embed of broadcast: values, -0.0 kept, fill +0.0"); + free(f); + /* F16 storage dtype: pure bit copy, no arithmetic dtype needed */ + uint16_t h[2] = {0x7e01U, 0x8000U}, hb[4] = {1, 1, 1, 1}; + OmegaTensor H, HE; + CHECK(omega_tensor_from_data(g, OMEGA_DT_F16, 1, (uint64_t[]){2}, h, &H) == 0, "f16 source"); + CHECK(omega_tensor_embed(g, H, 1, (uint64_t[]){4}, (uint64_t[]){1}, (uint64_t[]){2}, &HE) == 0 && + omega_tensor_read(g, HE, hb, sizeof(hb)) == 0 && hb[0] == 0 && hb[1] == 0x7e01U && hb[2] == 0 && + hb[3] == 0x8000U, "embed F16 bit copy (NaN payload, -0)"); + OmegaTensor rel[] = {HE, H, E2, RB, R, E1, TT, T}; + for (size_t i = 0; i < sizeof(rel) / sizeof(rel[0]); i++) omega_tensor_release(g, rel[i]); + } + + /* 4. concat: reference loop, slice recovers parts, views as parts */ + { + size_t bad = 0, badrt = 0; + int crc = 0; + for (int trial = 0; trial < 40; trial++) { + uint32_t r = 1 + (uint32_t)(rnd() % 4), ax = (uint32_t)(rnd() % r), np = 1 + (uint32_t)(rnd() % 4); + uint64_t base[4]; + for (uint32_t d = 0; d < r; d++) base[d] = 1 + rnd() % 3; + OmegaTensor P[4], C = {0, 0}; + uint32_t *pb[4]; + uint64_t ps[4][4], tot = 0; + for (uint32_t k = 0; k < np; k++) { + memcpy(ps[k], base, sizeof(base)); + ps[k][ax] = 1 + rnd() % 3; + tot += ps[k][ax]; + uint64_t n = prod(r, ps[k]); + float *x = malloc((size_t)n * 4); + fill(x, (size_t)n, rand_f32); + P[k] = mk(r, ps[k], x); + pb[k] = malloc((size_t)n * 4); + memcpy(pb[k], x, (size_t)n * 4); + free(x); + } + uint64_t cs[4]; + memcpy(cs, base, sizeof(base)); + cs[ax] = tot; + uint64_t cn = prod(r, cs), off = 0; + uint32_t *want = calloc((size_t)cn, 4); + for (uint32_t k = 0; k < np; k++) { /* reference: embed each part at its axis offset */ + uint64_t st[4] = {0, 0, 0, 0}; + st[ax] = off; + uint64_t cst[4]; + row_strides(r, cs, cst); + for (uint64_t e = 0; e < prod(r, ps[k]); e++) { + uint64_t pos = 0, rem = e; + for (uint32_t d = r; d-- > 0;) { pos += (rem % ps[k][d] + st[d]) * cst[d]; rem /= ps[k][d]; } + want[pos] = pb[k][e]; + } + off += ps[k][ax]; + } + int rc = omega_tensor_concat(g, np, P, ax, &C); + crc |= rc; + if (!rc) { + uint32_t *c = rdbits(C, cn); + bad += c ? bits_diff(c, want, cn) : 1; + free(c); + off = 0; + for (uint32_t k = 0; k < np; k++) { + uint64_t st[4] = {0, 0, 0, 0}, stop[4]; + memcpy(stop, cs, sizeof(cs)); + st[ax] = off; + stop[ax] = off + ps[k][ax]; + OmegaTensor S; + if (omega_tensor_slice(g, C, st, stop, NULL, &S) == 0) { + uint32_t *sb = rdbits(S, prod(r, ps[k])); + badrt += sb ? bits_diff(sb, pb[k], prod(r, ps[k])) : 1; + free(sb); + omega_tensor_release(g, S); + } else badrt++; + off += ps[k][ax]; + } + omega_tensor_release(g, C); + } + for (uint32_t k = 0; k < np; k++) { omega_tensor_release(g, P[k]); free(pb[k]); } + free(want); + } + CHECK(crc == 0, "concat: random trials all accepted"); + CHECK(bad == 0, "concat == independent reference (%zu bad)", bad); + CHECK(badrt == 0, "slice(concat(parts)) recovers each part (%zu bad)", badrt); + } + { /* view parts: transpose [2,3]->[3,2] and broadcast [1,2]->[2,2], concat on axis 0 */ + OmegaTensor A = mk(2, (uint64_t[]){2, 3}, (float[]){1, 2, 3, 4, 5, 6}), AT, B, BB, C; + omega_tensor_transpose(g, A, &AT); + B = mk(2, (uint64_t[]){1, 2}, (float[]){-0.0f, 9}); + omega_tensor_broadcast_to(g, B, 2, (uint64_t[]){2, 2}, &BB); + OmegaTensor parts[2] = {AT, BB}; + CHECK(omega_tensor_concat(g, 2, parts, 0, &C) == 0, "concat of views"); + uint32_t *c = rdbits(C, 10); + static const float want[10] = {1, 4, 2, 5, 3, 6, -0.0f, 9, -0.0f, 9}; + uint32_t wb[10]; + memcpy(wb, want, sizeof(wb)); + CHECK(c && bits_diff(c, wb, 10) == 0, "concat of transposed + broadcast views"); + free(c); + OmegaTensor parts1[2] = {BB, AT}; /* axis 1: [2,2] ++ [3,2] mismatch on axis 0 */ + CHECK(omega_tensor_concat(g, 2, parts1, 1, &C) == OMEGA_TENSOR_ERR_SHAPE, "concat: other axis mismatch"); + OmegaTensor rel[] = {BB, B, AT, A}; + for (size_t i = 0; i < 4; i++) omega_tensor_release(g, rel[i]); + omega_tensor_release(g, C); + } + + /* 5. refusal paths: typed error, nothing created */ + { + uint32_t live0 = live_tensors(); + OmegaTensor X = mk(2, (uint64_t[]){2, 3}, (float[]){1, 2, 3, 4, 5, 6}), O; + uint32_t live1 = live_tensors(); + uint64_t os[2] = {4, 6}, z2[2] = {0, 0}; + CHECK(omega_tensor_embed(g, X, 1, os, z2, NULL, &O) == OMEGA_TENSOR_ERR_RANK, "embed: rank mismatch"); + CHECK(omega_tensor_embed(g, X, 2, os, z2, (uint64_t[]){1, 0}, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, "embed: step 0"); + OmegaTensor tmp; + CHECK(omega_tensor_embed(g, X, 2, os, (uint64_t[]){2, 3}, NULL, &tmp) == 0, "embed: last position at edge ok"); + omega_tensor_release(g, tmp); + CHECK(omega_tensor_embed(g, X, 2, os, (uint64_t[]){3, 3}, NULL, &O) == OMEGA_TENSOR_ERR_BOUNDS, + "embed: start one past the edge"); + CHECK(omega_tensor_embed(g, X, 2, os, z2, (uint64_t[]){4, 1}, &O) == OMEGA_TENSOR_ERR_BOUNDS, + "embed: step pushes last position out (1*4 >= 4)"); + CHECK(omega_tensor_embed(g, X, 2, os, (uint64_t[]){UINT64_MAX, 0}, NULL, &O) == OMEGA_TENSOR_ERR_BOUNDS, + "embed: start overflow"); + CHECK(omega_tensor_embed(g, X, 2, os, z2, (uint64_t[]){UINT64_MAX, 1}, &O) == OMEGA_TENSOR_ERR_BOUNDS, + "embed: step overflow"); + CHECK(omega_tensor_embed(g, X, 2, (uint64_t[]){4, 0}, z2, NULL, &O) == OMEGA_TENSOR_ERR_SHAPE, + "embed: zero dim"); + CHECK(omega_tensor_embed(g, X, 2, (uint64_t[]){(1u << 15) + 1, 1u << 15}, z2, NULL, &O) == + OMEGA_TENSOR_ERR_CAPACITY, "embed: result > MAX_ELEMS"); + CHECK(omega_tensor_embed(g, X, 2, os, NULL, NULL, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, "embed: NULL start"); + CHECK(omega_tensor_embed(g, X, 2, os, z2, NULL, NULL) == OMEGA_TENSOR_ERR_BAD_ARGS, "embed: NULL out"); + OmegaTensor S0 = mk(0, NULL, (float[]){1}); + CHECK(omega_tensor_embed(g, S0, 0, NULL, NULL, NULL, &O) == OMEGA_TENSOR_ERR_RANK, "embed: rank 0"); + CHECK(live_tensors() == live1 + 1, "embed refusals created nothing"); + /* concat refusals */ + OmegaTensor Y = mk(2, (uint64_t[]){2, 3}, (float[]){1, 2, 3, 4, 5, 6}), V = mk(1, (uint64_t[]){3}, (float[]){1, 2, 3}); + OmegaTensor H; + uint16_t hv[6] = {0}; + omega_tensor_from_data(g, OMEGA_DT_F16, 2, (uint64_t[]){2, 3}, hv, &H); + uint32_t live2 = live_tensors(); + CHECK(omega_tensor_concat(g, 0, (OmegaTensor[]){X}, 0, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, "concat: n = 0"); + CHECK(omega_tensor_concat(g, 1, NULL, 0, &O) == OMEGA_TENSOR_ERR_BAD_ARGS, "concat: NULL list"); + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){X, H}, 0, &O) == OMEGA_TENSOR_ERR_DTYPE, "concat: dtype mismatch"); + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){X, V}, 0, &O) == OMEGA_TENSOR_ERR_RANK, "concat: rank mismatch"); + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){X, Y}, 2, &O) == OMEGA_TENSOR_ERR_AXIS, "concat: axis >= rank"); + CHECK(omega_tensor_concat(g, 1, (OmegaTensor[]){S0}, 0, &O) == OMEGA_TENSOR_ERR_RANK, "concat: rank 0"); + OmegaTensor one = mk(1, (uint64_t[]){1}, (float[]){1}), big; + CHECK(omega_tensor_broadcast_to(g, one, 1, (uint64_t[]){((uint64_t)1 << 29) + 1}, &big) == 0, "big bcast view"); + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){big, big}, 0, &O) == OMEGA_TENSOR_ERR_CAPACITY, + "concat: result > MAX_ELEMS"); + OmegaTensor dead = mk(1, (uint64_t[]){3}, (float[]){1, 2, 3}); + omega_tensor_release(g, dead); + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){V, dead}, 0, &O) == OMEGA_TENSOR_ERR_STALE, "concat: stale part"); + CHECK(omega_tensor_embed(g, dead, 1, (uint64_t[]){4}, (uint64_t[]){0}, NULL, &O) == OMEGA_TENSOR_ERR_STALE, + "embed: stale source"); + CHECK(live_tensors() == live2 + 2, "concat refusals created nothing"); + OmegaTensor ok2; + CHECK(omega_tensor_concat(g, 2, (OmegaTensor[]){X, Y}, 1, &ok2) == 0, "concat axis 1 accepted"); + omega_tensor_release(g, ok2); + OmegaTensor rel[] = {big, one, H, V, Y, S0, X}; + for (size_t i = 0; i < sizeof(rel) / sizeof(rel[0]); i++) omega_tensor_release(g, rel[i]); + CHECK(live_tensors() == live0, "placement refusals: all released"); + } +} + /* ---- 8. determinism ------------------------------------------------------- */ static void kat_digest(uint8_t out[32]) { uint64_t save = g_rng; @@ -1460,6 +1758,7 @@ int main(void) { test_unary2_specials(); test_trig_domain(); test_const_neg(); + test_placement(); test_determinism(); /* every test released what it made */ uint32_t lt, ls; diff --git a/tools/tensor_mutations.sh b/tools/tensor_mutations.sh index 3352a10ab..19ad1ab30 100755 --- a/tools/tensor_mutations.sh +++ b/tools/tensor_mutations.sh @@ -32,7 +32,10 @@ TRIG_DOMAIN_NUMBER|omega_tensor_cpu.c|s/out\[i\] = f(a\[i\]);/{ out[i] = f(a[i]) ZEROS_NEG0|omega_tensor.c|s/shape, 0.0f, out)/shape, -0.0f, out)/ NEG_SUB|omega_tensor.c|s/return u ^ 0x80000000U;/return omega_float_to_bits(0.0f - omega_bits_to_float(u));/ NEG_NAN|omega_tensor.c|s/return 0x7fc00000U;/return u ^ 0x80000000U;/ -FULL_CANON|omega_tensor.c|s/uint32_t u = omega_float_to_bits(value);/uint32_t u = canon_bits(OMEGA_DT_F32, (const uint8_t *)\&value);/' +FULL_CANON|omega_tensor.c|s/uint32_t u = omega_float_to_bits(value);/uint32_t u = canon_bits(OMEGA_DT_F32, (const uint8_t *)\&value);/ +EMBED_START|omega_tensor.c|s/start\[d\], ds\[d\]/start[d] + 1, ds[d]/ +EMBED_STEP|omega_tensor.c|s/ds\[d\], st, /ds[d], 1, / +CONCAT_AXIS_OFFSET|omega_tensor.c|s/aoff \* ds\[axis\]/aoff/' # Baseline: the unmutated build must PASS, otherwise every mutant would # look "caught" (e.g. the test refusing an unsuitable FP environment).