compat.cuh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. Copied from https://github.com/turboderp/exllamav2
  3. */
  4. #ifndef _compat_cuh
  5. #define _compat_cuh
  6. namespace aphrodite {
  7. namespace gptq {
  8. // atomicAdd for half types, to support CC < 7.x
  9. __device__ __forceinline__ void atomicAdd_half(half* address, half val)
  10. {
  11. unsigned int * address_as_ui = (unsigned int *) ((char *)address - ((size_t)address & 2));
  12. unsigned int old = *address_as_ui;
  13. unsigned int assumed;
  14. do
  15. {
  16. assumed = old;
  17. __half_raw hsum;
  18. hsum.x = (size_t)address & 2 ? (old >> 16) : (old & 0xffff);
  19. half tmpres = __hadd(hsum, val);
  20. hsum = __half_raw(tmpres);
  21. old = (size_t)address & 2 ? (old & 0xffff) | (hsum.x << 16) : (old & 0xffff0000) | hsum.x;
  22. old = atomicCAS(address_as_ui, assumed, old);
  23. }
  24. while (assumed != old);
  25. }
  26. // atomicAdd for half2 types
  27. __device__ __forceinline__ void atomicAdd_half2(half2* address, half2 val)
  28. {
  29. unsigned int* address_as_ui = (unsigned int*)address;
  30. unsigned int old = *address_as_ui;
  31. unsigned int assumed;
  32. do
  33. {
  34. assumed = old;
  35. half2 old_val = *((half2*)&old);
  36. half2 new_val = __hadd2(old_val, val);
  37. old = atomicCAS(address_as_ui, assumed, *((unsigned int*)&new_val));
  38. }
  39. while (assumed != old);
  40. }
  41. //
  42. #if defined(__CUDA_ARCH__) || defined(USE_ROCM)
  43. #if __CUDA_ARCH__ < 700 || defined(USE_ROCM)
  44. __device__ __forceinline__ void atomicAdd(half* address, half val) { atomicAdd_half(address, val); }
  45. #if __CUDA_ARCH__ < 600 || defined(USE_ROCM)
  46. __device__ __forceinline__ void atomicAdd(half2* address, half2 val) { atomicAdd_half2(address, val); }
  47. #endif
  48. #endif
  49. #endif
  50. } // namespace gptq
  51. } // namespace aphrodite
  52. #endif