#include "strata/prefill/moe_fused_q3k.hpp" #include #include #include #include #include #include #include static void check(cudaError_t error) { if (error != cudaSuccess) { std::fprintf(stderr, "%s\n", cudaGetErrorString(error)); std::exit(1); } } static void check_rows(bool same_inputs) { constexpr int K = 2560, G = 8, ELL = 1792, M = 576; constexpr size_t xrow = K / 32 * 36, yrow = G * ELL / 32 * 36; std::vector x(M * xrow); for (int row = 0; row < M; ++row) for (int block = 0; block < K / 32; ++block) { uint8_t* p = x.data() + row * xrow + block * 36; const uint16_t scale = 0x3000; std::memcpy(p, &scale, 2); int sum = 0; for (int j = 0; j < 32; ++j) { const int q = ((same_inputs ? 0 : row) + block + j) % 63 - 31; p[4 + j] = (uint8_t) q; sum += q; } const __half scaled_sum = __float2half_rn((float) sum * 0.125f); std::memcpy(p + 2, &scaled_sum, 2); } void *dx = nullptr, *dw = nullptr, *dy = nullptr; float* dinv = nullptr; std::vector inv(ELL); for (int j = 0; j < ELL; ++j) inv[j] = 0.5f + (float) (j % 17) / 16.0f; check(cudaMalloc((void**) &dinv, inv.size() * sizeof(float))); check(cudaMemcpy(dinv, inv.data(), inv.size() * sizeof(float), cudaMemcpyHostToDevice)); check(cudaMalloc(&dx, x.size())); check(cudaMalloc(&dy, M * yrow)); check(cudaMemcpy(dx, x.data(), x.size(), cudaMemcpyHostToDevice)); std::vector expected(M * yrow), actual(M * yrow); for (int fmt : {4, 8}) { const size_t block_bytes = fmt == 4 ? 18 : 34; std::vector w(G * ELL * (K / 32) * block_bytes); for (size_t block = 0; block < w.size() / block_bytes; ++block) { uint8_t* p = w.data() + block * block_bytes; const uint16_t scale = 0x3800; std::memcpy(p, &scale, 2); for (size_t j = 2; j < block_bytes; ++j) p[j] = (uint8_t) ((block * 13 + j * 7) % 251); } check(cudaMalloc(&dw, w.size())); check(cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice)); strata::prefill::fused_q3k::latent_dense_d(dw, dx, M, K, G, ELL, dy, nullptr, fmt, dinv); check(cudaDeviceSynchronize()); check(cudaMemcpy(expected.data(), dy, expected.size(), cudaMemcpyDeviceToHost)); if (same_inputs) for (int row = 1; row < M; ++row) if (std::memcmp(expected.data(), expected.data() + row * yrow, yrow) != 0) { std::fprintf(stderr, "identical inputs differ at row %d, fmt %d\n", row, fmt); std::exit(1); } for (int rows : {1, 2, 3, 5, 7, 8, 9, 15, 16, 17, 63, 64, 65, 69, 70, 71, 127, 128, 129, 511, 512, 513}) { check(cudaMemset(dy, 0, M * yrow)); strata::prefill::fused_q3k::latent_dense_d( dw, dx, rows, K, G, ELL, dy, nullptr, fmt, dinv); check(cudaDeviceSynchronize()); check(cudaMemcpy(actual.data(), dy, rows * yrow, cudaMemcpyDeviceToHost)); if (std::memcmp(actual.data(), expected.data(), rows * yrow) != 0) { std::fprintf(stderr, "fmt %d, rows %d differ from full tiles\n", fmt, rows); std::exit(1); } } check(cudaFree(dw)); } check(cudaFree(dinv)); check(cudaFree(dx)); check(cudaFree(dy)); } int main(int argc, char** argv) { check(cudaSetDevice(argc > 1 ? std::atoi(argv[1]) : 0)); check_rows(false); check_rows(true); std::puts("partial and identical latent D rows match, Q4 and Q8"); }