compat.cuh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 _compat_cuh
  24. #define _compat_cuh
  25. namespace aphrodite {
  26. namespace exl2 {
  27. // atomicAdd for half types, to support CC < 7.x
  28. __device__ __forceinline__ void atomicAdd_half(half* address, half val)
  29. {
  30. unsigned int * address_as_ui = (unsigned int *) ((char *)address - ((size_t)address & 2));
  31. unsigned int old = *address_as_ui;
  32. unsigned int assumed;
  33. do
  34. {
  35. assumed = old;
  36. __half_raw hsum;
  37. hsum.x = (size_t)address & 2 ? (old >> 16) : (old & 0xffff);
  38. half tmpres = __hadd(hsum, val);
  39. hsum = __half_raw(tmpres);
  40. old = (size_t)address & 2 ? (old & 0xffff) | (hsum.x << 16) : (old & 0xffff0000) | hsum.x;
  41. old = atomicCAS(address_as_ui, assumed, old);
  42. }
  43. while (assumed != old);
  44. }
  45. // atomicAdd for half2 types
  46. __device__ __forceinline__ void atomicAdd_half2(half2* address, half2 val)
  47. {
  48. unsigned int* address_as_ui = (unsigned int*)address;
  49. unsigned int old = *address_as_ui;
  50. unsigned int assumed;
  51. do
  52. {
  53. assumed = old;
  54. half2 old_val = *((half2*)&old);
  55. half2 new_val = __hadd2(old_val, val);
  56. old = atomicCAS(address_as_ui, assumed, *((unsigned int*)&new_val));
  57. }
  58. while (assumed != old);
  59. }
  60. //
  61. #if defined(__CUDA_ARCH__) || defined(USE_ROCM)
  62. #if __CUDA_ARCH__ < 700 || defined(USE_ROCM)
  63. __device__ __forceinline__ void atomicAdd(half* address, half val) { atomicAdd_half(address, val); }
  64. #if __CUDA_ARCH__ < 600 || defined(USE_ROCM)
  65. __device__ __forceinline__ void atomicAdd(half2* address, half2 val) { atomicAdd_half2(address, val); }
  66. #endif
  67. #endif
  68. #endif
  69. #endif
  70. } // namespace exl2
  71. } // namespace aphrodite