1
0

qdq_2.cuh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copied from https://github.com/turboderp/exllamav2
  3. */
  4. #ifndef _qdq_2_cuh
  5. #define _qdq_2_cuh
  6. #include "qdq_util.cuh"
  7. namespace aphrodite {
  8. namespace gptq {
  9. // Permutation:
  10. //
  11. // ffddbb99 77553311 eeccaa88 66442200
  12. __forceinline__ __device__ void shuffle_2bit_16
  13. (
  14. uint32_t* q,
  15. int stride
  16. )
  17. {
  18. uint32_t qa = q[0];
  19. uint32_t qb = 0;
  20. #pragma unroll
  21. for (int i = 0; i < 8; i++)
  22. {
  23. uint32_t qa0 = qa & 0x03;
  24. uint32_t qa1 = (qa & 0x0c) >> 2;
  25. qa >>= 4;
  26. qb |= (qa1 << (i * 2 + 16));
  27. qb |= (qa0 << (i * 2));
  28. }
  29. q[0] = qb;
  30. }
  31. __forceinline__ __device__ void dequant_2bit_16
  32. (
  33. const uint32_t q_0,
  34. half2 (&dq)[8],
  35. int stride,
  36. const uint32_t zero
  37. )
  38. {
  39. const uint32_t c0 = 0x64006400;
  40. const half y4_ = __float2half_rn(1.0f / 4.0f);
  41. const half y16_ = __float2half_rn(1.0f / 16.0f);
  42. const half y64_ = __float2half_rn(1.0f / 64.0f);
  43. const half2 y4 = __halves2half2(y4_, y4_);
  44. const half2 y16 = __halves2half2(y16_, y16_);
  45. const half2 y64 = __halves2half2(y64_, y64_);
  46. const half_uint16 z1_(0xe400 | zero); // half(-1024.0f - zero);
  47. const half z4_ = __hsub(__int2half_rn(-256), __int2half_rn(zero));
  48. const half z16_ = __hsub(__int2half_rn(-64), __int2half_rn(zero));
  49. const half z64_ = __hsub(__int2half_rn(-16), __int2half_rn(zero));
  50. const half2 z1 = __half2half2(z1_.as_half);
  51. const half2 z4 = __half2half2(z4_);
  52. const half2 z16 = __half2half2(z16_);
  53. const half2 z64 = __half2half2(z64_);
  54. uint32_t qa = q_0;
  55. half2_uint32 q0((qa & 0x00030003) | c0); // half2(q[ 0], q[ 1]) + 1024
  56. half2_uint32 q1((qa & 0x000c000c) | c0); // half2(q[ 2], q[ 3]) * 4 + 1024
  57. half2_uint32 q2((qa & 0x00300030) | c0); // half2(q[ 4], q[ 5]) * 16 + 1024
  58. half2_uint32 q3((qa & 0x00c000c0) | c0); // half2(q[ 6], q[ 7]) * 64 + 1024
  59. qa >>= 8;
  60. half2_uint32 q4((qa & 0x00030003) | c0); // half2(q[ 8], q[ 8]) + 1024
  61. half2_uint32 q5((qa & 0x000c000c) | c0); // half2(q[10], q[11]) * 4 + 1024
  62. half2_uint32 q6((qa & 0x00300030) | c0); // half2(q[12], q[13]) * 16 + 1024
  63. half2_uint32 q7((qa & 0x00c000c0) | c0); // half2(q[14], q[15]) * 64 + 1024
  64. dq[0] = __hadd2(q0.as_half2, z1);
  65. dq[1] = __hfma2(q1.as_half2, y4, z4);
  66. dq[2] = __hfma2(q2.as_half2, y16, z16);
  67. dq[3] = __hfma2(q3.as_half2, y64, z64);
  68. dq[4] = __hadd2(q4.as_half2, z1);
  69. dq[5] = __hfma2(q5.as_half2, y4, z4);
  70. dq[6] = __hfma2(q6.as_half2, y16, z16);
  71. dq[7] = __hfma2(q7.as_half2, y64, z64);
  72. }
  73. } // namespace gptq
  74. } // namespace aphrodite
  75. #endif