jasonfan commited on
Commit
8453714
·
verified ·
1 Parent(s): b1e8716

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/torch/csrc/utils/tensor_list.h +18 -0
  2. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_memoryformats.h +20 -0
  3. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_new.h +141 -0
  4. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_numpy.h +37 -0
  5. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_qschemes.h +14 -0
  6. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_types.h +25 -0
  7. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/throughput_benchmark-inl.h +176 -0
  8. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/throughput_benchmark.h +204 -0
  9. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/torch_dispatch_mode.h +73 -0
  10. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/variadic.h +116 -0
  11. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/verbose.h +13 -0
  12. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Event.h +21 -0
  13. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Module.h +16 -0
  14. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Stream.h +22 -0
  15. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/XPUPluggableAllocator.h +85 -0
  16. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/DeviceType.h +128 -0
  17. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Dispatch.h +73 -0
  18. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Dispatch_v2.h +170 -0
  19. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Layout.h +44 -0
  20. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/MemoryFormat.h +46 -0
  21. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/ScalarType.h +381 -0
  22. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/TensorAccessor.h +462 -0
  23. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/cpu/vec/intrinsics.h +50 -0
  24. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/cpu/vec/vec_half.h +59 -0
  25. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/Export.h +153 -0
  26. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/Macros.h +694 -0
  27. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/cmake_macros.h +14 -0
  28. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/BFloat16.h +480 -0
  29. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Deprecated.h +102 -0
  30. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Exception.h +83 -0
  31. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float4_e2m1fn_x2.h +47 -0
  32. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e4m3fn.h +531 -0
  33. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e4m3fnuz.h +444 -0
  34. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e5m2.h +458 -0
  35. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e5m2fnuz.h +448 -0
  36. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e8m0fnu.h +226 -0
  37. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_fnuz_cvt.h +69 -0
  38. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Half.h +788 -0
  39. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/HeaderOnlyArrayRef.h +248 -0
  40. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Metaprogramming.h +237 -0
  41. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeList.h +548 -0
  42. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeSafeSignMath.h +148 -0
  43. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeTraits.h +164 -0
  44. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/bit_cast.h +50 -0
  45. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/bits.h +71 -0
  46. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/complex.h +616 -0
  47. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/floating_point_utils.h +38 -0
  48. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/qint32.h +22 -0
  49. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/qint8.h +24 -0
  50. miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/quint2x4.h +23 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_list.h ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <torch/csrc/python_headers.h>
5
+
6
+ namespace at {
7
+ class Tensor;
8
+ }
9
+
10
+ namespace torch::utils {
11
+
12
+ PyObject* tensor_to_list(const at::Tensor& tensor);
13
+
14
+ }
15
+
16
+ #else
17
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
18
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_memoryformats.h ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/core/MemoryFormat.h>
5
+ #include <torch/csrc/Export.h>
6
+ #include <torch/csrc/utils/python_stub.h>
7
+
8
+ namespace torch::utils {
9
+
10
+ void initializeMemoryFormats();
11
+
12
+ // This methods returns a borrowed reference!
13
+ TORCH_PYTHON_API PyObject* getTHPMemoryFormat(
14
+ c10::MemoryFormat /*memory_format*/);
15
+
16
+ } // namespace torch::utils
17
+
18
+ #else
19
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
20
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_new.h ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <torch/csrc/python_headers.h>
5
+ #include <torch/csrc/utils/python_arg_parser.h>
6
+
7
+ #include <ATen/core/Tensor.h>
8
+
9
+ namespace torch::utils {
10
+
11
+ // NOTE: [torch.tensor, lift_fresh, and device movement]
12
+ //
13
+ // The `only_lift_cpu_tensors` flag controls what happens on torch.tensor([1, 2,
14
+ // 3], device="cuda") (or any non-CPU devices).
15
+ //
16
+ // If false (default):
17
+ // - the data gets moved into a CPU Tensor
18
+ // - then, it gets moved to cuda (via .to)
19
+ // - finally, we call lift_fresh() on it.
20
+ // Steps 1 and 2 happen with all modes disabled.
21
+ //
22
+ // If true:
23
+ // - the data gets moved into a CPU Tensor (with correct dtype)
24
+ // - we call lift_fresh() on it
25
+ // - finally, we move it to cuda (via .to)
26
+ // Step 1 happens with all modes disabled.
27
+ //
28
+ // `only_lift_cpu_tensors=true` is useful to prevent CUDA initialization under
29
+ // FakeTensorMode because it avoids moving concrete data to CUDA.
30
+ TORCH_API bool only_lift_cpu_tensors();
31
+ TORCH_API void set_only_lift_cpu_tensors(bool value);
32
+
33
+ at::Tensor base_tensor_ctor(PyObject* args, PyObject* kwargs);
34
+ TORCH_PYTHON_API at::Tensor legacy_tensor_ctor(
35
+ c10::DispatchKey dispatch_key,
36
+ at::ScalarType scalar_type,
37
+ PyObject* args,
38
+ PyObject* kwargs);
39
+ at::Tensor legacy_tensor_new(
40
+ c10::DispatchKey dispatch_key,
41
+ at::ScalarType scalar_type,
42
+ PyObject* args,
43
+ PyObject* kwargs);
44
+ at::Tensor indexing_tensor_from_data(
45
+ c10::TensorOptions options,
46
+ at::ScalarType scalar_type,
47
+ std::optional<at::Device> device,
48
+ PyObject* data);
49
+ at::Tensor sparse_coo_tensor_ctor(
50
+ c10::DispatchKey dispatch_key,
51
+ at::ScalarType scalar_type,
52
+ PythonArgs& r);
53
+ void _validate_sparse_coo_tensor_args(
54
+ c10::DispatchKey dispatch_key,
55
+ at::ScalarType scalar_type,
56
+ PyObject* args,
57
+ PyObject* kwargs);
58
+
59
+ at::Tensor sparse_compressed_tensor_ctor(
60
+ c10::DispatchKey dispatch_key,
61
+ at::ScalarType scalar_type,
62
+ PythonArgs& r);
63
+ at::Tensor sparse_csr_tensor_ctor(
64
+ c10::DispatchKey dispatch_key,
65
+ at::ScalarType scalar_type,
66
+ PythonArgs& r);
67
+ at::Tensor sparse_csc_tensor_ctor(
68
+ c10::DispatchKey dispatch_key,
69
+ at::ScalarType scalar_type,
70
+ PythonArgs& r);
71
+ at::Tensor sparse_bsr_tensor_ctor(
72
+ c10::DispatchKey dispatch_key,
73
+ at::ScalarType scalar_type,
74
+ PythonArgs& r);
75
+ at::Tensor sparse_bsc_tensor_ctor(
76
+ c10::DispatchKey dispatch_key,
77
+ at::ScalarType scalar_type,
78
+ PythonArgs& r);
79
+
80
+ void _validate_sparse_compressed_tensor_args(
81
+ c10::DispatchKey dispatch_key,
82
+ at::ScalarType scalar_type,
83
+ PyObject* args,
84
+ PyObject* kwargs);
85
+ void _validate_sparse_csr_tensor_args(
86
+ c10::DispatchKey dispatch_key,
87
+ at::ScalarType scalar_type,
88
+ PyObject* args,
89
+ PyObject* kwargs);
90
+ void _validate_sparse_csc_tensor_args(
91
+ c10::DispatchKey dispatch_key,
92
+ at::ScalarType scalar_type,
93
+ PyObject* args,
94
+ PyObject* kwargs);
95
+ void _validate_sparse_bsr_tensor_args(
96
+ c10::DispatchKey dispatch_key,
97
+ at::ScalarType scalar_type,
98
+ PyObject* args,
99
+ PyObject* kwargs);
100
+ void _validate_sparse_bsc_tensor_args(
101
+ c10::DispatchKey dispatch_key,
102
+ at::ScalarType scalar_type,
103
+ PyObject* args,
104
+ PyObject* kwargs);
105
+
106
+ at::Tensor tensor_ctor(
107
+ c10::DispatchKey dispatch_key,
108
+ at::ScalarType scalar_type,
109
+ PythonArgs& r);
110
+ at::Tensor as_tensor(
111
+ c10::DispatchKey dispatch_key,
112
+ at::ScalarType scalar_type,
113
+ PythonArgs& r);
114
+ at::Tensor new_tensor(
115
+ c10::DispatchKey dispatch_key,
116
+ at::ScalarType scalar_type,
117
+ PyObject* args,
118
+ PyObject* kwargs);
119
+ at::Tensor new_ones(
120
+ c10::DispatchKey dispatch_key,
121
+ at::ScalarType scalar_type,
122
+ PyObject* args,
123
+ PyObject* kwargs);
124
+ at::Tensor tensor_frombuffer(
125
+ PyObject* buffer,
126
+ at::ScalarType dtype,
127
+ int64_t count,
128
+ int64_t offset,
129
+ bool requires_grad);
130
+ at::Tensor tensor_fromDLPack(PyObject* data);
131
+ at::Tensor asarray(
132
+ PyObject* obj,
133
+ std::optional<c10::ScalarType> dtype,
134
+ std::optional<c10::Device> device,
135
+ std::optional<bool> copy,
136
+ bool requires_grad);
137
+ } // namespace torch::utils
138
+
139
+ #else
140
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
141
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_numpy.h ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/Tensor.h>
5
+ #include <torch/csrc/python_headers.h>
6
+
7
+ namespace torch::utils {
8
+
9
+ TORCH_API PyObject* tensor_to_numpy(
10
+ const at::Tensor& tensor,
11
+ bool force = false);
12
+
13
+ TORCH_API at::Tensor tensor_from_numpy(
14
+ PyObject* obj,
15
+ bool warn_if_not_writeable = true);
16
+
17
+ TORCH_API int aten_to_numpy_dtype(const at::ScalarType scalar_type);
18
+ TORCH_API at::ScalarType numpy_dtype_to_aten(int dtype);
19
+
20
+ TORCH_API bool is_numpy_available();
21
+ TORCH_API bool is_numpy_int(PyObject* obj);
22
+ TORCH_API bool is_numpy_bool(PyObject* obj);
23
+ TORCH_API bool is_numpy_scalar(PyObject* obj);
24
+
25
+ void warn_numpy_not_writeable();
26
+ at::Tensor tensor_from_cuda_array_interface(
27
+ PyObject* obj,
28
+ std::optional<c10::Device> device_opt = std::nullopt);
29
+
30
+ void validate_numpy_for_dlpack_deleter_bug();
31
+ bool is_numpy_dlpack_deleter_bugged();
32
+
33
+ } // namespace torch::utils
34
+
35
+ #else
36
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
37
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_qschemes.h ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ #include <torch/csrc/QScheme.h>
4
+
5
+ namespace torch::utils {
6
+
7
+ PyObject* getTHPQScheme(at::QScheme qscheme);
8
+ void initializeQSchemes();
9
+
10
+ } // namespace torch::utils
11
+
12
+ #else
13
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
14
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/tensor_types.h ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/DeprecatedTypeProperties.h>
5
+ #include <c10/core/TensorOptions.h>
6
+ #include <utility>
7
+ #include <vector>
8
+
9
+ namespace torch::utils {
10
+
11
+ std::string options_to_string(const at::TensorOptions& options);
12
+ std::string type_to_string(const at::DeprecatedTypeProperties& type);
13
+ at::TensorOptions options_from_string(const std::string& str);
14
+
15
+ // return a vector of all "declared" types, even those that weren't compiled
16
+ std::vector<std::pair<at::Backend, at::ScalarType>> all_declared_types();
17
+
18
+ // return python module name of backend, like torch.cuda, torch.foo
19
+ const char* backend_to_string(const at::Backend& backend);
20
+
21
+ } // namespace torch::utils
22
+
23
+ #else
24
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
25
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/throughput_benchmark-inl.h ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <random>
5
+ #include <thread>
6
+
7
+ #include <torch/csrc/autograd/profiler.h>
8
+ #include <torch/csrc/jit/python/pybind_utils.h>
9
+ #include <torch/csrc/utils/pybind.h>
10
+
11
+ #include <ATen/Parallel.h>
12
+ #include <ATen/autocast_mode.h>
13
+ #include <c10/core/GradMode.h>
14
+ #include <c10/core/impl/LocalDispatchKeySet.h>
15
+ #include <c10/util/irange.h>
16
+
17
+ namespace torch::throughput_benchmark::detail {
18
+
19
+ template <class Input, class Output, class Model>
20
+ BenchmarkExecutionStats BenchmarkHelper<Input, Output, Model>::benchmark(
21
+ const BenchmarkConfig& config) const {
22
+ CHECK(initialized_);
23
+ TORCH_CHECK(
24
+ config.num_worker_threads == 1,
25
+ "Only parallelization by callers is supported");
26
+
27
+ LOG(INFO) << at::get_parallel_info();
28
+
29
+ // We pre-generate inputs here for each of the threads. This allows us to
30
+ // safely move inputs out for each of the threads independently and thus avoid
31
+ // overhead from the benchmark runner itself
32
+ std::vector<std::vector<Input>> thread_inputs(config.num_calling_threads);
33
+ std::vector<size_t> input_iters(config.num_calling_threads);
34
+ {
35
+ std::random_device seeder;
36
+ std::mt19937 engine(seeder());
37
+ TORCH_CHECK(
38
+ !inputs_.empty(),
39
+ "Please provide benchmark inputs."
40
+ "Did you forget to call add_input()? ");
41
+ std::uniform_int_distribution<int> dist(0, inputs_.size() - 1);
42
+
43
+ for (const auto thread_id : c10::irange(config.num_calling_threads)) {
44
+ // Just in case we generate num_iters inputs for each of the threads
45
+ // This was if one thread does all the work we will be fine
46
+ for (const auto i [[maybe_unused]] :
47
+ c10::irange(config.num_iters + config.num_warmup_iters)) {
48
+ thread_inputs[thread_id].push_back(cloneInput(inputs_[dist(engine)]));
49
+ }
50
+ input_iters[thread_id] = 0;
51
+ }
52
+ }
53
+
54
+ std::mutex m;
55
+ std::condition_variable worker_main_cv;
56
+ std::condition_variable main_worker_cv;
57
+ // TODO: add GUARDED_BY once it is available
58
+ int64_t initialized{0};
59
+ int64_t finished{0};
60
+ bool start{false};
61
+ std::atomic<int64_t> num_attempted_iters{0};
62
+ std::vector<std::thread> callers;
63
+
64
+ callers.reserve(config.num_calling_threads);
65
+
66
+ static constexpr auto& DEVICES = at::autocast::_AUTOCAST_SUPPORTED_DEVICES;
67
+ std::array<bool, DEVICES.size()> autocast_enabled;
68
+ std::array<at::ScalarType, DEVICES.size()> autocast_dtype;
69
+ for (size_t i = 0; i < DEVICES.size(); i++) {
70
+ autocast_enabled[i] = at::autocast::is_autocast_enabled(DEVICES[i]);
71
+ autocast_dtype[i] = at::autocast::get_autocast_dtype(DEVICES[i]);
72
+ }
73
+ bool autocast_cache_enabled = at::autocast::is_autocast_cache_enabled();
74
+ bool tls_grad_enabled = c10::GradMode::is_enabled();
75
+ c10::impl::LocalDispatchKeySet tls_key_set =
76
+ c10::impl::tls_local_dispatch_key_set();
77
+
78
+ for (const auto thread_id : c10::irange(config.num_calling_threads)) {
79
+ callers.emplace_back([&, thread_id]() {
80
+ // We use conditional variable as a barrier to make sure each thread
81
+ // performs required warmeup iterations before we start measuring
82
+ c10::GradMode::set_enabled(tls_grad_enabled);
83
+ c10::impl::_force_tls_local_dispatch_key_set(tls_key_set);
84
+ for (size_t i = 0; i < DEVICES.size(); i++) {
85
+ at::autocast::set_autocast_enabled(DEVICES[i], autocast_enabled[i]);
86
+ at::autocast::set_autocast_dtype(DEVICES[i], autocast_dtype[i]);
87
+ }
88
+ at::autocast::set_autocast_cache_enabled(autocast_cache_enabled);
89
+
90
+ for (const auto j : c10::irange(config.num_warmup_iters)) {
91
+ (void)j;
92
+ runOnce(std::move(thread_inputs[thread_id][input_iters[thread_id]]));
93
+ ++input_iters[thread_id];
94
+ }
95
+ {
96
+ std::unique_lock<std::mutex> lock(m);
97
+ ++initialized;
98
+ worker_main_cv.notify_one();
99
+ // NOLINTNEXTLINE(bugprone-infinite-loop)
100
+ while (!start) {
101
+ main_worker_cv.wait(lock);
102
+ }
103
+ }
104
+ LOG(INFO) << "Starting forward thread " << thread_id;
105
+ while (num_attempted_iters.fetch_add(1) < config.num_iters) {
106
+ runOnce(std::move(thread_inputs[thread_id][input_iters[thread_id]]));
107
+ ++input_iters[thread_id];
108
+ }
109
+
110
+ {
111
+ std::unique_lock<std::mutex> lock(m);
112
+ ++finished;
113
+ worker_main_cv.notify_one();
114
+ LOG(INFO) << "Shutting down forward thread " << thread_id
115
+ << ". Total number of finished threads: " << finished;
116
+ }
117
+ });
118
+ }
119
+
120
+ using Clock = std::chrono::high_resolution_clock;
121
+ using RecordProfile = torch::autograd::profiler::RecordProfile;
122
+ using TimePoint = std::chrono::time_point<Clock>;
123
+ TimePoint start_time;
124
+
125
+ std::unique_ptr<RecordProfile> profiler_guard;
126
+ {
127
+ std::unique_lock<std::mutex> lock(m);
128
+ while (initialized != config.num_calling_threads) {
129
+ worker_main_cv.wait(lock);
130
+ }
131
+ if (!config.profiler_output_path.empty()) {
132
+ LOG(INFO) << "Using Autograd profiler. Trace will be saved to "
133
+ << config.profiler_output_path;
134
+ profiler_guard =
135
+ std::make_unique<RecordProfile>(config.profiler_output_path);
136
+ }
137
+ LOG(INFO) << "Starting threads";
138
+ start = true;
139
+ start_time = Clock::now();
140
+ }
141
+
142
+ main_worker_cv.notify_all();
143
+ {
144
+ std::unique_lock<std::mutex> lock(m);
145
+ worker_main_cv.wait(
146
+ lock, [&]() { return finished == config.num_calling_threads; });
147
+ }
148
+ auto end_time = std::chrono::high_resolution_clock::now();
149
+ profiler_guard.reset();
150
+ LOG(INFO) << "Finished benchmark";
151
+
152
+ BenchmarkExecutionStats stats;
153
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
154
+ float total_time_ms = std::chrono::duration_cast<std::chrono::nanoseconds>(
155
+ end_time - start_time)
156
+ .count() /
157
+ 1000.0 / 1000.0;
158
+ // We use config.num_iters instead of num_attempted_iters as it is
159
+ // repsesatative of the real work done. Last attempted iteration on each
160
+ // calling threads doesn't represent the real work (i.e. running the model)
161
+ stats.latency_avg_ms =
162
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
163
+ total_time_ms * config.num_calling_threads / config.num_iters;
164
+ stats.num_iters = config.num_iters;
165
+
166
+ for (auto& t : callers) {
167
+ t.join();
168
+ }
169
+ return stats;
170
+ }
171
+
172
+ } // namespace torch::throughput_benchmark::detail
173
+
174
+ #else
175
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
176
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/throughput_benchmark.h ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/ivalue.h>
5
+ #include <pybind11/pybind11.h>
6
+ #include <torch/csrc/jit/api/module.h>
7
+ #include <torch/csrc/utils/pybind.h>
8
+
9
+ #include <torch/csrc/jit/python/pybind_utils.h>
10
+
11
+ #include <iosfwd>
12
+ #include <memory>
13
+ #include <string>
14
+ #include <vector>
15
+
16
+ namespace py = pybind11;
17
+
18
+ namespace torch::throughput_benchmark {
19
+
20
+ /**
21
+ * The struct is used to provide results of a benchmark to the caller
22
+ * In the future all additional statistics should be added here.
23
+ */
24
+ struct BenchmarkExecutionStats {
25
+ float latency_avg_ms{-1};
26
+ int64_t num_iters{-1};
27
+ };
28
+
29
+ std::ostream& operator<<(
30
+ std::ostream& os,
31
+ const BenchmarkExecutionStats& value);
32
+
33
+ /**
34
+ * Use this struct in order to configure a throughput benchmark run.
35
+ * This struct should include parameters related to threading, batching, number
36
+ * of iterations, warm-up, etc. More configs can be added as needed.
37
+ * General rule here is that only things that c++ must(!) to be aware of should
38
+ * be here. If we can keep other parts in python, we should keep them there.
39
+ * This is typical for things that are not perf critical and don't affect
40
+ * execution statistics benchmark returns.
41
+ */
42
+ struct BenchmarkConfig {
43
+ public:
44
+ // Calling threads are those threads that are calling into a module in
45
+ // parallel.
46
+ int num_calling_threads{1};
47
+ // Worker threads are not supported yet. This is just an example that we plan
48
+ // to support some sort of multi-threaded forward calls. We may change this
49
+ // setting in the future to support different intra and inter op parallelism
50
+ // which is not available in PyTorch yet
51
+ int num_worker_threads{1};
52
+ // Warmup iters are used to make sure we run a module a few times before
53
+ // actually measuring things. This way we avoid cold caches and any other
54
+ // similar problems
55
+ int num_warmup_iters{1};
56
+ // Number of iterations the benchmark should run with. This number is separate
57
+ // from the warmup iterations
58
+ int64_t num_iters{100};
59
+ // If set autograd profiler will be enabled. I.e. this variable would be
60
+ // created before the main benchmark loop (but after the warmup):
61
+ // RecordProfile guard(profiler_output_path);
62
+ std::string profiler_output_path;
63
+ };
64
+
65
+ namespace detail {
66
+
67
+ /**
68
+ * A helper class to abstract out different models we test throughput of
69
+ */
70
+ template <class Input, class Output, class Model>
71
+ class BenchmarkHelper {
72
+ public:
73
+ BenchmarkHelper();
74
+ explicit BenchmarkHelper(Model model)
75
+ : model_(std::move(model)), initialized_(true) {}
76
+
77
+ // This method to be used in benchmark() method
78
+ // Note that there is no result. This way we don't have to call this under GIL
79
+ // even when running in the nn.Module mode. Otherwise destructor of the result
80
+ // would race with Python
81
+ void runOnce(Input&&) const;
82
+ // This method is to be used when calling from Python directly
83
+ Output runOnce(const py::args&, const py::kwargs&) const;
84
+ // Aggregate input in the format Model expects in order to avoid further
85
+ // conversions at the benchmark time
86
+ void addInput(py::args&&, py::kwargs&&);
87
+ void addInput(Input&&);
88
+ BenchmarkExecutionStats benchmark(const BenchmarkConfig& config) const;
89
+
90
+ bool initialized() const {
91
+ return initialized_;
92
+ }
93
+
94
+ // Destructor doesn't require the GIL because it is going to be executed on
95
+ // the PyThon thread
96
+ std::vector<Input> inputs_;
97
+ Model model_;
98
+ bool initialized_{false};
99
+ };
100
+
101
+ struct C10_HIDDEN ModuleInput {
102
+ ModuleInput(ModuleInput&& other) = default;
103
+
104
+ ModuleInput(const ModuleInput&) = delete;
105
+ ModuleInput& operator=(ModuleInput& other) = delete;
106
+ ModuleInput& operator=(ModuleInput&& other) = delete;
107
+ ~ModuleInput() = default;
108
+
109
+ ModuleInput(py::args&& args, py::kwargs&& kwargs)
110
+ : args(std::move(args)), kwargs(std::move(kwargs)) {}
111
+
112
+ py::args args;
113
+ py::kwargs kwargs;
114
+ };
115
+ typedef py::object ModuleOutput;
116
+ typedef std::vector<at::IValue> ScriptModuleInput;
117
+ typedef at::IValue ScriptModuleOutput;
118
+
119
+ template <class Input>
120
+ Input cloneInput(const Input& input);
121
+
122
+ typedef BenchmarkHelper<ScriptModuleInput, at::IValue, jit::Module>
123
+ ScriptModuleBenchmark;
124
+ template <>
125
+ inline BenchmarkHelper<ScriptModuleInput, at::IValue, jit::Module>::
126
+ BenchmarkHelper()
127
+ : model_("Module", std::make_shared<jit::CompilationUnit>()),
128
+ initialized_(false) {}
129
+ typedef BenchmarkHelper<ModuleInput, py::object, py::object> ModuleBenchmark;
130
+ template <>
131
+ inline BenchmarkHelper<ModuleInput, py::object, py::object>::BenchmarkHelper()
132
+ : initialized_(false) {}
133
+
134
+ template <>
135
+ void ScriptModuleBenchmark::runOnce(ScriptModuleInput&& input) const;
136
+
137
+ template <>
138
+ ScriptModuleOutput ScriptModuleBenchmark::runOnce(
139
+ const py::args& args,
140
+ const py::kwargs& kwargs) const;
141
+
142
+ template <>
143
+ void ModuleBenchmark::runOnce(ModuleInput&& input) const;
144
+
145
+ template <>
146
+ ModuleOutput ModuleBenchmark::runOnce(
147
+ const py::args& args,
148
+ const py::kwargs& kwargs) const;
149
+
150
+ template <>
151
+ void ScriptModuleBenchmark::addInput(py::args&& args, py::kwargs&& kwargs);
152
+ template <>
153
+ void ScriptModuleBenchmark::addInput(ScriptModuleInput&& input);
154
+
155
+ template <>
156
+ void ModuleBenchmark::addInput(py::args&& args, py::kwargs&& kwargs);
157
+
158
+ } // namespace detail
159
+
160
+ /**
161
+ * This class is a small c++ component responsible for executing a PyTorch
162
+ * module under an inference server like load. It can emulate multiple calling
163
+ * threads to a single module provided. In the future we plan to enhance this
164
+ * component to support inter and intra-op parallelism as well as multiple
165
+ * models running in a single process.
166
+ *
167
+ * For current available configurations refer to the BenchmarkConfig
168
+ * documentation
169
+ *
170
+ * The class supports working with either nn.Module or ScriptModule.
171
+ * Under the hood it just dispatches to corresponding specialization of
172
+ * class BenchmarkHelper<Input, Output, Model>
173
+ */
174
+ class C10_HIDDEN ThroughputBenchmark {
175
+ public:
176
+ explicit ThroughputBenchmark(const jit::Module& module);
177
+ explicit ThroughputBenchmark(py::object module);
178
+
179
+ // Add one more input example. This input example should be in the exact
180
+ // format the module under test expects. It is responsibility of the module to
181
+ // perform any such format checks, the benchmark doesn't perform any
182
+ // validation of its own
183
+ void addInput(py::args args, py::kwargs kwargs);
184
+
185
+ // Equivalent to just running the model directly on the given input
186
+ py::object runOnce(const py::args& args, const py::kwargs& kwargs);
187
+
188
+ // The main method of the class allows to perform a multi-threaded benchmark
189
+ // It returns BenchmarkExecutionStats object with a lot of useful statistics
190
+ // about runtime execution. We can enhance this class in the future to provide
191
+ // more information to the user
192
+ BenchmarkExecutionStats benchmark(const BenchmarkConfig& config) const;
193
+
194
+ private:
195
+ detail::ScriptModuleBenchmark script_module_;
196
+ detail::ModuleBenchmark module_;
197
+ };
198
+ } // namespace torch::throughput_benchmark
199
+
200
+ #include <torch/csrc/utils/throughput_benchmark-inl.h>
201
+
202
+ #else
203
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
204
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/torch_dispatch_mode.h ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/core/impl/TorchDispatchModeTLS.h>
5
+
6
+ namespace torch::torch_dispatch_mode {
7
+
8
+ struct StashTorchDispatchModeGuard {
9
+ public:
10
+ StashTorchDispatchModeGuard() {
11
+ if (c10::impl::TorchDispatchModeTLS::any_modes_set(
12
+ /*skip_infra_modes=*/true)) {
13
+ saved_mode_ = c10::impl::TorchDispatchModeTLS::pop_stack();
14
+ } else {
15
+ auto mode_and_key =
16
+ c10::impl::TorchDispatchModeTLS::pop_highest_infra_mode();
17
+ saved_mode_ = std::move(std::get<0>(mode_and_key));
18
+ saved_mode_key_ = std::get<1>(mode_and_key);
19
+ }
20
+ }
21
+
22
+ ~StashTorchDispatchModeGuard() {
23
+ if (saved_mode_key_.has_value()) {
24
+ c10::impl::TorchDispatchModeTLS::set_mode(
25
+ saved_mode_, saved_mode_key_.value());
26
+ } else {
27
+ c10::impl::TorchDispatchModeTLS::push_non_infra_mode_onto_stack(
28
+ std::move(saved_mode_));
29
+ }
30
+ }
31
+ StashTorchDispatchModeGuard(const StashTorchDispatchModeGuard&) = delete;
32
+ StashTorchDispatchModeGuard(StashTorchDispatchModeGuard&&) = delete;
33
+ StashTorchDispatchModeGuard& operator=(const StashTorchDispatchModeGuard&) =
34
+ delete;
35
+ StashTorchDispatchModeGuard& operator=(StashTorchDispatchModeGuard&&) =
36
+ delete;
37
+
38
+ const std::shared_ptr<c10::impl::PyObject_TorchDispatchMode>& get_cur_mode() {
39
+ return saved_mode_;
40
+ }
41
+
42
+ private:
43
+ std::shared_ptr<c10::impl::PyObject_TorchDispatchMode> saved_mode_;
44
+ std::optional<c10::impl::TorchDispatchModeKey> saved_mode_key_;
45
+ };
46
+
47
+ struct StashTorchDispatchStackGuard {
48
+ public:
49
+ StashTorchDispatchStackGuard() {
50
+ auto old = c10::impl::TorchDispatchModeTLS::get_state();
51
+ c10::impl::TorchDispatchModeTLS::set_state(std::move(saved_state_));
52
+ saved_state_ = std::move(old);
53
+ }
54
+ StashTorchDispatchStackGuard(const StashTorchDispatchStackGuard&) = delete;
55
+ StashTorchDispatchStackGuard(StashTorchDispatchStackGuard&&) = delete;
56
+ StashTorchDispatchStackGuard& operator=(const StashTorchDispatchStackGuard&) =
57
+ delete;
58
+ StashTorchDispatchStackGuard& operator=(StashTorchDispatchStackGuard&&) =
59
+ delete;
60
+
61
+ ~StashTorchDispatchStackGuard() {
62
+ c10::impl::TorchDispatchModeTLS::set_state(std::move(saved_state_));
63
+ }
64
+
65
+ private:
66
+ c10::impl::TorchDispatchModeTLS saved_state_;
67
+ };
68
+
69
+ } // namespace torch::torch_dispatch_mode
70
+
71
+ #else
72
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
73
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/variadic.h ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/core/Tensor.h>
5
+ #include <ATen/core/Variadic.h>
6
+ #include <torch/csrc/autograd/variable.h>
7
+
8
+ #include <type_traits>
9
+ #include <utility>
10
+
11
+ namespace torch {
12
+
13
+ using at::IterArgs;
14
+
15
+ struct CountTensors : IterArgs<CountTensors> {
16
+ size_t out = 0;
17
+ void operator()(const at::Tensor& x) {
18
+ out += 1;
19
+ }
20
+ void operator()(const std::optional<at::Tensor>& x) {
21
+ out += x.has_value();
22
+ }
23
+ void operator()(at::ArrayRef<at::Tensor> xs) {
24
+ out += xs.size();
25
+ }
26
+ };
27
+
28
+ template <typename... Args>
29
+ size_t count_tensors(Args&&... args) {
30
+ return CountTensors().apply(std::forward<Args>(args)...).out;
31
+ }
32
+
33
+ struct CountVariables : IterArgs<CountVariables> {
34
+ size_t out = 0;
35
+ void operator()(const autograd::Variable& x) {
36
+ out += 1;
37
+ }
38
+ void operator()(at::ArrayRef<autograd::Variable> xs) {
39
+ out += xs.size();
40
+ }
41
+ };
42
+
43
+ template <typename... Args>
44
+ inline size_t count_variables(Args&&... args) {
45
+ return CountVariables().apply(std::forward<Args>(args)...).out;
46
+ }
47
+
48
+ //===----------------------------------------------------------------------===//
49
+ // std::index_sequence shim for C++11
50
+ //===----------------------------------------------------------------------===//
51
+
52
+ // A container of type-template parameter indices.
53
+ template <size_t... Is>
54
+ struct Indices {};
55
+
56
+ // Decrements the index N, adds N-1 to the list of indices and forwards
57
+ // whatever we already have.
58
+ template <size_t N, size_t... Is>
59
+ struct MakeIndices : MakeIndices<N - 1, N - 1, Is...> {};
60
+
61
+ // Partial specialization that forms our base case. When N is zero, we stop
62
+ // and define a typedef that will be visible to earlier classes due to
63
+ // inheritance. The typedef we define is an index list containing the numbers
64
+ // 0 through N-1.
65
+ template <size_t... Is>
66
+ struct MakeIndices<0, Is...> {
67
+ using indices = Indices<Is...>;
68
+ };
69
+
70
+ //===----------------------------------------------------------------------===//
71
+ // Utilities
72
+ //===----------------------------------------------------------------------===//
73
+
74
+ template <typename Function, typename... Ts>
75
+ void apply(Function function, Ts&&... ts) {
76
+ // https://stackoverflow.com/questions/13978916/inserting-a-variadic-argument-list-into-a-vector
77
+ // Creates a dummy array, so that each function call is evaluated in order.
78
+ // `(function(), 0)` is because `function` should (!) return `void`, so
79
+ // according to the comma operator, it is evaluated and its result (`void`)
80
+ // is discarded. Then the zero is evaluated and used as an element in the
81
+ // array. The first zero ensures the array is not empty.
82
+ // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
83
+ int _[]{0, (function(std::forward<Ts>(ts)), 0)...};
84
+ (void)_;
85
+ }
86
+
87
+ template <
88
+ typename ReturnType,
89
+ typename... Ts,
90
+ typename Function,
91
+ typename Accessor>
92
+ ReturnType unpack(Function function, Accessor accessor) {
93
+ return ReturnType(unpack<ReturnType, Ts...>(
94
+ std::move(function),
95
+ std::move(accessor),
96
+ typename MakeIndices<sizeof...(Ts)>::indices()));
97
+ }
98
+
99
+ template <
100
+ typename ReturnType,
101
+ typename... Ts,
102
+ typename Function,
103
+ typename Accessor,
104
+ size_t... Is>
105
+ ReturnType unpack(
106
+ Function function,
107
+ Accessor accessor,
108
+ Indices<Is...> /*unused*/) {
109
+ return ReturnType(function(accessor.template operator()<Ts>(Is)...));
110
+ }
111
+
112
+ } // namespace torch
113
+
114
+ #else
115
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
116
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/utils/verbose.h ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+ #include <torch/csrc/python_headers.h>
4
+
5
+ namespace torch {
6
+
7
+ void initVerboseBindings(PyObject* module);
8
+
9
+ } // namespace torch
10
+
11
+ #else
12
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
13
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Event.h ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <ATen/xpu/XPUEvent.h>
5
+ #include <torch/csrc/Event.h>
6
+ #include <torch/csrc/python_headers.h>
7
+
8
+ struct THXPEvent : THPEvent {
9
+ at::xpu::XPUEvent xpu_event;
10
+ };
11
+ extern PyObject* THXPEventClass;
12
+
13
+ void THXPEvent_init(PyObject* module);
14
+
15
+ inline bool THXPEvent_Check(PyObject* obj) {
16
+ return THXPEventClass && PyObject_IsInstance(obj, THXPEventClass);
17
+ }
18
+
19
+ #else
20
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
21
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Module.h ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <torch/csrc/python_headers.h>
5
+
6
+ PyMethodDef* THXPModule_methods();
7
+
8
+ namespace torch::xpu {
9
+
10
+ void initModule(PyObject* module);
11
+
12
+ } // namespace torch::xpu
13
+
14
+ #else
15
+ #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
16
+ #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/csrc/xpu/Stream.h ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/xpu/XPUStream.h>
5
+ #include <torch/csrc/Stream.h>
6
+ #include <torch/csrc/python_headers.h>
7
+
8
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
9
+ struct THXPStream : THPStream {
10
+ at::xpu::XPUStream xpu_stream;
11
+ };
12
+ extern PyObject* THXPStreamClass;
13
+
14
+ void THXPStream_init(PyObject* module);
15
+
16
+ inline bool THXPStream_Check(PyObject* obj) {
17
+ return THXPStreamClass && PyObject_IsInstance(obj, THXPStreamClass);
18
+ }
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/torch/csrc/xpu/XPUPluggableAllocator.h ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
2
+ #pragma once
3
+
4
+ #include <c10/xpu/XPUCachingAllocator.h>
5
+ #include <torch/csrc/Export.h>
6
+
7
+ namespace torch::xpu::XPUPluggableAllocator {
8
+
9
+ struct _AllocationMetadata {
10
+ _AllocationMetadata() {}
11
+ _AllocationMetadata(
12
+ size_t size,
13
+ c10::DeviceIndex device_idx,
14
+ sycl::queue* queue)
15
+ : size(size), device_idx(device_idx), queue(queue) {}
16
+ size_t size{0};
17
+ c10::DeviceIndex device_idx{-1};
18
+ sycl::queue* queue{};
19
+ };
20
+
21
+ struct TORCH_PYTHON_API XPUPluggableAllocator
22
+ : public c10::xpu::XPUCachingAllocator::XPUAllocator {
23
+ XPUPluggableAllocator(
24
+ std::function<void*(size_t, int, sycl::queue*)> alloc_fn,
25
+ std::function<void(void*, size_t, int, sycl::queue*)> free_fn)
26
+ : alloc_fn_(std::move(alloc_fn)), free_fn_(std::move(free_fn)) {}
27
+
28
+ C10_DISABLE_COPY_AND_ASSIGN(XPUPluggableAllocator);
29
+
30
+ ~XPUPluggableAllocator() override = default;
31
+
32
+ void* malloc(size_t size, c10::DeviceIndex device, sycl::queue* stream);
33
+
34
+ c10::DataPtr allocate(size_t size) override;
35
+ c10::DeleterFnPtr raw_deleter() const override;
36
+
37
+ void* raw_alloc(size_t nbytes) override;
38
+ void raw_delete(void* ptr) override;
39
+ void init(c10::DeviceIndex device_count) override;
40
+ bool initialized() override;
41
+ void copy_data(void* dest, const void* src, std::size_t count) const final;
42
+
43
+ void recordStream(const c10::DataPtr&, c10::Stream stream) override;
44
+ void emptyCache(c10::MempoolId_t mempool_id = {0, 0}) override;
45
+ c10::CachingDeviceAllocator::DeviceStats getDeviceStats(
46
+ c10::DeviceIndex device) override;
47
+ void resetAccumulatedStats(c10::DeviceIndex device) override;
48
+ void resetPeakStats(c10::DeviceIndex device) override;
49
+
50
+ void set_init_fn(std::function<void(int)> init_fn) {
51
+ init_fn_ = std::move(init_fn);
52
+ }
53
+ void set_record_stream_fn(
54
+ std::function<void(void* ptr, sycl::queue* queue)> record_stream_fn) {
55
+ record_stream_fn_ = std::move(record_stream_fn);
56
+ }
57
+
58
+ protected:
59
+ std::function<void*(size_t, int, sycl::queue*)> alloc_fn_;
60
+ std::function<void(void*, size_t, int, sycl::queue*)> free_fn_;
61
+ std::function<void(int)> init_fn_;
62
+ std::function<void(void* ptr, sycl::queue*)> record_stream_fn_;
63
+ std::mutex allocator_mutex_;
64
+ // We do the bookkeeping here in order to simplify custom allocators
65
+ std::unordered_map<void*, _AllocationMetadata> allocation_metadata_;
66
+ bool initialized_ = false;
67
+ };
68
+
69
+ TORCH_XPU_API std::shared_ptr<c10::xpu::XPUCachingAllocator::XPUAllocator>
70
+ getCurrentAllocator();
71
+
72
+ TORCH_XPU_API std::shared_ptr<c10::xpu::XPUCachingAllocator::XPUAllocator>
73
+ createCustomAllocator(
74
+ std::function<void*(size_t, int, sycl::queue*)> alloc_fn,
75
+ std::function<void(void*, size_t, int, sycl::queue*)> free_fn);
76
+
77
+ TORCH_XPU_API void changeCurrentAllocator(
78
+ const std::shared_ptr<c10::xpu::XPUCachingAllocator::XPUAllocator>&
79
+ allocator);
80
+
81
+ } // namespace torch::xpu::XPUPluggableAllocator
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/torch/headeronly/core/DeviceType.h ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // This is directly synchronized with caffe2/proto/caffe2.proto, but
4
+ // doesn't require me to figure out how to get Protobuf headers into
5
+ // ATen/core (which would require a lot more build system hacking.)
6
+ // If you modify me, keep me synchronized with that file.
7
+
8
+ #include <torch/headeronly/macros/Export.h>
9
+ #include <torch/headeronly/macros/Macros.h>
10
+
11
+ #include <cstddef>
12
+ #include <cstdint>
13
+ #include <functional>
14
+
15
+ namespace c10 {
16
+
17
+ // These contains all device types that also have a BackendComponent
18
+ // and therefore participate in per-backend functionality dispatch keys.
19
+ // This is most backends except PrivateUse2 and PrivateUse3
20
+ #define C10_FORALL_BACKEND_DEVICE_TYPES(_, extra) \
21
+ _(CPU, extra) \
22
+ _(CUDA, extra) \
23
+ _(HIP, extra) \
24
+ _(XLA, extra) \
25
+ _(MPS, extra) \
26
+ _(IPU, extra) \
27
+ _(XPU, extra) \
28
+ _(HPU, extra) \
29
+ _(VE, extra) \
30
+ _(Lazy, extra) \
31
+ _(Meta, extra) \
32
+ _(MTIA, extra) \
33
+ _(PrivateUse1, extra)
34
+
35
+ enum class DeviceType : int8_t {
36
+ CPU = 0,
37
+ CUDA = 1, // CUDA.
38
+ MKLDNN = 2, // Reserved for explicit MKLDNN
39
+ OPENGL = 3, // OpenGL
40
+ OPENCL = 4, // OpenCL
41
+ IDEEP = 5, // IDEEP.
42
+ HIP = 6, // AMD HIP
43
+ FPGA = 7, // FPGA
44
+ MAIA = 8, // ONNX Runtime / Microsoft
45
+ XLA = 9, // XLA / TPU
46
+ Vulkan = 10, // Vulkan
47
+ Metal = 11, // Metal
48
+ XPU = 12, // XPU
49
+ MPS = 13, // MPS
50
+ Meta = 14, // Meta (tensors with no data)
51
+ HPU = 15, // HPU / HABANA
52
+ VE = 16, // SX-Aurora / NEC
53
+ Lazy = 17, // Lazy Tensors
54
+ IPU = 18, // Graphcore IPU
55
+ MTIA = 19, // Meta training and inference devices
56
+ PrivateUse1 = 20, // PrivateUse1 device
57
+ // NB: If you add more devices:
58
+ // - Change the implementations of DeviceTypeName and isValidDeviceType
59
+ // in c10/core/DeviceType.cpp
60
+ // - Change the number below
61
+ COMPILE_TIME_MAX_DEVICE_TYPES = 21,
62
+ };
63
+
64
+ constexpr DeviceType kCPU = DeviceType::CPU;
65
+ constexpr DeviceType kCUDA = DeviceType::CUDA;
66
+ constexpr DeviceType kHIP = DeviceType::HIP;
67
+ constexpr DeviceType kFPGA = DeviceType::FPGA;
68
+ constexpr DeviceType kMAIA = DeviceType::MAIA;
69
+ constexpr DeviceType kXLA = DeviceType::XLA;
70
+ constexpr DeviceType kMPS = DeviceType::MPS;
71
+ constexpr DeviceType kMeta = DeviceType::Meta;
72
+ constexpr DeviceType kVulkan = DeviceType::Vulkan;
73
+ constexpr DeviceType kMetal = DeviceType::Metal;
74
+ constexpr DeviceType kXPU = DeviceType::XPU;
75
+ constexpr DeviceType kHPU = DeviceType::HPU;
76
+ constexpr DeviceType kVE = DeviceType::VE;
77
+ constexpr DeviceType kLazy = DeviceType::Lazy;
78
+ constexpr DeviceType kIPU = DeviceType::IPU;
79
+ constexpr DeviceType kMTIA = DeviceType::MTIA;
80
+ constexpr DeviceType kPrivateUse1 = DeviceType::PrivateUse1;
81
+
82
+ // define explicit int constant
83
+ constexpr int COMPILE_TIME_MAX_DEVICE_TYPES =
84
+ static_cast<int>(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES);
85
+
86
+ static_assert(
87
+ COMPILE_TIME_MAX_DEVICE_TYPES <= 21,
88
+ "Hey! You seem to be adding a lot of new DeviceTypes. The intent was "
89
+ "for this constant to reflect the actual number of DeviceTypes we support "
90
+ "in PyTorch; it's important that this number is not too large as we "
91
+ "use this to allocate stack arrays in some places in our code. If you "
92
+ "are indeed just adding the 20th device type, feel free to change "
93
+ "the check to 32; but if you are adding some sort of extensible device "
94
+ "types registration, please be aware that you are affecting code that "
95
+ "this number is small. Try auditing uses of this constant.");
96
+
97
+ } // namespace c10
98
+
99
+ namespace std {
100
+ template <>
101
+ struct hash<c10::DeviceType> {
102
+ std::size_t operator()(c10::DeviceType k) const {
103
+ return std::hash<int>()(static_cast<int>(k));
104
+ }
105
+ };
106
+ } // namespace std
107
+
108
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
109
+ using c10::COMPILE_TIME_MAX_DEVICE_TYPES;
110
+ using c10::DeviceType;
111
+ using c10::kCPU;
112
+ using c10::kCUDA;
113
+ using c10::kFPGA;
114
+ using c10::kHIP;
115
+ using c10::kHPU;
116
+ using c10::kIPU;
117
+ using c10::kLazy;
118
+ using c10::kMAIA;
119
+ using c10::kMeta;
120
+ using c10::kMetal;
121
+ using c10::kMPS;
122
+ using c10::kMTIA;
123
+ using c10::kPrivateUse1;
124
+ using c10::kVE;
125
+ using c10::kVulkan;
126
+ using c10::kXLA;
127
+ using c10::kXPU;
128
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Dispatch.h ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/core/ScalarType.h>
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ // THO_PRIVATE_CASE_TYPE_USING_HINT_TMPL is same as
7
+ // AT_PRIVATE_CASE_TYPE_USING_HINT but with a custom PRELUDE macro:
8
+ #define THO_PRIVATE_CASE_TYPE_USING_HINT_TMPL(PRELUDE, enum_type, HINT, ...) \
9
+ case enum_type: { \
10
+ PRELUDE(enum_type); \
11
+ using HINT [[maybe_unused]] = \
12
+ torch::headeronly::impl::ScalarTypeToCPPTypeT<enum_type>; \
13
+ return __VA_ARGS__(); \
14
+ }
15
+
16
+ // THO_DISPATCH_CASE_TMPL is same as AT_DISPATCH_CASE but with a
17
+ // custom CASE_TYPE_USING_HINT macro:
18
+ #define THO_DISPATCH_CASE_TMPL(CASE_TYPE_USING_HINT, enum_type, ...) \
19
+ CASE_TYPE_USING_HINT(enum_type, scalar_t, __VA_ARGS__)
20
+
21
+ namespace detail {
22
+ inline torch::headeronly::ScalarType scalar_type(
23
+ torch::headeronly::ScalarType s) {
24
+ return s;
25
+ }
26
+ } // namespace detail
27
+
28
+ // THO_DISPATCH_SWITCH_TMPL is same as AT_DISPATCH_SWITCH but with
29
+ // custom PRELUDE and CHECK_NOT_IMPLEMENTED macros:
30
+ #define THO_DISPATCH_SWITCH_TMPL( \
31
+ PRELUDE, CHECK_NOT_IMPLEMENTED, TYPE, NAME, ...) \
32
+ [&] { \
33
+ const auto& the_type = TYPE; \
34
+ constexpr const char* at_dispatch_name = NAME; \
35
+ /* don't use TYPE again in case it is an expensive or side-effect op */ \
36
+ torch::headeronly::ScalarType _st = ::detail::scalar_type(the_type); \
37
+ PRELUDE(at_dispatch_name, _st); \
38
+ C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wswitch-enum") \
39
+ switch (_st) { \
40
+ __VA_ARGS__ \
41
+ default: \
42
+ CHECK_NOT_IMPLEMENTED( \
43
+ false, \
44
+ '"', \
45
+ at_dispatch_name, \
46
+ "\" not implemented for '", \
47
+ torch::headeronly::toString(_st), \
48
+ "'"); \
49
+ } \
50
+ C10_DIAGNOSTIC_POP() \
51
+ }()
52
+
53
+ // THO_EMPTY is a helper macro that discards its arguments.
54
+ #define THO_EMPTY(...)
55
+
56
+ // THO_PRIVATE_CASE_TYPE_USING_HINT is same as
57
+ // AT_PRIVATE_CASE_TYPE_USING_HINT with call to macro
58
+ // AT_PRIVATE_CHECK_SELECTIVE_BUILD removed.
59
+ #define THO_PRIVATE_CASE_TYPE_USING_HINT(enum_type, HINT, ...) \
60
+ THO_PRIVATE_CASE_TYPE_USING_HINT_TMPL(THO_EMPTY, enum_type, HINT, __VA_ARGS__)
61
+
62
+ // THO_DISPATCH_SWITCH is same as AT_DISPATCH_SWITCH with call to
63
+ // macro RECORD_KERNEL_FUNCTION_DTYPE removed and using
64
+ // STD_TORCH_CHECK instead of TORCH_CHECK_NOT_IMPLEMENTED.
65
+ #define THO_DISPATCH_SWITCH(TYPE, NAME, ...) \
66
+ THO_DISPATCH_SWITCH_TMPL(THO_EMPTY, STD_TORCH_CHECK, TYPE, NAME, __VA_ARGS__)
67
+
68
+ // THO_DISPATCH_CASE is same as AT_DISPATCH_CASE but using
69
+ // THO_PRIVATE_CASE_TYPE_USING_HINT instead of
70
+ // AT_PRIVATE_CASE_TYPE_USING_HINT.
71
+ #define THO_DISPATCH_CASE(enum_type, ...) \
72
+ THO_DISPATCH_CASE_TMPL( \
73
+ THO_PRIVATE_CASE_TYPE_USING_HINT, enum_type, __VA_ARGS__)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Dispatch_v2.h ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/core/Dispatch.h>
4
+ #include <torch/headeronly/core/ScalarType.h>
5
+
6
+ // This file provides THO_DISPATCH_V2_TMPL macro that is a generalized
7
+ // version of the original AT_DISPATCH_V2 (see ATen/Dispatch_v2.h for
8
+ // documentation): THO_DISPATCH_V2_TMPL extends AT_DISPATCH_V2 with
9
+ // extra DISPATCH_SWITCH and DISPATCH_CASE arguments for specifying
10
+ // custom implementations of the original AT_DISPATCH_SWITCH and
11
+ // AT_DISPATCH_CASE macros. Use the provided macros
12
+ // THO_DISPATCH_SWITCH_TMPL and THO_DISPATCH_CASE_TMPL to define the
13
+ // custom implementations of the switch and case macros, respectively.
14
+
15
+ // Public API macros
16
+
17
+ // THO_DISPATCH_V2_TMPL is same as AT_DISPATCH_V2 but with custom
18
+ // DISPATCH_SWITCH and DISPATCH_CASE macro arguments:
19
+ #define THO_DISPATCH_V2_TMPL( \
20
+ DISPATCH_SWITCH, DISPATCH_CASE, TYPE, NAME, BODY, ...) \
21
+ DISPATCH_SWITCH( \
22
+ TYPE, \
23
+ NAME, \
24
+ THO_AP_VAR_TMPL(DISPATCH_CASE, AT_WRAP(BODY), TYPE, __VA_ARGS__))
25
+
26
+ // THO_DISPATCH_V2 is same as AT_DISPATCH_V2 but using
27
+ // THO_DISPATCH_SWITCH and THO_DISPATCH_CASE instead of
28
+ // AT_DISPATCH_SWITCH and AT_DISPATCH_CASE, respectively.
29
+ #define THO_DISPATCH_V2(TYPE, NAME, BODY, ...) \
30
+ THO_DISPATCH_V2_TMPL( \
31
+ THO_DISPATCH_SWITCH, THO_DISPATCH_CASE, TYPE, NAME, BODY, __VA_ARGS__)
32
+
33
+ // Type collection macros
34
+
35
+ // This macro lets you pass an arbitrary expression that may contain internal
36
+ // commas to another macro without having the commas causing the expression
37
+ // to be interpreted as being multiple arguments
38
+ #define AT_WRAP(...) __VA_ARGS__
39
+
40
+ #define AT_FLOAT8_TYPES \
41
+ torch::headeronly::ScalarType::Float8_e5m2, \
42
+ torch::headeronly::ScalarType::Float8_e5m2fnuz, \
43
+ torch::headeronly::ScalarType::Float8_e4m3fn, \
44
+ torch::headeronly::ScalarType::Float8_e4m3fnuz, \
45
+ torch::headeronly::ScalarType::Float8_e8m0fnu
46
+
47
+ #define AT_INTEGRAL_TYPES \
48
+ torch::headeronly::ScalarType::Byte, torch::headeronly::ScalarType::Char, \
49
+ torch::headeronly::ScalarType::Int, torch::headeronly::ScalarType::Long, \
50
+ torch::headeronly::ScalarType::Short
51
+ #define AT_FLOATING_TYPES \
52
+ torch::headeronly::ScalarType::Double, torch::headeronly::ScalarType::Float
53
+ #define AT_BAREBONES_UNSIGNED_TYPES \
54
+ torch::headeronly::ScalarType::UInt16, \
55
+ torch::headeronly::ScalarType::UInt32, \
56
+ torch::headeronly::ScalarType::UInt64
57
+ #define AT_INTEGRAL_TYPES_V2 \
58
+ AT_EXPAND(AT_INTEGRAL_TYPES), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES)
59
+ #define AT_COMPLEX_TYPES \
60
+ torch::headeronly::ScalarType::ComplexDouble, \
61
+ torch::headeronly::ScalarType::ComplexFloat
62
+ #define AT_QINT_TYPES \
63
+ torch::headeronly::ScalarType::QInt8, torch::headeronly::ScalarType::QUInt8, \
64
+ torch::headeronly::ScalarType::QInt32
65
+ // NB: not *actually* all types
66
+ #define AT_ALL_TYPES AT_EXPAND(AT_INTEGRAL_TYPES), AT_EXPAND(AT_FLOATING_TYPES)
67
+ #define AT_ALL_TYPES_AND_COMPLEX \
68
+ AT_EXPAND(AT_ALL_TYPES), AT_EXPAND(AT_COMPLEX_TYPES)
69
+
70
+ // Helper macros
71
+
72
+ // THO_AP_VAR_TMPL is same as AT_AP_VAR but with a custom
73
+ // DISPATCH_CASE macro argument:
74
+ #define THO_AP_VAR_TMPL(C, N, T, ...) \
75
+ AT_EXPAND( \
76
+ AT_CONCAT(THO_AP, AT_NUM_ARGS(__VA_ARGS__))(C, AT_WRAP(N), __VA_ARGS__))
77
+ #define AT_CONCAT(a, b) AT_CONCAT_AUX(a, b)
78
+ #define AT_CONCAT_AUX(a, b) a##b
79
+ #define AT_EXPAND(X) X
80
+
81
+ // Ensure we never have too many scalar types for the expansion here to
82
+ // support. To bump this, you must regenerate the macros below.
83
+ static_assert(static_cast<int>(torch::headeronly::ScalarType::NumOptions) < 60);
84
+
85
+ // Python code to regenerate generate code below:
86
+ #if 0
87
+
88
+ num_args = 60
89
+
90
+ nums = ', '.join(str(i) for i in reversed(range(num_args+1)))
91
+ args = ', '.join(f'_{i}' for i in range(1, num_args+1))
92
+
93
+ print(f'#define AT_NUM_ARGS(...) AT_EXPAND(AT_NUM_ARGS_AUX(__VA_ARGS__, {nums}))')
94
+ print(f'#define AT_NUM_ARGS_AUX({args}, N, ...) N')
95
+
96
+ for i in range(1, num_args+1):
97
+ args = ', '.join(f'_{i}' for i in range(1, i+1))
98
+ cases = ' '.join([f'C(_{j}, N)' for j in range(1, i+1)])
99
+ print(f'#define THO_AP{i}(C, N, {args}) {cases}')
100
+
101
+ #endif
102
+
103
+ // Begin generated code
104
+ // clang-format off
105
+
106
+ #define AT_NUM_ARGS(...) AT_EXPAND(AT_NUM_ARGS_AUX(__VA_ARGS__, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
107
+ #define AT_NUM_ARGS_AUX(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, N, ...) N
108
+ #define THO_AP1(C, N, _1) C(_1, N)
109
+ #define THO_AP2(C, N, _1, _2) C(_1, N) C(_2, N)
110
+ #define THO_AP3(C, N, _1, _2, _3) C(_1, N) C(_2, N) C(_3, N)
111
+ #define THO_AP4(C, N, _1, _2, _3, _4) C(_1, N) C(_2, N) C(_3, N) C(_4, N)
112
+ #define THO_AP5(C, N, _1, _2, _3, _4, _5) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N)
113
+ #define THO_AP6(C, N, _1, _2, _3, _4, _5, _6) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N)
114
+ #define THO_AP7(C, N, _1, _2, _3, _4, _5, _6, _7) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N)
115
+ #define THO_AP8(C, N, _1, _2, _3, _4, _5, _6, _7, _8) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N)
116
+ #define THO_AP9(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N)
117
+ #define THO_AP10(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N)
118
+ #define THO_AP11(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N)
119
+ #define THO_AP12(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N)
120
+ #define THO_AP13(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N)
121
+ #define THO_AP14(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N)
122
+ #define THO_AP15(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N)
123
+ #define THO_AP16(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N)
124
+ #define THO_AP17(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N)
125
+ #define THO_AP18(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N)
126
+ #define THO_AP19(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N)
127
+ #define THO_AP20(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N)
128
+ #define THO_AP21(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N)
129
+ #define THO_AP22(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N)
130
+ #define THO_AP23(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N)
131
+ #define THO_AP24(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N)
132
+ #define THO_AP25(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N)
133
+ #define THO_AP26(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N)
134
+ #define THO_AP27(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N)
135
+ #define THO_AP28(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N)
136
+ #define THO_AP29(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N)
137
+ #define THO_AP30(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N)
138
+ #define THO_AP31(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N)
139
+ #define THO_AP32(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N)
140
+ #define THO_AP33(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N)
141
+ #define THO_AP34(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N)
142
+ #define THO_AP35(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N)
143
+ #define THO_AP36(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N)
144
+ #define THO_AP37(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N)
145
+ #define THO_AP38(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N)
146
+ #define THO_AP39(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N)
147
+ #define THO_AP40(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N)
148
+ #define THO_AP41(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N)
149
+ #define THO_AP42(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N)
150
+ #define THO_AP43(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N)
151
+ #define THO_AP44(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N)
152
+ #define THO_AP45(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N)
153
+ #define THO_AP46(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N)
154
+ #define THO_AP47(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N)
155
+ #define THO_AP48(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N)
156
+ #define THO_AP49(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N)
157
+ #define THO_AP50(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N)
158
+ #define THO_AP51(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N)
159
+ #define THO_AP52(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N)
160
+ #define THO_AP53(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N)
161
+ #define THO_AP54(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N)
162
+ #define THO_AP55(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N)
163
+ #define THO_AP56(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N) C(_56, N)
164
+ #define THO_AP57(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N) C(_56, N) C(_57, N)
165
+ #define THO_AP58(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N) C(_56, N) C(_57, N) C(_58, N)
166
+ #define THO_AP59(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N) C(_56, N) C(_57, N) C(_58, N) C(_59, N)
167
+ #define THO_AP60(C, N, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60) C(_1, N) C(_2, N) C(_3, N) C(_4, N) C(_5, N) C(_6, N) C(_7, N) C(_8, N) C(_9, N) C(_10, N) C(_11, N) C(_12, N) C(_13, N) C(_14, N) C(_15, N) C(_16, N) C(_17, N) C(_18, N) C(_19, N) C(_20, N) C(_21, N) C(_22, N) C(_23, N) C(_24, N) C(_25, N) C(_26, N) C(_27, N) C(_28, N) C(_29, N) C(_30, N) C(_31, N) C(_32, N) C(_33, N) C(_34, N) C(_35, N) C(_36, N) C(_37, N) C(_38, N) C(_39, N) C(_40, N) C(_41, N) C(_42, N) C(_43, N) C(_44, N) C(_45, N) C(_46, N) C(_47, N) C(_48, N) C(_49, N) C(_50, N) C(_51, N) C(_52, N) C(_53, N) C(_54, N) C(_55, N) C(_56, N) C(_57, N) C(_58, N) C(_59, N) C(_60, N)
168
+
169
+ // End generated code
170
+ // clang-format on
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/Layout.h ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/Exception.h>
5
+
6
+ #include <cstdint>
7
+ #include <ostream>
8
+
9
+ namespace c10 {
10
+
11
+ enum class Layout : int8_t {
12
+ Strided,
13
+ Sparse,
14
+ SparseCsr,
15
+ Mkldnn,
16
+ SparseCsc,
17
+ SparseBsr,
18
+ SparseBsc,
19
+ Jagged,
20
+ NumOptions
21
+ };
22
+
23
+ constexpr auto kStrided = Layout::Strided;
24
+ constexpr auto kSparse = Layout::Sparse;
25
+ constexpr auto kSparseCsr = Layout::SparseCsr;
26
+ constexpr auto kMkldnn = Layout::Mkldnn;
27
+ constexpr auto kSparseCsc = Layout::SparseCsc;
28
+ constexpr auto kSparseBsr = Layout::SparseBsr;
29
+ constexpr auto kSparseBsc = Layout::SparseBsc;
30
+ constexpr auto kJagged = Layout::Jagged;
31
+
32
+ } // namespace c10
33
+
34
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
35
+ using c10::kJagged;
36
+ using c10::kMkldnn;
37
+ using c10::kSparse;
38
+ using c10::kSparseBsc;
39
+ using c10::kSparseBsr;
40
+ using c10::kSparseCsc;
41
+ using c10::kSparseCsr;
42
+ using c10::kStrided;
43
+ using c10::Layout;
44
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/MemoryFormat.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/Exception.h>
5
+
6
+ #include <cstdint>
7
+ #include <ostream>
8
+
9
+ // Memory format is not the property of a Tensor. It is the way to tell an
10
+ // operator how the result should be organized in memory and nothing more. That
11
+ // means memory format should never be used as return value for any tensor state
12
+ // interrogation functions (internally and externally).
13
+ //
14
+ // Possible options are:
15
+ // Preserve:
16
+ // If any of the input tensors is in channels_last format, operator output
17
+ // should be in channels_last format
18
+ //
19
+ // Contiguous:
20
+ // Regardless of input tensors format, the output should be contiguous
21
+ // Tensor.
22
+ //
23
+ // ChannelsLast:
24
+ // Regardless of input tensors format, the output should be in channels_last
25
+ // format.
26
+
27
+ namespace c10 {
28
+
29
+ enum class MemoryFormat : int8_t {
30
+ Contiguous,
31
+ Preserve,
32
+ ChannelsLast,
33
+ ChannelsLast3d,
34
+ NumOptions
35
+ };
36
+
37
+ inline MemoryFormat get_contiguous_memory_format() {
38
+ return MemoryFormat::Contiguous;
39
+ }
40
+
41
+ } // namespace c10
42
+
43
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
44
+ using c10::get_contiguous_memory_format;
45
+ using c10::MemoryFormat;
46
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/ScalarType.h ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/BFloat16.h>
5
+ #include <torch/headeronly/util/Float4_e2m1fn_x2.h>
6
+ #include <torch/headeronly/util/Float8_e4m3fn.h>
7
+ #include <torch/headeronly/util/Float8_e4m3fnuz.h>
8
+ #include <torch/headeronly/util/Float8_e5m2.h>
9
+ #include <torch/headeronly/util/Float8_e5m2fnuz.h>
10
+ #include <torch/headeronly/util/Float8_e8m0fnu.h>
11
+ #include <torch/headeronly/util/Half.h>
12
+ #include <torch/headeronly/util/bits.h>
13
+ #include <torch/headeronly/util/complex.h>
14
+ #include <torch/headeronly/util/qint32.h>
15
+ #include <torch/headeronly/util/qint8.h>
16
+ #include <torch/headeronly/util/quint2x4.h>
17
+ #include <torch/headeronly/util/quint4x2.h>
18
+ #include <torch/headeronly/util/quint8.h>
19
+
20
+ #include <cstdint>
21
+
22
+ C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wswitch-enum")
23
+
24
+ namespace c10 {
25
+
26
+ // dummy struct for uint1 to uint7, actual functionality
27
+ // of these dtypes will be implemented in python with Tensor subclass
28
+ template <unsigned int N>
29
+ struct dummy_uint1_7_t {};
30
+
31
+ // dummy struct for int1 to int7, actual functionality
32
+ // of these dtypes will be implemented in python with Tensor subclass
33
+ template <unsigned int N>
34
+ struct dummy_int1_7_t {};
35
+
36
+ // [dtype Macros note] For the macros below:
37
+ //
38
+ // For users: If you want to macro some code for all non-QInt scalar types
39
+ // (i.e. types with complete information, you probably want one of the
40
+ // AT_FORALL_SCALAR_TYPES / AT_FORALL_SCALAR_TYPES_AND macros below, which are
41
+ // designed to behave similarly to the Dispatch macros with the same name.
42
+ //
43
+ // For adding a new dtype: In the beginning, we had an idea that there was a
44
+ // list of all scalar types, and you could use AT_FORALL_SCALAR_TYPES to
45
+ // iterate over them. But over the years we added weird types which couldn't
46
+ // be handled uniformly everywhere and so in the end we ended up with some
47
+ // mish-mosh of some helper macros, but mostly use sites making a call about
48
+ // what dtypes they can or can't support. So if you want to add a new dtype,
49
+ // the preferred resolution is to find a dtype similar to what you want,
50
+ // grep for it and edit all the sites you find this way. If you need to add
51
+ // a completely new kind of dtype, you're going to have to laboriously audit
52
+ // all of the sites everywhere to figure out how it should work. Consulting
53
+ // some old PRs where we added new dtypes (check history of this file) can
54
+ // help give you an idea where to start.
55
+
56
+ // If you want to support ComplexHalf for real, add ComplexHalf
57
+ // into this macro (and change the name). But beware: convert()
58
+ // doesn't work for all the conversions you need...
59
+ //
60
+ // TODO: To add unsigned int types here, we must define accumulate type.
61
+ // But uint8 currently accumulates into int64, so we would have to make
62
+ // an inconsistent choice for the larger types. Difficult.
63
+ #define AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF_F8NZ(_) \
64
+ _(uint8_t, Byte) \
65
+ _(int8_t, Char) \
66
+ _(int16_t, Short) \
67
+ _(int, Int) \
68
+ _(int64_t, Long) \
69
+ _(c10::Half, Half) \
70
+ _(float, Float) \
71
+ _(double, Double) \
72
+ _(c10::complex<float>, ComplexFloat) \
73
+ _(c10::complex<double>, ComplexDouble) \
74
+ _(bool, Bool) \
75
+ _(c10::BFloat16, BFloat16) \
76
+ _(c10::Float8_e5m2, Float8_e5m2) \
77
+ _(c10::Float8_e4m3fn, Float8_e4m3fn)
78
+
79
+ // This macro controls many of our C++ APIs, including constructors
80
+ // for Scalar as well as the data() and item() accessors on Tensor
81
+ #define AT_FORALL_SCALAR_TYPES_WITH_COMPLEX(_) \
82
+ _(uint8_t, Byte) \
83
+ _(int8_t, Char) \
84
+ _(int16_t, Short) \
85
+ _(int, Int) \
86
+ _(int64_t, Long) \
87
+ _(c10::Half, Half) \
88
+ _(float, Float) \
89
+ _(double, Double) \
90
+ _(c10::complex<c10::Half>, ComplexHalf) \
91
+ _(c10::complex<float>, ComplexFloat) \
92
+ _(c10::complex<double>, ComplexDouble) \
93
+ _(bool, Bool) \
94
+ _(c10::BFloat16, BFloat16) \
95
+ _(c10::Float8_e5m2, Float8_e5m2) \
96
+ _(c10::Float8_e4m3fn, Float8_e4m3fn) \
97
+ _(c10::Float8_e5m2fnuz, Float8_e5m2fnuz) \
98
+ _(c10::Float8_e4m3fnuz, Float8_e4m3fnuz) \
99
+ _(c10::Float8_e8m0fnu, Float8_e8m0fnu)
100
+
101
+ // NB: Order matters for this macro; it is relied upon in
102
+ // _promoteTypesLookup and the serialization format.
103
+ #define AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(_) \
104
+ _(uint8_t, Byte) /* 0 */ \
105
+ _(int8_t, Char) /* 1 */ \
106
+ _(int16_t, Short) /* 2 */ \
107
+ _(int, Int) /* 3 */ \
108
+ _(int64_t, Long) /* 4 */ \
109
+ _(c10::Half, Half) /* 5 */ \
110
+ _(float, Float) /* 6 */ \
111
+ _(double, Double) /* 7 */ \
112
+ _(c10::complex<c10::Half>, ComplexHalf) /* 8 */ \
113
+ _(c10::complex<float>, ComplexFloat) /* 9 */ \
114
+ _(c10::complex<double>, ComplexDouble) /* 10 */ \
115
+ _(bool, Bool) /* 11 */ \
116
+ _(c10::qint8, QInt8) /* 12 */ \
117
+ _(c10::quint8, QUInt8) /* 13 */ \
118
+ _(c10::qint32, QInt32) /* 14 */ \
119
+ _(c10::BFloat16, BFloat16) /* 15 */ \
120
+ _(c10::quint4x2, QUInt4x2) /* 16 */ \
121
+ _(c10::quint2x4, QUInt2x4) /* 17 */ \
122
+ _(c10::bits1x8, Bits1x8) /* 18 */ \
123
+ _(c10::bits2x4, Bits2x4) /* 19 */ \
124
+ _(c10::bits4x2, Bits4x2) /* 20 */ \
125
+ _(c10::bits8, Bits8) /* 21 */ \
126
+ _(c10::bits16, Bits16) /* 22 */ \
127
+ _(c10::Float8_e5m2, Float8_e5m2) /* 23 */ \
128
+ _(c10::Float8_e4m3fn, Float8_e4m3fn) /* 24 */ \
129
+ _(c10::Float8_e5m2fnuz, Float8_e5m2fnuz) /* 25 */ \
130
+ _(c10::Float8_e4m3fnuz, Float8_e4m3fnuz) /* 26 */ \
131
+ _(uint16_t, UInt16) /* 27 */ \
132
+ _(uint32_t, UInt32) /* 28 */ \
133
+ _(uint64_t, UInt64) /* 29 */ \
134
+ _(c10::dummy_uint1_7_t<1>, UInt1) /* 30 */ \
135
+ _(c10::dummy_uint1_7_t<2>, UInt2) /* 31 */ \
136
+ _(c10::dummy_uint1_7_t<3>, UInt3) /* 32 */ \
137
+ _(c10::dummy_uint1_7_t<4>, UInt4) /* 33 */ \
138
+ _(c10::dummy_uint1_7_t<5>, UInt5) /* 34 */ \
139
+ _(c10::dummy_uint1_7_t<6>, UInt6) /* 35 */ \
140
+ _(c10::dummy_uint1_7_t<7>, UInt7) /* 36 */ \
141
+ _(c10::dummy_int1_7_t<1>, Int1) /* 37 */ \
142
+ _(c10::dummy_int1_7_t<2>, Int2) /* 38 */ \
143
+ _(c10::dummy_int1_7_t<3>, Int3) /* 39 */ \
144
+ _(c10::dummy_int1_7_t<4>, Int4) /* 40 */ \
145
+ _(c10::dummy_int1_7_t<5>, Int5) /* 41 */ \
146
+ _(c10::dummy_int1_7_t<6>, Int6) /* 42 */ \
147
+ _(c10::dummy_int1_7_t<7>, Int7) /* 43 */ \
148
+ _(c10::Float8_e8m0fnu, Float8_e8m0fnu) /* 44 */ \
149
+ _(c10::Float4_e2m1fn_x2, Float4_e2m1fn_x2) /* 45 */
150
+
151
+ // NB: despite its generic sounding name, the macros that don't take _AND
152
+ // are mostly only used by tensorexpr
153
+ #define AT_FORALL_INT_TYPES(_) \
154
+ _(uint8_t, Byte) \
155
+ _(int8_t, Char) \
156
+ _(int16_t, Short) \
157
+ _(int, Int) \
158
+ _(int64_t, Long)
159
+
160
+ #define AT_FORALL_SCALAR_TYPES(_) \
161
+ _(uint8_t, Byte) \
162
+ _(int8_t, Char) \
163
+ _(int16_t, Short) \
164
+ _(int, Int) \
165
+ _(int64_t, Long) \
166
+ _(float, Float) \
167
+ _(double, Double)
168
+
169
+ // These macros are often controlling how many template instantiations we
170
+ // create for kernels. It is typically inappropriate to add new dtypes here,
171
+ // instead, new types should be added to use sites on a case-by-case basis.
172
+ // We generally are not accepting new dtypes due to binary size concerns.
173
+
174
+ #define AT_FORALL_SCALAR_TYPES_AND(SCALARTYPE, _) \
175
+ _(uint8_t, Byte) \
176
+ _(int8_t, Char) \
177
+ _(int16_t, Short) \
178
+ _(int, Int) \
179
+ _(int64_t, Long) \
180
+ _(float, Float) \
181
+ _(double, Double) \
182
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE>, SCALARTYPE)
183
+
184
+ #define AT_FORALL_SCALAR_TYPES_AND2(SCALARTYPE1, SCALARTYPE2, _) \
185
+ _(uint8_t, Byte) \
186
+ _(int8_t, Char) \
187
+ _(int16_t, Short) \
188
+ _(int, Int) \
189
+ _(int64_t, Long) \
190
+ _(float, Float) \
191
+ _(double, Double) \
192
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE1>, \
193
+ SCALARTYPE1) \
194
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE2>, SCALARTYPE2)
195
+
196
+ #define AT_FORALL_SCALAR_TYPES_AND3(SCALARTYPE1, SCALARTYPE2, SCALARTYPE3, _) \
197
+ _(uint8_t, Byte) \
198
+ _(int8_t, Char) \
199
+ _(int16_t, Short) \
200
+ _(int, Int) \
201
+ _(int64_t, Long) \
202
+ _(float, Float) \
203
+ _(double, Double) \
204
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE1>, \
205
+ SCALARTYPE1) \
206
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE2>, \
207
+ SCALARTYPE2) \
208
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE3>, SCALARTYPE3)
209
+
210
+ #define AT_FORALL_SCALAR_TYPES_AND7( \
211
+ SCALARTYPE1, \
212
+ SCALARTYPE2, \
213
+ SCALARTYPE3, \
214
+ SCALARTYPE4, \
215
+ SCALARTYPE5, \
216
+ SCALARTYPE6, \
217
+ SCALARTYPE7, \
218
+ _) \
219
+ _(uint8_t, Byte) \
220
+ _(int8_t, Char) \
221
+ _(int16_t, Short) \
222
+ _(int, Int) \
223
+ _(int64_t, Long) \
224
+ _(float, Float) \
225
+ _(double, Double) \
226
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE1>, \
227
+ SCALARTYPE1) \
228
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE2>, \
229
+ SCALARTYPE2) \
230
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE3>, \
231
+ SCALARTYPE3) \
232
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE4>, \
233
+ SCALARTYPE4) \
234
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE5>, \
235
+ SCALARTYPE5) \
236
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE6>, \
237
+ SCALARTYPE6) \
238
+ _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE7>, SCALARTYPE7)
239
+
240
+ #define AT_FORALL_QINT_TYPES(_) \
241
+ _(c10::qint8, QInt8) \
242
+ _(c10::quint8, QUInt8) \
243
+ _(c10::qint32, QInt32) \
244
+ _(c10::quint4x2, QUInt4x2) \
245
+ _(c10::quint2x4, QUInt2x4)
246
+
247
+ #define AT_FORALL_FLOAT8_TYPES(_) \
248
+ _(c10::Float8_e5m2, Float8_e5m2) \
249
+ _(c10::Float8_e4m3fn, Float8_e4m3fn) \
250
+ _(c10::Float8_e5m2fnuz, Float8_e5m2fnuz) \
251
+ _(c10::Float8_e4m3fnuz, Float8_e4m3fnuz) \
252
+ _(c10::Float8_e8m0fnu, Float8_e8m0fnu)
253
+
254
+ #define AT_FORALL_COMPLEX_TYPES(_) \
255
+ _(c10::complex<float>, ComplexFloat) \
256
+ _(c10::complex<double>, ComplexDouble)
257
+
258
+ enum class ScalarType : int8_t {
259
+ #define DEFINE_ST_ENUM_VAL_(_1, n) n,
260
+ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(DEFINE_ST_ENUM_VAL_)
261
+ #undef DEFINE_ENUM_ST_ENUM_VAL_
262
+ Undefined,
263
+ NumOptions
264
+ };
265
+
266
+ constexpr uint16_t NumScalarTypes =
267
+ static_cast<uint16_t>(ScalarType::NumOptions);
268
+
269
+ // Map from C++ type to ScalarType enum
270
+ template <typename T>
271
+ struct CppTypeToScalarType;
272
+
273
+ #define SPECIALIZE_CppTypeToScalarType(cpp_type, scalar_type) \
274
+ template <> \
275
+ struct CppTypeToScalarType<cpp_type> \
276
+ : std:: \
277
+ integral_constant<c10::ScalarType, c10::ScalarType::scalar_type> { \
278
+ };
279
+
280
+ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(SPECIALIZE_CppTypeToScalarType)
281
+
282
+ #undef SPECIALIZE_CppTypeToScalarType
283
+
284
+ namespace impl {
285
+
286
+ // These are used to map ScalarTypes to C++ types.
287
+
288
+ template <c10::ScalarType N>
289
+ struct ScalarTypeToCPPType;
290
+
291
+ #define SPECIALIZE_ScalarTypeToCPPType(cpp_type, scalar_type) \
292
+ template <> \
293
+ struct ScalarTypeToCPPType<c10::ScalarType::scalar_type> { \
294
+ using type = cpp_type; \
295
+ \
296
+ /* This is a workaround for the CUDA bug which prevents */ \
297
+ /* ::detail::ScalarTypeToCType<T>::type being used directly due to */ \
298
+ /* ambiguous reference which can't to be resolved. For some reason it */ \
299
+ /* can't pick between at::detail and at::cuda::detail. */ \
300
+ /* For repro example, please see: */ \
301
+ /* https://gist.github.com/izdeby/952ae7cf256ddb740a73776d39a7e7ba */ \
302
+ /* UPDATE: while the CUDA bug is fixed, we cannot remove the */ \
303
+ /* workaround as it is BC breaking. However, it is recommended to */ \
304
+ /* update any code that contains */ \
305
+ /* decltype(ScalarTypeToCPPType<T>::t) */ \
306
+ /* with */ \
307
+ /* ScalarTypeToCPPTypeT<T> */ \
308
+ static type t; \
309
+ };
310
+
311
+ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(SPECIALIZE_ScalarTypeToCPPType)
312
+
313
+ #undef SPECIALIZE_ScalarTypeToCPPType
314
+
315
+ template <c10::ScalarType N>
316
+ using ScalarTypeToCPPTypeT = typename ScalarTypeToCPPType<N>::type;
317
+
318
+ } // namespace impl
319
+
320
+ inline const char* toString(ScalarType t) {
321
+ #define DEFINE_CASE(_, name) \
322
+ case ScalarType::name: \
323
+ return #name;
324
+
325
+ switch (t) {
326
+ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(DEFINE_CASE)
327
+ default:
328
+ return "UNKNOWN_SCALAR";
329
+ }
330
+ #undef DEFINE_CASE
331
+ }
332
+
333
+ inline std::ostream& operator<<(
334
+ std::ostream& stream,
335
+ c10::ScalarType scalar_type) {
336
+ return stream << toString(scalar_type);
337
+ }
338
+
339
+ inline bool isQIntType(ScalarType t) {
340
+ // Don't forget to extend this when adding new QInt types
341
+ return t == ScalarType::QInt8 || t == ScalarType::QUInt8 ||
342
+ t == ScalarType::QInt32 || t == ScalarType::QUInt4x2 ||
343
+ t == ScalarType::QUInt2x4;
344
+ }
345
+
346
+ inline ScalarType toUnderlying(ScalarType t) {
347
+ switch (t) {
348
+ case ScalarType::QUInt8:
349
+ case ScalarType::QUInt4x2:
350
+ [[fallthrough]];
351
+ case ScalarType::QUInt2x4:
352
+ return ScalarType::Byte;
353
+ case ScalarType::QInt8:
354
+ return ScalarType::Char;
355
+ case ScalarType::QInt32:
356
+ return ScalarType::Int;
357
+ default:
358
+ return t;
359
+ }
360
+ }
361
+
362
+ } // namespace c10
363
+
364
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
365
+ using c10::CppTypeToScalarType;
366
+ using c10::dummy_int1_7_t;
367
+ using c10::dummy_uint1_7_t;
368
+ using c10::NumScalarTypes;
369
+ using c10::ScalarType;
370
+ using c10::toString;
371
+ using c10::operator<<;
372
+ using c10::isQIntType;
373
+ using c10::toUnderlying;
374
+
375
+ namespace impl {
376
+ using c10::impl::ScalarTypeToCPPTypeT;
377
+ } // namespace impl
378
+
379
+ HIDDEN_NAMESPACE_END(torch, headeronly)
380
+
381
+ C10_DIAGNOSTIC_POP()
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/core/TensorAccessor.h ADDED
@@ -0,0 +1,462 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/Exception.h>
5
+ #include <torch/headeronly/util/HeaderOnlyArrayRef.h>
6
+
7
+ #include <cstddef>
8
+ #include <cstdint>
9
+ #include <iterator>
10
+ #include <type_traits>
11
+
12
+ namespace torch::headeronly {
13
+
14
+ // The PtrTraits argument to the TensorAccessor/GenericPackedTensorAccessor
15
+ // is used to enable the __restrict__ keyword/modifier for the data
16
+ // passed to cuda.
17
+ template <typename T>
18
+ struct DefaultPtrTraits {
19
+ typedef T* PtrType;
20
+ };
21
+
22
+ #if defined(__CUDACC__) || defined(__HIPCC__)
23
+ template <typename T>
24
+ struct RestrictPtrTraits {
25
+ typedef T* __restrict__ PtrType;
26
+ };
27
+ #endif
28
+
29
+ namespace detail {
30
+ // Template classes in torch::headeronly::detail namespace are used
31
+ // to construct accessor template classes with custom ArrayRef and
32
+ // index bound check implementations. For instance,
33
+ // at::TensorAccessor and torch::headeronly::TensorAccessor template
34
+ // classes use c10::IntArrayRef and
35
+ // torch::headeronly::IntHeaderOnlyArrayRef classes, respectively,
36
+ // as return value types of sizes() and strides() methods.
37
+
38
+ // TensorAccessorBase and TensorAccessor are used for both CPU and CUDA tensors.
39
+ // For CUDA tensors it is used in device code (only). This means that we
40
+ // restrict ourselves to functions and types available there (e.g. IntArrayRef
41
+ // isn't).
42
+
43
+ // The PtrTraits argument is only relevant to cuda to support `__restrict__`
44
+ // pointers.
45
+ template <
46
+ class ArrayRefCls,
47
+ typename T,
48
+ size_t N,
49
+ template <typename U> class PtrTraits = DefaultPtrTraits,
50
+ typename index_t = int64_t>
51
+ class TensorAccessorBase {
52
+ public:
53
+ typedef typename PtrTraits<T>::PtrType PtrType;
54
+
55
+ C10_HOST_DEVICE TensorAccessorBase(
56
+ PtrType data_,
57
+ const index_t* sizes_,
58
+ const index_t* strides_)
59
+ : data_(data_), sizes_(sizes_), strides_(strides_) {}
60
+ C10_HOST ArrayRefCls sizes() const {
61
+ return ArrayRefCls(sizes_, N);
62
+ }
63
+ C10_HOST ArrayRefCls strides() const {
64
+ return ArrayRefCls(strides_, N);
65
+ }
66
+ C10_HOST_DEVICE index_t stride(index_t i) const {
67
+ return strides_[i];
68
+ }
69
+ C10_HOST_DEVICE index_t size(index_t i) const {
70
+ return sizes_[i];
71
+ }
72
+ C10_HOST_DEVICE PtrType data() {
73
+ return data_;
74
+ }
75
+ C10_HOST_DEVICE const PtrType data() const {
76
+ return data_;
77
+ }
78
+
79
+ protected:
80
+ PtrType data_;
81
+ const index_t* sizes_;
82
+ const index_t* strides_;
83
+ };
84
+
85
+ // The `TensorAccessor` is typically instantiated for CPU `Tensor`s using
86
+ // `Tensor.accessor<T, N>()`.
87
+ // For CUDA `Tensor`s, `GenericPackedTensorAccessor` is used on the host and
88
+ // only indexing on the device uses `TensorAccessor`s.
89
+ template <
90
+ class ArrayRefCls,
91
+ typename T,
92
+ size_t N,
93
+ template <typename U> class PtrTraits = DefaultPtrTraits,
94
+ typename index_t = int64_t>
95
+ class TensorAccessor
96
+ : public TensorAccessorBase<ArrayRefCls, T, N, PtrTraits, index_t> {
97
+ public:
98
+ typedef typename PtrTraits<T>::PtrType PtrType;
99
+
100
+ C10_HOST_DEVICE TensorAccessor(
101
+ PtrType data_,
102
+ const index_t* sizes_,
103
+ const index_t* strides_)
104
+ : TensorAccessorBase<ArrayRefCls, T, N, PtrTraits, index_t>(
105
+ data_,
106
+ sizes_,
107
+ strides_) {}
108
+
109
+ C10_HOST_DEVICE TensorAccessor<ArrayRefCls, T, N - 1, PtrTraits, index_t>
110
+ operator[](index_t i) {
111
+ return TensorAccessor<ArrayRefCls, T, N - 1, PtrTraits, index_t>(
112
+ this->data_ + this->strides_[0] * i,
113
+ this->sizes_ + 1,
114
+ this->strides_ + 1);
115
+ }
116
+
117
+ C10_HOST_DEVICE const TensorAccessor<
118
+ ArrayRefCls,
119
+ T,
120
+ N - 1,
121
+ PtrTraits,
122
+ index_t>
123
+ operator[](index_t i) const {
124
+ return TensorAccessor<ArrayRefCls, T, N - 1, PtrTraits, index_t>(
125
+ this->data_ + this->strides_[0] * i,
126
+ this->sizes_ + 1,
127
+ this->strides_ + 1);
128
+ }
129
+ };
130
+
131
+ template <
132
+ class ArrayRefCls,
133
+ typename T,
134
+ template <typename U> class PtrTraits,
135
+ typename index_t>
136
+ class TensorAccessor<ArrayRefCls, T, 1, PtrTraits, index_t>
137
+ : public TensorAccessorBase<ArrayRefCls, T, 1, PtrTraits, index_t> {
138
+ public:
139
+ typedef typename PtrTraits<T>::PtrType PtrType;
140
+
141
+ C10_HOST_DEVICE TensorAccessor(
142
+ PtrType data_,
143
+ const index_t* sizes_,
144
+ const index_t* strides_)
145
+ : TensorAccessorBase<ArrayRefCls, T, 1, PtrTraits, index_t>(
146
+ data_,
147
+ sizes_,
148
+ strides_) {}
149
+ C10_HOST_DEVICE T& operator[](index_t i) {
150
+ // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
151
+ return this->data_[this->strides_[0] * i];
152
+ }
153
+ C10_HOST_DEVICE const T& operator[](index_t i) const {
154
+ return this->data_[this->strides_[0] * i];
155
+ }
156
+ };
157
+
158
+ // GenericPackedTensorAccessorBase and GenericPackedTensorAccessor are used on
159
+ // for CUDA `Tensor`s on the host and as in contrast to `TensorAccessor`s, they
160
+ // copy the strides and sizes on instantiation (on the host) in order to
161
+ // transfer them on the device when calling kernels. On the device, indexing of
162
+ // multidimensional tensors gives to `TensorAccessor`s. Use RestrictPtrTraits as
163
+ // PtrTraits if you want the tensor's data pointer to be marked as __restrict__.
164
+ // Instantiation from data, sizes, strides is only needed on the host and
165
+ // std::copy isn't available on the device, so those functions are host only.
166
+ template <
167
+ typename IndexBoundsCheck,
168
+ typename T,
169
+ size_t N,
170
+ template <typename U> class PtrTraits = DefaultPtrTraits,
171
+ typename index_t = int64_t>
172
+ class GenericPackedTensorAccessorBase {
173
+ public:
174
+ typedef typename PtrTraits<T>::PtrType PtrType;
175
+ C10_HOST GenericPackedTensorAccessorBase(
176
+ PtrType data_,
177
+ const index_t* sizes_,
178
+ const index_t* strides_)
179
+ : data_(data_) {
180
+ std::copy(sizes_, sizes_ + N, std::begin(this->sizes_));
181
+ std::copy(strides_, strides_ + N, std::begin(this->strides_));
182
+ }
183
+
184
+ // if index_t is not int64_t, we want to have an int64_t constructor
185
+ template <
186
+ typename source_index_t,
187
+ class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
188
+ C10_HOST GenericPackedTensorAccessorBase(
189
+ PtrType data_,
190
+ const source_index_t* sizes_,
191
+ const source_index_t* strides_)
192
+ : data_(data_) {
193
+ for (size_t i = 0; i < N; ++i) {
194
+ this->sizes_[i] = sizes_[i];
195
+ this->strides_[i] = strides_[i];
196
+ }
197
+ }
198
+
199
+ C10_HOST_DEVICE index_t stride(index_t i) const {
200
+ return strides_[i];
201
+ }
202
+ C10_HOST_DEVICE index_t size(index_t i) const {
203
+ return sizes_[i];
204
+ }
205
+ C10_HOST_DEVICE PtrType data() {
206
+ return data_;
207
+ }
208
+ C10_HOST_DEVICE const PtrType data() const {
209
+ return data_;
210
+ }
211
+
212
+ protected:
213
+ PtrType data_;
214
+ // NOLINTNEXTLINE(*c-arrays*)
215
+ index_t sizes_[N];
216
+ // NOLINTNEXTLINE(*c-arrays*)
217
+ index_t strides_[N];
218
+ C10_HOST void bounds_check_(index_t i) const {
219
+ IndexBoundsCheck _(i);
220
+ }
221
+ };
222
+
223
+ template <
224
+ typename ItemAccessor,
225
+ typename IndexBoundsCheck,
226
+ typename T,
227
+ size_t N,
228
+ template <typename U> class PtrTraits = DefaultPtrTraits,
229
+ typename index_t = int64_t>
230
+ class GenericPackedTensorAccessor : public GenericPackedTensorAccessorBase<
231
+ IndexBoundsCheck,
232
+ T,
233
+ N,
234
+ PtrTraits,
235
+ index_t> {
236
+ public:
237
+ typedef typename PtrTraits<T>::PtrType PtrType;
238
+
239
+ C10_HOST GenericPackedTensorAccessor(
240
+ PtrType data_,
241
+ const index_t* sizes_,
242
+ const index_t* strides_)
243
+ : GenericPackedTensorAccessorBase<
244
+ IndexBoundsCheck,
245
+ T,
246
+ N,
247
+ PtrTraits,
248
+ index_t>(data_, sizes_, strides_) {}
249
+
250
+ // if index_t is not int64_t, we want to have an int64_t constructor
251
+ template <
252
+ typename source_index_t,
253
+ class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
254
+ C10_HOST GenericPackedTensorAccessor(
255
+ PtrType data_,
256
+ const source_index_t* sizes_,
257
+ const source_index_t* strides_)
258
+ : GenericPackedTensorAccessorBase<
259
+ IndexBoundsCheck,
260
+ T,
261
+ N,
262
+ PtrTraits,
263
+ index_t>(data_, sizes_, strides_) {}
264
+
265
+ C10_DEVICE ItemAccessor operator[](index_t i) {
266
+ index_t* new_sizes = this->sizes_ + 1;
267
+ index_t* new_strides = this->strides_ + 1;
268
+ return ItemAccessor(
269
+ this->data_ + this->strides_[0] * i, new_sizes, new_strides);
270
+ }
271
+
272
+ C10_DEVICE const ItemAccessor operator[](index_t i) const {
273
+ const index_t* new_sizes = this->sizes_ + 1;
274
+ const index_t* new_strides = this->strides_ + 1;
275
+ return ItemAccessor(
276
+ this->data_ + this->strides_[0] * i, new_sizes, new_strides);
277
+ }
278
+
279
+ /// Returns a PackedTensorAccessor of the same dimension after transposing the
280
+ /// two dimensions given. Does not actually move elements; transposition is
281
+ /// made by permuting the size/stride arrays. If the dimensions are not valid,
282
+ /// asserts.
283
+ C10_HOST GenericPackedTensorAccessor<
284
+ ItemAccessor,
285
+ IndexBoundsCheck,
286
+ T,
287
+ N,
288
+ PtrTraits,
289
+ index_t>
290
+ transpose(index_t dim1, index_t dim2) const {
291
+ this->bounds_check_(dim1);
292
+ this->bounds_check_(dim2);
293
+ GenericPackedTensorAccessor<
294
+ ItemAccessor,
295
+ IndexBoundsCheck,
296
+ T,
297
+ N,
298
+ PtrTraits,
299
+ index_t>
300
+ result(this->data_, this->sizes_, this->strides_);
301
+ std::swap(result.strides_[dim1], result.strides_[dim2]);
302
+ std::swap(result.sizes_[dim1], result.sizes_[dim2]);
303
+ return result;
304
+ }
305
+ };
306
+
307
+ template <
308
+ typename ItemAccessor,
309
+ typename IndexBoundsCheck,
310
+ typename T,
311
+ template <typename U> class PtrTraits,
312
+ typename index_t>
313
+ class GenericPackedTensorAccessor<
314
+ ItemAccessor,
315
+ IndexBoundsCheck,
316
+ T,
317
+ 1,
318
+ PtrTraits,
319
+ index_t>
320
+ : public GenericPackedTensorAccessorBase<
321
+ IndexBoundsCheck,
322
+ T,
323
+ 1,
324
+ PtrTraits,
325
+ index_t> {
326
+ public:
327
+ typedef typename PtrTraits<T>::PtrType PtrType;
328
+ C10_HOST GenericPackedTensorAccessor(
329
+ PtrType data_,
330
+ const index_t* sizes_,
331
+ const index_t* strides_)
332
+ : GenericPackedTensorAccessorBase<
333
+ IndexBoundsCheck,
334
+ T,
335
+ 1,
336
+ PtrTraits,
337
+ index_t>(data_, sizes_, strides_) {}
338
+
339
+ // if index_t is not int64_t, we want to have an int64_t constructor
340
+ template <
341
+ typename source_index_t,
342
+ class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
343
+ C10_HOST GenericPackedTensorAccessor(
344
+ PtrType data_,
345
+ const source_index_t* sizes_,
346
+ const source_index_t* strides_)
347
+ : GenericPackedTensorAccessorBase<
348
+ IndexBoundsCheck,
349
+ T,
350
+ 1,
351
+ PtrTraits,
352
+ index_t>(data_, sizes_, strides_) {}
353
+
354
+ C10_DEVICE T& operator[](index_t i) {
355
+ return this->data_[this->strides_[0] * i];
356
+ }
357
+ C10_DEVICE const T& operator[](index_t i) const {
358
+ return this->data_[this->strides_[0] * i];
359
+ }
360
+
361
+ // Same as in the general N-dimensional case, but note that in the
362
+ // 1-dimensional case the returned PackedTensorAccessor will always be an
363
+ // identical copy of the original
364
+ C10_HOST GenericPackedTensorAccessor<
365
+ ItemAccessor,
366
+ IndexBoundsCheck,
367
+ T,
368
+ 1,
369
+ PtrTraits,
370
+ index_t>
371
+ transpose(index_t dim1, index_t dim2) const {
372
+ this->bounds_check_(dim1);
373
+ this->bounds_check_(dim2);
374
+ return GenericPackedTensorAccessor<
375
+ ItemAccessor,
376
+ IndexBoundsCheck,
377
+ T,
378
+ 1,
379
+ PtrTraits,
380
+ index_t>(this->data_, this->sizes_, this->strides_);
381
+ }
382
+ };
383
+
384
+ template <size_t N, typename index_t>
385
+ struct HeaderOnlyIndexBoundsCheck {
386
+ HeaderOnlyIndexBoundsCheck(index_t i) {
387
+ STD_TORCH_CHECK(
388
+ 0 <= i && i < index_t{N},
389
+ "Index ",
390
+ i,
391
+ " is not within bounds of a tensor of dimension ",
392
+ N);
393
+ }
394
+ };
395
+
396
+ } // namespace detail
397
+
398
+ // HeaderOnlyTensorAccessorBase is same as at::TensorAccessorBase
399
+ // except sizes() and strides() return IntHeaderOnlyArrayRef instead
400
+ // of IntArrayRef.
401
+ template <
402
+ typename T,
403
+ size_t N,
404
+ template <typename U> class PtrTraits = DefaultPtrTraits,
405
+ typename index_t = int64_t>
406
+ using HeaderOnlyTensorAccessorBase = detail::TensorAccessorBase<
407
+ torch::headeronly::IntHeaderOnlyArrayRef,
408
+ T,
409
+ N,
410
+ PtrTraits,
411
+ index_t>;
412
+
413
+ // HeaderOnlyTensorAccessor is same as at::TensorAccessor except
414
+ // sizes() and strides() return IntHeaderOnlyArrayRef instead of
415
+ // IntArrayRef.
416
+ template <
417
+ typename T,
418
+ size_t N,
419
+ template <typename U> class PtrTraits = DefaultPtrTraits,
420
+ typename index_t = int64_t>
421
+ using HeaderOnlyTensorAccessor = detail::TensorAccessor<
422
+ torch::headeronly::IntHeaderOnlyArrayRef,
423
+ T,
424
+ N,
425
+ PtrTraits,
426
+ index_t>;
427
+
428
+ // HeaderOnlyGenericPackedTensorAccessorBase is same as
429
+ // at::GenericPackedTensorAccessorBase except sizes() and strides()
430
+ // return IntHeaderOnlyArrayRef instead of IntArrayRef.
431
+ template <
432
+ typename T,
433
+ size_t N,
434
+ template <typename U> class PtrTraits = DefaultPtrTraits,
435
+ typename index_t = int64_t>
436
+ using HeaderOnlyGenericPackedTensorAccessorBase =
437
+ detail::GenericPackedTensorAccessorBase<
438
+ detail::HeaderOnlyIndexBoundsCheck<N, index_t>,
439
+ T,
440
+ N,
441
+ PtrTraits,
442
+ index_t>;
443
+
444
+ // HeaderOnlyGenericPackedTensorAccessor is same as
445
+ // at::GenericPackedTensorAccessor except sizes() and strides() return
446
+ // IntHeaderOnlyArrayRef instead of IntArrayRef, and bounds check uses
447
+ // STD_TORCH_CHECK instead of TORCH_CHECK_INDEX.
448
+ template <
449
+ typename T,
450
+ size_t N,
451
+ template <typename U> class PtrTraits = DefaultPtrTraits,
452
+ typename index_t = int64_t>
453
+ using HeaderOnlyGenericPackedTensorAccessor =
454
+ detail::GenericPackedTensorAccessor<
455
+ HeaderOnlyTensorAccessor<T, N - 1, PtrTraits, index_t>,
456
+ detail::HeaderOnlyIndexBoundsCheck<N, index_t>,
457
+ T,
458
+ N,
459
+ PtrTraits,
460
+ index_t>;
461
+
462
+ } // namespace torch::headeronly
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/cpu/vec/intrinsics.h ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
3
+ /* GCC or clang-compatible compiler, targeting x86/x86-64 */
4
+ #include <x86intrin.h>
5
+ #elif defined(__clang__) && (defined(__ARM_NEON__) || defined(__aarch64__))
6
+ /* Clang-compatible compiler, targeting arm neon */
7
+ #include <arm_neon.h>
8
+ #if defined(__ARM_FEATURE_SVE)
9
+ /* CLANG-compatible compiler, targeting ARM with SVE */
10
+ #include <arm_sve.h>
11
+ #endif
12
+ #elif defined(_MSC_VER)
13
+ /* Microsoft C/C++-compatible compiler */
14
+ #include <intrin.h>
15
+ #if _MSC_VER <= 1900
16
+ #define _mm256_extract_epi64(X, Y) \
17
+ (_mm_extract_epi64(_mm256_extractf128_si256(X, Y >> 1), Y % 2))
18
+ #define _mm256_extract_epi32(X, Y) \
19
+ (_mm_extract_epi32(_mm256_extractf128_si256(X, Y >> 2), Y % 4))
20
+ #define _mm256_extract_epi16(X, Y) \
21
+ (_mm_extract_epi16(_mm256_extractf128_si256(X, Y >> 3), Y % 8))
22
+ #define _mm256_extract_epi8(X, Y) \
23
+ (_mm_extract_epi8(_mm256_extractf128_si256(X, Y >> 4), Y % 16))
24
+ #endif
25
+ #elif defined(__GNUC__) && (defined(__ARM_NEON__) || defined(__aarch64__))
26
+ /* GCC-compatible compiler, targeting ARM with NEON */
27
+ #include <arm_neon.h>
28
+ #if defined(__ARM_FEATURE_SVE)
29
+ /* GCC-compatible compiler, targeting ARM with SVE */
30
+ #include <arm_sve.h>
31
+ #endif
32
+ #elif defined(__GNUC__) && defined(__IWMMXT__)
33
+ /* GCC-compatible compiler, targeting ARM with WMMX */
34
+ #include <mmintrin.h>
35
+ #elif defined(__s390x__)
36
+ // targets Z/architecture
37
+ // we will include vecintrin later
38
+ #elif (defined(__GNUC__) || defined(__xlC__)) && \
39
+ (defined(__VEC__) || defined(__ALTIVEC__))
40
+ /* XLC or GCC-compatible compiler, targeting PowerPC with VMX/VSX */
41
+ #include <altivec.h>
42
+ /* We need to undef those tokens defined by <altivec.h> to avoid conflicts
43
+ with the C++ types. => Can still use __bool/__vector */
44
+ #undef bool
45
+ #undef vector
46
+ #undef pixel
47
+ #elif defined(__GNUC__) && defined(__SPE__)
48
+ /* GCC-compatible compiler, targeting PowerPC with SPE */
49
+ #include <spe.h>
50
+ #endif
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/cpu/vec/vec_half.h ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/cpu/vec/intrinsics.h>
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, vec)
7
+ // See Note [CPU_CAPABILITY namespace]
8
+ inline namespace CPU_CAPABILITY {
9
+
10
+ #if (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
11
+ !defined(__APPLE__)
12
+ static inline uint16_t float2half_scalar(float val) {
13
+ #if defined(CPU_CAPABILITY_AVX2)
14
+ #if defined(_MSC_VER)
15
+ __m256 v = _mm256_set1_ps(val);
16
+ __m128i o =
17
+ _mm256_cvtps_ph(v, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
18
+ return static_cast<std::uint16_t>(_mm_cvtsi128_si32(o));
19
+ #else
20
+ return _cvtss_sh(val, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
21
+ #endif
22
+ #elif defined(CPU_CAPABILITY_AVX512)
23
+ __m512 v = _mm512_set1_ps(val);
24
+ __m256i o =
25
+ _mm512_cvtps_ph(v, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
26
+ return static_cast<std::uint16_t>(
27
+ _mm_cvtsi128_si32(_mm256_castsi256_si128(o)));
28
+ #endif
29
+ }
30
+
31
+ static inline float half2float_scalar(uint16_t val) {
32
+ #if defined(CPU_CAPABILITY_AVX2)
33
+ #if defined(_MSC_VER)
34
+ __m128i v = _mm_cvtsi32_si128(val);
35
+ __m256 o = _mm256_cvtph_ps(v);
36
+ return _mm256_cvtss_f32(o);
37
+ #else
38
+ return _cvtsh_ss(val);
39
+ #endif
40
+ #elif defined(CPU_CAPABILITY_AVX512)
41
+ __m256i v =
42
+ _mm256_setr_epi16(val, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
43
+ __m512 o = _mm512_cvtph_ps(v);
44
+ return _mm512_cvtss_f32(o);
45
+ #endif
46
+ }
47
+
48
+ #endif
49
+
50
+ } // namespace CPU_CAPABILITY
51
+ HIDDEN_NAMESPACE_END(torch, headeronly, vec)
52
+
53
+ namespace at::vec {
54
+ #if (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
55
+ !defined(__APPLE__)
56
+ using torch::headeronly::vec::float2half_scalar;
57
+ using torch::headeronly::vec::half2float_scalar;
58
+ #endif
59
+ } // namespace at::vec
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/Export.h ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #ifndef C10_MACROS_EXPORT_H_
4
+ #define C10_MACROS_EXPORT_H_
5
+
6
+ #ifndef C10_USING_CUSTOM_GENERATED_MACROS
7
+ #include <torch/headeronly/macros/cmake_macros.h>
8
+ #endif // C10_USING_CUSTOM_GENERATED_MACROS
9
+
10
+ /* Header file to define the common scaffolding for exported symbols.
11
+ *
12
+ * Export is by itself a quite tricky situation to deal with, and if you are
13
+ * hitting this file, make sure you start with the background here:
14
+ * - Linux: https://gcc.gnu.org/wiki/Visibility
15
+ * - Windows:
16
+ * https://docs.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=vs-2017
17
+ *
18
+ * Do NOT include this file directly. Instead, use c10/macros/Macros.h
19
+ */
20
+
21
+ // You do not need to edit this part of file unless you are changing the core
22
+ // pytorch export abstractions.
23
+ //
24
+ // This part defines the C10 core export and import macros. This is controlled
25
+ // by whether we are building shared libraries or not, which is determined
26
+ // during build time and codified in c10/core/cmake_macros.h.
27
+ // When the library is built as a shared lib, EXPORT and IMPORT will contain
28
+ // visibility attributes. If it is being built as a static lib, then EXPORT
29
+ // and IMPORT basically have no effect.
30
+
31
+ // As a rule of thumb, you should almost NEVER mix static and shared builds for
32
+ // libraries that depend on c10. AKA, if c10 is built as a static library, we
33
+ // recommend everything dependent on c10 to be built statically. If c10 is built
34
+ // as a shared library, everything dependent on it should be built as shared. In
35
+ // the PyTorch project, all native libraries shall use the macro
36
+ // C10_BUILD_SHARED_LIB to check whether pytorch is building shared or static
37
+ // libraries.
38
+
39
+ // For build systems that do not directly depend on CMake and directly build
40
+ // from the source directory (such as Buck), one may not have a cmake_macros.h
41
+ // file at all. In this case, the build system is responsible for providing
42
+ // correct macro definitions corresponding to the cmake_macros.h.in file.
43
+ //
44
+ // In such scenarios, one should define the macro
45
+ // C10_USING_CUSTOM_GENERATED_MACROS
46
+ // to inform this header that it does not need to include the cmake_macros.h
47
+ // file.
48
+
49
+ #ifdef _WIN32
50
+ #define C10_HIDDEN
51
+ #if defined(C10_BUILD_SHARED_LIBS)
52
+ #define C10_EXPORT __declspec(dllexport)
53
+ #define C10_IMPORT __declspec(dllimport)
54
+ #else
55
+ #define C10_EXPORT
56
+ #define C10_IMPORT
57
+ #endif
58
+ #else // _WIN32
59
+ #if defined(__GNUC__)
60
+ #define C10_EXPORT __attribute__((__visibility__("default")))
61
+ #define C10_HIDDEN __attribute__((__visibility__("hidden")))
62
+ #else // defined(__GNUC__)
63
+ #define C10_EXPORT
64
+ #define C10_HIDDEN
65
+ #endif // defined(__GNUC__)
66
+ #define C10_IMPORT C10_EXPORT
67
+ #endif // _WIN32
68
+
69
+ #ifdef NO_EXPORT
70
+ #undef C10_EXPORT
71
+ #define C10_EXPORT
72
+ #endif
73
+
74
+ // Definition of an adaptive XX_API macro, that depends on whether you are
75
+ // building the library itself or not, routes to XX_EXPORT and XX_IMPORT.
76
+ // Basically, you will need to do this for each shared library that you are
77
+ // building, and the instruction is as follows: assuming that you are building
78
+ // a library called libawesome.so. You should:
79
+ // (1) for your cmake target (usually done by "add_library(awesome, ...)"),
80
+ // define a macro called AWESOME_BUILD_MAIN_LIB using
81
+ // target_compile_options.
82
+ // (2) define the AWESOME_API macro similar to the one below.
83
+ // And in the source file of your awesome library, use AWESOME_API to
84
+ // annotate public symbols.
85
+
86
+ // Here, for the C10 library, we will define the macro C10_API for both import
87
+ // and export.
88
+
89
+ // This one is being used by libc10.so
90
+ #ifdef C10_BUILD_MAIN_LIB
91
+ #define C10_API C10_EXPORT
92
+ #else
93
+ #define C10_API C10_IMPORT
94
+ #endif
95
+
96
+ // This one is being used by libtorch.so
97
+ #ifdef CAFFE2_BUILD_MAIN_LIB
98
+ #define TORCH_API C10_EXPORT
99
+ #else
100
+ #define TORCH_API C10_IMPORT
101
+ #endif
102
+
103
+ // You may be wondering why we have TORCH_CUDA_CPP_API and TORCH_CUDA_CU_API
104
+ // belonging to the same library instead of just one TORCH_CUDA_API. Well, it
105
+ // can indeed just be one TORCH_CUDA_API (and used to be)! TORCH_CUDA_CPP_API
106
+ // and TORCH_CUDA_CU_API are artifacts of when we needed a split build to
107
+ // avoid relocation marker linking errors. The context is as follows:
108
+ //
109
+ // Once upon a time, there _was_ only TORCH_CUDA_API. All was happy until we
110
+ // tried to compile PyTorch for CUDA 11.1, which ran into relocation marker
111
+ // issues when linking big binaries.
112
+ // (https://github.com/pytorch/pytorch/issues/39968) We had two choices:
113
+ // (1) Stop supporting so many GPU architectures
114
+ // (2) Do something else
115
+ // We chose #2 and decided to split the behemoth that was torch_cuda into two
116
+ // smaller libraries, one with most of the core kernel functions (torch_cuda_cu)
117
+ // and the other that had..well..everything else (torch_cuda_cpp). The idea was
118
+ // this: instead of linking our static libraries (like the hefty
119
+ // libcudnn_static.a) with another huge library, torch_cuda, and run into pesky
120
+ // relocation marker issues, we could link our static libraries to a smaller
121
+ // part of torch_cuda (torch_cuda_cpp) and avoid the issues.
122
+
123
+ // libtorch_cuda.so (where torch_cuda_cu and torch_cuda_cpp are a part of the
124
+ // same api)
125
+ #ifdef TORCH_CUDA_BUILD_MAIN_LIB
126
+ #define TORCH_CUDA_CPP_API C10_EXPORT
127
+ #define TORCH_CUDA_CU_API C10_EXPORT
128
+ #else
129
+ #define TORCH_CUDA_CPP_API C10_IMPORT
130
+ #define TORCH_CUDA_CU_API C10_IMPORT
131
+ #endif
132
+
133
+ #if defined(TORCH_HIP_BUILD_MAIN_LIB)
134
+ #define TORCH_HIP_CPP_API C10_EXPORT
135
+ #define TORCH_HIP_API C10_EXPORT
136
+ #else
137
+ #define TORCH_HIP_CPP_API C10_IMPORT
138
+ #define TORCH_HIP_API C10_IMPORT
139
+ #endif
140
+
141
+ #if defined(TORCH_XPU_BUILD_MAIN_LIB)
142
+ #define TORCH_XPU_API C10_EXPORT
143
+ #else
144
+ #define TORCH_XPU_API C10_IMPORT
145
+ #endif
146
+
147
+ // Enums only need to be exported on windows for non-CUDA files
148
+ #if defined(_WIN32) && defined(__CUDACC__)
149
+ #define C10_API_ENUM C10_API
150
+ #else
151
+ #define C10_API_ENUM
152
+ #endif
153
+ #endif // C10_MACROS_EXPORT_H_
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/Macros.h ADDED
@@ -0,0 +1,694 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_MACROS_MACROS_H_
2
+ #define C10_MACROS_MACROS_H_
3
+
4
+ #ifdef __cplusplus
5
+ #include <cassert>
6
+ #else
7
+ #include <assert.h>
8
+ #endif
9
+
10
+ /* Main entry for torch/headeronly/macros (used to be c10/macros).
11
+ *
12
+ * In your code, include torch/headeronly/macros/Macros.h directly, instead of
13
+ * individual files in this folder.
14
+ */
15
+
16
+ // For build systems that do not directly depend on CMake and directly build
17
+ // from the source directory (such as Buck), one may not have a cmake_macros.h
18
+ // file at all. In this case, the build system is responsible for providing
19
+ // correct macro definitions corresponding to the cmake_macros.h.in file.
20
+ //
21
+ // In such scenarios, one should define the macro
22
+ // C10_USING_CUSTOM_GENERATED_MACROS
23
+ // to inform this header that it does not need to include the cmake_macros.h
24
+ // file.
25
+
26
+ #ifndef C10_USING_CUSTOM_GENERATED_MACROS
27
+ #include <torch/headeronly/macros/cmake_macros.h>
28
+ #endif // C10_USING_CUSTOM_GENERATED_MACROS
29
+
30
+ #include <torch/headeronly/macros/Export.h>
31
+
32
+ #if defined(__clang__)
33
+ #define __ubsan_ignore_float_divide_by_zero__ \
34
+ __attribute__((no_sanitize("float-divide-by-zero")))
35
+ #define __ubsan_ignore_undefined__ __attribute__((no_sanitize("undefined")))
36
+ #define __ubsan_ignore_signed_int_overflow__ \
37
+ __attribute__((no_sanitize("signed-integer-overflow")))
38
+ #define __ubsan_ignore_pointer_overflow__ \
39
+ __attribute__((no_sanitize("pointer-overflow")))
40
+ #define __ubsan_ignore_function__ __attribute__((no_sanitize("function")))
41
+ #define __ubsan_ignore_float_cast_overflow__ \
42
+ __attribute__((no_sanitize("float-cast-overflow")))
43
+ #else
44
+ #define __ubsan_ignore_float_divide_by_zero__
45
+ #define __ubsan_ignore_undefined__
46
+ #define __ubsan_ignore_signed_int_overflow__
47
+ #define __ubsan_ignore_pointer_overflow__
48
+ #define __ubsan_ignore_function__
49
+ #define __ubsan_ignore_float_cast_overflow__
50
+ #endif
51
+
52
+ // Detect address sanitizer as some stuff doesn't work with it
53
+ #undef C10_ASAN_ENABLED
54
+
55
+ // for clang
56
+ #if defined(__has_feature)
57
+ #if ((__has_feature(address_sanitizer)))
58
+ #define C10_ASAN_ENABLED 1
59
+ #endif
60
+ #endif
61
+
62
+ // for gcc
63
+ #if defined(__SANITIZE_ADDRESS__)
64
+ #if __SANITIZE_ADDRESS__
65
+ #if !defined(C10_ASAN_ENABLED)
66
+ #define C10_ASAN_ENABLED 1
67
+ #endif
68
+ #endif
69
+ #endif
70
+
71
+ #if !defined(C10_ASAN_ENABLED)
72
+ #define C10_ASAN_ENABLED 0
73
+ #endif
74
+
75
+ // Detect undefined-behavior sanitizer (UBSAN)
76
+ #undef C10_UBSAN_ENABLED
77
+
78
+ // for clang or gcc >= 14
79
+ // NB: gcc 14 adds support for Clang's __has_feature
80
+ // https://gcc.gnu.org/gcc-14/changes.html
81
+ // gcc < 14 doesn't have a macro for UBSAN
82
+ // (e.g. __SANITIZE_UNDEFINED__ does not exist in gcc)
83
+ // https://github.com/google/sanitizers/issues/765
84
+ #if defined(__has_feature)
85
+ #if ((__has_feature(undefined_behavior_sanitizer)))
86
+ #define C10_UBSAN_ENABLED 1
87
+ #endif
88
+ #endif
89
+
90
+ #if !defined(C10_UBSAN_ENABLED)
91
+ #define C10_UBSAN_ENABLED 0
92
+ #endif
93
+
94
+ // Disable the copy and assignment operator for a class. Note that this will
95
+ // disable the usage of the class in std containers.
96
+ #define C10_DISABLE_COPY_AND_ASSIGN(classname) \
97
+ classname(const classname&) = delete; \
98
+ classname& operator=(const classname&) = delete
99
+
100
+ #define C10_CONCATENATE_IMPL(s1, s2) s1##s2
101
+ #define C10_CONCATENATE(s1, s2) C10_CONCATENATE_IMPL(s1, s2)
102
+
103
+ #define C10_MACRO_EXPAND(args) args
104
+
105
+ #define C10_STRINGIZE_IMPL(x) #x
106
+ #define C10_STRINGIZE(x) C10_STRINGIZE_IMPL(x)
107
+
108
+ /**
109
+ * C10_ANONYMOUS_VARIABLE(str) introduces a new identifier which starts with
110
+ * str and ends with a unique number.
111
+ */
112
+ #ifdef __COUNTER__
113
+ #define C10_UID __COUNTER__
114
+ #define C10_ANONYMOUS_VARIABLE(str) C10_CONCATENATE(str, __COUNTER__)
115
+ #else
116
+ #define C10_UID __LINE__
117
+ #define C10_ANONYMOUS_VARIABLE(str) C10_CONCATENATE(str, __LINE__)
118
+ #endif
119
+
120
+ #ifdef __has_cpp_attribute
121
+ #define C10_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
122
+ #else
123
+ #define C10_HAS_CPP_ATTRIBUTE(x) (0)
124
+ #endif
125
+
126
+ #ifndef FBCODE_CAFFE2
127
+ /// DEPRECATED: Warn if a type or return value is discarded.
128
+ #define C10_NODISCARD [[nodiscard]]
129
+
130
+ /// DEPRECATED: Suppress an unused variable.
131
+ #define C10_UNUSED [[maybe_unused]]
132
+ #endif
133
+
134
+ #if !defined(__has_attribute)
135
+ #define __has_attribute(x) 0
136
+ #endif
137
+
138
+ // Direct port of LLVM_ATTRIBUTE_USED.
139
+ #if __has_attribute(used)
140
+ #define C10_USED __attribute__((__used__))
141
+ #else
142
+ #define C10_USED
143
+ #endif
144
+
145
+ #define C10_RESTRICT __restrict
146
+
147
+ #ifdef __cplusplus
148
+
149
+ // Simply define the namespace, in case a dependent library want to refer to
150
+ // the c10 namespace but not any nontrivial files.
151
+ namespace c10 {}
152
+ namespace c10::cuda {}
153
+ namespace c10::hip {}
154
+ namespace c10::xpu {}
155
+
156
+ // Since C10 is the core library for caffe2 (and aten), we will simply reroute
157
+ // all abstractions defined in c10 to be available in caffe2 as well.
158
+ // This is only for backwards compatibility. Please use the symbols from the
159
+ // c10 namespace where possible.
160
+ namespace caffe2 {
161
+ using namespace c10;
162
+ }
163
+ namespace at {
164
+ using namespace c10;
165
+ }
166
+ namespace at::cuda {
167
+ using namespace c10::cuda;
168
+ } // namespace at::cuda
169
+
170
+ // WARNING!!! THIS IS A GIANT HACK!!!
171
+ // This line means you cannot simultaneously include c10/hip
172
+ // and c10/cuda and then use them from the at::cuda namespace.
173
+ // This is true in practice, because HIPIFY works inplace on
174
+ // files in ATen/cuda, so it assumes that c10::hip is available
175
+ // from at::cuda. This namespace makes that happen. When
176
+ // HIPIFY is no longer out-of-place, we can switch the cuda
177
+ // here to hip and everyone is happy.
178
+ namespace at::cuda {
179
+ using namespace c10::hip;
180
+ } // namespace at::cuda
181
+
182
+ namespace at::xpu {
183
+ using namespace c10::xpu;
184
+ } // namespace at::xpu
185
+
186
+ #endif // __cplusplus
187
+
188
+ // C10_LIKELY/C10_UNLIKELY
189
+ //
190
+ // These macros provide parentheses, so you can use these macros as:
191
+ //
192
+ // if C10_LIKELY(some_expr) {
193
+ // ...
194
+ // }
195
+ //
196
+ // NB: static_cast to boolean is mandatory in C++, because __builtin_expect
197
+ // takes a long argument, which means you may trigger the wrong conversion
198
+ // without it.
199
+ //
200
+ #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
201
+ #define C10_LIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 1))
202
+ #define C10_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0))
203
+ #else
204
+ #define C10_LIKELY(expr) (expr)
205
+ #define C10_UNLIKELY(expr) (expr)
206
+ #endif
207
+
208
+ /// C10_NOINLINE - Functions whose declaration is annotated with this will not
209
+ /// be inlined.
210
+ #ifdef __GNUC__
211
+ #define C10_NOINLINE __attribute__((noinline))
212
+ #elif _MSC_VER
213
+ #define C10_NOINLINE __declspec(noinline)
214
+ #else
215
+ #define C10_NOINLINE
216
+ #endif
217
+
218
+ #if defined(_MSC_VER)
219
+ #define C10_ALWAYS_INLINE __forceinline
220
+ #elif __has_attribute(always_inline) || defined(__GNUC__)
221
+ #define C10_ALWAYS_INLINE __attribute__((__always_inline__)) inline
222
+ #else
223
+ #define C10_ALWAYS_INLINE inline
224
+ #endif
225
+
226
+ // Unlike C10_ALWAYS_INLINE, C10_ALWAYS_INLINE_ATTRIBUTE can be used
227
+ // on a lambda.
228
+ #if defined(_MSC_VER)
229
+ // MSVC 14.39 is reasonably recent and doesn't like
230
+ // [[msvc::forceinline]] on a lambda, so don't try to use it.
231
+ #define C10_ALWAYS_INLINE_ATTRIBUTE
232
+ #elif __has_attribute(always_inline) || defined(__GNUC__)
233
+ #define C10_ALWAYS_INLINE_ATTRIBUTE __attribute__((__always_inline__))
234
+ #else
235
+ #define C10_ALWAYS_INLINE_ATTRIBUTE
236
+ #endif
237
+
238
+ #if defined(_MSC_VER)
239
+ #define C10_ATTR_VISIBILITY_HIDDEN
240
+ #elif defined(__GNUC__)
241
+ #define C10_ATTR_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
242
+ #else
243
+ #define C10_ATTR_VISIBILITY_HIDDEN
244
+ #endif
245
+
246
+ #define C10_ERASE C10_ALWAYS_INLINE C10_ATTR_VISIBILITY_HIDDEN
247
+
248
+ #ifdef __cplusplus
249
+ #include <cstdint>
250
+ #else
251
+ #include <stdint.h>
252
+ #endif
253
+
254
+ #ifdef __HIPCC__
255
+ // Unlike CUDA, HIP requires a HIP header to be included for __host__ to work.
256
+ // We do this #include here so that C10_HOST_DEVICE and friends will Just Work.
257
+ // See https://github.com/ROCm/hip/issues/441
258
+ #include <hip/hip_runtime.h>
259
+ #endif
260
+
261
+ #if defined(__CUDACC__) || defined(__HIPCC__)
262
+ // Designates functions callable from the host (CPU) and the device (GPU)
263
+ #define C10_HOST_DEVICE __host__ __device__
264
+ #define C10_DEVICE __device__
265
+ #define C10_HOST __host__
266
+ // constants from
267
+ // (https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications)
268
+ // The maximum number of threads per multiprocessor is 1024 for Turing
269
+ // architecture (7.5), 1536 for Geforce Ampere (8.6)/Jetson Orin (8.7), and
270
+ // 2048 for all other architectures. You'll get warnings if you exceed these
271
+ // constants. Hence, the following macros adjust the input values from the user
272
+ // to resolve potential warnings.
273
+ #if __CUDA_ARCH__ == 750
274
+ constexpr uint32_t CUDA_MAX_THREADS_PER_SM = 1024;
275
+ #elif __CUDA_ARCH__ == 860 || __CUDA_ARCH__ == 870 || __CUDA_ARCH__ == 890 || \
276
+ __CUDA_ARCH__ == 1200
277
+ constexpr uint32_t CUDA_MAX_THREADS_PER_SM = 1536;
278
+ #else
279
+ constexpr uint32_t CUDA_MAX_THREADS_PER_SM = 2048;
280
+ #endif
281
+ // CUDA_MAX_THREADS_PER_BLOCK is same for all architectures currently
282
+ constexpr uint32_t CUDA_MAX_THREADS_PER_BLOCK = 1024;
283
+ // CUDA_THREADS_PER_BLOCK_FALLBACK is the "canonical fallback" choice of block
284
+ // size. 256 is a good number for this fallback and should give good occupancy
285
+ // and versatility across all architectures.
286
+ constexpr uint32_t CUDA_THREADS_PER_BLOCK_FALLBACK = 256;
287
+ // NOTE: if you are thinking of constexpr-ify the inputs to launch bounds, it
288
+ // turns out that although __launch_bounds__ can take constexpr, it
289
+ // can't take a constexpr that has anything to do with templates.
290
+ // Currently we use launch_bounds that depend on template arguments in
291
+ // Loops.cuh, Reduce.cuh and LossCTC.cuh. Hence, C10_MAX_THREADS_PER_BLOCK
292
+ // and C10_MIN_BLOCKS_PER_SM are kept as macros.
293
+ // Suppose you were planning to write __launch_bounds__(a, b), based on your
294
+ // performance tuning on a modern GPU. Instead, you should write
295
+ // __launch_bounds__(C10_MAX_THREADS_PER_BLOCK(a), C10_MIN_BLOCKS_PER_SM(a, b)),
296
+ // which will also properly respect limits on old architectures.
297
+ #define C10_MAX_THREADS_PER_BLOCK(val) \
298
+ (((val) <= CUDA_MAX_THREADS_PER_BLOCK) ? (val) \
299
+ : CUDA_THREADS_PER_BLOCK_FALLBACK)
300
+ #define C10_MIN_BLOCKS_PER_SM(threads_per_block, blocks_per_sm) \
301
+ ((((threads_per_block) * (blocks_per_sm) <= CUDA_MAX_THREADS_PER_SM) \
302
+ ? (blocks_per_sm) \
303
+ : ((CUDA_MAX_THREADS_PER_SM + (threads_per_block) - 1) / \
304
+ (threads_per_block))))
305
+ // C10_LAUNCH_BOUNDS is analogous to __launch_bounds__
306
+ #define C10_LAUNCH_BOUNDS_0 \
307
+ __launch_bounds__( \
308
+ 256, 4) // default launch bounds that should give good occupancy and
309
+ // versatility across all architectures.
310
+ #define C10_LAUNCH_BOUNDS_1(max_threads_per_block) \
311
+ __launch_bounds__((C10_MAX_THREADS_PER_BLOCK((max_threads_per_block))))
312
+ #define C10_LAUNCH_BOUNDS_2(max_threads_per_block, min_blocks_per_sm) \
313
+ __launch_bounds__( \
314
+ (C10_MAX_THREADS_PER_BLOCK((max_threads_per_block))), \
315
+ (C10_MIN_BLOCKS_PER_SM((max_threads_per_block), (min_blocks_per_sm))))
316
+ #else
317
+ #define C10_HOST_DEVICE
318
+ #define C10_HOST
319
+ #define C10_DEVICE
320
+ #endif
321
+
322
+ #if defined(USE_ROCM)
323
+ #define C10_HIP_HOST_DEVICE __host__ __device__
324
+ #else
325
+ #define C10_HIP_HOST_DEVICE
326
+ #endif
327
+
328
+ #if defined(USE_ROCM)
329
+ // C10_WARP_SIZE is only allowed for device code.
330
+ // Host code _must_ use at::cuda::warp_size()
331
+ // HIP header used to define warpSize as a constexpr that was either 32 or 64
332
+ // depending on the target device, and then always set it to 64 for host code.
333
+ // Host pass of HIP compiler needs C10_WARP_SIZE defined to _something_ so we
334
+ // set it to something unreasonable to trigger obvious host code errors.
335
+
336
+ namespace at::cuda {
337
+ TORCH_CUDA_CPP_API int warp_size();
338
+ }
339
+ #ifdef __HIPCC__
340
+ static inline int __host__ C10_WARP_SIZE_INTERNAL() {
341
+ return at::cuda::warp_size();
342
+ }
343
+
344
+ static inline constexpr int __device__ C10_WARP_SIZE_INTERNAL() {
345
+ #if defined(__GFX9__)
346
+ return 64;
347
+ #else // __GFX9__
348
+ return 32;
349
+ #endif // __GFX9__
350
+ }
351
+ #else // __HIPCC__
352
+ static inline int C10_WARP_SIZE_INTERNAL() {
353
+ return at::cuda::warp_size();
354
+ }
355
+ #endif // __HIPCC__
356
+
357
+ #define C10_WARP_SIZE (C10_WARP_SIZE_INTERNAL())
358
+ #define C10_WARP_SIZE_STATIC 64
359
+
360
+ #else // defined(USE_ROCM)
361
+ #define C10_WARP_SIZE 32
362
+ #endif
363
+
364
+ #if defined(_MSC_VER) && _MSC_VER <= 1900
365
+ #define __func__ __FUNCTION__
366
+ #endif
367
+
368
+ // CUDA_KERNEL_ASSERT checks the assertion
369
+ // even when NDEBUG is defined. This is useful for important assertions in CUDA
370
+ // code that would otherwise be suppressed when building Release.
371
+ #if defined(__ANDROID__) || defined(__APPLE__) || defined(__FreeBSD__)
372
+ // Those platforms do not support assert()
373
+ #define CUDA_KERNEL_ASSERT(cond)
374
+ #define CUDA_KERNEL_ASSERT_MSG(cond, msg)
375
+ #define CUDA_KERNEL_ASSERT_PRINTF(cond, msg, ...)
376
+ #define SYCL_KERNEL_ASSERT(cond)
377
+ #elif defined(_MSC_VER)
378
+ #if defined(NDEBUG)
379
+ extern "C" {
380
+ C10_IMPORT
381
+ #if defined(__SYCL_DEVICE_ONLY__)
382
+ extern SYCL_EXTERNAL void _wassert(
383
+ const wchar_t* wexpr,
384
+ const wchar_t* wfile,
385
+ unsigned line);
386
+ #else
387
+ #if defined(__CUDA_ARCH__)
388
+ __host__ __device__
389
+ #endif // __CUDA_ARCH__
390
+ void
391
+ _wassert(wchar_t const* _Message, wchar_t const* _File, unsigned _Line);
392
+ #endif // __SYCL_DEVICE_ONLY__
393
+ }
394
+ #endif // NDEBUG
395
+ #define CUDA_KERNEL_ASSERT(cond) \
396
+ if (C10_UNLIKELY(!(cond))) { \
397
+ (void)(_wassert( \
398
+ _CRT_WIDE(#cond), \
399
+ _CRT_WIDE(__FILE__), \
400
+ static_cast<unsigned>(__LINE__)), \
401
+ 0); \
402
+ }
403
+ // TODO: This doesn't assert the message because I (chilli) couldn't figure out
404
+ // a nice way to convert a char* to a wchar_t*
405
+ #define CUDA_KERNEL_ASSERT_MSG(cond, msg) \
406
+ if (C10_UNLIKELY(!(cond))) { \
407
+ (void)(_wassert( \
408
+ _CRT_WIDE(#cond), \
409
+ _CRT_WIDE(__FILE__), \
410
+ static_cast<unsigned>(__LINE__)), \
411
+ 0); \
412
+ }
413
+ #define CUDA_KERNEL_ASSERT_PRINTF(cond, msg, ...) \
414
+ if (C10_UNLIKELY(!(cond))) { \
415
+ (void)(printf( \
416
+ "[CUDA_KERNEL_ASSERT] " __FILE__ ":" C10_STRINGIZE( \
417
+ __LINE__) ": %s: block: [%d,%d,%d], thread: [%d,%d,%d]: " \
418
+ "Assertion failed: `" #cond "`: " msg "\n", \
419
+ __func__, \
420
+ blockIdx.x, \
421
+ blockIdx.y, \
422
+ blockIdx.z, \
423
+ threadIdx.x, \
424
+ threadIdx.y, \
425
+ threadIdx.z, \
426
+ ##__VA_ARGS__)); \
427
+ (void)(_wassert( \
428
+ _CRT_WIDE(#cond), \
429
+ _CRT_WIDE(__FILE__), \
430
+ static_cast<unsigned>(__LINE__)), \
431
+ 0); \
432
+ }
433
+ #define SYCL_KERNEL_ASSERT(cond) \
434
+ if (C10_UNLIKELY(!(cond))) { \
435
+ (void)(_wassert( \
436
+ _CRT_WIDE(#cond), \
437
+ _CRT_WIDE(__FILE__), \
438
+ static_cast<unsigned>(__LINE__)), \
439
+ 0); \
440
+ }
441
+ #else // __APPLE__, _MSC_VER
442
+ #if defined(NDEBUG)
443
+ extern "C" {
444
+ #if defined(__SYCL_DEVICE_ONLY__)
445
+ extern SYCL_EXTERNAL void __assert_fail(
446
+ const char* expr,
447
+ const char* file,
448
+ unsigned int line,
449
+ const char* func);
450
+ #elif (defined(__EMSCRIPTEN__))
451
+ // As defined in assert.h in the Emscripten stdlib
452
+ _Noreturn void __assert_fail(
453
+ const char* expr,
454
+ const char* file,
455
+ int line,
456
+ const char* func);
457
+ #else // __SYCL_DEVICE_ONLY__
458
+ #if (defined(__CUDA_ARCH__) && !(defined(__clang__) && defined(__CUDA__)))
459
+ // CUDA supports __assert_fail function which are common for both device
460
+ // and host side code.
461
+ __host__ __device__
462
+ #endif
463
+
464
+ // This forward declaration matching the declaration of __assert_fail
465
+ // exactly how it is in glibc in case parts of the program are compiled with
466
+ // different NDEBUG settings. Otherwise we might get 'ambiguous declaration'
467
+ // error. Note: On ROCm - this declaration serves for host side compilation.
468
+ void
469
+ __assert_fail(
470
+ const char* assertion,
471
+ const char* file,
472
+ unsigned int line,
473
+ const char* function) noexcept __attribute__((__noreturn__));
474
+
475
+ #endif // __SYCL_DEVICE_ONLY__
476
+ }
477
+ #endif // NDEBUG
478
+ // ROCm disables kernel assert by default for performance considerations.
479
+ // Though ROCm supports __assert_fail, it uses kernel printf which has
480
+ // a non-negligible performance impact even if the assert condition is
481
+ // never triggered. We choose to use abort() instead which will still
482
+ // terminate the application but without a more useful error message.
483
+ #if !defined(C10_USE_ROCM_KERNEL_ASSERT) && defined(USE_ROCM)
484
+ #define CUDA_KERNEL_ASSERT(cond) \
485
+ if C10_UNLIKELY (!(cond)) { \
486
+ abort(); \
487
+ }
488
+ #define CUDA_KERNEL_ASSERT_MSG(cond, msg) \
489
+ if C10_UNLIKELY (!(cond)) { \
490
+ abort(); \
491
+ }
492
+ #define CUDA_KERNEL_ASSERT_PRINTF(cond, msg, ...) \
493
+ if C10_UNLIKELY (!(cond)) { \
494
+ abort(); \
495
+ }
496
+ #define SYCL_KERNEL_ASSERT(cond) \
497
+ if C10_UNLIKELY (!(cond)) { \
498
+ abort(); \
499
+ }
500
+ #else
501
+ #define CUDA_KERNEL_ASSERT(cond) \
502
+ if (C10_UNLIKELY(!(cond))) { \
503
+ __assert_fail( \
504
+ #cond, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \
505
+ }
506
+ #define CUDA_KERNEL_ASSERT_MSG(cond, msg) \
507
+ if (C10_UNLIKELY(!(cond))) { \
508
+ __assert_fail( \
509
+ msg, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \
510
+ }
511
+ #define CUDA_KERNEL_ASSERT_PRINTF(cond, msg, ...) \
512
+ if (C10_UNLIKELY(!(cond))) { \
513
+ printf( \
514
+ "[CUDA_KERNEL_ASSERT] " __FILE__ ":" C10_STRINGIZE( \
515
+ __LINE__) ": %s: block: [%d,%d,%d], thread: [%d,%d,%d]: " \
516
+ "Assertion failed: `" #cond "`: " msg "\n", \
517
+ __func__, \
518
+ blockIdx.x, \
519
+ blockIdx.y, \
520
+ blockIdx.z, \
521
+ threadIdx.x, \
522
+ threadIdx.y, \
523
+ threadIdx.z, \
524
+ ##__VA_ARGS__); \
525
+ __assert_fail( \
526
+ #cond, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \
527
+ }
528
+ #define SYCL_KERNEL_ASSERT(cond) \
529
+ if (C10_UNLIKELY(!(cond))) { \
530
+ __assert_fail( \
531
+ #cond, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \
532
+ }
533
+ #endif // C10_USE_ROCM_KERNEL_ASSERT && USE_ROCM
534
+ #endif // __APPLE__
535
+
536
+ // Compile-time switch to control how assertions are logged inside CUDA kernels.
537
+ // If C10_CUDA_VERBOSE_ASSERT is defined, CUDA_KERNEL_ASSERT_VERBOSE will
538
+ // take addition information passed to the macro and forward them to
539
+ // CUDA_KERNEL_ASSERT_PRINTF If C10_CUDA_VERBOSE_ASSERT is not defined,
540
+ // CUDA_KERNEL_ASSERT_VERBOSE will behave the same as CUDA_KERNEL_ASSERT.
541
+ #ifdef C10_ENABLE_VERBOSE_ASSERT
542
+ #define CUDA_KERNEL_ASSERT_VERBOSE(cond, ...) \
543
+ CUDA_KERNEL_ASSERT_PRINTF(cond, __VA_ARGS__)
544
+ #else
545
+ #define CUDA_KERNEL_ASSERT_VERBOSE(cond, ...) CUDA_KERNEL_ASSERT(cond)
546
+ #endif
547
+
548
+ #ifdef __APPLE__
549
+ #include <TargetConditionals.h>
550
+ #endif
551
+
552
+ #if defined(__ANDROID__)
553
+ #define C10_ANDROID 1
554
+ #define C10_MOBILE 1
555
+ #elif ( \
556
+ defined(__APPLE__) && \
557
+ (TARGET_IPHONE_SIMULATOR || TARGET_OS_SIMULATOR || TARGET_OS_IPHONE))
558
+ #define C10_IOS 1
559
+ #define C10_MOBILE 1
560
+ #endif // ANDROID / IOS
561
+
562
+ #if defined(C10_MOBILE) && C10_MOBILE
563
+ #define C10_ALWAYS_INLINE_UNLESS_MOBILE inline
564
+ #else
565
+ #define C10_ALWAYS_INLINE_UNLESS_MOBILE C10_ALWAYS_INLINE
566
+ #endif
567
+
568
+ #if !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
569
+ #define CONSTEXPR_EXCEPT_WIN_CUDA constexpr
570
+ #define C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA constexpr
571
+
572
+ #define STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(field, val) \
573
+ static constexpr const char field[] = val;
574
+ #define STATIC_CONST_STR_OUT_OF_LINE_FOR_WIN_CUDA(cls, field, val)
575
+ #endif // !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
576
+
577
+ #ifndef HAS_DEMANGLE
578
+ #if defined(__ANDROID__) || defined(_WIN32) || defined(__EMSCRIPTEN__)
579
+ #define HAS_DEMANGLE 0
580
+ #elif defined(__APPLE__) && \
581
+ (TARGET_IPHONE_SIMULATOR || TARGET_OS_SIMULATOR || TARGET_OS_IPHONE)
582
+ #define HAS_DEMANGLE 0
583
+ #else
584
+ #define HAS_DEMANGLE 1
585
+ #endif
586
+ #endif // HAS_DEMANGLE
587
+
588
+ #define _C10_PRAGMA__(string) _Pragma(#string)
589
+ #define _C10_PRAGMA_(string) _C10_PRAGMA__(string)
590
+
591
+ #ifdef __clang__
592
+ #define C10_CLANG_DIAGNOSTIC_PUSH() _Pragma("clang diagnostic push")
593
+ #define C10_CLANG_DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
594
+ #define C10_CLANG_DIAGNOSTIC_IGNORE(flag) \
595
+ _C10_PRAGMA_(clang diagnostic ignored flag)
596
+ #define C10_CLANG_HAS_WARNING(flag) __has_warning(flag)
597
+ #else
598
+ #define C10_CLANG_DIAGNOSTIC_PUSH()
599
+ #define C10_CLANG_DIAGNOSTIC_POP()
600
+ #define C10_CLANG_DIAGNOSTIC_IGNORE(flag)
601
+ #define C10_CLANG_HAS_WARNING(flag) 0
602
+ #endif
603
+
604
+ #ifdef __clang__
605
+
606
+ #define C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED(warning) \
607
+ _C10_PRAGMA_(clang diagnostic push) \
608
+ _C10_PRAGMA_(clang diagnostic ignored "-Wunknown-warning-option") \
609
+ _C10_PRAGMA_(clang diagnostic ignored warning)
610
+
611
+ #define C10_DIAGNOSTIC_POP() _C10_PRAGMA_(clang diagnostic pop)
612
+
613
+ #elif __GNUC__
614
+
615
+ #define C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED(warning) \
616
+ _C10_PRAGMA_(GCC diagnostic push) \
617
+ _C10_PRAGMA_(GCC diagnostic ignored "-Wpragmas") \
618
+ _C10_PRAGMA_(GCC diagnostic ignored warning)
619
+
620
+ #define C10_DIAGNOSTIC_POP() _C10_PRAGMA_(GCC diagnostic pop)
621
+
622
+ #else
623
+
624
+ #define C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED(warning)
625
+ #define C10_DIAGNOSTIC_POP()
626
+
627
+ #endif
628
+
629
+ // This macro is used to find older C++ compilers
630
+ // that don't support move optimization for return values.
631
+
632
+ #if (defined(__GNUC__) && __GNUC__ < 13) || \
633
+ (defined(__clang_major__) && __clang_major__ < 13)
634
+ #define C10_RETURN_MOVE_IF_OLD_COMPILER 1
635
+ #else
636
+ #define C10_RETURN_MOVE_IF_OLD_COMPILER 0
637
+ #endif
638
+
639
+ // The HIDDEN_NAMESPACE_BEGIN and HIDDEN_NAMESPACE_END below
640
+ // are needed for maintaining robustness in our header APIs in
641
+ // torch/headeronly and torch/csrc/stable under the namespaces
642
+ // torch::headeronly and torch::stable respectively. We enforce
643
+ // hidden visibility for these APIs because we want to enable
644
+ // loading custom extensions compiled against different libtorch
645
+ // versions where these APIs may have changed.
646
+
647
+ // Helper macros to handle 1-3 hidden namespace levels when not windows
648
+ #define _HIDDEN_NS_GET_MACRO(_1, _2, _3, NAME, ...) NAME
649
+ #define _HIDDEN_NS_1(n1) namespace n1 __attribute__((visibility("hidden"))) {
650
+ #define _HIDDEN_NS_2(n1, n2) \
651
+ namespace n1 { \
652
+ namespace n2 __attribute__((visibility("hidden"))) {
653
+ #define _HIDDEN_NS_3(n1, n2, n3) \
654
+ namespace n1::n2 { \
655
+ namespace n3 __attribute__((visibility("hidden"))) {
656
+
657
+ // Helper macros to close namespaces when not windows
658
+ #define _HIDDEN_NS_END_1(n1) }
659
+ #define _HIDDEN_NS_END_N(n1, ...) \
660
+ } \
661
+ }
662
+
663
+ // Helper macros to join strs with :: (for win, where symbols are hidden by
664
+ // default)
665
+ #define _EXPAND(...) __VA_ARGS__
666
+ #define _JOIN_GET_MACRO(_1, _2, _3, NAME, ...) NAME
667
+ #define _JOIN_NS1(a) a
668
+ #define _JOIN_NS2(a, b) a::b
669
+ #define _JOIN_NS3(a, b, c) a::b::c
670
+
671
+ #if !defined(HIDDEN_NAMESPACE_BEGIN)
672
+ #if defined(__GNUG__) && !defined(_WIN32)
673
+ #define HIDDEN_NAMESPACE_BEGIN(...) \
674
+ _HIDDEN_NS_GET_MACRO( \
675
+ __VA_ARGS__, _HIDDEN_NS_3, _HIDDEN_NS_2, _HIDDEN_NS_1)(__VA_ARGS__)
676
+ #else
677
+ #define HIDDEN_NAMESPACE_BEGIN(...) \
678
+ namespace _EXPAND(_JOIN_GET_MACRO( \
679
+ __VA_ARGS__, _JOIN_NS3, _JOIN_NS2, _JOIN_NS1)(__VA_ARGS__)) {
680
+ #endif
681
+ #endif
682
+
683
+ #if !defined(HIDDEN_NAMESPACE_END)
684
+ #if defined(__GNUG__) && !defined(_WIN32)
685
+ #define HIDDEN_NAMESPACE_END(...) \
686
+ _HIDDEN_NS_GET_MACRO( \
687
+ __VA_ARGS__, _HIDDEN_NS_END_N, _HIDDEN_NS_END_N, _HIDDEN_NS_END_1)( \
688
+ __VA_ARGS__)
689
+ #else
690
+ #define HIDDEN_NAMESPACE_END(...) }
691
+ #endif
692
+ #endif
693
+
694
+ #endif // C10_MACROS_MACROS_H_
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/macros/cmake_macros.h ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef C10_MACROS_CMAKE_MACROS_H_
2
+ #define C10_MACROS_CMAKE_MACROS_H_
3
+
4
+ // Automatically generated header file for the C10 library.
5
+ // Do not include this file directly. Instead, include torch/headeronly/macros/Macros.h.
6
+
7
+ #define C10_BUILD_SHARED_LIBS
8
+ /* #undef C10_USE_GLOG */
9
+ /* #undef C10_USE_GFLAGS */
10
+ /* #undef C10_USE_NUMA */
11
+ /* #undef C10_USE_MSVC_STATIC_RUNTIME */
12
+ /* #undef C10_USE_ROCM_KERNEL_ASSERT */
13
+
14
+ #endif // C10_MACROS_CMAKE_MACROS_H_
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/BFloat16.h ADDED
@@ -0,0 +1,480 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // Defines the bloat16 type (brain floating-point). This representation uses
4
+ // 1 bit for the sign, 8 bits for the exponent and 7 bits for the mantissa.
5
+
6
+ #include <torch/headeronly/macros/Macros.h>
7
+ #include <torch/headeronly/util/bit_cast.h>
8
+
9
+ #include <cmath>
10
+ #include <cstdint>
11
+ #include <cstring>
12
+ #include <iosfwd>
13
+ #include <ostream>
14
+
15
+ #if defined(__CUDACC__) && !defined(USE_ROCM)
16
+ #include <cuda_bf16.h>
17
+ #endif
18
+
19
+ #if defined(CL_SYCL_LANGUAGE_VERSION)
20
+ #include <CL/sycl.hpp> // for SYCL 1.2.1
21
+ #elif defined(SYCL_LANGUAGE_VERSION)
22
+ #include <sycl/sycl.hpp> // for SYCL 2020
23
+ #endif
24
+
25
+ namespace c10 {
26
+
27
+ struct alignas(2) BFloat16 {
28
+ uint16_t x;
29
+
30
+ // HIP wants __host__ __device__ tag, CUDA does not
31
+ #if defined(USE_ROCM) && defined(__HIPCC__)
32
+ C10_HOST_DEVICE BFloat16() = default;
33
+ #else
34
+ BFloat16() = default;
35
+ #endif
36
+
37
+ struct from_bits_t {};
38
+ static constexpr C10_HOST_DEVICE from_bits_t from_bits() {
39
+ return from_bits_t();
40
+ }
41
+
42
+ constexpr C10_HOST_DEVICE BFloat16(
43
+ unsigned short bits,
44
+ from_bits_t /*unused*/)
45
+ : x(bits) {}
46
+ /* implicit */ inline C10_HOST_DEVICE BFloat16(float value);
47
+ inline C10_HOST_DEVICE operator float() const;
48
+
49
+ #if defined(__CUDACC__) && !defined(USE_ROCM)
50
+ inline C10_HOST_DEVICE BFloat16(const __nv_bfloat16& value);
51
+ explicit inline C10_HOST_DEVICE operator __nv_bfloat16() const;
52
+ #endif
53
+
54
+ #if defined(SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS)
55
+ inline C10_HOST_DEVICE BFloat16(const sycl::ext::oneapi::bfloat16& value);
56
+ explicit inline C10_HOST_DEVICE operator sycl::ext::oneapi::bfloat16() const;
57
+ #endif
58
+ };
59
+
60
+ inline std::ostream& operator<<(std::ostream& out, const BFloat16& value) {
61
+ out << (float)value;
62
+ return out;
63
+ }
64
+
65
+ namespace detail {
66
+ inline C10_HOST_DEVICE float f32_from_bits(uint16_t src) {
67
+ float res = 0;
68
+ uint32_t tmp = src;
69
+ tmp <<= 16;
70
+
71
+ #if defined(USE_ROCM) && defined(__HIPCC__)
72
+ float* tempRes;
73
+
74
+ // We should be using memcpy in order to respect the strict aliasing rule
75
+ // but it fails in the HIP environment.
76
+ tempRes = reinterpret_cast<float*>(&tmp);
77
+ res = *tempRes;
78
+ #else
79
+ std::memcpy(&res, &tmp, sizeof(tmp));
80
+ #endif
81
+
82
+ return res;
83
+ }
84
+
85
+ inline C10_HOST_DEVICE uint16_t bits_from_f32(float src) {
86
+ uint32_t res = 0;
87
+
88
+ #if defined(USE_ROCM) && defined(__HIPCC__)
89
+ // We should be using memcpy in order to respect the strict aliasing rule
90
+ // but it fails in the HIP environment.
91
+ uint32_t* tempRes = reinterpret_cast<uint32_t*>(&src);
92
+ res = *tempRes;
93
+ #else
94
+ std::memcpy(&res, &src, sizeof(res));
95
+ #endif
96
+
97
+ return res >> 16;
98
+ }
99
+
100
+ inline C10_HOST_DEVICE uint16_t round_to_nearest_even(float src) {
101
+ #if defined(USE_ROCM) && defined(__HIPCC__)
102
+ if (src != src) {
103
+ #elif defined(_MSC_VER)
104
+ if (isnan(src)) {
105
+ #else
106
+ if (std::isnan(src)) {
107
+ #endif
108
+ return UINT16_C(0x7FC0);
109
+ } else {
110
+ const uint32_t U32 = c10::bit_cast<uint32_t>(src);
111
+ uint32_t rounding_bias = ((U32 >> 16) & 1) + UINT32_C(0x7FFF);
112
+ return static_cast<uint16_t>((U32 + rounding_bias) >> 16);
113
+ }
114
+ }
115
+
116
+ } // namespace detail
117
+
118
+ //-------- the following is copied from c10/util/BFloat16-inl.h ---------//
119
+ C10_CLANG_DIAGNOSTIC_PUSH()
120
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
121
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
122
+ #endif
123
+
124
+ /// Constructors
125
+ inline C10_HOST_DEVICE BFloat16::BFloat16(float value)
126
+ :
127
+ #if defined(__CUDACC__) && !defined(USE_ROCM) && defined(__CUDA_ARCH__) && \
128
+ __CUDA_ARCH__ >= 800
129
+ x(__bfloat16_as_ushort(__float2bfloat16(value)))
130
+ #elif defined(__SYCL_DEVICE_ONLY__) && \
131
+ defined(SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS)
132
+ x(c10::bit_cast<uint16_t>(sycl::ext::oneapi::bfloat16(value)))
133
+ #else
134
+ // RNE by default
135
+ x(detail::round_to_nearest_even(value))
136
+ #endif
137
+ {
138
+ }
139
+
140
+ /// Implicit conversions
141
+ inline C10_HOST_DEVICE BFloat16::operator float() const {
142
+ #if defined(__CUDACC__) && !defined(USE_ROCM)
143
+ return __bfloat162float(*reinterpret_cast<const __nv_bfloat16*>(&x));
144
+ #elif defined(__SYCL_DEVICE_ONLY__) && \
145
+ defined(SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS)
146
+ return float(*reinterpret_cast<const sycl::ext::oneapi::bfloat16*>(&x));
147
+ #else
148
+ return detail::f32_from_bits(x);
149
+ #endif
150
+ }
151
+
152
+ #if defined(__CUDACC__) && !defined(USE_ROCM)
153
+ inline C10_HOST_DEVICE BFloat16::BFloat16(const __nv_bfloat16& value) {
154
+ x = *reinterpret_cast<const unsigned short*>(&value);
155
+ }
156
+ inline C10_HOST_DEVICE BFloat16::operator __nv_bfloat16() const {
157
+ return *reinterpret_cast<const __nv_bfloat16*>(&x);
158
+ }
159
+ #endif
160
+
161
+ #if defined(SYCL_EXT_ONEAPI_BFLOAT16_MATH_FUNCTIONS)
162
+ inline C10_HOST_DEVICE BFloat16::BFloat16(
163
+ const sycl::ext::oneapi::bfloat16& value) {
164
+ x = *reinterpret_cast<const unsigned short*>(&value);
165
+ }
166
+ inline C10_HOST_DEVICE BFloat16::operator sycl::ext::oneapi::bfloat16() const {
167
+ return *reinterpret_cast<const sycl::ext::oneapi::bfloat16*>(&x);
168
+ }
169
+ #endif
170
+
171
+ // CUDA intrinsics
172
+
173
+ #if defined(__CUDACC__) || defined(__HIPCC__)
174
+ inline C10_DEVICE BFloat16 __ldg(const BFloat16* ptr) {
175
+ #if !defined(USE_ROCM) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
176
+ return __ldg(reinterpret_cast<const __nv_bfloat16*>(ptr));
177
+ #else
178
+ return *ptr;
179
+ #endif
180
+ }
181
+ #endif
182
+
183
+ /// Arithmetic
184
+
185
+ inline C10_HOST_DEVICE BFloat16
186
+ operator+(const BFloat16& a, const BFloat16& b) {
187
+ return static_cast<float>(a) + static_cast<float>(b);
188
+ }
189
+
190
+ inline C10_HOST_DEVICE BFloat16
191
+ operator-(const BFloat16& a, const BFloat16& b) {
192
+ return static_cast<float>(a) - static_cast<float>(b);
193
+ }
194
+
195
+ inline C10_HOST_DEVICE BFloat16
196
+ operator*(const BFloat16& a, const BFloat16& b) {
197
+ return static_cast<float>(a) * static_cast<float>(b);
198
+ }
199
+
200
+ inline C10_HOST_DEVICE BFloat16 operator/(const BFloat16& a, const BFloat16& b)
201
+ __ubsan_ignore_float_divide_by_zero__ {
202
+ return static_cast<float>(a) / static_cast<float>(b);
203
+ }
204
+
205
+ inline C10_HOST_DEVICE BFloat16 operator-(const BFloat16& a) {
206
+ return -static_cast<float>(a);
207
+ }
208
+
209
+ inline C10_HOST_DEVICE BFloat16& operator+=(BFloat16& a, const BFloat16& b) {
210
+ a = a + b;
211
+ return a;
212
+ }
213
+
214
+ inline C10_HOST_DEVICE BFloat16& operator-=(BFloat16& a, const BFloat16& b) {
215
+ a = a - b;
216
+ return a;
217
+ }
218
+
219
+ inline C10_HOST_DEVICE BFloat16& operator*=(BFloat16& a, const BFloat16& b) {
220
+ a = a * b;
221
+ return a;
222
+ }
223
+
224
+ inline C10_HOST_DEVICE BFloat16& operator/=(BFloat16& a, const BFloat16& b) {
225
+ a = a / b;
226
+ return a;
227
+ }
228
+
229
+ inline C10_HOST_DEVICE BFloat16& operator|(BFloat16& a, const BFloat16& b) {
230
+ a.x = a.x | b.x;
231
+ return a;
232
+ }
233
+
234
+ inline C10_HOST_DEVICE BFloat16& operator^(BFloat16& a, const BFloat16& b) {
235
+ a.x = a.x ^ b.x;
236
+ return a;
237
+ }
238
+
239
+ inline C10_HOST_DEVICE BFloat16& operator&(BFloat16& a, const BFloat16& b) {
240
+ a.x = a.x & b.x;
241
+ return a;
242
+ }
243
+
244
+ /// Arithmetic with floats
245
+
246
+ inline C10_HOST_DEVICE float operator+(BFloat16 a, float b) {
247
+ return static_cast<float>(a) + b;
248
+ }
249
+ inline C10_HOST_DEVICE float operator-(BFloat16 a, float b) {
250
+ return static_cast<float>(a) - b;
251
+ }
252
+ inline C10_HOST_DEVICE float operator*(BFloat16 a, float b) {
253
+ return static_cast<float>(a) * b;
254
+ }
255
+ inline C10_HOST_DEVICE float operator/(BFloat16 a, float b) {
256
+ return static_cast<float>(a) / b;
257
+ }
258
+
259
+ inline C10_HOST_DEVICE float operator+(float a, BFloat16 b) {
260
+ return a + static_cast<float>(b);
261
+ }
262
+ inline C10_HOST_DEVICE float operator-(float a, BFloat16 b) {
263
+ return a - static_cast<float>(b);
264
+ }
265
+ inline C10_HOST_DEVICE float operator*(float a, BFloat16 b) {
266
+ return a * static_cast<float>(b);
267
+ }
268
+ inline C10_HOST_DEVICE float operator/(float a, BFloat16 b) {
269
+ return a / static_cast<float>(b);
270
+ }
271
+
272
+ inline C10_HOST_DEVICE float& operator+=(float& a, const BFloat16& b) {
273
+ return a += static_cast<float>(b);
274
+ }
275
+ inline C10_HOST_DEVICE float& operator-=(float& a, const BFloat16& b) {
276
+ return a -= static_cast<float>(b);
277
+ }
278
+ inline C10_HOST_DEVICE float& operator*=(float& a, const BFloat16& b) {
279
+ return a *= static_cast<float>(b);
280
+ }
281
+ inline C10_HOST_DEVICE float& operator/=(float& a, const BFloat16& b) {
282
+ return a /= static_cast<float>(b);
283
+ }
284
+
285
+ /// Arithmetic with doubles
286
+
287
+ inline C10_HOST_DEVICE double operator+(BFloat16 a, double b) {
288
+ return static_cast<double>(a) + b;
289
+ }
290
+ inline C10_HOST_DEVICE double operator-(BFloat16 a, double b) {
291
+ return static_cast<double>(a) - b;
292
+ }
293
+ inline C10_HOST_DEVICE double operator*(BFloat16 a, double b) {
294
+ return static_cast<double>(a) * b;
295
+ }
296
+ inline C10_HOST_DEVICE double operator/(BFloat16 a, double b) {
297
+ return static_cast<double>(a) / b;
298
+ }
299
+
300
+ inline C10_HOST_DEVICE double operator+(double a, BFloat16 b) {
301
+ return a + static_cast<double>(b);
302
+ }
303
+ inline C10_HOST_DEVICE double operator-(double a, BFloat16 b) {
304
+ return a - static_cast<double>(b);
305
+ }
306
+ inline C10_HOST_DEVICE double operator*(double a, BFloat16 b) {
307
+ return a * static_cast<double>(b);
308
+ }
309
+ inline C10_HOST_DEVICE double operator/(double a, BFloat16 b) {
310
+ return a / static_cast<double>(b);
311
+ }
312
+
313
+ /// Arithmetic with ints
314
+
315
+ inline C10_HOST_DEVICE BFloat16 operator+(BFloat16 a, int b) {
316
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
317
+ return a + static_cast<BFloat16>(b);
318
+ }
319
+ inline C10_HOST_DEVICE BFloat16 operator-(BFloat16 a, int b) {
320
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
321
+ return a - static_cast<BFloat16>(b);
322
+ }
323
+ inline C10_HOST_DEVICE BFloat16 operator*(BFloat16 a, int b) {
324
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
325
+ return a * static_cast<BFloat16>(b);
326
+ }
327
+ inline C10_HOST_DEVICE BFloat16 operator/(BFloat16 a, int b) {
328
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
329
+ return a / static_cast<BFloat16>(b);
330
+ }
331
+
332
+ inline C10_HOST_DEVICE BFloat16 operator+(int a, BFloat16 b) {
333
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
334
+ return static_cast<BFloat16>(a) + b;
335
+ }
336
+ inline C10_HOST_DEVICE BFloat16 operator-(int a, BFloat16 b) {
337
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
338
+ return static_cast<BFloat16>(a) - b;
339
+ }
340
+ inline C10_HOST_DEVICE BFloat16 operator*(int a, BFloat16 b) {
341
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
342
+ return static_cast<BFloat16>(a) * b;
343
+ }
344
+ inline C10_HOST_DEVICE BFloat16 operator/(int a, BFloat16 b) {
345
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
346
+ return static_cast<BFloat16>(a) / b;
347
+ }
348
+
349
+ //// Arithmetic with int64_t
350
+
351
+ inline C10_HOST_DEVICE BFloat16 operator+(BFloat16 a, int64_t b) {
352
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
353
+ return a + static_cast<BFloat16>(b);
354
+ }
355
+ inline C10_HOST_DEVICE BFloat16 operator-(BFloat16 a, int64_t b) {
356
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
357
+ return a - static_cast<BFloat16>(b);
358
+ }
359
+ inline C10_HOST_DEVICE BFloat16 operator*(BFloat16 a, int64_t b) {
360
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
361
+ return a * static_cast<BFloat16>(b);
362
+ }
363
+ inline C10_HOST_DEVICE BFloat16 operator/(BFloat16 a, int64_t b) {
364
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
365
+ return a / static_cast<BFloat16>(b);
366
+ }
367
+
368
+ inline C10_HOST_DEVICE BFloat16 operator+(int64_t a, BFloat16 b) {
369
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
370
+ return static_cast<BFloat16>(a) + b;
371
+ }
372
+ inline C10_HOST_DEVICE BFloat16 operator-(int64_t a, BFloat16 b) {
373
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
374
+ return static_cast<BFloat16>(a) - b;
375
+ }
376
+ inline C10_HOST_DEVICE BFloat16 operator*(int64_t a, BFloat16 b) {
377
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
378
+ return static_cast<BFloat16>(a) * b;
379
+ }
380
+ inline C10_HOST_DEVICE BFloat16 operator/(int64_t a, BFloat16 b) {
381
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
382
+ return static_cast<BFloat16>(a) / b;
383
+ }
384
+
385
+ // Overloading < and > operators, because std::max and std::min use them.
386
+
387
+ inline C10_HOST_DEVICE bool operator>(BFloat16& lhs, BFloat16& rhs) {
388
+ return float(lhs) > float(rhs);
389
+ }
390
+
391
+ inline C10_HOST_DEVICE bool operator<(BFloat16& lhs, BFloat16& rhs) {
392
+ return float(lhs) < float(rhs);
393
+ }
394
+
395
+ C10_CLANG_DIAGNOSTIC_POP()
396
+ } // namespace c10
397
+
398
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
399
+
400
+ namespace detail {
401
+ using c10::detail::bits_from_f32;
402
+ using c10::detail::f32_from_bits;
403
+ using c10::detail::round_to_nearest_even;
404
+ } // namespace detail
405
+
406
+ using c10::BFloat16;
407
+ using c10::operator+;
408
+ using c10::operator-;
409
+ using c10::operator*;
410
+ using c10::operator/;
411
+ using c10::operator+=;
412
+ using c10::operator-=;
413
+ using c10::operator*=;
414
+ using c10::operator/=;
415
+ using c10::operator<;
416
+ using c10::operator>;
417
+ using c10::operator<<;
418
+ HIDDEN_NAMESPACE_END(torch, headeronly)
419
+
420
+ namespace std {
421
+
422
+ template <>
423
+ class numeric_limits<c10::BFloat16> {
424
+ public:
425
+ static constexpr bool is_signed = true;
426
+ static constexpr bool is_specialized = true;
427
+ static constexpr bool is_integer = false;
428
+ static constexpr bool is_exact = false;
429
+ static constexpr bool has_infinity = true;
430
+ static constexpr bool has_quiet_NaN = true;
431
+ static constexpr bool has_signaling_NaN = true;
432
+ static constexpr auto has_denorm = numeric_limits<float>::has_denorm;
433
+ static constexpr auto has_denorm_loss =
434
+ numeric_limits<float>::has_denorm_loss;
435
+ static constexpr auto round_style = numeric_limits<float>::round_style;
436
+ static constexpr bool is_iec559 = false;
437
+ static constexpr bool is_bounded = true;
438
+ static constexpr bool is_modulo = false;
439
+ static constexpr int digits = 8;
440
+ static constexpr int digits10 = 2;
441
+ static constexpr int max_digits10 = 4;
442
+ static constexpr int radix = 2;
443
+ static constexpr int min_exponent = -125;
444
+ static constexpr int min_exponent10 = -37;
445
+ static constexpr int max_exponent = 128;
446
+ static constexpr int max_exponent10 = 38;
447
+ static constexpr auto traps = numeric_limits<float>::traps;
448
+ static constexpr auto tinyness_before =
449
+ numeric_limits<float>::tinyness_before;
450
+
451
+ static constexpr c10::BFloat16 min() {
452
+ return c10::BFloat16(0x0080, c10::BFloat16::from_bits());
453
+ }
454
+ static constexpr c10::BFloat16 lowest() {
455
+ return c10::BFloat16(0xFF7F, c10::BFloat16::from_bits());
456
+ }
457
+ static constexpr c10::BFloat16 max() {
458
+ return c10::BFloat16(0x7F7F, c10::BFloat16::from_bits());
459
+ }
460
+ static constexpr c10::BFloat16 epsilon() {
461
+ return c10::BFloat16(0x3C00, c10::BFloat16::from_bits());
462
+ }
463
+ static constexpr c10::BFloat16 round_error() {
464
+ return c10::BFloat16(0x3F00, c10::BFloat16::from_bits());
465
+ }
466
+ static constexpr c10::BFloat16 infinity() {
467
+ return c10::BFloat16(0x7F80, c10::BFloat16::from_bits());
468
+ }
469
+ static constexpr c10::BFloat16 quiet_NaN() {
470
+ return c10::BFloat16(0x7FC0, c10::BFloat16::from_bits());
471
+ }
472
+ static constexpr c10::BFloat16 signaling_NaN() {
473
+ return c10::BFloat16(0x7F80, c10::BFloat16::from_bits());
474
+ }
475
+ static constexpr c10::BFloat16 denorm_min() {
476
+ return c10::BFloat16(0x0001, c10::BFloat16::from_bits());
477
+ }
478
+ };
479
+
480
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Deprecated.h ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /**
4
+ * This file provides portable macros for marking declarations
5
+ * as deprecated. You should generally use C10_DEPRECATED,
6
+ * except when marking 'using' declarations as deprecated,
7
+ * in which case you should use C10_DEFINE_DEPRECATED_USING
8
+ * (due to portability concerns).
9
+ */
10
+
11
+ // Sample usage:
12
+ //
13
+ // C10_DEPRECATED void bad_func();
14
+ // struct C10_DEPRECATED BadStruct {
15
+ // ...
16
+ // };
17
+
18
+ // NB: __cplusplus doesn't work for MSVC, so for now MSVC always uses
19
+ // the "__declspec(deprecated)" implementation and not the C++14
20
+ // "[[deprecated]]" attribute. We tried enabling "[[deprecated]]" for C++14 on
21
+ // MSVC, but ran into issues with some older MSVC versions.
22
+ #if (defined(__cplusplus) && __cplusplus >= 201402L)
23
+ #define C10_DEPRECATED [[deprecated]]
24
+ #define C10_DEPRECATED_MESSAGE(message) [[deprecated(message)]]
25
+ #elif defined(__GNUC__)
26
+ #define C10_DEPRECATED __attribute__((deprecated))
27
+ // TODO Is there some way to implement this?
28
+ #define C10_DEPRECATED_MESSAGE(message) __attribute__((deprecated))
29
+
30
+ #elif defined(_MSC_VER)
31
+ #define C10_DEPRECATED __declspec(deprecated)
32
+ #define C10_DEPRECATED_MESSAGE(message) __declspec(deprecated(message))
33
+ #else
34
+ #warning "You need to implement C10_DEPRECATED for this compiler"
35
+ #define C10_DEPRECATED
36
+ #endif
37
+
38
+ // Sample usage:
39
+ //
40
+ // C10_DEFINE_DEPRECATED_USING(BadType, int)
41
+ //
42
+ // which is the portable version of
43
+ //
44
+ // using BadType [[deprecated]] = int;
45
+
46
+ // technically [[deprecated]] syntax is from c++14 standard, but it works in
47
+ // many compilers.
48
+ #if defined(__has_cpp_attribute)
49
+ #if __has_cpp_attribute(deprecated) && !defined(__CUDACC__)
50
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
51
+ using TypeName [[deprecated]] = TypeThingy;
52
+ #endif
53
+ #endif
54
+
55
+ #if defined(_MSC_VER)
56
+ #if defined(__CUDACC__)
57
+ // neither [[deprecated]] nor __declspec(deprecated) work on nvcc on Windows;
58
+ // you get the error:
59
+ //
60
+ // error: attribute does not apply to any entity
61
+ //
62
+ // So we just turn the macro off in this case.
63
+ #if defined(C10_DEFINE_DEPRECATED_USING)
64
+ #undef C10_DEFINE_DEPRECATED_USING
65
+ #endif
66
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
67
+ using TypeName = TypeThingy;
68
+ #else
69
+ // [[deprecated]] does work in windows without nvcc, though msc doesn't support
70
+ // `__has_cpp_attribute` when c++14 is supported, otherwise
71
+ // __declspec(deprecated) is used as the alternative.
72
+ #ifndef C10_DEFINE_DEPRECATED_USING
73
+ #if defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
74
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
75
+ using TypeName [[deprecated]] = TypeThingy;
76
+ #else
77
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
78
+ using TypeName = __declspec(deprecated) TypeThingy;
79
+ #endif
80
+ #endif
81
+ #endif
82
+ #endif
83
+
84
+ #if !defined(C10_DEFINE_DEPRECATED_USING) && defined(__GNUC__)
85
+ // nvcc has a bug where it doesn't understand __attribute__((deprecated))
86
+ // declarations even when the host compiler supports it. We'll only use this gcc
87
+ // attribute when not cuda, and when using a GCC compiler that doesn't support
88
+ // the c++14 syntax we checked for above (available in __GNUC__ >= 5)
89
+ #if !defined(__CUDACC__)
90
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
91
+ using TypeName __attribute__((deprecated)) = TypeThingy;
92
+ #else
93
+ // using cuda + gcc < 5, neither deprecated syntax is available so turning off.
94
+ #define C10_DEFINE_DEPRECATED_USING(TypeName, TypeThingy) \
95
+ using TypeName = TypeThingy;
96
+ #endif
97
+ #endif
98
+
99
+ #if !defined(C10_DEFINE_DEPRECATED_USING)
100
+ #warning "You need to implement C10_DEFINE_DEPRECATED_USING for this compiler"
101
+ #define C10_DEFINE_DEPRECATED_USING
102
+ #endif
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Exception.h ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Export.h>
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ #include <sstream>
7
+ #include <string>
8
+
9
+ namespace c10 {
10
+ // On nvcc, C10_UNLIKELY thwarts missing return statement analysis. In cases
11
+ // where the unlikely expression may be a constant, use this macro to ensure
12
+ // return statement analysis keeps working (at the cost of not getting the
13
+ // likely/unlikely annotation on nvcc).
14
+ // https://github.com/pytorch/pytorch/issues/21418
15
+ //
16
+ // Currently, this is only used in the error reporting macros below. If you
17
+ // want to use it more generally, move me to Macros.h
18
+ //
19
+ // TODO: Brian Vaughan observed that we might be able to get this to work on
20
+ // nvcc by writing some sort of C++ overload that distinguishes constexpr inputs
21
+ // from non-constexpr. Since there isn't any evidence that losing C10_UNLIKELY
22
+ // in nvcc is causing us perf problems, this is not yet implemented, but this
23
+ // might be an interesting piece of C++ code for an intrepid bootcamper to
24
+ // write.
25
+ #if defined(__CUDACC__)
26
+ #define C10_UNLIKELY_OR_CONST(e) e
27
+ #else
28
+ #define C10_UNLIKELY_OR_CONST(e) C10_UNLIKELY(e)
29
+ #endif
30
+
31
+ } // namespace c10
32
+
33
+ // STD_TORCH_CHECK throws std::runtime_error instead of c10::Error which is
34
+ // useful when certain headers are used in a libtorch-independent way,
35
+ // e.g. when Vectorized<T> is used in AOTInductor generated code, or
36
+ // for custom ops to have an ABI stable dependency on libtorch.
37
+ #ifdef STRIP_ERROR_MESSAGES
38
+ #define STD_TORCH_CHECK_MSG(cond, type, ...) \
39
+ (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__))
40
+ #else // so STRIP_ERROR_MESSAGES is not defined
41
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, detail)
42
+ template <typename... Args>
43
+ std::string stdTorchCheckMsgImpl(const char* /*msg*/, const Args&... args) {
44
+ // This is similar to the one in c10/util/Exception.h, but does
45
+ // not depend on the more complex c10::str() function. ostringstream
46
+ // supports fewer data types than c10::str(), but should be sufficient
47
+ // in the headeronly world.
48
+ std::ostringstream oss;
49
+ ((oss << args), ...);
50
+ return oss.str();
51
+ }
52
+
53
+ inline const char* stdTorchCheckMsgImpl(const char* msg) {
54
+ return msg;
55
+ }
56
+ // If there is just 1 user-provided C-string argument, use it.
57
+ inline const char* stdTorchCheckMsgImpl(const char* /*msg*/, const char* args) {
58
+ return args;
59
+ }
60
+ HIDDEN_NAMESPACE_END(torch, headeronly, detail)
61
+
62
+ #define STD_TORCH_CHECK_MSG(cond, type, ...) \
63
+ (torch::headeronly::detail::stdTorchCheckMsgImpl( \
64
+ "Expected " #cond \
65
+ " to be true, but got false. " \
66
+ "(Could this error message be improved? If so, " \
67
+ "please report an enhancement request to PyTorch.)", \
68
+ ##__VA_ARGS__))
69
+ #endif // STRIP_ERROR_MESSAGES
70
+
71
+ #define STD_TORCH_CHECK(cond, ...) \
72
+ if (C10_UNLIKELY_OR_CONST(!(cond))) { \
73
+ throw std::runtime_error(STD_TORCH_CHECK_MSG( \
74
+ cond, \
75
+ "", \
76
+ __func__, \
77
+ ", ", \
78
+ __FILE__, \
79
+ ":", \
80
+ __LINE__, \
81
+ ", ", \
82
+ ##__VA_ARGS__)); \
83
+ }
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float4_e2m1fn_x2.h ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ /// Defines the Float4_e2m1fn_x2 type (4-bit floating-point, two elements packed
7
+ /// into one byte). This is the FP4 dtype from the OCP MX format spec
8
+ /// (https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf,
9
+ /// Section 5.3.3)
10
+ ///
11
+ /// Given two high precision values val0 and val1, here is the
12
+ /// binary configuration of their packed representation, from MSB to LSB:
13
+ ///
14
+ /// original value | val1 : val0
15
+ /// ========================================
16
+ /// bit index (MSB==7, LSB==0) | 7654 : 3210
17
+ /// sign/exponent/mantissa | seem : seem
18
+ ///
19
+
20
+ namespace c10 {
21
+
22
+ struct alignas(1) Float4_e2m1fn_x2 {
23
+ uint8_t val_;
24
+ Float4_e2m1fn_x2() = default;
25
+ C10_HOST_DEVICE explicit Float4_e2m1fn_x2(uint8_t val) : val_(val) {}
26
+ };
27
+
28
+ /// Comparison operators
29
+ inline C10_HOST_DEVICE bool operator==(
30
+ const Float4_e2m1fn_x2& a,
31
+ const Float4_e2m1fn_x2& b) {
32
+ return a.val_ == b.val_;
33
+ }
34
+
35
+ inline C10_HOST_DEVICE bool operator!=(
36
+ const Float4_e2m1fn_x2& a,
37
+ const Float4_e2m1fn_x2& b) {
38
+ return a.val_ != b.val_;
39
+ }
40
+
41
+ } // namespace c10
42
+
43
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
44
+ using c10::Float4_e2m1fn_x2;
45
+ using c10::operator==;
46
+ using c10::operator!=;
47
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e4m3fn.h ADDED
@@ -0,0 +1,531 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Float8_e4m3fn type (8-bit floating-point) including conversions
4
+ /// to standard C types and basic arithmetic operations. Note that arithmetic
5
+ /// operations are implemented by converting to floating point and
6
+ /// performing the operation in float32.
7
+ /// Binary configuration:
8
+ /// s eeee mmm
9
+ /// 1 sign bit
10
+ /// 4 exponent bits
11
+ /// 3 mantissa bits
12
+ /// bias = 7
13
+ ///
14
+ /// Implementation based on the paper https://arxiv.org/pdf/2209.05433.pdf
15
+ /// and inspired by Half implementation from pytorch/c10/util/Half.h
16
+
17
+ #include <torch/headeronly/macros/Macros.h>
18
+ #include <torch/headeronly/util/floating_point_utils.h>
19
+
20
+ #if defined(__cplusplus)
21
+ #include <cmath>
22
+ #include <cstdint>
23
+ #elif !defined(__OPENCL_VERSION__)
24
+ #include <math.h>
25
+ #include <stdint.h>
26
+ #endif
27
+
28
+ #ifdef _MSC_VER
29
+ #include <intrin.h>
30
+ #endif
31
+
32
+ #include <climits>
33
+ #include <iostream>
34
+
35
+ namespace c10 {
36
+
37
+ struct alignas(1) Float8_e4m3fn {
38
+ uint8_t x;
39
+
40
+ struct from_bits_t {};
41
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
42
+ return from_bits_t();
43
+ }
44
+
45
+ Float8_e4m3fn() = default;
46
+
47
+ constexpr C10_HOST_DEVICE Float8_e4m3fn(uint8_t bits, from_bits_t /*unused*/)
48
+ : x(bits) {}
49
+ inline C10_HOST_DEVICE Float8_e4m3fn(float value);
50
+ inline C10_HOST_DEVICE operator float() const;
51
+ inline C10_HOST_DEVICE bool isnan() const;
52
+ };
53
+
54
+ inline std::ostream& operator<<(std::ostream& out, const Float8_e4m3fn& value) {
55
+ out << (float)value;
56
+ return out;
57
+ }
58
+
59
+ namespace detail {
60
+
61
+ /*
62
+ * Convert a 8-bit floating-point number in fp8 E4M3FN format, in bit
63
+ * representation, to a 32-bit floating-point number in IEEE single-precision
64
+ * format, in bit representation.
65
+ *
66
+ * @note The implementation doesn't use any floating-point operations.
67
+ */
68
+ inline C10_HOST_DEVICE float fp8e4m3fn_to_fp32_value(uint8_t input) {
69
+ /*
70
+ * Extend the fp8 E4M3FN number to 32 bits and shift to the
71
+ * upper part of the 32-bit word:
72
+ * +---+----+---+-----------------------------+
73
+ * | S |EEEE|MMM|0000 0000 0000 0000 0000 0000|
74
+ * +---+----+---+-----------------------------+
75
+ * Bits 31 27-30 24-26 0-23
76
+ *
77
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
78
+ * - zero bits.
79
+ */
80
+ const uint32_t w = (uint32_t)input << 24;
81
+ /*
82
+ * Extract the sign of the input number into the high bit of the 32-bit word:
83
+ *
84
+ * +---+----------------------------------+
85
+ * | S |0000000 00000000 00000000 00000000|
86
+ * +---+----------------------------------+
87
+ * Bits 31 0-31
88
+ */
89
+ const uint32_t sign = w & UINT32_C(0x80000000);
90
+ /*
91
+ * Extract mantissa and biased exponent of the input number into the bits 0-30
92
+ * of the 32-bit word:
93
+ *
94
+ * +---+----+---+-----------------------------+
95
+ * | S |EEEE|MMM|0000 0000 0000 0000 0000 0000|
96
+ * +---+----+---+-----------------------------+
97
+ * Bits 31 27-30 24-26 0-23
98
+ */
99
+ const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
100
+ /*
101
+ * Renorm shift is the number of bits to shift mantissa left to make the
102
+ * half-precision number normalized. If the initial number is normalized, some
103
+ * of its high 5 bits (sign == 0 and 4-bit exponent) equals one. In this case
104
+ * renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note
105
+ * that if we shift denormalized nonsign by renorm_shift, the unit bit of
106
+ * mantissa will shift into exponent, turning the biased exponent into 1, and
107
+ * making mantissa normalized (i.e. without leading 1).
108
+ */
109
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
110
+ uint32_t renorm_shift = __clz(nonsign);
111
+ #elif defined(__SYCL_DEVICE_ONLY__)
112
+ // Note: zero is not a supported input into `__builtin_clz`
113
+ uint32_t renorm_shift =
114
+ nonsign != 0 ? __builtin_clz(nonsign) : sizeof(uint32_t) * CHAR_BIT;
115
+ #elif defined(_MSC_VER) && !defined(__clang__)
116
+ unsigned long nonsign_bsr;
117
+ _BitScanReverse(&nonsign_bsr, (unsigned long)nonsign);
118
+ uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
119
+ #else
120
+ // Note: zero is not a supported input into `__builtin_clz`
121
+ uint32_t renorm_shift =
122
+ nonsign != 0 ? __builtin_clz(nonsign) : sizeof(uint32_t) * CHAR_BIT;
123
+ #endif
124
+ renorm_shift = renorm_shift > 4 ? renorm_shift - 4 : 0;
125
+ /*
126
+ * Iff fp8e4m3fn number has all exponent and mantissa bits set to 1,
127
+ * the addition overflows it into bit 31, and the subsequent shift turns the
128
+ * high 9 bits into 1. Thus inf_nan_mask == 0x7F800000 if the fp8e4m3fn number
129
+ * is Nan, 0x00000000 otherwise
130
+ */
131
+ const int32_t inf_nan_mask =
132
+ ((int32_t)(nonsign + 0x01000000) >> 8) & INT32_C(0x7F800000);
133
+ /*
134
+ * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31
135
+ * into 1. Otherwise, bit 31 remains 0. The signed shift right by 31
136
+ * broadcasts bit 31 into all bits of the zero_mask. Thus zero_mask ==
137
+ * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
138
+ * 0x00000000 otherwise
139
+ */
140
+ const int32_t zero_mask = (int32_t)(nonsign - 1) >> 31;
141
+ /*
142
+ * 1. Shift nonsign left by renorm_shift to normalize it (if the input
143
+ * was denormal)
144
+ * 2. Shift nonsign right by 4 so the exponent (4 bits originally)
145
+ * becomes an 8-bit field and 3-bit mantissa shifts into the 3 high
146
+ * bits of the 23-bit mantissa of IEEE single-precision number.
147
+ * 3. Add 0x78 to the exponent (starting at bit 23) to compensate the
148
+ * different in exponent bias (0x7F for single-precision number less 0x07
149
+ * for fp8e4m3fn number).
150
+ * 4. Subtract renorm_shift from the exponent (starting at bit 23) to
151
+ * account for renormalization. As renorm_shift is less than 0x78, this
152
+ * can be combined with step 3.
153
+ * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the
154
+ * input was NaN or infinity.
155
+ * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent
156
+ * into zero if the input was zero.
157
+ * 7. Combine with the sign of the input number.
158
+ */
159
+ uint32_t result = sign |
160
+ ((((nonsign << renorm_shift >> 4) + ((0x78 - renorm_shift) << 23)) |
161
+ inf_nan_mask) &
162
+ ~zero_mask);
163
+ return fp32_from_bits(result);
164
+ }
165
+
166
+ /*
167
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
168
+ * 8-bit floating-point number in fp8 E4M3FN format, in bit representation.
169
+ */
170
+ inline C10_HOST_DEVICE uint8_t fp8e4m3fn_from_fp32_value(float f) {
171
+ /*
172
+ * Binary representation of 480.0f, which is the first value
173
+ * not representable in fp8e4m3fn range:
174
+ * 0 1111 111 - fp8e4m3fn
175
+ * 0 10000111 11100000000000000000000 - fp32
176
+ */
177
+ constexpr uint32_t fp8_max = UINT32_C(1087) << 20;
178
+
179
+ /*
180
+ * A mask for converting fp32 numbers lower than fp8e4m3fn normal range
181
+ * into denorm representation
182
+ * magic number: ((127 - 7) + (23 - 3) + 1)
183
+ */
184
+ constexpr uint32_t denorm_mask = UINT32_C(141) << 23;
185
+
186
+ uint32_t f_bits = fp32_to_bits(f);
187
+
188
+ uint8_t result = 0u;
189
+
190
+ /*
191
+ * Extract the sign of the input number into the high bit of the 32-bit word:
192
+ *
193
+ * +---+----------------------------------+
194
+ * | S |0000000 00000000 00000000 00000000|
195
+ * +---+----------------------------------+
196
+ * Bits 31 0-31
197
+ */
198
+ const uint32_t sign = f_bits & UINT32_C(0x80000000);
199
+
200
+ /*
201
+ * Set sign bit to 0
202
+ */
203
+ f_bits ^= sign;
204
+
205
+ if (f_bits >= fp8_max) {
206
+ // NaN - all exponent and mantissa bits set to 1
207
+ result = 0x7f;
208
+ } else {
209
+ if (f_bits < (UINT32_C(121) << 23)) {
210
+ // Input number is smaller than 2^(-6), which is the smallest
211
+ // fp8e4m3fn normal number
212
+ f_bits =
213
+ fp32_to_bits(fp32_from_bits(f_bits) + fp32_from_bits(denorm_mask));
214
+ result = static_cast<uint8_t>(f_bits - denorm_mask);
215
+ } else {
216
+ // resulting mantissa is odd
217
+ uint8_t mant_odd = (f_bits >> 20) & 1;
218
+
219
+ // update exponent, rounding bias part 1
220
+ f_bits += ((uint32_t)(7 - 127) << 23) + 0x7FFFF;
221
+
222
+ // rounding bias part 2
223
+ f_bits += mant_odd;
224
+
225
+ // take the bits!
226
+ result = static_cast<uint8_t>(f_bits >> 20);
227
+ }
228
+ }
229
+
230
+ result |= static_cast<uint8_t>(sign >> 24);
231
+ return result;
232
+ }
233
+
234
+ } // namespace detail
235
+
236
+ // -------- below is copied from c10/util/Float8_e4m3fn-inl.h --------//
237
+ C10_CLANG_DIAGNOSTIC_PUSH()
238
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
239
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
240
+ #endif
241
+
242
+ /// Constructors
243
+
244
+ inline C10_HOST_DEVICE Float8_e4m3fn::Float8_e4m3fn(float value)
245
+ : x(detail::fp8e4m3fn_from_fp32_value(value)) {}
246
+
247
+ /// Implicit conversions
248
+
249
+ inline C10_HOST_DEVICE Float8_e4m3fn::operator float() const {
250
+ return detail::fp8e4m3fn_to_fp32_value(x);
251
+ }
252
+
253
+ /// Special values helper
254
+
255
+ inline C10_HOST_DEVICE bool Float8_e4m3fn::isnan() const {
256
+ return (x & 0b01111111) == 0b01111111;
257
+ }
258
+
259
+ /// Arithmetic
260
+
261
+ inline C10_HOST_DEVICE Float8_e4m3fn
262
+ operator+(const Float8_e4m3fn& a, const Float8_e4m3fn& b) {
263
+ return static_cast<float>(a) + static_cast<float>(b);
264
+ }
265
+
266
+ inline C10_HOST_DEVICE Float8_e4m3fn
267
+ operator-(const Float8_e4m3fn& a, const Float8_e4m3fn& b) {
268
+ return static_cast<float>(a) - static_cast<float>(b);
269
+ }
270
+
271
+ inline C10_HOST_DEVICE Float8_e4m3fn
272
+ operator*(const Float8_e4m3fn& a, const Float8_e4m3fn& b) {
273
+ return static_cast<float>(a) * static_cast<float>(b);
274
+ }
275
+
276
+ inline C10_HOST_DEVICE Float8_e4m3fn operator/(
277
+ const Float8_e4m3fn& a,
278
+ const Float8_e4m3fn& b) __ubsan_ignore_float_divide_by_zero__ {
279
+ return static_cast<float>(a) / static_cast<float>(b);
280
+ }
281
+
282
+ inline C10_HOST_DEVICE Float8_e4m3fn operator-(const Float8_e4m3fn& a) {
283
+ return -static_cast<float>(a);
284
+ }
285
+
286
+ inline C10_HOST_DEVICE Float8_e4m3fn& operator+=(
287
+ Float8_e4m3fn& a,
288
+ const Float8_e4m3fn& b) {
289
+ a = a + b;
290
+ return a;
291
+ }
292
+
293
+ inline C10_HOST_DEVICE Float8_e4m3fn& operator-=(
294
+ Float8_e4m3fn& a,
295
+ const Float8_e4m3fn& b) {
296
+ a = a - b;
297
+ return a;
298
+ }
299
+
300
+ inline C10_HOST_DEVICE Float8_e4m3fn& operator*=(
301
+ Float8_e4m3fn& a,
302
+ const Float8_e4m3fn& b) {
303
+ a = a * b;
304
+ return a;
305
+ }
306
+
307
+ inline C10_HOST_DEVICE Float8_e4m3fn& operator/=(
308
+ Float8_e4m3fn& a,
309
+ const Float8_e4m3fn& b) {
310
+ a = a / b;
311
+ return a;
312
+ }
313
+
314
+ /// Arithmetic with floats
315
+
316
+ inline C10_HOST_DEVICE float operator+(Float8_e4m3fn a, float b) {
317
+ return static_cast<float>(a) + b;
318
+ }
319
+ inline C10_HOST_DEVICE float operator-(Float8_e4m3fn a, float b) {
320
+ return static_cast<float>(a) - b;
321
+ }
322
+ inline C10_HOST_DEVICE float operator*(Float8_e4m3fn a, float b) {
323
+ return static_cast<float>(a) * b;
324
+ }
325
+ inline C10_HOST_DEVICE float operator/(Float8_e4m3fn a, float b)
326
+ __ubsan_ignore_float_divide_by_zero__ {
327
+ return static_cast<float>(a) / b;
328
+ }
329
+
330
+ inline C10_HOST_DEVICE float operator+(float a, Float8_e4m3fn b) {
331
+ return a + static_cast<float>(b);
332
+ }
333
+ inline C10_HOST_DEVICE float operator-(float a, Float8_e4m3fn b) {
334
+ return a - static_cast<float>(b);
335
+ }
336
+ inline C10_HOST_DEVICE float operator*(float a, Float8_e4m3fn b) {
337
+ return a * static_cast<float>(b);
338
+ }
339
+ inline C10_HOST_DEVICE float operator/(float a, Float8_e4m3fn b)
340
+ __ubsan_ignore_float_divide_by_zero__ {
341
+ return a / static_cast<float>(b);
342
+ }
343
+
344
+ inline C10_HOST_DEVICE float& operator+=(float& a, const Float8_e4m3fn& b) {
345
+ return a += static_cast<float>(b);
346
+ }
347
+ inline C10_HOST_DEVICE float& operator-=(float& a, const Float8_e4m3fn& b) {
348
+ return a -= static_cast<float>(b);
349
+ }
350
+ inline C10_HOST_DEVICE float& operator*=(float& a, const Float8_e4m3fn& b) {
351
+ return a *= static_cast<float>(b);
352
+ }
353
+ inline C10_HOST_DEVICE float& operator/=(float& a, const Float8_e4m3fn& b) {
354
+ return a /= static_cast<float>(b);
355
+ }
356
+
357
+ /// Arithmetic with doubles
358
+
359
+ inline C10_HOST_DEVICE double operator+(Float8_e4m3fn a, double b) {
360
+ return static_cast<double>(a) + b;
361
+ }
362
+ inline C10_HOST_DEVICE double operator-(Float8_e4m3fn a, double b) {
363
+ return static_cast<double>(a) - b;
364
+ }
365
+ inline C10_HOST_DEVICE double operator*(Float8_e4m3fn a, double b) {
366
+ return static_cast<double>(a) * b;
367
+ }
368
+ inline C10_HOST_DEVICE double operator/(Float8_e4m3fn a, double b)
369
+ __ubsan_ignore_float_divide_by_zero__ {
370
+ return static_cast<double>(a) / b;
371
+ }
372
+
373
+ inline C10_HOST_DEVICE double operator+(double a, Float8_e4m3fn b) {
374
+ return a + static_cast<double>(b);
375
+ }
376
+ inline C10_HOST_DEVICE double operator-(double a, Float8_e4m3fn b) {
377
+ return a - static_cast<double>(b);
378
+ }
379
+ inline C10_HOST_DEVICE double operator*(double a, Float8_e4m3fn b) {
380
+ return a * static_cast<double>(b);
381
+ }
382
+ inline C10_HOST_DEVICE double operator/(double a, Float8_e4m3fn b)
383
+ __ubsan_ignore_float_divide_by_zero__ {
384
+ return a / static_cast<double>(b);
385
+ }
386
+
387
+ /// Arithmetic with ints
388
+
389
+ inline C10_HOST_DEVICE Float8_e4m3fn operator+(Float8_e4m3fn a, int b) {
390
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
391
+ return a + static_cast<Float8_e4m3fn>(b);
392
+ }
393
+ inline C10_HOST_DEVICE Float8_e4m3fn operator-(Float8_e4m3fn a, int b) {
394
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
395
+ return a - static_cast<Float8_e4m3fn>(b);
396
+ }
397
+ inline C10_HOST_DEVICE Float8_e4m3fn operator*(Float8_e4m3fn a, int b) {
398
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
399
+ return a * static_cast<Float8_e4m3fn>(b);
400
+ }
401
+ inline C10_HOST_DEVICE Float8_e4m3fn operator/(Float8_e4m3fn a, int b) {
402
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
403
+ return a / static_cast<Float8_e4m3fn>(b);
404
+ }
405
+
406
+ inline C10_HOST_DEVICE Float8_e4m3fn operator+(int a, Float8_e4m3fn b) {
407
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
408
+ return static_cast<Float8_e4m3fn>(a) + b;
409
+ }
410
+ inline C10_HOST_DEVICE Float8_e4m3fn operator-(int a, Float8_e4m3fn b) {
411
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
412
+ return static_cast<Float8_e4m3fn>(a) - b;
413
+ }
414
+ inline C10_HOST_DEVICE Float8_e4m3fn operator*(int a, Float8_e4m3fn b) {
415
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
416
+ return static_cast<Float8_e4m3fn>(a) * b;
417
+ }
418
+ inline C10_HOST_DEVICE Float8_e4m3fn operator/(int a, Float8_e4m3fn b) {
419
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
420
+ return static_cast<Float8_e4m3fn>(a) / b;
421
+ }
422
+
423
+ //// Arithmetic with int64_t
424
+
425
+ inline C10_HOST_DEVICE Float8_e4m3fn operator+(Float8_e4m3fn a, int64_t b) {
426
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
427
+ return a + static_cast<Float8_e4m3fn>(b);
428
+ }
429
+ inline C10_HOST_DEVICE Float8_e4m3fn operator-(Float8_e4m3fn a, int64_t b) {
430
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
431
+ return a - static_cast<Float8_e4m3fn>(b);
432
+ }
433
+ inline C10_HOST_DEVICE Float8_e4m3fn operator*(Float8_e4m3fn a, int64_t b) {
434
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
435
+ return a * static_cast<Float8_e4m3fn>(b);
436
+ }
437
+ inline C10_HOST_DEVICE Float8_e4m3fn operator/(Float8_e4m3fn a, int64_t b) {
438
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
439
+ return a / static_cast<Float8_e4m3fn>(b);
440
+ }
441
+
442
+ inline C10_HOST_DEVICE Float8_e4m3fn operator+(int64_t a, Float8_e4m3fn b) {
443
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
444
+ return static_cast<Float8_e4m3fn>(a) + b;
445
+ }
446
+ inline C10_HOST_DEVICE Float8_e4m3fn operator-(int64_t a, Float8_e4m3fn b) {
447
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
448
+ return static_cast<Float8_e4m3fn>(a) - b;
449
+ }
450
+ inline C10_HOST_DEVICE Float8_e4m3fn operator*(int64_t a, Float8_e4m3fn b) {
451
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
452
+ return static_cast<Float8_e4m3fn>(a) * b;
453
+ }
454
+ inline C10_HOST_DEVICE Float8_e4m3fn operator/(int64_t a, Float8_e4m3fn b) {
455
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
456
+ return static_cast<Float8_e4m3fn>(a) / b;
457
+ }
458
+
459
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
460
+ /// conversion from c10::Float8_e4m3fn to float.
461
+
462
+ C10_CLANG_DIAGNOSTIC_POP()
463
+
464
+ } // namespace c10
465
+
466
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
467
+ using c10::Float8_e4m3fn;
468
+ using c10::operator<<;
469
+ using c10::operator+;
470
+ using c10::operator-;
471
+ using c10::operator*;
472
+ using c10::operator/;
473
+ using c10::operator+=;
474
+ using c10::operator-=;
475
+ using c10::operator*=;
476
+ using c10::operator/=;
477
+ HIDDEN_NAMESPACE_END(torch, headeronly)
478
+
479
+ namespace std {
480
+
481
+ template <>
482
+ class numeric_limits<c10::Float8_e4m3fn> {
483
+ public:
484
+ static constexpr bool is_specialized = true;
485
+ static constexpr bool is_signed = true;
486
+ static constexpr bool is_integer = false;
487
+ static constexpr bool is_exact = false;
488
+ static constexpr bool has_infinity = false;
489
+ static constexpr bool has_quiet_NaN = true;
490
+ static constexpr bool has_signaling_NaN = false;
491
+ static constexpr auto has_denorm = true;
492
+ static constexpr auto has_denorm_loss = true;
493
+ static constexpr auto round_style = numeric_limits<float>::round_style;
494
+ static constexpr bool is_iec559 = false;
495
+ static constexpr bool is_bounded = true;
496
+ static constexpr bool is_modulo = false;
497
+ static constexpr int digits = 4;
498
+ static constexpr int digits10 = 0;
499
+ static constexpr int max_digits10 = 3;
500
+ static constexpr int radix = 2;
501
+ static constexpr int min_exponent = -5;
502
+ static constexpr int min_exponent10 = -1;
503
+ static constexpr int max_exponent = 8;
504
+ static constexpr int max_exponent10 = 2;
505
+ static constexpr auto traps = numeric_limits<float>::traps;
506
+ static constexpr auto tinyness_before = false;
507
+
508
+ static constexpr c10::Float8_e4m3fn min() {
509
+ return c10::Float8_e4m3fn(0x08, c10::Float8_e4m3fn::from_bits());
510
+ }
511
+ static constexpr c10::Float8_e4m3fn lowest() {
512
+ return c10::Float8_e4m3fn(0xFE, c10::Float8_e4m3fn::from_bits());
513
+ }
514
+ static constexpr c10::Float8_e4m3fn max() {
515
+ return c10::Float8_e4m3fn(0x7E, c10::Float8_e4m3fn::from_bits());
516
+ }
517
+ static constexpr c10::Float8_e4m3fn epsilon() {
518
+ return c10::Float8_e4m3fn(0x20, c10::Float8_e4m3fn::from_bits());
519
+ }
520
+ static constexpr c10::Float8_e4m3fn round_error() {
521
+ return c10::Float8_e4m3fn(0x30, c10::Float8_e4m3fn::from_bits());
522
+ }
523
+ static constexpr c10::Float8_e4m3fn quiet_NaN() {
524
+ return c10::Float8_e4m3fn(0x7F, c10::Float8_e4m3fn::from_bits());
525
+ }
526
+ static constexpr c10::Float8_e4m3fn denorm_min() {
527
+ return c10::Float8_e4m3fn(0x01, c10::Float8_e4m3fn::from_bits());
528
+ }
529
+ };
530
+
531
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e4m3fnuz.h ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Float8_e4m3fnuz type (8-bit floating-point) including
4
+ /// conversions to standard C types and basic arithmetic operations. Note that
5
+ /// arithmetic operations are implemented by converting to floating point and
6
+ /// performing the operation in float32.
7
+ /// Binary configuration remains the same as Float8_e4m3fn:
8
+ /// s eeee mmm
9
+ /// 1 sign bit
10
+ /// 4 exponent bits
11
+ /// 3 mantissa bits
12
+ /// The key differences versus Float8_e4m3fn are:
13
+ /// bias = 8
14
+ /// no infinities or negative zero
15
+ /// NaN only when sign bit is 1, rest all 0s
16
+ ///
17
+ /// Implementation based on the paper https://arxiv.org/pdf/2206.02915.pdf and
18
+ /// the existing Float8_e4m3fn implementation.
19
+
20
+ #include <torch/headeronly/macros/Macros.h>
21
+ #include <torch/headeronly/util/Float8_fnuz_cvt.h>
22
+ #include <torch/headeronly/util/floating_point_utils.h>
23
+
24
+ #include <limits>
25
+
26
+ #if defined(__cplusplus)
27
+ #include <cstdint>
28
+ #elif !defined(__OPENCL_VERSION__)
29
+ #include <math.h>
30
+ #include <stdint.h>
31
+ #endif
32
+
33
+ #include <iosfwd>
34
+ #include <ostream>
35
+
36
+ namespace c10 {
37
+
38
+ struct alignas(1) Float8_e4m3fnuz {
39
+ uint8_t x;
40
+
41
+ struct from_bits_t {};
42
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
43
+ return from_bits_t();
44
+ }
45
+
46
+ Float8_e4m3fnuz() = default;
47
+
48
+ constexpr C10_HOST_DEVICE Float8_e4m3fnuz(
49
+ uint8_t bits,
50
+ from_bits_t /*unused*/)
51
+ : x(bits) {}
52
+ inline C10_HOST_DEVICE Float8_e4m3fnuz(float value);
53
+ inline C10_HOST_DEVICE operator float() const;
54
+ inline C10_HOST_DEVICE bool isnan() const;
55
+ };
56
+
57
+ inline std::ostream& operator<<(
58
+ std::ostream& out,
59
+ const Float8_e4m3fnuz& value) {
60
+ out << (float)value;
61
+ return out;
62
+ }
63
+
64
+ namespace detail {
65
+
66
+ /*
67
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
68
+ * 8-bit floating-point number in fp8 E4M3FNUZ format, in bit representation.
69
+ */
70
+ inline C10_HOST_DEVICE uint8_t fp8e4m3fnuz_from_fp32_value(float f) {
71
+ /*
72
+ * Binary representation of 256.0f, which is the first value not representable
73
+ * (i.e. the first value which would overflow in to the sign bit, resulting in
74
+ * a NaN) in fp8e4m3fnuz range:
75
+ * 1 0000 000 - fp8e4m3fnuz
76
+ * 0 10000111 00000000000000000000000 - fp32
77
+ */
78
+ constexpr uint32_t fnuz_max = UINT32_C(0x87) << 23;
79
+
80
+ /*
81
+ * A mask for converting fp32 numbers lower than fp8e4m3fnuz normal range
82
+ * into denorm representation
83
+ * magic number: ((127 - 8) + (23 - 3) + 1)
84
+ */
85
+ constexpr uint32_t denorm_mask = UINT32_C(0x8C) << 23;
86
+
87
+ uint32_t f_bits = fp32_to_bits(f);
88
+
89
+ uint32_t result = 0u;
90
+
91
+ /*
92
+ * Extract the sign of the input number into the high bit of the 32-bit word:
93
+ *
94
+ * +---+----------------------------------+
95
+ * | S |0000000 00000000 00000000 00000000|
96
+ * +---+----------------------------------+
97
+ * Bits 31 0-31
98
+ */
99
+ const uint32_t sign = f_bits & UINT32_C(0x80000000);
100
+
101
+ /*
102
+ * Set sign bit to 0
103
+ */
104
+ f_bits ^= sign;
105
+
106
+ if (f_bits >= fnuz_max) {
107
+ // NaN -- sign bit set to 1, rest 0s.
108
+ return 0x80;
109
+ }
110
+
111
+ if (f_bits < (UINT32_C(0x78) << 23) /* 2^-7 in float32 */) {
112
+ // Input exponent is less than -7, the smallest e4m3fnuz exponent, so the
113
+ // number will become subnormal.
114
+ f_bits = fp32_to_bits(fp32_from_bits(f_bits) + fp32_from_bits(denorm_mask));
115
+ result = static_cast<uint8_t>(f_bits - denorm_mask);
116
+ if (result == 0) {
117
+ // fnuz types don't have negative zero.
118
+ return 0;
119
+ }
120
+ } else {
121
+ // resulting mantissa is odd
122
+ uint8_t mant_odd = (f_bits >> 20) & 1;
123
+
124
+ // update exponent, rounding bias part 1
125
+ f_bits += ((uint32_t)(8 - 127) << 23) + 0x7FFFF;
126
+
127
+ // rounding bias part 2
128
+ f_bits += mant_odd;
129
+
130
+ // take the bits!
131
+ result = static_cast<uint8_t>(f_bits >> 20);
132
+ }
133
+
134
+ result |= sign >> 24;
135
+ return result;
136
+ }
137
+
138
+ } // namespace detail
139
+
140
+ //------ below is copied from c10/util/Float8_e4m3fnuz-inl.h ------//
141
+ C10_CLANG_DIAGNOSTIC_PUSH()
142
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
143
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
144
+ #endif
145
+
146
+ /// Constructors
147
+
148
+ inline C10_HOST_DEVICE Float8_e4m3fnuz::Float8_e4m3fnuz(float value)
149
+ : x(detail::fp8e4m3fnuz_from_fp32_value(value)) {}
150
+
151
+ /// Implicit conversions
152
+
153
+ inline C10_HOST_DEVICE Float8_e4m3fnuz::operator float() const {
154
+ return torch::headeronly::detail::fp8_fnuz_to_fp32_value<4, 3>(x);
155
+ }
156
+
157
+ /// Special values helper
158
+
159
+ inline C10_HOST_DEVICE bool Float8_e4m3fnuz::isnan() const {
160
+ return x == 0b10000000;
161
+ }
162
+
163
+ /// Arithmetic
164
+
165
+ inline C10_HOST_DEVICE Float8_e4m3fnuz
166
+ operator+(const Float8_e4m3fnuz& a, const Float8_e4m3fnuz& b) {
167
+ return static_cast<float>(a) + static_cast<float>(b);
168
+ }
169
+
170
+ inline C10_HOST_DEVICE Float8_e4m3fnuz
171
+ operator-(const Float8_e4m3fnuz& a, const Float8_e4m3fnuz& b) {
172
+ return static_cast<float>(a) - static_cast<float>(b);
173
+ }
174
+
175
+ inline C10_HOST_DEVICE Float8_e4m3fnuz
176
+ operator*(const Float8_e4m3fnuz& a, const Float8_e4m3fnuz& b) {
177
+ return static_cast<float>(a) * static_cast<float>(b);
178
+ }
179
+
180
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator/(
181
+ const Float8_e4m3fnuz& a,
182
+ const Float8_e4m3fnuz& b) __ubsan_ignore_float_divide_by_zero__ {
183
+ return static_cast<float>(a) / static_cast<float>(b);
184
+ }
185
+
186
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator-(const Float8_e4m3fnuz& a) {
187
+ return -static_cast<float>(a);
188
+ }
189
+
190
+ inline C10_HOST_DEVICE Float8_e4m3fnuz& operator+=(
191
+ Float8_e4m3fnuz& a,
192
+ const Float8_e4m3fnuz& b) {
193
+ a = a + b;
194
+ return a;
195
+ }
196
+
197
+ inline C10_HOST_DEVICE Float8_e4m3fnuz& operator-=(
198
+ Float8_e4m3fnuz& a,
199
+ const Float8_e4m3fnuz& b) {
200
+ a = a - b;
201
+ return a;
202
+ }
203
+
204
+ inline C10_HOST_DEVICE Float8_e4m3fnuz& operator*=(
205
+ Float8_e4m3fnuz& a,
206
+ const Float8_e4m3fnuz& b) {
207
+ a = a * b;
208
+ return a;
209
+ }
210
+
211
+ inline C10_HOST_DEVICE Float8_e4m3fnuz& operator/=(
212
+ Float8_e4m3fnuz& a,
213
+ const Float8_e4m3fnuz& b) {
214
+ a = a / b;
215
+ return a;
216
+ }
217
+
218
+ /// Arithmetic with floats
219
+
220
+ inline C10_HOST_DEVICE float operator+(Float8_e4m3fnuz a, float b) {
221
+ return static_cast<float>(a) + b;
222
+ }
223
+ inline C10_HOST_DEVICE float operator-(Float8_e4m3fnuz a, float b) {
224
+ return static_cast<float>(a) - b;
225
+ }
226
+ inline C10_HOST_DEVICE float operator*(Float8_e4m3fnuz a, float b) {
227
+ return static_cast<float>(a) * b;
228
+ }
229
+ inline C10_HOST_DEVICE float operator/(Float8_e4m3fnuz a, float b)
230
+ __ubsan_ignore_float_divide_by_zero__ {
231
+ return static_cast<float>(a) / b;
232
+ }
233
+
234
+ inline C10_HOST_DEVICE float operator+(float a, Float8_e4m3fnuz b) {
235
+ return a + static_cast<float>(b);
236
+ }
237
+ inline C10_HOST_DEVICE float operator-(float a, Float8_e4m3fnuz b) {
238
+ return a - static_cast<float>(b);
239
+ }
240
+ inline C10_HOST_DEVICE float operator*(float a, Float8_e4m3fnuz b) {
241
+ return a * static_cast<float>(b);
242
+ }
243
+ inline C10_HOST_DEVICE float operator/(float a, Float8_e4m3fnuz b)
244
+ __ubsan_ignore_float_divide_by_zero__ {
245
+ return a / static_cast<float>(b);
246
+ }
247
+
248
+ inline C10_HOST_DEVICE float& operator+=(float& a, const Float8_e4m3fnuz& b) {
249
+ return a += static_cast<float>(b);
250
+ }
251
+ inline C10_HOST_DEVICE float& operator-=(float& a, const Float8_e4m3fnuz& b) {
252
+ return a -= static_cast<float>(b);
253
+ }
254
+ inline C10_HOST_DEVICE float& operator*=(float& a, const Float8_e4m3fnuz& b) {
255
+ return a *= static_cast<float>(b);
256
+ }
257
+ inline C10_HOST_DEVICE float& operator/=(float& a, const Float8_e4m3fnuz& b) {
258
+ return a /= static_cast<float>(b);
259
+ }
260
+
261
+ /// Arithmetic with doubles
262
+
263
+ inline C10_HOST_DEVICE double operator+(Float8_e4m3fnuz a, double b) {
264
+ return static_cast<double>(a) + b;
265
+ }
266
+ inline C10_HOST_DEVICE double operator-(Float8_e4m3fnuz a, double b) {
267
+ return static_cast<double>(a) - b;
268
+ }
269
+ inline C10_HOST_DEVICE double operator*(Float8_e4m3fnuz a, double b) {
270
+ return static_cast<double>(a) * b;
271
+ }
272
+ inline C10_HOST_DEVICE double operator/(Float8_e4m3fnuz a, double b)
273
+ __ubsan_ignore_float_divide_by_zero__ {
274
+ return static_cast<double>(a) / b;
275
+ }
276
+
277
+ inline C10_HOST_DEVICE double operator+(double a, Float8_e4m3fnuz b) {
278
+ return a + static_cast<double>(b);
279
+ }
280
+ inline C10_HOST_DEVICE double operator-(double a, Float8_e4m3fnuz b) {
281
+ return a - static_cast<double>(b);
282
+ }
283
+ inline C10_HOST_DEVICE double operator*(double a, Float8_e4m3fnuz b) {
284
+ return a * static_cast<double>(b);
285
+ }
286
+ inline C10_HOST_DEVICE double operator/(double a, Float8_e4m3fnuz b)
287
+ __ubsan_ignore_float_divide_by_zero__ {
288
+ return a / static_cast<double>(b);
289
+ }
290
+
291
+ /// Arithmetic with ints
292
+
293
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator+(Float8_e4m3fnuz a, int b) {
294
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
295
+ return a + static_cast<Float8_e4m3fnuz>(b);
296
+ }
297
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator-(Float8_e4m3fnuz a, int b) {
298
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
299
+ return a - static_cast<Float8_e4m3fnuz>(b);
300
+ }
301
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator*(Float8_e4m3fnuz a, int b) {
302
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
303
+ return a * static_cast<Float8_e4m3fnuz>(b);
304
+ }
305
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator/(Float8_e4m3fnuz a, int b) {
306
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
307
+ return a / static_cast<Float8_e4m3fnuz>(b);
308
+ }
309
+
310
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator+(int a, Float8_e4m3fnuz b) {
311
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
312
+ return static_cast<Float8_e4m3fnuz>(a) + b;
313
+ }
314
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator-(int a, Float8_e4m3fnuz b) {
315
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
316
+ return static_cast<Float8_e4m3fnuz>(a) - b;
317
+ }
318
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator*(int a, Float8_e4m3fnuz b) {
319
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
320
+ return static_cast<Float8_e4m3fnuz>(a) * b;
321
+ }
322
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator/(int a, Float8_e4m3fnuz b) {
323
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
324
+ return static_cast<Float8_e4m3fnuz>(a) / b;
325
+ }
326
+
327
+ //// Arithmetic with int64_t
328
+
329
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator+(Float8_e4m3fnuz a, int64_t b) {
330
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
331
+ return a + static_cast<Float8_e4m3fnuz>(b);
332
+ }
333
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator-(Float8_e4m3fnuz a, int64_t b) {
334
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
335
+ return a - static_cast<Float8_e4m3fnuz>(b);
336
+ }
337
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator*(Float8_e4m3fnuz a, int64_t b) {
338
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
339
+ return a * static_cast<Float8_e4m3fnuz>(b);
340
+ }
341
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator/(Float8_e4m3fnuz a, int64_t b) {
342
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
343
+ return a / static_cast<Float8_e4m3fnuz>(b);
344
+ }
345
+
346
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator+(int64_t a, Float8_e4m3fnuz b) {
347
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
348
+ return static_cast<Float8_e4m3fnuz>(a) + b;
349
+ }
350
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator-(int64_t a, Float8_e4m3fnuz b) {
351
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
352
+ return static_cast<Float8_e4m3fnuz>(a) - b;
353
+ }
354
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator*(int64_t a, Float8_e4m3fnuz b) {
355
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
356
+ return static_cast<Float8_e4m3fnuz>(a) * b;
357
+ }
358
+ inline C10_HOST_DEVICE Float8_e4m3fnuz operator/(int64_t a, Float8_e4m3fnuz b) {
359
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
360
+ return static_cast<Float8_e4m3fnuz>(a) / b;
361
+ }
362
+
363
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
364
+ /// conversion from c10::Float8_e4m3fnuz to float.
365
+
366
+ C10_CLANG_DIAGNOSTIC_POP()
367
+
368
+ } // namespace c10
369
+
370
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
371
+ using c10::Float8_e4m3fnuz;
372
+ using c10::operator+;
373
+ using c10::operator-;
374
+ using c10::operator*;
375
+ using c10::operator/;
376
+ using c10::operator+=;
377
+ using c10::operator-=;
378
+ using c10::operator*=;
379
+ using c10::operator/=;
380
+ using c10::operator<<;
381
+
382
+ namespace detail {
383
+ using c10::detail::fp8e4m3fnuz_from_fp32_value;
384
+ } // namespace detail
385
+
386
+ HIDDEN_NAMESPACE_END(torch, headeronly)
387
+
388
+ namespace std {
389
+
390
+ template <>
391
+ class numeric_limits<c10::Float8_e4m3fnuz> {
392
+ public:
393
+ static constexpr bool is_specialized = true;
394
+ static constexpr bool is_signed = true;
395
+ static constexpr bool is_integer = false;
396
+ static constexpr bool is_exact = false;
397
+ static constexpr bool has_infinity = false;
398
+ static constexpr bool has_quiet_NaN = true;
399
+ static constexpr bool has_signaling_NaN = false;
400
+ static constexpr auto has_denorm = true;
401
+ static constexpr auto has_denorm_loss = true;
402
+ static constexpr auto round_style = numeric_limits<float>::round_style;
403
+ static constexpr bool is_iec559 = false;
404
+ static constexpr bool is_bounded = true;
405
+ static constexpr bool is_modulo = false;
406
+ static constexpr int digits = 4;
407
+ static constexpr int digits10 = 0;
408
+ static constexpr int max_digits10 = 3;
409
+ static constexpr int radix = 2;
410
+ static constexpr int min_exponent = -6;
411
+ static constexpr int min_exponent10 = -1;
412
+ static constexpr int max_exponent = 8;
413
+ static constexpr int max_exponent10 = 2;
414
+ static constexpr auto traps = numeric_limits<float>::traps;
415
+ static constexpr auto tinyness_before = false;
416
+
417
+ static constexpr c10::Float8_e4m3fnuz min() {
418
+ return c10::Float8_e4m3fnuz(0x08, c10::Float8_e4m3fnuz::from_bits());
419
+ }
420
+ static constexpr c10::Float8_e4m3fnuz lowest() {
421
+ return c10::Float8_e4m3fnuz(0xFF, c10::Float8_e4m3fnuz::from_bits());
422
+ }
423
+ static constexpr c10::Float8_e4m3fnuz max() {
424
+ return c10::Float8_e4m3fnuz(0x7F, c10::Float8_e4m3fnuz::from_bits());
425
+ }
426
+ static constexpr c10::Float8_e4m3fnuz epsilon() {
427
+ return c10::Float8_e4m3fnuz(0x28, c10::Float8_e4m3fnuz::from_bits());
428
+ }
429
+ static constexpr c10::Float8_e4m3fnuz round_error() {
430
+ return c10::Float8_e4m3fnuz(0x38, c10::Float8_e4m3fnuz::from_bits());
431
+ }
432
+ static constexpr c10::Float8_e4m3fnuz infinity() {
433
+ // NaN (no infinities)
434
+ return c10::Float8_e4m3fnuz(0x80, c10::Float8_e4m3fnuz::from_bits());
435
+ }
436
+ static constexpr c10::Float8_e4m3fnuz quiet_NaN() {
437
+ return c10::Float8_e4m3fnuz(0x80, c10::Float8_e4m3fnuz::from_bits());
438
+ }
439
+ static constexpr c10::Float8_e4m3fnuz denorm_min() {
440
+ return c10::Float8_e4m3fnuz(0x01, c10::Float8_e4m3fnuz::from_bits());
441
+ }
442
+ };
443
+
444
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e5m2.h ADDED
@@ -0,0 +1,458 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Float8_e5m2 type (8-bit floating-point) including conversions
4
+ /// to standard C types and basic arithmetic operations. Note that arithmetic
5
+ /// operations are implemented by converting to floating point and
6
+ /// performing the operation in float32.
7
+ /// Binary configuration:
8
+ /// s eeeee mm
9
+ /// 1 sign bit
10
+ /// 5 exponent bits
11
+ /// 2 mantissa bits
12
+ /// bias = 15
13
+ ///
14
+ /// Implementation based on the paper https://arxiv.org/pdf/2209.05433.pdf
15
+ /// and inspired by Half implementation from pytorch/c10/util/Half.h
16
+
17
+ #include <torch/headeronly/macros/Macros.h>
18
+ #include <torch/headeronly/util/Half.h>
19
+
20
+ #include <limits>
21
+
22
+ namespace c10 {
23
+
24
+ struct alignas(1) Float8_e5m2 {
25
+ uint8_t x;
26
+
27
+ struct from_bits_t {};
28
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
29
+ return from_bits_t();
30
+ }
31
+
32
+ Float8_e5m2() = default;
33
+
34
+ constexpr C10_HOST_DEVICE Float8_e5m2(uint8_t bits, from_bits_t /*unused*/)
35
+ : x(bits) {}
36
+ inline C10_HOST_DEVICE Float8_e5m2(float value);
37
+ inline C10_HOST_DEVICE operator float() const;
38
+ inline C10_HOST_DEVICE bool isnan() const;
39
+ inline C10_HOST_DEVICE bool isinf() const;
40
+ };
41
+
42
+ inline std::ostream& operator<<(std::ostream& out, const Float8_e5m2& value) {
43
+ out << (float)value;
44
+ return out;
45
+ }
46
+
47
+ namespace detail {
48
+
49
+ /*
50
+ * Convert a 8-bit floating-point number in fp8 E5M2 format, in bit
51
+ * representation, to a 32-bit floating-point number in IEEE single-precision
52
+ * format, in bit representation.
53
+ *
54
+ * @note The implementation doesn't use any floating-point operations.
55
+ */
56
+ inline C10_HOST_DEVICE float fp8e5m2_to_fp32_value(uint8_t input) {
57
+ /*
58
+ * Extend the fp8 E5M2 number to 32 bits and shift to the
59
+ * upper part of the 32-bit word:
60
+ * +---+----+---+-----------------------------+
61
+ * | S |EEEEE|MM|0000 0000 0000 0000 0000 0000|
62
+ * +---+----+---+-----------------------------+
63
+ * Bits 31 26-30 24-25 0-23
64
+ *
65
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
66
+ * - zero bits.
67
+ */
68
+ uint16_t half_representation = input;
69
+ half_representation <<= 8;
70
+ return fp16_ieee_to_fp32_value(half_representation);
71
+ }
72
+
73
+ /*
74
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
75
+ * 8-bit floating-point number in fp8 E5M2 format, in bit representation.
76
+ */
77
+ inline C10_HOST_DEVICE uint8_t fp8e5m2_from_fp32_value(float f) {
78
+ /*
79
+ * Binary representation of fp32 infinity
80
+ * 0 11111111 00000000000000000000000
81
+ */
82
+ constexpr uint32_t fp32_inf = UINT32_C(255) << 23;
83
+
84
+ /*
85
+ * Binary representation of 65536.0f, which is the first value
86
+ * not representable in fp8e5m2 range:
87
+ * 0 11111 00 - fp8e5m2
88
+ * 0 10001111 00000000000000000000000 - fp32
89
+ */
90
+ constexpr uint32_t fp8_max = UINT32_C(143) << 23;
91
+
92
+ /*
93
+ * A mask for converting fp32 numbers lower than fp8e5m2 normal range
94
+ * into denorm representation
95
+ * magic number: ((127 - 15) + (23 - 2) + 1)
96
+ */
97
+ constexpr uint32_t denorm_mask = UINT32_C(134) << 23;
98
+
99
+ uint32_t f_bits = fp32_to_bits(f);
100
+ uint8_t result = 0u;
101
+
102
+ /*
103
+ * Extract the sign of the input number into the high bit of the 32-bit word:
104
+ *
105
+ * +---+----------------------------------+
106
+ * | S |0000000 00000000 00000000 00000000|
107
+ * +---+----------------------------------+
108
+ * Bits 31 0-31
109
+ */
110
+ const uint32_t sign = f_bits & UINT32_C(0x80000000);
111
+
112
+ /*
113
+ * Set sign bit to 0
114
+ */
115
+ f_bits ^= sign;
116
+
117
+ if (f_bits >= fp8_max) {
118
+ // NaN - all exponent and mantissa bits set to 1
119
+ result = f_bits > fp32_inf ? UINT8_C(0x7F) : UINT8_C(0x7C);
120
+ } else {
121
+ if (f_bits < (UINT32_C(113) << 23)) {
122
+ // Input number is smaller than 2^(-14), which is the smallest
123
+ // fp8e5m2 normal number
124
+ f_bits =
125
+ fp32_to_bits(fp32_from_bits(f_bits) + fp32_from_bits(denorm_mask));
126
+ result = static_cast<uint8_t>(f_bits - denorm_mask);
127
+ } else {
128
+ // resulting mantissa is odd
129
+ uint32_t mant_odd = (f_bits >> 21) & 1;
130
+
131
+ // update exponent, rounding bias part 1
132
+ f_bits += ((uint32_t)(15 - 127) << 23) + 0xFFFFF;
133
+
134
+ // rounding bias part 2
135
+ f_bits += mant_odd;
136
+
137
+ // take the bits!
138
+ result = static_cast<uint8_t>(f_bits >> 21);
139
+ }
140
+ }
141
+
142
+ result |= static_cast<uint8_t>(sign >> 24);
143
+ return result;
144
+ }
145
+
146
+ } // namespace detail
147
+
148
+ // -------- below is copied from c10/util/Float8_e5m2-inl.h --------//
149
+ C10_CLANG_DIAGNOSTIC_PUSH()
150
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
151
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
152
+ #endif
153
+
154
+ #define EXP_WIDTH_FP8 5
155
+ #define MAN_WIDTH_FP8 2
156
+ #define EXP_BIAS_FP8 15
157
+
158
+ /// Constructors
159
+
160
+ inline C10_HOST_DEVICE Float8_e5m2::Float8_e5m2(float value)
161
+ : x(detail::fp8e5m2_from_fp32_value(value)) {}
162
+
163
+ /// Implicit conversions
164
+
165
+ inline C10_HOST_DEVICE Float8_e5m2::operator float() const {
166
+ return detail::fp8e5m2_to_fp32_value(x);
167
+ }
168
+
169
+ /// Special values helpers
170
+
171
+ inline C10_HOST_DEVICE bool Float8_e5m2::isnan() const {
172
+ return (x & 0b01111111) > 0b01111100;
173
+ }
174
+
175
+ inline C10_HOST_DEVICE bool Float8_e5m2::isinf() const {
176
+ return (x & 0b01111111) == 0b01111100;
177
+ }
178
+
179
+ /// Arithmetic
180
+
181
+ inline C10_HOST_DEVICE Float8_e5m2
182
+ operator+(const Float8_e5m2& a, const Float8_e5m2& b) {
183
+ return static_cast<float>(a) + static_cast<float>(b);
184
+ }
185
+
186
+ inline C10_HOST_DEVICE Float8_e5m2
187
+ operator-(const Float8_e5m2& a, const Float8_e5m2& b) {
188
+ return static_cast<float>(a) - static_cast<float>(b);
189
+ }
190
+
191
+ inline C10_HOST_DEVICE Float8_e5m2
192
+ operator*(const Float8_e5m2& a, const Float8_e5m2& b) {
193
+ return static_cast<float>(a) * static_cast<float>(b);
194
+ }
195
+
196
+ inline C10_HOST_DEVICE Float8_e5m2 operator/(
197
+ const Float8_e5m2& a,
198
+ const Float8_e5m2& b) __ubsan_ignore_float_divide_by_zero__ {
199
+ return static_cast<float>(a) / static_cast<float>(b);
200
+ }
201
+
202
+ inline C10_HOST_DEVICE Float8_e5m2 operator-(const Float8_e5m2& a) {
203
+ return -static_cast<float>(a);
204
+ }
205
+
206
+ inline C10_HOST_DEVICE Float8_e5m2& operator+=(
207
+ Float8_e5m2& a,
208
+ const Float8_e5m2& b) {
209
+ a = a + b;
210
+ return a;
211
+ }
212
+
213
+ inline C10_HOST_DEVICE Float8_e5m2& operator-=(
214
+ Float8_e5m2& a,
215
+ const Float8_e5m2& b) {
216
+ a = a - b;
217
+ return a;
218
+ }
219
+
220
+ inline C10_HOST_DEVICE Float8_e5m2& operator*=(
221
+ Float8_e5m2& a,
222
+ const Float8_e5m2& b) {
223
+ a = a * b;
224
+ return a;
225
+ }
226
+
227
+ inline C10_HOST_DEVICE Float8_e5m2& operator/=(
228
+ Float8_e5m2& a,
229
+ const Float8_e5m2& b) {
230
+ a = a / b;
231
+ return a;
232
+ }
233
+
234
+ /// Arithmetic with floats
235
+
236
+ inline C10_HOST_DEVICE float operator+(Float8_e5m2 a, float b) {
237
+ return static_cast<float>(a) + b;
238
+ }
239
+ inline C10_HOST_DEVICE float operator-(Float8_e5m2 a, float b) {
240
+ return static_cast<float>(a) - b;
241
+ }
242
+ inline C10_HOST_DEVICE float operator*(Float8_e5m2 a, float b) {
243
+ return static_cast<float>(a) * b;
244
+ }
245
+ inline C10_HOST_DEVICE float operator/(Float8_e5m2 a, float b)
246
+ __ubsan_ignore_float_divide_by_zero__ {
247
+ return static_cast<float>(a) / b;
248
+ }
249
+
250
+ inline C10_HOST_DEVICE float operator+(float a, Float8_e5m2 b) {
251
+ return a + static_cast<float>(b);
252
+ }
253
+ inline C10_HOST_DEVICE float operator-(float a, Float8_e5m2 b) {
254
+ return a - static_cast<float>(b);
255
+ }
256
+ inline C10_HOST_DEVICE float operator*(float a, Float8_e5m2 b) {
257
+ return a * static_cast<float>(b);
258
+ }
259
+ inline C10_HOST_DEVICE float operator/(float a, Float8_e5m2 b)
260
+ __ubsan_ignore_float_divide_by_zero__ {
261
+ return a / static_cast<float>(b);
262
+ }
263
+
264
+ inline C10_HOST_DEVICE float& operator+=(float& a, const Float8_e5m2& b) {
265
+ return a += static_cast<float>(b);
266
+ }
267
+ inline C10_HOST_DEVICE float& operator-=(float& a, const Float8_e5m2& b) {
268
+ return a -= static_cast<float>(b);
269
+ }
270
+ inline C10_HOST_DEVICE float& operator*=(float& a, const Float8_e5m2& b) {
271
+ return a *= static_cast<float>(b);
272
+ }
273
+ inline C10_HOST_DEVICE float& operator/=(float& a, const Float8_e5m2& b) {
274
+ return a /= static_cast<float>(b);
275
+ }
276
+
277
+ /// Arithmetic with doubles
278
+
279
+ inline C10_HOST_DEVICE double operator+(Float8_e5m2 a, double b) {
280
+ return static_cast<double>(a) + b;
281
+ }
282
+ inline C10_HOST_DEVICE double operator-(Float8_e5m2 a, double b) {
283
+ return static_cast<double>(a) - b;
284
+ }
285
+ inline C10_HOST_DEVICE double operator*(Float8_e5m2 a, double b) {
286
+ return static_cast<double>(a) * b;
287
+ }
288
+ inline C10_HOST_DEVICE double operator/(Float8_e5m2 a, double b)
289
+ __ubsan_ignore_float_divide_by_zero__ {
290
+ return static_cast<double>(a) / b;
291
+ }
292
+
293
+ inline C10_HOST_DEVICE double operator+(double a, Float8_e5m2 b) {
294
+ return a + static_cast<double>(b);
295
+ }
296
+ inline C10_HOST_DEVICE double operator-(double a, Float8_e5m2 b) {
297
+ return a - static_cast<double>(b);
298
+ }
299
+ inline C10_HOST_DEVICE double operator*(double a, Float8_e5m2 b) {
300
+ return a * static_cast<double>(b);
301
+ }
302
+ inline C10_HOST_DEVICE double operator/(double a, Float8_e5m2 b)
303
+ __ubsan_ignore_float_divide_by_zero__ {
304
+ return a / static_cast<double>(b);
305
+ }
306
+
307
+ /// Arithmetic with ints
308
+
309
+ inline C10_HOST_DEVICE Float8_e5m2 operator+(Float8_e5m2 a, int b) {
310
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
311
+ return a + static_cast<Float8_e5m2>(b);
312
+ }
313
+ inline C10_HOST_DEVICE Float8_e5m2 operator-(Float8_e5m2 a, int b) {
314
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
315
+ return a - static_cast<Float8_e5m2>(b);
316
+ }
317
+ inline C10_HOST_DEVICE Float8_e5m2 operator*(Float8_e5m2 a, int b) {
318
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
319
+ return a * static_cast<Float8_e5m2>(b);
320
+ }
321
+ inline C10_HOST_DEVICE Float8_e5m2 operator/(Float8_e5m2 a, int b) {
322
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
323
+ return a / static_cast<Float8_e5m2>(b);
324
+ }
325
+
326
+ inline C10_HOST_DEVICE Float8_e5m2 operator+(int a, Float8_e5m2 b) {
327
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
328
+ return static_cast<Float8_e5m2>(a) + b;
329
+ }
330
+ inline C10_HOST_DEVICE Float8_e5m2 operator-(int a, Float8_e5m2 b) {
331
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
332
+ return static_cast<Float8_e5m2>(a) - b;
333
+ }
334
+ inline C10_HOST_DEVICE Float8_e5m2 operator*(int a, Float8_e5m2 b) {
335
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
336
+ return static_cast<Float8_e5m2>(a) * b;
337
+ }
338
+ inline C10_HOST_DEVICE Float8_e5m2 operator/(int a, Float8_e5m2 b) {
339
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
340
+ return static_cast<Float8_e5m2>(a) / b;
341
+ }
342
+
343
+ //// Arithmetic with int64_t
344
+
345
+ inline C10_HOST_DEVICE Float8_e5m2 operator+(Float8_e5m2 a, int64_t b) {
346
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
347
+ return a + static_cast<Float8_e5m2>(b);
348
+ }
349
+ inline C10_HOST_DEVICE Float8_e5m2 operator-(Float8_e5m2 a, int64_t b) {
350
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
351
+ return a - static_cast<Float8_e5m2>(b);
352
+ }
353
+ inline C10_HOST_DEVICE Float8_e5m2 operator*(Float8_e5m2 a, int64_t b) {
354
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
355
+ return a * static_cast<Float8_e5m2>(b);
356
+ }
357
+ inline C10_HOST_DEVICE Float8_e5m2 operator/(Float8_e5m2 a, int64_t b) {
358
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
359
+ return a / static_cast<Float8_e5m2>(b);
360
+ }
361
+
362
+ inline C10_HOST_DEVICE Float8_e5m2 operator+(int64_t a, Float8_e5m2 b) {
363
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
364
+ return static_cast<Float8_e5m2>(a) + b;
365
+ }
366
+ inline C10_HOST_DEVICE Float8_e5m2 operator-(int64_t a, Float8_e5m2 b) {
367
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
368
+ return static_cast<Float8_e5m2>(a) - b;
369
+ }
370
+ inline C10_HOST_DEVICE Float8_e5m2 operator*(int64_t a, Float8_e5m2 b) {
371
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
372
+ return static_cast<Float8_e5m2>(a) * b;
373
+ }
374
+ inline C10_HOST_DEVICE Float8_e5m2 operator/(int64_t a, Float8_e5m2 b) {
375
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
376
+ return static_cast<Float8_e5m2>(a) / b;
377
+ }
378
+
379
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
380
+ /// conversion from c10::Float8_e5m2 to float.
381
+ C10_CLANG_DIAGNOSTIC_POP()
382
+ } // namespace c10
383
+
384
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
385
+ using c10::Float8_e5m2;
386
+ using c10::operator<<;
387
+ using c10::operator+;
388
+ using c10::operator-;
389
+ using c10::operator*;
390
+ using c10::operator/;
391
+ using c10::operator+=;
392
+ using c10::operator-=;
393
+ using c10::operator*=;
394
+ using c10::operator/=;
395
+
396
+ namespace detail {
397
+ using c10::detail::fp8e5m2_from_fp32_value;
398
+ using c10::detail::fp8e5m2_to_fp32_value;
399
+ } // namespace detail
400
+ HIDDEN_NAMESPACE_END(torch, headeronly)
401
+
402
+ namespace std {
403
+
404
+ template <>
405
+ class numeric_limits<c10::Float8_e5m2> {
406
+ public:
407
+ static constexpr bool is_signed = true;
408
+ static constexpr bool is_integer = false;
409
+ static constexpr bool is_specialized = true;
410
+ static constexpr bool is_exact = false;
411
+ static constexpr bool has_infinity = true;
412
+ static constexpr bool has_quiet_NaN = true;
413
+ static constexpr bool has_signaling_NaN = false;
414
+ static constexpr auto has_denorm = true;
415
+ static constexpr auto has_denorm_loss = true;
416
+ static constexpr auto round_style = numeric_limits<float>::round_style;
417
+ static constexpr bool is_iec559 = false;
418
+ static constexpr bool is_bounded = true;
419
+ static constexpr bool is_modulo = false;
420
+ static constexpr int digits = 3;
421
+ static constexpr int digits10 = 0;
422
+ static constexpr int max_digits10 = 2;
423
+ static constexpr int radix = 2;
424
+ static constexpr int min_exponent = -13;
425
+ static constexpr int min_exponent10 = -4;
426
+ static constexpr int max_exponent = 16;
427
+ static constexpr int max_exponent10 = 4;
428
+ static constexpr auto traps = numeric_limits<float>::traps;
429
+ static constexpr auto tinyness_before =
430
+ numeric_limits<float>::tinyness_before;
431
+
432
+ static constexpr c10::Float8_e5m2 min() {
433
+ return c10::Float8_e5m2(0x4, c10::Float8_e5m2::from_bits());
434
+ }
435
+ static constexpr c10::Float8_e5m2 max() {
436
+ return c10::Float8_e5m2(0x7B, c10::Float8_e5m2::from_bits());
437
+ }
438
+ static constexpr c10::Float8_e5m2 lowest() {
439
+ return c10::Float8_e5m2(0xFB, c10::Float8_e5m2::from_bits());
440
+ }
441
+ static constexpr c10::Float8_e5m2 epsilon() {
442
+ return c10::Float8_e5m2(0x34, c10::Float8_e5m2::from_bits());
443
+ }
444
+ static constexpr c10::Float8_e5m2 round_error() {
445
+ return c10::Float8_e5m2(0x38, c10::Float8_e5m2::from_bits());
446
+ }
447
+ static constexpr c10::Float8_e5m2 infinity() {
448
+ return c10::Float8_e5m2(0x7C, c10::Float8_e5m2::from_bits());
449
+ }
450
+ static constexpr c10::Float8_e5m2 quiet_NaN() {
451
+ return c10::Float8_e5m2(0x7F, c10::Float8_e5m2::from_bits());
452
+ }
453
+ static constexpr c10::Float8_e5m2 denorm_min() {
454
+ return c10::Float8_e5m2(0x01, c10::Float8_e5m2::from_bits());
455
+ }
456
+ };
457
+
458
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e5m2fnuz.h ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Float8_e5m2fnuz type (8-bit floating-point) including
4
+ /// conversions to standard C types and basic arithmetic operations. Note that
5
+ /// arithmetic operations are implemented by converting to floating point and
6
+ /// performing the operation in float32.
7
+ /// Binary configuration remains the same as e5m2:
8
+ /// s eeeee mm
9
+ /// 1 sign bit
10
+ /// 5 exponent bits
11
+ /// 2 mantissa bits
12
+ /// The key differences that e5m2fnuz brings are:
13
+ /// bias = 16
14
+ /// no infinities or negative zero
15
+ /// NaN only when sign bit is 1, rest all 0s
16
+ ///
17
+ /// Implementation based on the paper https://arxiv.org/pdf/2206.02915.pdf and
18
+ /// the existing Float8_e4m3fn implementation.
19
+
20
+ #include <torch/headeronly/macros/Macros.h>
21
+ #include <torch/headeronly/util/Float8_fnuz_cvt.h>
22
+ #include <torch/headeronly/util/TypeSafeSignMath.h>
23
+ #include <torch/headeronly/util/floating_point_utils.h>
24
+
25
+ #if defined(__cplusplus)
26
+ #include <cstdint>
27
+ #elif !defined(__OPENCL_VERSION__)
28
+ #include <math.h>
29
+ #include <stdint.h>
30
+ #endif
31
+
32
+ #include <iosfwd>
33
+ #include <ostream>
34
+
35
+ namespace c10 {
36
+
37
+ struct alignas(1) Float8_e5m2fnuz {
38
+ uint8_t x;
39
+
40
+ struct from_bits_t {};
41
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
42
+ return from_bits_t();
43
+ }
44
+
45
+ Float8_e5m2fnuz() = default;
46
+
47
+ constexpr C10_HOST_DEVICE Float8_e5m2fnuz(
48
+ uint8_t bits,
49
+ from_bits_t /*unused*/)
50
+ : x(bits) {}
51
+ inline C10_HOST_DEVICE Float8_e5m2fnuz(float value);
52
+ inline C10_HOST_DEVICE operator float() const;
53
+ inline C10_HOST_DEVICE bool isnan() const;
54
+ inline C10_HOST_DEVICE bool isinf() const;
55
+ };
56
+
57
+ inline std::ostream& operator<<(
58
+ std::ostream& out,
59
+ const Float8_e5m2fnuz& value) {
60
+ out << (float)value;
61
+ return out;
62
+ }
63
+
64
+ namespace detail {
65
+
66
+ /*
67
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
68
+ * 8-bit floating-point number in fp8 E5M2 format, in bit representation.
69
+ */
70
+ inline C10_HOST_DEVICE uint8_t fp8e5m2fnuz_from_fp32_value(float f) {
71
+ /*
72
+ * Binary representation of 65536.0f, which is the first value not
73
+ * representable (i.e. the first value which would overflow in to the sign
74
+ * bit, resulting in a NaN) in fp8e4m3fnuz range:
75
+ * 1 00000 00 - fp8e5m2fnuz
76
+ * 0 10001111 00000000000000000000000 - fp32
77
+ */
78
+ constexpr uint32_t fnuz_max = UINT32_C(0x8F) << 23;
79
+
80
+ /*
81
+ * A mask for converting fp32 numbers lower than fp8e5m2fnuz normal range
82
+ * into denormalized representation.
83
+ * magic number: ((127 - 16) + (23 - 2) + 1)
84
+ */
85
+ constexpr uint32_t denorm_mask = UINT32_C(0x85) << 23;
86
+
87
+ uint32_t f_bits = fp32_to_bits(f);
88
+ uint32_t result = 0u;
89
+
90
+ /*
91
+ * Extract the sign of the input number into the high bit of the 32-bit word:
92
+ *
93
+ * +---+----------------------------------+
94
+ * | S |0000000 00000000 00000000 00000000|
95
+ * +---+----------------------------------+
96
+ * Bits 31 0-31
97
+ */
98
+ const uint32_t sign = f_bits & UINT32_C(0x80000000);
99
+
100
+ /*
101
+ * Set sign bit to 0
102
+ */
103
+ f_bits ^= sign;
104
+
105
+ if (f_bits >= fnuz_max) {
106
+ // NaN -- sign bit set to 1, rest 0s
107
+ return 0x80;
108
+ }
109
+
110
+ if (f_bits < (UINT32_C(0x70) << 23) /* 2^-15 in float32 */) {
111
+ // Input exponent is less than -15, the smallest e5m2fnuz exponent, so the
112
+ // number will become subnormal.
113
+ f_bits = fp32_to_bits(fp32_from_bits(f_bits) + fp32_from_bits(denorm_mask));
114
+ result = static_cast<uint8_t>(f_bits - denorm_mask);
115
+ if (result == 0) {
116
+ // fnuz types don't have negative zero.
117
+ return 0;
118
+ }
119
+ } else {
120
+ // resulting mantissa is odd
121
+ uint8_t mant_odd = (f_bits >> 21) & 1;
122
+
123
+ // update exponent, rounding bias part 1
124
+ f_bits += ((uint32_t)(16 - 127) << 23) + 0xFFFFF;
125
+
126
+ // rounding bias part 2
127
+ f_bits += mant_odd;
128
+
129
+ // take the bits!
130
+ result = static_cast<uint8_t>(f_bits >> 21);
131
+ }
132
+
133
+ result |= sign >> 24;
134
+ return result;
135
+ }
136
+
137
+ } // namespace detail
138
+
139
+ //------ below is copied from c10/util/Float8_e5m2fnuz-inl.h ------//
140
+ C10_CLANG_DIAGNOSTIC_PUSH()
141
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
142
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
143
+ #endif
144
+
145
+ /// Constructors
146
+
147
+ inline C10_HOST_DEVICE Float8_e5m2fnuz::Float8_e5m2fnuz(float value)
148
+ : x(detail::fp8e5m2fnuz_from_fp32_value(value)) {}
149
+
150
+ /// Implicit conversions
151
+
152
+ inline C10_HOST_DEVICE Float8_e5m2fnuz::operator float() const {
153
+ return torch::headeronly::detail::fp8_fnuz_to_fp32_value<5, 2>(x);
154
+ }
155
+
156
+ /// Special values helpers
157
+
158
+ inline C10_HOST_DEVICE bool Float8_e5m2fnuz::isnan() const {
159
+ return x == 0b10000000;
160
+ }
161
+
162
+ inline C10_HOST_DEVICE bool Float8_e5m2fnuz::isinf() const {
163
+ return false;
164
+ }
165
+
166
+ /// Arithmetic
167
+
168
+ inline C10_HOST_DEVICE Float8_e5m2fnuz
169
+ operator+(const Float8_e5m2fnuz& a, const Float8_e5m2fnuz& b) {
170
+ return static_cast<float>(a) + static_cast<float>(b);
171
+ }
172
+
173
+ inline C10_HOST_DEVICE Float8_e5m2fnuz
174
+ operator-(const Float8_e5m2fnuz& a, const Float8_e5m2fnuz& b) {
175
+ return static_cast<float>(a) - static_cast<float>(b);
176
+ }
177
+
178
+ inline C10_HOST_DEVICE Float8_e5m2fnuz
179
+ operator*(const Float8_e5m2fnuz& a, const Float8_e5m2fnuz& b) {
180
+ return static_cast<float>(a) * static_cast<float>(b);
181
+ }
182
+
183
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator/(
184
+ const Float8_e5m2fnuz& a,
185
+ const Float8_e5m2fnuz& b) __ubsan_ignore_float_divide_by_zero__ {
186
+ return static_cast<float>(a) / static_cast<float>(b);
187
+ }
188
+
189
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator-(const Float8_e5m2fnuz& a) {
190
+ return -static_cast<float>(a);
191
+ }
192
+
193
+ inline C10_HOST_DEVICE Float8_e5m2fnuz& operator+=(
194
+ Float8_e5m2fnuz& a,
195
+ const Float8_e5m2fnuz& b) {
196
+ a = a + b;
197
+ return a;
198
+ }
199
+
200
+ inline C10_HOST_DEVICE Float8_e5m2fnuz& operator-=(
201
+ Float8_e5m2fnuz& a,
202
+ const Float8_e5m2fnuz& b) {
203
+ a = a - b;
204
+ return a;
205
+ }
206
+
207
+ inline C10_HOST_DEVICE Float8_e5m2fnuz& operator*=(
208
+ Float8_e5m2fnuz& a,
209
+ const Float8_e5m2fnuz& b) {
210
+ a = a * b;
211
+ return a;
212
+ }
213
+
214
+ inline C10_HOST_DEVICE Float8_e5m2fnuz& operator/=(
215
+ Float8_e5m2fnuz& a,
216
+ const Float8_e5m2fnuz& b) {
217
+ a = a / b;
218
+ return a;
219
+ }
220
+
221
+ /// Arithmetic with floats
222
+
223
+ inline C10_HOST_DEVICE float operator+(Float8_e5m2fnuz a, float b) {
224
+ return static_cast<float>(a) + b;
225
+ }
226
+ inline C10_HOST_DEVICE float operator-(Float8_e5m2fnuz a, float b) {
227
+ return static_cast<float>(a) - b;
228
+ }
229
+ inline C10_HOST_DEVICE float operator*(Float8_e5m2fnuz a, float b) {
230
+ return static_cast<float>(a) * b;
231
+ }
232
+ inline C10_HOST_DEVICE float operator/(Float8_e5m2fnuz a, float b)
233
+ __ubsan_ignore_float_divide_by_zero__ {
234
+ return static_cast<float>(a) / b;
235
+ }
236
+
237
+ inline C10_HOST_DEVICE float operator+(float a, Float8_e5m2fnuz b) {
238
+ return a + static_cast<float>(b);
239
+ }
240
+ inline C10_HOST_DEVICE float operator-(float a, Float8_e5m2fnuz b) {
241
+ return a - static_cast<float>(b);
242
+ }
243
+ inline C10_HOST_DEVICE float operator*(float a, Float8_e5m2fnuz b) {
244
+ return a * static_cast<float>(b);
245
+ }
246
+ inline C10_HOST_DEVICE float operator/(float a, Float8_e5m2fnuz b)
247
+ __ubsan_ignore_float_divide_by_zero__ {
248
+ return a / static_cast<float>(b);
249
+ }
250
+
251
+ inline C10_HOST_DEVICE float& operator+=(float& a, const Float8_e5m2fnuz& b) {
252
+ return a += static_cast<float>(b);
253
+ }
254
+ inline C10_HOST_DEVICE float& operator-=(float& a, const Float8_e5m2fnuz& b) {
255
+ return a -= static_cast<float>(b);
256
+ }
257
+ inline C10_HOST_DEVICE float& operator*=(float& a, const Float8_e5m2fnuz& b) {
258
+ return a *= static_cast<float>(b);
259
+ }
260
+ inline C10_HOST_DEVICE float& operator/=(float& a, const Float8_e5m2fnuz& b) {
261
+ return a /= static_cast<float>(b);
262
+ }
263
+
264
+ /// Arithmetic with doubles
265
+
266
+ inline C10_HOST_DEVICE double operator+(Float8_e5m2fnuz a, double b) {
267
+ return static_cast<double>(a) + b;
268
+ }
269
+ inline C10_HOST_DEVICE double operator-(Float8_e5m2fnuz a, double b) {
270
+ return static_cast<double>(a) - b;
271
+ }
272
+ inline C10_HOST_DEVICE double operator*(Float8_e5m2fnuz a, double b) {
273
+ return static_cast<double>(a) * b;
274
+ }
275
+ inline C10_HOST_DEVICE double operator/(Float8_e5m2fnuz a, double b)
276
+ __ubsan_ignore_float_divide_by_zero__ {
277
+ return static_cast<double>(a) / b;
278
+ }
279
+
280
+ inline C10_HOST_DEVICE double operator+(double a, Float8_e5m2fnuz b) {
281
+ return a + static_cast<double>(b);
282
+ }
283
+ inline C10_HOST_DEVICE double operator-(double a, Float8_e5m2fnuz b) {
284
+ return a - static_cast<double>(b);
285
+ }
286
+ inline C10_HOST_DEVICE double operator*(double a, Float8_e5m2fnuz b) {
287
+ return a * static_cast<double>(b);
288
+ }
289
+ inline C10_HOST_DEVICE double operator/(double a, Float8_e5m2fnuz b)
290
+ __ubsan_ignore_float_divide_by_zero__ {
291
+ return a / static_cast<double>(b);
292
+ }
293
+
294
+ /// Arithmetic with ints
295
+
296
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator+(Float8_e5m2fnuz a, int b) {
297
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
298
+ return a + static_cast<Float8_e5m2fnuz>(b);
299
+ }
300
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator-(Float8_e5m2fnuz a, int b) {
301
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
302
+ return a - static_cast<Float8_e5m2fnuz>(b);
303
+ }
304
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator*(Float8_e5m2fnuz a, int b) {
305
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
306
+ return a * static_cast<Float8_e5m2fnuz>(b);
307
+ }
308
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator/(Float8_e5m2fnuz a, int b) {
309
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
310
+ return a / static_cast<Float8_e5m2fnuz>(b);
311
+ }
312
+
313
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator+(int a, Float8_e5m2fnuz b) {
314
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
315
+ return static_cast<Float8_e5m2fnuz>(a) + b;
316
+ }
317
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator-(int a, Float8_e5m2fnuz b) {
318
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
319
+ return static_cast<Float8_e5m2fnuz>(a) - b;
320
+ }
321
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator*(int a, Float8_e5m2fnuz b) {
322
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
323
+ return static_cast<Float8_e5m2fnuz>(a) * b;
324
+ }
325
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator/(int a, Float8_e5m2fnuz b) {
326
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
327
+ return static_cast<Float8_e5m2fnuz>(a) / b;
328
+ }
329
+
330
+ //// Arithmetic with int64_t
331
+
332
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator+(Float8_e5m2fnuz a, int64_t b) {
333
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
334
+ return a + static_cast<Float8_e5m2fnuz>(b);
335
+ }
336
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator-(Float8_e5m2fnuz a, int64_t b) {
337
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
338
+ return a - static_cast<Float8_e5m2fnuz>(b);
339
+ }
340
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator*(Float8_e5m2fnuz a, int64_t b) {
341
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
342
+ return a * static_cast<Float8_e5m2fnuz>(b);
343
+ }
344
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator/(Float8_e5m2fnuz a, int64_t b) {
345
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
346
+ return a / static_cast<Float8_e5m2fnuz>(b);
347
+ }
348
+
349
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator+(int64_t a, Float8_e5m2fnuz b) {
350
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
351
+ return static_cast<Float8_e5m2fnuz>(a) + b;
352
+ }
353
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator-(int64_t a, Float8_e5m2fnuz b) {
354
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
355
+ return static_cast<Float8_e5m2fnuz>(a) - b;
356
+ }
357
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator*(int64_t a, Float8_e5m2fnuz b) {
358
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
359
+ return static_cast<Float8_e5m2fnuz>(a) * b;
360
+ }
361
+ inline C10_HOST_DEVICE Float8_e5m2fnuz operator/(int64_t a, Float8_e5m2fnuz b) {
362
+ // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
363
+ return static_cast<Float8_e5m2fnuz>(a) / b;
364
+ }
365
+
366
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
367
+ /// conversion from c10::Float8_e5m2fnuz to float.
368
+
369
+ C10_CLANG_DIAGNOSTIC_POP()
370
+
371
+ } // namespace c10
372
+
373
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
374
+ using c10::Float8_e5m2fnuz;
375
+ using c10::operator<<;
376
+ using c10::operator+;
377
+ using c10::operator-;
378
+ using c10::operator*;
379
+ using c10::operator/;
380
+ using c10::operator+=;
381
+ using c10::operator-=;
382
+ using c10::operator*=;
383
+ using c10::operator/=;
384
+
385
+ namespace detail {
386
+ using c10::detail::fp8e5m2fnuz_from_fp32_value;
387
+ }
388
+ HIDDEN_NAMESPACE_END(torch, headeronly)
389
+
390
+ namespace std {
391
+
392
+ template <>
393
+ class numeric_limits<c10::Float8_e5m2fnuz> {
394
+ public:
395
+ static constexpr bool is_signed = true;
396
+ static constexpr bool is_integer = false;
397
+ static constexpr bool is_specialized = true;
398
+ static constexpr bool is_exact = false;
399
+ static constexpr bool has_infinity = false;
400
+ static constexpr bool has_quiet_NaN = true;
401
+ static constexpr bool has_signaling_NaN = false;
402
+ static constexpr auto has_denorm = true;
403
+ static constexpr auto has_denorm_loss = true;
404
+ static constexpr auto round_style = numeric_limits<float>::round_style;
405
+ static constexpr bool is_iec559 = false;
406
+ static constexpr bool is_bounded = true;
407
+ static constexpr bool is_modulo = false;
408
+ static constexpr int digits = 3;
409
+ static constexpr int digits10 = 0;
410
+ static constexpr int max_digits10 = 2;
411
+ static constexpr int radix = 2;
412
+ static constexpr int min_exponent = -14;
413
+ static constexpr int min_exponent10 = -4;
414
+ static constexpr int max_exponent = 16;
415
+ static constexpr int max_exponent10 = 4;
416
+ static constexpr auto traps = numeric_limits<float>::traps;
417
+ static constexpr auto tinyness_before =
418
+ numeric_limits<float>::tinyness_before;
419
+
420
+ static constexpr c10::Float8_e5m2fnuz min() {
421
+ return c10::Float8_e5m2fnuz(0x04, c10::Float8_e5m2fnuz::from_bits());
422
+ }
423
+ static constexpr c10::Float8_e5m2fnuz max() {
424
+ return c10::Float8_e5m2fnuz(0x7F, c10::Float8_e5m2fnuz::from_bits());
425
+ }
426
+ static constexpr c10::Float8_e5m2fnuz lowest() {
427
+ return c10::Float8_e5m2fnuz(0xFF, c10::Float8_e5m2fnuz::from_bits());
428
+ }
429
+ static constexpr c10::Float8_e5m2fnuz epsilon() {
430
+ return c10::Float8_e5m2fnuz(0x34, c10::Float8_e5m2fnuz::from_bits());
431
+ }
432
+ static constexpr c10::Float8_e5m2fnuz round_error() {
433
+ return c10::Float8_e5m2fnuz(0x38, c10::Float8_e5m2fnuz::from_bits());
434
+ }
435
+ static constexpr c10::Float8_e5m2fnuz infinity() {
436
+ return c10::Float8_e5m2fnuz(0x80, c10::Float8_e5m2fnuz::from_bits());
437
+ }
438
+ // TODO(future): we are mapping neg_zero to both inf and NaN, this is
439
+ // surprising and we should figure out what to do about it.
440
+ static constexpr c10::Float8_e5m2fnuz quiet_NaN() {
441
+ return c10::Float8_e5m2fnuz(0x80, c10::Float8_e5m2fnuz::from_bits());
442
+ }
443
+ static constexpr c10::Float8_e5m2fnuz denorm_min() {
444
+ return c10::Float8_e5m2fnuz(0x01, c10::Float8_e5m2fnuz::from_bits());
445
+ }
446
+ };
447
+
448
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_e8m0fnu.h ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Float8_e8m0fnu type (8-bit floating-point) including
4
+ /// conversions to standard C types
5
+ /// Binary configuration :
6
+ /// eeeeeeee
7
+ /// no sign bits
8
+ /// 8 exponent bits
9
+ /// no mantissa bits
10
+ ///
11
+ /// This is the E8M0 dtype from the OCP MX format spec
12
+ /// (https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf,
13
+ /// Section 5.4.1)
14
+
15
+ #include <torch/headeronly/macros/Macros.h>
16
+ #include <torch/headeronly/util/floating_point_utils.h>
17
+
18
+ // TODO(#146647): do we need to special case OPENCL?
19
+ #if defined(__cplusplus)
20
+ #include <cstdint>
21
+ #elif !defined(__OPENCL_VERSION__)
22
+ #include <math.h>
23
+ #include <stdint.h>
24
+ #endif
25
+
26
+ #include <iosfwd>
27
+ #include <limits>
28
+ #include <ostream>
29
+
30
+ namespace c10 {
31
+
32
+ struct alignas(1) Float8_e8m0fnu {
33
+ uint8_t x;
34
+
35
+ struct from_bits_t {};
36
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
37
+ return from_bits_t();
38
+ }
39
+
40
+ Float8_e8m0fnu() = default;
41
+
42
+ constexpr C10_HOST_DEVICE Float8_e8m0fnu(uint8_t bits, from_bits_t /*unused*/)
43
+ : x(bits) {}
44
+ inline C10_HOST_DEVICE Float8_e8m0fnu(float value);
45
+ inline C10_HOST_DEVICE operator float() const;
46
+ inline C10_HOST_DEVICE bool isnan() const;
47
+ };
48
+
49
+ inline std::ostream& operator<<(
50
+ std::ostream& out,
51
+ const Float8_e8m0fnu& value) {
52
+ out << (float)value;
53
+ return out;
54
+ }
55
+
56
+ namespace detail {
57
+ /*
58
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
59
+ * 8-bit floating-point number in fp8 e8m0fnu format, in bit representation.
60
+ */
61
+ inline C10_HOST_DEVICE uint8_t fp8e8m0fnu_from_fp32_value(float f) {
62
+ // TODO(#146647): maybe rewrite without control flow
63
+
64
+ uint32_t f_bits = c10::detail::fp32_to_bits(f);
65
+
66
+ // extract the exponent
67
+ uint32_t exponent = (f_bits >> 23) & 0b11111111;
68
+
69
+ // special case float32 NaN and +-inf to map to e8m0 nan
70
+ if (exponent == 0b11111111) {
71
+ return exponent;
72
+ }
73
+
74
+ // next, we use guard, round, sticky bits and the LSB to implement round to
75
+ // nearest, with ties to even
76
+
77
+ // guard bit - bit 23, or 22 zero-indexed
78
+ uint8_t g = (f_bits & 0x400000) > 0;
79
+ // round bit - bit 22, or 21 zero-indexed
80
+ uint8_t r = (f_bits & 0x200000) > 0;
81
+ // sticky bit - bits 21 to 1, or 20 to 0 zero-indexed
82
+ uint8_t s = (f_bits & 0x1FFFFF) > 0;
83
+ // in casting to e8m0, LSB is the implied mantissa bit. It equals to 0 if the
84
+ // original float32 is denormal, and to 1 if the original float32 is normal.
85
+ uint8_t lsb = exponent > 0;
86
+
87
+ // implement the RNE logic
88
+ bool round_up = false;
89
+
90
+ // if g == 0, round down (no-op)
91
+ if (g == 1) {
92
+ if ((r == 1) || (s == 1)) {
93
+ // round up
94
+ round_up = true;
95
+ } else {
96
+ if (lsb == 1) {
97
+ // round up
98
+ round_up = true;
99
+ }
100
+ // if lsb == 0, round down (no-op)
101
+ }
102
+ }
103
+
104
+ if (round_up) {
105
+ // adjust exponent
106
+ // note that if exponent was 255 we would have already returned earlier, so
107
+ // we know we can add one safely without running out of bounds
108
+ exponent++;
109
+ }
110
+
111
+ return exponent;
112
+ }
113
+
114
+ } // namespace detail
115
+
116
+ //------- the below is from c10/util/Float8_e8m0fnu-inl.h ------//
117
+ // TODO(#146647): Can we remove the below warning?
118
+ C10_CLANG_DIAGNOSTIC_PUSH()
119
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
120
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
121
+ #endif
122
+
123
+ /// Constructors
124
+ inline C10_HOST_DEVICE Float8_e8m0fnu::Float8_e8m0fnu(float value)
125
+ : x(detail::fp8e8m0fnu_from_fp32_value(value)) {}
126
+
127
+ /// Implicit conversions
128
+
129
+ inline C10_HOST_DEVICE Float8_e8m0fnu::operator float() const {
130
+ // TODO(#146647): maybe rewrite without control flow
131
+
132
+ // if exponent is zero, need to special case to return 2^-127 instead of zero
133
+ if (x == 0) {
134
+ return c10::detail::fp32_from_bits(0x00400000);
135
+ }
136
+
137
+ // if exponent is NaN, need to special case to return properly encoded NaN
138
+ if (isnan()) {
139
+ return c10::detail::fp32_from_bits(0x7f800001);
140
+ }
141
+
142
+ // leave sign at 0, set the exponent bits, leave stored mantissa at 0
143
+ uint32_t res = x << 23;
144
+
145
+ return c10::detail::fp32_from_bits(res);
146
+ }
147
+
148
+ /// Special values helper
149
+
150
+ inline C10_HOST_DEVICE bool Float8_e8m0fnu::isnan() const {
151
+ return x == 0b11111111;
152
+ }
153
+
154
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
155
+ /// conversion from c10::Float8_e8m0fnu to float.
156
+ C10_CLANG_DIAGNOSTIC_POP()
157
+
158
+ } // namespace c10
159
+
160
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
161
+ using c10::Float8_e8m0fnu;
162
+ using c10::operator<<;
163
+
164
+ namespace detail {
165
+ using c10::detail::fp8e8m0fnu_from_fp32_value;
166
+ } // namespace detail
167
+ HIDDEN_NAMESPACE_END(torch, headeronly)
168
+
169
+ namespace std {
170
+
171
+ template <>
172
+ class numeric_limits<c10::Float8_e8m0fnu> {
173
+ public:
174
+ static constexpr bool is_specialized = true;
175
+ static constexpr bool is_signed = false;
176
+ static constexpr bool is_integer = false;
177
+ static constexpr bool is_exact = false;
178
+ static constexpr bool has_infinity = false;
179
+ static constexpr bool has_quiet_NaN = true;
180
+ static constexpr bool has_signaling_NaN = false;
181
+ static constexpr auto has_denorm = false;
182
+ static constexpr auto has_denorm_loss = false;
183
+ static constexpr auto round_style = numeric_limits<float>::round_style;
184
+ static constexpr bool is_iec559 = false;
185
+ static constexpr bool is_bounded = true;
186
+ static constexpr bool is_modulo = false;
187
+ static constexpr int digits = 1;
188
+ static constexpr int digits10 = 0;
189
+ static constexpr int max_digits10 = 1; // just a 2!
190
+ static constexpr int radix = 2;
191
+ static constexpr int min_exponent = -126;
192
+ static constexpr int min_exponent10 = -38;
193
+ static constexpr int max_exponent = 128;
194
+ static constexpr int max_exponent10 = 38;
195
+ static constexpr auto traps = numeric_limits<float>::traps;
196
+ static constexpr auto tinyness_before = false;
197
+
198
+ static constexpr c10::Float8_e8m0fnu min() {
199
+ // 2^-127
200
+ return c10::Float8_e8m0fnu(0b00000000, c10::Float8_e8m0fnu::from_bits());
201
+ }
202
+ static constexpr c10::Float8_e8m0fnu lowest() {
203
+ // 2^-127
204
+ return c10::Float8_e8m0fnu(0b00000000, c10::Float8_e8m0fnu::from_bits());
205
+ }
206
+ static constexpr c10::Float8_e8m0fnu max() {
207
+ // 254 biased, which is 127 unbiased, so 2^127
208
+ return c10::Float8_e8m0fnu(0b11111110, c10::Float8_e8m0fnu::from_bits());
209
+ }
210
+ static constexpr c10::Float8_e8m0fnu epsilon() {
211
+ // according to https://en.cppreference.com/w/cpp/types/numeric_limits, this
212
+ // is "the difference between 1.0 and the next representable value of the
213
+ // given floating-point type". The next representable value is 2.0, so the
214
+ // difference is 1.0 which is 2^0. 0 unbiased is 127 biased.
215
+ return c10::Float8_e8m0fnu(0b01111111, c10::Float8_e8m0fnu::from_bits());
216
+ }
217
+ static constexpr c10::Float8_e8m0fnu round_error() {
218
+ // 0.5 in float, which is 2^-1, and -1 + 127 = 126
219
+ return c10::Float8_e8m0fnu(0b01111110, c10::Float8_e8m0fnu::from_bits());
220
+ }
221
+ static constexpr c10::Float8_e8m0fnu quiet_NaN() {
222
+ return c10::Float8_e8m0fnu(0b11111111, c10::Float8_e8m0fnu::from_bits());
223
+ }
224
+ };
225
+
226
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Float8_fnuz_cvt.h ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/floating_point_utils.h>
5
+
6
+ #include <cstdint>
7
+
8
+ #if defined(SYCL_LANGUAGE_VERSION)
9
+ #include <sycl/sycl.hpp>
10
+ #endif
11
+
12
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, detail)
13
+
14
+ /*
15
+ * Convert a 8-bit floating-point number in either f8 E4M3FNUZ or bf8 E5M2FNUZ
16
+ * format, in bit representation, to a 32-bit floating-point number.
17
+ */
18
+ template <uint32_t we, uint32_t wm>
19
+ inline C10_HOST_DEVICE float fp8_fnuz_to_fp32_value(uint8_t x) {
20
+ static_assert((we == 4 && wm == 3) || (we == 5 && wm == 2));
21
+ constexpr uint32_t weo = 8;
22
+ constexpr uint32_t wmo = 23;
23
+
24
+ if (x == 0) {
25
+ return 0;
26
+ }
27
+
28
+ if (x == 0x80) {
29
+ constexpr uint32_t ifNaN = 0x7F800001;
30
+ return fp32_from_bits(ifNaN);
31
+ }
32
+
33
+ uint32_t mantissa = x & ((1 << wm) - 1);
34
+ uint32_t exponent = (x & 0x7F) >> wm;
35
+
36
+ // subnormal input
37
+ if (exponent == 0) {
38
+ // guaranteed mantissa!=0 since cases 0x0 and 0x80 are handled above
39
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
40
+ uint32_t renorm_shift = __clz(mantissa);
41
+ #elif defined(__SYCL_DEVICE_ONLY__)
42
+ uint32_t renorm_shift = sycl::clz(mantissa);
43
+ #elif defined(_MSC_VER)
44
+ unsigned long nonsign_bsr;
45
+ _BitScanReverse(&nonsign_bsr, (unsigned long)mantissa);
46
+ uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
47
+ #else
48
+ uint32_t renorm_shift = __builtin_clz(mantissa);
49
+ #endif
50
+ uint32_t sh = 1 + renorm_shift - (32 - wm);
51
+ mantissa <<= sh;
52
+ exponent += 1 - sh;
53
+ mantissa &= ((1 << wm) - 1);
54
+ }
55
+
56
+ const uint32_t exp_low_cutoff = (1 << (weo - 1)) - (1 << (we - 1));
57
+ exponent += exp_low_cutoff - 1;
58
+ mantissa <<= wmo - wm;
59
+
60
+ uint32_t sign = x >> 7;
61
+ uint32_t retval = (sign << 31) | (exponent << 23) | mantissa;
62
+ return fp32_from_bits(retval);
63
+ }
64
+
65
+ HIDDEN_NAMESPACE_END(torch, headeronly, detail)
66
+
67
+ namespace c10::detail {
68
+ using torch::headeronly::detail::fp8_fnuz_to_fp32_value;
69
+ }
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Half.h ADDED
@@ -0,0 +1,788 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ /// Defines the Half type (half-precision floating-point) including conversions
4
+ /// to standard C types and basic arithmetic operations. Note that arithmetic
5
+ /// operations are implemented by converting to floating point and
6
+ /// performing the operation in float32, instead of using CUDA half intrinsics.
7
+ /// Most uses of this type within ATen are memory bound, including the
8
+ /// element-wise kernels, and the half intrinsics aren't efficient on all GPUs.
9
+ /// If you are writing a compute bound kernel, you can use the CUDA half
10
+ /// intrinsics directly on the Half type from device code.
11
+
12
+ #include <torch/headeronly/macros/Macros.h>
13
+ #include <torch/headeronly/util/bit_cast.h>
14
+ #include <torch/headeronly/util/floating_point_utils.h>
15
+
16
+ #if defined(__cplusplus)
17
+ #include <cmath>
18
+ #elif !defined(__OPENCL_VERSION__)
19
+ #include <math.h>
20
+ #endif
21
+
22
+ #ifdef _MSC_VER
23
+ #include <intrin.h>
24
+ #endif
25
+
26
+ #include <cstdint>
27
+ #include <cstring>
28
+ #include <ostream>
29
+
30
+ #ifdef __CUDACC__
31
+ #include <cuda_fp16.h>
32
+ #endif
33
+
34
+ #ifdef __HIPCC__
35
+ #include <hip/hip_fp16.h>
36
+ #endif
37
+
38
+ #if defined(CL_SYCL_LANGUAGE_VERSION)
39
+ #include <CL/sycl.hpp> // for SYCL 1.2.1
40
+ #elif defined(SYCL_LANGUAGE_VERSION)
41
+ #include <sycl/sycl.hpp> // for SYCL 2020
42
+ #endif
43
+
44
+ #if (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
45
+ !defined(__APPLE__)
46
+ #include <torch/headeronly/cpu/vec/vec_half.h>
47
+ #endif
48
+
49
+ #if defined(__aarch64__) && !defined(__CUDACC__)
50
+ #include <arm_neon.h>
51
+ #endif
52
+
53
+ #if defined(__GNUC__) || defined(__clang__)
54
+ #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || \
55
+ defined(_M_IX86)
56
+ #if defined(__F16C__) && \
57
+ !(defined(__CUDA_ARCH__) || defined(__CUDACC__) || \
58
+ defined(__HIP_DEVICE_COMPILE__))
59
+ #define C10_X86_F16 1
60
+ #include <immintrin.h> // import conversion ops from f16cintrin.h
61
+ #endif // defined(__F16C__) && !(defined(__CUDA_ARCH__) || defined(__CUDACC__)
62
+ // || defined(__HIP_DEVICE_COMPILE__))
63
+ #endif // __x86_64__ || _M_X64 || __i386 || _M_IX86
64
+ #endif // __GNUC__ || __clang__
65
+
66
+ namespace c10 {
67
+
68
+ struct alignas(2) Half {
69
+ unsigned short x;
70
+
71
+ struct from_bits_t {};
72
+ C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
73
+ return from_bits_t();
74
+ }
75
+
76
+ // HIP wants __host__ __device__ tag, CUDA does not
77
+ #if defined(USE_ROCM)
78
+ C10_HOST_DEVICE Half() = default;
79
+ #else
80
+ Half() = default;
81
+ #endif
82
+
83
+ constexpr C10_HOST_DEVICE Half(unsigned short bits, from_bits_t /*unused*/)
84
+ : x(bits) {}
85
+ #if defined(__aarch64__) && !defined(__CUDACC__)
86
+ inline Half(float16_t value);
87
+ inline operator float16_t() const;
88
+ #else
89
+ inline C10_HOST_DEVICE Half(float value);
90
+ inline C10_HOST_DEVICE operator float() const;
91
+ #endif
92
+
93
+ #if defined(__CUDACC__) || defined(__HIPCC__)
94
+ inline C10_HOST_DEVICE Half(const __half& value);
95
+ inline C10_HOST_DEVICE operator __half() const;
96
+ #endif
97
+ #ifdef SYCL_LANGUAGE_VERSION
98
+ inline C10_HOST_DEVICE Half(const sycl::half& value);
99
+ inline C10_HOST_DEVICE operator sycl::half() const;
100
+ #endif
101
+ };
102
+
103
+ inline std::ostream& operator<<(std::ostream& out, const Half& value) {
104
+ out << (float)value;
105
+ return out;
106
+ }
107
+
108
+ namespace detail {
109
+ /*
110
+ * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
111
+ * representation, to a 32-bit floating-point number in IEEE single-precision
112
+ * format.
113
+ *
114
+ * @note The implementation relies on IEEE-like (no assumption about rounding
115
+ * mode and no operations on denormals) floating-point operations and bitcasts
116
+ * between integer and floating-point variables.
117
+ */
118
+ C10_HOST_DEVICE inline float fp16_ieee_to_fp32_value(uint16_t h) {
119
+ #ifdef C10_X86_F16
120
+ return _cvtsh_ss(h);
121
+ #else
122
+ /*
123
+ * Extend the half-precision floating-point number to 32 bits and shift to the
124
+ * upper part of the 32-bit word:
125
+ * +---+-----+------------+-------------------+
126
+ * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
127
+ * +---+-----+------------+-------------------+
128
+ * Bits 31 26-30 16-25 0-15
129
+ *
130
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
131
+ * - zero bits.
132
+ */
133
+ const uint32_t w = (uint32_t)h << 16;
134
+ /*
135
+ * Extract the sign of the input number into the high bit of the 32-bit word:
136
+ *
137
+ * +---+----------------------------------+
138
+ * | S |0000000 00000000 00000000 00000000|
139
+ * +---+----------------------------------+
140
+ * Bits 31 0-31
141
+ */
142
+ const uint32_t sign = w & UINT32_C(0x80000000);
143
+ /*
144
+ * Extract mantissa and biased exponent of the input number into the high bits
145
+ * of the 32-bit word:
146
+ *
147
+ * +-----+------------+---------------------+
148
+ * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
149
+ * +-----+------------+---------------------+
150
+ * Bits 27-31 17-26 0-16
151
+ */
152
+ const uint32_t two_w = w + w;
153
+
154
+ /*
155
+ * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become
156
+ * mantissa and exponent of a single-precision floating-point number:
157
+ *
158
+ * S|Exponent | Mantissa
159
+ * +-+---+-----+------------+----------------+
160
+ * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
161
+ * +-+---+-----+------------+----------------+
162
+ * Bits | 23-31 | 0-22
163
+ *
164
+ * Next, there are some adjustments to the exponent:
165
+ * - The exponent needs to be corrected by the difference in exponent bias
166
+ * between single-precision and half-precision formats (0x7F - 0xF = 0x70)
167
+ * - Inf and NaN values in the inputs should become Inf and NaN values after
168
+ * conversion to the single-precision number. Therefore, if the biased
169
+ * exponent of the half-precision input was 0x1F (max possible value), the
170
+ * biased exponent of the single-precision output must be 0xFF (max possible
171
+ * value). We do this correction in two steps:
172
+ * - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset
173
+ * below) rather than by 0x70 suggested by the difference in the exponent bias
174
+ * (see above).
175
+ * - Then we multiply the single-precision result of exponent adjustment by
176
+ * 2**(-112) to reverse the effect of exponent adjustment by 0xE0 less the
177
+ * necessary exponent adjustment by 0x70 due to difference in exponent bias.
178
+ * The floating-point multiplication hardware would ensure than Inf and
179
+ * NaN would retain their value on at least partially IEEE754-compliant
180
+ * implementations.
181
+ *
182
+ * Note that the above operations do not handle denormal inputs (where biased
183
+ * exponent == 0). However, they also do not operate on denormal inputs, and
184
+ * do not produce denormal results.
185
+ */
186
+ constexpr uint32_t exp_offset = UINT32_C(0xE0) << 23;
187
+ // const float exp_scale = 0x1.0p-112f;
188
+ constexpr uint32_t scale_bits = (uint32_t)15 << 23;
189
+ float exp_scale_val = 0;
190
+ #if defined(_MSC_VER) && defined(__clang__)
191
+ __builtin_memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
192
+ #else
193
+ std::memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
194
+ #endif
195
+
196
+ const float exp_scale = exp_scale_val;
197
+ const float normalized_value =
198
+ fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
199
+
200
+ /*
201
+ * Convert denormalized half-precision inputs into single-precision results
202
+ * (always normalized). Zero inputs are also handled here.
203
+ *
204
+ * In a denormalized number the biased exponent is zero, and mantissa has
205
+ * on-zero bits. First, we shift mantissa into bits 0-9 of the 32-bit word.
206
+ *
207
+ * zeros | mantissa
208
+ * +---------------------------+------------+
209
+ * |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
210
+ * +---------------------------+------------+
211
+ * Bits 10-31 0-9
212
+ *
213
+ * Now, remember that denormalized half-precision numbers are represented as:
214
+ * FP16 = mantissa * 2**(-24).
215
+ * The trick is to construct a normalized single-precision number with the
216
+ * same mantissa and thehalf-precision input and with an exponent which would
217
+ * scale the corresponding mantissa bits to 2**(-24). A normalized
218
+ * single-precision floating-point number is represented as: FP32 = (1 +
219
+ * mantissa * 2**(-23)) * 2**(exponent - 127) Therefore, when the biased
220
+ * exponent is 126, a unit change in the mantissa of the input denormalized
221
+ * half-precision number causes a change of the constructed single-precision
222
+ * number by 2**(-24), i.e. the same amount.
223
+ *
224
+ * The last step is to adjust the bias of the constructed single-precision
225
+ * number. When the input half-precision number is zero, the constructed
226
+ * single-precision number has the value of FP32 = 1 * 2**(126 - 127) =
227
+ * 2**(-1) = 0.5 Therefore, we need to subtract 0.5 from the constructed
228
+ * single-precision number to get the numerical equivalent of the input
229
+ * half-precision number.
230
+ */
231
+ constexpr uint32_t magic_mask = UINT32_C(126) << 23;
232
+ constexpr float magic_bias = 0.5f;
233
+ const float denormalized_value =
234
+ fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
235
+
236
+ /*
237
+ * - Choose either results of conversion of input as a normalized number, or
238
+ * as a denormalized number, depending on the input exponent. The variable
239
+ * two_w contains input exponent in bits 27-31, therefore if its smaller than
240
+ * 2**27, the input is either a denormal number, or zero.
241
+ * - Combine the result of conversion of exponent and mantissa with the sign
242
+ * of the input number.
243
+ */
244
+ constexpr uint32_t denormalized_cutoff = UINT32_C(1) << 27;
245
+ const uint32_t result = sign |
246
+ (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value)
247
+ : fp32_to_bits(normalized_value));
248
+ return fp32_from_bits(result);
249
+ #endif // C10_X86_F16
250
+ }
251
+
252
+ /*
253
+ * Convert a 32-bit floating-point number in IEEE single-precision format to a
254
+ * 16-bit floating-point number in IEEE half-precision format, in bit
255
+ * representation.
256
+ *
257
+ * @note The implementation relies on IEEE-like (no assumption about rounding
258
+ * mode and no operations on denormals) floating-point operations and bitcasts
259
+ * between integer and floating-point variables.
260
+ */
261
+ inline uint16_t fp16_ieee_from_fp32_value(float f) {
262
+ #ifdef C10_X86_F16
263
+ return _cvtss_sh(f, _MM_FROUND_TO_NEAREST_INT);
264
+ #else
265
+ // const float scale_to_inf = 0x1.0p+112f;
266
+ // const float scale_to_zero = 0x1.0p-110f;
267
+ constexpr uint32_t scale_to_inf_bits = (uint32_t)239 << 23;
268
+ constexpr uint32_t scale_to_zero_bits = (uint32_t)17 << 23;
269
+ float scale_to_inf_val = 0, scale_to_zero_val = 0;
270
+ std::memcpy(&scale_to_inf_val, &scale_to_inf_bits, sizeof(scale_to_inf_val));
271
+ std::memcpy(
272
+ &scale_to_zero_val, &scale_to_zero_bits, sizeof(scale_to_zero_val));
273
+ const float scale_to_inf = scale_to_inf_val;
274
+ const float scale_to_zero = scale_to_zero_val;
275
+
276
+ #if defined(_MSC_VER) && _MSC_VER == 1916
277
+ float base = ((signbit(f) != 0 ? -f : f) * scale_to_inf) * scale_to_zero;
278
+ #else
279
+ float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
280
+ #endif
281
+
282
+ const uint32_t w = fp32_to_bits(f);
283
+ const uint32_t shl1_w = w + w;
284
+ const uint32_t sign = w & UINT32_C(0x80000000);
285
+ uint32_t bias = shl1_w & UINT32_C(0xFF000000);
286
+ if (bias < UINT32_C(0x71000000)) {
287
+ bias = UINT32_C(0x71000000);
288
+ }
289
+
290
+ base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
291
+ const uint32_t bits = fp32_to_bits(base);
292
+ const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
293
+ const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
294
+ const uint32_t nonsign = exp_bits + mantissa_bits;
295
+ return static_cast<uint16_t>(
296
+ (sign >> 16) |
297
+ (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign));
298
+ #endif // C10_X86_F16
299
+ }
300
+
301
+ /*
302
+ * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
303
+ * representation, to a 32-bit floating-point number in IEEE single-precision
304
+ * format, in bit representation.
305
+ *
306
+ * @note The implementation doesn't use any floating-point operations.
307
+ */
308
+ inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
309
+ /*
310
+ * Extend the half-precision floating-point number to 32 bits and shift to the
311
+ * upper part of the 32-bit word:
312
+ * +---+-----+------------+-------------------+
313
+ * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
314
+ * +---+-----+------------+-------------------+
315
+ * Bits 31 26-30 16-25 0-15
316
+ *
317
+ * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
318
+ * - zero bits.
319
+ */
320
+ const uint32_t w = (uint32_t)h << 16;
321
+ /*
322
+ * Extract the sign of the input number into the high bit of the 32-bit word:
323
+ *
324
+ * +---+----------------------------------+
325
+ * | S |0000000 00000000 00000000 00000000|
326
+ * +---+----------------------------------+
327
+ * Bits 31 0-31
328
+ */
329
+ const uint32_t sign = w & UINT32_C(0x80000000);
330
+ /*
331
+ * Extract mantissa and biased exponent of the input number into the bits 0-30
332
+ * of the 32-bit word:
333
+ *
334
+ * +---+-----+------------+-------------------+
335
+ * | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
336
+ * +---+-----+------------+-------------------+
337
+ * Bits 30 27-31 17-26 0-16
338
+ */
339
+ const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
340
+ /*
341
+ * Renorm shift is the number of bits to shift mantissa left to make the
342
+ * half-precision number normalized. If the initial number is normalized, some
343
+ * of its high 6 bits (sign == 0 and 5-bit exponent) equals one. In this case
344
+ * renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note
345
+ * that if we shift denormalized nonsign by renorm_shift, the unit bit of
346
+ * mantissa will shift into exponent, turning the biased exponent into 1, and
347
+ * making mantissa normalized (i.e. without leading 1).
348
+ */
349
+ #ifdef _MSC_VER
350
+ unsigned long nonsign_bsr;
351
+ _BitScanReverse(&nonsign_bsr, (unsigned long)nonsign);
352
+ uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
353
+ #else
354
+ uint32_t renorm_shift = __builtin_clz(nonsign);
355
+ #endif
356
+ renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
357
+ /*
358
+ * Iff half-precision number has exponent of 15, the addition overflows
359
+ * it into bit 31, and the subsequent shift turns the high 9 bits
360
+ * into 1. Thus inf_nan_mask == 0x7F800000 if the half-precision number
361
+ * had exponent of 15 (i.e. was NaN or infinity) 0x00000000 otherwise
362
+ */
363
+ const int32_t inf_nan_mask =
364
+ ((int32_t)(nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
365
+ /*
366
+ * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31
367
+ * into 1. Otherwise, bit 31 remains 0. The signed shift right by 31
368
+ * broadcasts bit 31 into all bits of the zero_mask. Thus zero_mask ==
369
+ * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
370
+ * 0x00000000 otherwise
371
+ */
372
+ const int32_t zero_mask = (int32_t)(nonsign - 1) >> 31;
373
+ /*
374
+ * 1. Shift nonsign left by renorm_shift to normalize it (if the input
375
+ * was denormal)
376
+ * 2. Shift nonsign right by 3 so the exponent (5 bits originally)
377
+ * becomes an 8-bit field and 10-bit mantissa shifts into the 10 high
378
+ * bits of the 23-bit mantissa of IEEE single-precision number.
379
+ * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the
380
+ * different in exponent bias (0x7F for single-precision number less 0xF
381
+ * for half-precision number).
382
+ * 4. Subtract renorm_shift from the exponent (starting at bit 23) to
383
+ * account for renormalization. As renorm_shift is less than 0x70, this
384
+ * can be combined with step 3.
385
+ * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the
386
+ * input was NaN or infinity.
387
+ * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent
388
+ * into zero if the input was zero.
389
+ * 7. Combine with the sign of the input number.
390
+ */
391
+ return sign |
392
+ ((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) |
393
+ inf_nan_mask) &
394
+ ~zero_mask);
395
+ }
396
+
397
+ #ifdef C10_X86_F16
398
+ #undef C10_X86_F16
399
+ #endif // C10_X86_F16
400
+
401
+ #if defined(__aarch64__) && !defined(__CUDACC__)
402
+ inline float16_t fp16_from_bits(uint16_t h) {
403
+ return c10::bit_cast<float16_t>(h);
404
+ }
405
+
406
+ inline uint16_t fp16_to_bits(float16_t f) {
407
+ return c10::bit_cast<uint16_t>(f);
408
+ }
409
+
410
+ // According to https://godbolt.org/z/frExdbsWG it would translate to single
411
+ // fcvt s0, h0
412
+ inline float native_fp16_to_fp32_value(uint16_t h) {
413
+ return static_cast<float>(fp16_from_bits(h));
414
+ }
415
+
416
+ inline uint16_t native_fp16_from_fp32_value(float f) {
417
+ return fp16_to_bits(static_cast<float16_t>(f));
418
+ }
419
+ #endif
420
+
421
+ } // namespace detail
422
+
423
+ //---------- below is copied from c10/util/Half-inl.h ----------------//
424
+ C10_CLANG_DIAGNOSTIC_PUSH()
425
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
426
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
427
+ #endif
428
+
429
+ #if defined(__aarch64__) && !defined(__CUDACC__)
430
+ /// Constructors
431
+ inline Half::Half(float16_t value) : x(detail::fp16_to_bits(value)) {}
432
+ inline Half::operator float16_t() const {
433
+ return detail::fp16_from_bits(x);
434
+ }
435
+ #else
436
+
437
+ inline C10_HOST_DEVICE Half::Half(float value)
438
+ :
439
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
440
+ x(__half_as_short(__float2half(value)))
441
+ #elif defined(__SYCL_DEVICE_ONLY__)
442
+ x(c10::bit_cast<uint16_t>(sycl::half(value)))
443
+ #elif (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
444
+ !defined(__APPLE__)
445
+ x(at::vec::float2half_scalar(value))
446
+ #else
447
+ x(detail::fp16_ieee_from_fp32_value(value))
448
+ #endif
449
+ {
450
+ }
451
+
452
+ /// Implicit conversions
453
+
454
+ inline C10_HOST_DEVICE Half::operator float() const {
455
+ #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
456
+ return __half2float(*reinterpret_cast<const __half*>(&x));
457
+ #elif defined(__SYCL_DEVICE_ONLY__)
458
+ return float(c10::bit_cast<sycl::half>(x));
459
+ #elif (defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_AVX512)) && \
460
+ !defined(__APPLE__)
461
+ return at::vec::half2float_scalar(x);
462
+ #elif defined(__aarch64__) && !defined(__CUDACC__)
463
+ return detail::native_fp16_to_fp32_value(x);
464
+ #else
465
+ return detail::fp16_ieee_to_fp32_value(x);
466
+ #endif
467
+ }
468
+
469
+ #endif /* !defined(__aarch64__) || defined(__CUDACC__) \
470
+ */
471
+
472
+ #if defined(__CUDACC__) || defined(__HIPCC__)
473
+ inline C10_HOST_DEVICE Half::Half(const __half& value) {
474
+ x = *reinterpret_cast<const unsigned short*>(&value);
475
+ }
476
+ inline C10_HOST_DEVICE Half::operator __half() const {
477
+ return *reinterpret_cast<const __half*>(&x);
478
+ }
479
+ #endif
480
+
481
+ #ifdef SYCL_LANGUAGE_VERSION
482
+ inline C10_HOST_DEVICE Half::Half(const sycl::half& value) {
483
+ x = *reinterpret_cast<const unsigned short*>(&value);
484
+ }
485
+ inline C10_HOST_DEVICE Half::operator sycl::half() const {
486
+ return *reinterpret_cast<const sycl::half*>(&x);
487
+ }
488
+ #endif
489
+
490
+ // CUDA intrinsics
491
+
492
+ #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 350)) || \
493
+ (defined(__clang__) && defined(__CUDA__))
494
+ inline __device__ Half __ldg(const Half* ptr) {
495
+ return __ldg(reinterpret_cast<const __half*>(ptr));
496
+ }
497
+ #endif
498
+
499
+ /// Arithmetic
500
+
501
+ inline C10_HOST_DEVICE Half operator+(const Half& a, const Half& b) {
502
+ return static_cast<float>(a) + static_cast<float>(b);
503
+ }
504
+
505
+ inline C10_HOST_DEVICE Half operator-(const Half& a, const Half& b) {
506
+ return static_cast<float>(a) - static_cast<float>(b);
507
+ }
508
+
509
+ inline C10_HOST_DEVICE Half operator*(const Half& a, const Half& b) {
510
+ return static_cast<float>(a) * static_cast<float>(b);
511
+ }
512
+
513
+ inline C10_HOST_DEVICE Half operator/(const Half& a, const Half& b)
514
+ __ubsan_ignore_float_divide_by_zero__ {
515
+ return static_cast<float>(a) / static_cast<float>(b);
516
+ }
517
+
518
+ inline C10_HOST_DEVICE Half operator-(const Half& a) {
519
+ #if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530) || \
520
+ defined(__HIP_DEVICE_COMPILE__)
521
+ return __hneg(a);
522
+ #elif defined(__SYCL_DEVICE_ONLY__)
523
+ return -c10::bit_cast<sycl::half>(a);
524
+ #else
525
+ return -static_cast<float>(a);
526
+ #endif
527
+ }
528
+
529
+ inline C10_HOST_DEVICE Half& operator+=(Half& a, const Half& b) {
530
+ a = a + b;
531
+ return a;
532
+ }
533
+
534
+ inline C10_HOST_DEVICE Half& operator-=(Half& a, const Half& b) {
535
+ a = a - b;
536
+ return a;
537
+ }
538
+
539
+ inline C10_HOST_DEVICE Half& operator*=(Half& a, const Half& b) {
540
+ a = a * b;
541
+ return a;
542
+ }
543
+
544
+ inline C10_HOST_DEVICE Half& operator/=(Half& a, const Half& b) {
545
+ a = a / b;
546
+ return a;
547
+ }
548
+
549
+ /// Arithmetic with floats
550
+
551
+ inline C10_HOST_DEVICE float operator+(Half a, float b) {
552
+ return static_cast<float>(a) + b;
553
+ }
554
+ inline C10_HOST_DEVICE float operator-(Half a, float b) {
555
+ return static_cast<float>(a) - b;
556
+ }
557
+ inline C10_HOST_DEVICE float operator*(Half a, float b) {
558
+ return static_cast<float>(a) * b;
559
+ }
560
+ inline C10_HOST_DEVICE float operator/(Half a, float b)
561
+ __ubsan_ignore_float_divide_by_zero__ {
562
+ return static_cast<float>(a) / b;
563
+ }
564
+
565
+ inline C10_HOST_DEVICE float operator+(float a, Half b) {
566
+ return a + static_cast<float>(b);
567
+ }
568
+ inline C10_HOST_DEVICE float operator-(float a, Half b) {
569
+ return a - static_cast<float>(b);
570
+ }
571
+ inline C10_HOST_DEVICE float operator*(float a, Half b) {
572
+ return a * static_cast<float>(b);
573
+ }
574
+ inline C10_HOST_DEVICE float operator/(float a, Half b)
575
+ __ubsan_ignore_float_divide_by_zero__ {
576
+ return a / static_cast<float>(b);
577
+ }
578
+
579
+ inline C10_HOST_DEVICE float& operator+=(float& a, const Half& b) {
580
+ return a += static_cast<float>(b);
581
+ }
582
+ inline C10_HOST_DEVICE float& operator-=(float& a, const Half& b) {
583
+ return a -= static_cast<float>(b);
584
+ }
585
+ inline C10_HOST_DEVICE float& operator*=(float& a, const Half& b) {
586
+ return a *= static_cast<float>(b);
587
+ }
588
+ inline C10_HOST_DEVICE float& operator/=(float& a, const Half& b) {
589
+ return a /= static_cast<float>(b);
590
+ }
591
+
592
+ /// Arithmetic with doubles
593
+
594
+ inline C10_HOST_DEVICE double operator+(Half a, double b) {
595
+ return static_cast<double>(a) + b;
596
+ }
597
+ inline C10_HOST_DEVICE double operator-(Half a, double b) {
598
+ return static_cast<double>(a) - b;
599
+ }
600
+ inline C10_HOST_DEVICE double operator*(Half a, double b) {
601
+ return static_cast<double>(a) * b;
602
+ }
603
+ inline C10_HOST_DEVICE double operator/(Half a, double b)
604
+ __ubsan_ignore_float_divide_by_zero__ {
605
+ return static_cast<double>(a) / b;
606
+ }
607
+
608
+ inline C10_HOST_DEVICE double operator+(double a, Half b) {
609
+ return a + static_cast<double>(b);
610
+ }
611
+ inline C10_HOST_DEVICE double operator-(double a, Half b) {
612
+ return a - static_cast<double>(b);
613
+ }
614
+ inline C10_HOST_DEVICE double operator*(double a, Half b) {
615
+ return a * static_cast<double>(b);
616
+ }
617
+ inline C10_HOST_DEVICE double operator/(double a, Half b)
618
+ __ubsan_ignore_float_divide_by_zero__ {
619
+ return a / static_cast<double>(b);
620
+ }
621
+
622
+ /// Arithmetic with ints
623
+
624
+ inline C10_HOST_DEVICE Half operator+(Half a, int b) {
625
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
626
+ return a + static_cast<Half>(b);
627
+ }
628
+ inline C10_HOST_DEVICE Half operator-(Half a, int b) {
629
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
630
+ return a - static_cast<Half>(b);
631
+ }
632
+ inline C10_HOST_DEVICE Half operator*(Half a, int b) {
633
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
634
+ return a * static_cast<Half>(b);
635
+ }
636
+ inline C10_HOST_DEVICE Half operator/(Half a, int b) {
637
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
638
+ return a / static_cast<Half>(b);
639
+ }
640
+
641
+ inline C10_HOST_DEVICE Half operator+(int a, Half b) {
642
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
643
+ return static_cast<Half>(a) + b;
644
+ }
645
+ inline C10_HOST_DEVICE Half operator-(int a, Half b) {
646
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
647
+ return static_cast<Half>(a) - b;
648
+ }
649
+ inline C10_HOST_DEVICE Half operator*(int a, Half b) {
650
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
651
+ return static_cast<Half>(a) * b;
652
+ }
653
+ inline C10_HOST_DEVICE Half operator/(int a, Half b) {
654
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
655
+ return static_cast<Half>(a) / b;
656
+ }
657
+
658
+ //// Arithmetic with int64_t
659
+
660
+ inline C10_HOST_DEVICE Half operator+(Half a, int64_t b) {
661
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
662
+ return a + static_cast<Half>(b);
663
+ }
664
+ inline C10_HOST_DEVICE Half operator-(Half a, int64_t b) {
665
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
666
+ return a - static_cast<Half>(b);
667
+ }
668
+ inline C10_HOST_DEVICE Half operator*(Half a, int64_t b) {
669
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
670
+ return a * static_cast<Half>(b);
671
+ }
672
+ inline C10_HOST_DEVICE Half operator/(Half a, int64_t b) {
673
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
674
+ return a / static_cast<Half>(b);
675
+ }
676
+
677
+ inline C10_HOST_DEVICE Half operator+(int64_t a, Half b) {
678
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
679
+ return static_cast<Half>(a) + b;
680
+ }
681
+ inline C10_HOST_DEVICE Half operator-(int64_t a, Half b) {
682
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
683
+ return static_cast<Half>(a) - b;
684
+ }
685
+ inline C10_HOST_DEVICE Half operator*(int64_t a, Half b) {
686
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
687
+ return static_cast<Half>(a) * b;
688
+ }
689
+ inline C10_HOST_DEVICE Half operator/(int64_t a, Half b) {
690
+ // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
691
+ return static_cast<Half>(a) / b;
692
+ }
693
+
694
+ /// NOTE: we do not define comparisons directly and instead rely on the implicit
695
+ /// conversion from c10::Half to float.
696
+
697
+ C10_CLANG_DIAGNOSTIC_POP()
698
+
699
+ } // namespace c10
700
+
701
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
702
+
703
+ using c10::Half;
704
+ using c10::operator+;
705
+ using c10::operator-;
706
+ using c10::operator*;
707
+ using c10::operator/;
708
+ using c10::operator+=;
709
+ using c10::operator-=;
710
+ using c10::operator*=;
711
+ using c10::operator/=;
712
+ using c10::operator<<;
713
+
714
+ namespace detail {
715
+ #if defined(__aarch64__) && !defined(__CUDACC__)
716
+ using c10::detail::fp16_from_bits;
717
+ using c10::detail::fp16_to_bits;
718
+ using c10::detail::native_fp16_from_fp32_value;
719
+ using c10::detail::native_fp16_to_fp32_value;
720
+ #endif
721
+
722
+ using c10::detail::fp16_ieee_from_fp32_value;
723
+ using c10::detail::fp16_ieee_to_fp32_bits;
724
+ using c10::detail::fp16_ieee_to_fp32_value;
725
+ } // namespace detail
726
+
727
+ HIDDEN_NAMESPACE_END(torch, headeronly)
728
+
729
+ namespace std {
730
+
731
+ template <>
732
+ class numeric_limits<c10::Half> {
733
+ public:
734
+ static constexpr bool is_specialized = true;
735
+ static constexpr bool is_signed = true;
736
+ static constexpr bool is_integer = false;
737
+ static constexpr bool is_exact = false;
738
+ static constexpr bool has_infinity = true;
739
+ static constexpr bool has_quiet_NaN = true;
740
+ static constexpr bool has_signaling_NaN = true;
741
+ static constexpr auto has_denorm = numeric_limits<float>::has_denorm;
742
+ static constexpr auto has_denorm_loss =
743
+ numeric_limits<float>::has_denorm_loss;
744
+ static constexpr auto round_style = numeric_limits<float>::round_style;
745
+ static constexpr bool is_iec559 = true;
746
+ static constexpr bool is_bounded = true;
747
+ static constexpr bool is_modulo = false;
748
+ static constexpr int digits = 11;
749
+ static constexpr int digits10 = 3;
750
+ static constexpr int max_digits10 = 5;
751
+ static constexpr int radix = 2;
752
+ static constexpr int min_exponent = -13;
753
+ static constexpr int min_exponent10 = -4;
754
+ static constexpr int max_exponent = 16;
755
+ static constexpr int max_exponent10 = 4;
756
+ static constexpr auto traps = numeric_limits<float>::traps;
757
+ static constexpr auto tinyness_before =
758
+ numeric_limits<float>::tinyness_before;
759
+ static constexpr c10::Half min() {
760
+ return c10::Half(0x0400, c10::Half::from_bits());
761
+ }
762
+ static constexpr c10::Half lowest() {
763
+ return c10::Half(0xFBFF, c10::Half::from_bits());
764
+ }
765
+ static constexpr c10::Half max() {
766
+ return c10::Half(0x7BFF, c10::Half::from_bits());
767
+ }
768
+ static constexpr c10::Half epsilon() {
769
+ return c10::Half(0x1400, c10::Half::from_bits());
770
+ }
771
+ static constexpr c10::Half round_error() {
772
+ return c10::Half(0x3800, c10::Half::from_bits());
773
+ }
774
+ static constexpr c10::Half infinity() {
775
+ return c10::Half(0x7C00, c10::Half::from_bits());
776
+ }
777
+ static constexpr c10::Half quiet_NaN() {
778
+ return c10::Half(0x7E00, c10::Half::from_bits());
779
+ }
780
+ static constexpr c10::Half signaling_NaN() {
781
+ return c10::Half(0x7D00, c10::Half::from_bits());
782
+ }
783
+ static constexpr c10::Half denorm_min() {
784
+ return c10::Half(0x0001, c10::Half::from_bits());
785
+ }
786
+ };
787
+
788
+ } // namespace std
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/HeaderOnlyArrayRef.h ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/Exception.h>
5
+
6
+ #include <algorithm>
7
+ #include <array>
8
+ #include <cstddef>
9
+ #include <functional>
10
+ #include <initializer_list>
11
+ #include <iterator>
12
+ #include <type_traits>
13
+ #include <vector>
14
+
15
+ namespace c10 {
16
+
17
+ /// HeaderOnlyArrayRef - A subset of ArrayRef that is implemented only
18
+ /// in headers. This will be a base class from which ArrayRef inherits, so that
19
+ /// we can keep much of the implementation shared.
20
+ ///
21
+ /// [HeaderOnlyArrayRef vs ArrayRef note]
22
+ /// As HeaderOnlyArrayRef is a subset of ArrayRef, it has slightly less
23
+ /// functionality than ArrayRef. We document the minor differences below:
24
+ /// 1. ArrayRef has an extra convenience constructor for SmallVector.
25
+ /// 2. ArrayRef uses TORCH_CHECK. HeaderOnlyArrayRef uses header-only
26
+ /// STD_TORCH_CHECK, which will output a std::runtime_error vs a
27
+ /// c10::Error. Consequently, you should use ArrayRef when possible
28
+ /// and HeaderOnlyArrayRef only when necessary to support headeronly code.
29
+ /// In all other aspects, HeaderOnlyArrayRef is identical to ArrayRef, with the
30
+ /// positive benefit of being header-only and thus independent of libtorch.so.
31
+ template <typename T>
32
+ class HeaderOnlyArrayRef {
33
+ public:
34
+ using iterator = const T*;
35
+ using const_iterator = const T*;
36
+ using size_type = size_t;
37
+ using value_type = T;
38
+
39
+ using reverse_iterator = std::reverse_iterator<iterator>;
40
+
41
+ protected:
42
+ /// The start of the array, in an external buffer.
43
+ const T* Data;
44
+
45
+ /// The number of elements.
46
+ size_type Length;
47
+
48
+ public:
49
+ /// @name Constructors
50
+ /// @{
51
+
52
+ /// Construct an empty HeaderOnlyArrayRef.
53
+ /* implicit */ constexpr HeaderOnlyArrayRef() : Data(nullptr), Length(0) {}
54
+
55
+ /// Construct a HeaderOnlyArrayRef from a single element.
56
+ // TODO Make this explicit
57
+ constexpr HeaderOnlyArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
58
+
59
+ /// Construct a HeaderOnlyArrayRef from a pointer and length.
60
+ constexpr HeaderOnlyArrayRef(const T* data, size_t length)
61
+ : Data(data), Length(length) {}
62
+
63
+ /// Construct a HeaderOnlyArrayRef from a range.
64
+ constexpr HeaderOnlyArrayRef(const T* begin, const T* end)
65
+ : Data(begin), Length(end - begin) {}
66
+
67
+ template <
68
+ typename Container,
69
+ typename U = decltype(std::declval<Container>().data()),
70
+ typename = std::enable_if_t<
71
+ (std::is_same_v<U, T*> || std::is_same_v<U, T const*>)>>
72
+ /* implicit */ HeaderOnlyArrayRef(const Container& container)
73
+ : Data(container.data()), Length(container.size()) {}
74
+
75
+ /// Construct a HeaderOnlyArrayRef from a std::vector.
76
+ // The enable_if stuff here makes sure that this isn't used for
77
+ // std::vector<bool>, because ArrayRef can't work on a std::vector<bool>
78
+ // bitfield.
79
+ template <typename A>
80
+ /* implicit */ HeaderOnlyArrayRef(const std::vector<T, A>& Vec)
81
+ : Data(Vec.data()), Length(Vec.size()) {
82
+ static_assert(
83
+ !std::is_same_v<T, bool>,
84
+ "HeaderOnlyArrayRef<bool> cannot be constructed from a std::vector<bool> bitfield.");
85
+ }
86
+
87
+ /// Construct a HeaderOnlyArrayRef from a std::array
88
+ template <size_t N>
89
+ /* implicit */ constexpr HeaderOnlyArrayRef(const std::array<T, N>& Arr)
90
+ : Data(Arr.data()), Length(N) {}
91
+
92
+ /// Construct a HeaderOnlyArrayRef from a C array.
93
+ template <size_t N>
94
+ // NOLINTNEXTLINE(*c-arrays*)
95
+ /* implicit */ constexpr HeaderOnlyArrayRef(const T (&Arr)[N])
96
+ : Data(Arr), Length(N) {}
97
+
98
+ /// Construct a HeaderOnlyArrayRef from a std::initializer_list.
99
+ /* implicit */ constexpr HeaderOnlyArrayRef(
100
+ const std::initializer_list<T>& Vec)
101
+ : Data(
102
+ std::begin(Vec) == std::end(Vec) ? static_cast<T*>(nullptr)
103
+ : std::begin(Vec)),
104
+ Length(Vec.size()) {}
105
+
106
+ /// @}
107
+ /// @name Simple Operations
108
+ /// @{
109
+
110
+ constexpr iterator begin() const {
111
+ return this->Data;
112
+ }
113
+ constexpr iterator end() const {
114
+ return this->Data + this->Length;
115
+ }
116
+
117
+ // These are actually the same as iterator, since ArrayRef only
118
+ // gives you const iterators.
119
+ constexpr const_iterator cbegin() const {
120
+ return this->Data;
121
+ }
122
+ constexpr const_iterator cend() const {
123
+ return this->Data + this->Length;
124
+ }
125
+
126
+ constexpr reverse_iterator rbegin() const {
127
+ return reverse_iterator(end());
128
+ }
129
+ constexpr reverse_iterator rend() const {
130
+ return reverse_iterator(begin());
131
+ }
132
+
133
+ /// Check if all elements in the array satisfy the given expression
134
+ constexpr bool allMatch(const std::function<bool(const T&)>& pred) const {
135
+ return std::all_of(cbegin(), cend(), pred);
136
+ }
137
+
138
+ /// empty - Check if the array is empty.
139
+ constexpr bool empty() const {
140
+ return this->Length == 0;
141
+ }
142
+
143
+ constexpr const T* data() const {
144
+ return this->Data;
145
+ }
146
+
147
+ /// size - Get the array size.
148
+ constexpr size_t size() const {
149
+ return this->Length;
150
+ }
151
+
152
+ /// front - Get the first element.
153
+ constexpr const T& front() const {
154
+ STD_TORCH_CHECK(
155
+ !this->empty(),
156
+ "HeaderOnlyArrayRef: attempted to access front() of empty list");
157
+ return this->Data[0];
158
+ }
159
+
160
+ /// back - Get the last element.
161
+ constexpr const T& back() const {
162
+ STD_TORCH_CHECK(
163
+ !this->empty(),
164
+ "HeaderOnlyArrayRef: attempted to access back() of empty list");
165
+ return this->Data[this->Length - 1];
166
+ }
167
+
168
+ /// equals - Check for element-wise equality.
169
+ constexpr bool equals(HeaderOnlyArrayRef RHS) const {
170
+ return this->Length == RHS.Length &&
171
+ std::equal(begin(), end(), RHS.begin());
172
+ }
173
+
174
+ /// slice(n, m) - Take M elements of the array starting at element N
175
+ constexpr HeaderOnlyArrayRef<T> slice(size_t N, size_t M) const {
176
+ STD_TORCH_CHECK(
177
+ N + M <= this->size(),
178
+ "HeaderOnlyArrayRef: invalid slice, N = ",
179
+ N,
180
+ "; M = ",
181
+ M,
182
+ "; size = ",
183
+ this->size());
184
+ return HeaderOnlyArrayRef<T>(this->data() + N, M);
185
+ }
186
+
187
+ /// slice(n) - Chop off the first N elements of the array.
188
+ constexpr HeaderOnlyArrayRef<T> slice(size_t N) const {
189
+ STD_TORCH_CHECK(
190
+ N <= this->size(),
191
+ "HeaderOnlyArrayRef: invalid slice, N = ",
192
+ N,
193
+ "; size = ",
194
+ this->size());
195
+ return slice(N, this->size() - N);
196
+ }
197
+
198
+ /// @}
199
+ /// @name Operator Overloads
200
+ /// @{
201
+ constexpr const T& operator[](size_t Index) const {
202
+ return this->Data[Index];
203
+ }
204
+
205
+ /// Vector compatibility
206
+ constexpr const T& at(size_t Index) const {
207
+ STD_TORCH_CHECK(
208
+ Index < this->Length,
209
+ "HeaderOnlyArrayRef: invalid index Index = ",
210
+ Index,
211
+ "; Length = ",
212
+ this->Length);
213
+ return this->Data[Index];
214
+ }
215
+
216
+ /// Disallow accidental assignment from a temporary.
217
+ ///
218
+ /// The declaration here is extra complicated so that "arrayRef = {}"
219
+ /// continues to select the move assignment operator.
220
+ template <typename U>
221
+ std::enable_if_t<std::is_same_v<U, T>, HeaderOnlyArrayRef<T>>& operator=(
222
+ // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
223
+ U&& Temporary) = delete;
224
+
225
+ /// Disallow accidental assignment from a temporary.
226
+ ///
227
+ /// The declaration here is extra complicated so that "arrayRef = {}"
228
+ /// continues to select the move assignment operator.
229
+ template <typename U>
230
+ std::enable_if_t<std::is_same_v<U, T>, HeaderOnlyArrayRef<T>>& operator=(
231
+ std::initializer_list<U>) = delete;
232
+
233
+ /// @}
234
+ /// @name Expensive Operations
235
+ /// @{
236
+ std::vector<T> vec() const {
237
+ return std::vector<T>(this->Data, this->Data + this->Length);
238
+ }
239
+
240
+ /// @}
241
+ };
242
+
243
+ } // namespace c10
244
+
245
+ namespace torch::headeronly {
246
+ using c10::HeaderOnlyArrayRef;
247
+ using IntHeaderOnlyArrayRef = HeaderOnlyArrayRef<int64_t>;
248
+ } // namespace torch::headeronly
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/Metaprogramming.h ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/TypeList.h>
5
+ #include <type_traits>
6
+
7
+ namespace c10::guts {
8
+
9
+ /**
10
+ * Access information about result type or arguments from a function type.
11
+ * Example:
12
+ * using A = function_traits<int (float, double)>::return_type // A == int
13
+ * using A = function_traits<int (float, double)>::parameter_types::tuple_type
14
+ * // A == tuple<float, double>
15
+ */
16
+ template <class Func>
17
+ struct function_traits {
18
+ static_assert(
19
+ !std::is_same_v<Func, Func>,
20
+ "In function_traits<Func>, Func must be a plain function type.");
21
+ };
22
+ template <class Result, class... Args>
23
+ struct function_traits<Result(Args...)> {
24
+ using func_type = Result(Args...);
25
+ using return_type = Result;
26
+ using parameter_types = typelist::typelist<Args...>;
27
+ static constexpr auto number_of_parameters = sizeof...(Args);
28
+ };
29
+
30
+ /**
31
+ * infer_function_traits: creates a `function_traits` type for a simple
32
+ * function (pointer) or functor (lambda/struct). Currently does not support
33
+ * class methods.
34
+ */
35
+
36
+ template <typename Functor>
37
+ struct infer_function_traits {
38
+ using type = function_traits<
39
+ c10::guts::detail::strip_class_t<decltype(&Functor::operator())>>;
40
+ };
41
+
42
+ template <typename Result, typename... Args>
43
+ struct infer_function_traits<Result (*)(Args...)> {
44
+ using type = function_traits<Result(Args...)>;
45
+ };
46
+
47
+ template <typename Result, typename... Args>
48
+ struct infer_function_traits<Result(Args...)> {
49
+ using type = function_traits<Result(Args...)>;
50
+ };
51
+
52
+ template <typename T>
53
+ using infer_function_traits_t = typename infer_function_traits<T>::type;
54
+
55
+ /**
56
+ * make_function_traits: creates a `function_traits` type given a Return type
57
+ * and a typelist of Argument types
58
+ *
59
+ * Example:
60
+ * bool f(int, int);
61
+ *
62
+ * infer_function_traits_t<f> == make_function_traits_t<bool,
63
+ * typelist::typelist<int, int>>
64
+ */
65
+ template <typename Result, typename ArgList>
66
+ struct make_function_traits {
67
+ static_assert(
68
+ false_t<ArgList>::value,
69
+ "In guts::make_function_traits<Result, TypeList>, the ArgList argument must be typelist<...>.");
70
+ };
71
+
72
+ template <typename Result, typename... Args>
73
+ struct make_function_traits<Result, typelist::typelist<Args...>> {
74
+ using type = function_traits<Result(Args...)>;
75
+ };
76
+
77
+ template <typename Result, typename ArgList>
78
+ using make_function_traits_t =
79
+ typename make_function_traits<Result, ArgList>::type;
80
+
81
+ /**
82
+ * make_offset_index_sequence<Start, N>
83
+ * Like make_index_sequence<N>, but starting from Start instead of 0.
84
+ *
85
+ * Example:
86
+ * make_offset_index_sequence<10, 3> == std::index_sequence<10, 11, 12>
87
+ */
88
+ template <size_t Start, size_t N, size_t... Is>
89
+ struct make_offset_index_sequence_impl
90
+ : make_offset_index_sequence_impl<Start, N - 1, Start + N - 1, Is...> {
91
+ static_assert(
92
+ static_cast<int>(Start) >= 0,
93
+ "make_offset_index_sequence: Start < 0");
94
+ static_assert(static_cast<int>(N) >= 0, "make_offset_index_sequence: N < 0");
95
+ };
96
+
97
+ template <size_t Start, size_t... Is>
98
+ struct make_offset_index_sequence_impl<Start, 0, Is...> {
99
+ typedef std::index_sequence<Is...> type;
100
+ };
101
+
102
+ template <size_t Start, size_t N>
103
+ using make_offset_index_sequence =
104
+ typename make_offset_index_sequence_impl<Start, N>::type;
105
+
106
+ /**
107
+ * Use tuple_elements to extract a position-indexed subset of elements
108
+ * from the argument tuple into a result tuple.
109
+ *
110
+ * Example:
111
+ * std::tuple<int, const char*, double> t = std::make_tuple(0, "HEY", 2.0);
112
+ * std::tuple<int, double> result = tuple_elements(t, std::index_sequence<0,
113
+ * 2>());
114
+ */
115
+ template <class Tuple, size_t... Is>
116
+ constexpr auto tuple_elements(Tuple t, std::index_sequence<Is...> /*unused*/) {
117
+ return std::tuple<std::tuple_element_t<Is, Tuple>...>(std::get<Is>(t)...);
118
+ }
119
+
120
+ /**
121
+ * Use tuple_take to extract the first or last n elements from the argument
122
+ * tuple into a result tuple.
123
+ *
124
+ * Example:
125
+ * std::tuple<int, const char*, double> t = std::make_tuple(0, "HEY", 2.0);
126
+ * std::tuple<int, const char*> first_two = tuple_take<decltype(t), 2>(t);
127
+ * std::tuple<const char*, double> last_two = tuple_take<decltype(t), -2>(t);
128
+ */
129
+ template <class Tuple, int N, class Enable = void>
130
+ struct TupleTake {};
131
+
132
+ template <class Tuple, int N>
133
+ struct TupleTake<Tuple, N, std::enable_if_t<N >= 0, void>> {
134
+ static auto call(Tuple t) {
135
+ constexpr size_t size = std::tuple_size<Tuple>();
136
+ static_assert(N <= size, "tuple_take: N > size");
137
+ return tuple_elements(t, std::make_index_sequence<N>{});
138
+ }
139
+ };
140
+
141
+ template <class Tuple, int N>
142
+ struct TupleTake < Tuple,
143
+ N, std::enable_if_t<N<0, void>> {
144
+ static auto call(Tuple t) {
145
+ constexpr size_t size = std::tuple_size<Tuple>();
146
+ static_assert(-N <= size, "tuple_take: -N > size");
147
+ return tuple_elements(t, make_offset_index_sequence<size + N, -N>{});
148
+ }
149
+ };
150
+
151
+ template <class Tuple, int N>
152
+ auto tuple_take(Tuple t) {
153
+ return TupleTake<Tuple, N>::call(t);
154
+ }
155
+
156
+ /**
157
+ * Use tuple_slice to extract a contiguous subtuple from the argument.
158
+ *
159
+ * Example:
160
+ * std::tuple<int, const char*, double, bool> t = std::make_tuple(0,
161
+ * "HEY", 2.0, false); std::tuple<int, const char*> middle_two =
162
+ * tuple_slice<decltype(t), 1, 2>(t);
163
+ */
164
+ template <class Tuple, size_t Start, size_t N>
165
+ constexpr auto tuple_slice(Tuple t) {
166
+ constexpr size_t size = std::tuple_size<Tuple>();
167
+ static_assert(Start + N <= size, "tuple_slice: Start + N > size");
168
+ return tuple_elements(t, make_offset_index_sequence<Start, N>{});
169
+ }
170
+
171
+ /**
172
+ * Use tuple_map to run a mapping function over a tuple to get a new tuple.
173
+ *
174
+ * Example 1:
175
+ * auto result = tuple_map(std::tuple<int32_t, int32_t, int32_t>(3, 4, 5), []
176
+ * (int32_t a) -> int16_t {return a+1;});
177
+ * // result == std::tuple<int16_t, int16_t, int16_t>(4, 5, 6)
178
+ *
179
+ * Example 2:
180
+ * struct Mapper {
181
+ * std::string operator()(int32_t a) const {
182
+ * return std::to_string(a);
183
+ * }
184
+ * int64_t operator()(const std::string& a) const {
185
+ * return atoi(a.c_str());
186
+ * }
187
+ * };
188
+ * auto result = tuple_map(std::tuple<int32_t, std::string>(3, "4"),
189
+ * Mapper());
190
+ * // result == std::tuple<std::string, int64_t>("3", 4)
191
+ *
192
+ * Example 3:
193
+ * struct A final {
194
+ * int32_t func() {
195
+ * return 5;
196
+ * }
197
+ * };
198
+ * struct B final {
199
+ * std::string func() {
200
+ * return "5";
201
+ * }
202
+ * };
203
+ * auto result = tuple_map(std::make_tuple(A(), B()), [] (auto a) { return
204
+ * a.func(); });
205
+ * // result == std::tuple<int32_t, std::string>(5, "5");
206
+ */
207
+ namespace detail {
208
+ template <class Mapper, class... Args, size_t... Indices>
209
+ auto tuple_map(
210
+ // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
211
+ std::tuple<Args...>&& tuple,
212
+ const Mapper& mapper,
213
+ std::index_sequence<Indices...> /*unused*/) {
214
+ return std::tuple<decltype(mapper(std::forward<Args>(std::get<Indices>(
215
+ tuple))))...>(mapper(std::forward<Args>(std::get<Indices>(tuple)))...);
216
+ }
217
+ } // namespace detail
218
+
219
+ template <class Mapper, class... Args>
220
+ auto tuple_map(std::tuple<Args...>&& tuple, const Mapper& mapper) {
221
+ return detail::tuple_map(
222
+ std::move(tuple), mapper, std::index_sequence_for<Args...>());
223
+ }
224
+
225
+ } // namespace c10::guts
226
+
227
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, guts)
228
+
229
+ using c10::guts::function_traits;
230
+ using c10::guts::infer_function_traits_t;
231
+ using c10::guts::make_function_traits_t;
232
+ using c10::guts::tuple_elements;
233
+ using c10::guts::tuple_map;
234
+ using c10::guts::tuple_slice;
235
+ using c10::guts::tuple_take;
236
+
237
+ HIDDEN_NAMESPACE_END(torch, headeronly, guts)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeList.h ADDED
@@ -0,0 +1,548 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/TypeTraits.h>
5
+ #include <algorithm>
6
+ #include <cstddef>
7
+ #include <tuple>
8
+ #include <type_traits>
9
+ #include <utility>
10
+
11
+ namespace c10::guts {
12
+
13
+ template <class... T>
14
+ struct false_t : std::false_type {};
15
+ template <template <class> class... T>
16
+ struct false_higher_t : std::false_type {};
17
+
18
+ namespace typelist {
19
+
20
+ /**
21
+ * Type holding a list of types for compile time type computations
22
+ */
23
+ template <class... Items>
24
+ struct typelist final {
25
+ public:
26
+ typelist() = delete; // not for instantiation
27
+ };
28
+
29
+ /**
30
+ * Returns the number of types in a typelist
31
+ * Example:
32
+ * 3 == size<typelist<int, int, double>>::value
33
+ */
34
+ template <class TypeList>
35
+ struct size final {
36
+ static_assert(
37
+ false_t<TypeList>::value,
38
+ "In typelist::size<T>, T must be typelist<...>.");
39
+ };
40
+ template <class... Types>
41
+ struct size<typelist<Types...>> final {
42
+ static constexpr size_t value = sizeof...(Types);
43
+ };
44
+
45
+ /**
46
+ * Transforms a list of types into a tuple holding these types.
47
+ * Example:
48
+ * std::tuple<int, string> == to_tuple_t<typelist<int, string>>
49
+ */
50
+ template <class TypeList>
51
+ struct to_tuple final {
52
+ static_assert(
53
+ false_t<TypeList>::value,
54
+ "In typelist::to_tuple<T>, T must be typelist<...>.");
55
+ };
56
+ template <class... Types>
57
+ struct to_tuple<typelist<Types...>> final {
58
+ using type = std::tuple<Types...>;
59
+ };
60
+ template <class TypeList>
61
+ using to_tuple_t = typename to_tuple<TypeList>::type;
62
+
63
+ /**
64
+ * Creates a typelist containing the types of a given tuple.
65
+ * Example:
66
+ * typelist<int, string> == from_tuple_t<std::tuple<int, string>>
67
+ */
68
+ template <class Tuple>
69
+ struct from_tuple final {
70
+ static_assert(
71
+ false_t<Tuple>::value,
72
+ "In typelist::from_tuple<T>, T must be std::tuple<...>.");
73
+ };
74
+ template <class... Types>
75
+ struct from_tuple<std::tuple<Types...>> final {
76
+ using type = typelist<Types...>;
77
+ };
78
+ template <class Tuple>
79
+ using from_tuple_t = typename from_tuple<Tuple>::type;
80
+
81
+ /**
82
+ * Concatenates multiple type lists.
83
+ * Example:
84
+ * typelist<int, string, int> == concat_t<typelist<int, string>,
85
+ * typelist<int>>
86
+ */
87
+ template <class... TypeLists>
88
+ struct concat final {
89
+ static_assert(
90
+ false_t<TypeLists...>::value,
91
+ "In typelist::concat<T1, ...>, the T arguments each must be typelist<...>.");
92
+ };
93
+ template <class... Head1Types, class... Head2Types, class... TailLists>
94
+ struct concat<typelist<Head1Types...>, typelist<Head2Types...>, TailLists...>
95
+ final {
96
+ using type =
97
+ typename concat<typelist<Head1Types..., Head2Types...>, TailLists...>::
98
+ type;
99
+ };
100
+ template <class... HeadTypes>
101
+ struct concat<typelist<HeadTypes...>> final {
102
+ using type = typelist<HeadTypes...>;
103
+ };
104
+ template <>
105
+ struct concat<> final {
106
+ using type = typelist<>;
107
+ };
108
+ template <class... TypeLists>
109
+ using concat_t = typename concat<TypeLists...>::type;
110
+
111
+ /**
112
+ * Filters the types in a type list by a type trait.
113
+ * Examples:
114
+ * typelist<int&, const string&&> == filter_t<std::is_reference,
115
+ * typelist<void, string, int&, bool, const string&&, int>>
116
+ */
117
+ template <template <class> class Condition, class TypeList>
118
+ struct filter final {
119
+ static_assert(
120
+ false_t<TypeList>::value,
121
+ "In typelist::filter<Condition, TypeList>, the TypeList argument must be typelist<...>.");
122
+ };
123
+ template <template <class> class Condition, class Head, class... Tail>
124
+ struct filter<Condition, typelist<Head, Tail...>> final {
125
+ static_assert(
126
+ is_type_condition<Condition>::value,
127
+ "In typelist::filter<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
128
+ using type = std::conditional_t<
129
+ Condition<Head>::value,
130
+ concat_t<
131
+ typelist<Head>,
132
+ typename filter<Condition, typelist<Tail...>>::type>,
133
+ typename filter<Condition, typelist<Tail...>>::type>;
134
+ };
135
+ template <template <class> class Condition>
136
+ struct filter<Condition, typelist<>> final {
137
+ static_assert(
138
+ is_type_condition<Condition>::value,
139
+ "In typelist::filter<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
140
+ using type = typelist<>;
141
+ };
142
+ template <template <class> class Condition, class TypeList>
143
+ using filter_t = typename filter<Condition, TypeList>::type;
144
+
145
+ /**
146
+ * Counts how many types in the list fulfill a type trait
147
+ * Examples:
148
+ * 2 == count_if<std::is_reference, typelist<void, string, int&, bool, const
149
+ * string&&, int>>
150
+ */
151
+ template <template <class> class Condition, class TypeList>
152
+ struct count_if final {
153
+ static_assert(
154
+ is_type_condition<Condition>::value,
155
+ "In typelist::count_if<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
156
+ static_assert(
157
+ is_instantiation_of<typelist, TypeList>::value,
158
+ "In typelist::count_if<Condition, TypeList>, the TypeList argument must be typelist<...>.");
159
+ // TODO Direct implementation might be faster
160
+ static constexpr size_t value = size<filter_t<Condition, TypeList>>::value;
161
+ };
162
+
163
+ /**
164
+ * Checks if a typelist contains a certain type.
165
+ * Examples:
166
+ * contains<typelist<int, string>, string> == true_type
167
+ * contains<typelist<int, string>, double> == false_type
168
+ */
169
+ namespace detail {
170
+ template <class TypeList, class Type, class Enable = void>
171
+ struct contains {};
172
+ template <class Type>
173
+ struct contains<typelist<>, Type, void> : std::false_type {};
174
+ template <class Type, class Head, class... Tail>
175
+ struct contains<
176
+ typelist<Head, Tail...>,
177
+ Type,
178
+ std::enable_if_t<std::is_same_v<Head, Type>>> : std::true_type {};
179
+ template <class Type, class Head, class... Tail>
180
+ struct contains<
181
+ typelist<Head, Tail...>,
182
+ Type,
183
+ std::enable_if_t<!std::is_same_v<Head, Type>>>
184
+ : contains<typelist<Tail...>, Type> {};
185
+ } // namespace detail
186
+ template <class TypeList, class Type>
187
+ using contains = typename detail::contains<TypeList, Type>::type;
188
+
189
+ /**
190
+ * Returns true iff the type trait is true for all types in the type list
191
+ * Examples:
192
+ * true == all<std::is_reference, typelist<int&, const float&&, const
193
+ * MyClass&>>::value false == all<std::is_reference, typelist<int&, const
194
+ * float&&, MyClass>>::value
195
+ */
196
+ template <template <class> class Condition, class TypeList>
197
+ struct all {
198
+ static_assert(
199
+ false_t<TypeList>::value,
200
+ "In typelist::all<Condition, TypeList>, the TypeList argument must be typelist<...>.");
201
+ };
202
+ template <template <class> class Condition, class... Types>
203
+ struct all<Condition, typelist<Types...>>
204
+ : std::conjunction<Condition<Types>...> {
205
+ static_assert(
206
+ is_type_condition<Condition>::value,
207
+ "In typelist::all<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
208
+ };
209
+
210
+ /**
211
+ * Returns true iff the type trait is true for any type in the type list
212
+ * Examples:
213
+ * true == true_for_any_type<std::is_reference, typelist<int, const
214
+ * float&&, const MyClass>>::value false ==
215
+ * true_for_any_type<std::is_reference, typelist<int, const float,
216
+ * MyClass>>::value
217
+ */
218
+ template <template <class> class Condition, class TypeList>
219
+ struct true_for_any_type final {
220
+ static_assert(
221
+ false_t<TypeList>::value,
222
+ "In typelist::true_for_any_type<Condition, TypeList>, the TypeList argument must be typelist<...>.");
223
+ };
224
+ template <template <class> class Condition, class... Types>
225
+ struct true_for_any_type<Condition, typelist<Types...>> final
226
+ : std::disjunction<Condition<Types>...> {
227
+ static_assert(
228
+ is_type_condition<Condition>::value,
229
+ "In typelist::true_for_any_type<Condition, TypeList>, the Condition argument must be a condition type trait, i.e. have a static constexpr bool ::value member.");
230
+ };
231
+
232
+ /**
233
+ * Maps types of a type list using a type trait
234
+ * Example:
235
+ * typelist<int&, double&, string&> == map_t<std::add_lvalue_reference_t,
236
+ * typelist<int, double, string>>
237
+ */
238
+ template <template <class> class Mapper, class TypeList>
239
+ struct map final {
240
+ static_assert(
241
+ false_t<TypeList>::value,
242
+ "In typelist::map<Mapper, TypeList>, the TypeList argument must be typelist<...>.");
243
+ };
244
+ template <template <class> class Mapper, class... Types>
245
+ struct map<Mapper, typelist<Types...>> final {
246
+ using type = typelist<Mapper<Types>...>;
247
+ };
248
+ template <template <class> class Mapper, class TypeList>
249
+ using map_t = typename map<Mapper, TypeList>::type;
250
+
251
+ /**
252
+ * Returns the first element of a type list.
253
+ * Example:
254
+ * int == head_t<typelist<int, string>>
255
+ */
256
+ template <class TypeList>
257
+ struct head final {
258
+ static_assert(
259
+ false_t<TypeList>::value,
260
+ "In typelist::head<T>, the T argument must be typelist<...>.");
261
+ };
262
+ template <class Head, class... Tail>
263
+ struct head<typelist<Head, Tail...>> final {
264
+ using type = Head;
265
+ };
266
+ template <class TypeList>
267
+ using head_t = typename head<TypeList>::type;
268
+
269
+ /**
270
+ * Returns the first element of a type list, or the specified default if the
271
+ * type list is empty. Example: int == head_t<bool, typelist<int, string>>
272
+ * bool == head_t<bool, typelist<>>
273
+ */
274
+ template <class Default, class TypeList>
275
+ struct head_with_default final {
276
+ using type = Default;
277
+ };
278
+ template <class Default, class Head, class... Tail>
279
+ struct head_with_default<Default, typelist<Head, Tail...>> final {
280
+ using type = Head;
281
+ };
282
+ template <class Default, class TypeList>
283
+ using head_with_default_t = typename head_with_default<Default, TypeList>::type;
284
+
285
+ /**
286
+ * Returns the N-th element of a type list.
287
+ * Example:
288
+ * int == element_t<1, typelist<float, int, char>>
289
+ */
290
+
291
+ /// Base template.
292
+ template <size_t Index, class TypeList>
293
+ struct element final {
294
+ static_assert(
295
+ false_t<TypeList>::value,
296
+ "In typelist::element<T>, the T argument must be typelist<...>.");
297
+ };
298
+
299
+ /// Successful case, we have reached the zero index and can "return" the head
300
+ /// type.
301
+ template <class Head, class... Tail>
302
+ struct element<0, typelist<Head, Tail...>> {
303
+ using type = Head;
304
+ };
305
+
306
+ /// Error case, we have an index but ran out of types! It will only be selected
307
+ /// if `Ts...` is actually empty!
308
+ template <size_t Index, class... Ts>
309
+ struct element<Index, typelist<Ts...>> {
310
+ static_assert(
311
+ Index < sizeof...(Ts),
312
+ "Index is out of bounds in typelist::element");
313
+ };
314
+
315
+ /// Shave off types until we hit the <0, Head, Tail...> or <Index> case.
316
+ template <size_t Index, class Head, class... Tail>
317
+ struct element<Index, typelist<Head, Tail...>>
318
+ : element<Index - 1, typelist<Tail...>> {};
319
+
320
+ /// Convenience alias.
321
+ template <size_t Index, class TypeList>
322
+ using element_t = typename element<Index, TypeList>::type;
323
+
324
+ /**
325
+ * Returns the last element of a type list.
326
+ * Example:
327
+ * int == last_t<typelist<int, string>>
328
+ */
329
+ template <class TypeList>
330
+ struct last final {
331
+ static_assert(
332
+ false_t<TypeList>::value,
333
+ "In typelist::last<T>, the T argument must be typelist<...>.");
334
+ };
335
+ template <class Head, class... Tail>
336
+ struct last<typelist<Head, Tail...>> final {
337
+ using type = typename last<typelist<Tail...>>::type;
338
+ };
339
+ template <class Head>
340
+ struct last<typelist<Head>> final {
341
+ using type = Head;
342
+ };
343
+ template <class TypeList>
344
+ using last_t = typename last<TypeList>::type;
345
+ static_assert(std::is_same_v<int, last_t<typelist<double, float, int>>>);
346
+
347
+ /**
348
+ * Take/drop a number of arguments from a typelist.
349
+ * Example:
350
+ * typelist<int, string> == take_t<typelist<int, string, bool>, 2>
351
+ * typelist<bool> == drop_t<typelist<int, string, bool>, 2>
352
+ */
353
+ namespace detail {
354
+ template <class TypeList, size_t offset, class IndexSequence>
355
+ struct take_elements final {};
356
+
357
+ template <class TypeList, size_t offset, size_t... Indices>
358
+ struct take_elements<TypeList, offset, std::index_sequence<Indices...>> final {
359
+ using type = typelist<typename element<offset + Indices, TypeList>::type...>;
360
+ };
361
+ } // namespace detail
362
+
363
+ template <class TypeList, size_t num>
364
+ struct take final {
365
+ static_assert(
366
+ is_instantiation_of<typelist, TypeList>::value,
367
+ "In typelist::take<T, num>, the T argument must be typelist<...>.");
368
+ static_assert(
369
+ num <= size<TypeList>::value,
370
+ "Tried to typelist::take more elements than there are in the list");
371
+ using type = typename detail::
372
+ take_elements<TypeList, 0, std::make_index_sequence<num>>::type;
373
+ };
374
+ template <class TypeList, size_t num>
375
+ using take_t = typename take<TypeList, num>::type;
376
+
377
+ template <class TypeList, size_t num>
378
+ struct drop final {
379
+ static_assert(
380
+ is_instantiation_of<typelist, TypeList>::value,
381
+ "In typelist::drop<T, num>, the T argument must be typelist<...>.");
382
+ static_assert(
383
+ num <= size<TypeList>::value,
384
+ "Tried to typelist::drop more elements than there are in the list");
385
+ using type = typename detail::take_elements<
386
+ TypeList,
387
+ num,
388
+ std::make_index_sequence<size<TypeList>::value - num>>::type;
389
+ };
390
+ template <class TypeList, size_t num>
391
+ using drop_t = typename drop<TypeList, num>::type;
392
+
393
+ /**
394
+ * Like drop, but returns an empty list rather than an assertion error if `num`
395
+ * is larger than the size of the TypeList.
396
+ * Example:
397
+ * typelist<> == drop_if_nonempty_t<typelist<string, bool>, 2>
398
+ * typelist<> == drop_if_nonempty_t<typelist<int, string, bool>, 3>
399
+ */
400
+ template <class TypeList, size_t num>
401
+ struct drop_if_nonempty final {
402
+ static_assert(
403
+ is_instantiation_of<typelist, TypeList>::value,
404
+ "In typelist::drop<T, num>, the T argument must be typelist<...>.");
405
+ using type = typename detail::take_elements<
406
+ TypeList,
407
+ std::min(num, size<TypeList>::value),
408
+ std::make_index_sequence<
409
+ size<TypeList>::value - std::min(num, size<TypeList>::value)>>::type;
410
+ };
411
+ template <class TypeList, size_t num>
412
+ using drop_if_nonempty_t = typename drop_if_nonempty<TypeList, num>::type;
413
+
414
+ /**
415
+ * Reverses a typelist.
416
+ * Example:
417
+ * typelist<int, string> == reverse_t<typelist<string, int>>
418
+ */
419
+ template <class TypeList>
420
+ struct reverse final {
421
+ static_assert(
422
+ false_t<TypeList>::value,
423
+ "In typelist::reverse<T>, the T argument must be typelist<...>.");
424
+ };
425
+ template <class Head, class... Tail>
426
+ struct reverse<typelist<Head, Tail...>> final {
427
+ using type =
428
+ concat_t<typename reverse<typelist<Tail...>>::type, typelist<Head>>;
429
+ };
430
+ template <>
431
+ struct reverse<typelist<>> final {
432
+ using type = typelist<>;
433
+ };
434
+ template <class TypeList>
435
+ using reverse_t = typename reverse<TypeList>::type;
436
+
437
+ /**
438
+ * Find the index of the first type in a typelist fulfilling a type trait
439
+ * condition. Example:
440
+ *
441
+ * 2 == find_if<typelist<char, int, char&, int&>, std::is_reference>::value
442
+ */
443
+ template <class TypeList, template <class> class Condition, class Enable = void>
444
+ struct find_if final {
445
+ static_assert(
446
+ false_t<TypeList>::value,
447
+ "In typelist::find_if<TypeList, Condition>, the TypeList argument must be typelist<...>.");
448
+ };
449
+ template <template <class> class Condition>
450
+ struct find_if<typelist<>, Condition, void> final {
451
+ static_assert(
452
+ false_higher_t<Condition>::value,
453
+ "In typelist::find_if<Type/List, Condition>, didn't find any type fulfilling the Condition.");
454
+ };
455
+ template <class Head, class... Tail, template <class> class Condition>
456
+ struct find_if<
457
+ typelist<Head, Tail...>,
458
+ Condition,
459
+ std::enable_if_t<Condition<Head>::value>>
460
+ final {
461
+ static constexpr size_t value = 0;
462
+ };
463
+ template <class Head, class... Tail, template <class> class Condition>
464
+ struct find_if<
465
+ typelist<Head, Tail...>,
466
+ Condition,
467
+ std::enable_if_t<!Condition<Head>::value>>
468
+ final {
469
+ static constexpr size_t value =
470
+ 1 + find_if<typelist<Tail...>, Condition>::value;
471
+ };
472
+
473
+ /**
474
+ * Maps a list of types into a list of values.
475
+ * Examples:
476
+ * // Example 1
477
+ * auto sizes =
478
+ * map_types_to_values<typelist<int64_t, bool, uint32_t>>(
479
+ * [] (auto t) { return sizeof(decltype(t)::type); }
480
+ * );
481
+ * // sizes == std::tuple<size_t, size_t, size_t>{8, 1, 4}
482
+ *
483
+ * // Example 2
484
+ * auto shared_ptrs =
485
+ * map_types_to_values<typelist<int, double>>(
486
+ * [] (auto t) { return make_shared<typename decltype(t)::type>(); }
487
+ * );
488
+ * // shared_ptrs == std::tuple<shared_ptr<int>, shared_ptr<double>>()
489
+ */
490
+ namespace detail {
491
+ template <class T>
492
+ struct type_ final {
493
+ using type = T;
494
+ };
495
+ template <class TypeList>
496
+ struct map_types_to_values final {
497
+ static_assert(
498
+ false_t<TypeList>::value,
499
+ "In typelist::map_types_to_values<T>, the T argument must be typelist<...>.");
500
+ };
501
+ template <class... Types>
502
+ struct map_types_to_values<typelist<Types...>> final {
503
+ template <class Func>
504
+ static auto call(Func&& func) {
505
+ return std::tuple{std::forward<Func>(func)(type_<Types>())...};
506
+ }
507
+ };
508
+ } // namespace detail
509
+
510
+ template <class TypeList, class Func>
511
+ auto map_types_to_values(Func&& func) {
512
+ return detail::map_types_to_values<TypeList>::call(std::forward<Func>(func));
513
+ }
514
+
515
+ } // namespace typelist
516
+ } // namespace c10::guts
517
+
518
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
519
+
520
+ namespace guts {
521
+ using c10::guts::false_t;
522
+
523
+ namespace typelist {
524
+ using c10::guts::typelist::all;
525
+ using c10::guts::typelist::concat_t;
526
+ using c10::guts::typelist::contains;
527
+ using c10::guts::typelist::count_if;
528
+ using c10::guts::typelist::drop_if_nonempty_t;
529
+ using c10::guts::typelist::drop_t;
530
+ using c10::guts::typelist::filter_t;
531
+ using c10::guts::typelist::find_if;
532
+ using c10::guts::typelist::from_tuple_t;
533
+ using c10::guts::typelist::head_t;
534
+ using c10::guts::typelist::head_with_default_t;
535
+ using c10::guts::typelist::map_t;
536
+ using c10::guts::typelist::map_types_to_values;
537
+ using c10::guts::typelist::reverse_t;
538
+ using c10::guts::typelist::size;
539
+ using c10::guts::typelist::take_t;
540
+ using c10::guts::typelist::to_tuple_t;
541
+ using c10::guts::typelist::true_for_any_type;
542
+ using c10::guts::typelist::typelist;
543
+
544
+ } // namespace typelist
545
+
546
+ } // namespace guts
547
+
548
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeSafeSignMath.h ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <limits>
5
+ #include <type_traits>
6
+
7
+ C10_CLANG_DIAGNOSTIC_PUSH()
8
+ #if C10_CLANG_HAS_WARNING("-Wstring-conversion")
9
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wstring-conversion")
10
+ #endif
11
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
12
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
13
+ #endif
14
+
15
+ namespace c10 {
16
+
17
+ /// Returns false since we cannot have x < 0 if x is unsigned.
18
+ template <typename T>
19
+ inline constexpr bool is_negative(
20
+ const T& /*x*/,
21
+ std::true_type /*is_unsigned*/) {
22
+ return false;
23
+ }
24
+
25
+ /// Returns true if a signed variable x < 0
26
+ template <typename T>
27
+ inline constexpr bool is_negative(const T& x, std::false_type /*is_unsigned*/) {
28
+ return x < T(0);
29
+ }
30
+
31
+ /// Returns true if x < 0
32
+ /// NOTE: Will fail on an unsigned custom type
33
+ /// For the most part it's possible to fix this if
34
+ /// the custom type has a constexpr constructor.
35
+ /// However, notably, c10::Half does not :-(
36
+ template <typename T>
37
+ inline constexpr bool is_negative(const T& x) {
38
+ return is_negative(x, std::is_unsigned<T>());
39
+ }
40
+
41
+ /// Returns the sign of an unsigned variable x as 0, 1
42
+ template <typename T>
43
+ inline constexpr int signum(const T& x, std::true_type /*is_unsigned*/) {
44
+ return T(0) < x;
45
+ }
46
+
47
+ /// Returns the sign of a signed variable x as -1, 0, 1
48
+ template <typename T>
49
+ inline constexpr int signum(const T& x, std::false_type /*is_unsigned*/) {
50
+ return (T(0) < x) - (x < T(0));
51
+ }
52
+
53
+ /// Returns the sign of x as -1, 0, 1
54
+ /// NOTE: Will fail on an unsigned custom type
55
+ /// For the most part it's possible to fix this if
56
+ /// the custom type has a constexpr constructor.
57
+ /// However, notably, c10::Half does not :-(
58
+ template <typename T>
59
+ inline constexpr int signum(const T& x) {
60
+ return signum(x, std::is_unsigned<T>());
61
+ }
62
+
63
+ /// Returns true if a and b are not both negative
64
+ template <typename T, typename U>
65
+ inline constexpr bool signs_differ(const T& a, const U& b) {
66
+ return is_negative(a) != is_negative(b);
67
+ }
68
+
69
+ // Suppress sign compare warning when compiling with GCC
70
+ // as later does not account for short-circuit rule before
71
+ // raising the warning, see https://godbolt.org/z/Tr3Msnz99
72
+ #ifdef __GNUC__
73
+ #pragma GCC diagnostic push
74
+ #pragma GCC diagnostic ignored "-Wsign-compare"
75
+ #endif
76
+
77
+ /// Returns true if x is greater than the greatest value of the type Limit
78
+ template <typename Limit, typename T>
79
+ inline constexpr bool greater_than_max(const T& x) {
80
+ constexpr bool can_overflow =
81
+ std::numeric_limits<T>::digits > std::numeric_limits<Limit>::digits;
82
+ return can_overflow && x > std::numeric_limits<Limit>::max();
83
+ }
84
+
85
+ #ifdef __GNUC__
86
+ #pragma GCC diagnostic pop
87
+ #endif
88
+
89
+ /// Returns true if x < lowest(Limit). Standard comparison
90
+ template <typename Limit, typename T>
91
+ inline constexpr bool less_than_lowest(
92
+ const T& x,
93
+ std::false_type /*limit_is_unsigned*/,
94
+ std::false_type /*x_is_unsigned*/) {
95
+ return x < std::numeric_limits<Limit>::lowest();
96
+ }
97
+
98
+ /// Returns false since all the limit is signed and therefore includes
99
+ /// negative values but x cannot be negative because it is unsigned
100
+ template <typename Limit, typename T>
101
+ inline constexpr bool less_than_lowest(
102
+ const T& /*x*/,
103
+ std::false_type /*limit_is_unsigned*/,
104
+ std::true_type /*x_is_unsigned*/) {
105
+ return false;
106
+ }
107
+
108
+ /// Returns true if x < 0, where 0 is constructed from T.
109
+ /// Limit is not signed, so its lower value is zero
110
+ template <typename Limit, typename T>
111
+ inline constexpr bool less_than_lowest(
112
+ const T& x,
113
+ std::true_type /*limit_is_unsigned*/,
114
+ std::false_type /*x_is_unsigned*/) {
115
+ return x < T(0);
116
+ }
117
+
118
+ /// Returns false sign both types are unsigned
119
+ template <typename Limit, typename T>
120
+ inline constexpr bool less_than_lowest(
121
+ const T& /*x*/,
122
+ std::true_type /*limit_is_unsigned*/,
123
+ std::true_type /*x_is_unsigned*/) {
124
+ return false;
125
+ }
126
+
127
+ /// Returns true if x is less than the lowest value of type T
128
+ /// NOTE: Will fail on an unsigned custom type
129
+ /// For the most part it's possible to fix this if
130
+ /// the custom type has a constexpr constructor.
131
+ /// However, notably, c10::Half does not :
132
+ template <typename Limit, typename T>
133
+ inline constexpr bool less_than_lowest(const T& x) {
134
+ return less_than_lowest<Limit>(
135
+ x, std::is_unsigned<Limit>(), std::is_unsigned<T>());
136
+ }
137
+
138
+ } // namespace c10
139
+
140
+ C10_CLANG_DIAGNOSTIC_POP()
141
+
142
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
143
+ using c10::greater_than_max;
144
+ using c10::is_negative;
145
+ using c10::less_than_lowest;
146
+ using c10::signs_differ;
147
+ using c10::signum;
148
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/TypeTraits.h ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+
5
+ #include <functional>
6
+ #include <type_traits>
7
+
8
+ namespace c10::guts {
9
+
10
+ /**
11
+ * is_equality_comparable<T> is true_type iff the equality operator is defined
12
+ * for T.
13
+ */
14
+ template <class T, class Enable = void>
15
+ struct is_equality_comparable : std::false_type {};
16
+ template <class T>
17
+ struct is_equality_comparable<
18
+ T,
19
+ std::void_t<decltype(std::declval<T&>() == std::declval<T&>())>>
20
+ : std::true_type {};
21
+ template <class T>
22
+ using is_equality_comparable_t = typename is_equality_comparable<T>::type;
23
+
24
+ /**
25
+ * is_hashable<T> is true_type iff std::hash is defined for T
26
+ */
27
+ template <class T, class Enable = void>
28
+ struct is_hashable : std::false_type {};
29
+ template <class T>
30
+ struct is_hashable<T, std::void_t<decltype(std::hash<T>()(std::declval<T&>()))>>
31
+ : std::true_type {};
32
+ template <class T>
33
+ using is_hashable_t = typename is_hashable<T>::type;
34
+
35
+ /**
36
+ * is_function_type<T> is true_type iff T is a plain function type (i.e.
37
+ * "Result(Args...)")
38
+ */
39
+ template <class T>
40
+ struct is_function_type : std::false_type {};
41
+ template <class Result, class... Args>
42
+ struct is_function_type<Result(Args...)> : std::true_type {};
43
+ template <class T>
44
+ using is_function_type_t = typename is_function_type<T>::type;
45
+
46
+ /**
47
+ * is_instantiation_of<T, I> is true_type iff I is a template instantiation of T
48
+ * (e.g. vector<int> is an instantiation of vector) Example:
49
+ * is_instantiation_of_t<vector, vector<int>> // true
50
+ * is_instantiation_of_t<pair, pair<int, string>> // true
51
+ * is_instantiation_of_t<vector, pair<int, string>> // false
52
+ */
53
+ template <template <class...> class Template, class T>
54
+ struct is_instantiation_of : std::false_type {};
55
+ template <template <class...> class Template, class... Args>
56
+ struct is_instantiation_of<Template, Template<Args...>> : std::true_type {};
57
+ template <template <class...> class Template, class T>
58
+ using is_instantiation_of_t = typename is_instantiation_of<Template, T>::type;
59
+
60
+ namespace detail {
61
+ /**
62
+ * strip_class: helper to remove the class type from pointers to `operator()`.
63
+ */
64
+
65
+ template <typename T>
66
+ struct strip_class {};
67
+ template <typename Class, typename Result, typename... Args>
68
+ struct strip_class<Result (Class::*)(Args...)> {
69
+ using type = Result(Args...);
70
+ };
71
+ template <typename Class, typename Result, typename... Args>
72
+ struct strip_class<Result (Class::*)(Args...) const> {
73
+ using type = Result(Args...);
74
+ };
75
+ template <typename T>
76
+ using strip_class_t = typename strip_class<T>::type;
77
+ } // namespace detail
78
+
79
+ /**
80
+ * Evaluates to true_type, iff the given class is a Functor
81
+ * (i.e. has a call operator with some set of arguments)
82
+ */
83
+
84
+ template <class Functor, class Enable = void>
85
+ struct is_functor : std::false_type {};
86
+ template <class Functor>
87
+ struct is_functor<
88
+ Functor,
89
+ std::enable_if_t<is_function_type<
90
+ detail::strip_class_t<decltype(&Functor::operator())>>::value>>
91
+ : std::true_type {};
92
+
93
+ /**
94
+ * lambda_is_stateless<T> is true iff the lambda type T is stateless
95
+ * (i.e. does not have a closure).
96
+ * Example:
97
+ * auto stateless_lambda = [] (int a) {return a;};
98
+ * lambda_is_stateless<decltype(stateless_lambda)> // true
99
+ * auto stateful_lambda = [&] (int a) {return a;};
100
+ * lambda_is_stateless<decltype(stateful_lambda)> // false
101
+ */
102
+ namespace detail {
103
+ template <class LambdaType, class FuncType>
104
+ struct is_stateless_lambda__ final {
105
+ static_assert(
106
+ !std::is_same_v<LambdaType, LambdaType>,
107
+ "Base case shouldn't be hit");
108
+ };
109
+ // implementation idea: According to the C++ standard, stateless lambdas are
110
+ // convertible to function pointers
111
+ template <class LambdaType, class C, class Result, class... Args>
112
+ struct is_stateless_lambda__<LambdaType, Result (C::*)(Args...) const>
113
+ : std::is_convertible<LambdaType, Result (*)(Args...)> {};
114
+ template <class LambdaType, class C, class Result, class... Args>
115
+ struct is_stateless_lambda__<LambdaType, Result (C::*)(Args...)>
116
+ : std::is_convertible<LambdaType, Result (*)(Args...)> {};
117
+
118
+ // case where LambdaType is not even a functor
119
+ template <class LambdaType, class Enable = void>
120
+ struct is_stateless_lambda_ final : std::false_type {};
121
+ // case where LambdaType is a functor
122
+ template <class LambdaType>
123
+ struct is_stateless_lambda_<
124
+ LambdaType,
125
+ std::enable_if_t<is_functor<LambdaType>::value>>
126
+ : is_stateless_lambda__<LambdaType, decltype(&LambdaType::operator())> {};
127
+ } // namespace detail
128
+ template <class T>
129
+ using is_stateless_lambda = detail::is_stateless_lambda_<std::decay_t<T>>;
130
+
131
+ /**
132
+ * is_type_condition<C> is true_type iff C<...> is a type trait representing a
133
+ * condition (i.e. has a constexpr static bool ::value member) Example:
134
+ * is_type_condition<std::is_reference> // true
135
+ */
136
+ template <template <class> class C, class Enable = void>
137
+ struct is_type_condition : std::false_type {};
138
+ template <template <class> class C>
139
+ struct is_type_condition<
140
+ C,
141
+ std::enable_if_t<
142
+ std::is_same_v<bool, std::remove_cv_t<decltype(C<int>::value)>>>>
143
+ : std::true_type {};
144
+
145
+ /**
146
+ * is_fundamental<T> is true_type iff the lambda type T is a fundamental type
147
+ * (that is, arithmetic type, void, or nullptr_t). Example: is_fundamental<int>
148
+ * // true We define it here to resolve a MSVC bug. See
149
+ * https://github.com/pytorch/pytorch/issues/30932 for details.
150
+ */
151
+ template <class T>
152
+ struct is_fundamental : std::is_fundamental<T> {};
153
+ } // namespace c10::guts
154
+
155
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, guts)
156
+
157
+ using c10::guts::is_equality_comparable;
158
+ using c10::guts::is_function_type;
159
+ using c10::guts::is_hashable;
160
+ using c10::guts::is_instantiation_of;
161
+ using c10::guts::is_stateless_lambda;
162
+ using c10::guts::is_type_condition;
163
+
164
+ HIDDEN_NAMESPACE_END(torch, headeronly, guts)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/bit_cast.h ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <cstring>
4
+ #include <type_traits>
5
+
6
+ #include <torch/headeronly/macros/Macros.h>
7
+
8
+ #if __has_include(<bit>) && (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L)
9
+ #include <bit>
10
+ #define C10_HAVE_STD_BIT_CAST 1
11
+ #else
12
+ #define C10_HAVE_STD_BIT_CAST 0
13
+ #endif // __has_include(<bit>) && (__cplusplus >= 202002L ||
14
+ // (defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L))
15
+
16
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
17
+
18
+ #if C10_HAVE_STD_BIT_CAST
19
+ using std::bit_cast;
20
+ #else
21
+ // Implementations of std::bit_cast() from C++ 20.
22
+ //
23
+ // This is a less sketchy version of reinterpret_cast.
24
+ //
25
+ // See https://en.cppreference.com/w/cpp/numeric/bit_cast for more
26
+ // information as well as the source of our implementations.
27
+ template <class To, class From>
28
+ C10_HOST_DEVICE std::enable_if_t<
29
+ sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
30
+ std::is_trivially_copyable_v<To>,
31
+ To>
32
+ // constexpr support needs compiler magic
33
+ bit_cast(const From& src) noexcept {
34
+ static_assert(
35
+ std::is_trivially_constructible_v<To>,
36
+ "This implementation additionally requires "
37
+ "destination type to be trivially constructible");
38
+
39
+ To dst;
40
+ std::memcpy(&dst, &src, sizeof(To));
41
+ return dst;
42
+ }
43
+ #endif // C10_HAVE_STD_BIT_CAST
44
+ #undef C10_HAVE_STD_BIT_CAST
45
+
46
+ HIDDEN_NAMESPACE_END(torch, headeronly)
47
+
48
+ namespace c10 {
49
+ using torch::headeronly::bit_cast;
50
+ } // namespace c10
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/bits.h ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * bits1x8 is an uninterpreted dtype of a tensor with 1 bit (packed to byte
10
+ * boundary), without any semantics defined.
11
+ */
12
+ struct alignas(1) bits1x8 {
13
+ using underlying = uint8_t;
14
+ uint8_t val_;
15
+ bits1x8() = default;
16
+ C10_HOST_DEVICE explicit bits1x8(uint8_t val) : val_(val) {}
17
+ };
18
+
19
+ /**
20
+ * bits2x4 is an uninterpreted dtype of a tensor with 2 bits (packed to byte
21
+ * boundary), without any semantics defined.
22
+ */
23
+ struct alignas(1) bits2x4 {
24
+ using underlying = uint8_t;
25
+ uint8_t val_;
26
+ bits2x4() = default;
27
+ C10_HOST_DEVICE explicit bits2x4(uint8_t val) : val_(val) {}
28
+ };
29
+
30
+ /**
31
+ * bits4x2 is an uninterpreted dtype of a tensor with 4 bits (packed to byte
32
+ * boundary), without any semantics defined.
33
+ */
34
+ struct alignas(1) bits4x2 {
35
+ using underlying = uint8_t;
36
+ uint8_t val_;
37
+ bits4x2() = default;
38
+ C10_HOST_DEVICE explicit bits4x2(uint8_t val) : val_(val) {}
39
+ };
40
+
41
+ /**
42
+ * bits8 is an uninterpreted dtype of a tensor with 8 bits, without any
43
+ * semantics defined.
44
+ */
45
+ struct alignas(1) bits8 {
46
+ uint8_t val_;
47
+ bits8() = default;
48
+ C10_HOST_DEVICE explicit bits8(uint8_t val) : val_(val) {}
49
+ };
50
+
51
+ /**
52
+ * bits16 is an uninterpreted dtype of a tensor with 16 bits, without any
53
+ * semantics defined.
54
+ */
55
+ struct alignas(2) bits16 {
56
+ uint16_t val_;
57
+ bits16() = default;
58
+ C10_HOST_DEVICE explicit bits16(uint16_t val) : val_(val) {}
59
+ };
60
+
61
+ } // namespace c10
62
+
63
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
64
+
65
+ using c10::bits16;
66
+ using c10::bits1x8;
67
+ using c10::bits2x4;
68
+ using c10::bits4x2;
69
+ using c10::bits8;
70
+
71
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/complex.h ADDED
@@ -0,0 +1,616 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <complex>
4
+
5
+ #include <torch/headeronly/macros/Macros.h>
6
+ #include <torch/headeronly/util/Half.h>
7
+
8
+ #if defined(__CUDACC__) || defined(__HIPCC__)
9
+ #include <thrust/complex.h>
10
+ #endif
11
+
12
+ C10_CLANG_DIAGNOSTIC_PUSH()
13
+ #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
14
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
15
+ #endif
16
+ #if C10_CLANG_HAS_WARNING("-Wfloat-conversion")
17
+ C10_CLANG_DIAGNOSTIC_IGNORE("-Wfloat-conversion")
18
+ #endif
19
+
20
+ namespace c10 {
21
+
22
+ // c10::complex is an implementation of complex numbers that aims
23
+ // to work on all devices supported by PyTorch
24
+ //
25
+ // Most of the APIs duplicates std::complex
26
+ // Reference: https://en.cppreference.com/w/cpp/numeric/complex
27
+ //
28
+ // [NOTE: Complex Operator Unification]
29
+ // Operators currently use a mix of std::complex, thrust::complex, and
30
+ // c10::complex internally. The end state is that all operators will use
31
+ // c10::complex internally. Until then, there may be some hacks to support all
32
+ // variants.
33
+ //
34
+ //
35
+ // [Note on Constructors]
36
+ //
37
+ // The APIs of constructors are mostly copied from C++ standard:
38
+ // https://en.cppreference.com/w/cpp/numeric/complex/complex
39
+ //
40
+ // Since C++14, all constructors are constexpr in std::complex
41
+ //
42
+ // There are three types of constructors:
43
+ // - initializing from real and imag:
44
+ // `constexpr complex( const T& re = T(), const T& im = T() );`
45
+ // - implicitly-declared copy constructor
46
+ // - converting constructors
47
+ //
48
+ // Converting constructors:
49
+ // - std::complex defines converting constructor between float/double/long
50
+ // double,
51
+ // while we define converting constructor between float/double.
52
+ // - For these converting constructors, upcasting is implicit, downcasting is
53
+ // explicit.
54
+ // - We also define explicit casting from std::complex/thrust::complex
55
+ // - Note that the conversion from thrust is not constexpr, because
56
+ // thrust does not define them as constexpr ????
57
+ //
58
+ //
59
+ // [Operator =]
60
+ //
61
+ // The APIs of operator = are mostly copied from C++ standard:
62
+ // https://en.cppreference.com/w/cpp/numeric/complex/operator%3D
63
+ //
64
+ // Since C++20, all operator= are constexpr. Although we are not building with
65
+ // C++20, we also obey this behavior.
66
+ //
67
+ // There are three types of assign operator:
68
+ // - Assign a real value from the same scalar type
69
+ // - In std, this is templated as complex& operator=(const T& x)
70
+ // with specialization `complex& operator=(T x)` for float/double/long
71
+ // double Since we only support float and double, on will use `complex&
72
+ // operator=(T x)`
73
+ // - Copy assignment operator and converting assignment operator
74
+ // - There is no specialization of converting assignment operators, which type
75
+ // is
76
+ // convertible is solely dependent on whether the scalar type is convertible
77
+ //
78
+ // In addition to the standard assignment, we also provide assignment operators
79
+ // with std and thrust
80
+ //
81
+ //
82
+ // [Casting operators]
83
+ //
84
+ // std::complex does not have casting operators. We define casting operators
85
+ // casting to std::complex and thrust::complex
86
+ //
87
+ //
88
+ // [Operator ""]
89
+ //
90
+ // std::complex has custom literals `i`, `if` and `il` defined in namespace
91
+ // `std::literals::complex_literals`. We define our own custom literals in the
92
+ // namespace `c10::complex_literals`. Our custom literals does not follow the
93
+ // same behavior as in std::complex, instead, we define _if, _id to construct
94
+ // float/double complex literals.
95
+ //
96
+ //
97
+ // [real() and imag()]
98
+ //
99
+ // In C++20, there are two overload of these functions, one it to return the
100
+ // real/imag, another is to set real/imag, they are both constexpr. We follow
101
+ // this design.
102
+ //
103
+ //
104
+ // [Operator +=,-=,*=,/=]
105
+ //
106
+ // Since C++20, these operators become constexpr. In our implementation, they
107
+ // are also constexpr.
108
+ //
109
+ // There are two types of such operators: operating with a real number, or
110
+ // operating with another complex number. For the operating with a real number,
111
+ // the generic template form has argument type `const T &`, while the overload
112
+ // for float/double/long double has `T`. We will follow the same type as
113
+ // float/double/long double in std.
114
+ //
115
+ // [Unary operator +-]
116
+ //
117
+ // Since C++20, they are constexpr. We also make them expr
118
+ //
119
+ // [Binary operators +-*/]
120
+ //
121
+ // Each operator has three versions (taking + as example):
122
+ // - complex + complex
123
+ // - complex + real
124
+ // - real + complex
125
+ //
126
+ // [Operator ==, !=]
127
+ //
128
+ // Each operator has three versions (taking == as example):
129
+ // - complex == complex
130
+ // - complex == real
131
+ // - real == complex
132
+ //
133
+ // Some of them are removed on C++20, but we decide to keep them
134
+ //
135
+ // [Operator <<, >>]
136
+ //
137
+ // These are implemented by casting to std::complex
138
+ //
139
+ //
140
+ //
141
+ // TODO(@zasdfgbnm): c10::complex<c10::Half> is not currently supported,
142
+ // because:
143
+ // - lots of members and functions of c10::Half are not constexpr
144
+ // - thrust::complex only support float and double
145
+
146
+ template <typename T>
147
+ struct alignas(sizeof(T) * 2) complex {
148
+ using value_type = T;
149
+
150
+ T real_ = T(0);
151
+ T imag_ = T(0);
152
+
153
+ constexpr complex() = default;
154
+ C10_HOST_DEVICE constexpr complex(const T& re, const T& im = T())
155
+ : real_(re), imag_(im) {}
156
+ template <typename U>
157
+ explicit constexpr complex(const std::complex<U>& other)
158
+ : complex(other.real(), other.imag()) {}
159
+ #if defined(__CUDACC__) || defined(__HIPCC__)
160
+ template <typename U>
161
+ explicit C10_HOST_DEVICE complex(const thrust::complex<U>& other)
162
+ : real_(other.real()), imag_(other.imag()) {}
163
+ // NOTE can not be implemented as follow due to ROCm bug:
164
+ // explicit C10_HOST_DEVICE complex(const thrust::complex<U> &other):
165
+ // complex(other.real(), other.imag()) {}
166
+ #endif
167
+
168
+ // Use SFINAE to specialize casting constructor for c10::complex<float> and
169
+ // c10::complex<double>
170
+ template <typename U = T>
171
+ C10_HOST_DEVICE explicit constexpr complex(
172
+ const std::enable_if_t<std::is_same_v<U, float>, complex<double>>& other)
173
+ : real_(other.real_), imag_(other.imag_) {}
174
+ template <typename U = T>
175
+ C10_HOST_DEVICE constexpr complex(
176
+ const std::enable_if_t<std::is_same_v<U, double>, complex<float>>& other)
177
+ : real_(other.real_), imag_(other.imag_) {}
178
+
179
+ constexpr complex<T>& operator=(T re) {
180
+ real_ = re;
181
+ imag_ = 0;
182
+ return *this;
183
+ }
184
+
185
+ constexpr complex<T>& operator+=(T re) {
186
+ real_ += re;
187
+ return *this;
188
+ }
189
+
190
+ constexpr complex<T>& operator-=(T re) {
191
+ real_ -= re;
192
+ return *this;
193
+ }
194
+
195
+ constexpr complex<T>& operator*=(T re) {
196
+ real_ *= re;
197
+ imag_ *= re;
198
+ return *this;
199
+ }
200
+
201
+ constexpr complex<T>& operator/=(T re) {
202
+ real_ /= re;
203
+ imag_ /= re;
204
+ return *this;
205
+ }
206
+
207
+ template <typename U>
208
+ constexpr complex<T>& operator=(const complex<U>& rhs) {
209
+ real_ = rhs.real();
210
+ imag_ = rhs.imag();
211
+ return *this;
212
+ }
213
+
214
+ template <typename U>
215
+ constexpr complex<T>& operator+=(const complex<U>& rhs) {
216
+ real_ += rhs.real();
217
+ imag_ += rhs.imag();
218
+ return *this;
219
+ }
220
+
221
+ template <typename U>
222
+ constexpr complex<T>& operator-=(const complex<U>& rhs) {
223
+ real_ -= rhs.real();
224
+ imag_ -= rhs.imag();
225
+ return *this;
226
+ }
227
+
228
+ template <typename U>
229
+ constexpr complex<T>& operator*=(const complex<U>& rhs) {
230
+ // (a + bi) * (c + di) = (a*c - b*d) + (a * d + b * c) i
231
+ T a = real_;
232
+ T b = imag_;
233
+ U c = rhs.real();
234
+ U d = rhs.imag();
235
+ real_ = a * c - b * d;
236
+ imag_ = a * d + b * c;
237
+ return *this;
238
+ }
239
+
240
+ #ifdef __APPLE__
241
+ #define FORCE_INLINE_APPLE __attribute__((always_inline))
242
+ #else
243
+ #define FORCE_INLINE_APPLE
244
+ #endif
245
+ template <typename U>
246
+ constexpr FORCE_INLINE_APPLE complex<T>& operator/=(const complex<U>& rhs)
247
+ __ubsan_ignore_float_divide_by_zero__ {
248
+ // (a + bi) / (c + di) = (ac + bd)/(c^2 + d^2) + (bc - ad)/(c^2 + d^2) i
249
+ // the calculation below follows numpy's complex division
250
+ T a = real_;
251
+ T b = imag_;
252
+ U c = rhs.real();
253
+ U d = rhs.imag();
254
+
255
+ #if defined(__GNUC__) && !defined(__clang__)
256
+ // std::abs is already constexpr by gcc
257
+ auto abs_c = std::abs(c);
258
+ auto abs_d = std::abs(d);
259
+ #else
260
+ auto abs_c = c < 0 ? -c : c;
261
+ auto abs_d = d < 0 ? -d : d;
262
+ #endif
263
+
264
+ if (abs_c >= abs_d) {
265
+ if (abs_c == U(0) && abs_d == U(0)) {
266
+ /* divide by zeros should yield a complex inf or nan */
267
+ real_ = a / abs_c;
268
+ imag_ = b / abs_d;
269
+ } else {
270
+ auto rat = d / c;
271
+ auto scl = U(1.0) / (c + d * rat);
272
+ real_ = (a + b * rat) * scl;
273
+ imag_ = (b - a * rat) * scl;
274
+ }
275
+ } else {
276
+ auto rat = c / d;
277
+ auto scl = U(1.0) / (d + c * rat);
278
+ real_ = (a * rat + b) * scl;
279
+ imag_ = (b * rat - a) * scl;
280
+ }
281
+ return *this;
282
+ }
283
+ #undef FORCE_INLINE_APPLE
284
+
285
+ template <typename U>
286
+ constexpr complex<T>& operator=(const std::complex<U>& rhs) {
287
+ real_ = rhs.real();
288
+ imag_ = rhs.imag();
289
+ return *this;
290
+ }
291
+
292
+ #if defined(__CUDACC__) || defined(__HIPCC__)
293
+ template <typename U>
294
+ C10_HOST_DEVICE complex<T>& operator=(const thrust::complex<U>& rhs) {
295
+ real_ = rhs.real();
296
+ imag_ = rhs.imag();
297
+ return *this;
298
+ }
299
+ #endif
300
+
301
+ template <typename U>
302
+ explicit constexpr operator std::complex<U>() const {
303
+ return std::complex<U>(std::complex<T>(real(), imag()));
304
+ }
305
+
306
+ #if defined(__CUDACC__) || defined(__HIPCC__)
307
+ template <typename U>
308
+ C10_HOST_DEVICE explicit operator thrust::complex<U>() const {
309
+ return static_cast<thrust::complex<U>>(thrust::complex<T>(real(), imag()));
310
+ }
311
+ #endif
312
+
313
+ // consistent with NumPy behavior
314
+ explicit constexpr operator bool() const {
315
+ return real() || imag();
316
+ }
317
+
318
+ C10_HOST_DEVICE constexpr T real() const {
319
+ return real_;
320
+ }
321
+ constexpr void real(T value) {
322
+ real_ = value;
323
+ }
324
+ C10_HOST_DEVICE constexpr T imag() const {
325
+ return imag_;
326
+ }
327
+ constexpr void imag(T value) {
328
+ imag_ = value;
329
+ }
330
+ };
331
+
332
+ namespace complex_literals {
333
+
334
+ constexpr complex<float> operator""_if(long double imag) {
335
+ return complex<float>(0.0f, static_cast<float>(imag));
336
+ }
337
+
338
+ constexpr complex<double> operator""_id(long double imag) {
339
+ return complex<double>(0.0, static_cast<double>(imag));
340
+ }
341
+
342
+ constexpr complex<float> operator""_if(unsigned long long imag) {
343
+ return complex<float>(0.0f, static_cast<float>(imag));
344
+ }
345
+
346
+ constexpr complex<double> operator""_id(unsigned long long imag) {
347
+ return complex<double>(0.0, static_cast<double>(imag));
348
+ }
349
+
350
+ } // namespace complex_literals
351
+
352
+ template <typename T>
353
+ constexpr complex<T> operator+(const complex<T>& val) {
354
+ return val;
355
+ }
356
+
357
+ template <typename T>
358
+ constexpr complex<T> operator-(const complex<T>& val) {
359
+ return complex<T>(-val.real(), -val.imag());
360
+ }
361
+
362
+ template <typename T>
363
+ constexpr complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs) {
364
+ complex<T> result = lhs;
365
+ return result += rhs;
366
+ }
367
+
368
+ template <typename T>
369
+ constexpr complex<T> operator+(const complex<T>& lhs, const T& rhs) {
370
+ complex<T> result = lhs;
371
+ return result += rhs;
372
+ }
373
+
374
+ template <typename T>
375
+ constexpr complex<T> operator+(const T& lhs, const complex<T>& rhs) {
376
+ return complex<T>(lhs + rhs.real(), rhs.imag());
377
+ }
378
+
379
+ template <typename T>
380
+ constexpr complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs) {
381
+ complex<T> result = lhs;
382
+ return result -= rhs;
383
+ }
384
+
385
+ template <typename T>
386
+ constexpr complex<T> operator-(const complex<T>& lhs, const T& rhs) {
387
+ complex<T> result = lhs;
388
+ return result -= rhs;
389
+ }
390
+
391
+ template <typename T>
392
+ constexpr complex<T> operator-(const T& lhs, const complex<T>& rhs) {
393
+ complex<T> result = -rhs;
394
+ return result += lhs;
395
+ }
396
+
397
+ template <typename T>
398
+ constexpr complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs) {
399
+ complex<T> result = lhs;
400
+ return result *= rhs;
401
+ }
402
+
403
+ template <typename T>
404
+ constexpr complex<T> operator*(const complex<T>& lhs, const T& rhs) {
405
+ complex<T> result = lhs;
406
+ return result *= rhs;
407
+ }
408
+
409
+ template <typename T>
410
+ constexpr complex<T> operator*(const T& lhs, const complex<T>& rhs) {
411
+ complex<T> result = rhs;
412
+ return result *= lhs;
413
+ }
414
+
415
+ template <typename T>
416
+ constexpr complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs) {
417
+ complex<T> result = lhs;
418
+ return result /= rhs;
419
+ }
420
+
421
+ template <typename T>
422
+ constexpr complex<T> operator/(const complex<T>& lhs, const T& rhs) {
423
+ complex<T> result = lhs;
424
+ return result /= rhs;
425
+ }
426
+
427
+ template <typename T>
428
+ constexpr complex<T> operator/(const T& lhs, const complex<T>& rhs) {
429
+ complex<T> result(lhs, T());
430
+ return result /= rhs;
431
+ }
432
+
433
+ // Define operators between integral scalars and c10::complex. std::complex does
434
+ // not support this when T is a floating-point number. This is useful because it
435
+ // saves a lot of "static_cast" when operate a complex and an integer. This
436
+ // makes the code both less verbose and potentially more efficient.
437
+ #define COMPLEX_INTEGER_OP_TEMPLATE_CONDITION \
438
+ typename std::enable_if_t< \
439
+ std::is_floating_point_v<fT> && std::is_integral_v<iT>, \
440
+ int> = 0
441
+
442
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
443
+ constexpr c10::complex<fT> operator+(const c10::complex<fT>& a, const iT& b) {
444
+ return a + static_cast<fT>(b);
445
+ }
446
+
447
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
448
+ constexpr c10::complex<fT> operator+(const iT& a, const c10::complex<fT>& b) {
449
+ return static_cast<fT>(a) + b;
450
+ }
451
+
452
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
453
+ constexpr c10::complex<fT> operator-(const c10::complex<fT>& a, const iT& b) {
454
+ return a - static_cast<fT>(b);
455
+ }
456
+
457
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
458
+ constexpr c10::complex<fT> operator-(const iT& a, const c10::complex<fT>& b) {
459
+ return static_cast<fT>(a) - b;
460
+ }
461
+
462
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
463
+ constexpr c10::complex<fT> operator*(const c10::complex<fT>& a, const iT& b) {
464
+ return a * static_cast<fT>(b);
465
+ }
466
+
467
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
468
+ constexpr c10::complex<fT> operator*(const iT& a, const c10::complex<fT>& b) {
469
+ return static_cast<fT>(a) * b;
470
+ }
471
+
472
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
473
+ constexpr c10::complex<fT> operator/(const c10::complex<fT>& a, const iT& b) {
474
+ return a / static_cast<fT>(b);
475
+ }
476
+
477
+ template <typename fT, typename iT, COMPLEX_INTEGER_OP_TEMPLATE_CONDITION>
478
+ constexpr c10::complex<fT> operator/(const iT& a, const c10::complex<fT>& b) {
479
+ return static_cast<fT>(a) / b;
480
+ }
481
+
482
+ #undef COMPLEX_INTEGER_OP_TEMPLATE_CONDITION
483
+
484
+ template <typename T>
485
+ constexpr bool operator==(const complex<T>& lhs, const complex<T>& rhs) {
486
+ return (lhs.real() == rhs.real()) && (lhs.imag() == rhs.imag());
487
+ }
488
+
489
+ template <typename T>
490
+ constexpr bool operator==(const complex<T>& lhs, const T& rhs) {
491
+ return (lhs.real() == rhs) && (lhs.imag() == T());
492
+ }
493
+
494
+ template <typename T>
495
+ constexpr bool operator==(const T& lhs, const complex<T>& rhs) {
496
+ return (lhs == rhs.real()) && (T() == rhs.imag());
497
+ }
498
+
499
+ template <typename T>
500
+ constexpr bool operator!=(const complex<T>& lhs, const complex<T>& rhs) {
501
+ return !(lhs == rhs);
502
+ }
503
+
504
+ template <typename T>
505
+ constexpr bool operator!=(const complex<T>& lhs, const T& rhs) {
506
+ return !(lhs == rhs);
507
+ }
508
+
509
+ template <typename T>
510
+ constexpr bool operator!=(const T& lhs, const complex<T>& rhs) {
511
+ return !(lhs == rhs);
512
+ }
513
+
514
+ template <typename T, typename CharT, typename Traits>
515
+ std::basic_ostream<CharT, Traits>& operator<<(
516
+ std::basic_ostream<CharT, Traits>& os,
517
+ const complex<T>& x) {
518
+ return (os << static_cast<std::complex<T>>(x));
519
+ }
520
+
521
+ template <typename T, typename CharT, typename Traits>
522
+ std::basic_istream<CharT, Traits>& operator>>(
523
+ std::basic_istream<CharT, Traits>& is,
524
+ complex<T>& x) {
525
+ std::complex<T> tmp;
526
+ is >> tmp;
527
+ x = tmp;
528
+ return is;
529
+ }
530
+
531
+ template <typename T>
532
+ C10_HOST_DEVICE complex<T> polar(const T& r, const T& theta = T()) {
533
+ #if defined(__CUDACC__) || defined(__HIPCC__)
534
+ return static_cast<complex<T>>(thrust::polar(r, theta));
535
+ #else
536
+ // std::polar() requires r >= 0, so spell out the explicit implementation to
537
+ // avoid a branch.
538
+ return complex<T>(r * std::cos(theta), r * std::sin(theta));
539
+ #endif
540
+ }
541
+
542
+ template <>
543
+ struct alignas(4) complex<Half> {
544
+ Half real_;
545
+ Half imag_;
546
+
547
+ // Constructors
548
+ complex() = default;
549
+ // Half constructor is not constexpr so the following constructor can't
550
+ // be constexpr
551
+ C10_HOST_DEVICE explicit inline complex(const Half& real, const Half& imag)
552
+ : real_(real), imag_(imag) {}
553
+ C10_HOST_DEVICE inline complex(const c10::complex<float>& value)
554
+ : real_(value.real()), imag_(value.imag()) {}
555
+
556
+ // Conversion operator
557
+ inline C10_HOST_DEVICE operator c10::complex<float>() const {
558
+ return {real_, imag_};
559
+ }
560
+
561
+ constexpr C10_HOST_DEVICE Half real() const {
562
+ return real_;
563
+ }
564
+ constexpr C10_HOST_DEVICE Half imag() const {
565
+ return imag_;
566
+ }
567
+
568
+ C10_HOST_DEVICE complex<Half>& operator+=(const complex<Half>& other) {
569
+ real_ = static_cast<float>(real_) + static_cast<float>(other.real_);
570
+ imag_ = static_cast<float>(imag_) + static_cast<float>(other.imag_);
571
+ return *this;
572
+ }
573
+
574
+ C10_HOST_DEVICE complex<Half>& operator-=(const complex<Half>& other) {
575
+ real_ = static_cast<float>(real_) - static_cast<float>(other.real_);
576
+ imag_ = static_cast<float>(imag_) - static_cast<float>(other.imag_);
577
+ return *this;
578
+ }
579
+
580
+ C10_HOST_DEVICE complex<Half>& operator*=(const complex<Half>& other) {
581
+ auto a = static_cast<float>(real_);
582
+ auto b = static_cast<float>(imag_);
583
+ auto c = static_cast<float>(other.real());
584
+ auto d = static_cast<float>(other.imag());
585
+ real_ = a * c - b * d;
586
+ imag_ = a * d + b * c;
587
+ return *this;
588
+ }
589
+ };
590
+
591
+ } // namespace c10
592
+
593
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
594
+ using c10::complex;
595
+ using c10::operator+;
596
+ using c10::operator-;
597
+ using c10::operator*;
598
+ using c10::operator/;
599
+ using c10::operator+=;
600
+ using c10::operator-=;
601
+ using c10::operator*=;
602
+ using c10::operator/=;
603
+ using c10::operator==;
604
+ using c10::operator!=;
605
+ using c10::operator<<;
606
+ using c10::operator>>;
607
+ using c10::polar;
608
+
609
+ namespace complex_literals {
610
+ using c10::complex_literals::operator""_if;
611
+ using c10::complex_literals::operator""_id;
612
+ } // namespace complex_literals
613
+
614
+ HIDDEN_NAMESPACE_END(torch, headeronly)
615
+
616
+ C10_CLANG_DIAGNOSTIC_POP()
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/floating_point_utils.h ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <torch/headeronly/macros/Macros.h>
4
+ #include <torch/headeronly/util/bit_cast.h>
5
+ #include <cstdint>
6
+
7
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly, detail)
8
+
9
+ C10_HOST_DEVICE inline float fp32_from_bits(uint32_t w) {
10
+ #if defined(__OPENCL_VERSION__)
11
+ return as_float(w);
12
+ #elif defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
13
+ return __uint_as_float((unsigned int)w);
14
+ #elif defined(__INTEL_COMPILER)
15
+ return _castu32_f32(w);
16
+ #else
17
+ return torch::headeronly::bit_cast<float>(w);
18
+ #endif
19
+ }
20
+
21
+ C10_HOST_DEVICE inline uint32_t fp32_to_bits(float f) {
22
+ #if defined(__OPENCL_VERSION__)
23
+ return as_uint(f);
24
+ #elif defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
25
+ return (uint32_t)__float_as_uint(f);
26
+ #elif defined(__INTEL_COMPILER)
27
+ return _castf32_u32(f);
28
+ #else
29
+ return torch::headeronly::bit_cast<uint32_t>(f);
30
+ #endif
31
+ }
32
+
33
+ HIDDEN_NAMESPACE_END(torch, headeronly, detail)
34
+
35
+ namespace c10::detail {
36
+ using torch::headeronly::detail::fp32_from_bits;
37
+ using torch::headeronly::detail::fp32_to_bits;
38
+ } // namespace c10::detail
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/qint32.h ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * qint32 is for signed 32 bit quantized Tensors
10
+ */
11
+ struct alignas(4) qint32 {
12
+ using underlying = int32_t;
13
+ int32_t val_;
14
+ qint32() = default;
15
+ C10_HOST_DEVICE explicit qint32(int32_t val) : val_(val) {}
16
+ };
17
+
18
+ } // namespace c10
19
+
20
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
21
+ using c10::qint32;
22
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/qint8.h ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * This is the data type for quantized Tensors. Right now we only have
10
+ * qint8 which is for 8 bit Tensors, and qint32 for 32 bit int Tensors,
11
+ * we might have 4 bit, 2 bit or 1 bit data types in the future.
12
+ */
13
+ struct alignas(1) qint8 {
14
+ using underlying = int8_t;
15
+ int8_t val_;
16
+ qint8() = default;
17
+ C10_HOST_DEVICE explicit qint8(int8_t val) : val_(val) {}
18
+ };
19
+
20
+ } // namespace c10
21
+
22
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
23
+ using c10::qint8;
24
+ HIDDEN_NAMESPACE_END(torch, headeronly)
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/torch/headeronly/util/quint2x4.h ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ #include <cstdint>
3
+
4
+ #include <torch/headeronly/macros/Macros.h>
5
+
6
+ namespace c10 {
7
+
8
+ /**
9
+ * quint2x4 is for un-signed 2 bit quantized Tensors that are packed to byte
10
+ * boundary.
11
+ */
12
+ struct alignas(1) quint2x4 {
13
+ using underlying = uint8_t;
14
+ uint8_t val_;
15
+ quint2x4() = default;
16
+ C10_HOST_DEVICE explicit quint2x4(uint8_t val) : val_(val) {}
17
+ };
18
+
19
+ } // namespace c10
20
+
21
+ HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
22
+ using c10::quint2x4;
23
+ HIDDEN_NAMESPACE_END(torch, headeronly)