type_convert.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef CSRC__PUNICA__TYPE_CONVERT_H__
  2. #define CSRC__PUNICA__TYPE_CONVERT_H__
  3. #ifndef USE_ROCM
  4. #include <cuda_bf16.h>
  5. #include <cuda_fp16.h>
  6. #else
  7. #include <hip/hip_bf16.h>
  8. #include <hip/hip_fp16.h>
  9. #define __TYPE_CONVERT__HOST_DEVICE__ __host__ __device__
  10. typedef __half nv_half;
  11. typedef __hip_bfloat16 nv_bfloat16;
  12. typedef __hip_bfloat162 nv_bfloat162;
  13. __TYPE_CONVERT__HOST_DEVICE__
  14. inline __hip_bfloat162 make_bfloat162(__hip_bfloat16 val) {
  15. return __hip_bfloat162{val, val};
  16. }
  17. __TYPE_CONVERT__HOST_DEVICE__
  18. inline __hip_bfloat162 make_bfloat162(__hip_bfloat16 vall, __hip_bfloat16 valr) {
  19. return __hip_bfloat162{vall, valr};
  20. }
  21. template <typename T_src, typename T_dst>
  22. __TYPE_CONVERT__HOST_DEVICE__
  23. inline T_dst convert_type(T_src val) {
  24. return static_cast<T_dst>(val);
  25. }
  26. template <>
  27. __TYPE_CONVERT__HOST_DEVICE__
  28. inline float convert_type<__half, float>(__half val) {
  29. return __half2float(val);
  30. }
  31. template <>
  32. __TYPE_CONVERT__HOST_DEVICE__
  33. inline __half convert_type<float, __half>(float val) {
  34. return __float2half(val);
  35. }
  36. template <>
  37. __TYPE_CONVERT__HOST_DEVICE__
  38. inline float convert_type<__hip_bfloat16, float>(__hip_bfloat16 val) {
  39. return __bfloat162float(val);
  40. }
  41. template <>
  42. __TYPE_CONVERT__HOST_DEVICE__
  43. inline __hip_bfloat16 convert_type<float, __hip_bfloat16>(float val) {
  44. return __float2bfloat16(val);
  45. }
  46. template <typename T>
  47. __TYPE_CONVERT__HOST_DEVICE__
  48. inline T aphrodite_add(T a, T b) {
  49. return a + b;
  50. }
  51. template <>
  52. __TYPE_CONVERT__HOST_DEVICE__
  53. inline __half aphrodite_add<__half>(__half a, __half b) {
  54. return __hadd(a, b);
  55. }
  56. template <>
  57. __TYPE_CONVERT__HOST_DEVICE__
  58. inline __hip_bfloat16 aphrodite_add<__hip_bfloat16>(__hip_bfloat16 a, __hip_bfloat16 b) {
  59. return __hadd(a, b);
  60. }
  61. #undef __TYPE_CONVERT__HOST_DEVICE__
  62. #endif // USE_ROCM
  63. #endif // CSRC__PUNICA__TYPE_CONVERT_H__