react-native-executorch 0.5.15 → 0.6.0
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.
- package/README.md +42 -36
- package/android/CMakeLists.txt +13 -25
- package/android/build.gradle +2 -3
- package/android/libs/classes.jar +0 -0
- package/android/src/main/cpp/CMakeLists.txt +2 -1
- package/common/rnexecutorch/RnExecutorchInstaller.cpp +18 -0
- package/common/rnexecutorch/TokenizerModule.cpp +3 -3
- package/common/rnexecutorch/data_processing/Numerical.cpp +31 -23
- package/common/rnexecutorch/data_processing/Numerical.h +6 -1
- package/common/rnexecutorch/data_processing/dsp.cpp +0 -46
- package/common/rnexecutorch/host_objects/JsiConversions.h +16 -0
- package/common/rnexecutorch/host_objects/ModelHostObject.h +26 -11
- package/common/rnexecutorch/jsi/OwningArrayBuffer.h +19 -2
- package/common/rnexecutorch/metaprogramming/TypeConcepts.h +0 -20
- package/common/rnexecutorch/models/BaseModel.cpp +12 -11
- package/common/rnexecutorch/models/BaseModel.h +18 -10
- package/common/rnexecutorch/models/embeddings/BaseEmbeddings.cpp +3 -11
- package/common/rnexecutorch/models/embeddings/text/TextEmbeddings.cpp +0 -1
- package/common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp +6 -12
- package/common/rnexecutorch/models/llm/LLM.cpp +25 -8
- package/common/rnexecutorch/models/llm/LLM.h +4 -4
- package/common/rnexecutorch/models/ocr/CTCLabelConverter.h +1 -1
- package/common/rnexecutorch/models/ocr/utils/RecognitionHandlerUtils.cpp +7 -4
- package/common/rnexecutorch/models/speech_to_text/SpeechToText.cpp +8 -13
- package/common/rnexecutorch/models/speech_to_text/SpeechToText.h +1 -3
- package/common/rnexecutorch/models/speech_to_text/asr/ASR.cpp +12 -19
- package/common/rnexecutorch/models/speech_to_text/asr/ASR.h +4 -5
- package/common/rnexecutorch/models/text_to_image/Constants.h +9 -0
- package/common/rnexecutorch/models/text_to_image/Decoder.cpp +32 -0
- package/common/rnexecutorch/models/text_to_image/Decoder.h +24 -0
- package/common/rnexecutorch/models/text_to_image/Encoder.cpp +44 -0
- package/common/rnexecutorch/models/text_to_image/Encoder.h +32 -0
- package/common/rnexecutorch/models/text_to_image/Scheduler.cpp +152 -0
- package/common/rnexecutorch/models/text_to_image/Scheduler.h +41 -0
- package/common/rnexecutorch/models/text_to_image/TextToImage.cpp +141 -0
- package/common/rnexecutorch/models/text_to_image/TextToImage.h +64 -0
- package/common/rnexecutorch/models/text_to_image/UNet.cpp +38 -0
- package/common/rnexecutorch/models/text_to_image/UNet.h +28 -0
- package/common/rnexecutorch/models/voice_activity_detection/Constants.h +27 -0
- package/common/rnexecutorch/models/voice_activity_detection/Types.h +12 -0
- package/common/rnexecutorch/models/voice_activity_detection/Utils.cpp +15 -0
- package/common/rnexecutorch/models/voice_activity_detection/Utils.h +13 -0
- package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.cpp +160 -0
- package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.h +36 -0
- package/common/rnexecutorch/tests/CMakeLists.txt +30 -0
- package/common/rnexecutorch/tests/NumericalTest.cpp +110 -0
- package/common/rnexecutorch/tests/README.md +30 -13
- package/common/rnexecutorch/threads/GlobalThreadPool.h +4 -0
- package/common/runner/arange_util.cpp +44 -0
- package/common/runner/arange_util.h +37 -0
- package/common/runner/constants.h +28 -0
- package/common/runner/io_manager.h +240 -0
- package/common/runner/irunner.h +87 -16
- package/common/runner/kernel_includes.h +23 -0
- package/common/runner/runner.cpp +151 -66
- package/common/runner/runner.h +39 -22
- package/common/runner/sampler.cpp +8 -1
- package/common/runner/sampler.h +4 -2
- package/common/runner/stats.h +1 -4
- package/common/runner/text_decoder_runner.cpp +26 -12
- package/common/runner/text_decoder_runner.h +52 -31
- package/common/runner/text_prefiller.cpp +46 -12
- package/common/runner/text_prefiller.h +38 -4
- package/common/runner/text_token_generator.h +51 -26
- package/common/runner/util.h +53 -8
- package/ios/RnExecutorch.xcodeproj/project.pbxproj +0 -23
- package/lib/module/Error.js +1 -0
- package/lib/module/Error.js.map +1 -1
- package/lib/module/constants/directories.js +1 -1
- package/lib/module/constants/directories.js.map +1 -1
- package/lib/module/constants/modelUrls.js +32 -1
- package/lib/module/constants/modelUrls.js.map +1 -1
- package/lib/module/constants/ocr/models.js +7 -7
- package/lib/module/constants/ocr/models.js.map +1 -1
- package/lib/module/constants/ocr/symbols.js +3 -2
- package/lib/module/constants/ocr/symbols.js.map +1 -1
- package/lib/module/controllers/LLMController.js +10 -1
- package/lib/module/controllers/LLMController.js.map +1 -1
- package/lib/module/controllers/OCRController.js +3 -3
- package/lib/module/controllers/OCRController.js.map +1 -1
- package/lib/module/controllers/VerticalOCRController.js +2 -2
- package/lib/module/controllers/VerticalOCRController.js.map +1 -1
- package/lib/module/hooks/computer_vision/useOCR.js +3 -3
- package/lib/module/hooks/computer_vision/useOCR.js.map +1 -1
- package/lib/module/hooks/{useNonStaticModule.js → computer_vision/useTextToImage.js} +21 -16
- package/lib/module/hooks/computer_vision/useTextToImage.js.map +1 -0
- package/lib/module/hooks/computer_vision/useVerticalOCR.js +3 -3
- package/lib/module/hooks/computer_vision/useVerticalOCR.js.map +1 -1
- package/lib/module/hooks/natural_language_processing/useLLM.js +3 -3
- package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
- package/lib/module/hooks/natural_language_processing/useTokenizer.js +5 -5
- package/lib/module/hooks/natural_language_processing/useTokenizer.js.map +1 -1
- package/lib/module/hooks/natural_language_processing/useVAD.js +13 -0
- package/lib/module/hooks/natural_language_processing/useVAD.js.map +1 -0
- package/lib/module/index.js +7 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/modules/computer_vision/OCRModule.js +2 -2
- package/lib/module/modules/computer_vision/OCRModule.js.map +1 -1
- package/lib/module/modules/computer_vision/TextToImageModule.js +48 -0
- package/lib/module/modules/computer_vision/TextToImageModule.js.map +1 -0
- package/lib/module/modules/computer_vision/VerticalOCRModule.js +2 -2
- package/lib/module/modules/computer_vision/VerticalOCRModule.js.map +1 -1
- package/lib/module/modules/natural_language_processing/SpeechToTextModule.js +7 -4
- package/lib/module/modules/natural_language_processing/SpeechToTextModule.js.map +1 -1
- package/lib/module/modules/natural_language_processing/VADModule.js +19 -0
- package/lib/module/modules/natural_language_processing/VADModule.js.map +1 -0
- package/lib/module/types/llm.js.map +1 -1
- package/lib/module/types/vad.js +2 -0
- package/lib/module/types/vad.js.map +1 -0
- package/lib/module/utils/ResourceFetcher.js +2 -1
- package/lib/module/utils/ResourceFetcher.js.map +1 -1
- package/lib/module/utils/ResourceFetcherUtils.js +6 -6
- package/lib/module/utils/ResourceFetcherUtils.js.map +1 -1
- package/lib/typescript/Error.d.ts +1 -0
- package/lib/typescript/Error.d.ts.map +1 -1
- package/lib/typescript/constants/modelUrls.d.ts +23 -0
- package/lib/typescript/constants/modelUrls.d.ts.map +1 -1
- package/lib/typescript/constants/ocr/symbols.d.ts +1 -1
- package/lib/typescript/constants/ocr/symbols.d.ts.map +1 -1
- package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
- package/lib/typescript/controllers/OCRController.d.ts +1 -1
- package/lib/typescript/controllers/OCRController.d.ts.map +1 -1
- package/lib/typescript/controllers/VerticalOCRController.d.ts +1 -1
- package/lib/typescript/controllers/VerticalOCRController.d.ts.map +1 -1
- package/lib/typescript/hooks/computer_vision/useOCR.d.ts +1 -1
- package/lib/typescript/hooks/computer_vision/useOCR.d.ts.map +1 -1
- package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts +22 -0
- package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts.map +1 -0
- package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts +1 -1
- package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts.map +1 -1
- package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
- package/lib/typescript/hooks/natural_language_processing/useSpeechToText.d.ts +2 -2
- package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts +16 -0
- package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +8 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/modules/computer_vision/OCRModule.d.ts +1 -1
- package/lib/typescript/modules/computer_vision/OCRModule.d.ts.map +1 -1
- package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts +16 -0
- package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts.map +1 -0
- package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts +1 -1
- package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts.map +1 -1
- package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts +3 -2
- package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts.map +1 -1
- package/lib/typescript/modules/natural_language_processing/VADModule.d.ts +10 -0
- package/lib/typescript/modules/natural_language_processing/VADModule.d.ts.map +1 -0
- package/lib/typescript/types/llm.d.ts +2 -0
- package/lib/typescript/types/llm.d.ts.map +1 -1
- package/lib/typescript/types/vad.d.ts +5 -0
- package/lib/typescript/types/vad.d.ts.map +1 -0
- package/lib/typescript/utils/ResourceFetcher.d.ts +29 -0
- package/lib/typescript/utils/ResourceFetcher.d.ts.map +1 -1
- package/lib/typescript/utils/ResourceFetcherUtils.d.ts +2 -2
- package/lib/typescript/utils/ResourceFetcherUtils.d.ts.map +1 -1
- package/package.json +11 -8
- package/react-native-executorch.podspec +9 -9
- package/src/Error.ts +1 -0
- package/src/constants/directories.ts +1 -1
- package/src/constants/modelUrls.ts +36 -1
- package/src/constants/ocr/models.ts +7 -7
- package/src/constants/ocr/symbols.ts +3 -2
- package/src/controllers/LLMController.ts +12 -1
- package/src/controllers/OCRController.ts +3 -3
- package/src/controllers/VerticalOCRController.ts +2 -2
- package/src/hooks/computer_vision/useOCR.ts +4 -5
- package/src/hooks/computer_vision/useTextToImage.ts +92 -0
- package/src/hooks/computer_vision/useVerticalOCR.ts +4 -5
- package/src/hooks/natural_language_processing/useLLM.ts +3 -4
- package/src/hooks/natural_language_processing/useTokenizer.ts +5 -5
- package/src/hooks/natural_language_processing/useVAD.ts +15 -0
- package/src/index.ts +20 -1
- package/src/modules/computer_vision/OCRModule.ts +2 -2
- package/src/modules/computer_vision/TextToImageModule.ts +93 -0
- package/src/modules/computer_vision/VerticalOCRModule.ts +2 -2
- package/src/modules/natural_language_processing/SpeechToTextModule.ts +8 -4
- package/src/modules/natural_language_processing/VADModule.ts +27 -0
- package/src/types/llm.ts +2 -0
- package/src/types/vad.ts +4 -0
- package/src/utils/ResourceFetcher.ts +2 -1
- package/src/utils/ResourceFetcherUtils.ts +8 -8
- package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
- package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
- package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
- package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
- package/third-party/include/c10/macros/Export.h +0 -78
- package/third-party/include/c10/macros/Macros.h +1 -520
- package/third-party/include/c10/util/BFloat16-inl.h +1 -339
- package/third-party/include/c10/util/BFloat16.h +1 -122
- package/third-party/include/c10/util/Half-inl.h +1 -347
- package/third-party/include/c10/util/Half.h +6 -419
- package/third-party/include/c10/util/TypeSafeSignMath.h +1 -133
- package/third-party/include/c10/util/bit_cast.h +1 -43
- package/third-party/include/c10/util/complex.h +1 -568
- package/third-party/include/c10/util/floating_point_utils.h +1 -33
- package/third-party/include/c10/util/irange.h +1 -1
- package/third-party/include/c10/util/llvmMathExtras.h +866 -0
- package/third-party/include/c10/util/safe_numerics.h +97 -0
- package/third-party/include/executorch/ExecuTorchError.h +6 -7
- package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLM.h +12 -0
- package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMConfig.h +56 -0
- package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMError.h +16 -0
- package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMMultimodalRunner.h +227 -0
- package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMTextRunner.h +97 -0
- package/third-party/include/executorch/ExecuTorchLLM/module.modulemap +4 -0
- package/third-party/include/executorch/ExecuTorchLog.h +1 -0
- package/third-party/include/executorch/ExecuTorchModule.h +177 -4
- package/third-party/include/executorch/ExecuTorchTensor.h +3 -4
- package/third-party/include/executorch/ExecuTorchValue.h +1 -7
- package/third-party/include/executorch/extension/module/module.h +139 -8
- package/third-party/include/executorch/extension/tensor/tensor.h +1 -0
- package/third-party/include/executorch/extension/tensor/tensor_ptr.h +88 -26
- package/third-party/include/executorch/extension/threadpool/threadpool.h +4 -1
- package/third-party/include/executorch/runtime/backend/backend_init_context.h +6 -0
- package/third-party/include/executorch/runtime/backend/interface.h +1 -1
- package/third-party/include/executorch/runtime/core/error.h +76 -49
- package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +18 -4
- package/third-party/include/executorch/runtime/core/memory_allocator.h +12 -2
- package/third-party/include/executorch/runtime/core/named_data_map.h +1 -11
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +0 -78
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +1 -520
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -339
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +1 -122
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half-inl.h +1 -347
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +6 -419
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/TypeSafeSignMath.h +1 -133
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -43
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +1 -568
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/floating_point_utils.h +1 -33
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +1 -1
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/llvmMathExtras.h +866 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/safe_numerics.h +97 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +66 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Macros.h +553 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/BFloat16.h +477 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/Half.h +781 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/TypeSafeSignMath.h +141 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/bit_cast.h +49 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/complex.h +593 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/floating_point_utils.h +38 -0
- package/third-party/include/executorch/runtime/core/tensor_layout.h +1 -1
- package/third-party/include/executorch/runtime/executor/merged_data_map.h +142 -0
- package/third-party/include/executorch/runtime/executor/method.h +21 -8
- package/third-party/include/executorch/runtime/executor/method_meta.h +20 -2
- package/third-party/include/executorch/runtime/executor/program.h +0 -10
- package/third-party/include/executorch/runtime/kernel/operator_registry.h +1 -1
- package/third-party/include/executorch/runtime/platform/compiler.h +2 -0
- package/third-party/include/executorch/schema/extended_header.h +10 -1
- package/third-party/include/torch/headeronly/macros/Export.h +66 -0
- package/third-party/include/torch/headeronly/macros/Macros.h +553 -0
- package/third-party/include/torch/headeronly/util/BFloat16.h +477 -0
- package/third-party/include/torch/headeronly/util/Half.h +781 -0
- package/third-party/include/torch/headeronly/util/TypeSafeSignMath.h +141 -0
- package/third-party/include/torch/headeronly/util/bit_cast.h +49 -0
- package/third-party/include/torch/headeronly/util/complex.h +593 -0
- package/third-party/include/torch/headeronly/util/floating_point_utils.h +38 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
- package/common/rnexecutorch/tests/run_all_tests.sh +0 -14
- package/common/rnexecutorch/tests/run_test.sh +0 -18
- package/ios/RnExecutorch/utils/Conversions.h +0 -14
- package/ios/RnExecutorch/utils/ETError.h +0 -26
- package/ios/RnExecutorch/utils/ImageProcessor.h +0 -15
- package/ios/RnExecutorch/utils/ImageProcessor.mm +0 -147
- package/ios/RnExecutorch/utils/Numerical.h +0 -3
- package/ios/RnExecutorch/utils/Numerical.mm +0 -18
- package/ios/RnExecutorch/utils/ScalarType.h +0 -14
- package/ios/RnExecutorch/utils/ScalarType.mm +0 -21
- package/lib/module/hooks/useNonStaticModule.js.map +0 -1
- package/lib/typescript/hooks/useNonStaticModule.d.ts +0 -21
- package/lib/typescript/hooks/useNonStaticModule.d.ts.map +0 -1
- package/src/hooks/useNonStaticModule.ts +0 -74
- package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +0 -181
- package/third-party/include/executorch/extension/kernel_util/meta_programming.h +0 -108
- package/third-party/include/executorch/extension/kernel_util/type_list.h +0 -137
- package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +0 -35
|
@@ -1,421 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
#include <torch/headeronly/util/Half.h>
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 <c10/macros/Export.h>
|
|
13
|
-
#include <c10/macros/Macros.h>
|
|
14
|
-
#include <c10/util/bit_cast.h>
|
|
15
|
-
#include <c10/util/floating_point_utils.h>
|
|
16
|
-
#include <type_traits>
|
|
17
|
-
|
|
18
|
-
#if defined(__cplusplus)
|
|
19
|
-
#include <cmath>
|
|
20
|
-
#elif !defined(__OPENCL_VERSION__)
|
|
21
|
-
#include <math.h>
|
|
22
|
-
#endif
|
|
23
|
-
|
|
24
|
-
#ifdef _MSC_VER
|
|
25
|
-
#include <intrin.h>
|
|
26
|
-
#endif
|
|
27
|
-
|
|
28
|
-
#include <cstdint>
|
|
29
|
-
#include <cstring>
|
|
30
|
-
#include <iosfwd>
|
|
31
|
-
#include <limits>
|
|
32
|
-
#include <ostream>
|
|
33
|
-
|
|
34
|
-
#ifdef __CUDACC__
|
|
35
|
-
#include <cuda_fp16.h>
|
|
36
|
-
#endif
|
|
37
|
-
|
|
38
|
-
#ifdef __HIPCC__
|
|
39
|
-
#include <hip/hip_fp16.h>
|
|
40
|
-
#endif
|
|
41
|
-
|
|
42
|
-
#if defined(CL_SYCL_LANGUAGE_VERSION)
|
|
43
|
-
#include <CL/sycl.hpp> // for SYCL 1.2.1
|
|
44
|
-
#elif defined(SYCL_LANGUAGE_VERSION)
|
|
45
|
-
#include <sycl/sycl.hpp> // for SYCL 2020
|
|
46
|
-
#endif
|
|
47
|
-
|
|
48
|
-
#if defined(__aarch64__) && !defined(__CUDACC__)
|
|
49
|
-
#include <arm_neon.h>
|
|
50
|
-
#endif
|
|
51
|
-
|
|
52
|
-
#if defined(__GNUC__) || defined(__clang__)
|
|
53
|
-
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || \
|
|
54
|
-
defined(_M_IX86)
|
|
55
|
-
#if defined(__F16C__) && !(defined(__CUDA_ARCH__) || defined(__CUDACC__) || \
|
|
56
|
-
defined(__HIP_DEVICE_COMPILE__))
|
|
57
|
-
#define C10_X86_F16 1
|
|
58
|
-
#include <immintrin.h> // import conversion ops from f16cintrin.h
|
|
59
|
-
#endif // defined(__F16C__) && !(defined(__CUDA_ARCH__) || defined(__CUDACC__)
|
|
60
|
-
// || defined(__HIP_DEVICE_COMPILE__))
|
|
61
|
-
#endif // __x86_64__ || _M_X64 || __i386 || _M_IX86
|
|
62
|
-
#endif // __GNUC__ || __clang__
|
|
63
|
-
|
|
64
|
-
namespace c10 {
|
|
65
|
-
|
|
66
|
-
namespace detail {
|
|
67
|
-
|
|
68
|
-
/*
|
|
69
|
-
* Convert a 16-bit floating-point number in IEEE half-precision format, in bit
|
|
70
|
-
* representation, to a 32-bit floating-point number in IEEE single-precision
|
|
71
|
-
* format, in bit representation.
|
|
72
|
-
*
|
|
73
|
-
* @note The implementation doesn't use any floating-point operations.
|
|
74
|
-
*/
|
|
75
|
-
inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
|
|
76
|
-
/*
|
|
77
|
-
* Extend the half-precision floating-point number to 32 bits and shift to the
|
|
78
|
-
* upper part of the 32-bit word:
|
|
79
|
-
* +---+-----+------------+-------------------+
|
|
80
|
-
* | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
|
|
81
|
-
* +---+-----+------------+-------------------+
|
|
82
|
-
* Bits 31 26-30 16-25 0-15
|
|
83
|
-
*
|
|
84
|
-
* S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
|
|
85
|
-
* - zero bits.
|
|
86
|
-
*/
|
|
87
|
-
const uint32_t w = (uint32_t)h << 16;
|
|
88
|
-
/*
|
|
89
|
-
* Extract the sign of the input number into the high bit of the 32-bit word:
|
|
90
|
-
*
|
|
91
|
-
* +---+----------------------------------+
|
|
92
|
-
* | S |0000000 00000000 00000000 00000000|
|
|
93
|
-
* +---+----------------------------------+
|
|
94
|
-
* Bits 31 0-31
|
|
95
|
-
*/
|
|
96
|
-
const uint32_t sign = w & UINT32_C(0x80000000);
|
|
97
|
-
/*
|
|
98
|
-
* Extract mantissa and biased exponent of the input number into the bits 0-30
|
|
99
|
-
* of the 32-bit word:
|
|
100
|
-
*
|
|
101
|
-
* +---+-----+------------+-------------------+
|
|
102
|
-
* | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
|
|
103
|
-
* +---+-----+------------+-------------------+
|
|
104
|
-
* Bits 30 27-31 17-26 0-16
|
|
105
|
-
*/
|
|
106
|
-
const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
|
|
107
|
-
/*
|
|
108
|
-
* Renorm shift is the number of bits to shift mantissa left to make the
|
|
109
|
-
* half-precision number normalized. If the initial number is normalized, some
|
|
110
|
-
* of its high 6 bits (sign == 0 and 5-bit exponent) equals one. In this case
|
|
111
|
-
* renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note
|
|
112
|
-
* that if we shift denormalized nonsign by renorm_shift, the unit bit of
|
|
113
|
-
* mantissa will shift into exponent, turning the biased exponent into 1, and
|
|
114
|
-
* making mantissa normalized (i.e. without leading 1).
|
|
115
|
-
*/
|
|
116
|
-
#ifdef _MSC_VER
|
|
117
|
-
unsigned long nonsign_bsr;
|
|
118
|
-
_BitScanReverse(&nonsign_bsr, (unsigned long)nonsign);
|
|
119
|
-
uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
|
|
120
|
-
#else
|
|
121
|
-
uint32_t renorm_shift = __builtin_clz(nonsign);
|
|
122
|
-
#endif
|
|
123
|
-
renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
|
|
124
|
-
/*
|
|
125
|
-
* Iff half-precision number has exponent of 15, the addition overflows
|
|
126
|
-
* it into bit 31, and the subsequent shift turns the high 9 bits
|
|
127
|
-
* into 1. Thus inf_nan_mask == 0x7F800000 if the half-precision number
|
|
128
|
-
* had exponent of 15 (i.e. was NaN or infinity) 0x00000000 otherwise
|
|
129
|
-
*/
|
|
130
|
-
const int32_t inf_nan_mask =
|
|
131
|
-
((int32_t)(nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
|
|
132
|
-
/*
|
|
133
|
-
* Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31
|
|
134
|
-
* into 1. Otherwise, bit 31 remains 0. The signed shift right by 31
|
|
135
|
-
* broadcasts bit 31 into all bits of the zero_mask. Thus zero_mask ==
|
|
136
|
-
* 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
|
|
137
|
-
* 0x00000000 otherwise
|
|
138
|
-
*/
|
|
139
|
-
const int32_t zero_mask = (int32_t)(nonsign - 1) >> 31;
|
|
140
|
-
/*
|
|
141
|
-
* 1. Shift nonsign left by renorm_shift to normalize it (if the input
|
|
142
|
-
* was denormal)
|
|
143
|
-
* 2. Shift nonsign right by 3 so the exponent (5 bits originally)
|
|
144
|
-
* becomes an 8-bit field and 10-bit mantissa shifts into the 10 high
|
|
145
|
-
* bits of the 23-bit mantissa of IEEE single-precision number.
|
|
146
|
-
* 3. Add 0x70 to the exponent (starting at bit 23) to compensate the
|
|
147
|
-
* different in exponent bias (0x7F for single-precision number less 0xF
|
|
148
|
-
* for half-precision number).
|
|
149
|
-
* 4. Subtract renorm_shift from the exponent (starting at bit 23) to
|
|
150
|
-
* account for renormalization. As renorm_shift is less than 0x70, this
|
|
151
|
-
* can be combined with step 3.
|
|
152
|
-
* 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the
|
|
153
|
-
* input was NaN or infinity.
|
|
154
|
-
* 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent
|
|
155
|
-
* into zero if the input was zero.
|
|
156
|
-
* 7. Combine with the sign of the input number.
|
|
157
|
-
*/
|
|
158
|
-
return sign |
|
|
159
|
-
((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) |
|
|
160
|
-
inf_nan_mask) &
|
|
161
|
-
~zero_mask);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/*
|
|
165
|
-
* Convert a 16-bit floating-point number in IEEE half-precision format, in bit
|
|
166
|
-
* representation, to a 32-bit floating-point number in IEEE single-precision
|
|
167
|
-
* format.
|
|
168
|
-
*
|
|
169
|
-
* @note The implementation relies on IEEE-like (no assumption about rounding
|
|
170
|
-
* mode and no operations on denormals) floating-point operations and bitcasts
|
|
171
|
-
* between integer and floating-point variables.
|
|
172
|
-
*/
|
|
173
|
-
C10_HOST_DEVICE inline float fp16_ieee_to_fp32_value(uint16_t h) {
|
|
174
|
-
#ifdef C10_X86_F16
|
|
175
|
-
return _cvtsh_ss(h);
|
|
176
|
-
#else
|
|
177
|
-
/*
|
|
178
|
-
* Extend the half-precision floating-point number to 32 bits and shift to the
|
|
179
|
-
* upper part of the 32-bit word:
|
|
180
|
-
* +---+-----+------------+-------------------+
|
|
181
|
-
* | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
|
|
182
|
-
* +---+-----+------------+-------------------+
|
|
183
|
-
* Bits 31 26-30 16-25 0-15
|
|
184
|
-
*
|
|
185
|
-
* S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
|
|
186
|
-
* - zero bits.
|
|
187
|
-
*/
|
|
188
|
-
const uint32_t w = (uint32_t)h << 16;
|
|
189
|
-
/*
|
|
190
|
-
* Extract the sign of the input number into the high bit of the 32-bit word:
|
|
191
|
-
*
|
|
192
|
-
* +---+----------------------------------+
|
|
193
|
-
* | S |0000000 00000000 00000000 00000000|
|
|
194
|
-
* +---+----------------------------------+
|
|
195
|
-
* Bits 31 0-31
|
|
196
|
-
*/
|
|
197
|
-
const uint32_t sign = w & UINT32_C(0x80000000);
|
|
198
|
-
/*
|
|
199
|
-
* Extract mantissa and biased exponent of the input number into the high bits
|
|
200
|
-
* of the 32-bit word:
|
|
201
|
-
*
|
|
202
|
-
* +-----+------------+---------------------+
|
|
203
|
-
* |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
|
|
204
|
-
* +-----+------------+---------------------+
|
|
205
|
-
* Bits 27-31 17-26 0-16
|
|
206
|
-
*/
|
|
207
|
-
const uint32_t two_w = w + w;
|
|
208
|
-
|
|
209
|
-
/*
|
|
210
|
-
* Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become
|
|
211
|
-
* mantissa and exponent of a single-precision floating-point number:
|
|
212
|
-
*
|
|
213
|
-
* S|Exponent | Mantissa
|
|
214
|
-
* +-+---+-----+------------+----------------+
|
|
215
|
-
* |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
|
|
216
|
-
* +-+---+-----+------------+----------------+
|
|
217
|
-
* Bits | 23-31 | 0-22
|
|
218
|
-
*
|
|
219
|
-
* Next, there are some adjustments to the exponent:
|
|
220
|
-
* - The exponent needs to be corrected by the difference in exponent bias
|
|
221
|
-
* between single-precision and half-precision formats (0x7F - 0xF = 0x70)
|
|
222
|
-
* - Inf and NaN values in the inputs should become Inf and NaN values after
|
|
223
|
-
* conversion to the single-precision number. Therefore, if the biased
|
|
224
|
-
* exponent of the half-precision input was 0x1F (max possible value), the
|
|
225
|
-
* biased exponent of the single-precision output must be 0xFF (max possible
|
|
226
|
-
* value). We do this correction in two steps:
|
|
227
|
-
* - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset
|
|
228
|
-
* below) rather than by 0x70 suggested by the difference in the exponent bias
|
|
229
|
-
* (see above).
|
|
230
|
-
* - Then we multiply the single-precision result of exponent adjustment by
|
|
231
|
-
* 2**(-112) to reverse the effect of exponent adjustment by 0xE0 less the
|
|
232
|
-
* necessary exponent adjustment by 0x70 due to difference in exponent bias.
|
|
233
|
-
* The floating-point multiplication hardware would ensure than Inf and
|
|
234
|
-
* NaN would retain their value on at least partially IEEE754-compliant
|
|
235
|
-
* implementations.
|
|
236
|
-
*
|
|
237
|
-
* Note that the above operations do not handle denormal inputs (where biased
|
|
238
|
-
* exponent == 0). However, they also do not operate on denormal inputs, and
|
|
239
|
-
* do not produce denormal results.
|
|
240
|
-
*/
|
|
241
|
-
constexpr uint32_t exp_offset = UINT32_C(0xE0) << 23;
|
|
242
|
-
// const float exp_scale = 0x1.0p-112f;
|
|
243
|
-
constexpr uint32_t scale_bits = (uint32_t)15 << 23;
|
|
244
|
-
float exp_scale_val = 0;
|
|
245
|
-
#if defined(_MSC_VER) && defined(__clang__)
|
|
246
|
-
__builtin_memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
|
|
247
|
-
#else
|
|
248
|
-
std::memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
|
|
249
|
-
#endif
|
|
250
|
-
|
|
251
|
-
const float exp_scale = exp_scale_val;
|
|
252
|
-
const float normalized_value =
|
|
253
|
-
fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
|
|
254
|
-
|
|
255
|
-
/*
|
|
256
|
-
* Convert denormalized half-precision inputs into single-precision results
|
|
257
|
-
* (always normalized). Zero inputs are also handled here.
|
|
258
|
-
*
|
|
259
|
-
* In a denormalized number the biased exponent is zero, and mantissa has
|
|
260
|
-
* on-zero bits. First, we shift mantissa into bits 0-9 of the 32-bit word.
|
|
261
|
-
*
|
|
262
|
-
* zeros | mantissa
|
|
263
|
-
* +---------------------------+------------+
|
|
264
|
-
* |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
|
|
265
|
-
* +---------------------------+------------+
|
|
266
|
-
* Bits 10-31 0-9
|
|
267
|
-
*
|
|
268
|
-
* Now, remember that denormalized half-precision numbers are represented as:
|
|
269
|
-
* FP16 = mantissa * 2**(-24).
|
|
270
|
-
* The trick is to construct a normalized single-precision number with the
|
|
271
|
-
* same mantissa and thehalf-precision input and with an exponent which would
|
|
272
|
-
* scale the corresponding mantissa bits to 2**(-24). A normalized
|
|
273
|
-
* single-precision floating-point number is represented as: FP32 = (1 +
|
|
274
|
-
* mantissa * 2**(-23)) * 2**(exponent - 127) Therefore, when the biased
|
|
275
|
-
* exponent is 126, a unit change in the mantissa of the input denormalized
|
|
276
|
-
* half-precision number causes a change of the constructed single-precision
|
|
277
|
-
* number by 2**(-24), i.e. the same amount.
|
|
278
|
-
*
|
|
279
|
-
* The last step is to adjust the bias of the constructed single-precision
|
|
280
|
-
* number. When the input half-precision number is zero, the constructed
|
|
281
|
-
* single-precision number has the value of FP32 = 1 * 2**(126 - 127) =
|
|
282
|
-
* 2**(-1) = 0.5 Therefore, we need to subtract 0.5 from the constructed
|
|
283
|
-
* single-precision number to get the numerical equivalent of the input
|
|
284
|
-
* half-precision number.
|
|
285
|
-
*/
|
|
286
|
-
constexpr uint32_t magic_mask = UINT32_C(126) << 23;
|
|
287
|
-
constexpr float magic_bias = 0.5f;
|
|
288
|
-
const float denormalized_value =
|
|
289
|
-
fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
|
|
290
|
-
|
|
291
|
-
/*
|
|
292
|
-
* - Choose either results of conversion of input as a normalized number, or
|
|
293
|
-
* as a denormalized number, depending on the input exponent. The variable
|
|
294
|
-
* two_w contains input exponent in bits 27-31, therefore if its smaller than
|
|
295
|
-
* 2**27, the input is either a denormal number, or zero.
|
|
296
|
-
* - Combine the result of conversion of exponent and mantissa with the sign
|
|
297
|
-
* of the input number.
|
|
298
|
-
*/
|
|
299
|
-
constexpr uint32_t denormalized_cutoff = UINT32_C(1) << 27;
|
|
300
|
-
const uint32_t result =
|
|
301
|
-
sign | (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value)
|
|
302
|
-
: fp32_to_bits(normalized_value));
|
|
303
|
-
return fp32_from_bits(result);
|
|
304
|
-
#endif // C10_X86_F16
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/*
|
|
308
|
-
* Convert a 32-bit floating-point number in IEEE single-precision format to a
|
|
309
|
-
* 16-bit floating-point number in IEEE half-precision format, in bit
|
|
310
|
-
* representation.
|
|
311
|
-
*
|
|
312
|
-
* @note The implementation relies on IEEE-like (no assumption about rounding
|
|
313
|
-
* mode and no operations on denormals) floating-point operations and bitcasts
|
|
314
|
-
* between integer and floating-point variables.
|
|
315
|
-
*/
|
|
316
|
-
inline uint16_t fp16_ieee_from_fp32_value(float f) {
|
|
317
|
-
#ifdef C10_X86_F16
|
|
318
|
-
return _cvtss_sh(f, _MM_FROUND_TO_NEAREST_INT);
|
|
319
|
-
#else
|
|
320
|
-
// const float scale_to_inf = 0x1.0p+112f;
|
|
321
|
-
// const float scale_to_zero = 0x1.0p-110f;
|
|
322
|
-
constexpr uint32_t scale_to_inf_bits = (uint32_t)239 << 23;
|
|
323
|
-
constexpr uint32_t scale_to_zero_bits = (uint32_t)17 << 23;
|
|
324
|
-
float scale_to_inf_val = 0, scale_to_zero_val = 0;
|
|
325
|
-
std::memcpy(&scale_to_inf_val, &scale_to_inf_bits, sizeof(scale_to_inf_val));
|
|
326
|
-
std::memcpy(&scale_to_zero_val, &scale_to_zero_bits,
|
|
327
|
-
sizeof(scale_to_zero_val));
|
|
328
|
-
const float scale_to_inf = scale_to_inf_val;
|
|
329
|
-
const float scale_to_zero = scale_to_zero_val;
|
|
330
|
-
|
|
331
|
-
#if defined(_MSC_VER) && _MSC_VER == 1916
|
|
332
|
-
float base = ((signbit(f) != 0 ? -f : f) * scale_to_inf) * scale_to_zero;
|
|
333
|
-
#else
|
|
334
|
-
float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
|
|
335
|
-
#endif
|
|
336
|
-
|
|
337
|
-
const uint32_t w = fp32_to_bits(f);
|
|
338
|
-
const uint32_t shl1_w = w + w;
|
|
339
|
-
const uint32_t sign = w & UINT32_C(0x80000000);
|
|
340
|
-
uint32_t bias = shl1_w & UINT32_C(0xFF000000);
|
|
341
|
-
if (bias < UINT32_C(0x71000000)) {
|
|
342
|
-
bias = UINT32_C(0x71000000);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
|
|
346
|
-
const uint32_t bits = fp32_to_bits(base);
|
|
347
|
-
const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
|
|
348
|
-
const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
|
|
349
|
-
const uint32_t nonsign = exp_bits + mantissa_bits;
|
|
350
|
-
return static_cast<uint16_t>(
|
|
351
|
-
(sign >> 16) |
|
|
352
|
-
(shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign));
|
|
353
|
-
#endif // C10_X86_F16
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
#ifdef C10_X86_F16
|
|
357
|
-
#undef C10_X86_F16
|
|
358
|
-
#endif // C10_X86_F16
|
|
359
|
-
|
|
360
|
-
#if defined(__aarch64__) && !defined(__CUDACC__)
|
|
361
|
-
inline float16_t fp16_from_bits(uint16_t h) {
|
|
362
|
-
return c10::bit_cast<float16_t>(h);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
inline uint16_t fp16_to_bits(float16_t f) { return c10::bit_cast<uint16_t>(f); }
|
|
366
|
-
|
|
367
|
-
// According to https://godbolt.org/z/frExdbsWG it would translate to single
|
|
368
|
-
// fcvt s0, h0
|
|
369
|
-
inline float native_fp16_to_fp32_value(uint16_t h) {
|
|
370
|
-
return static_cast<float>(fp16_from_bits(h));
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
inline uint16_t native_fp16_from_fp32_value(float f) {
|
|
374
|
-
return fp16_to_bits(static_cast<float16_t>(f));
|
|
375
|
-
}
|
|
376
|
-
#endif
|
|
377
|
-
|
|
378
|
-
} // namespace detail
|
|
379
|
-
|
|
380
|
-
struct alignas(2) Half {
|
|
381
|
-
unsigned short x;
|
|
382
|
-
|
|
383
|
-
struct from_bits_t {};
|
|
384
|
-
C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
|
|
385
|
-
return from_bits_t();
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// HIP wants __host__ __device__ tag, CUDA does not
|
|
389
|
-
#if defined(USE_ROCM)
|
|
390
|
-
C10_HOST_DEVICE Half() = default;
|
|
391
|
-
#else
|
|
392
|
-
Half() = default;
|
|
393
|
-
#endif
|
|
394
|
-
|
|
395
|
-
constexpr C10_HOST_DEVICE Half(unsigned short bits, from_bits_t) : x(bits) {}
|
|
396
|
-
#if defined(__aarch64__) && !defined(__CUDACC__)
|
|
397
|
-
inline Half(float16_t value);
|
|
398
|
-
inline operator float16_t() const;
|
|
399
|
-
#else
|
|
400
|
-
inline C10_HOST_DEVICE Half(float value);
|
|
401
|
-
inline C10_HOST_DEVICE operator float() const;
|
|
3
|
+
// need to keep the following for BC because the APIs in here were exposed
|
|
4
|
+
// before migrating Half to torch/headeronly
|
|
5
|
+
#if (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
|
|
6
|
+
!defined(__APPLE__)
|
|
7
|
+
#include <ATen/cpu/vec/vec_half.h>
|
|
402
8
|
#endif
|
|
403
|
-
|
|
404
|
-
#if defined(__CUDACC__) || defined(__HIPCC__)
|
|
405
|
-
inline C10_HOST_DEVICE Half(const __half &value);
|
|
406
|
-
inline C10_HOST_DEVICE operator __half() const;
|
|
407
|
-
#endif
|
|
408
|
-
#ifdef SYCL_LANGUAGE_VERSION
|
|
409
|
-
inline C10_HOST_DEVICE Half(const sycl::half &value);
|
|
410
|
-
inline C10_HOST_DEVICE operator sycl::half() const;
|
|
411
|
-
#endif
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
C10_API inline std::ostream &operator<<(std::ostream &out, const Half &value) {
|
|
415
|
-
out << (float)value;
|
|
416
|
-
return out;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
} // namespace c10
|
|
420
|
-
|
|
421
|
-
#include <c10/util/Half-inl.h> // IWYU pragma: keep
|
package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/TypeSafeSignMath.h
CHANGED
|
@@ -1,133 +1 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
#include <c10/macros/Macros.h>
|
|
4
|
-
#include <limits>
|
|
5
|
-
#include <type_traits>
|
|
6
|
-
|
|
7
|
-
C10_CLANG_DIAGNOSTIC_PUSH()
|
|
8
|
-
#if C10_CLANG_HAS_WARNING("-Wstring-conversion")
|
|
9
|
-
C10_CLANG_DIAGNOSTIC_IGNORE("-Wstring-conversion")
|
|
10
|
-
#endif
|
|
11
|
-
#if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
|
|
12
|
-
C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
|
|
13
|
-
#endif
|
|
14
|
-
|
|
15
|
-
namespace c10 {
|
|
16
|
-
|
|
17
|
-
/// Returns false since we cannot have x < 0 if x is unsigned.
|
|
18
|
-
template <typename T>
|
|
19
|
-
inline constexpr bool is_negative(const T & /*x*/,
|
|
20
|
-
std::true_type /*is_unsigned*/) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/// Returns true if a signed variable x < 0
|
|
25
|
-
template <typename T>
|
|
26
|
-
inline constexpr bool is_negative(const T &x, std::false_type /*is_unsigned*/) {
|
|
27
|
-
return x < T(0);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/// Returns true if x < 0
|
|
31
|
-
/// NOTE: Will fail on an unsigned custom type
|
|
32
|
-
/// For the most part it's possible to fix this if
|
|
33
|
-
/// the custom type has a constexpr constructor.
|
|
34
|
-
/// However, notably, c10::Half does not :-(
|
|
35
|
-
template <typename T> inline constexpr bool is_negative(const T &x) {
|
|
36
|
-
return is_negative(x, std::is_unsigned<T>());
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/// Returns the sign of an unsigned variable x as 0, 1
|
|
40
|
-
template <typename T>
|
|
41
|
-
inline constexpr int signum(const T &x, std::true_type /*is_unsigned*/) {
|
|
42
|
-
return T(0) < x;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/// Returns the sign of a signed variable x as -1, 0, 1
|
|
46
|
-
template <typename T>
|
|
47
|
-
inline constexpr int signum(const T &x, std::false_type /*is_unsigned*/) {
|
|
48
|
-
return (T(0) < x) - (x < T(0));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/// Returns the sign of x as -1, 0, 1
|
|
52
|
-
/// NOTE: Will fail on an unsigned custom type
|
|
53
|
-
/// For the most part it's possible to fix this if
|
|
54
|
-
/// the custom type has a constexpr constructor.
|
|
55
|
-
/// However, notably, c10::Half does not :-(
|
|
56
|
-
template <typename T> inline constexpr int signum(const T &x) {
|
|
57
|
-
return signum(x, std::is_unsigned<T>());
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/// Returns true if a and b are not both negative
|
|
61
|
-
template <typename T, typename U>
|
|
62
|
-
inline constexpr bool signs_differ(const T &a, const U &b) {
|
|
63
|
-
return is_negative(a) != is_negative(b);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Suppress sign compare warning when compiling with GCC
|
|
67
|
-
// as later does not account for short-circuit rule before
|
|
68
|
-
// raising the warning, see https://godbolt.org/z/Tr3Msnz99
|
|
69
|
-
#ifdef __GNUC__
|
|
70
|
-
#pragma GCC diagnostic push
|
|
71
|
-
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
72
|
-
#endif
|
|
73
|
-
|
|
74
|
-
/// Returns true if x is greater than the greatest value of the type Limit
|
|
75
|
-
template <typename Limit, typename T>
|
|
76
|
-
inline constexpr bool greater_than_max(const T &x) {
|
|
77
|
-
constexpr bool can_overflow =
|
|
78
|
-
std::numeric_limits<T>::digits > std::numeric_limits<Limit>::digits;
|
|
79
|
-
return can_overflow && x > std::numeric_limits<Limit>::max();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
#ifdef __GNUC__
|
|
83
|
-
#pragma GCC diagnostic pop
|
|
84
|
-
#endif
|
|
85
|
-
|
|
86
|
-
/// Returns true if x < lowest(Limit). Standard comparison
|
|
87
|
-
template <typename Limit, typename T>
|
|
88
|
-
inline constexpr bool less_than_lowest(const T &x,
|
|
89
|
-
std::false_type /*limit_is_unsigned*/,
|
|
90
|
-
std::false_type /*x_is_unsigned*/) {
|
|
91
|
-
return x < std::numeric_limits<Limit>::lowest();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/// Returns false since all the limit is signed and therefore includes
|
|
95
|
-
/// negative values but x cannot be negative because it is unsigned
|
|
96
|
-
template <typename Limit, typename T>
|
|
97
|
-
inline constexpr bool less_than_lowest(const T & /*x*/,
|
|
98
|
-
std::false_type /*limit_is_unsigned*/,
|
|
99
|
-
std::true_type /*x_is_unsigned*/) {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/// Returns true if x < 0, where 0 is constructed from T.
|
|
104
|
-
/// Limit is not signed, so its lower value is zero
|
|
105
|
-
template <typename Limit, typename T>
|
|
106
|
-
inline constexpr bool less_than_lowest(const T &x,
|
|
107
|
-
std::true_type /*limit_is_unsigned*/,
|
|
108
|
-
std::false_type /*x_is_unsigned*/) {
|
|
109
|
-
return x < T(0);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/// Returns false sign both types are unsigned
|
|
113
|
-
template <typename Limit, typename T>
|
|
114
|
-
inline constexpr bool less_than_lowest(const T & /*x*/,
|
|
115
|
-
std::true_type /*limit_is_unsigned*/,
|
|
116
|
-
std::true_type /*x_is_unsigned*/) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/// Returns true if x is less than the lowest value of type T
|
|
121
|
-
/// NOTE: Will fail on an unsigned custom type
|
|
122
|
-
/// For the most part it's possible to fix this if
|
|
123
|
-
/// the custom type has a constexpr constructor.
|
|
124
|
-
/// However, notably, c10::Half does not :
|
|
125
|
-
template <typename Limit, typename T>
|
|
126
|
-
inline constexpr bool less_than_lowest(const T &x) {
|
|
127
|
-
return less_than_lowest<Limit>(x, std::is_unsigned<Limit>(),
|
|
128
|
-
std::is_unsigned<T>());
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
} // namespace c10
|
|
132
|
-
|
|
133
|
-
C10_CLANG_DIAGNOSTIC_POP()
|
|
1
|
+
#include <torch/headeronly/util/TypeSafeSignMath.h>
|
|
@@ -1,43 +1 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
#include <cstring>
|
|
4
|
-
#include <type_traits>
|
|
5
|
-
|
|
6
|
-
#if __has_include(<bit>) && (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L)
|
|
7
|
-
#include <bit>
|
|
8
|
-
#define C10_HAVE_STD_BIT_CAST 1
|
|
9
|
-
#else
|
|
10
|
-
#define C10_HAVE_STD_BIT_CAST 0
|
|
11
|
-
#endif // __has_include(<bit>) && (__cplusplus >= 202002L ||
|
|
12
|
-
// (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L))
|
|
13
|
-
|
|
14
|
-
namespace c10 {
|
|
15
|
-
|
|
16
|
-
#if C10_HAVE_STD_BIT_CAST
|
|
17
|
-
using std::bit_cast;
|
|
18
|
-
#else
|
|
19
|
-
// Implementations of std::bit_cast() from C++ 20.
|
|
20
|
-
//
|
|
21
|
-
// This is a less sketchy version of reinterpret_cast.
|
|
22
|
-
//
|
|
23
|
-
// See https://en.cppreference.com/w/cpp/numeric/bit_cast for more
|
|
24
|
-
// information as well as the source of our implementations.
|
|
25
|
-
template <class To, class From>
|
|
26
|
-
std::enable_if_t<sizeof(To) == sizeof(From) &&
|
|
27
|
-
std::is_trivially_copyable_v<From> &&
|
|
28
|
-
std::is_trivially_copyable_v<To>,
|
|
29
|
-
To>
|
|
30
|
-
// constexpr support needs compiler magic
|
|
31
|
-
bit_cast(const From &src) noexcept {
|
|
32
|
-
static_assert(std::is_trivially_constructible_v<To>,
|
|
33
|
-
"This implementation additionally requires "
|
|
34
|
-
"destination type to be trivially constructible");
|
|
35
|
-
|
|
36
|
-
To dst;
|
|
37
|
-
std::memcpy(&dst, &src, sizeof(To));
|
|
38
|
-
return dst;
|
|
39
|
-
}
|
|
40
|
-
#endif // C10_HAVE_STD_BIT_CAST
|
|
41
|
-
#undef C10_HAVE_STD_BIT_CAST
|
|
42
|
-
|
|
43
|
-
} // namespace c10
|
|
1
|
+
#include <torch/headeronly/util/bit_cast.h>
|