src/jit/trace_ops.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <brotensor/jit/trace.h> | ||
| 2 | #include "trace_dag.h" | ||
| 3 | #include <brotensor/ops.h> | ||
| 4 | #include "../cpu/cpu_jit.h" | ||
| 5 | |||
| 6 | #if BROTENSOR_HAS_CUDA | ||
| 7 | #include "../cuda/cuda_jit.h" | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include <cmath> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <string> | ||
| 13 | #include <vector> | ||
| 14 | #include <stdexcept> | ||
| 15 | |||
| 16 | namespace brotensor::jit { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | // Result shape of a binary op whose operands may differ in rank: the operand | ||
| 21 | // with more elements wins, so `gate * x` and `x * gate` both produce x's | ||
| 22 | // shape when gate is a (1, D) row or a (1, 1) scalar. The trace records the | ||
| 23 | // broadcast as an addressing mode on that operand; nothing is materialised. | ||
| 24 | 75 | const Tensor& binary_shape(const Tensor& a, const Tensor& b) { | |
| 25 | 150 | return (static_cast<int64_t>(b.rows) * b.cols > | |
| 26 | 75 | static_cast<int64_t>(a.rows) * a.cols) | |
| 27 | 4 | ? b | |
| 28 | 71 | : a; | |
| 29 | } | ||
| 30 | |||
| 31 | 75 | void broadcast_shape(const Tensor& a, const Tensor& b, int& rows, int& cols) { | |
| 32 | 75 | const bool ok_r = (a.rows == b.rows || a.rows == 1 || b.rows == 1); | |
| 33 | 75 | const bool ok_c = (a.cols == b.cols || a.cols == 1 || b.cols == 1); | |
| 34 | 75 | if (ok_r && ok_c) { | |
| 35 | 75 | rows = std::max(a.rows, b.rows); | |
| 36 | 75 | cols = std::max(a.cols, b.cols); | |
| 37 | 75 | } else { | |
| 38 | ✗ | const Tensor& r = binary_shape(a, b); | |
| 39 | ✗ | rows = r.rows; | |
| 40 | ✗ | cols = r.cols; | |
| 41 | } | ||
| 42 | 75 | } | |
| 43 | |||
| 44 | 2 | void broadcast_shape3(const Tensor& c, const Tensor& a, const Tensor& b, int& rows, int& cols) { | |
| 45 | 2 | int ab_r = 0, ab_c = 0; | |
| 46 | 2 | broadcast_shape(a, b, ab_r, ab_c); | |
| 47 | 4 | const bool ok_r = (c.rows == ab_r || c.rows == 1 || ab_r == 1) && | |
| 48 | 2 | (a.rows == b.rows || a.rows == 1 || b.rows == 1); | |
| 49 | 4 | const bool ok_c = (c.cols == ab_c || c.cols == 1 || ab_c == 1) && | |
| 50 | 2 | (a.cols == b.cols || a.cols == 1 || b.cols == 1); | |
| 51 | 2 | if (ok_r && ok_c) { | |
| 52 | 2 | rows = std::max(c.rows, ab_r); | |
| 53 | 2 | cols = std::max(c.cols, ab_c); | |
| 54 | 2 | } else { | |
| 55 | ✗ | const Tensor& r = binary_shape(c, binary_shape(a, b)); | |
| 56 | ✗ | rows = r.rows; | |
| 57 | ✗ | cols = r.cols; | |
| 58 | } | ||
| 59 | 2 | } | |
| 60 | |||
| 61 | ✗ | bool same_shape(const Tensor& a, const Tensor& b) { | |
| 62 | ✗ | return a.rows == b.rows && a.cols == b.cols; | |
| 63 | } | ||
| 64 | |||
| 65 | // An operand the caller did not supply — the default-constructed Tensor that | ||
| 66 | // means "no gamma" / "no beta". A symbolic intermediate also has a null | ||
| 67 | // `data`, so emptiness alone does not answer the question. | ||
| 68 | 15 | bool absent(const Tensor& t) { | |
| 69 | 15 | return t.jit_slot < 0 && t.empty(); | |
| 70 | } | ||
| 71 | |||
| 72 | ✗ | [[noreturn]] void no_eager_broadcast(const char* op) { | |
| 73 | ✗ | throw std::runtime_error( | |
| 74 | ✗ | std::string("brotensor::jit: eager ") + op + | |
| 75 | " does not broadcast; trace the expression (begin_trace/end_trace) or " | ||
| 76 | "call the shaped op directly"); | ||
| 77 | ✗ | } | |
| 78 | |||
| 79 | } // namespace | ||
| 80 | |||
| 81 | 43 | Tensor operator+(const Tensor& a, const Tensor& b) { | |
| 82 | 43 | if (is_tracing()) { | |
| 83 | 43 | auto& ctx = TraceContext::current(); | |
| 84 | 43 | int slot_a = ctx.get_or_register_slot(a); | |
| 85 | 43 | int slot_b = ctx.get_or_register_slot(b); | |
| 86 | 43 | const Tensor& r = binary_shape(a, b); | |
| 87 | 43 | int rows = r.rows, cols = r.cols; | |
| 88 | 43 | broadcast_shape(a, b, rows, cols); | |
| 89 | 86 | return ctx.record_op(TraceOpKind::Add, {slot_a, slot_b}, 0.0f, | |
| 90 | 43 | r.device, r.dtype, rows, cols); | |
| 91 | } | ||
| 92 | ✗ | if (!same_shape(a, b)) no_eager_broadcast("+"); | |
| 93 | ✗ | Tensor out = a.clone(); | |
| 94 | ✗ | brotensor::add_inplace(out, b); | |
| 95 | ✗ | return out; | |
| 96 | 43 | } | |
| 97 | |||
| 98 | 7 | Tensor operator+(const Tensor& a, float s) { | |
| 99 | 7 | if (is_tracing()) { | |
| 100 | 7 | auto& ctx = TraceContext::current(); | |
| 101 | 7 | int slot_a = ctx.get_or_register_slot(a); | |
| 102 | 14 | return ctx.record_op(TraceOpKind::AddScalar, {slot_a}, s, | |
| 103 | 7 | a.device, a.dtype, a.rows, a.cols); | |
| 104 | } | ||
| 105 | ✗ | Tensor out = a.clone(); | |
| 106 | ✗ | brotensor::add_scalar_inplace(out, s); | |
| 107 | ✗ | return out; | |
| 108 | 7 | } | |
| 109 | |||
| 110 | 2 | Tensor operator-(const Tensor& a, const Tensor& b) { | |
| 111 | 2 | if (is_tracing()) { | |
| 112 | 2 | auto& ctx = TraceContext::current(); | |
| 113 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 114 | 2 | int slot_b = ctx.get_or_register_slot(b); | |
| 115 | 2 | const Tensor& r = binary_shape(a, b); | |
| 116 | 2 | int rows = r.rows, cols = r.cols; | |
| 117 | 2 | broadcast_shape(a, b, rows, cols); | |
| 118 | 4 | return ctx.record_op(TraceOpKind::Sub, {slot_a, slot_b}, 0.0f, | |
| 119 | 2 | r.device, r.dtype, rows, cols); | |
| 120 | } | ||
| 121 | ✗ | if (!same_shape(a, b)) no_eager_broadcast("-"); | |
| 122 | ✗ | Tensor out = a.clone(); | |
| 123 | ✗ | brotensor::axpby_inplace(out, b, 1.0f, -1.0f); | |
| 124 | ✗ | return out; | |
| 125 | 2 | } | |
| 126 | |||
| 127 | ✗ | Tensor operator-(const Tensor& a, float s) { | |
| 128 | ✗ | return a + (-s); | |
| 129 | } | ||
| 130 | |||
| 131 | 1 | Tensor operator-(float s, const Tensor& a) { | |
| 132 | 1 | if (is_tracing()) { | |
| 133 | 1 | auto& ctx = TraceContext::current(); | |
| 134 | 1 | int slot_a = ctx.get_or_register_slot(a); | |
| 135 | 2 | return ctx.record_op(TraceOpKind::ScalarSub, {slot_a}, s, | |
| 136 | 1 | a.device, a.dtype, a.rows, a.cols); | |
| 137 | } | ||
| 138 | ✗ | Tensor out = a.clone(); | |
| 139 | ✗ | brotensor::scale_inplace(out, -1.0f); | |
| 140 | ✗ | brotensor::add_scalar_inplace(out, s); | |
| 141 | ✗ | return out; | |
| 142 | 1 | } | |
| 143 | |||
| 144 | ✗ | Tensor operator-(const Tensor& a) { | |
| 145 | ✗ | return neg(a); | |
| 146 | } | ||
| 147 | |||
| 148 | 26 | Tensor operator*(const Tensor& a, const Tensor& b) { | |
| 149 | 26 | if (is_tracing()) { | |
| 150 | 26 | auto& ctx = TraceContext::current(); | |
| 151 | 26 | int slot_a = ctx.get_or_register_slot(a); | |
| 152 | 26 | int slot_b = ctx.get_or_register_slot(b); | |
| 153 | 26 | const Tensor& r = binary_shape(a, b); | |
| 154 | 26 | int rows = r.rows, cols = r.cols; | |
| 155 | 26 | broadcast_shape(a, b, rows, cols); | |
| 156 | 52 | return ctx.record_op(TraceOpKind::Mul, {slot_a, slot_b}, 0.0f, | |
| 157 | 26 | r.device, r.dtype, rows, cols); | |
| 158 | } | ||
| 159 | ✗ | if (!same_shape(a, b)) { | |
| 160 | // The one eager broadcast brotensor already has a kernel for: a | ||
| 161 | // length-D row against (L, D). Anything else is an error. | ||
| 162 | ✗ | const Tensor& big = (static_cast<int64_t>(b.rows) * b.cols > | |
| 163 | ✗ | static_cast<int64_t>(a.rows) * a.cols) | |
| 164 | ✗ | ? b | |
| 165 | ✗ | : a; | |
| 166 | ✗ | const Tensor& small = (&big == &a) ? b : a; | |
| 167 | ✗ | if (small.rows == 1 && small.cols == big.cols) { | |
| 168 | ✗ | Tensor out; | |
| 169 | ✗ | brotensor::broadcast_mul(big, small, out); | |
| 170 | ✗ | return out; | |
| 171 | ✗ | } | |
| 172 | ✗ | no_eager_broadcast("*"); | |
| 173 | } | ||
| 174 | ✗ | Tensor out = a.clone(); | |
| 175 | ✗ | brotensor::mul_inplace(out, b); | |
| 176 | ✗ | return out; | |
| 177 | 26 | } | |
| 178 | |||
| 179 | 4 | Tensor operator*(const Tensor& a, float s) { | |
| 180 | 4 | if (is_tracing()) { | |
| 181 | 4 | auto& ctx = TraceContext::current(); | |
| 182 | 4 | int slot_a = ctx.get_or_register_slot(a); | |
| 183 | 8 | return ctx.record_op(TraceOpKind::MulScalar, {slot_a}, s, | |
| 184 | 4 | a.device, a.dtype, a.rows, a.cols); | |
| 185 | } | ||
| 186 | ✗ | Tensor out = a.clone(); | |
| 187 | ✗ | brotensor::scale_inplace(out, s); | |
| 188 | ✗ | return out; | |
| 189 | 4 | } | |
| 190 | |||
| 191 | 2 | Tensor operator/(const Tensor& a, const Tensor& b) { | |
| 192 | 2 | if (is_tracing()) { | |
| 193 | 2 | auto& ctx = TraceContext::current(); | |
| 194 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 195 | 2 | int slot_b = ctx.get_or_register_slot(b); | |
| 196 | 2 | const Tensor& r = binary_shape(a, b); | |
| 197 | 2 | int rows = r.rows, cols = r.cols; | |
| 198 | 2 | broadcast_shape(a, b, rows, cols); | |
| 199 | 4 | return ctx.record_op(TraceOpKind::Div, {slot_a, slot_b}, 0.0f, | |
| 200 | 2 | r.device, r.dtype, rows, cols); | |
| 201 | } | ||
| 202 | ✗ | if (!same_shape(a, b)) no_eager_broadcast("/"); | |
| 203 | ✗ | Tensor out = a.clone(); | |
| 204 | ✗ | if (a.is_host()) { | |
| 205 | ✗ | float* p_out = out.ptr(); | |
| 206 | ✗ | const float* p_b = b.ptr(); | |
| 207 | ✗ | int n = a.size(); | |
| 208 | ✗ | for (int i = 0; i < n; ++i) p_out[i] /= p_b[i]; | |
| 209 | ✗ | } else if (a.device.is_vulkan()) { | |
| 210 | ✗ | brotensor::div_inplace(out, b); | |
| 211 | ✗ | } else { | |
| 212 | #if BROTENSOR_HAS_CUDA | ||
| 213 | if (a.device.is_cuda()) { | ||
| 214 | detail::cuda::jit::launch_elementwise_div_ptx( | ||
| 215 | out.ptr(), | ||
| 216 | b.ptr(), | ||
| 217 | a.size() | ||
| 218 | ); | ||
| 219 | } else { | ||
| 220 | throw std::runtime_error("brotensor: elementwise eager div on GPU requires CUDA backend"); | ||
| 221 | } | ||
| 222 | #else | ||
| 223 | ✗ | throw std::runtime_error("brotensor: elementwise eager div on GPU requires CUDA backend"); | |
| 224 | #endif | ||
| 225 | } | ||
| 226 | ✗ | return out; | |
| 227 | 2 | } | |
| 228 | |||
| 229 | 2 | Tensor operator/(const Tensor& a, float s) { | |
| 230 | 2 | return a * (1.0f / s); | |
| 231 | } | ||
| 232 | |||
| 233 | 1 | Tensor operator/(float s, const Tensor& a) { | |
| 234 | 1 | if (is_tracing()) { | |
| 235 | 1 | auto& ctx = TraceContext::current(); | |
| 236 | 1 | int slot_a = ctx.get_or_register_slot(a); | |
| 237 | 2 | return ctx.record_op(TraceOpKind::ScalarDiv, {slot_a}, s, | |
| 238 | 1 | a.device, a.dtype, a.rows, a.cols); | |
| 239 | } | ||
| 240 | ✗ | Tensor h = a.is_host() ? a.clone() : a.to(Device::CPU); | |
| 241 | ✗ | std::vector<float> v = h.to_host_vector(); | |
| 242 | ✗ | for (float& x : v) x = s / x; | |
| 243 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 244 | 1 | } | |
| 245 | |||
| 246 | 2 | Tensor& operator+=(Tensor& a, const Tensor& b) { | |
| 247 | 2 | if (is_tracing()) { | |
| 248 | 2 | auto& ctx = TraceContext::current(); | |
| 249 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 250 | 2 | int slot_b = ctx.get_or_register_slot(b); | |
| 251 | 2 | ctx.record_inplace_op(TraceOpKind::Add, slot_a, slot_b, 0.0f, a); | |
| 252 | 2 | return a; | |
| 253 | } | ||
| 254 | ✗ | brotensor::add_inplace(a, b); | |
| 255 | ✗ | return a; | |
| 256 | 2 | } | |
| 257 | |||
| 258 | ✗ | Tensor& operator+=(Tensor& a, float s) { | |
| 259 | ✗ | if (is_tracing()) { | |
| 260 | ✗ | auto& ctx = TraceContext::current(); | |
| 261 | ✗ | int slot_a = ctx.get_or_register_slot(a); | |
| 262 | ✗ | ctx.record_inplace_op(TraceOpKind::AddScalar, slot_a, -1, s, a); | |
| 263 | ✗ | return a; | |
| 264 | } | ||
| 265 | ✗ | brotensor::add_scalar_inplace(a, s); | |
| 266 | ✗ | return a; | |
| 267 | ✗ | } | |
| 268 | |||
| 269 | ✗ | Tensor& operator*=(Tensor& a, const Tensor& b) { | |
| 270 | ✗ | if (is_tracing()) { | |
| 271 | ✗ | auto& ctx = TraceContext::current(); | |
| 272 | ✗ | int slot_a = ctx.get_or_register_slot(a); | |
| 273 | ✗ | int slot_b = ctx.get_or_register_slot(b); | |
| 274 | ✗ | ctx.record_inplace_op(TraceOpKind::Mul, slot_a, slot_b, 0.0f, a); | |
| 275 | ✗ | return a; | |
| 276 | } | ||
| 277 | ✗ | brotensor::mul_inplace(a, b); | |
| 278 | ✗ | return a; | |
| 279 | ✗ | } | |
| 280 | |||
| 281 | ✗ | Tensor& operator*=(Tensor& a, float s) { | |
| 282 | ✗ | if (is_tracing()) { | |
| 283 | ✗ | auto& ctx = TraceContext::current(); | |
| 284 | ✗ | int slot_a = ctx.get_or_register_slot(a); | |
| 285 | ✗ | ctx.record_inplace_op(TraceOpKind::MulScalar, slot_a, -1, s, a); | |
| 286 | ✗ | return a; | |
| 287 | } | ||
| 288 | ✗ | brotensor::scale_inplace(a, s); | |
| 289 | ✗ | return a; | |
| 290 | ✗ | } | |
| 291 | |||
| 292 | ✗ | Tensor neg(const Tensor& a) { | |
| 293 | ✗ | if (is_tracing()) { | |
| 294 | ✗ | auto& ctx = TraceContext::current(); | |
| 295 | ✗ | int slot_a = ctx.get_or_register_slot(a); | |
| 296 | ✗ | return ctx.record_op(TraceOpKind::Neg, {slot_a}, 0.0f, | |
| 297 | ✗ | a.device, a.dtype, a.rows, a.cols); | |
| 298 | } | ||
| 299 | ✗ | Tensor out = a.clone(); | |
| 300 | ✗ | brotensor::scale_inplace(out, -1.0f); | |
| 301 | ✗ | return out; | |
| 302 | ✗ | } | |
| 303 | |||
| 304 | 1 | Tensor abs(const Tensor& a) { | |
| 305 | 1 | if (is_tracing()) { | |
| 306 | 1 | auto& ctx = TraceContext::current(); | |
| 307 | 1 | int slot_a = ctx.get_or_register_slot(a); | |
| 308 | 2 | return ctx.record_op(TraceOpKind::Abs, {slot_a}, 0.0f, | |
| 309 | 1 | a.device, a.dtype, a.rows, a.cols); | |
| 310 | } | ||
| 311 | ✗ | std::vector<float> v = (a.is_host() ? a : a.to(Device::CPU)).to_host_vector(); | |
| 312 | ✗ | for (float& x : v) x = std::fabs(x); | |
| 313 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 314 | 1 | } | |
| 315 | |||
| 316 | 2 | Tensor exp(const Tensor& a) { | |
| 317 | 2 | if (is_tracing()) { | |
| 318 | 2 | auto& ctx = TraceContext::current(); | |
| 319 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 320 | 4 | return ctx.record_op(TraceOpKind::Exp, {slot_a}, 0.0f, | |
| 321 | 2 | a.device, a.dtype, a.rows, a.cols); | |
| 322 | } | ||
| 323 | ✗ | std::vector<float> v = (a.is_host() ? a : a.to(Device::CPU)).to_host_vector(); | |
| 324 | ✗ | for (float& x : v) x = std::exp(x); | |
| 325 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 326 | 2 | } | |
| 327 | |||
| 328 | 2 | Tensor log(const Tensor& a) { | |
| 329 | 2 | if (is_tracing()) { | |
| 330 | 2 | auto& ctx = TraceContext::current(); | |
| 331 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 332 | 4 | return ctx.record_op(TraceOpKind::Log, {slot_a}, 0.0f, | |
| 333 | 2 | a.device, a.dtype, a.rows, a.cols); | |
| 334 | } | ||
| 335 | ✗ | std::vector<float> v = (a.is_host() ? a : a.to(Device::CPU)).to_host_vector(); | |
| 336 | ✗ | for (float& x : v) x = std::log(std::max(x, 1e-12f)); | |
| 337 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 338 | 2 | } | |
| 339 | |||
| 340 | 2 | Tensor sqrt(const Tensor& a) { | |
| 341 | 2 | if (is_tracing()) { | |
| 342 | 2 | auto& ctx = TraceContext::current(); | |
| 343 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 344 | 4 | return ctx.record_op(TraceOpKind::Sqrt, {slot_a}, 0.0f, | |
| 345 | 2 | a.device, a.dtype, a.rows, a.cols); | |
| 346 | } | ||
| 347 | ✗ | std::vector<float> v = (a.is_host() ? a : a.to(Device::CPU)).to_host_vector(); | |
| 348 | ✗ | for (float& x : v) x = std::sqrt(std::max(x, 0.0f)); | |
| 349 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 350 | 2 | } | |
| 351 | |||
| 352 | 2 | Tensor rsqrt(const Tensor& a) { | |
| 353 | 2 | if (is_tracing()) { | |
| 354 | 2 | auto& ctx = TraceContext::current(); | |
| 355 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 356 | 4 | return ctx.record_op(TraceOpKind::Rsqrt, {slot_a}, 0.0f, | |
| 357 | 2 | a.device, a.dtype, a.rows, a.cols); | |
| 358 | } | ||
| 359 | ✗ | std::vector<float> v = (a.is_host() ? a : a.to(Device::CPU)).to_host_vector(); | |
| 360 | ✗ | for (float& x : v) x = 1.0f / std::sqrt(std::max(x, 1e-12f)); | |
| 361 | ✗ | return Tensor::from_host_on(a.device, v.data(), a.rows, a.cols); | |
| 362 | 2 | } | |
| 363 | |||
| 364 | 20 | Tensor silu(const Tensor& a) { | |
| 365 | 20 | if (is_tracing()) { | |
| 366 | 20 | auto& ctx = TraceContext::current(); | |
| 367 | 20 | int slot_a = ctx.get_or_register_slot(a); | |
| 368 | 40 | return ctx.record_op(TraceOpKind::SiLU, {slot_a}, 0.0f, | |
| 369 | 20 | a.device, a.dtype, a.rows, a.cols); | |
| 370 | } | ||
| 371 | ✗ | Tensor out = Tensor::empty_on(a.device, a.rows, a.cols, a.dtype); | |
| 372 | ✗ | brotensor::silu_forward(a, out); | |
| 373 | ✗ | return out; | |
| 374 | 20 | } | |
| 375 | |||
| 376 | 3 | Tensor gelu(const Tensor& a) { | |
| 377 | 3 | if (is_tracing()) { | |
| 378 | 3 | auto& ctx = TraceContext::current(); | |
| 379 | 3 | int slot_a = ctx.get_or_register_slot(a); | |
| 380 | 6 | return ctx.record_op(TraceOpKind::GELU, {slot_a}, 0.0f, | |
| 381 | 3 | a.device, a.dtype, a.rows, a.cols); | |
| 382 | } | ||
| 383 | ✗ | Tensor out = Tensor::empty_on(a.device, a.rows, a.cols, a.dtype); | |
| 384 | ✗ | brotensor::gelu_forward(a, out); | |
| 385 | ✗ | return out; | |
| 386 | 3 | } | |
| 387 | |||
| 388 | 2 | Tensor relu(const Tensor& a) { | |
| 389 | 2 | if (is_tracing()) { | |
| 390 | 2 | auto& ctx = TraceContext::current(); | |
| 391 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 392 | 4 | return ctx.record_op(TraceOpKind::ReLU, {slot_a}, 0.0f, | |
| 393 | 2 | a.device, a.dtype, a.rows, a.cols); | |
| 394 | } | ||
| 395 | ✗ | Tensor out = Tensor::empty_on(a.device, a.rows, a.cols, a.dtype); | |
| 396 | ✗ | brotensor::relu_forward(a, out); | |
| 397 | ✗ | return out; | |
| 398 | 2 | } | |
| 399 | |||
| 400 | 1 | void store(Tensor& dst, const Tensor& src) { | |
| 401 | 1 | if (is_tracing()) { | |
| 402 | 1 | auto& ctx = TraceContext::current(); | |
| 403 | 1 | int slot_src = ctx.get_or_register_slot(src); | |
| 404 | 1 | ctx.record_store(slot_src, dst); | |
| 405 | 1 | return; | |
| 406 | } | ||
| 407 | ✗ | if (dst.rows != src.rows || dst.cols != src.cols || dst.dtype != src.dtype) { | |
| 408 | ✗ | throw std::runtime_error("brotensor::jit: store() needs dst and src to agree on " | |
| 409 | "shape and dtype"); | ||
| 410 | } | ||
| 411 | ✗ | brotensor::copy_d2d(src, 0, dst, 0, src.size()); | |
| 412 | 1 | } | |
| 413 | |||
| 414 | 3 | Tensor tanh(const Tensor& a) { | |
| 415 | 3 | if (is_tracing()) { | |
| 416 | 3 | auto& ctx = TraceContext::current(); | |
| 417 | 3 | int slot_a = ctx.get_or_register_slot(a); | |
| 418 | 6 | return ctx.record_op(TraceOpKind::Tanh, {slot_a}, 0.0f, | |
| 419 | 3 | a.device, a.dtype, a.rows, a.cols); | |
| 420 | } | ||
| 421 | ✗ | Tensor out = Tensor::empty_on(a.device, a.rows, a.cols, a.dtype); | |
| 422 | ✗ | brotensor::tanh_forward(a, out); | |
| 423 | ✗ | return out; | |
| 424 | 3 | } | |
| 425 | |||
| 426 | 3 | Tensor sigmoid(const Tensor& a) { | |
| 427 | 3 | if (is_tracing()) { | |
| 428 | 3 | auto& ctx = TraceContext::current(); | |
| 429 | 3 | int slot_a = ctx.get_or_register_slot(a); | |
| 430 | 6 | return ctx.record_op(TraceOpKind::Sigmoid, {slot_a}, 0.0f, | |
| 431 | 3 | a.device, a.dtype, a.rows, a.cols); | |
| 432 | } | ||
| 433 | ✗ | Tensor out = Tensor::empty_on(a.device, a.rows, a.cols, a.dtype); | |
| 434 | ✗ | brotensor::sigmoid_forward(a, out); | |
| 435 | ✗ | return out; | |
| 436 | 3 | } | |
| 437 | |||
| 438 | 5 | Tensor rms_norm(const Tensor& x, const Tensor& gamma, float eps) { | |
| 439 | 5 | if (is_tracing()) { | |
| 440 | 5 | auto& ctx = TraceContext::current(); | |
| 441 | 5 | int slot_x = ctx.get_or_register_slot(x); | |
| 442 | 5 | std::vector<int> inputs = {slot_x}; | |
| 443 | 5 | if (!absent(gamma)) { | |
| 444 | 5 | inputs.push_back(ctx.get_or_register_slot(gamma)); | |
| 445 | 5 | } | |
| 446 | 10 | return ctx.record_op(TraceOpKind::RMSNorm, inputs, eps, | |
| 447 | 5 | x.device, x.dtype, x.rows, x.cols); | |
| 448 | 5 | } | |
| 449 | |||
| 450 | ✗ | Tensor g = gamma; | |
| 451 | ✗ | if (g.empty()) { | |
| 452 | ✗ | std::vector<float> ones(x.cols, 1.0f); | |
| 453 | ✗ | g = Tensor::from_host_on(x.device, ones.data(), 1, x.cols); | |
| 454 | ✗ | } | |
| 455 | ✗ | Tensor out = Tensor::empty_on(x.device, x.rows, x.cols, x.dtype); | |
| 456 | ✗ | brotensor::rms_norm_forward(x, g, eps, out); | |
| 457 | ✗ | return out; | |
| 458 | 5 | } | |
| 459 | |||
| 460 | 5 | Tensor layernorm(const Tensor& x, const Tensor& gamma, const Tensor& beta, float eps) { | |
| 461 | 5 | if (is_tracing()) { | |
| 462 | 5 | auto& ctx = TraceContext::current(); | |
| 463 | 5 | int slot_x = ctx.get_or_register_slot(x); | |
| 464 | 5 | std::vector<int> inputs = {slot_x}; | |
| 465 | 5 | if (!absent(gamma)) inputs.push_back(ctx.get_or_register_slot(gamma)); | |
| 466 | 5 | if (!absent(beta)) inputs.push_back(ctx.get_or_register_slot(beta)); | |
| 467 | 10 | return ctx.record_op(TraceOpKind::LayerNorm, inputs, eps, | |
| 468 | 5 | x.device, x.dtype, x.rows, x.cols); | |
| 469 | 5 | } | |
| 470 | |||
| 471 | ✗ | Tensor g = gamma; | |
| 472 | ✗ | if (g.empty()) { | |
| 473 | ✗ | std::vector<float> ones(x.cols, 1.0f); | |
| 474 | ✗ | g = Tensor::from_host_on(x.device, ones.data(), 1, x.cols); | |
| 475 | ✗ | } | |
| 476 | ✗ | Tensor b = beta; | |
| 477 | ✗ | if (b.empty()) { | |
| 478 | ✗ | b = Tensor::zeros_on(x.device, 1, x.cols); | |
| 479 | ✗ | } | |
| 480 | ✗ | Tensor out = Tensor::empty_on(x.device, x.rows, x.cols, x.dtype); | |
| 481 | ✗ | brotensor::layernorm_forward_inference_batched(x, g, b, out, eps); | |
| 482 | ✗ | return out; | |
| 483 | 5 | } | |
| 484 | |||
| 485 | 1 | Tensor modulate(const Tensor& x, const Tensor& scale, const Tensor& shift) { | |
| 486 | 1 | if (is_tracing()) { | |
| 487 | 1 | auto& ctx = TraceContext::current(); | |
| 488 | 1 | int slot_x = ctx.get_or_register_slot(x); | |
| 489 | 1 | int slot_s = ctx.get_or_register_slot(scale); | |
| 490 | 1 | int slot_sh = ctx.get_or_register_slot(shift); | |
| 491 | 2 | return ctx.record_op(TraceOpKind::Modulate, {slot_x, slot_s, slot_sh}, 0.0f, | |
| 492 | 1 | x.device, x.dtype, x.rows, x.cols); | |
| 493 | } | ||
| 494 | |||
| 495 | ✗ | Tensor out = Tensor::empty_on(x.device, x.rows, x.cols, x.dtype); | |
| 496 | ✗ | if (x.is_host()) { | |
| 497 | ✗ | detail::cpu::jit::modulate(x.ptr(), scale.ptr(), shift.ptr(), out.ptr(), x.rows, x.cols); | |
| 498 | ✗ | } else if (x.device.is_vulkan()) { | |
| 499 | ✗ | brotensor::modulate(x, scale, shift, out); | |
| 500 | ✗ | } else { | |
| 501 | #if BROTENSOR_HAS_CUDA | ||
| 502 | detail::cuda::jit::launch_modulate_ptx( | ||
| 503 | static_cast<const float*>(x.data), | ||
| 504 | static_cast<const float*>(scale.data), | ||
| 505 | static_cast<const float*>(shift.data), | ||
| 506 | static_cast<float*>(out.data), | ||
| 507 | x.rows, x.cols | ||
| 508 | ); | ||
| 509 | #else | ||
| 510 | ✗ | throw std::runtime_error("brotensor: modulate on GPU requires CUDA backend"); | |
| 511 | #endif | ||
| 512 | } | ||
| 513 | ✗ | return out; | |
| 514 | 1 | } | |
| 515 | |||
| 516 | 2 | Tensor where(const Tensor& cond, const Tensor& a, const Tensor& b) { | |
| 517 | 2 | if (is_tracing()) { | |
| 518 | 2 | auto& ctx = TraceContext::current(); | |
| 519 | 2 | int slot_c = ctx.get_or_register_slot(cond); | |
| 520 | 2 | int slot_a = ctx.get_or_register_slot(a); | |
| 521 | 2 | int slot_b = ctx.get_or_register_slot(b); | |
| 522 | 2 | const Tensor& r = binary_shape(a, b); | |
| 523 | 2 | int rows = r.rows, cols = r.cols; | |
| 524 | 2 | broadcast_shape3(cond, a, b, rows, cols); | |
| 525 | 4 | return ctx.record_op(TraceOpKind::Where, {slot_c, slot_a, slot_b}, 0.0f, | |
| 526 | 2 | r.device, r.dtype, rows, cols); | |
| 527 | } | ||
| 528 | |||
| 529 | ✗ | if (cond.device != a.device || a.device != b.device) { | |
| 530 | ✗ | throw std::runtime_error("brotensor::jit::where: device mismatch between operands"); | |
| 531 | } | ||
| 532 | ✗ | int rows = 0, cols = 0; | |
| 533 | ✗ | broadcast_shape3(cond, a, b, rows, cols); | |
| 534 | ✗ | auto valid_bcast = [&](const Tensor& t) { | |
| 535 | ✗ | if (t.rows == rows && t.cols == cols) return true; | |
| 536 | ✗ | const BroadcastKind bk = broadcast_of(t.rows, t.cols, rows, cols); | |
| 537 | ✗ | return bk != BroadcastKind::Full; | |
| 538 | ✗ | }; | |
| 539 | ✗ | if (!valid_bcast(cond) || !valid_bcast(a) || !valid_bcast(b)) { | |
| 540 | ✗ | throw std::runtime_error("brotensor::jit::where: incompatible operand shapes"); | |
| 541 | } | ||
| 542 | |||
| 543 | ✗ | const Tensor& r = binary_shape(a, b); | |
| 544 | ✗ | auto to_host_f32 = [](const Tensor& t) -> std::vector<float> { | |
| 545 | ✗ | Tensor h = t.is_host() ? Tensor::view(Device::CPU, const_cast<void*>(t.data), t.rows, t.cols, t.dtype) | |
| 546 | ✗ | : t.to(Device::CPU); | |
| 547 | ✗ | if (h.dtype != Dtype::FP32) { | |
| 548 | ✗ | Tensor c; | |
| 549 | ✗ | brotensor::cast(h, c, Dtype::FP32); | |
| 550 | ✗ | return c.to_host_vector(); | |
| 551 | ✗ | } | |
| 552 | ✗ | return h.to_host_vector(); | |
| 553 | ✗ | }; | |
| 554 | ✗ | const std::vector<float> vc = to_host_f32(cond); | |
| 555 | ✗ | const std::vector<float> va = to_host_f32(a); | |
| 556 | ✗ | const std::vector<float> vb = to_host_f32(b); | |
| 557 | |||
| 558 | ✗ | auto idx_of = [cols](const Tensor& t, int row, int col) -> std::size_t { | |
| 559 | ✗ | if (t.rows == 1 && t.cols == 1) return 0; | |
| 560 | ✗ | if (t.rows == 1) return static_cast<std::size_t>(col); | |
| 561 | ✗ | if (t.cols == 1) return static_cast<std::size_t>(row); | |
| 562 | ✗ | return static_cast<std::size_t>(row) * cols + col; | |
| 563 | ✗ | }; | |
| 564 | |||
| 565 | ✗ | std::vector<float> vout(static_cast<std::size_t>(rows) * cols); | |
| 566 | ✗ | for (int row = 0; row < rows; ++row) { | |
| 567 | ✗ | for (int col = 0; col < cols; ++col) { | |
| 568 | ✗ | const float c_val = vc[idx_of(cond, row, col)]; | |
| 569 | ✗ | const float a_val = va[idx_of(a, row, col)]; | |
| 570 | ✗ | const float b_val = vb[idx_of(b, row, col)]; | |
| 571 | ✗ | vout[static_cast<std::size_t>(row) * cols + col] = (c_val != 0.0f) ? a_val : b_val; | |
| 572 | ✗ | } | |
| 573 | ✗ | } | |
| 574 | |||
| 575 | ✗ | Tensor out_f32 = Tensor::from_host_on(r.device, vout.data(), rows, cols); | |
| 576 | ✗ | if (r.dtype == Dtype::FP32) return out_f32; | |
| 577 | ✗ | Tensor out; | |
| 578 | ✗ | brotensor::cast(out_f32, out, r.dtype); | |
| 579 | ✗ | return out; | |
| 580 | 2 | } | |
| 581 | |||
| 582 | 2 | Tensor clamp(const Tensor& x, float lo, float hi) { | |
| 583 | 2 | if (is_tracing()) { | |
| 584 | 2 | auto& ctx = TraceContext::current(); | |
| 585 | 2 | int slot_x = ctx.get_or_register_slot(x); | |
| 586 | 4 | return ctx.record_op(TraceOpKind::Clamp, {slot_x}, lo, | |
| 587 | 2 | x.device, x.dtype, x.rows, x.cols, hi); | |
| 588 | } | ||
| 589 | ✗ | Tensor out = x.clone(); | |
| 590 | ✗ | brotensor::clamp(out, lo, hi); | |
| 591 | ✗ | return out; | |
| 592 | 2 | } | |
| 593 | |||
| 594 | 1 | Tensor cast(const Tensor& x, Dtype out_dtype) { | |
| 595 | 1 | if (is_tracing()) { | |
| 596 | 1 | auto& ctx = TraceContext::current(); | |
| 597 | 1 | int slot_x = ctx.get_or_register_slot(x); | |
| 598 | 2 | return ctx.record_op(TraceOpKind::Cast, {slot_x}, 0.0f, | |
| 599 | 1 | x.device, out_dtype, x.rows, x.cols); | |
| 600 | } | ||
| 601 | ✗ | Tensor out; | |
| 602 | ✗ | brotensor::cast(x, out, out_dtype); | |
| 603 | ✗ | return out; | |
| 604 | 1 | } | |
| 605 | |||
| 606 | } // namespace brotensor::jit | ||
| 607 |