brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
62 / 68
91.2%
Branches:
52 / 150
34.7%
Functions:
12 / 12
100.0%

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
1/2
✓ Branch 0 taken 1988 times.
✗ Branch 1 not taken.
1988 return i < a.size() ? a[i] : bronze::embed::undefined();
32 }
33
34 962 inline int32_t toInt32Js(double d) {
35
3/4
✓ Branch 0 taken 962 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 764 times.
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
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 2 times.
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
2/4
✓ Branch 0 taken 836 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 836 times.
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
2/4
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 124 times.
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
2/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
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
3/4
✓ Branch 0 taken 1008 times.
✓ Branch 1 taken 338 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1008 times.
1346 for (int64_t d : dims) if (d < 0) return -1;
62
2/2
✓ Branch 0 taken 996 times.
✓ Branch 1 taken 324 times.
1320 for (int64_t d : dims) {
63
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 982 times.
996 if (d == 0) return 0;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 982 times.
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
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (n <= 0) {
74
0/14
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ setError(std::string(label) + ": " + what + " is " + std::to_string(n) + ", must be positive");
75 }
76
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (n > INT_MAX) {
77
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ 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
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (v < lo || v > hi) {
85
0/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ setError(std::string(label) + ": " + what + " is " + std::to_string(v) + ", must be in [" +
86
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ 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
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (!t) {
93
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2 bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor");
94 }
95
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 2 times.
184 if (t->dtype != dt) {
96
7/14
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
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
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 if (!t) {
105
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2 bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor");
106 }
107
5/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
134 if (t->dtype != brotensor::Dtype::FP32 && t->dtype != brotensor::Dtype::FP16 &&
108 2 t->dtype != brotensor::Dtype::BF16) {
109
6/14
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
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
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 16 times.
38 if (!t) return true;
120
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!needElems(label, name, t, n)) return false;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
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
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 26 times.
44 if (!t) return true;
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
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