matrix_view.cuh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Adapted from https://github.com/turboderp/exllamav2 and https://github.com/turboderp/exllama
  3. */
  4. #ifndef _matrix_view_cuh
  5. #define _matrix_view_cuh
  6. #include <cuda_runtime.h>
  7. #include <cuda_fp16.h>
  8. #include "qdq_util.cuh"
  9. namespace aphrodite {
  10. namespace gptq {
  11. class MatrixView_half
  12. {
  13. public:
  14. const half* data;
  15. const int height;
  16. const int width;
  17. __device__ __forceinline__ MatrixView_half(const half* data, const int height, const int width)
  18. : data(data), height(height), width(width)
  19. { }
  20. __device__ __forceinline__ half item(int row, int column) const { return data[row * width + column]; }
  21. __device__ __forceinline__ half2 item_half2(int row, int column) const { return ((half2*)data)[(row * width + column) / 2]; }
  22. __device__ __forceinline__ half2 item_half2half2(int row, int column) const { return __half2half2(data[row * width + column]); }
  23. __device__ __forceinline__ const half* item_ptr(int row, int column) const { return &data[row * width + column]; }
  24. __device__ __forceinline__ void item4(half (&items)[4], int row, int column) const
  25. {
  26. half2* ptr = (half2*) item_ptr(row, column);
  27. half2 i01 = ptr[0];
  28. half2 i23 = ptr[1];
  29. items[0] = __low2half(i01);
  30. items[1] = __high2half(i01);
  31. items[2] = __low2half(i23);
  32. items[3] = __high2half(i23);
  33. }
  34. __device__ __forceinline__ void item4_f(float (&items)[4], int row, int column) const
  35. {
  36. half2* ptr = (half2*)item_ptr(row, column);
  37. half2 i01 = ptr[0];
  38. half2 i23 = ptr[1];
  39. items[0] = __half2float(__low2half(i01));
  40. items[1] = __half2float(__high2half(i01));
  41. items[2] = __half2float(__low2half(i23));
  42. items[3] = __half2float(__high2half(i23));
  43. }
  44. __device__ __forceinline__ void item4_h2(half2 (&items)[4], int row, int column) const
  45. {
  46. half2* ptr = (half2*)item_ptr(row, column);
  47. half2 i01 = ptr[0];
  48. half2 i23 = ptr[1];
  49. items[0] = __half2half2(__low2half(i01));
  50. items[1] = __half2half2(__high2half(i01));
  51. items[2] = __half2half2(__low2half(i23));
  52. items[3] = __half2half2(__high2half(i23));
  53. }
  54. };
  55. class MatrixView_half_rw
  56. {
  57. public:
  58. half* data;
  59. const int height;
  60. const int width;
  61. __device__ __forceinline__ MatrixView_half_rw(half* data, const int height, const int width)
  62. : data(data), height(height), width(width)
  63. { }
  64. __device__ __forceinline__ half item(int row, int column) const { return data[row * width + column]; }
  65. __device__ __forceinline__ half2 item_half2(int row, int column) const { return ((half2*)data)[(row * width + column) / 2]; }
  66. __device__ __forceinline__ half* item_ptr(int row, int column) { return &data[row * width + column]; }
  67. __device__ __forceinline__ void set(int row, int column, half value) { data[row * width + column] = value; }
  68. __device__ __forceinline__ void set_half2(int row, int column, half2 value) { ((half2*)data)[(row * width + column) / 2] = value; }
  69. __device__ __forceinline__ void set4(int row, int column, half v0, half v1, half v2, half v3)
  70. {
  71. half2 v01 = __halves2half2(v0, v1);
  72. half2 v23 = __halves2half2(v2, v3);
  73. half2* ptr = (half2*) item_ptr(row, column);
  74. ptr[0] = v01;
  75. ptr[1] = v23;
  76. }
  77. };
  78. class MatrixView_q4_row
  79. {
  80. public:
  81. const uint32_t* data;
  82. const int height;
  83. const int width;
  84. __device__ __forceinline__ MatrixView_q4_row(const uint32_t* data, const int height, const int width)
  85. : data(data), height(height), width(width)
  86. { }
  87. __device__ __forceinline__ int item(int row, int column) const
  88. {
  89. int shift = (column & 0x07) * 4;
  90. return (data[row * width / 8 + column / 8] >> shift) & 0x0f;
  91. }
  92. __device__ __forceinline__ void item2(int (&items)[2], int row, int column) const
  93. {
  94. int shift = (column & 0x07) * 4;
  95. uint32_t d = data[row * width / 8 + column / 8] >> shift;
  96. items[0] = d & 0x0f;
  97. items[1] = (d >> 4) & 0x0f;
  98. }
  99. __device__ __forceinline__ void item4(int (&items)[4], int row, int column) const
  100. {
  101. int shift = (column & 0x07) * 4;
  102. uint32_t d = data[row * width / 8 + column / 8] >> shift;
  103. items[0] = d & 0x0f;
  104. items[1] = (d >> 4) & 0x0f;
  105. items[2] = (d >> 8) & 0x0f;
  106. items[3] = (d >> 12) & 0x0f;
  107. }
  108. };
  109. class MatrixView_q4_column
  110. {
  111. public:
  112. const uint32_t* data;
  113. const int height;
  114. const int width;
  115. __device__ __forceinline__ MatrixView_q4_column(const uint32_t* data, const int height, const int width)
  116. : data(data), height(height), width(width)
  117. { }
  118. __device__ __forceinline__ int item(int row, int column) const
  119. {
  120. int shift = (row & 0x07) * 4;
  121. return (data[row / 8 * width + column] >> shift) & 0x0f;
  122. }
  123. __device__ __forceinline__ uint32_t item_uint32_t(int row, int column) { return data[row / 8 * width + column]; }
  124. __device__ __forceinline__ const uint32_t* item_uint32_ptr(int row, int column) { return &data[row / 8 * width + column]; }
  125. };
  126. class MatrixView_q2_row
  127. {
  128. public:
  129. const uint32_t* data;
  130. const int height;
  131. const int width;
  132. __device__ __forceinline__ MatrixView_q2_row(const uint32_t* data, const int height, const int width)
  133. : data(data), height(height), width(width)
  134. { }
  135. __device__ __forceinline__ int item(int row, int column) const
  136. {
  137. int shift = (column & 0x0f) * 2;
  138. return (data[row * width / 16 + column / 16] >> shift) & 0x03;
  139. }
  140. __device__ __forceinline__ void item2(int (&items)[2], int row, int column) const
  141. {
  142. int shift = (column & 0x0f) * 2;
  143. uint32_t d = data[row * width / 16 + column / 16] >> shift;
  144. items[0] = d & 0x03;
  145. items[1] = (d >> 2) & 0x03;
  146. }
  147. __device__ __forceinline__ void item4(int (&items)[4], int row, int column) const
  148. {
  149. int shift = (column & 0x0f) * 2;
  150. uint32_t d = data[row * width / 16 + column / 16] >> shift;
  151. items[0] = d & 0x03;
  152. items[1] = (d >> 2) & 0x03;
  153. items[2] = (d >> 4) & 0x03;
  154. items[3] = (d >> 6) & 0x03;
  155. }
  156. };
  157. class MatrixView_q3_row
  158. {
  159. public:
  160. const uint32_t* data;
  161. const int height;
  162. const int width;
  163. __device__ __forceinline__ MatrixView_q3_row(const uint32_t* data, const int height, const int width)
  164. : data(data), height(height), width(width)
  165. { }
  166. __device__ __forceinline__ int item(int row, int column) const
  167. {
  168. int z_w = column * 3 / 32;
  169. int z_mod = column & 0x1f;
  170. if (z_mod == 10) {
  171. return (data[row * width * 3 / 32 + z_w] >> 30) | ((data[row * width * 3 / 32 + (z_w + 1)] << 2) & 0x4);
  172. } else if (z_mod == 21) {
  173. return (data[row * width * 3 / 32 + z_w] >> 31) | ((data[row * width * 3 / 32 + (z_w + 1)] << 1) & 0x6);
  174. } else if (z_mod < 10) {
  175. return (data[row * width * 3 / 32 + z_w] >> (z_mod * 3)) & 0x07;
  176. } else if (z_mod < 21) {
  177. return (data[row * width * 3 / 32 + z_w] >> (z_mod * 3 - 32)) & 0x07;
  178. } else {
  179. return (data[row * width * 3 / 32 + z_w] >> (z_mod * 3 - 64)) & 0x07;
  180. }
  181. }
  182. __device__ __forceinline__ void item4(int (&items)[4], int row, int column) const
  183. {
  184. int shift = (column & 0x1f);
  185. uint32_t d;
  186. if (shift <= 4) {
  187. d = data[row * width / 32 * 3 + column * 3 / 32] >> (shift * 3);
  188. } else if (shift == 8) {
  189. d = (data[row * width / 32 * 3 + column * 3 / 32] >> 24) | ((data[row * width / 32 * 3 + column * 3 / 32 + 1] & 0x0f) << 8);
  190. } else if (shift <= 16) {
  191. d = data[row * width / 32 * 3 + column * 3 / 32] >> (shift * 3 - 32);
  192. } else if (shift == 20) {
  193. d = (data[row * width / 32 * 3 + column * 3 / 32] >> 28) | ((data[row * width / 32 * 3 + column * 3 / 32 + 1] & 0xff) << 4);
  194. } else {
  195. d = data[row * width / 32 * 3 + column * 3 / 32] >> (shift * 3 - 64);
  196. }
  197. items[0] = d & 0x07;
  198. items[1] = (d >> 3) & 0x07;
  199. items[2] = (d >> 6) & 0x07;
  200. items[3] = (d >> 9) & 0x07;
  201. }
  202. };
  203. class MatrixView_q8_row
  204. {
  205. public:
  206. const uint32_t* data;
  207. const int height;
  208. const int width;
  209. __device__ __forceinline__ MatrixView_q8_row(const uint32_t* data, const int height, const int width)
  210. : data(data), height(height), width(width)
  211. { }
  212. __device__ __forceinline__ int item(int row, int column) const
  213. {
  214. int shift = (column & 0x03) * 8;
  215. return (data[row * width / 4 + column / 4] >> shift) & 0xff;
  216. }
  217. __device__ __forceinline__ void item2(int (&items)[2], int row, int column) const
  218. {
  219. int shift = (column & 0x03) * 8;
  220. uint32_t d = data[row * width / 4 + column / 4] >> shift;
  221. items[0] = d & 0xff;
  222. items[1] = (d >> 8) & 0xff;
  223. }
  224. __device__ __forceinline__ void item4(int (&items)[4], int row, int column) const
  225. {
  226. int shift = (column & 0x03) * 2;
  227. uint32_t d = data[row * width / 4 + column / 4] >> shift;
  228. items[0] = d & 0xff;
  229. items[1] = (d >> 8) & 0xff;
  230. items[2] = (d >> 16) & 0xff;
  231. items[3] = (d >> 24) & 0xff;
  232. }
  233. };
  234. } // namespace gptq
  235. } // namespace aphrodite
  236. #endif