react-native-executorch 0.5.15 → 0.6.0-nightly-897eae9-20251213

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/README.md +42 -36
  2. package/android/CMakeLists.txt +13 -25
  3. package/android/build.gradle +2 -3
  4. package/android/libs/classes.jar +0 -0
  5. package/android/src/main/cpp/CMakeLists.txt +2 -1
  6. package/common/rnexecutorch/RnExecutorchInstaller.cpp +18 -0
  7. package/common/rnexecutorch/TokenizerModule.cpp +3 -3
  8. package/common/rnexecutorch/data_processing/Numerical.cpp +31 -23
  9. package/common/rnexecutorch/data_processing/Numerical.h +6 -1
  10. package/common/rnexecutorch/data_processing/dsp.cpp +0 -46
  11. package/common/rnexecutorch/host_objects/JsiConversions.h +16 -0
  12. package/common/rnexecutorch/host_objects/ModelHostObject.h +26 -11
  13. package/common/rnexecutorch/jsi/OwningArrayBuffer.h +19 -2
  14. package/common/rnexecutorch/metaprogramming/TypeConcepts.h +0 -20
  15. package/common/rnexecutorch/models/BaseModel.cpp +12 -11
  16. package/common/rnexecutorch/models/BaseModel.h +18 -10
  17. package/common/rnexecutorch/models/embeddings/BaseEmbeddings.cpp +3 -11
  18. package/common/rnexecutorch/models/embeddings/text/TextEmbeddings.cpp +0 -1
  19. package/common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp +6 -12
  20. package/common/rnexecutorch/models/llm/LLM.cpp +25 -8
  21. package/common/rnexecutorch/models/llm/LLM.h +4 -4
  22. package/common/rnexecutorch/models/ocr/CTCLabelConverter.h +1 -1
  23. package/common/rnexecutorch/models/ocr/utils/RecognitionHandlerUtils.cpp +7 -4
  24. package/common/rnexecutorch/models/speech_to_text/SpeechToText.cpp +8 -13
  25. package/common/rnexecutorch/models/speech_to_text/SpeechToText.h +1 -3
  26. package/common/rnexecutorch/models/speech_to_text/asr/ASR.cpp +12 -19
  27. package/common/rnexecutorch/models/speech_to_text/asr/ASR.h +4 -5
  28. package/common/rnexecutorch/models/text_to_image/Constants.h +9 -0
  29. package/common/rnexecutorch/models/text_to_image/Decoder.cpp +32 -0
  30. package/common/rnexecutorch/models/text_to_image/Decoder.h +24 -0
  31. package/common/rnexecutorch/models/text_to_image/Encoder.cpp +44 -0
  32. package/common/rnexecutorch/models/text_to_image/Encoder.h +32 -0
  33. package/common/rnexecutorch/models/text_to_image/Scheduler.cpp +152 -0
  34. package/common/rnexecutorch/models/text_to_image/Scheduler.h +41 -0
  35. package/common/rnexecutorch/models/text_to_image/TextToImage.cpp +141 -0
  36. package/common/rnexecutorch/models/text_to_image/TextToImage.h +64 -0
  37. package/common/rnexecutorch/models/text_to_image/UNet.cpp +38 -0
  38. package/common/rnexecutorch/models/text_to_image/UNet.h +28 -0
  39. package/common/rnexecutorch/models/voice_activity_detection/Constants.h +27 -0
  40. package/common/rnexecutorch/models/voice_activity_detection/Types.h +12 -0
  41. package/common/rnexecutorch/models/voice_activity_detection/Utils.cpp +15 -0
  42. package/common/rnexecutorch/models/voice_activity_detection/Utils.h +13 -0
  43. package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.cpp +160 -0
  44. package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.h +36 -0
  45. package/common/rnexecutorch/tests/CMakeLists.txt +30 -0
  46. package/common/rnexecutorch/tests/NumericalTest.cpp +110 -0
  47. package/common/rnexecutorch/tests/README.md +30 -13
  48. package/common/rnexecutorch/threads/GlobalThreadPool.h +4 -0
  49. package/common/runner/arange_util.cpp +44 -0
  50. package/common/runner/arange_util.h +37 -0
  51. package/common/runner/constants.h +28 -0
  52. package/common/runner/io_manager.h +240 -0
  53. package/common/runner/irunner.h +87 -16
  54. package/common/runner/kernel_includes.h +23 -0
  55. package/common/runner/runner.cpp +151 -66
  56. package/common/runner/runner.h +39 -22
  57. package/common/runner/sampler.cpp +8 -1
  58. package/common/runner/sampler.h +4 -2
  59. package/common/runner/stats.h +1 -4
  60. package/common/runner/text_decoder_runner.cpp +26 -12
  61. package/common/runner/text_decoder_runner.h +52 -31
  62. package/common/runner/text_prefiller.cpp +46 -12
  63. package/common/runner/text_prefiller.h +38 -4
  64. package/common/runner/text_token_generator.h +51 -26
  65. package/common/runner/util.h +53 -8
  66. package/ios/RnExecutorch.xcodeproj/project.pbxproj +0 -23
  67. package/lib/module/Error.js +1 -0
  68. package/lib/module/Error.js.map +1 -1
  69. package/lib/module/constants/directories.js +1 -1
  70. package/lib/module/constants/directories.js.map +1 -1
  71. package/lib/module/constants/modelUrls.js +32 -1
  72. package/lib/module/constants/modelUrls.js.map +1 -1
  73. package/lib/module/constants/ocr/models.js +7 -7
  74. package/lib/module/constants/ocr/models.js.map +1 -1
  75. package/lib/module/constants/ocr/symbols.js +3 -2
  76. package/lib/module/constants/ocr/symbols.js.map +1 -1
  77. package/lib/module/controllers/LLMController.js +10 -1
  78. package/lib/module/controllers/LLMController.js.map +1 -1
  79. package/lib/module/controllers/OCRController.js +3 -3
  80. package/lib/module/controllers/OCRController.js.map +1 -1
  81. package/lib/module/controllers/VerticalOCRController.js +2 -2
  82. package/lib/module/controllers/VerticalOCRController.js.map +1 -1
  83. package/lib/module/hooks/computer_vision/useOCR.js +3 -3
  84. package/lib/module/hooks/computer_vision/useOCR.js.map +1 -1
  85. package/lib/module/hooks/{useNonStaticModule.js → computer_vision/useTextToImage.js} +21 -16
  86. package/lib/module/hooks/computer_vision/useTextToImage.js.map +1 -0
  87. package/lib/module/hooks/computer_vision/useVerticalOCR.js +3 -3
  88. package/lib/module/hooks/computer_vision/useVerticalOCR.js.map +1 -1
  89. package/lib/module/hooks/natural_language_processing/useLLM.js +3 -3
  90. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  91. package/lib/module/hooks/natural_language_processing/useTokenizer.js +5 -5
  92. package/lib/module/hooks/natural_language_processing/useTokenizer.js.map +1 -1
  93. package/lib/module/hooks/natural_language_processing/useVAD.js +13 -0
  94. package/lib/module/hooks/natural_language_processing/useVAD.js.map +1 -0
  95. package/lib/module/index.js +7 -2
  96. package/lib/module/index.js.map +1 -1
  97. package/lib/module/modules/computer_vision/OCRModule.js +2 -2
  98. package/lib/module/modules/computer_vision/OCRModule.js.map +1 -1
  99. package/lib/module/modules/computer_vision/TextToImageModule.js +48 -0
  100. package/lib/module/modules/computer_vision/TextToImageModule.js.map +1 -0
  101. package/lib/module/modules/computer_vision/VerticalOCRModule.js +2 -2
  102. package/lib/module/modules/computer_vision/VerticalOCRModule.js.map +1 -1
  103. package/lib/module/modules/natural_language_processing/SpeechToTextModule.js +7 -4
  104. package/lib/module/modules/natural_language_processing/SpeechToTextModule.js.map +1 -1
  105. package/lib/module/modules/natural_language_processing/VADModule.js +19 -0
  106. package/lib/module/modules/natural_language_processing/VADModule.js.map +1 -0
  107. package/lib/module/types/llm.js.map +1 -1
  108. package/lib/module/types/vad.js +2 -0
  109. package/lib/module/types/vad.js.map +1 -0
  110. package/lib/module/utils/ResourceFetcher.js +2 -1
  111. package/lib/module/utils/ResourceFetcher.js.map +1 -1
  112. package/lib/module/utils/ResourceFetcherUtils.js +6 -6
  113. package/lib/module/utils/ResourceFetcherUtils.js.map +1 -1
  114. package/lib/typescript/Error.d.ts +1 -0
  115. package/lib/typescript/Error.d.ts.map +1 -1
  116. package/lib/typescript/constants/modelUrls.d.ts +23 -0
  117. package/lib/typescript/constants/modelUrls.d.ts.map +1 -1
  118. package/lib/typescript/constants/ocr/symbols.d.ts +1 -1
  119. package/lib/typescript/constants/ocr/symbols.d.ts.map +1 -1
  120. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  121. package/lib/typescript/controllers/OCRController.d.ts +1 -1
  122. package/lib/typescript/controllers/OCRController.d.ts.map +1 -1
  123. package/lib/typescript/controllers/VerticalOCRController.d.ts +1 -1
  124. package/lib/typescript/controllers/VerticalOCRController.d.ts.map +1 -1
  125. package/lib/typescript/hooks/computer_vision/useOCR.d.ts +1 -1
  126. package/lib/typescript/hooks/computer_vision/useOCR.d.ts.map +1 -1
  127. package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts +22 -0
  128. package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts.map +1 -0
  129. package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts +1 -1
  130. package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts.map +1 -1
  131. package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
  132. package/lib/typescript/hooks/natural_language_processing/useSpeechToText.d.ts +2 -2
  133. package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts +16 -0
  134. package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts.map +1 -0
  135. package/lib/typescript/index.d.ts +8 -1
  136. package/lib/typescript/index.d.ts.map +1 -1
  137. package/lib/typescript/modules/computer_vision/OCRModule.d.ts +1 -1
  138. package/lib/typescript/modules/computer_vision/OCRModule.d.ts.map +1 -1
  139. package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts +16 -0
  140. package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts.map +1 -0
  141. package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts +1 -1
  142. package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts.map +1 -1
  143. package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts +3 -2
  144. package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts.map +1 -1
  145. package/lib/typescript/modules/natural_language_processing/VADModule.d.ts +10 -0
  146. package/lib/typescript/modules/natural_language_processing/VADModule.d.ts.map +1 -0
  147. package/lib/typescript/types/llm.d.ts +2 -0
  148. package/lib/typescript/types/llm.d.ts.map +1 -1
  149. package/lib/typescript/types/vad.d.ts +5 -0
  150. package/lib/typescript/types/vad.d.ts.map +1 -0
  151. package/lib/typescript/utils/ResourceFetcher.d.ts +29 -0
  152. package/lib/typescript/utils/ResourceFetcher.d.ts.map +1 -1
  153. package/lib/typescript/utils/ResourceFetcherUtils.d.ts +2 -2
  154. package/lib/typescript/utils/ResourceFetcherUtils.d.ts.map +1 -1
  155. package/package.json +11 -8
  156. package/react-native-executorch.podspec +9 -9
  157. package/src/Error.ts +1 -0
  158. package/src/constants/directories.ts +1 -1
  159. package/src/constants/modelUrls.ts +36 -1
  160. package/src/constants/ocr/models.ts +7 -7
  161. package/src/constants/ocr/symbols.ts +3 -2
  162. package/src/controllers/LLMController.ts +12 -1
  163. package/src/controllers/OCRController.ts +3 -3
  164. package/src/controllers/VerticalOCRController.ts +2 -2
  165. package/src/hooks/computer_vision/useOCR.ts +4 -5
  166. package/src/hooks/computer_vision/useTextToImage.ts +92 -0
  167. package/src/hooks/computer_vision/useVerticalOCR.ts +4 -5
  168. package/src/hooks/natural_language_processing/useLLM.ts +3 -4
  169. package/src/hooks/natural_language_processing/useTokenizer.ts +5 -5
  170. package/src/hooks/natural_language_processing/useVAD.ts +15 -0
  171. package/src/index.ts +20 -1
  172. package/src/modules/computer_vision/OCRModule.ts +2 -2
  173. package/src/modules/computer_vision/TextToImageModule.ts +93 -0
  174. package/src/modules/computer_vision/VerticalOCRModule.ts +2 -2
  175. package/src/modules/natural_language_processing/SpeechToTextModule.ts +8 -4
  176. package/src/modules/natural_language_processing/VADModule.ts +27 -0
  177. package/src/types/llm.ts +2 -0
  178. package/src/types/vad.ts +4 -0
  179. package/src/utils/ResourceFetcher.ts +2 -1
  180. package/src/utils/ResourceFetcherUtils.ts +8 -8
  181. package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
  182. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  183. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  184. package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
  185. package/third-party/include/c10/macros/Export.h +0 -78
  186. package/third-party/include/c10/macros/Macros.h +1 -520
  187. package/third-party/include/c10/util/BFloat16-inl.h +1 -339
  188. package/third-party/include/c10/util/BFloat16.h +1 -122
  189. package/third-party/include/c10/util/Half-inl.h +1 -347
  190. package/third-party/include/c10/util/Half.h +6 -419
  191. package/third-party/include/c10/util/TypeSafeSignMath.h +1 -133
  192. package/third-party/include/c10/util/bit_cast.h +1 -43
  193. package/third-party/include/c10/util/complex.h +1 -568
  194. package/third-party/include/c10/util/floating_point_utils.h +1 -33
  195. package/third-party/include/c10/util/irange.h +1 -1
  196. package/third-party/include/c10/util/llvmMathExtras.h +866 -0
  197. package/third-party/include/c10/util/safe_numerics.h +97 -0
  198. package/third-party/include/executorch/ExecuTorchError.h +6 -7
  199. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLM.h +12 -0
  200. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMConfig.h +56 -0
  201. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMError.h +16 -0
  202. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMMultimodalRunner.h +227 -0
  203. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMTextRunner.h +97 -0
  204. package/third-party/include/executorch/ExecuTorchLLM/module.modulemap +4 -0
  205. package/third-party/include/executorch/ExecuTorchLog.h +1 -0
  206. package/third-party/include/executorch/ExecuTorchModule.h +177 -4
  207. package/third-party/include/executorch/ExecuTorchTensor.h +3 -4
  208. package/third-party/include/executorch/ExecuTorchValue.h +1 -7
  209. package/third-party/include/executorch/extension/module/module.h +139 -8
  210. package/third-party/include/executorch/extension/tensor/tensor.h +1 -0
  211. package/third-party/include/executorch/extension/tensor/tensor_ptr.h +88 -26
  212. package/third-party/include/executorch/extension/threadpool/threadpool.h +4 -1
  213. package/third-party/include/executorch/runtime/backend/backend_init_context.h +6 -0
  214. package/third-party/include/executorch/runtime/backend/interface.h +1 -1
  215. package/third-party/include/executorch/runtime/core/error.h +76 -49
  216. package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +18 -4
  217. package/third-party/include/executorch/runtime/core/memory_allocator.h +12 -2
  218. package/third-party/include/executorch/runtime/core/named_data_map.h +1 -11
  219. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +0 -78
  220. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +1 -520
  221. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -339
  222. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +1 -122
  223. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half-inl.h +1 -347
  224. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +6 -419
  225. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/TypeSafeSignMath.h +1 -133
  226. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -43
  227. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +1 -568
  228. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/floating_point_utils.h +1 -33
  229. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +1 -1
  230. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/llvmMathExtras.h +866 -0
  231. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/safe_numerics.h +97 -0
  232. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +66 -0
  233. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Macros.h +553 -0
  234. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/BFloat16.h +477 -0
  235. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/Half.h +781 -0
  236. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/TypeSafeSignMath.h +141 -0
  237. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/bit_cast.h +49 -0
  238. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/complex.h +593 -0
  239. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/floating_point_utils.h +38 -0
  240. package/third-party/include/executorch/runtime/core/tensor_layout.h +1 -1
  241. package/third-party/include/executorch/runtime/executor/merged_data_map.h +142 -0
  242. package/third-party/include/executorch/runtime/executor/method.h +21 -8
  243. package/third-party/include/executorch/runtime/executor/method_meta.h +20 -2
  244. package/third-party/include/executorch/runtime/executor/program.h +0 -10
  245. package/third-party/include/executorch/runtime/kernel/operator_registry.h +1 -1
  246. package/third-party/include/executorch/runtime/platform/compiler.h +2 -0
  247. package/third-party/include/executorch/schema/extended_header.h +10 -1
  248. package/third-party/include/torch/headeronly/macros/Export.h +66 -0
  249. package/third-party/include/torch/headeronly/macros/Macros.h +553 -0
  250. package/third-party/include/torch/headeronly/util/BFloat16.h +477 -0
  251. package/third-party/include/torch/headeronly/util/Half.h +781 -0
  252. package/third-party/include/torch/headeronly/util/TypeSafeSignMath.h +141 -0
  253. package/third-party/include/torch/headeronly/util/bit_cast.h +49 -0
  254. package/third-party/include/torch/headeronly/util/complex.h +593 -0
  255. package/third-party/include/torch/headeronly/util/floating_point_utils.h +38 -0
  256. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  257. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  258. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  259. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  260. package/common/rnexecutorch/tests/run_all_tests.sh +0 -14
  261. package/common/rnexecutorch/tests/run_test.sh +0 -18
  262. package/ios/RnExecutorch/utils/Conversions.h +0 -14
  263. package/ios/RnExecutorch/utils/ETError.h +0 -26
  264. package/ios/RnExecutorch/utils/ImageProcessor.h +0 -15
  265. package/ios/RnExecutorch/utils/ImageProcessor.mm +0 -147
  266. package/ios/RnExecutorch/utils/Numerical.h +0 -3
  267. package/ios/RnExecutorch/utils/Numerical.mm +0 -18
  268. package/ios/RnExecutorch/utils/ScalarType.h +0 -14
  269. package/ios/RnExecutorch/utils/ScalarType.mm +0 -21
  270. package/lib/module/hooks/useNonStaticModule.js.map +0 -1
  271. package/lib/typescript/hooks/useNonStaticModule.d.ts +0 -21
  272. package/lib/typescript/hooks/useNonStaticModule.d.ts.map +0 -1
  273. package/src/hooks/useNonStaticModule.ts +0 -74
  274. package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +0 -181
  275. package/third-party/include/executorch/extension/kernel_util/meta_programming.h +0 -108
  276. package/third-party/include/executorch/extension/kernel_util/type_list.h +0 -137
  277. package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +0 -35
