| Line |
Branch |
Exec |
Source |
| 1 |
|
|
// ─── Verified Fused Backward Trace JIT (TraceDAG::backward() Autodiff) ────── |
| 2 |
|
|
// |
| 3 |
|
|
// Reverse-mode automatic differentiation over `brotensor::jit::TraceDAG` with |
| 4 |
|
|
// compile-time formal verification via Brass: |
| 5 |
|
|
// 1. `mirEquivProven`: Formal equivalence proof (`brass::verify::verify_mir_equivalence`) |
| 6 |
|
|
// between unoptimized and optimized backward MIR functions. |
| 7 |
|
|
// 2. `spirvPtxEquivProven`: Cross-ISA equivalence proof (`brass::verify::verify_spirv_ptx_equivalence`) |
| 8 |
|
|
// between the backward MIR, Vulkan SPIR-V, and NVIDIA PTX kernels. |
| 9 |
|
|
// 3. `nanSafeProven`: Static compute shader safety proof (`brass::verify::prove_spirv_shader_safety`) |
| 10 |
|
|
// certifying absence of division-by-zero, domain violations, and NaN/Inf hazards. |
| 11 |
|
|
|
| 12 |
|
|
#include <brotensor/trace_autodiff.h> |
| 13 |
|
|
#include "trace_dag.h" |
| 14 |
|
|
#include "trace_compiler.h" |
| 15 |
|
|
#include "trace_plan.h" |
| 16 |
|
|
|
| 17 |
|
|
#if BROTENSOR_HAS_BRASS_JIT |
| 18 |
|
|
#include "spirv_emit.h" |
| 19 |
|
|
#include "gpu_mir_emit_detail.h" |
| 20 |
|
|
#include <brass/codegen/kernel_jit.hpp> |
| 21 |
|
|
#include <brass/mir/builder.hpp> |
| 22 |
|
|
#include <brass/mir/function.hpp> |
| 23 |
|
|
#include <brass/mir/instruction.hpp> |
| 24 |
|
|
#include <brass/mir/module.hpp> |
| 25 |
|
|
#include <brass/mir/types.hpp> |
| 26 |
|
|
#include <brass/target/ptx/ptx_isel.hpp> |
| 27 |
|
|
#include <brass/target/ptx_target.hpp> |
| 28 |
|
|
#include <brass/target/spirv/spirv_binary_reader.hpp> |
| 29 |
|
|
#include <brass/target/spirv/spirv_ir.hpp> |
| 30 |
|
|
#include <brass/target/spirv_target.hpp> |
| 31 |
|
|
#include <brass/verify/gpu_equiv.hpp> |
| 32 |
|
|
#include <brass/verify/gpu_kernel_prover.hpp> |
| 33 |
|
|
#include <brass/verify/smt_equiv.hpp> |
| 34 |
|
|
#endif |
| 35 |
|
|
|
| 36 |
|
|
#include <algorithm> |
| 37 |
|
|
#include <cmath> |
| 38 |
|
|
#include <cstddef> |
| 39 |
|
|
#include <cstdint> |
| 40 |
|
|
#include <span> |
| 41 |
|
|
#include <stdexcept> |
| 42 |
|
|
#include <string> |
| 43 |
|
|
#include <unordered_map> |
| 44 |
|
|
#include <vector> |
| 45 |
|
|
|
| 46 |
|
|
namespace brotensor::jit { |
| 47 |
|
|
|
| 48 |
|
|
namespace { |
| 49 |
|
|
|
| 50 |
|
|
constexpr float kSafeEps = 1e-12f; |
| 51 |
|
|
constexpr float kGeluK0 = 0.7978845608f; // sqrt(2 / pi) |
| 52 |
|
|
constexpr float kGeluK1 = 0.044715f; |
| 53 |
|
|
|
| 54 |
|
28 |
struct ShapeMeta { |
| 55 |
|
28 |
Device dev = Device::CPU; |
| 56 |
|
28 |
Dtype dt = Dtype::FP32; |
| 57 |
|
28 |
int rows = 1, cols = 1; |
| 58 |
|
|
}; |
| 59 |
|
|
|
| 60 |
|
28 |
ShapeMeta dominant_meta(const TraceDAG& dag) { |
| 61 |
|
28 |
ShapeMeta m; |
| 62 |
|
28 |
int r = 0, c = 0; |
| 63 |
|
28 |
dag.dominant_shape(r, c); |
| 64 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
m.rows = (r > 0) ? r : 1; |
| 65 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
m.cols = (c > 0) ? c : 1; |
| 66 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 |
for (const auto& n : dag.nodes()) { |
| 67 |
|
28 |
m.dev = n.device; |
| 68 |
|
28 |
m.dt = n.dtype; |
| 69 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
if (n.dtype == Dtype::FP32) break; |
| 70 |
|
|
} |
| 71 |
|
28 |
return m; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
73 |
int emit_const(TraceDAG& dag, int anchor, float val, const ShapeMeta& m) { |
| 75 |
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
|
73 |
int zero = dag.add_node(TraceOpKind::MulScalar, {anchor}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 76 |
5/8
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
73 |
return (val == 0.0f) ? zero : dag.add_node(TraceOpKind::AddScalar, {zero}, val, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 77 |
|
✗ |
} |
| 78 |
|
|
|
| 79 |
|
9 |
int emit_neg(TraceDAG& dag, int x, const ShapeMeta& m) { |
| 80 |
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 |
return dag.add_node(TraceOpKind::ScalarSub, {x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 81 |
|
✗ |
} |
| 82 |
|
|
|
| 83 |
|
3 |
int emit_abs(TraceDAG& dag, int x, const ShapeMeta& m) { |
| 84 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int pos = dag.add_node(TraceOpKind::ReLU, {x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 85 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int neg_part = dag.add_node(TraceOpKind::ReLU, {emit_neg(dag, x, m)}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 86 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
return dag.add_node(TraceOpKind::Add, {pos, neg_part}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 87 |
|
✗ |
} |
| 88 |
|
|
|
| 89 |
|
|
// Provably positive denominator: ReLU(x) + eps >= eps > 0 (`OpFAdd(NMax(x, 0), eps)`). |
| 90 |
|
194 |
int emit_pos_denom(TraceDAG& dag, int nonneg_x, float eps, const ShapeMeta& m) { |
| 91 |
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
|
194 |
int rx = dag.add_node(TraceOpKind::ReLU, {nonneg_x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 92 |
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
|
194 |
return dag.add_node(TraceOpKind::AddScalar, {rx}, eps, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 93 |
|
✗ |
} |
| 94 |
|
|
|
| 95 |
|
|
// Safe general division `num / den` for arbitrary signed `den` (including 0). |
| 96 |
|
27 |
int emit_safe_div_signed(TraceDAG& dag, int anchor, int num, int den, const ShapeMeta& m) { |
| 97 |
|
27 |
const auto& dn = dag.node(den); |
| 98 |
5/8
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 21 times.
|
27 |
if (dn.op == TraceOpKind::AddScalar && dn.scalar > 0.0f && !dn.inputs.empty() && |
| 99 |
|
21 |
dag.node(dn.inputs[0]).op == TraceOpKind::ReLU) { |
| 100 |
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 |
return dag.add_node(TraceOpKind::Div, {num, den}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 101 |
|
|
} |
| 102 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int pos_d = dag.add_node(TraceOpKind::ReLU, {den}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 103 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int neg_d = dag.add_node(TraceOpKind::ReLU, {emit_neg(dag, den, m)}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 104 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int abs_d = dag.add_node(TraceOpKind::Add, {pos_d, neg_d}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 105 |
|
6 |
int safe_den = emit_pos_denom(dag, abs_d, kSafeEps, m); |
| 106 |
|
6 |
int c_zero = emit_const(dag, anchor, 0.0f, m); |
| 107 |
|
6 |
int c_pos1 = emit_const(dag, anchor, 1.0f, m); |
| 108 |
|
6 |
int c_neg1 = emit_const(dag, anchor, -1.0f, m); |
| 109 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int neg_branch = dag.add_node(TraceOpKind::Where, {neg_d, c_neg1, c_zero}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 110 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int sign_d = dag.add_node(TraceOpKind::Where, {pos_d, c_pos1, neg_branch}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 111 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int signed_num = dag.add_node(TraceOpKind::Mul, {num, sign_d}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 112 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
return dag.add_node(TraceOpKind::Div, {signed_num, safe_den}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 113 |
|
27 |
} |
| 114 |
|
|
|
| 115 |
|
|
// Lowered Exp(x) using range reduction by 1/16 + Tanh + 4 squarings. |
| 116 |
|
6 |
int emit_lowered_exp(TraceDAG& dag, int x, const ShapeMeta& m) { |
| 117 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int xc = dag.add_node(TraceOpKind::Clamp, {x}, -88.0f, nullptr, m.dev, m.dt, m.rows, m.cols, 88.0f); |
| 118 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int u = dag.add_node(TraceOpKind::MulScalar, {xc}, 1.0f / 32.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 119 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int t = dag.add_node(TraceOpKind::Tanh, {u}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 120 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int num = dag.add_node(TraceOpKind::AddScalar, {t}, 1.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 121 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int den = emit_pos_denom(dag, dag.add_node(TraceOpKind::ScalarSub, {t}, 1.0f, nullptr, m.dev, m.dt, m.rows, m.cols), kSafeEps, m); |
| 122 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int e = dag.add_node(TraceOpKind::Div, {num, den}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 123 |
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
30 |
for (int i = 0; i < 4; ++i) e = dag.add_node(TraceOpKind::Mul, {e, e}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 124 |
|
6 |
return e; |
| 125 |
|
✗ |
} |
| 126 |
|
|
|
| 127 |
|
|
// Lowered Sqrt(x) via domain clamp + piecewise initial guess + 7 Newton-Raphson iterations. |
| 128 |
|
17 |
int emit_lowered_sqrt(TraceDAG& dag, int anchor, int x, const ShapeMeta& m) { |
| 129 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 |
int rx = dag.add_node(TraceOpKind::ReLU, {x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 130 |
|
17 |
int xs = emit_pos_denom(dag, rx, kSafeEps, m); |
| 131 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
34 |
int gt16 = dag.add_node(TraceOpKind::ReLU, |
| 132 |
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 |
{dag.add_node(TraceOpKind::SubScalar, {xs}, 16.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, |
| 133 |
|
17 |
0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 134 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
34 |
int guess_hi = dag.add_node(TraceOpKind::AddScalar, |
| 135 |
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 |
{dag.add_node(TraceOpKind::MulScalar, {xs}, 0.125f, nullptr, m.dev, m.dt, m.rows, m.cols)}, |
| 136 |
|
17 |
2.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 137 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
34 |
int guess_lo = dag.add_node(TraceOpKind::AddScalar, |
| 138 |
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 |
{dag.add_node(TraceOpKind::MulScalar, {xs}, 0.5f, nullptr, m.dev, m.dt, m.rows, m.cols)}, |
| 139 |
|
17 |
0.5f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 140 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 |
int y = dag.add_node(TraceOpKind::Where, {gt16, guess_hi, guess_lo}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 141 |
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 17 times.
|
136 |
for (int iter = 0; iter < 7; ++iter) { |
| 142 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
|
119 |
int q = dag.add_node(TraceOpKind::Div, {xs, emit_pos_denom(dag, y, kSafeEps, m)}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 143 |
3/6
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 119 times.
✗ Branch 5 not taken.
|
119 |
y = dag.add_node(TraceOpKind::MulScalar, {dag.add_node(TraceOpKind::Add, {y, q}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 0.5f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 144 |
|
119 |
} |
| 145 |
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 |
return dag.add_node(TraceOpKind::Where, {rx, y, emit_const(dag, anchor, 0.0f, m)}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 146 |
|
✗ |
} |
| 147 |
|
|
|
| 148 |
|
8 |
int emit_lowered_rsqrt(TraceDAG& dag, int anchor, int x, const ShapeMeta& m) { |
| 149 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
int rx = dag.add_node(TraceOpKind::ReLU, {x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 150 |
|
8 |
int s = emit_lowered_sqrt(dag, anchor, emit_pos_denom(dag, rx, kSafeEps, m), m); |
| 151 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
return dag.add_node(TraceOpKind::Div, {emit_const(dag, anchor, 1.0f, m), emit_pos_denom(dag, s, kSafeEps, m)}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 152 |
|
✗ |
} |
| 153 |
|
|
|
| 154 |
|
|
// Lowered Log(x) via 3 nested square roots (r = x^{1/8}) and 5-term atanh series. |
| 155 |
|
✗ |
int emit_lowered_log(TraceDAG& dag, int anchor, int x, const ShapeMeta& m) { |
| 156 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int rx = dag.add_node(TraceOpKind::ReLU, {x}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 157 |
|
✗ |
int r1 = emit_lowered_sqrt(dag, anchor, emit_pos_denom(dag, rx, kSafeEps, m), m); |
| 158 |
|
✗ |
int r2 = emit_lowered_sqrt(dag, anchor, r1, m); |
| 159 |
|
✗ |
int r3 = emit_lowered_sqrt(dag, anchor, r2, m); |
| 160 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int u = dag.add_node(TraceOpKind::Div, |
| 161 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
{dag.add_node(TraceOpKind::SubScalar, {r3}, 1.0f, nullptr, m.dev, m.dt, m.rows, m.cols), |
| 162 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
emit_pos_denom(dag, r3, 1.0f, m)}, |
| 163 |
|
✗ |
0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 164 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int u2 = dag.add_node(TraceOpKind::Mul, {u, u}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 165 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
int p = dag.add_node(TraceOpKind::AddScalar, {dag.add_node(TraceOpKind::MulScalar, {u2}, 1.0f / 9.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 1.0f / 7.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 166 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
p = dag.add_node(TraceOpKind::AddScalar, {dag.add_node(TraceOpKind::Mul, {p, u2}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 1.0f / 5.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 167 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
p = dag.add_node(TraceOpKind::AddScalar, {dag.add_node(TraceOpKind::Mul, {p, u2}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 1.0f / 3.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 168 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
p = dag.add_node(TraceOpKind::AddScalar, {dag.add_node(TraceOpKind::Mul, {p, u2}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 1.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 169 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
return dag.add_node(TraceOpKind::MulScalar, {dag.add_node(TraceOpKind::Mul, {u, p}, 0.0f, nullptr, m.dev, m.dt, m.rows, m.cols)}, 16.0f, nullptr, m.dev, m.dt, m.rows, m.cols); |
| 170 |
|
✗ |
} |
| 171 |
|
|
|
| 172 |
|
|
} // namespace |
| 173 |
|
|
|
| 174 |
|
48 |
TraceDAG canonicalize_trace_dag_for_codegen(const TraceDAG& dag) { |
| 175 |
|
48 |
bool needs_lowering = false; |
| 176 |
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 503 times.
|
534 |
for (const auto& n : dag.nodes()) { |
| 177 |
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 486 times.
|
503 |
switch (n.op) { |
| 178 |
|
|
case TraceOpKind::Const: |
| 179 |
|
|
case TraceOpKind::Neg: |
| 180 |
|
|
case TraceOpKind::Abs: |
| 181 |
|
|
case TraceOpKind::Exp: |
| 182 |
|
|
case TraceOpKind::Log: |
| 183 |
|
|
case TraceOpKind::Sqrt: |
| 184 |
|
|
case TraceOpKind::Rsqrt: |
| 185 |
|
|
case TraceOpKind::Div: |
| 186 |
|
|
case TraceOpKind::DivScalar: |
| 187 |
|
|
case TraceOpKind::ScalarDiv: |
| 188 |
|
|
case TraceOpKind::FMA: |
| 189 |
|
|
case TraceOpKind::Modulate: |
| 190 |
|
|
case TraceOpKind::GELU: |
| 191 |
|
17 |
needs_lowering = true; |
| 192 |
|
17 |
break; |
| 193 |
|
|
default: |
| 194 |
|
486 |
break; |
| 195 |
|
|
} |
| 196 |
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 486 times.
|
503 |
if (needs_lowering) break; |
| 197 |
|
|
} |
| 198 |
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 31 times.
|
48 |
if (!needs_lowering) return dag; |
| 199 |
|
|
|
| 200 |
|
17 |
TraceDAG out; |
| 201 |
|
17 |
std::unordered_map<int, int> remap; |
| 202 |
|
17 |
int anchor = -1; |
| 203 |
|
|
|
| 204 |
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 17 times.
|
720 |
for (const auto& n : dag.nodes()) { |
| 205 |
|
703 |
ShapeMeta m{n.device, n.dtype, n.rows, n.cols}; |
| 206 |
|
703 |
std::vector<int> ins; |
| 207 |
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
|
703 |
ins.reserve(n.inputs.size()); |
| 208 |
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 703 times.
|
1678 |
for (int old_in : n.inputs) { |
| 209 |
2/4
✓ Branch 0 taken 975 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 975 times.
✗ Branch 3 not taken.
|
975 |
ins.push_back(remap.at(old_in)); |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
|
703 |
int new_id = -1; |
| 213 |
8/15
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 596 times.
✓ Branch 8 taken 49 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 8 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
|
703 |
switch (n.op) { |
| 214 |
|
|
case TraceOpKind::Input: { |
| 215 |
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 |
new_id = out.add_node(TraceOpKind::Input, {}, 0.0f, n.buffer, n.device, n.dtype, n.rows, n.cols); |
| 216 |
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 32 times.
|
49 |
if (anchor < 0) anchor = new_id; |
| 217 |
|
49 |
break; |
| 218 |
|
|
} |
| 219 |
|
|
case TraceOpKind::Const: { |
| 220 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int a = (anchor >= 0) ? anchor : 0; |
| 221 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
new_id = emit_const(out, a, n.scalar, m); |
| 222 |
|
✗ |
break; |
| 223 |
|
|
} |
| 224 |
|
|
case TraceOpKind::Neg: { |
| 225 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
new_id = emit_neg(out, ins[0], m); |
| 226 |
|
✗ |
break; |
| 227 |
|
|
} |
| 228 |
|
|
case TraceOpKind::Abs: { |
| 229 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
new_id = emit_abs(out, ins[0], m); |
| 230 |
|
3 |
break; |
| 231 |
|
|
} |
| 232 |
|
|
case TraceOpKind::Exp: { |
| 233 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
new_id = emit_lowered_exp(out, ins[0], m); |
| 234 |
|
6 |
break; |
| 235 |
|
|
} |
| 236 |
|
|
case TraceOpKind::Sqrt: { |
| 237 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 |
new_id = emit_lowered_sqrt(out, (anchor >= 0) ? anchor : ins[0], ins[0], m); |
| 238 |
|
9 |
break; |
| 239 |
|
|
} |
| 240 |
|
|
case TraceOpKind::Rsqrt: { |
| 241 |
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 |
new_id = emit_lowered_rsqrt(out, (anchor >= 0) ? anchor : ins[0], ins[0], m); |
| 242 |
|
8 |
break; |
| 243 |
|
|
} |
| 244 |
|
|
case TraceOpKind::Log: { |
| 245 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
new_id = emit_lowered_log(out, (anchor >= 0) ? anchor : ins[0], ins[0], m); |
| 246 |
|
✗ |
break; |
| 247 |
|
|
} |
| 248 |
|
|
case TraceOpKind::Div: { |
| 249 |
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
27 |
new_id = emit_safe_div_signed(out, (anchor >= 0) ? anchor : ins[0], ins[0], ins[1], m); |
| 250 |
|
27 |
break; |
| 251 |
|
|
} |
| 252 |
|
|
case TraceOpKind::DivScalar: { |
| 253 |
|
✗ |
float s = n.scalar; |
| 254 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (std::fabs(s) < kSafeEps) s = (s < 0.0f) ? -kSafeEps : kSafeEps; |
| 255 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
new_id = out.add_node(TraceOpKind::MulScalar, {ins[0]}, 1.0f / s, |
| 256 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 257 |
|
✗ |
break; |
| 258 |
|
|
} |
| 259 |
|
|
case TraceOpKind::ScalarDiv: { |
| 260 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
int s_node = emit_const(out, (anchor >= 0) ? anchor : ins[0], n.scalar, m); |
| 261 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
new_id = emit_safe_div_signed(out, (anchor >= 0) ? anchor : ins[0], s_node, ins[0], m); |
| 262 |
|
✗ |
break; |
| 263 |
|
|
} |
| 264 |
|
|
case TraceOpKind::FMA: { |
| 265 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
int ab = out.add_node(TraceOpKind::Mul, {ins[0], ins[1]}, 0.0f, |
| 266 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 267 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
new_id = out.add_node(TraceOpKind::Add, {ab, ins[2]}, 0.0f, |
| 268 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 269 |
|
✗ |
break; |
| 270 |
|
|
} |
| 271 |
|
|
case TraceOpKind::Modulate: { |
| 272 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
int sp1 = out.add_node(TraceOpKind::AddScalar, {ins[1]}, 1.0f, |
| 273 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 274 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
int x_sp1 = out.add_node(TraceOpKind::Mul, {ins[0], sp1}, 0.0f, |
| 275 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 276 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
new_id = out.add_node(TraceOpKind::Add, {x_sp1, ins[2]}, 0.0f, |
| 277 |
|
✗ |
nullptr, n.device, n.dtype, n.rows, n.cols); |
| 278 |
|
✗ |
break; |
| 279 |
|
|
} |
| 280 |
|
|
case TraceOpKind::GELU: { |
| 281 |
|
5 |
int x = ins[0]; |
| 282 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int x2 = out.add_node(TraceOpKind::Mul, {x, x}, 0.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 283 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int x3 = out.add_node(TraceOpKind::Mul, {x2, x}, 0.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 284 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int kx3 = out.add_node(TraceOpKind::MulScalar, {x3}, kGeluK1, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 285 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int inner = out.add_node(TraceOpKind::Add, {x, kx3}, 0.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 286 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int u = out.add_node(TraceOpKind::MulScalar, {inner}, kGeluK0, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 287 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int t = out.add_node(TraceOpKind::Tanh, {u}, 0.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 288 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int one_p_t = out.add_node(TraceOpKind::AddScalar, {t}, 1.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 289 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int half_x = out.add_node(TraceOpKind::MulScalar, {x}, 0.5f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 290 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
new_id = out.add_node(TraceOpKind::Mul, {half_x, one_p_t}, 0.0f, nullptr, n.device, n.dtype, n.rows, n.cols); |
| 291 |
|
5 |
break; |
| 292 |
|
|
} |
| 293 |
|
|
default: { |
| 294 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
|
596 |
if (n.is_inplace) { |
| 295 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int target = remap.at(n.inplace_target_slot); |
| 296 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int arg = (n.inputs.size() > 1) ? ins[1] : -1; |
| 297 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
new_id = out.add_inplace_node(n.op, target, arg, n.buffer, n.scalar, n.scalar2); |
| 298 |
|
✗ |
} else { |
| 299 |
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
|
1192 |
new_id = out.add_node(n.op, ins, n.scalar, nullptr, |
| 300 |
|
596 |
n.device, n.dtype, n.rows, n.cols, n.scalar2); |
| 301 |
|
|
} |
| 302 |
|
596 |
break; |
| 303 |
|
|
} |
| 304 |
|
|
} |
| 305 |
|
703 |
TraceNode& created = out.nodes()[static_cast<size_t>(new_id)]; |
| 306 |
|
703 |
created.buffer = n.buffer; |
| 307 |
|
703 |
created.is_live_at_end = n.is_live_at_end; |
| 308 |
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
|
703 |
remap[n.id] = new_id; |
| 309 |
|
703 |
} |
| 310 |
|
|
|
| 311 |
2/2
✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 17 times.
|
1765 |
for (auto& nd : out.nodes()) nd.outputs.clear(); |
| 312 |
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1748 times.
|
1765 |
for (const auto& nd : out.nodes()) { |
| 313 |
2/2
✓ Branch 0 taken 2423 times.
✓ Branch 1 taken 1748 times.
|
4171 |
for (int in_id : nd.inputs) { |
| 314 |
2/4
✓ Branch 0 taken 2423 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2423 times.
|
2423 |
if (in_id >= 0 && in_id < static_cast<int>(out.nodes().size())) { |
| 315 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2423 times.
|
2423 |
out.nodes()[static_cast<size_t>(in_id)].outputs.push_back(nd.id); |
| 316 |
|
2423 |
} |
| 317 |
|
|
} |
| 318 |
|
|
} |
| 319 |
|
17 |
return out; |
| 320 |
|
65 |
} |
| 321 |
|
|
|
| 322 |
|
46 |
TraceDAG TraceDAG::backward(int target_input_idx) const { |
| 323 |
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 |
if (nodes_.empty()) { |
| 324 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
throw std::runtime_error("TraceDAG::backward: cannot differentiate an empty TraceDAG"); |
| 325 |
|
|
} |
| 326 |
|
|
|
| 327 |
|
46 |
const ShapeMeta m = dominant_meta(*this); |
| 328 |
|
46 |
TraceDAG bwd; |
| 329 |
|
46 |
std::unordered_map<int, int> fwd_to_bwd; |
| 330 |
|
46 |
std::vector<int> primal_input_ids; |
| 331 |
|
|
|
| 332 |
|
|
// 1. Copy primal Input nodes first so input slots [0..num_inputs-1] match forward_dag |
| 333 |
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 28 times.
|
294 |
for (const auto& n : nodes_) { |
| 334 |
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 104 times.
|
266 |
if (n.op == TraceOpKind::Input) { |
| 335 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
208 |
int bid = bwd.add_node(TraceOpKind::Input, {}, 0.0f, n.buffer, |
| 336 |
|
104 |
n.device, Dtype::FP32, n.rows, n.cols); |
| 337 |
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 18 times.
|
104 |
fwd_to_bwd[n.id] = bid; |
| 338 |
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 |
primal_input_ids.push_back(n.id); |
| 339 |
|
86 |
} |
| 340 |
|
|
} |
| 341 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
if (primal_input_ids.empty()) { |
| 342 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
throw std::runtime_error("TraceDAG::backward: forward TraceDAG has no Input nodes"); |
| 343 |
|
|
} |
| 344 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
if (target_input_idx >= static_cast<int>(primal_input_ids.size())) { |
| 345 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
throw std::invalid_argument("TraceDAG::backward: target_input_idx out of range"); |
| 346 |
|
|
} |
| 347 |
|
|
|
| 348 |
|
|
// 2. Register upstream gradient `grad_output` (`dL/dy`) as the next Input node |
| 349 |
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
56 |
const int grad_out_slot = bwd.add_node(TraceOpKind::Input, {}, 0.0f, nullptr, |
| 350 |
|
28 |
m.dev, Dtype::FP32, m.rows, m.cols); |
| 351 |
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 18 times.
|
28 |
const int anchor = fwd_to_bwd[primal_input_ids[0]]; |
| 352 |
|
|
|
| 353 |
|
|
// 3. Replay forward intermediate nodes into `bwd` (without live_at_end) so |
| 354 |
|
|
// backward derivative expressions can reference any primal intermediate |
| 355 |
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 46 times.
|
294 |
for (const auto& n : nodes_) { |
| 356 |
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 86 times.
|
248 |
if (n.op == TraceOpKind::Input) continue; |
| 357 |
|
162 |
std::vector<int> ins; |
| 358 |
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
|
162 |
ins.reserve(n.inputs.size()); |
| 359 |
4/6
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 263 times.
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 263 times.
✗ Branch 5 not taken.
|
425 |
for (int in_id : n.inputs) ins.push_back(fwd_to_bwd.at(in_id)); |
| 360 |
2/4
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 162 times.
✗ Branch 3 not taken.
|
324 |
int bid = bwd.add_node(n.op, ins, n.scalar, nullptr, |
| 361 |
|
162 |
n.device, Dtype::FP32, n.rows, n.cols, n.scalar2); |
| 362 |
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
|
162 |
fwd_to_bwd[n.id] = bid; |
| 363 |
|
162 |
} |
| 364 |
|
|
|
| 365 |
|
|
// 4. Seed adjoints: live-at-end output nodes receive `grad_out_slot` |
| 366 |
|
46 |
std::unordered_map<int, int> adj; |
| 367 |
|
46 |
bool seeded = false; |
| 368 |
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 46 times.
|
294 |
for (const auto& n : nodes_) { |
| 369 |
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 28 times.
|
248 |
if (n.is_live_at_end) { |
| 370 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
adj[n.id] = grad_out_slot; |
| 371 |
|
28 |
seeded = true; |
| 372 |
|
28 |
} |
| 373 |
|
|
} |
| 374 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 |
if (!seeded) { |
| 375 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
adj[nodes_.back().id] = grad_out_slot; |
| 376 |
|
✗ |
} |
| 377 |
|
|
|
| 378 |
|
306 |
auto accum_grad = [&](int fwd_id, int contrib_bwd_slot) { |
| 379 |
|
260 |
auto it = adj.find(fwd_id); |
| 380 |
2/2
✓ Branch 0 taken 217 times.
✓ Branch 1 taken 43 times.
|
260 |
if (it == adj.end()) { |
| 381 |
|
217 |
adj[fwd_id] = contrib_bwd_slot; |
| 382 |
|
217 |
} else { |
| 383 |
3/6
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
|
86 |
adj[fwd_id] = bwd.add_node(TraceOpKind::Add, {it->second, contrib_bwd_slot}, |
| 384 |
|
43 |
0.0f, nullptr, m.dev, Dtype::FP32, m.rows, m.cols); |
| 385 |
|
|
} |
| 386 |
|
260 |
}; |
| 387 |
|
|
|
| 388 |
|
652 |
auto add_op = [&](TraceOpKind op, const std::vector<int>& ins, float s = 0.0f, float s2 = 0.0f) { |
| 389 |
|
606 |
return bwd.add_node(op, ins, s, nullptr, m.dev, Dtype::FP32, m.rows, m.cols, s2); |
| 390 |
|
|
}; |
| 391 |
|
|
|
| 392 |
|
18 |
struct NormBwdAux { |
| 393 |
|
18 |
bool is_ln = false; |
| 394 |
|
18 |
int rstd_slot = -1; |
| 395 |
|
18 |
int mean_slot = -1; |
| 396 |
|
18 |
int c0_slot = -1; |
| 397 |
|
18 |
int c1_slot = -1; |
| 398 |
|
18 |
int xhat_node = -1; |
| 399 |
|
|
}; |
| 400 |
|
46 |
std::unordered_map<int, NormBwdAux> norm_aux; |
| 401 |
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 28 times.
|
294 |
for (const auto& n : nodes_) { |
| 402 |
4/4
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 239 times.
|
266 |
if (n.op != TraceOpKind::RMSNorm && n.op != TraceOpKind::LayerNorm) continue; |
| 403 |
|
27 |
NormBwdAux aux; |
| 404 |
|
27 |
aux.is_ln = (n.op == TraceOpKind::LayerNorm); |
| 405 |
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6 times.
|
27 |
if (aux.is_ln) { |
| 406 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
aux.mean_slot = bwd.add_node(TraceOpKind::Input, {}, 0.0f, nullptr, m.dev, Dtype::FP32, n.rows, 1); |
| 407 |
|
6 |
} |
| 408 |
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 |
aux.rstd_slot = bwd.add_node(TraceOpKind::Input, {}, 0.0f, nullptr, m.dev, Dtype::FP32, n.rows, 1); |
| 409 |
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 6 times.
|
27 |
if (aux.is_ln) { |
| 410 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
aux.c0_slot = bwd.add_node(TraceOpKind::Input, {}, 0.0f, nullptr, m.dev, Dtype::FP32, n.rows, 1); |
| 411 |
|
6 |
} |
| 412 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 |
aux.c1_slot = bwd.add_node(TraceOpKind::Input, {}, 0.0f, nullptr, m.dev, Dtype::FP32, n.rows, 1); |
| 413 |
|
|
|
| 414 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 18 times.
|
27 |
const int x_bwd = fwd_to_bwd.at(n.inputs[0]); |
| 415 |
6/10
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
9 |
const int centered = aux.is_ln ? add_op(TraceOpKind::Sub, {x_bwd, aux.mean_slot}) : x_bwd; |
| 416 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 |
aux.xhat_node = add_op(TraceOpKind::Mul, {centered, aux.rstd_slot}); |
| 417 |
|
9 |
int y_bwd = aux.xhat_node; |
| 418 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 |
if (n.inputs.size() > 1 && n.inputs[1] >= 0) { |
| 419 |
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
|
9 |
y_bwd = add_op(TraceOpKind::Mul, {y_bwd, fwd_to_bwd.at(n.inputs[1])}); |
| 420 |
|
9 |
} |
| 421 |
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
9 |
if (n.inputs.size() > 2 && n.inputs[2] >= 0) { |
| 422 |
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 |
y_bwd = add_op(TraceOpKind::Add, {y_bwd, fwd_to_bwd.at(n.inputs[2])}); |
| 423 |
|
6 |
} |
| 424 |
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 |
const int old_norm_bwd = fwd_to_bwd.at(n.id); |
| 425 |
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 9 times.
|
132 |
for (auto& bn : bwd.nodes()) { |
| 426 |
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 123 times.
|
228 |
for (int& inp : bn.inputs) { |
| 427 |
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 9 times.
|
105 |
if (inp == old_norm_bwd) inp = y_bwd; |
| 428 |
|
|
} |
| 429 |
|
|
} |
| 430 |
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 |
fwd_to_bwd[n.id] = y_bwd; |
| 431 |
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 |
norm_aux[n.id] = aux; |
| 432 |
|
|
} |
| 433 |
|
|
|
| 434 |
|
|
// 5. Reverse-mode traversal from last node to first |
| 435 |
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 28 times.
|
276 |
for (int idx = static_cast<int>(nodes_.size()) - 1; idx >= 0; --idx) { |
| 436 |
|
248 |
const TraceNode& n = nodes_[static_cast<size_t>(idx)]; |
| 437 |
1/2
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
|
248 |
auto it_g = adj.find(n.id); |
| 438 |
3/4
✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
✓ Branch 3 taken 3 times.
|
248 |
if (it_g == adj.end()) continue; |
| 439 |
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
|
245 |
const int dy = it_g->second; |
| 440 |
|
|
|
| 441 |
21/24
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 6 times.
✓ Branch 14 taken 7 times.
✓ Branch 15 taken 12 times.
✓ Branch 16 taken 7 times.
✓ Branch 17 taken 6 times.
✓ Branch 18 taken 6 times.
✓ Branch 19 taken 5 times.
✓ Branch 20 taken 6 times.
✓ Branch 21 taken 17 times.
✓ Branch 22 taken 83 times.
✗ Branch 23 not taken.
|
245 |
switch (n.op) { |
| 442 |
|
|
case TraceOpKind::Input: |
| 443 |
|
|
case TraceOpKind::Const: |
| 444 |
|
83 |
break; |
| 445 |
|
|
|
| 446 |
|
|
case TraceOpKind::Copy: |
| 447 |
|
|
case TraceOpKind::Cast: |
| 448 |
|
|
case TraceOpKind::AddScalar: |
| 449 |
|
|
case TraceOpKind::SubScalar: |
| 450 |
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
|
17 |
accum_grad(n.inputs[0], dy); |
| 451 |
|
8 |
break; |
| 452 |
|
|
|
| 453 |
|
|
case TraceOpKind::Add: |
| 454 |
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 |
accum_grad(n.inputs[0], dy); |
| 455 |
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 |
accum_grad(n.inputs[1], dy); |
| 456 |
|
39 |
break; |
| 457 |
|
|
|
| 458 |
|
|
case TraceOpKind::Sub: { |
| 459 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
accum_grad(n.inputs[0], dy); |
| 460 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int ndy = add_op(TraceOpKind::ScalarSub, {dy}, 0.0f); |
| 461 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
accum_grad(n.inputs[1], ndy); |
| 462 |
|
3 |
break; |
| 463 |
|
|
} |
| 464 |
|
|
|
| 465 |
|
|
case TraceOpKind::ScalarSub: |
| 466 |
|
|
case TraceOpKind::Neg: { |
| 467 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
int ndy = add_op(TraceOpKind::ScalarSub, {dy}, 0.0f); |
| 468 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
accum_grad(n.inputs[0], ndy); |
| 469 |
|
✗ |
break; |
| 470 |
|
|
} |
| 471 |
|
|
|
| 472 |
|
|
case TraceOpKind::Mul: { |
| 473 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 474 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
int b = fwd_to_bwd.at(n.inputs[1]); |
| 475 |
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, b})); |
| 476 |
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 |
accum_grad(n.inputs[1], add_op(TraceOpKind::Mul, {dy, a})); |
| 477 |
|
19 |
break; |
| 478 |
|
|
} |
| 479 |
|
|
|
| 480 |
|
|
case TraceOpKind::MulScalar: |
| 481 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[0], add_op(TraceOpKind::MulScalar, {dy}, n.scalar)); |
| 482 |
|
3 |
break; |
| 483 |
|
|
|
| 484 |
|
|
case TraceOpKind::DivScalar: { |
| 485 |
|
✗ |
float s = n.scalar; |
| 486 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (std::fabs(s) < kSafeEps) s = (s < 0.0f) ? -kSafeEps : kSafeEps; |
| 487 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
accum_grad(n.inputs[0], add_op(TraceOpKind::MulScalar, {dy}, 1.0f / s)); |
| 488 |
|
✗ |
break; |
| 489 |
|
|
} |
| 490 |
|
|
|
| 491 |
|
|
case TraceOpKind::Div: { |
| 492 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 493 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int b = fwd_to_bwd.at(n.inputs[1]); |
| 494 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int da = add_op(TraceOpKind::Div, {dy, b}); |
| 495 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int b2 = add_op(TraceOpKind::Mul, {b, b}); |
| 496 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int b2_safe = emit_pos_denom(bwd, b2, kSafeEps, m); |
| 497 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int dy_a = add_op(TraceOpKind::Mul, {dy, a}); |
| 498 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int ndy_a = add_op(TraceOpKind::ScalarSub, {dy_a}, 0.0f); |
| 499 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int db = add_op(TraceOpKind::Div, {ndy_a, b2_safe}); |
| 500 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
accum_grad(n.inputs[0], da); |
| 501 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
accum_grad(n.inputs[1], db); |
| 502 |
|
5 |
break; |
| 503 |
|
|
} |
| 504 |
|
|
|
| 505 |
|
|
case TraceOpKind::ScalarDiv: { |
| 506 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 507 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int a2 = add_op(TraceOpKind::Mul, {a, a}); |
| 508 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int a2_safe = emit_pos_denom(bwd, a2, kSafeEps, m); |
| 509 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int num = add_op(TraceOpKind::MulScalar, {dy}, -n.scalar); |
| 510 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Div, {num, a2_safe})); |
| 511 |
|
3 |
break; |
| 512 |
|
|
} |
| 513 |
|
|
|
| 514 |
|
|
case TraceOpKind::FMA: { |
| 515 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 516 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
int b = fwd_to_bwd.at(n.inputs[1]); |
| 517 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, b})); |
| 518 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
accum_grad(n.inputs[1], add_op(TraceOpKind::Mul, {dy, a})); |
| 519 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
accum_grad(n.inputs[2], dy); |
| 520 |
|
✗ |
break; |
| 521 |
|
|
} |
| 522 |
|
|
|
| 523 |
|
|
case TraceOpKind::Modulate: { |
| 524 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
int x = fwd_to_bwd.at(n.inputs[0]); |
| 525 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
int scale = fwd_to_bwd.at(n.inputs[1]); |
| 526 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int sp1 = add_op(TraceOpKind::AddScalar, {scale}, 1.0f); |
| 527 |
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, sp1})); |
| 528 |
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 |
accum_grad(n.inputs[1], add_op(TraceOpKind::Mul, {dy, x})); |
| 529 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
accum_grad(n.inputs[2], dy); |
| 530 |
|
7 |
break; |
| 531 |
|
|
} |
| 532 |
|
|
|
| 533 |
|
|
case TraceOpKind::ReLU: { |
| 534 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 535 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
int ra = add_op(TraceOpKind::ReLU, {a}); |
| 536 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 537 |
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {ra, dy, zero})); |
| 538 |
|
2 |
break; |
| 539 |
|
|
} |
| 540 |
|
|
|
| 541 |
|
|
case TraceOpKind::Abs: { |
| 542 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 543 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int pos = add_op(TraceOpKind::ReLU, {a}); |
| 544 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int na = add_op(TraceOpKind::ScalarSub, {a}, 0.0f); |
| 545 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int neg_part = add_op(TraceOpKind::ReLU, {na}); |
| 546 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int ndy = add_op(TraceOpKind::ScalarSub, {dy}, 0.0f); |
| 547 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 548 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int neg_sel = add_op(TraceOpKind::Where, {neg_part, ndy, zero}); |
| 549 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {pos, dy, neg_sel})); |
| 550 |
|
3 |
break; |
| 551 |
|
|
} |
| 552 |
|
|
|
| 553 |
|
|
case TraceOpKind::Clamp: { |
| 554 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 555 |
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
|
3 |
int gt_lo = add_op(TraceOpKind::ReLU, {add_op(TraceOpKind::SubScalar, {a}, n.scalar)}); |
| 556 |
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
|
3 |
int lt_hi = add_op(TraceOpKind::ReLU, {add_op(TraceOpKind::ScalarSub, {a}, n.scalar2)}); |
| 557 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
int inside = add_op(TraceOpKind::Mul, {gt_lo, lt_hi}); |
| 558 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 559 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {inside, dy, zero})); |
| 560 |
|
3 |
break; |
| 561 |
|
|
} |
| 562 |
|
|
|
| 563 |
|
|
case TraceOpKind::Where: { |
| 564 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int c = fwd_to_bwd.at(n.inputs[0]); |
| 565 |
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 566 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[1], add_op(TraceOpKind::Where, {c, dy, zero})); |
| 567 |
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 |
accum_grad(n.inputs[2], add_op(TraceOpKind::Where, {c, zero, dy})); |
| 568 |
|
3 |
break; |
| 569 |
|
|
} |
| 570 |
|
|
|
| 571 |
|
|
case TraceOpKind::Sigmoid: { |
| 572 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int sig = fwd_to_bwd.at(n.id); |
| 573 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int one_m_sig = add_op(TraceOpKind::ScalarSub, {sig}, 1.0f); |
| 574 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int dsig = add_op(TraceOpKind::Mul, {sig, one_m_sig}); |
| 575 |
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, dsig})); |
| 576 |
|
6 |
break; |
| 577 |
|
|
} |
| 578 |
|
|
|
| 579 |
|
|
case TraceOpKind::Tanh: { |
| 580 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
int th = fwd_to_bwd.at(n.id); |
| 581 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int th2 = add_op(TraceOpKind::Mul, {th, th}); |
| 582 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int dtanh = add_op(TraceOpKind::ScalarSub, {th2}, 1.0f); |
| 583 |
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, dtanh})); |
| 584 |
|
7 |
break; |
| 585 |
|
|
} |
| 586 |
|
|
|
| 587 |
|
|
case TraceOpKind::SiLU: { |
| 588 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 589 |
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 |
int sig = add_op(TraceOpKind::Sigmoid, {a}); |
| 590 |
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 |
int one_m_sig = add_op(TraceOpKind::ScalarSub, {sig}, 1.0f); |
| 591 |
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 |
int a_oms = add_op(TraceOpKind::Mul, {a, one_m_sig}); |
| 592 |
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 |
int factor = add_op(TraceOpKind::AddScalar, {a_oms}, 1.0f); |
| 593 |
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 |
int dsilu = add_op(TraceOpKind::Mul, {sig, factor}); |
| 594 |
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, dsilu})); |
| 595 |
|
12 |
break; |
| 596 |
|
|
} |
| 597 |
|
|
|
| 598 |
|
|
case TraceOpKind::GELU: { |
| 599 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 600 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int a2 = add_op(TraceOpKind::Mul, {a, a}); |
| 601 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int a3 = add_op(TraceOpKind::Mul, {a2, a}); |
| 602 |
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
|
7 |
int inner = add_op(TraceOpKind::Add, {a, add_op(TraceOpKind::MulScalar, {a3}, kGeluK1)}); |
| 603 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int u = add_op(TraceOpKind::MulScalar, {inner}, kGeluK0); |
| 604 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int t = add_op(TraceOpKind::Tanh, {u}); |
| 605 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int one_p_t = add_op(TraceOpKind::AddScalar, {t}, 1.0f); |
| 606 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int left = add_op(TraceOpKind::MulScalar, {one_p_t}, 0.5f); |
| 607 |
|
|
|
| 608 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int t2 = add_op(TraceOpKind::Mul, {t, t}); |
| 609 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int sech2 = add_op(TraceOpKind::ScalarSub, {t2}, 1.0f); |
| 610 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
int du = add_op(TraceOpKind::MulScalar, |
| 611 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
{add_op(TraceOpKind::AddScalar, |
| 612 |
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 |
{add_op(TraceOpKind::MulScalar, {a2}, 3.0f * kGeluK1)}, |
| 613 |
|
|
1.0f)}, |
| 614 |
|
|
kGeluK0); |
| 615 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int half_a = add_op(TraceOpKind::MulScalar, {a}, 0.5f); |
| 616 |
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
|
7 |
int right = add_op(TraceOpKind::Mul, {half_a, add_op(TraceOpKind::Mul, {sech2, du})}); |
| 617 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
int dgelu = add_op(TraceOpKind::Add, {left, right}); |
| 618 |
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, dgelu})); |
| 619 |
|
7 |
break; |
| 620 |
|
|
} |
| 621 |
|
|
|
| 622 |
|
|
case TraceOpKind::Exp: { |
| 623 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 624 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int ac = add_op(TraceOpKind::Clamp, {a}, -88.0f, 88.0f); |
| 625 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int ea = add_op(TraceOpKind::Exp, {ac}); |
| 626 |
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {dy, ea})); |
| 627 |
|
6 |
break; |
| 628 |
|
|
} |
| 629 |
|
|
|
| 630 |
|
|
case TraceOpKind::Log: { |
| 631 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 632 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int ra = add_op(TraceOpKind::ReLU, {a}); |
| 633 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int as = emit_pos_denom(bwd, ra, kSafeEps, m); |
| 634 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int q = add_op(TraceOpKind::Div, {dy, as}); |
| 635 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 636 |
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {ra, q, zero})); |
| 637 |
|
6 |
break; |
| 638 |
|
|
} |
| 639 |
|
|
|
| 640 |
|
|
case TraceOpKind::Sqrt: { |
| 641 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 642 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int ra = add_op(TraceOpKind::ReLU, {a}); |
| 643 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int as = emit_pos_denom(bwd, ra, kSafeEps, m); |
| 644 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
int sa = add_op(TraceOpKind::Sqrt, {as}); |
| 645 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int sa_safe = emit_pos_denom(bwd, sa, kSafeEps, m); |
| 646 |
4/8
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
|
5 |
int q = add_op(TraceOpKind::MulScalar, {add_op(TraceOpKind::Div, {dy, sa_safe})}, 0.5f); |
| 647 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 648 |
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
|
5 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {ra, q, zero})); |
| 649 |
|
5 |
break; |
| 650 |
|
|
} |
| 651 |
|
|
|
| 652 |
|
|
case TraceOpKind::Rsqrt: { |
| 653 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int a = fwd_to_bwd.at(n.inputs[0]); |
| 654 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int ra = add_op(TraceOpKind::ReLU, {a}); |
| 655 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int as = emit_pos_denom(bwd, ra, kSafeEps, m); |
| 656 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int r = add_op(TraceOpKind::Rsqrt, {as}); |
| 657 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int r2 = add_op(TraceOpKind::Mul, {r, r}); |
| 658 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
int r3 = add_op(TraceOpKind::Mul, {r2, r}); |
| 659 |
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 |
int dr = add_op(TraceOpKind::MulScalar, {add_op(TraceOpKind::Mul, {dy, r3})}, -0.5f); |
| 660 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
int zero = emit_const(bwd, anchor, 0.0f, m); |
| 661 |
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Where, {ra, dr, zero})); |
| 662 |
|
6 |
break; |
| 663 |
|
|
} |
| 664 |
|
|
|
| 665 |
|
|
case TraceOpKind::RMSNorm: |
| 666 |
|
|
case TraceOpKind::LayerNorm: { |
| 667 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
✗ |
const NormBwdAux& aux = norm_aux.at(n.id); |
| 668 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 |
const bool has_gamma = (n.inputs.size() > 1 && n.inputs[1] >= 0); |
| 669 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 |
const bool has_beta = (n.inputs.size() > 2 && n.inputs[2] >= 0); |
| 670 |
5/12
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 9 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
9 |
int g_dy = has_gamma ? add_op(TraceOpKind::Mul, {dy, fwd_to_bwd.at(n.inputs[1])}) : dy; |
| 671 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 |
int xhat_c1 = add_op(TraceOpKind::Mul, {aux.xhat_node, aux.c1_slot}); |
| 672 |
8/14
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
18 |
int inner = aux.is_ln |
| 673 |
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 |
? add_op(TraceOpKind::Sub, {add_op(TraceOpKind::Sub, {g_dy, aux.c0_slot}), xhat_c1}) |
| 674 |
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 |
: add_op(TraceOpKind::Sub, {g_dy, xhat_c1}); |
| 675 |
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
|
9 |
accum_grad(n.inputs[0], add_op(TraceOpKind::Mul, {aux.rstd_slot, inner})); |
| 676 |
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 |
if (has_gamma) { |
| 677 |
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
|
9 |
accum_grad(n.inputs[1], add_op(TraceOpKind::Mul, {dy, aux.xhat_node})); |
| 678 |
|
9 |
} |
| 679 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 |
if (has_beta) { |
| 680 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
accum_grad(n.inputs[2], dy); |
| 681 |
|
6 |
} |
| 682 |
|
9 |
break; |
| 683 |
|
|
} |
| 684 |
|
|
|
| 685 |
|
|
default: |
| 686 |
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
✗ |
throw std::runtime_error(std::string("TraceDAG::backward: unsupported op ") + |
| 687 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
op_kind_name(n.op)); |
| 688 |
|
|
} |
| 689 |
|
245 |
} |
| 690 |
|
|
|
| 691 |
|
|
// 6. Emit final output Copy nodes for the requested primal inputs |
| 692 |
|
28 |
std::vector<int> out_slots; |
| 693 |
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
|
28 |
if (target_input_idx >= 0) { |
| 694 |
|
21 |
int fwd_in = primal_input_ids[static_cast<size_t>(target_input_idx)]; |
| 695 |
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 |
auto it = adj.find(fwd_in); |
| 696 |
8/14
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 21 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 21 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 21 times.
✗ Branch 13 not taken.
|
21 |
out_slots.push_back(add_op(TraceOpKind::Copy, {(it != adj.end()) ? it->second : emit_const(bwd, anchor, 0.0f, m)})); |
| 697 |
|
21 |
} else { |
| 698 |
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
|
26 |
for (int fwd_in : primal_input_ids) { |
| 699 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
auto it = adj.find(fwd_in); |
| 700 |
8/14
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
✗ Branch 13 not taken.
|
19 |
out_slots.push_back(add_op(TraceOpKind::Copy, {(it != adj.end()) ? it->second : emit_const(bwd, anchor, 0.0f, m)})); |
| 701 |
|
|
} |
| 702 |
|
|
} |
| 703 |
|
|
|
| 704 |
|
|
// 7. Dead-node elimination: keep all Input nodes + backward slice of `out_slots` |
| 705 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
std::vector<bool> keep(bwd.node_count(), false); |
| 706 |
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1045 times.
|
1073 |
for (const auto& n : bwd.nodes()) { |
| 707 |
3/4
✓ Branch 0 taken 901 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
|
1045 |
if (n.op == TraceOpKind::Input) keep[static_cast<size_t>(n.id)] = true; |
| 708 |
|
|
} |
| 709 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
std::vector<int> stack = out_slots; |
| 710 |
2/2
✓ Branch 0 taken 625 times.
✓ Branch 1 taken 28 times.
|
653 |
while (!stack.empty()) { |
| 711 |
|
625 |
int cur = stack.back(); |
| 712 |
1/2
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
|
625 |
stack.pop_back(); |
| 713 |
4/6
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 625 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 580 times.
✓ Branch 5 taken 45 times.
|
625 |
if (cur < 0 || keep[static_cast<size_t>(cur)]) continue; |
| 714 |
1/2
✓ Branch 0 taken 580 times.
✗ Branch 1 not taken.
|
580 |
keep[static_cast<size_t>(cur)] = true; |
| 715 |
3/4
✓ Branch 0 taken 580 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 881 times.
✓ Branch 3 taken 580 times.
|
1461 |
for (int in_id : bwd.node(cur).inputs) { |
| 716 |
4/6
✓ Branch 0 taken 881 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
✓ Branch 3 taken 296 times.
✓ Branch 4 taken 585 times.
✗ Branch 5 not taken.
|
881 |
if (!keep[static_cast<size_t>(in_id)]) stack.push_back(in_id); |
| 717 |
|
|
} |
| 718 |
|
|
} |
| 719 |
|
|
|
| 720 |
|
28 |
TraceDAG pruned; |
| 721 |
|
28 |
std::unordered_map<int, int> prune_map; |
| 722 |
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1045 times.
|
1073 |
for (const auto& n : bwd.nodes()) { |
| 723 |
3/4
✓ Branch 0 taken 1045 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 321 times.
✓ Branch 3 taken 724 times.
|
1045 |
if (!keep[static_cast<size_t>(n.id)]) continue; |
| 724 |
|
724 |
std::vector<int> ins; |
| 725 |
4/6
✓ Branch 0 taken 724 times.
✓ Branch 1 taken 881 times.
✓ Branch 2 taken 881 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 881 times.
✗ Branch 5 not taken.
|
1605 |
for (int in_id : n.inputs) ins.push_back(prune_map.at(in_id)); |
| 726 |
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
|
724 |
prune_map[n.id] = pruned.add_node(n.op, ins, n.scalar, n.buffer, n.device, n.dtype, n.rows, n.cols, n.scalar2); |
| 727 |
|
724 |
} |
| 728 |
2/2
✓ Branch 0 taken 724 times.
✓ Branch 1 taken 28 times.
|
752 |
for (auto& n : pruned.nodes()) { |
| 729 |
|
724 |
n.outputs.clear(); |
| 730 |
|
724 |
n.is_live_at_end = false; |
| 731 |
|
|
} |
| 732 |
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 724 times.
|
752 |
for (const auto& n : pruned.nodes()) { |
| 733 |
3/4
✓ Branch 0 taken 724 times.
✓ Branch 1 taken 881 times.
✓ Branch 2 taken 881 times.
✗ Branch 3 not taken.
|
1605 |
for (int in_id : n.inputs) pruned.nodes()[static_cast<size_t>(in_id)].outputs.push_back(n.id); |
| 734 |
|
|
} |
| 735 |
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
68 |
for (int os : out_slots) pruned.nodes()[static_cast<size_t>(prune_map.at(os))].is_live_at_end = true; |
| 736 |
|
28 |
return pruned; |
| 737 |
|
100 |
} |
| 738 |
|
|
|
| 739 |
|
|
} // namespace brotensor::jit |
| 740 |
|
|
|