qdq_2.cuh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Adapted from https://github.com/turboderp/exllamav2
  3. * Copyright (c) 2024 turboderp
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #ifndef _qdq_2_cuh
  24. #define _qdq_2_cuh
  25. #include "qdq_util.cuh"
  26. namespace aphrodite {
  27. namespace exl2 {
  28. // Permutation:
  29. //
  30. // ffddbb99 77553311 eeccaa88 66442200
  31. __forceinline__ __device__ void shuffle_2bit_16
  32. (
  33. uint32_t* q,
  34. int stride
  35. )
  36. {
  37. uint32_t qa = q[0];
  38. uint32_t qb = 0;
  39. #pragma unroll
  40. for (int i = 0; i < 8; i++)
  41. {
  42. uint32_t qa0 = qa & 0x03;
  43. uint32_t qa1 = (qa & 0x0c) >> 2;
  44. qa >>= 4;
  45. qb |= (qa1 << (i * 2 + 16));
  46. qb |= (qa0 << (i * 2));
  47. }
  48. q[0] = qb;
  49. }
  50. __forceinline__ __device__ void dequant_2bit_16
  51. (
  52. const uint32_t q_0,
  53. half2 (&dq)[8],
  54. int stride
  55. )
  56. {
  57. const uint32_t c0 = 0x64006400;
  58. const half y4_ = __float2half_rn(1.0f / 4.0f);
  59. const half y16_ = __float2half_rn(1.0f / 16.0f);
  60. const half y64_ = __float2half_rn(1.0f / 64.0f);
  61. const half2 y4 = __halves2half2(y4_, y4_);
  62. const half2 y16 = __halves2half2(y16_, y16_);
  63. const half2 y64 = __halves2half2(y64_, y64_);
  64. const half z1_ = __float2half_rn(-1024.0f - 2.0f);
  65. const half z4_ = __float2half_rn(-1024.0f / 4.0f - 2.0f);
  66. const half z16_ = __float2half_rn(-1024.0f / 16.0f - 2.0f);
  67. const half z64_ = __float2half_rn(-1024.0f / 64.0f - 2.0f);
  68. const half2 z1 = __halves2half2(z1_, z1_);
  69. const half2 z4 = __halves2half2(z4_, z4_);
  70. const half2 z16 = __halves2half2(z16_, z16_);
  71. const half2 z64 = __halves2half2(z64_, z64_);
  72. uint32_t qa = q_0;
  73. half2_uint32 q0((qa & 0x00030003) | c0); // half2(q[ 0], q[ 1]) + 1024
  74. half2_uint32 q1((qa & 0x000c000c) | c0); // half2(q[ 2], q[ 3]) * 4 + 1024
  75. half2_uint32 q2((qa & 0x00300030) | c0); // half2(q[ 4], q[ 5]) * 16 + 1024
  76. half2_uint32 q3((qa & 0x00c000c0) | c0); // half2(q[ 6], q[ 7]) * 64 + 1024
  77. qa >>= 8;
  78. half2_uint32 q4((qa & 0x00030003) | c0); // half2(q[ 8], q[ 8]) + 1024
  79. half2_uint32 q5((qa & 0x000c000c) | c0); // half2(q[10], q[11]) * 4 + 1024
  80. half2_uint32 q6((qa & 0x00300030) | c0); // half2(q[12], q[13]) * 16 + 1024
  81. half2_uint32 q7((qa & 0x00c000c0) | c0); // half2(q[14], q[15]) * 64 + 1024
  82. dq[0] = __hadd2(q0.as_half2, z1);
  83. dq[1] = __hfma2(q1.as_half2, y4, z4);
  84. dq[2] = __hfma2(q2.as_half2, y16, z16);
  85. dq[3] = __hfma2(q3.as_half2, y64, z64);
  86. dq[4] = __hadd2(q4.as_half2, z1);
  87. dq[5] = __hfma2(q5.as_half2, y4, z4);
  88. dq[6] = __hfma2(q6.as_half2, y16, z16);
  89. dq[7] = __hfma2(q7.as_half2, y64, z64);
  90. }
  91. } // namespace exl2
  92. } // namespace aphrodite
  93. #endif