quantize_intX.cu 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (c) Microsoft Corporation.
  2. // SPDX-License-Identifier: Apache-2.0
  3. // DeepSpeed Team
  4. #include <assert.h>
  5. #include <cuda_fp16.h>
  6. #include <cuda_runtime.h>
  7. #include "memory_access_utils.h"
  8. template <typename T, int N>
  9. struct alignas(sizeof(T) * N) AlignedArray {
  10. using Element = T;
  11. static const int kElements = N;
  12. __device__ __host__ AlignedArray() {}
  13. __device__ __host__ AlignedArray(const T& rhs)
  14. {
  15. #pragma unroll
  16. for (int idx = 0; idx < kElements; ++idx) { this->at(idx) = rhs; }
  17. }
  18. __device__ __host__ T& operator[](int offset)
  19. {
  20. return reinterpret_cast<T&>(this->buffer[offset]);
  21. }
  22. __device__ __host__ const T& operator[](int offset) const
  23. {
  24. return reinterpret_cast<const T&>(this->buffer[offset]);
  25. }
  26. __device__ __host__ T& at(int offset) { return reinterpret_cast<T&>(this->buffer[offset]); }
  27. __device__ __host__ const T& at(int offset) const
  28. {
  29. return reinterpret_cast<const T&>(this->buffer[offset]);
  30. }
  31. __device__ __host__ AlignedArray<T, N> operator+(const AlignedArray<T, N>& rhs) const
  32. {
  33. AlignedArray<T, N> ret;
  34. #pragma unroll
  35. for (int idx = 0; idx < kElements; ++idx) { ret[idx] = this->at(idx) + rhs.at(idx); }
  36. return ret;
  37. }
  38. __device__ __forceinline__ void clear()
  39. {
  40. #pragma unroll
  41. for (int idx = 0; idx < kElements; ++idx) { this->at(idx) = Element(0); }
  42. }
  43. Element buffer[N];
  44. };
  45. template <typename T>
  46. struct reduce_max {
  47. __device__ __forceinline__ T operator()(const T& lhs, const T& rhs)
  48. {
  49. return lhs > rhs ? lhs : rhs;
  50. }
  51. };
  52. template <typename T>
  53. struct reduce_min {
  54. __device__ __forceinline__ T operator()(const T& lhs, const T& rhs)
  55. {
  56. return lhs < rhs ? lhs : rhs;
  57. }
  58. };
  59. template <typename T, int N>
  60. struct subtract {
  61. __device__ __forceinline__ AlignedArray<T, N> operator()(const AlignedArray<T, N>& lhs,
  62. const T& rhs)
  63. {
  64. AlignedArray<T, N> ret;
  65. #pragma unroll
  66. for (int idx = 0; idx < N; ++idx) { ret[idx] = lhs[idx] - rhs; }
  67. return ret;
  68. }
  69. };
  70. template <typename T, int N>
  71. struct plus {
  72. __device__ __forceinline__ AlignedArray<T, N> operator()(const AlignedArray<T, N>& lhs,
  73. const T& rhs)
  74. {
  75. AlignedArray<T, N> ret;
  76. #pragma unroll
  77. for (int idx = 0; idx < N; ++idx) { ret[idx] = lhs[idx] + rhs; }
  78. return ret;
  79. }
  80. };
  81. template <typename T, int N>
  82. struct multiply {
  83. __device__ __forceinline__ AlignedArray<T, N> operator()(const AlignedArray<T, N>& lhs,
  84. const T& rhs)
  85. {
  86. AlignedArray<T, N> ret;
  87. #pragma unroll
  88. for (int idx = 0; idx < N; ++idx) { ret[idx] = lhs[idx] * rhs; }
  89. return ret;
  90. }
  91. };
  92. template <typename T, int N>
  93. struct clamp {
  94. __device__ __forceinline__ AlignedArray<T, N> operator()(const AlignedArray<T, N>& lhs,
  95. const T& min_val,
  96. const T& max_val)
  97. {
  98. AlignedArray<T, N> ret;
  99. #pragma unroll
  100. for (int idx = 0; idx < N; ++idx) {
  101. ret[idx] = reduce_max<T>()(reduce_min<T>()(lhs[idx], max_val), min_val);
  102. }
  103. return ret;
  104. }
  105. };
  106. template <typename T, int N>
  107. struct round_int;
  108. template <int N>
  109. struct round_int<half, N> {
  110. __device__ __forceinline__ AlignedArray<half, N> operator()(const AlignedArray<half, N>& lhs)
  111. {
  112. AlignedArray<half, N> ret;
  113. #pragma unroll
  114. for (int idx = 0; idx < N; ++idx) { ret[idx] = hrint(lhs[idx]); }
  115. return ret;
  116. }
  117. };
  118. template <typename T, int N>
  119. struct divide {
  120. __device__ __forceinline__ AlignedArray<T, N> operator()(const AlignedArray<T, N>& lhs,
  121. const T& rhs)
  122. {
  123. AlignedArray<T, N> ret;
  124. #pragma unroll
  125. for (int idx = 0; idx < N; ++idx) { ret[idx] = lhs[idx] / rhs; }
  126. return ret;
  127. }
  128. };
  129. template <typename T, int N, typename Reducer>
  130. __device__ __forceinline__ T to_scalar(const AlignedArray<T, N>& data)
  131. {
  132. Reducer re;
  133. T res = data[0];
  134. #pragma unroll
  135. for (int idx = 1; idx < N; ++idx) { res = re(res, data[idx]); }
  136. return res;
  137. }
  138. template <int N>
  139. __device__ __forceinline__ AlignedArray<half, N * 2> int4_to_half(
  140. const AlignedArray<uint8_t, N>& data)
  141. {
  142. AlignedArray<half, N * 2> ret;
  143. #pragma unroll
  144. for (int idx = 0; idx < N * 2; idx += 2) {
  145. ret[idx] = half(int(data[idx / 2] >> 4));
  146. ret[idx + 1] = half(int(data[idx / 2] & 0xf));
  147. }
  148. return ret;
  149. }
  150. __global__ void dequantize_int4_to_half(uint8_t* data_in,
  151. half* data_out,
  152. half* scale_buffer,
  153. half* min_val_buffer,
  154. int num_group,
  155. int group_size)
  156. {
  157. using AccessType = AlignedArray<uint8_t, 4>;
  158. using AccessTypeOut = AlignedArray<half, 8>;
  159. for (int idx = threadIdx.x + blockIdx.x * blockDim.x; idx < num_group * group_size / 8;
  160. idx += blockDim.x * gridDim.x) {
  161. int id_group = idx / (group_size / 8);
  162. AccessType value = reinterpret_cast<AccessType*>(data_in)[idx];
  163. half scale = scale_buffer[id_group];
  164. half min_value = min_val_buffer[id_group];
  165. AccessTypeOut output = int4_to_half(value);
  166. output = divide<half, 8>()(output, scale);
  167. output = plus<half, 8>()(output, min_value);
  168. reinterpret_cast<AccessTypeOut*>(data_out)[idx] = output;
  169. }
  170. }
  171. void launch_dequantize_int4_to_half_experimental(uint8_t* data_in,
  172. half* data_out,
  173. half* scale_buffer,
  174. half* min_val_buffer,
  175. int num_group,
  176. int group_size,
  177. cudaStream_t stream)
  178. {
  179. int num_warp = num_group / 4;
  180. int num_block = num_warp / 8; // 256 trd / block
  181. dequantize_int4_to_half<<<num_block, 256, 0, stream>>>(
  182. data_in, data_out, scale_buffer, min_val_buffer, num_group, group_size);
  183. }
  184. template <int N>
  185. __device__ __forceinline__ AlignedArray<half, N> int8_to_half(const AlignedArray<uint8_t, N>& data)
  186. {
  187. AlignedArray<half, N> ret;
  188. #pragma unroll
  189. for (int idx = 0; idx < N; idx += 1) { ret[idx] = half(int(data[idx])); }
  190. return ret;
  191. }
  192. __global__ void dequantize_int8_to_half(uint8_t* data_in,
  193. half* data_out,
  194. half* scale_buffer,
  195. half* min_val_buffer,
  196. int num_group,
  197. int group_size)
  198. {
  199. using AccessType = AlignedArray<uint8_t, 8>;
  200. using AccessTypeOut = AlignedArray<half, 8>;
  201. for (int idx = threadIdx.x + blockIdx.x * blockDim.x; idx < num_group * group_size / 8;
  202. idx += blockDim.x * gridDim.x) {
  203. int id_group = idx / (group_size / 8);
  204. AccessType value = reinterpret_cast<AccessType*>(data_in)[idx];
  205. half scale = scale_buffer[id_group];
  206. half min_value = min_val_buffer[id_group];
  207. AccessTypeOut output = int8_to_half(value);
  208. output = divide<half, 8>()(output, scale);
  209. output = plus<half, 8>()(output, min_value);
  210. reinterpret_cast<AccessTypeOut*>(data_out)[idx] = output;
  211. }
  212. }
  213. void launch_dequantize_int8_to_half_experimental(uint8_t* data_in,
  214. half* data_out,
  215. half* scale_buffer,
  216. half* min_val_buffer,
  217. int num_group,
  218. int group_size,
  219. cudaStream_t stream)
  220. {
  221. int num_warp = num_group / 4;
  222. int num_block = num_warp / 8; // 256 trd / block
  223. dequantize_int8_to_half<<<num_block, 256, 0, stream>>>(
  224. data_in, data_out, scale_buffer, min_val_buffer, num_group, group_size);
  225. }