|
| 1 | +#include <sycl/sycl.hpp> |
| 2 | +#include <iostream> |
| 3 | +#include <vector> |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +using namespace sycl; |
| 7 | + |
| 8 | +void dequantize_q2_k_fp16_kernel(const int8_t *data, sycl::half *output, |
| 9 | + const int blk_size, const int ele_per_blk, |
| 10 | + const int num_blocks, |
| 11 | + const sycl::nd_item<3> &item_ct1) { |
| 12 | + long long global_idx = item_ct1.get_group(2) * item_ct1.get_local_range(2) + |
| 13 | + item_ct1.get_local_id(2); |
| 14 | + for (long long block_id = global_idx; block_id < num_blocks; |
| 15 | + block_id += |
| 16 | + item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) { |
| 17 | + sycl::half *__restrict__ output_blk = |
| 18 | + (sycl::half *)(output + block_id * ele_per_blk); |
| 19 | + |
| 20 | + const float d = |
| 21 | + sycl::vec<sycl::half, 1>(*(reinterpret_cast<const sycl::half *>( |
| 22 | + data + block_id * blk_size + 80))) |
| 23 | + .convert<float, sycl::rounding_mode::automatic>()[0]; |
| 24 | + const float min = |
| 25 | + sycl::vec<sycl::half, 1>(*(reinterpret_cast<const sycl::half *>( |
| 26 | + data + block_id * blk_size + 82))) |
| 27 | + .convert<float, sycl::rounding_mode::automatic>()[0]; |
| 28 | + |
| 29 | + const uint8_t * __restrict__ q = (uint8_t*)(data + block_id * blk_size + 16); |
| 30 | + |
| 31 | + int is = 0; |
| 32 | + float dl, ml; |
| 33 | + |
| 34 | + for (int n = 0; n < 256; n += 128) { |
| 35 | + int shift = 0; |
| 36 | + for (int j = 0; j < 4; ++j) { |
| 37 | + uint8_t* scales = (uint8_t*)(data + block_id * blk_size + (is++)); |
| 38 | + uint8_t sc = *scales; |
| 39 | + dl = d * (sc & 0xF); ml = min * (sc >> 4); |
| 40 | + for (int l = 0; l < 16; ++l) *output_blk++ = |
| 41 | + sycl::vec<float, 1>(dl * ((int8_t)((q[l] >> shift) & 3)) - |
| 42 | + ml) |
| 43 | + .convert<sycl::half, |
| 44 | + sycl::rounding_mode::automatic>()[0]; |
| 45 | + |
| 46 | + scales = (uint8_t*)(data + block_id * blk_size + (is++)); |
| 47 | + sc = *scales; |
| 48 | + |
| 49 | + dl = d * (sc & 0xF); ml = min * (sc >> 4); |
| 50 | + for (int l = 0; l < 16; ++l) *output_blk++ = |
| 51 | + sycl::vec<float, 1>( |
| 52 | + dl * ((int8_t)((q[l + 16] >> shift) & 3)) - ml) |
| 53 | + .convert<sycl::half, |
| 54 | + sycl::rounding_mode::automatic>()[0]; |
| 55 | + |
| 56 | + shift += 2; |
| 57 | + } |
| 58 | + q += 32; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +int main() { |
| 64 | + // Define the parameters |
| 65 | + const int blk_size = 128 + 16 + 2 * sizeof(sycl::half); // Adjusted to match the kernel's data layout |
| 66 | + const int ele_per_blk = 256; |
| 67 | + const int num_blocks = 2; |
| 68 | + |
| 69 | + // Initialize input data |
| 70 | + std::vector<int8_t> data(blk_size * num_blocks); |
| 71 | + std::vector<sycl::half> output(ele_per_blk * num_blocks, 0.0f); |
| 72 | + |
| 73 | + // Fill the data with some values |
| 74 | + for (int i = 0; i < num_blocks; ++i) { |
| 75 | + sycl::half d = 0.5f; |
| 76 | + sycl::half min = 0.1f; |
| 77 | + std::memcpy(data.data() + i * blk_size + 80, &d, sizeof(sycl::half)); |
| 78 | + std::memcpy(data.data() + i * blk_size + 82, &min, sizeof(sycl::half)); |
| 79 | + for (int j = 0; j < 16; ++j) { |
| 80 | + data[i * blk_size + j] = j; |
| 81 | + } |
| 82 | + for (int j = 16; j < 128 + 16; ++j) { |
| 83 | + data[i * blk_size + j] = (j - 16) % 256; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Create a SYCL queue |
| 88 | + queue q; |
| 89 | + |
| 90 | + // Allocate device memory |
| 91 | + int8_t* d_data = malloc_device<int8_t>(data.size(), q); |
| 92 | + sycl::half* d_output = malloc_device<sycl::half>(output.size(), q); |
| 93 | + |
| 94 | + // Copy data to device |
| 95 | + q.memcpy(d_data, data.data(), data.size() * sizeof(int8_t)).wait(); |
| 96 | + q.memcpy(d_output, output.data(), output.size() * sizeof(sycl::half)).wait(); |
| 97 | + |
| 98 | + // Define the kernel execution configuration |
| 99 | + range<3> global_work_size(1, 1, num_blocks); |
| 100 | + range<3> local_work_size(1, 1, 1); |
| 101 | + |
| 102 | + // Launch the kernel |
| 103 | + q.submit([&](handler& h) { |
| 104 | + h.parallel_for(nd_range<3>(global_work_size, local_work_size), [=](nd_item<3> item_ct1) { |
| 105 | + dequantize_q2_k_fp16_kernel(d_data, d_output, blk_size, ele_per_blk, num_blocks, item_ct1); |
| 106 | + }); |
| 107 | + }).wait(); |
| 108 | + |
| 109 | + // Copy the result back to host |
| 110 | + q.memcpy(output.data(), d_output, output.size() * sizeof(sycl::half)).wait(); |
| 111 | + |
| 112 | + // Free device memory |
| 113 | + free(d_data, q); |
| 114 | + free(d_output, q); |
| 115 | + |
| 116 | + // Check the results |
| 117 | + bool success = true; |
| 118 | + for (int i = 0; i < num_blocks; ++i) { |
| 119 | + sycl::half d = 0.5f; |
| 120 | + sycl::half min = 0.1f; |
| 121 | + for (int j = 0; j < ele_per_blk; ++j) { |
| 122 | + // Calculate expected value |
| 123 | + int block_offset = i * blk_size; |
| 124 | + int q_offset = block_offset + 16 + (j / 128) * 32; |
| 125 | + int scale_offset = block_offset + (j / 64) * 2; |
| 126 | + uint8_t sc = data[scale_offset]; |
| 127 | + float dl = d * (sc & 0xF); |
| 128 | + float ml = min * (sc >> 4); |
| 129 | + int q_idx = (j % 64) / 16; |
| 130 | + int shift = (j % 16) * 2; |
| 131 | + int8_t q_val = (data[q_offset + q_idx] >> shift) & 3; |
| 132 | + float expected = dl * q_val - ml; |
| 133 | + sycl::half expected_half = sycl::vec<float, 1>(expected).convert<sycl::half, sycl::rounding_mode::automatic>()[0]; |
| 134 | + |
| 135 | + if (std::fabs(static_cast<float>(output[i * ele_per_blk + j]) - static_cast<float>(expected_half)) > 1e-3) { |
| 136 | + success = false; |
| 137 | + std::cout << "Mismatch at block " << i << ", element " << j << ": expected " << static_cast<float>(expected_half) << ", got " << static_cast<float>(output[i * ele_per_blk + j]) << std::endl; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (success) { |
| 143 | + std::cout << "Test passed!" << std::endl; |
| 144 | + } else { |
| 145 | + std::cout << "Test failed!" << std::endl; |
| 146 | + } |
| 147 | + |
| 148 | + return 0; |
| 149 | +} |
0 commit comments