@@ -0,0 +1,781 @@
1
+ #pragma once
2
+
3
+ /// Defines the Half type (half-precision floating-point) including conversions
4
+ /// to standard C types and basic arithmetic operations. Note that arithmetic
5
+ /// operations are implemented by converting to floating point and
6
+ /// performing the operation in float32, instead of using CUDA half intrinsics.
7
+ /// Most uses of this type within ATen are memory bound, including the
8
+ /// element-wise kernels, and the half intrinsics aren't efficient on all GPUs.
9
+ /// If you are writing a compute bound kernel, you can use the CUDA half
10
+ /// intrinsics directly on the Half type from device code.
11
+
12
+ #include <torch/headeronly/macros/Macros.h>
13
+ #include <torch/headeronly/util/bit_cast.h>
14
+ #include <torch/headeronly/util/floating_point_utils.h>
15
+
16
+ #if defined(__cplusplus)
17
+ #include <cmath>
18
+ #elif !defined(__OPENCL_VERSION__)
19
+ #include <math.h>
20
+ #endif
21
+
22
+ #ifdef _MSC_VER
23
+ #include <intrin.h>
24
+ #endif
25
+
26
+ #include <cstdint>
27
+ #include <cstring>
28
+ #include <ostream>
29
+
30
+ #ifdef __CUDACC__
31
+ #include <cuda_fp16.h>
32
+ #endif
33
+
34
+ #ifdef __HIPCC__
35
+ #include <hip/hip_fp16.h>
36
+ #endif
37
+
38
+ #if defined(CL_SYCL_LANGUAGE_VERSION)
39
+ #include <CL/sycl.hpp> // for SYCL 1.2.1
40
+ #elif defined(SYCL_LANGUAGE_VERSION)
41
+ #include <sycl/sycl.hpp> // for SYCL 2020
42
+ #endif
43
+
44
+ #if (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
45
+ !defined(__APPLE__)
46
+ #include <torch/headeronly/cpu/vec/vec_half.h>
47
+ #endif
48
+
49
+ #if defined(__aarch64__) && !defined(__CUDACC__)
50
+ #include <arm_neon.h>
51
+ #endif
52
+
53
+ #if defined(__GNUC__) || defined(__clang__)
54
+ #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || \
55
+ defined(_M_IX86)
56
+ #if defined(__F16C__) && !(defined(__CUDA_ARCH__) || defined(__CUDACC__) || \
57
+ defined(__HIP_DEVICE_COMPILE__))
58
+ #define C10_X86_F16 1
59
+ #include <immintrin.h> // import conversion ops from f16cintrin.h
60
+ #endif // defined(__F16C__) && !(defined(__CUDA_ARCH__) || defined(__CUDACC__)
61
+ // || defined(__HIP_DEVICE_COMPILE__))
62
+ #endif // __x86_64__ || _M_X64 || __i386 || _M_IX86
63
+ #endif // __GNUC__ || __clang__
64
+
65
+ namespace c10 {
66
+
67
+ struct alignas(2) Half {
68
+ unsigned short x;
69
+
70
+ struct from_bits_t {};
71
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
72
+ return from_bits_t();
73
+ }
74
+
75
+ // HIP wants __host__ __device__ tag, CUDA does not
76
+ #if defined(USE_ROCM)
77
+ C10_HOST_DEVICE Half() = default;
78
+ #else
79
+ Half() = default;
80
+ #endif
81
+
82
+ constexpr C10_HOST_DEVICE Half(unsigned short bits, from_bits_t) : x(bits) {}
83
+ #if defined(__aarch64__) && !defined(__CUDACC__)
84
+ inline Half(float16_t value);
85
+ inline operator float16_t() const;
86
+ #else
87
+ inline C10_HOST_DEVICE Half(float value);
88
+ inline C10_HOST_DEVICE operator float() const;
89
+ #endif
90
+
91
+ #if defined(__CUDACC__) || defined(__HIPCC__)
92
+ inline C10_HOST_DEVICE Half(const __half &value);
93
+ inline C10_HOST_DEVICE operator __half() const;
94
+ #endif
95
+ #ifdef SYCL_LANGUAGE_VERSION
96
+ inline C10_HOST_DEVICE Half(const sycl::half &value);
97
+ inline C10_HOST_DEVICE operator sycl::half() const;
98
+ #endif
99
+ };
100
+
101
+ inline std::ostream &operator<<(std::ostream &out, const Half &value) {
102
+ out << (float)value;
103
+ return out;
104
+ }
105
+
106
+ namespace detail {
107
+ /*
108
+ * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
109
+ * representation, to a 32-bit floating-point number in IEEE single-precision
110
+ * format.
111
+ *
112
+ * @note The implementation relies on IEEE-like (no assumption about rounding
113
+ * mode and no operations on denormals) floating-point operations and bitcasts
114
+ * between integer and floating-point variables.
115
+ */
116
+ C10_HOST_DEVICE inline float fp16_ieee_to_fp32_value(uint16_t h) {
117
+ #ifdef C10_X86_F16
118
+ return _cvtsh_ss(h);
119
+ #else
120
+ /*
121
+ * Extend the half-precision floating-point number to 32 bits and shift to the
122
+ * upper part of the 32-bit word:
123
+ * +---+-----+------------+-------------------+
124
+ * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
125
+ * +---+-----+------------+-------------------+
126
+ * Bits 31 26-30 16-25 0-15
127
+ *
128
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
129
+ * - zero bits.
130
+ */
131
+ const uint32_t w = (uint32_t)h << 16;
132
+ /*
133
+ * Extract the sign of the input number into the high bit of the 32-bit word:
134
+ *
135
+ * +---+----------------------------------+
136
+ * | S |0000000 00000000 00000000 00000000|
137
+ * +---+----------------------------------+
138
+ * Bits 31 0-31
139
+ */
140
+ const uint32_t sign = w & UINT32_C(0x80000000);
141
+ /*
142
+ * Extract mantissa and biased exponent of the input number into the high bits
143
+ * of the 32-bit word:
144
+ *
145
+ * +-----+------------+---------------------+
146
+ * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
147
+ * +-----+------------+---------------------+
148
+ * Bits 27-31 17-26 0-16
149
+ */
150
+ const uint32_t two_w = w + w;
151
+
152
+ /*
153
+ * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become
154
+ * mantissa and exponent of a single-precision floating-point number:
155
+ *
156
+ * S|Exponent | Mantissa
157
+ * +-+---+-----+------------+----------------+
158
+ * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
159
+ * +-+---+-----+------------+----------------+
160
+ * Bits | 23-31 | 0-22
161
+ *
162
+ * Next, there are some adjustments to the exponent:
163
+ * - The exponent needs to be corrected by the difference in exponent bias
164
+ * between single-precision and half-precision formats (0x7F - 0xF = 0x70)
165
+ * - Inf and NaN values in the inputs should become Inf and NaN values after
166
+ * conversion to the single-precision number. Therefore, if the biased
167
+ * exponent of the half-precision input was 0x1F (max possible value), the
168
+ * biased exponent of the single-precision output must be 0xFF (max possible
169
+ * value). We do this correction in two steps:
170
+ * - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset
171
+ * below) rather than by 0x70 suggested by the difference in the exponent bias
172
+ * (see above).
173
+ * - Then we multiply the single-precision result of exponent adjustment by
174
+ * 2**(-112) to reverse the effect of exponent adjustment by 0xE0 less the
175
+ * necessary exponent adjustment by 0x70 due to difference in exponent bias.
176
+ * The floating-point multiplication hardware would ensure than Inf and
177
+ * NaN would retain their value on at least partially IEEE754-compliant
178
+ * implementations.
179
+ *
180
+ * Note that the above operations do not handle denormal inputs (where biased
181
+ * exponent == 0). However, they also do not operate on denormal inputs, and
182
+ * do not produce denormal results.
183
+ */
184
+ constexpr uint32_t exp_offset = UINT32_C(0xE0) << 23;
185
+ // const float exp_scale = 0x1.0p-112f;
186
+ constexpr uint32_t scale_bits = (uint32_t)15 << 23;
187
+ float exp_scale_val = 0;
188
+ #if defined(_MSC_VER) && defined(__clang__)
189
+ __builtin_memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
190
+ #else
191
+ std::memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
192
+ #endif
193
+
194
+ const float exp_scale = exp_scale_val;
195
+ const float normalized_value =
196
+ fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
197
+
198
+ /*
199
+ * Convert denormalized half-precision inputs into single-precision results
200
+ * (always normalized). Zero inputs are also handled here.
201
+ *
202
+ * In a denormalized number the biased exponent is zero, and mantissa has
203
+ * on-zero bits. First, we shift mantissa into bits 0-9 of the 32-bit word.
204
+ *
205
+ * zeros | mantissa
206
+ * +---------------------------+------------+
207
+ * |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
208
+ * +---------------------------+------------+
209
+ * Bits 10-31 0-9
210
+ *
211
+ * Now, remember that denormalized half-precision numbers are represented as:
212
+ * FP16 = mantissa * 2**(-24).
213
+ * The trick is to construct a normalized single-precision number with the
214
+ * same mantissa and thehalf-precision input and with an exponent which would
215
+ * scale the corresponding mantissa bits to 2**(-24). A normalized
216
+ * single-precision floating-point number is represented as: FP32 = (1 +
217
+ * mantissa * 2**(-23)) * 2**(exponent - 127) Therefore, when the biased
218
+ * exponent is 126, a unit change in the mantissa of the input denormalized
219
+ * half-precision number causes a change of the constructed single-precision
220
+ * number by 2**(-24), i.e. the same amount.
221
+ *
222
+ * The last step is to adjust the bias of the constructed single-precision
223
+ * number. When the input half-precision number is zero, the constructed
224
+ * single-precision number has the value of FP32 = 1 * 2**(126 - 127) =
225
+ * 2**(-1) = 0.5 Therefore, we need to subtract 0.5 from the constructed
226
+ * single-precision number to get the numerical equivalent of the input
227
+ * half-precision number.
228
+ */
229
+ constexpr uint32_t magic_mask = UINT32_C(126) << 23;
230
+ constexpr float magic_bias = 0.5f;
231
+ const float denormalized_value =
232
+ fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
233
+
234
+ /*
235
+ * - Choose either results of conversion of input as a normalized number, or
236
+ * as a denormalized number, depending on the input exponent. The variable
237
+ * two_w contains input exponent in bits 27-31, therefore if its smaller than
238
+ * 2**27, the input is either a denormal number, or zero.
239
+ * - Combine the result of conversion of exponent and mantissa with the sign
240
+ * of the input number.
241
+ */
242
+ constexpr uint32_t denormalized_cutoff = UINT32_C(1) << 27;
243
+ const uint32_t result =
244
+ sign | (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value)
245
+ : fp32_to_bits(normalized_value));
246
+ return fp32_from_bits(result);
247
+ #endif // C10_X86_F16
248
+ }
249
+
250
+ /*
251
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
252
+ * 16-bit floating-point number in IEEE half-precision format, in bit
253
+ * representation.
254
+ *
255
+ * @note The implementation relies on IEEE-like (no assumption about rounding
256
+ * mode and no operations on denormals) floating-point operations and bitcasts
257
+ * between integer and floating-point variables.
258
+ */
259
+ inline uint16_t fp16_ieee_from_fp32_value(float f) {
260
+ #ifdef C10_X86_F16
261
+ return _cvtss_sh(f, _MM_FROUND_TO_NEAREST_INT);
262
+ #else
263
+ // const float scale_to_inf = 0x1.0p+112f;
264
+ // const float scale_to_zero = 0x1.0p-110f;
265
+ constexpr uint32_t scale_to_inf_bits = (uint32_t)239 << 23;
266
+ constexpr uint32_t scale_to_zero_bits = (uint32_t)17 << 23;
267
+ float scale_to_inf_val = 0, scale_to_zero_val = 0;
268
+ std::memcpy(&scale_to_inf_val, &scale_to_inf_bits, sizeof(scale_to_inf_val));
269
+ std::memcpy(&scale_to_zero_val, &scale_to_zero_bits,
270
+ sizeof(scale_to_zero_val));
271
+ const float scale_to_inf = scale_to_inf_val;
272
+ const float scale_to_zero = scale_to_zero_val;
273
+
274
+ #if defined(_MSC_VER) && _MSC_VER == 1916
275
+ float base = ((signbit(f) != 0 ? -f : f) * scale_to_inf) * scale_to_zero;
276
+ #else
277
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
278
+ #endif
279
+
280
+ const uint32_t w = fp32_to_bits(f);
281
+ const uint32_t shl1_w = w + w;
282
+ const uint32_t sign = w & UINT32_C(0x80000000);
283
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
284
+ if (bias < UINT32_C(0x71000000)) {
285
+ bias = UINT32_C(0x71000000);
286
+ }
287
+
288
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
289
+ const uint32_t bits = fp32_to_bits(base);
290
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
291
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
292
+ const uint32_t nonsign = exp_bits + mantissa_bits;
293
+ return static_cast<uint16_t>(
294
+ (sign >> 16) |
295
+ (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign));
296
+ #endif // C10_X86_F16
297
+ }
298
+
299
+ /*
300
+ * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
301
+ * representation, to a 32-bit floating-point number in IEEE single-precision
302
+ * format, in bit representation.
303
+ *
304
+ * @note The implementation doesn't use any floating-point operations.
305
+ */
306
+ inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
307
+ /*
308
+ * Extend the half-precision floating-point number to 32 bits and shift to the
309
+ * upper part of the 32-bit word:
310
+ * +---+-----+------------+-------------------+
311
+ * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
312
+ * +---+-----+------------+-------------------+
313
+ * Bits 31 26-30 16-25 0-15
314
+ *
315
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
316
+ * - zero bits.
317
+ */
318
+ const uint32_t w = (uint32_t)h << 16;
319
+ /*
320
+ * Extract the sign of the input number into the high bit of the 32-bit word:
321
+ *
322
+ * +---+----------------------------------+
323
+ * | S |0000000 00000000 00000000 00000000|
324
+ * +---+----------------------------------+
325
+ * Bits 31 0-31
326
+ */
327
+ const uint32_t sign = w & UINT32_C(0x80000000);
328
+ /*
329
+ * Extract mantissa and biased exponent of the input number into the bits 0-30
330
+ * of the 32-bit word:
331
+ *
332
+ * +---+-----+------------+-------------------+
333
+ * | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
334
+ * +---+-----+------------+-------------------+
335
+ * Bits 30 27-31 17-26 0-16
336
+ */
337
+ const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
338
+ /*
339
+ * Renorm shift is the number of bits to shift mantissa left to make the
340
+ * half-precision number normalized. If the initial number is normalized, some
341
+ * of its high 6 bits (sign == 0 and 5-bit exponent) equals one. In this case
342
+ * renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note
343
+ * that if we shift denormalized nonsign by renorm_shift, the unit bit of
344
+ * mantissa will shift into exponent, turning the biased exponent into 1, and
345
+ * making mantissa normalized (i.e. without leading 1).
346
+ */
347
+ #ifdef _MSC_VER
348
+ unsigned long nonsign_bsr;
349
+ _BitScanReverse(&nonsign_bsr, (unsigned long)nonsign);
350
+ uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
351
+ #else
352
+ uint32_t renorm_shift = __builtin_clz(nonsign);
353
+ #endif
354
+ renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
355
+ /*
356
+ * Iff half-precision number has exponent of 15, the addition overflows
357
+ * it into bit 31, and the subsequent shift turns the high 9 bits
358
+ * into 1. Thus inf_nan_mask == 0x7F800000 if the half-precision number
359
+ * had exponent of 15 (i.e. was NaN or infinity) 0x00000000 otherwise
360
+ */
361
+ const int32_t inf_nan_mask =
362
+ ((int32_t)(nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
363
+ /*
364
+ * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31
365
+ * into 1. Otherwise, bit 31 remains 0. The signed shift right by 31
366
+ * broadcasts bit 31 into all bits of the zero_mask. Thus zero_mask ==
367
+ * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
368
+ * 0x00000000 otherwise
369
+ */
370
+ const int32_t zero_mask = (int32_t)(nonsign - 1) >> 31;
371
+ /*
372
+ * 1. Shift nonsign left by renorm_shift to normalize it (if the input
373
+ * was denormal)
374
+ * 2. Shift nonsign right by 3 so the exponent (5 bits originally)
375
+ * becomes an 8-bit field and 10-bit mantissa shifts into the 10 high
376
+ * bits of the 23-bit mantissa of IEEE single-precision number.
377
+ * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the
378
+ * different in exponent bias (0x7F for single-precision number less 0xF
379
+ * for half-precision number).
380
+ * 4. Subtract renorm_shift from the exponent (starting at bit 23) to
381
+ * account for renormalization. As renorm_shift is less than 0x70, this
382
+ * can be combined with step 3.
383
+ * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the
384
+ * input was NaN or infinity.
385
+ * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent
386
+ * into zero if the input was zero.
387
+ * 7. Combine with the sign of the input number.
388
+ */
389
+ return sign |
390
+ ((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) |
391
+ inf_nan_mask) &
392
+ ~zero_mask);
393
+ }
394
+
395
+ #ifdef C10_X86_F16
396
+ #undef C10_X86_F16
397
+ #endif // C10_X86_F16
398
+
399
+ #if defined(__aarch64__) && !defined(__CUDACC__)
400
+ inline float16_t fp16_from_bits(uint16_t h) {
401
+ return c10::bit_cast<float16_t>(h);
402
+ }
403
+
404
+ inline uint16_t fp16_to_bits(float16_t f) { return c10::bit_cast<uint16_t>(f); }
405
+
406
+ // According to https://godbolt.org/z/frExdbsWG it would translate to single
407
+ // fcvt s0, h0
408
+ inline float native_fp16_to_fp32_value(uint16_t h) {
409
+ return static_cast<float>(fp16_from_bits(h));
410
+ }
411
+
412
+ inline uint16_t native_fp16_from_fp32_value(float f) {
413
+ return fp16_to_bits(static_cast<float16_t>(f));
414
+ }
415
+ #endif
416
+
417
+ } // namespace detail
418
+
419
+ //---------- below is copied from c10/util/Half-inl.h ----------------//
420
+ C10_CLANG_DIAGNOSTIC_PUSH()
421
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
422
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
423
+ #endif
424
+
425
+ #if defined(__aarch64__) && !defined(__CUDACC__)
426
+ /// Constructors
427
+ inline Half::Half(float16_t value) : x(detail::fp16_to_bits(value)) {}
428
+ inline Half::operator float16_t() const { return detail::fp16_from_bits(x); }
429
+ #else
430
+
431
+ inline C10_HOST_DEVICE Half::Half(float value)
432
+ :
433
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
434
+ x(__half_as_short(__float2half(value)))
435
+ #elif defined(__SYCL_DEVICE_ONLY__)
436
+ x(c10::bit_cast<uint16_t>(sycl::half(value)))
437
+ #elif (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
438
+ !defined(__APPLE__)
439
+ x(at::vec::float2half_scalar(value))
440
+ #else
441
+ x(detail::fp16_ieee_from_fp32_value(value))
442
+ #endif
443
+ {
444
+ }
445
+
446
+ /// Implicit conversions
447
+
448
+ inline C10_HOST_DEVICE Half::operator float() const {
449
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
450
+ return __half2float(*reinterpret_cast<const __half *>(&x));
451
+ #elif defined(__SYCL_DEVICE_ONLY__)
452
+ return float(c10::bit_cast<sycl::half>(x));
453
+ #elif (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
454
+ !defined(__APPLE__)
455
+ return at::vec::half2float_scalar(x);
456
+ #elif defined(__aarch64__) && !defined(__CUDACC__)
457
+ return detail::native_fp16_to_fp32_value(x);
458
+ #else
459
+ return detail::fp16_ieee_to_fp32_value(x);
460
+ #endif
461
+ }
462
+
463
+ #endif /* !defined(__aarch64__) || defined(__CUDACC__) \
464
+ */
465
+
466
+ #if defined(__CUDACC__) || defined(__HIPCC__)
467
+ inline C10_HOST_DEVICE Half::Half(const __half &value) {
468
+ x = *reinterpret_cast<const unsigned short *>(&value);
469
+ }
470
+ inline C10_HOST_DEVICE Half::operator __half() const {
471
+ return *reinterpret_cast<const __half *>(&x);
472
+ }
473
+ #endif
474
+
475
+ #ifdef SYCL_LANGUAGE_VERSION
476
+ inline C10_HOST_DEVICE Half::Half(const sycl::half &value) {
477
+ x = *reinterpret_cast<const unsigned short *>(&value);
478
+ }
479
+ inline C10_HOST_DEVICE Half::operator sycl::half() const {
480
+ return *reinterpret_cast<const sycl::half *>(&x);
481
+ }
482
+ #endif
483
+
484
+ // CUDA intrinsics
485
+
486
+ #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 350)) || \
487
+ (defined(__clang__) && defined(__CUDA__))
488
+ inline __device__ Half __ldg(const Half *ptr) {
489
+ return __ldg(reinterpret_cast<const __half *>(ptr));
490
+ }
491
+ #endif
492
+
493
+ /// Arithmetic
494
+
495
+ inline C10_HOST_DEVICE Half operator+(const Half &a, const Half &b) {
496
+ return static_cast<float>(a) + static_cast<float>(b);
497
+ }
498
+
499
+ inline C10_HOST_DEVICE Half operator-(const Half &a, const Half &b) {
500
+ return static_cast<float>(a) - static_cast<float>(b);
501
+ }
502
+
503
+ inline C10_HOST_DEVICE Half operator*(const Half &a, const Half &b) {
504
+ return static_cast<float>(a) * static_cast<float>(b);
505
+ }
506
+
507
+ inline C10_HOST_DEVICE Half operator/(const Half &a, const Half &b)
508
+ __ubsan_ignore_float_divide_by_zero__ {
509
+ return static_cast<float>(a) / static_cast<float>(b);
510
+ }
511
+
512
+ inline C10_HOST_DEVICE Half operator-(const Half &a) {
513
+ #if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530) || \
514
+ defined(__HIP_DEVICE_COMPILE__)
515
+ return __hneg(a);
516
+ #elif defined(__SYCL_DEVICE_ONLY__)
517
+ return -c10::bit_cast<sycl::half>(a);
518
+ #else
519
+ return -static_cast<float>(a);
520
+ #endif
521
+ }
522
+
523
+ inline C10_HOST_DEVICE Half &operator+=(Half &a, const Half &b) {
524
+ a = a + b;
525
+ return a;
526
+ }
527
+
528
+ inline C10_HOST_DEVICE Half &operator-=(Half &a, const Half &b) {
529
+ a = a - b;
530
+ return a;
531
+ }
532
+
533
+ inline C10_HOST_DEVICE Half &operator*=(Half &a, const Half &b) {
534
+ a = a * b;
535
+ return a;
536
+ }
537
+
538
+ inline C10_HOST_DEVICE Half &operator/=(Half &a, const Half &b) {
539
+ a = a / b;
540
+ return a;
541
+ }
542
+
543
+ /// Arithmetic with floats
544
+
545
+ inline C10_HOST_DEVICE float operator+(Half a, float b) {
546
+ return static_cast<float>(a) + b;
547
+ }
548
+ inline C10_HOST_DEVICE float operator-(Half a, float b) {
549
+ return static_cast<float>(a) - b;
550
+ }
551
+ inline C10_HOST_DEVICE float operator*(Half a, float b) {
552
+ return static_cast<float>(a) * b;
553
+ }
554
+ inline C10_HOST_DEVICE float
555
+ operator/(Half a, float b) __ubsan_ignore_float_divide_by_zero__ {
556
+ return static_cast<float>(a) / b;
557
+ }
558
+
559
+ inline C10_HOST_DEVICE float operator+(float a, Half b) {
560
+ return a + static_cast<float>(b);
561
+ }
562
+ inline C10_HOST_DEVICE float operator-(float a, Half b) {
563
+ return a - static_cast<float>(b);
564
+ }
565
+ inline C10_HOST_DEVICE float operator*(float a, Half b) {
566
+ return a * static_cast<float>(b);
567
+ }
568
+ inline C10_HOST_DEVICE float
569
+ operator/(float a, Half b) __ubsan_ignore_float_divide_by_zero__ {
570
+ return a / static_cast<float>(b);
571
+ }
572
+
573
+ inline C10_HOST_DEVICE float &operator+=(float &a, const Half &b) {
574
+ return a += static_cast<float>(b);
575
+ }
576
+ inline C10_HOST_DEVICE float &operator-=(float &a, const Half &b) {
577
+ return a -= static_cast<float>(b);
578
+ }
579
+ inline C10_HOST_DEVICE float &operator*=(float &a, const Half &b) {
580
+ return a *= static_cast<float>(b);
581
+ }
582
+ inline C10_HOST_DEVICE float &operator/=(float &a, const Half &b) {
583
+ return a /= static_cast<float>(b);
584
+ }
585
+
586
+ /// Arithmetic with doubles
587
+
588
+ inline C10_HOST_DEVICE double operator+(Half a, double b) {
589
+ return static_cast<double>(a) + b;
590
+ }
591
+ inline C10_HOST_DEVICE double operator-(Half a, double b) {
592
+ return static_cast<double>(a) - b;
593
+ }
594
+ inline C10_HOST_DEVICE double operator*(Half a, double b) {
595
+ return static_cast<double>(a) * b;
596
+ }
597
+ inline C10_HOST_DEVICE double
598
+ operator/(Half a, double b) __ubsan_ignore_float_divide_by_zero__ {
599
+ return static_cast<double>(a) / b;
600
+ }
601
+
602
+ inline C10_HOST_DEVICE double operator+(double a, Half b) {
603
+ return a + static_cast<double>(b);
604
+ }
605
+ inline C10_HOST_DEVICE double operator-(double a, Half b) {
606
+ return a - static_cast<double>(b);
607
+ }
608
+ inline C10_HOST_DEVICE double operator*(double a, Half b) {
609
+ return a * static_cast<double>(b);
610
+ }
611
+ inline C10_HOST_DEVICE double
612
+ operator/(double a, Half b) __ubsan_ignore_float_divide_by_zero__ {
613
+ return a / static_cast<double>(b);
614
+ }
615
+
616
+ /// Arithmetic with ints
617
+
618
+ inline C10_HOST_DEVICE Half operator+(Half a, int b) {
619
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
620
+ return a + static_cast<Half>(b);
621
+ }
622
+ inline C10_HOST_DEVICE Half operator-(Half a, int b) {
623
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
624
+ return a - static_cast<Half>(b);
625
+ }
626
+ inline C10_HOST_DEVICE Half operator*(Half a, int b) {
627
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
628
+ return a * static_cast<Half>(b);
629
+ }
630
+ inline C10_HOST_DEVICE Half operator/(Half a, int b) {
631
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
632
+ return a / static_cast<Half>(b);
633
+ }
634
+
635
+ inline C10_HOST_DEVICE Half operator+(int a, Half b) {
636
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
637
+ return static_cast<Half>(a) + b;
638
+ }
639
+ inline C10_HOST_DEVICE Half operator-(int a, Half b) {
640
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
641
+ return static_cast<Half>(a) - b;
642
+ }
643
+ inline C10_HOST_DEVICE Half operator*(int a, Half b) {
644
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
645
+ return static_cast<Half>(a) * b;
646
+ }
647
+ inline C10_HOST_DEVICE Half operator/(int a, Half b) {
648
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
649
+ return static_cast<Half>(a) / b;
650
+ }
651
+
652
+ //// Arithmetic with int64_t
653
+
654
+ inline C10_HOST_DEVICE Half operator+(Half a, int64_t b) {
655
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
656
+ return a + static_cast<Half>(b);
657
+ }
658
+ inline C10_HOST_DEVICE Half operator-(Half a, int64_t b) {
659
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
660
+ return a - static_cast<Half>(b);
661
+ }
662
+ inline C10_HOST_DEVICE Half operator*(Half a, int64_t b) {
663
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
664
+ return a * static_cast<Half>(b);
665
+ }
666
+ inline C10_HOST_DEVICE Half operator/(Half a, int64_t b) {
667
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
668
+ return a / static_cast<Half>(b);
669
+ }
670
+
671
+ inline C10_HOST_DEVICE Half operator+(int64_t a, Half b) {
672
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
673
+ return static_cast<Half>(a) + b;
674
+ }
675
+ inline C10_HOST_DEVICE Half operator-(int64_t a, Half b) {
676
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
677
+ return static_cast<Half>(a) - b;
678
+ }
679
+ inline C10_HOST_DEVICE Half operator*(int64_t a, Half b) {
680
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
681
+ return static_cast<Half>(a) * b;
682
+ }
683
+ inline C10_HOST_DEVICE Half operator/(int64_t a, Half b) {
684
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
685
+ return static_cast<Half>(a) / b;
686
+ }
687
+
688
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
689
+ /// conversion from c10::Half to float.
690
+
691
+ C10_CLANG_DIAGNOSTIC_POP()
692
+
693
+ } // namespace c10
694
+
695
+ namespace torch::headeronly {
696
+
697
+ using c10::Half;
698
+ using c10::operator+;
699
+ using c10::operator-;
700
+ using c10::operator*;
701
+ using c10::operator/;
702
+ using c10::operator+=;
703
+ using c10::operator-=;
704
+ using c10::operator*=;
705
+ using c10::operator/=;
706
+ using c10::operator<<;
707
+
708
+ namespace detail {
709
+ #if defined(__aarch64__) && !defined(__CUDACC__)
710
+ using c10::detail::fp16_from_bits;
711
+ using c10::detail::fp16_to_bits;
712
+ using c10::detail::native_fp16_from_fp32_value;
713
+ using c10::detail::native_fp16_to_fp32_value;
714
+ #endif
715
+
716
+ using c10::detail::fp16_ieee_from_fp32_value;
717
+ using c10::detail::fp16_ieee_to_fp32_bits;
718
+ using c10::detail::fp16_ieee_to_fp32_value;
719
+ } // namespace detail
720
+
721
+ } // namespace torch::headeronly
722
+
723
+ namespace std {
724
+
725
+ template <> class numeric_limits<c10::Half> {
726
+ public:
727
+ static constexpr bool is_specialized = true;
728
+ static constexpr bool is_signed = true;
729
+ static constexpr bool is_integer = false;
730
+ static constexpr bool is_exact = false;
731
+ static constexpr bool has_infinity = true;
732
+ static constexpr bool has_quiet_NaN = true;
733
+ static constexpr bool has_signaling_NaN = true;
734
+ static constexpr auto has_denorm = numeric_limits<float>::has_denorm;
735
+ static constexpr auto has_denorm_loss =
736
+ numeric_limits<float>::has_denorm_loss;
737
+ static constexpr auto round_style = numeric_limits<float>::round_style;
738
+ static constexpr bool is_iec559 = true;
739
+ static constexpr bool is_bounded = true;
740
+ static constexpr bool is_modulo = false;
741
+ static constexpr int digits = 11;
742
+ static constexpr int digits10 = 3;
743
+ static constexpr int max_digits10 = 5;
744
+ static constexpr int radix = 2;
745
+ static constexpr int min_exponent = -13;
746
+ static constexpr int min_exponent10 = -4;
747
+ static constexpr int max_exponent = 16;
748
+ static constexpr int max_exponent10 = 4;
749
+ static constexpr auto traps = numeric_limits<float>::traps;
750
+ static constexpr auto tinyness_before =
751
+ numeric_limits<float>::tinyness_before;
752
+ static constexpr c10::Half min() {
753
+ return c10::Half(0x0400, c10::Half::from_bits());
754
+ }
755
+ static constexpr c10::Half lowest() {
756
+ return c10::Half(0xFBFF, c10::Half::from_bits());
757
+ }
758
+ static constexpr c10::Half max() {
759
+ return c10::Half(0x7BFF, c10::Half::from_bits());
760
+ }
761
+ static constexpr c10::Half epsilon() {
762
+ return c10::Half(0x1400, c10::Half::from_bits());
763
+ }
764
+ static constexpr c10::Half round_error() {
765
+ return c10::Half(0x3800, c10::Half::from_bits());
766
+ }
767
+ static constexpr c10::Half infinity() {
768
+ return c10::Half(0x7C00, c10::Half::from_bits());
769
+ }
770
+ static constexpr c10::Half quiet_NaN() {
771
+ return c10::Half(0x7E00, c10::Half::from_bits());
772
+ }
773
+ static constexpr c10::Half signaling_NaN() {
774
+ return c10::Half(0x7D00, c10::Half::from_bits());
775
+ }
776
+ static constexpr c10::Half denorm_min() {
777
+ return c10::Half(0x0001, c10::Half::from_bits());
778
+ }
779
+ };
780
+
781
+ } // namespace std