// C ABI over tt-metal's own host BFP tile packers (tt_metal/impl/data_format), for the bit-exactness test of // fnx.quant.bfp (ttnn has no bfloat2_b DataType, so Bfp2_b cannot be reached through ttnn.from_torch). // Input: tile-contiguous float32 (tile k = values [1024k, 1024k+1024), each tile row-major 32x32), exp_b format. // Built inside the TT image by fnx/tests/test_quant_bfp.py. #include #include #include #include #include #include #include extern "C" { // fmt: 2, 4 or 8. Returns the number of uint32 words written (<= cap), or -1. int64_t ttbfp_pack(int fmt, const float* data, int64_t n, uint32_t* out, int64_t cap) { ttsl::Span s(data, static_cast(n)); std::vector w; if (fmt == 2) { w = pack_as_bfp2_tiles(s, /*row_major_input=*/true, /*is_exp_a=*/false); } else if (fmt == 4) { w = pack_as_bfp4_tiles(s, true, false); } else if (fmt == 8) { w = pack_as_bfp8_tiles(s, true, false); } else { return -1; } if (static_cast(w.size()) > cap) return -1; std::memcpy(out, w.data(), w.size() * sizeof(uint32_t)); return static_cast(w.size()); } // Inverse: words -> n floats (tile-contiguous, row-major inside each tile). Returns n or -1. int64_t ttbfp_unpack(int fmt, const uint32_t* words, int64_t nw, float* out, int64_t n) { ttsl::Span s(words, static_cast(nw)); std::vector v; if (fmt == 2) { v = unpack_bfp2_tiles_into_float_vec(s, /*row_major_output=*/true, /*is_exp_a=*/false); } else if (fmt == 4) { v = unpack_bfp4_tiles_into_float_vec(s, true, false); } else if (fmt == 8) { v = unpack_bfp8_tiles_into_float_vec(s, true, false); } else { return -1; } if (static_cast(v.size()) != n) return -1; std::memcpy(out, v.data(), v.size() * sizeof(float)); return n; } } // extern "C"