react-native-executorch 0.5.6 → 0.5.7

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 (139) hide show
  1. package/android/libs/classes.jar +0 -0
  2. package/android/src/main/cpp/CMakeLists.txt +23 -14
  3. package/common/rnexecutorch/RnExecutorchInstaller.cpp +4 -21
  4. package/common/rnexecutorch/host_objects/ModelHostObject.h +67 -51
  5. package/common/rnexecutorch/models/llm/LLM.cpp +24 -1
  6. package/common/rnexecutorch/models/llm/LLM.h +4 -1
  7. package/common/rnexecutorch/threads/GlobalThreadPool.h +79 -0
  8. package/common/rnexecutorch/threads/HighPerformanceThreadPool.h +364 -0
  9. package/common/rnexecutorch/threads/utils/ThreadUtils.h +29 -0
  10. package/common/runner/runner.cpp +9 -3
  11. package/common/runner/runner.h +4 -3
  12. package/common/runner/text_token_generator.h +28 -10
  13. package/lib/module/controllers/LLMController.js +21 -2
  14. package/lib/module/controllers/LLMController.js.map +1 -1
  15. package/lib/module/hooks/natural_language_processing/useLLM.js +6 -2
  16. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  17. package/lib/module/modules/natural_language_processing/LLMModule.js +4 -2
  18. package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
  19. package/lib/module/types/llm.js.map +1 -1
  20. package/lib/typescript/controllers/LLMController.d.ts +4 -2
  21. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  22. package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
  23. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +3 -2
  24. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
  25. package/lib/typescript/types/llm.d.ts +7 -1
  26. package/lib/typescript/types/llm.d.ts.map +1 -1
  27. package/package.json +3 -1
  28. package/react-native-executorch.podspec +12 -31
  29. package/src/controllers/LLMController.ts +29 -5
  30. package/src/hooks/natural_language_processing/useLLM.ts +15 -1
  31. package/src/modules/natural_language_processing/LLMModule.ts +10 -2
  32. package/src/types/llm.ts +8 -0
  33. package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
  34. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  35. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  36. package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
  37. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libsentencepiece.a +0 -0
  38. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_c.a +0 -0
  39. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_cpp.a +0 -0
  40. package/third-party/android/libs/tokenizers-cpp/x86_64/libsentencepiece.a +0 -0
  41. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_c.a +0 -0
  42. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_cpp.a +0 -0
  43. package/third-party/include/c10/macros/Export.h +2 -86
  44. package/third-party/include/c10/macros/Macros.h +28 -5
  45. package/third-party/include/c10/util/BFloat16-inl.h +1 -4
  46. package/third-party/include/c10/util/BFloat16.h +5 -8
  47. package/third-party/include/c10/util/Half.h +5 -0
  48. package/third-party/include/c10/util/bit_cast.h +1 -1
  49. package/third-party/include/c10/util/complex.h +639 -0
  50. package/third-party/include/c10/util/complex_math.h +399 -0
  51. package/third-party/include/c10/util/complex_utils.h +41 -0
  52. package/third-party/include/c10/util/irange.h +2 -2
  53. package/third-party/include/c10/util/overflows.h +95 -0
  54. package/third-party/include/executorch/ExecuTorchError.h +75 -0
  55. package/third-party/include/executorch/ExecuTorchModule.h +115 -11
  56. package/third-party/include/executorch/ExecuTorchTensor.h +731 -51
  57. package/third-party/include/executorch/ExecuTorchValue.h +61 -9
  58. package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +181 -0
  59. package/third-party/include/executorch/extension/kernel_util/meta_programming.h +108 -0
  60. package/third-party/include/executorch/extension/kernel_util/type_list.h +137 -0
  61. package/third-party/include/executorch/extension/module/bundled_module.h +131 -0
  62. package/third-party/include/executorch/extension/module/module.h +46 -20
  63. package/third-party/include/executorch/extension/threadpool/cpuinfo_utils.h +1 -3
  64. package/third-party/include/executorch/extension/threadpool/threadpool.h +1 -3
  65. package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +35 -0
  66. package/third-party/include/executorch/runtime/backend/backend_execution_context.h +3 -3
  67. package/third-party/include/executorch/runtime/backend/backend_init_context.h +12 -6
  68. package/third-party/include/executorch/runtime/backend/backend_option_context.h +34 -0
  69. package/third-party/include/executorch/runtime/backend/interface.h +70 -9
  70. package/third-party/include/executorch/runtime/backend/options.h +206 -0
  71. package/third-party/include/executorch/runtime/core/evalue.h +19 -25
  72. package/third-party/include/executorch/runtime/core/event_tracer.h +32 -17
  73. package/third-party/include/executorch/runtime/core/event_tracer_hooks.h +23 -14
  74. package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +32 -9
  75. package/third-party/include/executorch/runtime/core/exec_aten/util/dim_order_util.h +3 -2
  76. package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +43 -75
  77. package/third-party/include/executorch/runtime/core/exec_aten/util/tensor_util.h +88 -87
  78. package/third-party/include/executorch/runtime/core/function_ref.h +100 -0
  79. package/third-party/include/executorch/runtime/core/named_data_map.h +14 -14
  80. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +2 -86
  81. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +28 -5
  82. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -4
  83. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +5 -8
  84. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +5 -0
  85. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -1
  86. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +639 -0
  87. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_math.h +399 -0
  88. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_utils.h +41 -0
  89. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +2 -2
  90. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/overflows.h +95 -0
  91. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +88 -0
  92. package/third-party/include/executorch/runtime/core/portable_type/complex.h +6 -29
  93. package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +20 -0
  94. package/third-party/include/executorch/runtime/core/span.h +4 -0
  95. package/third-party/include/executorch/runtime/core/tag.h +19 -0
  96. package/third-party/include/executorch/runtime/core/tensor_layout.h +2 -2
  97. package/third-party/include/executorch/runtime/executor/method.h +15 -3
  98. package/third-party/include/executorch/runtime/executor/method_meta.h +34 -5
  99. package/third-party/include/executorch/runtime/executor/program.h +3 -4
  100. package/third-party/include/executorch/runtime/executor/pte_data_map.h +9 -8
  101. package/third-party/include/executorch/runtime/executor/tensor_parser.h +14 -13
  102. package/third-party/include/executorch/runtime/kernel/kernel_runtime_context.h +5 -5
  103. package/third-party/include/executorch/runtime/kernel/operator_registry.h +21 -19
  104. package/third-party/include/executorch/runtime/platform/compiler.h +8 -0
  105. package/third-party/include/executorch/runtime/platform/platform.h +126 -0
  106. package/third-party/include/headeronly/macros/Export.h +88 -0
  107. package/third-party/include/tokenizers-cpp/tokenizers_c.h +61 -0
  108. package/third-party/include/torch/headeronly/macros/Export.h +88 -0
  109. package/third-party/ios/ExecutorchLib.xcframework/Info.plist +43 -0
  110. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  111. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  112. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  113. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  114. package/third-party/ios/libs/cpuinfo/libcpuinfo.a +0 -0
  115. package/third-party/ios/libs/pthreadpool/physical-arm64-release/libpthreadpool.a +0 -0
  116. package/third-party/ios/libs/pthreadpool/simulator-arm64-debug/libpthreadpool.a +0 -0
  117. package/ios/libs/executorch/libbackend_coreml_ios.a +0 -0
  118. package/ios/libs/executorch/libbackend_coreml_simulator.a +0 -0
  119. package/ios/libs/executorch/libbackend_mps_ios.a +0 -0
  120. package/ios/libs/executorch/libbackend_mps_simulator.a +0 -0
  121. package/ios/libs/executorch/libbackend_xnnpack_ios.a +0 -0
  122. package/ios/libs/executorch/libbackend_xnnpack_simulator.a +0 -0
  123. package/ios/libs/executorch/libexecutorch_ios.a +0 -0
  124. package/ios/libs/executorch/libexecutorch_simulator.a +0 -0
  125. package/ios/libs/executorch/libkernels_custom_ios.a +0 -0
  126. package/ios/libs/executorch/libkernels_custom_simulator.a +0 -0
  127. package/ios/libs/executorch/libkernels_optimized_ios.a +0 -0
  128. package/ios/libs/executorch/libkernels_optimized_simulator.a +0 -0
  129. package/ios/libs/executorch/libkernels_portable_ios.a +0 -0
  130. package/ios/libs/executorch/libkernels_portable_simulator.a +0 -0
  131. package/ios/libs/executorch/libkernels_quantized_ios.a +0 -0
  132. package/ios/libs/executorch/libkernels_quantized_simulator.a +0 -0
  133. package/third-party/ios/ios.toolchain.cmake +0 -1122
  134. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libsentencepiece.a +0 -0
  135. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_c.a +0 -0
  136. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_cpp.a +0 -0
  137. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libsentencepiece.a +0 -0
  138. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_c.a +0 -0
  139. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_cpp.a +0 -0
