omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / examples / deepinfer / flops_estimator-omega.patch
| 1 | --- a/csrc/engine/flops_estimator.hpp |
| 2 | +++ b/csrc/engine/flops_estimator.hpp |
| 3 | @@ -10,6 +10,10 @@ |
| 4 | #include <utility> |
| 5 | #include <vector> |
| 6 | |
| 7 | +// floor_prefix, capped_prefix and pairs run the Omega unit flops_pairs.om (aien-dev/omega-bridge |
| 8 | +// examples/deepinfer), bound by omega-bridge tools/native into libomega_native. |
| 9 | +#include "omega_native.h" |
| 10 | + |
| 11 | // GEMM coefficients include multiply-add (2 FLOPs) and the FP8-equivalent weight. |
| 12 | namespace flops_coef { |
| 13 | constexpr int64_t FP4 = 1; |
| 14 | @@ -237,23 +241,34 @@ |
| 15 | // Sum floor(t / ratio), t=1..n: compressed query-key pairs without a cap. |
| 16 | // Used for dense core attention and full-context index attention. |
| 17 | static int64_t floor_prefix(int64_t n, int64_t ratio) { |
| 18 | - const int64_t blocks = n / ratio; |
| 19 | - return blocks * (n + 1) - ratio * blocks * (blocks + 1) / 2; |
| 20 | + uint64_t bits = 0; |
| 21 | + const uint32_t status = omega2_floor_prefix(&bits, static_cast<uint64_t>(n), static_cast<uint64_t>(ratio)); |
| 22 | + return omega_result(status, bits, "floor_prefix"); |
| 23 | } |
| 24 | |
| 25 | // Sum min(floor(t / ratio), cap), t=1..n. |
| 26 | // Used for topk/candidate limits and SWA windows (ratio=1). |
| 27 | static int64_t capped_prefix(int64_t n, int64_t ratio, int64_t cap) { |
| 28 | - const int64_t stop = std::min(n, ratio * cap); |
| 29 | - return floor_prefix(stop, ratio) + (n - stop) * cap; |
| 30 | + uint64_t bits = 0; |
| 31 | + const uint32_t status = |
| 32 | + omega2_capped_prefix(&bits, static_cast<uint64_t>(n), static_cast<uint64_t>(ratio), static_cast<uint64_t>(cap)); |
| 33 | + return omega_result(status, bits, "capped_prefix"); |
| 34 | } |
| 35 | |
| 36 | // Query-key pairs for positions [begin, end), via prefix differences. |
| 37 | // Used for full/consumer query ranges; cap=0 means uncapped. |
| 38 | static int64_t pairs(int64_t begin, int64_t end, int64_t ratio, int64_t cap = 0) { |
| 39 | - if (cap == 0) |
| 40 | - return floor_prefix(end, ratio) - floor_prefix(begin, ratio); |
| 41 | - return capped_prefix(end, ratio, cap) - capped_prefix(begin, ratio, cap); |
| 42 | + uint64_t bits = 0; |
| 43 | + const uint32_t status = omega2_pairs(&bits, static_cast<uint64_t>(begin), static_cast<uint64_t>(end), |
| 44 | + static_cast<uint64_t>(ratio), static_cast<uint64_t>(cap)); |
| 45 | + return omega_result(status, bits, "pairs"); |
| 46 | + } |
| 47 | + |
| 48 | + // An Omega status is 0 when done; 1 when a step leaves int64_t (where the former C++ expression was |
| 49 | + // undefined behaviour); 9 outside n >= 0, ratio > 0, cap >= 0, 0 <= begin <= end. |
| 50 | + static int64_t omega_result(uint32_t status, uint64_t bits, const char *fn) { |
| 51 | + TORCH_CHECK(status == OMEGA_OK, fn, ": Omega trap ", status); |
| 52 | + return static_cast<int64_t>(bits); |
| 53 | } |
| 54 | |
| 55 | std::pair<QueryRange, QueryRange> query_ranges(int q, int k, bool is_prompt, int full_len, int swa_valid_start) const { |