src/api/native_tensor_extra_util.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // native_tensor_extra_util.h — argument checks and embed arg helpers shared by | ||
| 4 | // the native_tensor_extra* files, on top of api_internal.h's needElems / | ||
| 5 | // needSameSize / needSameDtype. | ||
| 6 | // | ||
| 7 | // Two things the extra group needs beyond those: | ||
| 8 | // - dims products that cannot overflow (a JS caller can pass any int32, and | ||
| 9 | // five of them multiply past INT64_MAX), and output extents that must fit | ||
| 10 | // the int the ops size tensors with; | ||
| 11 | // - INT32 index operands whose VALUES address memory (scatter rows, RoPE | ||
| 12 | // positions, sequence bounds, segment offsets). The kernels do not | ||
| 13 | // bounds-check those, so the native reads them on the host and checks | ||
| 14 | // them before the launch. The same read lets an FP32 tensor of whole | ||
| 15 | // numbers (what GpuTensor.prototype.upload always produces) stand in for | ||
| 16 | // an INT32 one, converted to an INT32 copy on the operand's device. | ||
| 17 | |||
| 18 | #include "api_internal.h" | ||
| 19 | |||
| 20 | #include <climits> | ||
| 21 | #include <cmath> | ||
| 22 | #include <cstdint> | ||
| 23 | #include <initializer_list> | ||
| 24 | #include <span> | ||
| 25 | #include <string> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace brotensor::api::extra { | ||
| 29 | |||
| 30 | 1988 | inline bronze::Value argAt(std::span<const bronze::Value> a, size_t i) { | |
| 31 | 1988 | return i < a.size() ? a[i] : bronze::embed::undefined(); | |
| 32 | } | ||
| 33 | |||
| 34 | 962 | inline int32_t toInt32Js(double d) { | |
| 35 | 962 | if (!std::isfinite(d) || d == 0.0) return 0; | |
| 36 | 764 | double t = std::trunc(d); | |
| 37 | 764 | double rem = std::fmod(t, 4294967296.0); | |
| 38 | 764 | if (rem < 0.0) rem += 4294967296.0; | |
| 39 | 764 | return static_cast<int32_t>(static_cast<uint32_t>(rem)); | |
| 40 | 962 | } | |
| 41 | |||
| 42 | 836 | inline int32_t intArg(bronze::Value v, int32_t def) { | |
| 43 | 836 | if (bronze::embed::isUndefined(v) || bronze::embed::isNull(v)) return def; | |
| 44 | 836 | return toInt32Js(bronze::embed::toDouble(v)); | |
| 45 | 836 | } | |
| 46 | |||
| 47 | 124 | inline double numArg(bronze::Value v, double def) { | |
| 48 | 124 | if (bronze::embed::isUndefined(v) || bronze::embed::isNull(v)) return def; | |
| 49 | 124 | return bronze::embed::toDouble(v); | |
| 50 | 124 | } | |
| 51 | |||
| 52 | 38 | inline bool boolArg(bronze::Value v, bool def) { | |
| 53 | 38 | if (bronze::embed::isUndefined(v) || bronze::embed::isNull(v)) return def; | |
| 54 | 38 | return bronze::embed::toBool(v); | |
| 55 | 38 | } | |
| 56 | |||
| 57 | // Product of dims in 64 bits: -1 when any dim is negative, INT64_MAX when the | ||
| 58 | // product would overflow (needElems then reports it as too large). | ||
| 59 | 338 | inline int64_t prod(std::initializer_list<int64_t> dims) { | |
| 60 | 338 | int64_t n = 1; | |
| 61 | 1346 | for (int64_t d : dims) if (d < 0) return -1; | |
| 62 | 1320 | for (int64_t d : dims) { | |
| 63 | 996 | if (d == 0) return 0; | |
| 64 | 982 | if (n > INT64_MAX / d) return INT64_MAX; | |
| 65 | 982 | n *= d; | |
| 66 | } | ||
| 67 | 324 | return n; | |
| 68 | 338 | } | |
| 69 | |||
| 70 | // An output extent (or element count) the op will allocate: positive and | ||
| 71 | // within the int the ops size tensors with. | ||
| 72 | 150 | inline bool needExtent(const char* label, const char* what, int64_t n) { | |
| 73 | 150 | if (n <= 0) { | |
| 74 | ✗ | setError(std::string(label) + ": " + what + " is " + std::to_string(n) + ", must be positive"); | |
| 75 | } | ||
| 76 | 150 | if (n > INT_MAX) { | |
| 77 | ✗ | setError(std::string(label) + ": " + what + " is too large"); | |
| 78 | } | ||
| 79 | 150 | return true; | |
| 80 | ✗ | } | |
| 81 | |||
| 82 | // A scalar argument in [lo, hi]. | ||
| 83 | 36 | inline bool needRange(const char* label, const char* what, int64_t v, int64_t lo, int64_t hi) { | |
| 84 | 36 | if (v < lo || v > hi) { | |
| 85 | ✗ | setError(std::string(label) + ": " + what + " is " + std::to_string(v) + ", must be in [" + | |
| 86 | ✗ | std::to_string(lo) + ", " + std::to_string(hi) + "]"); | |
| 87 | } | ||
| 88 | 36 | return true; | |
| 89 | ✗ | } | |
| 90 | |||
| 91 | 184 | inline bool needDtype(const char* label, const char* name, const brotensor::Tensor* t, brotensor::Dtype dt) { | |
| 92 | 184 | if (!t) { | |
| 93 | 2 | bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor"); | |
| 94 | } | ||
| 95 | 184 | if (t->dtype != dt) { | |
| 96 | 2 | setError(std::string(label) + ": " + name + " is " + dtypeToString(t->dtype) + ", expected " + | |
| 97 | 2 | dtypeToString(dt)); | |
| 98 | } | ||
| 99 | 182 | return true; | |
| 100 | 2 | } | |
| 101 | |||
| 102 | // FP32 / FP16 / BF16 — the float storage the dtype-dispatched kernels read. | ||
| 103 | 134 | inline bool needFloat(const char* label, const char* name, const brotensor::Tensor* t) { | |
| 104 | 134 | if (!t) { | |
| 105 | 2 | bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor"); | |
| 106 | } | ||
| 107 | 134 | if (t->dtype != brotensor::Dtype::FP32 && t->dtype != brotensor::Dtype::FP16 && | |
| 108 | 2 | t->dtype != brotensor::Dtype::BF16) { | |
| 109 | 2 | setError(std::string(label) + ": " + name + " is " + dtypeToString(t->dtype) + | |
| 110 | ", expected fp32, fp16 or bf16"); | ||
| 111 | } | ||
| 112 | 132 | return true; | |
| 113 | 2 | } | |
| 114 | |||
| 115 | // An optional operand: null passes; otherwise it must hold `n` elements of | ||
| 116 | // `ref`'s dtype (ref null = any dtype). | ||
| 117 | 38 | inline bool optElems(const char* label, const char* name, const brotensor::Tensor* t, int64_t n, | |
| 118 | const brotensor::Tensor* ref) { | ||
| 119 | 38 | if (!t) return true; | |
| 120 | 22 | if (!needElems(label, name, t, n)) return false; | |
| 121 | 22 | return !ref || needSameDtype(label, name, t, ref); | |
| 122 | 38 | } | |
| 123 | |||
| 124 | // An optional FP32 operand (the `const float* d_mask` slots): null passes. | ||
| 125 | 44 | inline bool optF32Elems(const char* label, const char* name, const brotensor::Tensor* t, int64_t n) { | |
| 126 | 44 | if (!t) return true; | |
| 127 | 18 | return needDtype(label, name, t, brotensor::Dtype::FP32) && needElems(label, name, t, n); | |
| 128 | 44 | } | |
| 129 | |||
| 130 | // The values of an index operand on the host: INT32 read as-is, FP32 whole | ||
| 131 | // numbers converted. Anything else (or fewer than `n` elements) is an error. | ||
| 132 | // Must be called OUTSIDE BROTENSOR_API_TRY. | ||
| 133 | bool hostInt32(const char* label, const char* name, const brotensor::Tensor* t, int64_t n, | ||
| 134 | std::vector<int32_t>& out); | ||
| 135 | |||
| 136 | // `t` itself when it is already INT32, else an INT32 copy of `vals` shaped | ||
| 137 | // like `t` on `t`'s device, held in `scratch`. | ||
| 138 | const brotensor::Tensor& int32Operand(const brotensor::Tensor& t, const std::vector<int32_t>& vals, | ||
| 139 | brotensor::Tensor& scratch); | ||
| 140 | |||
| 141 | // Every one of the first `n` values lies in [lo, hi). | ||
| 142 | bool needIndicesIn(const char* label, const char* name, const std::vector<int32_t>& vals, int64_t n, | ||
| 143 | int64_t lo, int64_t hi); | ||
| 144 | |||
| 145 | } // namespace brotensor::api::extra | ||
| 146 |