jasonfan commited on
Commit
dfcd861
·
verified ·
1 Parent(s): 6eee63f

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel.h +218 -0
  2. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel_impl.h +111 -0
  3. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction.h +346 -0
  4. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction_impl.h +395 -0
  5. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/OperatorKernel.h +32 -0
  6. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h +43 -0
  7. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h +46 -0
  8. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/boxing.h +415 -0
  9. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h +790 -0
  10. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/test_helpers.h +145 -0
  11. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/CppSignature.h +72 -0
  12. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/DispatchKeyExtractor.h +285 -0
  13. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h +955 -0
  14. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/ObservedOperators.h +22 -0
  15. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorEntry.h +342 -0
  16. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorOptions.h +35 -0
  17. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/RegistrationHandleRAII.h +41 -0
  18. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/adaption.h +86 -0
  19. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/infer_schema.h +162 -0
  20. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/op_allowlist.h +186 -0
  21. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/op_registration.h +599 -0
  22. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/qualified_name.h +166 -0
  23. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/rref_interface.h +46 -0
  24. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/stack.h +209 -0
  25. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/symbol.h +152 -0
  26. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/type_factory.h +113 -0
  27. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/type_ptr.h +59 -0
  28. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/typeid.h +6 -0
  29. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/FlushDenormal.h +19 -0
  30. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/Utils.h +38 -0
  31. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional.h +9 -0
  32. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_base.h +480 -0
  33. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_bfloat16.h +652 -0
  34. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/intrinsics.h +6 -0
  35. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/sve_helper.h +85 -0
  36. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_bfloat16.h +598 -0
  37. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_common_sve.h +241 -0
  38. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_double.h +622 -0
  39. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_float.h +760 -0
  40. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_int.h +504 -0
  41. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_qint.h +611 -0
  42. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec.h +62 -0
  43. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128.h +22 -0
  44. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_bfloat16_neon.h +703 -0
  45. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_convert.h +383 -0
  46. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_double_neon.h +591 -0
  47. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_float_neon.h +661 -0
  48. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_half_neon.h +627 -0
  49. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_int_aarch64.h +799 -0
  50. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_reduced_precision_common_neon.h +316 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel.h ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/boxing/OperatorKernel.h>
5
+ #include <c10/core/DispatchKeySet.h>
6
+ #include <c10/util/intrusive_ptr.h>
7
+
8
+ namespace c10 {
9
+
10
+ struct IValue;
11
+ using Stack = std::vector<IValue>;
12
+
13
+ class OperatorHandle;
14
+ class KernelFunction;
15
+
16
+ // This kernel implements the behavior of falling through to the next available
17
+ // registered dispatch key. The implementation of this function is FAST; it is
18
+ // no overhead to fallthrough to the next key. See cpp file for some more
19
+ // implementation notes; notably, this does NOT actually go through the
20
+ // boxing/unboxing codepath.
21
+ TORCH_API void fallthrough_kernel(
22
+ OperatorKernel* /*unused*/,
23
+ const OperatorHandle& /*unused*/,
24
+ DispatchKeySet /*unused*/,
25
+ Stack* /*unused*/);
26
+
27
+ // Note [Ambiguity in AutogradOther kernel]
28
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
+ // This error-reporting kernel is registered to the AutogradOther entry in the
30
+ // dispatch table when there is both a CompositeImplicitAutograd kernel and a
31
+ // backend kernel for ANY backend that maps to AutogradOther. To see why
32
+ // this is necessary in the AutogradOther case, it's helpful to first see
33
+ // why everything works out fine for a backend that has a reserved Autograd
34
+ // entry (see rule 2.2 in [Note] DispatchTable computation):
35
+ //
36
+ // CPU AutogradCPU
37
+ // reg? registers with...
38
+ // -------------------------------------------------
39
+ // y Autograd registration takes precedence
40
+ // over CompositeImplicitAutograd.
41
+ // This is good, because the CPU specific backend
42
+ // implementation is more specialized and typically better;
43
+ // if we used the composite, we would bypass it.
44
+ // (NB: the Autograd key is guaranteed to exist because
45
+ // the autograd codegen requires it!)
46
+ //
47
+ // n CompositeImplicitAutograd takes precedence.
48
+ // This is also good, because the Autograd
49
+ // registration (if it exists) would try to redispatch
50
+ // to the (non-existent) CPU implementation; by
51
+ // using the composite, we ensure the operator
52
+ // actually works.
53
+ //
54
+ // As you can see, when we have a specific Autograd key (AutogradCPU), we can
55
+ // decide whether or not to use the CompositeImplicitAutograd kernel or the
56
+ // Autograd kernel based on whether or not the backend kernel exists.
57
+ //
58
+ // However, for AutogradOther (which is the catchall autograd kernel for
59
+ // everything that doesn't have a specific Autograd key), we can't do this
60
+ // trick because there isn't any unique backend to peek at to disambiguate;
61
+ // if there are some backends that have implementations they prefer Autograd,
62
+ // but unimplemented backends would prefer CompositeImplicitAutograd. Rather
63
+ // than arbitrarily pick one or the other, we just register a kernel that raises
64
+ // an error and let the user decide how to proceed.
65
+ TORCH_API void ambiguous_autogradother_kernel(
66
+ OperatorKernel* /*unused*/,
67
+ const OperatorHandle& /*op*/,
68
+ DispatchKeySet /*unused*/,
69
+ Stack* /*unused*/);
70
+
71
+ // Note [named_not_supported_kernel]
72
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
+ // This kernel implements reporting an error message saying that named tensor is
74
+ // not supported. This kernel doesn't rely on the Stack, and so it is special
75
+ // cased in the dispatcher to be triggered before we attempt boxing (so we can
76
+ // give a good error message in cases when boxing is not supported). When
77
+ // boxing is universally supported this can be removed.
78
+ [[noreturn]] TORCH_API void named_not_supported_kernel(
79
+ OperatorKernel* /*unused*/,
80
+ const OperatorHandle& /*op*/,
81
+ DispatchKeySet /*unused*/,
82
+ Stack* /*unused*/);
83
+
84
+ /**
85
+ * BoxedKernel is similar to a std::function storing a boxed kernel.
86
+ */
87
+ class TORCH_API BoxedKernel final {
88
+ public:
89
+ // This is how boxed kernels are actually stored
90
+ //
91
+ // Note [Plumbing Keys Through The Dispatcher]
92
+ // Benchmarks have shown that it is expensive for the dispatcher to read from
93
+ // thread-local storage (TLS) upon every dispatch call into order to compute
94
+ // which kernel to dispatch to.
95
+ //
96
+ // To mitigate this, we've updated the calling convention inside the
97
+ // dispatcher to expect every kernel that it stores to have a first argument
98
+ // of type DispatchKeySet.
99
+ //
100
+ // What are the invariants of the DispatchKeySet when it gets passed to a
101
+ // kernel?
102
+ // - All keys to the left of the current dispatch key have been masked out.
103
+ // (e.g. a Tracing kernel that takes in the DispatchKeySet will expect the
104
+ // highest bit to be DispatchKey::Tracer)
105
+ // - All other keys that dispatcher normally would have computed through TLS +
106
+ // global state + op arguments
107
+ // are still in the set.
108
+ //
109
+ // Kernels can then opt into using this keyset to save the dispatcher from
110
+ // doing repeated work during redispatches: recalculating the highest-priority
111
+ // dispatch key, which involves reading from TLS. Instead, the kernels that
112
+ // opt in will calculate an updated DispatchKeySet directly from the old one,
113
+ // and pass the updated set directly into the dispatcher upon redispatching.
114
+ //
115
+ // This is an opt-in mechanism: Kernels can automatically opt in by setting
116
+ // the first argument in their signature to be of type DispatchKeySet. See the
117
+ // kernels in VariableTypeEverything.cpp and TraceTypeEverything.cpp for
118
+ // examples.
119
+ //
120
+ // The mechanism for optionally passing that DispatchKeySet into the kernel
121
+ // lives in make_boxed_from_unboxed_functor.h. See Note [Plumbing Keys Through
122
+ // The Dispatcher 2] for details.
123
+ using InternalBoxedKernelFunction =
124
+ void(OperatorKernel*, const OperatorHandle&, DispatchKeySet, Stack*);
125
+ // This is the public API for how boxed kernels are defined
126
+ using BoxedKernelFunction = void(const OperatorHandle&, Stack*);
127
+ using BoxedKernelFunction_withDispatchKeys =
128
+ void(const OperatorHandle&, DispatchKeySet, Stack*);
129
+
130
+ BoxedKernel();
131
+
132
+ // Fast path for dispatch to allow not touching the boxed kernel in
133
+ // the common case where unboxed is available.
134
+ bool isValid() const;
135
+ bool isFallthrough() const;
136
+
137
+ /**
138
+ * Call the function with boxed arguments.
139
+ */
140
+ void callBoxed(
141
+ const OperatorHandle& opHandle,
142
+ DispatchKeySet dispatchKeySet,
143
+ Stack* stack) const;
144
+
145
+ /**
146
+ * Create a KernelFunction from a boxed function.
147
+ *
148
+ * Example:
149
+ *
150
+ * > void boxed_func(OperatorKernel*, Stack* stack) {...}
151
+ * > BoxedFunction func = BoxedKernel::makeFromFunction<&boxed_func>();
152
+ */
153
+ template <BoxedKernelFunction* func>
154
+ static BoxedKernel makeFromFunction();
155
+
156
+ /**
157
+ * TODO: This will only be useful if we write a backend fallback that plumbs
158
+ * dispatch keys (currently there are none) See Note [Plumbing Keys Through
159
+ * The Dispatcher] for details.
160
+ */
161
+ template <BoxedKernelFunction_withDispatchKeys* func>
162
+ static BoxedKernel makeFromFunction();
163
+
164
+ /**
165
+ * Create a KernelFunction from a boxed functor.
166
+ *
167
+ * Example:
168
+ *
169
+ * > class MyFunctor final : public c10::OperatorKernel {
170
+ * > public:
171
+ * > void operator()(const OperatorHandle&, DispatchKeySet, Stack*) {...}
172
+ * > };
173
+ * > BoxedKernel func =
174
+ * BoxedKernel::makeFromFunctor(std::make_unique<MyFunctor>());
175
+ */
176
+ template <class KernelFunctor>
177
+ static BoxedKernel makeFromFunctor(
178
+ std::unique_ptr<KernelFunctor> kernelFunctor);
179
+
180
+ static BoxedKernel makeFallthrough();
181
+ static BoxedKernel makeAmbiguousAutogradOther();
182
+ static BoxedKernel makeNamedNotSupported();
183
+
184
+ private:
185
+ friend class KernelFunction;
186
+
187
+ template <BoxedKernelFunction* func>
188
+ static void make_boxed_function(
189
+ OperatorKernel* /*unused*/,
190
+ const OperatorHandle& opHandle,
191
+ DispatchKeySet /*unused*/,
192
+ Stack* stack);
193
+
194
+ template <BoxedKernelFunction_withDispatchKeys* func>
195
+ static void make_boxed_function(
196
+ OperatorKernel* /*unused*/,
197
+ const OperatorHandle& opHandle,
198
+ DispatchKeySet /*ks*/,
199
+ Stack* stack);
200
+
201
+ explicit BoxedKernel(
202
+ std::unique_ptr<OperatorKernel> functor,
203
+ InternalBoxedKernelFunction* boxed_kernel_func);
204
+
205
+ OperatorKernel* getFunctor() const;
206
+ InternalBoxedKernelFunction* getFnPtr() const;
207
+
208
+ c10::intrusive_ptr<OperatorKernel> functor_;
209
+ InternalBoxedKernelFunction* boxed_kernel_func_;
210
+ };
211
+
212
+ } // namespace c10
213
+
214
+ #include <ATen/core/boxing/BoxedKernel_impl.h>
215
+
216
+ #else
217
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
218
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel_impl.h ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ namespace c10 {
5
+
6
+ inline BoxedKernel::BoxedKernel() : boxed_kernel_func_(nullptr) {}
7
+
8
+ inline BoxedKernel::BoxedKernel(
9
+ std::unique_ptr<OperatorKernel> functor,
10
+ InternalBoxedKernelFunction* boxed_kernel_func)
11
+ : functor_(std::move(functor)), boxed_kernel_func_(boxed_kernel_func) {}
12
+
13
+ template <BoxedKernel::BoxedKernelFunction* func>
14
+ inline void BoxedKernel::make_boxed_function(
15
+ OperatorKernel* /*unused*/,
16
+ const OperatorHandle& opHandle,
17
+ DispatchKeySet /*unused*/,
18
+ Stack* stack) {
19
+ // Note that we're dropping the DispatchKeySet argument.
20
+ // See Note [Plumbing Keys Through The Dispatcher 2] for details.
21
+ func(opHandle, stack);
22
+ }
23
+
24
+ template <BoxedKernel::BoxedKernelFunction_withDispatchKeys* func>
25
+ inline void BoxedKernel::make_boxed_function(
26
+ OperatorKernel* /*unused*/,
27
+ const OperatorHandle& opHandle,
28
+ DispatchKeySet ks,
29
+ Stack* stack) {
30
+ // See Note [Plumbing Keys Through The Dispatcher 2] for details.
31
+ func(opHandle, ks, stack);
32
+ }
33
+
34
+ inline bool BoxedKernel::isValid() const {
35
+ return boxed_kernel_func_ != nullptr;
36
+ }
37
+
38
+ inline bool BoxedKernel::isFallthrough() const {
39
+ return boxed_kernel_func_ == &fallthrough_kernel;
40
+ }
41
+
42
+ inline void BoxedKernel::callBoxed(
43
+ const OperatorHandle& opHandle,
44
+ DispatchKeySet dispatchKeySet,
45
+ Stack* stack) const {
46
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
47
+ boxed_kernel_func_ != nullptr,
48
+ "Tried to call BoxedKernel::callBoxed() on an uninitialized BoxedKernel.");
49
+ (*boxed_kernel_func_)(functor_.get(), opHandle, dispatchKeySet, stack);
50
+ }
51
+
52
+ template <BoxedKernel::BoxedKernelFunction* func>
53
+ inline BoxedKernel BoxedKernel::makeFromFunction() {
54
+ return BoxedKernel(
55
+ nullptr, // no functor_ object
56
+ &make_boxed_function<func>);
57
+ }
58
+
59
+ template <BoxedKernel::BoxedKernelFunction_withDispatchKeys* func>
60
+ inline BoxedKernel BoxedKernel::makeFromFunction() {
61
+ return BoxedKernel(
62
+ nullptr, // no functor_ object
63
+ &make_boxed_function<func>);
64
+ }
65
+
66
+ inline BoxedKernel BoxedKernel::makeFallthrough() {
67
+ return BoxedKernel(
68
+ nullptr, // no functor_ object
69
+ &fallthrough_kernel);
70
+ }
71
+
72
+ inline BoxedKernel BoxedKernel::makeAmbiguousAutogradOther() {
73
+ return BoxedKernel(
74
+ nullptr, // no functor_ object
75
+ &ambiguous_autogradother_kernel);
76
+ }
77
+
78
+ inline BoxedKernel BoxedKernel::makeNamedNotSupported() {
79
+ return BoxedKernel(
80
+ nullptr, // no functor_ object
81
+ &named_not_supported_kernel);
82
+ }
83
+
84
+ template <class KernelFunctor>
85
+ inline BoxedKernel BoxedKernel::makeFromFunctor(
86
+ std::unique_ptr<KernelFunctor> kernelFunctor) {
87
+ static_assert(
88
+ std::is_base_of_v<OperatorKernel, KernelFunctor>,
89
+ "Tried to call BoxedKernel::makeFromFunctor<KernelFunctor>, but the functor doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
90
+ return BoxedKernel(
91
+ std::move(kernelFunctor),
92
+ [](OperatorKernel* kernel,
93
+ const OperatorHandle& op,
94
+ DispatchKeySet ks,
95
+ Stack* stack) {
96
+ (*static_cast<KernelFunctor*>(kernel))(op, ks, stack);
97
+ });
98
+ }
99
+
100
+ inline OperatorKernel* BoxedKernel::getFunctor() const {
101
+ return functor_.get();
102
+ }
103
+ inline BoxedKernel::InternalBoxedKernelFunction* BoxedKernel::getFnPtr() const {
104
+ return boxed_kernel_func_;
105
+ }
106
+
107
+ } // namespace c10
108
+
109
+ #else
110
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
111
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction.h ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/ATen_fwd.h>
5
+ #include <ATen/core/boxing/BoxedKernel.h>
6
+ #include <ATen/core/stack.h>
7
+ #include <c10/core/DispatchKeySet.h>
8
+ #include <c10/util/TypeList.h>
9
+ #include <c10/util/intrusive_ptr.h>
10
+ #include <atomic>
11
+ #include <memory>
12
+ #include <type_traits>
13
+
14
+ namespace c10 {
15
+
16
+ using Stack = torch::jit::Stack; // TODO Instead of this, move torch::jit::Stack
17
+ // to the c10 namespace.
18
+
19
+ class OperatorHandle;
20
+ struct OperatorKernel;
21
+ class KernelFunction;
22
+
23
+ class KernelToken;
24
+ class SafeKernelFunction;
25
+
26
+ template <typename T>
27
+ using has_symint = std::disjunction<
28
+ std::is_same<c10::SymInt, T>,
29
+ std::is_same<c10::SymIntArrayRef, T>,
30
+ std::is_same<at::OptionalSymIntArrayRef, T>,
31
+ std::is_same<std::optional<c10::SymInt>, T>>;
32
+
33
+ template <typename T>
34
+ struct remove_symint {
35
+ using type = T;
36
+ };
37
+
38
+ template <>
39
+ struct remove_symint<c10::SymInt> {
40
+ using type = int64_t;
41
+ };
42
+
43
+ template <>
44
+ struct remove_symint<at::OptionalSymIntArrayRef> {
45
+ using type = OptionalIntArrayRef;
46
+ };
47
+
48
+ template <>
49
+ struct remove_symint<c10::SymIntArrayRef> {
50
+ using type = c10::IntArrayRef;
51
+ };
52
+
53
+ template <>
54
+ struct remove_symint<std::optional<c10::SymInt>> {
55
+ using type = std::optional<int64_t>;
56
+ };
57
+
58
+ template <bool symint, typename T>
59
+ struct maybe_keep_symint final {};
60
+
61
+ template <typename T>
62
+ struct maybe_keep_symint<true, T> {
63
+ using type = T;
64
+ };
65
+
66
+ template <typename T>
67
+ struct maybe_keep_symint<false, T> {
68
+ using type = typename remove_symint<T>::type;
69
+ };
70
+
71
+ template <typename T>
72
+ using fn_has_symint = typename guts::typelist::true_for_any_type<
73
+ has_symint,
74
+ typename guts::infer_function_traits<T>::type::parameter_types>;
75
+
76
+ template <typename T>
77
+ struct fn_remove_symint;
78
+
79
+ template <typename Ret, typename... Args>
80
+ struct fn_remove_symint<Ret(Args...)> {
81
+ using type = Ret(typename remove_symint<Args>::type...);
82
+ };
83
+
84
+ /**
85
+ * KernelFunction is similar to std::function but stores a kernel function.
86
+ * You can create a KernelFunction from a boxed or unboxed
87
+ * function/functor/lambda and call it in a boxed or unboxed way. If the way it
88
+ * was created doesn't match the way it was called, it will do boxing or
89
+ * unboxing as necessary.
90
+ */
91
+ class TORCH_API KernelFunction final {
92
+ public:
93
+ using InternalBoxedKernelFunction = BoxedKernel::InternalBoxedKernelFunction;
94
+ using BoxedKernelFunction = BoxedKernel::BoxedKernelFunction;
95
+ using BoxedKernelFunction_withDispatchKeys =
96
+ BoxedKernel::BoxedKernelFunction_withDispatchKeys;
97
+
98
+ KernelFunction();
99
+ ~KernelFunction();
100
+
101
+ KernelFunction(const KernelFunction& other);
102
+ KernelFunction& operator=(const KernelFunction& other);
103
+
104
+ KernelFunction(KernelFunction&&) noexcept = default;
105
+
106
+ // Fast path for dispatch to allow not touching the boxed kernel in
107
+ // the common case where unboxed is available.
108
+ bool isValidUnboxed() const;
109
+ bool isValidSymUnboxed() const;
110
+ bool isValid() const;
111
+ bool isFallthrough() const;
112
+
113
+ /**
114
+ * Call the function in a boxed way.
115
+ * If the kernel function was created with an unboxed function,
116
+ * this will call an unboxing wrapper which then calls into that
117
+ * unboxed function.
118
+ *
119
+ * Example:
120
+ *
121
+ * > void boxed_func(OperatorKernel*, Stack* stack) {...}
122
+ * > KernelFunction func = KernelFunction::makeFromBoxedFunction(&boxed_func);
123
+ * > Tensor result = func.callBoxed(stack);
124
+ *
125
+ * Or, with an unboxed implementation:
126
+ *
127
+ * > KernelFunction func = KernelFunction::makeFromUnboxedLambda(
128
+ * > [] (Tensor a, bool b) -> Tensor {...});
129
+ * > Tensor result = func.callBoxed(stack);
130
+ */
131
+ void callBoxed(
132
+ const OperatorHandle& opHandle,
133
+ DispatchKeySet dispatchKeySet,
134
+ Stack* stack) const;
135
+
136
+ /**
137
+ * Call the function in an unboxed way.
138
+ * If the kernel function was created with a boxed function,
139
+ * this will box all inputs and then call into that boxed function.
140
+ *
141
+ * Note that this doesn't work for all types yet.
142
+ *
143
+ * Example:
144
+ *
145
+ * > KernelFunction func = KernelFunction::makeFromUnboxedLambda(
146
+ * > [] (Tensor a, bool b) -> Tensor {...});
147
+ * > Tensor result = func.call<Tensor, Tensor, bool>(tensor1, true);
148
+ *
149
+ * Or, with a boxed implementation:
150
+ *
151
+ * > void boxed_func(OperatorKernel*, Stack* stack) {...}
152
+ * > KernelFunction func = KernelFunction::makeFromBoxedFunction(&boxed_func);
153
+ * > Tensor result = func.call<Tensor, Tensor, bool>(tensor1, true);
154
+ */
155
+ template <class Return, class... Args>
156
+ Return call(
157
+ const OperatorHandle& opHandle,
158
+ DispatchKeySet dispatchKeySet,
159
+ Args... args) const;
160
+
161
+ /**
162
+ * Create a KernelFunction from a BoxedKernel.
163
+ */
164
+ static KernelFunction makeFromBoxedKernel(BoxedKernel boxed_fn);
165
+
166
+ /**
167
+ * Create a KernelFunction from a boxed function.
168
+ *
169
+ * Example:
170
+ *
171
+ * > void boxed_func(OperatorKernel*, Stack* stack) {...}
172
+ * > KernelFunction func =
173
+ * KernelFunction::makeFromBoxedFunction<&boxed_func>();
174
+ */
175
+ template <BoxedKernelFunction* func>
176
+ static KernelFunction makeFromBoxedFunction();
177
+
178
+ /**
179
+ * TODO: This will only be useful if we write a backend fallback that plumbs
180
+ * dispatch keys (currently there are none) See Note [Plumbing Keys Through
181
+ * The Dispatcher] for details.
182
+ */
183
+ template <BoxedKernelFunction_withDispatchKeys* func>
184
+ static KernelFunction makeFromBoxedFunction();
185
+
186
+ /**
187
+ * Create a KernelFunction from an unboxed functor.
188
+ *
189
+ * Example:
190
+ *
191
+ * > class MyFunctor final : public c10::OperatorKernel {
192
+ * > public:
193
+ * > Tensor operator()(Tensor a, Tensor b) {...}
194
+ * > };
195
+ * > KernelFunction func =
196
+ * KernelFunction::makeFromUnboxedFunctor<MyFunctor>(std::make_unique<MyFunctor>());
197
+ */
198
+ template <bool AllowLegacyTypes = false, class KernelFunctor>
199
+ static KernelFunction makeFromUnboxedFunctor(
200
+ std::unique_ptr<OperatorKernel> kernelFunctor);
201
+
202
+ /**
203
+ * Create a KernelFunction from a boxed functor.
204
+ *
205
+ * Example:
206
+ *
207
+ * > class MyFunctor final : public c10::OperatorKernel {
208
+ * > public:
209
+ * > void operator()(const OperatorHandle&, DispatchKeySet, Stack*) {...}
210
+ * > };
211
+ * > KernelFunction func =
212
+ * KernelFunction::makeFromBoxedFunctor(std::make_unique<MyFunctor>());
213
+ */
214
+ template <class KernelFunctor>
215
+ static KernelFunction makeFromBoxedFunctor(
216
+ std::unique_ptr<KernelFunctor> kernelFunctor);
217
+
218
+ /**
219
+ * Create a KernelFunction from an unboxed function.
220
+ * This is usually better than KernelFunction::makeFromUnboxedRuntimeFunction
221
+ * because knowing the function pointer as a template argument (i.e. at
222
+ * compile time) allows the compiler to inline the function into its
223
+ * unboxing wrapper and yields better performance when calling the function.
224
+ *
225
+ * Example:
226
+ *
227
+ * > Tensor unboxed_func(Tensor a, Tensor b) {...}
228
+ * > KernelFunction func =
229
+ * KernelFunction::makeFromUnboxedFunction<decltype(unboxed_func),
230
+ * &unboxed_func>();
231
+ */
232
+ template <class FuncPtr, bool AllowLegacyTypes = false>
233
+ static KernelFunction makeFromUnboxedFunction(FuncPtr /*func_ptr*/);
234
+
235
+ /**
236
+ * Create a KernelFunction from an unboxed function.
237
+ * KernelFunction::makeFromUnboxedFunction is usually a better choice than
238
+ * this if you know the function pointer at compile time, see doc comment
239
+ * there for an explanation.
240
+ *
241
+ * Example:
242
+ *
243
+ * > Tensor unboxed_func(Tensor a, Tensor b) {...}
244
+ * > KernelFunction func =
245
+ * KernelFunction::makeFromUnboxedRuntimeFunction(&unboxed_func);
246
+ */
247
+ template <bool AllowLegacyTypes = false, class FuncType>
248
+ static KernelFunction makeFromUnboxedRuntimeFunction(FuncType* func);
249
+
250
+ static KernelFunction makeFallthrough();
251
+ static KernelFunction makeAmbiguousAutogradOther();
252
+ static KernelFunction makeNamedNotSupported();
253
+
254
+ /**
255
+ * Create a KernelFunction from an unboxed lambda.
256
+ *
257
+ * Example:
258
+ *
259
+ * > KernelFunction func = KernelFunction::makeFromUnboxedLambda(
260
+ * > [] (Tensor a, bool b) -> Tensor {...});
261
+ */
262
+ template <bool AllowLegacyTypes = false, class Lambda>
263
+ static std::enable_if_t<
264
+ guts::is_stateless_lambda<std::decay_t<Lambda>>::value,
265
+ KernelFunction>
266
+ makeFromUnboxedLambda(Lambda&& lambda);
267
+ template <bool AllowLegacyTypes = false, class Lambda>
268
+ static std::enable_if_t<
269
+ !guts::is_stateless_lambda<std::decay_t<Lambda>>::value,
270
+ KernelFunction>
271
+ makeFromUnboxedLambda(Lambda&& lambda);
272
+
273
+ std::string dumpState() const;
274
+ // For testing internal invariants only
275
+ bool _equalsBoxedAndUnboxed(const KernelFunction& /*other*/) const;
276
+
277
+ // Register a token to be invalidated when this KernelFunction is destroyed
278
+ void registerToken(std::weak_ptr<KernelToken> token) const;
279
+
280
+ private:
281
+ explicit KernelFunction(
282
+ std::unique_ptr<OperatorKernel> functor,
283
+ InternalBoxedKernelFunction* boxed_kernel_func,
284
+ void* unboxed_kernel_func,
285
+ void* sym_unboxed_kernel_func);
286
+ explicit KernelFunction(
287
+ BoxedKernel boxed_fn,
288
+ void* unboxed_kernel_func,
289
+ void* sym_unboxed_kernel_func);
290
+
291
+ BoxedKernel boxed_kernel_func_;
292
+ void* unboxed_kernel_func_;
293
+ void* sym_unboxed_kernel_func_;
294
+ // List of tokens that need to be invalidated when this KernelFunction is
295
+ // destroyed (lazy allocation to save memory when empty)
296
+ mutable std::unique_ptr<std::vector<std::weak_ptr<KernelToken>>> tokens_;
297
+ };
298
+
299
+ // Token held by SafeKernelFunction that gets invalidated when KernelFunction is
300
+ // destroyed
301
+ class KernelToken {
302
+ public:
303
+ bool isValid() const;
304
+ void invalidate();
305
+
306
+ private:
307
+ std::atomic<bool> invalid_{false};
308
+ };
309
+
310
+ class SafeKernelFunction {
311
+ public:
312
+ SafeKernelFunction(
313
+ const KernelFunction* kernel,
314
+ std::string debug,
315
+ std::shared_ptr<OperatorHandle> opHandle);
316
+
317
+ // Safe callBoxed - checks token validity first
318
+ void callBoxed(
319
+ const OperatorHandle& opHandle,
320
+ DispatchKeySet dispatchKeySet,
321
+ Stack* stack) const;
322
+
323
+ // Get debug information
324
+ const std::string& debug() const {
325
+ return debug_;
326
+ }
327
+
328
+ // Get the OpHandle that lives on this SafeKernelFunction
329
+ const OperatorHandle& opHandle() const {
330
+ return *opHandle_;
331
+ }
332
+
333
+ private:
334
+ KernelFunction kernel_;
335
+ std::shared_ptr<KernelToken> token_;
336
+ std::string debug_;
337
+ std::shared_ptr<OperatorHandle> opHandle_;
338
+ };
339
+
340
+ } // namespace c10
341
+
342
+ #include <ATen/core/boxing/KernelFunction_impl.h>
343
+
344
+ #else
345
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
346
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction_impl.h ADDED
@@ -0,0 +1,395 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #include <ATen/core/boxing/impl/WrapFunctionIntoFunctor.h>
3
+ #include <ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h>
4
+ #include <ATen/core/boxing/impl/boxing.h>
5
+ #include <ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h>
6
+
7
+ #include <c10/util/C++17.h>
8
+ #include <type_traits>
9
+
10
+ namespace c10 {
11
+
12
+ namespace detail {
13
+ template <typename Base, typename Child, typename... Args>
14
+ std::enable_if_t<
15
+ !std::is_array_v<Base> && !std::is_array_v<Child> &&
16
+ std::is_base_of_v<Base, Child>,
17
+ std::unique_ptr<Base>>
18
+ make_unique_base(Args&&... args) {
19
+ return std::make_unique<Child>(std::forward<Args>(args)...);
20
+ }
21
+ } // namespace detail
22
+
23
+ inline KernelFunction::KernelFunction()
24
+ : unboxed_kernel_func_(nullptr), sym_unboxed_kernel_func_(nullptr) {}
25
+
26
+ inline KernelFunction::~KernelFunction() {
27
+ if (tokens_) {
28
+ for (auto& weak_token : *tokens_) {
29
+ if (auto token = weak_token.lock()) {
30
+ token->invalidate();
31
+ }
32
+ }
33
+ }
34
+ }
35
+
36
+ inline KernelFunction::KernelFunction(const KernelFunction& other)
37
+ : boxed_kernel_func_(other.boxed_kernel_func_),
38
+ unboxed_kernel_func_(other.unboxed_kernel_func_),
39
+ sym_unboxed_kernel_func_(other.sym_unboxed_kernel_func_) {
40
+ // tokens_ is intentionally not copied as we only care about invalidating
41
+ // tokens if the original KernelFunction is destroyed
42
+ }
43
+
44
+ inline KernelFunction& KernelFunction::operator=(const KernelFunction& other) {
45
+ if (this != &other) {
46
+ boxed_kernel_func_ = other.boxed_kernel_func_;
47
+ unboxed_kernel_func_ = other.unboxed_kernel_func_;
48
+ sym_unboxed_kernel_func_ = other.sym_unboxed_kernel_func_;
49
+
50
+ // tokens_ is intentionally not copied as we only care about invalidating
51
+ // tokens if the original KernelFunction is destroyed
52
+ }
53
+ return *this;
54
+ }
55
+
56
+ inline KernelFunction::KernelFunction(
57
+ std::unique_ptr<OperatorKernel> functor,
58
+ InternalBoxedKernelFunction* boxed_kernel_func,
59
+ void* unboxed_kernel_func,
60
+ void* sym_unboxed_kernel_func = nullptr)
61
+ : boxed_kernel_func_(std::move(functor), boxed_kernel_func),
62
+ unboxed_kernel_func_(unboxed_kernel_func),
63
+ sym_unboxed_kernel_func_(sym_unboxed_kernel_func) {}
64
+
65
+ inline KernelFunction::KernelFunction(
66
+ BoxedKernel boxed_fn,
67
+ void* unboxed_kernel_func,
68
+ void* sym_unboxed_kernel_func = nullptr)
69
+ : boxed_kernel_func_(std::move(boxed_fn)),
70
+ unboxed_kernel_func_(unboxed_kernel_func),
71
+ sym_unboxed_kernel_func_(sym_unboxed_kernel_func) {}
72
+
73
+ inline bool KernelFunction::isValidUnboxed() const {
74
+ return unboxed_kernel_func_ != nullptr;
75
+ }
76
+
77
+ inline bool KernelFunction::isValidSymUnboxed() const {
78
+ return sym_unboxed_kernel_func_ != nullptr;
79
+ }
80
+
81
+ inline bool KernelFunction::isValid() const {
82
+ return boxed_kernel_func_.isValid();
83
+ }
84
+
85
+ inline bool KernelFunction::isFallthrough() const {
86
+ return boxed_kernel_func_.isFallthrough();
87
+ }
88
+
89
+ inline void KernelFunction::callBoxed(
90
+ const OperatorHandle& opHandle,
91
+ DispatchKeySet dispatchKeySet,
92
+ Stack* stack) const {
93
+ boxed_kernel_func_.callBoxed(opHandle, dispatchKeySet, stack);
94
+ }
95
+
96
+ template <class Return, class... Args>
97
+ inline Return callUnboxedKernelFunction(
98
+ void* unboxed_kernel_func,
99
+ OperatorKernel* functor,
100
+ DispatchKeySet dispatchKeySet,
101
+ Args&&... args) {
102
+ using ActualSignature = Return(OperatorKernel*, DispatchKeySet, Args...);
103
+ ActualSignature* func =
104
+ reinterpret_cast<ActualSignature*>(unboxed_kernel_func);
105
+ return (*func)(functor, dispatchKeySet, std::forward<Args>(args)...);
106
+ }
107
+
108
+ // This template requires you to explicitly specify the argument you want to
109
+ // forward; it doesn't work if you try to deduce it
110
+ // NB: keep this in sync with cloneWithRealTypes in function_schema.cpp
111
+
112
+ template <typename T>
113
+ inline typename remove_symint<T>::type unpackSymInt(T x) {
114
+ return x;
115
+ }
116
+
117
+ template <>
118
+ inline remove_symint<c10::SymInt>::type unpackSymInt(c10::SymInt x) {
119
+ return x.guard_int(__FILE__, __LINE__);
120
+ }
121
+
122
+ template <>
123
+ inline remove_symint<c10::SymIntArrayRef>::type unpackSymInt(
124
+ c10::SymIntArrayRef x) {
125
+ return C10_AS_INTARRAYREF_SLOW(x);
126
+ }
127
+
128
+ template <>
129
+ inline remove_symint<std::optional<c10::SymInt>>::type unpackSymInt(
130
+ std::optional<c10::SymInt> x) {
131
+ return x.has_value() ? std::make_optional(x->guard_int(__FILE__, __LINE__))
132
+ : std::nullopt;
133
+ }
134
+
135
+ template <>
136
+ inline remove_symint<at::OptionalSymIntArrayRef>::type unpackSymInt(
137
+ at::OptionalSymIntArrayRef x) {
138
+ return x.has_value() ? std::make_optional(C10_AS_INTARRAYREF_SLOW(*x))
139
+ : std::nullopt;
140
+ }
141
+
142
+ template <class Return, class... Args>
143
+ C10_ALWAYS_INLINE Return KernelFunction::call(
144
+ const OperatorHandle& opHandle,
145
+ DispatchKeySet dispatchKeySet,
146
+ Args... args) const {
147
+ // note: Args above is intentionally not Args&&. We don't want perfect
148
+ // forwarding, which would require Args to be deduced, but instead we
149
+ // want callers to explicitly specify the Args.
150
+
151
+ if constexpr (std::disjunction_v<has_symint<Args>...>) {
152
+ if (sym_unboxed_kernel_func_ != nullptr) {
153
+ auto* functor = boxed_kernel_func_.getFunctor();
154
+ return callUnboxedKernelFunction<Return, Args...>(
155
+ sym_unboxed_kernel_func_,
156
+ functor,
157
+ dispatchKeySet,
158
+ std::forward<Args>(args)...);
159
+ }
160
+
161
+ if (unboxed_kernel_func_ != nullptr) {
162
+ auto* functor = boxed_kernel_func_.getFunctor();
163
+ return callUnboxedKernelFunction<
164
+ Return,
165
+ typename remove_symint<Args>::type...>(
166
+ unboxed_kernel_func_,
167
+ functor,
168
+ dispatchKeySet,
169
+ unpackSymInt<Args>(args)...);
170
+ }
171
+ } else {
172
+ if (C10_LIKELY(unboxed_kernel_func_ != nullptr)) {
173
+ auto* functor = boxed_kernel_func_.getFunctor();
174
+ return callUnboxedKernelFunction<Return, Args...>(
175
+ unboxed_kernel_func_,
176
+ functor,
177
+ dispatchKeySet,
178
+ std::forward<Args>(args)...);
179
+ }
180
+ }
181
+
182
+ return impl::BoxedKernelWrapper<Return(Args...)>::call(
183
+ boxed_kernel_func_,
184
+ opHandle,
185
+ dispatchKeySet,
186
+ std::forward<Args>(args)...);
187
+ }
188
+
189
+ inline void KernelFunction::registerToken(
190
+ std::weak_ptr<KernelToken> token) const {
191
+ if (!tokens_) {
192
+ tokens_ = std::make_unique<std::vector<std::weak_ptr<KernelToken>>>();
193
+ }
194
+ tokens_->push_back(std::move(token));
195
+ }
196
+
197
+ inline KernelFunction KernelFunction::makeFromBoxedKernel(
198
+ BoxedKernel boxed_fn) {
199
+ return KernelFunction(
200
+ std::move(boxed_fn), nullptr); // no unboxed function pointer
201
+ }
202
+
203
+ template <KernelFunction::BoxedKernelFunction* func>
204
+ inline KernelFunction KernelFunction::makeFromBoxedFunction() {
205
+ return KernelFunction::makeFromBoxedKernel(
206
+ BoxedKernel::makeFromFunction<func>());
207
+ }
208
+
209
+ template <KernelFunction::BoxedKernelFunction_withDispatchKeys* func>
210
+ inline KernelFunction KernelFunction::makeFromBoxedFunction() {
211
+ return KernelFunction::makeFromBoxedKernel(
212
+ BoxedKernel::makeFromFunction<func>());
213
+ }
214
+
215
+ inline KernelFunction KernelFunction::makeFallthrough() {
216
+ return KernelFunction::makeFromBoxedKernel(BoxedKernel::makeFallthrough());
217
+ }
218
+
219
+ inline KernelFunction KernelFunction::makeAmbiguousAutogradOther() {
220
+ return KernelFunction::makeFromBoxedKernel(
221
+ BoxedKernel::makeAmbiguousAutogradOther());
222
+ }
223
+
224
+ inline KernelFunction KernelFunction::makeNamedNotSupported() {
225
+ return KernelFunction::makeFromBoxedKernel(
226
+ BoxedKernel::makeNamedNotSupported());
227
+ }
228
+
229
+ template <bool AllowLegacyTypes, class KernelFunctor>
230
+ inline KernelFunction KernelFunction::makeFromUnboxedFunctor(
231
+ std::unique_ptr<OperatorKernel> kernelFunctor) {
232
+ #ifndef NDEBUG
233
+ // This assertion is costly for build time so it's debug-gated.
234
+ static_assert(
235
+ guts::is_functor<KernelFunctor>::value,
236
+ "Tried to call KernelFunction::makeFromUnboxedFunctor<KernelFunctor> but the argument is not a functor.");
237
+ #endif
238
+ static_assert(
239
+ std::is_base_of_v<OperatorKernel, KernelFunctor>,
240
+ "Tried to call KernelFunction::makeFromUnboxedFunctor<KernelFunctor>, but the functor doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
241
+
242
+ auto* unboxed_fn = &impl::wrap_kernel_functor_unboxed<KernelFunctor>::call;
243
+ void* void_unboxed_fn = reinterpret_cast<void*>(unboxed_fn);
244
+ bool is_symint = fn_has_symint<decltype(unboxed_fn)>::value;
245
+ return KernelFunction(
246
+ std::move(kernelFunctor),
247
+ &impl::make_boxed_from_unboxed_functor<KernelFunctor, AllowLegacyTypes>::
248
+ call,
249
+ is_symint ? nullptr : void_unboxed_fn,
250
+ is_symint ? void_unboxed_fn : nullptr);
251
+ }
252
+
253
+ template <class KernelFunctor>
254
+ inline KernelFunction KernelFunction::makeFromBoxedFunctor(
255
+ std::unique_ptr<KernelFunctor> kernelFunctor) {
256
+ return KernelFunction::makeFromBoxedKernel(
257
+ BoxedKernel::makeFromFunctor(std::move(kernelFunctor)));
258
+ }
259
+
260
+ template <class FuncPtr, bool AllowLegacyTypes>
261
+ inline KernelFunction KernelFunction::makeFromUnboxedFunction(
262
+ FuncPtr func_ptr) {
263
+ static_assert(
264
+ is_compile_time_function_pointer<FuncPtr>::value,
265
+ "Tried to call KernelFunction::makeFromUnboxedFunction with an invalid parameter. It must be a function pointer created with TORCH_FN.");
266
+ static_assert(
267
+ !std::is_same_v<typename FuncPtr::FuncType, BoxedKernelFunction>,
268
+ "Tried to call KernelFunction::makeFromUnboxedFunction with a boxed function pointer. Please use KernelFunction::makeFromBoxedFunction instead.");
269
+ #if defined(__GNUC__) && defined(__SANITIZE_ADDRESS__) && !defined(__CUDACC__)
270
+ TORCH_INTERNAL_ASSERT(
271
+ FuncPtr::func_ptr() != nullptr, "Kernel function cannot be nullptr");
272
+ #else
273
+ static_assert(
274
+ FuncPtr::func_ptr() != nullptr, "Kernel function cannot be nullptr");
275
+ #endif
276
+
277
+ #if !defined(C10_MOBILE)
278
+ (void)func_ptr; // Suppress unused variable warning
279
+ return makeFromUnboxedFunctor<
280
+ AllowLegacyTypes,
281
+ typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>(
282
+ detail::make_unique_base<
283
+ OperatorKernel,
284
+ typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>());
285
+ #else
286
+ // On mobile, we rather want to optimize for binary size than for performance,
287
+ // so let's not inline the kernel into the wrapper but use
288
+ // makeFromUnboxedRuntimeFunction instead.
289
+ return makeFromUnboxedRuntimeFunction(func_ptr.func_ptr());
290
+ #endif
291
+ }
292
+
293
+ template <bool AllowLegacyTypes, class FuncType>
294
+ inline KernelFunction KernelFunction::makeFromUnboxedRuntimeFunction(
295
+ FuncType* func) {
296
+ static_assert(
297
+ guts::is_function_type<FuncType>::value,
298
+ "Tried to call KernelFunction::makeFromUnboxedRuntimeFunction with a non-function type.");
299
+ static_assert(
300
+ !std::is_same_v<FuncType, BoxedKernelFunction>,
301
+ "Tried to call KernelFunction::makeFromUnboxedRuntimeFunction with a boxed function pointer. Please use KernelFunction::makeFromBoxedFunction instead.");
302
+ TORCH_INTERNAL_ASSERT(func != nullptr, "Kernel function cannot be nullptr");
303
+
304
+ return makeFromUnboxedFunctor<
305
+ AllowLegacyTypes,
306
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>(
307
+ detail::make_unique_base<
308
+ OperatorKernel,
309
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>(func));
310
+ }
311
+
312
+ template <bool AllowLegacyTypes, class Lambda>
313
+ inline std::enable_if_t<
314
+ guts::is_stateless_lambda<std::decay_t<Lambda>>::value,
315
+ KernelFunction>
316
+ KernelFunction::makeFromUnboxedLambda(Lambda&& lambda) {
317
+ static_assert(
318
+ guts::is_functor<std::decay_t<Lambda>>::value,
319
+ "Tried to call KernelFunction::makeFromUnboxedLambda with a non-lambda type.");
320
+
321
+ #if !defined(C10_MOBILE)
322
+ return makeFromUnboxedFunctor<
323
+ AllowLegacyTypes,
324
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
325
+ detail::make_unique_base<
326
+ OperatorKernel,
327
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
328
+ std::forward<Lambda>(lambda)));
329
+ #else
330
+ // On mobile, we rather want to optimize for binary size than for performance,
331
+ // so let's not inline the kernel into the wrapper but use
332
+ // makeFromUnboxedRuntimeFunction instead.
333
+ using FuncType =
334
+ typename guts::infer_function_traits_t<std::decay_t<Lambda>>::func_type;
335
+ return makeFromUnboxedRuntimeFunction<AllowLegacyTypes, FuncType>(lambda);
336
+ #endif
337
+ }
338
+
339
+ template <bool AllowLegacyTypes, class Lambda>
340
+ inline std::enable_if_t<
341
+ !guts::is_stateless_lambda<std::decay_t<Lambda>>::value,
342
+ KernelFunction>
343
+ KernelFunction::makeFromUnboxedLambda(Lambda&& lambda) {
344
+ static_assert(
345
+ guts::is_functor<std::decay_t<Lambda>>::value,
346
+ "Tried to call KernelFunction::makeFromUnboxedLambda with a non-lambda type.");
347
+
348
+ return makeFromUnboxedFunctor<
349
+ AllowLegacyTypes,
350
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
351
+ detail::make_unique_base<
352
+ OperatorKernel,
353
+ impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
354
+ std::forward<Lambda>(lambda)));
355
+ }
356
+
357
+ inline bool KernelToken::isValid() const {
358
+ return !invalid_.load(std::memory_order_acquire);
359
+ }
360
+
361
+ inline void KernelToken::invalidate() {
362
+ invalid_.store(true, std::memory_order_release);
363
+ }
364
+
365
+ inline SafeKernelFunction::SafeKernelFunction(
366
+ const KernelFunction* kernel,
367
+ std::string debug,
368
+ std::shared_ptr<OperatorHandle> opHandle)
369
+ : kernel_(kernel ? *kernel : KernelFunction()),
370
+ token_(std::make_shared<KernelToken>()),
371
+ debug_(std::move(debug)),
372
+ opHandle_(std::move(opHandle)) {
373
+ // Register the token with the original kernel so it gets invalidated when the
374
+ // kernel is destroyed
375
+ if (kernel) {
376
+ kernel->registerToken(token_);
377
+ }
378
+ }
379
+
380
+ inline void SafeKernelFunction::callBoxed(
381
+ const OperatorHandle& opHandle,
382
+ DispatchKeySet dispatchKeySet,
383
+ Stack* stack) const {
384
+ TORCH_CHECK(
385
+ token_ && token_->isValid(),
386
+ "SafeKernelFunction has been invalidated ",
387
+ debug_);
388
+ kernel_.callBoxed(opHandle, dispatchKeySet, stack);
389
+ }
390
+
391
+ } // namespace c10
392
+
393
+ #else
394
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
395
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/OperatorKernel.h ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ #include <c10/util/intrusive_ptr.h>
4
+
5
+ namespace c10 {
6
+
7
+ /**
8
+ * Inherit from OperatorKernel to implement a c10 kernel.
9
+ *
10
+ * Example:
11
+ * > namespace {
12
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
13
+ * > public:
14
+ * > Tensor operator()(Tensor a, Tensor b) {...}
15
+ * > };
16
+ * > }
17
+ *
18
+ * The kernel class is allowed to have members but these are equivalent
19
+ * to global variables. The kernel implementation is responsible for
20
+ * preventing race conditions on them.
21
+ *
22
+ * See below for how to register this kernel with PyTorch.
23
+ */
24
+ struct TORCH_API OperatorKernel : public c10::intrusive_ptr_target {
25
+ ~OperatorKernel() override = default;
26
+ };
27
+
28
+ } // namespace c10
29
+
30
+ #else
31
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
32
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoFunctor.h ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/core/CompileTimeFunctionPointer.h>
5
+
6
+ namespace c10::impl {
7
+ namespace detail {
8
+ template <class FuncPtr, class ReturnType, class ParameterList>
9
+ class WrapFunctionIntoFunctor_ {};
10
+ template <class FuncPtr, class ReturnType, class... Parameters>
11
+ class WrapFunctionIntoFunctor_<
12
+ FuncPtr,
13
+ ReturnType,
14
+ guts::typelist::typelist<Parameters...>>
15
+ final : public c10::OperatorKernel {
16
+ public:
17
+ C10_ALWAYS_INLINE decltype(auto) operator()(Parameters... args) {
18
+ return (*FuncPtr::func_ptr())(std::forward<Parameters>(args)...);
19
+ }
20
+ };
21
+ } // namespace detail
22
+
23
+ // WrapFunctionIntoFunctor: Wraps a compile time function pointer into a kernel
24
+ // functor. Since it is a compile time function pointer, many compilers can
25
+ // inline it into the wrapper and you don't get any performance overhead for
26
+ // wrapping.
27
+ template <class FuncPtr>
28
+ struct WrapFunctionIntoFunctor final {
29
+ static_assert(
30
+ c10::is_compile_time_function_pointer<FuncPtr>::value,
31
+ "WrapFunctionIntoFunctor can only wrap functions created with TORCH_FN.");
32
+ using type = detail::WrapFunctionIntoFunctor_<
33
+ FuncPtr,
34
+ typename guts::function_traits<typename FuncPtr::FuncType>::return_type,
35
+ typename guts::function_traits<
36
+ typename FuncPtr::FuncType>::parameter_types>;
37
+ };
38
+
39
+ } // namespace c10::impl
40
+
41
+ #else
42
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
43
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/util/TypeTraits.h>
5
+
6
+ namespace c10::impl {
7
+
8
+ namespace detail {
9
+ template <class FuncType, class ReturnType, class ParameterList>
10
+ class WrapFunctionIntoRuntimeFunctor_ {};
11
+ template <class FuncType, class ReturnType, class... Parameters>
12
+ class WrapFunctionIntoRuntimeFunctor_<
13
+ FuncType,
14
+ ReturnType,
15
+ guts::typelist::typelist<Parameters...>>
16
+ final : public c10::OperatorKernel {
17
+ public:
18
+ template <class FuncType_>
19
+ explicit WrapFunctionIntoRuntimeFunctor_(FuncType_&& kernel_func)
20
+ : kernel_func_(std::forward<FuncType_>(kernel_func)) {}
21
+
22
+ decltype(auto) operator()(Parameters... args) {
23
+ return kernel_func_(std::forward<Parameters>(args)...);
24
+ }
25
+
26
+ private:
27
+ FuncType kernel_func_;
28
+ };
29
+ } // namespace detail
30
+
31
+ // WrapFunctionIntoRuntimeFunctor: Wraps any runtime functor into a functor that
32
+ // inherits from c10::OperatorKernel, so it can be used as a c10 kernel.
33
+ // This can, for example, be used for lambdas, functors or even function
34
+ // pointers. In the case of function pointers, since it is a runtime function
35
+ // pointer, there is an overhead for calling it whenever the kernel is invoked.
36
+ template <class FuncType>
37
+ using WrapFunctionIntoRuntimeFunctor = detail::WrapFunctionIntoRuntimeFunctor_<
38
+ FuncType,
39
+ typename guts::infer_function_traits_t<FuncType>::return_type,
40
+ typename guts::infer_function_traits_t<FuncType>::parameter_types>;
41
+
42
+ } // namespace c10::impl
43
+
44
+ #else
45
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
46
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/boxing.h ADDED
@@ -0,0 +1,415 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // This file contains boxing (not unboxing) logic,
5
+ // i.e. how to make a vector<IValue> from a set of concrete arguments.
6
+
7
+ #include <ATen/core/ivalue.h>
8
+ #include <ATen/core/stack.h>
9
+ #include <c10/core/TensorOptions.h>
10
+
11
+ #include <ATen/core/boxing/BoxedKernel.h>
12
+
13
+ #include <c10/util/Metaprogramming.h>
14
+ #include <type_traits>
15
+
16
+ namespace c10::impl {
17
+
18
+ //
19
+ // utils
20
+ //
21
+
22
+ // is_mutable_tensor_ref
23
+ template <class T>
24
+ struct is_mutable_tensor_ref : std::false_type {};
25
+ template <>
26
+ struct is_mutable_tensor_ref<at::Tensor&> : std::true_type {};
27
+
28
+ // is_tuple_of_mutable_tensor_refs
29
+ //
30
+ template <class T, class Enable = void>
31
+ struct is_tuple_of_mutable_tensor_refs : std::false_type {};
32
+
33
+ template <class T>
34
+ struct is_tuple_of_mutable_tensor_refs<
35
+ T,
36
+ std::enable_if_t<guts::is_instantiation_of<std::tuple, T>::value, void>>
37
+ : guts::typelist::
38
+ all<is_mutable_tensor_ref, guts::typelist::from_tuple_t<T>> {};
39
+
40
+ // has_ivalue_to<T> tests the presence/absence of instance method
41
+ // IValue::to<T>()
42
+ //
43
+ template <class T, class Enable = void>
44
+ struct has_ivalue_to : std::false_type {};
45
+
46
+ template <class T>
47
+ struct ivalue_to_helper {
48
+ using type = decltype(std::declval<IValue>().template to<T>());
49
+ };
50
+ template <class T>
51
+ using ivalue_to_helper_t = typename ivalue_to_helper<T>::type;
52
+
53
+ template <class T>
54
+ struct has_ivalue_to<T, std::void_t<ivalue_to_helper_t<T>>> : std::true_type {};
55
+
56
+ //
57
+ // boxing predicates
58
+ //
59
+
60
+ // A boxable arg type is one that IValue has a constructor for.
61
+ template <typename T>
62
+ using can_box = std::disjunction<
63
+ std::is_constructible<IValue, std::decay_t<T>>,
64
+ // TensorOptions are not directly constructible into IValue,
65
+ // but torch::jit::push knows how to handle them
66
+ std::is_same<TensorOptions, std::decay_t<T>>>;
67
+
68
+ template <typename... Ts>
69
+ using can_box_all = std::conjunction<can_box<Ts>...>;
70
+
71
+ // an unboxable result is one that can be extracted from an IValue
72
+ template <typename T>
73
+ using can_unbox = std::conjunction<
74
+ std::disjunction<
75
+ has_ivalue_to<T>,
76
+ // void returns are ok
77
+ std::is_same<void, T>>,
78
+ std::negation<std::is_lvalue_reference<T>>>;
79
+
80
+ //
81
+ // boxArgs - utility for pushing unboxed args onto IValue stack
82
+ //
83
+ template <class... Args>
84
+ torch::jit::Stack boxArgs(Args... args) {
85
+ // TODO Reuse stack vector instead of allocating?
86
+ torch::jit::Stack stack;
87
+ stack.reserve(sizeof...(Args));
88
+ torch::jit::push(stack, std::forward<Args>(args)...);
89
+ return stack;
90
+ }
91
+
92
+ template <class T>
93
+ inline constexpr size_t boxed_size_one() {
94
+ static_assert(
95
+ !std::is_same_v<std::decay_t<T>, c10::TensorOptions>,
96
+ "need to patch this path to support TensorOptions passed by reference");
97
+ return 1;
98
+ }
99
+
100
+ // torch::jit::push pushes 4 values for a TensorOptions; this needs to
101
+ // be kept in sync.
102
+ template <>
103
+ inline constexpr size_t boxed_size_one<c10::TensorOptions>() {
104
+ return 4;
105
+ }
106
+
107
+ // NOTE: this could probably be simplified with C++17 fold expressions.
108
+ template <typename...>
109
+ struct BoxedSize : std::integral_constant<size_t, 0> {};
110
+ template <class T, class... Args>
111
+ struct BoxedSize<T, Args...>
112
+ : std::integral_constant<
113
+ size_t,
114
+ boxed_size_one<T>() + BoxedSize<Args...>::value> {};
115
+
116
+ template <class... Args>
117
+ static inline constexpr size_t boxed_size() {
118
+ return BoxedSize<Args...>::value;
119
+ }
120
+
121
+ template <typename T>
122
+ C10_ALWAYS_INLINE_UNLESS_MOBILE void boxToStack(IValue*& dest, T& arg) {
123
+ new (dest++) IValue(arg);
124
+ }
125
+
126
+ C10_ALWAYS_INLINE_UNLESS_MOBILE void boxToStack(
127
+ IValue*& dest,
128
+ c10::TensorOptions options) {
129
+ new (dest++) IValue(c10::typeMetaToScalarType(options.dtype()));
130
+ new (dest++) IValue(options.layout());
131
+ new (dest++) IValue(options.device());
132
+ new (dest++) IValue(options.pinned_memory());
133
+ }
134
+
135
+ inline void boxArgsToStack(IValue*& /*unused*/) {}
136
+
137
+ template <typename T, typename... Args>
138
+ C10_ALWAYS_INLINE_UNLESS_MOBILE void boxArgsToStack(
139
+ IValue*& dest,
140
+ T& arg,
141
+ Args&... args) {
142
+ boxToStack(dest, arg);
143
+ boxArgsToStack(dest, args...);
144
+ }
145
+
146
+ //
147
+ // PopResult is a helper class whose specializations handle popping single and
148
+ // multiple return values, respectively.
149
+ //
150
+ template <class Result>
151
+ struct PopResult final {
152
+ static Result call(Stack& stack) {
153
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
154
+ stack.size() == 1,
155
+ "Boxed kernel was expected to return one value on the stack, ",
156
+ "but instead pushed ",
157
+ stack.size(),
158
+ " values.");
159
+ return std::move(stack[0]).to<Result>();
160
+ }
161
+ };
162
+
163
+ template <class... Types>
164
+ struct PopResult<std::tuple<Types...>> final {
165
+ using Result = std::tuple<Types...>;
166
+
167
+ static Result call(Stack& stack) {
168
+ // for tuple return types, boxed kernel has pushed multiple values onto the
169
+ // stack
170
+ constexpr int RetCount = sizeof...(Types);
171
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
172
+ stack.size() == RetCount,
173
+ "Boxed kernel was expected to return ",
174
+ RetCount,
175
+ " values on the stack, ",
176
+ "but instead pushed ",
177
+ stack.size(),
178
+ " values.");
179
+ return pop_to_tuple_impl(stack, std::make_index_sequence<RetCount>());
180
+ }
181
+
182
+ private:
183
+ // note: this has been moved into its own helper only to avoid a parse error
184
+ // on `indices` otherwise. I'm sure there's an incantation that slips it past
185
+ // the parser but eh
186
+ template <size_t... indices>
187
+ static Result pop_to_tuple_impl(
188
+ Stack& stack,
189
+ std::index_sequence<indices...> /*unused*/) {
190
+ return std::make_tuple((std::move(stack[indices]).template to<Types>())...);
191
+ }
192
+ };
193
+
194
+ //
195
+ // BoxedKernelWrapper
196
+ //
197
+ // For a given function type FT, BoxedKernelWrapper<FT> implements
198
+ // a `call` method that
199
+ // - takes a boxed kernel and unboxed arguments as specified by FT,
200
+ // - calls `boxArgs` to box the arguments
201
+ // - calls the boxed kernel
202
+ // - unboxes and returns the result
203
+ //
204
+ // The partial specializations below handle various cases: in
205
+ // particular, not all types appearing in op signatures are supported,
206
+ // and ops returning references have nonstandard wrapper implementations.
207
+ //
208
+
209
+ // 1. The base specialization of BoxedKernelWrapper should never be
210
+ // instantiated. A "no call method defined on BoxedKernelWrapper" compile error
211
+ // means that an op signature has failed to trigger any of the partial
212
+ // specializations that follow this one.
213
+ //
214
+ template <class FuncType, class Enable = void>
215
+ struct BoxedKernelWrapper {
216
+ // The reason we're not just doing straight up static_assert(false, ...) here:
217
+ // Basically, the way to make sure a static_assert only fires if a template
218
+ // is actually instantiated (rather than every time the file is parsed) is to
219
+ // use template parameters in the expression, e.g. FuncType here. However,
220
+ // since `sizeof(FuncType) != sizeof(FuncType)` is always false, this has the
221
+ // same effect.
222
+ static_assert(
223
+ sizeof(FuncType) != sizeof(FuncType),
224
+ "Function signature contains one or more unsupported parameter and/or return types. "
225
+ "Look for a nearby error like "
226
+ "\"'call' is not a member of 'c10::impl::BoxedKernelWrapper<(your function type), void>'\" "
227
+ "- (your function type) is the unsupported signature.");
228
+ };
229
+
230
+ //
231
+ // 2. Supported signatures, other than those involving non-const Tensor refs -
232
+ // i.e., "functional" ops.
233
+ //
234
+
235
+ template <class Result, class... Args>
236
+ struct BoxedKernelWrapper<
237
+ Result(Args...),
238
+ std::enable_if_t<
239
+ can_box_all<Args...>::value && can_unbox<Result>::value &&
240
+ !is_tuple_of_mutable_tensor_refs<Result>::value,
241
+ void>> {
242
+ static Result call(
243
+ const BoxedKernel& boxed_kernel_func,
244
+ const OperatorHandle& opHandle,
245
+ DispatchKeySet dispatchKeySet,
246
+ Args... args) {
247
+ torch::jit::Stack stack = boxArgs<Args...>(std::forward<Args>(args)...);
248
+ boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
249
+
250
+ if constexpr (!std::is_same_v<void, Result>) {
251
+ // op has pushed one or more values onto the stack.
252
+ return PopResult<Result>::call(stack);
253
+ } else {
254
+ // op returns void, boxed kernel has pushed nothing onto stack.
255
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
256
+ stack.empty(),
257
+ "Boxed kernel was expected to return no values on the stack, ",
258
+ "but instead returned ",
259
+ stack.size(),
260
+ " values.");
261
+ }
262
+ }
263
+ };
264
+
265
+ //
266
+ // 3. in-place ops take a single non-const Tensor reference
267
+ // as their first argument, and return it.
268
+ //
269
+ // Note: all signatures matching this pattern are assumed to be for such ops.
270
+ // Because of this, the generated BoxedKernelWrapper specializations simply
271
+ // return the in-place argument.
272
+ //
273
+
274
+ template <class... OtherArgs>
275
+ struct BoxedKernelWrapper<
276
+ at::Tensor&(at::Tensor&, OtherArgs...),
277
+ std::enable_if_t<can_box_all<OtherArgs...>::value, void>> {
278
+ static at::Tensor& call(
279
+ const BoxedKernel& boxed_kernel_func,
280
+ const OperatorHandle& opHandle,
281
+ DispatchKeySet dispatchKeySet,
282
+ at::Tensor& outArg,
283
+ OtherArgs... otherArgs) {
284
+ torch::jit::Stack stack = boxArgs<at::Tensor&, OtherArgs...>(
285
+ outArg, std::forward<OtherArgs>(otherArgs)...);
286
+ boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
287
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
288
+ stack.size() == 1,
289
+ "Boxed kernel was expected to return a single value on the stack, ",
290
+ "but instead returned ",
291
+ stack.size(),
292
+ " values.");
293
+
294
+ return outArg;
295
+ }
296
+ };
297
+
298
+ //
299
+ // 3.5. In-process migration to make in-place ops take and return
300
+ // const references instead.
301
+ template <class... OtherArgs>
302
+ struct BoxedKernelWrapper<
303
+ const at::Tensor&(const at::Tensor&, OtherArgs...),
304
+ std::enable_if_t<can_box_all<OtherArgs...>::value, void>> {
305
+ static const at::Tensor& call(
306
+ const BoxedKernel& boxed_kernel_func,
307
+ const OperatorHandle& opHandle,
308
+ DispatchKeySet dispatchKeySet,
309
+ const at::Tensor& outArg,
310
+ OtherArgs... otherArgs) {
311
+ torch::jit::Stack stack = boxArgs(outArg, otherArgs...);
312
+ boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
313
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
314
+ stack.size() == 1,
315
+ "Boxed kernel was expected to return a single value on the stack, ",
316
+ "but instead returned ",
317
+ stack.size(),
318
+ " values.");
319
+
320
+ return outArg;
321
+ }
322
+ };
323
+
324
+ //
325
+ // 4. out of place ops that take a single non-const Tensor reference as their
326
+ // final argument, and also return it.
327
+ //
328
+ // Note: all signatures matching this pattern are assumed to be for such ops.
329
+ // This assumption permits the generated BoxedKernelWrapper specializations to
330
+ // simply return out arguments.
331
+ //
332
+ template <class FirstArg, class... RestArgs>
333
+ struct BoxedKernelWrapper<
334
+ at::Tensor&(FirstArg, RestArgs...),
335
+ std::enable_if_t<
336
+ can_box_all<FirstArg, RestArgs...>::value
337
+ // this skips over in-place kernels with a non-const Tensor
338
+ // arg at the front, so those can unambiguously trigger the
339
+ // preceding specialization.
340
+ && !is_mutable_tensor_ref<FirstArg>::value,
341
+ void>> {
342
+ static at::Tensor& call(
343
+ const BoxedKernel& boxed_kernel_func,
344
+ const OperatorHandle& opHandle,
345
+ DispatchKeySet dispatchKeySet,
346
+ FirstArg firstArg,
347
+ RestArgs... restArgs) {
348
+ torch::jit::Stack stack = boxArgs<FirstArg, RestArgs...>(
349
+ std::forward<FirstArg>(firstArg), std::forward<RestArgs>(restArgs)...);
350
+ boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
351
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
352
+ stack.size() == 1,
353
+ "Boxed kernel was expected to return a single value on the stack, ",
354
+ "but instead returned ",
355
+ stack.size(),
356
+ " values.");
357
+
358
+ // reusing restArgs after it has been forwarded here is ok because we know
359
+ // that the last element is of type `Tensor&`.
360
+ return std::get<sizeof...(RestArgs) - 1>(
361
+ std::tuple<RestArgs...>{restArgs...});
362
+ }
363
+ };
364
+
365
+ //
366
+ // 5. out of place ops that take multiple non-const Tensor references as their
367
+ // final arguments, and return them in a std::tuple.
368
+ //
369
+ // Note: all signatures matching this pattern are assumed to be for such ops.
370
+ // This assumption permits the generated BoxedKernelWrapper specializations to
371
+ // simply return the out arguments.
372
+ //
373
+ template <class Result, class... Args>
374
+ struct BoxedKernelWrapper<
375
+ Result(Args...),
376
+ std::enable_if_t<
377
+ can_box_all<Args...>::value &&
378
+ is_tuple_of_mutable_tensor_refs<Result>::value,
379
+ void>> {
380
+ static Result call(
381
+ const BoxedKernel& boxed_kernel_func,
382
+ const OperatorHandle& opHandle,
383
+ DispatchKeySet dispatchKeySet,
384
+ Args... args) {
385
+ using ArgTuple = std::tuple<Args...>;
386
+ constexpr int RetCount = std::tuple_size<Result>();
387
+
388
+ torch::jit::Stack stack = boxArgs<Args...>(std::forward<Args>(args)...);
389
+ boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
390
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
391
+ stack.size() == RetCount,
392
+ "Boxed kernel was expected to return ",
393
+ RetCount,
394
+ " values on the stack, ",
395
+ "but instead returned ",
396
+ stack.size(),
397
+ " values.");
398
+
399
+ // reusing args after it has been forwarded here is ok because we know
400
+ // that the last RetCount elements are of type `Tensor&`.
401
+ auto result = guts::tuple_take<ArgTuple, -RetCount>(
402
+ ArgTuple{std::forward<Args>(args)...});
403
+ static_assert(
404
+ std::is_same_v<Result, decltype(result)>,
405
+ "The parameter list of an op returning a tuple of Tensor references "
406
+ "must end with an equal number of Tensor reference parameters.");
407
+ return result;
408
+ }
409
+ };
410
+
411
+ } // namespace c10::impl
412
+
413
+ #else
414
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
415
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h ADDED
@@ -0,0 +1,790 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/IListRef.h>
5
+ #include <ATen/core/boxing/OperatorKernel.h>
6
+ #include <ATen/core/ivalue.h>
7
+ #include <ATen/core/stack.h>
8
+ #include <c10/util/Metaprogramming.h>
9
+ #include <c10/util/TypeList.h>
10
+ #include <c10/util/intrusive_ptr.h>
11
+
12
+ #include <utility>
13
+
14
+ namespace c10 {
15
+
16
+ using Stack = torch::jit::Stack; // TODO Instead of this, move torch::jit::Stack
17
+ // to the c10 namespace.
18
+ class OperatorHandle;
19
+
20
+ /*
21
+ * [Note: Argument forwarding in the dispatcher]
22
+ *
23
+ * The dispatcher uses a somewhat unusual way to forward arguments through
24
+ * several layers of wrapper functions. This can be confusing because an
25
+ * experienced C++ programmer would look at this and think "oh this is supposed
26
+ * to be forwarding a universal reference but the && is missing. This is a
27
+ * bug.". It is not a bug. The common way in C++ to forward arguments is to use
28
+ * universal references:
29
+ *
30
+ * > template<class T> void func(T&& arg) { func2(std::forward<T>(arg)); }
31
+ *
32
+ * but that relies on inferring the correct reference type (i.e. value vs & vs
33
+ * &&) from the argument. In our case, we cannot rely on the argument as
34
+ * supplied by the caller, because that could infer a different reference type
35
+ * than was used in the kernel function. The correct reference type is dictated
36
+ * by the kernel signature and must be identical since we cast function pointers
37
+ * through void* pointers and mismatches would be UB. So we need a forwarding
38
+ * pattern that determines the reference type to use by looking at the
39
+ * explicitly supplied operator signature, not by looking at the argument we're
40
+ * calling it with.
41
+ *
42
+ * What does std::forward do, exactly?
43
+ * ------------------------------------
44
+ * std::forward<T>(t) is a way to cast t to the reference type supplied in T.
45
+ * Let's assume decay_t<T> == U and T is either U or some reference of U.
46
+ * - std::forward<T&>(t) will return U&, no matter what kind of reference t is.
47
+ * - std::forward<T&&>(t) will return U&&, no matter what kind of reference t
48
+ * is.
49
+ * - std::forward<T>(t) will return U&& (not U!), no matter what kind of
50
+ * reference t is.
51
+ *
52
+ * For universal references, that means that in the following function
53
+ * > template<class T> void func(T&& arg) { func2(std::forward<T>(arg)); }
54
+ *
55
+ * - when called with arg being a rvalue reference or non-reference value, T
56
+ * gets inferred to be a non-reference U, and std::forward<T>(t) will return
57
+ * U&&, correctly moving the argument.
58
+ * - when called with arg behind a lvalue reference, T gets inferred to be U&
59
+ * because that's the only way to match the signature (in C++, a type that is
60
+ * (T&)&& will collapse to T&). That means std::forward<T>(t) will return U& and
61
+ * the value will not be moved but passed on as a lvalue reference.
62
+ *
63
+ * How do we use that?
64
+ * ------------------------------------
65
+ * But std::forward can also be used outside of the common "universal
66
+ * forwarding" pattern to change reference types. So instead of following the
67
+ * common C++ pattern, we notice what std::forward<T>() actually does, and that
68
+ * is it takes a value and changes its reference to the type of reference passed
69
+ * in as T. If we don't infer T but explicitly specify it, we can use this to
70
+ * forward based on an explicitly specified reference type instead of the
71
+ * inferred argument type.
72
+ *
73
+ * This is why many of the dispatcher functions look like
74
+ * > template<class T> func(T t) { func2<T>(std::forward<T>(t)); }
75
+ * instead of the common
76
+ * > template<class T> func(T&& t) { func2(std::forward<T>(t)); }
77
+ *
78
+ * and are expected to be called by explicitly specifying the template
79
+ * parameters in a way that matches the expected operator signature at each call
80
+ * site.
81
+ */
82
+
83
+ namespace impl {
84
+ // supported_primitive_arg_types defines which primitive types we allow in
85
+ // kernel functions as arguments or returns.
86
+ // Additionally, we support lists, dicts and optionals containing these types.
87
+ using supported_primitive_arg_types = guts::typelist::typelist<
88
+ int64_t,
89
+ double,
90
+ bool,
91
+ std::string_view,
92
+ at::Tensor,
93
+ at::Scalar,
94
+ c10::QScheme,
95
+ c10::ScalarType,
96
+ c10::Device,
97
+ c10::DeviceIndex,
98
+ c10::Layout,
99
+ c10::MemoryFormat,
100
+ at::Dimname>;
101
+
102
+ // We have an unboxed functor in hand that takes C++ arguments, and
103
+ // we're building a boxed functor wrapper for it that takes IValues.
104
+ // So "outside" is boxed and "inside" is unboxed.
105
+ //
106
+ // So a valid input type is one that our boxed functor wrapper can
107
+ // unbox from an IValue into a C++ value.
108
+ //
109
+ // Whereas a valid output type is one that our wrapper can receive
110
+ // as a C++ value from the unboxed functor, and box into an IValue.
111
+
112
+ //
113
+ // assert_is_valid_input_type
114
+ // checks that T can be unboxed from an IValue into a C++ value.
115
+ //
116
+
117
+ template <class T, bool AllowDeprecatedTypes, class Enable = void>
118
+ struct assert_is_valid_input_type {
119
+ assert_is_valid_input_type() {
120
+ if constexpr (guts::typelist::contains<supported_primitive_arg_types, T>::
121
+ value) {
122
+ /* everything is ok, this is a primitive type */
123
+ } else {
124
+ /* otherwise this must be an instance of a valid custom class, since it
125
+ can only have been created via IValue(x), which ensures this. */
126
+ }
127
+ }
128
+ };
129
+
130
+ template <class T, bool AllowDeprecatedTypes>
131
+ struct assert_is_valid_input_type<std::optional<T>, AllowDeprecatedTypes>
132
+ : assert_is_valid_input_type<T, AllowDeprecatedTypes> {};
133
+
134
+ template <bool AllowDeprecatedTypes, class... Args>
135
+ struct TypeCheckHelper;
136
+
137
+ template <bool AllowDeprecatedTypes>
138
+ struct TypeCheckHelper<AllowDeprecatedTypes> {};
139
+
140
+ template <bool AllowDeprecatedTypes, class Head, class... Rest>
141
+ struct TypeCheckHelper<AllowDeprecatedTypes, Head, Rest...>
142
+ : TypeCheckHelper<AllowDeprecatedTypes, Rest...> {
143
+ assert_is_valid_input_type<Head, AllowDeprecatedTypes> check;
144
+ };
145
+
146
+ template <class... Contained, bool AllowDeprecatedTypes>
147
+ struct assert_is_valid_input_type<
148
+ std::tuple<Contained...>,
149
+ AllowDeprecatedTypes>
150
+ : TypeCheckHelper<AllowDeprecatedTypes, Contained...> {};
151
+
152
+ template <class Key, class Value, bool AllowDeprecatedTypes>
153
+ struct assert_is_valid_input_type<Dict<Key, Value>, AllowDeprecatedTypes>
154
+ : assert_is_valid_input_type<Value, AllowDeprecatedTypes> {
155
+ static_assert(
156
+ guts::typelist::contains<impl::valid_dict_key_types, Key>::value,
157
+ "You tried to register a kernel with an unsupported input type: Dict<Key, Value> where Key is invalid. We only support int64_t, double, bool, and string.");
158
+ };
159
+
160
+ template <class Key, class Value, bool AllowDeprecatedTypes>
161
+ struct assert_is_valid_input_type<
162
+ std::unordered_map<Key, Value>,
163
+ AllowDeprecatedTypes>
164
+ : assert_is_valid_input_type<Value, AllowDeprecatedTypes> {
165
+ static_assert(
166
+ AllowDeprecatedTypes,
167
+ "You tried to register a kernel with an unsupported input type: std::unordered_map<Key, Value>. Please use Dict<Key, Value> instead.");
168
+ static_assert(
169
+ guts::typelist::contains<impl::valid_dict_key_types, Key>::value,
170
+ "You tried to register a kernel with an unsupported input type: std::unordered_map<Key, Value> where Key is invalid. We only support int64_t, double, bool, and string.");
171
+ };
172
+
173
+ template <class T, bool AllowDeprecatedTypes>
174
+ struct assert_is_valid_input_type<List<T>, AllowDeprecatedTypes>
175
+ : assert_is_valid_input_type<T, AllowDeprecatedTypes> {
176
+ static_assert(
177
+ !std::is_same_v<T, at::Scalar>,
178
+ "You tried to register a kernel with an unsupported input type: List<Scalar>. Please use List<int64_t>, List<double> or Tensor instead.");
179
+ };
180
+
181
+ template <class T, bool AllowDeprecatedTypes>
182
+ struct assert_is_valid_input_type<c10::ArrayRef<T>, AllowDeprecatedTypes>
183
+ : assert_is_valid_input_type<T, AllowDeprecatedTypes> {
184
+ static_assert(
185
+ !std::is_same_v<T, at::Scalar>,
186
+ "You tried to register a kernel with an unsupported input type: ArrayRef<Scalar>. Please use List<int64_t>, List<double> or Tensor instead.");
187
+ };
188
+
189
+ template <class T, bool AllowDeprecatedTypes>
190
+ struct assert_is_valid_input_type<
191
+ c10::OptionalArrayRef<T>,
192
+ AllowDeprecatedTypes>
193
+ : assert_is_valid_input_type<T, AllowDeprecatedTypes> {
194
+ static_assert(
195
+ !std::is_same_v<T, at::Scalar>,
196
+ "You tried to register a kernel with an unsupported input type: OptionalArrayRef<Scalar>. Please use List<int64_t>, List<double> or Tensor instead.");
197
+ };
198
+
199
+ template <class T, size_t N, bool AllowDeprecatedTypes>
200
+ struct assert_is_valid_input_type<std::array<T, N>, AllowDeprecatedTypes>
201
+ : assert_is_valid_input_type<T, AllowDeprecatedTypes> {
202
+ static_assert(
203
+ !std::is_same_v<T, at::Scalar>,
204
+ "You tried to register a kernel with an unsupported input type: std::array<Scalar, N>. Please use std::array<int64_t, N> instead.");
205
+ };
206
+
207
+ template <class T, bool AllowDeprecatedTypes>
208
+ struct assert_is_valid_input_type<
209
+ T,
210
+ AllowDeprecatedTypes,
211
+ std::enable_if_t<std::is_same_v<float, T>>> {
212
+ // There is no reason to support float when we have double. Keep the API lean.
213
+ static_assert(
214
+ guts::false_t<T>::value,
215
+ "You tried to register a kernel with an unsupported input type: float. Please use double instead; you should use `double` in the C++ function signature and `float` in the schema string.");
216
+ };
217
+ template <class T, bool AllowDeprecatedTypes>
218
+ struct assert_is_valid_input_type<
219
+ T,
220
+ AllowDeprecatedTypes,
221
+ std::enable_if_t<std::is_same_v<const char*, T>>> {
222
+ static_assert(
223
+ guts::false_t<T>::value,
224
+ "You tried to register a kernel with an unsupported input type: const char*. Please use std::string_view instead.");
225
+ };
226
+ template <class T, bool AllowDeprecatedTypes>
227
+ struct assert_is_valid_input_type<
228
+ T,
229
+ AllowDeprecatedTypes,
230
+ std::enable_if_t<std::is_same_v<std::vector<bool>, T>>> {
231
+ static_assert(
232
+ guts::false_t<T>::value,
233
+ "You tried to register a kernel with an unsupported input type: vector<bool>. Please use List<bool> instead.");
234
+ };
235
+ template <class T, bool AllowDeprecatedTypes>
236
+ struct assert_is_valid_input_type<
237
+ T,
238
+ AllowDeprecatedTypes,
239
+ std::enable_if_t<
240
+ std::is_integral_v<T> &&
241
+ !guts::typelist::contains<supported_primitive_arg_types, T>::value>> {
242
+ static_assert(
243
+ guts::false_t<T>::value,
244
+ "You tried to register a kernel with an unsupported integral input type. Please use int64_t instead; you should use `int64_t` in the C++ function signature and `int` in the schema string.");
245
+ };
246
+ template <class T, bool AllowDeprecatedTypes>
247
+ struct assert_is_valid_input_type<
248
+ T,
249
+ AllowDeprecatedTypes,
250
+ std::enable_if_t<std::is_same_v<const c10::SymInt&, T>>> {
251
+ static_assert(
252
+ guts::false_t<T>::value,
253
+ "You tried to register a kernel taking c10::SymInt by reference. Please accept it by value instead.");
254
+ };
255
+
256
+ // TODO: it probably would be good to tighten this up quite a bit more with
257
+ // an explicit list for everything
258
+
259
+ //
260
+ // assert_is_valid_output_type
261
+ //
262
+
263
+ template <class T, bool AllowDeprecatedTypes, class Enable = void>
264
+ struct assert_is_valid_output_type {
265
+ assert_is_valid_output_type() {
266
+ if constexpr (guts::typelist::contains<supported_primitive_arg_types, T>::
267
+ value) {
268
+ /* everything is ok, this is a primitive type */
269
+ } else {
270
+ /* otherwise T is verified to be a registered custom class in the IValue
271
+ constructor, so no benefit in double-checking here */
272
+ }
273
+ }
274
+ };
275
+
276
+ template <class T, bool AllowDeprecatedTypes>
277
+ struct assert_is_valid_output_type<std::optional<T>, AllowDeprecatedTypes>
278
+ : assert_is_valid_output_type<T, AllowDeprecatedTypes> {};
279
+
280
+ template <class T, bool AllowDeprecatedTypes>
281
+ struct assert_is_valid_output_type<
282
+ c10::OptionalArrayRef<T>,
283
+ AllowDeprecatedTypes>
284
+ : assert_is_valid_output_type<T, AllowDeprecatedTypes> {};
285
+
286
+ template <class Key, class Value, bool AllowDeprecatedTypes>
287
+ struct assert_is_valid_output_type<Dict<Key, Value>, AllowDeprecatedTypes>
288
+ : assert_is_valid_output_type<Value, AllowDeprecatedTypes> {
289
+ static_assert(
290
+ guts::typelist::contains<impl::valid_dict_key_types, Key>::value,
291
+ "You tried to register a kernel with an unsupported output type: Dict<Key, Value> where Key is invalid. We only support int64_t, double, bool, and string.");
292
+ static_assert(
293
+ !std::is_same_v<Value, at::Scalar>,
294
+ "You tried to register a kernel with an unsupported output type: Dict<Key, Scalar>. Please use Dict<Key, int64_t> or Dict<Key, double>.");
295
+ };
296
+
297
+ template <class Key, class Value, bool AllowDeprecatedTypes>
298
+ struct assert_is_valid_output_type<
299
+ std::unordered_map<Key, Value>,
300
+ AllowDeprecatedTypes>
301
+ : assert_is_valid_output_type<Value, AllowDeprecatedTypes> {
302
+ static_assert(
303
+ AllowDeprecatedTypes,
304
+ "You tried to register a kernel with an unsupported output type: std::unordered_map<Key, Value>. Please use Dict<Key, Value> instead.");
305
+ static_assert(
306
+ guts::typelist::contains<impl::valid_dict_key_types, Key>::value,
307
+ "You tried to register a kernel with an unsupported output type: std::unordered_map<Key, Value> where Key is invalid. We only support int64_t, double, bool, and string.");
308
+ static_assert(
309
+ !std::is_same_v<Value, at::Scalar>,
310
+ "You tried to register a kernel with an unsupported output type: std::unordered_map<Key, Scalar>. Please use Dict<Key, int64_t> or Dict<Key, double>.");
311
+ };
312
+
313
+ template <class T, bool AllowDeprecatedTypes>
314
+ struct assert_is_valid_output_type<List<T>, AllowDeprecatedTypes>
315
+ : assert_is_valid_output_type<T, AllowDeprecatedTypes> {
316
+ static_assert(
317
+ !std::is_same_v<T, at::Scalar>,
318
+ "You tried to register a kernel with an unsupported output type: List<Scalar>. Please use List<int64_t>, List<double> or Tensor instead.");
319
+ };
320
+
321
+ template <class T, bool AllowDeprecatedTypes>
322
+ struct assert_is_valid_output_type<std::vector<T>, AllowDeprecatedTypes>
323
+ : assert_is_valid_output_type<T, AllowDeprecatedTypes> {
324
+ static_assert(
325
+ !std::is_same_v<T, at::Scalar>,
326
+ "You tried to register a kernel with an unsupported output type: std::vector<Scalar>. Please use List<int64_t>, List<double> or Tensor instead.");
327
+ // TODO static_assert(AllowDeprecatedTypes, "You tried to register a kernel
328
+ // with an unsupported output type: std::vector<T>. Please use List<T>
329
+ // instead.");
330
+ };
331
+
332
+ template <class T, size_t N, bool AllowDeprecatedTypes>
333
+ struct assert_is_valid_output_type<std::array<T, N>, AllowDeprecatedTypes>
334
+ : assert_is_valid_output_type<T, AllowDeprecatedTypes> {
335
+ static_assert(
336
+ !std::is_same_v<T, at::Scalar>,
337
+ "You tried to register a kernel with an unsupported output type: std::array<Scalar, N>. Please use std::array<int64_t, N> instead.");
338
+ };
339
+
340
+ // The following specialisations of assert_is_valid_output_type are technically
341
+ // not necessary since we would hit the base case and show an error message
342
+ // there if they didn't exist, but we can show a better error message
343
+ // in some common error scenarios.
344
+ template <class T, bool AllowDeprecatedTypes>
345
+ struct assert_is_valid_output_type<
346
+ T,
347
+ AllowDeprecatedTypes,
348
+ std::enable_if_t<std::is_same_v<float, T>>> {
349
+ // There is no reason to support float when we have double. Keep the API lean.
350
+ static_assert(
351
+ guts::false_t<T>::value,
352
+ "You tried to register a kernel with an unsupported output type: float. Please use double instead; you should use `double` in the C++ function signature and `float` in the schema string.");
353
+ };
354
+ template <class T, bool AllowDeprecatedTypes>
355
+ struct assert_is_valid_output_type<
356
+ T,
357
+ AllowDeprecatedTypes,
358
+ std::enable_if_t<std::is_same_v<const char*, T>>> {
359
+ static_assert(
360
+ guts::false_t<T>::value,
361
+ "You tried to register a kernel with an unsupported output type: const char*. Please use std::string_view instead.");
362
+ };
363
+ template <class T, bool AllowDeprecatedTypes>
364
+ struct assert_is_valid_output_type<
365
+ T,
366
+ AllowDeprecatedTypes,
367
+ std::enable_if_t<std::is_same_v<std::vector<bool>, T>>> {
368
+ static_assert(
369
+ guts::false_t<T>::value,
370
+ "You tried to register a kernel with an unsupported output type: vector<bool>. Please use List<bool> instead.");
371
+ };
372
+ template <class T, bool AllowDeprecatedTypes>
373
+ struct assert_is_valid_output_type<
374
+ T,
375
+ AllowDeprecatedTypes,
376
+ std::enable_if_t<
377
+ std::is_integral_v<T> &&
378
+ !guts::typelist::contains<supported_primitive_arg_types, T>::value>> {
379
+ static_assert(
380
+ guts::false_t<T>::value,
381
+ "You tried to register a kernel with an unsupported integral output type. Please use int64_t instead; you should use `int64_t` in the C++ function signature and `int` in the schema string.");
382
+ };
383
+
384
+ // ivalue_to_arg
385
+
386
+ template <class T>
387
+ struct decay_if_not_tensor final {
388
+ using type = std::decay_t<T>;
389
+ };
390
+
391
+ template <>
392
+ struct decay_if_not_tensor<at::Tensor&> final {
393
+ using type = at::Tensor&;
394
+ };
395
+
396
+ template <>
397
+ struct decay_if_not_tensor<const at::Tensor&> final {
398
+ using type = const at::Tensor&;
399
+ };
400
+
401
+ template <class T, bool AllowDeprecatedTypes>
402
+ struct ivalue_to_arg final {
403
+ static decltype(auto) call(IValue& v) {
404
+ assert_is_valid_input_type<T, AllowDeprecatedTypes>();
405
+ return std::move(v).to<T>();
406
+ }
407
+ };
408
+
409
+ // The following two specializations take advantage of specialized
410
+ // `toTensor()` overloads on IValue to avoid copying.
411
+ template <bool AllowDeprecatedTypes>
412
+ struct ivalue_to_arg<at::Tensor&, AllowDeprecatedTypes> final {
413
+ // We cannot use the default implementation if they asked for a
414
+ // `at::Tensor&` because it moves from the IValue, so it can't get
415
+ // an lvalue reference.
416
+ static at::Tensor& call(IValue& v) {
417
+ // Tensor& is valid, don't bother asserting
418
+ return v.toTensor();
419
+ }
420
+ };
421
+
422
+ template <bool AllowDeprecatedTypes>
423
+ struct ivalue_to_arg<const at::Tensor&, AllowDeprecatedTypes> final {
424
+ // We should not use the default implementation if they asked for
425
+ // a `const at::Tensor&` because it moves from the IValue and they
426
+ // didn't ask for that.
427
+ static const at::Tensor& call(IValue& v) {
428
+ // const Tensor& is valid, don't bother asserting
429
+ return v.toTensor();
430
+ }
431
+ };
432
+
433
+ template <bool AllowDeprecatedTypes>
434
+ struct ivalue_to_arg<at::ITensorListRef, AllowDeprecatedTypes> final {
435
+ static List<at::Tensor> call(IValue& v) {
436
+ return v.toTensorList();
437
+ }
438
+ };
439
+
440
+ template <class T, bool AllowDeprecatedTypes>
441
+ struct ivalue_to_arg<ArrayRef<T>, AllowDeprecatedTypes> final {
442
+ // If an argument is ArrayRef<T>, convert the IValue to a std::vector<T> and
443
+ // pass that to the operator. std::vector<T> is implicitly convertible to
444
+ // ArrayRef<T>.
445
+ static std::vector<T> call(IValue& v) {
446
+ return ivalue_to_arg<std::vector<T>, AllowDeprecatedTypes>::call(v);
447
+ }
448
+ };
449
+ template <bool AllowDeprecatedTypes>
450
+ struct ivalue_to_arg<c10::SymIntArrayRef, AllowDeprecatedTypes> final {
451
+ static std::vector<c10::SymInt> call(IValue& v) {
452
+ if (v.isIntList()) {
453
+ std::vector<c10::SymInt> r;
454
+ auto src = v.toIntList();
455
+ std::transform(
456
+ src.begin(), src.end(), std::back_inserter(r), [](int64_t i) {
457
+ return c10::SymInt(i);
458
+ });
459
+ return r;
460
+ } else {
461
+ return ivalue_to_arg<std::vector<c10::SymInt>, AllowDeprecatedTypes>::
462
+ call(v);
463
+ }
464
+ }
465
+ };
466
+ template <bool AllowDeprecatedTypes>
467
+ struct ivalue_to_arg<c10::OptionalArray<c10::SymInt>, AllowDeprecatedTypes>
468
+ final {
469
+ static OptionalArray<c10::SymInt> call(IValue& v) {
470
+ if (v.isIntList()) {
471
+ std::vector<c10::SymInt> r;
472
+ auto src = v.toIntList();
473
+ std::transform(
474
+ src.begin(), src.end(), std::back_inserter(r), [](int64_t i) {
475
+ return c10::SymInt(i);
476
+ });
477
+ return OptionalArray<c10::SymInt>(std::move(r));
478
+ } else {
479
+ return std::move(v).to<OptionalArray<c10::SymInt>>();
480
+ }
481
+ }
482
+ };
483
+ template <class T, bool AllowDeprecatedTypes>
484
+ struct ivalue_to_arg<std::optional<ArrayRef<T>>, AllowDeprecatedTypes> final {
485
+ // If an argument is std::optional<ArrayRef<T>>, convert the IValue to an
486
+ // std::optional<std::vector<T>> and pass that to the operator.
487
+ // OptionalArray<T> is basically a std::optional<std::vector<T>> but
488
+ // implicitly convertible to std::optional<ArrayRef<T>>.
489
+ static OptionalArray<T> call(IValue& v) {
490
+ return ivalue_to_arg<OptionalArray<T>, AllowDeprecatedTypes>::call(v);
491
+ }
492
+ };
493
+
494
+ template <class T, bool AllowDeprecatedTypes>
495
+ struct ivalue_to_arg<OptionalArrayRef<T>, AllowDeprecatedTypes> final {
496
+ // If an argument is OptionalArrayRef<T>, convert the IValue to an
497
+ // std::optional<std::vector<T>> and pass that to the operator.
498
+ // OptionalArray<T> is basically a std::optional<std::vector<T>> but
499
+ // implicitly convertible to OptionalArrayRef<T>
500
+ static OptionalArray<T> call(IValue& v) {
501
+ return ivalue_to_arg<OptionalArray<T>, AllowDeprecatedTypes>::call(v);
502
+ }
503
+ };
504
+
505
+ // return_to_ivalue
506
+ template <class T, bool AllowDeprecatedTypes, class Enable = void>
507
+ struct return_to_ivalue final {};
508
+
509
+ template <class T, bool AllowDeprecatedTypes>
510
+ struct return_to_ivalue<
511
+ T,
512
+ AllowDeprecatedTypes,
513
+ std::enable_if_t<!std::is_same_v<at::Tensor&, T>>>
514
+ final {
515
+ static IValue call(T&& v) {
516
+ assert_is_valid_output_type<T, AllowDeprecatedTypes>();
517
+ return c10::ivalue::from(std::move(v));
518
+ }
519
+ static IValue copy(const T& v) {
520
+ assert_is_valid_output_type<T, AllowDeprecatedTypes>();
521
+ return IValue(v);
522
+ }
523
+ };
524
+
525
+ // Special case to allow kernels to return `Tensor&`.
526
+ // TODO Delete this once kernels don't do that anymore
527
+ template <bool AllowDeprecatedTypes>
528
+ struct return_to_ivalue<at::Tensor&, AllowDeprecatedTypes, void> final {
529
+ static IValue call(at::Tensor& v) {
530
+ return c10::ivalue::from(v);
531
+ }
532
+ static IValue copy(at::Tensor& v) {
533
+ return IValue(v);
534
+ }
535
+ };
536
+
537
+ // wrap_kernel_functor_unboxed_
538
+
539
+ template <class KernelFunctor, class OpSignature>
540
+ struct wrap_kernel_functor_unboxed_ final {};
541
+
542
+ // This specialization is for kernels with a first argument that is NOT of type
543
+ // DispatchKeySet This includes kernels with 0 arguments.
544
+ template <class KernelFunctor, class ReturnType, class... ParameterTypes>
545
+ struct wrap_kernel_functor_unboxed_<
546
+ KernelFunctor,
547
+ ReturnType(ParameterTypes...)>
548
+ final {
549
+ static_assert(
550
+ std::is_same_v<
551
+ ReturnType,
552
+ typename guts::infer_function_traits_t<KernelFunctor>::return_type>,
553
+ "Return type mismatch");
554
+ static_assert(
555
+ std::is_same_v<
556
+ guts::typelist::typelist<ParameterTypes...>,
557
+ typename guts::infer_function_traits_t<
558
+ KernelFunctor>::parameter_types>,
559
+ "Parameter types mismatch");
560
+
561
+ // See [Note: Argument forwarding in the dispatcher] for why ParameterTypes
562
+ // doesn't use &&
563
+ static ReturnType call(
564
+ OperatorKernel* functor,
565
+ DispatchKeySet /*unused*/,
566
+ ParameterTypes... args) {
567
+ KernelFunctor* functor_ = static_cast<KernelFunctor*>(functor);
568
+ // Note [Plumbing Keys Through The Dispatcher 2]
569
+ // See Note [Plumbing Keys Through The Dispatcher] for the background.
570
+ // This functor explicitly takes in a dispatchKeySet and drops it on the
571
+ // floor- it does not forward it to the registered kernel.
572
+ //
573
+ // This is due to the calling convention within the dispatcher, which
574
+ // expects all registered kernels to have a first argument of type
575
+ // DispatchKeySet.
576
+ // This is not the case for pretty much all manually written kernels,
577
+ // however- this functor serves to separate the calling convention of the
578
+ // dispatcher from the calling convention of manually written kernels.
579
+ return (*functor_)(std::forward<ParameterTypes>(args)...);
580
+ }
581
+ };
582
+
583
+ // This specialization is for kernels with a first argument of type
584
+ // DispatchKeySet
585
+ template <class KernelFunctor, class ReturnType, class... ParameterTypes>
586
+ struct wrap_kernel_functor_unboxed_<
587
+ KernelFunctor,
588
+ ReturnType(DispatchKeySet, ParameterTypes...)>
589
+ final {
590
+ static_assert(
591
+ std::is_same_v<
592
+ ReturnType,
593
+ typename guts::infer_function_traits_t<KernelFunctor>::return_type>,
594
+ "Return type mismatch");
595
+ static_assert(
596
+ std::is_same_v<
597
+ guts::typelist::typelist<DispatchKeySet, ParameterTypes...>,
598
+ typename guts::infer_function_traits_t<
599
+ KernelFunctor>::parameter_types>,
600
+ "Parameter types mismatch");
601
+
602
+ // See [Note: Argument forwarding in the dispatcher] for why ParameterTypes
603
+ // doesn't use &&
604
+ static ReturnType call(
605
+ OperatorKernel* functor,
606
+ DispatchKeySet dispatchKeySet,
607
+ ParameterTypes... args) {
608
+ KernelFunctor* functor_ = static_cast<KernelFunctor*>(functor);
609
+ // We're explicitly taking in a dispatchKeySet and forwarding it to the
610
+ // registered kernel. See Note [Plumbing Keys Through The Dispatcher 2] for
611
+ // details.
612
+ return (*functor_)(dispatchKeySet, std::forward<ParameterTypes>(args)...);
613
+ }
614
+ };
615
+
616
+ template <class KernelFunctor>
617
+ using wrap_kernel_functor_unboxed = wrap_kernel_functor_unboxed_<
618
+ KernelFunctor,
619
+ typename guts::infer_function_traits_t<KernelFunctor>::func_type>;
620
+
621
+ // call_functor_with_args_from_stack
622
+
623
+ template <
624
+ class Functor,
625
+ bool AllowDeprecatedTypes,
626
+ size_t... ivalue_arg_indices,
627
+ typename... ArgTypes>
628
+ std::decay_t<typename guts::infer_function_traits_t<Functor>::return_type>
629
+ call_functor_with_args_from_stack_(
630
+ OperatorKernel* functor,
631
+ DispatchKeySet dispatchKeySet,
632
+ Stack* stack,
633
+ std::index_sequence<ivalue_arg_indices...> /*unused*/,
634
+ guts::typelist::typelist<ArgTypes...>* /*unused*/) {
635
+ (void)stack; // when sizeof...(ivalue_arg_indices) == 0, this argument would
636
+ // be unused and we have to silence the compiler warning.
637
+
638
+ // We're explicitly filtering out DispatchKeySet from the argument list.
639
+ // Some kernels take a DispatchKeySet as their first argument in order to
640
+ // plumb keys through the dispatcher. We don't want to expose the
641
+ // DispatchKeySet type to jit, so we don't include this argument on the stack.
642
+ // See Note [Plumbing Keys Through The Dispatcher] for the background.
643
+ return wrap_kernel_functor_unboxed<Functor>::call(
644
+ functor,
645
+ dispatchKeySet,
646
+ ivalue_to_arg<
647
+ typename decay_if_not_tensor<ArgTypes>::type,
648
+ AllowDeprecatedTypes>::
649
+ call(torch::jit::peek(
650
+ *stack, ivalue_arg_indices, sizeof...(ivalue_arg_indices)))...);
651
+ }
652
+
653
+ template <class Functor, bool AllowDeprecatedTypes>
654
+ std::decay_t<typename guts::infer_function_traits_t<Functor>::return_type>
655
+ call_functor_with_args_from_stack(
656
+ OperatorKernel* functor,
657
+ DispatchKeySet dispatchKeySet,
658
+ Stack* stack) {
659
+ // We're explicitly filtering out DispatchKeySet from the argument list.
660
+ // Some kernels take a DispatchKeySet as their first argument in order to
661
+ // plumb keys through the dispatcher. We don't want to expose the
662
+ // DispatchKeySet type to jit, so we don't include this argument on the stack.
663
+ // See Note [Plumbing Keys Through The Dispatcher] for the background.
664
+ using ArgTypes = typename c10::remove_DispatchKeySet_arg_from_func<
665
+ Functor>::parameter_types;
666
+ constexpr size_t num_ivalue_args = guts::typelist::size<ArgTypes>::value;
667
+ return call_functor_with_args_from_stack_<Functor, AllowDeprecatedTypes>(
668
+ functor,
669
+ dispatchKeySet,
670
+ stack,
671
+ std::make_index_sequence<num_ivalue_args>(),
672
+ static_cast<ArgTypes*>(nullptr));
673
+ }
674
+
675
+ // push_outputs
676
+
677
+ template <class OutputType, bool AllowDeprecatedTypes>
678
+ struct push_outputs final {
679
+ // Contrary to [Note: Argument forwarding in the dispatcher], we use
680
+ // OutputType&& here to avoid one extra call to the move constructor in this
681
+ // case. This is still not a universal reference though because OutputType is
682
+ // an explicitly specified class template parameter.
683
+ static void call(OutputType&& output, Stack* stack) {
684
+ torch::jit::push(
685
+ *stack,
686
+ return_to_ivalue<OutputType, AllowDeprecatedTypes>::call(
687
+ std::forward<OutputType>(output)));
688
+ }
689
+ static void copy(const OutputType& output, Stack* stack) {
690
+ torch::jit::push(
691
+ *stack,
692
+ return_to_ivalue<OutputType, AllowDeprecatedTypes>::copy(output));
693
+ }
694
+ };
695
+ template <class... OutputTypes, bool AllowDeprecatedTypes>
696
+ struct push_outputs<std::tuple<OutputTypes...>, AllowDeprecatedTypes> final {
697
+ static void call(std::tuple<OutputTypes...>&& output, Stack* stack) {
698
+ call_(
699
+ std::move(output),
700
+ stack,
701
+ std::make_index_sequence<sizeof...(OutputTypes)>());
702
+ }
703
+ static void copy(const std::tuple<OutputTypes...>& output, Stack* stack) {
704
+ copy_(output, stack, std::make_index_sequence<sizeof...(OutputTypes)>());
705
+ }
706
+
707
+ private:
708
+ template <size_t... indices>
709
+ static void call_(
710
+ std::tuple<OutputTypes...>&& output,
711
+ Stack* stack,
712
+ std::index_sequence<indices...> /*unused*/) {
713
+ torch::jit::push(
714
+ *stack,
715
+ return_to_ivalue<OutputTypes, AllowDeprecatedTypes>::call(
716
+ std::forward<OutputTypes>(std::get<indices>(output)))...);
717
+ }
718
+ template <size_t... indices>
719
+ static void copy_(
720
+ const std::tuple<OutputTypes...>& output,
721
+ Stack* stack,
722
+ std::index_sequence<indices...> /*unused*/) {
723
+ torch::jit::push(
724
+ *stack,
725
+ return_to_ivalue<OutputTypes, AllowDeprecatedTypes>::copy(
726
+ std::get<indices>(output))...);
727
+ }
728
+ };
729
+ template <bool AllowDeprecatedTypes>
730
+ struct push_outputs<void, AllowDeprecatedTypes> final {
731
+ static void call(int /*dummy*/, Stack* /*stack*/) {}
732
+ static void copy(int /*dummy*/, Stack* /*stack*/) {}
733
+ };
734
+
735
+ // make_boxed_from_unboxed_functor
736
+
737
+ template <class KernelFunctor, bool AllowDeprecatedTypes>
738
+ struct make_boxed_from_unboxed_functor final {
739
+ static_assert(
740
+ std::is_base_of_v<OperatorKernel, KernelFunctor>,
741
+ "Tried to register a kernel functor using the kernel<Functor>() API, but it doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
742
+
743
+ static void call(
744
+ OperatorKernel* functor,
745
+ const OperatorHandle& /*unused*/,
746
+ DispatchKeySet dispatchKeySet,
747
+ Stack* stack) {
748
+ using ReturnType =
749
+ typename guts::infer_function_traits_t<KernelFunctor>::return_type;
750
+ // We're explicitly filtering out DispatchKeySet from the argument list.
751
+ // Some kernels take a DispatchKeySet as their first argument in order to
752
+ // plumb keys through the dispatcher. We don't want to expose the
753
+ // DispatchKeySet type to jit, so we don't include this argument on the
754
+ // stack. See Note [Plumbing Keys Through The Dispatcher] for the
755
+ // background.
756
+ using ArgTypes = typename c10::remove_DispatchKeySet_arg_from_func<
757
+ KernelFunctor>::parameter_types;
758
+ constexpr bool has_outputs = !std::is_same_v<void, ReturnType>;
759
+ constexpr size_t num_inputs = guts::typelist::size<ArgTypes>::value;
760
+ if constexpr (has_outputs) {
761
+ // Decay ReturnType to ReturnType_ so that if a reference gets returned,
762
+ // we actually store it by value and don't get a dangling reference. This
763
+ // is only required because some kernels still return `Tensor&`. [Note:
764
+ // VC++ and 'std': ambiguous symbol]
765
+ using ReturnType_ = ::std::decay_t<ReturnType>;
766
+ ReturnType_ output = call_functor_with_args_from_stack<
767
+ KernelFunctor,
768
+ AllowDeprecatedTypes>(functor, dispatchKeySet, stack);
769
+ torch::jit::drop(*stack, num_inputs);
770
+ // See note [ VC++ and 'std': ambiguous symbol]
771
+ push_outputs<ReturnType_, AllowDeprecatedTypes>::call(
772
+ ::std::move(output), stack);
773
+ } else {
774
+ call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(
775
+ functor, dispatchKeySet, stack);
776
+ torch::jit::drop(*stack, num_inputs);
777
+ }
778
+ }
779
+ };
780
+ } // namespace impl
781
+
782
+ } // namespace c10
783
+
784
+ namespace torch {
785
+ using OperatorKernel = c10::OperatorKernel;
786
+ }
787
+
788
+ #else
789
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
790
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/test_helpers.h ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <gmock/gmock.h>
5
+ #include <gtest/gtest.h>
6
+
7
+ #include <ATen/core/Tensor.h>
8
+ #include <ATen/core/dispatch/Dispatcher.h>
9
+ #include <ATen/core/ivalue.h>
10
+ #include <c10/core/CPUAllocator.h>
11
+ #include <c10/util/irange.h>
12
+
13
+ template <class... Inputs>
14
+ inline std::vector<c10::IValue> makeStack(Inputs&&... inputs) {
15
+ return {std::forward<Inputs>(inputs)...};
16
+ }
17
+
18
+ inline at::Tensor dummyTensor(
19
+ c10::DispatchKeySet ks,
20
+ bool requires_grad = false) {
21
+ auto* allocator = c10::GetCPUAllocator();
22
+ int64_t nelements = 1;
23
+ auto dtype = caffe2::TypeMeta::Make<float>();
24
+ int64_t size_bytes = nelements * dtype.itemsize();
25
+ auto storage_impl = c10::make_intrusive<c10::StorageImpl>(
26
+ c10::StorageImpl::use_byte_size_t(),
27
+ size_bytes,
28
+ allocator->allocate(size_bytes),
29
+ allocator,
30
+ /*resizable=*/true);
31
+ at::Tensor t =
32
+ at::detail::make_tensor<c10::TensorImpl>(storage_impl, ks, dtype);
33
+ // TODO: We add this to simulate the ideal case where we only have Autograd
34
+ // backend keys
35
+ // on Tensor when it requires grad. But currently Autograd keys are
36
+ // added in TensorImpl constructor by default.
37
+ if (!requires_grad) {
38
+ t.unsafeGetTensorImpl()->remove_autograd_key();
39
+ }
40
+ return t;
41
+ }
42
+
43
+ inline at::Tensor dummyTensor(
44
+ c10::DispatchKey dispatch_key,
45
+ bool requires_grad = false) {
46
+ return dummyTensor(c10::DispatchKeySet(dispatch_key), requires_grad);
47
+ }
48
+
49
+ template <class... Args>
50
+ inline std::vector<c10::IValue> callOp(
51
+ const c10::OperatorHandle& op,
52
+ Args... args) {
53
+ auto stack = makeStack(std::forward<Args>(args)...);
54
+ op.callBoxed(&stack);
55
+ return stack;
56
+ }
57
+
58
+ template <class Result, class... Args>
59
+ inline Result callOpUnboxed(const c10::OperatorHandle& op, Args... args) {
60
+ return op.typed<Result(Args...)>().call(std::forward<Args>(args)...);
61
+ }
62
+
63
+ template <class Result, class... Args>
64
+ inline Result callOpUnboxedWithDispatchKey(
65
+ const c10::OperatorHandle& op,
66
+ c10::DispatchKey dispatchKey,
67
+ Args... args) {
68
+ return op.typed<Result(Args...)>().callWithDispatchKey(
69
+ dispatchKey, std::forward<Args>(args)...);
70
+ }
71
+
72
+ template <class Result, class... Args>
73
+ inline Result callOpUnboxedWithPrecomputedDispatchKeySet(
74
+ const c10::OperatorHandle& op,
75
+ c10::DispatchKeySet ks,
76
+ Args... args) {
77
+ return op.typed<Result(Args...)>().redispatch(
78
+ ks, std::forward<Args>(args)...);
79
+ }
80
+
81
+ inline void expectDoesntFindKernel(
82
+ const char* op_name,
83
+ c10::DispatchKey dispatch_key) {
84
+ auto op = c10::Dispatcher::singleton().findSchema({op_name, ""});
85
+ EXPECT_ANY_THROW(callOp(*op, dummyTensor(dispatch_key), 5););
86
+ }
87
+
88
+ inline void expectDoesntFindOperator(const char* op_name) {
89
+ auto op = c10::Dispatcher::singleton().findSchema({op_name, ""});
90
+ EXPECT_FALSE(op.has_value());
91
+ }
92
+
93
+ template <class Exception, class Functor>
94
+ inline void expectThrows(Functor&& functor, const char* expectMessageContains) {
95
+ try {
96
+ std::forward<Functor>(functor)();
97
+ } catch (const Exception& e) {
98
+ EXPECT_THAT(e.what(), testing::HasSubstr(expectMessageContains));
99
+ return;
100
+ }
101
+ ADD_FAILURE() << "Expected to throw exception containing \""
102
+ << expectMessageContains << "\" but didn't throw";
103
+ }
104
+
105
+ template <class T, size_t N>
106
+ void expectListEquals(c10::ArrayRef<T> expected, std::array<T, N> actual) {
107
+ EXPECT_EQ(expected.size(), actual.size());
108
+ for (const auto i : c10::irange(expected.size())) {
109
+ EXPECT_EQ(expected[i], actual[i]);
110
+ }
111
+ }
112
+
113
+ template <class T>
114
+ void expectListEquals(c10::ArrayRef<T> expected, c10::ArrayRef<T> actual) {
115
+ EXPECT_EQ(expected.size(), actual.size());
116
+ for (const auto i : c10::irange(expected.size())) {
117
+ EXPECT_EQ(expected[i], actual[i]);
118
+ }
119
+ }
120
+
121
+ template <class T>
122
+ void expectListEquals(c10::ArrayRef<T> expected, c10::List<T> actual) {
123
+ EXPECT_EQ(expected.size(), actual.size());
124
+ for (const auto i : c10::irange(expected.size())) {
125
+ EXPECT_EQ(expected[i], actual.get(i));
126
+ }
127
+ }
128
+
129
+ template <class T>
130
+ void expectListEquals(c10::ArrayRef<T> expected, std::vector<T> actual) {
131
+ EXPECT_EQ(expected.size(), actual.size());
132
+ for (const auto i : c10::irange(expected.size())) {
133
+ EXPECT_EQ(expected[i], actual[i]);
134
+ }
135
+ }
136
+
137
+ // NB: This is not really sound, but all of the type sets constructed here
138
+ // are singletons so it's fine
139
+ static inline c10::DispatchKey extractDispatchKey(const at::Tensor& t) {
140
+ return legacyExtractDispatchKey(t.key_set());
141
+ }
142
+
143
+ #else
144
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
145
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/CppSignature.h ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/core/DispatchKeySet.h>
5
+ #include <c10/macros/Macros.h>
6
+ #include <c10/util/Metaprogramming.h>
7
+ #include <c10/util/Type.h>
8
+ #include <typeindex>
9
+
10
+ namespace c10::impl {
11
+
12
+ // A CppSignature object holds RTTI information about a C++ function signature
13
+ // at runtime and can compare them or get a debug-printable name.
14
+ class TORCH_API CppSignature final {
15
+ public:
16
+ CppSignature(const CppSignature&) = default;
17
+ CppSignature(CppSignature&&) noexcept = default;
18
+ CppSignature& operator=(const CppSignature&) = default;
19
+ CppSignature& operator=(CppSignature&&) noexcept = default;
20
+
21
+ template <class FuncType>
22
+ static CppSignature make() {
23
+ // Normalize functors, lambdas, function pointers, etc. into the plain
24
+ // function type The first argument of the schema might be of type
25
+ // DispatchKeySet, in which case we remove it. We do this to guarantee that
26
+ // all CppSignature's for an operator will match, even if they're registered
27
+ // with different calling conventions.
28
+ // See Note [Plumbing Keys Through The Dispatcher]
29
+ using decayed_function_type =
30
+ typename c10::remove_DispatchKeySet_arg_from_func<
31
+ std::decay_t<FuncType>>::func_type;
32
+
33
+ return CppSignature(std::type_index(typeid(decayed_function_type)));
34
+ }
35
+
36
+ std::string name() const {
37
+ return c10::demangle(signature_.name());
38
+ }
39
+
40
+ friend bool operator==(const CppSignature& lhs, const CppSignature& rhs) {
41
+ if (lhs.signature_ == rhs.signature_) {
42
+ return true;
43
+ }
44
+ // Without RTLD_GLOBAL, the type_index comparison could yield false because
45
+ // they point to different instances of the RTTI data, but the types would
46
+ // still be the same. Let's check for that case too.
47
+ // Note that there still is a case where this might not work, i.e. when
48
+ // linking libraries of different compilers together, they might have
49
+ // different ways to serialize a type name. That, together with a missing
50
+ // RTLD_GLOBAL, would still fail this.
51
+ if (0 == strcmp(lhs.signature_.name(), rhs.signature_.name())) {
52
+ return true;
53
+ }
54
+
55
+ return false;
56
+ }
57
+
58
+ private:
59
+ explicit CppSignature(std::type_index signature)
60
+ : signature_(std::move(signature)) {}
61
+ std::type_index signature_;
62
+ };
63
+
64
+ inline bool operator!=(const CppSignature& lhs, const CppSignature& rhs) {
65
+ return !(lhs == rhs);
66
+ }
67
+
68
+ } // namespace c10::impl
69
+
70
+ #else
71
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
72
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/DispatchKeyExtractor.h ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/Variadic.h>
5
+ #include <ATen/core/function_schema.h>
6
+ #include <ATen/core/jit_type.h>
7
+ #include <ATen/core/stack.h>
8
+ #include <c10/core/DispatchKeySet.h>
9
+ #include <c10/util/Bitset.h>
10
+ #include <c10/util/irange.h>
11
+ #include <cstdint>
12
+
13
+ namespace c10 {
14
+
15
+ namespace impl {
16
+
17
+ // Take a DispatchKeySet for a Tensor and determine what the actual dispatch
18
+ // DispatchKey should be, taking into account TLS, and skipping backends which
19
+ // fall through.
20
+ //
21
+ // Unlike Tensor::key_set(), the value of this on a tensor can change depending
22
+ // on TLS.
23
+ //
24
+ // NB: If there is no valid dispatch key, this will return Undefined
25
+ inline DispatchKeySet computeDispatchKeySet(
26
+ DispatchKeySet ks,
27
+ // The key mask lets us eliminate (by zero entries) keys which should not
28
+ // be considered for dispatch. There are two cases when we use this:
29
+ //
30
+ // - If an operator's dispatch table contains a fallthrough entry, we
31
+ // should bypass it entirely when finding the key
32
+ // - If a user invokes with redispatch, the mask lets us
33
+ // zero out the key the user asked us to stop.
34
+ //
35
+ // These excluded backends are NOT tracked in the TLS, but must be applied
36
+ // AFTER TLS (since the backend may have been introduced for consideration
37
+ // by the included TLS), which is why you have to pass them in to this
38
+ // function (as opposed to just applying it to the input 'ks').
39
+ DispatchKeySet key_mask) {
40
+ c10::impl::LocalDispatchKeySet local =
41
+ c10::impl::tls_local_dispatch_key_set();
42
+ // TODO: It's a bit irritating that we have to do logical ORs here, it would
43
+ // be nice to only do one. Can always_included be folded into the TLS? Well,
44
+ // it's a bit troublesome, because fastpath TLS access requires the type of
45
+ // the TLS in question to be zero-initialized, so you don't actually win
46
+ // anything in that case.
47
+ return (((ks | local.included_) - local.excluded_) & key_mask);
48
+ }
49
+
50
+ } // namespace impl
51
+
52
+ namespace detail {
53
+ // A small gadget to extract the DispatchKeySet from types which are known
54
+ // to have it. Used to extract dispatch keys from unboxed calls.
55
+ struct MultiDispatchKeySet : at::IterArgs<MultiDispatchKeySet> {
56
+ DispatchKeySet ts;
57
+ void operator()(const at::Tensor& x) {
58
+ ts = ts | x.key_set();
59
+ }
60
+ void operator()(const std::optional<at::Tensor>& x) {
61
+ if (x.has_value()) {
62
+ ts = ts | x->key_set();
63
+ }
64
+ }
65
+ void operator()(at::ArrayRef<at::Tensor> xs) {
66
+ for (const auto& x : xs) {
67
+ ts = ts | x.key_set();
68
+ }
69
+ }
70
+ // Tensor?[] translates to this case.
71
+ void operator()(const c10::List<std::optional<at::Tensor>>& xs) {
72
+ for (std::optional<at::Tensor> x : xs) {
73
+ if (x.has_value()) {
74
+ ts = ts | x.value().key_set();
75
+ }
76
+ }
77
+ }
78
+ // Structured Tensor[] translates to this case
79
+ void operator()(const at::ITensorListRef& xs) {
80
+ for (const auto& x : xs) {
81
+ ts = ts | x.key_set();
82
+ }
83
+ }
84
+ [[noreturn]] void operator()(
85
+ at::ArrayRef<std::optional<at::Tensor>> /*unused*/) {
86
+ // Just checking that the handling of Tensor?[] didn't change.
87
+ TORCH_INTERNAL_ASSERT(false);
88
+ }
89
+ void operator()(const at::Generator& gen) {
90
+ if (gen.defined()) {
91
+ ts = ts | gen.key_set();
92
+ }
93
+ }
94
+ void operator()(const std::optional<at::Generator>& gen) {
95
+ if (gen.has_value() && gen->defined()) {
96
+ ts = ts | gen->key_set();
97
+ }
98
+ }
99
+ template <typename T>
100
+ void operator()(const T& /*unused*/) {
101
+ // do nothing
102
+ }
103
+ };
104
+
105
+ // NB: take by const reference (Don't do universal forwarding here! You
106
+ // don't want to move into this function!)
107
+ template <typename... Args>
108
+ DispatchKeySet multi_dispatch_key_set(const Args&... args) {
109
+ return MultiDispatchKeySet().apply(args...).ts;
110
+ }
111
+ } // namespace detail
112
+
113
+ /**
114
+ * An instance of DispatchKeyExtractor knows how to get a dispatch key given
115
+ * a list of arguments for an operator call.
116
+ *
117
+ * The instance is specific for a certain operator as:
118
+ * - In boxed dispatch, different operators have different ways to extract
119
+ * the dispatch key (e.g. different numbers of arguments), and we precompute
120
+ * the stack locations we should look at; and
121
+ * - In all dispatch, some backends should be excluded from dispatch because
122
+ * they have been registered as fallthrough. The set of excluded backends
123
+ * varies from operator, as some operators may have overridden the
124
+ * fallthrough with custom behavior.
125
+ *
126
+ * Note - this should maintain identical impl to the py dispatcher key
127
+ * extraction logic at pytorch/torch/dispatcher.py
128
+ */
129
+ struct TORCH_API DispatchKeyExtractor final {
130
+ public:
131
+ static DispatchKeyExtractor make(const FunctionSchema& schema) {
132
+ return DispatchKeyExtractor(makeBitsetForDispatchArgs(schema));
133
+ }
134
+
135
+ static DispatchKeyExtractor makeUninitialized() {
136
+ return DispatchKeyExtractor(c10::utils::bitset());
137
+ }
138
+
139
+ void registerSchema(const FunctionSchema& schema) {
140
+ TORCH_INTERNAL_ASSERT(dispatch_arg_indices_reverse_.is_entirely_unset());
141
+ dispatch_arg_indices_reverse_ = makeBitsetForDispatchArgs(schema);
142
+ }
143
+ void deregisterSchema() {
144
+ dispatch_arg_indices_reverse_ = c10::utils::bitset();
145
+ }
146
+
147
+ DispatchKeySet getDispatchKeySetBoxed(const torch::jit::Stack* stack) const {
148
+ DispatchKeySet ks;
149
+ dispatch_arg_indices_reverse_.for_each_set_bit([&](size_t
150
+ reverse_arg_index) {
151
+ const auto& ivalue = torch::jit::peek(*stack, 0, reverse_arg_index + 1);
152
+ if (C10_LIKELY(ivalue.isTensor())) {
153
+ // NB: Take care not to introduce a refcount bump (there's
154
+ // no safe toTensorRef method, alas)
155
+ ks = ks | ivalue.unsafeToTensorImpl()->key_set();
156
+ } else if (C10_UNLIKELY(ivalue.isTensorList())) {
157
+ // NB: use toListRef as it doesn't induce refcount bumps
158
+ // (toTensorListRef is not a thing)
159
+ for (const auto& nv : ivalue.toListRef()) {
160
+ auto* tensor = nv.unsafeToTensorImpl();
161
+ ks = ks | tensor->key_set();
162
+ }
163
+ }
164
+ // Tensor?[] translates to a c10::List<IValue> so we need to peek inside
165
+ else if (C10_UNLIKELY(ivalue.isList())) {
166
+ for (const auto& elt : ivalue.toListRef()) {
167
+ if (elt.isTensor()) {
168
+ ks = ks | elt.toTensor().key_set();
169
+ }
170
+ }
171
+ }
172
+ });
173
+ // Keys that are fallthrough should be skipped
174
+ if (requiresBitsetPerBackend_) {
175
+ c10::impl::LocalDispatchKeySet tls =
176
+ c10::impl::tls_local_dispatch_key_set();
177
+ auto backend_idx =
178
+ ((ks | tls.included_) - tls.excluded_).getBackendIndex();
179
+ return impl::computeDispatchKeySet(
180
+ ks, nonFallthroughKeysPerBackend_[backend_idx]);
181
+ } else {
182
+ return impl::computeDispatchKeySet(ks, nonFallthroughKeys_);
183
+ }
184
+ }
185
+
186
+ template <class... Args>
187
+ DispatchKeySet getDispatchKeySetUnboxed(const Args&... args) const {
188
+ auto ks = detail::multi_dispatch_key_set(args...);
189
+ // Keys that are fallthrough should be skipped
190
+ if (requiresBitsetPerBackend_) {
191
+ c10::impl::LocalDispatchKeySet tls =
192
+ c10::impl::tls_local_dispatch_key_set();
193
+ auto backend_idx =
194
+ ((ks | tls.included_) - tls.excluded_).getBackendIndex();
195
+ return impl::computeDispatchKeySet(
196
+ ks, nonFallthroughKeysPerBackend_[backend_idx]);
197
+ } else {
198
+ return impl::computeDispatchKeySet(ks, nonFallthroughKeys_);
199
+ }
200
+ }
201
+
202
+ void setOperatorHasFallthroughForKey(DispatchKey k, bool has_fallthrough);
203
+
204
+ std::string dumpState() const;
205
+ void checkInvariants(const FunctionSchema& schema) const;
206
+
207
+ private:
208
+ static bool isDispatchType(const Type& type) {
209
+ // Checking isSubtypeOf on a DynamicType heap-allocates a
210
+ // DynamicType version of the argument if it's not a DynamicType
211
+ // already, and this has measurable overhead during startup.
212
+ #ifdef C10_MOBILE
213
+ struct CachedTypes {
214
+ DynamicTypePtr listOfTensors;
215
+ DynamicTypePtr listOfOptionalTensors;
216
+ DynamicTypePtr optionalOfTensor;
217
+ };
218
+ static const CachedTypes ct = {
219
+ DynamicType::create(*ListType::ofTensors()),
220
+ DynamicType::create(*ListType::ofOptionalTensors()),
221
+ DynamicType::create(*OptionalType::ofTensor())};
222
+ return type.isSubtypeOf(c10::TypeFactory::get<TensorType>()) ||
223
+ type.isSubtypeOf(ct.listOfTensors) ||
224
+ type.isSubtypeOf(ct.listOfOptionalTensors) ||
225
+ type.isSubtypeOf(ct.optionalOfTensor);
226
+ #else // C10_MOBILE
227
+ return type.isSubtypeOf(*TensorType::get()) ||
228
+ type.isSubtypeOf(*ListType::ofTensors()) ||
229
+ type.isSubtypeOf(*ListType::ofOptionalTensors()) ||
230
+ type.isSubtypeOf(*OptionalType::ofTensor());
231
+ #endif // C10_MOBILE
232
+ }
233
+ static c10::utils::bitset makeBitsetForDispatchArgs(
234
+ const FunctionSchema& schema) {
235
+ TORCH_CHECK(
236
+ schema.arguments().size() <= c10::utils::bitset::NUM_BITS(),
237
+ "The function schema has ",
238
+ schema.arguments().size(),
239
+ " arguments but this PyTorch build only supports ",
240
+ c10::utils::bitset::NUM_BITS());
241
+ c10::utils::bitset dispatch_arg_indices_reverse;
242
+ for (const auto index : c10::irange(schema.arguments().size())) {
243
+ if (isDispatchType(*schema.arguments()[index].type())) {
244
+ dispatch_arg_indices_reverse.set(schema.arguments().size() - 1 - index);
245
+ }
246
+ }
247
+ return dispatch_arg_indices_reverse;
248
+ }
249
+
250
+ explicit DispatchKeyExtractor(c10::utils::bitset dispatch_arg_indices_reverse)
251
+ : dispatch_arg_indices_reverse_(dispatch_arg_indices_reverse),
252
+ nonFallthroughKeys_(DispatchKeySet::FULL) {
253
+ for (const auto i : c10::irange(nonFallthroughKeysPerBackend_.size())) {
254
+ nonFallthroughKeysPerBackend_[i] = DispatchKeySet::FULL;
255
+ }
256
+ }
257
+
258
+ // this is a bitset that has ones for each argument index which has to be
259
+ // considered for dispatch. This avoids having to iterate over the stack
260
+ // to find all the tensors. The bits are stored in reverse order, i.e.
261
+ // dispatch_arg_indices_reverse_[i] == true, then the i-th argument from
262
+ // the top of the stack (i.e. the i-th last argument of the function)
263
+ // is relevant for dispatch.
264
+ // dispatch_arg_indices_reverse_ is allowed to have zero bits set; that just
265
+ // means you must do the fallthrough
266
+ c10::utils::bitset dispatch_arg_indices_reverse_;
267
+
268
+ // Set of functionality keys for which the operator does NOT have fallthrough
269
+ // kernel.
270
+ DispatchKeySet nonFallthroughKeys_;
271
+ // Set of functionality keys for which the operator does NOT have fallthrough
272
+ // kernel, defined PER BACKEND. This is only needed if we know that the
273
+ // operator has a different set of fallthroughs defined for some backends.
274
+ std::array<DispatchKeySet, num_backends> nonFallthroughKeysPerBackend_;
275
+ // Flag to tell us if we can use the single set of nonFallthroughKeys_ (fast
276
+ // path), or if we need to fall back to the slower path and check
277
+ // nonFallthroughKeysPerBackend_
278
+ bool requiresBitsetPerBackend_{false};
279
+ };
280
+
281
+ } // namespace c10
282
+
283
+ #else
284
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
285
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h ADDED
@@ -0,0 +1,955 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/SequenceNumber.h>
5
+ #include <ATen/core/boxing/KernelFunction.h>
6
+ #include <ATen/core/boxing/impl/boxing.h>
7
+ #include <ATen/core/dispatch/CppSignature.h>
8
+ #include <ATen/core/dispatch/OperatorEntry.h>
9
+ #include <ATen/core/dispatch/RegistrationHandleRAII.h>
10
+ #include <ATen/record_function.h>
11
+ #include <c10/core/SafePyObject.h>
12
+ #include <c10/util/Exception.h>
13
+ #include <c10/util/LeftRight.h>
14
+ #include <condition_variable>
15
+ #include <list>
16
+ #include <mutex>
17
+ #include <type_traits>
18
+
19
+ #include <ATen/core/enum_tag.h>
20
+ #include <ATen/core/grad_mode.h>
21
+
22
+ #ifndef NDEBUG
23
+ #include <iostream>
24
+ #endif
25
+
26
+ namespace c10 {
27
+
28
+ TORCH_API bool show_dispatch_trace();
29
+ TORCH_API void dispatch_trace_nesting_incr();
30
+ TORCH_API void dispatch_trace_nesting_decr();
31
+ TORCH_API int64_t dispatch_trace_nesting_value();
32
+
33
+ struct DispatchTraceNestingGuard {
34
+ DispatchTraceNestingGuard() {
35
+ dispatch_trace_nesting_incr();
36
+ }
37
+ ~DispatchTraceNestingGuard() {
38
+ dispatch_trace_nesting_decr();
39
+ }
40
+ };
41
+
42
+ class TORCH_API OperatorHandle;
43
+ template <class FuncType>
44
+ class TypedOperatorHandle;
45
+
46
+ /**
47
+ * Implement this interface and register your instance with the dispatcher
48
+ * to get notified when operators are registered or deregistered with
49
+ * the dispatcher.
50
+ *
51
+ * NB: registration events only occur when a 'def' occurs; we don't trigger
52
+ * on 'impl' or 'fallback' calls.
53
+ */
54
+ class TORCH_API OpRegistrationListener {
55
+ public:
56
+ virtual ~OpRegistrationListener();
57
+
58
+ virtual void onOperatorRegistered(const OperatorHandle& op) = 0;
59
+ virtual void onOperatorDeregistered(const OperatorHandle& op) = 0;
60
+ };
61
+
62
+ namespace detail {
63
+ class RegistrationListenerList;
64
+ }
65
+ class SchemaRegistrationHandleRAII;
66
+
67
+ /**
68
+ * Top-level dispatch interface for dispatching via the dynamic dispatcher.
69
+ * Most end users shouldn't use this directly; if you're trying to register
70
+ * ops look in op_registration
71
+ */
72
+ class TORCH_API Dispatcher final {
73
+ private:
74
+ // For direct access to backend fallback information
75
+ friend class impl::OperatorEntry;
76
+
77
+ struct OperatorDef final {
78
+ explicit OperatorDef(OperatorName&& op_name) : op(std::move(op_name)) {}
79
+
80
+ impl::OperatorEntry op;
81
+
82
+ // These refer to the number of outstanding RegistrationHandleRAII
83
+ // for this operator. def_count reflects only def() registrations
84
+ // (in the new world, this should only ever be 1, but old style
85
+ // registrations may register the schema multiple times, which
86
+ // will increase this count). def_and_impl_count reflects the number
87
+ // of combined def() and impl() registrations. When the last def() gets
88
+ // unregistered, we must immediately call the Deregistered listeners, but we
89
+ // must not actually delete the handle as there are other outstanding RAII
90
+ // destructors which will try to destruct and they had better still have a
91
+ // working operator handle in this case
92
+ size_t def_count = 0;
93
+ size_t def_and_impl_count = 0;
94
+ };
95
+ friend class OperatorHandle;
96
+ template <class>
97
+ friend class TypedOperatorHandle;
98
+
99
+ struct Guard final {
100
+ Guard() : alive(true) {}
101
+ std::atomic<bool> alive;
102
+ std::mutex mutex;
103
+ };
104
+
105
+ public:
106
+ ~Dispatcher();
107
+
108
+ // Implementation note: this class abstracts over the fact that we have
109
+ // per-operator dispatch tables. This could be easily adjusted to have a
110
+ // single global hash table.
111
+ static Dispatcher& realSingleton();
112
+
113
+ C10_ALWAYS_INLINE static Dispatcher& singleton() {
114
+ #if !defined C10_MOBILE
115
+ // Implemented inline so that steady-state code needn't incur
116
+ // function-call overhead. We can't just inline `realSingleton`
117
+ // because the function-local static would get duplicated across
118
+ // all DSOs that include & use this header, leading to multiple
119
+ // singleton instances.
120
+ static Dispatcher& s = realSingleton();
121
+ return s;
122
+ #else
123
+ // For C10_MOBILE, we should never inline a static function that
124
+ // has a static member, since the generated code calls
125
+ // __cxa_guard_acquire and __cxa_guard_release which help
126
+ // implement exactly once semantics for the initialization of the
127
+ // static Dispatcher& s above (for the non-mobile case). That
128
+ // additional code when duplicated across all operator stubs
129
+ // for every backend results in a lot of additional code
130
+ // being generated by the compiler.
131
+ return realSingleton();
132
+ #endif
133
+ }
134
+
135
+ // ------------------------------------------------------------------------
136
+ //
137
+ // Accessing operators by schema
138
+ //
139
+ // ------------------------------------------------------------------------
140
+
141
+ /**
142
+ * Looks for an operator schema with the given name and overload name
143
+ * and returns it if it is registered WITH A SCHEMA.
144
+ * Returns nullopt otherwise.
145
+ */
146
+ std::optional<OperatorHandle> findSchema(const OperatorName& operator_name);
147
+
148
+ /**
149
+ * Variant of findSchema that results in less code generated at the call site.
150
+ * It (1) takes const char* pointer rather than OperatorName (so we skip
151
+ * generating std::string constructor calls at the call site), and (2)
152
+ * it raises an exception if the operator is not found (so we skip
153
+ * generating exception raising code at the call site)
154
+ *
155
+ * Irritatingly, we still have to generate the handful of instructions
156
+ * for dealing with an exception being thrown during static initialization
157
+ * (e.g. __cxa_guard_abort). If we could annotate this method noexcept we
158
+ * could avoid this code too, but as the name of the function suggests,
159
+ * it does throw exceptions.
160
+ */
161
+ OperatorHandle findSchemaOrThrow(const char* name, const char* overload_name);
162
+
163
+ // Like findSchema, but also returns OperatorHandle even if there is no schema
164
+ std::optional<OperatorHandle> findOp(const OperatorName& operator_name);
165
+
166
+ // Returns a list of all operator names present in the operatorLookupTable_
167
+ const std::vector<OperatorName> getAllOpNames();
168
+
169
+ // Returns a list of all operator names present in the operatorLookupTable_
170
+ // for a given dispatch key
171
+ const std::vector<OperatorName> getAllOpNamesForDispatchKey(DispatchKey k);
172
+
173
+ // ------------------------------------------------------------------------
174
+ //
175
+ // Invoking operators
176
+ //
177
+ // ------------------------------------------------------------------------
178
+
179
+ template <class Return, class... Args>
180
+ Return call(const TypedOperatorHandle<Return(Args...)>& op, Args... args)
181
+ const;
182
+
183
+ template <class Return, class... Args>
184
+ static Return callWithDispatchKeySlowPath(
185
+ const TypedOperatorHandle<Return(Args...)>& op,
186
+ at::StepCallbacks& stepCallbacks,
187
+ DispatchKeySet dispatchKeySet,
188
+ const KernelFunction& kernel,
189
+ Args... args);
190
+
191
+ // Like call, but intended for use in a redispatch in kernels that have
192
+ // explicitly performed the DispatchKey update calculatulation. This will take
193
+ // the DispatchKeySet completely as is and dispatch to the kernel of the
194
+ // corresponding highest priority key in the set. Note that this version of
195
+ // redispatch treats the inputted DispatchKeySet *as is*, and does NOT mask
196
+ // out the highest priority key. See Note [Plumbing Keys Through The
197
+ // Dispatcher]
198
+ template <class Return, class... Args>
199
+ Return redispatch(
200
+ const TypedOperatorHandle<Return(Args...)>& op,
201
+ DispatchKeySet currentDispatchKeySet,
202
+ Args... args) const;
203
+
204
+ // Invoke an operator via the boxed calling convention using an IValue stack
205
+ void callBoxed(const OperatorHandle& op, Stack* stack) const;
206
+ void callBoxedForDispatchKey(
207
+ const OperatorHandle& op,
208
+ DispatchKey dk,
209
+ Stack* stack) const;
210
+
211
+ // TODO: This will only be useful if we write a backend fallback that plumbs
212
+ // dispatch keys (currently there are none) See Note [Plumbing Keys Through
213
+ // The Dispatcher]
214
+ void redispatchBoxed(
215
+ const OperatorHandle& op,
216
+ DispatchKeySet dispatchKeySet,
217
+ Stack* stack) const;
218
+
219
+ bool hasBackendFallbackForDispatchKey(DispatchKey dk) {
220
+ auto dispatch_ix = getDispatchTableIndexForDispatchKey(dk);
221
+ if (dispatch_ix < 0)
222
+ return false;
223
+ return backendFallbackKernels_[dispatch_ix].kernel.isValid();
224
+ }
225
+
226
+ // Used by torchdeploy/multipy for multiple // codespell:ignore: multipy
227
+ // interpreters racing.
228
+ void waitForDef(const FunctionSchema& schema);
229
+ void waitForImpl(
230
+ const OperatorName& op_name,
231
+ std::optional<DispatchKey> dispatch_key);
232
+
233
+ // ------------------------------------------------------------------------
234
+ //
235
+ // Performing registrations (NON user public; use op_registration)
236
+ //
237
+ // ------------------------------------------------------------------------
238
+
239
+ /**
240
+ * Register a new operator schema.
241
+ *
242
+ * If a schema with the same operator name and overload name already exists,
243
+ * this function will check that both schemas are exactly identical.
244
+ */
245
+ RegistrationHandleRAII registerDef(
246
+ FunctionSchema schema,
247
+ std::string debug,
248
+ std::vector<at::Tag> tags = {});
249
+
250
+ /**
251
+ * Register a kernel to the dispatch table for an operator.
252
+ * If dispatch_key is nullopt, then this registers a fallback kernel.
253
+ *
254
+ * @return A RAII object that manages the lifetime of the registration.
255
+ * Once that object is destructed, the kernel will be deregistered.
256
+ */
257
+ // NB: steals the inferred function schema, as we may need to hold on to
258
+ // it for a bit until the real schema turns up
259
+ RegistrationHandleRAII registerImpl(
260
+ OperatorName op_name,
261
+ std::optional<DispatchKey> dispatch_key,
262
+ KernelFunction kernel,
263
+ std::optional<impl::CppSignature> cpp_signature,
264
+ std::unique_ptr<FunctionSchema> inferred_function_schema,
265
+ std::string debug);
266
+
267
+ /**
268
+ * Given an operator, tells the Dispatcher that we have implemented a fake
269
+ * impl for this op in the given Python module. Call this a "pystub".
270
+ */
271
+ RegistrationHandleRAII registerPythonModule(
272
+ const OperatorName& op_name,
273
+ const char* pymodule,
274
+ const char* context);
275
+
276
+ /**
277
+ * Given an operator, throws if we have a pystub.
278
+ */
279
+ void throwIfHasPythonModule(OperatorName op_name);
280
+
281
+ std::optional<std::pair<const char*, const char*>> getPyStub(
282
+ OperatorName op_name);
283
+
284
+ /**
285
+ * Register a new operator by name.
286
+ */
287
+ RegistrationHandleRAII registerName(OperatorName op_name);
288
+
289
+ /**
290
+ * Register a fallback kernel for a backend.
291
+ * If an operator is called but there is no concrete kernel for the dispatch
292
+ * key of the given operator arguments, it will check if there is such a
293
+ * fallback kernel for the given dispatch key and, if yes, call that one.
294
+ */
295
+ RegistrationHandleRAII registerFallback(
296
+ DispatchKey dispatch_key,
297
+ KernelFunction kernel,
298
+ std::string debug);
299
+
300
+ /**
301
+ * Use to register whenever we had a TORCH_LIBRARY declaration in the frontend
302
+ * API. These invocations are only permitted once per program, so we raise
303
+ * an error if this is called again for the same namespace.
304
+ */
305
+ RegistrationHandleRAII registerLibrary(std::string ns, std::string debug);
306
+
307
+ // ------------------------------------------------------------------------
308
+ //
309
+ // Listeners on registrations
310
+ //
311
+ // ------------------------------------------------------------------------
312
+
313
+ /**
314
+ * Add a listener that gets called whenever a new op is registered or an
315
+ * existing op is deregistered. Immediately after registering, this listener
316
+ * gets called for all previously registered ops, so it can be used to keep
317
+ * track of ops registered with this dispatcher.
318
+ */
319
+ RegistrationHandleRAII addRegistrationListener(
320
+ std::unique_ptr<OpRegistrationListener> listener);
321
+
322
+ void checkInvariants() const;
323
+
324
+ //
325
+ // ------------------------------------------------------------------------
326
+ //
327
+ // Assertions
328
+ //
329
+ // ------------------------------------------------------------------------
330
+
331
+ /**
332
+ * For testing purposes.
333
+ * Returns a list of all operators that were created through calls to
334
+ * registerImpl(), without any corresponding calls to registerDef(). After
335
+ * static initialization is done this is almost certainly a bug, as the
336
+ * created OperatorHandle won't have any schema associated with it and users
337
+ * calling the op through the dispatcher won't be able to access it
338
+ *
339
+ * Note that we cannot enforce this invariant "as we go" during static
340
+ * initialization, due to undefined static initialization order- we have no
341
+ * guarantees over the order in which .def() and .impl() calls are registered
342
+ * in the dispatcher at static initialization time. So this function should
343
+ * only be called after static initialization.
344
+ */
345
+ std::vector<OperatorHandle> findDanglingImpls() const;
346
+
347
+ /**
348
+ * Useful for inspecting global Dispatcher registration state.
349
+ * Returns the names of all operators with a kernel registered for the
350
+ * specified DispatchKey. If no DispatchKey is specified, it returns all
351
+ * registered operators.
352
+ */
353
+ std::vector<OperatorName> getRegistrationsForDispatchKey(
354
+ std::optional<DispatchKey> k) const;
355
+
356
+ private:
357
+ Dispatcher();
358
+
359
+ static int64_t sequenceNumberForRunningRecordFunction(
360
+ DispatchKey dispatchKey,
361
+ DispatchKeySet dispatchKeySet);
362
+ static void runRecordFunction(
363
+ at::RecordFunction& guard,
364
+ at::RecordFunction::schema_ref_t schema_ref,
365
+ DispatchKey dispatchKey,
366
+ DispatchKeySet dispatchKeySet);
367
+ static void runRecordFunction(
368
+ at::RecordFunction& guard,
369
+ at::RecordFunction::schema_ref_t schema_ref,
370
+ DispatchKey dispatchKey,
371
+ DispatchKeySet dispatchKeySet,
372
+ c10::ArrayRef<const c10::IValue> args);
373
+
374
+ #ifdef FBCODE_CAFFE2
375
+ static bool profilingOperatorEvents();
376
+ static void fireOpStartUSDT(
377
+ at::RecordFunction::schema_ref_t schema_ref,
378
+ std::vector<void*>& argsAddresses,
379
+ std::vector<const char*>& argsTypes);
380
+ static void fireOpEndUSDT(at::RecordFunction::schema_ref_t schema_ref);
381
+ #endif // FBCODE_CAFFE2
382
+
383
+ OperatorHandle findOrRegisterSchema_(FunctionSchema&& schema);
384
+ OperatorHandle findOrRegisterName_(const OperatorName& op_name);
385
+
386
+ void deregisterDef_(const OperatorHandle& op, const OperatorName& op_name);
387
+ void deregisterImpl_(
388
+ const OperatorHandle& op,
389
+ const OperatorName& op_name,
390
+ std::optional<DispatchKey> dispatch_key,
391
+ impl::OperatorEntry::AnnotatedKernelContainerIterator kernel_handle);
392
+ void deregisterName_(const OperatorHandle& op, const OperatorName& op_name);
393
+ void deregisterFallback_(DispatchKey dispatchKey);
394
+ void deregisterLibrary_(const std::string& ns);
395
+ void cleanup(const OperatorHandle& op, const OperatorName& op_name);
396
+ void checkSchemaCompatibility(
397
+ const OperatorHandle& op,
398
+ const FunctionSchema& schema,
399
+ const std::string& debug);
400
+
401
+ std::list<OperatorDef> operators_;
402
+ #if !defined(C10_MOBILE)
403
+ LeftRight<ska::flat_hash_map<OperatorName, OperatorHandle>>
404
+ operatorLookupTable_;
405
+ #else
406
+ RWSafeLeftRightWrapper<ska::flat_hash_map<OperatorName, OperatorHandle>>
407
+ operatorLookupTable_;
408
+ #endif
409
+ // Map from namespace to debug string (saying, e.g., where the library was
410
+ // defined)
411
+ ska::flat_hash_map<std::string, std::string> libraries_;
412
+
413
+ std::array<impl::AnnotatedKernel, num_runtime_entries>
414
+ backendFallbackKernels_;
415
+
416
+ std::unique_ptr<detail::RegistrationListenerList> listeners_;
417
+
418
+ // This condition variable gets notified whenever we add a new def/impl to the
419
+ // dispatch table. This is primarily used by multiply/torchdeploy, when
420
+ // we have multiple interpreters trying to register to the dispatch table.
421
+ // In this situation, whenever the non-primary interpreter would have tried
422
+ // to register to the dispatch table, instead it will check to see if the
423
+ // expected registration has already been made, and if it hasn't, wait on
424
+ // this condition variable to see if it was just racing with the primary
425
+ // interpreter.
426
+ //
427
+ // We expect it to be rare for there to be any waiters on this condition
428
+ // variable. This is mostly just to help give better diagnostics if
429
+ // something goes horribly wrong
430
+ std::condition_variable cond_var_;
431
+
432
+ // Protect concurrent access to the dispatcher. We store this in a
433
+ // `shared_ptr` as we return callbacks that call back into dispatcher methods,
434
+ // and we need to be able to handle and guard against the event when the
435
+ // `Dispatcher` has been destroyed before the callbacks fire.
436
+ std::shared_ptr<Guard> guard_;
437
+ };
438
+
439
+ /**
440
+ * This is a handle to an operator schema registered with the dispatcher.
441
+ * This handle can be used to register kernels with the dispatcher or
442
+ * to lookup a kernel for a certain set of arguments.
443
+ */
444
+ class TORCH_API OperatorHandle {
445
+ template <typename T>
446
+ friend struct std::hash;
447
+
448
+ public:
449
+ OperatorHandle(OperatorHandle&&) noexcept = default;
450
+ OperatorHandle& operator=(OperatorHandle&&) noexcept = default;
451
+ OperatorHandle(const OperatorHandle&) = default;
452
+ OperatorHandle& operator=(const OperatorHandle&) = default;
453
+ // NOLINTNEXTLINE(performance-trivially-destructible)
454
+ ~OperatorHandle();
455
+
456
+ const OperatorName& operator_name() const {
457
+ return operatorDef_->op.operator_name();
458
+ }
459
+
460
+ bool hasSchema() const {
461
+ return operatorDef_->op.hasSchema();
462
+ }
463
+
464
+ const FunctionSchema& schema() const {
465
+ return operatorDef_->op.schema();
466
+ }
467
+
468
+ const std::string& debug() const {
469
+ return operatorDef_->op.debug();
470
+ }
471
+
472
+ std::string dumpState() const {
473
+ return operatorDef_->op.dumpState();
474
+ }
475
+
476
+ bool hasKernelForDispatchKey(DispatchKey k) const {
477
+ return operatorDef_->op.hasKernelForDispatchKey(k);
478
+ }
479
+
480
+ bool isKernelFallthroughKernel(DispatchKey k) const {
481
+ return operatorDef_->op.kernelForDispatchKey(k).isFallthrough();
482
+ }
483
+
484
+ bool hasKernelForAnyDispatchKey(DispatchKeySet k) const {
485
+ return operatorDef_->op.hasKernelForAnyDispatchKey(k);
486
+ }
487
+
488
+ bool hasComputedKernelForDispatchKey(DispatchKey k) const {
489
+ return operatorDef_->op.hasComputedKernelForDispatchKey(k);
490
+ }
491
+
492
+ SafeKernelFunction getComputedKernelForDispatchKey(DispatchKey k) const {
493
+ return operatorDef_->op.getComputedKernelForDispatchKey(k);
494
+ }
495
+
496
+ std::string dumpComputedTable() const {
497
+ return operatorDef_->op.dumpComputedTable();
498
+ }
499
+
500
+ void checkInvariants() const {
501
+ operatorDef_->op.checkInvariants();
502
+ }
503
+
504
+ c10::ArrayRef<at::Tag> getTags() const {
505
+ return operatorDef_->op.getTags();
506
+ }
507
+
508
+ void setReportErrorCallback_(std::unique_ptr<c10::SafePyObject> callback) {
509
+ operatorDef_->op.setReportErrorCallback_(std::move(callback));
510
+ }
511
+
512
+ bool hasTag(const at::Tag& tag) const {
513
+ for (const auto& tag_ : getTags()) {
514
+ if (tag == tag_) {
515
+ return true;
516
+ }
517
+ }
518
+ return false;
519
+ }
520
+
521
+ template <class FuncType>
522
+ TypedOperatorHandle<FuncType> typed() const {
523
+ // NB: This assert is not 100% sound: you can retrieve a typed() operator
524
+ // handle prior to ANY C++ signature being registered on the operator
525
+ // and the check will say everything is OK (at which point you can then
526
+ // smuggle in a kernel that is typed incorrectly). For everything
527
+ // in core library this won't happen, because all the static registrations
528
+ // will be done by the time a typed() handle is acquired.
529
+ #if !defined C10_MOBILE
530
+ operatorDef_->op.assertSignatureIsCorrect<FuncType>();
531
+ if (fn_has_symint<FuncType>::value) {
532
+ operatorDef_->op.assertSignatureIsCorrect<
533
+ typename fn_remove_symint<FuncType>::type>();
534
+ }
535
+ #endif
536
+ return TypedOperatorHandle<FuncType>(operatorIterator_);
537
+ }
538
+
539
+ void callBoxed(Stack* stack) const {
540
+ c10::Dispatcher::singleton().callBoxed(*this, stack);
541
+ }
542
+
543
+ void callBoxed(Stack& stack) const {
544
+ callBoxed(&stack);
545
+ }
546
+
547
+ void callBoxedForDispatchKey(DispatchKey dk, Stack& stack) const {
548
+ c10::Dispatcher::singleton().callBoxedForDispatchKey(*this, dk, &stack);
549
+ }
550
+
551
+ void redispatchBoxed(DispatchKeySet ks, Stack* stack) const {
552
+ c10::Dispatcher::singleton().redispatchBoxed(*this, ks, stack);
553
+ }
554
+
555
+ template <typename F>
556
+ PyObject* getPythonOp(
557
+ c10::impl::PyInterpreter* self_interpreter,
558
+ F slow_accessor) const {
559
+ return operatorDef_->op.getPythonOp(self_interpreter, slow_accessor);
560
+ }
561
+
562
+ bool operator==(const OperatorHandle& other) const {
563
+ return operatorDef_ == other.operatorDef_;
564
+ }
565
+
566
+ bool operator!=(const OperatorHandle& other) const {
567
+ return operatorDef_ != other.operatorDef_;
568
+ }
569
+
570
+ private:
571
+ explicit OperatorHandle(
572
+ std::list<Dispatcher::OperatorDef>::iterator operatorIterator)
573
+ : operatorDef_(&*operatorIterator), operatorIterator_(operatorIterator) {}
574
+ friend class Dispatcher;
575
+ template <class>
576
+ friend class TypedOperatorHandle;
577
+
578
+ // Storing a direct pointer to the OperatorDef even though we
579
+ // already have the iterator saves an instruction in the critical
580
+ // dispatch path. The iterator is effectively a
581
+ // pointer-to-std::list-node, and (at least in libstdc++'s
582
+ // implementation) the element is at an offset 16 bytes from that,
583
+ // because the prev/next pointers come first in the list node
584
+ // struct. So, an add instruction would be necessary to convert from the
585
+ // iterator to an OperatorDef*.
586
+ Dispatcher::OperatorDef* operatorDef_;
587
+
588
+ // We need to store this iterator in order to make
589
+ // Dispatcher::cleanup() fast -- it runs a lot on program
590
+ // termination (and presumably library unloading).
591
+ std::list<Dispatcher::OperatorDef>::iterator operatorIterator_;
592
+ };
593
+
594
+ /**
595
+ * This is a handle to an operator schema registered with the dispatcher.
596
+ * It holds the same information as an OperatorHandle, but it is templated
597
+ * on the operator arguments and allows calling the operator in an
598
+ * unboxed way.
599
+ */
600
+ template <class FuncType>
601
+ class TypedOperatorHandle final {
602
+ static_assert(
603
+ guts::false_t<FuncType>(),
604
+ "FuncType in OperatorHandle::typed<FuncType> was not a valid function type");
605
+ };
606
+ template <class Return, class... Args>
607
+ class TypedOperatorHandle<Return(Args...)> final : public OperatorHandle {
608
+ public:
609
+ TypedOperatorHandle(TypedOperatorHandle&&) noexcept = default;
610
+ TypedOperatorHandle& operator=(TypedOperatorHandle&&) noexcept = default;
611
+ TypedOperatorHandle(const TypedOperatorHandle&) = default;
612
+ TypedOperatorHandle& operator=(const TypedOperatorHandle&) = default;
613
+
614
+ // See [Note: Argument forwarding in the dispatcher] for why Args doesn't use
615
+ // &&
616
+ C10_ALWAYS_INLINE Return call(Args... args) const {
617
+ return c10::Dispatcher::singleton().call<Return, Args...>(
618
+ *this, std::forward<Args>(args)...);
619
+ }
620
+
621
+ // See [Note: Argument forwarding in the dispatcher] for why Args doesn't use
622
+ // &&
623
+ C10_ALWAYS_INLINE Return
624
+ redispatch(DispatchKeySet currentDispatchKeySet, Args... args) const {
625
+ return c10::Dispatcher::singleton().redispatch<Return, Args...>(
626
+ *this, currentDispatchKeySet, std::forward<Args>(args)...);
627
+ }
628
+
629
+ private:
630
+ explicit TypedOperatorHandle(
631
+ std::list<Dispatcher::OperatorDef>::iterator operatorIterator)
632
+ : OperatorHandle(operatorIterator) {}
633
+ friend class OperatorHandle;
634
+ };
635
+
636
+ namespace detail {
637
+ template <class... Args>
638
+ inline void unused_arg_(const Args&... /*unused*/) {}
639
+
640
+ // CaptureKernelCall is intended to capture return values from Dispatcher
641
+ // unboxed kernel calls. A record function may request to get outputs from the
642
+ // kernel calls. For boxed kernels, it's straightforward, the returned values
643
+ // are in the stack object. The stack can be passed to record functions. For
644
+ // unboxed kernels, we need to handle different kinds of return values, cache
645
+ // them temporarily, then release the values for the actual function call
646
+ // return.
647
+ template <typename ReturnType>
648
+ struct CaptureKernelCall {
649
+ template <typename F, typename... Args>
650
+ CaptureKernelCall(
651
+ const F& kernel,
652
+ const TypedOperatorHandle<ReturnType(Args...)>& op,
653
+ const DispatchKeySet& dispatchKeySet,
654
+ Args&&... args)
655
+ // Calls the kernel and capture the result in output_.
656
+ : output_{kernel.template call<ReturnType, Args...>(
657
+ op,
658
+ dispatchKeySet,
659
+ std::forward<Args>(args)...)} {}
660
+ // Wraps the return values in a Stack.
661
+ Stack getOutputs() {
662
+ Stack stack;
663
+ impl::push_outputs<ReturnType, false>::copy(output_, &stack);
664
+ return stack;
665
+ }
666
+ // Since we are returning the output_, we don't expect the output_ to be used
667
+ // afterward. Copy elision and RVO do not apply to class data members. Using
668
+ // move semantic to avoid copies when possible.
669
+ ReturnType release() && {
670
+ return std::move(output_);
671
+ }
672
+
673
+ private:
674
+ ReturnType output_;
675
+ };
676
+
677
+ // Handle the lvalue reference differently since it should not be moved.
678
+ template <>
679
+ inline at::Tensor& CaptureKernelCall<at::Tensor&>::release() && {
680
+ return output_;
681
+ }
682
+
683
+ // Handle case where the kernel returns void.
684
+ template <>
685
+ struct CaptureKernelCall<void> {
686
+ template <typename F, typename... Args>
687
+ CaptureKernelCall(
688
+ const F& kernel,
689
+ const TypedOperatorHandle<void(Args...)>& op,
690
+ const DispatchKeySet& dispatchKeySet,
691
+ Args&&... args) {
692
+ // Calling the kernel and no need to capture void.
693
+ kernel.template call<void, Args...>(
694
+ op, dispatchKeySet, std::forward<Args>(args)...);
695
+ }
696
+ Stack getOutputs() {
697
+ return Stack();
698
+ }
699
+ void release() && {}
700
+ };
701
+
702
+ TORCH_API void _print_dispatch_trace(
703
+ const std::string& label,
704
+ const std::string& op_name,
705
+ const DispatchKeySet& dispatchKeySet);
706
+
707
+ } // namespace detail
708
+
709
+ // See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
710
+ template <class Return, class... Args>
711
+ inline Return Dispatcher::callWithDispatchKeySlowPath(
712
+ const TypedOperatorHandle<Return(Args...)>& op,
713
+ at::StepCallbacks& stepCallbacks,
714
+ DispatchKeySet dispatchKeySet,
715
+ const KernelFunction& kernel,
716
+ Args... args) {
717
+ // If callbacks need inputs, we box the arguments and pass them to the guard.
718
+ // Note: For perf reasons we wouldn't want to prematurely box the arguments.
719
+ at::RecordFunction guard(std::move(stepCallbacks));
720
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(op.operatorDef_->op.isObserved());
721
+ auto dispatchKey = dispatchKeySet.highestPriorityTypeId();
722
+ auto& schema = op.schema();
723
+ auto schema_ref = std::reference_wrapper<const FunctionSchema>(schema);
724
+ constexpr auto num_boxed_args = impl::boxed_size<Args...>();
725
+ if constexpr (num_boxed_args != 0) {
726
+ if (guard.needsInputs()) {
727
+ // If we used std::array<IValue, num_boxed_args> here, we would
728
+ // have to spend time default constructing the IValues in
729
+ // boxedArgs. aligned_storage has no such requirement.
730
+ // NOLINTNEXTLINE(*array*)
731
+ alignas(IValue) std::byte boxedArgs[num_boxed_args * sizeof(IValue)];
732
+ // For debugging only; could be removed (but the compiler will do
733
+ // that for us and it's nice to have the extra assurance of
734
+ // correctness from our debug builds).
735
+ IValue* boxedArgsPtr = reinterpret_cast<IValue*>(boxedArgs);
736
+ impl::boxArgsToStack(boxedArgsPtr, args...);
737
+ TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
738
+ reinterpret_cast<std::byte*>(boxedArgsPtr) ==
739
+ boxedArgs + num_boxed_args * sizeof(IValue));
740
+ // I don't *think* we need std::launder here, because IValue has
741
+ // no subclasses and no const or reference fields.
742
+ runRecordFunction(
743
+ guard,
744
+ schema_ref,
745
+ dispatchKey,
746
+ dispatchKeySet,
747
+ c10::ArrayRef<const c10::IValue>(
748
+ reinterpret_cast<IValue*>(boxedArgs), num_boxed_args));
749
+ boxedArgsPtr = reinterpret_cast<IValue*>(boxedArgs);
750
+ for (size_t ii = 0; ii < num_boxed_args; ++ii) {
751
+ (boxedArgsPtr + ii)->~IValue();
752
+ }
753
+ } else {
754
+ runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
755
+ }
756
+ } else {
757
+ runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
758
+ }
759
+
760
+ if (C10_UNLIKELY(guard.needsOutputs())) {
761
+ // Calls the kernel and capture the output temporarily to pass to
762
+ // RecordFunction.
763
+ detail::CaptureKernelCall<Return> captureKernelCall(
764
+ kernel, op, dispatchKeySet, std::forward<Args>(args)...);
765
+ guard.setOutputs(captureKernelCall.getOutputs());
766
+ // Releases the captured output to return to caller.
767
+ return std::move(captureKernelCall).release();
768
+ }
769
+
770
+ // keeping the guard alive while executing the kernel
771
+ return kernel.template call<Return, Args...>(
772
+ op, dispatchKeySet, std::forward<Args>(args)...);
773
+ }
774
+
775
+ // See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
776
+ template <class Return, class... Args>
777
+ C10_ALWAYS_INLINE_UNLESS_MOBILE Return Dispatcher::call(
778
+ const TypedOperatorHandle<Return(Args...)>& op,
779
+ Args... args) const {
780
+ auto dispatchKeySet =
781
+ op.operatorDef_->op.dispatchKeyExtractor()
782
+ .template getDispatchKeySetUnboxed<Args...>(args...);
783
+ #if defined(HAS_TORCH_SHOW_DISPATCH_TRACE) || !defined(NDEBUG)
784
+ DispatchTraceNestingGuard debug_guard;
785
+ if (show_dispatch_trace()) {
786
+ detail::_print_dispatch_trace(
787
+ "[call]", toString(op.operator_name()), dispatchKeySet);
788
+ }
789
+ #endif
790
+ const KernelFunction& kernel = op.operatorDef_->op.lookup(dispatchKeySet);
791
+ #ifndef PYTORCH_DISABLE_PER_OP_PROFILING
792
+ auto step_callbacks =
793
+ at::getStepCallbacksUnlessEmpty(at::RecordScope::FUNCTION);
794
+ if (C10_UNLIKELY(
795
+ step_callbacks.has_value() && op.operatorDef_->op.isObserved())) {
796
+ return callWithDispatchKeySlowPath<Return, Args...>(
797
+ op,
798
+ *step_callbacks,
799
+ dispatchKeySet,
800
+ kernel,
801
+ std::forward<Args>(args)...);
802
+ }
803
+ #endif // PYTORCH_DISABLE_PER_OP_PROFILING
804
+
805
+ #ifdef FBCODE_CAFFE2
806
+ if (profilingOperatorEvents()) {
807
+ std::vector<void*> argsAddresses = {(void*)(&args)...};
808
+ std::vector<const char*> argsTypes = {(typeid(args).name())...};
809
+ struct FireOpRAII {
810
+ FireOpRAII(
811
+ at::RecordFunction::schema_ref_t schema_ref,
812
+ std::vector<void*>& argsAddresses,
813
+ std::vector<const char*>& argsTypes)
814
+ : schema_ref_(schema_ref) {
815
+ fireOpStartUSDT(schema_ref, argsAddresses, argsTypes);
816
+ }
817
+ ~FireOpRAII() {
818
+ fireOpEndUSDT(schema_ref_);
819
+ }
820
+ at::RecordFunction::schema_ref_t schema_ref_;
821
+ } event(op.schema(), argsAddresses, argsTypes);
822
+ return kernel.template call<Return, Args...>(
823
+ op, dispatchKeySet, std::forward<Args>(args)...);
824
+ } else {
825
+ return kernel.template call<Return, Args...>(
826
+ op, dispatchKeySet, std::forward<Args>(args)...);
827
+ }
828
+ #else
829
+ return kernel.template call<Return, Args...>(
830
+ op, dispatchKeySet, std::forward<Args>(args)...);
831
+ #endif // FBCODE_CAFFE2
832
+ }
833
+
834
+ // See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
835
+ template <class Return, class... Args>
836
+ inline Return Dispatcher::redispatch(
837
+ const TypedOperatorHandle<Return(Args...)>& op,
838
+ DispatchKeySet currentDispatchKeySet,
839
+ Args... args) const {
840
+ // do not use RecordFunction on redispatch
841
+ #if defined(HAS_TORCH_SHOW_DISPATCH_TRACE) || !defined(NDEBUG)
842
+ DispatchTraceNestingGuard debug_guard;
843
+ if (show_dispatch_trace()) {
844
+ detail::_print_dispatch_trace(
845
+ "[redispatch]", toString(op.operator_name()), currentDispatchKeySet);
846
+ }
847
+ #endif
848
+ const KernelFunction& kernel =
849
+ op.operatorDef_->op.lookup(currentDispatchKeySet);
850
+ return kernel.template call<Return, Args...>(
851
+ op, currentDispatchKeySet, std::forward<Args>(args)...);
852
+ }
853
+
854
+ inline void Dispatcher::callBoxed(const OperatorHandle& op, Stack* stack)
855
+ const {
856
+ // note: this doesn't need the mutex because write operations on the list keep
857
+ // iterators intact.
858
+ const auto& entry = op.operatorDef_->op;
859
+ auto dispatchKeySet =
860
+ entry.dispatchKeyExtractor().getDispatchKeySetBoxed(stack);
861
+ #if defined(HAS_TORCH_SHOW_DISPATCH_TRACE) || !defined(NDEBUG)
862
+ DispatchTraceNestingGuard debug_guard;
863
+ if (show_dispatch_trace()) {
864
+ detail::_print_dispatch_trace(
865
+ "[callBoxed]", toString(op.operator_name()), dispatchKeySet);
866
+ }
867
+ #endif
868
+ const auto& kernel = entry.lookup(dispatchKeySet);
869
+ #ifndef PYTORCH_DISABLE_PER_OP_PROFILING
870
+ auto step_callbacks =
871
+ at::getStepCallbacksUnlessEmpty(at::RecordScope::FUNCTION);
872
+ if (C10_UNLIKELY(step_callbacks.has_value() && entry.isObserved())) {
873
+ at::RecordFunction guard(std::move(*step_callbacks));
874
+ auto dispatchKey = dispatchKeySet.highestPriorityTypeId();
875
+ auto& schema = op.schema();
876
+ auto schema_ref = std::reference_wrapper<const FunctionSchema>(schema);
877
+ guard.needsInputs()
878
+ ? runRecordFunction(
879
+ guard,
880
+ schema_ref,
881
+ dispatchKey,
882
+ dispatchKeySet,
883
+ c10::ArrayRef<const c10::IValue>(stack->data(), stack->size()))
884
+ : runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
885
+
886
+ // keeping the guard alive while executing the kernel
887
+ kernel.callBoxed(op, dispatchKeySet, stack);
888
+
889
+ if (C10_UNLIKELY(guard.needsOutputs())) {
890
+ guard.setOutputs(*stack);
891
+ }
892
+ return;
893
+ }
894
+ #endif // PYTORCH_DISABLE_PER_OP_PROFILING
895
+ kernel.callBoxed(op, dispatchKeySet, stack);
896
+ }
897
+
898
+ // NB: this doesn't count as a "true" dispatcher jump, so no instrumentation
899
+ inline void Dispatcher::callBoxedForDispatchKey(
900
+ const OperatorHandle& op,
901
+ DispatchKey dk,
902
+ Stack* stack) const {
903
+ // note: this doesn't need the mutex because write operations on the list keep
904
+ // iterators intact.
905
+ const auto& entry = op.operatorDef_->op;
906
+ // We still compute this as we're obligated to pass it on to the internal
907
+ // kernel, if it is a boxed fallback
908
+ auto dispatchKeySet =
909
+ entry.dispatchKeyExtractor().getDispatchKeySetBoxed(stack);
910
+ const auto& kernel = ([&]() {
911
+ if (op.hasKernelForDispatchKey(dk)) {
912
+ return entry.kernelForDispatchKey(dk);
913
+ } else {
914
+ auto idx = getDispatchTableIndexForDispatchKey(dk);
915
+ TORCH_INTERNAL_ASSERT(idx >= 0);
916
+ return backendFallbackKernels_[idx].kernel;
917
+ }
918
+ })();
919
+ kernel.callBoxed(op, dispatchKeySet, stack);
920
+ }
921
+
922
+ inline void Dispatcher::redispatchBoxed(
923
+ const OperatorHandle& op,
924
+ DispatchKeySet dispatchKeySet,
925
+ Stack* stack) const {
926
+ // note: this doesn't need the mutex because write operations on the list keep
927
+ // iterators intact.
928
+ const auto& entry = op.operatorDef_->op;
929
+ #if defined(HAS_TORCH_SHOW_DISPATCH_TRACE) || !defined(NDEBUG)
930
+ DispatchTraceNestingGuard debug_guard;
931
+ if (show_dispatch_trace()) {
932
+ detail::_print_dispatch_trace(
933
+ "[redispatchBoxed]", toString(op.operator_name()), dispatchKeySet);
934
+ }
935
+ #endif
936
+ const auto& kernel = entry.lookup(dispatchKeySet);
937
+ kernel.callBoxed(op, dispatchKeySet, stack);
938
+ }
939
+
940
+ } // namespace c10
941
+
942
+ namespace std {
943
+
944
+ template <>
945
+ struct hash<c10::OperatorHandle> {
946
+ size_t operator()(const c10::OperatorHandle& op) const noexcept {
947
+ return std::hash<void*>{}(static_cast<void*>(op.operatorDef_));
948
+ }
949
+ };
950
+
951
+ } // namespace std
952
+
953
+ #else
954
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
955
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/ObservedOperators.h ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/operator_name.h>
5
+ #include <string>
6
+ #include <unordered_set>
7
+
8
+ namespace c10 {
9
+
10
+ struct TORCH_API ObservedOperators {
11
+ ObservedOperators() = delete;
12
+
13
+ static bool isObserved(const OperatorName& name);
14
+
15
+ static std::unordered_set<std::string>& getUnobservedOperatorList();
16
+ };
17
+
18
+ } // namespace c10
19
+
20
+ #else
21
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
22
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorEntry.h ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/boxing/KernelFunction.h>
5
+ #include <ATen/core/dispatch/DispatchKeyExtractor.h>
6
+ #include <ATen/core/function_schema.h>
7
+ #include <ATen/core/ivalue.h>
8
+ #include <c10/core/DispatchKey.h>
9
+ #include <c10/core/PyHandleCache.h>
10
+ #include <c10/core/SafePyObject.h>
11
+ #include <c10/util/Metaprogramming.h>
12
+ #include <c10/util/flat_hash_map.h>
13
+
14
+ #include <ATen/core/dispatch/CppSignature.h>
15
+ #include <ATen/core/dispatch/OperatorOptions.h>
16
+ #include <ATen/core/dispatch/RegistrationHandleRAII.h>
17
+ #include <ATen/core/enum_tag.h>
18
+
19
+ #include <array>
20
+ #include <list>
21
+ #include <optional>
22
+
23
+ #ifdef C10_MOBILE
24
+ #define C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
25
+ #endif
26
+
27
+ namespace c10 {
28
+
29
+ class Dispatcher;
30
+
31
+ namespace impl {
32
+
33
+ // This data structure represents a kernel that was registered to us from a
34
+ // user. Unlike KernelFunction, AnnotatedKernel contains some extra metadata
35
+ // about the kernel that isn't necessary for actual dispatching (this is why
36
+ // we don't put AnnotatedKernel in the actual DispatchTable), but is useful for
37
+ // giving good error messages.
38
+ struct AnnotatedKernel final {
39
+ AnnotatedKernel(
40
+ KernelFunction k,
41
+ std::unique_ptr<FunctionSchema> s,
42
+ std::string d)
43
+ : kernel(std::move(k)),
44
+ inferred_function_schema(std::move(s)),
45
+ debug(std::move(d)) {}
46
+ AnnotatedKernel() = default;
47
+ KernelFunction kernel;
48
+ std::unique_ptr<FunctionSchema> inferred_function_schema;
49
+ // A little debug string to help us identify the kernel in question.
50
+ // Most importantly it records the TORCH_LIBRARY block that did the
51
+ // registration.
52
+ std::string debug;
53
+ };
54
+
55
+ // This data structure represents operator schema, with metadata specifying
56
+ // where the registration of this schema occurred
57
+ struct AnnotatedSchema final {
58
+ AnnotatedSchema(FunctionSchema s, std::string d)
59
+ : schema(std::move(s)), debug(std::move(d)) {}
60
+ FunctionSchema schema;
61
+ std::string debug;
62
+ };
63
+
64
+ // Internal data structure that records information about a specific operator.
65
+ // It's not part of the public API; typically, users will interact with
66
+ // OperatorHandle instead.
67
+ //
68
+ // Concurrent writes to OperatorEntry are protected by the GLOBAL Dispatcher
69
+ // lock (this is important because some methods in OperatorEntry access
70
+ // dispatcher state)
71
+ class TORCH_API OperatorEntry final {
72
+ public:
73
+ explicit OperatorEntry(OperatorName&& operator_name);
74
+
75
+ OperatorEntry(const OperatorEntry&) = delete;
76
+ OperatorEntry(OperatorEntry&&) noexcept = delete;
77
+ OperatorEntry& operator=(const OperatorEntry&) = delete;
78
+ OperatorEntry& operator=(OperatorEntry&&) noexcept = delete;
79
+
80
+ const FunctionSchema& schema() const {
81
+ TORCH_INTERNAL_ASSERT(
82
+ schema_.has_value(),
83
+ "Tried to access the schema for ",
84
+ name_,
85
+ " which doesn't have a schema registered yet");
86
+ return schema_->schema;
87
+ }
88
+ const std::string& debug() const {
89
+ TORCH_INTERNAL_ASSERT(schema_.has_value());
90
+ return schema_->debug;
91
+ }
92
+ bool hasSchema() const {
93
+ return schema_.has_value();
94
+ }
95
+
96
+ bool isObserved() const {
97
+ return is_observed_;
98
+ }
99
+
100
+ // We may allocate an OperatorEntry for an operator even when we don't
101
+ // have a schema. When we receive the schema registration, we post
102
+ // facto register a schema.
103
+ //
104
+ // NB: registerSchema/deregisterSchema are not idempotent; if you
105
+ // attempt to register a schema when one is already present or vice
106
+ // versa that is an error. (Refcounting for the registrations is
107
+ // handled in the OperatorHandle in Dispatcher)
108
+ void registerSchema(
109
+ FunctionSchema&& /*schema*/,
110
+ std::string&& debug,
111
+ std::vector<at::Tag> tags = {});
112
+ void deregisterSchema();
113
+
114
+ const OperatorName& operator_name() const {
115
+ return name_;
116
+ }
117
+
118
+ #ifdef C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
119
+ using AnnotatedKernelContainer = std::array<AnnotatedKernel, 1>;
120
+ #else
121
+ using AnnotatedKernelContainer = std::list<AnnotatedKernel>;
122
+ #endif
123
+ using AnnotatedKernelContainerIterator = AnnotatedKernelContainer::iterator;
124
+
125
+ // Why are kernels and fallback asymmetric? It has to do with ownership.
126
+ // Kernels and the computed dispatch tables for them are canonically
127
+ // owned by OperatorEntry, but backend fallbacks are specified once
128
+ // and apply for all operators, so they should be owned by Dispatcher.
129
+ // However, the registration of a backend fallback affects the
130
+ // state of the computed dispatch table, so when a backend fallback
131
+ // is updated, we need to update the operator tables too. Thus,
132
+ // registerKernel is the mechanism by which we give kernels to
133
+ // operator entry to own (and update dispatch table), but we only
134
+ // need a non-owning mechanism to update fallback.
135
+
136
+ // Precondition: Dispatcher::mutex_ is held
137
+ // Postcondition: caller is responsible for disposing of the kernel
138
+ AnnotatedKernelContainerIterator registerKernel(
139
+ const Dispatcher& dispatcher,
140
+ std::optional<DispatchKey> dispatch_key,
141
+ KernelFunction kernel,
142
+ std::optional<CppSignature> cpp_signature,
143
+ std::unique_ptr<FunctionSchema> inferred_function_schema,
144
+ std::string debug);
145
+
146
+ // Precondition: Dispatcher::mutex_ is held
147
+ void deregisterKernel_(
148
+ const Dispatcher& dispatcher,
149
+ std::optional<DispatchKey> dispatch_key,
150
+ AnnotatedKernelContainerIterator kernel);
151
+
152
+ // Precondition: Dispatcher::mutex_ is held
153
+ void updateFallback(const Dispatcher& dispatcher, DispatchKey dispatch_key);
154
+
155
+ // Precondition: Dispatcher::mutex_ is held
156
+ void updateSchemaAliasAnalysis(AliasAnalysisKind a) {
157
+ TORCH_INTERNAL_ASSERT(schema_.has_value());
158
+ schema_->schema.setAliasAnalysis(a);
159
+ }
160
+
161
+ std::string dumpComputedTable() const;
162
+ std::string dumpState() const;
163
+ void checkInvariants() const;
164
+
165
+ const DispatchKeyExtractor& dispatchKeyExtractor() const {
166
+ return dispatchKeyExtractor_;
167
+ }
168
+
169
+ // Asserts that the given FuncType is correct for calling this operator in an
170
+ // unboxed way.
171
+ template <class FuncType>
172
+ inline void assertSignatureIsCorrect() {
173
+ assertSignatureIsCorrect(
174
+ CppSignature::make<FuncType>(), fn_has_symint<FuncType>::value);
175
+ }
176
+
177
+ void assertSignatureIsCorrect(
178
+ const CppSignature& call_signature,
179
+ bool has_symint) const;
180
+
181
+ [[noreturn]] void reportError(DispatchKey dispatchKey) const;
182
+
183
+ const KernelFunction& lookup(DispatchKeySet ks) const {
184
+ const auto idx = ks.getDispatchTableIndexForDispatchKeySet();
185
+ if (C10_UNLIKELY(idx == -1)) {
186
+ reportError(ks.highestPriorityTypeId());
187
+ }
188
+ const auto& kernel = dispatchTable_[idx];
189
+ // A valid kernel *always* has a boxed kernel and *may* have an
190
+ // unboxed kernel. However, we typically do unboxed calls in at::
191
+ // APIs, where the kernel 1) will very likely be valid and 2)
192
+ // should have an unboxed kernel. Checking the unboxed kernel
193
+ // first will allow us to avoid touching the boxed kernel at all
194
+ // in the common case.
195
+ if (C10_UNLIKELY(!kernel.isValidUnboxed())) {
196
+ if (!kernel.isValid()) {
197
+ reportError(ks.highestPriorityTypeId());
198
+ }
199
+ }
200
+ return kernel;
201
+ }
202
+
203
+ std::string listAllDispatchKeys() const;
204
+
205
+ // Returns true if kernel_ has entry for any key in ks.
206
+ //
207
+ // Invariant: There are no alias keys in the passed-in dispatch key set.
208
+ // Note [No Alias Keys in DispatchKeySet]
209
+ // Alias keys should be checked using `hasKernelForDispatchKey`
210
+ // Alias keys shouldn't go inside of a DispatchKeySet, since they can
211
+ // technically have a value > 63 (causing overflow).
212
+ bool hasKernelForAnyDispatchKey(DispatchKeySet ks) const;
213
+ // Returns true if kernel_ has entry for a particular key.
214
+ bool hasKernelForDispatchKey(DispatchKey k) const;
215
+ // Retrieves the kernel entry at a particular key. Symmetric with
216
+ // hasKernelForDispatchKey. To get the AnnotatedKernel, see
217
+ // getKernelForDispatchKey (private)
218
+ const KernelFunction& kernelForDispatchKey(DispatchKey k) const;
219
+ // Returns true if the "computed table" has an entry for a particular key.
220
+ bool hasComputedKernelForDispatchKey(DispatchKey k) const;
221
+ // Returns a KernelFunction corresponding to the kernel in dispatchTable
222
+ SafeKernelFunction getComputedKernelForDispatchKey(DispatchKey k) const;
223
+ // Returns all the operator tags added at the time of registration
224
+ const std::vector<at::Tag>& getTags() const;
225
+ void setReportErrorCallback_(std::unique_ptr<c10::SafePyObject> callback);
226
+
227
+ template <typename F>
228
+ PyObject* getPythonOp(PyInterpreter* self_interpreter, F slow_accessor)
229
+ const {
230
+ return py_cache_.ptr_or(self_interpreter, slow_accessor);
231
+ }
232
+
233
+ private:
234
+ OperatorName name_;
235
+ std::optional<AnnotatedSchema> schema_;
236
+ #ifndef C10_MOBILE
237
+ std::vector<at::Tag> tags_;
238
+ #endif
239
+ std::array<KernelFunction, c10::num_runtime_entries> dispatchTable_;
240
+ DispatchKeyExtractor dispatchKeyExtractor_;
241
+ // Pointer to the torch.ops.ns.op.overload object for speed
242
+ c10::PyHandleCache py_cache_;
243
+
244
+ // kernels_ stores all registered kernels for the corresponding dispatch key
245
+ // and catchAllKernels_ stores the catch-all kernels.
246
+ // If an operator library gets loaded that overwrites an already existing
247
+ // kernel, both kernels will be in that list but only the newer one will be in
248
+ // dispatchTable. If any of the kernels go away (say the library gets
249
+ // unloaded), we remove the kernel from this list and update the
250
+ // dispatchTable if necessary.
251
+ // Kernels in the list are ordered by registration time descendingly,
252
+ // newer registrations are before older registrations.
253
+ // We do not combine dispatchTable and kernels into one hash map because
254
+ // kernels is a larger data structure and accessed quite infrequently
255
+ // while dispatchTable is accessed often and should be kept small to fit
256
+ // into CPU caches.
257
+ // Invariants:
258
+ // - dispatchTable[dispatch_key] == kernels_[dispatch_key].front()
259
+ // - dispatchTable[dispatch_key] does not exist if and only if
260
+ // kernels_[dispatch_key] does not exist
261
+ // - If kernels_[dispatch_key] exists, then it has elements.
262
+ // It is never an empty list.
263
+ //
264
+ // Why do we do that?
265
+ // -----
266
+ // We mostly do this to enable Jupyter notebooks where a cell registering
267
+ // a kernel could be executed multiple times and the later execution
268
+ // should overwrite the earlier one. Note that this still fails when the
269
+ // function schema changed between the executions, but it works as long
270
+ // as the function schema didn't change. A better solution would be to
271
+ // unload the old extension library from the Jupyter cell when the cell is
272
+ // re-executed and then only allow one kernel here, i.e. error if a kernel
273
+ // is already registered, but that's a lot of effort to implement and
274
+ // currently not high-pri.
275
+ ska::flat_hash_map<
276
+ DispatchKey,
277
+ #ifdef C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
278
+ // On mobile, we needn't worry about Jupyter notebooks.
279
+ std::array<AnnotatedKernel, 1>
280
+ #else
281
+ std::list<AnnotatedKernel>
282
+ #endif
283
+ >
284
+ kernels_;
285
+
286
+ const AnnotatedKernel& missingKernel() const;
287
+ const AnnotatedKernel& ambiguousAutogradOtherKernel() const;
288
+
289
+ // cpp_signature_ stores function signature if any of
290
+ // the kernels was created in a way that allowed us to know the function
291
+ // signature (i.e. by supplying an unboxed C++ kernel function).
292
+ // If this is set, it will be used to check that future kernel
293
+ // registrations match and it will be used in unboxed function calls
294
+ // to verify their arguments against the known function signature.
295
+ struct CppSignatureWithDebug {
296
+ CppSignature signature;
297
+ std::string debug;
298
+ std::optional<DispatchKey> dispatch_key;
299
+ };
300
+ std::optional<CppSignatureWithDebug> cpp_signature_;
301
+ std::optional<CppSignatureWithDebug> sym_cpp_signature_;
302
+
303
+ // A Python custom error handler for OperatorEntry::reportError
304
+ std::unique_ptr<c10::SafePyObject> report_error_callback_;
305
+
306
+ // Whether this operator needs to be observed with RecordFunction
307
+ const bool is_observed_;
308
+
309
+ [[noreturn]] void reportSignatureError(
310
+ const CppSignature& call_signature,
311
+ const CppSignatureWithDebug& saved_signature) const;
312
+ const KernelFunction& computeDispatchTableEntry(
313
+ const c10::Dispatcher& dispatcher,
314
+ DispatchKey dispatch_key) const;
315
+ std::pair<const AnnotatedKernel&, const char*>
316
+ computeDispatchTableEntryWithDebug(
317
+ const c10::Dispatcher& dispatcher,
318
+ DispatchKey dispatch_key) const;
319
+ // This function re-establishes the invariant that dispatchTable
320
+ // contains the front element from the kernels list for a given runtime
321
+ // dispatch key.
322
+ void updateDispatchTableEntry_(
323
+ const c10::Dispatcher& dispatcher,
324
+ DispatchKey dispatch_key);
325
+ // Like above, but also handles alias dispatch keys.
326
+ void updateDispatchTable_(
327
+ const c10::Dispatcher& dispatcher,
328
+ DispatchKey dispatch_key);
329
+ // Like above, but for ALL entries in the dispatch table.
330
+ void updateDispatchTableFull_(const c10::Dispatcher& dispatcher);
331
+ // Retrieves a pointer to AnnotatedKernel at
332
+ // kernels_.at(dispatch_key).front().
333
+ const AnnotatedKernel* getKernelForDispatchKey(
334
+ DispatchKey dispatch_key) const;
335
+ };
336
+
337
+ } // namespace impl
338
+ } // namespace c10
339
+
340
+ #else
341
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
342
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorOptions.h ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <cstdint>
5
+
6
+ namespace c10 {
7
+
8
+ enum class AliasAnalysisKind : uint8_t {
9
+ INTERNAL_SPECIAL_CASE,
10
+ CONSERVATIVE, // The most conservative alias analysis type, assumes
11
+ // side-effects. This is the default analysis.
12
+ FROM_SCHEMA,
13
+ PURE_FUNCTION
14
+ };
15
+
16
+ #if !defined(_MSC_VER)
17
+ constexpr // Our current MSVC version has a bug that doesn't allow this to be
18
+ // constexpr.
19
+ #endif
20
+ inline const char*
21
+ toString(AliasAnalysisKind aliasAnalysisKind) {
22
+ return (aliasAnalysisKind == AliasAnalysisKind::CONSERVATIVE) ? "CONSERVATIVE"
23
+ : (aliasAnalysisKind == AliasAnalysisKind::FROM_SCHEMA) ? "FROM_SCHEMA"
24
+ : (aliasAnalysisKind == AliasAnalysisKind::PURE_FUNCTION)
25
+ ? "PURE_FUNCTION"
26
+ : (aliasAnalysisKind == AliasAnalysisKind::INTERNAL_SPECIAL_CASE)
27
+ ? "INTERNAL_SPECIAL_CASE"
28
+ : "UNKNOWN";
29
+ }
30
+
31
+ } // namespace c10
32
+
33
+ #else
34
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
35
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/RegistrationHandleRAII.h ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <functional>
5
+
6
+ namespace c10 {
7
+
8
+ class RegistrationHandleRAII final {
9
+ public:
10
+ explicit RegistrationHandleRAII(std::function<void()> onDestruction)
11
+ : onDestruction_(std::move(onDestruction)) {}
12
+
13
+ ~RegistrationHandleRAII() {
14
+ if (onDestruction_) {
15
+ onDestruction_();
16
+ }
17
+ }
18
+
19
+ RegistrationHandleRAII(const RegistrationHandleRAII&) = delete;
20
+ RegistrationHandleRAII& operator=(const RegistrationHandleRAII&) = delete;
21
+
22
+ RegistrationHandleRAII(RegistrationHandleRAII&& rhs) noexcept
23
+ : onDestruction_(std::move(rhs.onDestruction_)) {
24
+ rhs.onDestruction_ = nullptr;
25
+ }
26
+
27
+ RegistrationHandleRAII& operator=(RegistrationHandleRAII&& rhs) noexcept {
28
+ onDestruction_ = std::move(rhs.onDestruction_);
29
+ rhs.onDestruction_ = nullptr;
30
+ return *this;
31
+ }
32
+
33
+ private:
34
+ std::function<void()> onDestruction_;
35
+ };
36
+
37
+ } // namespace c10
38
+
39
+ #else
40
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
41
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/adaption.h ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/Tensor.h>
5
+ #include <ATen/TensorUtils.h>
6
+ #include <ATen/core/List.h>
7
+ #include <c10/core/TensorOptions.h>
8
+
9
+ /*
10
+ * [Note: hacky wrapper removal for optional tensor]
11
+ *
12
+ * The kernel implementation takes an optional tensor marked in the schema as
13
+ * Tensor? but the C++ function takes Tensor instead of the std::optional<Tensor>
14
+ * expected by the dispatcher.
15
+ *
16
+ * To remove the hacky wrapper, the C++ function is changed to take
17
+ * std::optional<Tensor> and unwrap the Tensor value at the beginning of
18
+ * the function, e.g.:
19
+ * > c10::MaybeOwned<Tensor> weight_maybe_owned =
20
+ * > at::borrow_from_optional_tensor(weight_opt);
21
+ * > const Tensor& weight = *weight_maybe_owned;
22
+ *
23
+ * We may want to make the kernel handle optional directly without
24
+ * going through the creation of a default-constructed Tensor in
25
+ * at::borrow_from_optional_tensor.
26
+ */
27
+
28
+ /*
29
+ * [Note: hacky wrapper removal for TensorOptions]
30
+ *
31
+ * The kernel implementation takes a TensorOptions argument but the dispatcher
32
+ * expects separate arguments for dtype, layout, device, pin_memory.
33
+ *
34
+ * To remove the hacky wrapper, the kernel implementation is changed to take
35
+ * the 4 arguments (dtype, layout, device, pin_memory), and assemble the
36
+ * TensorOptions value at the beginning of the function, e.g.:
37
+ * > TensorOptions options = TensorOptions().dtype(dtype).layout(layout)
38
+ * > .device(device).pinned_memory(pin_memory);
39
+ *
40
+ * We may want make the kernel handle these parameters directly without going
41
+ * through the creation of a TensorOptions value.
42
+ */
43
+
44
+ namespace c10::impl {
45
+
46
+ TORCH_API void common_device_check_failure(Device common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName);
47
+
48
+ inline void check_and_update_common_device(std::optional<Device>& common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName) {
49
+ // TODO: Remove this once the following issue is addressed:
50
+ // https://github.com/pytorch/pytorch/issues/57380
51
+ if (!tensor.defined()) {
52
+ return;
53
+ }
54
+
55
+ if (!common_device.has_value()) {
56
+ common_device = tensor.device();
57
+ return;
58
+ }
59
+
60
+ if (C10_UNLIKELY(common_device != tensor.device())) {
61
+ common_device_check_failure(*common_device, tensor, methodName, argName);
62
+ }
63
+ }
64
+
65
+ inline void check_and_update_common_device(std::optional<Device>& common_device, const std::optional<at::Tensor>& tensor, at::CheckedFrom methodName, at::CheckedFrom argName) {
66
+ if (tensor.has_value()) {
67
+ check_and_update_common_device(common_device, tensor.value(), methodName, argName);
68
+ }
69
+ }
70
+
71
+ inline void check_and_update_common_device(std::optional<Device>& common_device, at::ITensorListRef tensors, at::CheckedFrom methodName, at::CheckedFrom argName) {
72
+ for (const auto& tensor : tensors) {
73
+ check_and_update_common_device(common_device, tensor, methodName, argName);
74
+ }
75
+ }
76
+
77
+ inline void check_and_update_common_device(std::optional<Device>& common_device, const List<std::optional<at::Tensor>>& tensors, at::CheckedFrom methodName, at::CheckedFrom argName) {
78
+ for (const auto& tensor : tensors) {
79
+ check_and_update_common_device(common_device, tensor, methodName, argName);
80
+ }
81
+ }
82
+ } // namespace c10::impl
83
+
84
+ #else
85
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
86
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/infer_schema.h ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ /**
5
+ * This file contains functionality to take a C++ function and infer its
6
+ * c10::FunctionSchema.
7
+ */
8
+
9
+ #include <ATen/core/function_schema.h>
10
+ #include <c10/util/Metaprogramming.h>
11
+
12
+ namespace c10 {
13
+ namespace detail::infer_schema {
14
+
15
+ /// The templated inference code creates `ArgumentDef` instead of `Argument`,
16
+ /// because that can be constructed at compile time and has a much smaller
17
+ /// binary size than having calls to `Argument` constructors in the template.
18
+ /// Creating `Argument` objects from `ArgumentDef` can then be done at
19
+ /// runtime in a non-templated way.
20
+ struct ArgumentDef final {
21
+ using GetTypeFn = TypePtr();
22
+ GetTypeFn* getTypeFn;
23
+ GetTypeFn* getFakeTypeFn;
24
+ constexpr ArgumentDef(): getTypeFn(nullptr), getFakeTypeFn(nullptr) {}
25
+ explicit constexpr ArgumentDef(GetTypeFn *getTypeFn, GetTypeFn *getFakeTypeFn): getTypeFn(getTypeFn), getFakeTypeFn(getFakeTypeFn) {}
26
+ };
27
+
28
+ template<bool V>
29
+ struct bool_t {};
30
+ template<> struct bool_t<true> : std::true_type {};
31
+ template<> struct bool_t<false> : std::false_type {};
32
+
33
+ /// Checks the static C++ types `Types` for correctness to catch common error cases.
34
+ template <class... Types>
35
+ constexpr int checkStaticTypes() {
36
+ // Give nice error messages for some of the common error cases.
37
+ // Use a LOUD ERROR MESSAGE SO USERS SEE THE STATIC_ASSERT
38
+ static_assert(std::conjunction_v<
39
+ bool_t<!std::is_integral_v<Types> || std::is_same_v<Types, int8_t> || std::is_same_v<Types, int64_t> || std::is_same_v<Types, bool>>...
40
+ >, "INVALID TYPE: Only int8_t, int64_t and bool are supported as an integral argument type");
41
+ static_assert(std::conjunction_v<
42
+ bool_t<!std::is_same_v<Types, float>>...
43
+ >, "INVALID TYPE: float is not supported as an argument type, use double instead");
44
+ return 0;
45
+ }
46
+
47
+ template <typename... Ts, size_t... Is>
48
+ constexpr std::array<ArgumentDef, sizeof...(Ts)> createArgumentVectorFromTypes(std::index_sequence<Is...> /*unused*/) {
49
+ return (
50
+ // Check types for common errors
51
+ checkStaticTypes<Ts...>(),
52
+
53
+ // Create the return value
54
+ std::array<ArgumentDef, sizeof...(Ts)>{
55
+ ArgumentDef(&getTypePtrCopy<std::decay_t<Ts>>, &getFakeTypePtrCopy<std::decay_t<Ts>>)...}
56
+ );
57
+ }
58
+
59
+ /// Creates a vector of `ArgumentDef` from a list of C++ types that are specified
60
+ /// as template arguments.
61
+ template<class ParameterTypes> struct createArguments final {};
62
+ template<class... ParameterTypes>
63
+ struct createArguments<guts::typelist::typelist<ParameterTypes...>> final {
64
+ static constexpr std::array<ArgumentDef, sizeof...(ParameterTypes)> call() {
65
+ return createArgumentVectorFromTypes<ParameterTypes...>(
66
+ std::make_index_sequence<sizeof...(ParameterTypes)>()
67
+ );
68
+ }
69
+ };
70
+
71
+ /// Creates a vector of `ArgumentDef` from a list of C++ types that are specified
72
+ /// as a tuple (i.e. in the way c10 kernels return values).
73
+ /// It can be a tuple<A, B, C> if there's three output arguments with types A, B, C.
74
+ /// It can be an empty tuple<>, or void for kernels that don't return anything.
75
+ /// It can be a single type A (i.e. no tuple) for the case where a kernel just
76
+ /// returns one value.
77
+ template<class ReturnTypeTuple, class Enable = void> struct createReturns final {};
78
+
79
+ template<class... ReturnTypes>
80
+ struct createReturns<std::tuple<ReturnTypes...>, void> final {
81
+ static constexpr std::array<ArgumentDef, sizeof...(ReturnTypes)> call() {
82
+ return createArgumentVectorFromTypes<ReturnTypes...>(
83
+ std::make_index_sequence<sizeof...(ReturnTypes)>()
84
+ );
85
+ }
86
+ };
87
+
88
+ template<class ReturnType>
89
+ struct createReturns<ReturnType, std::enable_if_t<!std::is_same_v<void, ReturnType> && !guts::is_instantiation_of<std::tuple, ReturnType>::value>> final {
90
+ static constexpr std::array<ArgumentDef, 1> call() {
91
+ return createReturns<std::tuple<ReturnType>>::call();
92
+ }
93
+ };
94
+
95
+ template<>
96
+ struct createReturns<void, void> final {
97
+ static constexpr std::array<ArgumentDef, 0> call() {
98
+ return createReturns<std::tuple<>>::call();
99
+ }
100
+ };
101
+
102
+ template <typename ReturnType>
103
+ struct createSingleReturn {
104
+ static constexpr std::array<ArgumentDef, 1> call() {
105
+ return createArgumentVectorFromTypes<ReturnType>(std::make_index_sequence<1>());
106
+ }
107
+ };
108
+
109
+ TORCH_API FunctionSchema make_function_schema(std::string&& name, std::string&& overload_name, c10::ArrayRef<ArgumentDef> arguments, c10::ArrayRef<ArgumentDef> returns);
110
+ TORCH_API FunctionSchema make_function_schema(c10::ArrayRef<ArgumentDef> arguments, c10::ArrayRef<ArgumentDef> returns);
111
+
112
+ /// Creates a `FunctionSchema` object from a `FunctionTraits` type for a
113
+ /// function. Flattens std::tuple returns into multiple return types
114
+ template <typename FunctionTraits>
115
+ FunctionSchema createFunctionSchemaFromTraitsFlattenedReturns() {
116
+ using ReturnType = typename FunctionTraits::return_type;
117
+ using ParameterTypes = typename FunctionTraits::parameter_types;
118
+
119
+ // arguments and returns are computed into a std::array at compile time and embedded into the binary.
120
+ // The only code executed at runtime here is the one that creates a std::vector
121
+ // of the arguments/returns from the std::array.
122
+ constexpr auto arguments = createArguments<ParameterTypes>::call();
123
+ constexpr auto returns = createReturns<ReturnType>::call();
124
+
125
+ return make_function_schema(arguments, returns);
126
+ }
127
+
128
+ /// Creates a `FunctionSchema` object from a `FunctionTraits` type for a
129
+ /// function. Preserves std::tuple returns as a Tuple return type
130
+ template <typename FunctionTraits>
131
+ FunctionSchema createFunctionSchemaFromTraitsSingleReturn(std::string&& name, std::string&& overload_name) {
132
+ using ReturnType = typename FunctionTraits::return_type;
133
+ using ParameterTypes = typename FunctionTraits::parameter_types;
134
+
135
+ // arguments and returns are computed into a std::array at compile time and embedded into the binary.
136
+ // The only code executed at runtime here is the one that creates a std::vector
137
+ // of the arguments/returns from the std::array.
138
+ constexpr auto arguments = createArguments<ParameterTypes>::call();
139
+ constexpr auto returns = createSingleReturn<ReturnType>::call();
140
+
141
+ return make_function_schema(std::move(name), std::move(overload_name), arguments, returns);
142
+ }
143
+
144
+ }
145
+
146
+ template<class FuncType>
147
+ FunctionSchema inferFunctionSchemaFlattenedReturns() {
148
+ return detail::infer_schema::createFunctionSchemaFromTraitsFlattenedReturns<guts::infer_function_traits_t<FuncType>>();
149
+ }
150
+
151
+ template<class FuncType>
152
+ FunctionSchema inferFunctionSchemaSingleReturn(std::string&& name, std::string&& overload_name) {
153
+ return detail::infer_schema::createFunctionSchemaFromTraitsSingleReturn<guts::infer_function_traits_t<FuncType>>(std::move(name), std::move(overload_name));
154
+ }
155
+
156
+ TORCH_API std::optional<std::string> findSchemaDifferences(const FunctionSchema& inferred, const FunctionSchema& specified);
157
+
158
+ }
159
+
160
+ #else
161
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
162
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/op_allowlist.h ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // TODO: unify to C10_MOBILE. In theory this header could be used in OSS.
5
+ #ifdef TEMPLATE_SELECTIVE_BUILD
6
+ #include <ATen/selected_mobile_ops.h>
7
+ #endif
8
+
9
+ /**
10
+ * This header implements functionality to build PyTorch with only a certain
11
+ * set of operators (+ dependencies) included.
12
+ *
13
+ * - Build with -DTORCH_OPERATOR_WHITELIST="aten::add;aten::sub" and only these
14
+ * two ops will be included in your build. The allowlist records operators
15
+ * only, no overloads; if you include aten::add, all overloads of aten::add
16
+ * will be included.
17
+ *
18
+ * Internally, this is done by removing the operator registration calls
19
+ * using compile time programming, and the linker will then prune all
20
+ * operator functions that weren't registered.
21
+ * See Note [Selective build] for more details
22
+ *
23
+ * WARNING: The allowlist mechanism doesn't work for all ways you could go about
24
+ * registering an operator. If the dispatch key / operator name is not
25
+ * sufficiently obvious at compile time, then the allowlisting mechanism
26
+ * will fail (and the operator will be included in the binary anyway).
27
+ */
28
+
29
+ #include <string_view>
30
+ #include <c10/core/DispatchKey.h>
31
+ #include <c10/macros/Macros.h>
32
+
33
+
34
+ #if defined(ENABLE_RECORD_KERNEL_FUNCTION_DTYPE)
35
+ #include <ATen/record_function.h>
36
+ #endif
37
+
38
+ namespace c10::impl {
39
+
40
+ constexpr bool allowlist_contains(std::string_view allowlist, std::string_view item); // Forward Declare
41
+
42
+ /**
43
+ * In selective build mode returns true/false depending on whether a build
44
+ * feature is available or not.
45
+ *
46
+ * In instrumenting mode (tracing mode), always returns true, and doesn't
47
+ * trigger any side effects.
48
+ */
49
+ constexpr bool is_build_feature_available(const char* name) {
50
+ #if !defined(ENABLE_RECORD_KERNEL_FUNCTION_DTYPE)
51
+ // Selective Build mode.
52
+ #if !defined(TORCH_BUILD_FEATURE_ALLOWLIST)
53
+ (void)name;
54
+ return true;
55
+ #else
56
+ return allowlist_contains(
57
+ C10_STRINGIZE(TORCH_BUILD_FEATURE_ALLOWLIST),
58
+ name);
59
+ #endif
60
+
61
+ #else
62
+ // Instrumenting mode.
63
+ (void)name;
64
+ return true;
65
+ #endif
66
+ }
67
+
68
+ [[noreturn]] void build_feature_required_feature_not_available(const char* feature);
69
+
70
+ /**
71
+ * Use BUILD_FEATURE_REQUIRED macro in user-code.
72
+ *
73
+ * In selective build mode becomes a no-op if the build feature passed
74
+ * in is available. If not available, throws an exception (c10::Error).
75
+ * The compiler is able to perform dead code elimination for code
76
+ * following this method if the build feature is not available.
77
+ *
78
+ * In instrumenting mode (tracing mode), registers (as a side effect)
79
+ * the presence of this specific build feature being triggered.
80
+ */
81
+ #if !defined(ENABLE_RECORD_KERNEL_FUNCTION_DTYPE) // selective build mode
82
+
83
+ #if defined(TORCH_BUILD_FEATURE_ALLOWLIST)
84
+ #define BUILD_FEATURE_REQUIRED(NAME) \
85
+ if (!c10::impl::is_build_feature_available(NAME)) { \
86
+ ::c10::impl::build_feature_required_feature_not_available(NAME); \
87
+ }
88
+ #else // Everything trivially selected
89
+ #define BUILD_FEATURE_REQUIRED(NAME)
90
+
91
+ #endif
92
+
93
+ #else // trace mode
94
+ #define BUILD_FEATURE_REQUIRED(NAME) \
95
+ RECORD_FUNCTION_WITH_SCOPE( \
96
+ at::RecordScope::BUILD_FEATURE, \
97
+ std::string(NAME), \
98
+ {});
99
+ #endif
100
+
101
+ // Use this macro, and not is_build_feature_available
102
+ #define BUILD_FEATURE_AVAILABLE(NAME) ::c10::impl::is_build_feature_available(NAME)
103
+
104
+ // returns true iff allowlist contains item
105
+ // allowlist_contains("a;bc;d", "bc") == true
106
+ constexpr bool allowlist_contains(std::string_view allowlist, std::string_view item) {
107
+ //Choose a really big value for next so that if something goes wrong
108
+ //this code will blow up in a hopefully detectable way.
109
+ size_t next = std::numeric_limits<size_t>::max();
110
+ for (size_t cur = 0; cur <= allowlist.size(); cur = next) {
111
+ next = allowlist.find(';', cur);
112
+ if (next != std::string_view::npos) {
113
+ if (allowlist.substr(cur, next - cur) == item) {
114
+ return true;
115
+ }
116
+ next++;
117
+ } else {
118
+ if (allowlist.substr(cur) == item) {
119
+ return true;
120
+ }
121
+ break;
122
+ }
123
+ }
124
+ return false;
125
+ }
126
+
127
+ // Returns true iff the given op name is on the allowlist
128
+ // and should be registered
129
+ constexpr bool op_allowlist_check(std::string_view op_name [[maybe_unused]]) {
130
+ assert(op_name.find("::") != std::string_view::npos);
131
+ // Use assert() instead of throw() due to a gcc bug. See:
132
+ // https://stackoverflow.com/questions/34280729/throw-in-constexpr-function
133
+ // https://github.com/fmtlib/fmt/issues/682
134
+ assert(op_name.find('(') == std::string_view::npos);
135
+ #if !defined(TORCH_OPERATOR_WHITELIST)
136
+ // If the TORCH_OPERATOR_WHITELIST parameter is not defined,
137
+ // all ops are to be registered
138
+ return true;
139
+ #else
140
+ return allowlist_contains(
141
+ C10_STRINGIZE(TORCH_OPERATOR_WHITELIST),
142
+ // This function is majorly used for mobile selective build with
143
+ // root operators, where the overload is included in the allowlist.
144
+ op_name);
145
+ // // Strip overload name (as allowlist doesn't contain overloads)
146
+ // // Another function based on this may be added when there's usage
147
+ // // on op names without overload.
148
+ // OperatorNameView::parse(op_name).name);
149
+ #endif
150
+ }
151
+
152
+ // Returns true iff the given schema string is on the allowlist
153
+ // and should be registered
154
+ constexpr bool schema_allowlist_check(std::string_view schema) {
155
+ #if defined(TORCH_FORCE_SCHEMA_REGISTRATION)
156
+ return true;
157
+ #else
158
+ return op_allowlist_check(schema.substr(0, schema.find('(')));
159
+ #endif
160
+ }
161
+
162
+ // Returns true iff the given custom class name is on the allowlist
163
+ // and should be registered
164
+ constexpr bool custom_class_allowlist_check(std::string_view custom_class_name [[maybe_unused]]) {
165
+ #if !defined(TORCH_CUSTOM_CLASS_ALLOWLIST)
166
+ // If the TORCH_CUSTOM_CLASS_ALLOWLIST parameter is not defined,
167
+ // all custom classes are to be registered
168
+ return true;
169
+ #else
170
+ return allowlist_contains(
171
+ C10_STRINGIZE(TORCH_CUSTOM_CLASS_ALLOWLIST),
172
+ custom_class_name);
173
+ #endif
174
+ }
175
+
176
+ // schema_allowlist_check() implicitly depends on a macro, TORCH_OPERATOR_WHITELIST.
177
+ // Add this API to pass arbitrary allowlist.
178
+ constexpr bool op_allowlist_contains_name_in_schema(std::string_view allowlist, std::string_view schema) {
179
+ return allowlist_contains(allowlist, schema.substr(0, schema.find('(')));
180
+ }
181
+
182
+ } // namespace c10::impl
183
+
184
+ #else
185
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
186
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/op_registration.h ADDED
@@ -0,0 +1,599 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ /**
5
+ * Include this file if you want to register operators. It includes all
6
+ * functionality needed to do so for you.
7
+ */
8
+
9
+ #include <c10/core/DispatchKey.h>
10
+ #include <c10/core/DispatchKeySet.h>
11
+ #include <c10/core/CompileTimeFunctionPointer.h>
12
+ #include <ATen/core/boxing/KernelFunction.h>
13
+ #include <ATen/core/dispatch/CppSignature.h>
14
+ #include <ATen/core/dispatch/RegistrationHandleRAII.h>
15
+ #include <ATen/core/op_registration/infer_schema.h>
16
+ #if defined(EXPOSE_C2_OPS) || !defined(CAFFE2_IS_XPLAT_BUILD)
17
+ #include <torch/csrc/jit/frontend/function_schema_parser.h>
18
+ #endif
19
+ #include <ATen/core/ATenOpList.h>
20
+
21
+ namespace c10 {
22
+
23
+ namespace detail {
24
+ // The first argument of the schema might be of type DispatchKeySet, in which case we remove it.
25
+ // We do this because every argument in a function schema is expected to be convertible
26
+ // to an ivalue, but DispatchKeySet is not a type we want the jit to be aware of.
27
+ // See Note [Plumbing Keys Through The Dispatcher]
28
+ template<class KernelFunctor>
29
+ std::unique_ptr<FunctionSchema> inferFunctionSchemaFromFunctor() {
30
+ using func_type = typename c10::remove_DispatchKeySet_arg_from_func<KernelFunctor>::func_type;
31
+ return std::make_unique<FunctionSchema>(inferFunctionSchemaFlattenedReturns<func_type>());
32
+ }
33
+ }
34
+
35
+ /**
36
+ * An instance of this class handles the registration for one or more operators.
37
+ * Make sure you keep the RegisterOperators instance around since it will
38
+ * deregister the operator it's responsible for in its destructor.
39
+ *
40
+ * Example:
41
+ *
42
+ * > namespace {
43
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
44
+ * > public:
45
+ * > Tensor operator()(Tensor a, Tensor b) {...}
46
+ * > };
47
+ * > }
48
+ * >
49
+ * > static auto registry = c10::RegisterOperators()
50
+ * > .op(c10::RegisterOperators::options()
51
+ * > .schema("my_op")
52
+ * > .kernel<my_kernel_cpu>(DispatchKey::CPU));
53
+ */
54
+ class TORCH_API RegisterOperators final {
55
+ public:
56
+ RegisterOperators() = default;
57
+ ~RegisterOperators() = default;
58
+
59
+ RegisterOperators(const RegisterOperators&) = delete;
60
+ RegisterOperators& operator=(const RegisterOperators&) = delete;
61
+ RegisterOperators(RegisterOperators&&) noexcept = default;
62
+ RegisterOperators& operator=(RegisterOperators&&) noexcept = default;
63
+
64
+ class TORCH_API Options final {
65
+ public:
66
+ Options(const Options&) = delete;
67
+ Options(Options&&) noexcept = delete;
68
+ Options& operator=(const Options&) = delete;
69
+ Options& operator=(Options&&) noexcept = delete;
70
+
71
+ // internal-only for registering stack based kernels
72
+ template<KernelFunction::BoxedKernelFunction* kernel_func>
73
+ Options&& kernel(DispatchKey dispatch_key) && {
74
+ return std::move(*this).kernel(dispatch_key, KernelFunction::makeFromBoxedFunction<kernel_func>(), std::nullopt, nullptr);
75
+ }
76
+
77
+ // internal-only for registering stack based catch-all kernels
78
+ template<KernelFunction::BoxedKernelFunction* kernel_func>
79
+ Options&& catchAllKernel() && {
80
+ return std::move(*this).kernel(std::nullopt, KernelFunction::makeFromBoxedFunction<kernel_func>(), std::nullopt, nullptr);
81
+ }
82
+
83
+ // internal only for registering caffe2 ops
84
+ Options&& schema(FunctionSchema&& schema) {
85
+ TORCH_CHECK(!schemaOrName_.has_value(), "You can only specify the schema once per operator registration.");
86
+ schemaOrName_ = FunctionSchema(std::move(schema));
87
+ return std::move(*this);
88
+ }
89
+
90
+ /**
91
+ * Use this to specify the schema for an operator. You can also specify
92
+ * the operator name only to have the function signature part of the
93
+ * schema be inferred from the kernel function.
94
+ *
95
+ * Example:
96
+ *
97
+ * > // Infer function signature from my_kernel_cpu
98
+ * > static auto registry = c10::RegisterOperators()
99
+ * > .op(c10::RegisterOperators::options()
100
+ * > .schema("my_op")
101
+ * > .kernel<my_kernel_cpu>(DispatchKey::CPU));
102
+ * >
103
+ * >
104
+ * > // Explicitly specify full schema
105
+ * > static auto registry = c10::RegisterOperators()
106
+ * > .op(c10::RegisterOperators::options()
107
+ * > .schema("my_op(Tensor a) -> Tensor")
108
+ * > .kernel<my_kernel_cpu>(DispatchKey::CPU));
109
+ */
110
+ Options&& schema(const std::string& schemaOrName) {
111
+ TORCH_CHECK(!schemaOrName_.has_value(), "Tried to register operator ", schemaOrName," but specified schema multiple times. You can only specify the schema once per operator registration.");
112
+
113
+ #if !defined(EXPOSE_C2_OPS) && defined(CAFFE2_IS_XPLAT_BUILD)
114
+ throw std::logic_error("Tried to register operator " + schemaOrName + ". We don't support registering c10 ops on mobile yet because the function schema parser isn't present in the mobile build.");
115
+ #else
116
+ schemaOrName_ = torch::jit::parseSchemaOrName(schemaOrName);
117
+ #endif
118
+
119
+ return std::move(*this);
120
+ }
121
+
122
+ /**
123
+ * Use this to register an operator whose kernel is implemented as a functor.
124
+ * The kernel is only called for inputs matching the given dispatch key.
125
+ * You can register multiple kernels for different dispatch keys.
126
+ *
127
+ * Example:
128
+ *
129
+ * > namespace {
130
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
131
+ * > public:
132
+ * > Tensor operator()(Tensor a, Tensor b) {...}
133
+ * > };
134
+ * > }
135
+ * >
136
+ * > static auto registry = c10::RegisterOperators()
137
+ * > .op(c10::RegisterOperators::options()
138
+ * > .schema("my_op")
139
+ * > .kernel<my_kernel_cpu>(DispatchKey::CPU));
140
+ *
141
+ * The functor constructor can take arguments to configure the kernel.
142
+ * The arguments are defined in the kernel registration.
143
+ * Example:
144
+ *
145
+ * > namespace {
146
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
147
+ * > public:
148
+ * > explicit my_kernel_cpu(std::string some_configuration, int a, bool b)
149
+ * > : ... {...}
150
+ * >
151
+ * > Tensor operator()(Tensor a, Tensor b) {...}
152
+ * > };
153
+ * > }
154
+ * >
155
+ * > static auto registry = c10::RegisterOperators()
156
+ * > .op(c10::RegisterOperators::options()
157
+ * > .schema("my_op")
158
+ * > .kernel<my_kernel_cpu>(DispatchKey::CPU, "some_configuration", 3, true));
159
+ */
160
+ template<class KernelFunctor, class... ConstructorParameters>
161
+ // enable_if: only enable it if KernelFunctor is actually a functor
162
+ std::enable_if_t<guts::is_functor<KernelFunctor>::value, Options&&> kernel(DispatchKey dispatch_key, ConstructorParameters&&... constructorParameters) && {
163
+ static_assert(std::is_base_of_v<OperatorKernel, KernelFunctor>, "Tried to register a kernel functor using the kernel<Functor>() API, but it doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
164
+ static_assert(std::is_constructible_v<KernelFunctor, ConstructorParameters...>, "Wrong argument list for constructor of kernel functor. The arguments to kernel<Functor>(arguments...) must match one of the constructors of Functor.");
165
+
166
+ return std::move(*this).kernel(
167
+ dispatch_key,
168
+ KernelFunction::makeFromUnboxedFunctor<false, KernelFunctor>(std::make_unique<KernelFunctor>(std::forward<ConstructorParameters>(constructorParameters)...)),
169
+ impl::CppSignature::make<KernelFunctor>(),
170
+ detail::inferFunctionSchemaFromFunctor<KernelFunctor>()
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Use this to register an operator whose kernel is implemented as a functor.
176
+ * The kernel is a catch-all kernel, meaning it's called independent from
177
+ * the input. Dispatch is disabled for this operator.
178
+ *
179
+ * Example:
180
+ *
181
+ * > namespace {
182
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
183
+ * > public:
184
+ * > Tensor operator()(Tensor a, Tensor b) {...}
185
+ * > };
186
+ * > }
187
+ * >
188
+ * > static auto registry = c10::RegisterOperators()
189
+ * > .op(c10::RegisterOperators::options()
190
+ * > .schema("my_op")
191
+ * > .catchAllKernel<my_kernel_cpu>());
192
+ *
193
+ * The functor constructor can take arguments to configure the kernel.
194
+ * The arguments are defined in the kernel registration.
195
+ * Example:
196
+ *
197
+ * > namespace {
198
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
199
+ * > public:
200
+ * > explicit my_kernel_cpu(std::string some_configuration, int a, bool b)
201
+ * > : ... {...}
202
+ * >
203
+ * > Tensor operator()(Tensor a, Tensor b) {...}
204
+ * > };
205
+ * > }
206
+ * >
207
+ * > static auto registry = c10::RegisterOperators()
208
+ * > .op(c10::RegisterOperators::options()
209
+ * > .schema("my_op")
210
+ * > .catchAllKernel<my_kernel_cpu>("some_configuration", 3, true));
211
+ */
212
+ template<class KernelFunctor, class... ConstructorParameters>
213
+ // enable_if: only enable it if KernelFunctor is actually a functor
214
+ std::enable_if_t<guts::is_functor<KernelFunctor>::value, Options&&> catchAllKernel(ConstructorParameters&&... constructorParameters) && {
215
+ static_assert(std::is_base_of_v<OperatorKernel, KernelFunctor>, "Tried to register a kernel functor using the kernel<Functor>() API, but it doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
216
+ static_assert(std::is_constructible_v<KernelFunctor, ConstructorParameters...>, "Wrong argument list for constructor of kernel functor. The arguments to kernel<Functor>(arguments...) must match one of the constructors of Functor.");
217
+
218
+ return std::move(*this).kernel(
219
+ std::nullopt,
220
+ KernelFunction::makeFromUnboxedFunctor<false, KernelFunctor>(std::make_unique<KernelFunctor>(std::forward<ConstructorParameters>(constructorParameters)...)),
221
+ impl::CppSignature::make<KernelFunctor>(),
222
+ detail::inferFunctionSchemaFromFunctor<KernelFunctor>()
223
+ );
224
+ }
225
+
226
+ /**
227
+ * Use this to register an operator whose kernel is implemented by a function.
228
+ * The kernel is only called for inputs matching the given dispatch key.
229
+ * You can register multiple kernels for different dispatch keys.
230
+ *
231
+ * Example:
232
+ *
233
+ * > namespace { Tensor my_kernel_cpu(Tensor a, Tensor b) {...} }
234
+ * >
235
+ * > static auto registry = c10::RegisterOperators()
236
+ * > .op(c10::RegisterOperators::options()
237
+ * > .schema("my_op")
238
+ * > .kernel<decltype(my_kernel_cpu), &my_kernel_cpu>(DispatchKey::CPU));
239
+ */
240
+ template<class FuncType, FuncType* kernel_func>
241
+ // enable_if: only enable it if FuncType is actually a function
242
+ std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> kernel(DispatchKey dispatch_key) && {
243
+ static_assert(!std::is_same_v<FuncType, KernelFunction::BoxedKernelFunction>, "Tried to register a stackbased (i.e. internal) kernel function using the public kernel<...>() API. Please either use the internal kernel(...) API or also implement the kernel function as defined by the public API.");
244
+ static_assert(kernel_func != nullptr, "Kernel function cannot be nullptr");
245
+
246
+ return std::move(*this).kernel(
247
+ dispatch_key,
248
+ KernelFunction::makeFromUnboxedFunction(TORCH_FN(kernel_func)),
249
+ impl::CppSignature::make<FuncType>(),
250
+ // TODO Do schema inference without relying on WrapFunctionIntoFunctor
251
+ detail::inferFunctionSchemaFromFunctor<typename impl::WrapFunctionIntoFunctor<CompileTimeFunctionPointer<FuncType, kernel_func>>::type>()
252
+ );
253
+ }
254
+
255
+ /**
256
+ * Use this to register an operator whose kernel is implemented by a function.
257
+ * The kernel is a catch-all kernel, meaning it's called independent from
258
+ * the input. Dispatch is disabled for this operator.
259
+ *
260
+ * Example:
261
+ *
262
+ * > namespace { Tensor my_kernel_cpu(Tensor a, Tensor b) {...} }
263
+ * >
264
+ * > static auto registry = c10::RegisterOperators()
265
+ * > .op(c10::RegisterOperators::options()
266
+ * > .schema("my_op")
267
+ * > .catchAllKernel<decltype(my_kernel_cpu), &my_kernel_cpu>());
268
+ */
269
+ template<class FuncType, FuncType* kernel_func>
270
+ // enable_if: only enable it if FuncType is actually a function
271
+ std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> catchAllKernel() && {
272
+ static_assert(!std::is_same_v<FuncType, KernelFunction::BoxedKernelFunction>, "Tried to register a stackbased (i.e. internal) kernel function using the public kernel<...>() API. Please either use the internal kernel(...) API or also implement the kernel function as defined by the public API.");
273
+ static_assert(kernel_func != nullptr, "Kernel function cannot be nullptr");
274
+
275
+ return std::move(*this).kernel(
276
+ std::nullopt,
277
+ KernelFunction::makeFromUnboxedFunction(TORCH_FN(kernel_func)),
278
+ impl::CppSignature::make<FuncType>(),
279
+ // TODO Do schema inference without relying on WrapFunctionIntoFunctor
280
+ detail::inferFunctionSchemaFromFunctor<typename impl::WrapFunctionIntoFunctor<CompileTimeFunctionPointer<FuncType, kernel_func>>::type>()
281
+ );
282
+ }
283
+
284
+ template<class FuncType>
285
+ // enable_if: only enable it if FuncType is actually a function
286
+ std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> kernel(DispatchKey dispatch_key, FuncType* kernel_func) && {
287
+ static_assert(!std::is_same_v<FuncType, KernelFunction::BoxedKernelFunction>, "Tried to register a stackbased (i.e. internal) kernel function using the public kernel<...>() API. Please either use the internal kernel(...) API or also implement the kernel function as defined by the public API.");
288
+ TORCH_INTERNAL_ASSERT(kernel_func != nullptr, "Kernel function cannot be nullptr");
289
+
290
+ return std::move(*this).kernel(
291
+ dispatch_key,
292
+ KernelFunction::makeFromUnboxedRuntimeFunction(kernel_func),
293
+ impl::CppSignature::make<FuncType>(),
294
+ // TODO Do schema inference without relying on WrapFunctionIntoFunctor
295
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>()
296
+ );
297
+ }
298
+
299
+ template<class FuncType>
300
+ // enable_if: only enable it if FuncType is actually a function
301
+ std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> catchAllKernel(FuncType* kernel_func) && {
302
+ static_assert(!std::is_same_v<FuncType, KernelFunction::BoxedKernelFunction>, "Tried to register a stackbased (i.e. internal) kernel function using the public kernel<...>() API. Please either use the internal kernel(...) API or also implement the kernel function as defined by the public API.");
303
+ TORCH_INTERNAL_ASSERT(kernel_func != nullptr, "Kernel function cannot be nullptr");
304
+
305
+ return std::move(*this).kernel(
306
+ std::nullopt,
307
+ KernelFunction::makeFromUnboxedRuntimeFunction(kernel_func),
308
+ impl::CppSignature::make<FuncType>(),
309
+ // TODO Do schema inference without relying on WrapFunctionIntoFunctor
310
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>()
311
+ );
312
+ }
313
+
314
+ /**
315
+ * Use this to register an operator whose kernel is implemented as a lambda.
316
+ * The kernel is only called for inputs matching the given dispatch key.
317
+ * You can register multiple kernels for different dispatch keys.
318
+ *
319
+ * The lambda must be stateless, i.e. not have a capture. If your kernel
320
+ * needs to store some configuration parameters, write the kernel as a
321
+ * functor instead.
322
+ *
323
+ * Example:
324
+ *
325
+ * > static auto registry = c10::RegisterOperators()
326
+ * > .op(c10::RegisterOperators::options()
327
+ * > .schema("my_op")
328
+ * > .kernel(DispatchKey::CPU, [] (Tensor a) -> Tensor {...}));
329
+ */
330
+ template<class Lambda>
331
+ // enable_if: only enable it if Lambda is a functor (note: lambdas are functors)
332
+ std::enable_if_t<
333
+ guts::is_functor<std::decay_t<Lambda>>::value
334
+ && !std::is_same_v<typename guts::infer_function_traits_t<std::decay_t<Lambda>>::func_type, KernelFunction::BoxedKernelFunction>,
335
+ Options&&> kernel(DispatchKey dispatch_key, Lambda&& functor) && {
336
+ static_assert(!std::is_base_of_v<OperatorKernel, std::decay_t<Lambda>>, "The kernel(x) API for registering a kernel is only meant to be used with lambdas. Your kernel is a functor. Please use the kernel<Functor>() API instead.");
337
+
338
+ // We don't support stateful lambdas (i.e. lambdas with a capture), because their
339
+ // behavior would be nonobvious. A functor kernel with cache gets a new instance of
340
+ // its cache each time the kernel is looked up from the dispatch table.
341
+ // A lambda with a capture would be global and share its capture between all kernel lookups.
342
+ // So, instead of making users having to think about it (including the thread-safety
343
+ // issues this causes), let's just forbid stateful lambdas altogether.
344
+ static_assert(guts::is_stateless_lambda<std::decay_t<Lambda>>::value, "The kernel(x) API for registering a kernel only works for stateless lambdas (i.e. lambdas without captures). If you need a cache, please use the functor based API kernel<Functor>() instead.");
345
+
346
+ return std::move(*this).kernel(
347
+ dispatch_key,
348
+ KernelFunction::makeFromUnboxedLambda(std::forward<Lambda>(functor)),
349
+ impl::CppSignature::make<Lambda>(),
350
+ // TODO Do schema inference without relying on WrapFunctionIntoRuntimeFunctor
351
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>()
352
+ );
353
+ }
354
+
355
+ /**
356
+ * Use this to register an operator whose kernel is implemented as a lambda.
357
+ * The kernel is a catch-all kernel, meaning it's called independent from
358
+ * the input. Dispatch is disabled for this operator.
359
+ *
360
+ * The lambda must be stateless, i.e. not have a capture. If your kernel
361
+ * needs to store some configuration parameters, write the kernel as a
362
+ * functor instead.
363
+ *
364
+ * Example:
365
+ *
366
+ * > static auto registry = c10::RegisterOperators()
367
+ * > .op(c10::RegisterOperators::options()
368
+ * > .schema("my_op")
369
+ * > .catchAllKernel([] (Tensor a) -> Tensor {...}));
370
+ */
371
+ template<class Lambda>
372
+ // enable_if: only enable it if Lambda is a functor (note: lambdas are functors)
373
+ std::enable_if_t<
374
+ guts::is_functor<std::decay_t<Lambda>>::value
375
+ && !std::is_same_v<typename guts::infer_function_traits_t<std::decay_t<Lambda>>::func_type, KernelFunction::BoxedKernelFunction>,
376
+ Options&&> catchAllKernel(Lambda&& lambda) && {
377
+ static_assert(!std::is_base_of_v<OperatorKernel, std::decay_t<Lambda>>, "The kernel(x) API for registering a kernel is only meant to be used with lambdas. Your kernel is a functor. Please use the kernel<Functor>() API instead.");
378
+
379
+ // We don't support stateful lambdas (i.e. lambdas with a capture), because their
380
+ // behavior would be nonobvious.
381
+ // A lambda with a capture would be global and share its capture between all kernel lookups.
382
+ // This would be a likely source for unexpected race conditions, so we forbid it.
383
+ // If a kernel really needs global state, they can just have regular global state
384
+ // in their .cpp file next to the kernel lambda.
385
+ static_assert(guts::is_stateless_lambda<std::decay_t<Lambda>>::value, "The kernel(x) API for registering a kernel only works for stateless lambdas (i.e. lambdas without captures). If you need a cache, please use the functor based API kernel<Functor>() instead.");
386
+
387
+ return std::move(*this).kernel(
388
+ std::nullopt,
389
+ KernelFunction::makeFromUnboxedLambda(std::forward<Lambda>(lambda)),
390
+ impl::CppSignature::make<Lambda>(),
391
+ // TODO Do schema inference without relying on WrapFunctionIntoRuntimeFunctor
392
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>()
393
+ );
394
+ }
395
+
396
+ Options&& aliasAnalysis(AliasAnalysisKind aliasAnalysisKind) && {
397
+ TORCH_CHECK(!aliasAnalysisKind_.has_value(), "You can only call aliasAnalysis() once per operator registration.");
398
+ aliasAnalysisKind_ = aliasAnalysisKind;
399
+ return std::move(*this);
400
+ }
401
+
402
+ private:
403
+ Options&& kernel(std::optional<DispatchKey> dispatch_key, KernelFunction&& func, std::optional<impl::CppSignature> cpp_signature, std::unique_ptr<FunctionSchema>&& inferred_function_schema) && {
404
+ KernelRegistrationConfig config;
405
+ config.dispatch_key = dispatch_key;
406
+ config.func = std::move(func);
407
+ config.cpp_signature = cpp_signature;
408
+ config.inferred_function_schema = std::move(inferred_function_schema);
409
+ kernels.push_back(std::move(config));
410
+ return std::move(*this);
411
+ }
412
+
413
+ Options()
414
+ : schemaOrName_(std::nullopt)
415
+ , aliasAnalysisKind_(std::nullopt)
416
+ {}
417
+
418
+ // KernelRegistrationConfig accumulates all information from the config
419
+ // parameters passed to a RegisterOperators::op() call into one object.
420
+ struct KernelRegistrationConfig final {
421
+ KernelRegistrationConfig()
422
+ : dispatch_key(std::nullopt)
423
+ , cpp_signature(std::nullopt)
424
+ , inferred_function_schema(nullptr)
425
+ {}
426
+
427
+ std::optional<DispatchKey> dispatch_key;
428
+ KernelFunction func;
429
+ std::optional<impl::CppSignature> cpp_signature;
430
+ std::unique_ptr<FunctionSchema> inferred_function_schema;
431
+ };
432
+
433
+ std::optional<std::variant<OperatorName, FunctionSchema>> schemaOrName_;
434
+
435
+ std::vector<KernelRegistrationConfig> kernels;
436
+ std::optional<AliasAnalysisKind> aliasAnalysisKind_;
437
+ friend class RegisterOperators;
438
+ friend class Library;
439
+ };
440
+
441
+ /**
442
+ * Call this to get an instance of registration options, which
443
+ * can be passed to a call to RegisterOperators::op() to specify
444
+ * these options for the operator registration.
445
+ * See class doc comment for examples.
446
+ */
447
+ static Options options() {
448
+ return {};
449
+ }
450
+
451
+ /**
452
+ * Call this to register an operator. See class doc comment for examples.
453
+ */
454
+ RegisterOperators&& op(Options&& options) && {
455
+ checkSchemaAndRegisterOp_(std::move(options));
456
+ return std::move(*this);
457
+ }
458
+
459
+ // Regular mutator version of the && version above
460
+ RegisterOperators& op(Options&& options) & {
461
+ checkSchemaAndRegisterOp_(std::move(options));
462
+ return *this;
463
+ }
464
+
465
+ /**
466
+ * This is a shorthand for RegisterOperators::op(Options) where you can
467
+ * specify the operator schema outside of the options parameter.
468
+ * See class doc comment for examples.
469
+ */
470
+ RegisterOperators&& op(const std::string& schemaOrName, Options&& options = RegisterOperators::options()) && {
471
+ return std::move(*this).op(std::move(options).schema(schemaOrName));
472
+ }
473
+
474
+ // internal only for registering caffe2 ops
475
+ RegisterOperators&& op(FunctionSchema schema, Options&& options) && {
476
+ return std::move(*this).op(std::move(options).schema(std::move(schema)));
477
+ }
478
+
479
+ template<class FuncType>
480
+ explicit RegisterOperators(const std::string& schemaOrName, FuncType&& func, Options&& options = RegisterOperators::options())
481
+ : RegisterOperators() {
482
+ std::move(*this).op(schemaOrName, std::forward<FuncType>(func), std::move(options));
483
+ }
484
+
485
+ /**
486
+ * This API registers an operator based on a kernel function pointer.
487
+ *
488
+ * Given a kernel
489
+ *
490
+ * > namespace { Tensor my_kernel_cpu(Tensor a, Tensor b) {...} }
491
+ *
492
+ * This API looks like:
493
+ *
494
+ * > static auto registry = c10::RegisterOperators()
495
+ * > .op("my_op", &my_kernel_cpu);
496
+ *
497
+ * If your kernel is small and the overhead of calling it matters,
498
+ * then this API might be the wrong choice since the following API
499
+ * has a slightly lower overhead for calling into the kernel:
500
+ *
501
+ * > static auto registry = c10::RegisterOperators()
502
+ * > .op("my_op", c10::RegisterOperators::options()
503
+ * > .kernel<decltype(my_kernel_cpu), &my_kernel_cpu>());
504
+ *
505
+ * Or, alternatively, write your kernel as a functor:
506
+ *
507
+ * > namespace {
508
+ * > class my_kernel_cpu final : public c10::OperatorKernel {
509
+ * > public:
510
+ * > Tensor operator()(Tensor a, Tensor b) {...}
511
+ * > };
512
+ * > }
513
+ * >
514
+ * > static auto registry = c10::RegisterOperators()
515
+ * > .op("my_op", c10::RegisterOperators::options()
516
+ * > .kernel<my_kernel_cpu>());
517
+ */
518
+ template<class FuncType>
519
+ // enable_if: only enable it if FuncType is actually a function, but not a stack based BoxedKernelFunction.
520
+ std::enable_if_t<guts::is_function_type<FuncType>::value && !std::is_same_v<FuncType, KernelFunction::BoxedKernelFunction>, RegisterOperators&&>
521
+ op(const std::string& schemaOrName, FuncType* func, Options&& options = RegisterOperators::options()) && {
522
+ constexpr bool AllowLegacyTypes = true;
523
+ return std::move(*this).op(std::move(options).schema(schemaOrName).kernel(
524
+ std::nullopt,
525
+ KernelFunction::makeFromUnboxedRuntimeFunction<AllowLegacyTypes>(func),
526
+ impl::CppSignature::make<FuncType>(),
527
+ // TODO Do schema inference without relying on WrapFunctionIntoRuntimeFunctor
528
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>()
529
+ ));
530
+ }
531
+
532
+ /**
533
+ * This API registers an operator based on a kernel lambda.
534
+ *
535
+ * This API looks like:
536
+ *
537
+ * > static auto registry = c10::RegisterOperators()
538
+ * > .op("my_op", [] (Tensor a, Tensor b) {...});
539
+ *
540
+ * This is equivalent to:
541
+ *
542
+ * > static auto registry = c10::RegisterOperators()
543
+ * > .op("my_op", c10::RegisterOperators::options()
544
+ * > .catchAllKernel([] (Tensor a, Tensor b) {...}));
545
+ *
546
+ */
547
+ template<class Lambda>
548
+ // enable_if: only enable it if Lambda is actually a stateless lambda
549
+ std::enable_if_t<guts::is_functor<Lambda>::value && guts::is_stateless_lambda<std::decay_t<Lambda>>::value, RegisterOperators&&>
550
+ op(const std::string& schemaOrName, Lambda&& lambda, Options&& options = RegisterOperators::options()) && {
551
+ static_assert(!std::is_base_of_v<OperatorKernel, Lambda>, "c10::OperatorKernel is part of the new kernel registration API and shouldn't be used together with the deprecated registration API. Please use the new RegisterOperators::options().kernel() based API instead.");
552
+
553
+ constexpr bool AllowLegacyTypes = true;
554
+ return std::move(*this).op(std::move(options).schema(schemaOrName).kernel(
555
+ std::nullopt,
556
+ KernelFunction::makeFromUnboxedLambda<AllowLegacyTypes>(std::forward<Lambda>(lambda)),
557
+ impl::CppSignature::make<Lambda>(),
558
+ // TODO Do schema inference without relying on WrapFunctionIntoRuntimeFunctor
559
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>()
560
+ ));
561
+ }
562
+
563
+ template<class Lambda>
564
+ C10_DEPRECATED_MESSAGE("Registering operator kernels with stateful lambdas (i.e. lambdas with a capture) has non-obvious behavior. This is deprecated. Please use a lambda without a capture or a functor class instead.")
565
+ // enable_if: only enable it if Lambda is actually a functor but not a stateless lambda
566
+ std::enable_if_t<guts::is_functor<Lambda>::value && !guts::is_stateless_lambda<std::decay_t<Lambda>>::value, RegisterOperators&&>
567
+ op(const std::string& schemaOrName, Lambda&& lambda, Options&& options = RegisterOperators::options()) && {
568
+ static_assert(!std::is_base_of_v<OperatorKernel, Lambda>, "c10::OperatorKernel is part of the new kernel registration API and shouldn't be used together with the deprecated registration API. Please use the new RegisterOperators::options().kernel() based API instead.");
569
+
570
+ constexpr bool AllowLegacyTypes = true;
571
+ return std::move(*this).op(std::move(options).schema(schemaOrName).kernel(
572
+ std::nullopt,
573
+ KernelFunction::makeFromUnboxedLambda<AllowLegacyTypes>(std::forward<Lambda>(lambda)),
574
+ impl::CppSignature::make<Lambda>(),
575
+ // TODO Do schema inference without relying on WrapFunctionIntoRuntimeFunctor
576
+ detail::inferFunctionSchemaFromFunctor<impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>()
577
+ ));
578
+ }
579
+
580
+ private:
581
+ void checkSchemaAndRegisterOp_(Options&& config);
582
+
583
+ static c10::FunctionSchema inferSchemaFromKernels_(const OperatorName& opNameStr, const Options& options);
584
+ void checkNoDuplicateKernels_(const Options& options);
585
+ void registerOp_(Options&& options);
586
+
587
+ std::vector<RegistrationHandleRAII> registrars_;
588
+ };
589
+
590
+ } // namespace c10
591
+
592
+ namespace torch {
593
+ // Old-style API
594
+ using RegisterOperators = c10::RegisterOperators;
595
+ }
596
+
597
+ #else
598
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
599
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/qualified_name.h ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/util/ArrayRef.h>
5
+ #include <c10/util/Exception.h>
6
+ #include <c10/util/StringUtil.h>
7
+ #include <c10/util/irange.h>
8
+ #include <string>
9
+
10
+ namespace c10 {
11
+
12
+ // Represents a name of the form "foo.bar.baz"
13
+ struct QualifiedName {
14
+ QualifiedName() = default;
15
+
16
+ // `name` can be a dotted string, like "foo.bar.baz", or just a bare name.
17
+ /* implicit */ QualifiedName(const std::string& name) {
18
+ TORCH_CHECK(!name.empty());
19
+ // split the string into its atoms.
20
+ size_t startSearchFrom = 0;
21
+ size_t pos = name.find(delimiter_, startSearchFrom);
22
+
23
+ while (pos != std::string::npos) {
24
+ auto atom = name.substr(startSearchFrom, pos - startSearchFrom);
25
+ TORCH_INTERNAL_ASSERT(
26
+ !atom.empty(), "Invalid name for qualified name: '", name, "'");
27
+ atoms_.push_back(std::move(atom));
28
+ startSearchFrom = pos + 1;
29
+ pos = name.find(delimiter_, startSearchFrom);
30
+ }
31
+
32
+ auto finalAtom = name.substr(startSearchFrom);
33
+ TORCH_INTERNAL_ASSERT(
34
+ !finalAtom.empty(), "Invalid name for qualified name: '", name, "'");
35
+ atoms_.emplace_back(std::move(finalAtom));
36
+
37
+ cacheAccessors();
38
+ }
39
+
40
+ explicit QualifiedName(std::vector<std::string> atoms) : atoms_(std::move(atoms)) {
41
+ for (const auto& atom : atoms_) {
42
+ TORCH_CHECK(!atom.empty(), "Atom cannot be empty");
43
+ TORCH_CHECK(
44
+ atom.find(delimiter_) == std::string::npos,
45
+ "Delimiter not allowed in atom");
46
+ }
47
+
48
+ cacheAccessors();
49
+ }
50
+ // Unnecessary copy. Ideally we'd use something like std::string_view.
51
+ /* implicit */ QualifiedName(const char* name)
52
+ : QualifiedName(std::string(name)) {}
53
+
54
+ // `name` must be a bare name (no dots!)
55
+ explicit QualifiedName(const QualifiedName& prefix, std::string name) {
56
+ TORCH_INTERNAL_ASSERT(!name.empty());
57
+ TORCH_INTERNAL_ASSERT(name.find(delimiter_) == std::string::npos);
58
+ atoms_.insert(atoms_.begin(), prefix.atoms_.begin(), prefix.atoms_.end());
59
+ atoms_.push_back(std::move(name));
60
+
61
+ cacheAccessors();
62
+ }
63
+
64
+ // Is `this` a prefix of `other`?
65
+ // For example, "foo.bar" is a prefix of "foo.bar.baz"
66
+ bool isPrefixOf(const QualifiedName& other) const {
67
+ const auto& thisAtoms = atoms_;
68
+ const auto& otherAtoms = other.atoms_;
69
+
70
+ if (thisAtoms.size() > otherAtoms.size()) {
71
+ // Can't be a prefix if it's bigger
72
+ return false;
73
+ }
74
+ for (const auto i : c10::irange(thisAtoms.size())) {
75
+ if (thisAtoms[i] != otherAtoms[i]) {
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ }
81
+
82
+ // The fully qualified name, like "foo.bar.baz"
83
+ const std::string& qualifiedName() const {
84
+ return qualifiedName_;
85
+ }
86
+
87
+ // The leading qualifier, like "foo.bar"
88
+ const std::string& prefix() const {
89
+ return prefix_;
90
+ }
91
+
92
+ // The base name, like "baz"
93
+ const std::string& name() const {
94
+ return name_;
95
+ }
96
+
97
+ const std::vector<std::string>& atoms() const {
98
+ return atoms_;
99
+ }
100
+
101
+ bool operator==(const QualifiedName& other) const {
102
+ return this->qualifiedName_ == other.qualifiedName_;
103
+ }
104
+
105
+ bool operator!=(const QualifiedName& other) const {
106
+ return !(*this == other);
107
+ }
108
+
109
+ private:
110
+ static constexpr char delimiter_ = '.';
111
+
112
+ // Helper for cacheAccessors() below.
113
+ template<typename T>
114
+ std::string join(char delimiter, const T& v) {
115
+ std::string out;
116
+ size_t reserve = 0;
117
+ for (const auto& e : v) {
118
+ reserve += e.size() + 1;
119
+ }
120
+ out.reserve(reserve);
121
+ for (const auto i : c10::irange(v.size())) {
122
+ if (i != 0) {
123
+ out.push_back(delimiter);
124
+ }
125
+ out.append(v[i]);
126
+ }
127
+ return out;
128
+ }
129
+
130
+ void cacheAccessors() {
131
+ qualifiedName_ = join(delimiter_, atoms_);
132
+ if (atoms_.size() > 1) {
133
+ ArrayRef<std::string> view(atoms_);
134
+ const auto prefixView = view.slice(0, view.size() - 1);
135
+ prefix_ = join(delimiter_, prefixView);
136
+ }
137
+
138
+ if (!atoms_.empty()) {
139
+ name_ = atoms_.back();
140
+ }
141
+ }
142
+
143
+ // The actual list of names, like "{foo, bar, baz}"
144
+ std::vector<std::string> atoms_;
145
+
146
+ /*
147
+ * Cached accessors, derived from `atoms_`.
148
+ */
149
+ std::string qualifiedName_;
150
+ std::string prefix_;
151
+ std::string name_;
152
+ };
153
+ } // namespace c10
154
+
155
+ namespace std {
156
+ template <>
157
+ struct hash<c10::QualifiedName> {
158
+ size_t operator()(const c10::QualifiedName& n) const noexcept {
159
+ return std::hash<std::string>()(n.qualifiedName());
160
+ }
161
+ };
162
+ } // namespace std
163
+
164
+ #else
165
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
166
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/rref_interface.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/util/intrusive_ptr.h>
5
+ #include <ATen/core/jit_type_base.h>
6
+
7
+ namespace c10 {
8
+
9
+ struct Type;
10
+ using worker_id_t = int16_t;
11
+
12
+ // This abstract class contains only user-facing APIs, and will be shared
13
+ // between jit and distributed to implement TorchScript support.
14
+ class C10_EXPORT RRefInterface : public c10::intrusive_ptr_target {
15
+ public:
16
+ RRefInterface() = default;
17
+ // RRef is made NOT copyable NOT movable to prevent messing up reference
18
+ // counting.
19
+ RRefInterface(const RRefInterface& other) = delete;
20
+ RRefInterface(RRefInterface&& other) = delete;
21
+ RRefInterface& operator=(const RRefInterface& other) = delete;
22
+ RRefInterface& operator=(RRefInterface&& other) = delete;
23
+
24
+ ~RRefInterface() override = default;
25
+
26
+ // returns the worker id of the owner
27
+ virtual worker_id_t owner() const = 0;
28
+
29
+ // returns the worker name of the owner
30
+ virtual std::string ownerName() const = 0;
31
+
32
+ // Returns true if this is the ``OwnerRRef``
33
+ virtual bool isOwner() const = 0;
34
+
35
+ // Returns true if this is an ``OwnerRRef`` or if this ``UserRRef`` has been
36
+ // confirmed by its owner.
37
+ virtual bool confirmedByOwner() const = 0;
38
+
39
+ virtual const TypePtr type() const = 0;
40
+ };
41
+
42
+ }
43
+
44
+ #else
45
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
46
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/stack.h ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <type_traits>
5
+
6
+ #include <ATen/core/ivalue.h>
7
+ #include <c10/util/Deprecated.h>
8
+ #include <c10/util/irange.h>
9
+
10
+ // TODO move this to c10 namespace
11
+
12
+
13
+ namespace torch::jit {
14
+
15
+ using c10::IValue;
16
+ using Stack = std::vector<IValue>;
17
+
18
+ class Operation {
19
+ template <typename F, typename Arg>
20
+ using accepts = std::is_constructible<std::function<void(Arg)>, F&&>;
21
+
22
+ public:
23
+ template <typename F,
24
+ std::enable_if_t<accepts<F, Stack*>::value, int> = 0>
25
+ C10_DEPRECATED_MESSAGE("Please use void(Stack&) to register operator instead.")
26
+ Operation(F&& raw): op_([raw = std::forward<F>(raw)](Stack& stack) {
27
+ raw(&stack);
28
+ }) {}
29
+
30
+ template <typename F,
31
+ std::enable_if_t<accepts<F, Stack&>::value &&
32
+ !std::is_same_v<std::decay_t<F>, Operation>, int> = 0>
33
+ Operation(F&& op): op_(std::forward<F>(op)) {}
34
+
35
+ Operation(std::nullptr_t) noexcept {}
36
+
37
+ explicit operator bool() const noexcept {
38
+ return op_ ? true : false;
39
+ }
40
+
41
+ void operator()(Stack& stack) {
42
+ op_(stack);
43
+ }
44
+
45
+ template <typename T>
46
+ T* target() noexcept {
47
+ return op_.target<T>();
48
+ }
49
+
50
+ private:
51
+ std::function<void(Stack&)> op_;
52
+ };
53
+
54
+ // An operation with N inputs and M outputs pops the last N inputs off
55
+ // the stack and pushes its M inputs onto the stack
56
+ // before: <other stack items> I0, I1, ... IN <- stack.back()
57
+ // after: <other stack items> O0, O1, ... OM
58
+ // operations are defined this way so that ownership of inputs can be
59
+ // transferred to the operation and it can incrementally drop ownership of
60
+ // tensors when they become unneeded. For large operations, like 'run an entire
61
+ // subgraph', this functionality is very important for minimizing gpu memory
62
+ // usage return value is the relative 'offset' to jump to for the next
63
+ // operation:
64
+ // pc += 1 + offset
65
+ // so a return value of 0 goes to the next instruction
66
+
67
+ // treat the last N elements of the stack as a list, looking up
68
+ // element i
69
+ inline IValue& peek(Stack& stack, size_t i, size_t N) {
70
+ // NOLINTNEXTLINE(*-narrowing-conversions)
71
+ return *(stack.end() - N + i);
72
+ }
73
+ inline IValue& peek(Stack* stack, size_t i, size_t N) {
74
+ return peek(*stack, i, N);
75
+ }
76
+ inline const IValue& peek(const Stack& stack, size_t i, size_t N) {
77
+ // NOLINTNEXTLINE(*-narrowing-conversions)
78
+ return *(stack.end() - N + i);
79
+ }
80
+ inline const IValue& peek(const Stack* stack, size_t i, size_t N) {
81
+ return peek(*stack, i, N);
82
+ }
83
+ // treat the last N elements of the stack as a list, looking up the
84
+ // slice starting at index i and having length len
85
+ inline at::ArrayRef<IValue> peekSlice(
86
+ const Stack& stack,
87
+ size_t i,
88
+ size_t len,
89
+ size_t N) {
90
+ return at::ArrayRef<IValue>(stack).slice(stack.size() - N + i, len);
91
+ }
92
+ inline at::ArrayRef<IValue> last(const Stack& stack, size_t N) {
93
+ return peekSlice(stack, 0, N, N);
94
+ }
95
+ inline at::ArrayRef<IValue> last(const Stack* stack, size_t N) {
96
+ return last(*stack, N);
97
+ }
98
+ inline void drop(Stack& stack, size_t n) {
99
+ // NOLINTNEXTLINE(*-narrowing-conversions)
100
+ stack.erase(stack.end() - n, stack.end());
101
+ }
102
+ inline void drop(Stack* stack, size_t n) {
103
+ drop(*stack, n);
104
+ }
105
+ inline IValue pop(Stack& stack) {
106
+ TORCH_CHECK(!stack.empty(), "pop() called on empty stack");
107
+ auto r = std::move(stack.back());
108
+ stack.pop_back();
109
+ return r;
110
+ }
111
+ inline IValue pop(Stack* stack) {
112
+ return pop(*stack);
113
+ }
114
+ inline std::vector<IValue> pop(Stack& stack, size_t n) {
115
+ std::vector<IValue> result;
116
+ result.reserve(n);
117
+ for (const auto i : c10::irange(n)) {
118
+ result.push_back(std::move(peek(stack, i, n)));
119
+ }
120
+ drop(stack, n);
121
+ return result;
122
+ }
123
+
124
+ // variadic pop:
125
+ // int64_t a; at::Tensor b;
126
+ // pop(stack, a, b);
127
+ // equivalent to:
128
+ // b = pop(stack).toTensor();
129
+ // a = pop(stack).toInt();
130
+ template <typename... Types>
131
+ inline void pop(Stack& stack, Types&... args) {
132
+ size_t i = 0;
133
+ constexpr size_t N = sizeof...(args);
134
+ (void)std::initializer_list<int>{
135
+ (args = std::move(peek(stack, i++, N)).template to<Types>(), 0)...};
136
+ drop(stack, N);
137
+ }
138
+ template <typename... Types>
139
+ inline void pop(Stack* stack, Types&... args) {
140
+ pop(*stack, args...);
141
+ }
142
+ template <typename Type>
143
+ inline void push_one(Stack& stack, Type&& arg) {
144
+ stack.emplace_back(std::forward<Type>(arg));
145
+ }
146
+
147
+ inline void push_one(Stack& stack, c10::TensorOptions options) {
148
+ stack.emplace_back(c10::typeMetaToScalarType(options.dtype()));
149
+ stack.emplace_back(options.layout());
150
+ stack.emplace_back(options.device());
151
+ stack.emplace_back(options.pinned_memory());
152
+ }
153
+
154
+ template <typename... Types>
155
+ inline void push(Stack& stack, Types&&... args) {
156
+ (void)std::initializer_list<int>{(push_one(stack, std::forward<Types>(args)), 0)...};
157
+ }
158
+ template <typename... Types>
159
+ inline void push(Stack* stack, Types&&... args) {
160
+ return push(*stack, std::forward<Types>(args)...);
161
+ }
162
+ template <class T>
163
+ inline void push_list_elements(Stack& stack, const c10::List<T>& elements) {
164
+ for (T elem : elements) {
165
+ stack.push_back(std::move(elem));
166
+ }
167
+ }
168
+
169
+ // The packer here is carefully written not to make any unnecessary
170
+ // copies.
171
+
172
+ // pack takes the return values of aten functions pushes them onto the stack
173
+ template <typename T>
174
+ inline void pack(Stack& stack, T&& v) {
175
+ stack.emplace_back(std::forward<T>(v));
176
+ }
177
+ template <typename T>
178
+ inline void pack(Stack* stack, T&& v) {
179
+ pack(*stack, std::forward<T>(v));
180
+ }
181
+
182
+ template <std::size_t remaining, typename... Args>
183
+ struct TuplePacker {
184
+ // NB: *Not* a universal reference.
185
+ static void execute(Stack& stack, std::tuple<Args...>&& t) {
186
+ // NB: The move here does not "destroy" the entire tuple, that is
187
+ // not what std::move does; only the particular tuple index
188
+ // processed here gets stolen.
189
+ pack(stack, std::get<sizeof...(Args) - remaining>(std::move(t)));
190
+ TuplePacker<remaining - 1, Args...>::execute(stack, std::move(t));
191
+ }
192
+ };
193
+
194
+ template <typename... Args>
195
+ struct TuplePacker<0, Args...> {
196
+ // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
197
+ static void execute(Stack& /*stack*/, std::tuple<Args...>&& /*t*/){}
198
+ };
199
+
200
+ template <typename... Args>
201
+ inline void pack(Stack& stack, std::tuple<Args...>&& t) {
202
+ TuplePacker<sizeof...(Args), Args...>::execute(stack, std::move(t));
203
+ }
204
+
205
+ } // namespace torch::jit
206
+
207
+ #else
208
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
209
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/symbol.h ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ #include <c10/macros/Export.h>
4
+ #include <cstdint>
5
+ #include <functional> // For std::hash
6
+ #include <string>
7
+
8
+
9
+ namespace c10 {
10
+
11
+ // 'prim' symbols are synthetic operators that occur only in the IR
12
+ // and don't have corresponding implementations in ATen.
13
+
14
+ // 'onnx' symbols correspond to ONNX operators. Their semantics
15
+ // are defined in https://github.com/onnx/onnx/blob/master/docs/Operators.md
16
+ // The particular version we are targeting is specified by '_onnx_opset_version'
17
+ // in torch.onnx.symbolic_helper
18
+ //
19
+ // In general, most ONNX operators won't get an entry here, because they
20
+ // are handled from the Python end. However, you may occasionally need
21
+ // to intern an ONNX symbol here so that you can conveniently write an
22
+ // optimization on ONNX operations.
23
+
24
+ // 'attr' symbols are attribute keys. They are shared between both ONNX and ATen
25
+ // operators (you disambiguate their meaning by looking at the operator itself).
26
+ // In general, you only need to define attribute keys that are used by
27
+ // onnx or prim; ATen attributes are automatically generated in FORALL_ATTR_BASE_SYMBOLS.
28
+
29
+ // Note [Symbol allocation]
30
+ // ~~~~~~~~~~~~~~~~~~~~~~~~
31
+ //
32
+ // 1. Symbol namespace is split up into namespaces.
33
+ //
34
+ // 2. The intended access pattern for built-in symbols is onnx::MatMul
35
+ // in the c10 namespace (this is a Symbol).
36
+ //
37
+
38
+ // Built-in constant definition strategy:
39
+ // - Enum is the most convenient way to generate a contiguous sequence
40
+ // of numbers for an identifier.
41
+ // - However, an enum gives you a fresh type. We want onnx::MatMul to
42
+ // be type Symbol, not some random enum type!
43
+ // - Therefore, after using enums to generate the sequence of integers,
44
+ // we then declare constexpr Symbols to get everything the actual Symbol
45
+ // type we want. Symbols must be constexpr to be valid to be "case"ed on.
46
+
47
+ using unique_t = uint32_t;
48
+
49
+ const std::string& domain_prefix();
50
+
51
+ // A Symbol is like an interned string, but with a little extra
52
+ // structure; it is namespaced via SymbolNamespace and the resulting
53
+ // intern pointers support efficient namespace testing.
54
+ struct TORCH_API Symbol {
55
+ explicit constexpr Symbol() : value(0) {}
56
+ explicit constexpr Symbol(unique_t uniq)
57
+ : value(uniq) {}
58
+
59
+ // Get a Symbol for a qualified string like "attr::bar"
60
+ static Symbol fromQualString(const std::string & s);
61
+
62
+ // Get a Symbol from a domain and an unqualified string like "org.pytorch.attr" and "bar"
63
+ static Symbol fromDomainAndUnqualString(const std::string & d, const std::string & s);
64
+
65
+ // Constructors for our various namespaced strings. This will construct
66
+ // the appropriate namespaced string, e.g., "attr::foo" for the
67
+ // argument "foo", and then attempt to intern it. DO NOT USE THIS
68
+ // with a string literal; attr::foo should be available in that case
69
+ // (and if it's not, you should add it to the built-ins list above.)
70
+ static Symbol attr(const std::string & s);
71
+ static Symbol aten(const std::string & s);
72
+ static Symbol cuda(const std::string & s);
73
+ static Symbol onnx(const std::string & s);
74
+ static Symbol prim(const std::string & s);
75
+ static Symbol user(const std::string & s);
76
+ static Symbol caffe2(const std::string & s);
77
+ static Symbol dimname(const std::string & s);
78
+ // TODO: eliminate me
79
+ static Symbol scope(const std::string & s);
80
+
81
+ bool is_attr() const;
82
+ bool is_aten() const;
83
+ bool is_cuda() const;
84
+ bool is_prim() const;
85
+ bool is_prims() const;
86
+ bool is_nvprims() const;
87
+ bool is_onnx() const;
88
+ bool is_user() const;
89
+ bool is_caffe2() const;
90
+ bool is_dimname() const;
91
+
92
+ // So we can switch on this
93
+ constexpr operator unique_t() const {
94
+ return value;
95
+ }
96
+
97
+ Symbol ns() const;
98
+
99
+ // Give a string corresponding to the unqualified version of this name, e.g.,
100
+ // "mm". Use this in a context where the intended namespace of the string is
101
+ // obvious; this is a *lossy* conversion.
102
+ const char * toUnqualString() const;
103
+
104
+ // Give a string corresponding to the qualified version of this name,
105
+ // e.g., "aten::mm". This string format is made available to Python bindings
106
+ // (so we know how to parse it.)
107
+ const char * toQualString() const;
108
+
109
+ // This describes a symbol in a case where humans read it. At the moment it's
110
+ // the same as toQualString. This has to be a const char* returned because
111
+ // a lot of printf style macros use it.
112
+ const char * toDisplayString() const;
113
+
114
+ // Give a string corresponding to the domain name for the symbol,
115
+ // e.g., "org.pytorch.aten".
116
+ std::string domainString() const;
117
+
118
+ private:
119
+
120
+ explicit Symbol(Symbol ns, const std::string & s);
121
+ unique_t value;
122
+ };
123
+
124
+ static inline bool operator==(Symbol lhs, Symbol rhs) {
125
+ return static_cast<unique_t>(lhs) == static_cast<unique_t>(rhs);
126
+ }
127
+
128
+ inline Symbol Symbol::attr(const std::string & s) { return Symbol::fromQualString("attr::" + s); }
129
+ inline Symbol Symbol::aten(const std::string & s) { return Symbol::fromQualString("aten::" + s); }
130
+ inline Symbol Symbol::cuda(const std::string & s) { return Symbol::fromQualString("cuda::" + s); }
131
+ inline Symbol Symbol::onnx(const std::string & s) { return Symbol::fromQualString("onnx::" + s); }
132
+ inline Symbol Symbol::prim(const std::string & s) { return Symbol::fromQualString("prim::" + s); }
133
+ inline Symbol Symbol::scope(const std::string & s) { return Symbol::fromQualString("scope::" + s); }
134
+ inline Symbol Symbol::user(const std::string & s) { return Symbol::fromQualString("user::" + s); }
135
+ inline Symbol Symbol::caffe2(const std::string & s) { return Symbol::fromQualString("_caffe2::" + s); }
136
+ inline Symbol Symbol::dimname(const std::string & s) { return Symbol::fromQualString("dimname::" + s); }
137
+
138
+ } // namespace c10
139
+
140
+ // make symbol behave like an integer in hash tables
141
+ namespace std {
142
+ template <>
143
+ struct hash<c10::Symbol> {
144
+ size_t operator()(c10::Symbol s) const {
145
+ return std::hash<uint32_t>()(static_cast<uint32_t>(s));
146
+ }
147
+ };
148
+ }
149
+
150
+ #else
151
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
152
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/type_factory.h ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <type_traits>
5
+ #include <unordered_map>
6
+
7
+ #include <ATen/core/dynamic_type.h>
8
+ #include <ATen/core/jit_type_base.h>
9
+ #include <c10/macros/Macros.h>
10
+
11
+ namespace c10 {
12
+
13
+ template <typename T>
14
+ struct TORCH_API TypeFactoryBase {};
15
+
16
+ template <>
17
+ struct TORCH_API TypeFactoryBase<c10::DynamicType> {
18
+ template <typename T, typename... Args>
19
+ static c10::DynamicTypePtr create(TypePtr ty, Args&&... args) {
20
+ return std::make_shared<c10::DynamicType>(
21
+ c10::DynamicTypeTrait<T>::tagValue(),
22
+ c10::DynamicType::Arguments(c10::ArrayRef<c10::TypePtr>(
23
+ {std::move(ty), std::forward<Args>(args)...})));
24
+ }
25
+ template <typename T>
26
+ static c10::DynamicTypePtr create(const std::vector<c10::TypePtr>& types) {
27
+ return std::make_shared<c10::DynamicType>(
28
+ c10::DynamicTypeTrait<T>::tagValue(),
29
+ c10::DynamicType::Arguments(types));
30
+ }
31
+ static c10::DynamicTypePtr createNamedTuple(
32
+ const std::string& name,
33
+ const std::vector<std::string_view>& fields,
34
+ const std::vector<c10::TypePtr>& types) {
35
+ return std::make_shared<c10::DynamicType>(
36
+ c10::DynamicType::Tag::Tuple,
37
+ name,
38
+ c10::DynamicType::Arguments(fields, types));
39
+ }
40
+ template <typename T>
41
+ C10_ERASE static c10::DynamicTypePtr createNamed(const std::string& name) {
42
+ return std::make_shared<c10::DynamicType>(
43
+ c10::DynamicTypeTrait<T>::tagValue(),
44
+ name,
45
+ c10::DynamicType::Arguments{});
46
+ }
47
+ template <typename T>
48
+ C10_ERASE static decltype(auto) get() {
49
+ return DynamicTypeTrait<T>::getBaseType();
50
+ }
51
+ static const std::unordered_map<std::string, c10::TypePtr>& basePythonTypes();
52
+ };
53
+
54
+ using DynamicTypeFactory = TypeFactoryBase<c10::DynamicType>;
55
+
56
+ // Helper functions for constructing DynamicTypes inline.
57
+ template <
58
+ typename T,
59
+ std::enable_if_t<DynamicTypeTrait<T>::isBaseType, int> = 0>
60
+ C10_ERASE DynamicTypePtr dynT() {
61
+ return DynamicTypeFactory::get<T>();
62
+ }
63
+
64
+ template <
65
+ typename T,
66
+ typename... Args,
67
+ std::enable_if_t<!DynamicTypeTrait<T>::isBaseType, int> = 0>
68
+ C10_ERASE DynamicTypePtr dynT(Args&&... args) {
69
+ return DynamicTypeFactory::create<T>(std::forward<Args>(args)...);
70
+ }
71
+
72
+ template <>
73
+ struct TORCH_API TypeFactoryBase<c10::Type> {
74
+ template <typename T, typename... Args>
75
+ static c10::TypePtr create(TypePtr ty, Args&&... args) {
76
+ return T::create(std::move(ty), std::forward<Args>(args)...);
77
+ }
78
+ template <typename T>
79
+ static c10::TypePtr create(std::vector<c10::TypePtr> types) {
80
+ return T::create(std::move(types));
81
+ }
82
+ static c10::TypePtr createNamedTuple(
83
+ const std::string& name,
84
+ const std::vector<std::string_view>& fields,
85
+ const std::vector<c10::TypePtr>& types);
86
+ template <typename T>
87
+ C10_ERASE static c10::TypePtr createNamed(const std::string& name) {
88
+ return T::create(name);
89
+ }
90
+ static const std::unordered_map<std::string, c10::TypePtr>& basePythonTypes();
91
+ template <typename T>
92
+ C10_ERASE static c10::TypePtr get() {
93
+ return T::get();
94
+ }
95
+ };
96
+
97
+ using DefaultTypeFactory = TypeFactoryBase<c10::Type>;
98
+
99
+ using PlatformType =
100
+ #ifdef C10_MOBILE
101
+ c10::DynamicType
102
+ #else
103
+ c10::Type
104
+ #endif
105
+ ;
106
+
107
+ using TypeFactory = TypeFactoryBase<PlatformType>;
108
+
109
+ } // namespace c10
110
+
111
+ #else
112
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
113
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/type_ptr.h ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <memory>
5
+ #include <type_traits>
6
+
7
+ #include <c10/util/Exception.h>
8
+ #include <c10/util/MaybeOwned.h>
9
+
10
+ namespace c10 {
11
+
12
+ // Compatibility wrapper around a raw pointer so that existing code
13
+ // written to deal with a shared_ptr can keep working.
14
+ template <typename T>
15
+ class SingletonTypePtr {
16
+ public:
17
+ /* implicit */ SingletonTypePtr(T* p) : repr_(p) {}
18
+
19
+ // We need this to satisfy Pybind11, but it shouldn't be hit.
20
+ explicit SingletonTypePtr(std::shared_ptr<T> /*unused*/) { TORCH_CHECK(false); }
21
+
22
+ using element_type = typename std::shared_ptr<T>::element_type;
23
+
24
+ template <typename U = T, std::enable_if_t<!std::is_same_v<std::remove_const_t<U>, void>, bool> = true>
25
+ T& operator*() const {
26
+ return *repr_;
27
+ }
28
+
29
+ T* get() const {
30
+ return repr_;
31
+ }
32
+
33
+ T* operator->() const {
34
+ return repr_;
35
+ }
36
+
37
+ operator bool() const {
38
+ return repr_ != nullptr;
39
+ }
40
+
41
+ private:
42
+ T* repr_{nullptr};
43
+ };
44
+
45
+ template <typename T, typename U>
46
+ bool operator==(SingletonTypePtr<T> lhs, SingletonTypePtr<U> rhs) {
47
+ return (void*)lhs.get() == (void*)rhs.get();
48
+ }
49
+
50
+ template <typename T, typename U>
51
+ bool operator!=(SingletonTypePtr<T> lhs, SingletonTypePtr<U> rhs) {
52
+ return !(lhs == rhs);
53
+ }
54
+
55
+ } // namespace c10
56
+
57
+ #else
58
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
59
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/typeid.h ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #include <c10/util/typeid.h>
3
+
4
+ #else
5
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
6
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/FlushDenormal.h ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ /// Flush-To-Zero and Denormals-Are-Zero mode
3
+ ///
4
+ /// Flush-To-Zero (FTZ) and Denormals-Are-Zero (DAZ) are modes that bypass
5
+ /// IEEE 754 methods of dealing with denormal floating-point numbers on x86-64
6
+ /// and some x86 CPUs. They result in reduced precision for values near zero,
7
+ /// but increased performance.
8
+ ///
9
+ /// See https://software.intel.com/en-us/articles/x87-and-sse-floating-point-assists-in-ia-32-flush-to-zero-ftz-and-denormals-are-zero-daz
10
+
11
+ namespace at::cpu {
12
+
13
+ bool set_flush_denormal(bool on);
14
+
15
+ } // namespace at::cpu
16
+
17
+ #else
18
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
19
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/Utils.h ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <cstdint>
5
+
6
+ #include <c10/macros/Export.h>
7
+
8
+ namespace at::cpu {
9
+
10
+ TORCH_API bool is_avx2_supported();
11
+ TORCH_API bool is_avx512_supported();
12
+
13
+ // Detect if CPU support Vector Neural Network Instruction.
14
+ TORCH_API bool is_avx512_vnni_supported();
15
+
16
+ // Detect if CPU supports AVX512_BF16 ISA
17
+ TORCH_API bool is_avx512_bf16_supported();
18
+
19
+ // Detect if CPU support Advanced Matrix Extension.
20
+ TORCH_API bool is_amx_tile_supported();
21
+
22
+ // Detect if CPU support Advanced Matrix Extension for fp16.
23
+ TORCH_API bool is_amx_fp16_supported();
24
+
25
+ // Enable the system to use AMX instructions.
26
+ TORCH_API bool init_amx();
27
+
28
+ // Get the L1 cache size per core in Byte
29
+ TORCH_API uint32_t L1d_cache_size();
30
+
31
+ // Get the L2 cache size per core in Byte
32
+ TORCH_API uint32_t L2_cache_size();
33
+
34
+ } // namespace at::cpu
35
+
36
+ #else
37
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
38
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional.h ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/functional_base.h>
5
+ #include <ATen/cpu/vec/functional_bfloat16.h>
6
+
7
+ #else
8
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
9
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_base.h ADDED
@@ -0,0 +1,480 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with AVX]
6
+
7
+ #include <ATen/cpu/vec/vec.h>
8
+ #include <c10/util/irange.h>
9
+
10
+ namespace at {
11
+ namespace detail {
12
+ // We prefer to convert through float for reduced-precision floating
13
+ // point types if we have a Vectorized specialization for float and we
14
+ // don't have one for the actual type in question.
15
+ template <typename T>
16
+ struct should_prefer_converting_through_float
17
+ : std::bool_constant<
18
+ is_reduced_floating_point_v<T> &&
19
+ vec::is_vec_specialized_for_v<float> &&
20
+ !vec::is_vec_specialized_for_v<T>> {};
21
+
22
+ template <typename T>
23
+ constexpr auto should_prefer_converting_through_float_v =
24
+ should_prefer_converting_through_float<T>::value;
25
+ } // namespace detail
26
+
27
+ namespace vec {
28
+ // slow path
29
+ template <typename scalar_t, typename Op>
30
+ inline scalar_t vec_reduce_all(
31
+ const Op& vec_fun,
32
+ vec::Vectorized<scalar_t> acc_vec,
33
+ int64_t size) {
34
+ using Vec = vec::Vectorized<scalar_t>;
35
+ scalar_t acc_arr[Vec::size()];
36
+ acc_vec.store(acc_arr);
37
+ for (const auto i : c10::irange(1, size)) {
38
+ std::array<scalar_t, Vec::size()> acc_arr_next = {0};
39
+ acc_arr_next[0] = acc_arr[i];
40
+ Vec acc_vec_next = Vec::loadu(acc_arr_next.data());
41
+ acc_vec = vec_fun(acc_vec, acc_vec_next);
42
+ }
43
+ acc_vec.store(acc_arr);
44
+ return acc_arr[0];
45
+ }
46
+
47
+ template <typename scalar_t, typename Op>
48
+ struct VecReduceAllSIMD {
49
+ static inline scalar_t apply(
50
+ const Op& vec_fun,
51
+ const Vectorized<scalar_t>& acc_vec) {
52
+ return vec_reduce_all(vec_fun, acc_vec, Vectorized<scalar_t>::size());
53
+ }
54
+ };
55
+
56
+ #if defined(__GNUC__) && (__GNUC__ > 5) && !defined(_MSC_VER) && \
57
+ !defined(C10_MOBILE)
58
+ #if defined(CPU_CAPABILITY_AVX2)
59
+ template <typename Op>
60
+ struct VecReduceAllSIMD<float, Op> {
61
+ static inline float apply(
62
+ const Op& vec_fun,
63
+ const Vectorized<float>& acc_vec) {
64
+ using Vec = Vectorized<float>;
65
+ Vec v = acc_vec;
66
+ // 128-bit shuffle
67
+ Vec v1 = _mm256_permute2f128_ps(v, v, 0x1);
68
+ v = vec_fun(v, v1);
69
+ // 64-bit shuffle
70
+ v1 = _mm256_shuffle_ps(v, v, 0x4E);
71
+ v = vec_fun(v, v1);
72
+ // 32-bit shuffle
73
+ v1 = _mm256_shuffle_ps(v, v, 0xB1);
74
+ v = vec_fun(v, v1);
75
+ return _mm256_cvtss_f32(v);
76
+ }
77
+ };
78
+ #endif // defined(CPU_CAPABILITY_AVX2)
79
+ #if defined(CPU_CAPABILITY_AVX512)
80
+ template <typename Op>
81
+ struct VecReduceAllSIMD<float, Op> {
82
+ static inline float apply(
83
+ const Op& vec_fun,
84
+ const Vectorized<float>& acc_vec) {
85
+ using Vec = Vectorized<float>;
86
+ Vec v = acc_vec;
87
+ // 256-bit shuffle
88
+ Vec v1 = _mm512_shuffle_f32x4(v, v, 0x4E);
89
+ v = vec_fun(v, v1);
90
+ // 128-bit shuffle
91
+ v1 = _mm512_shuffle_f32x4(v, v, 0xB1);
92
+ v = vec_fun(v, v1);
93
+ // 64-bit shuffle
94
+ v1 = _mm512_shuffle_ps(v, v, 0x4E);
95
+ v = vec_fun(v, v1);
96
+ // 32-bit shuffle
97
+ v1 = _mm512_shuffle_ps(v, v, 0xB1);
98
+ v = vec_fun(v, v1);
99
+ return _mm512_cvtss_f32(v);
100
+ }
101
+ };
102
+ #endif // defined(CPU_CAPABILITY_AVX512)
103
+ #endif // defined(__GNUC__) && (__GNUC__ > 5) && !defined(_MSC_VER) &&
104
+ // !defined(C10_MOBILE)
105
+
106
+ #if defined(__aarch64__) && !defined(C10_MOBILE) && !defined(__CUDACC__) && \
107
+ !defined(CPU_CAPABILITY_SVE)
108
+ template <typename Op>
109
+ struct VecReduceAllSIMD<float, Op> {
110
+ static inline float apply(
111
+ const Op& vec_fun,
112
+ const Vectorized<float>& acc_vec) {
113
+ using Vec = Vectorized<float>;
114
+ Vec v = acc_vec;
115
+
116
+ // 64-bit shuffle: [a1+a5, a2+a6, a3+a7, a4+a8, -, -, -, -] -> [a3+a7,
117
+ // a4+a8, a1+a5, a2+a6, -, -, -, -]
118
+ float32x4_t v1_1 = vextq_f32(v, v, 2);
119
+ Vec v1 = v1_1;
120
+ // [a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, -, -, -, -]
121
+ v = vec_fun(v, v1);
122
+
123
+ // 32-bit shuffle: [a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, -,
124
+ // -, -, -] -> [a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, -, -, -,
125
+ // -]
126
+ v1_1 = vrev64q_f32(v);
127
+ v1 = v1_1;
128
+ // [a1+a2+a3+a4+a5+a6+a7+a8, a1+a2+a3+a4+a5+a6+a7+a8,
129
+ // a1+a2+a3+a4+a5+a6+a7+a8, a1+a2+a3+a4+a5+a6+a7+a8, -, -, -, -]
130
+ v = vec_fun(v, v1);
131
+
132
+ return v[0];
133
+ }
134
+ };
135
+
136
+ template <>
137
+ struct VecReduceAllSIMD<float, std::plus<Vectorized<float>>> {
138
+ static inline float apply(
139
+ const std::plus<Vectorized<float>>& vec_fun,
140
+ const Vectorized<float>& acc_vec) {
141
+ return vaddvq_f32(acc_vec);
142
+ }
143
+ };
144
+ #endif // defined(__aarch64__) && !defined(C10_MOBILE) && !defined(__CUDACC__)
145
+ // && !defined(CPU_CAPABILITY_SVE)
146
+
147
+ #if defined(__aarch64__) && !defined(C10_MOBILE) && !defined(__CUDACC__) && \
148
+ defined(CPU_CAPABILITY_SVE256)
149
+ template <typename Op>
150
+ struct VecReduceAllSIMD<float, Op> {
151
+ static inline float apply(
152
+ const Op& vec_fun,
153
+ const Vectorized<float>& acc_vec) {
154
+ using Vec = Vectorized<float>;
155
+ Vec v = acc_vec;
156
+ // 128-bit shuffle
157
+ svuint32_t ind = svdupq_n_u32(4, 5, 6, 7);
158
+ Vec v1 = svtbl_f32(v, ind);
159
+ v = vec_fun(v, v1);
160
+ // 64-bit shuffle
161
+ ind = svdupq_n_u32(2, 3, 0, 1);
162
+ v1 = svtbl_f32(v, ind);
163
+ v = vec_fun(v, v1);
164
+ // 32-bit shuffle
165
+ ind = svdupq_n_u32(1, 0, 2, 3);
166
+ v1 = svtbl_f32(v, ind);
167
+ v = vec_fun(v, v1);
168
+ return svlasta(svpfalse(), v);
169
+ }
170
+ };
171
+ #endif // defined(__aarch64__) && !defined(C10_MOBILE) && !defined(__CUDACC__)
172
+ // && defined(CPU_CAPABILITY_SVE256)
173
+
174
+ template <typename scalar_t, typename Op>
175
+ inline scalar_t vec_reduce_all(
176
+ const Op& vec_fun,
177
+ const Vectorized<scalar_t>& acc_vec) {
178
+ return VecReduceAllSIMD<scalar_t, Op>::apply(vec_fun, acc_vec);
179
+ }
180
+
181
+ template <
182
+ typename scalar_t,
183
+ typename Op,
184
+ typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
185
+ inline scalar_t reduce_all(
186
+ const Op& vec_fun,
187
+ const scalar_t* data,
188
+ int64_t size) {
189
+ using Vec = vec::Vectorized<scalar_t>;
190
+ if (size < Vec::size())
191
+ return vec_reduce_all(vec_fun, Vec::loadu(data, size), size);
192
+ int64_t d = Vec::size();
193
+ Vec acc_vec = Vec::loadu(data);
194
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
195
+ Vec data_vec = Vec::loadu(data + d);
196
+ acc_vec = vec_fun(acc_vec, data_vec);
197
+ }
198
+ if (size - d > 0) {
199
+ Vec data_vec = Vec::loadu(data + d, size - d);
200
+ acc_vec = Vec::set(acc_vec, vec_fun(acc_vec, data_vec), size - d);
201
+ }
202
+ return vec_reduce_all(vec_fun, acc_vec);
203
+ }
204
+
205
+ // similar to reduce_all, but reduces into two outputs
206
+ template <
207
+ typename scalar_t,
208
+ typename Op1,
209
+ typename Op2,
210
+ typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
211
+ inline std::pair<scalar_t, scalar_t> reduce2_all(
212
+ const Op1& vec_fun1,
213
+ const Op2& vec_fun2,
214
+ const scalar_t* data,
215
+ int64_t size) {
216
+ using Vec = vec::Vectorized<scalar_t>;
217
+ if (size < Vec::size()) {
218
+ auto loaded_data = Vec::loadu(data, size);
219
+ return std::pair<scalar_t, scalar_t>(
220
+ vec_reduce_all(vec_fun1, loaded_data, size),
221
+ vec_reduce_all(vec_fun2, loaded_data, size));
222
+ }
223
+ int64_t d = Vec::size();
224
+ Vec acc_vec1 = Vec::loadu(data);
225
+ Vec acc_vec2 = Vec::loadu(data);
226
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
227
+ Vec data_vec = Vec::loadu(data + d);
228
+ acc_vec1 = vec_fun1(acc_vec1, data_vec);
229
+ acc_vec2 = vec_fun2(acc_vec2, data_vec);
230
+ }
231
+ if (size - d > 0) {
232
+ Vec data_vec = Vec::loadu(data + d, size - d);
233
+ acc_vec1 = Vec::set(acc_vec1, vec_fun1(acc_vec1, data_vec), size - d);
234
+ acc_vec2 = Vec::set(acc_vec2, vec_fun2(acc_vec2, data_vec), size - d);
235
+ }
236
+ return std::pair<scalar_t, scalar_t>(
237
+ vec_reduce_all(vec_fun1, acc_vec1), vec_reduce_all(vec_fun2, acc_vec2));
238
+ }
239
+
240
+ template <
241
+ typename scalar_t,
242
+ typename MapOp,
243
+ typename ReduceOp,
244
+ typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
245
+ inline scalar_t map_reduce_all(
246
+ const MapOp& map_fun,
247
+ const ReduceOp& red_fun,
248
+ const scalar_t* data,
249
+ int64_t size) {
250
+ using Vec = vec::Vectorized<scalar_t>;
251
+ if (size < Vec::size())
252
+ return vec_reduce_all(red_fun, map_fun(Vec::loadu(data, size)), size);
253
+ int64_t d = Vec::size();
254
+ Vec acc_vec = map_fun(Vec::loadu(data));
255
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
256
+ Vec data_vec = Vec::loadu(data + d);
257
+ data_vec = map_fun(data_vec);
258
+ acc_vec = red_fun(acc_vec, data_vec);
259
+ }
260
+ if (size - d > 0) {
261
+ Vec data_vec = Vec::loadu(data + d, size - d);
262
+ data_vec = map_fun(data_vec);
263
+ acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
264
+ }
265
+ return vec_reduce_all(red_fun, acc_vec);
266
+ }
267
+
268
+ template <
269
+ typename scalar_t,
270
+ typename MapOp,
271
+ typename ReduceOp,
272
+ typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
273
+ inline scalar_t map2_reduce_all(
274
+ const MapOp& map_fun,
275
+ const ReduceOp& red_fun,
276
+ const scalar_t* data,
277
+ const scalar_t* data2,
278
+ int64_t size) {
279
+ using Vec = vec::Vectorized<scalar_t>;
280
+ if (size < Vec::size()) {
281
+ Vec data_vec = Vec::loadu(data, size);
282
+ Vec data2_vec = Vec::loadu(data2, size);
283
+ data_vec = map_fun(data_vec, data2_vec);
284
+ return vec_reduce_all(red_fun, data_vec, size);
285
+ }
286
+ int64_t d = Vec::size();
287
+ Vec acc_vec = map_fun(Vec::loadu(data), Vec::loadu(data2));
288
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
289
+ Vec data_vec = Vec::loadu(data + d);
290
+ Vec data2_vec = Vec::loadu(data2 + d);
291
+ data_vec = map_fun(data_vec, data2_vec);
292
+ acc_vec = red_fun(acc_vec, data_vec);
293
+ }
294
+ if (size - d > 0) {
295
+ Vec data_vec = Vec::loadu(data + d, size - d);
296
+ Vec data2_vec = Vec::loadu(data2 + d, size - d);
297
+ data_vec = map_fun(data_vec, data2_vec);
298
+ acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
299
+ }
300
+ return vec_reduce_all(red_fun, acc_vec);
301
+ }
302
+
303
+ template <
304
+ typename scalar_t,
305
+ typename MapOp,
306
+ typename ReduceOp,
307
+ typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
308
+ inline scalar_t map3_reduce_all(
309
+ const MapOp& map_fun,
310
+ const ReduceOp& red_fun,
311
+ const scalar_t* data,
312
+ const scalar_t* data2,
313
+ const scalar_t* data3,
314
+ int64_t size) {
315
+ using Vec = vec::Vectorized<scalar_t>;
316
+ if (size < Vec::size()) {
317
+ Vec data_vec = Vec::loadu(data, size);
318
+ Vec data2_vec = Vec::loadu(data2, size);
319
+ Vec data3_vec = Vec::loadu(data3, size);
320
+ data_vec = map_fun(data_vec, data2_vec, data3_vec);
321
+ return vec_reduce_all(red_fun, data_vec, size);
322
+ }
323
+
324
+ int64_t d = Vec::size();
325
+ Vec acc_vec = map_fun(Vec::loadu(data), Vec::loadu(data2), Vec::loadu(data3));
326
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
327
+ Vec data_vec = Vec::loadu(data + d);
328
+ Vec data2_vec = Vec::loadu(data2 + d);
329
+ Vec data3_vec = Vec::loadu(data3 + d);
330
+ data_vec = map_fun(data_vec, data2_vec, data3_vec);
331
+ acc_vec = red_fun(acc_vec, data_vec);
332
+ }
333
+ if (size - d > 0) {
334
+ Vec data_vec = Vec::loadu(data + d, size - d);
335
+ Vec data2_vec = Vec::loadu(data2 + d, size - d);
336
+ Vec data3_vec = Vec::loadu(data3 + d, size - d);
337
+ data_vec = map_fun(data_vec, data2_vec, data3_vec);
338
+ acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
339
+ }
340
+ return vec_reduce_all(red_fun, acc_vec);
341
+ }
342
+
343
+ template <
344
+ typename scalar_t,
345
+ typename Op,
346
+ typename std::enable_if_t<
347
+ !detail::should_prefer_converting_through_float_v<scalar_t> &&
348
+ std::is_invocable_v<Op, vec::Vectorized<scalar_t>>,
349
+ int> = 0>
350
+ inline void map(
351
+ const Op& vec_fun,
352
+ scalar_t* output_data,
353
+ const scalar_t* input_data,
354
+ int64_t size) {
355
+ using Vec = vec::Vectorized<scalar_t>;
356
+ int64_t d = 0;
357
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
358
+ Vec output_vec = vec_fun(Vec::loadu(input_data + d));
359
+ output_vec.store(output_data + d);
360
+ }
361
+ if (size - d > 0) {
362
+ Vec output_vec = vec_fun(Vec::loadu(input_data + d, size - d));
363
+ output_vec.store(output_data + d, size - d);
364
+ }
365
+ }
366
+
367
+ template <
368
+ typename scalar_t,
369
+ typename Op,
370
+ typename std::enable_if_t<
371
+ !detail::should_prefer_converting_through_float_v<scalar_t> &&
372
+ std::is_invocable_v<
373
+ Op,
374
+ vec::Vectorized<scalar_t>,
375
+ vec::Vectorized<scalar_t>>,
376
+ int> = 0>
377
+ inline void map2(
378
+ const Op& vec_fun,
379
+ scalar_t* output_data,
380
+ const scalar_t* input_data,
381
+ const scalar_t* input_data2,
382
+ int64_t size) {
383
+ using Vec = vec::Vectorized<scalar_t>;
384
+ int64_t d = 0;
385
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
386
+ Vec data_vec = Vec::loadu(input_data + d);
387
+ Vec data_vec2 = Vec::loadu(input_data2 + d);
388
+ Vec output_vec = vec_fun(data_vec, data_vec2);
389
+ output_vec.store(output_data + d);
390
+ }
391
+ if (size - d > 0) {
392
+ Vec data_vec = Vec::loadu(input_data + d, size - d);
393
+ Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
394
+ Vec output_vec = vec_fun(data_vec, data_vec2);
395
+ output_vec.store(output_data + d, size - d);
396
+ }
397
+ }
398
+
399
+ template <
400
+ typename scalar_t,
401
+ typename Op,
402
+ typename std::enable_if_t<
403
+ !detail::should_prefer_converting_through_float_v<scalar_t> &&
404
+ std::is_invocable_v<
405
+ Op,
406
+ vec::Vectorized<scalar_t>,
407
+ vec::Vectorized<scalar_t>,
408
+ vec::Vectorized<scalar_t>>,
409
+ int> = 0>
410
+ inline void map3(
411
+ const Op& vec_fun,
412
+ scalar_t* output_data,
413
+ const scalar_t* input_data1,
414
+ const scalar_t* input_data2,
415
+ const scalar_t* input_data3,
416
+ int64_t size) {
417
+ using Vec = vec::Vectorized<scalar_t>;
418
+ int64_t d = 0;
419
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
420
+ Vec data_vec1 = Vec::loadu(input_data1 + d);
421
+ Vec data_vec2 = Vec::loadu(input_data2 + d);
422
+ Vec data_vec3 = Vec::loadu(input_data3 + d);
423
+ Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3);
424
+ output_vec.store(output_data + d);
425
+ }
426
+ if (size - d > 0) {
427
+ Vec data_vec1 = Vec::loadu(input_data1 + d, size - d);
428
+ Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
429
+ Vec data_vec3 = Vec::loadu(input_data3 + d, size - d);
430
+ Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3);
431
+ output_vec.store(output_data + d, size - d);
432
+ }
433
+ }
434
+
435
+ template <
436
+ typename scalar_t,
437
+ typename Op,
438
+ typename std::enable_if_t<
439
+ !detail::should_prefer_converting_through_float_v<scalar_t> &&
440
+ std::is_invocable_v<
441
+ Op,
442
+ vec::Vectorized<scalar_t>,
443
+ vec::Vectorized<scalar_t>,
444
+ vec::Vectorized<scalar_t>,
445
+ vec::Vectorized<scalar_t>>,
446
+ int> = 0>
447
+ inline void map4(
448
+ const Op& vec_fun,
449
+ scalar_t* output_data,
450
+ const scalar_t* input_data1,
451
+ const scalar_t* input_data2,
452
+ const scalar_t* input_data3,
453
+ const scalar_t* input_data4,
454
+ int64_t size) {
455
+ using Vec = vec::Vectorized<scalar_t>;
456
+ int64_t d = 0;
457
+ for (; d < size - (size % Vec::size()); d += Vec::size()) {
458
+ Vec data_vec1 = Vec::loadu(input_data1 + d);
459
+ Vec data_vec2 = Vec::loadu(input_data2 + d);
460
+ Vec data_vec3 = Vec::loadu(input_data3 + d);
461
+ Vec data_vec4 = Vec::loadu(input_data4 + d);
462
+ Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3, data_vec4);
463
+ output_vec.store(output_data + d);
464
+ }
465
+ if (size - d > 0) {
466
+ Vec data_vec1 = Vec::loadu(input_data1 + d, size - d);
467
+ Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
468
+ Vec data_vec3 = Vec::loadu(input_data3 + d, size - d);
469
+ Vec data_vec4 = Vec::loadu(input_data4 + d, size - d);
470
+ Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3, data_vec4);
471
+ output_vec.store(output_data + d, size - d);
472
+ }
473
+ }
474
+
475
+ } // namespace vec
476
+ } // namespace at
477
+
478
+ #else
479
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
480
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_bfloat16.h ADDED
@@ -0,0 +1,652 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with AVX]
6
+
7
+ #include <ATen/cpu/vec/vec.h>
8
+
9
+ namespace at::vec {
10
+ // BFloat16 specification
11
+ template <typename scalar_t>
12
+ struct VecScalarType {
13
+ using type = scalar_t;
14
+ };
15
+ template <>
16
+ struct VecScalarType<BFloat16> {
17
+ using type = float;
18
+ };
19
+ template <>
20
+ struct VecScalarType<Half> {
21
+ using type = float;
22
+ };
23
+
24
+ // This is different from at::acc_type since we only need to specialize BFloat16
25
+ template <typename scalar_t>
26
+ using vec_scalar_t = typename VecScalarType<scalar_t>::type;
27
+
28
+ // Vector conversion between float and bfloat16/half
29
+ template <>
30
+ inline std::tuple<Vectorized<float>, Vectorized<float>> convert_to_float<
31
+ BFloat16>(const Vectorized<BFloat16>& a) {
32
+ return convert_bfloat16_float(a);
33
+ }
34
+
35
+ template <>
36
+ inline std::tuple<Vectorized<float>, Vectorized<float>> convert_to_float<Half>(
37
+ const Vectorized<Half>& a) {
38
+ return convert_half_float(a);
39
+ }
40
+
41
+ template <>
42
+ inline Vectorized<BFloat16> convert_from_float<BFloat16>(
43
+ const Vectorized<float>& a,
44
+ const Vectorized<float>& b) {
45
+ return convert_float_bfloat16(a, b);
46
+ }
47
+
48
+ template <>
49
+ inline Vectorized<Half> convert_from_float<Half>(
50
+ const Vectorized<float>& a,
51
+ const Vectorized<float>& b) {
52
+ return convert_float_half(a, b);
53
+ }
54
+
55
+ template <
56
+ typename scalar_t,
57
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
58
+ inline void load_to_float(
59
+ const scalar_t* data,
60
+ Vectorized<float>& out1,
61
+ Vectorized<float>& out2);
62
+
63
+ template <>
64
+ inline void load_to_float<BFloat16>(
65
+ const BFloat16* data,
66
+ Vectorized<float>& out1,
67
+ Vectorized<float>& out2) {
68
+ load_fp32_from_bf16(data, out1, out2);
69
+ }
70
+
71
+ template <>
72
+ inline void load_to_float<Half>(
73
+ const Half* data,
74
+ Vectorized<float>& out1,
75
+ Vectorized<float>& out2) {
76
+ load_fp32_from_fp16(data, out1, out2);
77
+ }
78
+
79
+ template <
80
+ typename scalar_t,
81
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
82
+ inline void load_to_float(const scalar_t* data, Vectorized<float>& out);
83
+
84
+ template <>
85
+ inline void load_to_float<BFloat16>(
86
+ const BFloat16* data,
87
+ Vectorized<float>& out) {
88
+ load_fp32_from_bf16(data, out);
89
+ }
90
+
91
+ template <>
92
+ inline void load_to_float<Half>(const Half* data, Vectorized<float>& out) {
93
+ load_fp32_from_fp16(data, out);
94
+ }
95
+
96
+ // Note that we already have specialized member of Vectorized<scalar_t> for
97
+ // BFloat16 so the following functions would run smoothly:
98
+ // using Vec = Vectorized<BFloat16>;
99
+ // Vec one = Vec(BFloat16(1));
100
+ // vec::map([](Vec x) { return one / (one + x.exp()); }, y_ptr, x_ptr, N);
101
+ //
102
+ // Then why we still need to specialize "functional"?
103
+ // If we do specialization at Vectorized<> level, the above example would need
104
+ // 3 pairs of conversion of bf16->fp32/fp32->bf16, each for ".exp()", "+" and
105
+ // "/". If we do specialization at vec::map<>() level, we have only 1 pair of
106
+ // conversion of bf16->fp32/fp32->bf16, for the input and output BFloat16
107
+ // vector only.
108
+ //
109
+ // The following BFloat16 functionality will only do data type conversion for
110
+ // input and output vector (reduce functionality will only convert the final
111
+ // scalar back to bf16). Compared to Vectorized<> specialization,
112
+ // 1. better performance since we have less data type conversion;
113
+ // 2. less rounding error since immediate results are kept in fp32;
114
+ // 3. accumulation done on data type of fp32.
115
+ //
116
+ // If you plan to extend this file, please ensure adding unit tests at
117
+ // aten/src/ATen/test/vec_test_all_types.cpp
118
+ //
119
+ template <
120
+ typename scalar_t,
121
+ typename Op,
122
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
123
+ inline float reduce_all(const Op& vec_fun, const scalar_t* data, int64_t size) {
124
+ using bVec = vec::Vectorized<scalar_t>;
125
+ using fVec = vec::Vectorized<float>;
126
+ if (size < bVec::size()) {
127
+ bVec data_bvec = bVec::loadu(data, size);
128
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
129
+ if (size > fVec::size()) {
130
+ data_fvec0 = fVec::set(
131
+ data_fvec0, vec_fun(data_fvec0, data_fvec1), size - fVec::size());
132
+ return vec_reduce_all<float>(vec_fun, data_fvec0, fVec::size());
133
+ } else {
134
+ return vec_reduce_all<float>(vec_fun, data_fvec0, size);
135
+ }
136
+ }
137
+ int64_t d = bVec::size();
138
+ bVec acc_bvec = bVec::loadu(data);
139
+ auto [acc_fvec0, acc_fvec1] = convert_to_float<scalar_t>(acc_bvec);
140
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
141
+ bVec data_bvec = bVec::loadu(data + d);
142
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
143
+ acc_fvec0 = vec_fun(acc_fvec0, data_fvec0);
144
+ acc_fvec1 = vec_fun(acc_fvec1, data_fvec1);
145
+ }
146
+ if (size - d > 0) {
147
+ bVec data_bvec = bVec::loadu(data + d, size - d);
148
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
149
+ if (size - d > fVec::size()) {
150
+ acc_fvec0 = vec_fun(acc_fvec0, data_fvec0);
151
+ acc_fvec1 = fVec::set(
152
+ acc_fvec1, vec_fun(acc_fvec1, data_fvec1), size - d - fVec::size());
153
+ } else {
154
+ acc_fvec0 =
155
+ fVec::set(acc_fvec0, vec_fun(acc_fvec0, data_fvec0), size - d);
156
+ }
157
+ }
158
+ acc_fvec0 = vec_fun(acc_fvec0, acc_fvec1);
159
+ return vec_reduce_all<float>(vec_fun, acc_fvec0);
160
+ }
161
+
162
+ template <
163
+ typename scalar_t,
164
+ typename Op1,
165
+ typename Op2,
166
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
167
+ inline std::pair<float, float> reduce2_all(
168
+ const Op1& vec_fun1,
169
+ const Op2& vec_fun2,
170
+ const scalar_t* data,
171
+ int64_t size) {
172
+ using bVec = vec::Vectorized<scalar_t>;
173
+ using fVec = vec::Vectorized<float>;
174
+ if (size < bVec::size()) {
175
+ bVec data_bvec = bVec::loadu(data, size);
176
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
177
+ if (size > fVec::size()) {
178
+ fVec acc1_fvec = fVec::set(
179
+ data_fvec0, vec_fun1(data_fvec0, data_fvec1), size - fVec::size());
180
+ fVec acc2_fvec = fVec::set(
181
+ data_fvec0, vec_fun2(data_fvec0, data_fvec1), size - fVec::size());
182
+ return std::pair<scalar_t, scalar_t>(
183
+ vec_reduce_all<float>(vec_fun1, acc1_fvec, fVec::size()),
184
+ vec_reduce_all<float>(vec_fun2, acc2_fvec, fVec::size()));
185
+ } else {
186
+ return std::pair<scalar_t, scalar_t>(
187
+ vec_reduce_all<float>(vec_fun1, data_fvec0, size),
188
+ vec_reduce_all<float>(vec_fun2, data_fvec0, size));
189
+ }
190
+ }
191
+ int64_t d = bVec::size();
192
+ bVec acc_bvec = bVec::loadu(data);
193
+ auto [acc1_fvec0, acc1_fvec1] = convert_to_float<scalar_t>(acc_bvec);
194
+ auto [acc2_fvec0, acc2_fvec1] = convert_to_float<scalar_t>(acc_bvec);
195
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
196
+ bVec data_bvec = bVec::loadu(data + d);
197
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
198
+ acc1_fvec0 = vec_fun1(acc1_fvec0, data_fvec0);
199
+ acc1_fvec1 = vec_fun1(acc1_fvec1, data_fvec1);
200
+ acc2_fvec0 = vec_fun2(acc2_fvec0, data_fvec0);
201
+ acc2_fvec1 = vec_fun2(acc2_fvec1, data_fvec1);
202
+ }
203
+ if (size - d > 0) {
204
+ bVec data_bvec = bVec::loadu(data + d, size - d);
205
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
206
+ if (size - d > fVec::size()) {
207
+ acc1_fvec0 = vec_fun1(acc1_fvec0, data_fvec0);
208
+ acc1_fvec1 = fVec::set(
209
+ acc1_fvec1,
210
+ vec_fun1(acc1_fvec1, data_fvec1),
211
+ size - d - fVec::size());
212
+ acc2_fvec0 = vec_fun2(acc2_fvec0, data_fvec0);
213
+ acc2_fvec1 = fVec::set(
214
+ acc2_fvec1,
215
+ vec_fun2(acc2_fvec1, data_fvec1),
216
+ size - d - fVec::size());
217
+ } else {
218
+ acc1_fvec0 =
219
+ fVec::set(acc1_fvec0, vec_fun1(acc1_fvec0, data_fvec0), size - d);
220
+ acc2_fvec0 =
221
+ fVec::set(acc2_fvec0, vec_fun2(acc2_fvec0, data_fvec0), size - d);
222
+ }
223
+ }
224
+ acc1_fvec0 = vec_fun1(acc1_fvec0, acc1_fvec1);
225
+ acc2_fvec0 = vec_fun2(acc2_fvec0, acc2_fvec1);
226
+ return std::pair<scalar_t, scalar_t>(
227
+ vec_reduce_all<float>(vec_fun1, acc1_fvec0),
228
+ vec_reduce_all<float>(vec_fun2, acc2_fvec0));
229
+ }
230
+
231
+ template <
232
+ typename scalar_t,
233
+ typename MapOp,
234
+ typename ReduceOp,
235
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
236
+ inline float map_reduce_all(
237
+ const MapOp& map_fun,
238
+ const ReduceOp& red_fun,
239
+ const scalar_t* data,
240
+ int64_t size) {
241
+ using bVec = vec::Vectorized<scalar_t>;
242
+ using fVec = vec::Vectorized<float>;
243
+ if (size < bVec::size()) {
244
+ bVec data_bvec = bVec::loadu(data, size);
245
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
246
+ if (size > fVec::size()) {
247
+ data_fvec0 = map_fun(data_fvec0);
248
+ data_fvec1 = map_fun(data_fvec1);
249
+ data_fvec0 = fVec::set(
250
+ data_fvec0, red_fun(data_fvec0, data_fvec1), size - fVec::size());
251
+ return vec_reduce_all<float>(red_fun, data_fvec0, fVec::size());
252
+ } else {
253
+ data_fvec0 = map_fun(data_fvec0);
254
+ return vec_reduce_all<float>(red_fun, data_fvec0, size);
255
+ }
256
+ }
257
+ int64_t d = bVec::size();
258
+ bVec acc_bvec = bVec::loadu(data);
259
+ auto [acc_fvec0, acc_fvec1] = convert_to_float<scalar_t>(acc_bvec);
260
+ acc_fvec0 = map_fun(acc_fvec0);
261
+ acc_fvec1 = map_fun(acc_fvec1);
262
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
263
+ bVec data_bvec = bVec::loadu(data + d);
264
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
265
+ data_fvec0 = map_fun(data_fvec0);
266
+ data_fvec1 = map_fun(data_fvec1);
267
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
268
+ acc_fvec1 = red_fun(acc_fvec1, data_fvec1);
269
+ }
270
+ if (size - d > 0) {
271
+ bVec data_bvec = bVec::loadu(data + d, size - d);
272
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
273
+ if (size - d > fVec::size()) {
274
+ data_fvec0 = map_fun(data_fvec0);
275
+ data_fvec1 = map_fun(data_fvec1);
276
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
277
+ acc_fvec1 = fVec::set(
278
+ acc_fvec1, red_fun(acc_fvec1, data_fvec1), size - d - fVec::size());
279
+ } else {
280
+ data_fvec0 = map_fun(data_fvec0);
281
+ acc_fvec0 =
282
+ fVec::set(acc_fvec0, red_fun(acc_fvec0, data_fvec0), size - d);
283
+ }
284
+ }
285
+ acc_fvec0 = red_fun(acc_fvec0, acc_fvec1);
286
+ return vec_reduce_all<float>(red_fun, acc_fvec0);
287
+ }
288
+
289
+ template <
290
+ typename scalar_t,
291
+ typename MapOp,
292
+ typename ReduceOp,
293
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
294
+ inline float map2_reduce_all(
295
+ const MapOp& map_fun,
296
+ const ReduceOp& red_fun,
297
+ const scalar_t* data,
298
+ const scalar_t* data2,
299
+ int64_t size) {
300
+ using bVec = vec::Vectorized<scalar_t>;
301
+ using fVec = vec::Vectorized<float>;
302
+ if (size < bVec::size()) {
303
+ bVec data_bvec = bVec::loadu(data, size);
304
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
305
+ bVec data2_bvec = bVec::loadu(data2, size);
306
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
307
+ if (size > fVec::size()) {
308
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0);
309
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1);
310
+ data_fvec0 = fVec::set(
311
+ data_fvec0, red_fun(data_fvec0, data_fvec1), size - fVec::size());
312
+ return vec_reduce_all<float>(red_fun, data_fvec0, fVec::size());
313
+ } else {
314
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0);
315
+ return vec_reduce_all<float>(red_fun, data_fvec0, size);
316
+ }
317
+ }
318
+ int64_t d = bVec::size();
319
+ bVec acc_bvec = bVec::loadu(data);
320
+ auto [acc_fvec0, acc_fvec1] = convert_to_float<scalar_t>(acc_bvec);
321
+ bVec acc2_bvec = bVec::loadu(data2);
322
+ auto [acc2_fvec0, acc2_fvec1] = convert_to_float<scalar_t>(acc2_bvec);
323
+ acc_fvec0 = map_fun(acc_fvec0, acc2_fvec0);
324
+ acc_fvec1 = map_fun(acc_fvec1, acc2_fvec1);
325
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
326
+ bVec data_bvec = bVec::loadu(data + d);
327
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
328
+ bVec data2_bvec = bVec::loadu(data2 + d);
329
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
330
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0);
331
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1);
332
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
333
+ acc_fvec1 = red_fun(acc_fvec1, data_fvec1);
334
+ }
335
+ if (size - d > 0) {
336
+ bVec data_bvec = bVec::loadu(data + d, size - d);
337
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
338
+ bVec data2_bvec = bVec::loadu(data2 + d, size - d);
339
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
340
+ if (size - d > fVec::size()) {
341
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0);
342
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1);
343
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
344
+ acc_fvec1 = fVec::set(
345
+ acc_fvec1, red_fun(acc_fvec1, data_fvec1), size - d - fVec::size());
346
+ } else {
347
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0);
348
+ acc_fvec0 =
349
+ fVec::set(acc_fvec0, red_fun(acc_fvec0, data_fvec0), size - d);
350
+ }
351
+ }
352
+ acc_fvec0 = red_fun(acc_fvec0, acc_fvec1);
353
+ return vec_reduce_all<float>(red_fun, acc_fvec0);
354
+ }
355
+
356
+ template <
357
+ typename scalar_t,
358
+ typename MapOp,
359
+ typename ReduceOp,
360
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
361
+ inline float map3_reduce_all(
362
+ const MapOp& map_fun,
363
+ const ReduceOp& red_fun,
364
+ const scalar_t* data,
365
+ const scalar_t* data2,
366
+ const scalar_t* data3,
367
+ int64_t size) {
368
+ using bVec = vec::Vectorized<scalar_t>;
369
+ using fVec = vec::Vectorized<float>;
370
+ if (size < bVec::size()) {
371
+ bVec data_bvec = bVec::loadu(data, size);
372
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
373
+ bVec data2_bvec = bVec::loadu(data2, size);
374
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
375
+ bVec data3_bvec = bVec::loadu(data3, size);
376
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
377
+ if (size > fVec::size()) {
378
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0, data3_fvec0);
379
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1, data3_fvec1);
380
+ data_fvec0 = fVec::set(
381
+ data_fvec0, red_fun(data_fvec0, data_fvec1), size - fVec::size());
382
+ return vec_reduce_all<float>(red_fun, data_fvec0, fVec::size());
383
+ } else {
384
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0, data3_fvec0);
385
+ return vec_reduce_all<float>(red_fun, data_fvec0, size);
386
+ }
387
+ }
388
+ int64_t d = bVec::size();
389
+ bVec acc_bvec = bVec::loadu(data);
390
+ auto [acc_fvec0, acc_fvec1] = convert_to_float<scalar_t>(acc_bvec);
391
+ bVec acc2_bvec = bVec::loadu(data2);
392
+ auto [acc2_fvec0, acc2_fvec1] = convert_to_float<scalar_t>(acc2_bvec);
393
+ bVec acc3_bvec = bVec::loadu(data3);
394
+ auto [acc3_fvec0, acc3_fvec1] = convert_to_float<scalar_t>(acc3_bvec);
395
+ acc_fvec0 = map_fun(acc_fvec0, acc2_fvec0, acc3_fvec0);
396
+ acc_fvec1 = map_fun(acc_fvec1, acc2_fvec1, acc3_fvec1);
397
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
398
+ bVec data_bvec = bVec::loadu(data + d);
399
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
400
+ bVec data2_bvec = bVec::loadu(data2 + d);
401
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
402
+ bVec data3_bvec = bVec::loadu(data3 + d);
403
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
404
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0, data3_fvec0);
405
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1, data3_fvec1);
406
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
407
+ acc_fvec1 = red_fun(acc_fvec1, data_fvec1);
408
+ }
409
+ if (size - d > 0) {
410
+ bVec data_bvec = bVec::loadu(data + d, size - d);
411
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
412
+ bVec data2_bvec = bVec::loadu(data2 + d, size - d);
413
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
414
+ bVec data3_bvec = bVec::loadu(data3 + d, size - d);
415
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
416
+ if (size - d > fVec::size()) {
417
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0, data3_fvec0);
418
+ data_fvec1 = map_fun(data_fvec1, data2_fvec1, data3_fvec1);
419
+ acc_fvec0 = red_fun(acc_fvec0, data_fvec0);
420
+ acc_fvec1 = fVec::set(
421
+ acc_fvec1, red_fun(acc_fvec1, data_fvec1), size - d - fVec::size());
422
+ } else {
423
+ data_fvec0 = map_fun(data_fvec0, data2_fvec0, data3_fvec0);
424
+ acc_fvec0 =
425
+ fVec::set(acc_fvec0, red_fun(acc_fvec0, data_fvec0), size - d);
426
+ }
427
+ }
428
+ acc_fvec0 = red_fun(acc_fvec0, acc_fvec1);
429
+ return vec_reduce_all<float>(red_fun, acc_fvec0);
430
+ }
431
+
432
+ template <
433
+ typename scalar_t,
434
+ typename Op,
435
+ typename std::enable_if_t<
436
+ !(!detail::should_prefer_converting_through_float_v<scalar_t> &&
437
+ std::is_invocable_v<Op, vec::Vectorized<scalar_t>>),
438
+ int> = 0>
439
+ inline void map(
440
+ const Op& vec_fun,
441
+ scalar_t* output_data,
442
+ const scalar_t* input_data,
443
+ int64_t size) {
444
+ using bVec = vec::Vectorized<scalar_t>;
445
+ using fVec = vec::Vectorized<float>;
446
+ int64_t d = 0;
447
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
448
+ bVec data_bvec = bVec::loadu(input_data + d);
449
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
450
+ fVec output_fvec0 = vec_fun(data_fvec0);
451
+ fVec output_fvec1 = vec_fun(data_fvec1);
452
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
453
+ output_bvec.store(output_data + d);
454
+ }
455
+ if (size - d > 0) {
456
+ bVec data_bvec = bVec::loadu(input_data + d, size - d);
457
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
458
+ fVec output_fvec0 = vec_fun(data_fvec0);
459
+ fVec output_fvec1 = vec_fun(data_fvec1);
460
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
461
+ output_bvec.store(output_data + d, size - d);
462
+ }
463
+ }
464
+
465
+ template <
466
+ typename scalar_t,
467
+ typename Op,
468
+ typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
469
+ inline void map(
470
+ const Op& vec_fun,
471
+ scalar_t* output_data,
472
+ const float* input_data,
473
+ int64_t size) {
474
+ using bVec = vec::Vectorized<scalar_t>;
475
+ using fVec = vec::Vectorized<float>;
476
+ int64_t d = 0;
477
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
478
+ fVec data_fvec0 = fVec::loadu(input_data + d);
479
+ fVec data_fvec1 = fVec::loadu(input_data + d + fVec::size());
480
+ fVec output_fvec0 = vec_fun(data_fvec0);
481
+ fVec output_fvec1 = vec_fun(data_fvec1);
482
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
483
+ output_bvec.store(output_data + d);
484
+ }
485
+ if (size - d > 0) {
486
+ fVec data_fvec0, data_fvec1;
487
+ if (size - d > fVec::size()) {
488
+ data_fvec0 = fVec::loadu(input_data + d);
489
+ data_fvec1 =
490
+ fVec::loadu(input_data + d + fVec::size(), size - d - fVec::size());
491
+ } else {
492
+ // choose to align with behaviour of bVec::loadu(ptr, size),
493
+ // which leaves data_fvec1 uninitialized
494
+ data_fvec0 = fVec::loadu(input_data + d, size - d);
495
+ }
496
+ fVec output_fvec0 = vec_fun(data_fvec0);
497
+ fVec output_fvec1 = vec_fun(data_fvec1);
498
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
499
+ output_bvec.store(output_data + d, size - d);
500
+ }
501
+ }
502
+
503
+ template <
504
+ typename scalar_t,
505
+ typename Op,
506
+ typename std::enable_if_t<
507
+ !(!detail::should_prefer_converting_through_float_v<scalar_t> &&
508
+ std::is_invocable_v<
509
+ Op,
510
+ vec::Vectorized<scalar_t>,
511
+ vec::Vectorized<scalar_t>>),
512
+ int> = 0>
513
+ inline void map2(
514
+ const Op& vec_fun,
515
+ scalar_t* output_data,
516
+ const scalar_t* input_data,
517
+ const scalar_t* input_data2,
518
+ int64_t size) {
519
+ using bVec = vec::Vectorized<scalar_t>;
520
+ using fVec = vec::Vectorized<float>;
521
+ int64_t d = 0;
522
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
523
+ bVec data_bvec = bVec::loadu(input_data + d);
524
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
525
+ bVec data2_bvec = bVec::loadu(input_data2 + d);
526
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
527
+ fVec output_fvec0 = vec_fun(data_fvec0, data2_fvec0);
528
+ fVec output_fvec1 = vec_fun(data_fvec1, data2_fvec1);
529
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
530
+ output_bvec.store(output_data + d);
531
+ }
532
+ if (size - d > 0) {
533
+ bVec data_bvec = bVec::loadu(input_data + d, size - d);
534
+ auto [data_fvec0, data_fvec1] = convert_to_float<scalar_t>(data_bvec);
535
+ bVec data2_bvec = bVec::loadu(input_data2 + d, size - d);
536
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
537
+ fVec output_fvec0 = vec_fun(data_fvec0, data2_fvec0);
538
+ fVec output_fvec1 = vec_fun(data_fvec1, data2_fvec1);
539
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
540
+ output_bvec.store(output_data + d, size - d);
541
+ }
542
+ }
543
+
544
+ template <
545
+ typename scalar_t,
546
+ typename Op,
547
+ typename std::enable_if_t<
548
+ !(!detail::should_prefer_converting_through_float_v<scalar_t> &&
549
+ std::is_invocable_v<
550
+ Op,
551
+ vec::Vectorized<scalar_t>,
552
+ vec::Vectorized<scalar_t>,
553
+ vec::Vectorized<scalar_t>>),
554
+ int> = 0>
555
+ inline void map3(
556
+ const Op& vec_fun,
557
+ scalar_t* output_data,
558
+ const scalar_t* input_data1,
559
+ const scalar_t* input_data2,
560
+ const scalar_t* input_data3,
561
+ int64_t size) {
562
+ using bVec = vec::Vectorized<scalar_t>;
563
+ using fVec = vec::Vectorized<float>;
564
+ int64_t d = 0;
565
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
566
+ bVec data1_bvec = bVec::loadu(input_data1 + d);
567
+ auto [data1_fvec0, data1_fvec1] = convert_to_float<scalar_t>(data1_bvec);
568
+ bVec data2_bvec = bVec::loadu(input_data2 + d);
569
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
570
+ bVec data3_bvec = bVec::loadu(input_data3 + d);
571
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
572
+ fVec output_fvec0 = vec_fun(data1_fvec0, data2_fvec0, data3_fvec0);
573
+ fVec output_fvec1 = vec_fun(data1_fvec1, data2_fvec1, data3_fvec1);
574
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
575
+ output_bvec.store(output_data + d);
576
+ }
577
+ if (size - d > 0) {
578
+ bVec data1_bvec = bVec::loadu(input_data1 + d, size - d);
579
+ auto [data1_fvec0, data1_fvec1] = convert_to_float<scalar_t>(data1_bvec);
580
+ bVec data2_bvec = bVec::loadu(input_data2 + d, size - d);
581
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
582
+ bVec data3_bvec = bVec::loadu(input_data3 + d, size - d);
583
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
584
+ fVec output_fvec0 = vec_fun(data1_fvec0, data2_fvec0, data3_fvec0);
585
+ fVec output_fvec1 = vec_fun(data1_fvec1, data2_fvec1, data3_fvec1);
586
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
587
+ output_bvec.store(output_data + d, size - d);
588
+ }
589
+ }
590
+
591
+ template <
592
+ typename scalar_t,
593
+ typename Op,
594
+ typename std::enable_if_t<
595
+ !(!detail::should_prefer_converting_through_float_v<scalar_t> &&
596
+ std::is_invocable_v<
597
+ Op,
598
+ vec::Vectorized<scalar_t>,
599
+ vec::Vectorized<scalar_t>,
600
+ vec::Vectorized<scalar_t>,
601
+ vec::Vectorized<scalar_t>>),
602
+ int> = 0>
603
+ inline void map4(
604
+ const Op& vec_fun,
605
+ scalar_t* output_data,
606
+ const scalar_t* input_data1,
607
+ const scalar_t* input_data2,
608
+ const scalar_t* input_data3,
609
+ const scalar_t* input_data4,
610
+ int64_t size) {
611
+ using bVec = vec::Vectorized<scalar_t>;
612
+ using fVec = vec::Vectorized<float>;
613
+ int64_t d = 0;
614
+ for (; d < size - (size % bVec::size()); d += bVec::size()) {
615
+ bVec data1_bvec = bVec::loadu(input_data1 + d);
616
+ auto [data1_fvec0, data1_fvec1] = convert_to_float<scalar_t>(data1_bvec);
617
+ bVec data2_bvec = bVec::loadu(input_data2 + d);
618
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
619
+ bVec data3_bvec = bVec::loadu(input_data3 + d);
620
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
621
+ bVec data4_bvec = bVec::loadu(input_data4 + d);
622
+ auto [data4_fvec0, data4_fvec1] = convert_to_float<scalar_t>(data4_bvec);
623
+ fVec output_fvec0 =
624
+ vec_fun(data1_fvec0, data2_fvec0, data3_fvec0, data4_fvec0);
625
+ fVec output_fvec1 =
626
+ vec_fun(data1_fvec1, data2_fvec1, data3_fvec1, data4_fvec1);
627
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
628
+ output_bvec.store(output_data + d);
629
+ }
630
+ if (size - d > 0) {
631
+ bVec data1_bvec = bVec::loadu(input_data1 + d, size - d);
632
+ auto [data1_fvec0, data1_fvec1] = convert_to_float<scalar_t>(data1_bvec);
633
+ bVec data2_bvec = bVec::loadu(input_data2 + d, size - d);
634
+ auto [data2_fvec0, data2_fvec1] = convert_to_float<scalar_t>(data2_bvec);
635
+ bVec data3_bvec = bVec::loadu(input_data3 + d, size - d);
636
+ auto [data3_fvec0, data3_fvec1] = convert_to_float<scalar_t>(data3_bvec);
637
+ bVec data4_bvec = bVec::loadu(input_data4 + d, size - d);
638
+ auto [data4_fvec0, data4_fvec1] = convert_to_float<scalar_t>(data4_bvec);
639
+ fVec output_fvec0 =
640
+ vec_fun(data1_fvec0, data2_fvec0, data3_fvec0, data4_fvec0);
641
+ fVec output_fvec1 =
642
+ vec_fun(data1_fvec1, data2_fvec1, data3_fvec1, data4_fvec1);
643
+ bVec output_bvec = convert_from_float<scalar_t>(output_fvec0, output_fvec1);
644
+ output_bvec.store(output_data + d, size - d);
645
+ }
646
+ }
647
+
648
+ } // namespace at::vec
649
+
650
+ #else
651
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
652
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/intrinsics.h ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #include <torch/headeronly/cpu/vec/intrinsics.h>
3
+
4
+ #else
5
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
6
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/sve_helper.h ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+
6
+ #include <ATen/cpu/vec/vec_base.h>
7
+
8
+ #if defined(CPU_CAPABILITY_SVE)
9
+
10
+ // Define the data type of VLS(vector-length specific).
11
+ typedef svbool_t vls_pred_t
12
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
13
+ typedef svint8_t vls_int8_t
14
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
15
+ typedef svint16_t vls_int16_t
16
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
17
+ typedef svint32_t vls_int32_t
18
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
19
+ typedef svint64_t vls_int64_t
20
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
21
+ typedef svuint8_t vls_uint8_t
22
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
23
+ typedef svuint16_t vls_uint16_t
24
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
25
+ typedef svuint32_t vls_uint32_t
26
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
27
+ typedef svuint64_t vls_uint64_t
28
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
29
+ typedef svfloat16_t vls_float16_t
30
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
31
+ typedef svbfloat16_t vls_bfloat16_t
32
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
33
+ typedef svfloat32_t vls_float32_t
34
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
35
+ typedef svfloat64_t vls_float64_t
36
+ __attribute__((arm_sve_vector_bits(VECTOR_WIDTH * 8)));
37
+
38
+ #define ptrue svptrue_b8()
39
+ #define ZERO_S8 svdup_n_s8(0)
40
+ #define ZERO_S16 svdup_n_s16(0)
41
+ #define ZERO_S32 svdup_n_s32(0)
42
+ #define ZERO_S64 svdup_n_s64(0)
43
+ #define ZERO_U8 svdup_n_u8(0)
44
+ #define ZERO_U16 svdup_n_u16(0)
45
+ #define ZERO_U32 svdup_n_u32(0)
46
+ #define ZERO_U64 svdup_n_u64(0)
47
+ #define ZERO_F16 svdup_n_f16(0.f)
48
+ #define ZERO_F32 svdup_n_f32(0.f)
49
+ #define ZERO_F64 svdup_n_f64(0.0)
50
+ #define ONE_S8 svdup_n_s8(1)
51
+ #define ONE_S16 svdup_n_s16(1)
52
+ #define ONE_S32 svdup_n_s32(1)
53
+ #define ONE_S64 svdup_n_s64(1)
54
+ #define ONE_U8 svdup_n_u8(1)
55
+ #define ONE_U16 svdup_n_u16(1)
56
+ #define ONE_U32 svdup_n_u32(1)
57
+ #define ONE_U64 svdup_n_u64(1)
58
+ #define ONE_F16 svdup_n_f16(1.f)
59
+ #define ONE_BF16 svdup_n_bf16(1.f)
60
+ #define ONE_F32 svdup_n_f32(1.f)
61
+ #define ONE_F64 svdup_n_f64(1.0)
62
+ #define ALL_S8_TRUE_MASK svdup_n_s8(0xff)
63
+ #define ALL_S8_FALSE_MASK svdup_n_s8(0x0)
64
+ #define ALL_S16_TRUE_MASK svdup_n_s16(0xffff)
65
+ #define ALL_S16_FALSE_MASK svdup_n_s16(0x0)
66
+ #define ALL_S32_TRUE_MASK svdup_n_s32(0xffffffff)
67
+ #define ALL_S32_FALSE_MASK svdup_n_s32(0x0)
68
+ #define ALL_S64_TRUE_MASK svdup_n_s64(0xffffffffffffffff)
69
+ #define ALL_S64_FALSE_MASK svdup_n_s64(0x0)
70
+ #define ALL_U8_TRUE_MASK svdup_n_u8(0x01)
71
+ #define ALL_U8_FALSE_MASK svdup_n_u8(0x00)
72
+ #define ALL_F16_TRUE_MASK svreinterpret_f16_s16(ALL_S16_TRUE_MASK)
73
+ #define ALL_F16_FALSE_MASK svreinterpret_f16_s16(ALL_S16_FALSE_MASK)
74
+ #define ALL_BF16_TRUE_MASK svreinterpret_bf16_s16(ALL_S16_TRUE_MASK)
75
+ #define ALL_BF16_FALSE_MASK svreinterpret_bf16_s16(ALL_S16_FALSE_MASK)
76
+ #define ALL_F32_TRUE_MASK svreinterpret_f32_s32(ALL_S32_TRUE_MASK)
77
+ #define ALL_F32_FALSE_MASK svreinterpret_f32_s32(ALL_S32_FALSE_MASK)
78
+ #define ALL_F64_TRUE_MASK svreinterpret_f64_s64(ALL_S64_TRUE_MASK)
79
+ #define ALL_F64_FALSE_MASK svreinterpret_f64_s64(ALL_S64_FALSE_MASK)
80
+
81
+ #endif // defined(CPU_CAPABILITY_SVE)
82
+
83
+ #else
84
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
85
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_bfloat16.h ADDED
@@ -0,0 +1,598 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/sve/sve_helper.h>
6
+ #include <ATen/cpu/vec/sve/vec_common_sve.h>
7
+ #include <ATen/cpu/vec/sve/vec_float.h>
8
+ #include <ATen/cpu/vec/vec_base.h>
9
+ #include <c10/util/bit_cast.h>
10
+ #include <cmath>
11
+ namespace at {
12
+ namespace vec {
13
+ // Note [CPU_CAPABILITY namespace]
14
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
+ // This header, and all of its subheaders, will be compiled with
16
+ // different architecture flags for each supported set of vector
17
+ // intrinsics. So we need to make sure they aren't inadvertently
18
+ // linked together. We do this by declaring objects in an `inline
19
+ // namespace` which changes the name mangling, but can still be
20
+ // accessed as `at::vec`.
21
+ inline namespace CPU_CAPABILITY {
22
+
23
+ #if defined(CPU_CAPABILITY_SVE256) && defined(__ARM_FEATURE_BF16)
24
+
25
+ template <>
26
+ struct is_vec_specialized_for<BFloat16> : std::bool_constant<true> {};
27
+
28
+ template <>
29
+ class Vectorized<BFloat16> {
30
+ private:
31
+ vls_bfloat16_t values;
32
+
33
+ public:
34
+ using value_type = BFloat16;
35
+ using size_type = int;
36
+
37
+ static constexpr size_type size() {
38
+ return VECTOR_WIDTH / sizeof(BFloat16);
39
+ }
40
+
41
+ Vectorized();
42
+ Vectorized(svbfloat16_t v) : values(v) {}
43
+ Vectorized(int val);
44
+ Vectorized(BFloat16 val);
45
+
46
+ template <
47
+ typename... Args,
48
+ typename = std::enable_if_t<(sizeof...(Args) == size())>>
49
+ Vectorized(Args... vals) {
50
+ __at_align__ BFloat16 buffer[size()] = {vals...};
51
+ values = svld1_bf16(ptrue, reinterpret_cast<const bfloat16_t*>(buffer));
52
+ }
53
+
54
+ operator svbfloat16_t() const {
55
+ return values;
56
+ }
57
+ static Vectorized<BFloat16> blendv(
58
+ const Vectorized<BFloat16>& a,
59
+ const Vectorized<BFloat16>& b,
60
+ const Vectorized<BFloat16>& mask_) {
61
+ svbool_t mask =
62
+ svcmpeq_s16(ptrue, svreinterpret_s16_bf16(mask_), ALL_S16_TRUE_MASK);
63
+ return svsel_bf16(mask, b, a);
64
+ }
65
+ template <typename step_t>
66
+ static Vectorized<BFloat16> arange(
67
+ BFloat16 base = 0.f,
68
+ step_t step = static_cast<step_t>(1)) {
69
+ __at_align__ BFloat16 buffer[size()];
70
+ for (int64_t i = 0; i < size(); i++) {
71
+ buffer[i] = base + i * step;
72
+ }
73
+ return svld1_bf16(ptrue, reinterpret_cast<bfloat16_t*>(buffer));
74
+ }
75
+ static Vectorized<BFloat16> set(
76
+ const Vectorized<BFloat16>& a,
77
+ const Vectorized<BFloat16>& b,
78
+ int64_t count = size()) {
79
+ if (count == 0) {
80
+ return a;
81
+ } else if (count < size()) {
82
+ return svsel_bf16(svwhilelt_b16(0ull, count), b, a);
83
+ }
84
+ return b;
85
+ }
86
+ static Vectorized<BFloat16> loadu(const void* ptr, int64_t count = size()) {
87
+ if (count == size())
88
+ return svld1_bf16(ptrue, reinterpret_cast<const bfloat16_t*>(ptr));
89
+ svbool_t pg = svwhilelt_b16(0ull, count);
90
+ return svld1_bf16(pg, reinterpret_cast<const bfloat16_t*>(ptr));
91
+ }
92
+ void store(void* ptr, int64_t count = size()) const {
93
+ __at_align__ bfloat16_t tmp[size()];
94
+ std::memset(tmp, 0, sizeof(tmp));
95
+ if (count == size()) {
96
+ svst1_bf16(ptrue, reinterpret_cast<bfloat16_t*>(tmp), values);
97
+ } else {
98
+ svbool_t pg = svwhilelt_b16(0ull, count);
99
+ svst1_bf16(pg, reinterpret_cast<bfloat16_t*>(tmp), values);
100
+ }
101
+ std::memcpy(
102
+ reinterpret_cast<bfloat16_t*>(ptr),
103
+ reinterpret_cast<const bfloat16_t*>(tmp),
104
+ count * sizeof(bfloat16_t));
105
+ }
106
+ const BFloat16& operator[](int idx) const = delete;
107
+ BFloat16& operator[](int idx) = delete;
108
+ int64_t zero_mask() const {
109
+ int64_t mask = 0;
110
+ // returns an integer mask where all zero elements are translated to
111
+ // 1-bit and others are translated to 0-bit int64_t mask = 0;
112
+ __at_align__ int16_t mask_array[size()];
113
+
114
+ svbool_t svbool_mask =
115
+ svcmpeq_f16(ptrue, svreinterpret_f16_bf16(values), ZERO_F16);
116
+ svst1_s16(
117
+ ptrue,
118
+ mask_array,
119
+ svsel_s16(svbool_mask, ALL_S16_TRUE_MASK, ALL_S16_FALSE_MASK));
120
+ for (int64_t i = 0; i < size(); ++i) {
121
+ if (mask_array[i])
122
+ mask |= (1ull << i);
123
+ }
124
+ return mask;
125
+ }
126
+ Vectorized<BFloat16> isnan() const;
127
+ bool has_inf_nan() const;
128
+ Vectorized<BFloat16> map(BFloat16 (*f)(BFloat16)) const {
129
+ __at_align__ BFloat16 tmp[size()];
130
+ store(tmp);
131
+ for (int64_t i = 0; i < size(); ++i) {
132
+ tmp[i] = f(tmp[i]);
133
+ }
134
+ return loadu(tmp);
135
+ }
136
+ Vectorized<BFloat16> abs() const {
137
+ auto mask = svdup_n_u16(0x7FFF);
138
+ auto vals = svreinterpret_u16_bf16(values);
139
+ vals = svand_u16_x(ptrue, vals, mask);
140
+ return svreinterpret_bf16_u16(vals);
141
+ }
142
+ Vectorized<BFloat16> angle() const;
143
+ Vectorized<BFloat16> real() const {
144
+ return values;
145
+ }
146
+ Vectorized<BFloat16> imag() const {
147
+ return Vectorized<BFloat16>(0.f);
148
+ }
149
+ Vectorized<BFloat16> conj() const {
150
+ return values;
151
+ }
152
+ Vectorized<BFloat16> acos() const;
153
+ Vectorized<BFloat16> acosh() const;
154
+ Vectorized<BFloat16> asin() const;
155
+ Vectorized<BFloat16> atan() const;
156
+ Vectorized<BFloat16> atanh() const;
157
+ Vectorized<BFloat16> atan2(const Vectorized<BFloat16>& b) const;
158
+ Vectorized<BFloat16> copysign(const Vectorized<BFloat16>& sign) const;
159
+ Vectorized<BFloat16> erf() const;
160
+ Vectorized<BFloat16> erfc() const;
161
+ Vectorized<BFloat16> erfinv() const;
162
+ Vectorized<BFloat16> exp() const;
163
+ Vectorized<BFloat16> exp2() const;
164
+ Vectorized<BFloat16> expm1() const;
165
+ Vectorized<BFloat16> exp_u20() const {
166
+ return exp();
167
+ }
168
+ Vectorized<BFloat16> fexp_u20() const {
169
+ return exp();
170
+ }
171
+ Vectorized<BFloat16> fmod(const Vectorized<BFloat16>& q) const;
172
+ Vectorized<BFloat16> hypot(const Vectorized<BFloat16>& b) const;
173
+ Vectorized<BFloat16> i0() const;
174
+ Vectorized<BFloat16> i0e() const;
175
+ Vectorized<BFloat16> digamma() const;
176
+ Vectorized<BFloat16> igamma(const Vectorized<BFloat16>& x) const;
177
+ Vectorized<BFloat16> igammac(const Vectorized<BFloat16>& x) const;
178
+ Vectorized<BFloat16> nextafter(const Vectorized<BFloat16>& b) const;
179
+ Vectorized<BFloat16> log() const;
180
+ Vectorized<BFloat16> log2() const;
181
+ Vectorized<BFloat16> log10() const;
182
+ Vectorized<BFloat16> log1p() const;
183
+ Vectorized<BFloat16> frac() const;
184
+ Vectorized<BFloat16> sin() const;
185
+ Vectorized<BFloat16> sinh() const;
186
+ Vectorized<BFloat16> cos() const;
187
+ Vectorized<BFloat16> cosh() const;
188
+ Vectorized<BFloat16> ceil() const;
189
+ Vectorized<BFloat16> floor() const;
190
+ Vectorized<BFloat16> neg() const {
191
+ auto mask = svdup_n_u16(0x8000);
192
+ auto vals = svreinterpret_u16_bf16(values);
193
+ vals = sveor_u16_x(ptrue, vals, mask);
194
+ return svreinterpret_bf16_u16(vals);
195
+ }
196
+ Vectorized<BFloat16> round() const;
197
+ Vectorized<BFloat16> tan() const;
198
+ Vectorized<BFloat16> tanh() const;
199
+ Vectorized<BFloat16> trunc() const;
200
+ Vectorized<BFloat16> lgamma() const;
201
+ Vectorized<BFloat16> sqrt() const;
202
+ Vectorized<BFloat16> reciprocal() const;
203
+ Vectorized<BFloat16> rsqrt() const;
204
+ Vectorized<BFloat16> pow(const Vectorized<BFloat16>& b) const;
205
+ // Comparison using the _CMP_**_OQ predicate.
206
+ // `O`: get false if an operand is NaN
207
+ // `Q`: do not raise if an operand is NaN
208
+ Vectorized<BFloat16> operator==(const Vectorized<BFloat16>& other) const;
209
+
210
+ Vectorized<BFloat16> operator!=(const Vectorized<BFloat16>& other) const;
211
+
212
+ Vectorized<BFloat16> operator<(const Vectorized<BFloat16>& other) const;
213
+
214
+ Vectorized<BFloat16> operator<=(const Vectorized<BFloat16>& other) const;
215
+
216
+ Vectorized<BFloat16> operator>(const Vectorized<BFloat16>& other) const;
217
+
218
+ Vectorized<BFloat16> operator>=(const Vectorized<BFloat16>& other) const;
219
+
220
+ Vectorized<BFloat16> eq(const Vectorized<BFloat16>& other) const;
221
+ Vectorized<BFloat16> ne(const Vectorized<BFloat16>& other) const;
222
+ Vectorized<BFloat16> gt(const Vectorized<BFloat16>& other) const;
223
+ Vectorized<BFloat16> ge(const Vectorized<BFloat16>& other) const;
224
+ Vectorized<BFloat16> lt(const Vectorized<BFloat16>& other) const;
225
+ Vectorized<BFloat16> le(const Vectorized<BFloat16>& other) const;
226
+ };
227
+
228
+ #if defined(__GNUC__) && __GNUC__ == 14
229
+ // Workaround for gcc-14.2.0 ICE during RTL pass: vregs when compiling for SVE
230
+ __attribute__((optimize("no-tree-vectorize")))
231
+ #endif
232
+ inline std::tuple<Vectorized<float>, Vectorized<float>>
233
+ convert_bfloat16_float(const Vectorized<c10::BFloat16>& a) {
234
+ static_assert(
235
+ Vectorized<c10::BFloat16>::size() == 2 * Vectorized<float>::size());
236
+ auto zero = svreinterpret_bf16_f32(svdup_n_f32(0.0f));
237
+ auto bf16_vec1 = svzip1_bf16(zero, a);
238
+ auto bf16_vec2 = svzip2_bf16(zero, a);
239
+ auto x1 = svreinterpret_f32_bf16(bf16_vec1);
240
+ auto x2 = svreinterpret_f32_bf16(bf16_vec2);
241
+ return {Vectorized<float>(x1), Vectorized<float>(x2)};
242
+ }
243
+
244
+ inline Vectorized<c10::BFloat16> convert_float_bfloat16(
245
+ const Vectorized<float>& a,
246
+ const Vectorized<float>& b) {
247
+ static_assert(
248
+ Vectorized<c10::BFloat16>::size() == 2 * Vectorized<float>::size());
249
+ svbfloat16_t x1 = svcvt_bf16_f32_z(ptrue, a);
250
+ svbfloat16_t x2 = svcvt_bf16_f32_z(ptrue, b);
251
+ return Vectorized<c10::BFloat16>(svuzp1_bf16(x1, x2));
252
+ }
253
+
254
+ inline void load_fp32_from_bf16(const BFloat16* data, Vectorized<float>& out) {
255
+ __at_align__ float values[Vectorized<float>::size()];
256
+ for (const auto k : c10::irange(Vectorized<float>::size())) {
257
+ values[k] = data[k];
258
+ }
259
+ out = Vectorized<float>::loadu(values);
260
+ }
261
+
262
+ inline void load_fp32_from_bf16(
263
+ const BFloat16* data,
264
+ Vectorized<float>& out1,
265
+ Vectorized<float>& out2) {
266
+ Vectorized<BFloat16> bf16_vec = Vectorized<BFloat16>::loadu(data);
267
+ auto floats = convert_bfloat16_float(bf16_vec);
268
+ out1 = std::get<0>(floats);
269
+ out2 = std::get<1>(floats);
270
+ }
271
+
272
+ template <typename Op>
273
+ Vectorized<c10::BFloat16> binary_operator_via_float(
274
+ Op op,
275
+ const Vectorized<c10::BFloat16>& a,
276
+ const Vectorized<c10::BFloat16>& b) {
277
+ const auto [a_float_low, a_float_high] = convert_bfloat16_float(a);
278
+ const auto [b_float_low, b_float_high] = convert_bfloat16_float(b);
279
+ return convert_float_bfloat16(
280
+ op(a_float_low, b_float_low), op(a_float_high, b_float_high));
281
+ }
282
+
283
+ template <>
284
+ Vectorized<c10::BFloat16> inline operator+(
285
+ const Vectorized<c10::BFloat16>& a,
286
+ const Vectorized<c10::BFloat16>& b) {
287
+ return binary_operator_via_float(std::plus<Vectorized<float>>(), a, b);
288
+ }
289
+
290
+ template <>
291
+ Vectorized<c10::BFloat16> inline operator-(
292
+ const Vectorized<c10::BFloat16>& a,
293
+ const Vectorized<c10::BFloat16>& b) {
294
+ return binary_operator_via_float(std::minus<Vectorized<float>>(), a, b);
295
+ }
296
+
297
+ template <>
298
+ Vectorized<c10::BFloat16> inline operator*(
299
+ const Vectorized<c10::BFloat16>& a,
300
+ const Vectorized<c10::BFloat16>& b) {
301
+ return binary_operator_via_float(std::multiplies<Vectorized<float>>(), a, b);
302
+ }
303
+
304
+ template <>
305
+ Vectorized<c10::BFloat16> inline operator/(
306
+ const Vectorized<c10::BFloat16>& a,
307
+ const Vectorized<c10::BFloat16>& b) {
308
+ return binary_operator_via_float(std::divides<Vectorized<float>>(), a, b);
309
+ }
310
+
311
+ inline Vectorized<BFloat16>::Vectorized() {
312
+ auto vals_f = svdup_n_f32(0);
313
+ values = convert_float_bfloat16(vals_f, vals_f);
314
+ }
315
+
316
+ inline Vectorized<BFloat16>::Vectorized(int val) {
317
+ auto vals_f = svdup_n_f32(val);
318
+ values = convert_float_bfloat16(vals_f, vals_f);
319
+ }
320
+
321
+ inline Vectorized<BFloat16>::Vectorized(BFloat16 val) {
322
+ auto vals_f = svdup_n_f32((float)val);
323
+ values = convert_float_bfloat16(vals_f, vals_f);
324
+ }
325
+
326
+ bool inline Vectorized<c10::BFloat16>::has_inf_nan() const {
327
+ auto [v1, v2] = convert_bfloat16_float(values);
328
+ return v1.has_inf_nan() || v2.has_inf_nan();
329
+ }
330
+ // frac. Implement this here so we can use subtraction
331
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::frac() const {
332
+ return *this - this->trunc();
333
+ }
334
+
335
+ #define DEFINE_BF16_FUNC_VIA_FLOAT(func_name) \
336
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::func_name() const { \
337
+ auto [v1, v2] = convert_bfloat16_float(*this); \
338
+ v1 = v1.func_name(); \
339
+ v2 = v2.func_name(); \
340
+ return convert_float_bfloat16(v1, v2); \
341
+ }
342
+
343
+ #define DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(func_name) \
344
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::func_name( \
345
+ const Vectorized<BFloat16>& a) const { \
346
+ auto [v1, v2] = convert_bfloat16_float(*this); \
347
+ auto [v3, v4] = convert_bfloat16_float(a); \
348
+ v1 = v1.func_name(v3); \
349
+ v2 = v2.func_name(v4); \
350
+ return convert_float_bfloat16(v1, v2); \
351
+ }
352
+
353
+ DEFINE_BF16_FUNC_VIA_FLOAT(isnan)
354
+ DEFINE_BF16_FUNC_VIA_FLOAT(angle)
355
+ DEFINE_BF16_FUNC_VIA_FLOAT(acos)
356
+ DEFINE_BF16_FUNC_VIA_FLOAT(acosh)
357
+ DEFINE_BF16_FUNC_VIA_FLOAT(asin)
358
+ DEFINE_BF16_FUNC_VIA_FLOAT(atan)
359
+ DEFINE_BF16_FUNC_VIA_FLOAT(atanh)
360
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(atan2)
361
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(copysign)
362
+ DEFINE_BF16_FUNC_VIA_FLOAT(erf)
363
+ DEFINE_BF16_FUNC_VIA_FLOAT(erfc)
364
+ DEFINE_BF16_FUNC_VIA_FLOAT(exp)
365
+ DEFINE_BF16_FUNC_VIA_FLOAT(exp2)
366
+ DEFINE_BF16_FUNC_VIA_FLOAT(expm1)
367
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(fmod)
368
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(hypot)
369
+ DEFINE_BF16_FUNC_VIA_FLOAT(i0)
370
+ DEFINE_BF16_FUNC_VIA_FLOAT(i0e)
371
+ DEFINE_BF16_FUNC_VIA_FLOAT(digamma)
372
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(igamma)
373
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(igammac)
374
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(nextafter)
375
+ DEFINE_BF16_FUNC_VIA_FLOAT(log)
376
+ DEFINE_BF16_FUNC_VIA_FLOAT(log2)
377
+ DEFINE_BF16_FUNC_VIA_FLOAT(log10)
378
+ DEFINE_BF16_FUNC_VIA_FLOAT(log1p)
379
+ DEFINE_BF16_FUNC_VIA_FLOAT(sin)
380
+ DEFINE_BF16_FUNC_VIA_FLOAT(sinh)
381
+ DEFINE_BF16_FUNC_VIA_FLOAT(cos)
382
+ DEFINE_BF16_FUNC_VIA_FLOAT(cosh)
383
+ DEFINE_BF16_FUNC_VIA_FLOAT(ceil)
384
+ DEFINE_BF16_FUNC_VIA_FLOAT(floor)
385
+ DEFINE_BF16_FUNC_VIA_FLOAT(round)
386
+ DEFINE_BF16_FUNC_VIA_FLOAT(tan)
387
+ DEFINE_BF16_FUNC_VIA_FLOAT(tanh)
388
+ DEFINE_BF16_FUNC_VIA_FLOAT(trunc)
389
+ DEFINE_BF16_FUNC_VIA_FLOAT(lgamma)
390
+ DEFINE_BF16_FUNC_VIA_FLOAT(sqrt)
391
+ DEFINE_BF16_FUNC_VIA_FLOAT(reciprocal)
392
+ DEFINE_BF16_FUNC_VIA_FLOAT(rsqrt)
393
+ DEFINE_BF16_FUNC_VIA_FLOAT_W_ARG(pow)
394
+
395
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator==(
396
+ const Vectorized<BFloat16>& other) const {
397
+ auto [f1, f2] = convert_bfloat16_float(values);
398
+ auto [f3, f4] = convert_bfloat16_float(other);
399
+ svbool_t mask1 = svcmpeq_f32(ptrue, f1, f3);
400
+ svbool_t mask2 = svcmpeq_f32(ptrue, f2, f4);
401
+ auto res1 = svsel_f32(mask1, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
402
+ auto res2 = svsel_f32(mask2, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
403
+
404
+ auto bf16_1 = svreinterpret_bf16_f32(res1);
405
+ auto bf16_2 = svreinterpret_bf16_f32(res2);
406
+ return svuzp1_bf16(bf16_1, bf16_2);
407
+ }
408
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator!=(
409
+ const Vectorized<BFloat16>& other) const {
410
+ auto [f1, f2] = convert_bfloat16_float(values);
411
+ auto [f3, f4] = convert_bfloat16_float(other);
412
+ svbool_t mask1 = svcmpne_f32(ptrue, f1, f3);
413
+ svbool_t mask2 = svcmpne_f32(ptrue, f2, f4);
414
+ auto res1 = svsel_f32(mask1, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
415
+ auto res2 = svsel_f32(mask2, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
416
+
417
+ auto bf16_1 = svreinterpret_bf16_f32(res1);
418
+ auto bf16_2 = svreinterpret_bf16_f32(res2);
419
+ return svuzp1_bf16(bf16_1, bf16_2);
420
+ }
421
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator>(
422
+ const Vectorized<BFloat16>& other) const {
423
+ auto [v1, v2] = convert_bfloat16_float(*this);
424
+ auto [v3, v4] = convert_bfloat16_float(other);
425
+ return convert_float_bfloat16(v1 > v3, v2 > v4);
426
+ }
427
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator>=(
428
+ const Vectorized<BFloat16>& other) const {
429
+ auto [v1, v2] = convert_bfloat16_float(*this);
430
+ auto [v3, v4] = convert_bfloat16_float(other);
431
+ return convert_float_bfloat16(v1 >= v3, v2 >= v4);
432
+ }
433
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator<(
434
+ const Vectorized<BFloat16>& other) const {
435
+ auto [v1, v2] = convert_bfloat16_float(*this);
436
+ auto [v3, v4] = convert_bfloat16_float(other);
437
+ return convert_float_bfloat16(v1 < v3, v2 < v4);
438
+ }
439
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::operator<=(
440
+ const Vectorized<BFloat16>& other) const {
441
+ auto [v1, v2] = convert_bfloat16_float(*this);
442
+ auto [v3, v4] = convert_bfloat16_float(other);
443
+ return convert_float_bfloat16(v1 <= v3, v2 <= v4);
444
+ }
445
+
446
+ // Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
447
+ // either input is a NaN.
448
+ template <>
449
+ Vectorized<BFloat16> inline maximum(
450
+ const Vectorized<BFloat16>& a,
451
+ const Vectorized<BFloat16>& b) {
452
+ return binary_operator_via_float(
453
+ static_cast<Vectorized<float> (*)(
454
+ const Vectorized<float>&, const Vectorized<float>&)>(&maximum),
455
+ a,
456
+ b);
457
+ }
458
+
459
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
460
+ // either input is a NaN.
461
+ template <>
462
+ Vectorized<BFloat16> inline minimum(
463
+ const Vectorized<BFloat16>& a,
464
+ const Vectorized<BFloat16>& b) {
465
+ return binary_operator_via_float(
466
+ static_cast<Vectorized<float> (*)(
467
+ const Vectorized<float>&, const Vectorized<float>&)>(&minimum),
468
+ a,
469
+ b);
470
+ }
471
+
472
+ template <>
473
+ Vectorized<BFloat16> inline clamp_max(
474
+ const Vectorized<BFloat16>& a,
475
+ const Vectorized<BFloat16>& max) {
476
+ return binary_operator_via_float(
477
+ static_cast<Vectorized<float> (*)(
478
+ const Vectorized<float>&, const Vectorized<float>&)>(&clamp_max),
479
+ a,
480
+ max);
481
+ }
482
+
483
+ template <>
484
+ Vectorized<BFloat16> inline clamp_min(
485
+ const Vectorized<BFloat16>& a,
486
+ const Vectorized<BFloat16>& min) {
487
+ return binary_operator_via_float(
488
+ static_cast<Vectorized<float> (*)(
489
+ const Vectorized<float>&, const Vectorized<float>&)>(&clamp_min),
490
+ a,
491
+ min);
492
+ }
493
+
494
+ template <>
495
+ Vectorized<BFloat16> inline clamp(
496
+ const Vectorized<BFloat16>& a,
497
+ const Vectorized<BFloat16>& min,
498
+ const Vectorized<BFloat16>& max) {
499
+ return clamp_min(clamp_max(a, max), min);
500
+ }
501
+
502
+ template <>
503
+ Vectorized<BFloat16> inline operator&(
504
+ const Vectorized<BFloat16>& a,
505
+ const Vectorized<BFloat16>& b) {
506
+ return svreinterpret_bf16_u16(
507
+ svand_u16_x(ptrue, svreinterpret_u16_bf16(a), svreinterpret_u16_bf16(b)));
508
+ }
509
+
510
+ template <>
511
+ Vectorized<BFloat16> inline operator|(
512
+ const Vectorized<BFloat16>& a,
513
+ const Vectorized<BFloat16>& b) {
514
+ return svreinterpret_bf16_u16(
515
+ svorr_u16_x(ptrue, svreinterpret_u16_bf16(a), svreinterpret_u16_bf16(b)));
516
+ }
517
+
518
+ template <>
519
+ Vectorized<BFloat16> inline operator^(
520
+ const Vectorized<BFloat16>& a,
521
+ const Vectorized<BFloat16>& b) {
522
+ return svreinterpret_bf16_u16(
523
+ sveor_u16_x(ptrue, svreinterpret_u16_bf16(a), svreinterpret_u16_bf16(b)));
524
+ }
525
+
526
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::eq(
527
+ const Vectorized<BFloat16>& other) const {
528
+ return (*this == other) & Vectorized<BFloat16>(1.0f);
529
+ }
530
+
531
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::ne(
532
+ const Vectorized<BFloat16>& other) const {
533
+ return (*this != other) & Vectorized<BFloat16>(1.0f);
534
+ }
535
+
536
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::gt(
537
+ const Vectorized<BFloat16>& other) const {
538
+ return (*this > other) & Vectorized<BFloat16>(1.0f);
539
+ }
540
+
541
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::ge(
542
+ const Vectorized<BFloat16>& other) const {
543
+ return (*this >= other) & Vectorized<BFloat16>(1.0f);
544
+ }
545
+
546
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::lt(
547
+ const Vectorized<BFloat16>& other) const {
548
+ return (*this < other) & Vectorized<BFloat16>(1.0f);
549
+ }
550
+
551
+ Vectorized<BFloat16> inline Vectorized<BFloat16>::le(
552
+ const Vectorized<BFloat16>& other) const {
553
+ return (*this <= other) & Vectorized<BFloat16>(1.0f);
554
+ }
555
+
556
+ template <>
557
+ inline void convert(const BFloat16* src, BFloat16* dst, int64_t n) {
558
+ const int64_t fraction = n % Vectorized<BFloat16>::size();
559
+ #pragma unroll
560
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<BFloat16>::size()) {
561
+ svst1_bf16(
562
+ ptrue,
563
+ const_cast<bfloat16_t*>(reinterpret_cast<const bfloat16_t*>(dst)) + i,
564
+ svldnt1_bf16(
565
+ ptrue,
566
+ const_cast<bfloat16_t*>(reinterpret_cast<const bfloat16_t*>(src)) +
567
+ i));
568
+ }
569
+ #pragma unroll
570
+ for (int64_t i = n - fraction; i < n; i += Vectorized<BFloat16>::size()) {
571
+ svbool_t pg = svwhilelt_b16(i, n);
572
+ svst1_bf16(
573
+ pg,
574
+ const_cast<bfloat16_t*>(reinterpret_cast<const bfloat16_t*>(dst)) + i,
575
+ svldnt1_bf16(
576
+ pg,
577
+ const_cast<bfloat16_t*>(reinterpret_cast<const bfloat16_t*>(src)) +
578
+ i));
579
+ }
580
+ }
581
+
582
+ template <>
583
+ Vectorized<BFloat16> inline fmadd(
584
+ const Vectorized<BFloat16>& a,
585
+ const Vectorized<BFloat16>& b,
586
+ const Vectorized<BFloat16>& c) {
587
+ return a * b + c;
588
+ }
589
+
590
+ #endif // defined(CPU_CAPABILITY_SVE) && defined(__ARM_FEATURE_BF16)
591
+
592
+ } // namespace CPU_CAPABILITY
593
+ } // namespace vec
594
+ } // namespace at
595
+
596
+ #else
597
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
598
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_common_sve.h ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with SVE]
6
+
7
+ #include <ATen/cpu/vec/intrinsics.h>
8
+
9
+ #include <ATen/cpu/vec/sve/sve_helper.h>
10
+ #include <ATen/cpu/vec/vec_base.h>
11
+
12
+ #if defined(CPU_CAPABILITY_SVE)
13
+ #include <ATen/cpu/vec/sve/vec_bfloat16.h>
14
+ #include <ATen/cpu/vec/sve/vec_double.h>
15
+ #include <ATen/cpu/vec/sve/vec_float.h>
16
+ #include <ATen/cpu/vec/sve/vec_int.h>
17
+ #include <ATen/cpu/vec/sve/vec_qint.h>
18
+ #endif
19
+
20
+ namespace at::vec {
21
+ // Note [CPU_CAPABILITY namespace]
22
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23
+ // This header, and all of its subheaders, will be compiled with
24
+ // different architecture flags for each supported set of vector
25
+ // intrinsics. So we need to make sure they aren't inadvertently
26
+ // linked together. We do this by declaring objects in an `inline
27
+ // namespace` which changes the name mangling, but can still be
28
+ // accessed as `at::vec`.
29
+ inline namespace CPU_CAPABILITY {
30
+
31
+ #if defined(CPU_CAPABILITY_SVE)
32
+
33
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CAST ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
+ #define DEFINE_SVE_CAST(t1_t, t1_prefix, t2_t, t2_prefix) \
35
+ template <> \
36
+ inline Vectorized<t1_t> cast<t1_t, t2_t>(const Vectorized<t2_t>& src) { \
37
+ return svreinterpret_##t1_prefix##_##t2_prefix(src); \
38
+ } \
39
+ template <> \
40
+ inline Vectorized<t2_t> cast<t2_t, t1_t>(const Vectorized<t1_t>& src) { \
41
+ return svreinterpret_##t2_prefix##_##t1_prefix(src); \
42
+ }
43
+
44
+ DEFINE_SVE_CAST(int64_t, s64, double, f64)
45
+ DEFINE_SVE_CAST(int32_t, s32, double, f64)
46
+ DEFINE_SVE_CAST(int16_t, s16, double, f64)
47
+ DEFINE_SVE_CAST(int64_t, s64, float, f32)
48
+ DEFINE_SVE_CAST(int32_t, s32, float, f32)
49
+ DEFINE_SVE_CAST(int16_t, s16, float, f32)
50
+ DEFINE_SVE_CAST(float, f32, double, f64)
51
+
52
+ #ifdef __ARM_FEATURE_BF16
53
+ DEFINE_SVE_CAST(int64_t, s64, c10::BFloat16, bf16)
54
+ DEFINE_SVE_CAST(int32_t, s32, c10::BFloat16, bf16)
55
+ DEFINE_SVE_CAST(int16_t, s16, c10::BFloat16, bf16)
56
+ #endif // __ARM_FEATURE_BF16
57
+
58
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GATHER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59
+
60
+ template <int64_t scale = 1>
61
+ std::enable_if_t<
62
+ scale == 1 || scale == 2 || scale == 4 || scale == 8,
63
+ Vectorized<
64
+ double>> inline gather(const double* base_addr, const Vectorized<int64_t>& vindex_) {
65
+ svint64_t vindex =
66
+ svasrd_n_s64_x(ptrue, svmul_s64_x(ptrue, vindex_, svdup_n_s64(scale)), 3);
67
+ return svld1_gather_s64index_f64(ptrue, base_addr, vindex);
68
+ }
69
+
70
+ template <int64_t scale = 1>
71
+ std::enable_if_t<
72
+ scale == 1 || scale == 2 || scale == 4 || scale == 8,
73
+ Vectorized<
74
+ float>> inline gather(const float* base_addr, const Vectorized<int32_t>& vindex_) {
75
+ svint32_t vindex =
76
+ svasrd_n_s32_x(ptrue, svmul_s32_x(ptrue, vindex_, svdup_n_s32(scale)), 2);
77
+ return svld1_gather_s32index_f32(ptrue, base_addr, vindex);
78
+ }
79
+
80
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MASK GATHER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81
+
82
+ template <int64_t scale = 1>
83
+ std::
84
+ enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<double>> inline mask_gather(
85
+ const Vectorized<double>& src,
86
+ const double* base_addr,
87
+ const Vectorized<int64_t>& vindex_,
88
+ const Vectorized<double>& mask_) {
89
+ svbool_t mask =
90
+ svcmpeq_s64(ptrue, svreinterpret_s64_f64(mask_), ALL_S64_TRUE_MASK);
91
+ svint64_t vindex =
92
+ svasrd_n_s64_x(ptrue, svmul_s64_x(ptrue, vindex_, svdup_n_s64(scale)), 3);
93
+ return svsel_f64(
94
+ mask, svld1_gather_s64index_f64(mask, base_addr, vindex), src);
95
+ }
96
+
97
+ template <int64_t scale = 1>
98
+ std::
99
+ enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<float>> inline mask_gather(
100
+ const Vectorized<float>& src,
101
+ const float* base_addr,
102
+ const Vectorized<int32_t>& vindex_,
103
+ const Vectorized<float>& mask_) {
104
+ svbool_t mask =
105
+ svcmpeq_s32(ptrue, svreinterpret_s32_f32(mask_), ALL_S32_TRUE_MASK);
106
+ svint32_t vindex =
107
+ svasrd_n_s32_x(ptrue, svmul_s32_x(ptrue, vindex_, svdup_n_s32(scale)), 2);
108
+ return svsel_f32(
109
+ mask, svld1_gather_s32index_f32(mask, base_addr, vindex), src);
110
+ }
111
+
112
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CONVERT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
+
114
+ // Only works for inputs in the range: [-2^51, 2^51]
115
+ // From: https://stackoverflow.com/a/41148578
116
+ template <>
117
+ Vectorized<int64_t> inline convert_to_int_of_same_size<double>(
118
+ const Vectorized<double>& src) {
119
+ svfloat64_t x = svadd_f64_x(ptrue, src, svdup_n_f64(0x0018000000000000));
120
+ return svsub_s64_x(
121
+ ptrue,
122
+ svreinterpret_s64_f64(x),
123
+ svreinterpret_s64_f64(svdup_n_f64(0x0018000000000000)));
124
+ }
125
+
126
+ template <>
127
+ Vectorized<int32_t> inline convert_to_int_of_same_size<float>(
128
+ const Vectorized<float>& src) {
129
+ return svcvt_s32_f32_x(ptrue, src);
130
+ }
131
+
132
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INTERLEAVE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133
+
134
+ template <>
135
+ std::pair<Vectorized<double>, Vectorized<double>> inline interleave2<double>(
136
+ const Vectorized<double>& a,
137
+ const Vectorized<double>& b) {
138
+ // inputs:
139
+ // a = {a0, a1, a3, a3}
140
+ // b = {b0, b1, b2, b3}
141
+ // group cols crossing lanes:
142
+ // return {a0, b0, a1, b1}
143
+ // {a2, b2, a3, b3}
144
+ return std::make_pair(
145
+ Vectorized<double>(svzip1_f64(a, b)),
146
+ Vectorized<double>(svzip2_f64(a, b)));
147
+ }
148
+
149
+ template <>
150
+ std::pair<Vectorized<float>, Vectorized<float>> inline interleave2<float>(
151
+ const Vectorized<float>& a,
152
+ const Vectorized<float>& b) {
153
+ // inputs:
154
+ // a = {a0, a1, a2, a3, a4, a5, a6, a7}
155
+ // b = {b0, b1, b2, b3, b4, b5, b6, b7}
156
+ // group cols crossing lanes:
157
+ // return {a0, b0, a1, b1, a2, b2, a3, b3}
158
+ // {a4, b4, a5, b5, a6, b6, a7, b7}
159
+ return std::make_pair(
160
+ Vectorized<float>(svzip1_f32(a, b)), Vectorized<float>(svzip2_f32(a, b)));
161
+ }
162
+
163
+ #ifdef __ARM_FEATURE_BF16
164
+ template <>
165
+ std::pair<
166
+ Vectorized<c10::BFloat16>,
167
+ Vectorized<c10::BFloat16>> inline interleave2<c10::
168
+ BFloat16>(
169
+ const Vectorized<c10::BFloat16>& a,
170
+ const Vectorized<c10::BFloat16>& b) {
171
+ // inputs:
172
+ // a = {a0, a1, a2, a3, a4, a5, a6, a7}
173
+ // b = {b0, b1, b2, b3, b4, b5, b6, b7}
174
+ // group cols crossing lanes:
175
+ // return {a0, b0, a1, b1, a2, b2, a3, b3}
176
+ // {a4, b4, a5, b5, a6, b6, a7, b7}
177
+ return std::make_pair(
178
+ Vectorized<c10::BFloat16>(svzip1_bf16(a, b)),
179
+ Vectorized<c10::BFloat16>(svzip2_bf16(a, b)));
180
+ }
181
+ #endif // __ARM_FEATURE_BF16
182
+
183
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DEINTERLEAVE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184
+
185
+ template <>
186
+ std::pair<Vectorized<double>, Vectorized<double>> inline deinterleave2<double>(
187
+ const Vectorized<double>& a,
188
+ const Vectorized<double>& b) {
189
+ // inputs:
190
+ // a = {a0, b0, a1, b1}
191
+ // b = {a2, b2, a3, b3}
192
+ // swap lanes:
193
+ // return {a0, a1, a2, a3}
194
+ // {b0, b1, b2, b3}
195
+ return std::make_pair(
196
+ Vectorized<double>(svuzp1_f64(a, b)),
197
+ Vectorized<double>(svuzp2_f64(a, b)));
198
+ }
199
+
200
+ template <>
201
+ std::pair<Vectorized<float>, Vectorized<float>> inline deinterleave2<float>(
202
+ const Vectorized<float>& a,
203
+ const Vectorized<float>& b) {
204
+ // inputs:
205
+ // a = {a0, b0, a1, b1, a2, b2, a3, b3}
206
+ // b = {a4, b4, a5, b5, a6, b6, a7, b7}
207
+ // swap lanes:
208
+ // return {a0, a1, a2, a3, a4, a5, a6, a7}
209
+ // {b0, b1, b2, b3, b4, b5, b6, b7}
210
+ return std::make_pair(
211
+ Vectorized<float>(svuzp1_f32(a, b)), Vectorized<float>(svuzp2_f32(a, b)));
212
+ }
213
+
214
+ #ifdef __ARM_FEATURE_BF16
215
+ template <>
216
+ std::pair<
217
+ Vectorized<c10::BFloat16>,
218
+ Vectorized<c10::BFloat16>> inline deinterleave2<c10::
219
+ BFloat16>(
220
+ const Vectorized<c10::BFloat16>& a,
221
+ const Vectorized<c10::BFloat16>& b) {
222
+ // inputs:
223
+ // a = {a0, b0, a1, b1, a2, b2, a3, b3}
224
+ // b = {a4, b4, a5, b5, a6, b6, a7, b7}
225
+ // swap lanes:
226
+ // return {a0, a1, a2, a3, a4, a5, a6, a7}
227
+ // {b0, b1, b2, b3, b4, b5, b6, b7}
228
+ return std::make_pair(
229
+ Vectorized<c10::BFloat16>(svuzp1_bf16((svbfloat16_t)a, (svbfloat16_t)b)),
230
+ Vectorized<c10::BFloat16>(svuzp2_bf16((svbfloat16_t)a, (svbfloat16_t)b)));
231
+ }
232
+ #endif // __ARM_FEATURE_BF16
233
+
234
+ #endif // defined(CPU_CAPABILITY_SVE)
235
+
236
+ } // namespace CPU_CAPABILITY
237
+ } // namespace at::vec
238
+
239
+ #else
240
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
241
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_double.h ADDED
@@ -0,0 +1,622 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/sve/sve_helper.h>
6
+ #include <ATen/cpu/vec/vec_base.h>
7
+ #include <cmath>
8
+ #if defined(__aarch64__) && defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
9
+ #include <sleef.h>
10
+ #define USE_SLEEF(sleef_code, non_sleef_code) sleef_code
11
+ #else
12
+ #define USE_SLEEF(sleef_code, non_sleef_code) non_sleef_code
13
+ #endif
14
+
15
+ namespace at::vec {
16
+ // Note [CPU_CAPABILITY namespace]
17
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
+ // This header, and all of its subheaders, will be compiled with
19
+ // different architecture flags for each supported set of vector
20
+ // intrinsics. So we need to make sure they aren't inadvertently
21
+ // linked together. We do this by declaring objects in an `inline
22
+ // namespace` which changes the name mangling, but can still be
23
+ // accessed as `at::vec`.
24
+ inline namespace CPU_CAPABILITY {
25
+
26
+ #if defined(CPU_CAPABILITY_SVE)
27
+
28
+ template <>
29
+ struct is_vec_specialized_for<double> : std::bool_constant<true> {};
30
+
31
+ template <>
32
+ class Vectorized<double> {
33
+ private:
34
+ vls_float64_t values;
35
+
36
+ public:
37
+ using value_type = double;
38
+ using size_type = int;
39
+ static constexpr size_type size() {
40
+ return VECTOR_WIDTH / sizeof(double);
41
+ }
42
+ Vectorized() {
43
+ values = svdup_n_f64(0);
44
+ }
45
+ Vectorized(svfloat64_t v) : values(v) {}
46
+ Vectorized(double val) {
47
+ values = svdup_n_f64(val);
48
+ }
49
+ template <
50
+ typename... Args,
51
+ typename = std::enable_if_t<(sizeof...(Args) == size())>>
52
+ Vectorized(Args... vals) {
53
+ __at_align__ double buffer[size()] = {vals...};
54
+ values = svld1_f64(ptrue, buffer);
55
+ }
56
+ operator svfloat64_t() const {
57
+ return values;
58
+ }
59
+ template <uint64_t mask>
60
+ static Vectorized<double> blend(
61
+ const Vectorized<double>& a,
62
+ const Vectorized<double>& b) {
63
+ // Build an array of flags: each element is 1 if the corresponding bit in
64
+ // 'mask' is set, 0 otherwise.
65
+ __at_align__ int64_t flag_arr[size()];
66
+ for (int i = 0; i < size(); i++) {
67
+ flag_arr[i] = (mask & (1ULL << i)) ? 1 : 0;
68
+ }
69
+ // Load the flag array into an SVE int64 vector.
70
+ svint64_t int_mask = svld1_s64(svptrue_b64(), flag_arr);
71
+ // Compare each lane of int_mask to 0; returns an svbool_t predicate where
72
+ // true indicates a nonzero flag.
73
+ svbool_t blend_mask = svcmpne_n_s64(svptrue_b64(), int_mask, 0);
74
+
75
+ // Use svsel to select elements from b where the predicate is true, else
76
+ // from a.
77
+ svfloat64_t result = svsel(blend_mask, b.values, a.values);
78
+ return Vectorized<double>(result);
79
+ }
80
+ static Vectorized<double> blendv(
81
+ const Vectorized<double>& a,
82
+ const Vectorized<double>& b,
83
+ const Vectorized<double>& mask_) {
84
+ svbool_t mask =
85
+ svcmpeq_s64(ptrue, svreinterpret_s64_f64(mask_), ALL_S64_TRUE_MASK);
86
+ return svsel_f64(mask, b, a);
87
+ }
88
+ template <typename step_t>
89
+ static Vectorized<double> arange(
90
+ double base = 0.,
91
+ step_t step = static_cast<step_t>(1)) {
92
+ __at_align__ double buffer[size()];
93
+ for (int64_t i = 0; i < size(); i++) {
94
+ buffer[i] = base + i * step;
95
+ }
96
+ return svld1_f64(ptrue, buffer);
97
+ }
98
+ static Vectorized<double> set(
99
+ const Vectorized<double>& a,
100
+ const Vectorized<double>& b,
101
+ int64_t count = size()) {
102
+ if (count == 0) {
103
+ return a;
104
+ } else if (count < size()) {
105
+ return svsel_f64(svwhilelt_b64(0ull, count), b, a);
106
+ }
107
+ return b;
108
+ }
109
+ static Vectorized<double> loadu(const void* ptr, int64_t count = size()) {
110
+ if (count == size())
111
+ return svld1_f64(ptrue, reinterpret_cast<const double*>(ptr));
112
+ svbool_t pg = svwhilelt_b64(0ull, count);
113
+ return svld1_f64(pg, reinterpret_cast<const double*>(ptr));
114
+ }
115
+ void store(void* ptr, int64_t count = size()) const {
116
+ if (count == size()) {
117
+ svst1_f64(ptrue, reinterpret_cast<double*>(ptr), values);
118
+ } else {
119
+ svbool_t pg = svwhilelt_b64(0ull, count);
120
+ svst1_f64(pg, reinterpret_cast<double*>(ptr), values);
121
+ }
122
+ }
123
+ const double& operator[](int idx) const = delete;
124
+ double& operator[](int idx) = delete;
125
+ int64_t zero_mask() const {
126
+ // returns an integer mask where all zero elements are translated to 1-bit
127
+ // and others are translated to 0-bit
128
+ int64_t mask = 0;
129
+ __at_align__ int64_t mask_array[size()];
130
+
131
+ svbool_t svbool_mask = svcmpeq_f64(ptrue, values, ZERO_F64);
132
+ svst1_s64(
133
+ ptrue,
134
+ mask_array,
135
+ svsel_s64(svbool_mask, ALL_S64_TRUE_MASK, ALL_S64_FALSE_MASK));
136
+ for (int64_t i = 0; i < size(); ++i) {
137
+ if (mask_array[i])
138
+ mask |= (1ull << i);
139
+ }
140
+ return mask;
141
+ }
142
+ Vectorized<double> isnan() const {
143
+ // NaN check
144
+ svbool_t mask = svcmpuo_f64(ptrue, values, ZERO_F64);
145
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
146
+ }
147
+ bool has_inf_nan() const {
148
+ return svptest_any(
149
+ ptrue,
150
+ svcmpuo_f64(ptrue, svsub_f64_x(ptrue, values, values), ZERO_F64));
151
+ }
152
+ Vectorized<double> map(double (*f)(double)) const {
153
+ __at_align__ double tmp[size()];
154
+ store(tmp);
155
+ for (int64_t i = 0; i < size(); ++i) {
156
+ tmp[i] = f(tmp[i]);
157
+ }
158
+ return loadu(tmp);
159
+ }
160
+ Vectorized<double> abs() const {
161
+ return svabs_f64_x(ptrue, values);
162
+ }
163
+ Vectorized<double> angle() const {
164
+ const auto nan_vec = svdup_n_f64(NAN);
165
+ const auto nan_mask = svcmpuo_f64(ptrue, values, ZERO_F64);
166
+ const auto pi = svdup_n_f64(c10::pi<double>);
167
+
168
+ const auto neg_mask = svcmplt_f64(ptrue, values, ZERO_F64);
169
+ auto angle = svsel_f64(neg_mask, pi, ZERO_F64);
170
+ angle = svsel_f64(nan_mask, nan_vec, angle);
171
+ return angle;
172
+ }
173
+ Vectorized<double> real() const {
174
+ return *this;
175
+ }
176
+ Vectorized<double> imag() const {
177
+ return Vectorized<double>(0.0);
178
+ }
179
+ Vectorized<double> conj() const {
180
+ return *this;
181
+ }
182
+ Vectorized<double> acos() const {
183
+ return USE_SLEEF(
184
+ Vectorized<double>(Sleef_acosdx_u10sve(values)), map(std::acos));
185
+ }
186
+ Vectorized<double> acosh() const {
187
+ return USE_SLEEF(
188
+ Vectorized<double>(Sleef_acoshdx_u10sve(values)), map(std::acosh));
189
+ }
190
+ Vectorized<double> asin() const {
191
+ return USE_SLEEF(
192
+ Vectorized<double>(Sleef_asindx_u10sve(values)), map(std::asin));
193
+ }
194
+ Vectorized<double> asinh() const {
195
+ return USE_SLEEF(
196
+ Vectorized<double>(Sleef_asinhdx_u10sve(values)), map(std::asinh));
197
+ }
198
+ Vectorized<double> atan() const {
199
+ return USE_SLEEF(
200
+ Vectorized<double>(Sleef_atandx_u10sve(values)), map(std::atan));
201
+ }
202
+ Vectorized<double> atanh() const {
203
+ return USE_SLEEF(
204
+ Vectorized<double>(Sleef_atanhdx_u10sve(values)), map(std::atanh));
205
+ }
206
+ Vectorized<double> atan2(const Vectorized<double>& b) const {USE_SLEEF(
207
+ { return Vectorized<double>(Sleef_atan2dx_u10sve(values, b)); },
208
+ {
209
+ __at_align__ double tmp[size()];
210
+ __at_align__ double tmp_b[size()];
211
+ store(tmp);
212
+ b.store(tmp_b);
213
+ for (int64_t i = 0; i < size(); i++) {
214
+ tmp[i] = std::atan2(tmp[i], tmp_b[i]);
215
+ }
216
+ return loadu(tmp);
217
+ })} Vectorized<double> copysign(const Vectorized<double>& sign) const {
218
+ USE_SLEEF(
219
+ { return Vectorized<double>(Sleef_copysigndx_sve(values, sign)); },
220
+ {
221
+ __at_align__ double tmp[size()];
222
+ __at_align__ double tmp_sign[size()];
223
+ store(tmp);
224
+ sign.store(tmp_sign);
225
+ for (int64_t i = 0; i < size(); i++) {
226
+ tmp[i] = std::copysign(tmp[i], tmp_sign[i]);
227
+ }
228
+ return loadu(tmp);
229
+ })} Vectorized<double> erf() const {
230
+ return USE_SLEEF(
231
+ Vectorized<double>(Sleef_erfdx_u10sve(values)), map(std::erf));
232
+ }
233
+ Vectorized<double> erfc() const {
234
+ return USE_SLEEF(
235
+ Vectorized<double>(Sleef_erfcdx_u15sve(values)), map(std::erfc));
236
+ }
237
+ Vectorized<double> erfinv() const {
238
+ return map(calc_erfinv);
239
+ }
240
+ Vectorized<double> exp() const {
241
+ return USE_SLEEF(
242
+ Vectorized<double>(Sleef_expdx_u10sve(values)), map(std::exp));
243
+ }
244
+ Vectorized<double> exp2() const {
245
+ return USE_SLEEF(
246
+ Vectorized<double>(Sleef_exp2dx_u10sve(values)), map(std::exp2));
247
+ }
248
+ Vectorized<double> expm1() const {
249
+ return USE_SLEEF(
250
+ Vectorized<double>(Sleef_expm1dx_u10sve(values)), map(std::expm1));
251
+ }
252
+ Vectorized<double> exp_u20() const {
253
+ return exp();
254
+ }
255
+ Vectorized<double> fexp_u20() const {
256
+ return exp();
257
+ }
258
+ Vectorized<double> fmod(const Vectorized<double>& q) const {USE_SLEEF(
259
+ { return Vectorized<double>(Sleef_fmoddx_sve(values, q)); },
260
+ {
261
+ __at_align__ double tmp[size()];
262
+ __at_align__ double tmp_q[size()];
263
+ store(tmp);
264
+ q.store(tmp_q);
265
+ for (int64_t i = 0; i < size(); i++) {
266
+ tmp[i] = std::fmod(tmp[i], tmp_q[i]);
267
+ }
268
+ return loadu(tmp);
269
+ })} Vectorized<double> hypot(const Vectorized<double>& b) const {
270
+ USE_SLEEF(
271
+ { return Vectorized<double>(Sleef_hypotdx_u05sve(values, b)); },
272
+ {
273
+ __at_align__ double tmp[size()];
274
+ __at_align__ double tmp_b[size()];
275
+ store(tmp);
276
+ b.store(tmp_b);
277
+ for (int64_t i = 0; i < size(); i++) {
278
+ tmp[i] = std::hypot(tmp[i], tmp_b[i]);
279
+ }
280
+ return loadu(tmp);
281
+ })} Vectorized<double> i0() const {
282
+ return map(calc_i0);
283
+ }
284
+ Vectorized<double> i0e() const {
285
+ return map(calc_i0e);
286
+ }
287
+ Vectorized<double> digamma() const {
288
+ return map(calc_digamma);
289
+ }
290
+ Vectorized<double> igamma(const Vectorized<double>& x) const {
291
+ __at_align__ double tmp[size()];
292
+ __at_align__ double tmp_x[size()];
293
+ store(tmp);
294
+ x.store(tmp_x);
295
+ for (int64_t i = 0; i < size(); i++) {
296
+ tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
297
+ }
298
+ return loadu(tmp);
299
+ }
300
+ Vectorized<double> igammac(const Vectorized<double>& x) const {
301
+ __at_align__ double tmp[size()];
302
+ __at_align__ double tmp_x[size()];
303
+ store(tmp);
304
+ x.store(tmp_x);
305
+ for (int64_t i = 0; i < size(); i++) {
306
+ tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
307
+ }
308
+ return loadu(tmp);
309
+ }
310
+ Vectorized<double> nextafter(const Vectorized<double>& b) const {USE_SLEEF(
311
+ { return Vectorized<double>(Sleef_nextafterdx_sve(values, b)); },
312
+ {
313
+ __at_align__ double tmp[size()];
314
+ __at_align__ double tmp_b[size()];
315
+ store(tmp);
316
+ b.store(tmp_b);
317
+ for (int64_t i = 0; i < size(); ++i) {
318
+ tmp[i] = std::nextafter(tmp[i], tmp_b[i]);
319
+ }
320
+ return loadu(tmp);
321
+ })} Vectorized<double> log() const {
322
+ return USE_SLEEF(
323
+ Vectorized<double>(Sleef_logdx_u10sve(values)), map(std::log));
324
+ }
325
+ Vectorized<double> log2() const {
326
+ return USE_SLEEF(
327
+ Vectorized<double>(Sleef_log2dx_u10sve(values)), map(std::log2));
328
+ }
329
+ Vectorized<double> log10() const {
330
+ return USE_SLEEF(
331
+ Vectorized<double>(Sleef_log10dx_u10sve(values)), map(std::log10));
332
+ }
333
+ Vectorized<double> log1p() const {
334
+ return USE_SLEEF(
335
+ Vectorized<double>(Sleef_log1pdx_u10sve(values)), map(std::log1p));
336
+ }
337
+ Vectorized<double> frac() const;
338
+ Vectorized<double> sin() const {
339
+ return USE_SLEEF(
340
+ Vectorized<double>(Sleef_sindx_u10sve(values)), map(std::sin));
341
+ }
342
+ Vectorized<double> sinh() const {
343
+ return USE_SLEEF(
344
+ Vectorized<double>(Sleef_sinhdx_u10sve(values)), map(std::sinh));
345
+ }
346
+ Vectorized<double> cos() const {
347
+ return USE_SLEEF(
348
+ Vectorized<double>(Sleef_cosdx_u10sve(values)), map(std::cos));
349
+ }
350
+ Vectorized<double> cosh() const {
351
+ return USE_SLEEF(
352
+ Vectorized<double>(Sleef_coshdx_u10sve(values)), map(std::cosh));
353
+ }
354
+ Vectorized<double> ceil() const {
355
+ return svrintp_f64_x(ptrue, values);
356
+ }
357
+ Vectorized<double> floor() const {
358
+ return svrintm_f64_x(ptrue, values);
359
+ }
360
+ Vectorized<double> neg() const {
361
+ return svneg_f64_x(ptrue, values);
362
+ }
363
+ Vectorized<double> round() const {
364
+ return svrinti_f64_x(ptrue, values);
365
+ }
366
+ Vectorized<double> tan() const {
367
+ return USE_SLEEF(
368
+ Vectorized<double>(Sleef_tandx_u10sve(values)), map(std::tan));
369
+ }
370
+ Vectorized<double> tanh() const {
371
+ return USE_SLEEF(
372
+ Vectorized<double>(Sleef_tanhdx_u10sve(values)), map(std::tanh));
373
+ }
374
+ Vectorized<double> trunc() const {
375
+ return svrintz_f64_x(ptrue, values);
376
+ }
377
+ Vectorized<double> lgamma() const {
378
+ return USE_SLEEF(
379
+ Vectorized<double>(Sleef_lgammadx_u10sve(values)), map(std::lgamma));
380
+ }
381
+ Vectorized<double> sqrt() const {
382
+ return svsqrt_f64_x(ptrue, values);
383
+ }
384
+ Vectorized<double> reciprocal() const {
385
+ return svdivr_f64_x(ptrue, values, ONE_F64);
386
+ }
387
+ Vectorized<double> rsqrt() const {
388
+ return svdivr_f64_x(ptrue, svsqrt_f64_x(ptrue, values), ONE_F64);
389
+ }
390
+ Vectorized<double> pow(const Vectorized<double>& b) const {USE_SLEEF(
391
+ { return Vectorized<double>(Sleef_powdx_u10sve(values, b)); },
392
+ {
393
+ __at_align__ double tmp[size()];
394
+ __at_align__ double tmp_b[size()];
395
+ store(tmp);
396
+ b.store(tmp_b);
397
+ for (int64_t i = 0; i < size(); i++) {
398
+ tmp[i] = std::pow(tmp[i], tmp_b[i]);
399
+ }
400
+ return loadu(tmp);
401
+ })} // Comparison using the _CMP_**_OQ predicate.
402
+ // `O`: get false if an operand is NaN
403
+ // `Q`: do not raise if an operand is NaN
404
+ Vectorized<double> operator==(const Vectorized<double>& other) const {
405
+ svbool_t mask = svcmpeq_f64(ptrue, values, other);
406
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
407
+ }
408
+
409
+ Vectorized<double> operator!=(const Vectorized<double>& other) const {
410
+ svbool_t mask = svcmpne_f64(ptrue, values, other);
411
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
412
+ }
413
+
414
+ Vectorized<double> operator<(const Vectorized<double>& other) const {
415
+ svbool_t mask = svcmplt_f64(ptrue, values, other);
416
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
417
+ }
418
+
419
+ Vectorized<double> operator<=(const Vectorized<double>& other) const {
420
+ svbool_t mask = svcmple_f64(ptrue, values, other);
421
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
422
+ }
423
+
424
+ Vectorized<double> operator>(const Vectorized<double>& other) const {
425
+ svbool_t mask = svcmpgt_f64(ptrue, values, other);
426
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
427
+ }
428
+
429
+ Vectorized<double> operator>=(const Vectorized<double>& other) const {
430
+ svbool_t mask = svcmpge_f64(ptrue, values, other);
431
+ return svsel_f64(mask, ALL_F64_TRUE_MASK, ALL_F64_FALSE_MASK);
432
+ }
433
+
434
+ Vectorized<double> eq(const Vectorized<double>& other) const;
435
+ Vectorized<double> ne(const Vectorized<double>& other) const;
436
+ Vectorized<double> gt(const Vectorized<double>& other) const;
437
+ Vectorized<double> ge(const Vectorized<double>& other) const;
438
+ Vectorized<double> lt(const Vectorized<double>& other) const;
439
+ Vectorized<double> le(const Vectorized<double>& other) const;
440
+ };
441
+
442
+ template <>
443
+ Vectorized<double> inline operator+(
444
+ const Vectorized<double>& a,
445
+ const Vectorized<double>& b) {
446
+ return svadd_f64_x(ptrue, a, b);
447
+ }
448
+
449
+ template <>
450
+ Vectorized<double> inline operator-(
451
+ const Vectorized<double>& a,
452
+ const Vectorized<double>& b) {
453
+ return svsub_f64_x(ptrue, a, b);
454
+ }
455
+
456
+ template <>
457
+ Vectorized<double> inline operator*(
458
+ const Vectorized<double>& a,
459
+ const Vectorized<double>& b) {
460
+ return svmul_f64_x(ptrue, a, b);
461
+ }
462
+
463
+ template <>
464
+ Vectorized<double> inline operator/(
465
+ const Vectorized<double>& a,
466
+ const Vectorized<double>& b) {
467
+ return svdiv_f64_x(ptrue, a, b);
468
+ }
469
+
470
+ // frac. Implement this here so we can use subtraction
471
+ Vectorized<double> inline Vectorized<double>::frac() const {
472
+ return *this - this->trunc();
473
+ }
474
+
475
+ // Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
476
+ // either input is a NaN.
477
+ template <>
478
+ Vectorized<double> inline maximum(
479
+ const Vectorized<double>& a,
480
+ const Vectorized<double>& b) {
481
+ return svmax_f64_x(ptrue, a, b);
482
+ }
483
+
484
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
485
+ // either input is a NaN.
486
+ template <>
487
+ Vectorized<double> inline minimum(
488
+ const Vectorized<double>& a,
489
+ const Vectorized<double>& b) {
490
+ return svmin_f64_x(ptrue, a, b);
491
+ }
492
+
493
+ template <>
494
+ Vectorized<double> inline clamp(
495
+ const Vectorized<double>& a,
496
+ const Vectorized<double>& min,
497
+ const Vectorized<double>& max) {
498
+ return svmin_f64_x(ptrue, max, svmax_f64_x(ptrue, min, a));
499
+ }
500
+
501
+ template <>
502
+ Vectorized<double> inline clamp_max(
503
+ const Vectorized<double>& a,
504
+ const Vectorized<double>& max) {
505
+ return svmin_f64_x(ptrue, max, a);
506
+ }
507
+
508
+ template <>
509
+ Vectorized<double> inline clamp_min(
510
+ const Vectorized<double>& a,
511
+ const Vectorized<double>& min) {
512
+ return svmax_f64_x(ptrue, min, a);
513
+ }
514
+
515
+ template <>
516
+ Vectorized<double> inline operator&(
517
+ const Vectorized<double>& a,
518
+ const Vectorized<double>& b) {
519
+ return svreinterpret_f64_s64(
520
+ svand_s64_x(ptrue, svreinterpret_s64_f64(a), svreinterpret_s64_f64(b)));
521
+ }
522
+
523
+ template <>
524
+ Vectorized<double> inline operator|(
525
+ const Vectorized<double>& a,
526
+ const Vectorized<double>& b) {
527
+ return svreinterpret_f64_s64(
528
+ svorr_s64_x(ptrue, svreinterpret_s64_f64(a), svreinterpret_s64_f64(b)));
529
+ }
530
+
531
+ template <>
532
+ Vectorized<double> inline operator^(
533
+ const Vectorized<double>& a,
534
+ const Vectorized<double>& b) {
535
+ return svreinterpret_f64_s64(
536
+ sveor_s64_x(ptrue, svreinterpret_s64_f64(a), svreinterpret_s64_f64(b)));
537
+ }
538
+
539
+ Vectorized<double> inline Vectorized<double>::eq(
540
+ const Vectorized<double>& other) const {
541
+ return (*this == other) & Vectorized<double>(1.0);
542
+ }
543
+
544
+ Vectorized<double> inline Vectorized<double>::ne(
545
+ const Vectorized<double>& other) const {
546
+ return (*this != other) & Vectorized<double>(1.0);
547
+ }
548
+
549
+ Vectorized<double> inline Vectorized<double>::gt(
550
+ const Vectorized<double>& other) const {
551
+ return (*this > other) & Vectorized<double>(1.0);
552
+ }
553
+
554
+ Vectorized<double> inline Vectorized<double>::ge(
555
+ const Vectorized<double>& other) const {
556
+ return (*this >= other) & Vectorized<double>(1.0);
557
+ }
558
+
559
+ Vectorized<double> inline Vectorized<double>::lt(
560
+ const Vectorized<double>& other) const {
561
+ return (*this < other) & Vectorized<double>(1.0);
562
+ }
563
+
564
+ Vectorized<double> inline Vectorized<double>::le(
565
+ const Vectorized<double>& other) const {
566
+ return (*this <= other) & Vectorized<double>(1.0);
567
+ }
568
+
569
+ template <>
570
+ inline void convert(const double* src, double* dst, int64_t n) {
571
+ const int64_t fraction = n % Vectorized<double>::size();
572
+ #pragma unroll
573
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<double>::size()) {
574
+ svst1_f64(ptrue, dst + i, svldnt1_f64(ptrue, src + i));
575
+ }
576
+ #pragma unroll
577
+ for (int64_t i = n - fraction; i < n; i += Vectorized<double>::size()) {
578
+ svbool_t pg = svwhilelt_b64(i, n);
579
+ svst1_f64(pg, dst + i, svldnt1_f64(pg, src + i));
580
+ }
581
+ }
582
+
583
+ template <>
584
+ Vectorized<double> inline fmadd(
585
+ const Vectorized<double>& a,
586
+ const Vectorized<double>& b,
587
+ const Vectorized<double>& c) {
588
+ return svmad_f64_x(ptrue, a, b, c);
589
+ }
590
+
591
+ template <>
592
+ Vectorized<double> inline fnmadd(
593
+ const Vectorized<double>& a,
594
+ const Vectorized<double>& b,
595
+ const Vectorized<double>& c) {
596
+ return svmsb_f64_x(ptrue, a, b, c);
597
+ }
598
+
599
+ template <>
600
+ Vectorized<double> inline fmsub(
601
+ const Vectorized<double>& a,
602
+ const Vectorized<double>& b,
603
+ const Vectorized<double>& c) {
604
+ return svnmsb_f64_x(ptrue, a, b, c);
605
+ }
606
+
607
+ template <>
608
+ Vectorized<double> inline fnmsub(
609
+ const Vectorized<double>& a,
610
+ const Vectorized<double>& b,
611
+ const Vectorized<double>& c) {
612
+ return svnmad_f64_x(ptrue, a, b, c);
613
+ }
614
+
615
+ #endif // defined(CPU_CAPABILITY_SVE)
616
+
617
+ } // namespace CPU_CAPABILITY
618
+ } // namespace at::vec
619
+
620
+ #else
621
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
622
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_float.h ADDED
@@ -0,0 +1,760 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/sve/sve_helper.h>
6
+ #include <ATen/cpu/vec/vec_base.h>
7
+ #include <cmath>
8
+ #if defined(__aarch64__) && defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
9
+ #include <sleef.h>
10
+ #define USE_SLEEF(sleef_code, non_sleef_code) sleef_code
11
+ #else
12
+ #define USE_SLEEF(sleef_code, non_sleef_code) non_sleef_code
13
+ #endif
14
+
15
+ namespace at::vec {
16
+ // Note [CPU_CAPABILITY namespace]
17
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
+ // This header, and all of its subheaders, will be compiled with
19
+ // different architecture flags for each supported set of vector
20
+ // intrinsics. So we need to make sure they aren't inadvertently
21
+ // linked together. We do this by declaring objects in an `inline
22
+ // namespace` which changes the name mangling, but can still be
23
+ // accessed as `at::vec`.
24
+ inline namespace CPU_CAPABILITY {
25
+
26
+ #if defined(CPU_CAPABILITY_SVE)
27
+
28
+ template <>
29
+ struct is_vec_specialized_for<float> : std::bool_constant<true> {};
30
+
31
+ template <>
32
+ class Vectorized<float> {
33
+ private:
34
+ vls_float32_t values;
35
+
36
+ public:
37
+ using value_type = float;
38
+ using size_type = int;
39
+ static constexpr size_type size() {
40
+ return VECTOR_WIDTH / sizeof(float);
41
+ }
42
+ Vectorized() {
43
+ values = svdup_n_f32(0);
44
+ }
45
+ Vectorized(svfloat32_t v) : values(v) {}
46
+ Vectorized(float val) {
47
+ values = svdup_n_f32(val);
48
+ }
49
+ template <
50
+ typename... Args,
51
+ typename = std::enable_if_t<(sizeof...(Args) == size())>>
52
+ Vectorized(Args... vals) {
53
+ __at_align__ float buffer[size()] = {vals...};
54
+ values = svld1_f32(ptrue, buffer);
55
+ }
56
+ operator svfloat32_t() const {
57
+ return values;
58
+ }
59
+ template <uint64_t mask>
60
+ static Vectorized<float> blend(
61
+ const Vectorized<float>& a,
62
+ const Vectorized<float>& b) {
63
+ // Build an array of flags: each element is 1 if the corresponding bit in
64
+ // 'mask' is set, 0 otherwise.
65
+ __at_align__ int32_t flag_arr[size()];
66
+ for (int i = 0; i < size(); i++) {
67
+ flag_arr[i] = (mask & (1ULL << i)) ? 1 : 0;
68
+ }
69
+ // Load the flag array into an SVE int32 vector.
70
+ svint32_t int_mask = svld1_s32(svptrue_b32(), flag_arr);
71
+ // Compare each lane of int_mask to 0; returns an svbool_t predicate where
72
+ // true indicates a nonzero flag.
73
+ svbool_t blend_mask = svcmpne_n_s32(svptrue_b32(), int_mask, 0);
74
+ // Use svsel to select elements from b where the predicate is true, else
75
+ // from a.
76
+ svfloat32_t result = svsel_f32(blend_mask, b.values, a.values);
77
+ return Vectorized<float>(result);
78
+ }
79
+ static Vectorized<float> blendv(
80
+ const Vectorized<float>& a,
81
+ const Vectorized<float>& b,
82
+ const Vectorized<float>& mask_) {
83
+ svbool_t mask =
84
+ svcmpeq_s32(ptrue, svreinterpret_s32_f32(mask_), ALL_S32_TRUE_MASK);
85
+ return svsel_f32(mask, b, a);
86
+ }
87
+ template <typename step_t>
88
+ static Vectorized<float> arange(
89
+ float base = 0.f,
90
+ step_t step = static_cast<step_t>(1)) {
91
+ __at_align__ float buffer[size()];
92
+ for (int64_t i = 0; i < size(); i++) {
93
+ buffer[i] = base + i * step;
94
+ }
95
+ return svld1_f32(ptrue, buffer);
96
+ }
97
+ static Vectorized<float> set(
98
+ const Vectorized<float>& a,
99
+ const Vectorized<float>& b,
100
+ int64_t count = size()) {
101
+ if (count == 0) {
102
+ return a;
103
+ } else if (count < size()) {
104
+ return svsel_f32(svwhilelt_b32(0ull, count), b, a);
105
+ }
106
+ return b;
107
+ }
108
+ static Vectorized<float> loadu(const void* ptr, int64_t count = size()) {
109
+ if (count == size())
110
+ return svld1_f32(ptrue, reinterpret_cast<const float*>(ptr));
111
+ svbool_t pg = svwhilelt_b32(0ull, count);
112
+ return svld1_f32(pg, reinterpret_cast<const float*>(ptr));
113
+ }
114
+ void store(void* ptr, int64_t count = size()) const {
115
+ if (count == size()) {
116
+ svst1_f32(ptrue, reinterpret_cast<float*>(ptr), values);
117
+ } else {
118
+ svbool_t pg = svwhilelt_b32(0ull, count);
119
+ svst1_f32(pg, reinterpret_cast<float*>(ptr), values);
120
+ }
121
+ }
122
+ const float& operator[](int idx) const = delete;
123
+ float& operator[](int idx) = delete;
124
+ int64_t zero_mask() const {
125
+ // returns an integer mask where all zero elements are translated to 1-bit
126
+ // and others are translated to 0-bit
127
+ int64_t mask = 0;
128
+ __at_align__ int32_t mask_array[size()];
129
+
130
+ svbool_t svbool_mask = svcmpeq_f32(ptrue, values, ZERO_F32);
131
+ svst1_s32(
132
+ ptrue,
133
+ mask_array,
134
+ svsel_s32(svbool_mask, ALL_S32_TRUE_MASK, ALL_S32_FALSE_MASK));
135
+ for (int64_t i = 0; i < size(); ++i) {
136
+ if (mask_array[i])
137
+ mask |= (1ull << i);
138
+ }
139
+ return mask;
140
+ }
141
+ Vectorized<float> isnan() const {
142
+ // NaN check
143
+ svbool_t mask = svcmpuo_f32(ptrue, values, ZERO_F32);
144
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
145
+ }
146
+ bool has_inf_nan() const {
147
+ return svptest_any(
148
+ ptrue,
149
+ svcmpuo_f32(ptrue, svsub_f32_x(ptrue, values, values), ZERO_F32));
150
+ }
151
+ Vectorized<float> map(float (*f)(float)) const {
152
+ __at_align__ float tmp[size()];
153
+ store(tmp);
154
+ for (int64_t i = 0; i < size(); ++i) {
155
+ tmp[i] = f(tmp[i]);
156
+ }
157
+ return loadu(tmp);
158
+ }
159
+ Vectorized<float> abs() const {
160
+ return svabs_f32_x(ptrue, values);
161
+ }
162
+ Vectorized<float> angle() const {
163
+ const auto nan_vec = svdup_n_f32(NAN);
164
+ const auto nan_mask = svcmpuo_f32(ptrue, values, ZERO_F32);
165
+ const auto pi = svdup_n_f32(c10::pi<float>);
166
+
167
+ const auto neg_mask = svcmplt_f32(ptrue, values, ZERO_F32);
168
+ auto angle = svsel_f32(neg_mask, pi, ZERO_F32);
169
+ angle = svsel_f32(nan_mask, nan_vec, angle);
170
+ return angle;
171
+ }
172
+ Vectorized<float> real() const {
173
+ return values;
174
+ }
175
+ Vectorized<float> imag() const {
176
+ return Vectorized<float>(0.f);
177
+ }
178
+ Vectorized<float> conj() const {
179
+ return values;
180
+ }
181
+ Vectorized<float> acos() const {
182
+ return USE_SLEEF(
183
+ Vectorized<float>(Sleef_acosfx_u10sve(values)), map(std::acos));
184
+ }
185
+ Vectorized<float> acosh() const {
186
+ return USE_SLEEF(
187
+ Vectorized<float>(Sleef_acoshfx_u10sve(values)), map(std::acosh));
188
+ }
189
+ Vectorized<float> asin() const {
190
+ return USE_SLEEF(
191
+ Vectorized<float>(Sleef_asinfx_u10sve(values)), map(std::asin));
192
+ }
193
+ Vectorized<float> asinh() const {
194
+ return USE_SLEEF(
195
+ Vectorized<float>(Sleef_asinhfx_u10sve(values)), map(std::asinh));
196
+ }
197
+ Vectorized<float> atan() const {
198
+ return USE_SLEEF(
199
+ Vectorized<float>(Sleef_atanfx_u10sve(values)), map(std::atan));
200
+ }
201
+ Vectorized<float> atanh() const {
202
+ return USE_SLEEF(
203
+ Vectorized<float>(Sleef_atanhfx_u10sve(values)), map(std::atanh));
204
+ }
205
+ Vectorized<float> atan2(const Vectorized<float>& b) const {USE_SLEEF(
206
+ { return Vectorized<float>(Sleef_atan2fx_u10sve(values, b)); },
207
+ {
208
+ __at_align__ float tmp[size()];
209
+ __at_align__ float tmp_b[size()];
210
+ store(tmp);
211
+ b.store(tmp_b);
212
+ for (int64_t i = 0; i < size(); i++) {
213
+ tmp[i] = std::atan2(tmp[i], tmp_b[i]);
214
+ }
215
+ return loadu(tmp);
216
+ })} Vectorized<float> copysign(const Vectorized<float>& sign) const {
217
+
218
+ USE_SLEEF(
219
+ { return Vectorized<float>(Sleef_copysignfx_sve(values, sign)); },
220
+ {
221
+ __at_align__ float tmp[size()];
222
+ __at_align__ float tmp_sign[size()];
223
+ store(tmp);
224
+ sign.store(tmp_sign);
225
+ for (int64_t i = 0; i < size(); ++i) {
226
+ tmp[i] = std::copysign(tmp[i], tmp_sign[i]);
227
+ }
228
+ return loadu(tmp);
229
+ })} Vectorized<float> erf() const {
230
+ return USE_SLEEF(
231
+ Vectorized<float>(Sleef_erffx_u10sve(values)), map(std::erf));
232
+ }
233
+ Vectorized<float> erfc() const {
234
+ return USE_SLEEF(
235
+ Vectorized<float>(Sleef_erfcfx_u15sve(values)), map(std::erfc));
236
+ }
237
+ Vectorized<float> erfinv() const {
238
+ return map(calc_erfinv);
239
+ }
240
+ Vectorized<float> exp() const {
241
+ return USE_SLEEF(
242
+ Vectorized<float>(Sleef_expfx_u10sve(values)), map(std::exp));
243
+ }
244
+ Vectorized<float> exp2() const {
245
+ return USE_SLEEF(
246
+ Vectorized<float>(Sleef_exp2fx_u10sve(values)), map(std::exp2));
247
+ }
248
+ Vectorized<float> expm1() const {
249
+ return USE_SLEEF(
250
+ Vectorized<float>(Sleef_expm1fx_u10sve(values)), map(std::expm1));
251
+ }
252
+ // Implementation copied from Arm Optimized Routines:
253
+ // https://github.com/ARM-software/optimized-routines/blob/master/math/aarch64/sve/expf.c
254
+ Vectorized<float> exp_u20() const {
255
+ // special case to handle special inputs that are too large or too small
256
+ // i.e. where there's at least one element x, s.t. |x| >= 87.3...
257
+ svbool_t is_special_case = svacgt(svptrue_b32(), values, 0x1.5d5e2ap+6f);
258
+ if (svptest_any(svptrue_b32(), is_special_case)) {
259
+ return exp();
260
+ }
261
+ const svfloat32_t ln2_hi = svdup_n_f32(0x1.62e4p-1f);
262
+ const svfloat32_t ln2_lo = svdup_n_f32(0x1.7f7d1cp-20f);
263
+ const svfloat32_t c1 = svdup_n_f32(0.5f);
264
+ const svfloat32_t inv_ln2 = svdup_n_f32(0x1.715476p+0f);
265
+
266
+ const float shift = 0x1.803f8p17f;
267
+
268
+ /* n = round(x/(ln2/N)). */
269
+ svfloat32_t z = svmad_x(svptrue_b32(), inv_ln2, values, shift);
270
+ svfloat32_t n = svsub_x(svptrue_b32(), z, shift);
271
+
272
+ /* r = x - n*ln2/N. */
273
+ svfloat32_t r = values;
274
+ r = svmls_x(svptrue_b32(), r, n, ln2_hi);
275
+ r = svmls_x(svptrue_b32(), r, n, ln2_lo);
276
+
277
+ /* scale = 2^(n/N). */
278
+ svfloat32_t scale = svexpa(svreinterpret_u32(z));
279
+
280
+ /* poly(r) = exp(r) - 1 ~= r + 0.5 r^2. */
281
+ svfloat32_t r2 = svmul_x(svptrue_b32(), r, r);
282
+ svfloat32_t poly = svmla_x(svptrue_b32(), r, r2, c1);
283
+ return svmla_x(svptrue_b32(), scale, scale, poly);
284
+ }
285
+ Vectorized<float> fexp_u20() const {
286
+ return exp_u20();
287
+ }
288
+ Vectorized<float> fmod(const Vectorized<float>& q) const {USE_SLEEF(
289
+ { return Vectorized<float>(Sleef_fmodfx_sve(values, q)); },
290
+ {
291
+ __at_align__ float tmp[size()];
292
+ __at_align__ float tmp_q[size()];
293
+ store(tmp);
294
+ q.store(tmp_q);
295
+ for (int64_t i = 0; i < size(); ++i) {
296
+ tmp[i] = std::fmod(tmp[i], tmp_q[i]);
297
+ }
298
+ return loadu(tmp);
299
+ })} Vectorized<float> hypot(const Vectorized<float>& b) const {
300
+ USE_SLEEF(
301
+ { return Vectorized<float>(Sleef_hypotfx_u05sve(values, b)); },
302
+ {
303
+ __at_align__ float tmp[size()];
304
+ __at_align__ float tmp_b[size()];
305
+ store(tmp);
306
+ b.store(tmp_b);
307
+ for (int64_t i = 0; i < size(); i++) {
308
+ tmp[i] = std::hypot(tmp[i], tmp_b[i]);
309
+ }
310
+ return loadu(tmp);
311
+ })} Vectorized<float> i0() const {
312
+ return map(calc_i0);
313
+ }
314
+ Vectorized<float> i0e() const {
315
+ return map(calc_i0e);
316
+ }
317
+ Vectorized<float> digamma() const {
318
+ return map(calc_digamma);
319
+ }
320
+ Vectorized<float> igamma(const Vectorized<float>& x) const {
321
+ __at_align__ float tmp[size()];
322
+ __at_align__ float tmp_x[size()];
323
+ store(tmp);
324
+ x.store(tmp_x);
325
+ for (int64_t i = 0; i < size(); i++) {
326
+ tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
327
+ }
328
+ return loadu(tmp);
329
+ }
330
+ Vectorized<float> igammac(const Vectorized<float>& x) const {
331
+ __at_align__ float tmp[size()];
332
+ __at_align__ float tmp_x[size()];
333
+ store(tmp);
334
+ x.store(tmp_x);
335
+ for (int64_t i = 0; i < size(); i++) {
336
+ tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
337
+ }
338
+ return loadu(tmp);
339
+ }
340
+ Vectorized<float> nextafter(const Vectorized<float>& b) const {USE_SLEEF(
341
+ { return Vectorized<float>(Sleef_nextafterfx_sve(values, b)); },
342
+ {
343
+ __at_align__ float tmp[size()];
344
+ __at_align__ float tmp_b[size()];
345
+ store(tmp);
346
+ b.store(tmp_b);
347
+ for (int64_t i = 0; i < size(); ++i) {
348
+ tmp[i] = std::nextafter(tmp[i], tmp_b[i]);
349
+ }
350
+ return loadu(tmp);
351
+ })} Vectorized<float> log() const {
352
+ return USE_SLEEF(
353
+ Vectorized<float>(Sleef_logfx_u10sve(values)), map(std::log));
354
+ }
355
+ Vectorized<float> log2() const {
356
+ return USE_SLEEF(
357
+ Vectorized<float>(Sleef_log2fx_u10sve(values)), map(std::log2));
358
+ }
359
+ Vectorized<float> log10() const {
360
+ return USE_SLEEF(
361
+ Vectorized<float>(Sleef_log10fx_u10sve(values)), map(std::log10));
362
+ }
363
+ Vectorized<float> log1p() const {
364
+ return USE_SLEEF(
365
+ Vectorized<float>(Sleef_log1pfx_u10sve(values)), map(std::log1p));
366
+ }
367
+ Vectorized<float> frac() const;
368
+ Vectorized<float> sin() const {
369
+ return USE_SLEEF(
370
+ Vectorized<float>(Sleef_sinfx_u10sve(values)), map(std::sin));
371
+ }
372
+ Vectorized<float> sinh() const {
373
+ return USE_SLEEF(
374
+ Vectorized<float>(Sleef_sinhfx_u10sve(values)), map(std::sinh));
375
+ }
376
+ Vectorized<float> cos() const {
377
+ return USE_SLEEF(
378
+ Vectorized<float>(Sleef_cosfx_u10sve(values)), map(std::cos));
379
+ }
380
+ Vectorized<float> cosh() const {
381
+ return USE_SLEEF(
382
+ Vectorized<float>(Sleef_coshfx_u10sve(values)), map(std::cosh));
383
+ }
384
+ Vectorized<float> ceil() const {
385
+ return svrintp_f32_x(ptrue, values);
386
+ }
387
+ Vectorized<float> floor() const {
388
+ return svrintm_f32_x(ptrue, values);
389
+ }
390
+ Vectorized<float> neg() const {
391
+ return svneg_f32_x(ptrue, values);
392
+ }
393
+ Vectorized<float> round() const {
394
+ return svrinti_f32_x(ptrue, values);
395
+ }
396
+ Vectorized<float> tan() const {
397
+ return USE_SLEEF(
398
+ Vectorized<float>(Sleef_tanfx_u10sve(values)), map(std::tan));
399
+ }
400
+ // Implementation is picked from
401
+ // https://github.com/ARM-software/ComputeLibrary/blob/v25.01/src/core/NEON/SVEMath.inl#L179
402
+ Vectorized<float> tanh() const {
403
+ // Constants used for the tanh calculation.
404
+ const svfloat32_t CONST_1 =
405
+ svdup_n_f32(1.f); // Constant 1.0f for the tanh formula.
406
+ const svfloat32_t CONST_2 = svdup_n_f32(
407
+ 2.f); // Constant 2.0f for the tanh formula (used in exp(2x)).
408
+ const svfloat32_t CONST_MIN_TANH = svdup_n_f32(
409
+ -10.f); // Minimum threshold for input values to prevent overflow.
410
+ const svfloat32_t CONST_MAX_TANH = svdup_n_f32(
411
+ 10.f); // Maximum threshold for input values to prevent overflow.
412
+
413
+ // Step 1: Clamp the values within the range [-10, 10] to prevent overflow
414
+ // during exponentiation. The tanh function approaches ±1 rapidly as the
415
+ // input grows large, so we limit the input range to avoid numerical
416
+ // instability. svmax_f32_z ensures values are greater than -10, and
417
+ // svmin_f32_z ensures they are less than 10.
418
+ svfloat32_t x = svmin_f32_z(
419
+ ptrue, svmax_f32_z(ptrue, values, CONST_MIN_TANH), CONST_MAX_TANH);
420
+
421
+ // Step 2: Calculate exp(2 * x), where x is the clamped value.
422
+ // svmul_f32_z computes 2 * x, and exp_u20() computes the exponential of
423
+ // the result (via Vectorized<float>, then auto-converts back to
424
+ // svfloat32_t).
425
+ svfloat32_t exp2x =
426
+ Vectorized<float>(svmul_f32_z(ptrue, CONST_2, x)).exp_u20();
427
+
428
+ // Step 3: Calculate the numerator of the tanh function, which is exp(2x)
429
+ // - 1.
430
+ svfloat32_t num = svsub_f32_z(ptrue, exp2x, CONST_1);
431
+
432
+ // Step 4: Calculate the denominator of the tanh function, which is exp(2x)
433
+ // + 1.
434
+ svfloat32_t den = svadd_f32_z(ptrue, exp2x, CONST_1);
435
+
436
+ // Step 5: Calculate the tanh function as the ratio of the numerator and
437
+ // denominator: num / den.
438
+ svfloat32_t tanh = svdiv_f32_z(ptrue, num, den);
439
+
440
+ // Return the calculated tanh values.
441
+ return tanh;
442
+ }
443
+ Vectorized<float> trunc() const {
444
+ return svrintz_f32_x(ptrue, values);
445
+ }
446
+ Vectorized<float> lgamma() const {
447
+ return USE_SLEEF(
448
+ Vectorized<float>(Sleef_lgammafx_u10sve(values)), map(std::lgamma));
449
+ }
450
+ Vectorized<float> sqrt() const {
451
+ return svsqrt_f32_x(ptrue, values);
452
+ }
453
+ Vectorized<float> reciprocal() const {
454
+ return svdivr_f32_x(ptrue, values, ONE_F32);
455
+ }
456
+ Vectorized<float> rsqrt() const {
457
+ return svdivr_f32_x(ptrue, svsqrt_f32_x(ptrue, values), ONE_F32);
458
+ }
459
+ Vectorized<float> pow(const Vectorized<float>& b) const {USE_SLEEF(
460
+ { return Vectorized<float>(Sleef_powfx_u10sve(values, b)); },
461
+ {
462
+ __at_align__ float tmp[size()];
463
+ __at_align__ float tmp_b[size()];
464
+ store(tmp);
465
+ b.store(tmp_b);
466
+ for (int64_t i = 0; i < size(); i++) {
467
+ tmp[i] = std::pow(tmp[i], tmp_b[i]);
468
+ }
469
+ return loadu(tmp);
470
+ })} // Comparison using the _CMP_**_OQ predicate.
471
+ // `O`: get false if an operand is NaN
472
+ // `Q`: do not raise if an operand is NaN
473
+ Vectorized<float> operator==(const Vectorized<float>& other) const {
474
+ svbool_t mask = svcmpeq_f32(ptrue, values, other);
475
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
476
+ }
477
+
478
+ Vectorized<float> operator!=(const Vectorized<float>& other) const {
479
+ svbool_t mask = svcmpne_f32(ptrue, values, other);
480
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
481
+ }
482
+
483
+ Vectorized<float> operator<(const Vectorized<float>& other) const {
484
+ svbool_t mask = svcmplt_f32(ptrue, values, other);
485
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
486
+ }
487
+
488
+ Vectorized<float> operator<=(const Vectorized<float>& other) const {
489
+ svbool_t mask = svcmple_f32(ptrue, values, other);
490
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
491
+ }
492
+
493
+ Vectorized<float> operator>(const Vectorized<float>& other) const {
494
+ svbool_t mask = svcmpgt_f32(ptrue, values, other);
495
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
496
+ }
497
+
498
+ Vectorized<float> operator>=(const Vectorized<float>& other) const {
499
+ svbool_t mask = svcmpge_f32(ptrue, values, other);
500
+ return svsel_f32(mask, ALL_F32_TRUE_MASK, ALL_F32_FALSE_MASK);
501
+ }
502
+
503
+ Vectorized<float> eq(const Vectorized<float>& other) const;
504
+ Vectorized<float> ne(const Vectorized<float>& other) const;
505
+ Vectorized<float> gt(const Vectorized<float>& other) const;
506
+ Vectorized<float> ge(const Vectorized<float>& other) const;
507
+ Vectorized<float> lt(const Vectorized<float>& other) const;
508
+ Vectorized<float> le(const Vectorized<float>& other) const;
509
+ };
510
+
511
+ template <>
512
+ Vectorized<float> inline operator+(
513
+ const Vectorized<float>& a,
514
+ const Vectorized<float>& b) {
515
+ return svadd_f32_x(ptrue, a, b);
516
+ }
517
+
518
+ template <>
519
+ Vectorized<float> inline operator-(
520
+ const Vectorized<float>& a,
521
+ const Vectorized<float>& b) {
522
+ return svsub_f32_x(ptrue, a, b);
523
+ }
524
+
525
+ template <>
526
+ Vectorized<float> inline operator*(
527
+ const Vectorized<float>& a,
528
+ const Vectorized<float>& b) {
529
+ return svmul_f32_x(ptrue, a, b);
530
+ }
531
+
532
+ template <>
533
+ Vectorized<float> inline operator/(
534
+ const Vectorized<float>& a,
535
+ const Vectorized<float>& b) {
536
+ return svdiv_f32_x(ptrue, a, b);
537
+ }
538
+
539
+ // frac. Implement this here so we can use subtraction
540
+ Vectorized<float> inline Vectorized<float>::frac() const {
541
+ return *this - this->trunc();
542
+ }
543
+
544
+ // Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
545
+ // either input is a NaN.
546
+ template <>
547
+ Vectorized<float> inline maximum(
548
+ const Vectorized<float>& a,
549
+ const Vectorized<float>& b) {
550
+ return svmax_f32_x(ptrue, a, b);
551
+ }
552
+
553
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
554
+ // either input is a NaN.
555
+ template <>
556
+ Vectorized<float> inline minimum(
557
+ const Vectorized<float>& a,
558
+ const Vectorized<float>& b) {
559
+ return svmin_f32_x(ptrue, a, b);
560
+ }
561
+
562
+ template <>
563
+ Vectorized<float> inline clamp(
564
+ const Vectorized<float>& a,
565
+ const Vectorized<float>& min,
566
+ const Vectorized<float>& max) {
567
+ return svmin_f32_x(ptrue, max, svmax_f32_x(ptrue, min, a));
568
+ }
569
+
570
+ template <>
571
+ Vectorized<float> inline clamp_max(
572
+ const Vectorized<float>& a,
573
+ const Vectorized<float>& max) {
574
+ return svmin_f32_x(ptrue, max, a);
575
+ }
576
+
577
+ template <>
578
+ Vectorized<float> inline clamp_min(
579
+ const Vectorized<float>& a,
580
+ const Vectorized<float>& min) {
581
+ return svmax_f32_x(ptrue, min, a);
582
+ }
583
+
584
+ template <>
585
+ Vectorized<float> inline operator&(
586
+ const Vectorized<float>& a,
587
+ const Vectorized<float>& b) {
588
+ return svreinterpret_f32_s32(
589
+ svand_s32_x(ptrue, svreinterpret_s32_f32(a), svreinterpret_s32_f32(b)));
590
+ }
591
+
592
+ template <>
593
+ Vectorized<float> inline operator|(
594
+ const Vectorized<float>& a,
595
+ const Vectorized<float>& b) {
596
+ return svreinterpret_f32_s32(
597
+ svorr_s32_x(ptrue, svreinterpret_s32_f32(a), svreinterpret_s32_f32(b)));
598
+ }
599
+
600
+ template <>
601
+ Vectorized<float> inline operator^(
602
+ const Vectorized<float>& a,
603
+ const Vectorized<float>& b) {
604
+ return svreinterpret_f32_s32(
605
+ sveor_s32_x(ptrue, svreinterpret_s32_f32(a), svreinterpret_s32_f32(b)));
606
+ }
607
+
608
+ Vectorized<float> inline Vectorized<float>::eq(
609
+ const Vectorized<float>& other) const {
610
+ return (*this == other) & Vectorized<float>(1.0f);
611
+ }
612
+
613
+ Vectorized<float> inline Vectorized<float>::ne(
614
+ const Vectorized<float>& other) const {
615
+ return (*this != other) & Vectorized<float>(1.0f);
616
+ }
617
+
618
+ Vectorized<float> inline Vectorized<float>::gt(
619
+ const Vectorized<float>& other) const {
620
+ return (*this > other) & Vectorized<float>(1.0f);
621
+ }
622
+
623
+ Vectorized<float> inline Vectorized<float>::ge(
624
+ const Vectorized<float>& other) const {
625
+ return (*this >= other) & Vectorized<float>(1.0f);
626
+ }
627
+
628
+ Vectorized<float> inline Vectorized<float>::lt(
629
+ const Vectorized<float>& other) const {
630
+ return (*this < other) & Vectorized<float>(1.0f);
631
+ }
632
+
633
+ Vectorized<float> inline Vectorized<float>::le(
634
+ const Vectorized<float>& other) const {
635
+ return (*this <= other) & Vectorized<float>(1.0f);
636
+ }
637
+
638
+ template <>
639
+ inline void convert(const float* src, float* dst, int64_t n) {
640
+ const int64_t fraction = n % Vectorized<float>::size();
641
+ #pragma unroll
642
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<float>::size()) {
643
+ svst1_f32(ptrue, dst + i, svldnt1_f32(ptrue, src + i));
644
+ }
645
+ #pragma unroll
646
+ for (int64_t i = n - fraction; i < n; i += Vectorized<float>::size()) {
647
+ svbool_t pg = svwhilelt_b32(i, n);
648
+ svst1_f32(pg, dst + i, svldnt1_f32(pg, src + i));
649
+ }
650
+ }
651
+
652
+ template <>
653
+ inline void convert(const float* src, at::Half* dst, int64_t n) {
654
+ const int64_t fraction = n % Vectorized<float>::size();
655
+ svbool_t pg_16 = svwhilelt_b16(0ull, Vectorized<float>::size());
656
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<float>::size());
657
+ #pragma unroll
658
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<float>::size()) {
659
+ svfloat16_t src_vec = svuzp1_f16(
660
+ svcvt_f16_f32_x(ptrue, svldnt1_f32(pg_32, src + i)), ZERO_F16);
661
+ svst1_f16(pg_16, reinterpret_cast<float16_t*>(dst) + i, src_vec);
662
+ }
663
+ #pragma unroll
664
+ for (int64_t i = n - fraction; i < n; i += Vectorized<float>::size()) {
665
+ pg_16 = svwhilelt_b16(i, n);
666
+ pg_32 = svwhilelt_b32(i, n);
667
+ svfloat16_t src_vec = svuzp1_f16(
668
+ svcvt_f16_f32_x(ptrue, svldnt1_f32(pg_32, src + i)), ZERO_F16);
669
+ svst1_f16(pg_16, reinterpret_cast<float16_t*>(dst) + i, src_vec);
670
+ }
671
+ }
672
+
673
+ template <>
674
+ inline void convert(const at::Half* src, float* dst, int64_t n) {
675
+ const int64_t fraction = n % Vectorized<float>::size();
676
+ svbool_t pg_16 = svwhilelt_b16(0ull, Vectorized<float>::size());
677
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<float>::size());
678
+ #pragma unroll
679
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<float>::size()) {
680
+ svfloat16_t src_vec = svzip1_f16(
681
+ svldnt1_f16(pg_16, reinterpret_cast<const float16_t*>(src) + i),
682
+ ZERO_F16);
683
+ svst1_f32(pg_32, dst + i, svcvt_f32_f16_x(ptrue, src_vec));
684
+ }
685
+ #pragma unroll
686
+ for (int64_t i = n - fraction; i < n; i += Vectorized<float>::size()) {
687
+ pg_16 = svwhilelt_b16(i, n);
688
+ pg_32 = svwhilelt_b32(i, n);
689
+ svfloat16_t src_vec = svzip1_f16(
690
+ svldnt1_f16(pg_16, reinterpret_cast<const float16_t*>(src) + i),
691
+ ZERO_F16);
692
+ svst1_f32(pg_32, dst + i, svcvt_f32_f16_x(ptrue, src_vec));
693
+ }
694
+ }
695
+
696
+ template <>
697
+ inline void convert(const bool* src, float* dst, int64_t n) {
698
+ const int64_t fraction = n % Vectorized<float>::size();
699
+ svbool_t pg_8 = svwhilelt_b8(0ull, Vectorized<float>::size());
700
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<float>::size());
701
+ #pragma unroll
702
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<float>::size()) {
703
+ svuint8_t src_vec_u8 =
704
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
705
+ svuint32_t src_vec_u32 = svunpklo_u32(svunpklo_u16(src_vec_u8));
706
+ svbool_t mask = svcmpne_u32(pg_32, src_vec_u32, ZERO_U32);
707
+ svst1_f32(pg_32, dst + i, svsel_f32(mask, ONE_F32, ZERO_F32));
708
+ }
709
+ #pragma unroll
710
+ for (int64_t i = n - fraction; i < n; i += Vectorized<float>::size()) {
711
+ pg_8 = svwhilelt_b8(i, n);
712
+ pg_32 = svwhilelt_b32(i, n);
713
+ svuint8_t src_vec_u8 =
714
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
715
+ svuint32_t src_vec_u32 = svunpklo_u32(svunpklo_u16(src_vec_u8));
716
+ svbool_t mask = svcmpne_u32(pg_32, src_vec_u32, ZERO_U32);
717
+ svst1_f32(pg_32, dst + i, svsel_f32(mask, ONE_F32, ZERO_F32));
718
+ }
719
+ }
720
+
721
+ template <>
722
+ Vectorized<float> inline fmadd(
723
+ const Vectorized<float>& a,
724
+ const Vectorized<float>& b,
725
+ const Vectorized<float>& c) {
726
+ return svmad_f32_x(ptrue, a, b, c);
727
+ }
728
+
729
+ template <>
730
+ Vectorized<float> inline fnmadd(
731
+ const Vectorized<float>& a,
732
+ const Vectorized<float>& b,
733
+ const Vectorized<float>& c) {
734
+ return svmsb_f32_x(ptrue, a, b, c);
735
+ }
736
+
737
+ template <>
738
+ Vectorized<float> inline fmsub(
739
+ const Vectorized<float>& a,
740
+ const Vectorized<float>& b,
741
+ const Vectorized<float>& c) {
742
+ return svnmsb_f32_x(ptrue, a, b, c);
743
+ }
744
+
745
+ template <>
746
+ Vectorized<float> inline fnmsub(
747
+ const Vectorized<float>& a,
748
+ const Vectorized<float>& b,
749
+ const Vectorized<float>& c) {
750
+ return svnmad_f32_x(ptrue, a, b, c);
751
+ }
752
+
753
+ #endif // defined(CPU_CAPABILITY_SVE)
754
+
755
+ } // namespace CPU_CAPABILITY
756
+ } // namespace at::vec
757
+
758
+ #else
759
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
760
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_int.h ADDED
@@ -0,0 +1,504 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/sve/sve_helper.h>
6
+ #include <ATen/cpu/vec/vec_base.h>
7
+
8
+ namespace at::vec {
9
+ // Note [CPU_CAPABILITY namespace]
10
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11
+ // This header, and all of its subheaders, will be compiled with
12
+ // different architecture flags for each supported set of vector
13
+ // intrinsics. So we need to make sure they aren't inadvertently
14
+ // linked together. We do this by declaring objects in an `inline
15
+ // namespace` which changes the name mangling, but can still be
16
+ // accessed as `at::vec`.
17
+ inline namespace CPU_CAPABILITY {
18
+
19
+ #if defined(CPU_CAPABILITY_SVE)
20
+
21
+ #define VEC_INT_SVE_TEMPLATE(vl, bit) \
22
+ template <> \
23
+ struct is_vec_specialized_for<int##bit##_t> : std::bool_constant<true> {}; \
24
+ \
25
+ template <> \
26
+ class Vectorized<int##bit##_t> { \
27
+ private: \
28
+ vls_int##bit##_t values; \
29
+ \
30
+ public: \
31
+ using value_type = int##bit##_t; \
32
+ using size_type = int; \
33
+ static constexpr size_type size() { \
34
+ return vl; \
35
+ } \
36
+ Vectorized() { \
37
+ values = svdup_n_s##bit(0); \
38
+ } \
39
+ Vectorized(svint##bit##_t v) : values(v) {} \
40
+ Vectorized(int##bit##_t val) { \
41
+ values = svdup_n_s##bit(val); \
42
+ } \
43
+ template < \
44
+ typename... Args, \
45
+ typename = std::enable_if_t<(sizeof...(Args) == size())>> \
46
+ Vectorized(Args... vals) { \
47
+ __at_align__ int##bit##_t buffer[size()] = {vals...}; \
48
+ values = svld1_s##bit(ptrue, buffer); \
49
+ } \
50
+ operator svint##bit##_t() const { \
51
+ return values; \
52
+ } \
53
+ template <uint64_t mask> \
54
+ static Vectorized<int##bit##_t> blend( \
55
+ const Vectorized<int##bit##_t>& a, \
56
+ const Vectorized<int##bit##_t>& b) { \
57
+ __at_align__ int##bit##_t flag_arr[size()]; \
58
+ for (int i = 0; i < size(); ++i) { \
59
+ flag_arr[i] = (i < 64 && (mask & (1ULL << i))) ? 1 : 0; \
60
+ } \
61
+ svbool_t blend_mask = svcmpne_n_s##bit( \
62
+ svptrue_b##bit(), svld1_s##bit(svptrue_b##bit(), flag_arr), 0); \
63
+ return Vectorized<int##bit##_t>( \
64
+ svsel_s##bit(blend_mask, b.values, a.values)); \
65
+ } \
66
+ static Vectorized<int##bit##_t> blendv( \
67
+ const Vectorized<int##bit##_t>& a, \
68
+ const Vectorized<int##bit##_t>& b, \
69
+ const Vectorized<int##bit##_t>& mask_) { \
70
+ svbool_t mask = svcmpeq_s##bit(ptrue, mask_, ALL_S##bit##_TRUE_MASK); \
71
+ return svsel_s##bit(mask, b, a); \
72
+ } \
73
+ /* step sometimes requires a higher precision type (e.g., T=int, \
74
+ * step_t=double) */ \
75
+ template <typename step_t> \
76
+ static Vectorized<int##bit##_t> arange( \
77
+ int##bit##_t base = 0, \
78
+ step_t step = static_cast<step_t>(1)) { \
79
+ __at_align__ int##bit##_t buffer[size()]; \
80
+ for (int64_t i = 0; i < size(); i++) { \
81
+ buffer[i] = base + i * step; \
82
+ } \
83
+ return svld1_s##bit(ptrue, buffer); \
84
+ } \
85
+ static Vectorized<int##bit##_t> set( \
86
+ const Vectorized<int##bit##_t>& a, \
87
+ const Vectorized<int##bit##_t>& b, \
88
+ int##bit##_t count = size()) { \
89
+ if (count == 0) { \
90
+ return a; \
91
+ } else if (count < size()) { \
92
+ return svsel_s##bit(svwhilelt_b##bit(0ull, count), b, a); \
93
+ } \
94
+ return b; \
95
+ } \
96
+ static Vectorized<int##bit##_t> loadu( \
97
+ const void* ptr, \
98
+ int64_t count = size()) { \
99
+ if (count == size()) \
100
+ return svld1_s##bit( \
101
+ ptrue, reinterpret_cast<const int##bit##_t*>(ptr)); \
102
+ svbool_t pg = svwhilelt_b##bit(0ull, count); \
103
+ return svld1_s##bit(pg, reinterpret_cast<const int##bit##_t*>(ptr)); \
104
+ } \
105
+ void store(void* ptr, int64_t count = size()) const { \
106
+ if (count == size()) { \
107
+ svst1_s##bit(ptrue, reinterpret_cast<int##bit##_t*>(ptr), values); \
108
+ } else { \
109
+ svbool_t pg = svwhilelt_b##bit(0ull, count); \
110
+ svst1_s##bit(pg, reinterpret_cast<int##bit##_t*>(ptr), values); \
111
+ } \
112
+ } \
113
+ const int##bit##_t& operator[](int idx) const = delete; \
114
+ int##bit##_t& operator[](int idx) = delete; \
115
+ Vectorized<int##bit##_t> abs() const { \
116
+ return svabs_s##bit##_x(ptrue, values); \
117
+ } \
118
+ Vectorized<int##bit##_t> real() const { \
119
+ return values; \
120
+ } \
121
+ Vectorized<int##bit##_t> imag() const { \
122
+ return svdup_n_s##bit(0); \
123
+ } \
124
+ Vectorized<int##bit##_t> conj() const { \
125
+ return values; \
126
+ } \
127
+ Vectorized<int##bit##_t> frac() const; \
128
+ Vectorized<int##bit##_t> neg() const { \
129
+ return svneg_s##bit##_x(ptrue, values); \
130
+ } \
131
+ Vectorized<int##bit##_t> operator==( \
132
+ const Vectorized<int##bit##_t>& other) const { \
133
+ svbool_t mask = svcmpeq_s##bit(ptrue, values, other); \
134
+ return svsel_s##bit( \
135
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
136
+ } \
137
+ Vectorized<int##bit##_t> operator!=( \
138
+ const Vectorized<int##bit##_t>& other) const { \
139
+ svbool_t mask = svcmpne_s##bit(ptrue, values, other); \
140
+ return svsel_s##bit( \
141
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
142
+ } \
143
+ Vectorized<int##bit##_t> operator<( \
144
+ const Vectorized<int##bit##_t>& other) const { \
145
+ svbool_t mask = svcmplt_s##bit(ptrue, values, other); \
146
+ return svsel_s##bit( \
147
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
148
+ } \
149
+ Vectorized<int##bit##_t> operator<=( \
150
+ const Vectorized<int##bit##_t>& other) const { \
151
+ svbool_t mask = svcmple_s##bit(ptrue, values, other); \
152
+ return svsel_s##bit( \
153
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
154
+ } \
155
+ Vectorized<int##bit##_t> operator>( \
156
+ const Vectorized<int##bit##_t>& other) const { \
157
+ svbool_t mask = svcmpgt_s##bit(ptrue, values, other); \
158
+ return svsel_s##bit( \
159
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
160
+ } \
161
+ Vectorized<int##bit##_t> operator>=( \
162
+ const Vectorized<int##bit##_t>& other) const { \
163
+ svbool_t mask = svcmpge_s##bit(ptrue, values, other); \
164
+ return svsel_s##bit( \
165
+ mask, ALL_S##bit##_TRUE_MASK, ALL_S##bit##_FALSE_MASK); \
166
+ } \
167
+ Vectorized<int##bit##_t> eq(const Vectorized<int##bit##_t>& other) const; \
168
+ Vectorized<int##bit##_t> ne(const Vectorized<int##bit##_t>& other) const; \
169
+ Vectorized<int##bit##_t> gt(const Vectorized<int##bit##_t>& other) const; \
170
+ Vectorized<int##bit##_t> ge(const Vectorized<int##bit##_t>& other) const; \
171
+ Vectorized<int##bit##_t> lt(const Vectorized<int##bit##_t>& other) const; \
172
+ Vectorized<int##bit##_t> le(const Vectorized<int##bit##_t>& other) const; \
173
+ }; \
174
+ template <> \
175
+ Vectorized<int##bit##_t> inline operator+( \
176
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
177
+ return svadd_s##bit##_x(ptrue, a, b); \
178
+ } \
179
+ template <> \
180
+ Vectorized<int##bit##_t> inline operator-( \
181
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
182
+ return svsub_s##bit##_x(ptrue, a, b); \
183
+ } \
184
+ template <> \
185
+ Vectorized<int##bit##_t> inline operator*( \
186
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
187
+ return svmul_s##bit##_x(ptrue, a, b); \
188
+ } \
189
+ template <> \
190
+ Vectorized<int##bit##_t> inline maximum( \
191
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
192
+ return svmax_s##bit##_x(ptrue, a, b); \
193
+ } \
194
+ template <> \
195
+ Vectorized<int##bit##_t> inline minimum( \
196
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
197
+ return svmin_s##bit##_x(ptrue, a, b); \
198
+ } \
199
+ template <> \
200
+ Vectorized<int##bit##_t> inline clamp( \
201
+ const Vectorized<int##bit##_t>& a, \
202
+ const Vectorized<int##bit##_t>& min, \
203
+ const Vectorized<int##bit##_t>& max) { \
204
+ return svmin_s##bit##_x(ptrue, max, svmax_s##bit##_x(ptrue, min, a)); \
205
+ } \
206
+ template <> \
207
+ Vectorized<int##bit##_t> inline clamp_max( \
208
+ const Vectorized<int##bit##_t>& a, \
209
+ const Vectorized<int##bit##_t>& max) { \
210
+ return svmin_s##bit##_x(ptrue, max, a); \
211
+ } \
212
+ template <> \
213
+ Vectorized<int##bit##_t> inline clamp_min( \
214
+ const Vectorized<int##bit##_t>& a, \
215
+ const Vectorized<int##bit##_t>& min) { \
216
+ return svmax_s##bit##_x(ptrue, min, a); \
217
+ } \
218
+ template <> \
219
+ Vectorized<int##bit##_t> inline operator&( \
220
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
221
+ return svand_s##bit##_x(ptrue, a, b); \
222
+ } \
223
+ template <> \
224
+ Vectorized<int##bit##_t> inline operator|( \
225
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
226
+ return svorr_s##bit##_x(ptrue, a, b); \
227
+ } \
228
+ template <> \
229
+ Vectorized<int##bit##_t> inline operator^( \
230
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
231
+ return sveor_s##bit##_x(ptrue, a, b); \
232
+ } \
233
+ template <> \
234
+ inline Vectorized<int##bit##_t> operator~( \
235
+ const Vectorized<int##bit##_t>& a) { \
236
+ return sveor_s##bit##_x(ptrue, a, svdup_n_s##bit(-1)); \
237
+ } \
238
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::eq( \
239
+ const Vectorized<int##bit##_t>& other) const { \
240
+ return (*this == other) & Vectorized<int##bit##_t>(1); \
241
+ } \
242
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::ne( \
243
+ const Vectorized<int##bit##_t>& other) const { \
244
+ return (*this != other) & Vectorized<int##bit##_t>(1); \
245
+ } \
246
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::gt( \
247
+ const Vectorized<int##bit##_t>& other) const { \
248
+ return (*this > other) & Vectorized<int##bit##_t>(1); \
249
+ } \
250
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::ge( \
251
+ const Vectorized<int##bit##_t>& other) const { \
252
+ return (*this >= other) & Vectorized<int##bit##_t>(1); \
253
+ } \
254
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::lt( \
255
+ const Vectorized<int##bit##_t>& other) const { \
256
+ return (*this < other) & Vectorized<int##bit##_t>(1); \
257
+ } \
258
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::le( \
259
+ const Vectorized<int##bit##_t>& other) const { \
260
+ return (*this <= other) & Vectorized<int##bit##_t>(1); \
261
+ }
262
+
263
+ VEC_INT_SVE_TEMPLATE(VECTOR_WIDTH / sizeof(int64_t), 64)
264
+ VEC_INT_SVE_TEMPLATE(VECTOR_WIDTH / sizeof(int32_t), 32)
265
+ VEC_INT_SVE_TEMPLATE(VECTOR_WIDTH / sizeof(int16_t), 16)
266
+ VEC_INT_SVE_TEMPLATE(VECTOR_WIDTH / sizeof(int8_t), 8)
267
+
268
+ template <typename T>
269
+ Vectorized<T> inline intdiv_nosve(
270
+ const Vectorized<T>& a,
271
+ const Vectorized<T>& b) {
272
+ T values_a[Vectorized<T>::size()];
273
+ T values_b[Vectorized<T>::size()];
274
+ a.store(values_a);
275
+ b.store(values_b);
276
+ for (int i = 0; i != Vectorized<T>::size(); i++) {
277
+ values_a[i] /= values_b[i];
278
+ }
279
+ return Vectorized<T>::loadu(values_a);
280
+ }
281
+
282
+ template <>
283
+ Vectorized<int64_t> inline operator/(
284
+ const Vectorized<int64_t>& a,
285
+ const Vectorized<int64_t>& b) {
286
+ return svdiv_s64_x(ptrue, a, b);
287
+ }
288
+
289
+ template <>
290
+ Vectorized<int32_t> inline operator/(
291
+ const Vectorized<int32_t>& a,
292
+ const Vectorized<int32_t>& b) {
293
+ return svdiv_s32_x(ptrue, a, b);
294
+ }
295
+
296
+ template <>
297
+ Vectorized<int16_t> inline operator/(
298
+ const Vectorized<int16_t>& a,
299
+ const Vectorized<int16_t>& b) {
300
+ return intdiv_nosve(a, b);
301
+ }
302
+
303
+ template <>
304
+ Vectorized<int8_t> inline operator/(
305
+ const Vectorized<int8_t>& a,
306
+ const Vectorized<int8_t>& b) {
307
+ return intdiv_nosve(a, b);
308
+ }
309
+
310
+ template <>
311
+ inline void convert(const int32_t* src, int64_t* dst, int64_t n) {
312
+ const int64_t fraction = n % Vectorized<int64_t>::size();
313
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<int64_t>::size());
314
+ svbool_t pg_64 = svwhilelt_b64(0ull, Vectorized<int64_t>::size());
315
+ #pragma unroll
316
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<int64_t>::size())
317
+ svst1_s64(pg_64, dst + i, svunpklo_s64(svldnt1_s32(pg_32, src + i)));
318
+ #pragma unroll
319
+ for (int64_t i = n - fraction; i < n; i += Vectorized<int64_t>::size()) {
320
+ pg_32 = svwhilelt_b32(i, n);
321
+ pg_64 = svwhilelt_b64(i, n);
322
+ svst1_s64(pg_64, dst + i, svunpklo_s64(svldnt1_s32(pg_32, src + i)));
323
+ }
324
+ }
325
+
326
+ template <>
327
+ inline void convert(const int64_t* src, float* dst, int64_t n) {
328
+ const int64_t fraction = n % Vectorized<int64_t>::size();
329
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<int64_t>::size());
330
+ svbool_t pg_64 = svwhilelt_b64(0ull, Vectorized<int64_t>::size());
331
+ #pragma unroll
332
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<int64_t>::size()) {
333
+ svint64_t src_vec_s64 = svldnt1_s64(pg_64, src + i);
334
+ svfloat32_t src_vec_f32 =
335
+ svuzp1_f32(svcvt_f32_s64_x(pg_64, src_vec_s64), ZERO_F32);
336
+ svst1_f32(pg_32, dst + i, src_vec_f32);
337
+ }
338
+ #pragma unroll
339
+ for (int64_t i = n - fraction; i < n; i += Vectorized<int64_t>::size()) {
340
+ pg_32 = svwhilelt_b32(i, n);
341
+ pg_64 = svwhilelt_b64(i, n);
342
+ svint64_t src_vec_s64 = svldnt1_s64(pg_64, src + i);
343
+ svfloat32_t src_vec_f32 =
344
+ svuzp1_f32(svcvt_f32_s64_x(pg_64, src_vec_s64), ZERO_F32);
345
+ svst1_f32(pg_32, dst + i, src_vec_f32);
346
+ }
347
+ }
348
+
349
+ template <>
350
+ inline void convert(const int32_t* src, float* dst, int64_t n) {
351
+ const int64_t fraction = n % Vectorized<int32_t>::size();
352
+ svbool_t pg = svwhilelt_b32(0ull, Vectorized<int32_t>::size());
353
+ #pragma unroll
354
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<int32_t>::size()) {
355
+ svint32_t src_vec = svldnt1_s32(pg, src + i);
356
+ svst1_f32(pg, dst + i, svcvt_f32_s32_x(pg, src_vec));
357
+ }
358
+ #pragma unroll
359
+ for (int64_t i = n - fraction; i < n; i += Vectorized<int32_t>::size()) {
360
+ pg = svwhilelt_b32(i, n);
361
+ svint32_t src_vec = svldnt1_s32(pg, src + i);
362
+ svst1_f32(pg, dst + i, svcvt_f32_s32_x(pg, src_vec));
363
+ }
364
+ }
365
+
366
+ template <>
367
+ inline void convert(const bool* src, int64_t* dst, int64_t n) {
368
+ const int64_t fraction = n % Vectorized<int64_t>::size();
369
+ svbool_t pg_8 = svwhilelt_b8(0ull, Vectorized<int64_t>::size());
370
+ svbool_t pg_64 = svwhilelt_b64(0ull, Vectorized<int64_t>::size());
371
+ #pragma unroll
372
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<int64_t>::size()) {
373
+ svuint8_t src_vec_u8 =
374
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
375
+ svuint64_t src_vec_u64 =
376
+ svunpklo_u64(svunpklo_u32(svunpklo_u16(src_vec_u8)));
377
+ svbool_t mask = svcmpne_u64(pg_64, src_vec_u64, ZERO_U64);
378
+ svst1_s64(pg_64, dst + i, svsel_s64(mask, ONE_S64, ZERO_S64));
379
+ }
380
+ #pragma unroll
381
+ for (int64_t i = n - fraction; i < n; i += Vectorized<int64_t>::size()) {
382
+ pg_8 = svwhilelt_b8(i, n);
383
+ pg_64 = svwhilelt_b64(i, n);
384
+ svuint8_t src_vec_u8 =
385
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
386
+ svuint64_t src_vec_u64 =
387
+ svunpklo_u64(svunpklo_u32(svunpklo_u16(src_vec_u8)));
388
+ svbool_t mask = svcmpne_u64(pg_64, src_vec_u64, ZERO_U64);
389
+ svst1_s64(pg_64, dst + i, svsel_s64(mask, ONE_S64, ZERO_S64));
390
+ }
391
+ }
392
+
393
+ template <>
394
+ inline void convert(const bool* src, int32_t* dst, int64_t n) {
395
+ const int64_t fraction = n % Vectorized<int32_t>::size();
396
+ svbool_t pg_8 = svwhilelt_b8(0ull, Vectorized<int32_t>::size());
397
+ svbool_t pg_32 = svwhilelt_b32(0ull, Vectorized<int32_t>::size());
398
+ #pragma unroll
399
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<int32_t>::size()) {
400
+ svuint8_t src_vec_u8 =
401
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
402
+ svuint32_t src_vec_u32 = svunpklo_u32(svunpklo_u16(src_vec_u8));
403
+ svbool_t mask = svcmpne_u32(pg_32, src_vec_u32, ZERO_U32);
404
+ svst1_s32(pg_32, dst + i, svsel_s32(mask, ONE_S32, ZERO_S32));
405
+ }
406
+ #pragma unroll
407
+ for (int64_t i = n - fraction; i < n; i += Vectorized<int32_t>::size()) {
408
+ pg_8 = svwhilelt_b8(i, n);
409
+ pg_32 = svwhilelt_b32(i, n);
410
+ svuint8_t src_vec_u8 =
411
+ svldnt1_u8(pg_8, reinterpret_cast<const uint8_t*>(src) + i);
412
+ svuint32_t src_vec_u32 = svunpklo_u32(svunpklo_u16(src_vec_u8));
413
+ svbool_t mask = svcmpne_u32(pg_32, src_vec_u32, ZERO_U32);
414
+ svst1_s32(pg_32, dst + i, svsel_s32(mask, ONE_S32, ZERO_S32));
415
+ }
416
+ }
417
+
418
+ template <>
419
+ inline void convert(const uint8_t* src, bool* dst, int64_t n) {
420
+ const int64_t fraction = n % Vectorized<uint8_t>::size();
421
+ svbool_t pg = svwhilelt_b8(0ull, Vectorized<uint8_t>::size());
422
+ #pragma unroll
423
+ for (int64_t i = 0; i < n - fraction; i += Vectorized<uint8_t>::size()) {
424
+ svbool_t mask = svcmpne_u8(pg, svldnt1_u8(pg, src + i), ZERO_U8);
425
+ svst1_u8(
426
+ pg,
427
+ reinterpret_cast<uint8_t*>(dst) + i,
428
+ svsel_u8(mask, ALL_U8_TRUE_MASK, ALL_U8_FALSE_MASK));
429
+ }
430
+ #pragma unroll
431
+ for (int64_t i = n - fraction; i < n; i += Vectorized<uint8_t>::size()) {
432
+ pg = svwhilelt_b8(i, n);
433
+ svbool_t mask = svcmpne_u8(pg, svldnt1_u8(pg, src + i), ZERO_U8);
434
+ svst1_u8(
435
+ pg,
436
+ reinterpret_cast<uint8_t*>(dst) + i,
437
+ svsel_u8(mask, ALL_U8_TRUE_MASK, ALL_U8_FALSE_MASK));
438
+ }
439
+ }
440
+
441
+ template <>
442
+ Vectorized<int64_t> inline operator<<(
443
+ const Vectorized<int64_t>& a,
444
+ const Vectorized<int64_t>& b) {
445
+ return svlsl_s64_x(ptrue, a, svreinterpret_u64_s64(b));
446
+ }
447
+
448
+ template <>
449
+ Vectorized<int32_t> inline operator<<(
450
+ const Vectorized<int32_t>& a,
451
+ const Vectorized<int32_t>& b) {
452
+ return svlsl_s32_x(ptrue, a, svreinterpret_u32_s32(b));
453
+ }
454
+
455
+ template <>
456
+ Vectorized<int16_t> inline operator<<(
457
+ const Vectorized<int16_t>& a,
458
+ const Vectorized<int16_t>& b) {
459
+ return svlsl_s16_x(ptrue, a, svreinterpret_u16_s16(b));
460
+ }
461
+
462
+ template <>
463
+ Vectorized<int8_t> inline operator<<(
464
+ const Vectorized<int8_t>& a,
465
+ const Vectorized<int8_t>& b) {
466
+ return svlsl_s8_x(ptrue, a, svreinterpret_u8_s8(b));
467
+ }
468
+
469
+ template <>
470
+ Vectorized<int64_t> inline operator>>(
471
+ const Vectorized<int64_t>& a,
472
+ const Vectorized<int64_t>& b) {
473
+ return svasr_s64_x(ptrue, a, svreinterpret_u64_s64(b));
474
+ }
475
+
476
+ template <>
477
+ Vectorized<int32_t> inline operator>>(
478
+ const Vectorized<int32_t>& a,
479
+ const Vectorized<int32_t>& b) {
480
+ return svasr_s32_x(ptrue, a, svreinterpret_u32_s32(b));
481
+ }
482
+
483
+ template <>
484
+ Vectorized<int16_t> inline operator>>(
485
+ const Vectorized<int16_t>& a,
486
+ const Vectorized<int16_t>& b) {
487
+ return svasr_s16_x(ptrue, a, svreinterpret_u16_s16(b));
488
+ }
489
+
490
+ template <>
491
+ Vectorized<int8_t> inline operator>>(
492
+ const Vectorized<int8_t>& a,
493
+ const Vectorized<int8_t>& b) {
494
+ return svasr_s8_x(ptrue, a, svreinterpret_u8_s8(b));
495
+ }
496
+
497
+ #endif // defined(CPU_CAPABILITY_SVE)
498
+
499
+ } // namespace CPU_CAPABILITY
500
+ } // namespace at::vec
501
+
502
+ #else
503
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
504
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/sve/vec_qint.h ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with SVE]
6
+
7
+ #include <ATen/cpu/vec/intrinsics.h>
8
+ #include <ATen/cpu/vec/vec_base.h>
9
+ #include <ATen/native/quantized/AffineQuantizerBase.h>
10
+ #include <c10/util/qint32.h>
11
+ #include <c10/util/qint8.h>
12
+ #include <c10/util/quint8.h>
13
+
14
+ #include <array>
15
+
16
+ // This file defines Vectorized<> for the quantized types.
17
+ //
18
+ //
19
+ // Currently, we simply use these classes as efficient converters between
20
+ // the quantized types and Vectorized<float>, usually in bandwidth-bound cases
21
+ // where doing the arithmetic in full-precision is acceptable (e.g.
22
+ // elementwise operators).
23
+ //
24
+ //
25
+ // Conversions are as follows:
26
+ // Vectorized<qint8> -> 4x Vectorized<float>
27
+ // Vectorized<quint8> -> 4x Vectorized<float>
28
+ // Vectorized<qint32> -> 1x Vectorized<float>
29
+ //
30
+ // The size of the returned float vector is specified by the special
31
+ // constexpr function float_num_vecs. The type of the value returned
32
+ // from dequantize (and expected as an argument to quantize) is
33
+ // specified by float_vec_return_type.
34
+ //
35
+ // When writing kernels with these vectors, it is expected that floating-
36
+ // point operations will be carried out in a loop over
37
+ // Vectorized<T>::float_num_vecs iterations.
38
+
39
+ namespace at::vec {
40
+ // Note [CPU_CAPABILITY namespace]
41
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
+ // This header, and all of its subheaders, will be compiled with
43
+ // different architecture flags for each supported set of vector
44
+ // intrinsics. So we need to make sure they aren't inadvertently
45
+ // linked together. We do this by declaring objects in an `inline
46
+ // namespace` which changes the name mangling, but can still be
47
+ // accessed as `at::vec`.
48
+ inline namespace CPU_CAPABILITY {
49
+
50
+ #if defined(CPU_CAPABILITY_SVE)
51
+
52
+ // NOTE: These are low-performance implementations that we fall back on
53
+ // if we are not building with SVE. This may not be an issue, because
54
+ // currently for quantization we assume the user has at least SVE
55
+ // installed, so these can simply act as a reference implementation.
56
+ //
57
+ // If in the future we relax this requirement (SVE+), we should probably
58
+ // revisit these implementations
59
+
60
+ template <
61
+ typename T,
62
+ typename float_vec_return_type_,
63
+ typename int_vec_return_type_,
64
+ int size_>
65
+ struct VectorizedQuantizedConverter {
66
+ using size_type = int;
67
+ static constexpr size_type size() {
68
+ return size_;
69
+ }
70
+
71
+ static constexpr int float_num_vecs() {
72
+ return size() / Vectorized<float>::size();
73
+ }
74
+
75
+ static constexpr int int_num_vecs() {
76
+ return size() / Vectorized<int32_t>::size();
77
+ }
78
+
79
+ using float_vec_return_type = float_vec_return_type_;
80
+ using int_vec_return_type = int_vec_return_type_;
81
+
82
+ using value_type = typename T::underlying;
83
+ std::array<value_type, size_> vals;
84
+
85
+ VectorizedQuantizedConverter(T val) {
86
+ for (size_t i = 0; i < size(); ++i) {
87
+ vals[i] = val.val_;
88
+ }
89
+ }
90
+
91
+ VectorizedQuantizedConverter(const void* ptr) {
92
+ memcpy(vals.data(), ptr, sizeof(value_type) * size());
93
+ }
94
+
95
+ void store(void* ptr, int count = size()) const {
96
+ memcpy(ptr, vals.data(), count * sizeof(value_type));
97
+ }
98
+
99
+ float_vec_return_type dequantize(
100
+ Vectorized<float> scale,
101
+ Vectorized<float> zero_point,
102
+ Vectorized<float> scale_zp_premul) const {
103
+ float_vec_return_type rv;
104
+ float tmp_scale[Vectorized<float>::size()];
105
+ float tmp_zero_point[Vectorized<float>::size()];
106
+ scale.store(tmp_scale);
107
+ zero_point.store(tmp_zero_point);
108
+ for (int i = 0; i < float_num_vecs(); ++i) {
109
+ float tmp_vals[Vectorized<float>::size()];
110
+ for (int j = 0; j < Vectorized<float>::size(); ++j) {
111
+ tmp_vals[j] = at::native::dequantize_val<T>(
112
+ tmp_scale[j],
113
+ tmp_zero_point[j],
114
+ T(vals[Vectorized<float>::size() * i + j]));
115
+ }
116
+ rv[i] = Vectorized<float>::loadu(tmp_vals);
117
+ }
118
+ return rv;
119
+ }
120
+
121
+ float_vec_return_type dequantize(
122
+ Vectorized<float> scale,
123
+ Vectorized<float> zero_point) const {
124
+ float_vec_return_type rv;
125
+ float tmp_scale[Vectorized<float>::size()];
126
+ float tmp_zero_point[Vectorized<float>::size()];
127
+ scale.store(tmp_scale);
128
+ zero_point.store(tmp_zero_point);
129
+ for (int i = 0; i < float_num_vecs(); ++i) {
130
+ float tmp_vals[Vectorized<float>::size()];
131
+ for (int j = 0; j < Vectorized<float>::size(); ++j) {
132
+ tmp_vals[j] = at::native::dequantize_val<T>(
133
+ tmp_scale[j],
134
+ tmp_zero_point[j],
135
+ T(vals[Vectorized<float>::size() * i + j]));
136
+ }
137
+ rv[i] = Vectorized<float>::loadu(tmp_vals);
138
+ }
139
+ return rv;
140
+ }
141
+
142
+ protected:
143
+ VectorizedQuantizedConverter() {}
144
+ };
145
+
146
+ template <>
147
+ struct is_vec_specialized_for<c10::qint32> : std::bool_constant<true> {};
148
+
149
+ template <>
150
+ struct Vectorized<c10::qint32> : public VectorizedQuantizedConverter<
151
+ c10::qint32,
152
+ std::array<Vectorized<float>, 1>,
153
+ std::array<Vectorized<c10::qint32>, 1>,
154
+ VECTOR_WIDTH / 4> {
155
+ Vectorized()
156
+ : VectorizedQuantizedConverter<
157
+ c10::qint32,
158
+ std::array<Vectorized<float>, 1>,
159
+ std::array<Vectorized<c10::qint32>, 1>,
160
+ VECTOR_WIDTH / 4>() {}
161
+ Vectorized(c10::qint32 val)
162
+ : VectorizedQuantizedConverter<
163
+ c10::qint32,
164
+ std::array<Vectorized<float>, 1>,
165
+ std::array<Vectorized<c10::qint32>, 1>,
166
+ VECTOR_WIDTH / 4>(val) {}
167
+ Vectorized(const void* ptr)
168
+ : VectorizedQuantizedConverter<
169
+ c10::qint32,
170
+ std::array<Vectorized<float>, 1>,
171
+ std::array<Vectorized<c10::qint32>, 1>,
172
+ VECTOR_WIDTH / 4>(ptr) {}
173
+ #if 1
174
+ static Vectorized<c10::qint32> loadu(const void* ptr) {
175
+ return Vectorized<c10::qint32>(ptr);
176
+ }
177
+
178
+ static Vectorized<c10::qint32> loadu(const void* ptr, int64_t count) {
179
+ __at_align__ value_type tmp_values[size()];
180
+ // Ensure uninitialized memory does not change the output value See
181
+ // https://github.com/pytorch/pytorch/issues/32502 for more details. We do
182
+ // not initialize arrays to zero using "={0}" because gcc would compile it
183
+ // to two instructions while a loop would be compiled to one instruction.
184
+ for (const auto i : c10::irange(size())) {
185
+ tmp_values[i] = 0;
186
+ }
187
+ std::memcpy(
188
+ tmp_values,
189
+ reinterpret_cast<const value_type*>(ptr),
190
+ count * sizeof(value_type));
191
+ return loadu(tmp_values);
192
+ }
193
+ #else
194
+ static Vectorized<c10::qint32> loadu(
195
+ const void* ptr,
196
+ int64_t count = size()) {
197
+ if (count == size())
198
+ return svld1_s32(ptrue, reinterpret_cast<const int32_t*>(ptr));
199
+ svbool_t pg = svwhilelt_b32(0ull, count);
200
+ return svld1_s32(pg, reinterpret_cast<const int32_t*>(ptr));
201
+ }
202
+ #endif
203
+ static Vectorized<c10::qint32> quantize(
204
+ const float_vec_return_type& rhs,
205
+ float scale,
206
+ int32_t zero_point,
207
+ float inverse_scale) {
208
+ std::array<value_type, size()> qvals;
209
+ std::array<float, float_num_vecs() * Vectorized<float>::size()> float_vals;
210
+
211
+ for (int i = 0; i < float_num_vecs(); ++i) {
212
+ rhs[i].store(
213
+ &float_vals[i * Vectorized<float>::size()],
214
+ Vectorized<float>::size());
215
+ }
216
+
217
+ at::native::quantize_vec<c10::qint32, /*precision=*/32>(
218
+ scale,
219
+ zero_point,
220
+ float_vals.data(),
221
+ (c10::qint32*)qvals.data(),
222
+ Vectorized<float>::size() * float_num_vecs());
223
+
224
+ return Vectorized<c10::qint32>::loadu(qvals.data());
225
+ }
226
+
227
+ Vectorized<c10::qint32> maximum(Vectorized<c10::qint32> b) const {
228
+ Vectorized<c10::qint32> retval;
229
+ for (size_t i = 0; i < size(); ++i) {
230
+ retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
231
+ }
232
+ return retval;
233
+ }
234
+
235
+ Vectorized<c10::qint32> minimum(Vectorized<c10::qint32> b) const {
236
+ Vectorized<c10::qint32> retval;
237
+ for (size_t i = 0; i < size(); ++i) {
238
+ retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
239
+ }
240
+ return retval;
241
+ }
242
+
243
+ Vectorized<c10::qint32> relu(Vectorized<c10::qint32> zero_point) const {
244
+ return maximum(zero_point);
245
+ }
246
+
247
+ Vectorized<c10::qint32> relu6(
248
+ Vectorized<c10::qint32> zero_point,
249
+ Vectorized<c10::qint32> q_six) {
250
+ Vectorized<c10::qint32> retval;
251
+ for (size_t i = 0; i < size(); ++i) {
252
+ retval.vals[i] = std::min<value_type>(
253
+ std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
254
+ }
255
+ return retval;
256
+ }
257
+
258
+ int_vec_return_type widening_subtract(Vectorized<c10::qint32> b) const {
259
+ int_vec_return_type retval;
260
+ for (size_t i = 0; i < size(); ++i) {
261
+ retval[0].vals[i] = vals[i] - b.vals[i];
262
+ }
263
+ return retval;
264
+ }
265
+
266
+ static Vectorized<c10::qint32> requantize_from_int(
267
+ const int_vec_return_type& inp,
268
+ float multiplier,
269
+ int32_t zero_point) {
270
+ Vectorized<c10::qint32> retval;
271
+ for (size_t i = 0; i < size(); ++i) {
272
+ retval.vals[i] =
273
+ nearbyint(static_cast<float>(inp[0].vals[i]) * multiplier) +
274
+ zero_point;
275
+ }
276
+ return retval;
277
+ }
278
+ };
279
+
280
+ template <>
281
+ Vectorized<c10::qint32> inline maximum(
282
+ const Vectorized<c10::qint32>& a,
283
+ const Vectorized<c10::qint32>& b) {
284
+ return a.maximum(b);
285
+ }
286
+
287
+ template <>
288
+ Vectorized<c10::qint32> inline operator*(
289
+ const Vectorized<c10::qint32>& a,
290
+ const Vectorized<c10::qint32>& b) {
291
+ Vectorized<c10::qint32> retval;
292
+ for (size_t i = 0; i < std::decay_t<decltype(a)>::size(); ++i) {
293
+ retval.vals[i] = a.vals[i] * b.vals[i];
294
+ }
295
+ return retval;
296
+ }
297
+
298
+ template <>
299
+ Vectorized<c10::qint32> inline operator+(
300
+ const Vectorized<c10::qint32>& a,
301
+ const Vectorized<c10::qint32>& b) {
302
+ Vectorized<c10::qint32> retval;
303
+ for (size_t i = 0; i < std::decay_t<decltype(a)>::size(); ++i) {
304
+ retval.vals[i] = a.vals[i] + b.vals[i];
305
+ }
306
+ return retval;
307
+ }
308
+
309
+ template <>
310
+ struct is_vec_specialized_for<c10::qint8> : std::bool_constant<true> {};
311
+
312
+ template <>
313
+ struct Vectorized<c10::qint8> : public VectorizedQuantizedConverter<
314
+ c10::qint8,
315
+ std::array<Vectorized<float>, 4>,
316
+ std::array<Vectorized<c10::qint32>, 4>,
317
+ VECTOR_WIDTH> {
318
+ Vectorized()
319
+ : VectorizedQuantizedConverter<
320
+ c10::qint8,
321
+ std::array<Vectorized<float>, 4>,
322
+ std::array<Vectorized<c10::qint32>, 4>,
323
+ VECTOR_WIDTH>() {}
324
+ Vectorized(c10::qint8 val)
325
+ : VectorizedQuantizedConverter<
326
+ c10::qint8,
327
+ std::array<Vectorized<float>, 4>,
328
+ std::array<Vectorized<c10::qint32>, 4>,
329
+ VECTOR_WIDTH>(val) {}
330
+ Vectorized(const void* ptr)
331
+ : VectorizedQuantizedConverter<
332
+ c10::qint8,
333
+ std::array<Vectorized<float>, 4>,
334
+ std::array<Vectorized<c10::qint32>, 4>,
335
+ VECTOR_WIDTH>(ptr) {}
336
+
337
+ static Vectorized<c10::qint8> loadu(const void* ptr) {
338
+ return Vectorized<c10::qint8>(ptr);
339
+ }
340
+
341
+ static Vectorized<c10::qint8> loadu(const void* ptr, int64_t count) {
342
+ __at_align__ value_type tmp_values[size()];
343
+ // Ensure uninitialized memory does not change the output value See
344
+ // https://github.com/pytorch/pytorch/issues/32502 for more details. We do
345
+ // not initialize arrays to zero using "={0}" because gcc would compile it
346
+ // to two instructions while a loop would be compiled to one instruction.
347
+ for (const auto i : c10::irange(size())) {
348
+ tmp_values[i] = 0;
349
+ }
350
+ std::memcpy(
351
+ tmp_values,
352
+ reinterpret_cast<const value_type*>(ptr),
353
+ count * sizeof(value_type));
354
+ return loadu(tmp_values);
355
+ }
356
+
357
+ static Vectorized<c10::qint8> quantize(
358
+ const float_vec_return_type& rhs,
359
+ float scale,
360
+ int32_t zero_point,
361
+ float inverse_scale) {
362
+ std::array<value_type, size()> qvals;
363
+ std::array<float, float_num_vecs() * Vectorized<float>::size()> float_vals;
364
+
365
+ for (int i = 0; i < float_num_vecs(); ++i) {
366
+ rhs[i].store(
367
+ &float_vals[i * Vectorized<float>::size()],
368
+ Vectorized<float>::size());
369
+ }
370
+
371
+ at::native::quantize_vec<c10::qint8>(
372
+ scale,
373
+ zero_point,
374
+ float_vals.data(),
375
+ (c10::qint8*)qvals.data(),
376
+ Vectorized<float>::size() * float_num_vecs());
377
+
378
+ return Vectorized<c10::qint8>::loadu(qvals.data());
379
+ }
380
+
381
+ Vectorized<c10::qint8> maximum(Vectorized<c10::qint8> b) const {
382
+ Vectorized<c10::qint8> retval;
383
+ for (size_t i = 0; i < size(); ++i) {
384
+ retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
385
+ }
386
+ return retval;
387
+ }
388
+
389
+ Vectorized<c10::qint8> minimum(Vectorized<c10::qint8> b) const {
390
+ Vectorized<c10::qint8> retval;
391
+ for (size_t i = 0; i < size(); ++i) {
392
+ retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
393
+ }
394
+ return retval;
395
+ }
396
+
397
+ Vectorized<c10::qint8> relu(Vectorized<c10::qint8> zero_point) const {
398
+ return maximum(zero_point);
399
+ }
400
+
401
+ Vectorized<c10::qint8> relu6(
402
+ Vectorized<c10::qint8> zero_point,
403
+ Vectorized<c10::qint8> q_six) {
404
+ Vectorized<c10::qint8> retval;
405
+ for (size_t i = 0; i < size(); ++i) {
406
+ retval.vals[i] = std::min<value_type>(
407
+ std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
408
+ }
409
+ return retval;
410
+ }
411
+
412
+ int_vec_return_type widening_subtract(Vectorized<c10::qint8> b) const {
413
+ int_vec_return_type retval;
414
+ constexpr int elem_per_int_vec = size() / int_num_vecs();
415
+ for (size_t i = 0; i < int_num_vecs(); ++i) {
416
+ for (size_t j = 0; j < elem_per_int_vec; ++j) {
417
+ retval[i].vals[j] =
418
+ static_cast<int32_t>(vals[i * elem_per_int_vec + j]) -
419
+ static_cast<int32_t>(b.vals[i * elem_per_int_vec + j]);
420
+ }
421
+ }
422
+ return retval;
423
+ }
424
+ static Vectorized<c10::qint8> requantize_from_int(
425
+ const int_vec_return_type& inp,
426
+ float multiplier,
427
+ int32_t zero_point) {
428
+ constexpr int elem_per_int_vec = size() / int_num_vecs();
429
+ constexpr auto min_val = std::numeric_limits<value_type>::min();
430
+ constexpr auto max_val = std::numeric_limits<value_type>::max();
431
+ Vectorized<c10::qint8> retval;
432
+ for (size_t i = 0; i < int_num_vecs(); ++i) {
433
+ for (size_t j = 0; j < elem_per_int_vec; ++j) {
434
+ int32_t rounded =
435
+ nearbyint(static_cast<float>(inp[i].vals[j]) * multiplier) +
436
+ zero_point;
437
+ retval.vals[i * elem_per_int_vec + j] =
438
+ std::min<int32_t>(std::max<int32_t>(rounded, min_val), max_val);
439
+ }
440
+ }
441
+ return retval;
442
+ }
443
+ };
444
+
445
+ template <>
446
+ Vectorized<c10::qint8> inline maximum(
447
+ const Vectorized<c10::qint8>& a,
448
+ const Vectorized<c10::qint8>& b) {
449
+ return a.maximum(b);
450
+ }
451
+
452
+ template <>
453
+ struct is_vec_specialized_for<c10::quint8> : std::bool_constant<true> {};
454
+
455
+ template <>
456
+ struct Vectorized<c10::quint8> : public VectorizedQuantizedConverter<
457
+ c10::quint8,
458
+ std::array<Vectorized<float>, 4>,
459
+ std::array<Vectorized<c10::qint32>, 4>,
460
+ VECTOR_WIDTH> {
461
+ Vectorized()
462
+ : VectorizedQuantizedConverter<
463
+ c10::quint8,
464
+ std::array<Vectorized<float>, 4>,
465
+ std::array<Vectorized<c10::qint32>, 4>,
466
+ VECTOR_WIDTH>() {}
467
+ Vectorized(c10::quint8 val)
468
+ : VectorizedQuantizedConverter<
469
+ c10::quint8,
470
+ std::array<Vectorized<float>, 4>,
471
+ std::array<Vectorized<c10::qint32>, 4>,
472
+ VECTOR_WIDTH>(val) {}
473
+ Vectorized(const void* ptr)
474
+ : VectorizedQuantizedConverter<
475
+ c10::quint8,
476
+ std::array<Vectorized<float>, 4>,
477
+ std::array<Vectorized<c10::qint32>, 4>,
478
+ VECTOR_WIDTH>(ptr) {}
479
+ #if 1
480
+ static Vectorized<c10::quint8> loadu(const void* ptr) {
481
+ return Vectorized<c10::quint8>(ptr);
482
+ }
483
+
484
+ static Vectorized<c10::quint8> loadu(const void* ptr, int64_t count) {
485
+ __at_align__ value_type tmp_values[size()];
486
+ // Ensure uninitialized memory does not change the output value See
487
+ // https://github.com/pytorch/pytorch/issues/32502 for more details. We do
488
+ // not initialize arrays to zero using "={0}" because gcc would compile it
489
+ // to two instructions while a loop would be compiled to one instruction.
490
+ for (const auto i : c10::irange(size())) {
491
+ tmp_values[i] = 0;
492
+ }
493
+ std::memcpy(
494
+ tmp_values,
495
+ reinterpret_cast<const value_type*>(ptr),
496
+ count * sizeof(value_type));
497
+ return loadu(tmp_values);
498
+ }
499
+ #else
500
+ static Vectorized<c10::quint8> loadu(
501
+ const void* ptr,
502
+ int64_t count = size()) {
503
+ if (count == size())
504
+ return svld1_u8(ptrue, reinterpret_cast<const uint8_t*>(ptr));
505
+ svbool_t pg = svwhilelt_b8(0ull, count);
506
+ return svld1_u8(pg, reinterpret_cast<const uint8_t*>(ptr));
507
+ }
508
+ #endif
509
+ static Vectorized<c10::quint8> quantize(
510
+ const float_vec_return_type& rhs,
511
+ float scale,
512
+ int32_t zero_point,
513
+ float inverse_scale) {
514
+ std::array<value_type, size()> qvals;
515
+ std::array<float, float_num_vecs() * Vectorized<float>::size()> float_vals;
516
+
517
+ for (int i = 0; i < float_num_vecs(); ++i) {
518
+ rhs[i].store(
519
+ &float_vals[i * Vectorized<float>::size()],
520
+ Vectorized<float>::size());
521
+ }
522
+
523
+ at::native::quantize_vec<c10::quint8>(
524
+ scale,
525
+ zero_point,
526
+ float_vals.data(),
527
+ (c10::quint8*)qvals.data(),
528
+ Vectorized<float>::size() * float_num_vecs());
529
+
530
+ return Vectorized<c10::quint8>::loadu(qvals.data());
531
+ }
532
+
533
+ Vectorized<c10::quint8> maximum(Vectorized<c10::quint8> b) const {
534
+ Vectorized<c10::quint8> retval;
535
+ for (size_t i = 0; i < size(); ++i) {
536
+ retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
537
+ }
538
+ return retval;
539
+ }
540
+
541
+ Vectorized<c10::quint8> minimum(Vectorized<c10::quint8> b) const {
542
+ Vectorized<c10::quint8> retval;
543
+ for (size_t i = 0; i < size(); ++i) {
544
+ retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
545
+ }
546
+ return retval;
547
+ }
548
+
549
+ Vectorized<c10::quint8> relu(Vectorized<c10::quint8> zero_point) const {
550
+ return maximum(zero_point);
551
+ }
552
+
553
+ Vectorized<c10::quint8> relu6(
554
+ Vectorized<c10::quint8> zero_point,
555
+ Vectorized<c10::quint8> q_six) {
556
+ Vectorized<c10::quint8> retval;
557
+ for (size_t i = 0; i < size(); ++i) {
558
+ retval.vals[i] = std::min<value_type>(
559
+ std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
560
+ }
561
+ return retval;
562
+ }
563
+
564
+ int_vec_return_type widening_subtract(Vectorized<c10::quint8> b) const {
565
+ int_vec_return_type retval;
566
+ constexpr int elem_per_int_vec = size() / int_num_vecs();
567
+ for (size_t i = 0; i < int_num_vecs(); ++i) {
568
+ for (size_t j = 0; j < elem_per_int_vec; ++j) {
569
+ retval[i].vals[j] =
570
+ static_cast<int32_t>(vals[i * elem_per_int_vec + j]) -
571
+ static_cast<int32_t>(b.vals[i * elem_per_int_vec + j]);
572
+ }
573
+ }
574
+ return retval;
575
+ }
576
+ static Vectorized<c10::quint8> requantize_from_int(
577
+ const int_vec_return_type& inp,
578
+ float multiplier,
579
+ int32_t zero_point) {
580
+ constexpr int elem_per_int_vec = size() / int_num_vecs();
581
+ constexpr auto min_val = std::numeric_limits<value_type>::min();
582
+ constexpr auto max_val = std::numeric_limits<value_type>::max();
583
+ Vectorized<c10::quint8> retval;
584
+ for (size_t i = 0; i < int_num_vecs(); ++i) {
585
+ for (size_t j = 0; j < elem_per_int_vec; ++j) {
586
+ int32_t rounded =
587
+ nearbyint(static_cast<float>(inp[i].vals[j]) * multiplier) +
588
+ zero_point;
589
+ retval.vals[i * elem_per_int_vec + j] =
590
+ std::min<int32_t>(std::max<int32_t>(rounded, min_val), max_val);
591
+ }
592
+ }
593
+ return retval;
594
+ }
595
+ };
596
+
597
+ template <>
598
+ Vectorized<c10::quint8> inline maximum(
599
+ const Vectorized<c10::quint8>& a,
600
+ const Vectorized<c10::quint8>& b) {
601
+ return a.maximum(b);
602
+ }
603
+
604
+ #endif // defined(CPU_CAPABILITY_SVE)
605
+
606
+ } // namespace CPU_CAPABILITY
607
+ } // namespace at::vec
608
+
609
+ #else
610
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
611
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec.h ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #if defined(CPU_CAPABILITY_AVX512)
5
+ #include <ATen/cpu/vec/vec512/vec512.h>
6
+ #else
7
+ #include <ATen/cpu/vec/vec128/vec128.h>
8
+ #include <ATen/cpu/vec/vec256/vec256.h>
9
+ #endif
10
+
11
+ namespace at::vec {
12
+ // See Note [CPU_CAPABILITY namespace]
13
+ inline namespace CPU_CAPABILITY {
14
+
15
+ inline Vectorized<bool> convert_to_bool(Vectorized<int8_t> x) {
16
+ __at_align__ bool buffer[x.size()];
17
+ x.ne(Vectorized<int8_t>(0)).store(buffer);
18
+
19
+ Vectorized<bool> ret;
20
+ static_assert(x.size() == ret.size());
21
+ std::memcpy(ret, buffer, ret.size() * sizeof(bool));
22
+ return ret;
23
+ }
24
+
25
+ template <>
26
+ inline Vectorized<bool> Vectorized<bool>::loadu(const void* ptr) {
27
+ // See NOTE [Loading boolean values]
28
+ return convert_to_bool(Vectorized<int8_t>::loadu(ptr));
29
+ }
30
+
31
+ template <>
32
+ inline Vectorized<bool> Vectorized<bool>::loadu(
33
+ const void* ptr,
34
+ int64_t count) {
35
+ // See NOTE [Loading boolean values]
36
+ return convert_to_bool(Vectorized<int8_t>::loadu(ptr, count));
37
+ }
38
+
39
+ template <typename VT>
40
+ struct VecHoldType {
41
+ using hold_type = typename VT::value_type;
42
+ };
43
+
44
+ template <>
45
+ struct VecHoldType<Vectorized<BFloat16>> {
46
+ using hold_type = BFloat16;
47
+ };
48
+
49
+ template <>
50
+ struct VecHoldType<Vectorized<Half>> {
51
+ using hold_type = Half;
52
+ };
53
+
54
+ template <typename VT>
55
+ using vechold_type = typename VecHoldType<VT>::hold_type;
56
+
57
+ } // namespace CPU_CAPABILITY
58
+ } // namespace at::vec
59
+
60
+ #else
61
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
62
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128.h ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ // ARM NEON uses 128-bit vector registers.
4
+
5
+ #include <ATen/cpu/vec/intrinsics.h>
6
+
7
+ #ifdef __aarch64__
8
+ #if !defined(CPU_CAPABILITY_SVE)
9
+ #include <ATen/cpu/vec/vec128/vec128_bfloat16_neon.h>
10
+ #include <ATen/cpu/vec/vec128/vec128_double_neon.h>
11
+ #include <ATen/cpu/vec/vec128/vec128_float_neon.h>
12
+ #include <ATen/cpu/vec/vec128/vec128_half_neon.h>
13
+ #include <ATen/cpu/vec/vec128/vec128_int_aarch64.h>
14
+ #include <ATen/cpu/vec/vec128/vec128_uint_aarch64.h>
15
+ #endif
16
+
17
+ #include <ATen/cpu/vec/vec128/vec128_convert.h>
18
+ #endif
19
+
20
+ #else
21
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
22
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_bfloat16_neon.h ADDED
@@ -0,0 +1,703 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with AVX]
6
+ #include <ATen/cpu/vec/vec128/vec128_float_neon.h>
7
+ #include <ATen/cpu/vec/vec128/vec128_reduced_precision_common_neon.h>
8
+ #include <ATen/cpu/vec/vec_base.h>
9
+ #include <c10/util/BFloat16.h>
10
+ #include <c10/util/bit_cast.h>
11
+ #include <c10/util/irange.h>
12
+
13
+ namespace at::vec {
14
+ // See Note [CPU_CAPABILITY namespace]
15
+ inline namespace CPU_CAPABILITY {
16
+
17
+ // Following vec128_half_neon.h, we only support aarch64.
18
+ #if !defined(C10_MOBILE) && defined(__aarch64__)
19
+ #ifdef __BIG_ENDIAN__
20
+ #error "Big endian is not supported."
21
+ #endif
22
+
23
+ // GCC does not properly optimize bf16 operators
24
+ #if defined(__ARM_FEATURE_BF16) && (__clang_major__ >= 19)
25
+ #define BF16_ARITHMETIC_SUPPORTED() 1
26
+ #else
27
+ #define BF16_ARITHMETIC_SUPPORTED() 0
28
+ #endif
29
+
30
+ // Unlike the float16_t family of types, bfloat16_t is not available
31
+ // when we're not targeting bfloat16 hardware support on some
32
+ // platforms (but not Mac, so we have to be careful not to shadow the
33
+ // definitions in case they are actually there!). (See
34
+ // https://godbolt.org/z/orv6e94n4 ) So, we need to handle it as
35
+ // uint16_t in that case.
36
+ #define IMPLEMENT_AT_BF16_SHIM(vec_suffix) \
37
+ inline at_bfloat16x4_t at_vget_low_bf16(at_bfloat16x8_t a) { \
38
+ return vget_low_##vec_suffix(a); \
39
+ } \
40
+ \
41
+ inline at_bfloat16x4_t at_vget_high_bf16(at_bfloat16x8_t a) { \
42
+ return vget_high_##vec_suffix(a); \
43
+ } \
44
+ \
45
+ inline at_bfloat16x8_t at_vcombine_bf16( \
46
+ at_bfloat16x4_t low, at_bfloat16x4_t high) { \
47
+ return vcombine_##vec_suffix(low, high); \
48
+ } \
49
+ \
50
+ inline at_bfloat16x8_t at_vdupq_n_bf16(at_bfloat16_t value) { \
51
+ return vdupq_n_##vec_suffix(value); \
52
+ } \
53
+ \
54
+ inline at_bfloat16x8_t at_vld1q_bf16(const at_bfloat16_t* ptr) { \
55
+ return vld1q_##vec_suffix(ptr); \
56
+ } \
57
+ \
58
+ inline void at_vst1q_bf16(at_bfloat16_t* ptr, at_bfloat16x8_t value) { \
59
+ vst1q_##vec_suffix(ptr, value); \
60
+ } \
61
+ \
62
+ template <typename T> \
63
+ inline at_bfloat16x8_t at_vreinterpretq_bf16_u16(T val) { \
64
+ if constexpr (std::is_same_v<at_bfloat16x8_t, uint16x8_t>) { \
65
+ return val; \
66
+ } else { \
67
+ return vreinterpretq_bf16_u16(val); \
68
+ } \
69
+ } \
70
+ template <typename T> \
71
+ inline at_bfloat16x4_t at_vreinterpret_bf16_u16(T val) { \
72
+ if constexpr (std::is_same_v<at_bfloat16x4_t, uint16x4_t>) { \
73
+ return val; \
74
+ } else { \
75
+ return vreinterpret_bf16_u16(val); \
76
+ } \
77
+ } \
78
+ template <typename T> \
79
+ inline uint16x8_t at_vreinterpretq_u16_bf16(T val) { \
80
+ if constexpr (std::is_same_v<at_bfloat16x8_t, uint16x8_t>) { \
81
+ return val; \
82
+ } else { \
83
+ return vreinterpretq_u16_bf16(val); \
84
+ } \
85
+ } \
86
+ template <typename T> \
87
+ inline uint16x4_t at_vreinterpret_u16_bf16(T val) { \
88
+ if constexpr (std::is_same_v<at_bfloat16x4_t, uint16x4_t>) { \
89
+ return val; \
90
+ } else { \
91
+ return vreinterpret_u16_bf16(val); \
92
+ } \
93
+ }
94
+
95
+ #ifdef __ARM_FEATURE_BF16
96
+ using at_bfloat16x8_t = bfloat16x8_t;
97
+ using at_bfloat16x4_t = bfloat16x4_t;
98
+ using at_bfloat16_t = bfloat16_t;
99
+ IMPLEMENT_AT_BF16_SHIM(bf16)
100
+ #define at_vsetq_lane_bf16 vsetq_lane_bf16
101
+ #define at_vgetq_lane_bf16 vgetq_lane_bf16
102
+ #else
103
+ using at_bfloat16x8_t = uint16x8_t;
104
+ using at_bfloat16x4_t = uint16x4_t;
105
+ using at_bfloat16_t = uint16_t;
106
+ IMPLEMENT_AT_BF16_SHIM(u16)
107
+ #define at_vsetq_lane_bf16 vsetq_lane_u16
108
+ #define at_vgetq_lane_bf16 vgetq_lane_u16
109
+ #endif // __ARM_FEATURE_BF16
110
+
111
+ template <int index, bool mask_val>
112
+ struct BlendBFloat16Regs {
113
+ static at_bfloat16x8_t impl(
114
+ const at_bfloat16x8_t& a,
115
+ const at_bfloat16x8_t& b,
116
+ at_bfloat16x8_t& res);
117
+ };
118
+
119
+ template <int index>
120
+ struct BlendBFloat16Regs<index, true> {
121
+ static at_bfloat16x8_t impl(
122
+ const at_bfloat16x8_t& a,
123
+ const at_bfloat16x8_t& b,
124
+ at_bfloat16x8_t& res) {
125
+ return at_vsetq_lane_bf16(at_vgetq_lane_bf16(b, index), res, index);
126
+ }
127
+ };
128
+
129
+ template <int index>
130
+ struct BlendBFloat16Regs<index, false> {
131
+ static at_bfloat16x8_t impl(
132
+ const at_bfloat16x8_t& a,
133
+ const at_bfloat16x8_t& b,
134
+ at_bfloat16x8_t& res) {
135
+ return at_vsetq_lane_bf16(at_vgetq_lane_bf16(a, index), res, index);
136
+ }
137
+ };
138
+
139
+ template <>
140
+ struct is_vec_specialized_for<c10::BFloat16> : std::bool_constant<true> {};
141
+
142
+ template <>
143
+ class Vectorized<c10::BFloat16> : public Vectorized16<
144
+ at_bfloat16x8_t,
145
+ c10::BFloat16,
146
+ BlendBFloat16Regs,
147
+ Vectorized<c10::BFloat16>> {
148
+ using Base = Vectorized16<
149
+ at_bfloat16x8_t,
150
+ c10::BFloat16,
151
+ BlendBFloat16Regs,
152
+ Vectorized<c10::BFloat16>>;
153
+ friend Base;
154
+ friend std::tuple<Vectorized<float>, Vectorized<float>> convert_bfloat16_float(
155
+ const Vectorized<c10::BFloat16>& a);
156
+ friend Vectorized<c10::BFloat16> convert_float_bfloat16(
157
+ const Vectorized<float>& a,
158
+ const Vectorized<float>& b);
159
+
160
+ private:
161
+ Vectorized<c10::BFloat16> map2(
162
+ const Vectorized<c10::BFloat16>& second,
163
+ c10::BFloat16 (*const f)(c10::BFloat16, c10::BFloat16)) const {
164
+ __at_align__ c10::BFloat16 tmp_first[size()];
165
+ __at_align__ c10::BFloat16 tmp_second[size()];
166
+ store(tmp_first); // store this to tmp_first
167
+ second.store(tmp_second);
168
+ for (const auto i : c10::irange(size())) {
169
+ tmp_first[i] = f(tmp_first[i], tmp_second[i]);
170
+ }
171
+ return loadu(tmp_first);
172
+ }
173
+
174
+ static float32x4_t convert_f32_bf16(at_bfloat16x4_t bf16) {
175
+ #ifdef __ARM_FEATURE_BF16
176
+ return vcvt_f32_bf16(bf16);
177
+ #else
178
+ int32x4_t shift = vdupq_n_s32(16);
179
+ return vreinterpretq_f32_u32(vshlq_u32(vmovl_u16(bf16), shift));
180
+ #endif // __ARM_FEATURE_BF16
181
+ }
182
+
183
+ static at_bfloat16x4_t convert_bf16_f32(const Vectorized<float>& f32) {
184
+ #ifdef __ARM_FEATURE_BF16
185
+ return vcvt_bf16_f32(f32);
186
+ #else
187
+ static_assert(std::is_same_v<uint16x4_t, at_bfloat16x4_t>);
188
+ uint32x4_t as_uint32 = vreinterpretq_u32_f32(f32);
189
+ uint32x4_t rounding_bias = vaddq_u32(
190
+ vandq_u32(vshrq_n_u32(as_uint32, 16), vdupq_n_u32(1)),
191
+ vdupq_n_u32(0x7FFF));
192
+ at_bfloat16x4_t rounded =
193
+ vshrn_n_u32(vaddq_u32(as_uint32, rounding_bias), 16);
194
+ const auto bf16_nan = vdup_n_u16(0x7FC0);
195
+ return vbsl_u16(
196
+ vmovn_u32(vreinterpretq_u32_f32(f32.isnan())), bf16_nan, rounded);
197
+ #endif // __ARM_FEATURE_BF16
198
+ }
199
+
200
+ Vectorized<c10::BFloat16> map_with_vec_float_method(
201
+ Vectorized<float> (Vectorized<float>::*m)() const) const {
202
+ float32x4_t v00 = convert_f32_bf16(at_vget_low_bf16(values));
203
+ float32x4_t v01 = convert_f32_bf16(at_vget_high_bf16(values));
204
+ Vectorized<float> mv0 = (Vectorized<float>(v00).*m)();
205
+ Vectorized<float> mv1 = (Vectorized<float>(v01).*m)();
206
+ at_bfloat16x4_t r00 = convert_bf16_f32(mv0);
207
+ at_bfloat16x4_t r01 = convert_bf16_f32(mv1);
208
+ return Vectorized<c10::BFloat16>(at_vcombine_bf16(r00, r01));
209
+ }
210
+
211
+ Vectorized<c10::BFloat16> map2_with_vec_float_method(
212
+ const Vectorized<c10::BFloat16>& second,
213
+ Vectorized<float> (Vectorized<float>::*m)(const Vectorized<float>&)
214
+ const) const {
215
+ float32x4_t v00 = convert_f32_bf16(at_vget_low_bf16(values));
216
+ float32x4_t v01 = convert_f32_bf16(at_vget_high_bf16(values));
217
+ float32x4_t second_v00 = convert_f32_bf16(at_vget_low_bf16(second.values));
218
+ float32x4_t second_v01 = convert_f32_bf16(at_vget_high_bf16(second.values));
219
+ Vectorized<float> mv0 = (Vectorized<float>(v00).*m)(second_v00);
220
+ Vectorized<float> mv1 = (Vectorized<float>(v01).*m)(second_v01);
221
+ at_bfloat16x4_t r00 = convert_bf16_f32(mv0);
222
+ at_bfloat16x4_t r01 = convert_bf16_f32(mv1);
223
+ return Vectorized<c10::BFloat16>(at_vcombine_bf16(r00, r01));
224
+ }
225
+
226
+ Vectorized<c10::BFloat16> map2_bitmask_with_vec_float_method(
227
+ const Vectorized<c10::BFloat16>& second,
228
+ Vectorized<float> (Vectorized<float>::*m)(const Vectorized<float>&)
229
+ const) const {
230
+ float32x4_t v00 = convert_f32_bf16(at_vget_low_bf16(values));
231
+ float32x4_t v01 = convert_f32_bf16(at_vget_high_bf16(values));
232
+ float32x4_t second_v00 = convert_f32_bf16(at_vget_low_bf16(second.values));
233
+ float32x4_t second_v01 = convert_f32_bf16(at_vget_high_bf16(second.values));
234
+ Vectorized<float> mv0 = (Vectorized<float>(v00).*m)(second_v00);
235
+ Vectorized<float> mv1 = (Vectorized<float>(v01).*m)(second_v01);
236
+ // Assume the operator returns a bitmask, not "real" floats, and
237
+ // just narrow the bits. All-ones is a NaN and will get mangled by
238
+ // conversion!
239
+ at_bfloat16x4_t r00 =
240
+ at_vreinterpret_bf16_u16(vmovn_u32(vreinterpretq_u32_f32(mv0)));
241
+ at_bfloat16x4_t r01 =
242
+ at_vreinterpret_bf16_u16(vmovn_u32(vreinterpretq_u32_f32(mv1)));
243
+ return Vectorized<c10::BFloat16>(at_vcombine_bf16(r00, r01));
244
+ }
245
+
246
+ public:
247
+ using Vectorized16::Vectorized16;
248
+
249
+ Vectorized() = default;
250
+
251
+ Vectorized(c10::BFloat16 val)
252
+ : Vectorized16(at_vdupq_n_bf16(c10::bit_cast<at_bfloat16_t>(val.x))) {}
253
+ Vectorized(float val) : Vectorized(c10::BFloat16(val)) {}
254
+ Vectorized(
255
+ value_type val0,
256
+ value_type val1,
257
+ value_type val2,
258
+ value_type val3,
259
+ value_type val4,
260
+ value_type val5,
261
+ value_type val6,
262
+ value_type val7)
263
+ : Vectorized16(at_bfloat16x8_t{
264
+ c10::bit_cast<at_bfloat16_t>(val0.x),
265
+ c10::bit_cast<at_bfloat16_t>(val1.x),
266
+ c10::bit_cast<at_bfloat16_t>(val2.x),
267
+ c10::bit_cast<at_bfloat16_t>(val3.x),
268
+ c10::bit_cast<at_bfloat16_t>(val4.x),
269
+ c10::bit_cast<at_bfloat16_t>(val5.x),
270
+ c10::bit_cast<at_bfloat16_t>(val6.x),
271
+ c10::bit_cast<at_bfloat16_t>(val7.x)}) {}
272
+
273
+ static Vectorized<c10::BFloat16> blendv(
274
+ const Vectorized<c10::BFloat16>& a,
275
+ const Vectorized<c10::BFloat16>& b,
276
+ const Vectorized<c10::BFloat16>& mask) {
277
+ // NOTE: blendv has the same problems as it does for Half; see comments in
278
+ // vec128_half_neon.h.
279
+ Vectorized<c10::BFloat16> vec(mask.values);
280
+ vec.values = at_vreinterpretq_bf16_u16(vbslq_u16(
281
+ at_vreinterpretq_u16_bf16(vec.values),
282
+ at_vreinterpretq_u16_bf16(b.values),
283
+ at_vreinterpretq_u16_bf16(a.values)));
284
+ return vec;
285
+ }
286
+ static Vectorized<c10::BFloat16> set(
287
+ const Vectorized<c10::BFloat16>& a,
288
+ const Vectorized<c10::BFloat16>& b,
289
+ int64_t count = size()) {
290
+ uint16_t pre_mask[size()] = {0};
291
+ for (int i = 0; i < count; i++) {
292
+ pre_mask[i] = 0xFFFF;
293
+ }
294
+ uint16x8_t mask = vld1q_u16(pre_mask);
295
+
296
+ Vectorized<c10::BFloat16> vec(at_vreinterpretq_bf16_u16(vbslq_u16(
297
+ mask,
298
+ at_vreinterpretq_u16_bf16(b.values),
299
+ at_vreinterpretq_u16_bf16(a.values))));
300
+
301
+ return vec;
302
+ }
303
+ static Vectorized<c10::BFloat16> loadu(
304
+ const void* ptr,
305
+ int64_t count = size()) {
306
+ if (count == size()) {
307
+ return at_vld1q_bf16(reinterpret_cast<const at_bfloat16_t*>(ptr));
308
+ }
309
+ __at_align__ at_bfloat16_t tmp_values[size()];
310
+ std::memset(tmp_values, 0, sizeof(tmp_values));
311
+ std::memcpy(
312
+ tmp_values,
313
+ reinterpret_cast<const at_bfloat16_t*>(ptr),
314
+ count * sizeof(at_bfloat16_t));
315
+ return at_vld1q_bf16(reinterpret_cast<const at_bfloat16_t*>(tmp_values));
316
+ }
317
+ void store(void* ptr, int64_t count = size()) const {
318
+ if (count == size()) {
319
+ at_vst1q_bf16(reinterpret_cast<at_bfloat16_t*>(ptr), values);
320
+ return;
321
+ } else {
322
+ at_bfloat16_t tmp_values[size()];
323
+ at_vst1q_bf16(reinterpret_cast<at_bfloat16_t*>(tmp_values), values);
324
+ std::memcpy(ptr, tmp_values, count * sizeof(at_bfloat16_t));
325
+ }
326
+ }
327
+ Vectorized<c10::BFloat16> isnan() const {
328
+ // NOTE: we could make this faster by doing vectorized checks of
329
+ // exponent/payload bits.
330
+ __at_align__ c10::BFloat16 tmp[size()];
331
+ __at_align__ c10::BFloat16 res[size()];
332
+ store(tmp);
333
+ for (const auto i : c10::irange(size())) {
334
+ if (_isnan(tmp[i])) {
335
+ std::memset(static_cast<void*>(&res[i]), 0xFF, sizeof(c10::BFloat16));
336
+ } else {
337
+ std::memset(static_cast<void*>(&res[i]), 0, sizeof(c10::BFloat16));
338
+ }
339
+ }
340
+ return loadu(res);
341
+ }
342
+ bool has_inf_nan() const {
343
+ __at_align__ c10::BFloat16 tmp[size()];
344
+ store(tmp);
345
+ for (const auto i : c10::irange(size())) {
346
+ if (_isnan(tmp[i]) || _isinf(tmp[i])) {
347
+ return true;
348
+ }
349
+ }
350
+ return false;
351
+ }
352
+ #define DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(name) \
353
+ Vectorized name() const { \
354
+ return map_with_vec_float_method(&Vectorized<float>::name); \
355
+ }
356
+
357
+ #define DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(name) \
358
+ Vectorized name(const Vectorized& other) const { \
359
+ return map2_bitmask_with_vec_float_method( \
360
+ other, &Vectorized<float>::name); \
361
+ }
362
+
363
+ Vectorized frac() const;
364
+ DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(trunc)
365
+ DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(sqrt)
366
+
367
+ #ifdef __ARM_FEATURE_BF16
368
+ // Flip sign bit
369
+ Vectorized<c10::BFloat16> neg() const {
370
+ return vreinterpretq_bf16_s16(vreinterpretq_s16_bf16(values) ^ (-32768));
371
+ }
372
+ // Fast reciprocal is fine because we are truncating results
373
+ Vectorized<c10::BFloat16> reciprocal() const {
374
+ auto x = vcvtq_low_f32_bf16(values);
375
+ auto y = vcvtq_high_f32_bf16(values);
376
+ x = vrecpeq_f32(x);
377
+ y = vrecpeq_f32(y);
378
+ return vcvtq_high_bf16_f32(vcvtq_low_bf16_f32(x), y);
379
+ }
380
+ // Clearing the sign bit
381
+ Vectorized<c10::BFloat16> abs() const {
382
+ return vreinterpretq_bf16_u16(vreinterpretq_u16_bf16(values) & 0x7FFF);
383
+ }
384
+ #else
385
+ DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(abs)
386
+ DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(neg)
387
+ DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD(reciprocal)
388
+ #endif
389
+
390
+ // These functions are optimized on clang-21+
391
+ #if BF16_ARITHMETIC_SUPPORTED() && (__clang_major__ >= 21)
392
+ Vectorized<c10::BFloat16> operator==(
393
+ const Vectorized<c10::BFloat16>& other) const {
394
+ return values == other.values;
395
+ }
396
+
397
+ Vectorized<c10::BFloat16> operator!=(
398
+ const Vectorized<c10::BFloat16>& other) const {
399
+ return values != other.values;
400
+ }
401
+
402
+ Vectorized<c10::BFloat16> operator<(
403
+ const Vectorized<c10::BFloat16>& other) const {
404
+ return values < other.values;
405
+ }
406
+
407
+ Vectorized<c10::BFloat16> operator<=(
408
+ const Vectorized<c10::BFloat16>& other) const {
409
+ return values <= other.values;
410
+ }
411
+
412
+ Vectorized<c10::BFloat16> operator>(
413
+ const Vectorized<c10::BFloat16>& other) const {
414
+ return values > other.values;
415
+ }
416
+
417
+ Vectorized<c10::BFloat16> operator>=(
418
+ const Vectorized<c10::BFloat16>& other) const {
419
+ return values >= other.values;
420
+ }
421
+ #else
422
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator==)
423
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator!=)
424
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator<)
425
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator<=)
426
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator>)
427
+ DEFINE_BINARY_COMPARISON_OPERATOR_VIA_FLOAT_METHOD(operator>=)
428
+ #endif
429
+
430
+ #undef DEFINE_UNARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD
431
+ #undef DEFINE_BINARY_ELEMENTWISE_FUNC_VIA_FLOAT_METHOD
432
+
433
+ Vectorized eq(const Vectorized& other) const;
434
+ Vectorized ne(const Vectorized& other) const;
435
+ Vectorized gt(const Vectorized& other) const;
436
+ Vectorized ge(const Vectorized& other) const;
437
+ Vectorized lt(const Vectorized& other) const;
438
+ Vectorized le(const Vectorized& other) const;
439
+ }; // Vectorized<c10::BFloat16>
440
+
441
+ inline std::tuple<Vectorized<float>, Vectorized<float>> convert_bfloat16_float(
442
+ const Vectorized<c10::BFloat16>& a) {
443
+ static_assert(
444
+ Vectorized<c10::BFloat16>::size() == 2 * Vectorized<float>::size());
445
+ at_bfloat16x8_t x = a;
446
+ float32x4_t x1 =
447
+ Vectorized<c10::BFloat16>::convert_f32_bf16(at_vget_low_bf16(x));
448
+ float32x4_t x2 =
449
+ Vectorized<c10::BFloat16>::convert_f32_bf16(at_vget_high_bf16(x));
450
+ return {Vectorized<float>(x1), Vectorized<float>(x2)};
451
+ }
452
+ inline Vectorized<c10::BFloat16> convert_float_bfloat16(
453
+ const Vectorized<float>& a,
454
+ const Vectorized<float>& b) {
455
+ static_assert(
456
+ Vectorized<c10::BFloat16>::size() == 2 * Vectorized<float>::size());
457
+ at_bfloat16x4_t x1 = Vectorized<c10::BFloat16>::convert_bf16_f32(a);
458
+ at_bfloat16x4_t x2 = Vectorized<c10::BFloat16>::convert_bf16_f32(b);
459
+ return Vectorized<c10::BFloat16>(at_vcombine_bf16(x1, x2));
460
+ }
461
+
462
+ template <typename Op>
463
+ Vectorized<c10::BFloat16> binary_operator_via_float(
464
+ Op op,
465
+ const Vectorized<c10::BFloat16>& a,
466
+ const Vectorized<c10::BFloat16>& b) {
467
+ const auto [a_float_low, a_float_high] = convert_bfloat16_float(a);
468
+ const auto [b_float_low, b_float_high] = convert_bfloat16_float(b);
469
+ return convert_float_bfloat16(
470
+ op(a_float_low, b_float_low), op(a_float_high, b_float_high));
471
+ }
472
+
473
+ template <>
474
+ Vectorized<c10::BFloat16> inline operator+(
475
+ const Vectorized<c10::BFloat16>& a,
476
+ const Vectorized<c10::BFloat16>& b) {
477
+ #if BF16_ARITHMETIC_SUPPORTED()
478
+ bfloat16x8_t x = a;
479
+ bfloat16x8_t y = b;
480
+ return x + y;
481
+ #else
482
+ return binary_operator_via_float(std::plus<Vectorized<float>>(), a, b);
483
+ #endif
484
+ }
485
+
486
+ template <>
487
+ Vectorized<c10::BFloat16> inline operator-(
488
+ const Vectorized<c10::BFloat16>& a,
489
+ const Vectorized<c10::BFloat16>& b) {
490
+ #if BF16_ARITHMETIC_SUPPORTED()
491
+ bfloat16x8_t x = a;
492
+ bfloat16x8_t y = b;
493
+ return x - y;
494
+ #else
495
+ return binary_operator_via_float(std::minus<Vectorized<float>>(), a, b);
496
+ #endif
497
+ }
498
+
499
+ template <>
500
+ Vectorized<c10::BFloat16> inline operator*(
501
+ const Vectorized<c10::BFloat16>& a,
502
+ const Vectorized<c10::BFloat16>& b) {
503
+ #if BF16_ARITHMETIC_SUPPORTED()
504
+ bfloat16x8_t x = a;
505
+ bfloat16x8_t y = b;
506
+ return x * y;
507
+ #else
508
+ return binary_operator_via_float(std::multiplies<Vectorized<float>>(), a, b);
509
+ #endif
510
+ }
511
+
512
+ template <>
513
+ Vectorized<c10::BFloat16> inline operator/(
514
+ const Vectorized<c10::BFloat16>& a,
515
+ const Vectorized<c10::BFloat16>& b) {
516
+ #if BF16_ARITHMETIC_SUPPORTED()
517
+ bfloat16x8_t x = a;
518
+ bfloat16x8_t y = b;
519
+ return x / y;
520
+ #else
521
+ return binary_operator_via_float(std::divides<Vectorized<float>>(), a, b);
522
+ #endif
523
+ }
524
+
525
+ // frac. Implement this here so we can use subtraction
526
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::frac() const {
527
+ return *this - this->trunc();
528
+ }
529
+
530
+ template <>
531
+ Vectorized<c10::BFloat16> inline maximum(
532
+ const Vectorized<c10::BFloat16>& a,
533
+ const Vectorized<c10::BFloat16>& b) {
534
+ return binary_operator_via_float(
535
+ static_cast<Vectorized<float> (*)(
536
+ const Vectorized<float>&, const Vectorized<float>&)>(&maximum),
537
+ a,
538
+ b);
539
+ }
540
+
541
+ template <>
542
+ Vectorized<c10::BFloat16> inline minimum(
543
+ const Vectorized<c10::BFloat16>& a,
544
+ const Vectorized<c10::BFloat16>& b) {
545
+ return binary_operator_via_float(
546
+ static_cast<Vectorized<float> (*)(
547
+ const Vectorized<float>&, const Vectorized<float>&)>(&minimum),
548
+ a,
549
+ b);
550
+ }
551
+
552
+ template <>
553
+ Vectorized<c10::BFloat16> inline clamp(
554
+ const Vectorized<c10::BFloat16>& a,
555
+ const Vectorized<c10::BFloat16>& min,
556
+ const Vectorized<c10::BFloat16>& max) {
557
+ return minimum(max, maximum(min, a));
558
+ }
559
+
560
+ template <>
561
+ Vectorized<c10::BFloat16> inline clamp_max(
562
+ const Vectorized<c10::BFloat16>& a,
563
+ const Vectorized<c10::BFloat16>& max) {
564
+ return minimum(max, a);
565
+ }
566
+
567
+ template <>
568
+ Vectorized<c10::BFloat16> inline clamp_min(
569
+ const Vectorized<c10::BFloat16>& a,
570
+ const Vectorized<c10::BFloat16>& min) {
571
+ return maximum(min, a);
572
+ }
573
+
574
+ template <>
575
+ Vectorized<c10::BFloat16> inline operator&(
576
+ const Vectorized<c10::BFloat16>& a,
577
+ const Vectorized<c10::BFloat16>& b) {
578
+ return Vectorized<c10::BFloat16>(at_vreinterpretq_bf16_u16(
579
+ vandq_u16(at_vreinterpretq_u16_bf16(a), at_vreinterpretq_u16_bf16(b))));
580
+ }
581
+
582
+ template <>
583
+ Vectorized<c10::BFloat16> inline operator|(
584
+ const Vectorized<c10::BFloat16>& a,
585
+ const Vectorized<c10::BFloat16>& b) {
586
+ return Vectorized<c10::BFloat16>(at_vreinterpretq_bf16_u16(
587
+ vorrq_u16(at_vreinterpretq_u16_bf16(a), at_vreinterpretq_u16_bf16(b))));
588
+ }
589
+
590
+ template <>
591
+ Vectorized<c10::BFloat16> inline operator^(
592
+ const Vectorized<c10::BFloat16>& a,
593
+ const Vectorized<c10::BFloat16>& b) {
594
+ return Vectorized<c10::BFloat16>(at_vreinterpretq_bf16_u16(
595
+ veorq_u16(at_vreinterpretq_u16_bf16(a), at_vreinterpretq_u16_bf16(b))));
596
+ }
597
+
598
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::eq(
599
+ const Vectorized<c10::BFloat16>& other) const {
600
+ return (*this == other) & Vectorized<c10::BFloat16>(1);
601
+ }
602
+
603
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::ne(
604
+ const Vectorized<c10::BFloat16>& other) const {
605
+ return (*this != other) & Vectorized<c10::BFloat16>(1);
606
+ }
607
+
608
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::gt(
609
+ const Vectorized<c10::BFloat16>& other) const {
610
+ return (*this > other) & Vectorized<c10::BFloat16>(1);
611
+ }
612
+
613
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::ge(
614
+ const Vectorized<c10::BFloat16>& other) const {
615
+ return (*this >= other) & Vectorized<c10::BFloat16>(1);
616
+ }
617
+
618
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::lt(
619
+ const Vectorized<c10::BFloat16>& other) const {
620
+ return (*this < other) & Vectorized<c10::BFloat16>(1);
621
+ }
622
+
623
+ inline Vectorized<c10::BFloat16> Vectorized<c10::BFloat16>::le(
624
+ const Vectorized<c10::BFloat16>& other) const {
625
+ return (*this <= other) & Vectorized<c10::BFloat16>(1);
626
+ }
627
+
628
+ template <>
629
+ Vectorized<c10::BFloat16> inline fmadd(
630
+ const Vectorized<c10::BFloat16>& a,
631
+ const Vectorized<c10::BFloat16>& b,
632
+ const Vectorized<c10::BFloat16>& c) {
633
+ #if BF16_ARITHMETIC_SUPPORTED()
634
+ bfloat16x8_t x = a;
635
+ bfloat16x8_t y = b;
636
+ bfloat16x8_t z = c;
637
+ return x * y + z;
638
+ #else
639
+ // NOTE [BF16 FMA]: There isn't an FMA that accumulates into BF16! Also,
640
+ // vbfmlalbq_f32 and vbfmlaltq_f32 take the even and odd-numbered
641
+ // elements, not the bottom and top half, so they don't seem
642
+ // particularly useful here. Ideally we would include dot product in
643
+ // the Vectorized interface...
644
+ return a * b + c;
645
+ #endif
646
+ }
647
+
648
+ template <>
649
+ Vectorized<c10::BFloat16> inline fnmadd(
650
+ const Vectorized<c10::BFloat16>& a,
651
+ const Vectorized<c10::BFloat16>& b,
652
+ const Vectorized<c10::BFloat16>& c) {
653
+ #if BF16_ARITHMETIC_SUPPORTED()
654
+ bfloat16x8_t x = a;
655
+ bfloat16x8_t y = b;
656
+ bfloat16x8_t z = c;
657
+ return (-x) * y + z;
658
+ #else
659
+ // See NOTE [BF16 FMA] above.
660
+ return -a * b + c;
661
+ #endif
662
+ }
663
+
664
+ template <>
665
+ Vectorized<c10::BFloat16> inline fmsub(
666
+ const Vectorized<c10::BFloat16>& a,
667
+ const Vectorized<c10::BFloat16>& b,
668
+ const Vectorized<c10::BFloat16>& c) {
669
+ #if BF16_ARITHMETIC_SUPPORTED()
670
+ bfloat16x8_t x = a;
671
+ bfloat16x8_t y = b;
672
+ bfloat16x8_t z = c;
673
+ return x * y - z;
674
+ #else
675
+ // See NOTE [BF16 FMA] above.
676
+ return a * b - c;
677
+ #endif
678
+ }
679
+
680
+ template <>
681
+ Vectorized<c10::BFloat16> inline fnmsub(
682
+ const Vectorized<c10::BFloat16>& a,
683
+ const Vectorized<c10::BFloat16>& b,
684
+ const Vectorized<c10::BFloat16>& c) {
685
+ #if BF16_ARITHMETIC_SUPPORTED()
686
+ bfloat16x8_t x = a;
687
+ bfloat16x8_t y = b;
688
+ bfloat16x8_t z = c;
689
+ return (-x) * y - z;
690
+ #else
691
+ // See NOTE [BF16 FMA] above.
692
+ return -a * b - c;
693
+ #endif
694
+ }
695
+
696
+ #endif // !defined(C10_MOBILE) && defined(__aarch64__)
697
+
698
+ } // namespace CPU_CAPABILITY
699
+ } // namespace at::vec
700
+
701
+ #else
702
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
703
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_convert.h ADDED
@@ -0,0 +1,383 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ #include <ATen/cpu/vec/vec_base.h>
4
+ #include <ATen/cpu/vec/vec_convert.h>
5
+
6
+ namespace at::vec {
7
+ inline namespace CPU_CAPABILITY {
8
+ #if (defined(__aarch64__) && !defined(CPU_CAPABILITY_SVE256))
9
+
10
+ // Enable auto-vectorization for clang-17+
11
+ // GCC-12 has a bug: gcc.gnu.org/bugzilla/show_bug.cgi?id=117001
12
+ #if defined(__clang__) && (__clang_major__ >= 17)
13
+
14
+ template <typename from_type, typename to_type>
15
+ inline void convertImpl(
16
+ const from_type* __restrict src,
17
+ to_type* __restrict dst,
18
+ int64_t n) {
19
+ uint64_t len = static_cast<uint64_t>(n);
20
+ for (uint64_t i = 0; i < len; i++) {
21
+ dst[i] = static_cast<to_type>(src[i]);
22
+ }
23
+ }
24
+
25
+ template <typename to_type>
26
+ inline void convertFromBool(
27
+ const bool* __restrict src,
28
+ to_type* __restrict dst,
29
+ int64_t n) {
30
+ const uint8_t* srcPtr = reinterpret_cast<const uint8_t*>(src);
31
+ uint64_t len = static_cast<uint64_t>(n);
32
+ for (uint64_t i = 0; i < len; i++) {
33
+ dst[i] = srcPtr[i] != 0 ? static_cast<to_type>(1) : static_cast<to_type>(0);
34
+ }
35
+ }
36
+
37
+ template <typename from_type>
38
+ inline void convertToBool(
39
+ const from_type* __restrict src,
40
+ bool* __restrict dst,
41
+ int64_t n) {
42
+ uint8_t* dstPtr = reinterpret_cast<uint8_t*>(dst);
43
+ uint64_t len = static_cast<uint64_t>(n);
44
+ for (uint64_t i = 0; i < len; i++) {
45
+ dstPtr[i] = src[i] != static_cast<from_type>(0) ? 1 : 0;
46
+ }
47
+ }
48
+
49
+ #define CONVERT_TEMPLATE(from_type, to_type) \
50
+ template <> \
51
+ inline void convert(const from_type* src, to_type* dst, int64_t n) { \
52
+ return convertImpl<from_type, to_type>(src, dst, n); \
53
+ }
54
+
55
+ #define CONVERT_FROM_BOOL_TEMPLATE(to_type) \
56
+ inline void convert(const bool* src, to_type* dst, int64_t n) { \
57
+ return convertFromBool<to_type>(src, dst, n); \
58
+ }
59
+
60
+ #define CONVERT_TO_BOOL_TEMPLATE(from_type) \
61
+ inline void convert(const from_type* src, bool* dst, int64_t n) { \
62
+ return convertToBool<from_type>(src, dst, n); \
63
+ }
64
+
65
+ CONVERT_TEMPLATE(uint8_t, uint8_t)
66
+ CONVERT_TEMPLATE(uint8_t, int8_t)
67
+ CONVERT_TEMPLATE(uint8_t, int16_t)
68
+ CONVERT_TEMPLATE(uint8_t, int32_t)
69
+ CONVERT_TEMPLATE(uint8_t, int64_t)
70
+ CONVERT_TEMPLATE(uint8_t, float)
71
+ CONVERT_TEMPLATE(uint8_t, double)
72
+ CONVERT_TO_BOOL_TEMPLATE(uint8_t)
73
+ CONVERT_TEMPLATE(int8_t, uint8_t)
74
+ CONVERT_TEMPLATE(int8_t, int8_t)
75
+ CONVERT_TEMPLATE(int8_t, int16_t)
76
+ CONVERT_TEMPLATE(int8_t, int32_t)
77
+ CONVERT_TEMPLATE(int8_t, int64_t)
78
+ CONVERT_TEMPLATE(int8_t, float)
79
+ CONVERT_TEMPLATE(int8_t, double)
80
+ CONVERT_TO_BOOL_TEMPLATE(int8_t)
81
+ CONVERT_TEMPLATE(int16_t, uint8_t)
82
+ CONVERT_TEMPLATE(int16_t, int8_t)
83
+ CONVERT_TEMPLATE(int16_t, int16_t)
84
+ CONVERT_TEMPLATE(int16_t, int32_t)
85
+ CONVERT_TEMPLATE(int16_t, int64_t)
86
+ CONVERT_TEMPLATE(int16_t, float)
87
+ CONVERT_TEMPLATE(int16_t, double)
88
+ CONVERT_TO_BOOL_TEMPLATE(int16_t)
89
+ CONVERT_TEMPLATE(int32_t, uint8_t)
90
+ CONVERT_TEMPLATE(int32_t, int8_t)
91
+ CONVERT_TEMPLATE(int32_t, int16_t)
92
+ CONVERT_TEMPLATE(int32_t, int32_t)
93
+ CONVERT_TEMPLATE(int32_t, int64_t)
94
+ CONVERT_TEMPLATE(int32_t, float)
95
+ CONVERT_TEMPLATE(int32_t, double)
96
+ CONVERT_TO_BOOL_TEMPLATE(int32_t)
97
+ CONVERT_TEMPLATE(int64_t, uint8_t)
98
+ CONVERT_TEMPLATE(int64_t, int8_t)
99
+ CONVERT_TEMPLATE(int64_t, int16_t)
100
+ CONVERT_TEMPLATE(int64_t, int32_t)
101
+ CONVERT_TEMPLATE(int64_t, int64_t)
102
+ CONVERT_TEMPLATE(int64_t, float)
103
+ CONVERT_TEMPLATE(int64_t, double)
104
+ CONVERT_TO_BOOL_TEMPLATE(int64_t)
105
+ CONVERT_TEMPLATE(float, uint8_t)
106
+ CONVERT_TEMPLATE(float, int8_t)
107
+ CONVERT_TEMPLATE(float, int16_t)
108
+ CONVERT_TEMPLATE(float, int32_t)
109
+ CONVERT_TEMPLATE(float, int64_t)
110
+ CONVERT_TEMPLATE(float, float)
111
+ CONVERT_TEMPLATE(float, double)
112
+ CONVERT_TO_BOOL_TEMPLATE(float)
113
+ CONVERT_TEMPLATE(double, uint8_t)
114
+ CONVERT_TEMPLATE(double, int8_t)
115
+ CONVERT_TEMPLATE(double, int16_t)
116
+ CONVERT_TEMPLATE(double, int32_t)
117
+ CONVERT_TEMPLATE(double, int64_t)
118
+ CONVERT_TEMPLATE(double, float)
119
+ CONVERT_TEMPLATE(double, double)
120
+ CONVERT_TO_BOOL_TEMPLATE(double)
121
+ CONVERT_FROM_BOOL_TEMPLATE(uint8_t)
122
+ CONVERT_FROM_BOOL_TEMPLATE(int8_t)
123
+ CONVERT_FROM_BOOL_TEMPLATE(int16_t)
124
+ CONVERT_FROM_BOOL_TEMPLATE(int32_t)
125
+ CONVERT_FROM_BOOL_TEMPLATE(int64_t)
126
+ CONVERT_FROM_BOOL_TEMPLATE(float)
127
+ CONVERT_FROM_BOOL_TEMPLATE(double)
128
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
129
+
130
+ #define CONVERT_FROM_FP16_TEMPLATE(to_type) \
131
+ template <> \
132
+ inline void convert(const at::Half* src, to_type* dst, int64_t n) { \
133
+ const float16_t* srcPtr = reinterpret_cast<const float16_t*>(src); \
134
+ return convertImpl<float16_t, to_type>(srcPtr, dst, n); \
135
+ }
136
+
137
+ #define CONVERT_TO_FP16_TEMPLATE(from_type) \
138
+ template <> \
139
+ inline void convert(const from_type* src, at::Half* dst, int64_t n) { \
140
+ float16_t* dstPtr = reinterpret_cast<float16_t*>(dst); \
141
+ return convertImpl<from_type, float16_t>(src, dstPtr, n); \
142
+ }
143
+
144
+ CONVERT_FROM_FP16_TEMPLATE(uint8_t)
145
+ CONVERT_FROM_FP16_TEMPLATE(int8_t)
146
+ CONVERT_FROM_FP16_TEMPLATE(int16_t)
147
+ CONVERT_FROM_FP16_TEMPLATE(int32_t)
148
+ CONVERT_FROM_FP16_TEMPLATE(int64_t)
149
+ CONVERT_FROM_FP16_TEMPLATE(float16_t)
150
+ CONVERT_FROM_FP16_TEMPLATE(float)
151
+ CONVERT_FROM_FP16_TEMPLATE(double)
152
+ CONVERT_TO_FP16_TEMPLATE(uint8_t)
153
+ CONVERT_TO_FP16_TEMPLATE(int8_t)
154
+ CONVERT_TO_FP16_TEMPLATE(int16_t)
155
+ CONVERT_TO_FP16_TEMPLATE(int32_t)
156
+ CONVERT_TO_FP16_TEMPLATE(int64_t)
157
+ CONVERT_TO_FP16_TEMPLATE(float)
158
+ CONVERT_TO_FP16_TEMPLATE(double)
159
+
160
+ inline void convertBoolToFp16Impl(
161
+ const bool* __restrict src,
162
+ at::Half* __restrict dst,
163
+ int64_t n) {
164
+ const uint8_t* srcPtr = reinterpret_cast<const uint8_t*>(src);
165
+ float16_t* dstPtr = reinterpret_cast<float16_t*>(dst);
166
+ uint64_t len = static_cast<uint64_t>(n);
167
+ for (uint64_t i = 0; i < len; i++) {
168
+ dstPtr[i] = srcPtr[i] != 0 ? 1.0 : 0;
169
+ }
170
+ }
171
+
172
+ template <>
173
+ inline void convert(const bool* src, at::Half* dst, int64_t n) {
174
+ return convertBoolToFp16Impl(src, dst, n);
175
+ }
176
+
177
+ inline void convertFp16ToBoolImpl(
178
+ const at::Half* __restrict src,
179
+ bool* __restrict dst,
180
+ int64_t n) {
181
+ const float16_t* srcPtr = reinterpret_cast<const float16_t*>(src);
182
+ uint8_t* dstPtr = reinterpret_cast<uint8_t*>(dst);
183
+ uint64_t len = static_cast<uint64_t>(n);
184
+ for (uint64_t i = 0; i < len; i++) {
185
+ dstPtr[i] = srcPtr[i] != 0.0 ? 1 : 0;
186
+ }
187
+ }
188
+
189
+ template <>
190
+ inline void convert(const at::Half* src, bool* dst, int64_t n) {
191
+ return convertFp16ToBoolImpl(src, dst, n);
192
+ }
193
+
194
+ #endif
195
+
196
+ template <typename to_type>
197
+ inline void convertFromBf16Impl(
198
+ const c10::BFloat16* __restrict src,
199
+ to_type* __restrict dst,
200
+ int64_t n) {
201
+ const uint16_t* srcPtr = reinterpret_cast<const uint16_t*>(src);
202
+ uint64_t len = static_cast<uint64_t>(n);
203
+ for (uint64_t i = 0; i < len; i++) {
204
+ uint32_t tmp = static_cast<uint32_t>(srcPtr[i]) << 16;
205
+ float tmpF;
206
+ __builtin_memcpy(&tmpF, &tmp, sizeof(float));
207
+ dst[i] = static_cast<to_type>(tmpF);
208
+ }
209
+ }
210
+ #define CONVERT_FROM_BF16_TEMPLATE(to_type) \
211
+ template <> \
212
+ inline void convert(const c10::BFloat16* src, to_type* dst, int64_t n) { \
213
+ return convertFromBf16Impl<to_type>(src, dst, n); \
214
+ }
215
+
216
+ CONVERT_FROM_BF16_TEMPLATE(uint8_t)
217
+ CONVERT_FROM_BF16_TEMPLATE(int8_t)
218
+ CONVERT_FROM_BF16_TEMPLATE(int16_t)
219
+ CONVERT_FROM_BF16_TEMPLATE(int32_t)
220
+ CONVERT_FROM_BF16_TEMPLATE(int64_t)
221
+ CONVERT_FROM_BF16_TEMPLATE(float)
222
+ CONVERT_FROM_BF16_TEMPLATE(double)
223
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
224
+ CONVERT_FROM_BF16_TEMPLATE(float16_t)
225
+ #endif
226
+
227
+ #ifdef __ARM_FEATURE_BF16
228
+
229
+ // clang-[17, 20] crashes when autovectorizing static cast to bf16
230
+ // Below is a workaround to have some vectorization
231
+ // Works decently well for smaller int types
232
+ template <typename from_type>
233
+ inline void convertToBf16Impl(
234
+ const from_type* __restrict src,
235
+ c10::BFloat16* __restrict dst,
236
+ uint64_t n) {
237
+ bfloat16_t* dstPtr = reinterpret_cast<bfloat16_t*>(dst);
238
+ uint64_t loopBound = n - (n % 16);
239
+ uint64_t i = 0;
240
+ for (; i < loopBound; i += 16) {
241
+ float32x4_t a, b, c, d;
242
+ a[0] = static_cast<float>(src[i]);
243
+ a[1] = static_cast<float>(src[i + 1]);
244
+ a[2] = static_cast<float>(src[i + 2]);
245
+ a[3] = static_cast<float>(src[i + 3]);
246
+ b[0] = static_cast<float>(src[i + 4]);
247
+ b[1] = static_cast<float>(src[i + 5]);
248
+ b[2] = static_cast<float>(src[i + 6]);
249
+ b[3] = static_cast<float>(src[i + 7]);
250
+ c[0] = static_cast<float>(src[i + 8]);
251
+ c[1] = static_cast<float>(src[i + 9]);
252
+ c[2] = static_cast<float>(src[i + 10]);
253
+ c[3] = static_cast<float>(src[i + 11]);
254
+ d[0] = static_cast<float>(src[i + 12]);
255
+ d[1] = static_cast<float>(src[i + 13]);
256
+ d[2] = static_cast<float>(src[i + 14]);
257
+ d[3] = static_cast<float>(src[i + 15]);
258
+
259
+ vst1q_bf16(dstPtr + i, vcvtq_high_bf16_f32(vcvtq_low_bf16_f32(a), b));
260
+ vst1q_bf16(dstPtr + i + 8, vcvtq_high_bf16_f32(vcvtq_low_bf16_f32(c), d));
261
+ }
262
+
263
+ #pragma clang loop vectorize(disable) interleave(disable) unroll(disable)
264
+ for (; i < n; i++) {
265
+ float a = static_cast<float>(src[i]);
266
+ dstPtr[i] = vcvth_bf16_f32(a);
267
+ }
268
+ }
269
+
270
+ #define CONVERT_TO_BF16_TEMPLATE(from_type) \
271
+ template <> \
272
+ inline void convert(const from_type* src, c10::BFloat16* dst, int64_t n) { \
273
+ return convertToBf16Impl<from_type>(src, dst, n); \
274
+ }
275
+
276
+ CONVERT_TO_BF16_TEMPLATE(uint8_t)
277
+ CONVERT_TO_BF16_TEMPLATE(int8_t)
278
+ CONVERT_TO_BF16_TEMPLATE(int16_t)
279
+ CONVERT_TO_BF16_TEMPLATE(int32_t)
280
+
281
+ #endif
282
+
283
+ inline void convertBoolToBfloat16Impl(
284
+ const bool* __restrict src,
285
+ c10::BFloat16* __restrict dst,
286
+ int64_t n) {
287
+ const uint8_t* srcPtr = reinterpret_cast<const uint8_t*>(src);
288
+ uint16_t* dstPtr = reinterpret_cast<uint16_t*>(dst);
289
+ uint64_t len = static_cast<uint64_t>(n);
290
+ constexpr uint16_t kBf16One = 0x3f80; // 1.0 in bfloat16
291
+ for (uint64_t i = 0; i < len; i++) {
292
+ dstPtr[i] = srcPtr[i] != 0 ? kBf16One : 0;
293
+ }
294
+ }
295
+
296
+ template <>
297
+ inline void convert(const bool* src, c10::BFloat16* dst, int64_t n) {
298
+ return convertBoolToBfloat16Impl(src, dst, n);
299
+ }
300
+
301
+ inline void convertBfloat16ToBoolImpl(
302
+ const c10::BFloat16* __restrict src,
303
+ bool* __restrict dst,
304
+ int64_t n) {
305
+ uint8_t* dstPtr = reinterpret_cast<uint8_t*>(dst);
306
+ const uint16_t* srcPtr = reinterpret_cast<const uint16_t*>(src);
307
+ uint64_t len = static_cast<uint64_t>(n);
308
+ for (uint64_t i = 0; i < len; i++) {
309
+ // Check if all non-sign bits are 0
310
+ bool isBf16Zero = (srcPtr[i] & 0x7fff) == 0;
311
+ dstPtr[i] = isBf16Zero ? 0 : 1;
312
+ }
313
+ }
314
+
315
+ template <>
316
+ inline void convert(const c10::BFloat16* src, bool* dst, int64_t n) {
317
+ return convertBfloat16ToBoolImpl(src, dst, n);
318
+ }
319
+
320
+ #endif
321
+
322
+ template <typename src_t>
323
+ struct VecConvert<
324
+ float,
325
+ 1,
326
+ src_t,
327
+ 1,
328
+ typename std::enable_if_t<is_8bit_integer_v<src_t>, void>> {
329
+ static inline VectorizedN<float, 1> apply(const VectorizedN<src_t, 1>& src) {
330
+ return convert_int8_half_register_to_float(src[0]);
331
+ }
332
+ };
333
+ template <typename src_t>
334
+ struct VecConvert<
335
+ float,
336
+ 2,
337
+ src_t,
338
+ 1,
339
+ typename std::enable_if_t<is_8bit_integer_v<src_t>, void>> {
340
+ static inline VectorizedN<float, 2> apply(const VectorizedN<src_t, 1>& src) {
341
+ const auto [v0, v1] = convert_int8_to_float(src[0]);
342
+ return VectorizedN<float, 2>(v0, v1);
343
+ }
344
+ };
345
+
346
+ template <>
347
+ struct VecConvert<float, 2, BFloat16, 1> {
348
+ static inline VectorizedN<float, 2> apply(
349
+ const VectorizedN<BFloat16, 1>& src) {
350
+ VectorizedN<float, 2> result;
351
+ uint16x8_t u16_8 = vld1q_u16(reinterpret_cast<const uint16_t*>(&src[0]));
352
+ auto u16_low1 = vget_low_u16(u16_8);
353
+ auto u16_high1 = vget_high_u16(u16_8);
354
+ float32x4_t f32x4_0 =
355
+ vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(u16_low1), 16));
356
+ float32x4_t f32x4_1 =
357
+ vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(u16_high1), 16));
358
+ result[0] = f32x4_0;
359
+ result[1] = f32x4_1;
360
+ return result;
361
+ }
362
+ };
363
+ // Half register to full register.
364
+ template <>
365
+ struct VecConvert<float, 1, BFloat16, 1> {
366
+ static inline VectorizedN<float, 1> apply(
367
+ const VectorizedN<BFloat16, 1>& src) {
368
+ VectorizedN<float, 1> result;
369
+ uint16x4_t u16_8 = vld1_u16(reinterpret_cast<const uint16_t*>(&src[0]));
370
+ float32x4_t f32x4_0 =
371
+ vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(u16_8), 16));
372
+ result[0] = f32x4_0;
373
+ return result;
374
+ }
375
+ };
376
+
377
+ #endif // defined(__aarch64__) && !defined(CPU_CAPABILITY_SVE256)
378
+ } // namespace CPU_CAPABILITY
379
+ } // namespace at::vec
380
+
381
+ #else
382
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
383
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_double_neon.h ADDED
@@ -0,0 +1,591 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/vec_base.h>
6
+ #include <c10/macros/Macros.h>
7
+ #include <c10/util/irange.h>
8
+ #include <cmath>
9
+
10
+ namespace at::vec {
11
+ // Note [CPU_CAPABILITY namespace]
12
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13
+ // This header, and all of its subheaders, will be compiled with
14
+ // different architecture flags for each supported set of vector
15
+ // intrinsics. So we need to make sure they aren't inadvertently
16
+ // linked together. We do this by declaring objects in an `inline
17
+ // namespace` which changes the name mangling, but can still be
18
+ // accessed as `at::vec`.
19
+ inline namespace CPU_CAPABILITY {
20
+
21
+ template <>
22
+ struct is_vec_specialized_for<double> : std::bool_constant<true> {};
23
+
24
+ template <>
25
+ class Vectorized<double> {
26
+ private:
27
+ float64x2_t values;
28
+
29
+ public:
30
+ using value_type = double;
31
+ using size_type = int;
32
+ static constexpr size_type size() {
33
+ return 2;
34
+ }
35
+ Vectorized() {
36
+ values = vdupq_n_f64(0.0);
37
+ }
38
+ Vectorized(float64x2_t v) : values(v) {}
39
+ Vectorized(double val) {
40
+ values = vdupq_n_f64(val);
41
+ }
42
+ template <
43
+ typename... Args,
44
+ typename = std::enable_if_t<(sizeof...(Args) == size())>>
45
+ Vectorized(Args... vals) {
46
+ __at_align__ double buffer[size()] = {vals...};
47
+ values = vld1q_f64(buffer);
48
+ }
49
+ operator float64x2_t() const {
50
+ return values;
51
+ }
52
+ template <int64_t mask>
53
+ static Vectorized<double> blend(
54
+ const Vectorized<double>& a,
55
+ const Vectorized<double>& b) {
56
+ // Build an array of flags: each bit of element is 1 if the corresponding
57
+ // bit in 'mask' is set, 0 otherwise.
58
+ uint64x2_t maskArray = {
59
+ (mask & 1ULL) ? 0xFFFFFFFFFFFFFFFF : 0,
60
+ (mask & 2ULL) ? 0xFFFFFFFFFFFFFFFF : 0};
61
+ // Use BSL to select elements from b where the mask is 1, else from a
62
+ return vbslq_f64(maskArray, b.values, a.values);
63
+ }
64
+ static Vectorized<double> blendv(
65
+ const Vectorized<double>& a,
66
+ const Vectorized<double>& b,
67
+ const Vectorized<double>& mask_) {
68
+ return vbslq_f64(vreinterpretq_u64_f64(mask_.values), b.values, a.values);
69
+ }
70
+ template <typename step_t>
71
+ static Vectorized<double> arange(
72
+ double base = 0.,
73
+ step_t step = static_cast<step_t>(1)) {
74
+ return {base, base + static_cast<double>(step)};
75
+ }
76
+ static inline Vectorized<double> set(
77
+ const Vectorized<double>& a,
78
+ const Vectorized<double>& b,
79
+ int64_t count = size()) {
80
+ if (count == 0) {
81
+ return a;
82
+ } else if (count >= 2) {
83
+ return b;
84
+ } else {
85
+ float64x2_t c = {b.values[0], a.values[1]};
86
+ return c;
87
+ }
88
+ }
89
+ static Vectorized<double> loadu(const void* ptr, int64_t count = size()) {
90
+ if (count == size()) {
91
+ return vld1q_f64(reinterpret_cast<const double*>(ptr));
92
+ } else if (count == 1) {
93
+ float64x1_t x = vld1_f64(reinterpret_cast<const double*>(ptr));
94
+ float64x1_t z = {0.0};
95
+ return vcombine_f64(x, z);
96
+ } else {
97
+ return vdupq_n_f64(0.0);
98
+ }
99
+ }
100
+ void store(void* ptr, int64_t count = size()) const {
101
+ if (count == size()) {
102
+ vst1q_f64(reinterpret_cast<double*>(ptr), values);
103
+ } else if (count == 1) {
104
+ vst1_f64(reinterpret_cast<double*>(ptr), vget_low_f64(values));
105
+ }
106
+ }
107
+ const double& operator[](int idx) const = delete;
108
+ double& operator[](int idx) = delete;
109
+ int64_t zero_mask() const {
110
+ // returns an integer mask where all zero elements are translated to 1-bit
111
+ // and others are translated to 0-bit
112
+ uint64x2_t cmpReg = vceqzq_f64(values);
113
+ uint64x2_t mask = {1, 2};
114
+ uint64x2_t res = vandq_u64(cmpReg, mask);
115
+ return res[0] | res[1];
116
+ }
117
+ Vectorized<double> isnan() const {
118
+ // NaN check
119
+ return vreinterpretq_f64_u32(
120
+ vmvnq_u32(vreinterpretq_u32_u64(vceqq_f64(values, values))));
121
+ }
122
+ bool has_inf_nan() const {
123
+ Vectorized<double> x = vsubq_f64(values, values);
124
+ float64x2_t r = x.isnan();
125
+ uint64x2_t u = vreinterpretq_u64_f64(r);
126
+ return u[0] | u[1];
127
+ }
128
+ Vectorized<double> map(double (*f)(double)) const {
129
+ float64x2_t result;
130
+ result[0] = f(values[0]);
131
+ result[1] = f(values[1]);
132
+ return result;
133
+ }
134
+ Vectorized<double> map2(
135
+ const Vectorized<double>& second,
136
+ double (*const f)(double, double)) const {
137
+ float64x2_t result;
138
+ result[0] = f(values[0], second.values[0]);
139
+ result[1] = f(values[1], second.values[1]);
140
+ return result;
141
+ }
142
+ Vectorized<double> abs() const {
143
+ return vabsq_f64(values);
144
+ }
145
+ Vectorized<double> angle() const {
146
+ auto zero = Vectorized<double>(0.0);
147
+ auto pi = Vectorized<double>(c10::pi<double>);
148
+ auto tmp = blendv(zero, pi, vreinterpretq_f64_u64(vcltzq_f64(values)));
149
+ return blendv(tmp, *this, isnan());
150
+ }
151
+ Vectorized<double> real() const {
152
+ return *this;
153
+ }
154
+ Vectorized<double> imag() const {
155
+ return Vectorized<double>(0.0);
156
+ }
157
+ Vectorized<double> conj() const {
158
+ return *this;
159
+ }
160
+ Vectorized<double> acos() const {
161
+ return USE_SLEEF(
162
+ Vectorized<double>(Sleef_acosd2_u10(values)), map(std::acos));
163
+ }
164
+ Vectorized<double> acosh() const {
165
+ return USE_SLEEF(
166
+ Vectorized<double>(Sleef_acoshd2_u10(values)), map(std::acosh));
167
+ }
168
+ Vectorized<double> asin() const {
169
+ return USE_SLEEF(
170
+ Vectorized<double>(Sleef_asind2_u10(values)), map(std::asin));
171
+ }
172
+ Vectorized<double> asinh() const {
173
+ return USE_SLEEF(
174
+ Vectorized<double>(Sleef_asinhd2_u10(values)), map(std::asinh));
175
+ }
176
+ Vectorized<double> atan() const {
177
+ return USE_SLEEF(
178
+ Vectorized<double>(Sleef_atand2_u10(values)), map(std::atan));
179
+ }
180
+ Vectorized<double> atanh() const {
181
+ return USE_SLEEF(
182
+ Vectorized<double>(Sleef_atanhd2_u10(values)), map(std::atanh));
183
+ }
184
+ Vectorized<double> atan2(const Vectorized<double>& b) const {USE_SLEEF(
185
+ { return Vectorized<double>(Sleef_atan2d2_u10(values, b)); },
186
+ {
187
+ __at_align__ double tmp[size()];
188
+ __at_align__ double tmp_b[size()];
189
+ store(tmp);
190
+ b.store(tmp_b);
191
+ for (int64_t i = 0; i < size(); i++) {
192
+ tmp[i] = std::atan2(tmp[i], tmp_b[i]);
193
+ }
194
+ return loadu(tmp);
195
+ })} Vectorized<double> copysign(const Vectorized<double>& sign) const {
196
+ USE_SLEEF(
197
+ { return Vectorized<double>(Sleef_copysignd2(values, sign)); },
198
+ {
199
+ __at_align__ double tmp[size()];
200
+ __at_align__ double tmp_sign[size()];
201
+ store(tmp);
202
+ sign.store(tmp_sign);
203
+ for (int64_t i = 0; i < size(); i++) {
204
+ tmp[i] = std::copysign(tmp[i], tmp_sign[i]);
205
+ }
206
+ return loadu(tmp);
207
+ })} Vectorized<double> erf() const {
208
+ return USE_SLEEF(
209
+ Vectorized<double>(Sleef_erfd2_u10(values)), map(std::erf));
210
+ }
211
+ Vectorized<double> erfc() const {
212
+ return USE_SLEEF(
213
+ Vectorized<double>(Sleef_erfcd2_u15(values)), map(std::erfc));
214
+ }
215
+ Vectorized<double> exp() const {
216
+ return USE_SLEEF(
217
+ Vectorized<double>(Sleef_expd2_u10(values)), map(std::exp));
218
+ }
219
+ Vectorized<double> exp2() const {
220
+ return USE_SLEEF(
221
+ Vectorized<double>(Sleef_exp2d2_u10(values)), map(std::exp2));
222
+ }
223
+ Vectorized<double> expm1() const {
224
+ return USE_SLEEF(
225
+ Vectorized<double>(Sleef_expm1d2_u10(values)), map(std::expm1));
226
+ }
227
+ Vectorized<double> fmod(const Vectorized<double>& q) const {USE_SLEEF(
228
+ { return Vectorized<double>(Sleef_fmodd2(values, q)); },
229
+ {
230
+ __at_align__ double tmp[size()];
231
+ __at_align__ double tmp_q[size()];
232
+ store(tmp);
233
+ q.store(tmp_q);
234
+ for (int64_t i = 0; i < size(); i++) {
235
+ tmp[i] = std::fmod(tmp[i], tmp_q[i]);
236
+ }
237
+ return loadu(tmp);
238
+ })} Vectorized<double> hypot(const Vectorized<double>& b) const {
239
+ USE_SLEEF(
240
+ { return Vectorized<double>(Sleef_hypotd2_u05(values, b)); },
241
+ {
242
+ __at_align__ double tmp[size()];
243
+ __at_align__ double tmp_b[size()];
244
+ store(tmp);
245
+ b.store(tmp_b);
246
+ for (int64_t i = 0; i < size(); i++) {
247
+ tmp[i] = std::hypot(tmp[i], tmp_b[i]);
248
+ }
249
+ return loadu(tmp);
250
+ })} Vectorized<double> i0() const {
251
+ return map(calc_i0);
252
+ }
253
+ Vectorized<double> nextafter(const Vectorized<double>& b) const {USE_SLEEF(
254
+ { return Vectorized<double>(Sleef_nextafterd2(values, b)); },
255
+ {
256
+ __at_align__ double tmp[size()];
257
+ __at_align__ double tmp_b[size()];
258
+ store(tmp);
259
+ b.store(tmp_b);
260
+ for (int64_t i = 0; i < size(); ++i) {
261
+ tmp[i] = std::nextafter(tmp[i], tmp_b[i]);
262
+ }
263
+ return loadu(tmp);
264
+ })} Vectorized<double> log() const {
265
+ return USE_SLEEF(
266
+ Vectorized<double>(Sleef_logd2_u10(values)), map(std::log));
267
+ }
268
+ Vectorized<double> log2() const {
269
+ return USE_SLEEF(
270
+ Vectorized<double>(Sleef_log2d2_u10(values)), map(std::log2));
271
+ }
272
+ Vectorized<double> log10() const {
273
+ return USE_SLEEF(
274
+ Vectorized<double>(Sleef_log10d2_u10(values)), map(std::log10));
275
+ }
276
+ Vectorized<double> log1p() const {
277
+ return USE_SLEEF(
278
+ Vectorized<double>(Sleef_log1pd2_u10(values)), map(std::log1p));
279
+ }
280
+ Vectorized<double> frac() const;
281
+ Vectorized<double> sin() const {
282
+ return USE_SLEEF(
283
+ Vectorized<double>(Sleef_sind2_u10(values)), map(std::sin));
284
+ }
285
+ Vectorized<double> sinh() const {
286
+ return USE_SLEEF(
287
+ Vectorized<double>(Sleef_sinhd2_u10(values)), map(std::sinh));
288
+ }
289
+ Vectorized<double> cos() const {
290
+ return USE_SLEEF(
291
+ Vectorized<double>(Sleef_cosd2_u10(values)), map(std::cos));
292
+ }
293
+ Vectorized<double> cosh() const {
294
+ return USE_SLEEF(
295
+ Vectorized<double>(Sleef_coshd2_u10(values)), map(std::cosh));
296
+ }
297
+ Vectorized<double> pow(const Vectorized<double>& b) const {USE_SLEEF(
298
+ { return Vectorized<double>(Sleef_powd2_u10(values, b)); },
299
+ {
300
+ __at_align__ double tmp[size()];
301
+ __at_align__ double tmp_b[size()];
302
+ store(tmp);
303
+ b.store(tmp_b);
304
+ for (int64_t i = 0; i < size(); i++) {
305
+ tmp[i] = std::pow(tmp[i], tmp_b[i]);
306
+ }
307
+ return loadu(tmp);
308
+ })} // Comparison using the _CMP_**_OQ predicate.
309
+ // `O`: get false if an operand is NaN
310
+ // `Q`: do not raise if an operand is NaN
311
+ Vectorized<double> tan() const {
312
+ return USE_SLEEF(
313
+ Vectorized<double>(Sleef_tand2_u10(values)), map(std::tan));
314
+ }
315
+ Vectorized<double> tanh() const {
316
+ return USE_SLEEF(
317
+ Vectorized<double>(Sleef_tanhd2_u10(values)), map(std::tanh));
318
+ }
319
+ Vectorized<double> lgamma() const {
320
+ return USE_SLEEF(
321
+ Vectorized<double>(Sleef_lgammad2_u10(values)), map(std::lgamma));
322
+ }
323
+ Vectorized<double> erfinv() const {
324
+ return map(calc_erfinv);
325
+ }
326
+ Vectorized<double> exp_u20() const {
327
+ return exp();
328
+ }
329
+ Vectorized<double> fexp_u20() const {
330
+ return exp();
331
+ }
332
+ Vectorized<double> i0e() const {
333
+ return map(calc_i0e);
334
+ }
335
+ Vectorized<double> digamma() const {
336
+ return map(calc_digamma);
337
+ }
338
+ Vectorized<double> igamma(const Vectorized<double>& x) const {
339
+ __at_align__ double tmp[size()];
340
+ __at_align__ double tmp_x[size()];
341
+ store(tmp);
342
+ x.store(tmp_x);
343
+ for (int64_t i = 0; i < size(); i++) {
344
+ tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
345
+ }
346
+ return loadu(tmp);
347
+ }
348
+ Vectorized<double> igammac(const Vectorized<double>& x) const {
349
+ __at_align__ double tmp[size()];
350
+ __at_align__ double tmp_x[size()];
351
+ store(tmp);
352
+ x.store(tmp_x);
353
+ for (int64_t i = 0; i < size(); i++) {
354
+ tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
355
+ }
356
+ return loadu(tmp);
357
+ }
358
+ Vectorized<double> ceil() const {
359
+ return vrndpq_f64(values);
360
+ }
361
+ Vectorized<double> floor() const {
362
+ return vrndmq_f64(values);
363
+ }
364
+ Vectorized<double> neg() const {
365
+ return vnegq_f64(values);
366
+ }
367
+ Vectorized<double> round() const {
368
+ return vrndiq_f64(values);
369
+ }
370
+ Vectorized<double> trunc() const {
371
+ return vrndq_f64(values);
372
+ }
373
+ Vectorized<double> sqrt() const {
374
+ return vsqrtq_f64(values);
375
+ }
376
+ Vectorized<double> reciprocal() const {
377
+ return vdivq_f64(vdupq_n_f64(1.0), values);
378
+ }
379
+ Vectorized<double> rsqrt() const {
380
+ return vdivq_f64(vdupq_n_f64(1.0), vsqrtq_f64(values));
381
+ }
382
+ double reduce_add() const {
383
+ return vaddvq_f64(values);
384
+ }
385
+ double reduce_max() const {
386
+ return vmaxvq_f64(values);
387
+ }
388
+ Vectorized<double> operator==(const Vectorized<double>& other) const {
389
+ return Vectorized<double>(
390
+ vreinterpretq_f64_u64(vceqq_f64(values, other.values)));
391
+ }
392
+
393
+ Vectorized<double> operator!=(const Vectorized<double>& other) const {
394
+ float64x2_t r0 = vreinterpretq_f64_u32(
395
+ vmvnq_u32(vreinterpretq_u32_u64(vceqq_f64(values, other.values))));
396
+ return Vectorized<double>(r0);
397
+ }
398
+
399
+ Vectorized<double> operator<(const Vectorized<double>& other) const {
400
+ return Vectorized<double>(
401
+ vreinterpretq_f64_u64(vcltq_f64(values, other.values)));
402
+ }
403
+
404
+ Vectorized<double> operator<=(const Vectorized<double>& other) const {
405
+ return Vectorized<double>(
406
+ vreinterpretq_f64_u64(vcleq_f64(values, other.values)));
407
+ }
408
+
409
+ Vectorized<double> operator>(const Vectorized<double>& other) const {
410
+ return Vectorized<double>(
411
+ vreinterpretq_f64_u64(vcgtq_f64(values, other.values)));
412
+ }
413
+
414
+ Vectorized<double> operator>=(const Vectorized<double>& other) const {
415
+ return Vectorized<double>(
416
+ vreinterpretq_f64_u64(vcgeq_f64(values, other.values)));
417
+ }
418
+
419
+ Vectorized<double> eq(const Vectorized<double>& other) const;
420
+ Vectorized<double> ne(const Vectorized<double>& other) const;
421
+ Vectorized<double> gt(const Vectorized<double>& other) const;
422
+ Vectorized<double> ge(const Vectorized<double>& other) const;
423
+ Vectorized<double> lt(const Vectorized<double>& other) const;
424
+ Vectorized<double> le(const Vectorized<double>& other) const;
425
+ };
426
+
427
+ template <>
428
+ Vectorized<double> inline operator+(
429
+ const Vectorized<double>& a,
430
+ const Vectorized<double>& b) {
431
+ return vaddq_f64(a, b);
432
+ }
433
+
434
+ template <>
435
+ Vectorized<double> inline operator-(
436
+ const Vectorized<double>& a,
437
+ const Vectorized<double>& b) {
438
+ return vsubq_f64(a, b);
439
+ }
440
+
441
+ template <>
442
+ Vectorized<double> inline operator*(
443
+ const Vectorized<double>& a,
444
+ const Vectorized<double>& b) {
445
+ return vmulq_f64(a, b);
446
+ }
447
+
448
+ template <>
449
+ Vectorized<double> inline operator/(
450
+ const Vectorized<double>& a,
451
+ const Vectorized<double>& b) {
452
+ return vdivq_f64(a, b);
453
+ }
454
+
455
+ // frac. Implement this here so we can use subtraction
456
+ Vectorized<double> inline Vectorized<double>::frac() const {
457
+ return *this - this->trunc();
458
+ }
459
+
460
+ // Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
461
+ // either input is a NaN.
462
+ template <>
463
+ Vectorized<double> inline maximum(
464
+ const Vectorized<double>& a,
465
+ const Vectorized<double>& b) {
466
+ return vmaxq_f64(a, b);
467
+ }
468
+
469
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
470
+ // either input is a NaN.
471
+ template <>
472
+ Vectorized<double> inline minimum(
473
+ const Vectorized<double>& a,
474
+ const Vectorized<double>& b) {
475
+ return vminq_f64(a, b);
476
+ }
477
+
478
+ template <>
479
+ Vectorized<double> inline clamp(
480
+ const Vectorized<double>& a,
481
+ const Vectorized<double>& min,
482
+ const Vectorized<double>& max) {
483
+ return vminq_f64(max, vmaxq_f64(min, a));
484
+ }
485
+
486
+ template <>
487
+ Vectorized<double> inline clamp_max(
488
+ const Vectorized<double>& a,
489
+ const Vectorized<double>& max) {
490
+ return vminq_f64(max, a);
491
+ }
492
+
493
+ template <>
494
+ Vectorized<double> inline clamp_min(
495
+ const Vectorized<double>& a,
496
+ const Vectorized<double>& min) {
497
+ return vmaxq_f64(min, a);
498
+ }
499
+
500
+ template <>
501
+ Vectorized<double> inline operator&(
502
+ const Vectorized<double>& a,
503
+ const Vectorized<double>& b) {
504
+ return vreinterpretq_f64_u64(
505
+ vandq_u64(vreinterpretq_u64_f64(a), vreinterpretq_u64_f64(b)));
506
+ }
507
+
508
+ template <>
509
+ Vectorized<double> inline operator|(
510
+ const Vectorized<double>& a,
511
+ const Vectorized<double>& b) {
512
+ return vreinterpretq_f64_u64(
513
+ vorrq_u64(vreinterpretq_u64_f64(a), vreinterpretq_u64_f64(b)));
514
+ }
515
+
516
+ template <>
517
+ Vectorized<double> inline operator^(
518
+ const Vectorized<double>& a,
519
+ const Vectorized<double>& b) {
520
+ return vreinterpretq_f64_u64(
521
+ veorq_u64(vreinterpretq_u64_f64(a), vreinterpretq_u64_f64(b)));
522
+ }
523
+
524
+ inline Vectorized<double> Vectorized<double>::eq(
525
+ const Vectorized<double>& other) const {
526
+ return (*this == other) & Vectorized<double>(1.0);
527
+ }
528
+
529
+ inline Vectorized<double> Vectorized<double>::ne(
530
+ const Vectorized<double>& other) const {
531
+ return (*this != other) & Vectorized<double>(1.0);
532
+ }
533
+
534
+ inline Vectorized<double> Vectorized<double>::gt(
535
+ const Vectorized<double>& other) const {
536
+ return (*this > other) & Vectorized<double>(1.0);
537
+ }
538
+
539
+ inline Vectorized<double> Vectorized<double>::ge(
540
+ const Vectorized<double>& other) const {
541
+ return (*this >= other) & Vectorized<double>(1.0);
542
+ }
543
+
544
+ inline Vectorized<double> Vectorized<double>::lt(
545
+ const Vectorized<double>& other) const {
546
+ return (*this < other) & Vectorized<double>(1.0);
547
+ }
548
+
549
+ inline Vectorized<double> Vectorized<double>::le(
550
+ const Vectorized<double>& other) const {
551
+ return (*this <= other) & Vectorized<double>(1.0);
552
+ }
553
+
554
+ template <>
555
+ Vectorized<double> inline fmadd(
556
+ const Vectorized<double>& a,
557
+ const Vectorized<double>& b,
558
+ const Vectorized<double>& c) {
559
+ return vfmaq_f64(c, a, b);
560
+ }
561
+
562
+ template <>
563
+ Vectorized<double> inline fnmadd(
564
+ const Vectorized<double>& a,
565
+ const Vectorized<double>& b,
566
+ const Vectorized<double>& c) {
567
+ return vfmsq_f64(c, a, b);
568
+ }
569
+
570
+ template <>
571
+ Vectorized<double> inline fmsub(
572
+ const Vectorized<double>& a,
573
+ const Vectorized<double>& b,
574
+ const Vectorized<double>& c) {
575
+ return vfmaq_f64(vnegq_f64(c), a, b);
576
+ }
577
+
578
+ template <>
579
+ Vectorized<double> inline fnmsub(
580
+ const Vectorized<double>& a,
581
+ const Vectorized<double>& b,
582
+ const Vectorized<double>& c) {
583
+ return vfmsq_f64(vnegq_f64(c), a, b);
584
+ }
585
+
586
+ } // namespace CPU_CAPABILITY
587
+ } // namespace at::vec
588
+
589
+ #else
590
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
591
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_float_neon.h ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with AVX]
6
+
7
+ #include <ATen/cpu/vec/intrinsics.h>
8
+ #include <ATen/cpu/vec/vec_base.h>
9
+ #include <c10/util/irange.h>
10
+
11
+ #if defined(__aarch64__) && defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
12
+ #include <sleef.h>
13
+ #endif
14
+
15
+ C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wswitch-default")
16
+
17
+ // Sleef offers vectorized versions of some transcedentals
18
+ // such as sin, cos, tan etc..
19
+ // However for now opting for STL, since we are not building
20
+ // with Sleef for mobile yet.
21
+
22
+ namespace at::vec {
23
+ // See Note [CPU_CAPABILITY namespace]
24
+ inline namespace CPU_CAPABILITY {
25
+
26
+ // Right now contains only aarch64 implementation.
27
+ // Due to follow two reasons aarch32 is not currently supported.
28
+ // 1. Due to difference in ISA been aarch32 and aarch64, intrinsics
29
+ // that work for aarch64 dont work for aarch32.
30
+ // 2. Android NDK r21 has problems with compiling aarch32.
31
+ // Clang seg faults.
32
+ // https://github.com/android/ndk/issues/1248
33
+ // https://bugs.llvm.org/show_bug.cgi?id=45824
34
+ // Most likely we will do aarch32 support with inline asm.
35
+ #if defined(__aarch64__)
36
+
37
+ #ifdef __BIG_ENDIAN__
38
+ #error "Big endian is not supported."
39
+ #endif
40
+
41
+ #if defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
42
+ #define USE_SLEEF(sleef_code, non_sleef_code) sleef_code
43
+ #else
44
+ #define USE_SLEEF(sleef_code, non_sleef_code) non_sleef_code
45
+ #endif
46
+
47
+ template <int index, bool mask_val>
48
+ struct BlendRegs {
49
+ static float32x4_t impl(
50
+ const float32x4_t& a,
51
+ const float32x4_t& b,
52
+ float32x4_t& res);
53
+ };
54
+
55
+ template <int index>
56
+ struct BlendRegs<index, true> {
57
+ static float32x4_t impl(
58
+ const float32x4_t& a,
59
+ const float32x4_t& b,
60
+ float32x4_t& res) {
61
+ return vsetq_lane_f32(vgetq_lane_f32(b, index), res, index);
62
+ }
63
+ };
64
+
65
+ template <int index>
66
+ struct BlendRegs<index, false> {
67
+ static float32x4_t impl(
68
+ const float32x4_t& a,
69
+ const float32x4_t& b,
70
+ float32x4_t& res) {
71
+ return vsetq_lane_f32(vgetq_lane_f32(a, index), res, index);
72
+ }
73
+ };
74
+
75
+ template <>
76
+ struct is_vec_specialized_for<float> : std::bool_constant<true> {};
77
+
78
+ template <>
79
+ class Vectorized<float> {
80
+ private:
81
+ float32x4_t values;
82
+
83
+ public:
84
+ using value_type = float;
85
+ using size_type = int;
86
+ static constexpr size_type size() {
87
+ return 4;
88
+ }
89
+ Vectorized() {
90
+ values = vmovq_n_f32(0);
91
+ }
92
+ Vectorized(float32x4_t v) : values(v) {}
93
+ Vectorized(float val) : values{vdupq_n_f32(val)} {}
94
+ Vectorized(float val0, float val1, float val2, float val3)
95
+ : values{val0, val1, val2, val3} {}
96
+ Vectorized(float (&arr)[4]) : Vectorized(arr[0], arr[1], arr[2], arr[3]) {}
97
+ operator float32x4_t() const {
98
+ return values;
99
+ }
100
+ template <int64_t mask>
101
+ static Vectorized<float> blend(
102
+ const Vectorized<float>& a,
103
+ const Vectorized<float>& b) {
104
+ Vectorized<float> vec;
105
+ vec.values = BlendRegs < 0,
106
+ (mask & 0x01) != 0 > ::impl(a.values, b.values, vec.values);
107
+ vec.values = BlendRegs < 1,
108
+ (mask & 0x02) != 0 > ::impl(a.values, b.values, vec.values);
109
+ vec.values = BlendRegs < 2,
110
+ (mask & 0x04) != 0 > ::impl(a.values, b.values, vec.values);
111
+ vec.values = BlendRegs < 3,
112
+ (mask & 0x08) != 0 > ::impl(a.values, b.values, vec.values);
113
+ return vec;
114
+ }
115
+ static Vectorized<float> blendv(
116
+ const Vectorized<float>& a,
117
+ const Vectorized<float>& b,
118
+ const Vectorized<float>& mask) {
119
+ // TODO
120
+ // NB: This requires that each value, i.e., each uint value,
121
+ // of the mask either all be zeros or all be 1s.
122
+ // We perhaps need some kind of an assert?
123
+ // But that will affect performance.
124
+ Vectorized<float> vec(mask.values);
125
+ vec.values =
126
+ vbslq_f32(vreinterpretq_u32_f32(vec.values), b.values, a.values);
127
+ return vec;
128
+ }
129
+ template <typename step_t>
130
+ static Vectorized<float> arange(
131
+ float base = 0.f,
132
+ step_t step = static_cast<step_t>(1)) {
133
+ const Vectorized<float> base_vec(base);
134
+ const Vectorized<float> step_vec(step);
135
+ const Vectorized<float> step_sizes(0, 1, 2, 3);
136
+ return fmadd(step_sizes, step_vec, base_vec);
137
+ }
138
+ static Vectorized<float> set(
139
+ const Vectorized<float>& a,
140
+ const Vectorized<float>& b,
141
+ int64_t count = size()) {
142
+ switch (count) {
143
+ case 0:
144
+ return a;
145
+ case 1: {
146
+ Vectorized<float> vec;
147
+ static uint32x4_t mask_low = {0xFFFFFFFF, 0x0, 0x0, 0x0};
148
+ vec.values = vreinterpretq_f32_u32(mask_low);
149
+ vec.values =
150
+ vbslq_f32(vreinterpretq_u32_f32(vec.values), b.values, a.values);
151
+ return vec;
152
+ }
153
+ case 2: {
154
+ Vectorized<float> vec;
155
+ static uint32x4_t mask_low = {0xFFFFFFFF, 0xFFFFFFFF, 0x0, 0x0};
156
+ vec.values = vreinterpretq_f32_u32(mask_low);
157
+ vec.values =
158
+ vbslq_f32(vreinterpretq_u32_f32(vec.values), b.values, a.values);
159
+ return vec;
160
+ }
161
+ case 3: {
162
+ Vectorized<float> vec;
163
+ static uint32x4_t mask_low = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0};
164
+ vec.values = vreinterpretq_f32_u32(mask_low);
165
+ vec.values =
166
+ vbslq_f32(vreinterpretq_u32_f32(vec.values), b.values, a.values);
167
+ return vec;
168
+ }
169
+ }
170
+ return b;
171
+ }
172
+ static Vectorized<float> loadu(const void* ptr, int64_t count = size()) {
173
+ if (count == size()) {
174
+ return vld1q_f32(reinterpret_cast<const float*>(ptr));
175
+ } else {
176
+ __at_align__ float tmp_values[size()];
177
+ for (const auto i : c10::irange(size())) {
178
+ tmp_values[i] = 0.0;
179
+ }
180
+ std::memcpy(
181
+ tmp_values,
182
+ reinterpret_cast<const float*>(ptr),
183
+ count * sizeof(float));
184
+ return vld1q_f32(reinterpret_cast<const float*>(tmp_values));
185
+ }
186
+ }
187
+ void store(void* ptr, int64_t count = size()) const {
188
+ if (count == size()) {
189
+ vst1q_f32(reinterpret_cast<float*>(ptr), values);
190
+ } else {
191
+ float tmp_values[size()];
192
+ vst1q_f32(reinterpret_cast<float*>(tmp_values), values);
193
+ std::memcpy(ptr, tmp_values, count * sizeof(float));
194
+ }
195
+ }
196
+ // Very slow implementation of indexing.
197
+ // Only required because vec256_qint refers to this.
198
+ // Once we specialize that implementation for ARM
199
+ // this should be removed. TODO (kimishpatel)
200
+ float operator[](int idx) const {
201
+ __at_align__ float tmp[size()];
202
+ store(tmp);
203
+ return tmp[idx];
204
+ }
205
+ float operator[](int idx) {
206
+ __at_align__ float tmp[size()];
207
+ store(tmp);
208
+ return tmp[idx];
209
+ }
210
+ int zero_mask() const {
211
+ uint32x4_t is_zero_vec = vceqzq_f32(values);
212
+ const int32x4_t shift = vcombine_s32(
213
+ vcreate_s32(0x0 | (int64_t(0x1) << 32)),
214
+ vcreate_s32(0x2 | (int64_t(0x3) << 32)));
215
+ uint32x4_t bits_vec =
216
+ vshlq_u32(vandq_u32(is_zero_vec, vdupq_n_u32(1)), shift);
217
+ return vaddvq_u32(bits_vec);
218
+ }
219
+ Vectorized<float> isnan() const {
220
+ return vreinterpretq_f32_u32(vmvnq_u32(vceqq_f32(values, values)));
221
+ }
222
+ bool has_inf_nan() const {
223
+ __at_align__ float tmp[size()];
224
+ store(tmp);
225
+ for (const auto i : c10::irange(size())) {
226
+ if (_isnan(tmp[i]) || _isinf(tmp[i])) {
227
+ return true;
228
+ }
229
+ }
230
+ return false;
231
+ }
232
+ Vectorized<float> map(float (*const f)(float)) const {
233
+ __at_align__ float tmp[size()];
234
+ store(tmp);
235
+ for (const auto i : c10::irange(size())) {
236
+ tmp[i] = f(tmp[i]);
237
+ }
238
+ return loadu(tmp);
239
+ }
240
+ Vectorized<float> map2(
241
+ const Vectorized<float>& second,
242
+ float (*const f)(float, float)) const {
243
+ __at_align__ float tmp[size()];
244
+ __at_align__ float tmp_second[size()];
245
+ store(tmp);
246
+ second.store(tmp_second);
247
+ for (const auto i : c10::irange(size())) {
248
+ tmp[i] = f(tmp[i], tmp_second[i]);
249
+ }
250
+ return loadu(tmp);
251
+ }
252
+ Vectorized<float> abs() const {
253
+ return Vectorized<float>(vabsq_f32(values));
254
+ }
255
+ Vectorized<float> angle() const {
256
+ auto zero = Vectorized<float>(0);
257
+ auto pi = Vectorized<float>(c10::pi<float>);
258
+ auto tmp = blendv(zero, pi, *this < zero);
259
+ return blendv(tmp, *this, isnan());
260
+ }
261
+ Vectorized<float> real() const {
262
+ return *this;
263
+ }
264
+ Vectorized<float> imag() const {
265
+ return Vectorized<float>(0.f);
266
+ }
267
+ Vectorized<float> conj() const {
268
+ return *this;
269
+ }
270
+ #define DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME( \
271
+ name, sleef_name) \
272
+ Vectorized<float> name() const { \
273
+ return USE_SLEEF(Vectorized<float>(sleef_name(values)), map(std::name)); \
274
+ }
275
+
276
+ #define DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(name) \
277
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME( \
278
+ name, Sleef_##name##f4_u10)
279
+
280
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(acos)
281
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(acosh)
282
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(asin)
283
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(asinh)
284
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(atan)
285
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(atanh)
286
+
287
+ #define DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME( \
288
+ name, sleef_name) \
289
+ Vectorized<float> name(const Vectorized<float>& arg) const { \
290
+ return USE_SLEEF( \
291
+ Vectorized<float>(sleef_name(values, arg.values)), \
292
+ map2(arg, std::name)); \
293
+ }
294
+
295
+ #define DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC(name) \
296
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME( \
297
+ name, Sleef_##name##f4_u10)
298
+
299
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC(atan2)
300
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME(
301
+ copysign,
302
+ Sleef_copysignf4)
303
+ Vectorized<float> erf() const;
304
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME(
305
+ erfc,
306
+ Sleef_erfcf4_u15)
307
+ Vectorized<float> erfinv() const {
308
+ return map(calc_erfinv);
309
+ }
310
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(exp)
311
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(exp2)
312
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(expm1)
313
+ // Implementation copied from Arm Optimized Routine
314
+ // https://github.com/ARM-software/optimized-routines/blob/master/math/aarch64/advsimd/expf.c
315
+ inline Vectorized<float> vexpq_f32_u20() const {
316
+ // bail out to sleef if it's a special case:
317
+ // i.e. there's an input s.t. |input| > 87.3....
318
+ const float32x4_t special_bound = vdupq_n_f32(0x1.5d5e2ap+6f);
319
+ uint32x4_t cmp = vcagtq_f32(values, special_bound);
320
+ if (vpaddd_u64(vreinterpretq_u64_u32(cmp)) != 0) {
321
+ return exp();
322
+ }
323
+
324
+ const float32x4_t inv_ln2 = vdupq_n_f32(0x1.715476p+0f);
325
+ const float ln2_hi = 0x1.62e4p-1f;
326
+ const float ln2_lo = 0x1.7f7d1cp-20f;
327
+ const float c0 = 0x1.0e4020p-7f;
328
+ const float c2 = 0x1.555e66p-3f;
329
+ const float32x4_t ln2_c02 = {ln2_hi, ln2_lo, c0, c2};
330
+
331
+ const uint32x4_t exponent_bias = vdupq_n_u32(0x3f800000);
332
+ const float32x4_t c1 = vdupq_n_f32(0x1.573e2ep-5f);
333
+ const float32x4_t c3 = vdupq_n_f32(0x1.fffdb6p-2f);
334
+ const float32x4_t c4 = vdupq_n_f32(0x1.ffffecp-1f);
335
+
336
+ /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
337
+ x = ln2*n + r, with r in [-ln2/2, ln2/2]. */
338
+
339
+ float32x4_t n = vrndaq_f32(vmulq_f32(values, inv_ln2));
340
+ float32x4_t r = vfmsq_laneq_f32(values, n, ln2_c02, 0);
341
+ r = vfmsq_laneq_f32(r, n, ln2_c02, 1);
342
+ uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_s32(vcvtq_s32_f32(n)), 23);
343
+ float32x4_t scale = vreinterpretq_f32_u32(vaddq_u32(e, exponent_bias));
344
+
345
+ float32x4_t r2 = vmulq_f32(r, r);
346
+ float32x4_t p = vfmaq_laneq_f32(c1, r, ln2_c02, 2);
347
+ float32x4_t q = vfmaq_laneq_f32(c3, r, ln2_c02, 3);
348
+ q = vfmaq_f32(q, p, r2);
349
+ p = vmulq_f32(c4, r);
350
+ float32x4_t poly = vfmaq_f32(p, q, r2);
351
+
352
+ return vfmaq_f32(scale, poly, scale);
353
+ }
354
+ Vectorized<float> exp_u20() const {
355
+ return vexpq_f32_u20();
356
+ }
357
+ Vectorized<float> fexp_u20() const {
358
+ return exp_u20();
359
+ }
360
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME(
361
+ fmod,
362
+ Sleef_fmodf4)
363
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME(
364
+ hypot,
365
+ Sleef_hypotf4_u05)
366
+ Vectorized<float> i0() const {
367
+ return map(calc_i0);
368
+ }
369
+ Vectorized<float> i0e() const {
370
+ return map(calc_i0e);
371
+ }
372
+ Vectorized<float> digamma() const {
373
+ return map(calc_digamma);
374
+ }
375
+ Vectorized<float> igamma(const Vectorized<float>& x) const {
376
+ return map2(x, calc_igamma);
377
+ }
378
+ Vectorized<float> igammac(const Vectorized<float>& x) const {
379
+ return map2(x, calc_igammac);
380
+ }
381
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(log)
382
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(log10)
383
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(log1p)
384
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(log2)
385
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC_WITH_SLEEF_NAME(
386
+ nextafter,
387
+ Sleef_nextafterf4)
388
+ Vectorized<float> frac() const;
389
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(sin)
390
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(sinh)
391
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(cos)
392
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(cosh)
393
+ Vectorized<float> ceil() const {
394
+ return map(at::native::ceil_impl);
395
+ }
396
+ Vectorized<float> floor() const {
397
+ return map(at::native::floor_impl);
398
+ }
399
+ Vectorized<float> neg() const {
400
+ return Vectorized<float>(vnegq_f32(values));
401
+ }
402
+ Vectorized<float> round() const {
403
+ // We do not use std::round because we would like to round midway numbers to
404
+ // the nearest even integer.
405
+ return map(at::native::round_impl);
406
+ }
407
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(tan)
408
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(tanh)
409
+ Vectorized<float> trunc() const {
410
+ return Vectorized<float>(vrndq_f32(values));
411
+ }
412
+ DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC(lgamma)
413
+ Vectorized<float> sqrt() const {
414
+ return Vectorized<float>(vsqrtq_f32(values));
415
+ }
416
+ Vectorized<float> reciprocal() const {
417
+ return Vectorized<float>(vdivq_f32(vdupq_n_f32(1.0f), values));
418
+ }
419
+ Vectorized<float> rsqrt() const {
420
+ return this->sqrt().reciprocal();
421
+ }
422
+ DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC(pow)
423
+ Vectorized<float> operator==(const Vectorized<float>& other) const {
424
+ return Vectorized<float>(
425
+ vreinterpretq_f32_u32(vceqq_f32(values, other.values)));
426
+ }
427
+
428
+ Vectorized<float> operator!=(const Vectorized<float>& other) const {
429
+ float32x4_t r0 =
430
+ vreinterpretq_f32_u32(vmvnq_u32(vceqq_f32(values, other.values)));
431
+ return Vectorized<float>(r0);
432
+ }
433
+
434
+ Vectorized<float> operator<(const Vectorized<float>& other) const {
435
+ return Vectorized<float>(
436
+ vreinterpretq_f32_u32(vcltq_f32(values, other.values)));
437
+ }
438
+
439
+ Vectorized<float> operator<=(const Vectorized<float>& other) const {
440
+ return Vectorized<float>(
441
+ vreinterpretq_f32_u32(vcleq_f32(values, other.values)));
442
+ }
443
+
444
+ Vectorized<float> operator>(const Vectorized<float>& other) const {
445
+ return Vectorized<float>(
446
+ vreinterpretq_f32_u32(vcgtq_f32(values, other.values)));
447
+ }
448
+
449
+ Vectorized<float> operator>=(const Vectorized<float>& other) const {
450
+ return Vectorized<float>(
451
+ vreinterpretq_f32_u32(vcgeq_f32(values, other.values)));
452
+ }
453
+
454
+ Vectorized<float> eq(const Vectorized<float>& other) const;
455
+ Vectorized<float> ne(const Vectorized<float>& other) const;
456
+ Vectorized<float> gt(const Vectorized<float>& other) const;
457
+ Vectorized<float> ge(const Vectorized<float>& other) const;
458
+ Vectorized<float> lt(const Vectorized<float>& other) const;
459
+ Vectorized<float> le(const Vectorized<float>& other) const;
460
+ };
461
+
462
+ template <>
463
+ Vectorized<float> inline operator+(
464
+ const Vectorized<float>& a,
465
+ const Vectorized<float>& b) {
466
+ return Vectorized<float>(vaddq_f32(a, b));
467
+ }
468
+
469
+ template <>
470
+ Vectorized<float> inline operator-(
471
+ const Vectorized<float>& a,
472
+ const Vectorized<float>& b) {
473
+ return Vectorized<float>(vsubq_f32(a, b));
474
+ }
475
+
476
+ template <>
477
+ Vectorized<float> inline operator*(
478
+ const Vectorized<float>& a,
479
+ const Vectorized<float>& b) {
480
+ return Vectorized<float>(vmulq_f32(a, b));
481
+ }
482
+
483
+ template <>
484
+ Vectorized<float> inline operator/(
485
+ const Vectorized<float>& a,
486
+ const Vectorized<float>& b) {
487
+ return Vectorized<float>(vdivq_f32(a, b));
488
+ }
489
+
490
+ // frac. Implement this here so we can use subtraction
491
+ inline Vectorized<float> Vectorized<float>::frac() const {
492
+ return *this - this->trunc();
493
+ }
494
+
495
+ template <>
496
+ Vectorized<float> inline maximum(
497
+ const Vectorized<float>& a,
498
+ const Vectorized<float>& b) {
499
+ return Vectorized<float>(vmaxq_f32(a, b));
500
+ }
501
+
502
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
503
+ // either input is a NaN.
504
+ template <>
505
+ Vectorized<float> inline minimum(
506
+ const Vectorized<float>& a,
507
+ const Vectorized<float>& b) {
508
+ return Vectorized<float>(vminq_f32(a, b));
509
+ }
510
+
511
+ template <>
512
+ Vectorized<float> inline clamp(
513
+ const Vectorized<float>& a,
514
+ const Vectorized<float>& min,
515
+ const Vectorized<float>& max) {
516
+ return minimum(max, maximum(min, a));
517
+ }
518
+
519
+ template <>
520
+ Vectorized<float> inline clamp_max(
521
+ const Vectorized<float>& a,
522
+ const Vectorized<float>& max) {
523
+ return minimum(max, a);
524
+ }
525
+
526
+ template <>
527
+ Vectorized<float> inline clamp_min(
528
+ const Vectorized<float>& a,
529
+ const Vectorized<float>& min) {
530
+ return maximum(min, a);
531
+ }
532
+
533
+ template <>
534
+ Vectorized<float> inline operator&(
535
+ const Vectorized<float>& a,
536
+ const Vectorized<float>& b) {
537
+ return Vectorized<float>(vreinterpretq_f32_u32(
538
+ vandq_u32(vreinterpretq_u32_f32(a), vreinterpretq_u32_f32(b))));
539
+ }
540
+
541
+ template <>
542
+ Vectorized<float> inline operator|(
543
+ const Vectorized<float>& a,
544
+ const Vectorized<float>& b) {
545
+ return Vectorized<float>(vreinterpretq_f32_u32(
546
+ vorrq_u32(vreinterpretq_u32_f32(a), vreinterpretq_u32_f32(b))));
547
+ }
548
+
549
+ template <>
550
+ Vectorized<float> inline operator^(
551
+ const Vectorized<float>& a,
552
+ const Vectorized<float>& b) {
553
+ return Vectorized<float>(vreinterpretq_f32_u32(
554
+ veorq_u32(vreinterpretq_u32_f32(a), vreinterpretq_u32_f32(b))));
555
+ }
556
+
557
+ inline Vectorized<float> Vectorized<float>::eq(
558
+ const Vectorized<float>& other) const {
559
+ return (*this == other) & Vectorized<float>(1.0f);
560
+ }
561
+
562
+ inline Vectorized<float> Vectorized<float>::ne(
563
+ const Vectorized<float>& other) const {
564
+ return (*this != other) & Vectorized<float>(1.0f);
565
+ }
566
+
567
+ inline Vectorized<float> Vectorized<float>::gt(
568
+ const Vectorized<float>& other) const {
569
+ return (*this > other) & Vectorized<float>(1.0f);
570
+ }
571
+
572
+ inline Vectorized<float> Vectorized<float>::ge(
573
+ const Vectorized<float>& other) const {
574
+ return (*this >= other) & Vectorized<float>(1.0f);
575
+ }
576
+
577
+ inline Vectorized<float> Vectorized<float>::lt(
578
+ const Vectorized<float>& other) const {
579
+ return (*this < other) & Vectorized<float>(1.0f);
580
+ }
581
+
582
+ inline Vectorized<float> Vectorized<float>::le(
583
+ const Vectorized<float>& other) const {
584
+ return (*this <= other) & Vectorized<float>(1.0f);
585
+ }
586
+
587
+ template <>
588
+ Vectorized<float> inline fmadd(
589
+ const Vectorized<float>& a,
590
+ const Vectorized<float>& b,
591
+ const Vectorized<float>& c) {
592
+ return Vectorized<float>(vfmaq_f32(c, a, b));
593
+ }
594
+
595
+ template <>
596
+ Vectorized<float> inline fnmadd(
597
+ const Vectorized<float>& a,
598
+ const Vectorized<float>& b,
599
+ const Vectorized<float>& c) {
600
+ return Vectorized<float>(vfmsq_f32(c, a, b));
601
+ }
602
+
603
+ template <>
604
+ Vectorized<float> inline fmsub(
605
+ const Vectorized<float>& a,
606
+ const Vectorized<float>& b,
607
+ const Vectorized<float>& c) {
608
+ return Vectorized<float>(vnegq_f32(vfmsq_f32(c, a, b)));
609
+ }
610
+
611
+ template <>
612
+ Vectorized<float> inline fnmsub(
613
+ const Vectorized<float>& a,
614
+ const Vectorized<float>& b,
615
+ const Vectorized<float>& c) {
616
+ return Vectorized<float>(vnegq_f32(vfmaq_f32(c, a, b)));
617
+ }
618
+
619
+ inline Vectorized<float> Vectorized<float>::erf() const {
620
+ // constants
621
+ const Vectorized<float> neg_zero_vec(-0.f);
622
+ const Vectorized<float> one_vec(1.0f);
623
+ const Vectorized<float> p(0.3275911f);
624
+ const Vectorized<float> p1(0.254829592f);
625
+ const Vectorized<float> p2(-0.284496736f);
626
+ const Vectorized<float> p3(1.421413741f);
627
+ const Vectorized<float> p4(-1.453152027f);
628
+ const Vectorized<float> p5(1.061405429f);
629
+ // sign(x)
630
+ auto sign_mask = neg_zero_vec & *this;
631
+ auto abs_vec = this->abs();
632
+ // t = 1 / (p * abs(x) + 1)
633
+ auto tmp0 = fmadd(p, abs_vec, one_vec);
634
+ auto t = one_vec / tmp0;
635
+ // r = p5 * t ^ 4 + p4 * t ^ 3 + p3 * t ^ 2 + p2 * t + p1
636
+ auto tmp1 = fmadd(p5, t, p4);
637
+ auto tmp2 = fmadd(tmp1, t, p3);
638
+ auto tmp3 = fmadd(tmp2, t, p2);
639
+ auto r = fmadd(tmp3, t, p1);
640
+ // - exp(- x * x)
641
+ auto pow_2 = (*this) * (*this);
642
+ auto neg_pow_2 = pow_2 ^ neg_zero_vec;
643
+ auto tmp4 = neg_pow_2.vexpq_f32_u20();
644
+ auto tmp5 = tmp4 ^ neg_zero_vec;
645
+ // erf(x) = sign(x) * (1 - r * t * exp(- x * x))
646
+ auto tmp6 = t * tmp5;
647
+ auto tmp7 = fmadd(tmp6, r, one_vec);
648
+ return tmp7 ^ sign_mask;
649
+ }
650
+ #undef DEFINE_SLEEF_COMPATIBLE_BINARY_ELEMENTWISE_FUNC
651
+ #undef DEFINE_SLEEF_COMPATIBLE_UNARY_ELEMENTWISE_FUNC
652
+ #endif /* defined(aarch64) */
653
+
654
+ } // namespace CPU_CAPABILITY
655
+ } // namespace at::vec
656
+
657
+ C10_DIAGNOSTIC_POP()
658
+
659
+ #else
660
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
661
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_half_neon.h ADDED
@@ -0,0 +1,627 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
5
+ // See Note [Do not compile initializers with AVX]
6
+
7
+ #include <ATen/cpu/vec/intrinsics.h>
8
+ #include <ATen/cpu/vec/vec128/vec128_convert.h>
9
+ #include <ATen/cpu/vec/vec128/vec128_float_neon.h>
10
+ #include <ATen/cpu/vec/vec128/vec128_reduced_precision_common_neon.h>
11
+ #include <ATen/cpu/vec/vec_base.h>
12
+ #include <c10/util/Half.h>
13
+ #include <c10/util/irange.h>
14
+
15
+ namespace at::vec {
16
+ // See Note [CPU_CAPABILITY namespace]
17
+ inline namespace CPU_CAPABILITY {
18
+
19
+ // Right now contains only aarch64 implementation.
20
+ // Due to follow two reasons aarch32 is not currently supported.
21
+ // 1. Due to difference in ISA been aarch32 and aarch64, intrinsics
22
+ // that work for aarch64 dont work for aarch32.
23
+ // 2. Android NDK r21 has problems with compiling aarch32.
24
+ // Clang seg faults.
25
+ // https://github.com/android/ndk/issues/1248
26
+ // https://bugs.llvm.org/show_bug.cgi?id=45824
27
+ // Most likely we will do aarch32 support with inline asm.
28
+ #if !defined(C10_MOBILE) && defined(__aarch64__)
29
+
30
+ #ifdef __BIG_ENDIAN__
31
+ #error "Big endian is not supported."
32
+ #endif
33
+
34
+ template <int index, bool mask_val>
35
+ struct BlendHalfRegs {
36
+ static float16x8_t impl(
37
+ const float16x8_t& a,
38
+ const float16x8_t& b,
39
+ float16x8_t& res);
40
+ };
41
+
42
+ template <int index>
43
+ struct BlendHalfRegs<index, true> {
44
+ static float16x8_t impl(
45
+ const float16x8_t& a,
46
+ const float16x8_t& b,
47
+ float16x8_t& res) {
48
+ return vsetq_lane_f16(vgetq_lane_f16(b, index), res, index);
49
+ }
50
+ };
51
+
52
+ template <int index>
53
+ struct BlendHalfRegs<index, false> {
54
+ static float16x8_t impl(
55
+ const float16x8_t& a,
56
+ const float16x8_t& b,
57
+ float16x8_t& res) {
58
+ return vsetq_lane_f16(vgetq_lane_f16(a, index), res, index);
59
+ }
60
+ };
61
+
62
+ template <>
63
+ struct is_vec_specialized_for<c10::Half> : std::bool_constant<true> {};
64
+
65
+ // On ARM, Half type supports float16_t->Half constructor and Half->float16_t
66
+ // conversion
67
+ template <>
68
+ class Vectorized<c10::Half> : public Vectorized16<
69
+ float16x8_t,
70
+ c10::Half,
71
+ BlendHalfRegs,
72
+ Vectorized<c10::Half>> {
73
+ using Base = Vectorized16<
74
+ float16x8_t,
75
+ c10::Half,
76
+ BlendHalfRegs,
77
+ Vectorized<c10::Half>>;
78
+ friend Base;
79
+
80
+ private:
81
+ // We use these private map functions to implement various methods
82
+ Vectorized<c10::Half> map_with_vec_float_method(
83
+ Vectorized<float> (Vectorized<float>::*m)() const) const {
84
+ float32x4_t v00 = vcvt_f32_f16(vget_low_f16(values));
85
+ float32x4_t v01 = vcvt_f32_f16(vget_high_f16(values));
86
+ Vectorized<float> mv0 = (Vectorized<float>(v00).*m)();
87
+ Vectorized<float> mv1 = (Vectorized<float>(v01).*m)();
88
+ float16x4_t r00 = vcvt_f16_f32(mv0);
89
+ float16x4_t r01 = vcvt_f16_f32(mv1);
90
+ return Vectorized<c10::Half>(vcombine_f16(r00, r01));
91
+ }
92
+
93
+ Vectorized<c10::Half> map2_with_vec_float_method(
94
+ const Vectorized<c10::Half>& second,
95
+ Vectorized<float> (Vectorized<float>::*m)(const Vectorized<float>&)
96
+ const) const {
97
+ float32x4_t v00 = vcvt_f32_f16(vget_low_f16(values));
98
+ float32x4_t v01 = vcvt_f32_f16(vget_high_f16(values));
99
+ float32x4_t second_v00 = vcvt_f32_f16(vget_low_f16(second.values));
100
+ float32x4_t second_v01 = vcvt_f32_f16(vget_high_f16(second.values));
101
+ Vectorized<float> mv0 =
102
+ (Vectorized<float>(v00).*m)(Vectorized<float>(second_v00));
103
+ Vectorized<float> mv1 =
104
+ (Vectorized<float>(v01).*m)(Vectorized<float>(second_v01));
105
+ float16x4_t r00 = vcvt_f16_f32(mv0);
106
+ float16x4_t r01 = vcvt_f16_f32(mv1);
107
+
108
+ // Pack result into Vectorized<c10::Half>
109
+ return Vectorized<c10::Half>(vcombine_f16(r00, r01));
110
+ }
111
+
112
+ Vectorized<c10::Half> map2_bitmask_with_vec_float_method(
113
+ const Vectorized<c10::Half>& second,
114
+ Vectorized<float> (Vectorized<float>::*m)(const Vectorized<float>&)
115
+ const) const {
116
+ float32x4_t v00 = vcvt_f32_f16(vget_low_f16(values));
117
+ float32x4_t v01 = vcvt_f32_f16(vget_high_f16(values));
118
+ float32x4_t second_v00 = vcvt_f32_f16(vget_low_f16(second.values));
119
+ float32x4_t second_v01 = vcvt_f32_f16(vget_high_f16(second.values));
120
+ Vectorized<float> mv0 =
121
+ (Vectorized<float>(v00).*m)(Vectorized<float>(second_v00));
122
+ Vectorized<float> mv1 =
123
+ (Vectorized<float>(v01).*m)(Vectorized<float>(second_v01));
124
+ // Assume the operator returns a bitmask, not "real" floats, and
125
+ // just narrow the bits. All-ones is a NaN and will get mangled by
126
+ // conversion!
127
+ float16x4_t r00 =
128
+ vreinterpret_f16_u16(vmovn_u32(vreinterpretq_u32_f32(mv0)));
129
+ float16x4_t r01 =
130
+ vreinterpret_f16_u16(vmovn_u32(vreinterpretq_u32_f32(mv1)));
131
+
132
+ // Pack result into Vectorized<c10::Half>
133
+ return Vectorized<c10::Half>(vcombine_f16(r00, r01));
134
+ }
135
+
136
+ public:
137
+ using Vectorized16::Vectorized16;
138
+
139
+ Vectorized() = default;
140
+
141
+ // A ctor that accepts c10::Half is needed to fit interface with vec_base.h
142
+ // A second constructor that takes float16_t is also included
143
+ Vectorized(c10::Half val) : Vectorized((float16_t)val) {}
144
+ Vectorized(float16_t val) : Vectorized16(vdupq_n_f16(val)) {}
145
+ Vectorized(
146
+ value_type val0,
147
+ value_type val1,
148
+ value_type val2,
149
+ value_type val3,
150
+ value_type val4,
151
+ value_type val5,
152
+ value_type val6,
153
+ value_type val7)
154
+ : Vectorized16(
155
+ float16x8_t{val0, val1, val2, val3, val4, val5, val6, val7}) {}
156
+
157
+ static Vectorized<c10::Half> blendv(
158
+ const Vectorized<c10::Half>& a,
159
+ const Vectorized<c10::Half>& b,
160
+ const Vectorized<c10::Half>& mask) {
161
+ // Note: using blendv is very awkward because 0xFFFF is one of
162
+ // many NaN's in FP16 It's unfortunate that the mask has type Half
163
+ // (required from vec_base)
164
+
165
+ // TODO
166
+ // NB: This requires that each value, i.e., each uint value,
167
+ // of the mask either all be zeros or all be 1s.
168
+ // We perhaps need some kind of an assert?
169
+ // But that will affect performance.
170
+
171
+ // NOTE [vbslq_f16]: vbslq_f16 doesn't work on clang without
172
+ // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC. vbslq_u16 generates the
173
+ // same instruction anyway. see https://godbolt.org/z/cY4a55Y7P
174
+ Vectorized<c10::Half> vec(mask.values);
175
+ vec.values = vreinterpretq_f16_u16(vbslq_u16(
176
+ vreinterpretq_u16_f16(vec.values),
177
+ vreinterpretq_u16_f16(b.values),
178
+ vreinterpretq_u16_f16(a.values)));
179
+ return vec;
180
+ }
181
+ static Vectorized<c10::Half> set(
182
+ const Vectorized<c10::Half>& a,
183
+ const Vectorized<c10::Half>& b,
184
+ int64_t count = size()) {
185
+ uint16_t pre_mask[size()] = {0};
186
+ for (int i = 0; i < count; i++) {
187
+ pre_mask[i] = 0xFFFF;
188
+ }
189
+ uint16x8_t mask = vld1q_u16(pre_mask);
190
+
191
+ // Using blendv is awkward because 0xFFFF is one of many NaN's in FP16
192
+ // so we directly use vbslq_u16 instead. (See NOTE [vbslq_f16] above.)
193
+ Vectorized<c10::Half> vec(vreinterpretq_f16_u16(vbslq_u16(
194
+ mask,
195
+ vreinterpretq_u16_f16(b.values),
196
+ vreinterpretq_u16_f16(a.values))));
197
+
198
+ return vec;
199
+ }
200
+ static Vectorized<c10::Half> loadu(const void* ptr, int64_t count = size()) {
201
+ if (count == size()) {
202
+ return vld1q_f16(reinterpret_cast<const float16_t*>(ptr));
203
+ }
204
+ __at_align__ float16_t tmp_values[size()];
205
+ for (const auto i : c10::irange(size())) {
206
+ tmp_values[i] = 0;
207
+ }
208
+ std::memcpy(
209
+ tmp_values,
210
+ reinterpret_cast<const float16_t*>(ptr),
211
+ count * sizeof(float16_t));
212
+ return vld1q_f16(reinterpret_cast<const float16_t*>(tmp_values));
213
+ }
214
+ void store(void* ptr, int64_t count = size()) const {
215
+ if (count == size()) {
216
+ vst1q_f16(reinterpret_cast<float16_t*>(ptr), values);
217
+ return;
218
+ } else {
219
+ float16_t tmp_values[size()];
220
+ vst1q_f16(reinterpret_cast<float16_t*>(tmp_values), values);
221
+ std::memcpy(ptr, tmp_values, count * sizeof(float16_t));
222
+ }
223
+ }
224
+ int zero_mask() const {
225
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
226
+ uint16x8_t is_zero_vec = vceqzq_f16(values);
227
+ const int16x8_t shift = vcombine_s16(
228
+ vcreate_s16(
229
+ 0x0 | (int64_t(0x1) << 16) | (int64_t(0x2) << 32) |
230
+ (int64_t(0x3) << 48)),
231
+ vcreate_s16(
232
+ 0x4 | (int64_t(0x5) << 16) | (int64_t(0x6) << 32) |
233
+ (int64_t(0x7) << 48)));
234
+ uint16x8_t bits_vec =
235
+ vshlq_u16(vandq_u16(is_zero_vec, vdupq_n_u16(1)), shift);
236
+ return vaddvq_u16(bits_vec);
237
+ #else // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
238
+ // use known working implementation.
239
+ __at_align__ value_type tmp[size()];
240
+ store(tmp);
241
+ int mask = 0;
242
+ for (int i = 0; i < size(); ++i) {
243
+ if (tmp[i] == 0) {
244
+ mask |= (1 << i);
245
+ }
246
+ }
247
+ return mask;
248
+ #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
249
+ }
250
+ Vectorized<c10::Half> isnan() const {
251
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
252
+ return vreinterpretq_f16_u16(vmvnq_u16(vceqq_f16(values, values)));
253
+ #else
254
+ // NOTE: we could make this faster by doing vectorized checks of
255
+ // exponent/payload bits.
256
+ __at_align__ c10::Half tmp[size()];
257
+ __at_align__ c10::Half res[size()];
258
+ store(tmp);
259
+ for (const auto i : c10::irange(size())) {
260
+ if (_isnan(tmp[i])) {
261
+ std::memset(static_cast<void*>(&res[i]), 0xFF, sizeof(c10::Half));
262
+ } else {
263
+ std::memset(static_cast<void*>(&res[i]), 0, sizeof(c10::Half));
264
+ }
265
+ }
266
+ return loadu(res);
267
+ #endif
268
+ }
269
+ bool has_inf_nan() const {
270
+ __at_align__ c10::Half tmp[size()];
271
+ store(tmp);
272
+ for (const auto i : c10::irange(size())) {
273
+ if (_isnan(tmp[i]) || _isinf(tmp[i])) {
274
+ return true;
275
+ }
276
+ }
277
+ return false;
278
+ }
279
+ Vectorized<c10::Half> abs() const {
280
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
281
+ return Vectorized<c10::Half>(vabsq_f16(values));
282
+ #else
283
+ return map_with_vec_float_method(&Vectorized<float>::abs);
284
+ #endif
285
+ }
286
+ Vectorized<c10::Half> frac() const;
287
+ Vectorized<c10::Half> neg() const {
288
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
289
+ return Vectorized<c10::Half>(vnegq_f16(values));
290
+ #else
291
+ return map_with_vec_float_method(&Vectorized<float>::neg);
292
+ #endif
293
+ }
294
+ Vectorized<c10::Half> trunc() const {
295
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
296
+ return Vectorized<c10::Half>(vrndq_f16(values));
297
+ #else
298
+ return map_with_vec_float_method(&Vectorized<float>::trunc);
299
+ #endif
300
+ }
301
+ Vectorized<c10::Half> sqrt() const {
302
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
303
+ return Vectorized<c10::Half>(vsqrtq_f16(values));
304
+ #else
305
+ return map_with_vec_float_method(&Vectorized<float>::sqrt);
306
+ #endif
307
+ }
308
+ Vectorized<c10::Half> reciprocal() const {
309
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
310
+ auto ones = vdupq_n_f16(1.0f);
311
+ return Vectorized<c10::Half>(vdivq_f16(ones, values));
312
+ #else
313
+ return map_with_vec_float_method(&Vectorized<float>::reciprocal);
314
+ #endif
315
+ }
316
+ Vectorized<c10::Half> operator==(const Vectorized<c10::Half>& other) const {
317
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
318
+ return Vectorized<c10::Half>(
319
+ vreinterpretq_f16_u16(vceqq_f16(values, other.values)));
320
+ #else
321
+ return map2_bitmask_with_vec_float_method(
322
+ other, &Vectorized<float>::operator==);
323
+ #endif
324
+ }
325
+
326
+ Vectorized<c10::Half> operator!=(const Vectorized<c10::Half>& other) const {
327
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
328
+ return Vectorized<c10::Half>(
329
+ vreinterpretq_f16_u16(vmvnq_u16(vceqq_f16(values, other.values))));
330
+ #else
331
+ return map2_bitmask_with_vec_float_method(
332
+ other, &Vectorized<float>::operator!=);
333
+ #endif
334
+ }
335
+
336
+ Vectorized<c10::Half> operator<(const Vectorized<c10::Half>& other) const {
337
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
338
+ return Vectorized<c10::Half>(
339
+ vreinterpretq_f16_u16(vcltq_f16(values, other.values)));
340
+ #else
341
+ return map2_bitmask_with_vec_float_method(
342
+ other, &Vectorized<float>::operator<);
343
+ #endif
344
+ }
345
+
346
+ Vectorized<c10::Half> operator<=(const Vectorized<c10::Half>& other) const {
347
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
348
+ return Vectorized<c10::Half>(
349
+ vreinterpretq_f16_u16(vcleq_f16(values, other.values)));
350
+ #else
351
+ return map2_bitmask_with_vec_float_method(
352
+ other, &Vectorized<float>::operator<=);
353
+ #endif
354
+ }
355
+
356
+ Vectorized<c10::Half> operator>(const Vectorized<c10::Half>& other) const {
357
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
358
+ return Vectorized<c10::Half>(
359
+ vreinterpretq_f16_u16(vcgtq_f16(values, other.values)));
360
+ #else
361
+ return map2_bitmask_with_vec_float_method(
362
+ other, &Vectorized<float>::operator>);
363
+ #endif
364
+ }
365
+
366
+ Vectorized<c10::Half> operator>=(const Vectorized<c10::Half>& other) const {
367
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
368
+ return Vectorized<c10::Half>(
369
+ vreinterpretq_f16_u16(vcgeq_f16(values, other.values)));
370
+ #else
371
+ return map2_bitmask_with_vec_float_method(
372
+ other, &Vectorized<float>::operator>=);
373
+ #endif
374
+ }
375
+
376
+ Vectorized<c10::Half> eq(const Vectorized<c10::Half>& other) const;
377
+ Vectorized<c10::Half> ne(const Vectorized<c10::Half>& other) const;
378
+ Vectorized<c10::Half> gt(const Vectorized<c10::Half>& other) const;
379
+ Vectorized<c10::Half> ge(const Vectorized<c10::Half>& other) const;
380
+ Vectorized<c10::Half> lt(const Vectorized<c10::Half>& other) const;
381
+ Vectorized<c10::Half> le(const Vectorized<c10::Half>& other) const;
382
+ }; // Vectorized<Half>
383
+
384
+ inline std::tuple<Vectorized<float>, Vectorized<float>> convert_half_float(
385
+ const Vectorized<Half>& a) {
386
+ static_assert(Vectorized<Half>::size() == 2 * Vectorized<float>::size());
387
+ float16x8_t x = a;
388
+ float32x4_t x1 = vcvt_f32_f16(vget_low_f16(x));
389
+ float32x4_t x2 = vcvt_f32_f16(vget_high_f16(x));
390
+ return {Vectorized<float>(x1), Vectorized<float>(x2)};
391
+ }
392
+ inline Vectorized<Half> convert_float_half(
393
+ const Vectorized<float>& a,
394
+ const Vectorized<float>& b) {
395
+ static_assert(Vectorized<Half>::size() == 2 * Vectorized<float>::size());
396
+ float32x4_t x = a;
397
+ float32x4_t y = b;
398
+ float16x4_t x1 = vcvt_f16_f32(x);
399
+ float16x4_t x2 = vcvt_f16_f32(y);
400
+ return Vectorized<Half>(vcombine_f16(x1, x2));
401
+ }
402
+
403
+ template <typename Op>
404
+ Vectorized<c10::Half> binary_operator_via_float(
405
+ Op op,
406
+ const Vectorized<c10::Half>& a,
407
+ const Vectorized<c10::Half>& b) {
408
+ const auto [a_float_low, a_float_high] = convert_half_float(a);
409
+ const auto [b_float_low, b_float_high] = convert_half_float(b);
410
+ return convert_float_half(
411
+ op(a_float_low, b_float_low), op(a_float_high, b_float_high));
412
+ }
413
+
414
+ template <>
415
+ Vectorized<c10::Half> inline operator+(
416
+ const Vectorized<c10::Half>& a,
417
+ const Vectorized<c10::Half>& b) {
418
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
419
+ return Vectorized<c10::Half>(vaddq_f16(a, b));
420
+ #else
421
+ return binary_operator_via_float(std::plus<Vectorized<float>>(), a, b);
422
+ #endif
423
+ }
424
+
425
+ template <>
426
+ Vectorized<c10::Half> inline operator-(
427
+ const Vectorized<c10::Half>& a,
428
+ const Vectorized<c10::Half>& b) {
429
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
430
+ return Vectorized<c10::Half>(vsubq_f16(a, b));
431
+ #else
432
+ return binary_operator_via_float(std::minus<Vectorized<float>>(), a, b);
433
+ #endif
434
+ }
435
+
436
+ template <>
437
+ Vectorized<c10::Half> inline operator*(
438
+ const Vectorized<c10::Half>& a,
439
+ const Vectorized<c10::Half>& b) {
440
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
441
+ return Vectorized<c10::Half>(vmulq_f16(a, b));
442
+ #else
443
+ return binary_operator_via_float(std::multiplies<Vectorized<float>>(), a, b);
444
+ #endif
445
+ }
446
+
447
+ template <>
448
+ Vectorized<c10::Half> inline operator/(
449
+ const Vectorized<c10::Half>& a,
450
+ const Vectorized<c10::Half>& b) {
451
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
452
+ return Vectorized<c10::Half>(vdivq_f16(a, b));
453
+ #else
454
+ return binary_operator_via_float(std::divides<Vectorized<float>>(), a, b);
455
+ #endif
456
+ }
457
+
458
+ // frac. Implement this here so we can use subtraction
459
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::frac() const {
460
+ return *this - this->trunc();
461
+ }
462
+
463
+ // Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
464
+ // either input is a NaN.
465
+ template <>
466
+ Vectorized<c10::Half> inline maximum(
467
+ const Vectorized<c10::Half>& a,
468
+ const Vectorized<c10::Half>& b) {
469
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
470
+ return Vectorized<c10::Half>(vmaxq_f16(a, b));
471
+ #else
472
+ return binary_operator_via_float(
473
+ static_cast<Vectorized<float> (*)(
474
+ const Vectorized<float>&, const Vectorized<float>&)>(&maximum),
475
+ a,
476
+ b);
477
+ #endif
478
+ }
479
+
480
+ // Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
481
+ // either input is a NaN.
482
+ template <>
483
+ Vectorized<c10::Half> inline minimum(
484
+ const Vectorized<c10::Half>& a,
485
+ const Vectorized<c10::Half>& b) {
486
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
487
+ return Vectorized<c10::Half>(vminq_f16(a, b));
488
+ #else
489
+ return binary_operator_via_float(
490
+ static_cast<Vectorized<float> (*)(
491
+ const Vectorized<float>&, const Vectorized<float>&)>(&minimum),
492
+ a,
493
+ b);
494
+ #endif
495
+ }
496
+
497
+ template <>
498
+ Vectorized<c10::Half> inline clamp(
499
+ const Vectorized<c10::Half>& a,
500
+ const Vectorized<c10::Half>& min,
501
+ const Vectorized<c10::Half>& max) {
502
+ return minimum(max, maximum(min, a));
503
+ }
504
+
505
+ template <>
506
+ Vectorized<c10::Half> inline clamp_max(
507
+ const Vectorized<c10::Half>& a,
508
+ const Vectorized<c10::Half>& max) {
509
+ return minimum(max, a);
510
+ }
511
+
512
+ template <>
513
+ Vectorized<c10::Half> inline clamp_min(
514
+ const Vectorized<c10::Half>& a,
515
+ const Vectorized<c10::Half>& min) {
516
+ return maximum(min, a);
517
+ }
518
+
519
+ template <>
520
+ Vectorized<c10::Half> inline operator&(
521
+ const Vectorized<c10::Half>& a,
522
+ const Vectorized<c10::Half>& b) {
523
+ return Vectorized<c10::Half>(vreinterpretq_f16_u16(
524
+ vandq_u16(vreinterpretq_u16_f16(a), vreinterpretq_u16_f16(b))));
525
+ }
526
+
527
+ template <>
528
+ Vectorized<c10::Half> inline operator|(
529
+ const Vectorized<c10::Half>& a,
530
+ const Vectorized<c10::Half>& b) {
531
+ return Vectorized<c10::Half>(vreinterpretq_f16_u16(
532
+ vorrq_u16(vreinterpretq_u16_f16(a), vreinterpretq_u16_f16(b))));
533
+ }
534
+
535
+ template <>
536
+ Vectorized<c10::Half> inline operator^(
537
+ const Vectorized<c10::Half>& a,
538
+ const Vectorized<c10::Half>& b) {
539
+ return Vectorized<c10::Half>(vreinterpretq_f16_u16(
540
+ veorq_u16(vreinterpretq_u16_f16(a), vreinterpretq_u16_f16(b))));
541
+ }
542
+
543
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::eq(
544
+ const Vectorized<c10::Half>& other) const {
545
+ return (*this == other) & Vectorized<c10::Half>(1);
546
+ }
547
+
548
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::ne(
549
+ const Vectorized<c10::Half>& other) const {
550
+ return (*this != other) & Vectorized<c10::Half>(1);
551
+ }
552
+
553
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::gt(
554
+ const Vectorized<c10::Half>& other) const {
555
+ return (*this > other) & Vectorized<c10::Half>(1);
556
+ }
557
+
558
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::ge(
559
+ const Vectorized<c10::Half>& other) const {
560
+ return (*this >= other) & Vectorized<c10::Half>(1);
561
+ }
562
+
563
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::lt(
564
+ const Vectorized<c10::Half>& other) const {
565
+ return (*this < other) & Vectorized<c10::Half>(1);
566
+ }
567
+
568
+ inline Vectorized<c10::Half> Vectorized<c10::Half>::le(
569
+ const Vectorized<c10::Half>& other) const {
570
+ return (*this <= other) & Vectorized<c10::Half>(1);
571
+ }
572
+
573
+ template <>
574
+ Vectorized<c10::Half> inline fmadd(
575
+ const Vectorized<c10::Half>& a,
576
+ const Vectorized<c10::Half>& b,
577
+ const Vectorized<c10::Half>& c) {
578
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
579
+ return Vectorized<c10::Half>(vfmaq_f16(c, a, b));
580
+ #else
581
+ return a * b + c;
582
+ #endif
583
+ }
584
+
585
+ template <>
586
+ Vectorized<c10::Half> inline fnmadd(
587
+ const Vectorized<c10::Half>& a,
588
+ const Vectorized<c10::Half>& b,
589
+ const Vectorized<c10::Half>& c) {
590
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
591
+ return Vectorized<c10::Half>(vfmsq_f16(c, a, b));
592
+ #else
593
+ return -a * b + c;
594
+ #endif
595
+ }
596
+
597
+ template <>
598
+ Vectorized<c10::Half> inline fmsub(
599
+ const Vectorized<c10::Half>& a,
600
+ const Vectorized<c10::Half>& b,
601
+ const Vectorized<c10::Half>& c) {
602
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
603
+ return Vectorized<c10::Half>(vnegq_f16(vfmsq_f16(c, a, b)));
604
+ #else
605
+ return a * b - c;
606
+ #endif
607
+ }
608
+
609
+ template <>
610
+ Vectorized<c10::Half> inline fnmsub(
611
+ const Vectorized<c10::Half>& a,
612
+ const Vectorized<c10::Half>& b,
613
+ const Vectorized<c10::Half>& c) {
614
+ #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
615
+ return Vectorized<c10::Half>(vnegq_f16(vfmaq_f16(c, a, b)));
616
+ #else
617
+ return -a * b - c;
618
+ #endif
619
+ }
620
+ #endif // !defined(C10_MOBILE) && defined(__aarch64__)
621
+
622
+ } // namespace CPU_CAPABILITY
623
+ } // namespace at::vec
624
+
625
+ #else
626
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
627
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_int_aarch64.h ADDED
@@ -0,0 +1,799 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/cpu/vec/intrinsics.h>
5
+ #include <ATen/cpu/vec/vec_base.h>
6
+ #include <c10/macros/Macros.h>
7
+ #include <c10/util/irange.h>
8
+
9
+ namespace at::vec {
10
+ // Note [CPU_CAPABILITY namespace]
11
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12
+ // This header, and all of its subheaders, will be compiled with
13
+ // different architecture flags for each supported set of vector
14
+ // intrinsics. So we need to make sure they aren't inadvertently
15
+ // linked together. We do this by declaring objects in an `inline
16
+ // namespace` which changes the name mangling, but can still be
17
+ // accessed as `at::vec`.
18
+ inline namespace CPU_CAPABILITY {
19
+
20
+ #define VEC_INT_NEON_TEMPLATE(vl, bit) \
21
+ template <> \
22
+ struct is_vec_specialized_for<int##bit##_t> : std::bool_constant<true> {}; \
23
+ \
24
+ template <> \
25
+ class Vectorized<int##bit##_t> { \
26
+ using neon_type = int##bit##x##vl##_t; \
27
+ \
28
+ private: \
29
+ neon_type values; \
30
+ \
31
+ public: \
32
+ using value_type = int##bit##_t; \
33
+ using size_type = int; \
34
+ static constexpr size_type size() { \
35
+ return vl; \
36
+ } \
37
+ Vectorized() { \
38
+ values = vdupq_n_s##bit(0); \
39
+ } \
40
+ Vectorized(neon_type v) : values(v) {} \
41
+ Vectorized(int##bit##_t val); \
42
+ template < \
43
+ typename... Args, \
44
+ typename = std::enable_if_t<(sizeof...(Args) == size())>> \
45
+ Vectorized(Args... vals) { \
46
+ __at_align__ int##bit##_t buffer[size()] = {vals...}; \
47
+ values = vld1q_s##bit(buffer); \
48
+ } \
49
+ operator neon_type() const { \
50
+ return values; \
51
+ } \
52
+ static Vectorized<int##bit##_t> loadu( \
53
+ const void* ptr, \
54
+ int64_t count = size()); \
55
+ void store(void* ptr, int64_t count = size()) const; \
56
+ template <int64_t mask> \
57
+ static Vectorized<int##bit##_t> blend( \
58
+ const Vectorized<int##bit##_t>& a, \
59
+ const Vectorized<int##bit##_t>& b); \
60
+ static Vectorized<int##bit##_t> blendv( \
61
+ const Vectorized<int##bit##_t>& a, \
62
+ const Vectorized<int##bit##_t>& b, \
63
+ const Vectorized<int##bit##_t>& mask_) { \
64
+ return vbslq_s##bit(vreinterpretq_u##bit##_s##bit(mask_.values), b, a); \
65
+ } \
66
+ template <typename step_t> \
67
+ static Vectorized<int##bit##_t> arange( \
68
+ value_type base = 0, \
69
+ step_t step = static_cast<step_t>(1)); \
70
+ static Vectorized<int##bit##_t> set( \
71
+ const Vectorized<int##bit##_t>& a, \
72
+ const Vectorized<int##bit##_t>& b, \
73
+ int64_t count = size()); \
74
+ const int##bit##_t& operator[](int idx) const = delete; \
75
+ int##bit##_t& operator[](int idx) = delete; \
76
+ Vectorized<int##bit##_t> abs() const { \
77
+ return vabsq_s##bit(values); \
78
+ } \
79
+ Vectorized<int##bit##_t> real() const { \
80
+ return values; \
81
+ } \
82
+ Vectorized<int##bit##_t> imag() const { \
83
+ return vdupq_n_s##bit(0); \
84
+ } \
85
+ Vectorized<int##bit##_t> conj() const { \
86
+ return values; \
87
+ } \
88
+ Vectorized<int##bit##_t> neg() const { \
89
+ return vnegq_s##bit(values); \
90
+ } \
91
+ int##bit##_t reduce_add() const { \
92
+ return vaddvq_s##bit(values); \
93
+ } \
94
+ int##bit##_t reduce_max() const; \
95
+ Vectorized<int##bit##_t> operator==( \
96
+ const Vectorized<int##bit##_t>& other) const { \
97
+ return Vectorized<value_type>( \
98
+ vreinterpretq_s##bit##_u##bit(vceqq_s##bit(values, other.values))); \
99
+ } \
100
+ Vectorized<int##bit##_t> operator!=( \
101
+ const Vectorized<int##bit##_t>& other) const; \
102
+ Vectorized<int##bit##_t> operator<( \
103
+ const Vectorized<int##bit##_t>& other) const { \
104
+ return Vectorized<value_type>( \
105
+ vreinterpretq_s##bit##_u##bit(vcltq_s##bit(values, other.values))); \
106
+ } \
107
+ Vectorized<int##bit##_t> operator<=( \
108
+ const Vectorized<int##bit##_t>& other) const { \
109
+ return Vectorized<value_type>( \
110
+ vreinterpretq_s##bit##_u##bit(vcleq_s##bit(values, other.values))); \
111
+ } \
112
+ Vectorized<int##bit##_t> operator>( \
113
+ const Vectorized<int##bit##_t>& other) const { \
114
+ return Vectorized<value_type>( \
115
+ vreinterpretq_s##bit##_u##bit(vcgtq_s##bit(values, other.values))); \
116
+ } \
117
+ Vectorized<int##bit##_t> operator>=( \
118
+ const Vectorized<int##bit##_t>& other) const { \
119
+ return Vectorized<value_type>( \
120
+ vreinterpretq_s##bit##_u##bit(vcgeq_s##bit(values, other.values))); \
121
+ } \
122
+ Vectorized<int##bit##_t> eq(const Vectorized<int##bit##_t>& other) const; \
123
+ Vectorized<int##bit##_t> ne(const Vectorized<int##bit##_t>& other) const; \
124
+ Vectorized<int##bit##_t> gt(const Vectorized<int##bit##_t>& other) const; \
125
+ Vectorized<int##bit##_t> ge(const Vectorized<int##bit##_t>& other) const; \
126
+ Vectorized<int##bit##_t> lt(const Vectorized<int##bit##_t>& other) const; \
127
+ Vectorized<int##bit##_t> le(const Vectorized<int##bit##_t>& other) const; \
128
+ }; \
129
+ template <> \
130
+ Vectorized<int##bit##_t> inline operator+( \
131
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
132
+ return vaddq_s##bit(a, b); \
133
+ } \
134
+ template <> \
135
+ Vectorized<int##bit##_t> inline operator-( \
136
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
137
+ return vsubq_s##bit(a, b); \
138
+ } \
139
+ template <> \
140
+ Vectorized<int##bit##_t> inline operator&( \
141
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
142
+ return vandq_s##bit(a, b); \
143
+ } \
144
+ template <> \
145
+ Vectorized<int##bit##_t> inline operator|( \
146
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
147
+ return vorrq_s##bit(a, b); \
148
+ } \
149
+ template <> \
150
+ Vectorized<int##bit##_t> inline operator^( \
151
+ const Vectorized<int##bit##_t>& a, const Vectorized<int##bit##_t>& b) { \
152
+ return veorq_s##bit(a, b); \
153
+ } \
154
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::eq( \
155
+ const Vectorized<int##bit##_t>& other) const { \
156
+ return (*this == other) & Vectorized<int##bit##_t>(1); \
157
+ } \
158
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::ne( \
159
+ const Vectorized<int##bit##_t>& other) const { \
160
+ return (*this != other) & Vectorized<int##bit##_t>(1); \
161
+ } \
162
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::gt( \
163
+ const Vectorized<int##bit##_t>& other) const { \
164
+ return (*this > other) & Vectorized<int##bit##_t>(1); \
165
+ } \
166
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::ge( \
167
+ const Vectorized<int##bit##_t>& other) const { \
168
+ return (*this >= other) & Vectorized<int##bit##_t>(1); \
169
+ } \
170
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::lt( \
171
+ const Vectorized<int##bit##_t>& other) const { \
172
+ return (*this < other) & Vectorized<int##bit##_t>(1); \
173
+ } \
174
+ Vectorized<int##bit##_t> inline Vectorized<int##bit##_t>::le( \
175
+ const Vectorized<int##bit##_t>& other) const { \
176
+ return (*this <= other) & Vectorized<int##bit##_t>(1); \
177
+ }
178
+
179
+ VEC_INT_NEON_TEMPLATE(2, 64)
180
+ VEC_INT_NEON_TEMPLATE(4, 32)
181
+ VEC_INT_NEON_TEMPLATE(8, 16)
182
+ VEC_INT_NEON_TEMPLATE(16, 8)
183
+
184
+ inline int32_t Vectorized<int32_t>::reduce_max() const {
185
+ return vmaxvq_s32(values);
186
+ }
187
+
188
+ inline int16_t Vectorized<int16_t>::reduce_max() const {
189
+ return vmaxvq_s16(values);
190
+ }
191
+
192
+ inline int8_t Vectorized<int8_t>::reduce_max() const {
193
+ return vmaxvq_s8(values);
194
+ }
195
+
196
+ template <>
197
+ Vectorized<int32_t> inline operator*(
198
+ const Vectorized<int32_t>& a,
199
+ const Vectorized<int32_t>& b) {
200
+ return vmulq_s32(a, b);
201
+ }
202
+
203
+ template <>
204
+ Vectorized<int16_t> inline operator*(
205
+ const Vectorized<int16_t>& a,
206
+ const Vectorized<int16_t>& b) {
207
+ return vmulq_s16(a, b);
208
+ }
209
+
210
+ template <>
211
+ Vectorized<int8_t> inline operator*(
212
+ const Vectorized<int8_t>& a,
213
+ const Vectorized<int8_t>& b) {
214
+ return vmulq_s8(a, b);
215
+ }
216
+
217
+ template <>
218
+ inline Vectorized<int64_t> operator~(const Vectorized<int64_t>& a) {
219
+ int64x2_t val = a;
220
+ return ~val;
221
+ }
222
+
223
+ template <>
224
+ inline Vectorized<int32_t> operator~(const Vectorized<int32_t>& a) {
225
+ return vmvnq_s32(a);
226
+ }
227
+
228
+ template <>
229
+ inline Vectorized<int16_t> operator~(const Vectorized<int16_t>& a) {
230
+ return vmvnq_s16(a);
231
+ }
232
+
233
+ template <>
234
+ inline Vectorized<int8_t> operator~(const Vectorized<int8_t>& a) {
235
+ return vmvnq_s8(a);
236
+ }
237
+
238
+ inline Vectorized<int64_t> Vectorized<int64_t>::operator!=(
239
+ const Vectorized<int64_t>& other) const {
240
+ return ~(*this == other);
241
+ }
242
+
243
+ inline Vectorized<int32_t> Vectorized<int32_t>::operator!=(
244
+ const Vectorized<int32_t>& other) const {
245
+ return ~(*this == other);
246
+ }
247
+
248
+ inline Vectorized<int16_t> Vectorized<int16_t>::operator!=(
249
+ const Vectorized<int16_t>& other) const {
250
+ return ~(*this == other);
251
+ }
252
+
253
+ inline Vectorized<int8_t> Vectorized<int8_t>::operator!=(
254
+ const Vectorized<int8_t>& other) const {
255
+ return ~(*this == other);
256
+ }
257
+
258
+ template <>
259
+ Vectorized<int32_t> inline minimum(
260
+ const Vectorized<int32_t>& a,
261
+ const Vectorized<int32_t>& b) {
262
+ return vminq_s32(a, b);
263
+ }
264
+
265
+ template <>
266
+ Vectorized<int16_t> inline minimum(
267
+ const Vectorized<int16_t>& a,
268
+ const Vectorized<int16_t>& b) {
269
+ return vminq_s16(a, b);
270
+ }
271
+
272
+ template <>
273
+ Vectorized<int8_t> inline minimum(
274
+ const Vectorized<int8_t>& a,
275
+ const Vectorized<int8_t>& b) {
276
+ return vminq_s8(a, b);
277
+ }
278
+
279
+ template <>
280
+ Vectorized<int32_t> inline maximum(
281
+ const Vectorized<int32_t>& a,
282
+ const Vectorized<int32_t>& b) {
283
+ return vmaxq_s32(a, b);
284
+ }
285
+
286
+ template <>
287
+ Vectorized<int16_t> inline maximum(
288
+ const Vectorized<int16_t>& a,
289
+ const Vectorized<int16_t>& b) {
290
+ return vmaxq_s16(a, b);
291
+ }
292
+
293
+ template <>
294
+ Vectorized<int8_t> inline maximum(
295
+ const Vectorized<int8_t>& a,
296
+ const Vectorized<int8_t>& b) {
297
+ return vmaxq_s8(a, b);
298
+ }
299
+
300
+ template <int64_t mask>
301
+ Vectorized<int64_t> Vectorized<int64_t>::blend(
302
+ const Vectorized<int64_t>& a,
303
+ const Vectorized<int64_t>& b) {
304
+ // Build an array of flags: each bit of element is 1 if the corresponding bit
305
+ // in 'mask' is set, 0 otherwise.
306
+ uint64x2_t maskArray = {
307
+ (mask & 1LL) ? 0xFFFFFFFFFFFFFFFF : 0,
308
+ (mask & 2LL) ? 0xFFFFFFFFFFFFFFFF : 0};
309
+ // Use BSL to select elements from b where the mask is 1, else from a
310
+ return vbslq_s64(maskArray, b.values, a.values);
311
+ }
312
+
313
+ template <int64_t mask>
314
+ Vectorized<int32_t> Vectorized<int32_t>::blend(
315
+ const Vectorized<int32_t>& a,
316
+ const Vectorized<int32_t>& b) {
317
+ // Build an array of flags: each bit of element is 1 if the corresponding bit
318
+ // in 'mask' is set, 0 otherwise.
319
+ uint32x4_t maskArray = {
320
+ (mask & 1LL) ? 0xFFFFFFFF : 0,
321
+ (mask & 2LL) ? 0xFFFFFFFF : 0,
322
+ (mask & 4LL) ? 0xFFFFFFFF : 0,
323
+ (mask & 8LL) ? 0xFFFFFFFF : 0};
324
+ // Use BSL to select elements from b where the mask is 1, else from a
325
+ return vbslq_s32(maskArray, b.values, a.values);
326
+ }
327
+
328
+ template <int64_t mask>
329
+ Vectorized<int16_t> Vectorized<int16_t>::blend(
330
+ const Vectorized<int16_t>& a,
331
+ const Vectorized<int16_t>& b) {
332
+ // Build an array of flags: each bit of element is 1 if the corresponding bit
333
+ // in 'mask' is set, 0 otherwise.
334
+ uint16x8_t maskArray = {
335
+ (mask & 1LL) ? 0xFFFF : 0,
336
+ (mask & 2LL) ? 0xFFFF : 0,
337
+ (mask & 4LL) ? 0xFFFF : 0,
338
+ (mask & 8LL) ? 0xFFFF : 0,
339
+ (mask & 16LL) ? 0xFFFF : 0,
340
+ (mask & 32LL) ? 0xFFFF : 0,
341
+ (mask & 64LL) ? 0xFFFF : 0,
342
+ (mask & 128LL) ? 0xFFFF : 0};
343
+ // Use BSL to select elements from b where the mask is 1, else from a
344
+ return vbslq_s16(maskArray, b.values, a.values);
345
+ }
346
+
347
+ template <int64_t mask>
348
+ Vectorized<int8_t> Vectorized<int8_t>::blend(
349
+ const Vectorized<int8_t>& a,
350
+ const Vectorized<int8_t>& b) {
351
+ // Build an array of flags: each bit of element is 1 if the corresponding bit
352
+ // in 'mask' is set, 0 otherwise.
353
+ uint8x16_t maskArray = {
354
+ (mask & 1LL) ? 0xFF : 0,
355
+ (mask & 2LL) ? 0xFF : 0,
356
+ (mask & 4LL) ? 0xFF : 0,
357
+ (mask & 8LL) ? 0xFF : 0,
358
+ (mask & 16LL) ? 0xFF : 0,
359
+ (mask & 32LL) ? 0xFF : 0,
360
+ (mask & 64LL) ? 0xFF : 0,
361
+ (mask & 128LL) ? 0xFF : 0,
362
+ (mask & 256LL) ? 0xFF : 0,
363
+ (mask & 512LL) ? 0xFF : 0,
364
+ (mask & 1024LL) ? 0xFF : 0,
365
+ (mask & 2048LL) ? 0xFF : 0,
366
+ (mask & 4096LL) ? 0xFF : 0,
367
+ (mask & 8192LL) ? 0xFF : 0,
368
+ (mask & 16384LL) ? 0xFF : 0,
369
+ (mask & 32768LL) ? 0xFF : 0};
370
+ // Use BSL to select elements from b where the mask is 1, else from a
371
+ return vbslq_s8(maskArray, b.values, a.values);
372
+ }
373
+
374
+ #define VEC_INT_NEON_OPS(vl, bit) \
375
+ inline Vectorized<int##bit##_t>::Vectorized(int##bit##_t val) { \
376
+ values = vdupq_n_s##bit(val); \
377
+ } \
378
+ inline Vectorized<int##bit##_t> Vectorized<int##bit##_t>::loadu( \
379
+ const void* ptr, int64_t count) { \
380
+ if (count == size()) { \
381
+ return vld1q_s##bit(reinterpret_cast<const int##bit##_t*>(ptr)); \
382
+ } else { \
383
+ __at_align__ int##bit##_t tmp_values[size()]; \
384
+ for (const auto i : c10::irange(size())) { \
385
+ tmp_values[i] = 0; \
386
+ } \
387
+ std::memcpy( \
388
+ tmp_values, \
389
+ reinterpret_cast<const int##bit##_t*>(ptr), \
390
+ count * sizeof(int##bit##_t)); \
391
+ return vld1q_s##bit(reinterpret_cast<const int##bit##_t*>(tmp_values)); \
392
+ } \
393
+ } \
394
+ inline void Vectorized<int##bit##_t>::store(void* ptr, int64_t count) \
395
+ const { \
396
+ if (count == size()) { \
397
+ vst1q_s##bit(reinterpret_cast<int##bit##_t*>(ptr), values); \
398
+ } else { \
399
+ int##bit##_t tmp_values[size()]; \
400
+ vst1q_s##bit(reinterpret_cast<int##bit##_t*>(tmp_values), values); \
401
+ std::memcpy(ptr, tmp_values, count * sizeof(int##bit##_t)); \
402
+ } \
403
+ }
404
+
405
+ VEC_INT_NEON_OPS(2, 64)
406
+ VEC_INT_NEON_OPS(4, 32)
407
+ VEC_INT_NEON_OPS(8, 16)
408
+ VEC_INT_NEON_OPS(16, 8)
409
+
410
+ template <>
411
+ Vectorized<int64_t> inline operator*(
412
+ const Vectorized<int64_t>& a,
413
+ const Vectorized<int64_t>& b) {
414
+ int64x2_t x = a;
415
+ int64x2_t y = b;
416
+ return x * y;
417
+ }
418
+
419
+ template <>
420
+ Vectorized<int64_t> inline operator/(
421
+ const Vectorized<int64_t>& a,
422
+ const Vectorized<int64_t>& b) {
423
+ int64x2_t x = a;
424
+ int64x2_t y = b;
425
+ return x / y;
426
+ }
427
+
428
+ template <>
429
+ Vectorized<int32_t> inline operator/(
430
+ const Vectorized<int32_t>& a,
431
+ const Vectorized<int32_t>& b) {
432
+ int32x4_t x = a;
433
+ int32x4_t y = b;
434
+ return x / y;
435
+ }
436
+
437
+ inline int64_t Vectorized<int64_t>::reduce_max() const {
438
+ return std::max(values[0], values[1]);
439
+ }
440
+
441
+ template <>
442
+ Vectorized<int64_t> inline minimum(
443
+ const Vectorized<int64_t>& a,
444
+ const Vectorized<int64_t>& b) {
445
+ int64x2_t x = a;
446
+ int64x2_t y = b;
447
+ return {std::min(x[0], y[0]), std::min(x[1], y[1])};
448
+ }
449
+
450
+ template <>
451
+ Vectorized<int64_t> inline maximum(
452
+ const Vectorized<int64_t>& a,
453
+ const Vectorized<int64_t>& b) {
454
+ int64x2_t x = a;
455
+ int64x2_t y = b;
456
+ return {std::max(x[0], y[0]), std::max(x[1], y[1])};
457
+ }
458
+
459
+ template <typename step_t>
460
+ inline Vectorized<int64_t> Vectorized<int64_t>::arange(
461
+ int64_t base,
462
+ step_t step) {
463
+ const Vectorized<int64_t> base_vec(base);
464
+ const Vectorized<int64_t> step_vec(step);
465
+ const int64x2_t step_sizes = {0, 1};
466
+ return base_vec.values + step_sizes * step_vec.values;
467
+ }
468
+
469
+ template <typename step_t>
470
+ inline Vectorized<int32_t> Vectorized<int32_t>::arange(
471
+ int32_t base,
472
+ step_t step) {
473
+ const Vectorized<int32_t> base_vec(base);
474
+ const Vectorized<int32_t> step_vec(step);
475
+ const int32x4_t step_sizes = {0, 1, 2, 3};
476
+ return vmlaq_s32(base_vec, step_sizes, step_vec);
477
+ }
478
+
479
+ template <typename step_t>
480
+ inline Vectorized<int16_t> Vectorized<int16_t>::arange(
481
+ int16_t base,
482
+ step_t step) {
483
+ const Vectorized<int16_t> base_vec(base);
484
+ const Vectorized<int16_t> step_vec(step);
485
+ const int16x8_t step_sizes = {0, 1, 2, 3, 4, 5, 6, 7};
486
+ return vmlaq_s16(base_vec, step_sizes, step_vec);
487
+ }
488
+
489
+ template <typename step_t>
490
+ inline Vectorized<int8_t> Vectorized<int8_t>::arange(int8_t base, step_t step) {
491
+ const Vectorized<int8_t> base_vec(base);
492
+ const Vectorized<int8_t> step_vec(step);
493
+ const int8x16_t step_sizes = {
494
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
495
+ return vmlaq_s8(base_vec, step_sizes, step_vec);
496
+ }
497
+
498
+ template <>
499
+ Vectorized<int64_t> inline operator>>(
500
+ const Vectorized<int64_t>& a,
501
+ const Vectorized<int64_t>& b) {
502
+ int64x2_t x = a;
503
+ int64x2_t y = b;
504
+ uint64x2_t u = vreinterpretq_u64_s64(y);
505
+ uint64x2_t z = {std::min(u[0], (uint64_t)63), std::min(u[1], (uint64_t)63)};
506
+ return x >> vreinterpretq_s64_u64(z);
507
+ }
508
+
509
+ template <>
510
+ Vectorized<int32_t> inline operator>>(
511
+ const Vectorized<int32_t>& a,
512
+ const Vectorized<int32_t>& b) {
513
+ int32x4_t x = a;
514
+ int32x4_t y = b;
515
+ uint32x4_t bound = vdupq_n_u32(31);
516
+ uint32x4_t z = vminq_u32(vreinterpretq_u32_s32(y), bound);
517
+ return x >> vreinterpretq_s32_u32(z);
518
+ }
519
+
520
+ template <>
521
+ Vectorized<int16_t> inline operator>>(
522
+ const Vectorized<int16_t>& a,
523
+ const Vectorized<int16_t>& b) {
524
+ int16x8_t x = a;
525
+ int16x8_t y = b;
526
+ uint16x8_t bound = vdupq_n_u16(15);
527
+ uint16x8_t z = vminq_u16(vreinterpretq_u16_s16(y), bound);
528
+ return x >> vreinterpretq_s16_u16(z);
529
+ }
530
+
531
+ template <>
532
+ Vectorized<int8_t> inline operator>>(
533
+ const Vectorized<int8_t>& a,
534
+ const Vectorized<int8_t>& b) {
535
+ int8x16_t x = a;
536
+ int8x16_t y = b;
537
+ uint8x16_t bound = vdupq_n_u8(7);
538
+ int8x16_t z = vreinterpretq_s8_u8(vminq_u8(vreinterpretq_u8_s8(y), bound));
539
+ return x >> z;
540
+ }
541
+
542
+ template <>
543
+ Vectorized<int64_t> inline operator<<(
544
+ const Vectorized<int64_t>& a,
545
+ const Vectorized<int64_t>& b) {
546
+ int64x2_t y = b;
547
+ uint64x2_t u = vreinterpretq_u64_s64(y);
548
+ uint64x2_t z = {std::min(u[0], (uint64_t)64), std::min(u[1], (uint64_t)64)};
549
+ return vshlq_s64(a, vreinterpretq_s64_u64(z));
550
+ }
551
+
552
+ template <>
553
+ Vectorized<int32_t> inline operator<<(
554
+ const Vectorized<int32_t>& a,
555
+ const Vectorized<int32_t>& b) {
556
+ int32x4_t y = b;
557
+ uint32x4_t bound = vdupq_n_u32(32);
558
+ uint32x4_t z = vminq_u32(vreinterpretq_u32_s32(y), bound);
559
+ return vshlq_s32(a, vreinterpretq_s32_u32(z));
560
+ }
561
+
562
+ template <>
563
+ Vectorized<int16_t> inline operator<<(
564
+ const Vectorized<int16_t>& a,
565
+ const Vectorized<int16_t>& b) {
566
+ int16x8_t y = b;
567
+ uint16x8_t bound = vdupq_n_u16(16);
568
+ uint16x8_t z = vminq_u16(vreinterpretq_u16_s16(y), bound);
569
+ return vshlq_s16(a, vreinterpretq_s16_u16(z));
570
+ }
571
+
572
+ template <>
573
+ Vectorized<int8_t> inline operator<<(
574
+ const Vectorized<int8_t>& a,
575
+ const Vectorized<int8_t>& b) {
576
+ int8x16_t y = b;
577
+ uint8x16_t bound = vdupq_n_u8(8);
578
+ int8x16_t z = vreinterpretq_s8_u8(vminq_u8(vreinterpretq_u8_s8(y), bound));
579
+ return vshlq_s8(a, z);
580
+ }
581
+
582
+ inline Vectorized<int64_t> Vectorized<int64_t>::set(
583
+ const Vectorized<int64_t>& a,
584
+ const Vectorized<int64_t>& b,
585
+ int64_t count) {
586
+ if (count == 0) {
587
+ return a;
588
+ } else if (count >= 2) {
589
+ return b;
590
+ } else {
591
+ int64x2_t c = {b.values[0], a.values[1]};
592
+ return c;
593
+ }
594
+ }
595
+
596
+ inline Vectorized<int32_t> Vectorized<int32_t>::set(
597
+ const Vectorized<int32_t>& a,
598
+ const Vectorized<int32_t>& b,
599
+ int64_t count) {
600
+ if (count == 0) {
601
+ return a;
602
+ } else if (count >= 4) {
603
+ return b;
604
+ } else {
605
+ // Build an array of flags: each bit of element is 1 if the corresponding
606
+ // bit in 'mask' is set, 0 otherwise.
607
+ uint32x4_t maskArray = {
608
+ (count >= 1LL) ? 0xFFFFFFFF : 0,
609
+ (count >= 2LL) ? 0xFFFFFFFF : 0,
610
+ (count >= 3LL) ? 0xFFFFFFFF : 0,
611
+ 0};
612
+ // Use BSL to select elements from b where the mask is 1, else from a
613
+ return vbslq_s32(maskArray, b.values, a.values);
614
+ }
615
+ }
616
+
617
+ inline Vectorized<int16_t> Vectorized<int16_t>::set(
618
+ const Vectorized<int16_t>& a,
619
+ const Vectorized<int16_t>& b,
620
+ int64_t count) {
621
+ if (count == 0) {
622
+ return a;
623
+ } else if (count >= 8) {
624
+ return b;
625
+ } else {
626
+ // Build an array of flags: each bit of element is 1 if the corresponding
627
+ // bit in 'mask' is set, 0 otherwise.
628
+ uint16x8_t maskArray = {
629
+ static_cast<uint16_t>((count >= 1LL) ? 0xFFFF : 0),
630
+ static_cast<uint16_t>((count >= 2LL) ? 0xFFFF : 0),
631
+ static_cast<uint16_t>((count >= 3LL) ? 0xFFFF : 0),
632
+ static_cast<uint16_t>((count >= 4LL) ? 0xFFFF : 0),
633
+ static_cast<uint16_t>((count >= 5LL) ? 0xFFFF : 0),
634
+ static_cast<uint16_t>((count >= 6LL) ? 0xFFFF : 0),
635
+ static_cast<uint16_t>((count >= 7LL) ? 0xFFFF : 0),
636
+ 0};
637
+ // Use BSL to select elements from b where the mask is 1, else from a
638
+ return vbslq_s16(maskArray, b.values, a.values);
639
+ }
640
+ }
641
+
642
+ inline Vectorized<int8_t> Vectorized<int8_t>::set(
643
+ const Vectorized<int8_t>& a,
644
+ const Vectorized<int8_t>& b,
645
+ int64_t count) {
646
+ if (count == 0) {
647
+ return a;
648
+ } else if (count >= 16) {
649
+ return b;
650
+ } else {
651
+ // Build an array of flags: each bit of element is 1 if the corresponding
652
+ // bit in 'mask' is set, 0 otherwise.
653
+ uint8x16_t maskArray = {
654
+ static_cast<uint8_t>((count >= 1LL) ? 0xFF : 0),
655
+ static_cast<uint8_t>((count >= 2LL) ? 0xFF : 0),
656
+ static_cast<uint8_t>((count >= 3LL) ? 0xFF : 0),
657
+ static_cast<uint8_t>((count >= 4LL) ? 0xFF : 0),
658
+ static_cast<uint8_t>((count >= 5LL) ? 0xFF : 0),
659
+ static_cast<uint8_t>((count >= 6LL) ? 0xFF : 0),
660
+ static_cast<uint8_t>((count >= 7LL) ? 0xFF : 0),
661
+ static_cast<uint8_t>((count >= 8LL) ? 0xFF : 0),
662
+ static_cast<uint8_t>((count >= 9LL) ? 0xFF : 0),
663
+ static_cast<uint8_t>((count >= 10LL) ? 0xFF : 0),
664
+ static_cast<uint8_t>((count >= 11LL) ? 0xFF : 0),
665
+ static_cast<uint8_t>((count >= 12LL) ? 0xFF : 0),
666
+ static_cast<uint8_t>((count >= 13LL) ? 0xFF : 0),
667
+ static_cast<uint8_t>((count >= 14LL) ? 0xFF : 0),
668
+ static_cast<uint8_t>((count >= 15LL) ? 0xFF : 0),
669
+ 0};
670
+
671
+ // Use BSL to select elements from b where the mask is 1, else from a
672
+ return vbslq_s8(maskArray, b.values, a.values);
673
+ }
674
+ }
675
+
676
+ template <>
677
+ Vectorized<int16_t> inline operator/(
678
+ const Vectorized<int16_t>& a,
679
+ const Vectorized<int16_t>& b) {
680
+ Vectorized<int32_t> highBitsA = vmovl_high_s16(a);
681
+ Vectorized<int32_t> highBitsB = vmovl_high_s16(b);
682
+ Vectorized<int32_t> lowBitsA = vmovl_s16(vget_low_s16(a));
683
+ Vectorized<int32_t> lowBitsB = vmovl_s16(vget_low_s16(b));
684
+ int32x4_t highBitsResult = highBitsA / highBitsB;
685
+ int32x4_t lowBitsResult = lowBitsA / lowBitsB;
686
+ return vuzp1q_s16(
687
+ vreinterpretq_s16_s32(lowBitsResult),
688
+ vreinterpretq_s16_s32(highBitsResult));
689
+ }
690
+
691
+ template <>
692
+ Vectorized<int8_t> inline operator/(
693
+ const Vectorized<int8_t>& a,
694
+ const Vectorized<int8_t>& b) {
695
+ Vectorized<int16_t> highBitsA = vmovl_high_s8(a);
696
+ Vectorized<int16_t> highBitsB = vmovl_high_s8(b);
697
+ Vectorized<int16_t> lowBitsA = vmovl_s8(vget_low_s8(a));
698
+ Vectorized<int16_t> lowBitsB = vmovl_s8(vget_low_s8(b));
699
+ int16x8_t highBitsResult = highBitsA / highBitsB;
700
+ int16x8_t lowBitsResult = lowBitsA / lowBitsB;
701
+ return vuzp1q_s8(
702
+ vreinterpretq_s8_s16(lowBitsResult),
703
+ vreinterpretq_s8_s16(highBitsResult));
704
+ }
705
+
706
+ template <>
707
+ Vectorized<int64_t> inline clamp(
708
+ const Vectorized<int64_t>& a,
709
+ const Vectorized<int64_t>& min,
710
+ const Vectorized<int64_t>& max) {
711
+ return minimum(max, maximum(min, a));
712
+ }
713
+
714
+ template <>
715
+ Vectorized<int32_t> inline clamp(
716
+ const Vectorized<int32_t>& a,
717
+ const Vectorized<int32_t>& min,
718
+ const Vectorized<int32_t>& max) {
719
+ return minimum(max, maximum(min, a));
720
+ }
721
+
722
+ template <>
723
+ Vectorized<int16_t> inline clamp(
724
+ const Vectorized<int16_t>& a,
725
+ const Vectorized<int16_t>& min,
726
+ const Vectorized<int16_t>& max) {
727
+ return minimum(max, maximum(min, a));
728
+ }
729
+
730
+ template <>
731
+ Vectorized<int8_t> inline clamp(
732
+ const Vectorized<int8_t>& a,
733
+ const Vectorized<int8_t>& min,
734
+ const Vectorized<int8_t>& max) {
735
+ return minimum(max, maximum(min, a));
736
+ }
737
+
738
+ template <>
739
+ Vectorized<int64_t> inline clamp_max(
740
+ const Vectorized<int64_t>& a,
741
+ const Vectorized<int64_t>& max) {
742
+ return minimum(max, a);
743
+ }
744
+
745
+ template <>
746
+ Vectorized<int32_t> inline clamp_max(
747
+ const Vectorized<int32_t>& a,
748
+ const Vectorized<int32_t>& max) {
749
+ return minimum(max, a);
750
+ }
751
+
752
+ template <>
753
+ Vectorized<int16_t> inline clamp_max(
754
+ const Vectorized<int16_t>& a,
755
+ const Vectorized<int16_t>& max) {
756
+ return minimum(max, a);
757
+ }
758
+
759
+ template <>
760
+ Vectorized<int8_t> inline clamp_max(
761
+ const Vectorized<int8_t>& a,
762
+ const Vectorized<int8_t>& max) {
763
+ return minimum(max, a);
764
+ }
765
+
766
+ template <>
767
+ Vectorized<int64_t> inline clamp_min(
768
+ const Vectorized<int64_t>& a,
769
+ const Vectorized<int64_t>& min) {
770
+ return maximum(min, a);
771
+ }
772
+
773
+ template <>
774
+ Vectorized<int32_t> inline clamp_min(
775
+ const Vectorized<int32_t>& a,
776
+ const Vectorized<int32_t>& min) {
777
+ return maximum(min, a);
778
+ }
779
+
780
+ template <>
781
+ Vectorized<int16_t> inline clamp_min(
782
+ const Vectorized<int16_t>& a,
783
+ const Vectorized<int16_t>& min) {
784
+ return maximum(min, a);
785
+ }
786
+
787
+ template <>
788
+ Vectorized<int8_t> inline clamp_min(
789
+ const Vectorized<int8_t>& a,
790
+ const Vectorized<int8_t>& min) {
791
+ return maximum(min, a);
792
+ }
793
+
794
+ } // namespace CPU_CAPABILITY
795
+ } // namespace at::vec
796
+
797
+ #else
798
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
799
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec128/vec128_reduced_precision_common_neon.h ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ // Shared code for bfloat16 and float16.
4
+
5
+ // DO NOT DEFINE STATIC DATA IN THIS HEADER!
6
+ // See Note [Do not compile initializers with AVX]
7
+
8
+ namespace at::vec {
9
+ inline namespace CPU_CAPABILITY {
10
+
11
+ // Shared implementation between Vectorized<c10::Half> and
12
+ // Vectorized<c10::BFloat16>. Uses CRTP to allow derived class
13
+ // customization.
14
+ template <
15
+ typename VecT,
16
+ typename ValueT,
17
+ template <int, bool> typename BlendRegs,
18
+ typename Derived>
19
+ struct Vectorized16 {
20
+ protected:
21
+ VecT values;
22
+
23
+ public:
24
+ using value_type = ValueT;
25
+ using size_type = int;
26
+ static constexpr size_type size() {
27
+ static_assert(sizeof(VecT) == 8 * sizeof(value_type));
28
+ return 8;
29
+ }
30
+
31
+ protected:
32
+ Derived map2(
33
+ const Derived& second,
34
+ value_type (*const f)(value_type, value_type)) const {
35
+ __at_align__ value_type tmp_first[size()];
36
+ __at_align__ value_type tmp_second[size()];
37
+ static_cast<const Derived*>(this)->store(
38
+ tmp_first); // store this to tmp_first
39
+ second.store(tmp_second);
40
+ for (const auto i : c10::irange(size())) {
41
+ tmp_first[i] = f(tmp_first[i], tmp_second[i]);
42
+ }
43
+ return Derived::loadu(tmp_first);
44
+ }
45
+
46
+ public:
47
+ Vectorized16() = default;
48
+ Vectorized16(VecT v) : values(v) {}
49
+
50
+ operator VecT() const {
51
+ return values;
52
+ }
53
+
54
+ template <int64_t mask>
55
+ static Derived blend(const Derived& a, const Derived& b) {
56
+ Derived vec;
57
+ vec.values = BlendRegs < 0,
58
+ (mask & 0x01) != 0 > ::impl(a.values, b.values, vec.values);
59
+ vec.values = BlendRegs < 1,
60
+ (mask & 0x02) != 0 > ::impl(a.values, b.values, vec.values);
61
+ vec.values = BlendRegs < 2,
62
+ (mask & 0x04) != 0 > ::impl(a.values, b.values, vec.values);
63
+ vec.values = BlendRegs < 3,
64
+ (mask & 0x08) != 0 > ::impl(a.values, b.values, vec.values);
65
+
66
+ vec.values = BlendRegs < 4,
67
+ (mask & 0x10) != 0 > ::impl(a.values, b.values, vec.values);
68
+ vec.values = BlendRegs < 5,
69
+ (mask & 0x20) != 0 > ::impl(a.values, b.values, vec.values);
70
+ vec.values = BlendRegs < 6,
71
+ (mask & 0x40) != 0 > ::impl(a.values, b.values, vec.values);
72
+ vec.values = BlendRegs < 7,
73
+ (mask & 0x80) != 0 > ::impl(a.values, b.values, vec.values);
74
+
75
+ return vec;
76
+ }
77
+
78
+ template <typename step_t>
79
+ static Derived arange(
80
+ value_type base = 0,
81
+ step_t step = static_cast<step_t>(1)) {
82
+ const Derived base_vec(base);
83
+ const Derived step_vec(step);
84
+ const Derived step_sizes(
85
+ value_type(0),
86
+ value_type(1),
87
+ value_type(2),
88
+ value_type(3),
89
+ value_type(4),
90
+ value_type(5),
91
+ value_type(6),
92
+ value_type(7));
93
+ return fmadd(step_sizes, step_vec, base_vec);
94
+ }
95
+
96
+ // Very slow implementation of indexing.
97
+ // Only required because vec256_qint refers to this.
98
+ // Once we specialize that implementation for ARM
99
+ // this should be removed. TODO (kimishpatel)
100
+ value_type operator[](int idx) const {
101
+ __at_align__ value_type tmp[size()];
102
+ static_cast<const Derived*>(this)->store(tmp);
103
+ return tmp[idx];
104
+ }
105
+
106
+ int zero_mask() const {
107
+ __at_align__ value_type tmp[size()];
108
+ static_cast<const Derived*>(this)->store(tmp);
109
+ int mask = 0;
110
+ for (int i = 0; i < size(); ++i) {
111
+ if (tmp[i] == 0) {
112
+ mask |= (1 << i);
113
+ }
114
+ }
115
+ return mask;
116
+ }
117
+
118
+ Derived map(value_type (*const f)(value_type)) const {
119
+ __at_align__ value_type tmp[size()];
120
+ static_cast<const Derived*>(this)->store(tmp);
121
+ for (const auto i : c10::irange(size())) {
122
+ tmp[i] = f(tmp[i]);
123
+ }
124
+ return Derived::loadu(tmp);
125
+ }
126
+
127
+ Derived angle() const {
128
+ auto zero = Derived(0);
129
+ auto pi = Derived(c10::pi<value_type>);
130
+ auto tmp =
131
+ Derived::blendv(zero, pi, *static_cast<const Derived*>(this) < zero);
132
+ return Derived::blendv(
133
+ tmp,
134
+ *static_cast<const Derived*>(this),
135
+ static_cast<const Derived*>(this)->isnan());
136
+ }
137
+ Derived real() const {
138
+ return *this;
139
+ }
140
+ Derived imag() const {
141
+ return Derived(0);
142
+ }
143
+ Derived conj() const {
144
+ return *this;
145
+ }
146
+
147
+ // Sleef does not support FP16/BF16, so many math functions are applied by
148
+ // converting to FP32, applying the math function, and then converting back to
149
+ // FP16/BF16.
150
+ Derived acos() const {
151
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
152
+ &Vectorized<float>::acos);
153
+ }
154
+ Derived acosh() const {
155
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
156
+ &Vectorized<float>::acosh);
157
+ }
158
+ Derived asin() const {
159
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
160
+ &Vectorized<float>::asin);
161
+ }
162
+ Derived asinh() const {
163
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
164
+ &Vectorized<float>::asinh);
165
+ }
166
+ Derived atan() const {
167
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
168
+ &Vectorized<float>::atan);
169
+ }
170
+ Derived atanh() const {
171
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
172
+ &Vectorized<float>::atanh);
173
+ }
174
+ Derived atan2(const Derived& exp) const {
175
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
176
+ exp, &Vectorized<float>::atan2);
177
+ }
178
+ Derived copysign(const Derived& sign) const {
179
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
180
+ sign, &Vectorized<float>::copysign);
181
+ }
182
+ Derived erf() const {
183
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
184
+ &Vectorized<float>::erf);
185
+ }
186
+ Derived erfc() const {
187
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
188
+ &Vectorized<float>::erfc);
189
+ }
190
+ Derived erfinv() const {
191
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
192
+ &Vectorized<float>::erfinv);
193
+ }
194
+ Derived exp() const {
195
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
196
+ &Vectorized<float>::exp);
197
+ }
198
+ Derived exp2() const {
199
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
200
+ &Vectorized<float>::exp2);
201
+ }
202
+ Derived expm1() const {
203
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
204
+ &Vectorized<float>::expm1);
205
+ }
206
+ Derived exp_u20() const {
207
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
208
+ &Vectorized<float>::exp_u20);
209
+ }
210
+ Derived fexp_u20() const {
211
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
212
+ &Vectorized<float>::exp_u20);
213
+ }
214
+ Derived fmod(const Derived& q) const {
215
+ // This function is questionable with a conversion, so we use map2
216
+ return map2(q, std::fmod);
217
+ }
218
+ Derived hypot(const Derived& b) const {
219
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
220
+ b, &Vectorized<float>::hypot);
221
+ }
222
+ Derived i0() const {
223
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
224
+ &Vectorized<float>::i0);
225
+ }
226
+ Derived i0e() const {
227
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
228
+ &Vectorized<float>::i0e);
229
+ }
230
+ Derived digamma() const {
231
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
232
+ &Vectorized<float>::digamma);
233
+ }
234
+ Derived igamma(const Derived& x) const {
235
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
236
+ x, &Vectorized<float>::igamma);
237
+ }
238
+ Derived igammac(const Derived& x) const {
239
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
240
+ x, &Vectorized<float>::igammac);
241
+ }
242
+ Derived log() const {
243
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
244
+ &Vectorized<float>::log);
245
+ }
246
+ Derived log10() const {
247
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
248
+ &Vectorized<float>::log10);
249
+ }
250
+ Derived log1p() const {
251
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
252
+ &Vectorized<float>::log1p);
253
+ }
254
+ Derived log2() const {
255
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
256
+ &Vectorized<float>::log2);
257
+ }
258
+ Derived nextafter(const Derived& b) const {
259
+ // This function does not make sense with conversion, so we use map2
260
+ return map2(b, std::nextafter);
261
+ }
262
+ Derived sin() const {
263
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
264
+ &Vectorized<float>::sin);
265
+ }
266
+ Derived sinh() const {
267
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
268
+ &Vectorized<float>::sinh);
269
+ }
270
+ Derived cos() const {
271
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
272
+ &Vectorized<float>::cos);
273
+ }
274
+ Derived cosh() const {
275
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
276
+ &Vectorized<float>::cosh);
277
+ }
278
+ Derived ceil() const {
279
+ // This function is questionable with a conversion, so we use map
280
+ return map(at::native::ceil_impl);
281
+ }
282
+ Derived floor() const {
283
+ // This function is questionable with a conversion, so we use map
284
+ return map(at::native::floor_impl);
285
+ }
286
+ Derived round() const {
287
+ // This function is questionable with a conversion, so we use map
288
+ return map(at::native::round_impl);
289
+ }
290
+ Derived tan() const {
291
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
292
+ &Vectorized<float>::tan);
293
+ }
294
+ Derived tanh() const {
295
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
296
+ &Vectorized<float>::tanh);
297
+ }
298
+ Derived lgamma() const {
299
+ return static_cast<const Derived*>(this)->map_with_vec_float_method(
300
+ &Vectorized<float>::lgamma);
301
+ }
302
+ Derived rsqrt() const {
303
+ return static_cast<const Derived*>(this)->sqrt().reciprocal();
304
+ }
305
+ Derived pow(const Derived& exp) const {
306
+ return static_cast<const Derived*>(this)->map2_with_vec_float_method(
307
+ exp, &Vectorized<float>::pow);
308
+ }
309
+ };
310
+
311
+ } // namespace CPU_CAPABILITY
312
+ } // namespace at::vec
313
+
314
+ #else
315
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
316
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)