brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
99 / 102
97.1%
Branches:
46 / 94
48.9%
Functions:
11 / 11
100.0%

src/kernels/mir_linear_bwd.cpp
Line Branch Exec Source
1 #include "mir_linear_bwd.h"
2 #include "mir_common.h"
3
4 #include <stdexcept>
5 #include <vector>
6
7 namespace brotensor::kernels {
8
9 namespace {
10
11 6 Value* load_one_f32(Ctx& c, Dtype dt, Value* base, Value* idx) {
12 6 return load_elem_dt(c, dt, base, idx);
13 }
14
15 3 void store_one_f32(Ctx& c, Dtype dt, Value* base, Value* idx, Value* v) {
16 3 store_elem_dt(c, dt, base, idx, v);
17 3 }
18
19 } // namespace
20
21 3 Function* build_linear_bwd_mir(Module& mod, std::uint32_t op, Dtype dt) {
22
2/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 if (dt != Dtype::FP32 && dt != Dtype::FP16 && dt != Dtype::BF16) {
23
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_linear_bwd_mir: unsupported dtype");
24 }
25
26
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (op == 0u) {
27 // p: W(0), dY(1), dX(2), out_dim(3), in_dim(4)
28 2 Function* fn = mod.create_function(
29 1 "mir_linear_bwd_dx", Type::void_type(),
30 1 {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()});
31 1 brass::codegen::KernelBuilder kb(mod, fn);
32
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
33
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
34
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* dytile = kb.shared_alloc_f32(128u);
35
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
36
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* col = kb.global_tid_x();
37 1 Value* out_dim = p[3];
38 1 Value* in_dim = p[4];
39
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* col_ok = c.b.build_slt(col, in_dim);
40
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const int esz = elem_bytes(dt);
41
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const int shift = log2_exact(esz);
42
43
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* acc = kb.for_range_reduce(
44
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 c.i32(0), out_dim, c.i32(128), c.f32(0.0f),
45
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* t0, Value* cur) -> Value* {
46 1 Value* rem = c.b.build_sub(out_dim, t0);
47 1 Value* t_len = smin_i32(c, c.i32(128), rem);
48
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.if_then(c.b.build_slt(tid, t_len), [&] {
49 1 Value* v = load_one_f32(c, dt, p[1], c.b.build_add(t0, tid));
50 1 kb.shared_store_f32_indexed(dytile, tid, v);
51 1 });
52 1 kb.sync();
53
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* nxt = emit_if_else_values(
54 1 c, col_ok, {Type::f32()},
55 2 [&]() -> std::vector<Value*> {
56 1 Value* w_ptr = ptr_at(
57 1 c, p[0],
58 3 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(t0),
59 1 c.b.build_zext_i64(in_dim)),
60 1 c.b.build_zext_i64(col)),
61 1 esz);
62 1 Value* in64_bytes = c.b.build_shl(c.b.build_zext_i64(in_dim), kb.const_i64(shift));
63
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> r = kb.for_range_reduce_n(
64 1 c.i32(0), t_len, c.i32(1), {cur, w_ptr},
65
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* k, const std::vector<Value*>& st) -> std::vector<Value*> {
66 1 Value* wv = load_lanes(c, dt, 1, st[1])[0];
67 1 Value* dyv = kb.shared_load_f32_indexed(dytile, k);
68 1 Value* sum = c.b.build_fma(wv, dyv, st[0]);
69 1 return {sum, c.b.build_add(st[1], in64_bytes)};
70 });
71
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return {r[0]};
72 1 },
73 2 [&]() -> std::vector<Value*> { return {cur}; })[0];
74 1 kb.sync();
75 1 return nxt;
76 ✗ });
77
78
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 kb.if_then(col_ok, [&] { store_one_f32(c, dt, p[2], col, acc); });
79
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
80
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
81 1 return fn;
82 1 }
83
84
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (op == 1u) {
85 // p: dY(0), x(1), dW(2), out_dim(3), in_dim(4)
86 2 Function* fn = mod.create_function(
87 1 "mir_linear_bwd_dw", Type::void_type(),
88 1 {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()});
89 1 brass::codegen::KernelBuilder kb(mod, fn);
90
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
91
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
92
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* idx = kb.global_tid_x();
93 1 Value* in_dim = p[4];
94
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* total = c.b.build_mul(p[3], in_dim);
95
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 kb.if_then(c.b.build_ult(idx, total), [&] {
96 1 Value* i = c.b.build_udiv(idx, in_dim);
97 1 Value* j = c.b.build_umod(idx, in_dim);
98 1 Value* prev = load_one_f32(c, dt, p[2], idx);
99 1 Value* dy = load_one_f32(c, dt, p[0], i);
100 1 Value* xv = load_one_f32(c, dt, p[1], j);
101 1 store_one_f32(c, dt, p[2], idx, c.b.build_fma(dy, xv, prev));
102 1 });
103
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
104
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
105 1 return fn;
106 1 }
107
108
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (op == 2u) {
109 // p: dY(0), dB(1), out_dim(2)
110 2 Function* fn = mod.create_function(
111 1 "mir_linear_bwd_db", Type::void_type(),
112 1 {Type::ptr(), Type::ptr(), Type::i32()});
113 1 brass::codegen::KernelBuilder kb(mod, fn);
114
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
115
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
116
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* i = kb.global_tid_x();
117
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 kb.if_then(c.b.build_ult(i, p[2]), [&] {
118 1 Value* dy = load_one_f32(c, dt, p[0], i);
119 1 Value* db = load_one_f32(c, dt, p[1], i);
120 1 store_one_f32(c, dt, p[1], i, c.b.build_add(db, dy));
121 1 });
122
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
123
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
124 1 return fn;
125 1 }
126
127
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_linear_bwd_mir: unknown op");
128 3 }
129
130 } // namespace brotensor::kernels
131