src/tensor.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // brotensor::Tensor implementation. | ||
| 2 | // | ||
| 3 | // All allocation, freeing, and inter-device memory traffic routes through the | ||
| 4 | // backend AllocVTable obtained from detail::alloc_for(device). The Tensor type | ||
| 5 | // itself is backend-agnostic; CUDA/Metal/CPU specifics live entirely behind | ||
| 6 | // the vtable indirection. | ||
| 7 | |||
| 8 | #include <brotensor/tensor.h> | ||
| 9 | #include <brotensor/runtime.h> | ||
| 10 | #include <brotensor/detail/dispatch.h> | ||
| 11 | |||
| 12 | #include <atomic> | ||
| 13 | #include <cmath> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <limits> | ||
| 16 | #include <cstring> | ||
| 17 | #include <memory> | ||
| 18 | #include <stdexcept> | ||
| 19 | #include <string> | ||
| 20 | #include <vector> | ||
| 21 | |||
| 22 | #include <chrono> | ||
| 23 | #include <cstdio> | ||
| 24 | #include <cstdlib> | ||
| 25 | |||
| 26 | namespace brotensor { | ||
| 27 | |||
| 28 | // ─── env-gated weight-load profiler (BROTENSOR_TIME_LOAD=1) ────────────────── | ||
| 29 | // Attributes GPU weight-upload wall time to allocation vs H2D copy, accumulated | ||
| 30 | // across every from_host_*_on call and dumped once at process exit. Purely a | ||
| 31 | // diagnostic; the fast path (flag unset) is a single getenv-cached bool check. | ||
| 32 | namespace { | ||
| 33 | struct LoadProf { | ||
| 34 | 190 | bool on = false; | |
| 35 | 190 | double alloc = 0.0, copy = 0.0, bytes = 0.0; | |
| 36 | 190 | long calls = 0; | |
| 37 | 570 | LoadProf() { on = std::getenv("BROTENSOR_TIME_LOAD") != nullptr; } | |
| 38 | ✗ | ~LoadProf() { | |
| 39 | ✗ | if (!on || calls == 0) return; | |
| 40 | ✗ | std::fprintf(stderr, | |
| 41 | "[brotensor load] %ld uploads, %.2f GB: alloc %.2f s, h2d %.2f s " | ||
| 42 | "(%.2f GB/s copy)\n", | ||
| 43 | ✗ | calls, bytes / 1e9, alloc, copy, | |
| 44 | ✗ | copy > 0 ? (bytes / 1e9) / copy : 0.0); | |
| 45 | ✗ | } | |
| 46 | }; | ||
| 47 | 190 | LoadProf g_loadprof; | |
| 48 | 4041 | inline bool loadprof_on() { return g_loadprof.on; } | |
| 49 | } // namespace | ||
| 50 | |||
| 51 | // ─── dtype + device helpers ──────────────────────────────────────────────── | ||
| 52 | |||
| 53 | 186300 | int dtype_size_bytes(Dtype dt) { | |
| 54 | 186300 | switch (dt) { | |
| 55 | 103469 | case Dtype::FP32: return 4; | |
| 56 | 17474 | case Dtype::FP16: return 2; | |
| 57 | 11709 | case Dtype::BF16: return 2; | |
| 58 | 575 | case Dtype::INT8: return 1; | |
| 59 | 88 | case Dtype::FP8_E4M3: return 1; | |
| 60 | 52968 | case Dtype::INT32: return 4; | |
| 61 | 5 | case Dtype::F64: return 8; | |
| 62 | // Quant dtypes are block-addressed; no per-element size. | ||
| 63 | case Dtype::Q4_0: case Dtype::Q4_1: | ||
| 64 | case Dtype::Q5_0: case Dtype::Q5_1: | ||
| 65 | case Dtype::Q8_0: case Dtype::Q8_1: | ||
| 66 | case Dtype::Q2_K: case Dtype::Q3_K: | ||
| 67 | case Dtype::Q4_K: case Dtype::Q5_K: | ||
| 68 | case Dtype::Q6_K: case Dtype::Q8_K: | ||
| 69 | 12 | return 0; | |
| 70 | } | ||
| 71 | ✗ | return 0; | |
| 72 | 186300 | } | |
| 73 | |||
| 74 | 412613 | bool dtype_is_quant(Dtype dt) { | |
| 75 | 412613 | switch (dt) { | |
| 76 | case Dtype::Q4_0: case Dtype::Q4_1: | ||
| 77 | case Dtype::Q5_0: case Dtype::Q5_1: | ||
| 78 | case Dtype::Q8_0: case Dtype::Q8_1: | ||
| 79 | case Dtype::Q2_K: case Dtype::Q3_K: | ||
| 80 | case Dtype::Q4_K: case Dtype::Q5_K: | ||
| 81 | case Dtype::Q6_K: case Dtype::Q8_K: | ||
| 82 | 988 | return true; | |
| 83 | default: | ||
| 84 | 411625 | return false; | |
| 85 | } | ||
| 86 | 412613 | } | |
| 87 | |||
| 88 | 27 | const char* dtype_name(Dtype dt) { | |
| 89 | 27 | switch (dt) { | |
| 90 | ✗ | case Dtype::FP32: return "FP32"; | |
| 91 | ✗ | case Dtype::FP16: return "FP16"; | |
| 92 | ✗ | case Dtype::BF16: return "BF16"; | |
| 93 | ✗ | case Dtype::INT8: return "INT8"; | |
| 94 | ✗ | case Dtype::INT32: return "INT32"; | |
| 95 | ✗ | case Dtype::F64: return "F64"; | |
| 96 | ✗ | case Dtype::FP8_E4M3: return "FP8_E4M3"; | |
| 97 | ✗ | case Dtype::Q4_0: return "Q4_0"; | |
| 98 | ✗ | case Dtype::Q4_1: return "Q4_1"; | |
| 99 | ✗ | case Dtype::Q5_0: return "Q5_0"; | |
| 100 | ✗ | case Dtype::Q5_1: return "Q5_1"; | |
| 101 | 8 | case Dtype::Q8_0: return "Q8_0"; | |
| 102 | ✗ | case Dtype::Q8_1: return "Q8_1"; | |
| 103 | ✗ | case Dtype::Q2_K: return "Q2_K"; | |
| 104 | ✗ | case Dtype::Q3_K: return "Q3_K"; | |
| 105 | 13 | case Dtype::Q4_K: return "Q4_K"; | |
| 106 | ✗ | case Dtype::Q5_K: return "Q5_K"; | |
| 107 | 6 | case Dtype::Q6_K: return "Q6_K"; | |
| 108 | ✗ | case Dtype::Q8_K: return "Q8_K"; | |
| 109 | } | ||
| 110 | ✗ | return "?"; | |
| 111 | 27 | } | |
| 112 | |||
| 113 | 1167 | int dtype_block_size(Dtype dt) { | |
| 114 | 1167 | switch (dt) { | |
| 115 | case Dtype::Q4_0: case Dtype::Q4_1: | ||
| 116 | case Dtype::Q5_0: case Dtype::Q5_1: | ||
| 117 | case Dtype::Q8_0: case Dtype::Q8_1: | ||
| 118 | 786 | return 32; | |
| 119 | case Dtype::Q2_K: case Dtype::Q3_K: | ||
| 120 | case Dtype::Q4_K: case Dtype::Q5_K: | ||
| 121 | case Dtype::Q6_K: case Dtype::Q8_K: | ||
| 122 | 375 | return 256; | |
| 123 | default: | ||
| 124 | 6 | return 1; | |
| 125 | } | ||
| 126 | 1167 | } | |
| 127 | |||
| 128 | 442 | int dtype_block_bytes(Dtype dt) { | |
| 129 | 442 | switch (dt) { | |
| 130 | 10 | case Dtype::Q4_0: return 18; | |
| 131 | 5 | case Dtype::Q4_1: return 20; | |
| 132 | 5 | case Dtype::Q5_0: return 22; | |
| 133 | 5 | case Dtype::Q5_1: return 24; | |
| 134 | 316 | case Dtype::Q8_0: return 34; | |
| 135 | 5 | case Dtype::Q8_1: return 36; | |
| 136 | 5 | case Dtype::Q2_K: return 82; | |
| 137 | 5 | case Dtype::Q3_K: return 110; | |
| 138 | 50 | case Dtype::Q4_K: return 144; | |
| 139 | 5 | case Dtype::Q5_K: return 176; | |
| 140 | 20 | case Dtype::Q6_K: return 210; | |
| 141 | 5 | case Dtype::Q8_K: return 292; | |
| 142 | 6 | default: return dtype_size_bytes(dt); | |
| 143 | } | ||
| 144 | 442 | } | |
| 145 | |||
| 146 | 185484 | std::size_t dtype_storage_bytes(Dtype d, std::int64_t numel) { | |
| 147 | 185484 | if (numel < 0) { | |
| 148 | 2 | throw std::runtime_error("brotensor: dtype_storage_bytes: negative numel"); | |
| 149 | } | ||
| 150 | 185482 | if (!dtype_is_quant(d)) { | |
| 151 | 370120 | return static_cast<std::size_t>(numel) * | |
| 152 | 185060 | static_cast<std::size_t>(dtype_size_bytes(d)); | |
| 153 | } | ||
| 154 | 422 | const int bs = dtype_block_size(d); | |
| 155 | 422 | const int bb = dtype_block_bytes(d); | |
| 156 | 422 | if (numel % bs != 0) { | |
| 157 | 13 | throw std::runtime_error( | |
| 158 | "brotensor: dtype_storage_bytes: numel not a multiple of block size"); | ||
| 159 | } | ||
| 160 | 818 | return (static_cast<std::size_t>(numel) / static_cast<std::size_t>(bs)) * | |
| 161 | 409 | static_cast<std::size_t>(bb); | |
| 162 | 185469 | } | |
| 163 | |||
| 164 | const Device Device::CPU{DeviceType::CPU, 0}; | ||
| 165 | const Device Device::CUDA{DeviceType::CUDA, 0}; | ||
| 166 | const Device Device::Metal{DeviceType::Metal, 0}; | ||
| 167 | const Device Device::VULKAN{DeviceType::VULKAN, 0}; | ||
| 168 | |||
| 169 | 84 | const char* device_name(Device d) { | |
| 170 | 84 | if (d.is_cpu()) return "CPU"; | |
| 171 | 69 | if (d.is_cuda()) { | |
| 172 | 1 | if (d.index == 0) return "CUDA"; | |
| 173 | static thread_local char buf[32]; | ||
| 174 | ✗ | std::snprintf(buf, sizeof(buf), "CUDA:%d", d.index); | |
| 175 | ✗ | return buf; | |
| 176 | } | ||
| 177 | 68 | if (d.is_metal()) { | |
| 178 | 65 | if (d.index == 0) return "Metal"; | |
| 179 | static thread_local char buf[32]; | ||
| 180 | ✗ | std::snprintf(buf, sizeof(buf), "Metal:%d", d.index); | |
| 181 | ✗ | return buf; | |
| 182 | } | ||
| 183 | 3 | if (d.is_vulkan()) { | |
| 184 | 3 | if (d.index == 0) return "vulkan"; | |
| 185 | static thread_local char buf[32]; | ||
| 186 | ✗ | std::snprintf(buf, sizeof(buf), "vulkan:%d", d.index); | |
| 187 | ✗ | return buf; | |
| 188 | } | ||
| 189 | ✗ | return "?"; | |
| 190 | 84 | } | |
| 191 | |||
| 192 | 2 | std::string to_string(Device d) { | |
| 193 | 2 | return device_name(d); | |
| 194 | } | ||
| 195 | |||
| 196 | // ─── FP16 ↔ FP32 conversion (host-side IEEE 754 binary16) ────────────────── | ||
| 197 | |||
| 198 | 629314361 | uint16_t fp32_to_fp16_bits(float v) { | |
| 199 | uint32_t x; | ||
| 200 | 629314361 | std::memcpy(&x, &v, 4); | |
| 201 | 629314361 | const uint32_t sign = (x >> 16) & 0x8000u; | |
| 202 | 629314361 | int32_t exp = static_cast<int32_t>((x >> 23) & 0xFFu) - 127 + 15; | |
| 203 | 629314361 | uint32_t mant = x & 0x7FFFFFu; | |
| 204 | |||
| 205 | 629314361 | if (((x >> 23) & 0xFFu) == 0xFFu) { | |
| 206 | // Inf or NaN. | ||
| 207 | 22702240 | uint16_t out = static_cast<uint16_t>(sign | 0x7C00u); | |
| 208 | 22702240 | if (mant) out |= 0x0200u; // quiet NaN | |
| 209 | 22702240 | return out; | |
| 210 | } | ||
| 211 | 606612121 | if (exp >= 0x1F) { | |
| 212 | ✗ | return static_cast<uint16_t>(sign | 0x7C00u); | |
| 213 | } | ||
| 214 | 606612121 | if (exp <= 0) { | |
| 215 | 647462 | if (exp < -10) return static_cast<uint16_t>(sign); | |
| 216 | 247011 | mant |= 0x800000u; | |
| 217 | 247011 | const int shift = 14 - exp; | |
| 218 | // Round to nearest, ties to even, as the normal path and the device's | ||
| 219 | // __float2half do (half up made a tie one subnormal ulp off). | ||
| 220 | 247011 | const uint32_t round = (1u << (shift - 1)) - 1u + ((mant >> shift) & 1u); | |
| 221 | 247011 | uint32_t r = (mant + round) >> shift; | |
| 222 | 247011 | return static_cast<uint16_t>(sign | r); | |
| 223 | } | ||
| 224 | 605964659 | const uint32_t lsb = (mant >> 13) & 1u; | |
| 225 | 605964659 | const uint32_t round = 0x00001000u + lsb - 1u; | |
| 226 | 605964659 | uint32_t r_mant = (mant + round) >> 13; | |
| 227 | 605964659 | uint32_t r_exp = static_cast<uint32_t>(exp); | |
| 228 | 605964659 | if (r_mant & 0x400u) { | |
| 229 | 309793 | r_mant = 0; | |
| 230 | 309793 | r_exp += 1; | |
| 231 | 309793 | if (r_exp >= 0x1F) return static_cast<uint16_t>(sign | 0x7C00u); | |
| 232 | 309793 | } | |
| 233 | 605964659 | return static_cast<uint16_t>(sign | (r_exp << 10) | r_mant); | |
| 234 | 629314361 | } | |
| 235 | |||
| 236 | 422592981 | float fp16_bits_to_fp32(uint16_t bits) { | |
| 237 | 422592981 | const uint32_t sign = (static_cast<uint32_t>(bits) & 0x8000u) << 16; | |
| 238 | 422592981 | uint32_t exp = (bits >> 10) & 0x1Fu; | |
| 239 | 422592981 | uint32_t mant = bits & 0x3FFu; | |
| 240 | uint32_t out; | ||
| 241 | 422592981 | if (exp == 0) { | |
| 242 | 737687 | if (mant == 0) { | |
| 243 | 245001 | out = sign; | |
| 244 | 245001 | } else { | |
| 245 | 1802564 | while ((mant & 0x400u) == 0) { mant <<= 1; exp -= 1; } | |
| 246 | 492686 | mant &= 0x3FFu; | |
| 247 | 492686 | uint32_t e = (exp + (127 - 15) + 1) & 0xFFu; | |
| 248 | 492686 | out = sign | (e << 23) | (mant << 13); | |
| 249 | } | ||
| 250 | 422592981 | } else if (exp == 0x1F) { | |
| 251 | ✗ | out = sign | 0x7F800000u | (mant << 13); | |
| 252 | ✗ | } else { | |
| 253 | 421855294 | uint32_t e = exp + (127 - 15); | |
| 254 | 421855294 | out = sign | (e << 23) | (mant << 13); | |
| 255 | } | ||
| 256 | float f; | ||
| 257 | 422592981 | std::memcpy(&f, &out, 4); | |
| 258 | 422592981 | return f; | |
| 259 | } | ||
| 260 | |||
| 261 | // ─── BF16 ↔ FP32 conversion (host-side bfloat16) ─────────────────────────── | ||
| 262 | |||
| 263 | 301702584 | uint16_t fp32_to_bf16_bits(float v) { | |
| 264 | uint32_t x; | ||
| 265 | 301702584 | std::memcpy(&x, &v, 4); | |
| 266 | // NaN: keep it a NaN (the rounding add below could otherwise carry a | ||
| 267 | // NaN's mantissa into the exponent and produce an infinity). | ||
| 268 | 301702584 | if (((x >> 23) & 0xFFu) == 0xFFu && (x & 0x7FFFFFu) != 0) { | |
| 269 | 2 | return static_cast<uint16_t>((x >> 16) | 0x0040u); | |
| 270 | } | ||
| 271 | // Round to nearest, ties to even: add 0x7FFF + LSB-of-result. | ||
| 272 | 301702582 | const uint32_t rounding_bias = 0x7FFFu + ((x >> 16) & 1u); | |
| 273 | 301702582 | x += rounding_bias; | |
| 274 | 301702582 | return static_cast<uint16_t>(x >> 16); | |
| 275 | 301702584 | } | |
| 276 | |||
| 277 | 169422312 | float bf16_bits_to_fp32(uint16_t bits) { | |
| 278 | 169422312 | const uint32_t x = static_cast<uint32_t>(bits) << 16; | |
| 279 | float f; | ||
| 280 | 169422312 | std::memcpy(&f, &x, 4); | |
| 281 | 169422312 | return f; | |
| 282 | } | ||
| 283 | |||
| 284 | 225668027 | float fp8_e4m3_bits_to_fp32(uint8_t bits) { | |
| 285 | 225668027 | const int s = bits >> 7, e = (bits >> 3) & 15, m = bits & 7; | |
| 286 | 225668027 | if (e == 15 && m == 7) return std::numeric_limits<float>::quiet_NaN(); | |
| 287 | 225668027 | const float mag = e == 0 ? std::ldexp(static_cast<float>(m), -9) // m/8 * 2^-6 | |
| 288 | 225655998 | : std::ldexp(1.0f + m / 8.0f, e - 7); | |
| 289 | 225668027 | return s ? -mag : mag; | |
| 290 | 225668027 | } | |
| 291 | |||
| 292 | 22627276 | uint8_t fp32_to_fp8_e4m3_bits(float v) { | |
| 293 | 22627276 | if (std::isnan(v)) return 0x7f; | |
| 294 | 22627276 | const uint8_t sign = std::signbit(v) ? 0x80 : 0x00; | |
| 295 | 22627276 | const float a = std::fabs(v); | |
| 296 | 22627276 | if (a >= 448.0f) return sign | 0x7e; | |
| 297 | // Magnitude codes 0..126 ascend with their value: binary search the | ||
| 298 | // bracket, then round to the nearer, ties to the even code. | ||
| 299 | 22561575 | int lo = 0, hi = 126; | |
| 300 | 180479178 | while (hi - lo > 1) { | |
| 301 | 157917603 | const int mid = (lo + hi) / 2; | |
| 302 | 157917603 | if (fp8_e4m3_bits_to_fp32(static_cast<uint8_t>(mid)) <= a) lo = mid; | |
| 303 | 51190035 | else hi = mid; | |
| 304 | } | ||
| 305 | 22561575 | const float dl = a - fp8_e4m3_bits_to_fp32(static_cast<uint8_t>(lo)); | |
| 306 | 22561575 | const float dh = fp8_e4m3_bits_to_fp32(static_cast<uint8_t>(hi)) - a; | |
| 307 | 22561575 | const int code = dl < dh ? lo : dh < dl ? hi : ((lo & 1) ? hi : lo); | |
| 308 | 22561575 | return sign | static_cast<uint8_t>(code); | |
| 309 | 22627276 | } | |
| 310 | |||
| 311 | // ─── internal helpers ────────────────────────────────────────────────────── | ||
| 312 | |||
| 313 | namespace { | ||
| 314 | |||
| 315 | 45 | [[noreturn]] void throw_msg(const std::string& m) { | |
| 316 | 45 | throw std::runtime_error(m); | |
| 317 | ✗ | } | |
| 318 | |||
| 319 | 32742387924 | void check_host(const Tensor& t, const char* who) { | |
| 320 | 32742387924 | if (t.device != Device::CPU) { | |
| 321 | 10 | std::string m = "brotensor: "; | |
| 322 | 10 | m += who; | |
| 323 | 10 | m += ": tensor is on "; | |
| 324 | 10 | m += device_name(t.device); | |
| 325 | 10 | m += ", not CPU"; | |
| 326 | 10 | throw_msg(m); | |
| 327 | 10 | } | |
| 328 | 32742387924 | } | |
| 329 | |||
| 330 | // Guard against negative dimensions before they are cast to std::size_t in | ||
| 331 | // bytes() — an unchecked (size_t)(-1) underflows to an astronomical count | ||
| 332 | // that sails past the bytes==0 short-circuits and reaches backend_alloc. | ||
| 333 | 227104 | void check_dims(int r, int c, const char* who) { | |
| 334 | 227104 | if (r < 0 || c < 0) { | |
| 335 | 7 | std::string m = "brotensor: "; | |
| 336 | 7 | m += who; | |
| 337 | 7 | m += ": negative dimension"; | |
| 338 | 7 | throw_msg(m); | |
| 339 | 7 | } | |
| 340 | 227104 | } | |
| 341 | |||
| 342 | 226871 | void check_quant_cols(Dtype dt, int c, const char* who) { | |
| 343 | 226871 | if (!dtype_is_quant(dt) || c <= 0) return; | |
| 344 | 411 | const int bs = dtype_block_size(dt); | |
| 345 | 411 | if (c % bs != 0) { | |
| 346 | 6 | std::string m = "brotensor: "; | |
| 347 | 6 | m += who; | |
| 348 | 6 | m += ": "; | |
| 349 | 6 | m += dtype_name(dt); | |
| 350 | 6 | m += " cols ("; | |
| 351 | 6 | m += std::to_string(c); | |
| 352 | 6 | m += ") is not a multiple of block size ("; | |
| 353 | 6 | m += std::to_string(bs); | |
| 354 | 6 | m += ")"; | |
| 355 | 6 | throw_msg(m); | |
| 356 | 6 | } | |
| 357 | 226871 | } | |
| 358 | |||
| 359 | 32742285518 | void check_dtype(const Tensor& t, Dtype expected, const char* who) { | |
| 360 | 32742285518 | if (t.dtype != expected) { | |
| 361 | 9 | std::string m = "brotensor: "; | |
| 362 | 9 | m += who; | |
| 363 | 9 | m += ": dtype mismatch"; | |
| 364 | 9 | throw_msg(m); | |
| 365 | 9 | } | |
| 366 | 32742285518 | } | |
| 367 | |||
| 368 | // Allocation accounting. Two relaxed atomics on a path that is already an | ||
| 369 | // allocator call, so the cost is nil; see AllocStats in tensor.h for why the | ||
| 370 | // counters exist at all. | ||
| 371 | std::atomic<std::uint64_t> g_alloc_count{0}; | ||
| 372 | std::atomic<std::uint64_t> g_alloc_bytes{0}; | ||
| 373 | |||
| 374 | 103710 | void* backend_alloc(Device d, std::size_t bytes) { | |
| 375 | 103710 | if (bytes == 0) return nullptr; | |
| 376 | 103445 | g_alloc_count.fetch_add(1, std::memory_order_relaxed); | |
| 377 | 103445 | g_alloc_bytes.fetch_add(bytes, std::memory_order_relaxed); | |
| 378 | 103445 | return detail::alloc_for(d).alloc(bytes, d.index); | |
| 379 | 103710 | } | |
| 380 | |||
| 381 | 102569 | void backend_free(Device d, void* p) { | |
| 382 | 102569 | if (!p) return; | |
| 383 | 102569 | detail::alloc_for(d).free(p, d.index); | |
| 384 | 102569 | } | |
| 385 | |||
| 386 | 19510 | void backend_zero(Device d, void* p, std::size_t bytes) { | |
| 387 | 19510 | if (bytes == 0 || !p) return; | |
| 388 | 19505 | detail::alloc_for(d).memset_zero(p, bytes, d.index); | |
| 389 | 19510 | } | |
| 390 | |||
| 391 | } // namespace | ||
| 392 | |||
| 393 | // ─── jit symbolic-tensor tracking ────────────────────────────────────────── | ||
| 394 | |||
| 395 | namespace detail { | ||
| 396 | // Null until brotensor::jit starts a trace. See tensor.h. | ||
| 397 | JitSlotRetargetHook jit_slot_retarget = nullptr; | ||
| 398 | JitClampInplaceHook jit_clamp_inplace_hook = nullptr; | ||
| 399 | JitCastOutHook jit_cast_out_hook = nullptr; | ||
| 400 | } // namespace detail | ||
| 401 | |||
| 402 | namespace { | ||
| 403 | |||
| 404 | // Tensor is 48 bytes on a 64-bit build. The only hole in the 40-byte layout | ||
| 405 | // it had before `jit_slot` was the three bytes after `owns_`, which a | ||
| 406 | // four-byte field cannot use, so the tag cost one alignment word. Pinned so a | ||
| 407 | // future field lands by decision rather than by accident. | ||
| 408 | static_assert(sizeof(void*) != 8 || sizeof(Tensor) == 48, | ||
| 409 | "brotensor::Tensor layout changed unexpectedly"); | ||
| 410 | |||
| 411 | 1 | [[noreturn]] void throw_symbolic(const char* who) { | |
| 412 | 1 | std::string m = "brotensor: "; | |
| 413 | 1 | m += who; | |
| 414 | 1 | m += ": this Tensor is a traced intermediate, not a tensor. It stands for " | |
| 415 | "a node of the trace being recorded between begin_trace() and " | ||
| 416 | "end_trace(), owns no memory, and has no contents to read or copy — " | ||
| 417 | "two Tensors cannot both become the one buffer end_trace() allocates " | ||
| 418 | "for it. Use it in the expression, move it, or store() it into a " | ||
| 419 | "buffer you already own."; | ||
| 420 | 1 | throw std::runtime_error(m); | |
| 421 | 1 | } | |
| 422 | |||
| 423 | 79102 | inline void check_not_symbolic(const Tensor& t, const char* who) { | |
| 424 | 79102 | if (t.jit_slot >= 0) throw_symbolic(who); | |
| 425 | 79101 | } | |
| 426 | |||
| 427 | } // namespace | ||
| 428 | |||
| 429 | // ─── Tensor lifetime ─────────────────────────────────────────────────────── | ||
| 430 | |||
| 431 | 223296 | Tensor::~Tensor() { | |
| 432 | 111648 | if (jit_slot >= 0 && detail::jit_slot_retarget) { | |
| 433 | 105 | detail::jit_slot_retarget(jit_slot, this, nullptr); | |
| 434 | 105 | } | |
| 435 | 111648 | release_(); | |
| 436 | 223296 | } | |
| 437 | |||
| 438 | 181304 | void Tensor::release_() { | |
| 439 | 181304 | if (owns_ && data) { | |
| 440 | // alloc_for can throw if the backend was somehow un-registered between | ||
| 441 | // alloc and free; let it propagate — better than leaking a different | ||
| 442 | // backend's pointer. | ||
| 443 | 102569 | backend_free(device, data); | |
| 444 | 102569 | } | |
| 445 | 181304 | data = nullptr; | |
| 446 | 181304 | owns_ = false; | |
| 447 | 181304 | cap_bytes_ = 0; | |
| 448 | 181304 | } | |
| 449 | |||
| 450 | // Move: a symbolic traced intermediate moves like any other tensor, and the | ||
| 451 | // jit's slot -> Tensor* table is retargeted onto the new object so end_trace() | ||
| 452 | // still knows where the caller is holding that value. | ||
| 453 | 624 | Tensor::Tensor(Tensor&& o) noexcept | |
| 454 | 312 | : data(o.data), rows(o.rows), cols(o.cols), | |
| 455 | 312 | dtype(o.dtype), device(o.device), jit_slot(o.jit_slot), owns_(o.owns_), | |
| 456 | 624 | cap_bytes_(o.cap_bytes_) { | |
| 457 | 312 | o.data = nullptr; | |
| 458 | 312 | o.rows = 0; | |
| 459 | 312 | o.cols = 0; | |
| 460 | 312 | o.owns_ = false; | |
| 461 | 312 | o.cap_bytes_ = 0; | |
| 462 | 312 | if (jit_slot >= 0) { | |
| 463 | ✗ | o.jit_slot = -1; | |
| 464 | ✗ | if (detail::jit_slot_retarget) detail::jit_slot_retarget(jit_slot, &o, this); | |
| 465 | ✗ | } | |
| 466 | 624 | } | |
| 467 | |||
| 468 | 7094 | Tensor& Tensor::operator=(Tensor&& o) noexcept { | |
| 469 | 7094 | if (this != &o) { | |
| 470 | 7093 | if (jit_slot >= 0 && detail::jit_slot_retarget) { | |
| 471 | ✗ | detail::jit_slot_retarget(jit_slot, this, nullptr); | |
| 472 | ✗ | } | |
| 473 | 7093 | jit_slot = -1; | |
| 474 | 7093 | release_(); | |
| 475 | 7093 | data = o.data; | |
| 476 | 7093 | rows = o.rows; | |
| 477 | 7093 | cols = o.cols; | |
| 478 | 7093 | dtype = o.dtype; | |
| 479 | 7093 | device = o.device; | |
| 480 | 7093 | owns_ = o.owns_; | |
| 481 | 7093 | cap_bytes_ = o.cap_bytes_; | |
| 482 | 7093 | o.data = nullptr; | |
| 483 | 7093 | o.rows = 0; | |
| 484 | 7093 | o.cols = 0; | |
| 485 | 7093 | o.owns_ = false; | |
| 486 | 7093 | o.cap_bytes_ = 0; | |
| 487 | 7093 | if (o.jit_slot >= 0) { | |
| 488 | 8 | jit_slot = o.jit_slot; | |
| 489 | 8 | o.jit_slot = -1; | |
| 490 | 8 | if (detail::jit_slot_retarget) detail::jit_slot_retarget(jit_slot, &o, this); | |
| 491 | 8 | } | |
| 492 | 7093 | } | |
| 493 | 7094 | return *this; | |
| 494 | } | ||
| 495 | |||
| 496 | // Copy ctor / assignment: device-aware deep copy via clone(). Implemented in | ||
| 497 | // terms of the move assignment so all the ownership bookkeeping lives in one | ||
| 498 | // place. A symbolic traced intermediate has no contents to copy and cannot be | ||
| 499 | // owned twice, so copying one is rejected rather than silently producing an | ||
| 500 | // empty tensor. | ||
| 501 | 3194 | Tensor::Tensor(const Tensor& o) { | |
| 502 | 1597 | check_not_symbolic(o, "copy"); | |
| 503 | 1597 | *this = o.clone(); | |
| 504 | 1597 | } | |
| 505 | |||
| 506 | 139 | Tensor& Tensor::operator=(const Tensor& o) { | |
| 507 | 139 | if (this != &o) { | |
| 508 | 138 | check_not_symbolic(o, "copy"); | |
| 509 | 138 | *this = o.clone(); | |
| 510 | 138 | } | |
| 511 | 139 | return *this; | |
| 512 | } | ||
| 513 | |||
| 514 | 122539 | std::size_t Tensor::bytes() const { | |
| 515 | 122539 | check_dims(rows, cols, "bytes"); | |
| 516 | 122539 | if (rows > 0 && cols > 0) { | |
| 517 | 122316 | check_quant_cols(dtype, cols, "Tensor::bytes"); | |
| 518 | 122316 | } | |
| 519 | 122539 | return dtype_storage_bytes( | |
| 520 | 122539 | dtype, | |
| 521 | 122539 | static_cast<std::int64_t>(rows) * static_cast<std::int64_t>(cols)); | |
| 522 | } | ||
| 523 | |||
| 524 | 3 | std::size_t Tensor::row_bytes() const { | |
| 525 | 3 | check_dims(rows, cols, "row_bytes"); | |
| 526 | 3 | check_quant_cols(dtype, cols, "Tensor::row_bytes"); | |
| 527 | 3 | return dtype_storage_bytes(dtype, static_cast<std::int64_t>(cols)); | |
| 528 | } | ||
| 529 | |||
| 530 | // ─── Factories ───────────────────────────────────────────────────────────── | ||
| 531 | |||
| 532 | 41147 | Tensor Tensor::empty_on(Device d, int r, int c, Dtype dt) { | |
| 533 | 41147 | check_dims(r, c, "empty_on"); | |
| 534 | 41147 | check_quant_cols(dt, c, "Tensor::empty_on"); | |
| 535 | 41147 | d = detail::resolve_device_alias(d); | |
| 536 | 41147 | Tensor t; | |
| 537 | 41147 | t.device = d; | |
| 538 | 41147 | t.dtype = dt; | |
| 539 | 41147 | t.rows = r; | |
| 540 | 41147 | t.cols = c; | |
| 541 | 41147 | t.data = backend_alloc(d, t.bytes()); | |
| 542 | 41147 | t.owns_ = (t.data != nullptr); | |
| 543 | 41147 | t.cap_bytes_ = t.owns_ ? t.bytes() : 0; | |
| 544 | 41147 | return t; | |
| 545 | 41147 | } | |
| 546 | |||
| 547 | 19235 | Tensor Tensor::zeros_on(Device d, int r, int c, Dtype dt) { | |
| 548 | 19235 | Tensor t = empty_on(d, r, c, dt); | |
| 549 | 19235 | backend_zero(d, t.data, t.bytes()); | |
| 550 | 19235 | return t; | |
| 551 | 19235 | } | |
| 552 | |||
| 553 | 1 | Tensor Tensor::empty(int r, int c, Dtype dt) { | |
| 554 | 1 | return empty_on(default_device(), r, c, dt); | |
| 555 | } | ||
| 556 | |||
| 557 | 5 | Tensor Tensor::zeros(int r, int c, Dtype dt) { | |
| 558 | 5 | return zeros_on(default_device(), r, c, dt); | |
| 559 | } | ||
| 560 | |||
| 561 | namespace { | ||
| 562 | // Shared upload core for from_host_*_on. When BROTENSOR_TIME_LOAD is set it | ||
| 563 | // attributes host wall time to the empty_on (alloc enqueue) and the H2D copy. | ||
| 564 | 4041 | Tensor from_host_typed(Device d, const void* src, int r, int c, Dtype dt) { | |
| 565 | using clock = std::chrono::steady_clock; | ||
| 566 | 4041 | const bool prof = loadprof_on() && d != Device::CPU; | |
| 567 | 4041 | auto t0 = clock::now(); | |
| 568 | 4041 | Tensor t = Tensor::empty_on(d, r, c, dt); | |
| 569 | 4041 | const std::size_t n = t.bytes(); | |
| 570 | 4041 | if (n == 0) return t; | |
| 571 | 4041 | auto t1 = clock::now(); | |
| 572 | 4041 | if (d == Device::CPU) { | |
| 573 | 877 | std::memcpy(t.data, src, n); | |
| 574 | 877 | } else { | |
| 575 | 3164 | detail::alloc_for(d).memcpy_h2d(t.data, src, n, d.index); | |
| 576 | } | ||
| 577 | 4041 | if (prof) { | |
| 578 | ✗ | auto t2 = clock::now(); | |
| 579 | ✗ | g_loadprof.alloc += std::chrono::duration<double>(t1 - t0).count(); | |
| 580 | ✗ | g_loadprof.copy += std::chrono::duration<double>(t2 - t1).count(); | |
| 581 | ✗ | g_loadprof.bytes += static_cast<double>(n); | |
| 582 | ✗ | ++g_loadprof.calls; | |
| 583 | ✗ | } | |
| 584 | 4041 | return t; | |
| 585 | 4041 | } | |
| 586 | } // namespace | ||
| 587 | |||
| 588 | 2136 | Tensor Tensor::from_host_on(Device d, const float* src, int r, int c) { | |
| 589 | 2136 | return from_host_typed(d, src, r, c, Dtype::FP32); | |
| 590 | } | ||
| 591 | |||
| 592 | 1505 | Tensor Tensor::from_host_fp16_on(Device d, const uint16_t* src, int r, int c) { | |
| 593 | 1505 | return from_host_typed(d, src, r, c, Dtype::FP16); | |
| 594 | } | ||
| 595 | |||
| 596 | 400 | Tensor Tensor::from_host_bf16_on(Device d, const uint16_t* src, int r, int c) { | |
| 597 | 400 | return from_host_typed(d, src, r, c, Dtype::BF16); | |
| 598 | } | ||
| 599 | |||
| 600 | 23 | Tensor Tensor::from_host_int8_on(Device d, const int8_t* src, int r, int c) { | |
| 601 | 23 | Tensor t = empty_on(d, r, c, Dtype::INT8); | |
| 602 | 23 | const std::size_t n = t.bytes(); | |
| 603 | 23 | if (n == 0) return t; | |
| 604 | 23 | if (d == Device::CPU) { | |
| 605 | ✗ | std::memcpy(t.data, src, n); | |
| 606 | ✗ | } else { | |
| 607 | 23 | detail::alloc_for(d).memcpy_h2d(t.data, src, n, d.index); | |
| 608 | } | ||
| 609 | 23 | return t; | |
| 610 | 23 | } | |
| 611 | |||
| 612 | 147 | Tensor Tensor::from_raw_bytes_on(Device target, const void* src, int r, int c, | |
| 613 | Dtype dt, std::size_t nbytes) { | ||
| 614 | 147 | Tensor t = empty_on(target, r, c, dt); | |
| 615 | 147 | if (t.bytes() != nbytes) { | |
| 616 | 1 | throw std::runtime_error( | |
| 617 | "brotensor: from_raw_bytes_on: nbytes does not match tensor size"); | ||
| 618 | } | ||
| 619 | 146 | if (nbytes == 0) return t; | |
| 620 | 145 | if (target == Device::CPU) { | |
| 621 | 27 | std::memcpy(t.data, src, nbytes); | |
| 622 | 27 | } else { | |
| 623 | 118 | detail::alloc_for(target).memcpy_h2d(t.data, src, nbytes, target.index); | |
| 624 | } | ||
| 625 | 145 | return t; | |
| 626 | 147 | } | |
| 627 | |||
| 628 | 21 | Tensor Tensor::from_host(const float* src, int r, int c) { | |
| 629 | 21 | return from_host_on(default_device(), src, r, c); | |
| 630 | } | ||
| 631 | |||
| 632 | 14 | Tensor Tensor::from_host_fp16(const uint16_t* src, int r, int c) { | |
| 633 | 14 | return from_host_fp16_on(default_device(), src, r, c); | |
| 634 | } | ||
| 635 | |||
| 636 | 5 | Tensor Tensor::from_host_bf16(const uint16_t* src, int r, int c) { | |
| 637 | 5 | return from_host_bf16_on(default_device(), src, r, c); | |
| 638 | } | ||
| 639 | |||
| 640 | ✗ | Tensor Tensor::from_host_int8(const int8_t* src, int r, int c) { | |
| 641 | ✗ | return from_host_int8_on(default_device(), src, r, c); | |
| 642 | } | ||
| 643 | |||
| 644 | 515 | Tensor Tensor::view(Device d, void* data, int r, int c, Dtype dt) { | |
| 645 | 515 | check_dims(r, c, "view"); | |
| 646 | 515 | check_quant_cols(dt, c, "Tensor::view"); | |
| 647 | 515 | d = detail::resolve_device_alias(d); | |
| 648 | 515 | Tensor t; | |
| 649 | 515 | t.device = d; | |
| 650 | 515 | t.dtype = dt; | |
| 651 | 515 | t.rows = r; | |
| 652 | 515 | t.cols = c; | |
| 653 | 515 | t.data = data; | |
| 654 | 515 | t.owns_ = false; | |
| 655 | 515 | return t; | |
| 656 | 515 | } | |
| 657 | |||
| 658 | // ─── Migration ───────────────────────────────────────────────────────────── | ||
| 659 | |||
| 660 | 3957 | Tensor Tensor::clone() const { | |
| 661 | 3957 | check_not_symbolic(*this, "clone"); | |
| 662 | 3957 | Tensor t = empty_on(device, rows, cols, dtype); | |
| 663 | 3957 | const std::size_t n = bytes(); | |
| 664 | 3957 | if (n == 0 || !data) return t; | |
| 665 | 3954 | if (device == Device::CPU) { | |
| 666 | 3934 | std::memcpy(t.data, data, n); | |
| 667 | 3934 | } else { | |
| 668 | 20 | detail::alloc_for(device).memcpy_d2d(t.data, data, n, device.index); | |
| 669 | } | ||
| 670 | 3954 | return t; | |
| 671 | 3957 | } | |
| 672 | |||
| 673 | 10521 | Tensor Tensor::to(Device target) const { | |
| 674 | 10521 | check_not_symbolic(*this, "to"); | |
| 675 | 10521 | if (target == device) return clone(); | |
| 676 | 10220 | Tensor t = empty_on(target, rows, cols, dtype); | |
| 677 | 10220 | const std::size_t n = bytes(); | |
| 678 | 10220 | if (n == 0 || !data) return t; | |
| 679 | |||
| 680 | 10220 | if (device.is_cpu()) { | |
| 681 | // CPU → GPU. | ||
| 682 | 6130 | detail::alloc_for(target).memcpy_h2d(t.data, data, n, target.index); | |
| 683 | 10220 | } else if (target.is_cpu()) { | |
| 684 | // GPU → CPU. | ||
| 685 | 4090 | detail::alloc_for(device).memcpy_d2h(t.data, data, n, device.index); | |
| 686 | 4090 | } else if (device.type == target.type) { | |
| 687 | // Direct peer copy between two GPUs of one backend (CUDA or Vulkan; | ||
| 688 | // Metal has a single device and never gets here). | ||
| 689 | ✗ | auto peer_fn = detail::alloc_for(target).memcpy_peer; | |
| 690 | ✗ | if (peer_fn) { | |
| 691 | ✗ | peer_fn(t.data, target.index, data, device.index, n); | |
| 692 | ✗ | } else { | |
| 693 | ✗ | std::unique_ptr<unsigned char[]> staging(new unsigned char[n]); | |
| 694 | ✗ | detail::alloc_for(device).memcpy_d2h(staging.get(), data, n, device.index); | |
| 695 | ✗ | detail::alloc_for(target).memcpy_h2d(t.data, staging.get(), n, target.index); | |
| 696 | ✗ | } | |
| 697 | ✗ | } else { | |
| 698 | // GPU → different GPU backend. Bounce through host. | ||
| 699 | ✗ | std::unique_ptr<unsigned char[]> staging(new unsigned char[n]); | |
| 700 | ✗ | detail::alloc_for(device).memcpy_d2h(staging.get(), data, n, device.index); | |
| 701 | ✗ | detail::alloc_for(target).memcpy_h2d(t.data, staging.get(), n, target.index); | |
| 702 | ✗ | } | |
| 703 | 10220 | return t; | |
| 704 | 20741 | } | |
| 705 | |||
| 706 | // ─── Mutators ────────────────────────────────────────────────────────────── | ||
| 707 | |||
| 708 | 275 | void Tensor::zero() { | |
| 709 | 275 | backend_zero(device, data, bytes()); | |
| 710 | 275 | } | |
| 711 | |||
| 712 | 62886 | void Tensor::resize(int r, int c, Dtype dt) { | |
| 713 | 62886 | check_not_symbolic(*this, "resize"); | |
| 714 | 62886 | check_dims(r, c, "resize"); | |
| 715 | 62886 | check_quant_cols(dt, c, "Tensor::resize"); | |
| 716 | 62886 | if (r == rows && c == cols && dt == dtype && data != nullptr) return; | |
| 717 | 62872 | const std::size_t new_bytes = dtype_storage_bytes( | |
| 718 | 62872 | dt, | |
| 719 | 62872 | static_cast<std::int64_t>(r) * static_cast<std::int64_t>(c)); | |
| 720 | // A non-owning view over real storage cannot be reshaped: reallocating | ||
| 721 | // would silently allocate fresh owned memory and sever the view, leaving | ||
| 722 | // callers with a tensor that no longer aliases what they passed to | ||
| 723 | // view(). Reject it explicitly rather than converting it in place. | ||
| 724 | // (A default-constructed / released tensor — owns_ == false but | ||
| 725 | // data == nullptr — is not a view and resizes normally.) | ||
| 726 | 62872 | if (!owns_ && data != nullptr) { | |
| 727 | 2 | throw_msg("brotensor: resize: cannot reshape a non-owning view; " | |
| 728 | "allocate a fresh tensor or re-view() with the new shape"); | ||
| 729 | } | ||
| 730 | // Keep the existing storage whenever the new shape fits — the buffer | ||
| 731 | // stabilises at its high-water mark and its device pointer stays put | ||
| 732 | // (required when the op sequence using it is CUDA-graph captured). | ||
| 733 | 62870 | if (new_bytes > cap_bytes_ || !owns_) { | |
| 734 | 62563 | release_(); | |
| 735 | 62563 | device = detail::resolve_device_alias(device); | |
| 736 | 62563 | data = backend_alloc(device, new_bytes); | |
| 737 | 62563 | owns_ = (data != nullptr); | |
| 738 | 62563 | cap_bytes_ = owns_ ? new_bytes : 0; | |
| 739 | 62563 | } | |
| 740 | 62870 | rows = r; | |
| 741 | 62870 | cols = c; | |
| 742 | 62870 | dtype = dt; | |
| 743 | 62886 | } | |
| 744 | |||
| 745 | // ─── Host accessors ──────────────────────────────────────────────────────── | ||
| 746 | |||
| 747 | 429339190 | float* Tensor::host_f32_mut() { | |
| 748 | 429339190 | check_host(*this, "host_f32_mut"); | |
| 749 | 429339190 | check_dtype(*this, Dtype::FP32, "host_f32_mut"); | |
| 750 | 429339190 | return static_cast<float*>(data); | |
| 751 | } | ||
| 752 | 32312820450 | const float* Tensor::host_f32() const { | |
| 753 | 32312820450 | check_host(*this, "host_f32"); | |
| 754 | 32312820450 | check_dtype(*this, Dtype::FP32, "host_f32"); | |
| 755 | 32312820450 | return static_cast<const float*>(data); | |
| 756 | } | ||
| 757 | |||
| 758 | 1462 | uint16_t* Tensor::host_fp16_mut() { | |
| 759 | 1462 | check_host(*this, "host_fp16_mut"); | |
| 760 | 1462 | check_dtype(*this, Dtype::FP16, "host_fp16_mut"); | |
| 761 | 1462 | return static_cast<uint16_t*>(data); | |
| 762 | } | ||
| 763 | 4295 | const uint16_t* Tensor::host_fp16() const { | |
| 764 | 4295 | check_host(*this, "host_fp16"); | |
| 765 | 4295 | check_dtype(*this, Dtype::FP16, "host_fp16"); | |
| 766 | 4295 | return static_cast<const uint16_t*>(data); | |
| 767 | } | ||
| 768 | |||
| 769 | 1330 | uint16_t* Tensor::host_bf16_mut() { | |
| 770 | 1330 | check_host(*this, "host_bf16_mut"); | |
| 771 | 1330 | check_dtype(*this, Dtype::BF16, "host_bf16_mut"); | |
| 772 | 1330 | return static_cast<uint16_t*>(data); | |
| 773 | } | ||
| 774 | 1891 | const uint16_t* Tensor::host_bf16() const { | |
| 775 | 1891 | check_host(*this, "host_bf16"); | |
| 776 | 1891 | check_dtype(*this, Dtype::BF16, "host_bf16"); | |
| 777 | 1891 | return static_cast<const uint16_t*>(data); | |
| 778 | } | ||
| 779 | |||
| 780 | 50543 | void* Tensor::host_raw_mut() { | |
| 781 | 50543 | check_host(*this, "host_raw_mut"); | |
| 782 | 50543 | return data; | |
| 783 | } | ||
| 784 | 51855 | const void* Tensor::host_raw() const { | |
| 785 | 51855 | check_host(*this, "host_raw"); | |
| 786 | 51855 | return data; | |
| 787 | } | ||
| 788 | |||
| 789 | 71999 | float& Tensor::at(int r, int c) { | |
| 790 | 71999 | check_host(*this, "at"); | |
| 791 | 71999 | check_dtype(*this, Dtype::FP32, "at"); | |
| 792 | 71999 | if (r < 0 || r >= rows || c < 0 || c >= cols) { | |
| 793 | 4 | throw_msg("brotensor: at: index out of range"); | |
| 794 | } | ||
| 795 | 71995 | return static_cast<float*>(data)[static_cast<std::size_t>(r) * cols + c]; | |
| 796 | 4 | } | |
| 797 | |||
| 798 | 44909 | float Tensor::at(int r, int c) const { | |
| 799 | 44909 | check_host(*this, "at"); | |
| 800 | 44909 | check_dtype(*this, Dtype::FP32, "at"); | |
| 801 | 44909 | if (r < 0 || r >= rows || c < 0 || c >= cols) { | |
| 802 | 1 | throw_msg("brotensor: at: index out of range"); | |
| 803 | } | ||
| 804 | 44908 | return static_cast<const float*>(data)[static_cast<std::size_t>(r) * cols + c]; | |
| 805 | 1 | } | |
| 806 | |||
| 807 | // ─── Host roundtrip ──────────────────────────────────────────────────────── | ||
| 808 | |||
| 809 | 253 | std::vector<float> Tensor::to_host_vector() const { | |
| 810 | 253 | if (dtype != Dtype::FP32) { | |
| 811 | 1 | throw_msg("brotensor: to_host_vector: dtype not FP32"); | |
| 812 | } | ||
| 813 | 252 | std::vector<float> out(static_cast<std::size_t>(rows) * cols); | |
| 814 | 252 | const std::size_t n = bytes(); | |
| 815 | 252 | if (n == 0) return out; | |
| 816 | 251 | if (device.is_cpu()) { | |
| 817 | 139 | std::memcpy(out.data(), data, n); | |
| 818 | 139 | } else { | |
| 819 | 112 | detail::alloc_for(device).memcpy_d2h(out.data(), data, n, device.index); | |
| 820 | } | ||
| 821 | 251 | return out; | |
| 822 | 253 | } | |
| 823 | |||
| 824 | 316 | std::vector<uint16_t> Tensor::to_host_vector_fp16() const { | |
| 825 | 316 | if (dtype != Dtype::FP16) { | |
| 826 | 1 | throw_msg("brotensor: to_host_vector_fp16: dtype not FP16"); | |
| 827 | } | ||
| 828 | 315 | std::vector<uint16_t> out(static_cast<std::size_t>(rows) * cols); | |
| 829 | 315 | const std::size_t n = bytes(); | |
| 830 | 315 | if (n == 0) return out; | |
| 831 | 314 | if (device.is_cpu()) { | |
| 832 | 7 | std::memcpy(out.data(), data, n); | |
| 833 | 7 | } else { | |
| 834 | 307 | detail::alloc_for(device).memcpy_d2h(out.data(), data, n, device.index); | |
| 835 | } | ||
| 836 | 314 | return out; | |
| 837 | 316 | } | |
| 838 | |||
| 839 | 115 | std::vector<uint16_t> Tensor::to_host_vector_bf16() const { | |
| 840 | 115 | if (dtype != Dtype::BF16) { | |
| 841 | 1 | throw_msg("brotensor: to_host_vector_bf16: dtype not BF16"); | |
| 842 | } | ||
| 843 | 114 | std::vector<uint16_t> out(static_cast<std::size_t>(rows) * cols); | |
| 844 | 114 | const std::size_t n = bytes(); | |
| 845 | 114 | if (n == 0) return out; | |
| 846 | 113 | if (device.is_cpu()) { | |
| 847 | 5 | std::memcpy(out.data(), data, n); | |
| 848 | 5 | } else { | |
| 849 | 108 | detail::alloc_for(device).memcpy_d2h(out.data(), data, n, device.index); | |
| 850 | } | ||
| 851 | 113 | return out; | |
| 852 | 115 | } | |
| 853 | |||
| 854 | 891 | void Tensor::copy_to_host(float* dst) const { | |
| 855 | 891 | if (dtype != Dtype::FP32) { | |
| 856 | 1 | throw_msg("brotensor: copy_to_host: dtype not FP32"); | |
| 857 | } | ||
| 858 | 890 | const std::size_t n = bytes(); | |
| 859 | 890 | if (n == 0) return; | |
| 860 | 889 | if (device.is_cpu()) { | |
| 861 | 107 | std::memcpy(dst, data, n); | |
| 862 | 107 | } else { | |
| 863 | 782 | detail::alloc_for(device).memcpy_d2h(dst, data, n, device.index); | |
| 864 | } | ||
| 865 | 891 | } | |
| 866 | |||
| 867 | 514 | void Tensor::copy_to_host_fp16(uint16_t* dst) const { | |
| 868 | 514 | if (dtype != Dtype::FP16) { | |
| 869 | 1 | throw_msg("brotensor: copy_to_host_fp16: dtype not FP16"); | |
| 870 | } | ||
| 871 | 513 | const std::size_t n = bytes(); | |
| 872 | 513 | if (n == 0) return; | |
| 873 | 512 | if (device.is_cpu()) { | |
| 874 | ✗ | std::memcpy(dst, data, n); | |
| 875 | ✗ | } else { | |
| 876 | 512 | detail::alloc_for(device).memcpy_d2h(dst, data, n, device.index); | |
| 877 | } | ||
| 878 | 514 | } | |
| 879 | |||
| 880 | 58 | void Tensor::copy_to_host_bf16(uint16_t* dst) const { | |
| 881 | 58 | if (dtype != Dtype::BF16) { | |
| 882 | 1 | throw_msg("brotensor: copy_to_host_bf16: dtype not BF16"); | |
| 883 | } | ||
| 884 | 57 | const std::size_t n = bytes(); | |
| 885 | 57 | if (n == 0) return; | |
| 886 | 56 | if (device.is_cpu()) { | |
| 887 | ✗ | std::memcpy(dst, data, n); | |
| 888 | ✗ | } else { | |
| 889 | 56 | detail::alloc_for(device).memcpy_d2h(dst, data, n, device.index); | |
| 890 | } | ||
| 891 | 58 | } | |
| 892 | |||
| 893 | 42 | void Tensor::copy_from_host_raw(const void* src, std::size_t nbytes) { | |
| 894 | 42 | if (nbytes > bytes()) { | |
| 895 | ✗ | throw_msg("brotensor: copy_from_host_raw: nbytes exceeds the tensor's size"); | |
| 896 | } | ||
| 897 | 42 | if (nbytes == 0) return; | |
| 898 | 42 | if (device.is_cpu()) { | |
| 899 | ✗ | std::memcpy(data, src, nbytes); | |
| 900 | ✗ | } else { | |
| 901 | 42 | detail::alloc_for(device).memcpy_h2d(data, src, nbytes, device.index); | |
| 902 | } | ||
| 903 | 42 | } | |
| 904 | |||
| 905 | 28 | void Tensor::copy_to_host_raw(void* dst, std::size_t nbytes) const { | |
| 906 | 28 | if (nbytes > bytes()) { | |
| 907 | ✗ | throw_msg("brotensor: copy_to_host_raw: nbytes exceeds the tensor's size"); | |
| 908 | } | ||
| 909 | 28 | if (nbytes == 0) return; | |
| 910 | 28 | if (device.is_cpu()) { | |
| 911 | ✗ | std::memcpy(dst, data, nbytes); | |
| 912 | ✗ | } else { | |
| 913 | 28 | detail::alloc_for(device).memcpy_d2h(dst, data, nbytes, device.index); | |
| 914 | } | ||
| 915 | 28 | } | |
| 916 | |||
| 917 | // ─── Allocation accounting ───────────────────────────────────────────────── | ||
| 918 | |||
| 919 | 4 | AllocStats alloc_stats() { | |
| 920 | 4 | AllocStats s; | |
| 921 | 4 | s.count = g_alloc_count.load(std::memory_order_relaxed); | |
| 922 | 4 | s.bytes = g_alloc_bytes.load(std::memory_order_relaxed); | |
| 923 | 4 | return s; | |
| 924 | } | ||
| 925 | |||
| 926 | } // namespace brotensor | ||
| 927 |