qdq_util.cuh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_util_cuh
  24. #define _qdq_util_cuh
  25. namespace aphrodite {
  26. namespace exl2 {
  27. union half2_uint32
  28. {
  29. uint32_t as_uint32;
  30. half2 as_half2;
  31. __device__ half2_uint32(uint32_t val) : as_uint32(val) {}
  32. __device__ half2_uint32(half2 val) : as_half2(val) {}
  33. __device__ half2_uint32() : as_uint32(0) {}
  34. };
  35. union half_uint16
  36. {
  37. uint16_t as_uint16;
  38. half as_half;
  39. __device__ half_uint16(uint16_t val) : as_uint16(val) {}
  40. __device__ half_uint16(half val) : as_half(val) {}
  41. __device__ half_uint16() : as_uint16(0) {}
  42. };
  43. // Max_scale premultiplied by 1/256
  44. __forceinline__ __device__ half dq_scale(const int qs, const half max_scale)
  45. {
  46. int qs_i = qs + 1;
  47. half qs_h = __int2half_rn(qs_i * qs_i);
  48. qs_h = __hmul(qs_h, max_scale);
  49. return qs_h;
  50. }
  51. __forceinline__ __device__ half dq(const int q, const int qzero, const half scale)
  52. {
  53. return __hmul(__int2half_rn(q - qzero), scale);
  54. }
  55. __forceinline__ __device__ half dq_ns(const int q, const int qzero)
  56. {
  57. //return __hsub(__int2half_rn(q), __int2half_rn(qzero));
  58. return __int2half_rn(q - qzero);
  59. }
  60. __forceinline__ __device__ int exb(const uint32_t q, const int shift, const int mask)
  61. {
  62. return (int)((q >> shift) & mask);
  63. }
  64. __forceinline__ __device__ int exb(const uint32_t q1, const uint32_t q0, const int shift, const int mask)
  65. {
  66. return (int)(__funnelshift_rc(q0, q1, shift) & mask);
  67. }
  68. } // namespace exl2
  69. } // namespace aphrodite
  70. #endif