src/jit/trace_dag.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "trace_dag.h" | ||
| 2 | #include <cstring> | ||
| 3 | #include <stdexcept> | ||
| 4 | |||
| 5 | namespace brotensor::jit { | ||
| 6 | |||
| 7 | ✗ | const char* op_kind_name(TraceOpKind op) { | |
| 8 | ✗ | switch (op) { | |
| 9 | ✗ | case TraceOpKind::Input: return "Input"; | |
| 10 | ✗ | case TraceOpKind::Add: return "Add"; | |
| 11 | ✗ | case TraceOpKind::Sub: return "Sub"; | |
| 12 | ✗ | case TraceOpKind::Mul: return "Mul"; | |
| 13 | ✗ | case TraceOpKind::Div: return "Div"; | |
| 14 | ✗ | case TraceOpKind::AddScalar: return "AddScalar"; | |
| 15 | ✗ | case TraceOpKind::SubScalar: return "SubScalar"; | |
| 16 | ✗ | case TraceOpKind::MulScalar: return "MulScalar"; | |
| 17 | ✗ | case TraceOpKind::DivScalar: return "DivScalar"; | |
| 18 | ✗ | case TraceOpKind::ScalarSub: return "ScalarSub"; | |
| 19 | ✗ | case TraceOpKind::ScalarDiv: return "ScalarDiv"; | |
| 20 | ✗ | case TraceOpKind::FMA: return "FMA"; | |
| 21 | ✗ | case TraceOpKind::SiLU: return "SiLU"; | |
| 22 | ✗ | case TraceOpKind::GELU: return "GELU"; | |
| 23 | ✗ | case TraceOpKind::ReLU: return "ReLU"; | |
| 24 | ✗ | case TraceOpKind::RMSNorm: return "RMSNorm"; | |
| 25 | ✗ | case TraceOpKind::LayerNorm: return "LayerNorm"; | |
| 26 | ✗ | case TraceOpKind::Modulate: return "Modulate"; | |
| 27 | ✗ | case TraceOpKind::Tanh: return "Tanh"; | |
| 28 | ✗ | case TraceOpKind::Sigmoid: return "Sigmoid"; | |
| 29 | ✗ | case TraceOpKind::Copy: return "Copy"; | |
| 30 | ✗ | case TraceOpKind::Where: return "Where"; | |
| 31 | ✗ | case TraceOpKind::Clamp: return "Clamp"; | |
| 32 | ✗ | case TraceOpKind::Cast: return "Cast"; | |
| 33 | ✗ | case TraceOpKind::Const: return "Const"; | |
| 34 | ✗ | case TraceOpKind::Neg: return "Neg"; | |
| 35 | ✗ | case TraceOpKind::Exp: return "Exp"; | |
| 36 | ✗ | case TraceOpKind::Log: return "Log"; | |
| 37 | ✗ | case TraceOpKind::Sqrt: return "Sqrt"; | |
| 38 | ✗ | case TraceOpKind::Rsqrt: return "Rsqrt"; | |
| 39 | ✗ | case TraceOpKind::Abs: return "Abs"; | |
| 40 | } | ||
| 41 | ✗ | return "Unknown"; | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | 1191 | BroadcastKind broadcast_of(int node_rows, int node_cols, int rows, int cols) { | |
| 45 | 1191 | if (node_rows == 1 && node_cols == 1 && rows * cols != 1) { | |
| 46 | 6 | return BroadcastKind::Scalar; | |
| 47 | } | ||
| 48 | 1185 | if (node_rows == 1 && node_cols == cols && rows != 1) { | |
| 49 | 78 | return BroadcastKind::Row; | |
| 50 | } | ||
| 51 | 1107 | if (node_rows == rows && node_cols == 1 && cols != 1) { | |
| 52 | 54 | return BroadcastKind::Col; | |
| 53 | } | ||
| 54 | // Anything else is addressed linearly. A node whose element count does not | ||
| 55 | // match the dominant shape is rejected by the compiler before it gets | ||
| 56 | // here (see trace_compiler_cuda.cpp's plan builder). | ||
| 57 | 1053 | return BroadcastKind::Full; | |
| 58 | 1191 | } | |
| 59 | |||
| 60 | 44 | bool TraceDAG::slot_matches(int slot, const Tensor& t) const { | |
| 61 | 44 | if (slot < 0 || slot >= static_cast<int>(nodes_.size())) return false; | |
| 62 | 44 | const TraceNode& n = nodes_[static_cast<std::size_t>(slot)]; | |
| 63 | 88 | return n.buffer == t.data && n.rows == t.rows && n.cols == t.cols && | |
| 64 | 44 | n.dtype == t.dtype && n.device == t.device; | |
| 65 | 44 | } | |
| 66 | |||
| 67 | 111 | int TraceDAG::register_input(const Tensor& t) { | |
| 68 | 111 | auto it = input_ptr_to_slot_.find(t.data); | |
| 69 | 111 | if (it != input_ptr_to_slot_.end() && slot_matches(it->second, t)) { | |
| 70 | ✗ | return it->second; | |
| 71 | } | ||
| 72 | |||
| 73 | 111 | int id = static_cast<int>(nodes_.size()); | |
| 74 | 111 | TraceNode node; | |
| 75 | 111 | node.id = id; | |
| 76 | 111 | node.op = TraceOpKind::Input; | |
| 77 | 111 | node.buffer = t.data; | |
| 78 | 111 | node.device = t.device; | |
| 79 | 111 | node.dtype = t.dtype; | |
| 80 | 111 | node.rows = t.rows; | |
| 81 | 111 | node.cols = t.cols; | |
| 82 | 111 | node.is_inplace = false; | |
| 83 | 111 | node.is_live_at_end = false; | |
| 84 | |||
| 85 | 111 | nodes_.push_back(std::move(node)); | |
| 86 | 111 | input_ptr_to_slot_[t.data] = id; | |
| 87 | 111 | return id; | |
| 88 | 111 | } | |
| 89 | |||
| 90 | 3878 | int TraceDAG::add_node(TraceOpKind op, const std::vector<int>& inputs, float scalar, | |
| 91 | void* buffer, const Device& dev, Dtype dt, int rows, int cols, | ||
| 92 | float scalar2) { | ||
| 93 | 3878 | int id = static_cast<int>(nodes_.size()); | |
| 94 | 3878 | TraceNode node; | |
| 95 | 3878 | node.id = id; | |
| 96 | 3878 | node.op = op; | |
| 97 | 3878 | node.inputs = inputs; | |
| 98 | 3878 | node.scalar = scalar; | |
| 99 | 3878 | node.scalar2 = scalar2; | |
| 100 | 3878 | node.buffer = buffer; | |
| 101 | 3878 | node.device = dev; | |
| 102 | 3878 | node.dtype = dt; | |
| 103 | 3878 | node.rows = rows; | |
| 104 | 3878 | node.cols = cols; | |
| 105 | 3878 | node.is_inplace = false; | |
| 106 | 3878 | node.is_live_at_end = false; | |
| 107 | |||
| 108 | 3878 | nodes_.push_back(std::move(node)); | |
| 109 | 3878 | return id; | |
| 110 | 3878 | } | |
| 111 | |||
| 112 | 3 | int TraceDAG::add_inplace_node(TraceOpKind op, int target_slot, int arg_slot, | |
| 113 | void* buffer, float scalar, float scalar2) { | ||
| 114 | 3 | int id = static_cast<int>(nodes_.size()); | |
| 115 | 3 | const auto& target_node = nodes_.at(target_slot); | |
| 116 | |||
| 117 | 3 | TraceNode node; | |
| 118 | 3 | node.id = id; | |
| 119 | 3 | node.op = op; | |
| 120 | 3 | if (arg_slot >= 0) { | |
| 121 | 2 | node.inputs = {target_slot, arg_slot}; | |
| 122 | 2 | } else { | |
| 123 | 1 | node.inputs = {target_slot}; | |
| 124 | } | ||
| 125 | 3 | node.scalar = scalar; | |
| 126 | 3 | node.scalar2 = scalar2; | |
| 127 | 3 | node.buffer = buffer; | |
| 128 | 3 | node.device = target_node.device; | |
| 129 | 3 | node.dtype = target_node.dtype; | |
| 130 | 3 | node.rows = target_node.rows; | |
| 131 | 3 | node.cols = target_node.cols; | |
| 132 | 3 | node.is_inplace = true; | |
| 133 | 3 | node.inplace_target_slot = target_slot; | |
| 134 | 3 | node.is_live_at_end = true; | |
| 135 | |||
| 136 | 3 | nodes_.push_back(std::move(node)); | |
| 137 | 3 | return id; | |
| 138 | 3 | } | |
| 139 | |||
| 140 | 35 | void TraceDAG::analyze_liveness() { | |
| 141 | 289 | for (auto& n : nodes_) { | |
| 142 | 254 | n.outputs.clear(); | |
| 143 | } | ||
| 144 | 289 | for (const auto& n : nodes_) { | |
| 145 | 496 | for (int in_id : n.inputs) { | |
| 146 | 242 | if (in_id >= 0 && in_id < static_cast<int>(nodes_.size())) { | |
| 147 | 242 | nodes_[in_id].outputs.push_back(n.id); | |
| 148 | 242 | } | |
| 149 | } | ||
| 150 | } | ||
| 151 | 289 | for (auto& n : nodes_) { | |
| 152 | 254 | if (n.op == TraceOpKind::Input) { | |
| 153 | 108 | n.is_live_at_end = false; | |
| 154 | 254 | } else if (n.is_inplace) { | |
| 155 | 3 | n.is_live_at_end = true; | |
| 156 | 146 | } else if (n.outputs.empty()) { | |
| 157 | 34 | n.is_live_at_end = true; | |
| 158 | 34 | } else { | |
| 159 | 109 | n.is_live_at_end = false; | |
| 160 | } | ||
| 161 | } | ||
| 162 | 35 | } | |
| 163 | |||
| 164 | 144 | void TraceDAG::dominant_shape(int& rows, int& cols) const { | |
| 165 | 144 | rows = 0; | |
| 166 | 144 | cols = 0; | |
| 167 | 144 | int64_t best = 0; | |
| 168 | 3429 | for (const auto& n : nodes_) { | |
| 169 | 3285 | const int64_t numel = static_cast<int64_t>(n.rows) * static_cast<int64_t>(n.cols); | |
| 170 | 3285 | if (numel > best) { | |
| 171 | 146 | best = numel; | |
| 172 | 146 | rows = n.rows; | |
| 173 | 146 | cols = n.cols; | |
| 174 | 146 | } | |
| 175 | } | ||
| 176 | 144 | } | |
| 177 | |||
| 178 | 53 | uint64_t TraceDAG::compute_hash() const { | |
| 179 | // 64-bit FNV-1a hash | ||
| 180 | 53 | uint64_t hash = 14695981039346656037ULL; | |
| 181 | 12188 | auto hash_val = [&hash](uint64_t val) { | |
| 182 | 12135 | hash ^= val; | |
| 183 | 12135 | hash *= 1099511628211ULL; | |
| 184 | 12135 | }; | |
| 185 | |||
| 186 | 53 | hash_val(nodes_.size()); | |
| 187 | 1042 | for (const auto& n : nodes_) { | |
| 188 | 989 | hash_val(static_cast<uint64_t>(n.op)); | |
| 189 | 989 | hash_val(static_cast<uint64_t>(n.device.type)); | |
| 190 | 989 | hash_val(static_cast<uint64_t>(n.device.index)); | |
| 191 | 989 | hash_val(static_cast<uint64_t>(n.dtype)); | |
| 192 | 989 | hash_val(static_cast<uint64_t>(n.rows)); | |
| 193 | 989 | hash_val(static_cast<uint64_t>(n.cols)); | |
| 194 | 989 | hash_val(n.is_inplace ? 1ULL : 0ULL); | |
| 195 | 989 | hash_val(n.is_live_at_end ? 1ULL : 0ULL); | |
| 196 | |||
| 197 | 989 | uint32_t scalar_bits = 0; | |
| 198 | 989 | std::memcpy(&scalar_bits, &n.scalar, sizeof(float)); | |
| 199 | 989 | hash_val(scalar_bits); | |
| 200 | |||
| 201 | 989 | uint32_t scalar2_bits = 0; | |
| 202 | 989 | std::memcpy(&scalar2_bits, &n.scalar2, sizeof(float)); | |
| 203 | 989 | hash_val(scalar2_bits); | |
| 204 | |||
| 205 | 989 | hash_val(n.inputs.size()); | |
| 206 | 2192 | for (int in_id : n.inputs) { | |
| 207 | 1203 | hash_val(static_cast<uint64_t>(in_id)); | |
| 208 | } | ||
| 209 | } | ||
| 210 | 53 | return hash; | |
| 211 | } | ||
| 212 | |||
| 213 | 46 | std::string TraceDAG::key_material() const { | |
| 214 | 46 | std::string out; | |
| 215 | 46 | out.reserve(16 + nodes_.size() * 64); | |
| 216 | 14858 | auto put = [&out](uint64_t v, int bytes) { | |
| 217 | 66135 | for (int i = 0; i < bytes; ++i) out.push_back(static_cast<char>((v >> (8 * i)) & 0xFFu)); | |
| 218 | 14812 | }; | |
| 219 | 1848 | auto put_f32 = [&put](float f) { | |
| 220 | 1802 | uint32_t bits = 0; | |
| 221 | 1802 | std::memcpy(&bits, &f, sizeof(bits)); | |
| 222 | 1802 | put(bits, 4); | |
| 223 | 1802 | }; | |
| 224 | 46 | put(nodes_.size(), 8); | |
| 225 | 947 | for (const auto& n : nodes_) { | |
| 226 | 901 | put(static_cast<uint32_t>(n.id), 4); | |
| 227 | 901 | put(static_cast<uint64_t>(n.op), 1); | |
| 228 | 901 | put(static_cast<uint64_t>(n.device.type), 4); | |
| 229 | 901 | put(static_cast<uint32_t>(n.device.index), 4); | |
| 230 | 901 | put(static_cast<uint64_t>(n.dtype), 4); | |
| 231 | 901 | put(static_cast<uint32_t>(n.rows), 4); | |
| 232 | 901 | put(static_cast<uint32_t>(n.cols), 4); | |
| 233 | 901 | put(n.is_inplace ? 1u : 0u, 1); | |
| 234 | 901 | put(static_cast<uint32_t>(n.inplace_target_slot), 4); | |
| 235 | 901 | put(n.is_live_at_end ? 1u : 0u, 1); | |
| 236 | 901 | put_f32(n.scalar); | |
| 237 | 901 | put_f32(n.scalar2); | |
| 238 | 901 | put(n.inputs.size(), 4); | |
| 239 | 2004 | for (int in_id : n.inputs) put(static_cast<uint32_t>(in_id), 4); | |
| 240 | 901 | put(n.outputs.size(), 4); | |
| 241 | 1950 | for (int out_id : n.outputs) put(static_cast<uint32_t>(out_id), 4); | |
| 242 | } | ||
| 243 | 46 | return out; | |
| 244 | 46 | } | |
| 245 | |||
| 246 | 38 | void TraceDAG::reset() { | |
| 247 | 38 | nodes_.clear(); | |
| 248 | 38 | input_ptr_to_slot_.clear(); | |
| 249 | 38 | } | |
| 250 | |||
| 251 | 556 | TraceContext& TraceContext::current() { | |
| 252 | 556 | static thread_local TraceContext instance; | |
| 253 | 556 | return instance; | |
| 254 | } | ||
| 255 | |||
| 256 | namespace { | ||
| 257 | |||
| 258 | // The hook Tensor calls from its move ctor / move assignment / destructor. | ||
| 259 | // Installed by begin(), which is the earliest point at which a symbolic | ||
| 260 | // tensor can exist, so there is no static-initialisation order to get wrong. | ||
| 261 | 113 | void slot_retarget(int slot, const Tensor* from, Tensor* to) { | |
| 262 | 113 | TraceContext::current().retarget(slot, from, to); | |
| 263 | 113 | } | |
| 264 | |||
| 265 | 1 | bool clamp_inplace_hook(Tensor& y, float lo, float hi) { | |
| 266 | 1 | auto& ctx = TraceContext::current(); | |
| 267 | 1 | if (!ctx.is_active()) return false; | |
| 268 | 1 | const int slot_y = ctx.get_or_register_slot(y); | |
| 269 | 1 | ctx.record_inplace_op(TraceOpKind::Clamp, slot_y, -1, lo, y, hi); | |
| 270 | 1 | return true; | |
| 271 | 1 | } | |
| 272 | |||
| 273 | 47 | bool cast_out_hook(const Tensor& src, Tensor& dst, Dtype out_dtype) { | |
| 274 | 47 | auto& ctx = TraceContext::current(); | |
| 275 | 47 | if (!ctx.is_active()) return false; | |
| 276 | 1 | const int slot_src = ctx.get_or_register_slot(src); | |
| 277 | 1 | ctx.record_cast_out(slot_src, src, dst, out_dtype); | |
| 278 | 1 | return true; | |
| 279 | 47 | } | |
| 280 | |||
| 281 | } // namespace | ||
| 282 | |||
| 283 | 113 | void TraceContext::retarget(int slot, const Tensor* from, Tensor* to) noexcept { | |
| 284 | 113 | auto it = slot_to_tensor_.find(slot); | |
| 285 | // A stale entry — the slot was already materialised or re-tracked — must | ||
| 286 | // not be clobbered by a tensor that no longer stands for it. | ||
| 287 | 113 | if (it == slot_to_tensor_.end() || it->second != from) return; | |
| 288 | 113 | if (to) { | |
| 289 | 8 | it->second = to; | |
| 290 | 8 | } else { | |
| 291 | 105 | slot_to_tensor_.erase(it); | |
| 292 | } | ||
| 293 | 113 | } | |
| 294 | |||
| 295 | 72 | void TraceContext::release_tracked_() noexcept { | |
| 296 | 78 | for (auto& kv : slot_to_tensor_) { | |
| 297 | 6 | Tensor* t = kv.second; | |
| 298 | 6 | if (!t) continue; | |
| 299 | // It never owned anything, and its value was never written anywhere, | ||
| 300 | // so the honest result is an empty tensor rather than a shape with no | ||
| 301 | // storage behind it. | ||
| 302 | 6 | t->jit_slot = -1; | |
| 303 | 6 | t->rows = 0; | |
| 304 | 6 | t->cols = 0; | |
| 305 | } | ||
| 306 | 72 | slot_to_tensor_.clear(); | |
| 307 | 72 | } | |
| 308 | |||
| 309 | 36 | void TraceContext::begin() { | |
| 310 | 36 | if (active_) { | |
| 311 | ✗ | throw std::runtime_error("brotensor::jit: Nested begin_trace() calls are not permitted"); | |
| 312 | } | ||
| 313 | 36 | detail::jit_slot_retarget = &slot_retarget; | |
| 314 | 36 | detail::jit_clamp_inplace_hook = &clamp_inplace_hook; | |
| 315 | 36 | detail::jit_cast_out_hook = &cast_out_hook; | |
| 316 | 36 | dag_.reset(); | |
| 317 | 36 | active_ptr_to_slot_.clear(); | |
| 318 | 36 | release_tracked_(); | |
| 319 | 36 | active_ = true; | |
| 320 | 36 | } | |
| 321 | |||
| 322 | 2 | void TraceContext::discard() { | |
| 323 | 2 | dag_.reset(); | |
| 324 | 2 | active_ptr_to_slot_.clear(); | |
| 325 | 2 | release_tracked_(); | |
| 326 | 2 | active_ = false; | |
| 327 | 2 | } | |
| 328 | |||
| 329 | 34 | void TraceContext::materialize_() { | |
| 330 | 284 | for (TraceNode& n : dag_.nodes()) { | |
| 331 | 250 | if (!n.is_live_at_end) continue; | |
| 332 | 36 | if (n.buffer != nullptr) continue; // caller already owns the storage | |
| 333 | |||
| 334 | 32 | auto it = slot_to_tensor_.find(n.id); | |
| 335 | 32 | if (it == slot_to_tensor_.end() || it->second == nullptr) { | |
| 336 | // The caller dropped the Tensor, so nothing can read this value. | ||
| 337 | // Leave it an interior value of the fused kernel rather than | ||
| 338 | // allocating a buffer no one will look at. | ||
| 339 | ✗ | n.is_live_at_end = false; | |
| 340 | ✗ | continue; | |
| 341 | } | ||
| 342 | |||
| 343 | 32 | Tensor* held = it->second; | |
| 344 | 32 | Tensor buf = Tensor::empty_on(n.device, n.rows, n.cols, n.dtype); | |
| 345 | 32 | if (buf.data == nullptr) { | |
| 346 | ✗ | n.is_live_at_end = false; | |
| 347 | ✗ | continue; | |
| 348 | } | ||
| 349 | // Drop the tracking first: the move assignment below must see an | ||
| 350 | // ordinary tensor on both sides, or it would retarget the slot it is | ||
| 351 | // in the middle of retiring. | ||
| 352 | 32 | slot_to_tensor_.erase(it); | |
| 353 | 32 | held->jit_slot = -1; | |
| 354 | 32 | *held = std::move(buf); | |
| 355 | 32 | n.buffer = held->data; | |
| 356 | 32 | } | |
| 357 | 34 | } | |
| 358 | |||
| 359 | 34 | TraceDAG TraceContext::end() { | |
| 360 | 34 | if (!active_) { | |
| 361 | ✗ | throw std::runtime_error("brotensor::jit: end_trace() called without matching begin_trace()"); | |
| 362 | } | ||
| 363 | 34 | dag_.analyze_liveness(); | |
| 364 | 34 | materialize_(); | |
| 365 | 34 | active_ = false; | |
| 366 | 34 | active_ptr_to_slot_.clear(); | |
| 367 | // Whatever is still tracked stands for a value the fused kernel only ever | ||
| 368 | // holds in registers; there is nothing to hand back. | ||
| 369 | 34 | release_tracked_(); | |
| 370 | |||
| 371 | 34 | bool any_output = false; | |
| 372 | 247 | for (const TraceNode& n : dag_.nodes()) { | |
| 373 | 247 | if (n.is_live_at_end) { any_output = true; break; } | |
| 374 | } | ||
| 375 | 34 | if (!any_output) { | |
| 376 | ✗ | dag_.reset(); | |
| 377 | ✗ | throw std::runtime_error( | |
| 378 | "brotensor::jit: the trace has no observable result — every value it " | ||
| 379 | "produced was dropped before end_trace(), so there is nothing for the " | ||
| 380 | "fused kernel to write. Keep the result Tensor alive across " | ||
| 381 | "end_trace(), store() it into a buffer you own, or write in place " | ||
| 382 | "into one of the inputs"); | ||
| 383 | } | ||
| 384 | 34 | return std::move(dag_); | |
| 385 | ✗ | } | |
| 386 | |||
| 387 | 262 | int TraceContext::get_or_register_slot(const Tensor& t) { | |
| 388 | // A symbolic intermediate already *is* a node: it names its own slot, and | ||
| 389 | // it has no buffer to key on. | ||
| 390 | 262 | if (t.jit_slot >= 0) return t.jit_slot; | |
| 391 | |||
| 392 | // Everything else is caller-owned storage, keyed on its address. The | ||
| 393 | // address is stable for the trace's lifetime because the caller owns it — | ||
| 394 | // but a slot recorded earlier can still be stale if that tensor was freed | ||
| 395 | // and its address reused, so confirm the node still describes the tensor | ||
| 396 | // in hand before reusing its slot. | ||
| 397 | 153 | auto it = active_ptr_to_slot_.find(t.data); | |
| 398 | 153 | if (it != active_ptr_to_slot_.end() && dag_.slot_matches(it->second, t)) { | |
| 399 | 44 | return it->second; | |
| 400 | } | ||
| 401 | 109 | int slot = dag_.register_input(t); | |
| 402 | 109 | active_ptr_to_slot_[t.data] = slot; | |
| 403 | 109 | return slot; | |
| 404 | 262 | } | |
| 405 | |||
| 406 | 143 | Tensor TraceContext::record_op(TraceOpKind op, const std::vector<int>& in_slots, | |
| 407 | float scalar, const Device& dev, Dtype dt, | ||
| 408 | int rows, int cols, float scalar2) { | ||
| 409 | 286 | const int slot = dag_.add_node(op, in_slots, scalar, /*buffer=*/nullptr, | |
| 410 | 143 | dev, dt, rows, cols, scalar2); | |
| 411 | 143 | Tensor out; | |
| 412 | 143 | out.device = dev; | |
| 413 | 143 | out.dtype = dt; | |
| 414 | 143 | out.rows = rows; | |
| 415 | 143 | out.cols = cols; | |
| 416 | 143 | out.jit_slot = slot; | |
| 417 | // With copy elision `out` is already the caller's object; without it, the | ||
| 418 | // move ctor retargets this entry onto wherever it lands. | ||
| 419 | 143 | slot_to_tensor_[slot] = &out; | |
| 420 | 143 | return out; | |
| 421 | 143 | } | |
| 422 | |||
| 423 | 1 | void TraceContext::record_store(int src_slot, const Tensor& dst) { | |
| 424 | 1 | if (dst.jit_slot >= 0) { | |
| 425 | ✗ | throw std::runtime_error( | |
| 426 | "brotensor::jit: store() needs a destination the caller owns; its " | ||
| 427 | "target is a traced intermediate, which has no buffer to write to"); | ||
| 428 | } | ||
| 429 | 2 | const int slot = dag_.add_node(TraceOpKind::Copy, {src_slot}, 0.0f, dst.data, | |
| 430 | 1 | dst.device, dst.dtype, dst.rows, dst.cols); | |
| 431 | 1 | active_ptr_to_slot_[dst.data] = slot; | |
| 432 | 1 | } | |
| 433 | |||
| 434 | 1 | void TraceContext::record_cast_out(int src_slot, const Tensor& src, Tensor& dst, Dtype out_dtype) { | |
| 435 | 1 | if (dst.jit_slot < 0 && dst.data != nullptr && dst.device == src.device && | |
| 436 | ✗ | dst.rows == src.rows && dst.cols == src.cols && dst.dtype == out_dtype) { | |
| 437 | ✗ | const int slot = dag_.add_node(TraceOpKind::Cast, {src_slot}, 0.0f, dst.data, | |
| 438 | ✗ | dst.device, out_dtype, dst.rows, dst.cols); | |
| 439 | ✗ | active_ptr_to_slot_[dst.data] = slot; | |
| 440 | ✗ | return; | |
| 441 | } | ||
| 442 | 2 | dst = record_op(TraceOpKind::Cast, {src_slot}, 0.0f, src.device, out_dtype, | |
| 443 | 1 | src.rows, src.cols); | |
| 444 | 1 | } | |
| 445 | |||
| 446 | 3 | int TraceContext::record_inplace_op(TraceOpKind op, int target_slot, int arg_slot, | |
| 447 | float scalar, Tensor& target_tensor, | ||
| 448 | float scalar2) { | ||
| 449 | 6 | int slot = dag_.add_inplace_node(op, target_slot, arg_slot, target_tensor.data, | |
| 450 | 3 | scalar, scalar2); | |
| 451 | 3 | if (target_tensor.jit_slot >= 0) { | |
| 452 | // In-place on a traced intermediate: it has no buffer, and from here | ||
| 453 | // on it stands for the node this op just produced. | ||
| 454 | ✗ | slot_to_tensor_.erase(target_tensor.jit_slot); | |
| 455 | ✗ | target_tensor.jit_slot = slot; | |
| 456 | ✗ | slot_to_tensor_[slot] = &target_tensor; | |
| 457 | ✗ | } else { | |
| 458 | 3 | active_ptr_to_slot_[target_tensor.data] = slot; | |
| 459 | } | ||
| 460 | 3 | return slot; | |
| 461 | } | ||
| 462 | |||
| 463 | } // namespace brotensor::jit | ||
| 464 |