qdq_4.cuh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copied from https://github.com/turboderp/exllamav2
  3. */
  4. #ifndef _qdq_4_cuh
  5. #define _qdq_4_cuh
  6. #include "qdq_util.cuh"
  7. namespace aphrodite {
  8. namespace gptq {
  9. // Permutation:
  10. //
  11. // 77775555 33331111 66664444 22220000
  12. __forceinline__ __device__ void shuffle_4bit_8
  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 < 4; i++)
  22. {
  23. uint32_t qa0 = qa & 0x0f;
  24. uint32_t qa1 = (qa & 0xf0) >> 4;
  25. qa >>= 8;
  26. qb |= (qa1 << (i * 4 + 16));
  27. qb |= (qa0 << (i * 4));
  28. }
  29. q[0] = qb;
  30. }
  31. __forceinline__ __device__ void dequant_4bit_8
  32. (
  33. const uint32_t q_0,
  34. half2 (&dq)[4],
  35. int stride,
  36. const uint32_t zero
  37. )
  38. {
  39. const uint32_t c0 = 0x64006400;
  40. const half y16_ = __float2half_rn(1.0f / 16.0f);
  41. const half2 y16 = __halves2half2(y16_, y16_);
  42. const half_uint16 z1_(0xe400 | zero); // half(-1024.0f - zero);
  43. const half z16_ = __hsub(__int2half_rn(-64), __int2half_rn(zero));
  44. const half2 z1 = __half2half2(z1_.as_half);
  45. const half2 z16 = __half2half2(z16_);
  46. uint32_t qa = q_0;
  47. half2_uint32 q0((qa & 0x000f000f) | c0); // half2(q[ 0], q[ 1]) + 1024
  48. half2_uint32 q1((qa & 0x00f000f0) | c0); // half2(q[ 2], q[ 3]) * 16 + 1024
  49. qa >>= 8;
  50. half2_uint32 q2((qa & 0x000f000f) | c0); // half2(q[ 4], q[ 5]) + 1024
  51. half2_uint32 q3((qa & 0x00f000f0) | c0); // half2(q[ 6], q[ 7]) * 16 + 1024
  52. dq[0] = __hadd2(q0.as_half2, z1);
  53. dq[1] = __hfma2(q1.as_half2, y16, z16);
  54. dq[2] = __hadd2(q2.as_half2, z1);
  55. dq[3] = __hfma2(q3.as_half2, y16, z16);
  56. }
  57. __forceinline__ __device__ void dequant_4bit_8_prep_zero_scale
  58. (
  59. const uint32_t zero,
  60. const half scale,
  61. half2 (&z1z16)[2],
  62. half2 (&y1y16)[2]
  63. )
  64. {
  65. half_uint16 z1(0xe400 | zero); // half(-1024.0f - zero);
  66. half z16 = __hsub(__int2half_rn(-64), __int2half_rn(zero));
  67. half2 scale2 = __half2half2(scale);
  68. z1z16[0] = __hmul2(scale2, __half2half2(z1.as_half));
  69. z1z16[1] = __hmul2(scale2, __half2half2(z16));
  70. const half y1 = __float2half_rn(1.0f);
  71. const half y16 = __float2half_rn(1.0f / 16.0f);
  72. y1y16[0] = __hmul2(scale2, __half2half2(y1));
  73. y1y16[1] = __hmul2(scale2, __half2half2(y16));
  74. }
  75. __forceinline__ __device__ void dequant_4bit_8_prep_zero
  76. (
  77. const uint32_t zero,
  78. half2(&z1z16)[2],
  79. half2(&y1y16)[2]
  80. )
  81. {
  82. half_uint16 z1(0xe400 | zero); // half(-1024.0f - zero);
  83. half z16 = __hsub(__int2half_rn(-64), __int2half_rn(zero));
  84. z1z16[0] = __half2half2(z1.as_half);
  85. z1z16[1] = __half2half2(z16);
  86. const half y1 = __float2half_rn(1.0f);
  87. const half y16 = __float2half_rn(1.0f / 16.0f);
  88. y1y16[0] = __half2half2(y1);
  89. y1y16[1] = __half2half2(y16);
  90. }
  91. __forceinline__ __device__ void dequant_4bit_8_gptq
  92. (
  93. const uint32_t q_0,
  94. half2 (&dq)[4],
  95. half2 (&z1z16)[2],
  96. half2 (&y1y16)[2],
  97. int stride,
  98. bool scaled
  99. )
  100. {
  101. const uint32_t c0 = 0x64006400;
  102. uint32_t qa = q_0;
  103. half2_uint32 q0((qa & 0x000f000f) | c0); // half2( q[0] + 1024, q[1] + 1024 )
  104. half2_uint32 q1((qa & 0x00f000f0) | c0); // half2( q[2] * 16 + 1024, q[3] * 16 + 1024 )
  105. qa >>= 8;
  106. half2_uint32 q2((qa & 0x000f000f) | c0); // half2( q[4] + 1024, q[5] + 1024 )
  107. half2_uint32 q3((qa & 0x00f000f0) | c0); // half2( q[6] * 16 + 1024, q[7] * 16 + 1024 )
  108. if (scaled)
  109. {
  110. dq[0] = __hfma2(q0.as_half2, y1y16[0], z1z16[0]); // half2( q[0] * s - z * s, q[1] * s - z * s)
  111. dq[1] = __hfma2(q1.as_half2, y1y16[1], z1z16[1]); // half2( q[2] * s - z * s, q[3] * s - z * s)
  112. dq[2] = __hfma2(q2.as_half2, y1y16[0], z1z16[0]);
  113. dq[3] = __hfma2(q3.as_half2, y1y16[1], z1z16[1]);
  114. }
  115. else
  116. {
  117. dq[0] = __hadd2(q0.as_half2, z1z16[0]); // half2( q[0] - z, q[1] - z )
  118. dq[1] = __hfma2(q1.as_half2, y1y16[1], z1z16[1]); // half2( q[2] - z, q[3] - z )
  119. dq[2] = __hadd2(q2.as_half2, z1z16[0]); // half2( q[4] - z, q[5] - z )
  120. dq[3] = __hfma2(q3.as_half2, y1y16[1], z1z16[1]); // half2( q[6] - z, q[7] - z )
  121. }
  122. }
  123. } // namespace gptq
  124. } // namespace aphrodite
  125. #endif