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
omega-mv / src / omega_numeric_reduce.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef OMEGA_NUMERIC_REDUCE_H |
| 18 | #define OMEGA_NUMERIC_REDUCE_H |
| 19 | |
| 20 | /* |
| 21 | * E1 WP-D: general FP32 reductions with a frozen summation order |
| 22 | * (docs/numeric/E1_REDUCTION_CONTRACT.md). |
| 23 | * |
| 24 | * The order depends only on n and the element index, never on scheduling, |
| 25 | * thread count or CTA count: |
| 26 | * |
| 27 | * level 0 values are x[0..n). |
| 28 | * One level turns len values into ceil(len/32) values: value j of the next |
| 29 | * level is the 32-lane warp tree (OMEGA_WARP_REDUCTION_DECLARED_ORDER, |
| 30 | * lane i combines with lane i+d for d = 16, 8, 4, 2, 1, lower lane on the |
| 31 | * left) over values [32j, 32j+32) of this level, positions >= len filled |
| 32 | * with the identity of the op. |
| 33 | * At least one level is applied; levels repeat until one value is left. |
| 34 | * The number of levels is the smallest L >= 1 with 32^L >= n. |
| 35 | * |
| 36 | * Every level is a pure tile-of-32 tree, so the order is exactly what the |
| 37 | * chip-proven REDUCE_SUM warp kernel computes per tile, and what a future |
| 38 | * 1024-thread CTA (32 warps: warp tree, then warp 0 tree over the 32 warp |
| 39 | * results via shared memory) would compute for two levels. |
| 40 | * |
| 41 | * Build with -ffp-contract=off. |
| 42 | */ |
| 43 | |
| 44 | #include <stdbool.h> |
| 45 | #include <stddef.h> |
| 46 | #include <stdint.h> |
| 47 | |
| 48 | #include "omega_numeric.h" |
| 49 | |
| 50 | #define OMEGA_REDUCE_DECLARED_ORDER \ |
| 51 | "RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL" |
| 52 | |
| 53 | typedef enum { |
| 54 | OMEGA_RED_SUM = 0, /* IEEE fadd, RNE, subnormals kept; identity -0.0 */ |
| 55 | OMEGA_RED_MAX, /* FMNMX_MAX rule: NaN is missing data, -0 < +0; |
| 56 | identity: quiet NaN */ |
| 57 | OMEGA_RED_MIN, /* FMNMX_MIN rule, identity: quiet NaN */ |
| 58 | OMEGA_RED_MEAN, /* omega_math_div(SUM, (float)n), 1 <= n <= 2^24 */ |
| 59 | OMEGA_RED_COUNT |
| 60 | } OmegaReduceOp; |
| 61 | |
| 62 | /* Empty input (n = 0): SUM -> +0.0, MAX/MIN/MEAN -> quiet NaN 0x7fc00000. */ |
| 63 | #define OMEGA_REDUCE_EMPTY_SUM_BITS 0x00000000U |
| 64 | #define OMEGA_REDUCE_EMPTY_OTHER_BITS 0x7fc00000U |
| 65 | #define OMEGA_REDUCE_SUM_PAD_BITS 0x80000000U /* -0.0: x + -0 == x for all x */ |
| 66 | #define OMEGA_REDUCE_MINMAX_PAD_BITS 0x7fc00000U /* NaN: max(NaN, x) == x */ |
| 67 | #define OMEGA_REDUCE_MEAN_MAX_N (1u << 24) /* n exact in FP32 */ |
| 68 | |
| 69 | const char *omega_reduce_op_name(OmegaReduceOp op); |
| 70 | /* Padding value of one level (MEAN pads like SUM). */ |
| 71 | float omega_reduce_identity(OmegaReduceOp op); |
| 72 | /* Levels applied for n elements: smallest L >= 1 with 32^L >= n (n = 0 -> 0). */ |
| 73 | unsigned omega_reduce_levels(size_t n); |
| 74 | |
| 75 | /* |
| 76 | * Return codes are the OMEGA_NUMERIC_* codes of omega_numeric.h: |
| 77 | * BAD_ARGS (unknown op, NULL buffer with n > 0), OPERANDS (MEAN with |
| 78 | * n > 2^24, row_stride < n with rows > 1), FPENV (host FPCR not RNE/no FTZ). |
| 79 | */ |
| 80 | |
| 81 | /* Semantic reference tier: written straight from the definition above (copy |
| 82 | * each tile into 32 slots, pad, run the lane tree). */ |
| 83 | int omega_reduce_reference(OmegaReduceOp op, const float *x, size_t n, float *out); |
| 84 | |
| 85 | /* CPU realization tier: independent code (bottom-up in place, explicit |
| 86 | * AArch64 FADD / FMAXNM / FMINNM / FDIV / UCVTF). */ |
| 87 | int omega_reduce_cpu(OmegaReduceOp op, const float *x, size_t n, float *out); |
| 88 | |
| 89 | /* Batched: out[r] = reduce(x[r*row_stride .. r*row_stride + n)) for r < rows, |
| 90 | * each row with the same frozen order (reduce along the last axis). */ |
| 91 | int omega_reduce_rows_reference(OmegaReduceOp op, const float *x, size_t rows, size_t n, |
| 92 | size_t row_stride, float *out); |
| 93 | int omega_reduce_rows_cpu(OmegaReduceOp op, const float *x, size_t rows, size_t n, |
| 94 | size_t row_stride, float *out); |
| 95 | |
| 96 | /* NOT the contract. Left-to-right sum, used only by the negative test to |
| 97 | * show that a different order gives different bits and is caught. */ |
| 98 | float omega_reduce_sequential_sum_not_contract(const float *x, size_t n); |
| 99 | |
| 100 | /* |
| 101 | * GB10 realization (src/omega_numeric_reduce_gb10.c), all four ops. Each |
| 102 | * level is padded with the op identity to a multiple of 32 on the host and run |
| 103 | * through a warp kernel in chunks of at most OMEGA_NUMERIC_MAX_COUNT values; |
| 104 | * the host only gathers lane 0 of each warp between launches (data movement, |
| 105 | * no arithmetic). |
| 106 | * SUM the chip-proven REDUCE_SUM patch words, own launch with a semaphore wait. |
| 107 | * MAX, MIN this file's warp patch: five SHFL.DOWN (16..1) + FMNMX pairs. |
| 108 | * MEAN SUM levels on the chip, then omega_math_div(SUM, u2f(n)) as the GB10 |
| 109 | * DIV kernel (E1 row 7), one element. No host step. |
| 110 | * |
| 111 | * omega_reduce_gb10_check runs every pre-submission check (CHECK: markers) |
| 112 | * without opening a device. |
| 113 | */ |
| 114 | #define OMEGA_REDUCE_GB10_MAX_N ((size_t)1 << 26) |
| 115 | int omega_reduce_gb10_check(OmegaReduceOp op, const float *x, size_t n, const float *out, |
| 116 | char *err, size_t err_len); |
| 117 | int omega_reduce_gb10(OmegaReduceOp op, const float *x, size_t n, float *out); |
| 118 | /* Launches the GB10 path made in the last omega_reduce_gb10 call. */ |
| 119 | unsigned omega_reduce_gb10_last_launches(void); |
| 120 | |
| 121 | /* MAX/MIN warp patch (12 instructions written at OMEGA_NUMERIC_PATCH_OFFSET |
| 122 | * of the vecadd kernel). Returns the count or OMEGA_NUMERIC_ERR_NOT_ENCODED. */ |
| 123 | int omega_reduce_gb10_minmax_patch(OmegaReduceOp op, OmegaNumericPatchInsn out[OMEGA_NUMERIC_PATCH_MAX]); |
| 124 | /* Structural check of a MAX/MIN patch against the QMD it runs in: the numeric |
| 125 | * executor's generic rules plus deltas 16,8,4,2,1 in order, FMNMX form and |
| 126 | * min/max predicate, operand order, waits, stall, store, exit. */ |
| 127 | int omega_reduce_gb10_check_minmax_patch(OmegaReduceOp op, const OmegaNumericPatchInsn *p, int n, |
| 128 | const uint32_t *qmd1, char *err, size_t err_len); |
| 129 | /* vecadd kernel with the MAX/MIN patch applied (what the executor submits). */ |
| 130 | int omega_reduce_gb10_build_minmax_kernel(OmegaReduceOp op, uint8_t *code, size_t code_len, size_t *out_len); |
| 131 | |
| 132 | #endif /* OMEGA_NUMERIC_REDUCE_H */ |