dequantize.cuh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "common.cuh"
  2. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  3. const block_q4_0 * x = (const block_q4_0 *) vx;
  4. const dfloat d = x[ib].d;
  5. const int vui = x[ib].qs[iqs];
  6. v.x = vui & 0xF;
  7. v.y = vui >> 4;
  8. #ifdef GGML_CUDA_F16
  9. v = __hsub2(v, {8.0f, 8.0f});
  10. v = __hmul2(v, {d, d});
  11. #else
  12. v.x = (v.x - 8.0f) * d;
  13. v.y = (v.y - 8.0f) * d;
  14. #endif // GGML_CUDA_F16
  15. }
  16. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  17. const block_q4_1 * x = (const block_q4_1 *) vx;
  18. const dfloat d = __low2half(x[ib].dm);
  19. const dfloat m = __high2half(x[ib].dm);
  20. const int vui = x[ib].qs[iqs];
  21. v.x = vui & 0xF;
  22. v.y = vui >> 4;
  23. #ifdef GGML_CUDA_F16
  24. v = __hmul2(v, {d, d});
  25. v = __hadd2(v, {m, m});
  26. #else
  27. v.x = (v.x * d) + m;
  28. v.y = (v.y * d) + m;
  29. #endif // GGML_CUDA_F16
  30. }
  31. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  32. const block_q5_0 * x = (const block_q5_0 *) vx;
  33. const dfloat d = x[ib].d;
  34. uint32_t qh;
  35. memcpy(&qh, x[ib].qh, sizeof(qh));
  36. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  37. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  38. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  39. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  40. #ifdef GGML_CUDA_F16
  41. v = __hsub2(v, {16.0f, 16.0f});
  42. v = __hmul2(v, {d, d});
  43. #else
  44. v.x = (v.x - 16.0f) * d;
  45. v.y = (v.y - 16.0f) * d;
  46. #endif // GGML_CUDA_F16
  47. }
  48. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  49. const block_q5_1 * x = (const block_q5_1 *) vx;
  50. const dfloat d = __low2half(x[ib].dm);
  51. const dfloat m = __high2half(x[ib].dm);
  52. uint32_t qh;
  53. memcpy(&qh, x[ib].qh, sizeof(qh));
  54. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  55. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  56. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  57. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  58. #ifdef GGML_CUDA_F16
  59. v = __hmul2(v, {d, d});
  60. v = __hadd2(v, {m, m});
  61. #else
  62. v.x = (v.x * d) + m;
  63. v.y = (v.y * d) + m;
  64. #endif // GGML_CUDA_F16
  65. }
  66. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
  67. const block_q8_0 * x = (const block_q8_0 *) vx;
  68. const dfloat d = x[ib].d;
  69. v.x = x[ib].qs[iqs + 0];
  70. v.y = x[ib].qs[iqs + 1];
  71. #ifdef GGML_CUDA_F16
  72. v = __hmul2(v, {d, d});
  73. #else
  74. v.x *= d;
  75. v.y *= d;
  76. #endif // GGML_CUDA_F16
  77. }