src/api/api_internal.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "brotensor/tensor.h" | ||
| 4 | #include "brotensor/ops.h" | ||
| 5 | #include "brotensor/runtime.h" | ||
| 6 | #include "brotensor/safetensors.h" | ||
| 7 | #include "embed/embed.h" | ||
| 8 | #include "embed/embed_access.h" | ||
| 9 | #include "embed/embed_bind.h" | ||
| 10 | |||
| 11 | #include <climits> | ||
| 12 | #include <cmath> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <cstring> | ||
| 16 | #include <exception> | ||
| 17 | #include <initializer_list> | ||
| 18 | #include <memory> | ||
| 19 | #include <span> | ||
| 20 | #include <string> | ||
| 21 | #include <vector> | ||
| 22 | |||
| 23 | namespace brotensor::api { | ||
| 24 | |||
| 25 | inline bronze::embed::HostClass g_gpuTensorClass; | ||
| 26 | inline bronze::embed::HostClass g_safetensorsClass; | ||
| 27 | |||
| 28 | struct GpuTensorHandle { | ||
| 29 | brotensor::Tensor tensor; | ||
| 30 | std::vector<int64_t> shape; | ||
| 31 | |||
| 32 | 3500 | GpuTensorHandle() = default; | |
| 33 | 4 | GpuTensorHandle(brotensor::Tensor&& t, std::vector<int64_t> s = {}) | |
| 34 | 4 | : tensor(std::move(t)), shape(std::move(s)) {} | |
| 35 | GpuTensorHandle(const GpuTensorHandle&) = delete; | ||
| 36 | GpuTensorHandle& operator=(const GpuTensorHandle&) = delete; | ||
| 37 | }; | ||
| 38 | |||
| 39 | struct SafetensorsHandle { | ||
| 40 | std::unique_ptr<brotensor::safetensors::File> file; | ||
| 41 | }; | ||
| 42 | |||
| 43 | } // namespace brotensor::api | ||
| 44 | |||
| 45 | 996 | BRONZE_DECLARE_CLASS_TRAITS(brotensor::api::GpuTensorHandle, | |
| 46 | brotensor::api::g_gpuTensorClass, | ||
| 47 | "GpuTensor"); | ||
| 48 | 2 | BRONZE_DECLARE_CLASS_TRAITS(brotensor::api::SafetensorsHandle, | |
| 49 | brotensor::api::g_safetensorsClass, | ||
| 50 | "SafetensorsFile"); | ||
| 51 | |||
| 52 | namespace brotensor::api { | ||
| 53 | |||
| 54 | 14092 | inline bronze::Value arg(std::span<const bronze::Value> args, size_t idx) { | |
| 55 | 14092 | return idx < args.size() ? args[idx] : bronze::embed::undefined(); | |
| 56 | } | ||
| 57 | |||
| 58 | // ---- handle unwrapping ----------------------------------------------------- | ||
| 59 | |||
| 60 | 5052 | inline GpuTensorHandle* toHandle(bronze::Value v) { | |
| 61 | 5052 | return static_cast<GpuTensorHandle*>(g_gpuTensorClass.unwrap(v)); | |
| 62 | } | ||
| 63 | |||
| 64 | 4 | inline brotensor::Tensor* toTensor(bronze::Value v) { | |
| 65 | 4 | auto* h = toHandle(v); | |
| 66 | 4 | return h ? &h->tensor : nullptr; | |
| 67 | } | ||
| 68 | |||
| 69 | 2492 | inline GpuTensorHandle* requireHandle(bronze::Value v, const char* label, | |
| 70 | const char* argName = "tensor") { | ||
| 71 | 2492 | auto* h = toHandle(v); | |
| 72 | 2492 | if (!h) { | |
| 73 | 50 | bronze::embed::throwTypeError(std::string(label) + ": " + argName + | |
| 74 | " must be a GpuTensor"); | ||
| 75 | } | ||
| 76 | 2442 | return h; | |
| 77 | 50 | } | |
| 78 | |||
| 79 | 2492 | inline brotensor::Tensor* requireTensor(bronze::Value v, const char* label, | |
| 80 | const char* argName = "tensor") { | ||
| 81 | 2492 | return &requireHandle(v, label, argName)->tensor; | |
| 82 | } | ||
| 83 | |||
| 84 | // Nullable tensor slots (mask|null, bias|null, ...): null/undefined -> nullptr; | ||
| 85 | // any non-nullish non-GpuTensor throws TypeError. | ||
| 86 | 446 | inline brotensor::Tensor* optionalTensor(bronze::Value v, const char* label, | |
| 87 | const char* argName = "tensor") { | ||
| 88 | 446 | if (bronze::embed::isNull(v) || bronze::embed::isUndefined(v)) return nullptr; | |
| 89 | 132 | return requireTensor(v, label, argName); | |
| 90 | 446 | } | |
| 91 | |||
| 92 | 74 | inline const float* maskPtr(const brotensor::Tensor* t) { | |
| 93 | 74 | return t ? static_cast<const float*>(t->data) : nullptr; | |
| 94 | } | ||
| 95 | |||
| 96 | inline const float* maskPtrVal(bronze::Value v, const char* label = "op", | ||
| 97 | const char* argName = "mask") { | ||
| 98 | return maskPtr(optionalTensor(v, label, argName)); | ||
| 99 | } | ||
| 100 | |||
| 101 | // ---- error helpers --------------------------------------------------------- | ||
| 102 | |||
| 103 | 232 | [[noreturn]] inline void setError(const std::string& msg) { | |
| 104 | 232 | bronze::embed::throwError(msg); | |
| 105 | } | ||
| 106 | |||
| 107 | // IMPORTANT: Run argument validation BEFORE BROTENSOR_API_TRY so embed throws | ||
| 108 | // are never caught and re-wrapped by `catch (const std::exception&)`, and never | ||
| 109 | // call `throwError` inside a C++ `catch` block (MSVC ABI rule). | ||
| 110 | #define BROTENSOR_API_TRY \ | ||
| 111 | { \ | ||
| 112 | std::string _bro_api_err_; \ | ||
| 113 | try { | ||
| 114 | #define BROTENSOR_API_CATCH(label) \ | ||
| 115 | } catch (const std::exception& _e_) { \ | ||
| 116 | _bro_api_err_ = std::string(label) + ": " + _e_.what(); \ | ||
| 117 | } \ | ||
| 118 | if (!_bro_api_err_.empty()) { \ | ||
| 119 | ::bronze::embed::throwError(_bro_api_err_); \ | ||
| 120 | } \ | ||
| 121 | } | ||
| 122 | |||
| 123 | // ---- dtype / scalar parsing ------------------------------------------------ | ||
| 124 | |||
| 125 | inline brotensor::Dtype parseDtype(const char* dt) { | ||
| 126 | if (!dt) return brotensor::Dtype::FP32; | ||
| 127 | if (std::strcmp(dt, "fp32") == 0 || std::strcmp(dt, "f32") == 0) return brotensor::Dtype::FP32; | ||
| 128 | if (std::strcmp(dt, "fp16") == 0 || std::strcmp(dt, "f16") == 0) return brotensor::Dtype::FP16; | ||
| 129 | if (std::strcmp(dt, "bf16") == 0) return brotensor::Dtype::BF16; | ||
| 130 | if (std::strcmp(dt, "int8") == 0 || std::strcmp(dt, "i8") == 0) return brotensor::Dtype::INT8; | ||
| 131 | if (std::strcmp(dt, "int32") == 0 || std::strcmp(dt, "i32") == 0) return brotensor::Dtype::INT32; | ||
| 132 | if (std::strcmp(dt, "f64") == 0 || std::strcmp(dt, "fp64") == 0) return brotensor::Dtype::F64; | ||
| 133 | if (std::strcmp(dt, "fp8_e4m3") == 0 || std::strcmp(dt, "fp8") == 0) return brotensor::Dtype::FP8_E4M3; | ||
| 134 | return brotensor::Dtype::FP32; | ||
| 135 | } | ||
| 136 | |||
| 137 | 740 | inline brotensor::Dtype parseDtypeValue(bronze::Value v, | |
| 138 | brotensor::Dtype def = brotensor::Dtype::FP32, | ||
| 139 | const char* label = "tensor") { | ||
| 140 | 740 | if (bronze::embed::isUndefined(v) || bronze::embed::isNull(v)) return def; | |
| 141 | 248 | if (bronze::embed::isNumber(v)) { | |
| 142 | 4 | double d = bronze::embed::toDouble(v); | |
| 143 | 4 | if (!std::isfinite(d)) { | |
| 144 | ✗ | bronze::embed::throwRangeError(std::string(label) + ": invalid dtype"); | |
| 145 | } | ||
| 146 | 4 | int code = static_cast<int>(d); | |
| 147 | 4 | if (static_cast<double>(code) != d) { | |
| 148 | ✗ | bronze::embed::throwRangeError(std::string(label) + ": invalid dtype"); | |
| 149 | } | ||
| 150 | 4 | switch (static_cast<brotensor::Dtype>(code)) { | |
| 151 | case brotensor::Dtype::FP32: | ||
| 152 | case brotensor::Dtype::FP16: | ||
| 153 | case brotensor::Dtype::INT8: | ||
| 154 | case brotensor::Dtype::INT32: | ||
| 155 | case brotensor::Dtype::BF16: | ||
| 156 | case brotensor::Dtype::F64: | ||
| 157 | case brotensor::Dtype::FP8_E4M3: | ||
| 158 | 4 | return static_cast<brotensor::Dtype>(code); | |
| 159 | default: | ||
| 160 | ✗ | bronze::embed::throwRangeError(std::string(label) + ": unsupported dtype code " + | |
| 161 | ✗ | std::to_string(code)); | |
| 162 | } | ||
| 163 | } | ||
| 164 | 244 | if (bronze::embed::isString(v)) { | |
| 165 | 244 | std::string s = bronze::embed::toUtf8(v); | |
| 166 | 244 | if (s == "fp32" || s == "f32") return brotensor::Dtype::FP32; | |
| 167 | 240 | if (s == "fp16" || s == "f16") return brotensor::Dtype::FP16; | |
| 168 | 72 | if (s == "bf16") return brotensor::Dtype::BF16; | |
| 169 | 72 | if (s == "int8" || s == "i8") return brotensor::Dtype::INT8; | |
| 170 | 54 | if (s == "int32" || s == "i32") return brotensor::Dtype::INT32; | |
| 171 | ✗ | if (s == "f64" || s == "fp64") return brotensor::Dtype::F64; | |
| 172 | ✗ | if (s == "fp8_e4m3" || s == "fp8") return brotensor::Dtype::FP8_E4M3; | |
| 173 | ✗ | bronze::embed::throwTypeError(std::string(label) + ": unknown dtype '" + s + "'"); | |
| 174 | 244 | } | |
| 175 | ✗ | bronze::embed::throwTypeError(std::string(label) + ": dtype must be a string or dtype enum"); | |
| 176 | 740 | } | |
| 177 | |||
| 178 | 54 | inline const char* dtypeToString(brotensor::Dtype dt) { | |
| 179 | 54 | switch (dt) { | |
| 180 | 14 | case brotensor::Dtype::FP32: return "fp32"; | |
| 181 | 18 | case brotensor::Dtype::FP16: return "fp16"; | |
| 182 | ✗ | case brotensor::Dtype::BF16: return "bf16"; | |
| 183 | 2 | case brotensor::Dtype::INT8: return "int8"; | |
| 184 | 20 | case brotensor::Dtype::INT32: return "int32"; | |
| 185 | ✗ | case brotensor::Dtype::F64: return "f64"; | |
| 186 | ✗ | case brotensor::Dtype::FP8_E4M3: return "fp8_e4m3"; | |
| 187 | ✗ | default: return "unknown"; | |
| 188 | } | ||
| 189 | 54 | } | |
| 190 | |||
| 191 | 54 | inline uint64_t readSeedValue(bronze::Value v, uint64_t def, const char* label = "rand") { | |
| 192 | 54 | if (bronze::embed::isUndefined(v) || bronze::embed::isNull(v)) return def; | |
| 193 | 54 | if (bronze::embed::isBigInt(v)) return bronze::embed::toUint64(v); | |
| 194 | 50 | if (bronze::embed::isNumber(v)) { | |
| 195 | 48 | double d = bronze::embed::toDouble(v); | |
| 196 | 48 | if (!std::isfinite(d)) { | |
| 197 | 2 | bronze::embed::throwRangeError(std::string(label) + ": seed must be finite"); | |
| 198 | } | ||
| 199 | 48 | return d >= 0.0 ? static_cast<uint64_t>(d) | |
| 200 | ✗ | : static_cast<uint64_t>(static_cast<int64_t>(d)); | |
| 201 | } | ||
| 202 | 2 | bronze::embed::throwTypeError(std::string(label) + ": seed must be a number or BigInt"); | |
| 203 | 54 | } | |
| 204 | |||
| 205 | 18 | inline void readTensorArrayVal(bronze::Value v, std::vector<brotensor::Tensor*>& out, | |
| 206 | const char* label = "op") { | ||
| 207 | 18 | out.clear(); | |
| 208 | 18 | if (!bronze::embed::isObject(v)) { | |
| 209 | ✗ | bronze::embed::throwTypeError(std::string(label) + ": expected array of GpuTensor"); | |
| 210 | } | ||
| 211 | 18 | bronze::Value lenVal = bronze::embed::getProperty(v, "length"); | |
| 212 | 18 | if (!bronze::embed::isNumber(lenVal)) { | |
| 213 | ✗ | bronze::embed::throwTypeError(std::string(label) + ": expected array of GpuTensor"); | |
| 214 | } | ||
| 215 | 18 | double lenD = bronze::embed::toDouble(lenVal); | |
| 216 | 18 | if (!std::isfinite(lenD) || lenD < 0.0 || lenD > 4294967295.0) { | |
| 217 | ✗ | bronze::embed::throwRangeError(std::string(label) + ": invalid array length"); | |
| 218 | } | ||
| 219 | 18 | uint32_t n = static_cast<uint32_t>(lenD); | |
| 220 | 18 | out.reserve(n); | |
| 221 | 52 | for (uint32_t i = 0; i < n; ++i) { | |
| 222 | 34 | out.push_back(requireTensor(bronze::embed::getElement(v, i), label, "array element")); | |
| 223 | 34 | } | |
| 224 | 18 | } | |
| 225 | |||
| 226 | // ---- input-size & contract validation -------------------------------------- | ||
| 227 | |||
| 228 | inline bool need(const char* label, std::initializer_list<const void*> ptrs) { | ||
| 229 | for (const void* p : ptrs) { | ||
| 230 | if (!p) { | ||
| 231 | bronze::embed::throwTypeError(std::string(label) + ": GpuTensor argument missing"); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | return true; | ||
| 235 | } | ||
| 236 | |||
| 237 | 902 | inline bool needElems(const char* label, const char* name, | |
| 238 | const brotensor::Tensor* t, int64_t n) { | ||
| 239 | 902 | if (!t) { | |
| 240 | 104 | bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor"); | |
| 241 | } | ||
| 242 | 902 | if (n < 0) { | |
| 243 | ✗ | setError(std::string(label) + ": negative dimension for " + name); | |
| 244 | } | ||
| 245 | 902 | if (static_cast<int64_t>(t->size()) < n || (n > 0 && t->data == nullptr)) { | |
| 246 | 208 | setError(std::string(label) + ": " + name + " holds " + std::to_string(t->size()) + | |
| 247 | 104 | " elements, the dims need " + std::to_string(n)); | |
| 248 | } | ||
| 249 | 798 | return true; | |
| 250 | 104 | } | |
| 251 | |||
| 252 | 110 | inline bool needSameSize(const char* label, const char* name, | |
| 253 | const brotensor::Tensor* t, const brotensor::Tensor* ref) { | ||
| 254 | 110 | if (!t || !ref) { | |
| 255 | 34 | bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor"); | |
| 256 | } | ||
| 257 | 110 | if (t->size() != ref->size()) { | |
| 258 | 68 | setError(std::string(label) + ": " + name + " has " + std::to_string(t->size()) + | |
| 259 | 34 | " elements, expected " + std::to_string(ref->size())); | |
| 260 | } | ||
| 261 | 76 | return true; | |
| 262 | 34 | } | |
| 263 | |||
| 264 | 738 | inline bool needSameDtype(const char* label, const char* name, | |
| 265 | const brotensor::Tensor* t, const brotensor::Tensor* ref) { | ||
| 266 | 738 | if (!t || !ref) { | |
| 267 | 6 | bronze::embed::throwTypeError(std::string(label) + ": " + name + " must be a GpuTensor"); | |
| 268 | } | ||
| 269 | 738 | if (t->dtype != ref->dtype) { | |
| 270 | 12 | setError(std::string(label) + ": " + name + " is " + dtypeToString(t->dtype) + | |
| 271 | 6 | ", expected " + dtypeToString(ref->dtype)); | |
| 272 | } | ||
| 273 | 732 | return true; | |
| 274 | 6 | } | |
| 275 | |||
| 276 | 278 | inline int64_t elems(std::initializer_list<int64_t> dims) { | |
| 277 | 278 | int64_t n = 1; | |
| 278 | 1130 | for (int64_t d : dims) { | |
| 279 | 852 | if (d < 0) return -1; | |
| 280 | 852 | n *= d; | |
| 281 | } | ||
| 282 | 278 | return n; | |
| 283 | 278 | } | |
| 284 | |||
| 285 | 74 | inline bool needPair(const char* label, const char* name, | |
| 286 | const brotensor::Tensor* t, const brotensor::Tensor* ref) { | ||
| 287 | 74 | return needSameSize(label, name, t, ref) && needSameDtype(label, name, t, ref); | |
| 288 | } | ||
| 289 | |||
| 290 | 136 | inline bool needPositive(const char* label, std::initializer_list<int64_t> vals) { | |
| 291 | 578 | for (int64_t v : vals) { | |
| 292 | 446 | if (v <= 0) { | |
| 293 | 4 | setError(std::string(label) + ": kernel/stride/dilation/group/head counts must be positive"); | |
| 294 | } | ||
| 295 | } | ||
| 296 | 132 | return true; | |
| 297 | 4 | } | |
| 298 | |||
| 299 | 158 | inline bool needNonNegative(const char* label, std::initializer_list<int64_t> vals) { | |
| 300 | 730 | for (int64_t v : vals) { | |
| 301 | 574 | if (v < 0) { | |
| 302 | 2 | setError(std::string(label) + ": dims, paddings and offsets must be non-negative"); | |
| 303 | } | ||
| 304 | } | ||
| 305 | 156 | return true; | |
| 306 | 2 | } | |
| 307 | |||
| 308 | 32 | inline bool convGeom2d(const char* label, int64_t C_in, int64_t C_out, int64_t H, int64_t W, | |
| 309 | int64_t kH, int64_t kW, int64_t sH, int64_t sW, | ||
| 310 | int64_t pH, int64_t pW, int64_t dH, int64_t dW, int64_t groups, | ||
| 311 | int64_t& H_out, int64_t& W_out) { | ||
| 312 | 32 | if (!needPositive(label, {kH, kW, sH, sW, dH, dW, groups})) return false; | |
| 313 | 32 | if (!needNonNegative(label, {C_in, C_out, H, W, pH, pW})) return false; | |
| 314 | 32 | if (C_in % groups != 0 || C_out % groups != 0) { | |
| 315 | 2 | setError(std::string(label) + ": groups must divide C_in and C_out"); | |
| 316 | } | ||
| 317 | 30 | const int64_t spanH = H + 2 * pH - dH * (kH - 1) - 1; | |
| 318 | 30 | const int64_t spanW = W + 2 * pW - dW * (kW - 1) - 1; | |
| 319 | 30 | H_out = spanH < 0 ? 0 : spanH / sH + 1; | |
| 320 | 30 | W_out = spanW < 0 ? 0 : spanW / sW + 1; | |
| 321 | 30 | return true; | |
| 322 | 32 | } | |
| 323 | |||
| 324 | struct Operand { | ||
| 325 | const char* name; | ||
| 326 | const brotensor::Tensor* t; | ||
| 327 | int64_t n; | ||
| 328 | }; | ||
| 329 | |||
| 330 | 116 | inline bool needOperands(const char* label, const brotensor::Tensor* ref, | |
| 331 | std::initializer_list<Operand> ops) { | ||
| 332 | 636 | for (const Operand& o : ops) { | |
| 333 | 520 | if (!o.t) continue; | |
| 334 | 436 | if (!needElems(label, o.name, o.t, o.n)) return false; | |
| 335 | 436 | if (ref && !needSameDtype(label, o.name, o.t, ref)) return false; | |
| 336 | } | ||
| 337 | 116 | return true; | |
| 338 | 116 | } | |
| 339 | |||
| 340 | 84 | inline bool needMask(const char* label, const brotensor::Tensor* mt, int64_t n, | |
| 341 | const char* name = "mask") { | ||
| 342 | 84 | if (!mt) return true; | |
| 343 | 28 | if (!needElems(label, name, mt, n)) return false; | |
| 344 | 28 | if (mt->dtype != brotensor::Dtype::FP32) { | |
| 345 | ✗ | setError(std::string(label) + ": " + name + " must be FP32"); | |
| 346 | } | ||
| 347 | 28 | return true; | |
| 348 | 84 | } | |
| 349 | |||
| 350 | 40 | inline bool needHeads(const char* label, int64_t D, int64_t numHeads) { | |
| 351 | 40 | if (!needPositive(label, {numHeads})) return false; | |
| 352 | 40 | if (D % numHeads != 0) { | |
| 353 | 4 | setError(std::string(label) + ": numHeads (" + std::to_string(numHeads) + | |
| 354 | 2 | ") must divide D (" + std::to_string(D) + ")"); | |
| 355 | } | ||
| 356 | 38 | return true; | |
| 357 | 40 | } | |
| 358 | |||
| 359 | // ---- file path resolution & access gate ------------------------------------ | ||
| 360 | |||
| 361 | std::string resolvePath(const std::string& path); | ||
| 362 | |||
| 363 | // Resolves `path` into `resolved` and checks the host's access gate; throws | ||
| 364 | // an EACCES Error via bronze::embed::throwAccessError when refused. | ||
| 365 | std::string gatedPath(const char* path, bool write); | ||
| 366 | |||
| 367 | // ---- modular installers ---------------------------------------------------- | ||
| 368 | |||
| 369 | void installTensorCore(bronze::Value ns); | ||
| 370 | void installTensorOps(bronze::Value ns); | ||
| 371 | void installTensorNn(bronze::Value ns); | ||
| 372 | void installTensorAttention(bronze::Value ns); | ||
| 373 | void installTensorSafetensors(bronze::Value ns); | ||
| 374 | void installTensorBatched(bronze::Value ns); | ||
| 375 | void installTensorAttn2(bronze::Value ns); | ||
| 376 | void installTensorAudio(bronze::Value ns); | ||
| 377 | void installTensorAudioB(bronze::Value ns); | ||
| 378 | void installTensorConv(bronze::Value ns); | ||
| 379 | void installTensorInt8(bronze::Value ns); | ||
| 380 | void installTensorMisc(bronze::Value ns); | ||
| 381 | void installTensorExtra(bronze::Value ns); | ||
| 382 | void installTensorExtraNn(bronze::Value ns); | ||
| 383 | void installTensorExtraConv(bronze::Value ns); | ||
| 384 | |||
| 385 | } // namespace brotensor::api | ||
| 386 |