src/kernels/mir_norm.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Backend-neutral brass MIR kernel for row normalizations, row softmax, | ||
| 4 | // and their column reductions (LayerNorm/RMSNorm/L2/Softmax fwd+bwd + col sums). | ||
| 5 | |||
| 6 | #include <brotensor/tensor.h> | ||
| 7 | |||
| 8 | #include <brass/mir/function.hpp> | ||
| 9 | #include <brass/mir/module.hpp> | ||
| 10 | |||
| 11 | #include <cstdint> | ||
| 12 | |||
| 13 | namespace brotensor::kernels { | ||
| 14 | |||
| 15 | constexpr std::uint32_t kNormBlock = 256; | ||
| 16 | |||
| 17 | struct NormMirFlags { | ||
| 18 | bool has_g = false; | ||
| 19 | bool has_b = false; | ||
| 20 | bool has_xh = false; | ||
| 21 | bool has_s0 = false; | ||
| 22 | bool has_s1 = false; | ||
| 23 | |||
| 24 | std::uint8_t pack() const noexcept { | ||
| 25 | return static_cast<std::uint8_t>( | ||
| 26 | (has_g ? 1 : 0) | (has_b ? 2 : 0) | (has_xh ? 4 : 0) | | ||
| 27 | (has_s0 ? 8 : 0) | (has_s1 ? 16 : 0)); | ||
| 28 | } | ||
| 29 | 65 | static NormMirFlags unpack(std::uint8_t m) noexcept { | |
| 30 | 390 | return { | |
| 31 | 325 | (m & 1) != 0, (m & 2) != 0, (m & 4) != 0, (m & 8) != 0, (m & 16) != 0}; | |
| 32 | } | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Params (11): (ptr x, ptr y, ptr g, ptr b, ptr xh, ptr s0, ptr s1, | ||
| 36 | // i32 rows, i32 d, f32 eps, f32 rstd). | ||
| 37 | brass::Function* build_norm_mir(brass::Module& mod, std::uint32_t mode, Dtype dt_x, Dtype dt_y, | ||
| 38 | Dtype dt_g, NormMirFlags flags, int lanes); | ||
| 39 | |||
| 40 | } // namespace brotensor::kernels | ||
| 41 |