react-native-executorch 0.5.5 → 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 (141) 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/models/speech_to_text/SpeechToText.cpp +2 -5
  8. package/common/rnexecutorch/models/speech_to_text/SpeechToText.h +1 -1
  9. package/common/rnexecutorch/threads/GlobalThreadPool.h +79 -0
  10. package/common/rnexecutorch/threads/HighPerformanceThreadPool.h +364 -0
  11. package/common/rnexecutorch/threads/utils/ThreadUtils.h +29 -0
  12. package/common/runner/runner.cpp +9 -3
  13. package/common/runner/runner.h +4 -3
  14. package/common/runner/text_token_generator.h +28 -10
  15. package/lib/module/controllers/LLMController.js +21 -2
  16. package/lib/module/controllers/LLMController.js.map +1 -1
  17. package/lib/module/hooks/natural_language_processing/useLLM.js +6 -2
  18. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  19. package/lib/module/modules/natural_language_processing/LLMModule.js +4 -2
  20. package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
  21. package/lib/module/types/llm.js.map +1 -1
  22. package/lib/typescript/controllers/LLMController.d.ts +4 -2
  23. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  24. package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
  25. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +3 -2
  26. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
  27. package/lib/typescript/types/llm.d.ts +7 -1
  28. package/lib/typescript/types/llm.d.ts.map +1 -1
  29. package/package.json +3 -1
  30. package/react-native-executorch.podspec +12 -31
  31. package/src/controllers/LLMController.ts +29 -5
  32. package/src/hooks/natural_language_processing/useLLM.ts +15 -1
  33. package/src/modules/natural_language_processing/LLMModule.ts +10 -2
  34. package/src/types/llm.ts +8 -0
  35. package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
  36. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  37. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  38. package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
  39. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libsentencepiece.a +0 -0
  40. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_c.a +0 -0
  41. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_cpp.a +0 -0
  42. package/third-party/android/libs/tokenizers-cpp/x86_64/libsentencepiece.a +0 -0
  43. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_c.a +0 -0
  44. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_cpp.a +0 -0
  45. package/third-party/include/c10/macros/Export.h +2 -86
  46. package/third-party/include/c10/macros/Macros.h +28 -5
  47. package/third-party/include/c10/util/BFloat16-inl.h +1 -4
  48. package/third-party/include/c10/util/BFloat16.h +5 -8
  49. package/third-party/include/c10/util/Half.h +5 -0
  50. package/third-party/include/c10/util/bit_cast.h +1 -1
  51. package/third-party/include/c10/util/complex.h +639 -0
  52. package/third-party/include/c10/util/complex_math.h +399 -0
  53. package/third-party/include/c10/util/complex_utils.h +41 -0
  54. package/third-party/include/c10/util/irange.h +2 -2
  55. package/third-party/include/c10/util/overflows.h +95 -0
  56. package/third-party/include/executorch/ExecuTorchError.h +75 -0
  57. package/third-party/include/executorch/ExecuTorchModule.h +115 -11
  58. package/third-party/include/executorch/ExecuTorchTensor.h +731 -51
  59. package/third-party/include/executorch/ExecuTorchValue.h +61 -9
  60. package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +181 -0
  61. package/third-party/include/executorch/extension/kernel_util/meta_programming.h +108 -0
  62. package/third-party/include/executorch/extension/kernel_util/type_list.h +137 -0
  63. package/third-party/include/executorch/extension/module/bundled_module.h +131 -0
  64. package/third-party/include/executorch/extension/module/module.h +46 -20
  65. package/third-party/include/executorch/extension/threadpool/cpuinfo_utils.h +1 -3
  66. package/third-party/include/executorch/extension/threadpool/threadpool.h +1 -3
  67. package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +35 -0
  68. package/third-party/include/executorch/runtime/backend/backend_execution_context.h +3 -3
  69. package/third-party/include/executorch/runtime/backend/backend_init_context.h +12 -6
  70. package/third-party/include/executorch/runtime/backend/backend_option_context.h +34 -0
  71. package/third-party/include/executorch/runtime/backend/interface.h +70 -9
  72. package/third-party/include/executorch/runtime/backend/options.h +206 -0
  73. package/third-party/include/executorch/runtime/core/evalue.h +19 -25
  74. package/third-party/include/executorch/runtime/core/event_tracer.h +32 -17
  75. package/third-party/include/executorch/runtime/core/event_tracer_hooks.h +23 -14
  76. package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +32 -9
  77. package/third-party/include/executorch/runtime/core/exec_aten/util/dim_order_util.h +3 -2
  78. package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +43 -75
  79. package/third-party/include/executorch/runtime/core/exec_aten/util/tensor_util.h +88 -87
  80. package/third-party/include/executorch/runtime/core/function_ref.h +100 -0
  81. package/third-party/include/executorch/runtime/core/named_data_map.h +14 -14
  82. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +2 -86
  83. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +28 -5
  84. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -4
  85. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +5 -8
  86. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +5 -0
  87. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -1
  88. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +639 -0
  89. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_math.h +399 -0
  90. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_utils.h +41 -0
  91. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +2 -2
  92. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/overflows.h +95 -0
  93. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +88 -0
  94. package/third-party/include/executorch/runtime/core/portable_type/complex.h +6 -29
  95. package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +20 -0
  96. package/third-party/include/executorch/runtime/core/span.h +4 -0
  97. package/third-party/include/executorch/runtime/core/tag.h +19 -0
  98. package/third-party/include/executorch/runtime/core/tensor_layout.h +2 -2
  99. package/third-party/include/executorch/runtime/executor/method.h +15 -3
  100. package/third-party/include/executorch/runtime/executor/method_meta.h +34 -5
  101. package/third-party/include/executorch/runtime/executor/program.h +3 -4
  102. package/third-party/include/executorch/runtime/executor/pte_data_map.h +9 -8
  103. package/third-party/include/executorch/runtime/executor/tensor_parser.h +14 -13
  104. package/third-party/include/executorch/runtime/kernel/kernel_runtime_context.h +5 -5
  105. package/third-party/include/executorch/runtime/kernel/operator_registry.h +21 -19
  106. package/third-party/include/executorch/runtime/platform/compiler.h +8 -0
  107. package/third-party/include/executorch/runtime/platform/platform.h +126 -0
  108. package/third-party/include/headeronly/macros/Export.h +88 -0
  109. package/third-party/include/tokenizers-cpp/tokenizers_c.h +61 -0
  110. package/third-party/include/torch/headeronly/macros/Export.h +88 -0
  111. package/third-party/ios/ExecutorchLib.xcframework/Info.plist +43 -0
  112. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  113. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  114. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  115. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  116. package/third-party/ios/libs/cpuinfo/libcpuinfo.a +0 -0
  117. package/third-party/ios/libs/pthreadpool/physical-arm64-release/libpthreadpool.a +0 -0
  118. package/third-party/ios/libs/pthreadpool/simulator-arm64-debug/libpthreadpool.a +0 -0
  119. package/ios/libs/executorch/libbackend_coreml_ios.a +0 -0
  120. package/ios/libs/executorch/libbackend_coreml_simulator.a +0 -0
  121. package/ios/libs/executorch/libbackend_mps_ios.a +0 -0
  122. package/ios/libs/executorch/libbackend_mps_simulator.a +0 -0
  123. package/ios/libs/executorch/libbackend_xnnpack_ios.a +0 -0
  124. package/ios/libs/executorch/libbackend_xnnpack_simulator.a +0 -0
  125. package/ios/libs/executorch/libexecutorch_ios.a +0 -0
  126. package/ios/libs/executorch/libexecutorch_simulator.a +0 -0
  127. package/ios/libs/executorch/libkernels_custom_ios.a +0 -0
  128. package/ios/libs/executorch/libkernels_custom_simulator.a +0 -0
  129. package/ios/libs/executorch/libkernels_optimized_ios.a +0 -0
  130. package/ios/libs/executorch/libkernels_optimized_simulator.a +0 -0
  131. package/ios/libs/executorch/libkernels_portable_ios.a +0 -0
  132. package/ios/libs/executorch/libkernels_portable_simulator.a +0 -0
  133. package/ios/libs/executorch/libkernels_quantized_ios.a +0 -0
  134. package/ios/libs/executorch/libkernels_quantized_simulator.a +0 -0
  135. package/third-party/ios/ios.toolchain.cmake +0 -1122
  136. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libsentencepiece.a +0 -0
  137. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_c.a +0 -0
  138. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_cpp.a +0 -0
  139. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libsentencepiece.a +0 -0
  140. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_c.a +0 -0
  141. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_cpp.a +0 -0
