| 25 | } |
| 26 | |
| 27 | bool check_format(int gu, int down, int ff, bool folded = false) { |
| 28 | cpu::NativeFmt fmt; |
| 29 | std::string error; |
| 30 | if (!cpu::native_fmt(gu, down, hidden, ff, fmt, error)) { |
| 31 | std::fprintf(stderr, "%s\n", error.c_str()); |
| 32 | return false; |
| 33 | } |
| 34 | std::vector<float> gu_signs(hidden), down_signs(ff); |
| 35 | if (folded) { |
| 36 | for (int i = 0; i < hidden; ++i) gu_signs[i] = i % 3 ? 1.f : -1.f; |
| 37 | for (int i = 0; i < ff; ++i) down_signs[i] = i % 5 ? 1.f : -1.f; |
| 38 | fmt.had_block = 128; |
| 39 | fmt.had_gu = fmt.had_d = true; |
| 40 | fmt.gu_signs = gu_signs.data(); |
| 41 | fmt.d_signs = down_signs.data(); |
| 42 | } |
| 43 | std::mt19937 rng(20261016 + gu * 100 + down + ff); |
| 44 | std::vector<uint8_t> blob(fmt.bytes); |
| 45 | for (auto &byte : blob) |
| 46 | byte = static_cast<uint8_t>(rng()); |
| 47 | // Use finite block scales and random packed values. NVFP4 has four UE4M3 sub-block scales. |
| 48 | const uint16_t scale = 0x2400; // 2^-6; exclude NaN/Inf scales from the random bytes. |
| 49 | auto scales = [&](size_t first, size_t last, int type) { |
| 50 | const size_t block = cpu::tq_supported(type) ? cpu::tq_block_bytes(type) : ggml_type_size(static_cast<ggml_type>(type)); |
| 51 | for (size_t at = first; at < last; at += block) { |
| 52 | if (type == cpu::kNvfp4Type) std::memset(blob.data() + at, 0x28, 4); // finite UE4M3 sub-block scales |
| 53 | else { |
| 54 | std::memcpy(blob.data() + at, &scale, sizeof(scale)); |
| 55 | if (type == 12 || type == 7) std::memcpy(blob.data() + at + 2, &scale, sizeof(scale)); |
| 56 | } |
| 57 | } |
| 58 | }; |
| 59 | scales(0, fmt.down_off, gu); |
| 60 | scales(fmt.down_off, fmt.tail_off ? fmt.tail_off : blob.size(), down); |
| 61 | if (fmt.tail_off) { |
| 62 | const float tail[4] = {gu == cpu::kNvfp4Type ? .5f : 1.f, gu == cpu::kNvfp4Type ? .75f : 1.f, |
| 63 | down == cpu::kNvfp4Type ? 1.25f : 1.f, 0.f}; |
| 64 | std::memcpy(blob.data() + fmt.tail_off, tail, sizeof(tail)); |
| 65 | } |
| 66 | |
| 67 | std::vector<std::vector<uint8_t>> act(width, std::vector<uint8_t>(fmt.act_bytes)); |
| 68 | std::vector<std::vector<uint8_t>> hq(width, std::vector<uint8_t>(fmt.h_bytes)); |
| 69 | std::vector<std::vector<float>> gates(width, std::vector<float>(ff)), downs(width, std::vector<float>(hidden)); |
| 70 | auto solo_gates = gates, solo_downs = downs; |
| 71 | const void *ap[width], *hp[width]; |
| 72 | float *gp[width], *dp[width], *sg[width], *sd[width]; |
| 73 | for (int t = 0; t < width; ++t) { |
| 74 | ap[t] = act[t].data(); |
| 75 | hp[t] = hq[t].data(); |
| 76 | gp[t] = gates[t].data(); |
| 77 | dp[t] = downs[t].data(); |
| 78 | sg[t] = solo_gates[t].data(); |
| 79 | sd[t] = solo_downs[t].data(); |
| 80 | } |
| 81 | for (float amplitude : {0.f, .01f, .3f, 3.f}) { |
| 82 | std::vector<float> x(hidden), h(ff); |
| 83 | for (int t = 0; t < width; ++t) { |
| 84 | for (auto &v : x) |
no test coverage detected