@@ -0,0 +1,399 @@
1
+ #if !defined(C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H)
2
+ #error \
3
+ "c10/util/complex_math.h is not meant to be individually included. Include c10/util/complex.h instead."
4
+ #endif
5
+
6
+ namespace c10_complex_math {
7
+
8
+ // Exponential functions
9
+
10
+ template <typename T>
11
+ C10_HOST_DEVICE inline c10::complex<T> exp(const c10::complex<T> &x) {
12
+ #if defined(__CUDACC__) || defined(__HIPCC__)
13
+ return static_cast<c10::complex<T>>(
14
+ thrust::exp(static_cast<thrust::complex<T>>(x)));
15
+ #else
16
+ return static_cast<c10::complex<T>>(
17
+ std::exp(static_cast<std::complex<T>>(x)));
18
+ #endif
19
+ }
20
+
21
+ template <typename T>
22
+ C10_HOST_DEVICE inline c10::complex<T> log(const c10::complex<T> &x) {
23
+ #if defined(__CUDACC__) || defined(__HIPCC__)
24
+ return static_cast<c10::complex<T>>(
25
+ thrust::log(static_cast<thrust::complex<T>>(x)));
26
+ #else
27
+ return static_cast<c10::complex<T>>(
28
+ std::log(static_cast<std::complex<T>>(x)));
29
+ #endif
30
+ }
31
+
32
+ template <typename T>
33
+ C10_HOST_DEVICE inline c10::complex<T> log10(const c10::complex<T> &x) {
34
+ #if defined(__CUDACC__) || defined(__HIPCC__)
35
+ return static_cast<c10::complex<T>>(
36
+ thrust::log10(static_cast<thrust::complex<T>>(x)));
37
+ #else
38
+ return static_cast<c10::complex<T>>(
39
+ std::log10(static_cast<std::complex<T>>(x)));
40
+ #endif
41
+ }
42
+
43
+ template <typename T>
44
+ C10_HOST_DEVICE inline c10::complex<T> log2(const c10::complex<T> &x) {
45
+ const c10::complex<T> log2 = c10::complex<T>(::log(2.0), 0.0);
46
+ return c10_complex_math::log(x) / log2;
47
+ }
48
+
49
+ // Power functions
50
+ //
51
+ #if defined(_LIBCPP_VERSION) || \
52
+ (defined(__GLIBCXX__) && !defined(_GLIBCXX11_USE_C99_COMPLEX))
53
+ namespace _detail {
54
+ C10_API c10::complex<float> sqrt(const c10::complex<float> &in);
55
+ C10_API c10::complex<double> sqrt(const c10::complex<double> &in);
56
+ C10_API c10::complex<float> acos(const c10::complex<float> &in);
57
+ C10_API c10::complex<double> acos(const c10::complex<double> &in);
58
+ } // namespace _detail
59
+ #endif
60
+
61
+ template <typename T>
62
+ C10_HOST_DEVICE inline c10::complex<T> sqrt(const c10::complex<T> &x) {
63
+ #if defined(__CUDACC__) || defined(__HIPCC__)
64
+ return static_cast<c10::complex<T>>(
65
+ thrust::sqrt(static_cast<thrust::complex<T>>(x)));
66
+ #elif !(defined(_LIBCPP_VERSION) || \
67
+ (defined(__GLIBCXX__) && !defined(_GLIBCXX11_USE_C99_COMPLEX)))
68
+ return static_cast<c10::complex<T>>(
69
+ std::sqrt(static_cast<std::complex<T>>(x)));
70
+ #else
71
+ return _detail::sqrt(x);
72
+ #endif
73
+ }
74
+
75
+ template <typename T>
76
+ C10_HOST_DEVICE inline c10::complex<T> pow(const c10::complex<T> &x,
77
+ const c10::complex<T> &y) {
78
+ #if defined(__CUDACC__) || defined(__HIPCC__)
79
+ return static_cast<c10::complex<T>>(thrust::pow(
80
+ static_cast<thrust::complex<T>>(x), static_cast<thrust::complex<T>>(y)));
81
+ #else
82
+ return static_cast<c10::complex<T>>(std::pow(
83
+ static_cast<std::complex<T>>(x), static_cast<std::complex<T>>(y)));
84
+ #endif
85
+ }
86
+
87
+ template <typename T>
88
+ C10_HOST_DEVICE inline c10::complex<T> pow(const c10::complex<T> &x,
89
+ const T &y) {
90
+ #if defined(__CUDACC__) || defined(__HIPCC__)
91
+ return static_cast<c10::complex<T>>(
92
+ thrust::pow(static_cast<thrust::complex<T>>(x), y));
93
+ #else
94
+ return static_cast<c10::complex<T>>(
95
+ std::pow(static_cast<std::complex<T>>(x), y));
96
+ #endif
97
+ }
98
+
99
+ template <typename T>
100
+ C10_HOST_DEVICE inline c10::complex<T> pow(const T &x,
101
+ const c10::complex<T> &y) {
102
+ #if defined(__CUDACC__) || defined(__HIPCC__)
103
+ return static_cast<c10::complex<T>>(
104
+ thrust::pow(x, static_cast<thrust::complex<T>>(y)));
105
+ #else
106
+ return static_cast<c10::complex<T>>(
107
+ std::pow(x, static_cast<std::complex<T>>(y)));
108
+ #endif
109
+ }
110
+
111
+ template <typename T, typename U>
112
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())>
113
+ pow(const c10::complex<T> &x, const c10::complex<U> &y) {
114
+ #if defined(__CUDACC__) || defined(__HIPCC__)
115
+ return static_cast<c10::complex<T>>(thrust::pow(
116
+ static_cast<thrust::complex<T>>(x), static_cast<thrust::complex<T>>(y)));
117
+ #else
118
+ return static_cast<c10::complex<T>>(std::pow(
119
+ static_cast<std::complex<T>>(x), static_cast<std::complex<T>>(y)));
120
+ #endif
121
+ }
122
+
123
+ template <typename T, typename U>
124
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())>
125
+ pow(const c10::complex<T> &x, const U &y) {
126
+ #if defined(__CUDACC__) || defined(__HIPCC__)
127
+ return static_cast<c10::complex<T>>(
128
+ thrust::pow(static_cast<thrust::complex<T>>(x), y));
129
+ #else
130
+ return static_cast<c10::complex<T>>(
131
+ std::pow(static_cast<std::complex<T>>(x), y));
132
+ #endif
133
+ }
134
+
135
+ template <typename T, typename U>
136
+ C10_HOST_DEVICE inline c10::complex<decltype(T() * U())>
137
+ pow(const T &x, const c10::complex<U> &y) {
138
+ #if defined(__CUDACC__) || defined(__HIPCC__)
139
+ return static_cast<c10::complex<T>>(
140
+ thrust::pow(x, static_cast<thrust::complex<T>>(y)));
141
+ #else
142
+ return static_cast<c10::complex<T>>(
143
+ std::pow(x, static_cast<std::complex<T>>(y)));
144
+ #endif
145
+ }
146
+
147
+ // Trigonometric functions
148
+
149
+ template <typename T>
150
+ C10_HOST_DEVICE inline c10::complex<T> sin(const c10::complex<T> &x) {
151
+ #if defined(__CUDACC__) || defined(__HIPCC__)
152
+ return static_cast<c10::complex<T>>(
153
+ thrust::sin(static_cast<thrust::complex<T>>(x)));
154
+ #else
155
+ return static_cast<c10::complex<T>>(
156
+ std::sin(static_cast<std::complex<T>>(x)));
157
+ #endif
158
+ }
159
+
160
+ template <typename T>
161
+ C10_HOST_DEVICE inline c10::complex<T> cos(const c10::complex<T> &x) {
162
+ #if defined(__CUDACC__) || defined(__HIPCC__)
163
+ return static_cast<c10::complex<T>>(
164
+ thrust::cos(static_cast<thrust::complex<T>>(x)));
165
+ #else
166
+ return static_cast<c10::complex<T>>(
167
+ std::cos(static_cast<std::complex<T>>(x)));
168
+ #endif
169
+ }
170
+
171
+ template <typename T>
172
+ C10_HOST_DEVICE inline c10::complex<T> tan(const c10::complex<T> &x) {
173
+ #if defined(__CUDACC__) || defined(__HIPCC__)
174
+ return static_cast<c10::complex<T>>(
175
+ thrust::tan(static_cast<thrust::complex<T>>(x)));
176
+ #else
177
+ return static_cast<c10::complex<T>>(
178
+ std::tan(static_cast<std::complex<T>>(x)));
179
+ #endif
180
+ }
181
+
182
+ template <typename T>
183
+ C10_HOST_DEVICE inline c10::complex<T> asin(const c10::complex<T> &x) {
184
+ #if defined(__CUDACC__) || defined(__HIPCC__)
185
+ return static_cast<c10::complex<T>>(
186
+ thrust::asin(static_cast<thrust::complex<T>>(x)));
187
+ #else
188
+ return static_cast<c10::complex<T>>(
189
+ std::asin(static_cast<std::complex<T>>(x)));
190
+ #endif
191
+ }
192
+
193
+ template <typename T>
194
+ C10_HOST_DEVICE inline c10::complex<T> acos(const c10::complex<T> &x) {
195
+ #if defined(__CUDACC__) || defined(__HIPCC__)
196
+ return static_cast<c10::complex<T>>(
197
+ thrust::acos(static_cast<thrust::complex<T>>(x)));
198
+ #elif !defined(_LIBCPP_VERSION)
199
+ return static_cast<c10::complex<T>>(
200
+ std::acos(static_cast<std::complex<T>>(x)));
201
+ #else
202
+ return _detail::acos(x);
203
+ #endif
204
+ }
205
+
206
+ template <typename T>
207
+ C10_HOST_DEVICE inline c10::complex<T> atan(const c10::complex<T> &x) {
208
+ #if defined(__CUDACC__) || defined(__HIPCC__)
209
+ return static_cast<c10::complex<T>>(
210
+ thrust::atan(static_cast<thrust::complex<T>>(x)));
211
+ #else
212
+ return static_cast<c10::complex<T>>(
213
+ std::atan(static_cast<std::complex<T>>(x)));
214
+ #endif
215
+ }
216
+
217
+ // Hyperbolic functions
218
+
219
+ template <typename T>
220
+ C10_HOST_DEVICE inline c10::complex<T> sinh(const c10::complex<T> &x) {
221
+ #if defined(__CUDACC__) || defined(__HIPCC__)
222
+ return static_cast<c10::complex<T>>(
223
+ thrust::sinh(static_cast<thrust::complex<T>>(x)));
224
+ #else
225
+ return static_cast<c10::complex<T>>(
226
+ std::sinh(static_cast<std::complex<T>>(x)));
227
+ #endif
228
+ }
229
+
230
+ template <typename T>
231
+ C10_HOST_DEVICE inline c10::complex<T> cosh(const c10::complex<T> &x) {
232
+ #if defined(__CUDACC__) || defined(__HIPCC__)
233
+ return static_cast<c10::complex<T>>(
234
+ thrust::cosh(static_cast<thrust::complex<T>>(x)));
235
+ #else
236
+ return static_cast<c10::complex<T>>(
237
+ std::cosh(static_cast<std::complex<T>>(x)));
238
+ #endif
239
+ }
240
+
241
+ template <typename T>
242
+ C10_HOST_DEVICE inline c10::complex<T> tanh(const c10::complex<T> &x) {
243
+ #if defined(__CUDACC__) || defined(__HIPCC__)
244
+ return static_cast<c10::complex<T>>(
245
+ thrust::tanh(static_cast<thrust::complex<T>>(x)));
246
+ #else
247
+ return static_cast<c10::complex<T>>(
248
+ std::tanh(static_cast<std::complex<T>>(x)));
249
+ #endif
250
+ }
251
+
252
+ template <typename T>
253
+ C10_HOST_DEVICE inline c10::complex<T> asinh(const c10::complex<T> &x) {
254
+ #if defined(__CUDACC__) || defined(__HIPCC__)
255
+ return static_cast<c10::complex<T>>(
256
+ thrust::asinh(static_cast<thrust::complex<T>>(x)));
257
+ #else
258
+ return static_cast<c10::complex<T>>(
259
+ std::asinh(static_cast<std::complex<T>>(x)));
260
+ #endif
261
+ }
262
+
263
+ template <typename T>
264
+ C10_HOST_DEVICE inline c10::complex<T> acosh(const c10::complex<T> &x) {
265
+ #if defined(__CUDACC__) || defined(__HIPCC__)
266
+ return static_cast<c10::complex<T>>(
267
+ thrust::acosh(static_cast<thrust::complex<T>>(x)));
268
+ #else
269
+ return static_cast<c10::complex<T>>(
270
+ std::acosh(static_cast<std::complex<T>>(x)));
271
+ #endif
272
+ }
273
+
274
+ template <typename T>
275
+ C10_HOST_DEVICE inline c10::complex<T> atanh(const c10::complex<T> &x) {
276
+ #if defined(__CUDACC__) || defined(__HIPCC__)
277
+ return static_cast<c10::complex<T>>(
278
+ thrust::atanh(static_cast<thrust::complex<T>>(x)));
279
+ #else
280
+ return static_cast<c10::complex<T>>(
281
+ std::atanh(static_cast<std::complex<T>>(x)));
282
+ #endif
283
+ }
284
+
285
+ template <typename T>
286
+ C10_HOST_DEVICE inline c10::complex<T> log1p(const c10::complex<T> &z) {
287
+ #if defined(__APPLE__) || defined(__MACOSX) || defined(__CUDACC__) || \
288
+ defined(__HIPCC__)
289
+ // For Mac, the new implementation yielded a high relative error. Falling back
290
+ // to the old version for now.
291
+ // See https://github.com/numpy/numpy/pull/22611#issuecomment-1667945354
292
+ // For CUDA we also use this one, as thrust::log(thrust::complex) takes
293
+ // *forever* to compile
294
+
295
+ // log1p(z) = log(1 + z)
296
+ // Let's define 1 + z = r * e ^ (i * a), then we have
297
+ // log(r * e ^ (i * a)) = log(r) + i * a
298
+ // With z = x + iy, the term r can be written as
299
+ // r = ((1 + x) ^ 2 + y ^ 2) ^ 0.5
300
+ // = (1 + x ^ 2 + 2 * x + y ^ 2) ^ 0.5
301
+ // So, log(r) is
302
+ // log(r) = 0.5 * log(1 + x ^ 2 + 2 * x + y ^ 2)
303
+ // = 0.5 * log1p(x * (x + 2) + y ^ 2)
304
+ // we need to use the expression only on certain condition to avoid overflow
305
+ // and underflow from `(x * (x + 2) + y ^ 2)`
306
+ T x = z.real();
307
+ T y = z.imag();
308
+ T zabs = std::abs(z);
309
+ T theta = std::atan2(y, x + T(1));
310
+ if (zabs < 0.5) {
311
+ T r = x * (T(2) + x) + y * y;
312
+ if (r == 0) { // handle underflow
313
+ return {x, theta};
314
+ }
315
+ return {T(0.5) * std::log1p(r), theta};
316
+ } else {
317
+ T z0 = std::hypot(x + 1, y);
318
+ return {std::log(z0), theta};
319
+ }
320
+ #else
321
+ // CPU path
322
+ // Based on https://github.com/numpy/numpy/pull/22611#issuecomment-1667945354
323
+ c10::complex<T> u = z + T(1);
324
+ if (u == T(1)) {
325
+ return z;
326
+ } else {
327
+ auto log_u = log(u);
328
+ if (u - T(1) == z) {
329
+ return log_u;
330
+ }
331
+ return log_u * (z / (u - T(1)));
332
+ }
333
+ #endif
334
+ }
335
+
336
+ template <typename T>
337
+ C10_HOST_DEVICE inline c10::complex<T> expm1(const c10::complex<T> &z) {
338
+ // expm1(z) = exp(z) - 1
339
+ // Define z = x + i * y
340
+ // f = e ^ (x + i * y) - 1
341
+ // = e ^ x * e ^ (i * y) - 1
342
+ // = (e ^ x * cos(y) - 1) + i * (e ^ x * sin(y))
343
+ // = (e ^ x - 1) * cos(y) - (1 - cos(y)) + i * e ^ x * sin(y)
344
+ // = expm1(x) * cos(y) - 2 * sin(y / 2) ^ 2 + i * e ^ x * sin(y)
345
+ T x = z.real();
346
+ T y = z.imag();
347
+ T a = std::sin(y / 2);
348
+ T er = std::expm1(x) * std::cos(y) - T(2) * a * a;
349
+ T ei = std::exp(x) * std::sin(y);
350
+ return {er, ei};
351
+ }
352
+
353
+ } // namespace c10_complex_math
354
+
355
+ using c10_complex_math::acos;
356
+ using c10_complex_math::acosh;
357
+ using c10_complex_math::asin;
358
+ using c10_complex_math::asinh;
359
+ using c10_complex_math::atan;
360
+ using c10_complex_math::atanh;
361
+ using c10_complex_math::cos;
362
+ using c10_complex_math::cosh;
363
+ using c10_complex_math::exp;
364
+ using c10_complex_math::expm1;
365
+ using c10_complex_math::log;
366
+ using c10_complex_math::log10;
367
+ using c10_complex_math::log1p;
368
+ using c10_complex_math::log2;
369
+ using c10_complex_math::pow;
370
+ using c10_complex_math::sin;
371
+ using c10_complex_math::sinh;
372
+ using c10_complex_math::sqrt;
373
+ using c10_complex_math::tan;
374
+ using c10_complex_math::tanh;
375
+
376
+ namespace std {
377
+
378
+ using c10_complex_math::acos;
379
+ using c10_complex_math::acosh;
380
+ using c10_complex_math::asin;
381
+ using c10_complex_math::asinh;
382
+ using c10_complex_math::atan;
383
+ using c10_complex_math::atanh;
384
+ using c10_complex_math::cos;
385
+ using c10_complex_math::cosh;
386
+ using c10_complex_math::exp;
387
+ using c10_complex_math::expm1;
388
+ using c10_complex_math::log;
389
+ using c10_complex_math::log10;
390
+ using c10_complex_math::log1p;
391
+ using c10_complex_math::log2;
392
+ using c10_complex_math::pow;
393
+ using c10_complex_math::sin;
394
+ using c10_complex_math::sinh;
395
+ using c10_complex_math::sqrt;
396
+ using c10_complex_math::tan;
397
+ using c10_complex_math::tanh;
398
+
399
+ } // namespace std
@@ -0,0 +1,41 @@
1
+ #if !defined(C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H)
2
+ #error \
3
+ "c10/util/complex_utils.h is not meant to be individually included. Include c10/util/complex.h instead."
4
+ #endif
5
+
6
+ #include <limits>
7
+
8
+ namespace c10 {
9
+
10
+ template <typename T> struct is_complex : public std::false_type {};
11
+
12
+ template <typename T>
13
+ struct is_complex<std::complex<T>> : public std::true_type {};
14
+
15
+ template <typename T>
16
+ struct is_complex<c10::complex<T>> : public std::true_type {};
17
+
18
+ // Extract double from std::complex<double>; is identity otherwise
19
+ // TODO: Write in more idiomatic C++17
20
+ template <typename T> struct scalar_value_type {
21
+ using type = T;
22
+ };
23
+ template <typename T> struct scalar_value_type<std::complex<T>> {
24
+ using type = T;
25
+ };
26
+ template <typename T> struct scalar_value_type<c10::complex<T>> {
27
+ using type = T;
28
+ };
29
+
30
+ } // namespace c10
31
+
32
+ namespace std {
33
+
34
+ template <typename T>
35
+ class numeric_limits<c10::complex<T>> : public numeric_limits<T> {};
36
+
37
+ template <typename T> bool isnan(const c10::complex<T> &v) {
38
+ return std::isnan(v.real()) || std::isnan(v.imag());
39
+ }
40
+
41
+ } // namespace std
@@ -22,7 +22,7 @@ struct integer_iterator {
22
22
  using pointer = I *;
23
23
  using reference = I &;
24
24
 
25
- explicit constexpr integer_iterator(I value_) : value(value_) {}
25
+ explicit constexpr integer_iterator(I value) : value(value) {}
26
26
 
27
27
  constexpr I operator*() const { return value; }
28
28
 
@@ -89,7 +89,7 @@ private:
89
89
  template <typename Integer1, typename Integer2,
90
90
  std::enable_if_t<std::is_integral_v<Integer1>, bool> = true,
91
91
  std::enable_if_t<std::is_integral_v<Integer2>, bool> = true>
92
- integer_range<Integer2> irange(Integer1 begin, Integer2 end) {
92
+ constexpr integer_range<Integer2> irange(Integer1 begin, Integer2 end) {
93
93
  // If end<=begin then the range is empty; we can achieve this effect by
94
94
  // choosing the larger of {begin, end} as the loop terminator
95
95
  return {static_cast<Integer2>(begin),
@@ -0,0 +1,95 @@
1
+ #pragma once
2
+
3
+ #include <c10/macros/Macros.h>
4
+ #include <c10/util/TypeSafeSignMath.h>
5
+ #include <c10/util/complex.h>
6
+
7
+ #include <cmath>
8
+ #include <limits>
9
+ #include <type_traits>
10
+
11
+ namespace c10 {
12
+ // In some versions of MSVC, there will be a compiler error when building.
13
+ // C4146: unary minus operator applied to unsigned type, result still unsigned
14
+ // C4804: unsafe use of type 'bool' in operation
15
+ // It can be addressed by disabling the following warning.
16
+ #ifdef _MSC_VER
17
+ #pragma warning(push)
18
+ #pragma warning(disable : 4146)
19
+ #pragma warning(disable : 4804)
20
+ #pragma warning(disable : 4018)
21
+ #endif
22
+
23
+ // The overflow checks may involve float to int conversion which may
24
+ // trigger precision loss warning. Re-enable the warning once the code
25
+ // is fixed. See T58053069.
26
+ C10_CLANG_DIAGNOSTIC_PUSH()
27
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
28
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
29
+ #endif
30
+
31
+ // bool can be converted to any type.
32
+ // Without specializing on bool, in pytorch_linux_trusty_py2_7_9_build:
33
+ // `error: comparison of constant '255' with boolean expression is always false`
34
+ // for `f > limit::max()` below
35
+ template <typename To, typename From>
36
+ std::enable_if_t<std::is_same_v<From, bool>, bool>
37
+ overflows(From /*f*/, bool strict_unsigned [[maybe_unused]] = false) {
38
+ return false;
39
+ }
40
+
41
+ // skip isnan and isinf check for integral types
42
+ template <typename To, typename From>
43
+ std::enable_if_t<std::is_integral_v<From> && !std::is_same_v<From, bool>, bool>
44
+ overflows(From f, bool strict_unsigned = false) {
45
+ using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
46
+ if constexpr (!limit::is_signed && std::numeric_limits<From>::is_signed) {
47
+ // allow for negative numbers to wrap using two's complement arithmetic.
48
+ // For example, with uint8, this allows for `a - b` to be treated as
49
+ // `a + 255 * b`.
50
+ if (!strict_unsigned) {
51
+ return greater_than_max<To>(f) ||
52
+ (c10::is_negative(f) &&
53
+ -static_cast<uint64_t>(f) > static_cast<uint64_t>(limit::max()));
54
+ }
55
+ }
56
+ return c10::less_than_lowest<To>(f) || greater_than_max<To>(f);
57
+ }
58
+
59
+ template <typename To, typename From>
60
+ std::enable_if_t<std::is_floating_point_v<From>, bool>
61
+ overflows(From f, bool strict_unsigned [[maybe_unused]] = false) {
62
+ using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
63
+ if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
64
+ return false;
65
+ }
66
+ if (!limit::has_quiet_NaN && (f != f)) {
67
+ return true;
68
+ }
69
+ return f < limit::lowest() || f > limit::max();
70
+ }
71
+
72
+ C10_CLANG_DIAGNOSTIC_POP()
73
+
74
+ #ifdef _MSC_VER
75
+ #pragma warning(pop)
76
+ #endif
77
+
78
+ template <typename To, typename From>
79
+ std::enable_if_t<is_complex<From>::value, bool>
80
+ overflows(From f, bool strict_unsigned = false) {
81
+ // casts from complex to real are considered to overflow if the
82
+ // imaginary component is non-zero
83
+ if (!is_complex<To>::value && f.imag() != 0) {
84
+ return true;
85
+ }
86
+ // Check for overflow componentwise
87
+ // (Technically, the imag overflow check is guaranteed to be false
88
+ // when !is_complex<To>, but any optimizer worth its salt will be
89
+ // able to figure it out.)
90
+ return overflows<typename scalar_value_type<To>::type,
91
+ typename From::value_type>(f.real(), strict_unsigned) ||
92
+ overflows<typename scalar_value_type<To>::type,
93
+ typename From::value_type>(f.imag(), strict_unsigned);
94
+ }
95
+ } // namespace c10
@@ -0,0 +1,88 @@
1
+ #define C10_USING_CUSTOM_GENERATED_MACROS
2
+ #pragma once
3
+
4
+ /* Header file to define the common scaffolding for exported symbols.
5
+ *
6
+ * Export is by itself a quite tricky situation to deal with, and if you are
7
+ * hitting this file, make sure you start with the background here:
8
+ * - Linux: https://gcc.gnu.org/wiki/Visibility
9
+ * - Windows:
10
+ * https://docs.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=vs-2017
11
+ *
12
+ * Do NOT include this file directly. Instead, use c10/macros/Macros.h
13
+ */
14
+
15
+ // You do not need to edit this part of file unless you are changing the core
16
+ // pytorch export abstractions.
17
+ //
18
+ // This part defines the C10 core export and import macros. This is controlled
19
+ // by whether we are building shared libraries or not, which is determined
20
+ // during build time and codified in c10/core/cmake_macros.h.
21
+ // When the library is built as a shared lib, EXPORT and IMPORT will contain
22
+ // visibility attributes. If it is being built as a static lib, then EXPORT
23
+ // and IMPORT basically have no effect.
24
+
25
+ // As a rule of thumb, you should almost NEVER mix static and shared builds for
26
+ // libraries that depend on c10. AKA, if c10 is built as a static library, we
27
+ // recommend everything dependent on c10 to be built statically. If c10 is built
28
+ // as a shared library, everything dependent on it should be built as shared. In
29
+ // the PyTorch project, all native libraries shall use the macro
30
+ // C10_BUILD_SHARED_LIB to check whether pytorch is building shared or static
31
+ // libraries.
32
+
33
+ // For build systems that do not directly depend on CMake and directly build
34
+ // from the source directory (such as Buck), one may not have a cmake_macros.h
35
+ // file at all. In this case, the build system is responsible for providing
36
+ // correct macro definitions corresponding to the cmake_macros.h.in file.
37
+ //
38
+ // In such scenarios, one should define the macro
39
+ // C10_USING_CUSTOM_GENERATED_MACROS
40
+ // to inform this header that it does not need to include the cmake_macros.h
41
+ // file.
42
+
43
+ #ifdef _WIN32
44
+ #define C10_HIDDEN
45
+ #if defined(C10_BUILD_SHARED_LIBS)
46
+ #define C10_EXPORT __declspec(dllexport)
47
+ #define C10_IMPORT __declspec(dllimport)
48
+ #else
49
+ #define C10_EXPORT
50
+ #define C10_IMPORT
51
+ #endif
52
+ #else // _WIN32
53
+ #if defined(__GNUC__)
54
+ #define C10_EXPORT __attribute__((__visibility__("default")))
55
+ #define C10_HIDDEN __attribute__((__visibility__("hidden")))
56
+ #else // defined(__GNUC__)
57
+ #define C10_EXPORT
58
+ #define C10_HIDDEN
59
+ #endif // defined(__GNUC__)
60
+ #define C10_IMPORT C10_EXPORT
61
+ #endif // _WIN32
62
+
63
+ #ifdef NO_EXPORT
64
+ #undef C10_EXPORT
65
+ #define C10_EXPORT
66
+ #endif
67
+
68
+ // Definition of an adaptive XX_API macro, that depends on whether you are
69
+ // building the library itself or not, routes to XX_EXPORT and XX_IMPORT.
70
+ // Basically, you will need to do this for each shared library that you are
71
+ // building, and the instruction is as follows: assuming that you are building
72
+ // a library called libawesome.so. You should:
73
+ // (1) for your cmake target (usually done by "add_library(awesome, ...)"),
74
+ // define a macro called AWESOME_BUILD_MAIN_LIB using
75
+ // target_compile_options.
76
+ // (2) define the AWESOME_API macro similar to the one below.
77
+ // And in the source file of your awesome library, use AWESOME_API to
78
+ // annotate public symbols.
79
+
80
+ // Here, for the C10 library, we will define the macro C10_API for both import
81
+ // and export.
82
+
83
+ // This one is being used by libc10.so
84
+ #ifdef C10_BUILD_MAIN_LIB
85
+ #define C10_API C10_EXPORT
86
+ #else
87
+ #define C10_API C10_IMPORT
88
+ #endif
@@ -8,37 +8,14 @@
8
8
 
9
9
  #pragma once
10
10
 
11
- #include <executorch/runtime/core/portable_type/half.h>
11
+ #include <c10/util/complex.h>
12
12
 
13
- namespace executorch {
14
- namespace runtime {
15
- namespace etensor {
13
+ namespace executorch::runtime::etensor {
14
+ using c10::complex;
15
+ } // namespace executorch::runtime::etensor
16
16
 
17
- /**
18
- * An implementation of complex numbers, compatible with c10/util/complex.h from
19
- * pytorch core.
20
- */
21
- template <typename T> struct alignas(sizeof(T) * 2) complex {
22
- T real_ = T(0);
23
- T imag_ = T(0);
24
- };
25
-
26
- /**
27
- * Specialization for Half, which is not a primitive C numeric type.
28
- */
29
- template <> struct alignas(4) complex<Half> {
30
- Half real_;
31
- Half imag_;
32
- };
33
-
34
- } // namespace etensor
35
- } // namespace runtime
36
- } // namespace executorch
37
-
38
- namespace torch {
39
- namespace executor {
17
+ namespace torch::executor {
40
18
  // TODO(T197294990): Remove these deprecated aliases once all users have moved
41
19
  // to the new `::executorch` namespaces.
42
20
  using ::executorch::runtime::etensor::complex;
43
- } // namespace executor
44
- } // namespace torch
21
+ } // namespace torch::executor