@@ -0,0 +1,639 @@
1
+ #pragma once
2
+
3
+ #include <complex>
4
+
5
+ #include <c10/macros/Macros.h>
6
+ #include <c10/util/Half.h>
7
+
8
+ #if defined(__CUDACC__) || defined(__HIPCC__)
9
+ #include <thrust/complex.h>
10
+ #endif
11
+
12
+ C10_CLANG_DIAGNOSTIC_PUSH()
13
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
14
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
15
+ #endif
16
+ #if C10_CLANG_HAS_WARNING("-Wfloat-conversion")
17
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wfloat-conversion")
18
+ #endif
19
+
20
+ namespace c10 {
21
+
22
+ // c10::complex is an implementation of complex numbers that aims
23
+ // to work on all devices supported by PyTorch
24
+ //
25
+ // Most of the APIs duplicates std::complex
26
+ // Reference: https://en.cppreference.com/w/cpp/numeric/complex
27
+ //
28
+ // [NOTE: Complex Operator Unification]
29
+ // Operators currently use a mix of std::complex, thrust::complex, and
30
+ // c10::complex internally. The end state is that all operators will use
31
+ // c10::complex internally. Until then, there may be some hacks to support all
32
+ // variants.
33
+ //
34
+ //
35
+ // [Note on Constructors]
36
+ //
37
+ // The APIs of constructors are mostly copied from C++ standard:
38
+ // https://en.cppreference.com/w/cpp/numeric/complex/complex
39
+ //
40
+ // Since C++14, all constructors are constexpr in std::complex
41
+ //
42
+ // There are three types of constructors:
43
+ // - initializing from real and imag:
44
+ // `constexpr complex( const T& re = T(), const T& im = T() );`
45
+ // - implicitly-declared copy constructor
46
+ // - converting constructors
47
+ //
48
+ // Converting constructors:
49
+ // - std::complex defines converting constructor between float/double/long
50
+ // double,
51
+ // while we define converting constructor between float/double.
52
+ // - For these converting constructors, upcasting is implicit, downcasting is
53
+ // explicit.
54
+ // - We also define explicit casting from std::complex/thrust::complex
55
+ // - Note that the conversion from thrust is not constexpr, because
56
+ // thrust does not define them as constexpr ????
57
+ //
58
+ //
59
+ // [Operator =]
60
+ //
61
+ // The APIs of operator = are mostly copied from C++ standard:
62
+ // https://en.cppreference.com/w/cpp/numeric/complex/operator%3D
63
+ //
64
+ // Since C++20, all operator= are constexpr. Although we are not building with
65
+ // C++20, we also obey this behavior.
66
+ //
67
+ // There are three types of assign operator:
68
+ // - Assign a real value from the same scalar type
69
+ // - In std, this is templated as complex& operator=(const T& x)
70
+ // with specialization `complex& operator=(T x)` for float/double/long
71
+ // double Since we only support float and double, on will use `complex&
72
+ // operator=(T x)`
73
+ // - Copy assignment operator and converting assignment operator
74
+ // - There is no specialization of converting assignment operators, which type
75
+ // is
76
+ // convertible is solely dependent on whether the scalar type is convertible
77
+ //
78
+ // In addition to the standard assignment, we also provide assignment operators
79
+ // with std and thrust
80
+ //
81
+ //
82
+ // [Casting operators]
83
+ //
84
+ // std::complex does not have casting operators. We define casting operators
85
+ // casting to std::complex and thrust::complex
86
+ //
87
+ //
88
+ // [Operator ""]
89
+ //
90
+ // std::complex has custom literals `i`, `if` and `il` defined in namespace
91
+ // `std::literals::complex_literals`. We define our own custom literals in the
92
+ // namespace `c10::complex_literals`. Our custom literals does not follow the
93
+ // same behavior as in std::complex, instead, we define _if, _id to construct
94
+ // float/double complex literals.
95
+ //
96
+ //
97
+ // [real() and imag()]
98
+ //
99
+ // In C++20, there are two overload of these functions, one it to return the
100
+ // real/imag, another is to set real/imag, they are both constexpr. We follow
101
+ // this design.
102
+ //
103
+ //
104
+ // [Operator +=,-=,*=,/=]
105
+ //
106
+ // Since C++20, these operators become constexpr. In our implementation, they
107
+ // are also constexpr.
108
+ //
109
+ // There are two types of such operators: operating with a real number, or
110
+ // operating with another complex number. For the operating with a real number,
111
+ // the generic template form has argument type `const T &`, while the overload
112
+ // for float/double/long double has `T`. We will follow the same type as
113
+ // float/double/long double in std.
114
+ //
115
+ // [Unary operator +-]
116
+ //
117
+ // Since C++20, they are constexpr. We also make them expr
118
+ //
119
+ // [Binary operators +-*/]
120
+ //
121
+ // Each operator has three versions (taking + as example):
122
+ // - complex + complex
123
+ // - complex + real
124
+ // - real + complex
125
+ //
126
+ // [Operator ==, !=]
127
+ //
128
+ // Each operator has three versions (taking == as example):
129
+ // - complex == complex
130
+ // - complex == real
131
+ // - real == complex
132
+ //
133
+ // Some of them are removed on C++20, but we decide to keep them
134
+ //
135
+ // [Operator <<, >>]
136
+ //
137
+ // These are implemented by casting to std::complex
138
+ //
139
+ //
140
+ //
141
+ // TODO(@zasdfgbnm): c10::complex<c10::Half> is not currently supported,
142
+ // because:
143
+ // - lots of members and functions of c10::Half are not constexpr
144
+ // - thrust::complex only support float and double
145
+
146
+ template <typename T> struct alignas(sizeof(T) * 2) complex {
147
+ using value_type = T;
148
+
149
+ T real_ = T(0);
150
+ T imag_ = T(0);
151
+
152
+ constexpr complex() = default;
153
+ C10_HOST_DEVICE constexpr complex(const T &re, const T &im = T())
154
+ : real_(re), imag_(im) {}
155
+ template <typename U>
156
+ explicit constexpr complex(const std::complex<U> &other)
157
+ : complex(other.real(), other.imag()) {}
158
+ #if defined(__CUDACC__) || defined(__HIPCC__)
159
+ template <typename U>
160
+ explicit C10_HOST_DEVICE complex(const thrust::complex<U> &other)
161
+ : real_(other.real()), imag_(other.imag()) {}
162
+ // NOTE can not be implemented as follow due to ROCm bug:
163
+ // explicit C10_HOST_DEVICE complex(const thrust::complex<U> &other):
164
+ // complex(other.real(), other.imag()) {}
165
+ #endif
166
+
167
+ // Use SFINAE to specialize casting constructor for c10::complex<float> and
168
+ // c10::complex<double>
169
+ template <typename U = T>
170
+ C10_HOST_DEVICE explicit constexpr complex(
171
+ const std::enable_if_t<std::is_same_v<U, float>, complex<double>> &other)
172
+ : real_(other.real_), imag_(other.imag_) {}
173
+ template <typename U = T>
174
+ C10_HOST_DEVICE constexpr complex(
175
+ const std::enable_if_t<std::is_same_v<U, double>, complex<float>> &other)
176
+ : real_(other.real_), imag_(other.imag_) {}
177
+
178
+ constexpr complex<T> &operator=(T re) {
179
+ real_ = re;
180
+ imag_ = 0;
181
+ return *this;
182
+ }
183
+
184
+ constexpr complex<T> &operator+=(T re) {
185
+ real_ += re;
186
+ return *this;
187
+ }
188
+
189
+ constexpr complex<T> &operator-=(T re) {
190
+ real_ -= re;
191
+ return *this;
192
+ }
193
+
194
+ constexpr complex<T> &operator*=(T re) {
195
+ real_ *= re;
196
+ imag_ *= re;
197
+ return *this;
198
+ }
199
+
200
+ constexpr complex<T> &operator/=(T re) {
201
+ real_ /= re;
202
+ imag_ /= re;
203
+ return *this;
204
+ }
205
+
206
+ template <typename U> constexpr complex<T> &operator=(const complex<U> &rhs) {
207
+ real_ = rhs.real();
208
+ imag_ = rhs.imag();
209
+ return *this;
210
+ }
211
+
212
+ template <typename U>
213
+ constexpr complex<T> &operator+=(const complex<U> &rhs) {
214
+ real_ += rhs.real();
215
+ imag_ += rhs.imag();
216
+ return *this;
217
+ }
218
+
219
+ template <typename U>
220
+ constexpr complex<T> &operator-=(const complex<U> &rhs) {
221
+ real_ -= rhs.real();
222
+ imag_ -= rhs.imag();
223
+ return *this;
224
+ }
225
+
226
+ template <typename U>
227
+ constexpr complex<T> &operator*=(const complex<U> &rhs) {
228
+ // (a + bi) * (c + di) = (a*c - b*d) + (a * d + b * c) i
229
+ T a = real_;
230
+ T b = imag_;
231
+ U c = rhs.real();
232
+ U d = rhs.imag();
233
+ real_ = a * c - b * d;
234
+ imag_ = a * d + b * c;
235
+ return *this;
236
+ }
237
+
238
+ #ifdef __APPLE__
239
+ #define FORCE_INLINE_APPLE __attribute__((always_inline))
240
+ #else
241
+ #define FORCE_INLINE_APPLE
242
+ #endif
243
+ template <typename U>
244
+ constexpr FORCE_INLINE_APPLE complex<T> &
245
+ operator/=(const complex<U> &rhs) __ubsan_ignore_float_divide_by_zero__ {
246
+ // (a + bi) / (c + di) = (ac + bd)/(c^2 + d^2) + (bc - ad)/(c^2 + d^2) i
247
+ // the calculation below follows numpy's complex division
248
+ T a = real_;
249
+ T b = imag_;
250
+ U c = rhs.real();
251
+ U d = rhs.imag();
252
+
253
+ #if defined(__GNUC__) && !defined(__clang__)
254
+ // std::abs is already constexpr by gcc
255
+ auto abs_c = std::abs(c);
256
+ auto abs_d = std::abs(d);
257
+ #else
258
+ auto abs_c = c < 0 ? -c : c;
259
+ auto abs_d = d < 0 ? -d : d;
260
+ #endif
261
+
262
+ if (abs_c >= abs_d) {
263
+ if (abs_c == U(0) && abs_d == U(0)) {
264
+ /* divide by zeros should yield a complex inf or nan */
265
+ real_ = a / abs_c;
266
+ imag_ = b / abs_d;
267
+ } else {
268
+ auto rat = d / c;
269
+ auto scl = U(1.0) / (c + d * rat);
270
+ real_ = (a + b * rat) * scl;
271
+ imag_ = (b - a * rat) * scl;
272
+ }
273
+ } else {
274
+ auto rat = c / d;
275
+ auto scl = U(1.0) / (d + c * rat);
276
+ real_ = (a * rat + b) * scl;
277
+ imag_ = (b * rat - a) * scl;
278
+ }
279
+ return *this;
280
+ }
281
+ #undef FORCE_INLINE_APPLE
282
+
283
+ template <typename U>
284
+ constexpr complex<T> &operator=(const std::complex<U> &rhs) {
285
+ real_ = rhs.real();
286
+ imag_ = rhs.imag();
287
+ return *this;
288
+ }
289
+
290
+ #if defined(__CUDACC__) || defined(__HIPCC__)
291
+ template <typename U>
292
+ C10_HOST_DEVICE complex<T> &operator=(const thrust::complex<U> &rhs) {
293
+ real_ = rhs.real();
294
+ imag_ = rhs.imag();
295
+ return *this;
296
+ }
297
+ #endif
298
+
299
+ template <typename U> explicit constexpr operator std::complex<U>() const {
300
+ return std::complex<U>(std::complex<T>(real(), imag()));
301
+ }
302
+
303
+ #if defined(__CUDACC__) || defined(__HIPCC__)
304
+ template <typename U>
305
+ C10_HOST_DEVICE explicit operator thrust::complex<U>() const {
306
+ return static_cast<thrust::complex<U>>(thrust::complex<T>(real(), imag()));
307
+ }
308
+ #endif
309
+
310
+ // consistent with NumPy behavior
311
+ explicit constexpr operator bool() const { return real() || imag(); }
312
+
313
+ C10_HOST_DEVICE constexpr T real() const { return real_; }
314
+ constexpr void real(T value) { real_ = value; }
315
+ C10_HOST_DEVICE constexpr T imag() const { return imag_; }
316
+ constexpr void imag(T value) { imag_ = value; }
317
+ };
318
+
319
+ namespace complex_literals {
320
+
321
+ constexpr complex<float> operator""_if(long double imag) {
322
+ return complex<float>(0.0f, static_cast<float>(imag));
323
+ }
324
+
325
+ constexpr complex<double> operator""_id(long double imag) {
326
+ return complex<double>(0.0, static_cast<double>(imag));
327
+ }
328
+
329
+ constexpr complex<float> operator""_if(unsigned long long imag) {
330
+ return complex<float>(0.0f, static_cast<float>(imag));
331
+ }
332
+
333
+ constexpr complex<double> operator""_id(unsigned long long imag) {
334
+ return complex<double>(0.0, static_cast<double>(imag));
335
+ }
336
+
337
+ } // namespace complex_literals
338
+
339
+ template <typename T> constexpr complex<T> operator+(const complex<T> &val) {
340
+ return val;
341
+ }
342
+
343
+ template <typename T> constexpr complex<T> operator-(const complex<T> &val) {
344
+ return complex<T>(-val.real(), -val.imag());
345
+ }
346
+
347
+ template <typename T>
348
+ constexpr complex<T> operator+(const complex<T> &lhs, const complex<T> &rhs) {
349
+ complex<T> result = lhs;
350
+ return result += rhs;
351
+ }
352
+
353
+ template <typename T>
354
+ constexpr complex<T> operator+(const complex<T> &lhs, const T &rhs) {
355
+ complex<T> result = lhs;
356
+ return result += rhs;
357
+ }
358
+
359
+ template <typename T>
360
+ constexpr complex<T> operator+(const T &lhs, const complex<T> &rhs) {
361
+ return complex<T>(lhs + rhs.real(), rhs.imag());
362
+ }
363
+
364
+ template <typename T>
365
+ constexpr complex<T> operator-(const complex<T> &lhs, const complex<T> &rhs) {
366
+ complex<T> result = lhs;
367
+ return result -= rhs;
368
+ }
369
+
370
+ template <typename T>
371
+ constexpr complex<T> operator-(const complex<T> &lhs, const T &rhs) {
372
+ complex<T> result = lhs;
373
+ return result -= rhs;
374
+ }
375
+
376
+ template <typename T>
377
+ constexpr complex<T> operator-(const T &lhs, const complex<T> &rhs) {
378
+ complex<T> result = -rhs;
379
+ return result += lhs;
380
+ }
381
+
382
+ template <typename T>
383
+ constexpr complex<T> operator*(const complex<T> &lhs, const complex<T> &rhs) {
384
+ complex<T> result = lhs;
385
+ return result *= rhs;
386
+ }
387
+
388
+ template <typename T>
389
+ constexpr complex<T> operator*(const complex<T> &lhs, const T &rhs) {
390
+ complex<T> result = lhs;
391
+ return result *= rhs;
392
+ }
393
+
394
+ template <typename T>
395
+ constexpr complex<T> operator*(const T &lhs, const complex<T> &rhs) {
396
+ complex<T> result = rhs;
397
+ return result *= lhs;
398
+ }
399
+
400
+ template <typename T>
401
+ constexpr complex<T> operator/(const complex<T> &lhs, const complex<T> &rhs) {
402
+ complex<T> result = lhs;
403
+ return result /= rhs;
404
+ }
405
+
406
+ template <typename T>
407
+ constexpr complex<T> operator/(const complex<T> &lhs, const T &rhs) {
408
+ complex<T> result = lhs;
409
+ return result /= rhs;
410
+ }
411
+
412
+ template <typename T>
413
+ constexpr complex<T> operator/(const T &lhs, const complex<T> &rhs) {
414
+ complex<T> result(lhs, T());
415
+ return result /= rhs;
416
+ }
417
+
418
+ // Define operators between integral scalars and c10::complex. std::complex does
419
+ // not support this when T is a floating-point number. This is useful because it
420
+ // saves a lot of "static_cast" when operate a complex and an integer. This
421
+ // makes the code both less verbose and potentially more efficient.
422
+ #define COMPLEX_INTEGER_OP_TEMPLATE_CONDITION \
423
+ typename std::enable_if_t< \
424
+ std::is_floating_point_v<fT> && std::is_integral_v<iT>, int> = 0
425
+
426
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
427
+ constexpr c10::complex<fT> operator+(const c10::complex<fT> &a, const iT &b) {
428
+ return a + static_cast<fT>(b);
429
+ }
430
+
431
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
432
+ constexpr c10::complex<fT> operator+(const iT &a, const c10::complex<fT> &b) {
433
+ return static_cast<fT>(a) + b;
434
+ }
435
+
436
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
437
+ constexpr c10::complex<fT> operator-(const c10::complex<fT> &a, const iT &b) {
438
+ return a - static_cast<fT>(b);
439
+ }
440
+
441
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
442
+ constexpr c10::complex<fT> operator-(const iT &a, const c10::complex<fT> &b) {
443
+ return static_cast<fT>(a) - b;
444
+ }
445
+
446
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
447
+ constexpr c10::complex<fT> operator*(const c10::complex<fT> &a, const iT &b) {
448
+ return a * static_cast<fT>(b);
449
+ }
450
+
451
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
452
+ constexpr c10::complex<fT> operator*(const iT &a, const c10::complex<fT> &b) {
453
+ return static_cast<fT>(a) * b;
454
+ }
455
+
456
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
457
+ constexpr c10::complex<fT> operator/(const c10::complex<fT> &a, const iT &b) {
458
+ return a / static_cast<fT>(b);
459
+ }
460
+
461
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
462
+ constexpr c10::complex<fT> operator/(const iT &a, const c10::complex<fT> &b) {
463
+ return static_cast<fT>(a) / b;
464
+ }
465
+
466
+ #undef COMPLEX_INTEGER_OP_TEMPLATE_CONDITION
467
+
468
+ template <typename T>
469
+ constexpr bool operator==(const complex<T> &lhs, const complex<T> &rhs) {
470
+ return (lhs.real() == rhs.real()) && (lhs.imag() == rhs.imag());
471
+ }
472
+
473
+ template <typename T>
474
+ constexpr bool operator==(const complex<T> &lhs, const T &rhs) {
475
+ return (lhs.real() == rhs) && (lhs.imag() == T());
476
+ }
477
+
478
+ template <typename T>
479
+ constexpr bool operator==(const T &lhs, const complex<T> &rhs) {
480
+ return (lhs == rhs.real()) && (T() == rhs.imag());
481
+ }
482
+
483
+ template <typename T>
484
+ constexpr bool operator!=(const complex<T> &lhs, const complex<T> &rhs) {
485
+ return !(lhs == rhs);
486
+ }
487
+
488
+ template <typename T>
489
+ constexpr bool operator!=(const complex<T> &lhs, const T &rhs) {
490
+ return !(lhs == rhs);
491
+ }
492
+
493
+ template <typename T>
494
+ constexpr bool operator!=(const T &lhs, const complex<T> &rhs) {
495
+ return !(lhs == rhs);
496
+ }
497
+
498
+ template <typename T, typename CharT, typename Traits>
499
+ std::basic_ostream<CharT, Traits> &
500
+ operator<<(std::basic_ostream<CharT, Traits> &os, const complex<T> &x) {
501
+ return (os << static_cast<std::complex<T>>(x));
502
+ }
503
+
504
+ template <typename T, typename CharT, typename Traits>
505
+ std::basic_istream<CharT, Traits> &
506
+ operator>>(std::basic_istream<CharT, Traits> &is, complex<T> &x) {
507
+ std::complex<T> tmp;
508
+ is >> tmp;
509
+ x = tmp;
510
+ return is;
511
+ }
512
+
513
+ } // namespace c10
514
+
515
+ // std functions
516
+ //
517
+ // The implementation of these functions also follow the design of C++20
518
+
519
+ namespace std {
520
+
521
+ template <typename T> constexpr T real(const c10::complex<T> &z) {
522
+ return z.real();
523
+ }
524
+
525
+ template <typename T> constexpr T imag(const c10::complex<T> &z) {
526
+ return z.imag();
527
+ }
528
+
529
+ template <typename T> C10_HOST_DEVICE T abs(const c10::complex<T> &z) {
530
+ #if defined(__CUDACC__) || defined(__HIPCC__)
531
+ return thrust::abs(static_cast<thrust::complex<T>>(z));
532
+ #else
533
+ return std::abs(static_cast<std::complex<T>>(z));
534
+ #endif
535
+ }
536
+
537
+ #if defined(USE_ROCM)
538
+ #define ROCm_Bug(x)
539
+ #else
540
+ #define ROCm_Bug(x) x
541
+ #endif
542
+
543
+ template <typename T> C10_HOST_DEVICE T arg(const c10::complex<T> &z) {
544
+ return ROCm_Bug(std)::atan2(std::imag(z), std::real(z));
545
+ }
546
+
547
+ #undef ROCm_Bug
548
+
549
+ template <typename T> constexpr T norm(const c10::complex<T> &z) {
550
+ return z.real() * z.real() + z.imag() * z.imag();
551
+ }
552
+
553
+ // For std::conj, there are other versions of it:
554
+ // constexpr std::complex<float> conj( float z );
555
+ // template< class DoubleOrInteger >
556
+ // constexpr std::complex<double> conj( DoubleOrInteger z );
557
+ // constexpr std::complex<long double> conj( long double z );
558
+ // These are not implemented
559
+ // TODO(@zasdfgbnm): implement them as c10::conj
560
+ template <typename T> constexpr c10::complex<T> conj(const c10::complex<T> &z) {
561
+ return c10::complex<T>(z.real(), -z.imag());
562
+ }
563
+
564
+ // Thrust does not have complex --> complex version of thrust::proj,
565
+ // so this function is not implemented at c10 right now.
566
+ // TODO(@zasdfgbnm): implement it by ourselves
567
+
568
+ // There is no c10 version of std::polar, because std::polar always
569
+ // returns std::complex. Use c10::polar instead;
570
+
571
+ } // namespace std
572
+
573
+ namespace c10 {
574
+
575
+ template <typename T>
576
+ C10_HOST_DEVICE complex<T> polar(const T &r, const T &theta = T()) {
577
+ #if defined(__CUDACC__) || defined(__HIPCC__)
578
+ return static_cast<complex<T>>(thrust::polar(r, theta));
579
+ #else
580
+ // std::polar() requires r >= 0, so spell out the explicit implementation to
581
+ // avoid a branch.
582
+ return complex<T>(r * std::cos(theta), r * std::sin(theta));
583
+ #endif
584
+ }
585
+
586
+ template <> struct alignas(4) complex<Half> {
587
+ Half real_;
588
+ Half imag_;
589
+
590
+ // Constructors
591
+ complex() = default;
592
+ // Half constructor is not constexpr so the following constructor can't
593
+ // be constexpr
594
+ C10_HOST_DEVICE explicit inline complex(const Half &real, const Half &imag)
595
+ : real_(real), imag_(imag) {}
596
+ C10_HOST_DEVICE inline complex(const c10::complex<float> &value)
597
+ : real_(value.real()), imag_(value.imag()) {}
598
+
599
+ // Conversion operator
600
+ inline C10_HOST_DEVICE operator c10::complex<float>() const {
601
+ return {real_, imag_};
602
+ }
603
+
604
+ constexpr C10_HOST_DEVICE Half real() const { return real_; }
605
+ constexpr C10_HOST_DEVICE Half imag() const { return imag_; }
606
+
607
+ C10_HOST_DEVICE complex<Half> &operator+=(const complex<Half> &other) {
608
+ real_ = static_cast<float>(real_) + static_cast<float>(other.real_);
609
+ imag_ = static_cast<float>(imag_) + static_cast<float>(other.imag_);
610
+ return *this;
611
+ }
612
+
613
+ C10_HOST_DEVICE complex<Half> &operator-=(const complex<Half> &other) {
614
+ real_ = static_cast<float>(real_) - static_cast<float>(other.real_);
615
+ imag_ = static_cast<float>(imag_) - static_cast<float>(other.imag_);
616
+ return *this;
617
+ }
618
+
619
+ C10_HOST_DEVICE complex<Half> &operator*=(const complex<Half> &other) {
620
+ auto a = static_cast<float>(real_);
621
+ auto b = static_cast<float>(imag_);
622
+ auto c = static_cast<float>(other.real());
623
+ auto d = static_cast<float>(other.imag());
624
+ real_ = a * c - b * d;
625
+ imag_ = a * d + b * c;
626
+ return *this;
627
+ }
628
+ };
629
+
630
+ } // namespace c10
631
+
632
+ C10_CLANG_DIAGNOSTIC_POP()
633
+
634
+ #define C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H
635
+ // math functions are included in a separate file
636
+ #include <c10/util/complex_math.h> // IWYU pragma: keep
637
+ // utilities for complex types
638
+ #include <c10/util/complex_utils.h> // IWYU pragma: keep
639
+ #undef C10_INTERNAL_INCLUDE_COMPLEX_REMAINING_H