Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PyInterpreterHooks.h +45 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PyObjectSlot.h +70 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PythonDispatcherTLS.h +34 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/SizesAndStrides.h +336 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/TorchDispatchModeTLS.h +72 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/VirtualGuardImpl.h +117 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/alloc_cpu.h +32 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAAlgorithm.h +36 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAAllocatorConfig.h +211 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDACachingAllocator.h +582 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDADeviceAssertion.h +103 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDADeviceAssertionHost.h +169 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAException.h +102 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAFunctions.h +131 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAGraphsC10Utils.h +81 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAGuard.h +311 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAMacros.h +56 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAMathCompat.h +157 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAMiscFunctions.h +20 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAStream.h +273 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/driver_api.h +124 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/impl/CUDAGuardImpl.h +270 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/impl/CUDATest.h +14 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/impl/cuda_cmake_macros.h +11 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/Export.h +6 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/Macros.h +6 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/cmake_macros.h +10 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/atomic.h +182 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/common.h +50 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/error.h +116 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/expm1f.h +102 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/igamma.h +749 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/indexing.h +1050 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/random.h +83 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/reduction_utils.h +364 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/special_math.h +2064 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/utils.h +386 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/mobile/CPUCachingAllocator.h +111 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/mobile/CPUProfilingAllocator.h +157 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/test/util/Macros.h +14 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/test/util/complex_math_test_common.h +672 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/test/util/complex_test_common.h +663 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/AbortHandler.h +88 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/AlignOf.h +181 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/ApproximateClock.h +120 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/Array.h +23 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/ArrayRef.h +326 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16-inl.h +6 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16-math.h +304 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16.h +6 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PyInterpreterHooks.h
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 5 |
+
#include <c10/macros/Export.h>
|
| 6 |
+
#include <c10/util/Registry.h>
|
| 7 |
+
#include <memory>
|
| 8 |
+
|
| 9 |
+
namespace c10::impl {
|
| 10 |
+
|
| 11 |
+
// Minimal interface for PyInterpreter hooks
|
| 12 |
+
struct C10_API PyInterpreterHooksInterface {
|
| 13 |
+
virtual ~PyInterpreterHooksInterface() = default;
|
| 14 |
+
|
| 15 |
+
// Get the PyInterpreter instance
|
| 16 |
+
// Stub implementation throws error when Python is not available
|
| 17 |
+
virtual PyInterpreter* getPyInterpreter() const {
|
| 18 |
+
TORCH_CHECK(
|
| 19 |
+
false,
|
| 20 |
+
"PyTorch was compiled without Python support. "
|
| 21 |
+
"Cannot access Python interpreter from C++.");
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
struct C10_API PyInterpreterHooksArgs{};
|
| 26 |
+
|
| 27 |
+
C10_DECLARE_REGISTRY(
|
| 28 |
+
PyInterpreterHooksRegistry,
|
| 29 |
+
PyInterpreterHooksInterface,
|
| 30 |
+
PyInterpreterHooksArgs);
|
| 31 |
+
|
| 32 |
+
#define REGISTER_PYTHON_HOOKS(clsname) \
|
| 33 |
+
C10_REGISTER_CLASS(PyInterpreterHooksRegistry, clsname, clsname)
|
| 34 |
+
|
| 35 |
+
// Get the global PyInterpreter hooks instance
|
| 36 |
+
C10_API const PyInterpreterHooksInterface& getPyInterpreterHooks();
|
| 37 |
+
|
| 38 |
+
// Helper function to get the global interpreter
|
| 39 |
+
C10_API PyInterpreter* getGlobalPyInterpreter();
|
| 40 |
+
|
| 41 |
+
} // namespace c10::impl
|
| 42 |
+
|
| 43 |
+
#else
|
| 44 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 45 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PyObjectSlot.h
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/HermeticPyObjectTLS.h>
|
| 5 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 6 |
+
#include <c10/core/impl/PyInterpreterHooks.h>
|
| 7 |
+
#include <c10/util/python_stub.h>
|
| 8 |
+
#include <optional>
|
| 9 |
+
|
| 10 |
+
#include <atomic>
|
| 11 |
+
|
| 12 |
+
namespace torch::utils {
|
| 13 |
+
class PyObjectPreservation;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
namespace c10::impl {
|
| 17 |
+
|
| 18 |
+
struct C10_API PyObjectSlot {
|
| 19 |
+
public:
|
| 20 |
+
PyObjectSlot() : pyobj_interpreter_(nullptr), pyobj_(nullptr) {}
|
| 21 |
+
|
| 22 |
+
// Query the PyObject interpreter. This may return null if there is no
|
| 23 |
+
// interpreter.
|
| 24 |
+
PyInterpreter* pyobj_interpreter() const {
|
| 25 |
+
return pyobj_interpreter_.load(std::memory_order_acquire);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
PyInterpreter& load_pyobj_interpreter() const {
|
| 29 |
+
auto interpreter = pyobj_interpreter_.load(std::memory_order_acquire);
|
| 30 |
+
TORCH_INTERNAL_ASSERT(
|
| 31 |
+
interpreter, "cannot access PyObject for Tensor - no interpreter set");
|
| 32 |
+
return *interpreter;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
PyObject* load_pyobj() const {
|
| 36 |
+
return pyobj_.load(std::memory_order_acquire);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
void store_pyobj(PyObject* obj) {
|
| 40 |
+
pyobj_.store(obj, std::memory_order_release);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
bool has_unique_reference() const {
|
| 44 |
+
PyObject* pyobj = load_pyobj();
|
| 45 |
+
return pyobj != nullptr && load_pyobj_interpreter()->refcnt(pyobj) == 1;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
void clear() {
|
| 49 |
+
pyobj_.store(nullptr, std::memory_order_relaxed);
|
| 50 |
+
pyobj_interpreter_.store(nullptr, std::memory_order_relaxed);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
private:
|
| 54 |
+
// This is now always the global interpreter if the PyObject is set.
|
| 55 |
+
// Maybe we can remove this field some day...
|
| 56 |
+
std::atomic<PyInterpreter*> pyobj_interpreter_;
|
| 57 |
+
|
| 58 |
+
// The PyObject representing this Tensor or nullptr. Ownership is managed
|
| 59 |
+
// by intrusive_ptr. By the time the PyObjectSlot is destroyed, this
|
| 60 |
+
// reference is already dead.
|
| 61 |
+
std::atomic<PyObject*> pyobj_;
|
| 62 |
+
|
| 63 |
+
friend class torch::utils::PyObjectPreservation;
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
} // namespace c10::impl
|
| 67 |
+
|
| 68 |
+
#else
|
| 69 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 70 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/PythonDispatcherTLS.h
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 5 |
+
#include <c10/macros/Export.h>
|
| 6 |
+
|
| 7 |
+
namespace c10::impl {
|
| 8 |
+
|
| 9 |
+
struct C10_API PythonDispatcherTLS {
|
| 10 |
+
static void set_state(PyInterpreter* state);
|
| 11 |
+
static PyInterpreter* get_state();
|
| 12 |
+
static void reset_state();
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
struct C10_API DisablePythonDispatcher {
|
| 16 |
+
DisablePythonDispatcher() : old_(PythonDispatcherTLS::get_state()) {
|
| 17 |
+
PythonDispatcherTLS::set_state({});
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
DisablePythonDispatcher(DisablePythonDispatcher&& other) = delete;
|
| 21 |
+
DisablePythonDispatcher(const DisablePythonDispatcher&) = delete;
|
| 22 |
+
DisablePythonDispatcher& operator=(const DisablePythonDispatcher&) = delete;
|
| 23 |
+
DisablePythonDispatcher& operator=(DisablePythonDispatcher&&) = delete;
|
| 24 |
+
~DisablePythonDispatcher() {
|
| 25 |
+
PythonDispatcherTLS::set_state(old_);
|
| 26 |
+
}
|
| 27 |
+
PyInterpreter* old_;
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
} // namespace c10::impl
|
| 31 |
+
|
| 32 |
+
#else
|
| 33 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 34 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/SizesAndStrides.h
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <algorithm>
|
| 5 |
+
#include <cstdint>
|
| 6 |
+
|
| 7 |
+
#include <c10/macros/Macros.h>
|
| 8 |
+
#include <c10/util/ArrayRef.h>
|
| 9 |
+
#include <c10/util/SmallVector.h>
|
| 10 |
+
|
| 11 |
+
#define C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE 5
|
| 12 |
+
|
| 13 |
+
namespace c10::impl {
|
| 14 |
+
|
| 15 |
+
// Packed container for TensorImpl sizes and strides.
|
| 16 |
+
// This design improves on the previous approach of using a pair of
|
| 17 |
+
// c10::SmallVector<int64_t, 5> by specializing for the operations we
|
| 18 |
+
// actually use and enforcing that the number of sizes is the same as
|
| 19 |
+
// the number of strides. The memory layout is as follows:
|
| 20 |
+
//
|
| 21 |
+
// 1 size_t for the size
|
| 22 |
+
// 5 eightbytes of inline sizes and 5 eightbytes of inline strides, OR pointer
|
| 23 |
+
// to out-of-line array
|
| 24 |
+
class C10_API SizesAndStrides {
|
| 25 |
+
public:
|
| 26 |
+
// TODO: different iterator types for sizes & strides to prevent
|
| 27 |
+
// mixing the two accidentally.
|
| 28 |
+
using sizes_iterator = int64_t*;
|
| 29 |
+
using sizes_const_iterator = const int64_t*;
|
| 30 |
+
using strides_iterator = int64_t*;
|
| 31 |
+
using strides_const_iterator = const int64_t*;
|
| 32 |
+
|
| 33 |
+
SizesAndStrides() {
|
| 34 |
+
size_at_unchecked(0) = 0;
|
| 35 |
+
stride_at_unchecked(0) = 1;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
~SizesAndStrides() {
|
| 39 |
+
if (C10_UNLIKELY(!isInline())) {
|
| 40 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 41 |
+
free(outOfLineStorage_);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
SizesAndStrides(const SizesAndStrides& rhs) : size_(rhs.size_) {
|
| 46 |
+
if (C10_LIKELY(rhs.isInline())) {
|
| 47 |
+
copyDataInline(rhs);
|
| 48 |
+
} else {
|
| 49 |
+
allocateOutOfLineStorage(size_);
|
| 50 |
+
copyDataOutline(rhs);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
bool operator==(const SizesAndStrides& other) const {
|
| 55 |
+
if (size_ != other.size_) {
|
| 56 |
+
return false;
|
| 57 |
+
}
|
| 58 |
+
return !(
|
| 59 |
+
isInline()
|
| 60 |
+
? std::memcmp(
|
| 61 |
+
inlineStorage_, other.inlineStorage_, sizeof(inlineStorage_))
|
| 62 |
+
: std::memcmp(
|
| 63 |
+
outOfLineStorage_,
|
| 64 |
+
other.outOfLineStorage_,
|
| 65 |
+
storageBytes(size_)));
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
bool operator!=(const SizesAndStrides& other) const {
|
| 69 |
+
return !(*this == other);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
SizesAndStrides& operator=(const SizesAndStrides& rhs) {
|
| 73 |
+
if (this == &rhs) {
|
| 74 |
+
return *this;
|
| 75 |
+
}
|
| 76 |
+
if (C10_LIKELY(rhs.isInline())) {
|
| 77 |
+
if (C10_UNLIKELY(!isInline())) {
|
| 78 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 79 |
+
free(outOfLineStorage_);
|
| 80 |
+
}
|
| 81 |
+
copyDataInline(rhs);
|
| 82 |
+
} else {
|
| 83 |
+
if (isInline()) {
|
| 84 |
+
allocateOutOfLineStorage(rhs.size_);
|
| 85 |
+
} else {
|
| 86 |
+
resizeOutOfLineStorage(rhs.size_);
|
| 87 |
+
}
|
| 88 |
+
copyDataOutline(rhs);
|
| 89 |
+
}
|
| 90 |
+
size_ = rhs.size_;
|
| 91 |
+
return *this;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
// Move from rhs. rhs.size() == 0 afterwards.
|
| 95 |
+
SizesAndStrides(SizesAndStrides&& rhs) noexcept : size_(rhs.size_) {
|
| 96 |
+
if (C10_LIKELY(isInline())) {
|
| 97 |
+
memcpy(inlineStorage_, rhs.inlineStorage_, sizeof(inlineStorage_));
|
| 98 |
+
} else {
|
| 99 |
+
outOfLineStorage_ = rhs.outOfLineStorage_;
|
| 100 |
+
rhs.outOfLineStorage_ = nullptr;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
rhs.size_ = 0;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// Move from rhs. rhs.size() == 0 afterwards.
|
| 107 |
+
SizesAndStrides& operator=(SizesAndStrides&& rhs) noexcept {
|
| 108 |
+
if (this == &rhs) {
|
| 109 |
+
return *this;
|
| 110 |
+
}
|
| 111 |
+
if (C10_LIKELY(rhs.isInline())) {
|
| 112 |
+
if (C10_UNLIKELY(!isInline())) {
|
| 113 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 114 |
+
free(outOfLineStorage_);
|
| 115 |
+
}
|
| 116 |
+
copyDataInline(rhs);
|
| 117 |
+
} else {
|
| 118 |
+
// They're outline. We're going to steal their vector.
|
| 119 |
+
if (!isInline()) {
|
| 120 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 121 |
+
free(outOfLineStorage_);
|
| 122 |
+
}
|
| 123 |
+
outOfLineStorage_ = rhs.outOfLineStorage_;
|
| 124 |
+
rhs.outOfLineStorage_ = nullptr;
|
| 125 |
+
}
|
| 126 |
+
size_ = rhs.size_;
|
| 127 |
+
rhs.size_ = 0;
|
| 128 |
+
|
| 129 |
+
return *this;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
size_t size() const noexcept {
|
| 133 |
+
return size_;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
const int64_t* sizes_data() const noexcept {
|
| 137 |
+
if (C10_LIKELY(isInline())) {
|
| 138 |
+
return &inlineStorage_[0];
|
| 139 |
+
} else {
|
| 140 |
+
return &outOfLineStorage_[0];
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
int64_t* sizes_data() noexcept {
|
| 145 |
+
if (C10_LIKELY(isInline())) {
|
| 146 |
+
return &inlineStorage_[0];
|
| 147 |
+
} else {
|
| 148 |
+
return &outOfLineStorage_[0];
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
sizes_const_iterator sizes_begin() const noexcept {
|
| 153 |
+
return sizes_data();
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
sizes_iterator sizes_begin() noexcept {
|
| 157 |
+
return sizes_data();
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
sizes_const_iterator sizes_end() const noexcept {
|
| 161 |
+
return sizes_begin() + size();
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
sizes_iterator sizes_end() noexcept {
|
| 165 |
+
return sizes_begin() + size();
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
IntArrayRef sizes_arrayref() const noexcept {
|
| 169 |
+
return IntArrayRef{sizes_data(), size()};
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
void set_sizes(IntArrayRef newSizes) {
|
| 173 |
+
resize(newSizes.size());
|
| 174 |
+
std::copy(newSizes.begin(), newSizes.end(), sizes_begin());
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
void set_strides(IntArrayRef strides) {
|
| 178 |
+
TORCH_INTERNAL_ASSERT(strides.size() == size());
|
| 179 |
+
std::copy(strides.begin(), strides.end(), strides_begin());
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
const int64_t* strides_data() const noexcept {
|
| 183 |
+
if (C10_LIKELY(isInline())) {
|
| 184 |
+
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
|
| 185 |
+
} else {
|
| 186 |
+
return &outOfLineStorage_[size()];
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
int64_t* strides_data() noexcept {
|
| 191 |
+
if (C10_LIKELY(isInline())) {
|
| 192 |
+
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
|
| 193 |
+
} else {
|
| 194 |
+
return &outOfLineStorage_[size()];
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
strides_const_iterator strides_begin() const noexcept {
|
| 199 |
+
if (C10_LIKELY(isInline())) {
|
| 200 |
+
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
|
| 201 |
+
} else {
|
| 202 |
+
return &outOfLineStorage_[size()];
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
strides_iterator strides_begin() noexcept {
|
| 207 |
+
if (C10_LIKELY(isInline())) {
|
| 208 |
+
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
|
| 209 |
+
} else {
|
| 210 |
+
return &outOfLineStorage_[size()];
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
strides_const_iterator strides_end() const noexcept {
|
| 215 |
+
return strides_begin() + size();
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
strides_iterator strides_end() noexcept {
|
| 219 |
+
return strides_begin() + size();
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
IntArrayRef strides_arrayref() const noexcept {
|
| 223 |
+
return IntArrayRef{strides_data(), size()};
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
// Size accessors.
|
| 227 |
+
int64_t size_at(size_t idx) const noexcept {
|
| 228 |
+
assert(idx < size());
|
| 229 |
+
return sizes_data()[idx];
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
int64_t& size_at(size_t idx) noexcept {
|
| 233 |
+
assert(idx < size());
|
| 234 |
+
return sizes_data()[idx];
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
int64_t size_at_unchecked(size_t idx) const noexcept {
|
| 238 |
+
return sizes_data()[idx];
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
int64_t& size_at_unchecked(size_t idx) noexcept {
|
| 242 |
+
return sizes_data()[idx];
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// Size accessors.
|
| 246 |
+
int64_t stride_at(size_t idx) const noexcept {
|
| 247 |
+
assert(idx < size());
|
| 248 |
+
return strides_data()[idx];
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
int64_t& stride_at(size_t idx) noexcept {
|
| 252 |
+
assert(idx < size());
|
| 253 |
+
return strides_data()[idx];
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
int64_t stride_at_unchecked(size_t idx) const noexcept {
|
| 257 |
+
return strides_data()[idx];
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
int64_t& stride_at_unchecked(size_t idx) noexcept {
|
| 261 |
+
return strides_data()[idx];
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
void resize(size_t newSize) {
|
| 265 |
+
const auto oldSize = size();
|
| 266 |
+
if (newSize == oldSize) {
|
| 267 |
+
return;
|
| 268 |
+
}
|
| 269 |
+
if (C10_LIKELY(
|
| 270 |
+
newSize <= C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE && isInline())) {
|
| 271 |
+
if (oldSize < newSize) {
|
| 272 |
+
const auto bytesToZero =
|
| 273 |
+
(newSize - oldSize) * sizeof(inlineStorage_[0]);
|
| 274 |
+
memset(&inlineStorage_[oldSize], 0, bytesToZero);
|
| 275 |
+
memset(
|
| 276 |
+
&inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE + oldSize],
|
| 277 |
+
0,
|
| 278 |
+
bytesToZero);
|
| 279 |
+
}
|
| 280 |
+
size_ = newSize;
|
| 281 |
+
} else {
|
| 282 |
+
resizeSlowPath(newSize, oldSize);
|
| 283 |
+
}
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
void resizeSlowPath(size_t newSize, size_t oldSize);
|
| 287 |
+
|
| 288 |
+
private:
|
| 289 |
+
bool isInline() const noexcept {
|
| 290 |
+
return size_ <= C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
void copyDataInline(const SizesAndStrides& rhs) {
|
| 294 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(rhs.isInline());
|
| 295 |
+
memcpy(inlineStorage_, rhs.inlineStorage_, sizeof(inlineStorage_));
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
static size_t storageBytes(size_t size) noexcept {
|
| 299 |
+
return size * 2 * sizeof(int64_t);
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
void allocateOutOfLineStorage(size_t size) {
|
| 303 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 304 |
+
outOfLineStorage_ = static_cast<int64_t*>(malloc(storageBytes(size)));
|
| 305 |
+
TORCH_CHECK(
|
| 306 |
+
outOfLineStorage_,
|
| 307 |
+
"Could not allocate memory for Tensor SizesAndStrides!");
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
void resizeOutOfLineStorage(size_t newSize) {
|
| 311 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!isInline());
|
| 312 |
+
outOfLineStorage_ = static_cast<int64_t*>(
|
| 313 |
+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
| 314 |
+
realloc(outOfLineStorage_, storageBytes(newSize)));
|
| 315 |
+
TORCH_CHECK(
|
| 316 |
+
outOfLineStorage_,
|
| 317 |
+
"Could not allocate memory for Tensor SizesAndStrides!");
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
void copyDataOutline(const SizesAndStrides& rhs) noexcept {
|
| 321 |
+
memcpy(outOfLineStorage_, rhs.outOfLineStorage_, storageBytes(rhs.size_));
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
size_t size_{1};
|
| 325 |
+
union {
|
| 326 |
+
int64_t* outOfLineStorage_;
|
| 327 |
+
// NOLINTNEXTLINE(*c-array*)
|
| 328 |
+
int64_t inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE * 2]{};
|
| 329 |
+
};
|
| 330 |
+
};
|
| 331 |
+
|
| 332 |
+
} // namespace c10::impl
|
| 333 |
+
|
| 334 |
+
#else
|
| 335 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 336 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/TorchDispatchModeTLS.h
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/SafePyObject.h>
|
| 5 |
+
#include <c10/macros/Export.h>
|
| 6 |
+
|
| 7 |
+
namespace c10::impl {
|
| 8 |
+
|
| 9 |
+
enum class TorchDispatchModeKey : int8_t {
|
| 10 |
+
FAKE,
|
| 11 |
+
PROXY,
|
| 12 |
+
FUNCTIONAL,
|
| 13 |
+
NUM_MODE_KEYS
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
using PyObject_TorchDispatchMode = SafePyObjectT<TorchDispatchModeKey>;
|
| 17 |
+
|
| 18 |
+
struct C10_API TorchDispatchModeTLS {
|
| 19 |
+
// This API is NOT invariant safe.
|
| 20 |
+
// It must not take in an infra mode that uses TorchDispatchModeKey
|
| 21 |
+
// If you're pushing an infra mode onto the stack, we expect
|
| 22 |
+
// you to use set_mode
|
| 23 |
+
static void push_non_infra_mode_onto_stack(
|
| 24 |
+
std::shared_ptr<PyObject_TorchDispatchMode> mode);
|
| 25 |
+
// Pops the top mode of the stack,
|
| 26 |
+
// giving precedence to user modes before attempting to pop
|
| 27 |
+
// any infra modes
|
| 28 |
+
static const std::shared_ptr<PyObject_TorchDispatchMode> pop_stack();
|
| 29 |
+
// Returns the highest-priority infra mode on the stack,
|
| 30 |
+
// along with its mode key.
|
| 31 |
+
static const std::
|
| 32 |
+
tuple<std::shared_ptr<PyObject_TorchDispatchMode>, TorchDispatchModeKey>
|
| 33 |
+
pop_highest_infra_mode();
|
| 34 |
+
|
| 35 |
+
static const std::shared_ptr<PyObject_TorchDispatchMode>& get_stack_at(
|
| 36 |
+
int64_t idx);
|
| 37 |
+
static int64_t stack_len();
|
| 38 |
+
|
| 39 |
+
static const std::optional<std::shared_ptr<PyObject_TorchDispatchMode>>
|
| 40 |
+
get_mode(TorchDispatchModeKey mode_key);
|
| 41 |
+
static const std::optional<std::shared_ptr<PyObject_TorchDispatchMode>>
|
| 42 |
+
unset_mode(TorchDispatchModeKey mode_key);
|
| 43 |
+
static void set_mode(
|
| 44 |
+
const std::shared_ptr<PyObject_TorchDispatchMode>& mode,
|
| 45 |
+
TorchDispatchModeKey mode_key);
|
| 46 |
+
|
| 47 |
+
static const TorchDispatchModeTLS& get_state();
|
| 48 |
+
static void set_state(TorchDispatchModeTLS state);
|
| 49 |
+
|
| 50 |
+
static bool any_modes_set(bool skip_infra_modes = false);
|
| 51 |
+
|
| 52 |
+
private:
|
| 53 |
+
std::vector<std::shared_ptr<PyObject_TorchDispatchMode>> stack_;
|
| 54 |
+
// Users are allowed to push multiple ProxyTorchDispatchMode objects onto the
|
| 55 |
+
// stack
|
| 56 |
+
// However, we only allow a single FakeTensorMode onto the stack at a time
|
| 57 |
+
// (Pushing additional FakeTensorModes onto the stack is a no-op)
|
| 58 |
+
std::array<
|
| 59 |
+
std::optional<std::shared_ptr<PyObject_TorchDispatchMode>>,
|
| 60 |
+
static_cast<size_t>(TorchDispatchModeKey::NUM_MODE_KEYS)>
|
| 61 |
+
infra_modes_;
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
C10_API bool dispatch_mode_enabled();
|
| 65 |
+
|
| 66 |
+
C10_API std::string to_string(TorchDispatchModeKey mode_key);
|
| 67 |
+
|
| 68 |
+
} // namespace c10::impl
|
| 69 |
+
|
| 70 |
+
#else
|
| 71 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 72 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/VirtualGuardImpl.h
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/DeviceGuardImplInterface.h>
|
| 5 |
+
|
| 6 |
+
namespace c10::impl {
|
| 7 |
+
|
| 8 |
+
/**
|
| 9 |
+
* An implementation of DeviceGuardImplInterface which delegates
|
| 10 |
+
* to virtual dispatch on the DeviceGuardImpl registry.
|
| 11 |
+
*/
|
| 12 |
+
class VirtualGuardImpl final : public DeviceGuardImplInterface {
|
| 13 |
+
public:
|
| 14 |
+
VirtualGuardImpl(DeviceType device_type)
|
| 15 |
+
: impl_(getDeviceGuardImpl(device_type)) {}
|
| 16 |
+
// This constructor exists purely for testing
|
| 17 |
+
VirtualGuardImpl(const DeviceGuardImplInterface* impl) : impl_(impl) {}
|
| 18 |
+
|
| 19 |
+
// Copying and moving is OK!
|
| 20 |
+
VirtualGuardImpl(const VirtualGuardImpl&) = default;
|
| 21 |
+
VirtualGuardImpl& operator=(const VirtualGuardImpl&) = default;
|
| 22 |
+
VirtualGuardImpl(VirtualGuardImpl&&) noexcept = default;
|
| 23 |
+
VirtualGuardImpl& operator=(VirtualGuardImpl&&) noexcept = default;
|
| 24 |
+
~VirtualGuardImpl() override = default;
|
| 25 |
+
|
| 26 |
+
DeviceType type() const override {
|
| 27 |
+
return impl_->type();
|
| 28 |
+
}
|
| 29 |
+
Device exchangeDevice(Device d) const override {
|
| 30 |
+
return impl_->exchangeDevice(d);
|
| 31 |
+
}
|
| 32 |
+
Device getDevice() const override {
|
| 33 |
+
return impl_->getDevice();
|
| 34 |
+
}
|
| 35 |
+
void setDevice(Device d) const override {
|
| 36 |
+
impl_->setDevice(d);
|
| 37 |
+
}
|
| 38 |
+
void uncheckedSetDevice(Device d) const noexcept override {
|
| 39 |
+
impl_->uncheckedSetDevice(d);
|
| 40 |
+
}
|
| 41 |
+
Stream getStream(Device d) const override {
|
| 42 |
+
return impl_->getStream(d);
|
| 43 |
+
}
|
| 44 |
+
Stream getNewStream(Device d, int priority = 0) const override {
|
| 45 |
+
return impl_->getNewStream(d, priority);
|
| 46 |
+
}
|
| 47 |
+
Stream getDefaultStream(Device d) const override {
|
| 48 |
+
return impl_->getDefaultStream(d);
|
| 49 |
+
}
|
| 50 |
+
Stream getStreamFromGlobalPool(Device d, bool isHighPriority = false)
|
| 51 |
+
const override {
|
| 52 |
+
return impl_->getStreamFromGlobalPool(d, isHighPriority);
|
| 53 |
+
}
|
| 54 |
+
Stream exchangeStream(Stream s) const override {
|
| 55 |
+
return impl_->exchangeStream(s);
|
| 56 |
+
}
|
| 57 |
+
DeviceIndex deviceCount() const noexcept override {
|
| 58 |
+
return impl_->deviceCount();
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
DeviceCapability getDeviceCapability(Device d) const override {
|
| 62 |
+
return impl_->getDeviceCapability(d);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
// Event functions
|
| 66 |
+
void record(
|
| 67 |
+
void** event,
|
| 68 |
+
const Stream& stream,
|
| 69 |
+
const DeviceIndex device_index,
|
| 70 |
+
const EventFlag flag) const override {
|
| 71 |
+
impl_->record(event, stream, device_index, flag);
|
| 72 |
+
}
|
| 73 |
+
void block(void* event, const Stream& stream) const override {
|
| 74 |
+
impl_->block(event, stream);
|
| 75 |
+
}
|
| 76 |
+
bool queryEvent(void* event) const override {
|
| 77 |
+
return impl_->queryEvent(event);
|
| 78 |
+
}
|
| 79 |
+
void destroyEvent(void* event, const DeviceIndex device_index)
|
| 80 |
+
const noexcept override {
|
| 81 |
+
impl_->destroyEvent(event, device_index);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
bool queryStream(const Stream& stream) const override {
|
| 85 |
+
return impl_->queryStream(stream);
|
| 86 |
+
}
|
| 87 |
+
void synchronizeStream(const Stream& stream) const override {
|
| 88 |
+
impl_->synchronizeStream(stream);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
void recordDataPtrOnStream(const c10::DataPtr& data_ptr, const Stream& stream)
|
| 92 |
+
const override {
|
| 93 |
+
impl_->recordDataPtrOnStream(data_ptr, stream);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
double elapsedTime(void* event1, void* event2, const DeviceIndex device_index)
|
| 97 |
+
const override {
|
| 98 |
+
return impl_->elapsedTime(event1, event2, device_index);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
void synchronizeEvent(void* event) const override {
|
| 102 |
+
impl_->synchronizeEvent(event);
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
void synchronizeDevice(const DeviceIndex device_index) const override {
|
| 106 |
+
impl_->synchronizeDevice(device_index);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
private:
|
| 110 |
+
const DeviceGuardImplInterface* impl_ = nullptr;
|
| 111 |
+
};
|
| 112 |
+
|
| 113 |
+
} // namespace c10::impl
|
| 114 |
+
|
| 115 |
+
#else
|
| 116 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 117 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/core/impl/alloc_cpu.h
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/macros/Export.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
|
| 7 |
+
#include <cstddef>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
|
| 11 |
+
C10_API void* alloc_cpu(size_t nbytes);
|
| 12 |
+
C10_API void free_cpu(void* data);
|
| 13 |
+
|
| 14 |
+
#if defined(__linux__) && !defined(__ANDROID__)
|
| 15 |
+
C10_API size_t c10_compute_alignment(size_t nbytes);
|
| 16 |
+
#endif
|
| 17 |
+
|
| 18 |
+
#ifdef USE_MIMALLOC_ON_MKL
|
| 19 |
+
namespace mi_malloc_wrapper {
|
| 20 |
+
C10_API void* c10_mi_malloc(size_t size);
|
| 21 |
+
C10_API void* c10_mi_calloc(size_t count, size_t size);
|
| 22 |
+
C10_API void* c10_mi_realloc(void* p, size_t newsize);
|
| 23 |
+
C10_API void* c10_mi_malloc_aligned(size_t size, size_t alignment);
|
| 24 |
+
C10_API void c10_mi_free(void* p);
|
| 25 |
+
} // namespace mi_malloc_wrapper
|
| 26 |
+
#endif
|
| 27 |
+
|
| 28 |
+
} // namespace c10
|
| 29 |
+
|
| 30 |
+
#else
|
| 31 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 32 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAAlgorithm.h
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifdef THRUST_DEVICE_LOWER_BOUND_WORKS
|
| 3 |
+
#include <thrust/binary_search.h>
|
| 4 |
+
#include <thrust/device_vector.h>
|
| 5 |
+
#include <thrust/execution_policy.h>
|
| 6 |
+
#include <thrust/functional.h>
|
| 7 |
+
#endif
|
| 8 |
+
namespace c10::cuda {
|
| 9 |
+
#ifdef THRUST_DEVICE_LOWER_BOUND_WORKS
|
| 10 |
+
template <typename Iter, typename Scalar>
|
| 11 |
+
__forceinline__ __device__ Iter
|
| 12 |
+
lower_bound(Iter start, Iter end, Scalar value) {
|
| 13 |
+
return thrust::lower_bound(thrust::device, start, end, value);
|
| 14 |
+
}
|
| 15 |
+
#else
|
| 16 |
+
// thrust::lower_bound is broken on device, see
|
| 17 |
+
// https://github.com/NVIDIA/thrust/issues/1734 Implementation inspired by
|
| 18 |
+
// https://github.com/pytorch/pytorch/blob/805120ab572efef66425c9f595d9c6c464383336/aten/src/ATen/native/cuda/Bucketization.cu#L28
|
| 19 |
+
template <typename Iter, typename Scalar>
|
| 20 |
+
__device__ Iter lower_bound(Iter start, Iter end, Scalar value) {
|
| 21 |
+
while (start < end) {
|
| 22 |
+
auto mid = start + ((end - start) >> 1);
|
| 23 |
+
if (*mid < value) {
|
| 24 |
+
start = mid + 1;
|
| 25 |
+
} else {
|
| 26 |
+
end = mid;
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
return end;
|
| 30 |
+
}
|
| 31 |
+
#endif // THRUST_DEVICE_LOWER_BOUND_WORKS
|
| 32 |
+
} // namespace c10::cuda
|
| 33 |
+
|
| 34 |
+
#else
|
| 35 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 36 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAAllocatorConfig.h
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 7 |
+
#include <c10/util/Deprecated.h>
|
| 8 |
+
#include <c10/util/Exception.h>
|
| 9 |
+
#include <c10/util/env.h>
|
| 10 |
+
|
| 11 |
+
namespace c10::cuda::CUDACachingAllocator {
|
| 12 |
+
|
| 13 |
+
enum class Expandable_Segments_Handle_Type : int {
|
| 14 |
+
UNSPECIFIED = 0,
|
| 15 |
+
POSIX_FD = 1,
|
| 16 |
+
FABRIC_HANDLE = 2,
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
// Environment config parser
|
| 20 |
+
class C10_CUDA_API CUDAAllocatorConfig {
|
| 21 |
+
public:
|
| 22 |
+
C10_DEPRECATED_MESSAGE(
|
| 23 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::max_split_size() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::max_split_size() instead.")
|
| 24 |
+
static size_t max_split_size() {
|
| 25 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::max_split_size();
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
C10_DEPRECATED_MESSAGE(
|
| 29 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::garbage_collection_threshold() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::garbage_collection_threshold() instead.")
|
| 30 |
+
static double garbage_collection_threshold() {
|
| 31 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 32 |
+
garbage_collection_threshold();
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
static bool expandable_segments() {
|
| 36 |
+
bool enabled = c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 37 |
+
use_expandable_segments();
|
| 38 |
+
#ifndef PYTORCH_C10_DRIVER_API_SUPPORTED
|
| 39 |
+
if (enabled) {
|
| 40 |
+
TORCH_WARN_ONCE("expandable_segments not supported on this platform")
|
| 41 |
+
}
|
| 42 |
+
return false;
|
| 43 |
+
#else
|
| 44 |
+
return enabled;
|
| 45 |
+
#endif
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
static Expandable_Segments_Handle_Type expandable_segments_handle_type() {
|
| 49 |
+
return instance().m_expandable_segments_handle_type;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
static void set_expandable_segments_handle_type(
|
| 53 |
+
Expandable_Segments_Handle_Type handle_type) {
|
| 54 |
+
instance().m_expandable_segments_handle_type = handle_type;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
static bool release_lock_on_cudamalloc() {
|
| 58 |
+
return instance().m_release_lock_on_cudamalloc;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
static bool graph_capture_record_stream_reuse() {
|
| 62 |
+
return instance().m_graph_capture_record_stream_reuse;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
static double per_process_memory_fraction() {
|
| 66 |
+
return instance().m_per_process_memory_fraction;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
/** Pinned memory allocator settings */
|
| 70 |
+
static bool pinned_use_cuda_host_register() {
|
| 71 |
+
return instance().m_pinned_use_cuda_host_register;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
static size_t pinned_num_register_threads() {
|
| 75 |
+
return instance().m_pinned_num_register_threads;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
C10_DEPRECATED_MESSAGE(
|
| 79 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::pinned_use_background_threads() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::pinned_use_background_threads() instead.")
|
| 80 |
+
static bool pinned_use_background_threads() {
|
| 81 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 82 |
+
pinned_use_background_threads();
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
static size_t pinned_reserve_segment_size_mb() {
|
| 86 |
+
return instance().m_pinned_reserve_segment_size_mb;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
static size_t pinned_max_register_threads() {
|
| 90 |
+
// Based on the benchmark results, we see better allocation performance
|
| 91 |
+
// with 8 threads. However on future systems, we may need more threads
|
| 92 |
+
// and limiting this to 128 threads.
|
| 93 |
+
return 128;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
C10_DEPRECATED_MESSAGE(
|
| 97 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::roundup_power2_divisions() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::roundup_power2_divisions() instead.")
|
| 98 |
+
static size_t roundup_power2_divisions(size_t size) {
|
| 99 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 100 |
+
roundup_power2_divisions(size);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
C10_DEPRECATED_MESSAGE(
|
| 104 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::roundup_power2_divisions() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::roundup_power2_divisions() instead.")
|
| 105 |
+
static std::vector<size_t> roundup_power2_divisions() {
|
| 106 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 107 |
+
roundup_power2_divisions();
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
static size_t max_non_split_rounding_size() {
|
| 111 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 112 |
+
max_non_split_rounding_size();
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
C10_DEPRECATED_MESSAGE(
|
| 116 |
+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::last_allocator_settings() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::last_allocator_settings() instead.")
|
| 117 |
+
static std::string last_allocator_settings() {
|
| 118 |
+
return c10::CachingAllocator::getAllocatorSettings();
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
static CUDAAllocatorConfig& instance() {
|
| 122 |
+
static CUDAAllocatorConfig* s_instance = ([]() {
|
| 123 |
+
auto inst = new CUDAAllocatorConfig();
|
| 124 |
+
auto env = c10::utils::get_env("PYTORCH_CUDA_ALLOC_CONF");
|
| 125 |
+
#ifdef USE_ROCM
|
| 126 |
+
// convenience for ROCm users, allow alternative HIP token
|
| 127 |
+
if (!env.has_value()) {
|
| 128 |
+
env = c10::utils::get_env("PYTORCH_HIP_ALLOC_CONF");
|
| 129 |
+
}
|
| 130 |
+
#endif
|
| 131 |
+
// Note: keep the parsing order and logic stable to avoid potential
|
| 132 |
+
// performance regressions in internal tests.
|
| 133 |
+
if (!env.has_value()) {
|
| 134 |
+
env = c10::utils::get_env("PYTORCH_ALLOC_CONF");
|
| 135 |
+
}
|
| 136 |
+
if (env.has_value()) {
|
| 137 |
+
inst->parseArgs(env.value());
|
| 138 |
+
}
|
| 139 |
+
return inst;
|
| 140 |
+
})();
|
| 141 |
+
return *s_instance;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// Use `Construct On First Use Idiom` to avoid `Static Initialization Order`
|
| 145 |
+
// issue.
|
| 146 |
+
static const std::unordered_set<std::string>& getKeys() {
|
| 147 |
+
static std::unordered_set<std::string> keys{
|
| 148 |
+
"backend",
|
| 149 |
+
// keep BC for Rocm: `cuda` -> `cud` `a`, to avoid hipify issues
|
| 150 |
+
// NOLINTBEGIN(bugprone-suspicious-missing-comma,-warnings-as-errors)
|
| 151 |
+
"release_lock_on_cud"
|
| 152 |
+
"amalloc",
|
| 153 |
+
"pinned_use_cud"
|
| 154 |
+
"a_host_register",
|
| 155 |
+
// NOLINTEND(bugprone-suspicious-missing-comma,-warnings-as-errors)
|
| 156 |
+
"release_lock_on_hipmalloc",
|
| 157 |
+
"pinned_use_hip_host_register",
|
| 158 |
+
"graph_capture_record_stream_reuse",
|
| 159 |
+
"pinned_reserve_segment_size_mb",
|
| 160 |
+
"pinned_num_register_threads",
|
| 161 |
+
"per_process_memory_fraction"};
|
| 162 |
+
return keys;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
void parseArgs(const std::string& env);
|
| 166 |
+
|
| 167 |
+
private:
|
| 168 |
+
CUDAAllocatorConfig() = default;
|
| 169 |
+
|
| 170 |
+
size_t parseAllocatorConfig(
|
| 171 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 172 |
+
size_t i,
|
| 173 |
+
bool& used_cudaMallocAsync);
|
| 174 |
+
size_t parsePinnedUseCudaHostRegister(
|
| 175 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 176 |
+
size_t i);
|
| 177 |
+
size_t parsePinnedNumRegisterThreads(
|
| 178 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 179 |
+
size_t i);
|
| 180 |
+
size_t parsePinnedReserveSegmentSize(
|
| 181 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 182 |
+
size_t i);
|
| 183 |
+
size_t parseGraphCaptureRecordStreamReuse(
|
| 184 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 185 |
+
size_t i);
|
| 186 |
+
double parsePerProcessMemoryFraction(
|
| 187 |
+
const c10::CachingAllocator::ConfigTokenizer& tokenizer,
|
| 188 |
+
size_t i);
|
| 189 |
+
|
| 190 |
+
std::atomic<size_t> m_pinned_num_register_threads{1};
|
| 191 |
+
std::atomic<size_t> m_pinned_reserve_segment_size_mb{0};
|
| 192 |
+
std::atomic<Expandable_Segments_Handle_Type> m_expandable_segments_handle_type
|
| 193 |
+
#if CUDA_VERSION >= 12030
|
| 194 |
+
{Expandable_Segments_Handle_Type::UNSPECIFIED};
|
| 195 |
+
#else
|
| 196 |
+
{Expandable_Segments_Handle_Type::POSIX_FD};
|
| 197 |
+
#endif
|
| 198 |
+
std::atomic<bool> m_release_lock_on_cudamalloc{false};
|
| 199 |
+
std::atomic<bool> m_pinned_use_cuda_host_register{false};
|
| 200 |
+
std::atomic<bool> m_graph_capture_record_stream_reuse{false};
|
| 201 |
+
std::atomic<double> m_per_process_memory_fraction{1.0};
|
| 202 |
+
};
|
| 203 |
+
|
| 204 |
+
// Keep this for backwards compatibility
|
| 205 |
+
using c10::CachingAllocator::setAllocatorSettings;
|
| 206 |
+
|
| 207 |
+
} // namespace c10::cuda::CUDACachingAllocator
|
| 208 |
+
|
| 209 |
+
#else
|
| 210 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 211 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDACachingAllocator.h
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/AllocatorConfig.h>
|
| 5 |
+
#include <c10/core/CachingDeviceAllocator.h>
|
| 6 |
+
#include <c10/cuda/CUDAAllocatorConfig.h>
|
| 7 |
+
#include <c10/cuda/CUDAGraphsC10Utils.h>
|
| 8 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 9 |
+
#include <c10/cuda/CUDAStream.h>
|
| 10 |
+
#include <c10/util/ApproximateClock.h>
|
| 11 |
+
#include <c10/util/Exception.h>
|
| 12 |
+
#include <c10/util/Registry.h>
|
| 13 |
+
|
| 14 |
+
#include <atomic>
|
| 15 |
+
#include <cstddef>
|
| 16 |
+
#include <cstdint>
|
| 17 |
+
#include <functional>
|
| 18 |
+
#include <memory>
|
| 19 |
+
#include <string>
|
| 20 |
+
#include <unordered_set>
|
| 21 |
+
#include <utility>
|
| 22 |
+
|
| 23 |
+
namespace c10 {
|
| 24 |
+
|
| 25 |
+
// Caching allocator will execute every registered callback if it unable to find
|
| 26 |
+
// block inside of already allocated area.
|
| 27 |
+
class C10_CUDA_API FreeMemoryCallback {
|
| 28 |
+
public:
|
| 29 |
+
virtual ~FreeMemoryCallback() = default;
|
| 30 |
+
virtual bool Execute() = 0;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
C10_DECLARE_REGISTRY(FreeCudaMemoryCallbacksRegistry, FreeMemoryCallback);
|
| 34 |
+
#define REGISTER_FREE_MEMORY_CALLBACK(name, ...) \
|
| 35 |
+
C10_REGISTER_CLASS(FreeCudaMemoryCallbacksRegistry, name, __VA_ARGS__)
|
| 36 |
+
} // namespace c10
|
| 37 |
+
//
|
| 38 |
+
// TODO: Turn this into an honest to goodness class. I briefly attempted to do
|
| 39 |
+
// this, but it was a bit irritating to figure out how to also correctly
|
| 40 |
+
// apply pimpl pattern so I didn't have to leak any internal implementation
|
| 41 |
+
// details in the header (CUDACachingAllocator could be made a pimpl, but
|
| 42 |
+
// you also need to appropriately define a class which is a subclass
|
| 43 |
+
// of Allocator. Not impossible, but required a bit more surgery than
|
| 44 |
+
// I wanted to do at the time.)
|
| 45 |
+
//
|
| 46 |
+
// Why is this using a namespace rather than old-style THCCachingAllocator_
|
| 47 |
+
// prefix? Mostly because it made the HIPify rules easier to write; _ is
|
| 48 |
+
// not counted as a word boundary, so you would otherwise have to list each
|
| 49 |
+
// of these functions.
|
| 50 |
+
|
| 51 |
+
namespace c10::cuda::CUDACachingAllocator {
|
| 52 |
+
|
| 53 |
+
// Preserved only for BC reasons
|
| 54 |
+
// NOLINTNEXTLINE(misc-unused-using-decls)
|
| 55 |
+
using c10::CachingAllocator::kLargeBuffer;
|
| 56 |
+
using c10::CachingDeviceAllocator::DeviceStats;
|
| 57 |
+
|
| 58 |
+
typedef std::shared_ptr<GatheredContext> (*CreateContextFn)();
|
| 59 |
+
|
| 60 |
+
// Struct containing info of an allocation block (i.e. a fractional part of a
|
| 61 |
+
// cudaMalloc)..
|
| 62 |
+
struct BlockInfo {
|
| 63 |
+
size_t size = 0;
|
| 64 |
+
size_t requested_size = 0;
|
| 65 |
+
int32_t gc_counter = 0;
|
| 66 |
+
bool allocated = false;
|
| 67 |
+
bool active = false;
|
| 68 |
+
std::shared_ptr<GatheredContext>
|
| 69 |
+
context_when_allocated; // per-watcher context
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
// Struct containing info of a memory segment (i.e. one contiguous cudaMalloc).
|
| 73 |
+
struct SegmentInfo {
|
| 74 |
+
c10::DeviceIndex device = 0;
|
| 75 |
+
size_t address = 0;
|
| 76 |
+
size_t total_size = 0;
|
| 77 |
+
size_t requested_size = 0; // unrounded, actually requested size
|
| 78 |
+
size_t allocated_size = 0;
|
| 79 |
+
size_t active_size = 0;
|
| 80 |
+
cudaStream_t stream = nullptr;
|
| 81 |
+
bool is_large = false;
|
| 82 |
+
bool is_expandable = false;
|
| 83 |
+
MempoolId_t owner_private_pool_id = {0, 0};
|
| 84 |
+
std::vector<BlockInfo> blocks;
|
| 85 |
+
std::shared_ptr<GatheredContext> context_when_allocated;
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
struct AllocatorState {
|
| 89 |
+
virtual ~AllocatorState() = default;
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
union trace_time_ {
|
| 93 |
+
time_t t_;
|
| 94 |
+
approx_time_t approx_t_;
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
struct TraceEntry {
|
| 98 |
+
enum Action {
|
| 99 |
+
ALLOC, // API made to the caching allocator for new memory
|
| 100 |
+
FREE_REQUESTED, // API call made to the caching allocator to free memory
|
| 101 |
+
FREE_COMPLETED, // The allocator might have to delay a free because
|
| 102 |
+
// it is still in use on another stream via record_stream
|
| 103 |
+
// This event is generated when a free actually completes.
|
| 104 |
+
SEGMENT_ALLOC, // a call to cudaMalloc to get more memory from the OS
|
| 105 |
+
SEGMENT_FREE, // a call to cudaFree to return memory to the OS (e.g. to
|
| 106 |
+
// defragment or empty_caches)
|
| 107 |
+
SEGMENT_MAP, // a call to cuMemMap (used with expandable_segments)
|
| 108 |
+
SEGMENT_UNMAP, // unmap part of a segment (used with expandable segments)
|
| 109 |
+
SNAPSHOT, // a call to snapshot, used to correlate memory snapshots to trace
|
| 110 |
+
// events
|
| 111 |
+
OOM // the allocator threw an OutOfMemoryError (addr_ is the amount of free
|
| 112 |
+
// bytes reported by cuda)
|
| 113 |
+
};
|
| 114 |
+
TraceEntry(
|
| 115 |
+
Action action,
|
| 116 |
+
c10::DeviceIndex device,
|
| 117 |
+
size_t addr,
|
| 118 |
+
size_t size,
|
| 119 |
+
cudaStream_t stream,
|
| 120 |
+
MempoolId_t mempool,
|
| 121 |
+
approx_time_t time,
|
| 122 |
+
std::shared_ptr<GatheredContext> context = nullptr,
|
| 123 |
+
std::string compile_context = "",
|
| 124 |
+
std::string user_metadata = "")
|
| 125 |
+
: action_(action),
|
| 126 |
+
device_(device),
|
| 127 |
+
addr_(addr),
|
| 128 |
+
context_(std::move(context)),
|
| 129 |
+
stream_(stream),
|
| 130 |
+
size_(size),
|
| 131 |
+
mempool_(std::move(mempool)),
|
| 132 |
+
compile_context_(std::move(compile_context)),
|
| 133 |
+
user_metadata_(std::move(user_metadata)) {
|
| 134 |
+
time_.approx_t_ = time;
|
| 135 |
+
}
|
| 136 |
+
Action action_;
|
| 137 |
+
c10::DeviceIndex device_;
|
| 138 |
+
size_t addr_; // for OOM, this is the amount of free bytes reported by cuda
|
| 139 |
+
std::shared_ptr<GatheredContext> context_;
|
| 140 |
+
cudaStream_t stream_{};
|
| 141 |
+
size_t size_;
|
| 142 |
+
MempoolId_t mempool_;
|
| 143 |
+
trace_time_ time_{};
|
| 144 |
+
std::string compile_context_;
|
| 145 |
+
std::string user_metadata_;
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
// Calls made by record_function will save annotations
|
| 149 |
+
struct AnnotationEntry {
|
| 150 |
+
AnnotationEntry(c10::DeviceIndex device, approx_time_t time)
|
| 151 |
+
: device_(device) {
|
| 152 |
+
time_.approx_t_ = time;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
void recordUserMetadata(const std::string& name, std::string value) {
|
| 156 |
+
metadata_[name] = std::move(value);
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
c10::DeviceIndex device_;
|
| 160 |
+
trace_time_ time_{};
|
| 161 |
+
std::unordered_map<std::string, std::string> metadata_;
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
struct AllocatorConfigInfo {
|
| 165 |
+
double garbage_collection_threshold;
|
| 166 |
+
size_t max_split_size;
|
| 167 |
+
size_t pinned_num_register_threads;
|
| 168 |
+
bool expandable_segments;
|
| 169 |
+
bool release_lock_on_malloc;
|
| 170 |
+
bool pinned_use_host_register;
|
| 171 |
+
bool graph_capture_record_stream_reuse;
|
| 172 |
+
std::string last_allocator_settings;
|
| 173 |
+
std::vector<size_t> roundup_power2_divisions;
|
| 174 |
+
};
|
| 175 |
+
|
| 176 |
+
struct SnapshotInfo {
|
| 177 |
+
std::vector<SegmentInfo> segments;
|
| 178 |
+
std::vector<std::vector<TraceEntry>> device_traces;
|
| 179 |
+
std::vector<AnnotationEntry> external_annotations;
|
| 180 |
+
AllocatorConfigInfo config_metadata;
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
// returns the pointers freed in the pool
|
| 184 |
+
// and the pointers allocated. Note: a pointer
|
| 185 |
+
// may appear in both freed and allocated
|
| 186 |
+
struct CheckpointDelta {
|
| 187 |
+
std::vector<void*> ptrs_freed;
|
| 188 |
+
std::vector<at::DataPtr> dataptrs_allocd;
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
enum struct RecordContext {
|
| 192 |
+
NEVER = 0,
|
| 193 |
+
STATE = 1, // only keep stacks for active allocations
|
| 194 |
+
ALLOC = 2, // additionally keep stacks for allocations in the trace history
|
| 195 |
+
ALL = 3, // additionally record stacks for when something is freed
|
| 196 |
+
};
|
| 197 |
+
|
| 198 |
+
using OutOfMemoryObserver = std::function<void(
|
| 199 |
+
int64_t device,
|
| 200 |
+
size_t allocated,
|
| 201 |
+
size_t device_total,
|
| 202 |
+
size_t device_free)>;
|
| 203 |
+
|
| 204 |
+
using AllocatorTraceTracker = std::function<void(const TraceEntry&)>;
|
| 205 |
+
|
| 206 |
+
struct ShareableHandle {
|
| 207 |
+
ptrdiff_t offset;
|
| 208 |
+
std::string handle;
|
| 209 |
+
};
|
| 210 |
+
|
| 211 |
+
struct StreamSegmentSize {
|
| 212 |
+
StreamSegmentSize(cudaStream_t s, bool small, size_t sz)
|
| 213 |
+
: stream(s), is_small_pool(small), total_size(sz) {}
|
| 214 |
+
cudaStream_t stream;
|
| 215 |
+
bool is_small_pool;
|
| 216 |
+
size_t total_size;
|
| 217 |
+
};
|
| 218 |
+
|
| 219 |
+
class CUDAAllocator : public DeviceAllocator {
|
| 220 |
+
public:
|
| 221 |
+
virtual void* raw_alloc(size_t nbytes) = 0;
|
| 222 |
+
virtual void* raw_alloc_with_stream(size_t nbytes, cudaStream_t stream) = 0;
|
| 223 |
+
virtual void raw_delete(void* ptr) = 0;
|
| 224 |
+
virtual void init(int device_count) = 0;
|
| 225 |
+
virtual double getMemoryFraction(c10::DeviceIndex device) = 0;
|
| 226 |
+
virtual void setMemoryFraction(double fraction, c10::DeviceIndex device) = 0;
|
| 227 |
+
virtual std::vector<StreamSegmentSize> getExpandableSegmentSizes(
|
| 228 |
+
c10::DeviceIndex device) = 0;
|
| 229 |
+
virtual void enable(bool value) = 0;
|
| 230 |
+
virtual bool isEnabled() const = 0;
|
| 231 |
+
virtual void cacheInfo(c10::DeviceIndex device, size_t* largestBlock) = 0;
|
| 232 |
+
virtual void* getBaseAllocation(void* ptr, size_t* size) = 0;
|
| 233 |
+
// Keep for BC only
|
| 234 |
+
virtual void recordStream(const DataPtr& ptr, CUDAStream stream) = 0;
|
| 235 |
+
void recordStream(const DataPtr& ptr, c10::Stream stream) override {
|
| 236 |
+
CUDAStream cuda_stream = CUDAStream(stream);
|
| 237 |
+
recordStream(ptr, cuda_stream);
|
| 238 |
+
}
|
| 239 |
+
virtual SnapshotInfo snapshot(MempoolId_t mempool_id = {0, 0}) = 0;
|
| 240 |
+
virtual void beginAllocateToPool(
|
| 241 |
+
c10::DeviceIndex device,
|
| 242 |
+
MempoolId_t mempool_id,
|
| 243 |
+
std::function<bool(cudaStream_t)> filter) = 0;
|
| 244 |
+
virtual void endAllocateToPool(
|
| 245 |
+
c10::DeviceIndex device,
|
| 246 |
+
MempoolId_t mempool_id) = 0;
|
| 247 |
+
virtual void releasePool(c10::DeviceIndex device, MempoolId_t mempool_id) = 0;
|
| 248 |
+
virtual int getPoolUseCount(
|
| 249 |
+
c10::DeviceIndex /*device*/,
|
| 250 |
+
MempoolId_t /*mempool_id*/) {
|
| 251 |
+
TORCH_CHECK(
|
| 252 |
+
false,
|
| 253 |
+
name(),
|
| 254 |
+
" does not yet support getPoolUseCount. "
|
| 255 |
+
"If you need it, please file an issue describing your use case.");
|
| 256 |
+
}
|
| 257 |
+
virtual void createOrIncrefPool(
|
| 258 |
+
c10::DeviceIndex /*device*/,
|
| 259 |
+
MempoolId_t /*mempool_id*/,
|
| 260 |
+
CUDAAllocator* allocator = nullptr) {
|
| 261 |
+
TORCH_CHECK(
|
| 262 |
+
false,
|
| 263 |
+
name(),
|
| 264 |
+
" does not yet support createOrIncrefPool. "
|
| 265 |
+
"If you need it, please file an issue describing your use case.");
|
| 266 |
+
}
|
| 267 |
+
virtual void setUseOnOOM(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 268 |
+
TORCH_CHECK(
|
| 269 |
+
false,
|
| 270 |
+
name(),
|
| 271 |
+
" does not yet support setUseOnOOM. "
|
| 272 |
+
"If you need it, please file an issue describing your use case.");
|
| 273 |
+
}
|
| 274 |
+
virtual void setNoSplit(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 275 |
+
TORCH_CHECK(
|
| 276 |
+
false,
|
| 277 |
+
name(),
|
| 278 |
+
" does not yet support setNoSplit. "
|
| 279 |
+
"If you need it, please file an issue describing your use case.");
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// returns true if the allocated blocks are equal to expected live allocations
|
| 283 |
+
virtual bool checkPoolLiveAllocations(
|
| 284 |
+
c10::DeviceIndex /*device*/,
|
| 285 |
+
MempoolId_t /*mempool_id*/,
|
| 286 |
+
const std::unordered_set<void*>& /*expected_live_allocations*/) {
|
| 287 |
+
TORCH_CHECK(
|
| 288 |
+
false,
|
| 289 |
+
name(),
|
| 290 |
+
" does not yet support checkPoolLiveAllocations. "
|
| 291 |
+
"If you need it, please file an issue describing your use case.");
|
| 292 |
+
}
|
| 293 |
+
virtual ShareableHandle shareIpcHandle(void* ptr) = 0;
|
| 294 |
+
virtual std::shared_ptr<void> getIpcDevPtr(std::string handle) = 0;
|
| 295 |
+
virtual bool isHistoryEnabled() {
|
| 296 |
+
TORCH_CHECK(
|
| 297 |
+
false,
|
| 298 |
+
name(),
|
| 299 |
+
" does not yet support recordHistory. "
|
| 300 |
+
"If you need it, please file an issue describing your use case.");
|
| 301 |
+
}
|
| 302 |
+
virtual void recordHistory(
|
| 303 |
+
bool enabled,
|
| 304 |
+
CreateContextFn context_recorder,
|
| 305 |
+
size_t alloc_trace_max_entries,
|
| 306 |
+
RecordContext when,
|
| 307 |
+
bool clearHistory) = 0;
|
| 308 |
+
virtual void recordAnnotation(
|
| 309 |
+
const std::vector<std::pair<std::string, std::string>>& /*md*/) {}
|
| 310 |
+
virtual void pushCompileContext(std::string& md) {}
|
| 311 |
+
virtual void popCompileContext() {}
|
| 312 |
+
virtual void setUserMetadata(const std::string& metadata) {}
|
| 313 |
+
virtual std::string getUserMetadata() {
|
| 314 |
+
return "";
|
| 315 |
+
}
|
| 316 |
+
virtual void attachOutOfMemoryObserver(OutOfMemoryObserver observer) = 0;
|
| 317 |
+
|
| 318 |
+
// Attached AllocatorTraceTracker callbacks will be called while the
|
| 319 |
+
// per-device allocator lock is held. Any additional locks taken from within
|
| 320 |
+
// the callback must be proven to always have the lock order that never
|
| 321 |
+
// triggers a deadlock. In particular, Python's GIL may be held when
|
| 322 |
+
// calling the allocator so it is unsafe to try to acquire the GIL in this
|
| 323 |
+
// callback.
|
| 324 |
+
virtual void attachAllocatorTraceTracker(AllocatorTraceTracker tracker) = 0;
|
| 325 |
+
|
| 326 |
+
virtual void enablePeerAccess(
|
| 327 |
+
c10::DeviceIndex dev,
|
| 328 |
+
c10::DeviceIndex dev_to_access) = 0;
|
| 329 |
+
|
| 330 |
+
// memory not allocated from cudaMalloc cannot be copied
|
| 331 |
+
// across devices using cudaMemcpyAsync if peer to peer access is disabled.
|
| 332 |
+
// instead it requires cudaMemcpyAsyncPeer
|
| 333 |
+
// with P2P Enabled, all combinations work
|
| 334 |
+
// with P2P Disabled:
|
| 335 |
+
// cudaMalloc cudaMallocAsync/cuMemMap
|
| 336 |
+
// cudaMemcpyAsyncPeer works works
|
| 337 |
+
// cudaMemcpyAsync works error
|
| 338 |
+
|
| 339 |
+
// This function performs chooses to use the Peer version of
|
| 340 |
+
// memcpy if required based on where the allocated put dst/src.
|
| 341 |
+
virtual cudaError_t memcpyAsync(
|
| 342 |
+
void* dst,
|
| 343 |
+
int dstDevice,
|
| 344 |
+
const void* src,
|
| 345 |
+
int srcDevice,
|
| 346 |
+
size_t count,
|
| 347 |
+
cudaStream_t stream,
|
| 348 |
+
bool p2p_enabled) = 0;
|
| 349 |
+
virtual std::shared_ptr<AllocatorState> getCheckpointState(
|
| 350 |
+
c10::DeviceIndex device,
|
| 351 |
+
MempoolId_t id) = 0;
|
| 352 |
+
virtual CheckpointDelta setCheckpointPoolState(
|
| 353 |
+
c10::DeviceIndex device,
|
| 354 |
+
std::shared_ptr<AllocatorState> pps) = 0;
|
| 355 |
+
virtual std::string name() = 0;
|
| 356 |
+
std::pair<size_t, size_t> getMemoryInfo(c10::DeviceIndex device) override {
|
| 357 |
+
c10::DeviceGuard device_guard({at::kCUDA, device});
|
| 358 |
+
size_t free = 0;
|
| 359 |
+
size_t total = 0;
|
| 360 |
+
C10_CUDA_CHECK(cudaMemGetInfo(&free, &total));
|
| 361 |
+
return {free, total};
|
| 362 |
+
}
|
| 363 |
+
};
|
| 364 |
+
|
| 365 |
+
// Allocator object, statically initialized
|
| 366 |
+
// See BackendInitializer in CUDACachingAllocator.cpp.
|
| 367 |
+
// Atomic loads on x86 are just normal loads,
|
| 368 |
+
// (atomic stores are different), so reading this value
|
| 369 |
+
// is no different than loading a pointer.
|
| 370 |
+
C10_CUDA_API extern std::atomic<CUDAAllocator*> allocator;
|
| 371 |
+
|
| 372 |
+
inline CUDAAllocator* get() {
|
| 373 |
+
return allocator.load();
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
// Called directly by clients.
|
| 377 |
+
inline void* raw_alloc(size_t nbytes) {
|
| 378 |
+
return get()->raw_alloc(nbytes);
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
inline void* raw_alloc_with_stream(size_t nbytes, cudaStream_t stream) {
|
| 382 |
+
return get()->raw_alloc_with_stream(nbytes, stream);
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
inline void raw_delete(void* ptr) {
|
| 386 |
+
get()->raw_delete(ptr);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
inline void init(int device_count) {
|
| 390 |
+
get()->init(device_count);
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
inline double getMemoryFraction(c10::DeviceIndex device) {
|
| 394 |
+
return get()->getMemoryFraction(device);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
inline void setMemoryFraction(double fraction, c10::DeviceIndex device) {
|
| 398 |
+
get()->setMemoryFraction(fraction, device);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
inline std::vector<StreamSegmentSize> getExpandableSegmentSizes(
|
| 402 |
+
c10::DeviceIndex device) {
|
| 403 |
+
return get()->getExpandableSegmentSizes(device);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
inline void emptyCache(MempoolId_t mempool_id = {0, 0}) {
|
| 407 |
+
get()->emptyCache(mempool_id);
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
inline void enable(bool value) {
|
| 411 |
+
get()->enable(value);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
inline bool isEnabled() {
|
| 415 |
+
return get()->isEnabled();
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
inline void cacheInfo(c10::DeviceIndex device, size_t* largestBlock) {
|
| 419 |
+
get()->cacheInfo(device, largestBlock);
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
inline void* getBaseAllocation(void* ptr, size_t* size) {
|
| 423 |
+
return get()->getBaseAllocation(ptr, size);
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
inline void recordStream(const DataPtr& dataPtr, CUDAStream stream) {
|
| 427 |
+
get()->recordStream(dataPtr, stream);
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
inline c10::CachingDeviceAllocator::DeviceStats getDeviceStats(
|
| 431 |
+
c10::DeviceIndex device) {
|
| 432 |
+
return get()->getDeviceStats(device);
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
inline void resetAccumulatedStats(c10::DeviceIndex device) {
|
| 436 |
+
get()->resetAccumulatedStats(device);
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
inline void resetPeakStats(c10::DeviceIndex device) {
|
| 440 |
+
get()->resetPeakStats(device);
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
inline SnapshotInfo snapshot(MempoolId_t mempool_id = {0, 0}) {
|
| 444 |
+
return get()->snapshot(mempool_id);
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
inline std::shared_ptr<AllocatorState> getCheckpointState(
|
| 448 |
+
c10::DeviceIndex device,
|
| 449 |
+
MempoolId_t id) {
|
| 450 |
+
return get()->getCheckpointState(device, id);
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
inline CheckpointDelta setCheckpointPoolState(
|
| 454 |
+
c10::DeviceIndex device,
|
| 455 |
+
std::shared_ptr<AllocatorState> pps) {
|
| 456 |
+
return get()->setCheckpointPoolState(device, std::move(pps));
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
// CUDAGraph interactions
|
| 460 |
+
inline void beginAllocateToPool(
|
| 461 |
+
c10::DeviceIndex device,
|
| 462 |
+
MempoolId_t mempool_id,
|
| 463 |
+
std::function<bool(cudaStream_t)> filter) {
|
| 464 |
+
get()->beginAllocateToPool(device, mempool_id, std::move(filter));
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
inline void endAllocateToPool(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 468 |
+
get()->endAllocateToPool(device, mempool_id);
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
inline void recordHistory(
|
| 472 |
+
bool enabled,
|
| 473 |
+
CreateContextFn context_recorder,
|
| 474 |
+
size_t alloc_trace_max_entries,
|
| 475 |
+
RecordContext when,
|
| 476 |
+
bool clearHistory) {
|
| 477 |
+
get()->recordHistory(
|
| 478 |
+
enabled, context_recorder, alloc_trace_max_entries, when, clearHistory);
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
inline void recordAnnotation(
|
| 482 |
+
const std::vector<std::pair<std::string, std::string>>& md) {
|
| 483 |
+
get()->recordAnnotation(md);
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
inline void pushCompileContext(std::string& md) {
|
| 487 |
+
get()->pushCompileContext(md);
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
inline void popCompileContext() {
|
| 491 |
+
get()->popCompileContext();
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
inline bool isHistoryEnabled() {
|
| 495 |
+
return get()->isHistoryEnabled();
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
inline bool checkPoolLiveAllocations(
|
| 499 |
+
c10::DeviceIndex device,
|
| 500 |
+
MempoolId_t mempool_id,
|
| 501 |
+
const std::unordered_set<void*>& expected_live_allocations) {
|
| 502 |
+
return get()->checkPoolLiveAllocations(
|
| 503 |
+
device, mempool_id, expected_live_allocations);
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
inline void attachOutOfMemoryObserver(OutOfMemoryObserver observer) {
|
| 507 |
+
get()->attachOutOfMemoryObserver(std::move(observer));
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
inline void attachAllocatorTraceTracker(AllocatorTraceTracker tracker) {
|
| 511 |
+
get()->attachAllocatorTraceTracker(std::move(tracker));
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
inline void releasePool(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 515 |
+
get()->releasePool(device, mempool_id);
|
| 516 |
+
}
|
| 517 |
+
inline void createOrIncrefPool(
|
| 518 |
+
c10::DeviceIndex device,
|
| 519 |
+
MempoolId_t mempool_id,
|
| 520 |
+
CUDAAllocator* allocator_ptr = nullptr) {
|
| 521 |
+
get()->createOrIncrefPool(device, mempool_id, allocator_ptr);
|
| 522 |
+
}
|
| 523 |
+
inline void setUseOnOOM(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 524 |
+
get()->setUseOnOOM(device, mempool_id);
|
| 525 |
+
}
|
| 526 |
+
inline void setNoSplit(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 527 |
+
get()->setNoSplit(device, mempool_id);
|
| 528 |
+
}
|
| 529 |
+
inline int getPoolUseCount(c10::DeviceIndex device, MempoolId_t mempool_id) {
|
| 530 |
+
return get()->getPoolUseCount(device, mempool_id);
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
// Not part of CUDA_ALLOCATOR_BACKEND_INTERFACE
|
| 534 |
+
inline std::shared_ptr<void> getIpcDevPtr(std::string handle) {
|
| 535 |
+
return get()->getIpcDevPtr(std::move(handle));
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
inline ShareableHandle shareIpcHandle(void* ptr) {
|
| 539 |
+
return get()->shareIpcHandle(ptr);
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
inline std::string name() {
|
| 543 |
+
return get()->name();
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
inline cudaError_t memcpyAsync(
|
| 547 |
+
void* dst,
|
| 548 |
+
int dstDevice,
|
| 549 |
+
const void* src,
|
| 550 |
+
int srcDevice,
|
| 551 |
+
size_t count,
|
| 552 |
+
cudaStream_t stream,
|
| 553 |
+
bool p2p_enabled) {
|
| 554 |
+
return get()->memcpyAsync(
|
| 555 |
+
dst, dstDevice, src, srcDevice, count, stream, p2p_enabled);
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
+
inline void enablePeerAccess(
|
| 559 |
+
c10::DeviceIndex dev,
|
| 560 |
+
c10::DeviceIndex dev_to_access) {
|
| 561 |
+
get()->enablePeerAccess(dev, dev_to_access);
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
inline void setUserMetadata(const std::string& metadata) {
|
| 565 |
+
get()->setUserMetadata(metadata);
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
inline std::string getUserMetadata() {
|
| 569 |
+
return get()->getUserMetadata();
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
} // namespace c10::cuda::CUDACachingAllocator
|
| 573 |
+
|
| 574 |
+
namespace c10::cuda {
|
| 575 |
+
// Keep BC only
|
| 576 |
+
using c10::CaptureId_t;
|
| 577 |
+
using c10::MempoolId_t;
|
| 578 |
+
} // namespace c10::cuda
|
| 579 |
+
|
| 580 |
+
#else
|
| 581 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 582 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDADeviceAssertion.h
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDAException.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
|
| 7 |
+
namespace c10::cuda {
|
| 8 |
+
|
| 9 |
+
#ifdef TORCH_USE_CUDA_DSA
|
| 10 |
+
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wunused-function")
|
| 11 |
+
// Copy string from `src` to `dst`
|
| 12 |
+
static __device__ void dstrcpy(char* dst, const char* src) {
|
| 13 |
+
int i = 0;
|
| 14 |
+
// Copy string from source to destination, ensuring that it
|
| 15 |
+
// isn't longer than `C10_CUDA_DSA_MAX_STR_LEN-1`
|
| 16 |
+
while (*src != '\0' && i++ < C10_CUDA_DSA_MAX_STR_LEN - 1) {
|
| 17 |
+
*dst++ = *src++;
|
| 18 |
+
}
|
| 19 |
+
*dst = '\0';
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
static __device__ void dsa_add_new_assertion_failure(
|
| 23 |
+
DeviceAssertionsData* assertions_data,
|
| 24 |
+
const char* assertion_msg,
|
| 25 |
+
const char* filename,
|
| 26 |
+
const char* function_name,
|
| 27 |
+
const int line_number,
|
| 28 |
+
const uint32_t caller,
|
| 29 |
+
const dim3 block_id,
|
| 30 |
+
const dim3 thread_id) {
|
| 31 |
+
// `assertions_data` may be nullptr if device-side assertion checking
|
| 32 |
+
// is disabled at run-time. If it is disabled at compile time this
|
| 33 |
+
// function will never be called
|
| 34 |
+
if (!assertions_data) {
|
| 35 |
+
return;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Atomically increment so other threads can fail at the same time
|
| 39 |
+
// Note that incrementing this means that the CPU can observe that
|
| 40 |
+
// a failure has happened and can begin to respond before we've
|
| 41 |
+
// written information about that failure out to the buffer.
|
| 42 |
+
const auto nid = atomicAdd(&(assertions_data->assertion_count), 1);
|
| 43 |
+
|
| 44 |
+
if (nid >= C10_CUDA_DSA_ASSERTION_COUNT) {
|
| 45 |
+
// At this point we're ran out of assertion buffer space.
|
| 46 |
+
// We could print a message about this, but that'd get
|
| 47 |
+
// spammy if a lot of threads did it, so we just silently
|
| 48 |
+
// ignore any other assertion failures. In most cases the
|
| 49 |
+
// failures will all probably be analogous anyway.
|
| 50 |
+
return;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// Write information about the assertion failure to memory.
|
| 54 |
+
// Note that this occurs only after the `assertion_count`
|
| 55 |
+
// increment broadcasts that there's been a problem.
|
| 56 |
+
auto& self = assertions_data->assertions[nid];
|
| 57 |
+
dstrcpy(self.assertion_msg, assertion_msg);
|
| 58 |
+
dstrcpy(self.filename, filename);
|
| 59 |
+
dstrcpy(self.function_name, function_name);
|
| 60 |
+
self.line_number = line_number;
|
| 61 |
+
self.caller = caller;
|
| 62 |
+
self.block_id[0] = block_id.x;
|
| 63 |
+
self.block_id[1] = block_id.y;
|
| 64 |
+
self.block_id[2] = block_id.z;
|
| 65 |
+
self.thread_id[0] = thread_id.x;
|
| 66 |
+
self.thread_id[1] = thread_id.y;
|
| 67 |
+
self.thread_id[2] = thread_id.z;
|
| 68 |
+
}
|
| 69 |
+
C10_CLANG_DIAGNOSTIC_POP()
|
| 70 |
+
|
| 71 |
+
// Emulates a kernel assertion. The assertion won't stop the kernel's progress,
|
| 72 |
+
// so you should assume everything the kernel produces is garbage if there's an
|
| 73 |
+
// assertion failure.
|
| 74 |
+
// NOTE: This assumes that `assertions_data` and `assertion_caller_id` are
|
| 75 |
+
// arguments of the kernel and therefore accessible.
|
| 76 |
+
#define CUDA_KERNEL_ASSERT2(condition) \
|
| 77 |
+
do { \
|
| 78 |
+
if (C10_UNLIKELY(!(condition))) { \
|
| 79 |
+
/* Has an atomic element so threads can fail at the same time */ \
|
| 80 |
+
c10::cuda::dsa_add_new_assertion_failure( \
|
| 81 |
+
assertions_data, \
|
| 82 |
+
C10_STRINGIZE(condition), \
|
| 83 |
+
__FILE__, \
|
| 84 |
+
__FUNCTION__, \
|
| 85 |
+
__LINE__, \
|
| 86 |
+
assertion_caller_id, \
|
| 87 |
+
blockIdx, \
|
| 88 |
+
threadIdx); \
|
| 89 |
+
/* Now that the kernel has failed we early exit the kernel, but */ \
|
| 90 |
+
/* otherwise keep going and rely on the host to check UVM and */ \
|
| 91 |
+
/* determine we've had a problem */ \
|
| 92 |
+
return; \
|
| 93 |
+
} \
|
| 94 |
+
} while (false)
|
| 95 |
+
#else
|
| 96 |
+
#define CUDA_KERNEL_ASSERT2(condition) assert(condition)
|
| 97 |
+
#endif
|
| 98 |
+
|
| 99 |
+
} // namespace c10::cuda
|
| 100 |
+
|
| 101 |
+
#else
|
| 102 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 103 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDADeviceAssertionHost.h
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 5 |
+
|
| 6 |
+
#include <cstdint>
|
| 7 |
+
#include <memory>
|
| 8 |
+
#include <mutex>
|
| 9 |
+
#include <string>
|
| 10 |
+
#include <utility>
|
| 11 |
+
#include <vector>
|
| 12 |
+
|
| 13 |
+
#ifdef USE_CUDA
|
| 14 |
+
#define TORCH_USE_CUDA_DSA
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
/// Number of assertion failure messages we can store. If this is too small
|
| 18 |
+
/// threads will fail silently.
|
| 19 |
+
constexpr int C10_CUDA_DSA_ASSERTION_COUNT = 10;
|
| 20 |
+
constexpr int C10_CUDA_DSA_MAX_STR_LEN = 512;
|
| 21 |
+
|
| 22 |
+
namespace c10::cuda {
|
| 23 |
+
|
| 24 |
+
/// Holds information about any device-side assertions that fail.
|
| 25 |
+
/// Held in managed memory and access by both the CPU and the GPU.
|
| 26 |
+
struct DeviceAssertionData {
|
| 27 |
+
/// Stringification of the assertion
|
| 28 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 29 |
+
char assertion_msg[C10_CUDA_DSA_MAX_STR_LEN]{};
|
| 30 |
+
/// File the assertion was in
|
| 31 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 32 |
+
char filename[C10_CUDA_DSA_MAX_STR_LEN]{};
|
| 33 |
+
/// Name of the function the assertion was in
|
| 34 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 35 |
+
char function_name[C10_CUDA_DSA_MAX_STR_LEN]{};
|
| 36 |
+
/// Line number the assertion was at
|
| 37 |
+
int line_number{};
|
| 38 |
+
/// Number uniquely identifying the kernel launch that triggered the assertion
|
| 39 |
+
uint32_t caller{};
|
| 40 |
+
/// block_id of the thread that failed the assertion
|
| 41 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 42 |
+
int32_t block_id[3]{};
|
| 43 |
+
/// third_id of the thread that failed the assertion
|
| 44 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 45 |
+
int32_t thread_id[3]{};
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
/// Used to hold assertions generated by the device
|
| 49 |
+
/// Held in managed memory and access by both the CPU and the GPU.
|
| 50 |
+
struct DeviceAssertionsData {
|
| 51 |
+
/// Total number of assertions found; a subset of these will be recorded
|
| 52 |
+
/// in `assertions`
|
| 53 |
+
int32_t assertion_count{};
|
| 54 |
+
/// An array of assertions that will be written to in a race-free manner
|
| 55 |
+
// NOLINTNEXTLINE(*-c-arrays)
|
| 56 |
+
DeviceAssertionData assertions[C10_CUDA_DSA_ASSERTION_COUNT]{};
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
/// Use to hold info about kernel launches so that we can run kernels
|
| 60 |
+
/// asynchronously and still associate launches with device-side
|
| 61 |
+
/// assertion failures
|
| 62 |
+
struct CUDAKernelLaunchInfo {
|
| 63 |
+
/// Filename of the code where the kernel was launched from
|
| 64 |
+
const char* launch_filename;
|
| 65 |
+
/// Function from which the kernel was launched
|
| 66 |
+
const char* launch_function;
|
| 67 |
+
/// Line number of where the code was launched from
|
| 68 |
+
uint32_t launch_linenum;
|
| 69 |
+
/// Backtrace of where the kernel was launched from, only populated if
|
| 70 |
+
/// CUDAKernelLaunchRegistry::gather_launch_stacktrace is True
|
| 71 |
+
std::string launch_stacktrace;
|
| 72 |
+
/// Kernel that was launched
|
| 73 |
+
const char* kernel_name;
|
| 74 |
+
/// Device the kernel was launched on
|
| 75 |
+
int device;
|
| 76 |
+
/// Stream the kernel was launched on
|
| 77 |
+
int32_t stream;
|
| 78 |
+
/// A number that uniquely identifies the kernel launch
|
| 79 |
+
uint64_t generation_number;
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
/// Circular buffer used to hold information about kernel launches
|
| 83 |
+
/// this is later used to reconstruct how a device-side kernel assertion failure
|
| 84 |
+
/// occurred CUDAKernelLaunchRegistry is used as a singleton
|
| 85 |
+
class C10_CUDA_API CUDAKernelLaunchRegistry {
|
| 86 |
+
private:
|
| 87 |
+
/// Assume that this is the max number of kernel launches that might ever be
|
| 88 |
+
/// enqueued across all streams on a single device
|
| 89 |
+
static constexpr int max_kernel_launches = 1024;
|
| 90 |
+
/// How many kernel launch infos we've inserted. Used to ensure that circular
|
| 91 |
+
/// queue doesn't provide false information by always increasing, but also to
|
| 92 |
+
/// mark where we are inserting into the queue
|
| 93 |
+
#ifdef TORCH_USE_CUDA_DSA
|
| 94 |
+
uint64_t generation_number = 0;
|
| 95 |
+
#endif
|
| 96 |
+
/// Shared mutex between writer and accessor to ensure multi-threaded safety.
|
| 97 |
+
mutable std::mutex read_write_mutex;
|
| 98 |
+
/// Used to ensure prevent race conditions in GPU memory allocation
|
| 99 |
+
mutable std::mutex gpu_alloc_mutex;
|
| 100 |
+
/// Pointer to managed memory keeping track of device-side assertions. There
|
| 101 |
+
/// is one entry for each possible device the process might work with. Unused
|
| 102 |
+
/// entries are nullptrs. We could also use an unordered_set here, but this
|
| 103 |
+
/// vector design will be faster and the wasted memory is small since we
|
| 104 |
+
/// expect the number of GPUs per node will always be small
|
| 105 |
+
std::vector<
|
| 106 |
+
std::unique_ptr<DeviceAssertionsData, void (*)(DeviceAssertionsData*)>>
|
| 107 |
+
uvm_assertions;
|
| 108 |
+
/// A single circular buffer holds information about every kernel launch the
|
| 109 |
+
/// process makes across all devices.
|
| 110 |
+
std::vector<CUDAKernelLaunchInfo> kernel_launches;
|
| 111 |
+
bool check_env_for_enable_launch_stacktracing() const;
|
| 112 |
+
bool check_env_for_dsa_enabled() const;
|
| 113 |
+
|
| 114 |
+
public:
|
| 115 |
+
CUDAKernelLaunchRegistry();
|
| 116 |
+
/// Register a new kernel launch and obtain a generation number back to be
|
| 117 |
+
/// passed to the kernel
|
| 118 |
+
uint32_t insert(
|
| 119 |
+
const char* launch_filename,
|
| 120 |
+
const char* launch_function,
|
| 121 |
+
const uint32_t launch_linenum,
|
| 122 |
+
const char* kernel_name,
|
| 123 |
+
const int32_t stream_id);
|
| 124 |
+
/// Get copies of the kernel launch registry and each device's assertion
|
| 125 |
+
/// failure buffer so they can be inspected without raising race conditions
|
| 126 |
+
std::
|
| 127 |
+
pair<std::vector<DeviceAssertionsData>, std::vector<CUDAKernelLaunchInfo>>
|
| 128 |
+
snapshot() const;
|
| 129 |
+
/// Get a pointer to the current device's assertion failure buffer. If no such
|
| 130 |
+
/// buffer exists then one is created. This means that the first kernel launch
|
| 131 |
+
/// made on each device will be slightly slower because memory allocations are
|
| 132 |
+
/// required
|
| 133 |
+
DeviceAssertionsData* get_uvm_assertions_ptr_for_current_device();
|
| 134 |
+
/// Gets the global singleton of the registry
|
| 135 |
+
static CUDAKernelLaunchRegistry& get_singleton_ref();
|
| 136 |
+
/// If not all devices support DSA, we disable it
|
| 137 |
+
const bool do_all_devices_support_managed_memory = false;
|
| 138 |
+
/// Whether or not to gather stack traces when launching kernels
|
| 139 |
+
bool gather_launch_stacktrace = false;
|
| 140 |
+
/// Whether or not host-side DSA is enabled or disabled at run-time
|
| 141 |
+
/// Note: Device-side code cannot be enabled/disabled at run-time
|
| 142 |
+
bool enabled_at_runtime = false;
|
| 143 |
+
/// Whether or not a device has indicated a failure
|
| 144 |
+
bool has_failed() const;
|
| 145 |
+
#ifdef TORCH_USE_CUDA_DSA
|
| 146 |
+
const bool enabled_at_compile_time = true;
|
| 147 |
+
#else
|
| 148 |
+
const bool enabled_at_compile_time = false;
|
| 149 |
+
#endif
|
| 150 |
+
};
|
| 151 |
+
|
| 152 |
+
C10_CUDA_API std::string c10_retrieve_device_side_assertion_info();
|
| 153 |
+
|
| 154 |
+
} // namespace c10::cuda
|
| 155 |
+
|
| 156 |
+
// Each kernel launched with TORCH_DSA_KERNEL_LAUNCH
|
| 157 |
+
// requires the same input arguments. We introduce the following macro to
|
| 158 |
+
// standardize these.
|
| 159 |
+
#define TORCH_DSA_KERNEL_ARGS \
|
| 160 |
+
[[maybe_unused]] c10::cuda::DeviceAssertionsData *const assertions_data, \
|
| 161 |
+
[[maybe_unused]] uint32_t assertion_caller_id
|
| 162 |
+
|
| 163 |
+
// This macro can be used to pass the DSA arguments onward to another
|
| 164 |
+
// function
|
| 165 |
+
#define TORCH_DSA_KERNEL_ARGS_PASS assertions_data, assertion_caller_id
|
| 166 |
+
|
| 167 |
+
#else
|
| 168 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 169 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAException.h
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDADeviceAssertionHost.h>
|
| 5 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 6 |
+
#include <c10/cuda/CUDAMiscFunctions.h>
|
| 7 |
+
#include <c10/macros/Macros.h>
|
| 8 |
+
#include <c10/util/Exception.h>
|
| 9 |
+
#include <c10/util/irange.h>
|
| 10 |
+
#include <cuda.h>
|
| 11 |
+
|
| 12 |
+
// Note [CHECK macro]
|
| 13 |
+
// ~~~~~~~~~~~~~~~~~~
|
| 14 |
+
// This is a macro so that AT_ERROR can get accurate __LINE__
|
| 15 |
+
// and __FILE__ information. We could split this into a short
|
| 16 |
+
// macro and a function implementation if we pass along __LINE__
|
| 17 |
+
// and __FILE__, but no one has found this worth doing.
|
| 18 |
+
|
| 19 |
+
// Used to denote errors from CUDA framework.
|
| 20 |
+
// This needs to be declared here instead util/Exception.h for proper conversion
|
| 21 |
+
// during hipify.
|
| 22 |
+
namespace c10 {
|
| 23 |
+
class C10_CUDA_API CUDAError : public c10::Error {
|
| 24 |
+
using Error::Error;
|
| 25 |
+
};
|
| 26 |
+
} // namespace c10
|
| 27 |
+
|
| 28 |
+
#define C10_CUDA_CHECK(EXPR) \
|
| 29 |
+
do { \
|
| 30 |
+
const cudaError_t __err = EXPR; \
|
| 31 |
+
c10::cuda::c10_cuda_check_implementation( \
|
| 32 |
+
static_cast<int32_t>(__err), \
|
| 33 |
+
__FILE__, \
|
| 34 |
+
__func__, /* Line number data type not well-defined between \
|
| 35 |
+
compilers, so we perform an explicit cast */ \
|
| 36 |
+
static_cast<uint32_t>(__LINE__), \
|
| 37 |
+
true); \
|
| 38 |
+
} while (0)
|
| 39 |
+
|
| 40 |
+
#define C10_CUDA_CHECK_WARN(EXPR) \
|
| 41 |
+
do { \
|
| 42 |
+
const cudaError_t __err = EXPR; \
|
| 43 |
+
if (C10_UNLIKELY(__err != cudaSuccess)) { \
|
| 44 |
+
[[maybe_unused]] auto error_unused = cudaGetLastError(); \
|
| 45 |
+
TORCH_WARN("CUDA warning: ", cudaGetErrorString(__err)); \
|
| 46 |
+
} \
|
| 47 |
+
} while (0)
|
| 48 |
+
|
| 49 |
+
// Indicates that a CUDA error is handled in a non-standard way
|
| 50 |
+
#define C10_CUDA_ERROR_HANDLED(EXPR) EXPR
|
| 51 |
+
|
| 52 |
+
// Intentionally ignore a CUDA error
|
| 53 |
+
#define C10_CUDA_IGNORE_ERROR(EXPR) \
|
| 54 |
+
do { \
|
| 55 |
+
const cudaError_t __err = EXPR; \
|
| 56 |
+
if (C10_UNLIKELY(__err != cudaSuccess)) { \
|
| 57 |
+
[[maybe_unused]] cudaError_t error_unused = cudaGetLastError(); \
|
| 58 |
+
} \
|
| 59 |
+
} while (0)
|
| 60 |
+
|
| 61 |
+
// Clear the last CUDA error
|
| 62 |
+
#define C10_CUDA_CLEAR_ERROR() \
|
| 63 |
+
do { \
|
| 64 |
+
[[maybe_unused]] cudaError_t error_unused = cudaGetLastError(); \
|
| 65 |
+
} while (0)
|
| 66 |
+
|
| 67 |
+
// This should be used directly after every kernel launch to ensure
|
| 68 |
+
// the launch happened correctly and provide an early, close-to-source
|
| 69 |
+
// diagnostic if it didn't.
|
| 70 |
+
#define C10_CUDA_KERNEL_LAUNCH_CHECK() C10_CUDA_CHECK(cudaGetLastError())
|
| 71 |
+
|
| 72 |
+
/// Launches a CUDA kernel appending to it all the information need to handle
|
| 73 |
+
/// device-side assertion failures. Checks that the launch was successful.
|
| 74 |
+
#define TORCH_DSA_KERNEL_LAUNCH( \
|
| 75 |
+
kernel, blocks, threads, shared_mem, stream, ...) \
|
| 76 |
+
do { \
|
| 77 |
+
auto& launch_registry = \
|
| 78 |
+
c10::cuda::CUDAKernelLaunchRegistry::get_singleton_ref(); \
|
| 79 |
+
kernel<<<blocks, threads, shared_mem, stream>>>( \
|
| 80 |
+
__VA_ARGS__, \
|
| 81 |
+
launch_registry.get_uvm_assertions_ptr_for_current_device(), \
|
| 82 |
+
launch_registry.insert( \
|
| 83 |
+
__FILE__, __FUNCTION__, __LINE__, #kernel, stream.id())); \
|
| 84 |
+
C10_CUDA_KERNEL_LAUNCH_CHECK(); \
|
| 85 |
+
} while (0)
|
| 86 |
+
|
| 87 |
+
namespace c10::cuda {
|
| 88 |
+
|
| 89 |
+
/// In the event of a CUDA failure, formats a nice error message about that
|
| 90 |
+
/// failure and also checks for device-side assertion failures
|
| 91 |
+
C10_CUDA_API void c10_cuda_check_implementation(
|
| 92 |
+
const int32_t err,
|
| 93 |
+
const char* filename,
|
| 94 |
+
const char* function_name,
|
| 95 |
+
const uint32_t line_number,
|
| 96 |
+
const bool include_device_assertions);
|
| 97 |
+
|
| 98 |
+
} // namespace c10::cuda
|
| 99 |
+
|
| 100 |
+
#else
|
| 101 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 102 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAFunctions.h
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
// This header provides C++ wrappers around commonly used CUDA API functions.
|
| 5 |
+
// The benefit of using C++ here is that we can raise an exception in the
|
| 6 |
+
// event of an error, rather than explicitly pass around error codes. This
|
| 7 |
+
// leads to more natural APIs.
|
| 8 |
+
//
|
| 9 |
+
// The naming convention used here matches the naming convention of torch.cuda
|
| 10 |
+
|
| 11 |
+
#include <c10/core/Device.h>
|
| 12 |
+
#include <c10/core/impl/GPUTrace.h>
|
| 13 |
+
#include <c10/cuda/CUDAException.h>
|
| 14 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 15 |
+
#include <cuda_runtime_api.h>
|
| 16 |
+
namespace c10::cuda {
|
| 17 |
+
|
| 18 |
+
// NB: In the past, we were inconsistent about whether or not this reported
|
| 19 |
+
// an error if there were driver problems are not. Based on experience
|
| 20 |
+
// interacting with users, it seems that people basically ~never want this
|
| 21 |
+
// function to fail; it should just return zero if things are not working.
|
| 22 |
+
// Oblige them.
|
| 23 |
+
// It still might log a warning for user first time it's invoked
|
| 24 |
+
C10_CUDA_API DeviceIndex device_count() noexcept;
|
| 25 |
+
|
| 26 |
+
// Version of device_count that throws is no devices are detected
|
| 27 |
+
C10_CUDA_API DeviceIndex device_count_ensure_non_zero();
|
| 28 |
+
|
| 29 |
+
C10_CUDA_API DeviceIndex current_device();
|
| 30 |
+
|
| 31 |
+
C10_CUDA_API void set_device(DeviceIndex device, const bool force = false);
|
| 32 |
+
|
| 33 |
+
C10_CUDA_API void device_synchronize();
|
| 34 |
+
|
| 35 |
+
C10_CUDA_API void warn_or_error_on_sync();
|
| 36 |
+
|
| 37 |
+
// Raw CUDA device management functions
|
| 38 |
+
C10_CUDA_API cudaError_t GetDeviceCount(int* dev_count);
|
| 39 |
+
|
| 40 |
+
C10_CUDA_API cudaError_t GetDevice(DeviceIndex* device);
|
| 41 |
+
|
| 42 |
+
C10_CUDA_API cudaError_t
|
| 43 |
+
SetDevice(DeviceIndex device, const bool force = false);
|
| 44 |
+
|
| 45 |
+
C10_CUDA_API cudaError_t MaybeSetDevice(DeviceIndex device);
|
| 46 |
+
|
| 47 |
+
C10_CUDA_API DeviceIndex ExchangeDevice(DeviceIndex device);
|
| 48 |
+
|
| 49 |
+
C10_CUDA_API DeviceIndex MaybeExchangeDevice(DeviceIndex device);
|
| 50 |
+
|
| 51 |
+
C10_CUDA_API void SetTargetDevice();
|
| 52 |
+
|
| 53 |
+
enum class SyncDebugMode { L_DISABLED = 0, L_WARN, L_ERROR };
|
| 54 |
+
|
| 55 |
+
// this is a holder for c10 global state (similar to at GlobalContext)
|
| 56 |
+
// currently it's used to store cuda synchronization warning state,
|
| 57 |
+
// but can be expanded to hold other related global state, e.g. to
|
| 58 |
+
// record stream usage
|
| 59 |
+
class WarningState {
|
| 60 |
+
public:
|
| 61 |
+
void set_sync_debug_mode(SyncDebugMode l) {
|
| 62 |
+
sync_debug_mode = l;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
SyncDebugMode get_sync_debug_mode() {
|
| 66 |
+
return sync_debug_mode;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
private:
|
| 70 |
+
SyncDebugMode sync_debug_mode = SyncDebugMode::L_DISABLED;
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
C10_CUDA_API __inline__ WarningState& warning_state() {
|
| 74 |
+
static WarningState warning_state_;
|
| 75 |
+
return warning_state_;
|
| 76 |
+
}
|
| 77 |
+
// the subsequent functions are defined in the header because for performance
|
| 78 |
+
// reasons we want them to be inline
|
| 79 |
+
C10_CUDA_API void __inline__ memcpy_and_sync(
|
| 80 |
+
void* dst,
|
| 81 |
+
const void* src,
|
| 82 |
+
int64_t nbytes,
|
| 83 |
+
cudaMemcpyKind kind,
|
| 84 |
+
cudaStream_t stream) {
|
| 85 |
+
if (C10_UNLIKELY(
|
| 86 |
+
warning_state().get_sync_debug_mode() != SyncDebugMode::L_DISABLED)) {
|
| 87 |
+
warn_or_error_on_sync();
|
| 88 |
+
}
|
| 89 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 90 |
+
if (C10_UNLIKELY(interp)) {
|
| 91 |
+
(*interp)->trace_gpu_stream_synchronization(
|
| 92 |
+
c10::kCUDA, reinterpret_cast<uintptr_t>(stream));
|
| 93 |
+
}
|
| 94 |
+
#if defined(USE_ROCM) && USE_ROCM
|
| 95 |
+
// As of ROCm 6.4.1, HIP runtime does not raise an error during capture of
|
| 96 |
+
// hipMemcpyWithStream which is a synchronous call. Thus, we add a check
|
| 97 |
+
// here explicitly.
|
| 98 |
+
hipStreamCaptureStatus captureStatus;
|
| 99 |
+
C10_CUDA_CHECK(hipStreamGetCaptureInfo(stream, &captureStatus, nullptr));
|
| 100 |
+
if (C10_LIKELY(captureStatus == hipStreamCaptureStatusNone)) {
|
| 101 |
+
C10_CUDA_CHECK(hipMemcpyWithStream(dst, src, nbytes, kind, stream));
|
| 102 |
+
} else {
|
| 103 |
+
C10_CUDA_CHECK(hipErrorStreamCaptureUnsupported);
|
| 104 |
+
}
|
| 105 |
+
#else
|
| 106 |
+
C10_CUDA_CHECK(cudaMemcpyAsync(dst, src, nbytes, kind, stream));
|
| 107 |
+
C10_CUDA_CHECK(cudaStreamSynchronize(stream));
|
| 108 |
+
#endif
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
C10_CUDA_API void __inline__ stream_synchronize(cudaStream_t stream) {
|
| 112 |
+
if (C10_UNLIKELY(
|
| 113 |
+
warning_state().get_sync_debug_mode() != SyncDebugMode::L_DISABLED)) {
|
| 114 |
+
warn_or_error_on_sync();
|
| 115 |
+
}
|
| 116 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 117 |
+
if (C10_UNLIKELY(interp)) {
|
| 118 |
+
(*interp)->trace_gpu_stream_synchronization(
|
| 119 |
+
c10::kCUDA, reinterpret_cast<uintptr_t>(stream));
|
| 120 |
+
}
|
| 121 |
+
C10_CUDA_CHECK(cudaStreamSynchronize(stream));
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
C10_CUDA_API bool hasPrimaryContext(DeviceIndex device_index);
|
| 125 |
+
C10_CUDA_API std::optional<DeviceIndex> getDeviceIndexWithPrimaryContext();
|
| 126 |
+
|
| 127 |
+
} // namespace c10::cuda
|
| 128 |
+
|
| 129 |
+
#else
|
| 130 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 131 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAGraphsC10Utils.h
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDAStream.h>
|
| 5 |
+
#include <iostream>
|
| 6 |
+
#include <utility>
|
| 7 |
+
|
| 8 |
+
// CUDA Graphs utils used by c10 and aten.
|
| 9 |
+
// aten/cuda/CUDAGraphsUtils.cuh adds utils used by aten only.
|
| 10 |
+
|
| 11 |
+
namespace c10::cuda {
|
| 12 |
+
|
| 13 |
+
// RAII guard for "cudaStreamCaptureMode", a thread-local value
|
| 14 |
+
// that controls the error-checking strictness of a capture.
|
| 15 |
+
struct C10_CUDA_API CUDAStreamCaptureModeGuard {
|
| 16 |
+
CUDAStreamCaptureModeGuard(cudaStreamCaptureMode desired)
|
| 17 |
+
: strictness_(desired) {
|
| 18 |
+
C10_CUDA_CHECK(cudaThreadExchangeStreamCaptureMode(&strictness_));
|
| 19 |
+
}
|
| 20 |
+
CUDAStreamCaptureModeGuard(const CUDAStreamCaptureModeGuard&) = delete;
|
| 21 |
+
CUDAStreamCaptureModeGuard(CUDAStreamCaptureModeGuard&&) = delete;
|
| 22 |
+
CUDAStreamCaptureModeGuard& operator=(const CUDAStreamCaptureModeGuard&) =
|
| 23 |
+
delete;
|
| 24 |
+
CUDAStreamCaptureModeGuard& operator=(CUDAStreamCaptureModeGuard&&) = delete;
|
| 25 |
+
~CUDAStreamCaptureModeGuard() {
|
| 26 |
+
C10_CUDA_CHECK_WARN(cudaThreadExchangeStreamCaptureMode(&strictness_));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
private:
|
| 30 |
+
cudaStreamCaptureMode strictness_;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
// Protects against enum cudaStreamCaptureStatus implementation changes.
|
| 34 |
+
// Some compilers seem not to like static_assert without the messages.
|
| 35 |
+
static_assert(
|
| 36 |
+
int(cudaStreamCaptureStatus::cudaStreamCaptureStatusNone) == 0,
|
| 37 |
+
"unexpected int(cudaStreamCaptureStatusNone) value");
|
| 38 |
+
static_assert(
|
| 39 |
+
int(cudaStreamCaptureStatus::cudaStreamCaptureStatusActive) == 1,
|
| 40 |
+
"unexpected int(cudaStreamCaptureStatusActive) value");
|
| 41 |
+
static_assert(
|
| 42 |
+
int(cudaStreamCaptureStatus::cudaStreamCaptureStatusInvalidated) == 2,
|
| 43 |
+
"unexpected int(cudaStreamCaptureStatusInvalidated) value");
|
| 44 |
+
|
| 45 |
+
enum class CaptureStatus : int {
|
| 46 |
+
None = int(cudaStreamCaptureStatus::cudaStreamCaptureStatusNone),
|
| 47 |
+
Active = int(cudaStreamCaptureStatus::cudaStreamCaptureStatusActive),
|
| 48 |
+
Invalidated = int(cudaStreamCaptureStatus::cudaStreamCaptureStatusInvalidated)
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
inline std::ostream& operator<<(std::ostream& os, CaptureStatus status) {
|
| 52 |
+
switch (status) {
|
| 53 |
+
case CaptureStatus::None:
|
| 54 |
+
os << "cudaStreamCaptureStatusNone";
|
| 55 |
+
break;
|
| 56 |
+
case CaptureStatus::Active:
|
| 57 |
+
os << "cudaStreamCaptureStatusActive";
|
| 58 |
+
break;
|
| 59 |
+
case CaptureStatus::Invalidated:
|
| 60 |
+
os << "cudaStreamCaptureStatusInvalidated";
|
| 61 |
+
break;
|
| 62 |
+
default:
|
| 63 |
+
TORCH_INTERNAL_ASSERT(
|
| 64 |
+
false, "Unknown CUDA graph CaptureStatus", int(status));
|
| 65 |
+
}
|
| 66 |
+
return os;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// Use this version where you're sure a CUDA context exists already.
|
| 70 |
+
inline CaptureStatus currentStreamCaptureStatusMayInitCtx() {
|
| 71 |
+
cudaStreamCaptureStatus is_capturing{cudaStreamCaptureStatusNone};
|
| 72 |
+
C10_CUDA_CHECK(
|
| 73 |
+
cudaStreamIsCapturing(c10::cuda::getCurrentCUDAStream(), &is_capturing));
|
| 74 |
+
return CaptureStatus(is_capturing);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
} // namespace c10::cuda
|
| 78 |
+
|
| 79 |
+
#else
|
| 80 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 81 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAGuard.h
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/DeviceType.h>
|
| 5 |
+
#include <c10/core/impl/InlineDeviceGuard.h>
|
| 6 |
+
#include <c10/core/impl/InlineStreamGuard.h>
|
| 7 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 8 |
+
#include <c10/cuda/impl/CUDAGuardImpl.h>
|
| 9 |
+
|
| 10 |
+
namespace c10::cuda {
|
| 11 |
+
|
| 12 |
+
// This code is kind of boilerplatey. See Note [Whither the DeviceGuard
|
| 13 |
+
// boilerplate]
|
| 14 |
+
|
| 15 |
+
/// A variant of DeviceGuard that is specialized for CUDA. It accepts
|
| 16 |
+
/// integer indices (interpreting them as CUDA devices) and is a little
|
| 17 |
+
/// more efficient than DeviceGuard (it compiles to straight line
|
| 18 |
+
/// cudaSetDevice/cudaGetDevice calls); however, it can only be used
|
| 19 |
+
/// from code that links against CUDA directly.
|
| 20 |
+
struct CUDAGuard {
|
| 21 |
+
/// No default constructor; see Note [Omitted default constructor from RAII]
|
| 22 |
+
explicit CUDAGuard() = delete;
|
| 23 |
+
|
| 24 |
+
/// Set the current CUDA device to the passed device index.
|
| 25 |
+
explicit CUDAGuard(DeviceIndex device_index) : guard_(device_index) {}
|
| 26 |
+
|
| 27 |
+
/// Sets the current CUDA device to the passed device. Errors if the passed
|
| 28 |
+
/// device is not a CUDA device.
|
| 29 |
+
explicit CUDAGuard(Device device) : guard_(device) {}
|
| 30 |
+
|
| 31 |
+
// Copy is not allowed
|
| 32 |
+
CUDAGuard(const CUDAGuard&) = delete;
|
| 33 |
+
CUDAGuard& operator=(const CUDAGuard&) = delete;
|
| 34 |
+
|
| 35 |
+
// Move is not allowed (there is no uninitialized state)
|
| 36 |
+
CUDAGuard(CUDAGuard&& other) = delete;
|
| 37 |
+
CUDAGuard& operator=(CUDAGuard&& other) = delete;
|
| 38 |
+
~CUDAGuard() = default;
|
| 39 |
+
|
| 40 |
+
/// Sets the CUDA device to the given device. Errors if the given device
|
| 41 |
+
/// is not a CUDA device.
|
| 42 |
+
void set_device(Device device) {
|
| 43 |
+
guard_.set_device(device);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/// Sets the CUDA device to the given device. Errors if the given device
|
| 47 |
+
/// is not a CUDA device. (This method is provided for uniformity with
|
| 48 |
+
/// DeviceGuard).
|
| 49 |
+
void reset_device(Device device) {
|
| 50 |
+
guard_.reset_device(device);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/// Sets the CUDA device to the given device index.
|
| 54 |
+
void set_index(DeviceIndex device_index) {
|
| 55 |
+
guard_.set_index(device_index);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
/// Returns the device that was set upon construction of the guard
|
| 59 |
+
Device original_device() const {
|
| 60 |
+
return guard_.original_device();
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/// Returns the last device that was set via `set_device`, if any, otherwise
|
| 64 |
+
/// the device passed during construction.
|
| 65 |
+
Device current_device() const {
|
| 66 |
+
return guard_.current_device();
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
private:
|
| 70 |
+
/// The guard for the current device.
|
| 71 |
+
c10::impl::InlineDeviceGuard<impl::CUDAGuardImpl> guard_;
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
/// A variant of OptionalDeviceGuard that is specialized for CUDA. See
|
| 75 |
+
/// CUDAGuard for when you can use this.
|
| 76 |
+
struct OptionalCUDAGuard {
|
| 77 |
+
/// Create an uninitialized OptionalCUDAGuard.
|
| 78 |
+
explicit OptionalCUDAGuard() = default;
|
| 79 |
+
|
| 80 |
+
/// Set the current CUDA device to the passed Device, if it is not nullopt.
|
| 81 |
+
explicit OptionalCUDAGuard(std::optional<Device> device_opt)
|
| 82 |
+
: guard_(device_opt) {}
|
| 83 |
+
|
| 84 |
+
/// Set the current CUDA device to the passed device index, if it is not
|
| 85 |
+
/// nullopt
|
| 86 |
+
explicit OptionalCUDAGuard(std::optional<DeviceIndex> device_index_opt)
|
| 87 |
+
: guard_(device_index_opt) {}
|
| 88 |
+
|
| 89 |
+
// Copy is not allowed
|
| 90 |
+
OptionalCUDAGuard(const OptionalCUDAGuard&) = delete;
|
| 91 |
+
OptionalCUDAGuard& operator=(const OptionalCUDAGuard&) = delete;
|
| 92 |
+
|
| 93 |
+
// See Note [Move construction for RAII guards is tricky]
|
| 94 |
+
OptionalCUDAGuard(OptionalCUDAGuard&& other) = delete;
|
| 95 |
+
|
| 96 |
+
// See Note [Move assignment for RAII guards is tricky]
|
| 97 |
+
OptionalCUDAGuard& operator=(OptionalCUDAGuard&& other) = delete;
|
| 98 |
+
~OptionalCUDAGuard() = default;
|
| 99 |
+
|
| 100 |
+
/// Sets the CUDA device to the given device, initializing the guard if it
|
| 101 |
+
/// is not already initialized. Errors if the given device is not a CUDA
|
| 102 |
+
/// device.
|
| 103 |
+
void set_device(Device device) {
|
| 104 |
+
guard_.set_device(device);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/// Sets the CUDA device to the given device, initializing the guard if it is
|
| 108 |
+
/// not already initialized. Errors if the given device is not a CUDA device.
|
| 109 |
+
/// (This method is provided for uniformity with OptionalDeviceGuard).
|
| 110 |
+
void reset_device(Device device) {
|
| 111 |
+
guard_.reset_device(device);
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
/// Sets the CUDA device to the given device index, initializing the guard if
|
| 115 |
+
/// it is not already initialized.
|
| 116 |
+
void set_index(DeviceIndex device_index) {
|
| 117 |
+
guard_.set_index(device_index);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
/// Returns the device that was set immediately prior to initialization of the
|
| 121 |
+
/// guard, or nullopt if the guard is uninitialized.
|
| 122 |
+
std::optional<Device> original_device() const {
|
| 123 |
+
return guard_.original_device();
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/// Returns the most recent device that was set using this device guard,
|
| 127 |
+
/// either from construction, or via set_device, if the guard is initialized,
|
| 128 |
+
/// or nullopt if the guard is uninitialized.
|
| 129 |
+
std::optional<Device> current_device() const {
|
| 130 |
+
return guard_.current_device();
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
/// Restore the original CUDA device, resetting this guard to uninitialized
|
| 134 |
+
/// state.
|
| 135 |
+
void reset() {
|
| 136 |
+
guard_.reset();
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
private:
|
| 140 |
+
c10::impl::InlineOptionalDeviceGuard<impl::CUDAGuardImpl> guard_;
|
| 141 |
+
};
|
| 142 |
+
|
| 143 |
+
/// A variant of StreamGuard that is specialized for CUDA. See CUDAGuard
|
| 144 |
+
/// for when you can use this.
|
| 145 |
+
struct CUDAStreamGuard {
|
| 146 |
+
/// No default constructor, see Note [Omitted default constructor from RAII]
|
| 147 |
+
explicit CUDAStreamGuard() = delete;
|
| 148 |
+
|
| 149 |
+
/// Set the current CUDA device to the device associated with the passed
|
| 150 |
+
/// stream, and set the current CUDA stream on that device to the passed
|
| 151 |
+
/// stream. Errors if the Stream is not a CUDA stream.
|
| 152 |
+
explicit CUDAStreamGuard(Stream stream) : guard_(stream) {}
|
| 153 |
+
~CUDAStreamGuard() = default;
|
| 154 |
+
|
| 155 |
+
/// Copy is disallowed
|
| 156 |
+
CUDAStreamGuard(const CUDAStreamGuard&) = delete;
|
| 157 |
+
CUDAStreamGuard& operator=(const CUDAStreamGuard&) = delete;
|
| 158 |
+
|
| 159 |
+
/// Move is disallowed, as CUDAStreamGuard does not have an uninitialized
|
| 160 |
+
/// state, which is required for moves on types with nontrivial destructors.
|
| 161 |
+
CUDAStreamGuard(CUDAStreamGuard&& other) = delete;
|
| 162 |
+
CUDAStreamGuard& operator=(CUDAStreamGuard&& other) = delete;
|
| 163 |
+
|
| 164 |
+
/// Resets the currently set stream to the original stream and
|
| 165 |
+
/// the currently set device to the original device. Then,
|
| 166 |
+
/// set the current device to the device associated with the passed stream,
|
| 167 |
+
/// and set the current stream on that device to the passed stream.
|
| 168 |
+
/// Errors if the stream passed is not a CUDA stream.
|
| 169 |
+
///
|
| 170 |
+
/// NOTE: this implementation may skip some stream/device setting if
|
| 171 |
+
/// it can prove that it is unnecessary.
|
| 172 |
+
///
|
| 173 |
+
/// WARNING: reset_stream does NOT preserve previously set streams on
|
| 174 |
+
/// different devices. If you need to set streams on multiple devices
|
| 175 |
+
/// on CUDA, use CUDAMultiStreamGuard instead.
|
| 176 |
+
void reset_stream(Stream stream) {
|
| 177 |
+
guard_.reset_stream(stream);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
/// Returns the CUDA stream that was set at the time the guard was
|
| 181 |
+
/// constructed.
|
| 182 |
+
CUDAStream original_stream() const {
|
| 183 |
+
return CUDAStream(CUDAStream::UNCHECKED, guard_.original_stream());
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
/// Returns the most recent CUDA stream that was set using this device guard,
|
| 187 |
+
/// either from construction, or via set_stream.
|
| 188 |
+
CUDAStream current_stream() const {
|
| 189 |
+
return CUDAStream(CUDAStream::UNCHECKED, guard_.current_stream());
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
/// Returns the most recent CUDA device that was set using this device guard,
|
| 193 |
+
/// either from construction, or via set_device/reset_device/set_index.
|
| 194 |
+
Device current_device() const {
|
| 195 |
+
return guard_.current_device();
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/// Returns the CUDA device that was set at the most recent reset_stream(),
|
| 199 |
+
/// or otherwise the device at construction time.
|
| 200 |
+
Device original_device() const {
|
| 201 |
+
return guard_.original_device();
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
private:
|
| 205 |
+
c10::impl::InlineStreamGuard<impl::CUDAGuardImpl> guard_;
|
| 206 |
+
};
|
| 207 |
+
|
| 208 |
+
/// A variant of OptionalStreamGuard that is specialized for CUDA. See
|
| 209 |
+
/// CUDAGuard for when you can use this.
|
| 210 |
+
struct OptionalCUDAStreamGuard {
|
| 211 |
+
/// Create an uninitialized guard.
|
| 212 |
+
explicit OptionalCUDAStreamGuard() = default;
|
| 213 |
+
|
| 214 |
+
/// Set the current CUDA device to the device associated with the passed
|
| 215 |
+
/// stream, and set the current CUDA stream on that device to the passed
|
| 216 |
+
/// stream. Errors if the Stream is not a CUDA stream.
|
| 217 |
+
explicit OptionalCUDAStreamGuard(Stream stream) : guard_(stream) {}
|
| 218 |
+
|
| 219 |
+
/// Set the current device to the device associated with the passed stream,
|
| 220 |
+
/// and set the current stream on that device to the passed stream,
|
| 221 |
+
/// if the passed stream is not nullopt.
|
| 222 |
+
explicit OptionalCUDAStreamGuard(std::optional<Stream> stream_opt)
|
| 223 |
+
: guard_(stream_opt) {}
|
| 224 |
+
|
| 225 |
+
/// Copy is disallowed
|
| 226 |
+
OptionalCUDAStreamGuard(const OptionalCUDAStreamGuard&) = delete;
|
| 227 |
+
OptionalCUDAStreamGuard& operator=(const OptionalCUDAStreamGuard&) = delete;
|
| 228 |
+
|
| 229 |
+
// See Note [Move construction for RAII guards is tricky]
|
| 230 |
+
OptionalCUDAStreamGuard(OptionalCUDAStreamGuard&& other) = delete;
|
| 231 |
+
|
| 232 |
+
// See Note [Move assignment for RAII guards is tricky]
|
| 233 |
+
OptionalCUDAStreamGuard& operator=(OptionalCUDAStreamGuard&& other) = delete;
|
| 234 |
+
~OptionalCUDAStreamGuard() = default;
|
| 235 |
+
|
| 236 |
+
/// Resets the currently set CUDA stream to the original stream and
|
| 237 |
+
/// the currently set device to the original device. Then,
|
| 238 |
+
/// set the current device to the device associated with the passed stream,
|
| 239 |
+
/// and set the current stream on that device to the passed stream.
|
| 240 |
+
/// Initializes the guard if it was not previously initialized.
|
| 241 |
+
void reset_stream(Stream stream) {
|
| 242 |
+
guard_.reset_stream(stream);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
/// Returns the CUDA stream that was set at the time the guard was most
|
| 246 |
+
/// recently initialized, or nullopt if the guard is uninitialized.
|
| 247 |
+
std::optional<CUDAStream> original_stream() const {
|
| 248 |
+
auto r = guard_.original_stream();
|
| 249 |
+
if (r.has_value()) {
|
| 250 |
+
return CUDAStream(CUDAStream::UNCHECKED, r.value());
|
| 251 |
+
} else {
|
| 252 |
+
return std::nullopt;
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
/// Returns the most recent CUDA stream that was set using this stream guard,
|
| 257 |
+
/// either from construction, or via reset_stream, if the guard is
|
| 258 |
+
/// initialized, or nullopt if the guard is uninitialized.
|
| 259 |
+
std::optional<CUDAStream> current_stream() const {
|
| 260 |
+
auto r = guard_.current_stream();
|
| 261 |
+
if (r.has_value()) {
|
| 262 |
+
return CUDAStream(CUDAStream::UNCHECKED, r.value());
|
| 263 |
+
} else {
|
| 264 |
+
return std::nullopt;
|
| 265 |
+
}
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
/// Restore the original CUDA device and stream, resetting this guard to
|
| 269 |
+
/// uninitialized state.
|
| 270 |
+
void reset() {
|
| 271 |
+
guard_.reset();
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
private:
|
| 275 |
+
c10::impl::InlineOptionalStreamGuard<impl::CUDAGuardImpl> guard_;
|
| 276 |
+
};
|
| 277 |
+
|
| 278 |
+
/// A variant of MultiStreamGuard that is specialized for CUDA.
|
| 279 |
+
struct CUDAMultiStreamGuard {
|
| 280 |
+
explicit CUDAMultiStreamGuard(ArrayRef<CUDAStream> streams)
|
| 281 |
+
: guard_(unwrapStreams(streams)) {}
|
| 282 |
+
|
| 283 |
+
/// Copy is disallowed
|
| 284 |
+
CUDAMultiStreamGuard(const CUDAMultiStreamGuard&) = delete;
|
| 285 |
+
CUDAMultiStreamGuard& operator=(const CUDAMultiStreamGuard&) = delete;
|
| 286 |
+
|
| 287 |
+
// See Note [Move construction for RAII guards is tricky]
|
| 288 |
+
CUDAMultiStreamGuard(CUDAMultiStreamGuard&& other) = delete;
|
| 289 |
+
|
| 290 |
+
// See Note [Move assignment for RAII guards is tricky]
|
| 291 |
+
CUDAMultiStreamGuard& operator=(CUDAMultiStreamGuard&& other) = delete;
|
| 292 |
+
~CUDAMultiStreamGuard() = default;
|
| 293 |
+
|
| 294 |
+
private:
|
| 295 |
+
c10::impl::InlineMultiStreamGuard<impl::CUDAGuardImpl> guard_;
|
| 296 |
+
|
| 297 |
+
static std::vector<Stream> unwrapStreams(ArrayRef<CUDAStream> cudaStreams) {
|
| 298 |
+
std::vector<Stream> streams;
|
| 299 |
+
streams.reserve(cudaStreams.size());
|
| 300 |
+
for (const CUDAStream& cudaStream : cudaStreams) {
|
| 301 |
+
streams.push_back(cudaStream);
|
| 302 |
+
}
|
| 303 |
+
return streams;
|
| 304 |
+
}
|
| 305 |
+
};
|
| 306 |
+
|
| 307 |
+
} // namespace c10::cuda
|
| 308 |
+
|
| 309 |
+
#else
|
| 310 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 311 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAMacros.h
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifndef C10_USING_CUSTOM_GENERATED_MACROS
|
| 5 |
+
|
| 6 |
+
// We have not yet modified the AMD HIP build to generate this file so
|
| 7 |
+
// we add an extra option to specifically ignore it.
|
| 8 |
+
#ifndef C10_CUDA_NO_CMAKE_CONFIGURE_FILE
|
| 9 |
+
#include <c10/cuda/impl/cuda_cmake_macros.h>
|
| 10 |
+
#endif // C10_CUDA_NO_CMAKE_CONFIGURE_FILE
|
| 11 |
+
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
// See c10/macros/Export.h for a detailed explanation of what the function
|
| 15 |
+
// of these macros are. We need one set of macros for every separate library
|
| 16 |
+
// we build.
|
| 17 |
+
|
| 18 |
+
#ifdef _WIN32
|
| 19 |
+
#if defined(C10_CUDA_BUILD_SHARED_LIBS)
|
| 20 |
+
#define C10_CUDA_EXPORT __declspec(dllexport)
|
| 21 |
+
#define C10_CUDA_IMPORT __declspec(dllimport)
|
| 22 |
+
#else
|
| 23 |
+
#define C10_CUDA_EXPORT
|
| 24 |
+
#define C10_CUDA_IMPORT
|
| 25 |
+
#endif
|
| 26 |
+
#else // _WIN32
|
| 27 |
+
#if defined(__GNUC__)
|
| 28 |
+
#define C10_CUDA_EXPORT __attribute__((__visibility__("default")))
|
| 29 |
+
#else // defined(__GNUC__)
|
| 30 |
+
#define C10_CUDA_EXPORT
|
| 31 |
+
#endif // defined(__GNUC__)
|
| 32 |
+
#define C10_CUDA_IMPORT C10_CUDA_EXPORT
|
| 33 |
+
#endif // _WIN32
|
| 34 |
+
|
| 35 |
+
// This one is being used by libc10_cuda.so
|
| 36 |
+
#ifdef C10_CUDA_BUILD_MAIN_LIB
|
| 37 |
+
#define C10_CUDA_API C10_CUDA_EXPORT
|
| 38 |
+
#else
|
| 39 |
+
#define C10_CUDA_API C10_CUDA_IMPORT
|
| 40 |
+
#endif
|
| 41 |
+
|
| 42 |
+
/**
|
| 43 |
+
* The maximum number of GPUs that we recognizes. Increasing this beyond the
|
| 44 |
+
* initial limit of 16 broke Caffe2 testing, hence the ifdef guards.
|
| 45 |
+
* This value cannot be more than 128 because our DeviceIndex is a uint8_t.
|
| 46 |
+
o */
|
| 47 |
+
#ifdef FBCODE_CAFFE2
|
| 48 |
+
// fbcode depends on this value being 16
|
| 49 |
+
#define C10_COMPILE_TIME_MAX_GPUS 16
|
| 50 |
+
#else
|
| 51 |
+
#define C10_COMPILE_TIME_MAX_GPUS 120
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
#else
|
| 55 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 56 |
+
#endif // !defined(TORC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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
/* This file defines math functions compatible across different gpu
|
| 5 |
+
* platforms (currently CUDA and HIP).
|
| 6 |
+
*/
|
| 7 |
+
#if defined(__CUDACC__) || defined(__HIPCC__)
|
| 8 |
+
|
| 9 |
+
#include <c10/macros/Macros.h>
|
| 10 |
+
#include <c10/util/Exception.h>
|
| 11 |
+
|
| 12 |
+
#ifdef __HIPCC__
|
| 13 |
+
#define __MATH_FUNCTIONS_DECL__ inline C10_DEVICE
|
| 14 |
+
#else /* __HIPCC__ */
|
| 15 |
+
#ifdef __CUDACC_RTC__
|
| 16 |
+
#define __MATH_FUNCTIONS_DECL__ C10_HOST_DEVICE
|
| 17 |
+
#else /* __CUDACC_RTC__ */
|
| 18 |
+
#define __MATH_FUNCTIONS_DECL__ inline C10_HOST_DEVICE
|
| 19 |
+
#endif /* __CUDACC_RTC__ */
|
| 20 |
+
#endif /* __HIPCC__ */
|
| 21 |
+
|
| 22 |
+
namespace c10::cuda::compat {
|
| 23 |
+
|
| 24 |
+
__MATH_FUNCTIONS_DECL__ float abs(float x) {
|
| 25 |
+
return ::fabsf(x);
|
| 26 |
+
}
|
| 27 |
+
__MATH_FUNCTIONS_DECL__ double abs(double x) {
|
| 28 |
+
return ::fabs(x);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
__MATH_FUNCTIONS_DECL__ float exp(float x) {
|
| 32 |
+
return ::expf(x);
|
| 33 |
+
}
|
| 34 |
+
__MATH_FUNCTIONS_DECL__ double exp(double x) {
|
| 35 |
+
return ::exp(x);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
__MATH_FUNCTIONS_DECL__ float ceil(float x) {
|
| 39 |
+
return ::ceilf(x);
|
| 40 |
+
}
|
| 41 |
+
__MATH_FUNCTIONS_DECL__ double ceil(double x) {
|
| 42 |
+
return ::ceil(x);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
__MATH_FUNCTIONS_DECL__ float copysign(float x, float y) {
|
| 46 |
+
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
|
| 47 |
+
return ::copysignf(x, y);
|
| 48 |
+
#else
|
| 49 |
+
// std::copysign gets ICE/Segfaults with gcc 7.5/8 on arm64
|
| 50 |
+
// (e.g. Jetson), see PyTorch PR #51834
|
| 51 |
+
// This host function needs to be here for the compiler but is never used
|
| 52 |
+
TORCH_INTERNAL_ASSERT(
|
| 53 |
+
false, "CUDAMathCompat copysign should not run on the CPU");
|
| 54 |
+
#endif
|
| 55 |
+
}
|
| 56 |
+
__MATH_FUNCTIONS_DECL__ double copysign(double x, double y) {
|
| 57 |
+
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
|
| 58 |
+
return ::copysign(x, y);
|
| 59 |
+
#else
|
| 60 |
+
// see above
|
| 61 |
+
TORCH_INTERNAL_ASSERT(
|
| 62 |
+
false, "CUDAMathCompat copysign should not run on the CPU");
|
| 63 |
+
#endif
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
__MATH_FUNCTIONS_DECL__ float floor(float x) {
|
| 67 |
+
return ::floorf(x);
|
| 68 |
+
}
|
| 69 |
+
__MATH_FUNCTIONS_DECL__ double floor(double x) {
|
| 70 |
+
return ::floor(x);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
__MATH_FUNCTIONS_DECL__ float log(float x) {
|
| 74 |
+
return ::logf(x);
|
| 75 |
+
}
|
| 76 |
+
__MATH_FUNCTIONS_DECL__ double log(double x) {
|
| 77 |
+
return ::log(x);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
__MATH_FUNCTIONS_DECL__ float log1p(float x) {
|
| 81 |
+
return ::log1pf(x);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
__MATH_FUNCTIONS_DECL__ double log1p(double x) {
|
| 85 |
+
return ::log1p(x);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
__MATH_FUNCTIONS_DECL__ float max(float x, float y) {
|
| 89 |
+
return ::fmaxf(x, y);
|
| 90 |
+
}
|
| 91 |
+
__MATH_FUNCTIONS_DECL__ double max(double x, double y) {
|
| 92 |
+
return ::fmax(x, y);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__MATH_FUNCTIONS_DECL__ float min(float x, float y) {
|
| 96 |
+
return ::fminf(x, y);
|
| 97 |
+
}
|
| 98 |
+
__MATH_FUNCTIONS_DECL__ double min(double x, double y) {
|
| 99 |
+
return ::fmin(x, y);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
__MATH_FUNCTIONS_DECL__ float pow(float x, float y) {
|
| 103 |
+
return ::powf(x, y);
|
| 104 |
+
}
|
| 105 |
+
__MATH_FUNCTIONS_DECL__ double pow(double x, double y) {
|
| 106 |
+
return ::pow(x, y);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
__MATH_FUNCTIONS_DECL__ void sincos(float x, float* sptr, float* cptr) {
|
| 110 |
+
return ::sincosf(x, sptr, cptr);
|
| 111 |
+
}
|
| 112 |
+
__MATH_FUNCTIONS_DECL__ void sincos(double x, double* sptr, double* cptr) {
|
| 113 |
+
return ::sincos(x, sptr, cptr);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
__MATH_FUNCTIONS_DECL__ float sqrt(float x) {
|
| 117 |
+
return ::sqrtf(x);
|
| 118 |
+
}
|
| 119 |
+
__MATH_FUNCTIONS_DECL__ double sqrt(double x) {
|
| 120 |
+
return ::sqrt(x);
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
__MATH_FUNCTIONS_DECL__ float rsqrt(float x) {
|
| 124 |
+
return ::rsqrtf(x);
|
| 125 |
+
}
|
| 126 |
+
__MATH_FUNCTIONS_DECL__ double rsqrt(double x) {
|
| 127 |
+
return ::rsqrt(x);
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
__MATH_FUNCTIONS_DECL__ float tan(float x) {
|
| 131 |
+
return ::tanf(x);
|
| 132 |
+
}
|
| 133 |
+
__MATH_FUNCTIONS_DECL__ double tan(double x) {
|
| 134 |
+
return ::tan(x);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
__MATH_FUNCTIONS_DECL__ float tanh(float x) {
|
| 138 |
+
return ::tanhf(x);
|
| 139 |
+
}
|
| 140 |
+
__MATH_FUNCTIONS_DECL__ double tanh(double x) {
|
| 141 |
+
return ::tanh(x);
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
__MATH_FUNCTIONS_DECL__ float normcdf(float x) {
|
| 145 |
+
return ::normcdff(x);
|
| 146 |
+
}
|
| 147 |
+
__MATH_FUNCTIONS_DECL__ double normcdf(double x) {
|
| 148 |
+
return ::normcdf(x);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
} // namespace c10::cuda::compat
|
| 152 |
+
|
| 153 |
+
#endif
|
| 154 |
+
|
| 155 |
+
#else
|
| 156 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 157 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/CUDAMiscFunctions.h
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
// this file is to avoid circular dependency between CUDAFunctions.h and
|
| 4 |
+
// CUDAExceptions.h
|
| 5 |
+
|
| 6 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 7 |
+
#include <cuda_runtime.h>
|
| 8 |
+
|
| 9 |
+
#include <mutex>
|
| 10 |
+
#include <string>
|
| 11 |
+
|
| 12 |
+
namespace c10::cuda {
|
| 13 |
+
C10_CUDA_API std::string get_cuda_error_help(cudaError_t /*error*/) noexcept;
|
| 14 |
+
C10_CUDA_API const char* get_cuda_check_suffix() noexcept;
|
| 15 |
+
C10_CUDA_API std::mutex* getFreeMutex();
|
| 16 |
+
} // namespace c10::cuda
|
| 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/c10/cuda/CUDAStream.h
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <cuda_runtime_api.h>
|
| 5 |
+
|
| 6 |
+
#include <c10/core/DeviceGuard.h>
|
| 7 |
+
#include <c10/core/Stream.h>
|
| 8 |
+
#include <c10/cuda/CUDAFunctions.h>
|
| 9 |
+
#include <c10/util/Exception.h>
|
| 10 |
+
|
| 11 |
+
/*
|
| 12 |
+
* Stream pool note.
|
| 13 |
+
*
|
| 14 |
+
* A CUDAStream is an abstraction of an actual cuStream on the GPU. CUDAStreams
|
| 15 |
+
* are backed by cuStreams, but they use several pools to minimize the costs
|
| 16 |
+
* associated with creating, retaining, and destroying cuStreams.
|
| 17 |
+
*
|
| 18 |
+
* There are three pools per device, and a device's pools are lazily created.
|
| 19 |
+
*
|
| 20 |
+
* The first pool contains only the default stream. When the default stream
|
| 21 |
+
* is requested it's returned.
|
| 22 |
+
*
|
| 23 |
+
* The second pool is the "low priority" or "default priority" streams. In
|
| 24 |
+
* HIP builds there is no distinction between streams in this pool and streams
|
| 25 |
+
* in the third pool (below). There are 32 of these streams per device, and
|
| 26 |
+
* when a stream is requested one of these streams is returned round-robin.
|
| 27 |
+
* That is, the first stream requested is at index 0, the second at index 1...
|
| 28 |
+
* to index 31, then index 0 again.
|
| 29 |
+
*
|
| 30 |
+
* This means that if 33 low priority streams are requested, the first and
|
| 31 |
+
* last streams requested are actually the same stream (under the covers)
|
| 32 |
+
* and kernels enqueued on them cannot run concurrently.
|
| 33 |
+
*
|
| 34 |
+
* The third pool is the "high priority" streams. The third pool acts like
|
| 35 |
+
* the second pool except the streams are created with a higher priority.
|
| 36 |
+
*
|
| 37 |
+
* These pools suggest that stream users should prefer many short-lived streams,
|
| 38 |
+
* as the cost of acquiring and releasing streams is effectively zero. If
|
| 39 |
+
* many longer-lived streams are required in performance critical scenarios
|
| 40 |
+
* then the functionality here may need to be extended to allow, for example,
|
| 41 |
+
* "reserving" a subset of the pool so that other streams do not accidentally
|
| 42 |
+
* overlap the performance critical streams.
|
| 43 |
+
*
|
| 44 |
+
* Note: although the notion of "current stream for device" is thread local
|
| 45 |
+
* (every OS thread has a separate current stream, as one might expect),
|
| 46 |
+
* the stream pool is global across all threads; stream 0 is always stream 0
|
| 47 |
+
* no matter which thread you use it on. Multiple threads can synchronize
|
| 48 |
+
* on the same stream. Although the CUDA documentation is not very clear
|
| 49 |
+
* on the matter, streams are thread safe; e.g., it is safe to enqueue
|
| 50 |
+
* a kernel on the same stream from two different threads.
|
| 51 |
+
*/
|
| 52 |
+
|
| 53 |
+
namespace c10::cuda {
|
| 54 |
+
|
| 55 |
+
static constexpr int max_compile_time_stream_priorities = 4;
|
| 56 |
+
|
| 57 |
+
// Value object representing a CUDA stream. This is just a wrapper
|
| 58 |
+
// around c10::Stream, but it comes with a little extra CUDA-specific
|
| 59 |
+
// functionality (conversion to cudaStream_t), and a guarantee that
|
| 60 |
+
// the wrapped c10::Stream really is a CUDA stream.
|
| 61 |
+
class C10_CUDA_API CUDAStream {
|
| 62 |
+
public:
|
| 63 |
+
enum Unchecked { UNCHECKED };
|
| 64 |
+
|
| 65 |
+
/// Construct a CUDAStream from a Stream. This construction is checked,
|
| 66 |
+
/// and will raise an error if the Stream is not, in fact, a CUDA stream.
|
| 67 |
+
explicit CUDAStream(Stream stream) : stream_(stream) {
|
| 68 |
+
TORCH_CHECK(stream_.device_type() == DeviceType::CUDA);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// Construct a CUDAStream from a Stream with no error checking.
|
| 72 |
+
/// This constructor uses the "named" constructor idiom, and can
|
| 73 |
+
/// be invoked as: CUDAStream(CUDAStream::UNCHECKED, stream)
|
| 74 |
+
explicit CUDAStream(Unchecked /*unused*/, Stream stream) : stream_(stream) {}
|
| 75 |
+
|
| 76 |
+
bool operator==(const CUDAStream& other) const noexcept {
|
| 77 |
+
return unwrap() == other.unwrap();
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
bool operator!=(const CUDAStream& other) const noexcept {
|
| 81 |
+
return unwrap() != other.unwrap();
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
/// Implicit conversion to cudaStream_t.
|
| 85 |
+
operator cudaStream_t() const {
|
| 86 |
+
return stream();
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/// Implicit conversion to Stream (a.k.a., forget that the stream is a
|
| 90 |
+
/// CUDA stream).
|
| 91 |
+
operator Stream() const {
|
| 92 |
+
return unwrap();
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/// Used to avoid baking in device type explicitly to Python-side API.
|
| 96 |
+
DeviceType device_type() const {
|
| 97 |
+
return DeviceType::CUDA;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/// Get the CUDA device index that this stream is associated with.
|
| 101 |
+
DeviceIndex device_index() const {
|
| 102 |
+
return stream_.device_index();
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/// Get the full Device that this stream is associated with. The Device
|
| 106 |
+
/// is guaranteed to be a CUDA device.
|
| 107 |
+
Device device() const {
|
| 108 |
+
return Device(DeviceType::CUDA, device_index());
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
/// Return the stream ID corresponding to this particular stream.
|
| 112 |
+
StreamId id() const {
|
| 113 |
+
return stream_.id();
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
bool query() const {
|
| 117 |
+
DeviceGuard guard{stream_.device()};
|
| 118 |
+
cudaError_t err = C10_CUDA_ERROR_HANDLED(cudaStreamQuery(stream()));
|
| 119 |
+
|
| 120 |
+
if (err == cudaSuccess) {
|
| 121 |
+
return true;
|
| 122 |
+
} else if (err != cudaErrorNotReady) {
|
| 123 |
+
C10_CUDA_CHECK(err);
|
| 124 |
+
} else {
|
| 125 |
+
// ignore and clear the error if not ready
|
| 126 |
+
(void)cudaGetLastError();
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
return false;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
void synchronize() const {
|
| 133 |
+
DeviceGuard guard{stream_.device()};
|
| 134 |
+
c10::cuda::stream_synchronize(stream());
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
int priority() const {
|
| 138 |
+
DeviceGuard guard{stream_.device()};
|
| 139 |
+
int priority = 0;
|
| 140 |
+
C10_CUDA_CHECK(cudaStreamGetPriority(stream(), &priority));
|
| 141 |
+
return priority;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/// Explicit conversion to cudaStream_t.
|
| 145 |
+
cudaStream_t stream() const;
|
| 146 |
+
|
| 147 |
+
/// Explicit conversion to Stream.
|
| 148 |
+
Stream unwrap() const {
|
| 149 |
+
return stream_;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
/// Reversibly pack a CUDAStream into a struct representation.
|
| 153 |
+
/// Previously the stream's data was packed into a single int64_t,
|
| 154 |
+
/// as it was assumed the fields would not require more than
|
| 155 |
+
/// 64 bits of storage in total.
|
| 156 |
+
/// See https://github.com/pytorch/pytorch/issues/75854
|
| 157 |
+
/// for more information regarding newer platforms that may violate
|
| 158 |
+
/// this assumption.
|
| 159 |
+
///
|
| 160 |
+
/// The CUDAStream can be unpacked using unpack().
|
| 161 |
+
struct c10::StreamData3 pack3() const {
|
| 162 |
+
return stream_.pack3();
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// Unpack a CUDAStream from the 3 fields generated by pack().
|
| 166 |
+
static CUDAStream unpack3(
|
| 167 |
+
StreamId stream_id,
|
| 168 |
+
DeviceIndex device_index,
|
| 169 |
+
DeviceType device_type) {
|
| 170 |
+
return CUDAStream(Stream::unpack3(stream_id, device_index, device_type));
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
static std::tuple<int, int> priority_range() {
|
| 174 |
+
// Note: this returns the range of priority **supported by PyTorch**, not
|
| 175 |
+
// the range of priority **supported by CUDA**. The former is a subset of
|
| 176 |
+
// the latter.
|
| 177 |
+
int least_priority = 0, greatest_priority = 0;
|
| 178 |
+
C10_CUDA_CHECK(
|
| 179 |
+
cudaDeviceGetStreamPriorityRange(&least_priority, &greatest_priority));
|
| 180 |
+
#ifdef USE_ROCM
|
| 181 |
+
// See Note [HIP stream priorities]
|
| 182 |
+
TORCH_INTERNAL_ASSERT(
|
| 183 |
+
least_priority == 1, "Unexpected HIP stream priority range");
|
| 184 |
+
least_priority = 0;
|
| 185 |
+
#else
|
| 186 |
+
TORCH_INTERNAL_ASSERT(
|
| 187 |
+
least_priority == 0, "Unexpected CUDA stream priority range");
|
| 188 |
+
#endif
|
| 189 |
+
TORCH_INTERNAL_ASSERT(
|
| 190 |
+
greatest_priority <= -1, "Unexpected CUDA stream priority range");
|
| 191 |
+
greatest_priority = std::max(
|
| 192 |
+
-c10::cuda::max_compile_time_stream_priorities + 1, greatest_priority);
|
| 193 |
+
return std::make_tuple(least_priority, greatest_priority);
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// Deleted for now; use CUDAEvent::block instead
|
| 197 |
+
// void synchronize_with(const CUDAEvent& event) const;
|
| 198 |
+
|
| 199 |
+
private:
|
| 200 |
+
Stream stream_;
|
| 201 |
+
};
|
| 202 |
+
|
| 203 |
+
/**
|
| 204 |
+
* Get a new stream from the CUDA stream pool. You can think of this
|
| 205 |
+
* as "creating" a new stream, but no such creation actually happens;
|
| 206 |
+
* instead, streams are preallocated from the pool and returned in a
|
| 207 |
+
* round-robin fashion.
|
| 208 |
+
*
|
| 209 |
+
* You can request a stream from the high priority pool by setting
|
| 210 |
+
* isHighPriority to true, or a stream for a specific device by setting device
|
| 211 |
+
* (defaulting to the current CUDA stream.)
|
| 212 |
+
*/
|
| 213 |
+
C10_API CUDAStream
|
| 214 |
+
getStreamFromPool(const bool isHighPriority = false, DeviceIndex device = -1);
|
| 215 |
+
// no default priority to disambiguate overloads
|
| 216 |
+
C10_API CUDAStream
|
| 217 |
+
getStreamFromPool(const int priority, DeviceIndex device = -1);
|
| 218 |
+
|
| 219 |
+
/**
|
| 220 |
+
* Get a CUDAStream from a externally allocated one.
|
| 221 |
+
*
|
| 222 |
+
* This is mainly for interoperability with different libraries where we
|
| 223 |
+
* want to operate on a non-torch allocated stream for data exchange or similar
|
| 224 |
+
* purposes
|
| 225 |
+
*/
|
| 226 |
+
C10_API CUDAStream
|
| 227 |
+
getStreamFromExternal(cudaStream_t ext_stream, DeviceIndex device_index);
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* Get the default CUDA stream, for the passed CUDA device, or for the
|
| 231 |
+
* current device if no device index is passed. The default stream is
|
| 232 |
+
* where most computation occurs when you aren't explicitly using
|
| 233 |
+
* streams.
|
| 234 |
+
*/
|
| 235 |
+
C10_API CUDAStream getDefaultCUDAStream(DeviceIndex device_index = -1);
|
| 236 |
+
|
| 237 |
+
/**
|
| 238 |
+
* Get the current CUDA stream, for the passed CUDA device, or for the
|
| 239 |
+
* current device if no device index is passed. The current CUDA stream
|
| 240 |
+
* will usually be the default CUDA stream for the device, but it may
|
| 241 |
+
* be different if someone called 'setCurrentCUDAStream' or used 'StreamGuard'
|
| 242 |
+
* or 'CUDAStreamGuard'.
|
| 243 |
+
*/
|
| 244 |
+
C10_API CUDAStream getCurrentCUDAStream(DeviceIndex device_index = -1);
|
| 245 |
+
|
| 246 |
+
/**
|
| 247 |
+
* Set the current stream on the device of the passed in stream to be
|
| 248 |
+
* the passed in stream. Yes, you read that right: this function
|
| 249 |
+
* has *nothing* to do with the current device: it toggles the current
|
| 250 |
+
* stream of the device of the passed stream.
|
| 251 |
+
*
|
| 252 |
+
* Confused? Avoid using this function; prefer using 'CUDAStreamGuard' instead
|
| 253 |
+
* (which will switch both your current device and current stream in the way you
|
| 254 |
+
* expect, and reset it back to its original state afterwards).
|
| 255 |
+
*/
|
| 256 |
+
C10_API void setCurrentCUDAStream(CUDAStream stream);
|
| 257 |
+
|
| 258 |
+
C10_API std::ostream& operator<<(std::ostream& stream, const CUDAStream& s);
|
| 259 |
+
|
| 260 |
+
} // namespace c10::cuda
|
| 261 |
+
|
| 262 |
+
namespace std {
|
| 263 |
+
template <>
|
| 264 |
+
struct hash<c10::cuda::CUDAStream> {
|
| 265 |
+
size_t operator()(c10::cuda::CUDAStream s) const noexcept {
|
| 266 |
+
return std::hash<c10::Stream>{}(s.unwrap());
|
| 267 |
+
}
|
| 268 |
+
};
|
| 269 |
+
} // namespace std
|
| 270 |
+
|
| 271 |
+
#else
|
| 272 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 273 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/driver_api.h
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <cuda.h>
|
| 4 |
+
#define NVML_NO_UNVERSIONED_FUNC_DEFS
|
| 5 |
+
#include <nvml.h>
|
| 6 |
+
|
| 7 |
+
#include <c10/util/Exception.h>
|
| 8 |
+
|
| 9 |
+
#define C10_CUDA_DRIVER_CHECK(EXPR) \
|
| 10 |
+
do { \
|
| 11 |
+
CUresult __err = EXPR; \
|
| 12 |
+
if (__err != CUDA_SUCCESS) { \
|
| 13 |
+
const char* err_str; \
|
| 14 |
+
CUresult get_error_str_err [[maybe_unused]] = \
|
| 15 |
+
c10::cuda::DriverAPI::get()->cuGetErrorString_(__err, &err_str); \
|
| 16 |
+
if (get_error_str_err != CUDA_SUCCESS) { \
|
| 17 |
+
TORCH_CHECK(false, "CUDA driver error: unknown error"); \
|
| 18 |
+
} else { \
|
| 19 |
+
TORCH_CHECK(false, "CUDA driver error: ", err_str); \
|
| 20 |
+
} \
|
| 21 |
+
} \
|
| 22 |
+
} while (0)
|
| 23 |
+
|
| 24 |
+
#define C10_CUDA_DRIVER_CHECK_GOTO(EXPR, NEXT) \
|
| 25 |
+
do { \
|
| 26 |
+
CUresult __err = EXPR; \
|
| 27 |
+
if (__err != CUDA_SUCCESS) { \
|
| 28 |
+
const char* err_str; \
|
| 29 |
+
CUresult get_error_str_err [[maybe_unused]] = \
|
| 30 |
+
c10::cuda::DriverAPI::get()->cuGetErrorString_(__err, &err_str); \
|
| 31 |
+
if (get_error_str_err != CUDA_SUCCESS) { \
|
| 32 |
+
TORCH_WARN("CUDA driver error: unknown error"); \
|
| 33 |
+
} else { \
|
| 34 |
+
TORCH_WARN("CUDA driver error: ", err_str); \
|
| 35 |
+
} \
|
| 36 |
+
goto NEXT; \
|
| 37 |
+
} \
|
| 38 |
+
} while (0)
|
| 39 |
+
|
| 40 |
+
// The integer in the second column specifies the requested CUDA Driver API
|
| 41 |
+
// version. The dynamic loader will accept a driver with a newer version, but it
|
| 42 |
+
// ensures that the requested symbol exists in *at least* the specified version
|
| 43 |
+
// or earlier.
|
| 44 |
+
|
| 45 |
+
// Keep these requested versions as low as possible to maximize compatibility
|
| 46 |
+
// across different driver versions.
|
| 47 |
+
|
| 48 |
+
// Why do we pin to an older version instead of using the latest?
|
| 49 |
+
// If a user installs a newer driver, blindly resolving the symbol may bind to a
|
| 50 |
+
// newer version of the function with different behavior, potentially breaking
|
| 51 |
+
// PyTorch.
|
| 52 |
+
|
| 53 |
+
#define C10_LIBCUDA_DRIVER_API_REQUIRED(_) \
|
| 54 |
+
_(cuDeviceGetAttribute, 12000) \
|
| 55 |
+
_(cuMemAddressReserve, 12000) \
|
| 56 |
+
_(cuMemRelease, 12000) \
|
| 57 |
+
_(cuMemMap, 12000) \
|
| 58 |
+
_(cuMemAddressFree, 12000) \
|
| 59 |
+
_(cuMemSetAccess, 12000) \
|
| 60 |
+
_(cuMemUnmap, 12000) \
|
| 61 |
+
_(cuMemCreate, 12000) \
|
| 62 |
+
_(cuMemGetAllocationGranularity, 12000) \
|
| 63 |
+
_(cuMemExportToShareableHandle, 12000) \
|
| 64 |
+
_(cuMemImportFromShareableHandle, 12000) \
|
| 65 |
+
_(cuMemsetD32Async, 12000) \
|
| 66 |
+
_(cuStreamWriteValue32, 12000) \
|
| 67 |
+
_(cuGetErrorString, 12000)
|
| 68 |
+
|
| 69 |
+
#if defined(CUDA_VERSION) && (CUDA_VERSION >= 12030)
|
| 70 |
+
#define C10_LIBCUDA_DRIVER_API_OPTIONAL(_) \
|
| 71 |
+
_(cuCtxFromGreenCtx, 12080) \
|
| 72 |
+
_(cuCtxGetCurrent, 12080) \
|
| 73 |
+
_(cuCtxPopCurrent, 12080) \
|
| 74 |
+
_(cuCtxPushCurrent, 12080) \
|
| 75 |
+
_(cuCtxSetCurrent, 12080) \
|
| 76 |
+
_(cuGreenCtxCreate, 12080) \
|
| 77 |
+
_(cuGreenCtxDestroy, 12080) \
|
| 78 |
+
_(cuDevSmResourceSplitByCount, 12080) \
|
| 79 |
+
_(cuDeviceGet, 12080) \
|
| 80 |
+
_(cuDeviceGetDevResource, 12080) \
|
| 81 |
+
_(cuDevResourceGenerateDesc, 12080) \
|
| 82 |
+
_(cuMulticastAddDevice, 12030) \
|
| 83 |
+
_(cuMulticastBindMem, 12030) \
|
| 84 |
+
_(cuMulticastCreate, 12030) \
|
| 85 |
+
_(cuMulticastUnbind, 12030)
|
| 86 |
+
#else
|
| 87 |
+
#define C10_LIBCUDA_DRIVER_API_OPTIONAL(_)
|
| 88 |
+
#endif
|
| 89 |
+
|
| 90 |
+
#define C10_NVML_DRIVER_API(_) \
|
| 91 |
+
_(nvmlInit_v2) \
|
| 92 |
+
_(nvmlDeviceGetHandleByPciBusId_v2) \
|
| 93 |
+
_(nvmlDeviceGetNvLinkRemoteDeviceType) \
|
| 94 |
+
_(nvmlDeviceGetNvLinkRemotePciInfo_v2) \
|
| 95 |
+
_(nvmlDeviceGetComputeRunningProcesses) \
|
| 96 |
+
_(nvmlSystemGetCudaDriverVersion_v2)
|
| 97 |
+
|
| 98 |
+
#if defined(CUDA_VERSION) && (CUDA_VERSION >= 12040)
|
| 99 |
+
#define C10_NVML_DRIVER_API_OPTIONAL(_) _(nvmlDeviceGetGpuFabricInfoV)
|
| 100 |
+
#else
|
| 101 |
+
#define C10_NVML_DRIVER_API_OPTIONAL(_)
|
| 102 |
+
#endif
|
| 103 |
+
|
| 104 |
+
namespace c10::cuda {
|
| 105 |
+
|
| 106 |
+
struct DriverAPI {
|
| 107 |
+
#define CREATE_MEMBER_VERSIONED(name, version) decltype(&name) name##_;
|
| 108 |
+
#define CREATE_MEMBER(name) decltype(&name) name##_;
|
| 109 |
+
C10_LIBCUDA_DRIVER_API_REQUIRED(CREATE_MEMBER_VERSIONED)
|
| 110 |
+
C10_LIBCUDA_DRIVER_API_OPTIONAL(CREATE_MEMBER_VERSIONED)
|
| 111 |
+
C10_NVML_DRIVER_API(CREATE_MEMBER)
|
| 112 |
+
C10_NVML_DRIVER_API_OPTIONAL(CREATE_MEMBER)
|
| 113 |
+
#undef CREATE_MEMBER_VERSIONED
|
| 114 |
+
#undef CREATE_MEMBER
|
| 115 |
+
|
| 116 |
+
static DriverAPI* get();
|
| 117 |
+
static void* get_nvml_handle();
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
} // namespace c10::cuda
|
| 121 |
+
|
| 122 |
+
#else
|
| 123 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 124 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/impl/CUDAGuardImpl.h
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/DeviceGuardImplInterface.h>
|
| 5 |
+
#include <c10/core/impl/GPUTrace.h>
|
| 6 |
+
#include <c10/macros/Macros.h>
|
| 7 |
+
#include <c10/util/Exception.h>
|
| 8 |
+
|
| 9 |
+
#include <c10/cuda/CUDACachingAllocator.h>
|
| 10 |
+
#include <c10/cuda/CUDAException.h>
|
| 11 |
+
#include <c10/cuda/CUDAFunctions.h>
|
| 12 |
+
#include <c10/cuda/CUDAStream.h>
|
| 13 |
+
|
| 14 |
+
#include <c10/core/Device.h>
|
| 15 |
+
#include <c10/core/DeviceType.h>
|
| 16 |
+
#include <c10/core/Stream.h>
|
| 17 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 18 |
+
#include <cuda_runtime_api.h>
|
| 19 |
+
#include <cstdint>
|
| 20 |
+
#include <optional>
|
| 21 |
+
|
| 22 |
+
namespace c10::cuda::impl {
|
| 23 |
+
|
| 24 |
+
struct CUDAGuardImpl final : public c10::impl::DeviceGuardImplInterface {
|
| 25 |
+
static constexpr DeviceType static_type = DeviceType::CUDA;
|
| 26 |
+
|
| 27 |
+
CUDAGuardImpl() = default;
|
| 28 |
+
explicit CUDAGuardImpl(DeviceType t) {
|
| 29 |
+
TORCH_CHECK(
|
| 30 |
+
t == DeviceType::CUDA,
|
| 31 |
+
"CUDAGuardImpl initialized with non-CUDA DeviceType: ",
|
| 32 |
+
t);
|
| 33 |
+
}
|
| 34 |
+
DeviceType type() const override {
|
| 35 |
+
return DeviceType::CUDA;
|
| 36 |
+
}
|
| 37 |
+
Device exchangeDevice(Device d) const override {
|
| 38 |
+
TORCH_CHECK(d.is_cuda(), "Expected a CUDA device, but got ", d);
|
| 39 |
+
auto old_device_index = c10::cuda::ExchangeDevice(d.index());
|
| 40 |
+
return Device(DeviceType::CUDA, old_device_index);
|
| 41 |
+
}
|
| 42 |
+
Device getDevice() const override {
|
| 43 |
+
DeviceIndex device = 0;
|
| 44 |
+
C10_CUDA_CHECK(c10::cuda::GetDevice(&device));
|
| 45 |
+
return Device(DeviceType::CUDA, device);
|
| 46 |
+
}
|
| 47 |
+
std::optional<Device> uncheckedGetDevice() const noexcept {
|
| 48 |
+
DeviceIndex device{-1};
|
| 49 |
+
const auto err = C10_CUDA_ERROR_HANDLED(c10::cuda::GetDevice(&device));
|
| 50 |
+
C10_CUDA_CHECK_WARN(err);
|
| 51 |
+
if (err != cudaSuccess) {
|
| 52 |
+
return std::nullopt;
|
| 53 |
+
}
|
| 54 |
+
return Device(DeviceType::CUDA, device);
|
| 55 |
+
}
|
| 56 |
+
void setDevice(Device d) const override {
|
| 57 |
+
TORCH_CHECK(d.is_cuda(), "Expected a CUDA device, but got ", d);
|
| 58 |
+
C10_CUDA_CHECK(c10::cuda::SetDevice(d.index()));
|
| 59 |
+
}
|
| 60 |
+
void uncheckedSetDevice(Device d) const noexcept override {
|
| 61 |
+
C10_CUDA_CHECK_WARN(c10::cuda::MaybeSetDevice(d.index()));
|
| 62 |
+
}
|
| 63 |
+
Stream getStream(Device d) const override {
|
| 64 |
+
return getCurrentCUDAStream(d.index()).unwrap();
|
| 65 |
+
}
|
| 66 |
+
Stream getDefaultStream(Device d) const override {
|
| 67 |
+
return getDefaultCUDAStream(d.index());
|
| 68 |
+
}
|
| 69 |
+
Stream getNewStream(Device d, int priority = 0) const override {
|
| 70 |
+
return getStreamFromPool(priority, d.index());
|
| 71 |
+
}
|
| 72 |
+
Stream getStreamFromGlobalPool(Device d, bool isHighPriority = false)
|
| 73 |
+
const override {
|
| 74 |
+
return getStreamFromPool(isHighPriority, d.index());
|
| 75 |
+
}
|
| 76 |
+
// NB: These do NOT set the current device
|
| 77 |
+
Stream exchangeStream(Stream s) const override {
|
| 78 |
+
CUDAStream cs(s);
|
| 79 |
+
auto old_stream = getCurrentCUDAStream(s.device().index());
|
| 80 |
+
setCurrentCUDAStream(cs);
|
| 81 |
+
return old_stream.unwrap();
|
| 82 |
+
}
|
| 83 |
+
DeviceIndex deviceCount() const noexcept override {
|
| 84 |
+
return device_count();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// Event-related functions
|
| 88 |
+
void createEvent(cudaEvent_t* cuda_event, const EventFlag flag) const {
|
| 89 |
+
// Maps PyTorch's Event::Flag to CUDA flag
|
| 90 |
+
auto cuda_flag = cudaEventDefault;
|
| 91 |
+
switch (flag) {
|
| 92 |
+
case EventFlag::PYTORCH_DEFAULT:
|
| 93 |
+
cuda_flag = cudaEventDisableTiming;
|
| 94 |
+
break;
|
| 95 |
+
case EventFlag::BACKEND_DEFAULT:
|
| 96 |
+
cuda_flag = cudaEventDefault;
|
| 97 |
+
break;
|
| 98 |
+
default:
|
| 99 |
+
TORCH_CHECK(false, "CUDA event received unknown flag");
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
C10_CUDA_CHECK(cudaEventCreateWithFlags(cuda_event, cuda_flag));
|
| 103 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 104 |
+
if (C10_UNLIKELY(interp)) {
|
| 105 |
+
(*interp)->trace_gpu_event_creation(
|
| 106 |
+
c10::kCUDA, reinterpret_cast<uintptr_t>(cuda_event));
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
void destroyEvent(void* event, const DeviceIndex device_index)
|
| 111 |
+
const noexcept override {
|
| 112 |
+
if (!event)
|
| 113 |
+
return;
|
| 114 |
+
auto cuda_event = static_cast<cudaEvent_t>(event);
|
| 115 |
+
DeviceIndex orig_device{-1};
|
| 116 |
+
C10_CUDA_CHECK_WARN(c10::cuda::GetDevice(&orig_device));
|
| 117 |
+
C10_CUDA_CHECK_WARN(c10::cuda::SetDevice(device_index));
|
| 118 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 119 |
+
if (C10_UNLIKELY(interp)) {
|
| 120 |
+
(*interp)->trace_gpu_event_deletion(
|
| 121 |
+
c10::kCUDA, reinterpret_cast<uintptr_t>(cuda_event));
|
| 122 |
+
}
|
| 123 |
+
C10_CUDA_CHECK_WARN(cudaEventDestroy(cuda_event));
|
| 124 |
+
C10_CUDA_CHECK_WARN(c10::cuda::SetDevice(orig_device));
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
void record(
|
| 128 |
+
void** event,
|
| 129 |
+
const Stream& stream,
|
| 130 |
+
const DeviceIndex device_index,
|
| 131 |
+
const EventFlag flag) const override {
|
| 132 |
+
TORCH_CHECK(
|
| 133 |
+
device_index == -1 || device_index == stream.device_index(),
|
| 134 |
+
"Event device index ",
|
| 135 |
+
device_index,
|
| 136 |
+
" does not match recording stream's device index ",
|
| 137 |
+
stream.device_index(),
|
| 138 |
+
".");
|
| 139 |
+
|
| 140 |
+
cudaEvent_t cuda_event = static_cast<cudaEvent_t>(*event);
|
| 141 |
+
CUDAStream cuda_stream{stream};
|
| 142 |
+
|
| 143 |
+
// Moves to stream's device to record
|
| 144 |
+
const auto orig_device = getDevice();
|
| 145 |
+
setDevice(stream.device());
|
| 146 |
+
|
| 147 |
+
// Creates the event (lazily)
|
| 148 |
+
if (!cuda_event)
|
| 149 |
+
createEvent(&cuda_event, flag);
|
| 150 |
+
C10_CUDA_CHECK(cudaEventRecord(cuda_event, cuda_stream));
|
| 151 |
+
// Makes the void* point to the (possibly just allocated) CUDA event
|
| 152 |
+
*event = cuda_event;
|
| 153 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 154 |
+
if (C10_UNLIKELY(interp)) {
|
| 155 |
+
(*interp)->trace_gpu_event_record(
|
| 156 |
+
c10::kCUDA,
|
| 157 |
+
reinterpret_cast<uintptr_t>(cuda_event),
|
| 158 |
+
reinterpret_cast<uintptr_t>(cuda_stream.stream()));
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// Resets device
|
| 162 |
+
setDevice(orig_device);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
void block(void* event, const Stream& stream) const override {
|
| 166 |
+
if (!event)
|
| 167 |
+
return;
|
| 168 |
+
cudaEvent_t cuda_event = static_cast<cudaEvent_t>(event);
|
| 169 |
+
CUDAStream cuda_stream{stream};
|
| 170 |
+
const auto orig_device = getDevice();
|
| 171 |
+
setDevice(stream.device());
|
| 172 |
+
C10_CUDA_CHECK(cudaStreamWaitEvent(
|
| 173 |
+
cuda_stream,
|
| 174 |
+
cuda_event,
|
| 175 |
+
/*flags (must be zero)=*/0));
|
| 176 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 177 |
+
if (C10_UNLIKELY(interp)) {
|
| 178 |
+
(*interp)->trace_gpu_event_wait(
|
| 179 |
+
c10::kCUDA,
|
| 180 |
+
reinterpret_cast<uintptr_t>(cuda_event),
|
| 181 |
+
reinterpret_cast<uintptr_t>(cuda_stream.stream()));
|
| 182 |
+
}
|
| 183 |
+
setDevice(orig_device);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// May be called from any device
|
| 187 |
+
bool queryEvent(void* event) const override {
|
| 188 |
+
if (!event)
|
| 189 |
+
return true;
|
| 190 |
+
cudaEvent_t cuda_event = static_cast<cudaEvent_t>(event);
|
| 191 |
+
// Note: cudaEventQuery can be safely called from any device
|
| 192 |
+
const cudaError_t err = C10_CUDA_ERROR_HANDLED(cudaEventQuery(cuda_event));
|
| 193 |
+
if (err != cudaErrorNotReady) {
|
| 194 |
+
C10_CUDA_CHECK(err);
|
| 195 |
+
} else {
|
| 196 |
+
// ignore and clear the error if not ready
|
| 197 |
+
(void)cudaGetLastError();
|
| 198 |
+
}
|
| 199 |
+
return (err == cudaSuccess);
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// Stream-related functions
|
| 203 |
+
bool queryStream(const Stream& stream) const override {
|
| 204 |
+
CUDAStream cuda_stream{stream};
|
| 205 |
+
return cuda_stream.query();
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
void synchronizeStream(const Stream& stream) const override {
|
| 209 |
+
CUDAStream cuda_stream{stream};
|
| 210 |
+
cuda_stream.synchronize();
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
void synchronizeEvent(void* event) const override {
|
| 214 |
+
if (!event)
|
| 215 |
+
return;
|
| 216 |
+
cudaEvent_t cuda_event = static_cast<cudaEvent_t>(event);
|
| 217 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 218 |
+
if (C10_UNLIKELY(interp)) {
|
| 219 |
+
(*interp)->trace_gpu_event_synchronization(
|
| 220 |
+
c10::kCUDA, reinterpret_cast<uintptr_t>(cuda_event));
|
| 221 |
+
}
|
| 222 |
+
// Note: cudaEventSynchronize can be safely called from any device
|
| 223 |
+
C10_CUDA_CHECK(cudaEventSynchronize(cuda_event));
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
// Note: synchronizeDevice can be safely called from any device
|
| 227 |
+
void synchronizeDevice(const c10::DeviceIndex device_index) const override {
|
| 228 |
+
DeviceIndex orig_device{-1};
|
| 229 |
+
C10_CUDA_CHECK(c10::cuda::GetDevice(&orig_device));
|
| 230 |
+
C10_CUDA_CHECK(c10::cuda::SetDevice(device_index));
|
| 231 |
+
const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace();
|
| 232 |
+
if (C10_UNLIKELY(interp)) {
|
| 233 |
+
(*interp)->trace_gpu_device_synchronization(c10::kCUDA);
|
| 234 |
+
}
|
| 235 |
+
C10_CUDA_CHECK(cudaDeviceSynchronize());
|
| 236 |
+
C10_CUDA_CHECK(c10::cuda::SetDevice(orig_device));
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
void recordDataPtrOnStream(const c10::DataPtr& data_ptr, const Stream& stream)
|
| 240 |
+
const override {
|
| 241 |
+
CUDAStream cuda_stream{stream};
|
| 242 |
+
CUDACachingAllocator::recordStream(data_ptr, cuda_stream);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
double elapsedTime(void* event1, void* event2, const DeviceIndex device_index)
|
| 246 |
+
const override {
|
| 247 |
+
TORCH_CHECK(
|
| 248 |
+
event1 && event2,
|
| 249 |
+
"Both events must be recorded before calculating elapsed time.");
|
| 250 |
+
// Even though cudaEventElapsedTime can be safely called from any device, if
|
| 251 |
+
// the current device is not initialized, it will create a new cuda context,
|
| 252 |
+
// which will consume a lot of memory.
|
| 253 |
+
DeviceIndex orig_device{-1};
|
| 254 |
+
C10_CUDA_CHECK(c10::cuda::GetDevice(&orig_device));
|
| 255 |
+
C10_CUDA_CHECK(c10::cuda::SetDevice(device_index));
|
| 256 |
+
cudaEvent_t cuda_event1 = static_cast<cudaEvent_t>(event1);
|
| 257 |
+
cudaEvent_t cuda_event2 = static_cast<cudaEvent_t>(event2);
|
| 258 |
+
float time_ms = 0;
|
| 259 |
+
// raise cudaErrorNotReady if either event is recorded but not yet completed
|
| 260 |
+
C10_CUDA_CHECK(cudaEventElapsedTime(&time_ms, cuda_event1, cuda_event2));
|
| 261 |
+
C10_CUDA_CHECK(c10::cuda::SetDevice(orig_device));
|
| 262 |
+
return static_cast<double>(time_ms);
|
| 263 |
+
}
|
| 264 |
+
};
|
| 265 |
+
|
| 266 |
+
} // namespace c10::cuda::impl
|
| 267 |
+
|
| 268 |
+
#else
|
| 269 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 270 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/cuda/impl/CUDATest.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/cuda/CUDAMacros.h>
|
| 5 |
+
|
| 6 |
+
namespace c10::cuda::impl {
|
| 7 |
+
|
| 8 |
+
C10_CUDA_API int c10_cuda_test();
|
| 9 |
+
|
| 10 |
+
}
|
| 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/c10/cuda/impl/cuda_cmake_macros.h
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
// Automatically generated header file for the C10 CUDA library. Do not
|
| 5 |
+
// include this file directly. Instead, include c10/cuda/CUDAMacros.h
|
| 6 |
+
|
| 7 |
+
#define C10_CUDA_BUILD_SHARED_LIBS
|
| 8 |
+
|
| 9 |
+
#else
|
| 10 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 11 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/Export.h
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <torch/headeronly/macros/Export.h>
|
| 3 |
+
|
| 4 |
+
#else
|
| 5 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 6 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/Macros.h
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <torch/headeronly/macros/Macros.h>
|
| 3 |
+
|
| 4 |
+
#else
|
| 5 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 6 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/macros/cmake_macros.h
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// This file exists for backwards compatibility and has been moved to
|
| 3 |
+
// torch/headeronly/macros/cmake_macros.h.in. No end user library should be
|
| 4 |
+
// including this file directly anyway (cuz they should be including
|
| 5 |
+
// Macros.h instead).
|
| 6 |
+
#include <torch/headeronly/macros/cmake_macros.h>
|
| 7 |
+
|
| 8 |
+
#else
|
| 9 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 10 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/atomic.h
AD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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <metal_atomic>
|
| 4 |
+
namespace c10 {
|
| 5 |
+
namespace metal {
|
| 6 |
+
|
| 7 |
+
// Atomic operations helper
|
| 8 |
+
template <typename T>
|
| 9 |
+
struct AtomicType {};
|
| 10 |
+
template <typename T>
|
| 11 |
+
using AtomicType_t = typename AtomicType<T>::type;
|
| 12 |
+
|
| 13 |
+
template <>
|
| 14 |
+
struct AtomicType<float> {
|
| 15 |
+
using type = ::metal::atomic<float>;
|
| 16 |
+
static inline void atomic_add(device type* data, long offset, float value) {
|
| 17 |
+
::metal::atomic_fetch_add_explicit(
|
| 18 |
+
data + offset, value, ::metal::memory_order_relaxed);
|
| 19 |
+
}
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
template <>
|
| 23 |
+
struct AtomicType<int> {
|
| 24 |
+
using type = ::metal::atomic<int>;
|
| 25 |
+
static inline void atomic_add(device type* data, long offset, int value) {
|
| 26 |
+
::metal::atomic_fetch_add_explicit(
|
| 27 |
+
data + offset, value, ::metal::memory_order_relaxed);
|
| 28 |
+
}
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
// As of Metal3.2 atomic operations are not supported on half-precision floats,
|
| 32 |
+
// so they must be simulated Using atomic compare and exchange over 32-bit
|
| 33 |
+
// atomic type
|
| 34 |
+
template <typename T>
|
| 35 |
+
static inline void atomic_add_helper(
|
| 36 |
+
device ::metal::atomic<uint>* data,
|
| 37 |
+
long offset,
|
| 38 |
+
T value) {
|
| 39 |
+
constexpr auto elem_per_enum = sizeof(uint) / sizeof(T);
|
| 40 |
+
auto ptr = data + (offset / elem_per_enum);
|
| 41 |
+
auto old = ::metal::atomic_load_explicit(ptr, ::metal::memory_order_relaxed);
|
| 42 |
+
union {
|
| 43 |
+
uint i;
|
| 44 |
+
T t[elem_per_enum];
|
| 45 |
+
} val;
|
| 46 |
+
do {
|
| 47 |
+
val.i = old;
|
| 48 |
+
val.t[offset & (elem_per_enum - 1)] += value;
|
| 49 |
+
} while (!::metal::atomic_compare_exchange_weak_explicit(
|
| 50 |
+
ptr,
|
| 51 |
+
&old,
|
| 52 |
+
val.i,
|
| 53 |
+
::metal::memory_order_relaxed,
|
| 54 |
+
::metal::memory_order_relaxed));
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
template <>
|
| 58 |
+
struct AtomicType<half> {
|
| 59 |
+
using type = ::metal::atomic<uint>;
|
| 60 |
+
static inline void atomic_add(device type* data, long offset, half value) {
|
| 61 |
+
atomic_add_helper(data, offset, value);
|
| 62 |
+
}
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
template <>
|
| 66 |
+
struct AtomicType<short> {
|
| 67 |
+
using type = ::metal::atomic<uint>;
|
| 68 |
+
static inline void atomic_add(device type* data, long offset, short value) {
|
| 69 |
+
atomic_add_helper(data, offset, value);
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
template <>
|
| 74 |
+
struct AtomicType<char> {
|
| 75 |
+
using type = ::metal::atomic<uint>;
|
| 76 |
+
static inline void atomic_add(device type* data, long offset, char value) {
|
| 77 |
+
atomic_add_helper(data, offset, value);
|
| 78 |
+
}
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
template <>
|
| 82 |
+
struct AtomicType<uchar> {
|
| 83 |
+
using type = ::metal::atomic<uint>;
|
| 84 |
+
static inline void atomic_add(device type* data, long offset, char value) {
|
| 85 |
+
atomic_add_helper(data, offset, value);
|
| 86 |
+
}
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
template <>
|
| 90 |
+
struct AtomicType<bfloat> {
|
| 91 |
+
using type = ::metal::atomic<uint>;
|
| 92 |
+
static inline void atomic_add(device type* data, long offset, bfloat value) {
|
| 93 |
+
atomic_add_helper<bfloat>(data, offset, value);
|
| 94 |
+
}
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
// Metal supports atomic_store_explicit for bools, but
|
| 98 |
+
// sizeof(::metal::atomic_bool) is 4 Therefore it could not be used to
|
| 99 |
+
// atomically modify unaligned memory, so fall back to compare and exchange
|
| 100 |
+
// trick As accumulation over booleans are just or operation, do nothing if
|
| 101 |
+
// value is false
|
| 102 |
+
template <>
|
| 103 |
+
struct AtomicType<bool> {
|
| 104 |
+
using type = ::metal::atomic<uint>;
|
| 105 |
+
static inline void atomic_add(device type* data, long offset, bool value) {
|
| 106 |
+
if (!value) {
|
| 107 |
+
return;
|
| 108 |
+
}
|
| 109 |
+
auto ptr = data + (offset >> 2);
|
| 110 |
+
auto old =
|
| 111 |
+
::metal::atomic_load_explicit(ptr, ::metal::memory_order_relaxed);
|
| 112 |
+
union {
|
| 113 |
+
uint i;
|
| 114 |
+
bool t[4];
|
| 115 |
+
} val;
|
| 116 |
+
do {
|
| 117 |
+
val.i = old;
|
| 118 |
+
val.t[offset & 3] = true;
|
| 119 |
+
} while (!::metal::atomic_compare_exchange_weak_explicit(
|
| 120 |
+
ptr,
|
| 121 |
+
&old,
|
| 122 |
+
val.i,
|
| 123 |
+
::metal::memory_order_relaxed,
|
| 124 |
+
::metal::memory_order_relaxed));
|
| 125 |
+
}
|
| 126 |
+
};
|
| 127 |
+
|
| 128 |
+
// ComplexHalf atomic op
|
| 129 |
+
template <>
|
| 130 |
+
struct AtomicType<half2> {
|
| 131 |
+
using type = ::metal::atomic<uint>;
|
| 132 |
+
static inline void atomic_add(device type* data, long offset, half2 value) {
|
| 133 |
+
auto ptr = data + offset;
|
| 134 |
+
auto old =
|
| 135 |
+
::metal::atomic_load_explicit(ptr, ::metal::memory_order_relaxed);
|
| 136 |
+
while (!::metal::atomic_compare_exchange_weak_explicit(
|
| 137 |
+
ptr,
|
| 138 |
+
&old,
|
| 139 |
+
as_type<uint>(as_type<half2>(old) + value),
|
| 140 |
+
::metal::memory_order_relaxed,
|
| 141 |
+
::metal::memory_order_relaxed))
|
| 142 |
+
;
|
| 143 |
+
}
|
| 144 |
+
};
|
| 145 |
+
|
| 146 |
+
// There are no atomic 64-bit add in Metal yet, but templates below implements a
|
| 147 |
+
// consistent add I.e. if multiple threads are modify the same 64-bit value,
|
| 148 |
+
// results stored at the address will eventually be equal to its original value
|
| 149 |
+
// plus sum of all operands
|
| 150 |
+
template <>
|
| 151 |
+
struct AtomicType<long> {
|
| 152 |
+
using type = ::metal::atomic<uint>;
|
| 153 |
+
static inline void atomic_add(device type* data, long offset, long value) {
|
| 154 |
+
const auto value_bits = as_type<ulong>(value);
|
| 155 |
+
const uint low = static_cast<uint>(value_bits);
|
| 156 |
+
uint high = static_cast<uint>(value_bits >> 32);
|
| 157 |
+
auto ptr = data + (offset << 1);
|
| 158 |
+
auto old_low =
|
| 159 |
+
atomic_fetch_add_explicit(ptr, low, ::metal::memory_order_relaxed);
|
| 160 |
+
high += (old_low + low < old_low) ? 1 : 0;
|
| 161 |
+
atomic_fetch_add_explicit(ptr + 1, high, ::metal::memory_order_relaxed);
|
| 162 |
+
}
|
| 163 |
+
};
|
| 164 |
+
|
| 165 |
+
// ComplexFloat atomic op, which again is not really atomic, but eventually
|
| 166 |
+
// consistent
|
| 167 |
+
template <>
|
| 168 |
+
struct AtomicType<float2> {
|
| 169 |
+
using type = ::metal::atomic<float>;
|
| 170 |
+
static inline void atomic_add(device type* data, long offset, float2 value) {
|
| 171 |
+
auto ptr = data + (offset << 1);
|
| 172 |
+
atomic_fetch_add_explicit(ptr + 0, value.x, ::metal::memory_order_relaxed);
|
| 173 |
+
atomic_fetch_add_explicit(ptr + 1, value.y, ::metal::memory_order_relaxed);
|
| 174 |
+
}
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
+
} // namespace metal
|
| 178 |
+
} // namespace c10
|
| 179 |
+
|
| 180 |
+
#else
|
| 181 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 182 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/common.h
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
// Set of global constants that could be shareable between CPU and Metal code
|
| 4 |
+
|
| 5 |
+
#ifdef __METAL__
|
| 6 |
+
#include <metal_array>
|
| 7 |
+
#define C10_METAL_CONSTEXPR constant constexpr
|
| 8 |
+
#else
|
| 9 |
+
#include <array>
|
| 10 |
+
#define C10_METAL_CONSTEXPR constexpr
|
| 11 |
+
#endif
|
| 12 |
+
|
| 13 |
+
#define C10_METAL_ALL_TYPES_FUNCTOR(_) \
|
| 14 |
+
_(Byte, 0) \
|
| 15 |
+
_(Char, 1) \
|
| 16 |
+
_(Short, 2) \
|
| 17 |
+
_(Int, 3) \
|
| 18 |
+
_(Long, 4) \
|
| 19 |
+
_(Half, 5) \
|
| 20 |
+
_(Float, 6) \
|
| 21 |
+
_(ComplexHalf, 8) \
|
| 22 |
+
_(ComplexFloat, 9) \
|
| 23 |
+
_(Bool, 11) \
|
| 24 |
+
_(BFloat16, 15)
|
| 25 |
+
|
| 26 |
+
namespace c10 {
|
| 27 |
+
namespace metal {
|
| 28 |
+
C10_METAL_CONSTEXPR unsigned max_ndim = 16;
|
| 29 |
+
C10_METAL_CONSTEXPR unsigned simdgroup_size = 32;
|
| 30 |
+
|
| 31 |
+
#ifdef __METAL__
|
| 32 |
+
template <typename T, unsigned N>
|
| 33 |
+
using array = ::metal::array<T, N>;
|
| 34 |
+
#else
|
| 35 |
+
template <typename T, unsigned N>
|
| 36 |
+
using array = std::array<T, N>;
|
| 37 |
+
#endif
|
| 38 |
+
|
| 39 |
+
enum class ScalarType {
|
| 40 |
+
#define _DEFINE_ENUM_VAL_(_v, _n) _v = _n,
|
| 41 |
+
C10_METAL_ALL_TYPES_FUNCTOR(_DEFINE_ENUM_VAL_)
|
| 42 |
+
#undef _DEFINE_ENUM_VAL_
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
} // namespace metal
|
| 46 |
+
} // namespace c10
|
| 47 |
+
|
| 48 |
+
#else
|
| 49 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 50 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/error.h
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/metal/common.h>
|
| 4 |
+
|
| 5 |
+
namespace c10 {
|
| 6 |
+
namespace metal {
|
| 7 |
+
C10_METAL_CONSTEXPR unsigned error_message_count = 30;
|
| 8 |
+
struct ErrorMessage {
|
| 9 |
+
char file[128];
|
| 10 |
+
char func[128];
|
| 11 |
+
char message[250];
|
| 12 |
+
unsigned int line;
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
struct ErrorMessages {
|
| 16 |
+
#ifdef __METAL__
|
| 17 |
+
::metal::atomic<unsigned int> count;
|
| 18 |
+
#else
|
| 19 |
+
unsigned int count;
|
| 20 |
+
#endif
|
| 21 |
+
ErrorMessage msg[error_message_count];
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
#ifdef __METAL__
|
| 25 |
+
namespace detail {
|
| 26 |
+
static uint strncpy(device char* dst, constant const char* src, unsigned len) {
|
| 27 |
+
uint i = 0;
|
| 28 |
+
while (src[i] != 0 && i < len - 1) {
|
| 29 |
+
dst[i] = src[i];
|
| 30 |
+
i++;
|
| 31 |
+
}
|
| 32 |
+
dst[i] = 0;
|
| 33 |
+
return i;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
inline uint print_arg(
|
| 37 |
+
device char* ptr,
|
| 38 |
+
unsigned len,
|
| 39 |
+
constant const char* arg) {
|
| 40 |
+
return strncpy(ptr, arg, len);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Returns number length as string in base10
|
| 44 |
+
static inline uint base10_length(long num) {
|
| 45 |
+
uint rc = 1;
|
| 46 |
+
if (num < 0) {
|
| 47 |
+
num = -num;
|
| 48 |
+
rc += 1;
|
| 49 |
+
}
|
| 50 |
+
while (num > 9) {
|
| 51 |
+
num /= 10;
|
| 52 |
+
rc++;
|
| 53 |
+
}
|
| 54 |
+
return rc;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Converts signed integer to string
|
| 58 |
+
inline uint print_arg(device char* ptr, unsigned len, long arg) {
|
| 59 |
+
const auto arg_len = base10_length(arg);
|
| 60 |
+
if (arg_len >= len)
|
| 61 |
+
return 0;
|
| 62 |
+
if (arg < 0) {
|
| 63 |
+
ptr[0] = '-';
|
| 64 |
+
arg = -arg;
|
| 65 |
+
}
|
| 66 |
+
uint idx = 1;
|
| 67 |
+
do {
|
| 68 |
+
ptr[arg_len - idx] = '0' + (arg % 10);
|
| 69 |
+
arg /= 10;
|
| 70 |
+
idx++;
|
| 71 |
+
} while (arg > 0);
|
| 72 |
+
ptr[arg_len] = 0;
|
| 73 |
+
return arg_len;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
template <typename T>
|
| 77 |
+
inline void print_args(device char* ptr, unsigned len, T arg) {
|
| 78 |
+
print_arg(ptr, len, arg);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <typename T, typename... Args>
|
| 82 |
+
inline void print_args(device char* ptr, unsigned len, T arg, Args... args) {
|
| 83 |
+
const auto rc = print_arg(ptr, len, arg);
|
| 84 |
+
print_args(ptr + rc, len - rc, args...);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
} // namespace detail
|
| 88 |
+
|
| 89 |
+
template <typename... Args>
|
| 90 |
+
static void report_error(
|
| 91 |
+
device ErrorMessages* msgs,
|
| 92 |
+
constant const char* file,
|
| 93 |
+
int line,
|
| 94 |
+
constant const char* func,
|
| 95 |
+
Args... args) {
|
| 96 |
+
const auto idx =
|
| 97 |
+
atomic_fetch_add_explicit(&msgs->count, 1, ::metal::memory_order_relaxed);
|
| 98 |
+
if (idx >= error_message_count) {
|
| 99 |
+
return;
|
| 100 |
+
}
|
| 101 |
+
device auto* msg = &msgs->msg[idx];
|
| 102 |
+
detail::strncpy(msg->file, file, 128);
|
| 103 |
+
detail::strncpy(msg->func, func, 128);
|
| 104 |
+
detail::print_args(msg->message, 250, args...);
|
| 105 |
+
msg->line = line;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
#define TORCH_REPORT_ERROR(buf, ...) \
|
| 109 |
+
::c10::metal::report_error(buf, __FILE__, __LINE__, __func__, __VA_ARGS__)
|
| 110 |
+
#endif
|
| 111 |
+
} // namespace metal
|
| 112 |
+
} // namespace c10
|
| 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/c10/metal/expm1f.h
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Copy-and-pasted from:
|
| 3 |
+
// https://github.com/ml-explore/mlx/blob/99c33d011d63174f50cea37c3eede002958be6d3/mlx/backend/metal/kernels/expm1f.h
|
| 4 |
+
|
| 5 |
+
#pragma once
|
| 6 |
+
|
| 7 |
+
#include <metal_math>
|
| 8 |
+
|
| 9 |
+
// Original license copied below:
|
| 10 |
+
// Copyright (c) 2015-2023 Norbert Juffa
|
| 11 |
+
// All rights reserved.
|
| 12 |
+
//
|
| 13 |
+
// Redistribution and use in source and binary forms, with or without
|
| 14 |
+
// modification, are permitted provided that the following conditions
|
| 15 |
+
// are met:
|
| 16 |
+
//
|
| 17 |
+
// 1. Redistributions of source code must retain the above copyright
|
| 18 |
+
// notice, this list of conditions and the following disclaimer.
|
| 19 |
+
//
|
| 20 |
+
// 2. Redistributions in binary form must reproduce the above copyright
|
| 21 |
+
// notice, this list of conditions and the following disclaimer in the
|
| 22 |
+
// documentation and/or other materials provided with the distribution.
|
| 23 |
+
//
|
| 24 |
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 25 |
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 26 |
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 27 |
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 28 |
+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 29 |
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 30 |
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 31 |
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 32 |
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 33 |
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 34 |
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 35 |
+
|
| 36 |
+
namespace c10 {
|
| 37 |
+
namespace metal {
|
| 38 |
+
|
| 39 |
+
/* Compute exponential base e minus 1. Maximum ulp error = 0.997458
|
| 40 |
+
|
| 41 |
+
i = rint(a/log(2)), f = a-i*log(2). Then expm1(a) = 2**i * (expm1(f)+1) - 1.
|
| 42 |
+
Compute r = expm1(f). Then expm1(a)= 2 * (0.5 * 2**i * r + 0.5 * 2**i - 0.5).
|
| 43 |
+
With t = 0.5*2**i, expm1(a) = 2*(r * t + t-0.5). However, for best accuracy,
|
| 44 |
+
when i == 1, expm1(a)= 2*(r + 0.5), and when i == 0, expm1(a) = r.
|
| 45 |
+
|
| 46 |
+
NOTE: Scale factor b is only applied if i < 0 or i > 1 (should be power of 2)
|
| 47 |
+
*/
|
| 48 |
+
inline float expm1f_scaled_unchecked(float a, float b) {
|
| 49 |
+
float f, j, r, s, t, u, v, x, y;
|
| 50 |
+
int i;
|
| 51 |
+
|
| 52 |
+
// exp(a) = 2**i * exp(f); i = rintf (a / log(2))
|
| 53 |
+
j = ::metal::fma(1.442695f, a, 12582912.f); // 0x1.715476p0, 0x1.8p23
|
| 54 |
+
j = j - 12582912.0f; // 0x1.8p23
|
| 55 |
+
i = (int)j;
|
| 56 |
+
f = ::metal::fma(j, -6.93145752e-1f, a);
|
| 57 |
+
|
| 58 |
+
// approximate r = exp(f)-1 on interval [-log(2)/2, +log(2)/2]
|
| 59 |
+
s = f * f;
|
| 60 |
+
if (a == 0.0f)
|
| 61 |
+
s = a; // ensure -0 is passed through
|
| 62 |
+
// err = 0.997458 ulp1 = 11081805
|
| 63 |
+
r = 1.97350979e-4f; // 0x1.9de000p-13
|
| 64 |
+
r = ::metal::fma(r, f, 1.39309070e-3f); // 0x1.6d30bcp-10
|
| 65 |
+
r = ::metal::fma(r, f, 8.33343994e-3f); // 0x1.1111f6p-7
|
| 66 |
+
r = ::metal::fma(r, f, 4.16668020e-2f); // 0x1.55559ep-5
|
| 67 |
+
r = ::metal::fma(r, f, 1.66666716e-1f); // 0x1.55555cp-3
|
| 68 |
+
r = ::metal::fma(r, f, 4.99999970e-1f); // 0x1.fffffep-2
|
| 69 |
+
u = (j == 1) ? (f + 0.5f) : f;
|
| 70 |
+
v = ::metal::fma(r, s, u);
|
| 71 |
+
s = 0.5f * b;
|
| 72 |
+
t = ::metal::ldexp(s, i);
|
| 73 |
+
y = t - s;
|
| 74 |
+
x = (t - y) - s; // double-float canonicalization of difference
|
| 75 |
+
r = ::metal::fma(v, t, x) + y;
|
| 76 |
+
r = r + r;
|
| 77 |
+
if (j == 0)
|
| 78 |
+
r = v;
|
| 79 |
+
if (j == 1)
|
| 80 |
+
r = v + v;
|
| 81 |
+
return r;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
/* Compute exponential base e minus 1. max ulp err = 0.99746 */
|
| 85 |
+
inline float expm1f(float a) {
|
| 86 |
+
float r;
|
| 87 |
+
|
| 88 |
+
r = expm1f_scaled_unchecked(a, 1.0f);
|
| 89 |
+
/* handle severe overflow and underflow */
|
| 90 |
+
if (::metal::abs(a - 1.0f) > 88.0f) {
|
| 91 |
+
r = ::metal::pow(2, a);
|
| 92 |
+
r = ::metal::fma(r, r, -1.0f);
|
| 93 |
+
}
|
| 94 |
+
return r;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
} // namespace metal
|
| 98 |
+
} // namespace c10
|
| 99 |
+
|
| 100 |
+
#else
|
| 101 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 102 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/igamma.h
ADDED
|
@@ -0,0 +1,749 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/metal/utils.h>
|
| 5 |
+
#include <metal_math>
|
| 6 |
+
#include <metal_stdlib>
|
| 7 |
+
|
| 8 |
+
using namespace c10::metal;
|
| 9 |
+
using namespace metal;
|
| 10 |
+
|
| 11 |
+
namespace c10 {
|
| 12 |
+
namespace metal {
|
| 13 |
+
|
| 14 |
+
template <typename T>
|
| 15 |
+
inline float log_gamma(const T);
|
| 16 |
+
|
| 17 |
+
inline float expm1f(float a);
|
| 18 |
+
|
| 19 |
+
template <typename T>
|
| 20 |
+
float erfc(T x);
|
| 21 |
+
|
| 22 |
+
} // namespace metal
|
| 23 |
+
} // namespace c10
|
| 24 |
+
|
| 25 |
+
namespace {
|
| 26 |
+
|
| 27 |
+
template <typename T>
|
| 28 |
+
inline float lgamma(const T a) {
|
| 29 |
+
return log_gamma(a);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
inline float expm1(float a) {
|
| 33 |
+
return expm1f(a);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// NOTE: The following code was ported directly from the CUDA implementation in
|
| 37 |
+
// `aten/src/ATen/native/cuda/IGammaKernel.cu`
|
| 38 |
+
|
| 39 |
+
/*
|
| 40 |
+
* This implementation of the regularized incomplete gamma functions and
|
| 41 |
+
* their helper functions are derived from the implementation of SciPy's
|
| 42 |
+
* gammainc, Cephes's igam and igamc, and Boost's Lanczos approximations.
|
| 43 |
+
* See NOTICE for the licenses.
|
| 44 |
+
*/
|
| 45 |
+
// regularized lower & upper incomplete gamma
|
| 46 |
+
template <typename scalar_t>
|
| 47 |
+
scalar_t ratevl(
|
| 48 |
+
scalar_t x,
|
| 49 |
+
const scalar_t num[],
|
| 50 |
+
int64_t M,
|
| 51 |
+
const scalar_t denom[],
|
| 52 |
+
int64_t N) {
|
| 53 |
+
// evaluating rational function, i.e., the ratio of two polynomials
|
| 54 |
+
// the coefficients for numerator are given by `num` while coeffs for
|
| 55 |
+
// denumerator are given by `denom`
|
| 56 |
+
|
| 57 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 58 |
+
int64_t i, dir;
|
| 59 |
+
accscalar_t y, num_ans, denom_ans;
|
| 60 |
+
accscalar_t absx = ::fabs(x);
|
| 61 |
+
thread const accscalar_t* p;
|
| 62 |
+
|
| 63 |
+
if (absx > 1) {
|
| 64 |
+
/* Evaluate as a polynomial in 1/x. */
|
| 65 |
+
dir = -1;
|
| 66 |
+
p = num + M;
|
| 67 |
+
y = 1 / x;
|
| 68 |
+
} else {
|
| 69 |
+
dir = 1;
|
| 70 |
+
p = num;
|
| 71 |
+
y = x;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
/* Evaluate the numerator */
|
| 75 |
+
num_ans = *p;
|
| 76 |
+
p += dir;
|
| 77 |
+
for (i = 1; i <= M; i++) {
|
| 78 |
+
num_ans = num_ans * y + *p;
|
| 79 |
+
p += dir;
|
| 80 |
+
}
|
| 81 |
+
/* Evaluate the denominator */
|
| 82 |
+
if (absx > 1) {
|
| 83 |
+
p = denom + N;
|
| 84 |
+
} else {
|
| 85 |
+
p = denom;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
denom_ans = *p;
|
| 89 |
+
p += dir;
|
| 90 |
+
for (i = 1; i <= N; i++) {
|
| 91 |
+
denom_ans = denom_ans * y + *p;
|
| 92 |
+
p += dir;
|
| 93 |
+
}
|
| 94 |
+
if (absx > 1) {
|
| 95 |
+
i = N - M;
|
| 96 |
+
return ::pow(x, static_cast<accscalar_t>(i)) * num_ans / denom_ans;
|
| 97 |
+
} else {
|
| 98 |
+
return num_ans / denom_ans;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
template <typename scalar_t>
|
| 103 |
+
scalar_t lanczos_sum_expg_scaled(scalar_t x) {
|
| 104 |
+
// lanczos approximation
|
| 105 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 106 |
+
|
| 107 |
+
const accscalar_t lanczos_sum_expg_scaled_num[13] = {
|
| 108 |
+
0.006061842346248906525783753964555936883222,
|
| 109 |
+
0.5098416655656676188125178644804694509993,
|
| 110 |
+
19.51992788247617482847860966235652136208,
|
| 111 |
+
449.9445569063168119446858607650988409623,
|
| 112 |
+
6955.999602515376140356310115515198987526,
|
| 113 |
+
75999.29304014542649875303443598909137092,
|
| 114 |
+
601859.6171681098786670226533699352302507,
|
| 115 |
+
3481712.15498064590882071018964774556468,
|
| 116 |
+
14605578.08768506808414169982791359218571,
|
| 117 |
+
43338889.32467613834773723740590533316085,
|
| 118 |
+
86363131.28813859145546927288977868422342,
|
| 119 |
+
103794043.1163445451906271053616070238554,
|
| 120 |
+
56906521.91347156388090791033559122686859};
|
| 121 |
+
const accscalar_t lanczos_sum_expg_scaled_denom[13] = {
|
| 122 |
+
1.,
|
| 123 |
+
66.,
|
| 124 |
+
1925.,
|
| 125 |
+
32670.,
|
| 126 |
+
357423.,
|
| 127 |
+
2637558.,
|
| 128 |
+
13339535.,
|
| 129 |
+
45995730.,
|
| 130 |
+
105258076.,
|
| 131 |
+
150917976.,
|
| 132 |
+
120543840.,
|
| 133 |
+
39916800.,
|
| 134 |
+
0};
|
| 135 |
+
return ratevl(
|
| 136 |
+
static_cast<accscalar_t>(x),
|
| 137 |
+
lanczos_sum_expg_scaled_num,
|
| 138 |
+
sizeof(lanczos_sum_expg_scaled_num) /
|
| 139 |
+
sizeof(lanczos_sum_expg_scaled_num[0]) -
|
| 140 |
+
1,
|
| 141 |
+
lanczos_sum_expg_scaled_denom,
|
| 142 |
+
sizeof(lanczos_sum_expg_scaled_denom) /
|
| 143 |
+
sizeof(lanczos_sum_expg_scaled_denom[0]) -
|
| 144 |
+
1);
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
template <typename scalar_t>
|
| 148 |
+
scalar_t _igam_helper_fac(scalar_t a, scalar_t x) {
|
| 149 |
+
// compute x^a * exp(-a) / gamma(a)
|
| 150 |
+
// corrected from (15) and (16) in [igam2] by replacing exp(x - a) with
|
| 151 |
+
// exp(a - x).
|
| 152 |
+
|
| 153 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 154 |
+
accscalar_t ax, fac, res, num, numfac;
|
| 155 |
+
const accscalar_t MAXLOG = 88.72283905206835;
|
| 156 |
+
const accscalar_t EXP1 = 2.718281828459045;
|
| 157 |
+
const accscalar_t lanczos_g = 6.024680040776729583740234375;
|
| 158 |
+
|
| 159 |
+
if (::fabs(a - x) > 0.4 * ::fabs(a)) {
|
| 160 |
+
ax = a * ::log(x) - x - ::lgamma(a);
|
| 161 |
+
if (ax < -MAXLOG) {
|
| 162 |
+
return 0.0;
|
| 163 |
+
}
|
| 164 |
+
return ::exp(ax);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
fac = a + lanczos_g - 0.5;
|
| 168 |
+
res = ::sqrt(fac / EXP1) / lanczos_sum_expg_scaled(a);
|
| 169 |
+
|
| 170 |
+
if ((a < 200) && (x < 200)) {
|
| 171 |
+
res *= ::exp(a - x) * ::pow(x / fac, a);
|
| 172 |
+
} else {
|
| 173 |
+
num = x - a - lanczos_g + 0.5;
|
| 174 |
+
numfac = num / fac;
|
| 175 |
+
res *= ::exp(a * (::log1p(numfac) - numfac) + x * (0.5 - lanczos_g) / fac);
|
| 176 |
+
}
|
| 177 |
+
return res;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
template <typename scalar_t>
|
| 181 |
+
scalar_t _igam_helper_series(scalar_t a, scalar_t x) {
|
| 182 |
+
// Compute igam using DLMF 8.11.4. [igam1]
|
| 183 |
+
|
| 184 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 185 |
+
const accscalar_t MACHEP = 5.9604644775390625E-8;
|
| 186 |
+
const int MAXITER = 2000;
|
| 187 |
+
|
| 188 |
+
int i;
|
| 189 |
+
accscalar_t ans, ax, c, r;
|
| 190 |
+
|
| 191 |
+
ax = _igam_helper_fac(a, x);
|
| 192 |
+
if (ax == 0.0) {
|
| 193 |
+
return 0.0;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* power series */
|
| 197 |
+
r = a;
|
| 198 |
+
c = 1.0;
|
| 199 |
+
ans = 1.0;
|
| 200 |
+
|
| 201 |
+
for (i = 0; i < MAXITER; i++) {
|
| 202 |
+
r += 1.0;
|
| 203 |
+
c *= x / r;
|
| 204 |
+
ans += c;
|
| 205 |
+
if (c <= MACHEP * ans) {
|
| 206 |
+
break;
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
return (ans * ax / a);
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
template <typename scalar_t>
|
| 213 |
+
scalar_t _igamc_helper_series(scalar_t a, scalar_t x) {
|
| 214 |
+
// Compute igamc using DLMF 8.7.3 [igam1]. This is related to the series in
|
| 215 |
+
// _igam_helper_series but extra care is taken to avoid cancellation.
|
| 216 |
+
|
| 217 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 218 |
+
int n;
|
| 219 |
+
accscalar_t fac = 1;
|
| 220 |
+
accscalar_t sum = 0;
|
| 221 |
+
accscalar_t term, logx;
|
| 222 |
+
const int MAXITER = 2000;
|
| 223 |
+
const accscalar_t MACHEP = 5.9604644775390625E-8;
|
| 224 |
+
|
| 225 |
+
for (n = 1; n < MAXITER; n++) {
|
| 226 |
+
fac *= -x / n;
|
| 227 |
+
term = fac / (a + n);
|
| 228 |
+
sum += term;
|
| 229 |
+
if (::fabs(term) <= MACHEP * ::fabs(sum)) {
|
| 230 |
+
break;
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
logx = ::log(x);
|
| 235 |
+
term = -::expm1(a * logx - ::lgamma(1 + a));
|
| 236 |
+
return term - ::exp(a * logx - ::lgamma(a)) * sum;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
template <typename scalar_t>
|
| 240 |
+
scalar_t _igam_helper_asymptotic_series(scalar_t a, scalar_t x, bool igam) {
|
| 241 |
+
// Compute igam/igamc using DLMF 8.12.3/8.12.4 [igam1]
|
| 242 |
+
|
| 243 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 244 |
+
const accscalar_t d[25][25] = {
|
| 245 |
+
{-3.3333333333333333e-1, 8.3333333333333333e-2,
|
| 246 |
+
-1.4814814814814815e-2, 1.1574074074074074e-3,
|
| 247 |
+
3.527336860670194e-4, -1.7875514403292181e-4,
|
| 248 |
+
3.9192631785224378e-5, -2.1854485106799922e-6,
|
| 249 |
+
-1.85406221071516e-6, 8.296711340953086e-7,
|
| 250 |
+
-1.7665952736826079e-7, 6.7078535434014986e-9,
|
| 251 |
+
1.0261809784240308e-8, -4.3820360184533532e-9,
|
| 252 |
+
9.1476995822367902e-10, -2.551419399494625e-11,
|
| 253 |
+
-5.8307721325504251e-11, 2.4361948020667416e-11,
|
| 254 |
+
-5.0276692801141756e-12, 1.1004392031956135e-13,
|
| 255 |
+
3.3717632624009854e-13, -1.3923887224181621e-13,
|
| 256 |
+
2.8534893807047443e-14, -5.1391118342425726e-16,
|
| 257 |
+
-1.9752288294349443e-15},
|
| 258 |
+
{-1.8518518518518519e-3, -3.4722222222222222e-3, 2.6455026455026455e-3,
|
| 259 |
+
-9.9022633744855967e-4, 2.0576131687242798e-4, -4.0187757201646091e-7,
|
| 260 |
+
-1.8098550334489978e-5, 7.6491609160811101e-6, -1.6120900894563446e-6,
|
| 261 |
+
4.6471278028074343e-9, 1.378633446915721e-7, -5.752545603517705e-8,
|
| 262 |
+
1.1951628599778147e-8, -1.7543241719747648e-11, -1.0091543710600413e-9,
|
| 263 |
+
4.1627929918425826e-10, -8.5639070264929806e-11, 6.0672151016047586e-14,
|
| 264 |
+
7.1624989648114854e-12, -2.9331866437714371e-12, 5.9966963656836887e-13,
|
| 265 |
+
-2.1671786527323314e-16, -4.9783399723692616e-14, 2.0291628823713425e-14,
|
| 266 |
+
-4.13125571381061e-15},
|
| 267 |
+
{4.1335978835978836e-3, -2.6813271604938272e-3, 7.7160493827160494e-4,
|
| 268 |
+
2.0093878600823045e-6, -1.0736653226365161e-4, 5.2923448829120125e-5,
|
| 269 |
+
-1.2760635188618728e-5, 3.4235787340961381e-8, 1.3721957309062933e-6,
|
| 270 |
+
-6.298992138380055e-7, 1.4280614206064242e-7, -2.0477098421990866e-10,
|
| 271 |
+
-1.4092529910867521e-8, 6.228974084922022e-9, -1.3670488396617113e-9,
|
| 272 |
+
9.4283561590146782e-13, 1.2872252400089318e-10, -5.5645956134363321e-11,
|
| 273 |
+
1.1975935546366981e-11, -4.1689782251838635e-15, -1.0940640427884594e-12,
|
| 274 |
+
4.6622399463901357e-13, -9.905105763906906e-14, 1.8931876768373515e-17,
|
| 275 |
+
8.8592218725911273e-15},
|
| 276 |
+
{6.4943415637860082e-4, 2.2947209362139918e-4, -4.6918949439525571e-4,
|
| 277 |
+
2.6772063206283885e-4, -7.5618016718839764e-5, -2.3965051138672967e-7,
|
| 278 |
+
1.1082654115347302e-5, -5.6749528269915966e-6, 1.4230900732435884e-6,
|
| 279 |
+
-2.7861080291528142e-11, -1.6958404091930277e-7, 8.0994649053880824e-8,
|
| 280 |
+
-1.9111168485973654e-8, 2.3928620439808118e-12, 2.0620131815488798e-9,
|
| 281 |
+
-9.4604966618551322e-10, 2.1541049775774908e-10, -1.388823336813903e-14,
|
| 282 |
+
-2.1894761681963939e-11, 9.7909989511716851e-12, -2.1782191880180962e-12,
|
| 283 |
+
6.2088195734079014e-17, 2.126978363279737e-13, -9.3446887915174333e-14,
|
| 284 |
+
2.0453671226782849e-14},
|
| 285 |
+
{-8.618882909167117e-4, 7.8403922172006663e-4,
|
| 286 |
+
-2.9907248030319018e-4, -1.4638452578843418e-6,
|
| 287 |
+
6.6414982154651222e-5, -3.9683650471794347e-5,
|
| 288 |
+
1.1375726970678419e-5, 2.5074972262375328e-10,
|
| 289 |
+
-1.6954149536558306e-6, 8.9075075322053097e-7,
|
| 290 |
+
-2.2929348340008049e-7, 2.956794137544049e-11,
|
| 291 |
+
2.8865829742708784e-8, -1.4189739437803219e-8,
|
| 292 |
+
3.4463580499464897e-9, -2.3024517174528067e-13,
|
| 293 |
+
-3.9409233028046405e-10, 1.8602338968504502e-10,
|
| 294 |
+
-4.356323005056618e-11, 1.2786001016296231e-15,
|
| 295 |
+
4.6792750266579195e-12, -2.1492464706134829e-12,
|
| 296 |
+
4.9088156148096522e-13, -6.3385914848915603e-18,
|
| 297 |
+
-5.0453320690800944e-14},
|
| 298 |
+
{-3.3679855336635815e-4, -6.9728137583658578e-5, 2.7727532449593921e-4,
|
| 299 |
+
-1.9932570516188848e-4, 6.7977804779372078e-5, 1.419062920643967e-7,
|
| 300 |
+
-1.3594048189768693e-5, 8.0184702563342015e-6, -2.2914811765080952e-6,
|
| 301 |
+
-3.252473551298454e-10, 3.4652846491085265e-7, -1.8447187191171343e-7,
|
| 302 |
+
4.8240967037894181e-8, -1.7989466721743515e-14, -6.3061945000135234e-9,
|
| 303 |
+
3.1624176287745679e-9, -7.8409242536974293e-10, 5.1926791652540407e-15,
|
| 304 |
+
9.3589442423067836e-11, -4.5134262161632782e-11, 1.0799129993116827e-11,
|
| 305 |
+
-3.661886712685252e-17, -1.210902069055155e-12, 5.6807435849905643e-13,
|
| 306 |
+
-1.3249659916340829e-13},
|
| 307 |
+
{5.3130793646399222e-4, -5.9216643735369388e-4, 2.7087820967180448e-4,
|
| 308 |
+
7.9023532326603279e-7, -8.1539693675619688e-5, 5.6116827531062497e-5,
|
| 309 |
+
-1.8329116582843376e-5, -3.0796134506033048e-9, 3.4651553688036091e-6,
|
| 310 |
+
-2.0291327396058604e-6, 5.7887928631490037e-7, 2.338630673826657e-13,
|
| 311 |
+
-8.8286007463304835e-8, 4.7435958880408128e-8, -1.2545415020710382e-8,
|
| 312 |
+
8.6496488580102925e-14, 1.6846058979264063e-9, -8.5754928235775947e-10,
|
| 313 |
+
2.1598224929232125e-10, -7.6132305204761539e-16, -2.6639822008536144e-11,
|
| 314 |
+
1.3065700536611057e-11, -3.1799163902367977e-12, 4.7109761213674315e-18,
|
| 315 |
+
3.6902800842763467e-13},
|
| 316 |
+
{3.4436760689237767e-4, 5.1717909082605922e-5,
|
| 317 |
+
-3.3493161081142236e-4, 2.812695154763237e-4,
|
| 318 |
+
-1.0976582244684731e-4, -1.2741009095484485e-7,
|
| 319 |
+
2.7744451511563644e-5, -1.8263488805711333e-5,
|
| 320 |
+
5.7876949497350524e-6, 4.9387589339362704e-10,
|
| 321 |
+
-1.0595367014026043e-6, 6.1667143761104075e-7,
|
| 322 |
+
-1.7562973359060462e-7, -1.2974473287015439e-12,
|
| 323 |
+
2.695423606288966e-8, -1.4578352908731271e-8,
|
| 324 |
+
3.887645959386175e-9, -3.8810022510194121e-17,
|
| 325 |
+
-5.3279941738772867e-10, 2.7437977643314845e-10,
|
| 326 |
+
-6.9957960920705679e-11, 2.5899863874868481e-17,
|
| 327 |
+
8.8566890996696381e-12, -4.403168815871311e-12,
|
| 328 |
+
1.0865561947091654e-12},
|
| 329 |
+
{-6.5262391859530942e-4, 8.3949872067208728e-4, -4.3829709854172101e-4,
|
| 330 |
+
-6.969091458420552e-7, 1.6644846642067548e-4, -1.2783517679769219e-4,
|
| 331 |
+
4.6299532636913043e-5, 4.5579098679227077e-9, -1.0595271125805195e-5,
|
| 332 |
+
6.7833429048651666e-6, -2.1075476666258804e-6, -1.7213731432817145e-11,
|
| 333 |
+
3.7735877416110979e-7, -2.1867506700122867e-7, 6.2202288040189269e-8,
|
| 334 |
+
6.5977038267330006e-16, -9.5903864974256858e-9, 5.2132144922808078e-9,
|
| 335 |
+
-1.3991589583935709e-9, 5.382058999060575e-16, 1.9484714275467745e-10,
|
| 336 |
+
-1.0127287556389682e-10, 2.6077347197254926e-11, -5.0904186999932993e-18,
|
| 337 |
+
-3.3721464474854592e-12},
|
| 338 |
+
{-5.9676129019274625e-4, -7.2048954160200106e-5,
|
| 339 |
+
6.7823088376673284e-4, -6.4014752602627585e-4,
|
| 340 |
+
2.7750107634328704e-4, 1.8197008380465151e-7,
|
| 341 |
+
-8.4795071170685032e-5, 6.105192082501531e-5,
|
| 342 |
+
-2.1073920183404862e-5, -8.8585890141255994e-10,
|
| 343 |
+
4.5284535953805377e-6, -2.8427815022504408e-6,
|
| 344 |
+
8.7082341778646412e-7, 3.6886101871706965e-12,
|
| 345 |
+
-1.5344695190702061e-7, 8.862466778790695e-8,
|
| 346 |
+
-2.5184812301826817e-8, -1.0225912098215092e-14,
|
| 347 |
+
3.8969470758154777e-9, -2.1267304792235635e-9,
|
| 348 |
+
5.7370135528051385e-10, -1.887749850169741e-19,
|
| 349 |
+
-8.0931538694657866e-11, 4.2382723283449199e-11,
|
| 350 |
+
-1.1002224534207726e-11},
|
| 351 |
+
{1.3324454494800656e-3, -1.9144384985654775e-3, 1.1089369134596637e-3,
|
| 352 |
+
9.932404122642299e-7, -5.0874501293093199e-4, 4.2735056665392884e-4,
|
| 353 |
+
-1.6858853767910799e-4, -8.1301893922784998e-9, 4.5284402370562147e-5,
|
| 354 |
+
-3.127053674781734e-5, 1.044986828530338e-5, 4.8435226265680926e-11,
|
| 355 |
+
-2.1482565873456258e-6, 1.329369701097492e-6, -4.0295693092101029e-7,
|
| 356 |
+
-1.7567877666323291e-13, 7.0145043163668257e-8, -4.040787734999483e-8,
|
| 357 |
+
1.1474026743371963e-8, 3.9642746853563325e-18, -1.7804938269892714e-9,
|
| 358 |
+
9.7480262548731646e-10, -2.6405338676507616e-10, 5.794875163403742e-18,
|
| 359 |
+
3.7647749553543836e-11},
|
| 360 |
+
{1.579727660730835e-3, 1.6251626278391582e-4, -2.0633421035543276e-3,
|
| 361 |
+
2.1389686185689098e-3, -1.0108559391263003e-3, -3.9912705529919201e-7,
|
| 362 |
+
3.6235025084764691e-4, -2.8143901463712154e-4, 1.0449513336495887e-4,
|
| 363 |
+
2.1211418491830297e-9, -2.5779417251947842e-5, 1.7281818956040463e-5,
|
| 364 |
+
-5.6413773872904282e-6, -1.1024320105776174e-11, 1.1223224418895175e-6,
|
| 365 |
+
-6.8693396379526735e-7, 2.0653236975414887e-7, 4.6714772409838506e-14,
|
| 366 |
+
-3.5609886164949055e-8, 2.0470855345905963e-8, -5.8091738633283358e-9,
|
| 367 |
+
-1.332821287582869e-16, 9.0354604391335133e-10, -4.9598782517330834e-10,
|
| 368 |
+
1.3481607129399749e-10},
|
| 369 |
+
{-4.0725121195140166e-3, 6.4033628338080698e-3, -4.0410161081676618e-3,
|
| 370 |
+
-2.183732802866233e-6, 2.1740441801254639e-3, -1.9700440518418892e-3,
|
| 371 |
+
8.3595469747962458e-4, 1.9445447567109655e-8, -2.5779387120421696e-4,
|
| 372 |
+
1.9009987368139304e-4, -6.7696499937438965e-5, -1.4440629666426572e-10,
|
| 373 |
+
1.5712512518742269e-5, -1.0304008744776893e-5, 3.304517767401387e-6,
|
| 374 |
+
7.9829760242325709e-13, -6.4097794149313004e-7, 3.8894624761300056e-7,
|
| 375 |
+
-1.1618347644948869e-7, -2.816808630596451e-15, 1.9878012911297093e-8,
|
| 376 |
+
-1.1407719956357511e-8, 3.2355857064185555e-9, 4.1759468293455945e-20,
|
| 377 |
+
-5.0423112718105824e-10},
|
| 378 |
+
{-5.9475779383993003e-3, -5.4016476789260452e-4, 8.7910413550767898e-3,
|
| 379 |
+
-9.8576315587856125e-3, 5.0134695031021538e-3, 1.2807521786221875e-6,
|
| 380 |
+
-2.0626019342754683e-3, 1.7109128573523058e-3, -6.7695312714133799e-4,
|
| 381 |
+
-6.9011545676562133e-9, 1.8855128143995902e-4, -1.3395215663491969e-4,
|
| 382 |
+
4.6263183033528039e-5, 4.0034230613321351e-11, -1.0255652921494033e-5,
|
| 383 |
+
6.612086372797651e-6, -2.0913022027253008e-6, -2.0951775649603837e-13,
|
| 384 |
+
3.9756029041993247e-7, -2.3956211978815887e-7, 7.1182883382145864e-8,
|
| 385 |
+
8.925574873053455e-16, -1.2101547235064676e-8, 6.9350618248334386e-9,
|
| 386 |
+
-1.9661464453856102e-9},
|
| 387 |
+
{1.7402027787522711e-2, -2.9527880945699121e-2, 2.0045875571402799e-2,
|
| 388 |
+
7.0289515966903407e-6, -1.2375421071343148e-2, 1.1976293444235254e-2,
|
| 389 |
+
-5.4156038466518525e-3, -6.3290893396418616e-8, 1.8855118129005065e-3,
|
| 390 |
+
-1.473473274825001e-3, 5.5515810097708387e-4, 5.2406834412550662e-10,
|
| 391 |
+
-1.4357913535784836e-4, 9.9181293224943297e-5, -3.3460834749478311e-5,
|
| 392 |
+
-3.5755837291098993e-12, 7.1560851960630076e-6, -4.5516802628155526e-6,
|
| 393 |
+
1.4236576649271475e-6, 1.8803149082089664e-14, -2.6623403898929211e-7,
|
| 394 |
+
1.5950642189595716e-7, -4.7187514673841102e-8, -6.5107872958755177e-17,
|
| 395 |
+
7.9795091026746235e-9},
|
| 396 |
+
{3.0249124160905891e-2, 2.4817436002649977e-3, -4.9939134373457022e-2,
|
| 397 |
+
5.9915643009307869e-2, -3.2483207601623391e-2, -5.7212968652103441e-6,
|
| 398 |
+
1.5085251778569354e-2, -1.3261324005088445e-2, 5.5515262632426148e-3,
|
| 399 |
+
3.0263182257030016e-8, -1.7229548406756723e-3, 1.2893570099929637e-3,
|
| 400 |
+
-4.6845138348319876e-4, -1.830259937893045e-10, 1.1449739014822654e-4,
|
| 401 |
+
-7.7378565221244477e-5, 2.5625836246985201e-5, 1.0766165333192814e-12,
|
| 402 |
+
-5.3246809282422621e-6, 3.349634863064464e-6, -1.0381253128684018e-6,
|
| 403 |
+
-5.608909920621128e-15, 1.9150821930676591e-7, -1.1418365800203486e-7,
|
| 404 |
+
3.3654425209171788e-8},
|
| 405 |
+
{-9.9051020880159045e-2, 1.7954011706123486e-1, -1.2989606383463778e-1,
|
| 406 |
+
-3.1478872752284357e-5, 9.0510635276848131e-2, -9.2828824411184397e-2,
|
| 407 |
+
4.4412112839877808e-2, 2.7779236316835888e-7, -1.7229543805449697e-2,
|
| 408 |
+
1.4182925050891573e-2, -5.6214161633747336e-3, -2.39598509186381e-9,
|
| 409 |
+
1.6029634366079908e-3, -1.1606784674435773e-3, 4.1001337768153873e-4,
|
| 410 |
+
1.8365800754090661e-11, -9.5844256563655903e-5, 6.3643062337764708e-5,
|
| 411 |
+
-2.076250624489065e-5, -1.1806020912804483e-13, 4.2131808239120649e-6,
|
| 412 |
+
-2.6262241337012467e-6, 8.0770620494930662e-7, 6.0125912123632725e-16,
|
| 413 |
+
-1.4729737374018841e-7},
|
| 414 |
+
{-1.9994542198219728e-1, -1.5056113040026424e-2, 3.6470239469348489e-1,
|
| 415 |
+
-4.6435192311733545e-1, 2.6640934719197893e-1, 3.4038266027147191e-5,
|
| 416 |
+
-1.3784338709329624e-1, 1.276467178337056e-1, -5.6213828755200985e-2,
|
| 417 |
+
-1.753150885483011e-7, 1.9235592956768113e-2, -1.5088821281095315e-2,
|
| 418 |
+
5.7401854451350123e-3, 1.0622382710310225e-9, -1.5335082692563998e-3,
|
| 419 |
+
1.0819320643228214e-3, -3.7372510193945659e-4, -6.6170909729031985e-12,
|
| 420 |
+
8.4263617380909628e-5, -5.5150706827483479e-5, 1.7769536448348069e-5,
|
| 421 |
+
3.8827923210205533e-14, -3.53513697488768e-6, 2.1865832130045269e-6,
|
| 422 |
+
-6.6812849447625594e-7},
|
| 423 |
+
{7.2438608504029431e-1, -1.3918010932653375, 1.0654143352413968,
|
| 424 |
+
1.876173868950258e-4, -8.2705501176152696e-1, 8.9352433347828414e-1,
|
| 425 |
+
-4.4971003995291339e-1, -1.6107401567546652e-6, 1.9235590165271091e-1,
|
| 426 |
+
-1.6597702160042609e-1, 6.8882222681814333e-2, 1.3910091724608687e-8,
|
| 427 |
+
-2.146911561508663e-2, 1.6228980898865892e-2, -5.9796016172584256e-3,
|
| 428 |
+
-1.1287469112826745e-10, 1.5167451119784857e-3, -1.0478634293553899e-3,
|
| 429 |
+
3.5539072889126421e-4, 8.1704322111801517e-13, -7.7773013442452395e-5,
|
| 430 |
+
5.0291413897007722e-5, -1.6035083867000518e-5, 1.2469354315487605e-14,
|
| 431 |
+
3.1369106244517615e-6},
|
| 432 |
+
{1.6668949727276811, 1.165462765994632e-1, -3.3288393225018906,
|
| 433 |
+
4.4692325482864037, -2.6977693045875807, -2.600667859891061e-4,
|
| 434 |
+
1.5389017615694539, -1.4937962361134612, 6.8881964633233148e-1,
|
| 435 |
+
1.3077482004552385e-6, -2.5762963325596288e-1, 2.1097676102125449e-1,
|
| 436 |
+
-8.3714408359219882e-2, -7.7920428881354753e-9, 2.4267923064833599e-2,
|
| 437 |
+
-1.7813678334552311e-2, 6.3970330388900056e-3, 4.9430807090480523e-11,
|
| 438 |
+
-1.5554602758465635e-3, 1.0561196919903214e-3, -3.5277184460472902e-4,
|
| 439 |
+
9.3002334645022459e-14, 7.5285855026557172e-5, -4.8186515569156351e-5,
|
| 440 |
+
1.5227271505597605e-5},
|
| 441 |
+
{-6.6188298861372935, 1.3397985455142589e+1, -1.0789350606845146e+1,
|
| 442 |
+
-1.4352254537875018e-3, 9.2333694596189809, -1.0456552819547769e+1,
|
| 443 |
+
5.5105526029033471, 1.2024439690716742e-5, -2.5762961164755816,
|
| 444 |
+
2.3207442745387179, -1.0045728797216284, -1.0207833290021914e-7,
|
| 445 |
+
3.3975092171169466e-1, -2.6720517450757468e-1, 1.0235252851562706e-1,
|
| 446 |
+
8.4329730484871625e-10, -2.7998284958442595e-2, 2.0066274144976813e-2,
|
| 447 |
+
-7.0554368915086242e-3, 1.9402238183698188e-12, 1.6562888105449611e-3,
|
| 448 |
+
-1.1082898580743683e-3, 3.654545161310169e-4, -5.1290032026971794e-11,
|
| 449 |
+
-7.6340103696869031e-5},
|
| 450 |
+
{-1.7112706061976095e+1, -1.1208044642899116, 3.7131966511885444e+1,
|
| 451 |
+
-5.2298271025348962e+1, 3.3058589696624618e+1, 2.4791298976200222e-3,
|
| 452 |
+
-2.061089403411526e+1, 2.088672775145582e+1, -1.0045703956517752e+1,
|
| 453 |
+
-1.2238783449063012e-5, 4.0770134274221141, -3.473667358470195,
|
| 454 |
+
1.4329352617312006, 7.1359914411879712e-8, -4.4797257159115612e-1,
|
| 455 |
+
3.4112666080644461e-1, -1.2699786326594923e-1, -2.8953677269081528e-10,
|
| 456 |
+
3.3125776278259863e-2, -2.3274087021036101e-2, 8.0399993503648882e-3,
|
| 457 |
+
-1.177805216235265e-9, -1.8321624891071668e-3, 1.2108282933588665e-3,
|
| 458 |
+
-3.9479941246822517e-4},
|
| 459 |
+
{7.389033153567425e+1, -1.5680141270402273e+2, 1.322177542759164e+2,
|
| 460 |
+
1.3692876877324546e-2, -1.2366496885920151e+2, 1.4620689391062729e+2,
|
| 461 |
+
-8.0365587724865346e+1, -1.1259851148881298e-4, 4.0770132196179938e+1,
|
| 462 |
+
-3.8210340013273034e+1, 1.719522294277362e+1, 9.3519707955168356e-7,
|
| 463 |
+
-6.2716159907747034, 5.1168999071852637, -2.0319658112299095,
|
| 464 |
+
-4.9507215582761543e-9, 5.9626397294332597e-1, -4.4220765337238094e-1,
|
| 465 |
+
1.6079998700166273e-1, -2.4733786203223402e-8, -4.0307574759979762e-2,
|
| 466 |
+
2.7849050747097869e-2, -9.4751858992054221e-3, 6.419922235909132e-6,
|
| 467 |
+
2.1250180774699461e-3},
|
| 468 |
+
{2.1216837098382522e+2, 1.3107863022633868e+1, -4.9698285932871748e+2,
|
| 469 |
+
7.3121595266969204e+2, -4.8213821720890847e+2, -2.8817248692894889e-2,
|
| 470 |
+
3.2616720302947102e+2, -3.4389340280087117e+2, 1.7195193870816232e+2,
|
| 471 |
+
1.4038077378096158e-4, -7.52594195897599e+1, 6.651969984520934e+1,
|
| 472 |
+
-2.8447519748152462e+1, -7.613702615875391e-7, 9.5402237105304373,
|
| 473 |
+
-7.5175301113311376, 2.8943997568871961, -4.6612194999538201e-7,
|
| 474 |
+
-8.0615149598794088e-1, 5.8483006570631029e-1, -2.0845408972964956e-1,
|
| 475 |
+
1.4765818959305817e-4, 5.1000433863753019e-2, -3.3066252141883665e-2,
|
| 476 |
+
1.5109265210467774e-2},
|
| 477 |
+
{-9.8959643098322368e+2, 2.1925555360905233e+3, -1.9283586782723356e+3,
|
| 478 |
+
-1.5925738122215253e-1, 1.9569985945919857e+3, -2.4072514765081556e+3,
|
| 479 |
+
1.3756149959336496e+3, 1.2920735237496668e-3, -7.525941715948055e+2,
|
| 480 |
+
7.3171668742208716e+2, -3.4137023466220065e+2, -9.9857390260608043e-6,
|
| 481 |
+
1.3356313181291573e+2, -1.1276295161252794e+2, 4.6310396098204458e+1,
|
| 482 |
+
-7.9237387133614756e-6, -1.4510726927018646e+1, 1.1111771248100563e+1,
|
| 483 |
+
-4.1690817945270892, 3.1008219800117808e-3, 1.1220095449981468,
|
| 484 |
+
-7.6052379926149916e-1, 3.6262236505085254e-1, 2.216867741940747e-1,
|
| 485 |
+
4.8683443692930507e-1}};
|
| 486 |
+
|
| 487 |
+
int k, n, sgn;
|
| 488 |
+
int maxpow = 0;
|
| 489 |
+
const accscalar_t MACHEP = 5.9604644775390625E-8;
|
| 490 |
+
accscalar_t lambda = x / a;
|
| 491 |
+
accscalar_t sigma = (x - a) / a;
|
| 492 |
+
accscalar_t eta, res, ck, ckterm, term, absterm;
|
| 493 |
+
accscalar_t absoldterm = INFINITY;
|
| 494 |
+
accscalar_t etapow[25] = {1};
|
| 495 |
+
accscalar_t sum = 0;
|
| 496 |
+
accscalar_t afac = 1;
|
| 497 |
+
|
| 498 |
+
if (igam) {
|
| 499 |
+
sgn = -1;
|
| 500 |
+
} else {
|
| 501 |
+
sgn = 1;
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
if (lambda > 1) {
|
| 505 |
+
eta = ::sqrt(-2 * (::log1p(sigma) - sigma));
|
| 506 |
+
} else if (lambda < 1) {
|
| 507 |
+
eta = -::sqrt(-2 * (::log1p(sigma) - sigma));
|
| 508 |
+
} else {
|
| 509 |
+
eta = 0;
|
| 510 |
+
}
|
| 511 |
+
res = 0.5 * ::erfc(sgn * eta * ::sqrt(a / 2));
|
| 512 |
+
|
| 513 |
+
for (k = 0; k < 25; k++) {
|
| 514 |
+
ck = d[k][0];
|
| 515 |
+
for (n = 1; n < 25; n++) {
|
| 516 |
+
if (n > maxpow) {
|
| 517 |
+
etapow[n] = eta * etapow[n - 1];
|
| 518 |
+
maxpow += 1;
|
| 519 |
+
}
|
| 520 |
+
ckterm = d[k][n] * etapow[n];
|
| 521 |
+
ck += ckterm;
|
| 522 |
+
if (::fabs(ckterm) < MACHEP * ::fabs(ck)) {
|
| 523 |
+
break;
|
| 524 |
+
}
|
| 525 |
+
}
|
| 526 |
+
term = ck * afac;
|
| 527 |
+
absterm = ::fabs(term);
|
| 528 |
+
if (absterm > absoldterm) {
|
| 529 |
+
break;
|
| 530 |
+
}
|
| 531 |
+
sum += term;
|
| 532 |
+
if (absterm < MACHEP * ::fabs(sum)) {
|
| 533 |
+
break;
|
| 534 |
+
}
|
| 535 |
+
absoldterm = absterm;
|
| 536 |
+
afac /= a;
|
| 537 |
+
}
|
| 538 |
+
res += sgn * ::exp(-0.5 * a * eta * eta) * sum / ::sqrt(2 * 3.1415926535 * a);
|
| 539 |
+
|
| 540 |
+
return res;
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
template <typename scalar_t>
|
| 544 |
+
scalar_t _igamc_helper_continued_fraction(scalar_t a, scalar_t x) {
|
| 545 |
+
// Compute igamc using DLMF 8.9.2. [igam1]
|
| 546 |
+
|
| 547 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 548 |
+
int i;
|
| 549 |
+
accscalar_t ans, ax, c, yc, r, t, y, z;
|
| 550 |
+
accscalar_t pk, pkm1, pkm2, qk, qkm1, qkm2;
|
| 551 |
+
const int MAXITER = 2000;
|
| 552 |
+
const accscalar_t MACHEP = 5.9604644775390625E-8;
|
| 553 |
+
const accscalar_t BIG = 16777216.;
|
| 554 |
+
const accscalar_t BIGINV = 5.9604644775390625E-8;
|
| 555 |
+
|
| 556 |
+
ax = _igam_helper_fac(a, x);
|
| 557 |
+
if (ax == 0.0) {
|
| 558 |
+
return 0.0;
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
/* continued fraction */
|
| 562 |
+
y = 1.0 - a;
|
| 563 |
+
z = x + y + 1.0;
|
| 564 |
+
c = 0.0;
|
| 565 |
+
pkm2 = 1.0;
|
| 566 |
+
qkm2 = x;
|
| 567 |
+
pkm1 = x + 1.0;
|
| 568 |
+
qkm1 = z * x;
|
| 569 |
+
ans = pkm1 / qkm1;
|
| 570 |
+
|
| 571 |
+
for (i = 0; i < MAXITER; i++) {
|
| 572 |
+
c += 1.0;
|
| 573 |
+
y += 1.0;
|
| 574 |
+
z += 2.0;
|
| 575 |
+
yc = y * c;
|
| 576 |
+
pk = pkm1 * z - pkm2 * yc;
|
| 577 |
+
qk = qkm1 * z - qkm2 * yc;
|
| 578 |
+
if (qk != 0) {
|
| 579 |
+
r = pk / qk;
|
| 580 |
+
t = ::fabs((ans - r) / r);
|
| 581 |
+
ans = r;
|
| 582 |
+
} else {
|
| 583 |
+
t = 1.0;
|
| 584 |
+
}
|
| 585 |
+
pkm2 = pkm1;
|
| 586 |
+
pkm1 = pk;
|
| 587 |
+
qkm2 = qkm1;
|
| 588 |
+
qkm1 = qk;
|
| 589 |
+
if (::fabs(pk) > BIG) {
|
| 590 |
+
pkm2 *= BIGINV;
|
| 591 |
+
pkm1 *= BIGINV;
|
| 592 |
+
qkm2 *= BIGINV;
|
| 593 |
+
qkm1 *= BIGINV;
|
| 594 |
+
}
|
| 595 |
+
if (t <= MACHEP) {
|
| 596 |
+
break;
|
| 597 |
+
}
|
| 598 |
+
}
|
| 599 |
+
return ans * ax;
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
template <typename scalar_t>
|
| 603 |
+
scalar_t calc_igammac(scalar_t a, scalar_t x) {
|
| 604 |
+
/* the calculation of the regularized upper incomplete gamma function
|
| 605 |
+
* is done differently based on the values of a and x:
|
| 606 |
+
* - if x and/or a is at the boundary of defined region, then assign the
|
| 607 |
+
* result at the boundary
|
| 608 |
+
* - if a is large and a ~ x, then using Uniform Asymptotic Expansions for
|
| 609 |
+
* Large Parameter (see DLMF 8.12.4 [igam1])
|
| 610 |
+
* - if x > 1.1 and x < a, using the subtraction from the regularized lower
|
| 611 |
+
* incomplete gamma
|
| 612 |
+
* - otherwise, calculate the series from [igam2] eq (5)
|
| 613 |
+
*/
|
| 614 |
+
|
| 615 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 616 |
+
accscalar_t absxma_a;
|
| 617 |
+
|
| 618 |
+
const accscalar_t SMALL = 20.0;
|
| 619 |
+
const accscalar_t LARGE = 200.0;
|
| 620 |
+
const accscalar_t SMALLRATIO = 0.3;
|
| 621 |
+
const accscalar_t LARGERATIO = 4.5;
|
| 622 |
+
|
| 623 |
+
if ((x < 0) || (a < 0)) {
|
| 624 |
+
// out of defined-region of the function
|
| 625 |
+
return NAN;
|
| 626 |
+
} else if (a == 0) {
|
| 627 |
+
if (x > 0) {
|
| 628 |
+
return 0.0;
|
| 629 |
+
} else {
|
| 630 |
+
return NAN;
|
| 631 |
+
}
|
| 632 |
+
} else if (x == 0) {
|
| 633 |
+
return 1.0;
|
| 634 |
+
} else if (isinf(a)) {
|
| 635 |
+
if (isinf(x)) {
|
| 636 |
+
return NAN;
|
| 637 |
+
}
|
| 638 |
+
return 1.0;
|
| 639 |
+
} else if (isinf(x)) {
|
| 640 |
+
return 0.0;
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
absxma_a = ::fabs(x - a) / a;
|
| 644 |
+
if ((a > SMALL) && (a < LARGE) && (absxma_a < SMALLRATIO)) {
|
| 645 |
+
return _igam_helper_asymptotic_series(a, x, 0);
|
| 646 |
+
} else if ((a > LARGE) && (absxma_a < LARGERATIO / ::sqrt(a))) {
|
| 647 |
+
return _igam_helper_asymptotic_series(a, x, 0);
|
| 648 |
+
}
|
| 649 |
+
|
| 650 |
+
if (x > 1.1) {
|
| 651 |
+
if (x < a) {
|
| 652 |
+
return 1.0 - _igam_helper_series(a, x);
|
| 653 |
+
} else {
|
| 654 |
+
return _igamc_helper_continued_fraction(a, x);
|
| 655 |
+
}
|
| 656 |
+
} else if (x <= 0.5) {
|
| 657 |
+
if (-0.4 / ::log(x) < a) {
|
| 658 |
+
return 1.0 - _igam_helper_series(a, x);
|
| 659 |
+
} else {
|
| 660 |
+
return _igamc_helper_series(a, x);
|
| 661 |
+
}
|
| 662 |
+
} else {
|
| 663 |
+
if (x * 1.1 < a) {
|
| 664 |
+
return 1.0 - _igam_helper_series(a, x);
|
| 665 |
+
} else {
|
| 666 |
+
return _igamc_helper_series(a, x);
|
| 667 |
+
}
|
| 668 |
+
}
|
| 669 |
+
}
|
| 670 |
+
|
| 671 |
+
template <typename scalar_t>
|
| 672 |
+
scalar_t calc_igamma(scalar_t a, scalar_t x) {
|
| 673 |
+
/* the calculation of the regularized lower incomplete gamma function
|
| 674 |
+
* is done differently based on the values of a and x:
|
| 675 |
+
* - if x and/or a is at the boundary of defined region, then assign the
|
| 676 |
+
* result at the boundary
|
| 677 |
+
* - if a is large and a ~ x, then using Uniform Asymptotic Expansions for
|
| 678 |
+
* Large Parameter (see DLMF 8.12.3 [igam1])
|
| 679 |
+
* - if x > 1 and x > a, using the subtraction from the regularized upper
|
| 680 |
+
* incomplete gamma
|
| 681 |
+
* - otherwise, calculate the series from [igam2] eq (4)
|
| 682 |
+
*/
|
| 683 |
+
|
| 684 |
+
using accscalar_t = opmath_t<scalar_t>;
|
| 685 |
+
accscalar_t absxma_a;
|
| 686 |
+
const accscalar_t SMALL = 20.0;
|
| 687 |
+
const accscalar_t LARGE = 200.0;
|
| 688 |
+
const accscalar_t SMALLRATIO = 0.3;
|
| 689 |
+
const accscalar_t LARGERATIO = 4.5;
|
| 690 |
+
|
| 691 |
+
// boundary values following SciPy
|
| 692 |
+
if ((x < 0) || (a < 0)) {
|
| 693 |
+
// out of defined-region of the function
|
| 694 |
+
return NAN;
|
| 695 |
+
} else if (a == 0) {
|
| 696 |
+
if (x > 0) {
|
| 697 |
+
return 1.0;
|
| 698 |
+
} else {
|
| 699 |
+
return NAN;
|
| 700 |
+
}
|
| 701 |
+
} else if (x == 0) {
|
| 702 |
+
return 0.0; // zero integration limit
|
| 703 |
+
} else if (isinf(a)) {
|
| 704 |
+
if (isinf(x)) {
|
| 705 |
+
return NAN;
|
| 706 |
+
}
|
| 707 |
+
return 0.0;
|
| 708 |
+
} else if (isinf(x)) {
|
| 709 |
+
return 1.0;
|
| 710 |
+
}
|
| 711 |
+
|
| 712 |
+
/* Asymptotic regime where a ~ x. */
|
| 713 |
+
absxma_a = ::fabs(x - a) / a;
|
| 714 |
+
if ((a > SMALL) && (a < LARGE) && (absxma_a < SMALLRATIO)) {
|
| 715 |
+
return _igam_helper_asymptotic_series(a, x, 1);
|
| 716 |
+
} else if ((a > LARGE) && (absxma_a < LARGERATIO / ::sqrt(a))) {
|
| 717 |
+
return _igam_helper_asymptotic_series(a, x, 1);
|
| 718 |
+
}
|
| 719 |
+
|
| 720 |
+
if ((x > 1.0) && (x > a)) {
|
| 721 |
+
return 1.0 - calc_igammac(a, x);
|
| 722 |
+
}
|
| 723 |
+
|
| 724 |
+
return _igam_helper_series(a, x);
|
| 725 |
+
}
|
| 726 |
+
|
| 727 |
+
} // namespace
|
| 728 |
+
|
| 729 |
+
// end of regularized lower & upper incomplete gamma
|
| 730 |
+
|
| 731 |
+
namespace c10 {
|
| 732 |
+
namespace metal {
|
| 733 |
+
|
| 734 |
+
template <typename T>
|
| 735 |
+
inline T igamma(T a, T b) {
|
| 736 |
+
return calc_igamma(a, b);
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
+
template <typename T>
|
| 740 |
+
inline T igammac(T a, T b) {
|
| 741 |
+
return calc_igammac(a, b);
|
| 742 |
+
}
|
| 743 |
+
|
| 744 |
+
} // namespace metal
|
| 745 |
+
} // namespace c10
|
| 746 |
+
|
| 747 |
+
#else
|
| 748 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 749 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/indexing.h
ADDED
|
@@ -0,0 +1,1050 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Metal indexing primitives
|
| 3 |
+
#pragma once
|
| 4 |
+
#include <c10/metal/common.h>
|
| 5 |
+
#include <c10/metal/utils.h>
|
| 6 |
+
#include <metal_stdlib>
|
| 7 |
+
|
| 8 |
+
namespace c10 {
|
| 9 |
+
namespace metal {
|
| 10 |
+
|
| 11 |
+
// Given coordinates and strides, calculates offset from the start of the
|
| 12 |
+
// tensors
|
| 13 |
+
template <typename T>
|
| 14 |
+
inline T offset_from_coord(
|
| 15 |
+
thread T idx[max_ndim],
|
| 16 |
+
constant long* strides,
|
| 17 |
+
uint ndim) {
|
| 18 |
+
T rc = 0;
|
| 19 |
+
for (uint i = 0; i < ndim; ++i) {
|
| 20 |
+
rc += idx[i] * T(strides[i]);
|
| 21 |
+
}
|
| 22 |
+
return rc;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// Given thread index calculates position in the ndim tensor
|
| 26 |
+
template <typename T>
|
| 27 |
+
inline void pos_from_thread_index(
|
| 28 |
+
T idx,
|
| 29 |
+
thread T pos[max_ndim],
|
| 30 |
+
constant long* sizes,
|
| 31 |
+
uint ndim) {
|
| 32 |
+
for (uint i = 0; i < ndim; ++i) {
|
| 33 |
+
pos[i] = idx % T(sizes[i]);
|
| 34 |
+
idx /= T(sizes[i]);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
inline long offset_from_thread_index(
|
| 39 |
+
long idx,
|
| 40 |
+
constant long* sizes,
|
| 41 |
+
constant long* strides,
|
| 42 |
+
uint ndim) {
|
| 43 |
+
long pos[max_ndim];
|
| 44 |
+
pos_from_thread_index(idx, pos, sizes, ndim);
|
| 45 |
+
return offset_from_coord(pos, strides, ndim);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
template <typename T, typename F>
|
| 49 |
+
kernel void unary_dense(
|
| 50 |
+
device result_of<F, T>* output [[buffer(0)]],
|
| 51 |
+
constant T* input [[buffer(1)]],
|
| 52 |
+
uint index [[thread_position_in_grid]]) {
|
| 53 |
+
F f;
|
| 54 |
+
output[index] = f(input[index]);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
template <typename T, typename F>
|
| 58 |
+
kernel void unary_strided(
|
| 59 |
+
device result_of<F, T>* output [[buffer(0)]],
|
| 60 |
+
constant T* input [[buffer(1)]],
|
| 61 |
+
constant long* sizes [[buffer(2)]],
|
| 62 |
+
constant long* input_strides [[buffer(3)]],
|
| 63 |
+
constant long* output_strides [[buffer(4)]],
|
| 64 |
+
constant uint& ndim [[buffer(5)]],
|
| 65 |
+
uint index [[thread_position_in_grid]]) {
|
| 66 |
+
F f;
|
| 67 |
+
int pos[max_ndim];
|
| 68 |
+
pos_from_thread_index(int(index), pos, sizes, ndim);
|
| 69 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim);
|
| 70 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim);
|
| 71 |
+
output[output_offs] = f(input[input_offs]);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
#define REGISTER_UNARY_OP(NAME, DTYPE0, DTYPE1) \
|
| 75 |
+
static_assert( \
|
| 76 |
+
::metal:: \
|
| 77 |
+
is_same_v<DTYPE1, ::c10::metal::result_of<NAME##_functor, DTYPE0>>, \
|
| 78 |
+
"Output dtype mismatch for unary op " #NAME " and input " #DTYPE0); \
|
| 79 |
+
template [[host_name(#NAME "_dense_" #DTYPE1 "_" #DTYPE0)]] kernel void :: \
|
| 80 |
+
c10::metal::unary_dense<DTYPE0, NAME##_functor>( \
|
| 81 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPE0> * output, \
|
| 82 |
+
constant DTYPE0 * input, \
|
| 83 |
+
uint index); \
|
| 84 |
+
template [[host_name(#NAME "_strided_" #DTYPE1 "_" #DTYPE0)]] kernel void :: \
|
| 85 |
+
c10::metal::unary_strided<DTYPE0, NAME##_functor>( \
|
| 86 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPE0> * output, \
|
| 87 |
+
constant DTYPE0 * input, \
|
| 88 |
+
constant long* sizes, \
|
| 89 |
+
constant long* input_strides, \
|
| 90 |
+
constant long* output_strides, \
|
| 91 |
+
constant uint& ndim, \
|
| 92 |
+
uint index)
|
| 93 |
+
|
| 94 |
+
#define DEFINE_UNARY_FLOATING_FUNCTOR(NAME) \
|
| 95 |
+
struct NAME##_functor { \
|
| 96 |
+
template <typename T> \
|
| 97 |
+
inline ::metal::enable_if_t<::metal::is_floating_point_v<T>, T> operator()( \
|
| 98 |
+
const T x) { \
|
| 99 |
+
return T(NAME(x)); \
|
| 100 |
+
} \
|
| 101 |
+
template <typename T> \
|
| 102 |
+
inline ::metal::enable_if_t<::metal::is_integral_v<T>, float> operator()( \
|
| 103 |
+
const T x) { \
|
| 104 |
+
return NAME(static_cast<float>(x)); \
|
| 105 |
+
} \
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
template <typename T, typename T2, typename F>
|
| 109 |
+
kernel void unary_alpha_dense(
|
| 110 |
+
device result_of<F, T, T2>* output [[buffer(0)]],
|
| 111 |
+
constant T* input [[buffer(1)]],
|
| 112 |
+
constant T2& alpha [[buffer(2)]],
|
| 113 |
+
uint index [[thread_position_in_grid]]) {
|
| 114 |
+
F f;
|
| 115 |
+
output[index] = f(input[index], alpha);
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
template <typename T, typename T2, typename F>
|
| 119 |
+
kernel void unary_alpha_strided(
|
| 120 |
+
device result_of<F, T, T2>* output [[buffer(0)]],
|
| 121 |
+
constant T* input [[buffer(1)]],
|
| 122 |
+
constant long* sizes [[buffer(2)]],
|
| 123 |
+
constant long* input_strides [[buffer(3)]],
|
| 124 |
+
constant long* output_strides [[buffer(4)]],
|
| 125 |
+
constant uint& ndim [[buffer(5)]],
|
| 126 |
+
constant T2& alpha [[buffer(6)]],
|
| 127 |
+
uint index [[thread_position_in_grid]]) {
|
| 128 |
+
F f;
|
| 129 |
+
int pos[max_ndim];
|
| 130 |
+
pos_from_thread_index(int(index), pos, sizes, ndim);
|
| 131 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim);
|
| 132 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim);
|
| 133 |
+
output[output_offs] = f(input[input_offs], alpha);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
#define REGISTER_UNARY_ALPHA_OP(NAME, DTYPEI, DTYPEA, DTYPEO) \
|
| 137 |
+
static_assert( \
|
| 138 |
+
::metal::is_same_v< \
|
| 139 |
+
DTYPEO, \
|
| 140 |
+
::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEA>>, \
|
| 141 |
+
"Output dtype mismatch for unary op " #NAME " and input " #DTYPEI); \
|
| 142 |
+
template [[host_name(#NAME "_dense_" #DTYPEO "_" #DTYPEI \
|
| 143 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 144 |
+
unary_alpha_dense<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 145 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEA> * \
|
| 146 |
+
output, \
|
| 147 |
+
constant DTYPEI * input, \
|
| 148 |
+
constant DTYPEA & alpha, \
|
| 149 |
+
uint index); \
|
| 150 |
+
template [[host_name(#NAME "_strided_" #DTYPEO "_" #DTYPEI \
|
| 151 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 152 |
+
unary_alpha_strided<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 153 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEA> * \
|
| 154 |
+
output, \
|
| 155 |
+
constant DTYPEI * input, \
|
| 156 |
+
constant long* sizes, \
|
| 157 |
+
constant long* input_strides, \
|
| 158 |
+
constant long* output_strides, \
|
| 159 |
+
constant uint& ndim, \
|
| 160 |
+
constant DTYPEA& alpha, \
|
| 161 |
+
uint index)
|
| 162 |
+
|
| 163 |
+
template <typename T>
|
| 164 |
+
inline T val_at_offs(constant void* ptr, long offs) {
|
| 165 |
+
return *reinterpret_cast<constant T*>(
|
| 166 |
+
static_cast<constant char*>(ptr) + offs);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// Value at offset with dynamic cast from provided type
|
| 170 |
+
template <typename T>
|
| 171 |
+
inline T val_at_offs(device void* ptr, long offs) {
|
| 172 |
+
return *reinterpret_cast<device T*>(static_cast<device char*>(ptr) + offs);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
template <typename T, typename P>
|
| 176 |
+
inline T val_at_offs(P ptr, long offs, ScalarType type) {
|
| 177 |
+
switch (type) {
|
| 178 |
+
case ScalarType::Bool:
|
| 179 |
+
return cast_to<T>(val_at_offs<bool>(ptr, offs));
|
| 180 |
+
case ScalarType::Byte:
|
| 181 |
+
return cast_to<T>(val_at_offs<uchar>(ptr, offs));
|
| 182 |
+
case ScalarType::Char:
|
| 183 |
+
return cast_to<T>(val_at_offs<char>(ptr, offs));
|
| 184 |
+
case ScalarType::Short:
|
| 185 |
+
return cast_to<T>(val_at_offs<short>(ptr, offs));
|
| 186 |
+
case ScalarType::Int:
|
| 187 |
+
return cast_to<T>(val_at_offs<int>(ptr, offs));
|
| 188 |
+
case ScalarType::Long:
|
| 189 |
+
return cast_to<T>(val_at_offs<long>(ptr, offs));
|
| 190 |
+
// Floats
|
| 191 |
+
case ScalarType::Float:
|
| 192 |
+
return cast_to<T>(val_at_offs<float>(ptr, offs));
|
| 193 |
+
case ScalarType::Half:
|
| 194 |
+
return cast_to<T>(val_at_offs<half>(ptr, offs));
|
| 195 |
+
case ScalarType::BFloat16:
|
| 196 |
+
return cast_to<T>(val_at_offs<bfloat>(ptr, offs));
|
| 197 |
+
// Complex
|
| 198 |
+
case ScalarType::ComplexHalf:
|
| 199 |
+
return cast_to<T>(val_at_offs<half2>(ptr, offs));
|
| 200 |
+
case ScalarType::ComplexFloat:
|
| 201 |
+
return cast_to<T>(val_at_offs<float2>(ptr, offs));
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
template <typename T>
|
| 206 |
+
inline device T& ref_at_offs(device void* ptr, long offs) {
|
| 207 |
+
return *reinterpret_cast<device T*>(static_cast<device char*>(ptr) + offs);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// Binary elementwise ops kernels
|
| 211 |
+
// Right now there are 4 flavors available:
|
| 212 |
+
// - binary_dense where both input, other and output are dense and share the
|
| 213 |
+
// same type
|
| 214 |
+
// - binary_strided when all inputs are of the same types, but some elements are
|
| 215 |
+
// strided
|
| 216 |
+
// - binary_dense_cast - inputs are dense, but of different dtypes
|
| 217 |
+
// - binary_strided_cast - inputs or output are strided and of different dtypes
|
| 218 |
+
// - binary_dense_broadcast - one input is dense, another one is broadcastable
|
| 219 |
+
// Note about accuracy (for more info see
|
| 220 |
+
// https://github.com/pytorch/pytorch/issues/152736) Sometimes when kernel is
|
| 221 |
+
// invoked to produce `half` output, but one of the arguments is float arguments
|
| 222 |
+
// should be upcast to float, rather than downcast to half At the moment this is
|
| 223 |
+
// expressed with `om_t` optional argument (which stands for opmath_type) which
|
| 224 |
+
// is identical to output type but could be something else
|
| 225 |
+
|
| 226 |
+
template <typename T, typename F, typename om_t = T>
|
| 227 |
+
kernel void binary_strided(
|
| 228 |
+
device void* output [[buffer(0)]],
|
| 229 |
+
constant void* input [[buffer(1)]],
|
| 230 |
+
constant void* other [[buffer(2)]],
|
| 231 |
+
constant long* sizes [[buffer(3)]],
|
| 232 |
+
constant long* output_strides [[buffer(4)]],
|
| 233 |
+
constant long* input_strides [[buffer(5)]],
|
| 234 |
+
constant long* other_strides [[buffer(6)]],
|
| 235 |
+
constant uint3& ndim [[buffer(7)]],
|
| 236 |
+
uint index [[thread_position_in_grid]]) {
|
| 237 |
+
F f;
|
| 238 |
+
using res_t = result_of<F, T, T>;
|
| 239 |
+
int pos[max_ndim];
|
| 240 |
+
pos_from_thread_index(int(index), pos, sizes, ndim.x);
|
| 241 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim.x);
|
| 242 |
+
const auto other_offs = offset_from_coord(pos, other_strides, ndim.x);
|
| 243 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim.x);
|
| 244 |
+
const auto a = val_at_offs<T>(input, input_offs);
|
| 245 |
+
const auto b = val_at_offs<T>(other, other_offs);
|
| 246 |
+
ref_at_offs<res_t>(output, output_offs) =
|
| 247 |
+
static_cast<res_t>(f(om_t(a), om_t(b)));
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
template <typename T, typename T2, typename F>
|
| 251 |
+
kernel void binary_alpha_strided(
|
| 252 |
+
device void* output [[buffer(0)]],
|
| 253 |
+
constant void* input [[buffer(1)]],
|
| 254 |
+
constant void* other [[buffer(2)]],
|
| 255 |
+
constant T2& alpha [[buffer(3)]],
|
| 256 |
+
constant long* sizes [[buffer(4)]],
|
| 257 |
+
constant long* output_strides [[buffer(5)]],
|
| 258 |
+
constant long* input_strides [[buffer(6)]],
|
| 259 |
+
constant long* other_strides [[buffer(7)]],
|
| 260 |
+
constant uint3& ndim [[buffer(8)]],
|
| 261 |
+
uint index [[thread_position_in_grid]]) {
|
| 262 |
+
F f;
|
| 263 |
+
int pos[max_ndim];
|
| 264 |
+
pos_from_thread_index(int(index), pos, sizes, ndim.x);
|
| 265 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim.x);
|
| 266 |
+
const auto other_offs = offset_from_coord(pos, other_strides, ndim.x);
|
| 267 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim.x);
|
| 268 |
+
const auto a = val_at_offs<T>(input, input_offs);
|
| 269 |
+
const auto b = val_at_offs<T>(other, other_offs);
|
| 270 |
+
ref_at_offs<result_of<F, T, T, T2>>(output, output_offs) = f(a, b, alpha);
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 274 |
+
kernel void binary_strided_cast(
|
| 275 |
+
device void* output [[buffer(0)]],
|
| 276 |
+
constant void* input [[buffer(1)]],
|
| 277 |
+
constant void* other [[buffer(2)]],
|
| 278 |
+
constant long* sizes [[buffer(3)]],
|
| 279 |
+
constant long* output_strides [[buffer(4)]],
|
| 280 |
+
constant long* input_strides [[buffer(5)]],
|
| 281 |
+
constant long* other_strides [[buffer(6)]],
|
| 282 |
+
constant uint4& ndim_types [[buffer(7)]],
|
| 283 |
+
uint index [[thread_position_in_grid]]) {
|
| 284 |
+
F f;
|
| 285 |
+
using res_t = result_of<F, T, T>;
|
| 286 |
+
int pos[max_ndim];
|
| 287 |
+
pos_from_thread_index(int(index), pos, sizes, ndim_types.x);
|
| 288 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim_types.x);
|
| 289 |
+
const auto other_offs = offset_from_coord(pos, other_strides, ndim_types.x);
|
| 290 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim_types.x);
|
| 291 |
+
const auto a = val_at_offs<om_t>(
|
| 292 |
+
input, input_offs, static_cast<ScalarType>(ndim_types.y));
|
| 293 |
+
const auto b = val_at_offs<om_t>(
|
| 294 |
+
other, other_offs, static_cast<ScalarType>(ndim_types.z));
|
| 295 |
+
ref_at_offs<res_t>(output, output_offs) = static_cast<res_t>(f(a, b));
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
template <typename T, typename T2, typename F>
|
| 299 |
+
kernel void binary_alpha_strided_cast(
|
| 300 |
+
device void* output [[buffer(0)]],
|
| 301 |
+
constant void* input [[buffer(1)]],
|
| 302 |
+
constant void* other [[buffer(2)]],
|
| 303 |
+
constant T2& alpha [[buffer(3)]],
|
| 304 |
+
constant long* sizes [[buffer(4)]],
|
| 305 |
+
constant long* output_strides [[buffer(5)]],
|
| 306 |
+
constant long* input_strides [[buffer(6)]],
|
| 307 |
+
constant long* other_strides [[buffer(7)]],
|
| 308 |
+
constant uint4& ndim_types [[buffer(8)]],
|
| 309 |
+
uint index [[thread_position_in_grid]]) {
|
| 310 |
+
F f;
|
| 311 |
+
int pos[max_ndim];
|
| 312 |
+
pos_from_thread_index(int(index), pos, sizes, ndim_types.x);
|
| 313 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim_types.x);
|
| 314 |
+
const auto other_offs = offset_from_coord(pos, other_strides, ndim_types.x);
|
| 315 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim_types.x);
|
| 316 |
+
const auto a =
|
| 317 |
+
val_at_offs<T>(input, input_offs, static_cast<ScalarType>(ndim_types.y));
|
| 318 |
+
const auto b =
|
| 319 |
+
val_at_offs<T>(other, other_offs, static_cast<ScalarType>(ndim_types.z));
|
| 320 |
+
ref_at_offs<result_of<F, T, T, T2>>(output, output_offs) = f(a, b, alpha);
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 324 |
+
kernel void binary_dense(
|
| 325 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 326 |
+
constant T* input [[buffer(1)]],
|
| 327 |
+
constant T* other [[buffer(2)]],
|
| 328 |
+
uint tid [[thread_position_in_grid]]) {
|
| 329 |
+
F f;
|
| 330 |
+
using res_t = result_of<F, T, T>;
|
| 331 |
+
out[tid] = static_cast<res_t>(f(om_t(input[tid]), om_t(other[tid])));
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
template <typename T, typename T2, typename F>
|
| 335 |
+
kernel void binary_alpha_dense(
|
| 336 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 337 |
+
constant T* input [[buffer(1)]],
|
| 338 |
+
constant T* other [[buffer(2)]],
|
| 339 |
+
constant T2& alpha [[buffer(3)]],
|
| 340 |
+
uint tid [[thread_position_in_grid]]) {
|
| 341 |
+
F f;
|
| 342 |
+
out[tid] = f(input[tid], other[tid], alpha);
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
template <typename T, typename F, typename om_t = T>
|
| 346 |
+
kernel void binary_dense_cast(
|
| 347 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 348 |
+
constant void* input [[buffer(1)]],
|
| 349 |
+
constant void* other [[buffer(2)]],
|
| 350 |
+
constant uint4& sizes_types [[buffer(3)]],
|
| 351 |
+
uint tid [[thread_position_in_grid]]) {
|
| 352 |
+
F f;
|
| 353 |
+
using res_t = result_of<F, T, T>;
|
| 354 |
+
const auto a = val_at_offs<om_t>(
|
| 355 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 356 |
+
const auto b = val_at_offs<om_t>(
|
| 357 |
+
other, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 358 |
+
out[tid] = static_cast<res_t>(f(a, b));
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
template <typename T, typename T2, typename F>
|
| 362 |
+
kernel void binary_alpha_dense_cast(
|
| 363 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 364 |
+
constant void* input [[buffer(1)]],
|
| 365 |
+
constant void* other [[buffer(2)]],
|
| 366 |
+
constant T2& alpha [[buffer(3)]],
|
| 367 |
+
constant uint4& sizes_types [[buffer(4)]],
|
| 368 |
+
uint tid [[thread_position_in_grid]]) {
|
| 369 |
+
F f;
|
| 370 |
+
const auto a = val_at_offs<T>(
|
| 371 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 372 |
+
const auto b = val_at_offs<T>(
|
| 373 |
+
other, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 374 |
+
out[tid] = f(a, b, alpha);
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 378 |
+
kernel void binary_dense_broadcast(
|
| 379 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 380 |
+
constant T* input [[buffer(1)]],
|
| 381 |
+
constant T* broadcast [[buffer(2)]],
|
| 382 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 383 |
+
uint tid [[thread_position_in_grid]]) {
|
| 384 |
+
F f;
|
| 385 |
+
using res_t = result_of<F, T, T>;
|
| 386 |
+
out[tid] = static_cast<res_t>(
|
| 387 |
+
f(om_t(input[tid]), om_t(broadcast[tid % broadcast_numel])));
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 391 |
+
kernel void binary_dense_broadcast_rhs(
|
| 392 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 393 |
+
constant T* broadcast [[buffer(1)]],
|
| 394 |
+
constant T* input [[buffer(2)]],
|
| 395 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 396 |
+
uint tid [[thread_position_in_grid]]) {
|
| 397 |
+
F f;
|
| 398 |
+
using res_t = result_of<F, T, T>;
|
| 399 |
+
out[tid] = static_cast<res_t>(
|
| 400 |
+
f(om_t(broadcast[tid % broadcast_numel]), om_t(input[tid])));
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
template <typename T, typename T2, typename F>
|
| 404 |
+
kernel void binary_alpha_dense_broadcast(
|
| 405 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 406 |
+
constant T* input [[buffer(1)]],
|
| 407 |
+
constant T* broadcast [[buffer(2)]],
|
| 408 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 409 |
+
constant T2& alpha [[buffer(4)]],
|
| 410 |
+
uint tid [[thread_position_in_grid]]) {
|
| 411 |
+
F f;
|
| 412 |
+
out[tid] = f(input[tid], broadcast[tid % broadcast_numel], alpha);
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
template <typename T, typename T2, typename F>
|
| 416 |
+
kernel void binary_alpha_dense_broadcast_rhs(
|
| 417 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 418 |
+
constant T* broadcast [[buffer(1)]],
|
| 419 |
+
constant T* input [[buffer(2)]],
|
| 420 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 421 |
+
constant T2& alpha [[buffer(4)]],
|
| 422 |
+
uint tid [[thread_position_in_grid]]) {
|
| 423 |
+
F f;
|
| 424 |
+
out[tid] = f(broadcast[tid % broadcast_numel], input[tid], alpha);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
template <typename T, typename F, typename om_t = T>
|
| 428 |
+
kernel void binary_dense_broadcast_cast(
|
| 429 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 430 |
+
constant void* input [[buffer(1)]],
|
| 431 |
+
constant void* broadcast [[buffer(2)]],
|
| 432 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 433 |
+
constant uint4& sizes_types [[buffer(4)]],
|
| 434 |
+
uint tid [[thread_position_in_grid]]) {
|
| 435 |
+
F f;
|
| 436 |
+
using res_t = result_of<F, T, T>;
|
| 437 |
+
const auto a = val_at_offs<om_t>(
|
| 438 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 439 |
+
const auto b = val_at_offs<om_t>(
|
| 440 |
+
broadcast,
|
| 441 |
+
(tid % broadcast_numel) * sizes_types.y,
|
| 442 |
+
static_cast<ScalarType>(sizes_types.w));
|
| 443 |
+
out[tid] = static_cast<res_t>(f(a, b));
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
template <typename T, typename F, typename om_t = T>
|
| 447 |
+
kernel void binary_dense_broadcast_rhs_cast(
|
| 448 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 449 |
+
constant void* broadcast [[buffer(1)]],
|
| 450 |
+
constant void* input [[buffer(2)]],
|
| 451 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 452 |
+
constant uint4& sizes_types [[buffer(4)]],
|
| 453 |
+
uint tid [[thread_position_in_grid]]) {
|
| 454 |
+
F f;
|
| 455 |
+
using res_t = result_of<F, T, T>;
|
| 456 |
+
const auto a = val_at_offs<om_t>(
|
| 457 |
+
broadcast,
|
| 458 |
+
(tid % broadcast_numel) * sizes_types.x,
|
| 459 |
+
static_cast<ScalarType>(sizes_types.z));
|
| 460 |
+
const auto b = val_at_offs<om_t>(
|
| 461 |
+
input, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 462 |
+
out[tid] = static_cast<res_t>(f(a, b));
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
template <typename T, typename T2, typename F>
|
| 466 |
+
kernel void binary_alpha_dense_broadcast_cast(
|
| 467 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 468 |
+
constant void* input [[buffer(1)]],
|
| 469 |
+
constant void* broadcast [[buffer(2)]],
|
| 470 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 471 |
+
constant T2& alpha [[buffer(4)]],
|
| 472 |
+
constant uint4& sizes_types [[buffer(5)]],
|
| 473 |
+
uint tid [[thread_position_in_grid]]) {
|
| 474 |
+
F f;
|
| 475 |
+
const auto a = val_at_offs<T>(
|
| 476 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 477 |
+
const auto b = val_at_offs<T>(
|
| 478 |
+
broadcast,
|
| 479 |
+
(tid % broadcast_numel) * sizes_types.y,
|
| 480 |
+
static_cast<ScalarType>(sizes_types.w));
|
| 481 |
+
out[tid] = f(a, b, alpha);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
template <typename T, typename T2, typename F>
|
| 485 |
+
kernel void binary_alpha_dense_broadcast_rhs_cast(
|
| 486 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 487 |
+
constant void* broadcast [[buffer(1)]],
|
| 488 |
+
constant void* input [[buffer(2)]],
|
| 489 |
+
constant long& broadcast_numel [[buffer(3)]],
|
| 490 |
+
constant T2& alpha [[buffer(4)]],
|
| 491 |
+
constant uint4& sizes_types [[buffer(5)]],
|
| 492 |
+
uint tid [[thread_position_in_grid]]) {
|
| 493 |
+
F f;
|
| 494 |
+
const auto a = val_at_offs<T>(
|
| 495 |
+
broadcast,
|
| 496 |
+
(tid % broadcast_numel) * sizes_types.x,
|
| 497 |
+
static_cast<ScalarType>(sizes_types.z));
|
| 498 |
+
const auto b = val_at_offs<T>(
|
| 499 |
+
input, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 500 |
+
out[tid] = f(a, b, alpha);
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 504 |
+
kernel void binary_dense_scalar(
|
| 505 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 506 |
+
constant T* input [[buffer(1)]],
|
| 507 |
+
device T* scalar [[buffer(2)]],
|
| 508 |
+
uint tid [[thread_position_in_grid]]) {
|
| 509 |
+
F f;
|
| 510 |
+
using res_t = result_of<F, T, T>;
|
| 511 |
+
out[tid] = static_cast<res_t>(f(om_t(input[tid]), om_t(scalar[0])));
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 515 |
+
kernel void binary_dense_scalar_lhs(
|
| 516 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 517 |
+
device T* scalar [[buffer(1)]],
|
| 518 |
+
constant T* input [[buffer(2)]],
|
| 519 |
+
uint tid [[thread_position_in_grid]]) {
|
| 520 |
+
F f;
|
| 521 |
+
using res_t = result_of<F, T, T>;
|
| 522 |
+
out[tid] = static_cast<res_t>(f(om_t(scalar[0]), om_t(input[tid])));
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
template <typename T, typename F, typename om_t = T>
|
| 526 |
+
kernel void binary_dense_scalar_cast(
|
| 527 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 528 |
+
constant void* input [[buffer(1)]],
|
| 529 |
+
device void* scalar [[buffer(2)]],
|
| 530 |
+
constant uint4& sizes_types [[buffer(3)]],
|
| 531 |
+
uint tid [[thread_position_in_grid]]) {
|
| 532 |
+
F f;
|
| 533 |
+
using res_t = result_of<F, T, T>;
|
| 534 |
+
const auto a = val_at_offs<om_t>(
|
| 535 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 536 |
+
const auto b =
|
| 537 |
+
val_at_offs<om_t>(scalar, 0, static_cast<ScalarType>(sizes_types.w));
|
| 538 |
+
out[tid] = static_cast<res_t>(f(a, b));
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
template <typename T, typename F, typename om_t = T>
|
| 542 |
+
kernel void binary_dense_scalar_lhs_cast(
|
| 543 |
+
device result_of<F, T, T>* out [[buffer(0)]],
|
| 544 |
+
device void* scalar [[buffer(1)]],
|
| 545 |
+
constant void* input [[buffer(2)]],
|
| 546 |
+
constant uint4& sizes_types [[buffer(3)]],
|
| 547 |
+
uint tid [[thread_position_in_grid]]) {
|
| 548 |
+
F f;
|
| 549 |
+
using res_t = result_of<F, T, T>;
|
| 550 |
+
const auto a =
|
| 551 |
+
val_at_offs<om_t>(scalar, 0, static_cast<ScalarType>(sizes_types.z));
|
| 552 |
+
const auto b = val_at_offs<om_t>(
|
| 553 |
+
input, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 554 |
+
out[tid] = static_cast<res_t>(f(a, b));
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
template <typename T, typename T2, typename F>
|
| 558 |
+
kernel void binary_alpha_dense_scalar(
|
| 559 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 560 |
+
constant T* input [[buffer(1)]],
|
| 561 |
+
device T* scalar [[buffer(2)]],
|
| 562 |
+
constant T2& alpha [[buffer(3)]],
|
| 563 |
+
uint tid [[thread_position_in_grid]]) {
|
| 564 |
+
F f;
|
| 565 |
+
out[tid] = f(input[tid], scalar[0], alpha);
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
template <typename T, typename T2, typename F>
|
| 569 |
+
kernel void binary_alpha_dense_scalar_lhs(
|
| 570 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 571 |
+
device T* scalar [[buffer(1)]],
|
| 572 |
+
constant T* input [[buffer(2)]],
|
| 573 |
+
constant T2& alpha [[buffer(3)]],
|
| 574 |
+
uint tid [[thread_position_in_grid]]) {
|
| 575 |
+
F f;
|
| 576 |
+
out[tid] = f(scalar[0], input[tid], alpha);
|
| 577 |
+
}
|
| 578 |
+
|
| 579 |
+
template <typename T, typename T2, typename F>
|
| 580 |
+
kernel void binary_alpha_dense_scalar_cast(
|
| 581 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 582 |
+
constant void* input [[buffer(1)]],
|
| 583 |
+
device void* scalar [[buffer(2)]],
|
| 584 |
+
constant T2& alpha [[buffer(3)]],
|
| 585 |
+
constant uint4& sizes_types [[buffer(4)]],
|
| 586 |
+
uint tid [[thread_position_in_grid]]) {
|
| 587 |
+
F f;
|
| 588 |
+
const auto a = val_at_offs<T>(
|
| 589 |
+
input, tid * sizes_types.x, static_cast<ScalarType>(sizes_types.z));
|
| 590 |
+
const auto b =
|
| 591 |
+
val_at_offs<T>(scalar, 0, static_cast<ScalarType>(sizes_types.w));
|
| 592 |
+
out[tid] = f(a, b, alpha);
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
template <typename T, typename T2, typename F>
|
| 596 |
+
kernel void binary_alpha_dense_scalar_lhs_cast(
|
| 597 |
+
device result_of<F, T, T, T2>* out [[buffer(0)]],
|
| 598 |
+
device void* scalar [[buffer(1)]],
|
| 599 |
+
constant void* input [[buffer(2)]],
|
| 600 |
+
constant T2& alpha [[buffer(3)]],
|
| 601 |
+
constant uint4& sizes_types [[buffer(4)]],
|
| 602 |
+
uint tid [[thread_position_in_grid]]) {
|
| 603 |
+
F f;
|
| 604 |
+
const auto a =
|
| 605 |
+
val_at_offs<T>(scalar, 0, static_cast<ScalarType>(sizes_types.z));
|
| 606 |
+
const auto b = val_at_offs<T>(
|
| 607 |
+
input, tid * sizes_types.y, static_cast<ScalarType>(sizes_types.w));
|
| 608 |
+
out[tid] = f(a, b, alpha);
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
#define REGISTER_BINARY_OP_(NAME, DTYPEI, DTYPEO, OMT) \
|
| 612 |
+
static_assert( \
|
| 613 |
+
::metal::is_same_v< \
|
| 614 |
+
DTYPEO, \
|
| 615 |
+
::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI>>, \
|
| 616 |
+
"Output dtype mismatch for binary op " #NAME " and input " #DTYPEI); \
|
| 617 |
+
template [[host_name(#NAME "_strided_" #DTYPEO "_" #DTYPEI)]] kernel void :: \
|
| 618 |
+
c10::metal::binary_strided<DTYPEI, NAME##_functor, OMT>( \
|
| 619 |
+
device void* out, \
|
| 620 |
+
constant void* input, \
|
| 621 |
+
constant void* other, \
|
| 622 |
+
constant long* sizes, \
|
| 623 |
+
constant long* output_strides, \
|
| 624 |
+
constant long* input_strides, \
|
| 625 |
+
constant long* other_strides, \
|
| 626 |
+
constant uint3& ndim, \
|
| 627 |
+
uint tid); \
|
| 628 |
+
template [[host_name(#NAME "_strided_cast_" #DTYPEI)]] kernel void ::c10:: \
|
| 629 |
+
metal::binary_strided_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 630 |
+
device void* out, \
|
| 631 |
+
constant void* input, \
|
| 632 |
+
constant void* other, \
|
| 633 |
+
constant long* sizes, \
|
| 634 |
+
constant long* output_strides, \
|
| 635 |
+
constant long* input_strides, \
|
| 636 |
+
constant long* other_strides, \
|
| 637 |
+
constant uint4& ndim_types, \
|
| 638 |
+
uint tid); \
|
| 639 |
+
template [[host_name(#NAME "_dense_" #DTYPEO "_" #DTYPEI)]] kernel void :: \
|
| 640 |
+
c10::metal::binary_dense<DTYPEI, NAME##_functor, OMT>( \
|
| 641 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 642 |
+
out_, \
|
| 643 |
+
constant DTYPEI * input_, \
|
| 644 |
+
constant DTYPEI * other_, \
|
| 645 |
+
uint tid); \
|
| 646 |
+
template [[host_name(#NAME "_dense_cast_" #DTYPEI)]] kernel void ::c10:: \
|
| 647 |
+
metal::binary_dense_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 648 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 649 |
+
out_, \
|
| 650 |
+
constant void* input, \
|
| 651 |
+
constant void* other, \
|
| 652 |
+
constant uint4& sizes_types, \
|
| 653 |
+
uint tid); \
|
| 654 |
+
template [[host_name(#NAME "_dense_broadcast_" #DTYPEO "_" #DTYPEI)]] \
|
| 655 |
+
kernel void ::c10::metal:: \
|
| 656 |
+
binary_dense_broadcast<DTYPEI, NAME##_functor, OMT>( \
|
| 657 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 658 |
+
out_, \
|
| 659 |
+
constant DTYPEI * input_, \
|
| 660 |
+
constant DTYPEI * broadcast_, \
|
| 661 |
+
constant long& broadcast_numel, \
|
| 662 |
+
uint tid); \
|
| 663 |
+
template [[host_name(#NAME "_dense_broadcast_rhs_" #DTYPEO "_" #DTYPEI)]] \
|
| 664 |
+
kernel void ::c10::metal:: \
|
| 665 |
+
binary_dense_broadcast_rhs<DTYPEI, NAME##_functor, OMT>( \
|
| 666 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 667 |
+
out_, \
|
| 668 |
+
constant DTYPEI * broadcast_, \
|
| 669 |
+
constant DTYPEI * input_, \
|
| 670 |
+
constant long& broadcast_numel, \
|
| 671 |
+
uint tid); \
|
| 672 |
+
template [[host_name(#NAME "_dense_broadcast_cast_" #DTYPEI)]] \
|
| 673 |
+
kernel void ::c10::metal:: \
|
| 674 |
+
binary_dense_broadcast_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 675 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 676 |
+
out_, \
|
| 677 |
+
constant void* input_, \
|
| 678 |
+
constant void* broadcast_, \
|
| 679 |
+
constant long& broadcast_numel, \
|
| 680 |
+
constant uint4& sizes_types, \
|
| 681 |
+
uint tid); \
|
| 682 |
+
template [[host_name(#NAME "_dense_broadcast_rhs_cast_" #DTYPEI)]] \
|
| 683 |
+
kernel void ::c10::metal:: \
|
| 684 |
+
binary_dense_broadcast_rhs_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 685 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 686 |
+
out_, \
|
| 687 |
+
constant void* broadcast_, \
|
| 688 |
+
constant void* input_, \
|
| 689 |
+
constant long& broadcast_numel, \
|
| 690 |
+
constant uint4& sizes_types, \
|
| 691 |
+
uint tid); \
|
| 692 |
+
template [[host_name(#NAME "_dense_scalar_" #DTYPEO "_" #DTYPEI)]] \
|
| 693 |
+
kernel void ::c10::metal::binary_dense_scalar<DTYPEI, NAME##_functor, OMT>( \
|
| 694 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * out_, \
|
| 695 |
+
constant DTYPEI * input_, \
|
| 696 |
+
device DTYPEI * scalar_, \
|
| 697 |
+
uint tid); \
|
| 698 |
+
template [[host_name(#NAME "_dense_scalar_lhs_" #DTYPEO "_" #DTYPEI)]] \
|
| 699 |
+
kernel void ::c10::metal:: \
|
| 700 |
+
binary_dense_scalar_lhs<DTYPEI, NAME##_functor, OMT>( \
|
| 701 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 702 |
+
out_, \
|
| 703 |
+
device DTYPEI * scalar_, \
|
| 704 |
+
constant DTYPEI * input_, \
|
| 705 |
+
uint tid); \
|
| 706 |
+
template [[host_name(#NAME "_dense_scalar_cast_" #DTYPEI)]] \
|
| 707 |
+
kernel void ::c10::metal:: \
|
| 708 |
+
binary_dense_scalar_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 709 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 710 |
+
out_, \
|
| 711 |
+
constant void* input_, \
|
| 712 |
+
device void* scalar_, \
|
| 713 |
+
constant uint4& sizes_types, \
|
| 714 |
+
uint tid); \
|
| 715 |
+
template [[host_name(#NAME "_dense_scalar_lhs_cast_" #DTYPEI)]] \
|
| 716 |
+
kernel void ::c10::metal:: \
|
| 717 |
+
binary_dense_scalar_lhs_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 718 |
+
device ::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI> * \
|
| 719 |
+
out_, \
|
| 720 |
+
device void* scalar_, \
|
| 721 |
+
constant void* input_, \
|
| 722 |
+
constant uint4& sizes_types, \
|
| 723 |
+
uint tid)
|
| 724 |
+
|
| 725 |
+
// OpMath Binary Op promotes inputs to higher precision type before Functor call
|
| 726 |
+
#define REGISTER_OPMATH_BINARY_OP(NAME, DTYPEI, DTYPEO) \
|
| 727 |
+
REGISTER_BINARY_OP_(NAME, DTYPEI, DTYPEO, ::c10::metal::opmath_t<DTYPEI>)
|
| 728 |
+
|
| 729 |
+
#define REGISTER_BINARY_OP(NAME, DTYPEI, DTYPEO) \
|
| 730 |
+
REGISTER_BINARY_OP_(NAME, DTYPEI, DTYPEO, DTYPEI)
|
| 731 |
+
|
| 732 |
+
#define REGISTER_BINARY_ALPHA_OP(NAME, DTYPEI, DTYPEA, DTYPEO) \
|
| 733 |
+
static_assert( \
|
| 734 |
+
::metal::is_same_v< \
|
| 735 |
+
DTYPEO, \
|
| 736 |
+
::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA>>, \
|
| 737 |
+
"Output dtype mismatch for binary op " #NAME " and input " #DTYPEI); \
|
| 738 |
+
template [[host_name(#NAME "_strided_" #DTYPEO "_" #DTYPEI \
|
| 739 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 740 |
+
binary_alpha_strided<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 741 |
+
device void* out, \
|
| 742 |
+
constant void* input, \
|
| 743 |
+
constant void* other, \
|
| 744 |
+
constant DTYPEA& alpha, \
|
| 745 |
+
constant long* sizes, \
|
| 746 |
+
constant long* output_strides, \
|
| 747 |
+
constant long* input_strides, \
|
| 748 |
+
constant long* other_strides, \
|
| 749 |
+
constant uint3& ndim, \
|
| 750 |
+
uint tid); \
|
| 751 |
+
template [[host_name(#NAME "_strided_cast_" #DTYPEI \
|
| 752 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 753 |
+
binary_alpha_strided_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 754 |
+
device void* out, \
|
| 755 |
+
constant void* input, \
|
| 756 |
+
constant void* other, \
|
| 757 |
+
constant DTYPEA& alpha, \
|
| 758 |
+
constant long* sizes, \
|
| 759 |
+
constant long* output_strides, \
|
| 760 |
+
constant long* input_strides, \
|
| 761 |
+
constant long* other_strides, \
|
| 762 |
+
constant uint4& ndim_types, \
|
| 763 |
+
uint tid); \
|
| 764 |
+
template [[host_name(#NAME "_dense_" #DTYPEO "_" #DTYPEI \
|
| 765 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 766 |
+
binary_alpha_dense<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 767 |
+
device ::c10::metal:: \
|
| 768 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 769 |
+
out_, \
|
| 770 |
+
constant DTYPEI * input_, \
|
| 771 |
+
constant DTYPEI * other_, \
|
| 772 |
+
constant DTYPEA & alpha, \
|
| 773 |
+
uint tid); \
|
| 774 |
+
template \
|
| 775 |
+
[[host_name(#NAME "_dense_cast_" #DTYPEI "_" #DTYPEA)]] kernel void :: \
|
| 776 |
+
c10::metal::binary_alpha_dense_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 777 |
+
device ::c10::metal:: \
|
| 778 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 779 |
+
out_, \
|
| 780 |
+
constant void* input, \
|
| 781 |
+
constant void* other, \
|
| 782 |
+
constant DTYPEA& alpha, \
|
| 783 |
+
constant uint4& sizes_types, \
|
| 784 |
+
uint tid); \
|
| 785 |
+
template [[host_name(#NAME "_dense_broadcast_" #DTYPEO "_" #DTYPEI \
|
| 786 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 787 |
+
binary_alpha_dense_broadcast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 788 |
+
device ::c10::metal:: \
|
| 789 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 790 |
+
out_, \
|
| 791 |
+
constant DTYPEI * input_, \
|
| 792 |
+
constant DTYPEI * broadcast_, \
|
| 793 |
+
constant long& broadcast_numel, \
|
| 794 |
+
constant DTYPEA& alpha, \
|
| 795 |
+
uint tid); \
|
| 796 |
+
template [[host_name(#NAME "_dense_broadcast_rhs_" #DTYPEO "_" #DTYPEI \
|
| 797 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 798 |
+
binary_alpha_dense_broadcast_rhs<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 799 |
+
device ::c10::metal:: \
|
| 800 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 801 |
+
out_, \
|
| 802 |
+
constant DTYPEI * broadcast_, \
|
| 803 |
+
constant DTYPEI * input_, \
|
| 804 |
+
constant long& broadcast_numel, \
|
| 805 |
+
constant DTYPEA& alpha, \
|
| 806 |
+
uint tid); \
|
| 807 |
+
template [[host_name(#NAME "_dense_broadcast_cast_" #DTYPEI \
|
| 808 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 809 |
+
binary_alpha_dense_broadcast_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 810 |
+
device ::c10::metal:: \
|
| 811 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 812 |
+
out_, \
|
| 813 |
+
constant void* input_, \
|
| 814 |
+
constant void* broadcast_, \
|
| 815 |
+
constant long& broadcast_numel, \
|
| 816 |
+
constant DTYPEA& alpha, \
|
| 817 |
+
constant uint4& sizes_types, \
|
| 818 |
+
uint tid); \
|
| 819 |
+
template [[host_name(#NAME "_dense_broadcast_rhs_cast_" #DTYPEI \
|
| 820 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 821 |
+
binary_alpha_dense_broadcast_rhs_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 822 |
+
device ::c10::metal:: \
|
| 823 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 824 |
+
out_, \
|
| 825 |
+
constant void* broadcast_, \
|
| 826 |
+
constant void* input_, \
|
| 827 |
+
constant long& broadcast_numel, \
|
| 828 |
+
constant DTYPEA& alpha, \
|
| 829 |
+
constant uint4& sizes_types, \
|
| 830 |
+
uint tid); \
|
| 831 |
+
template [[host_name(#NAME "_dense_scalar_" #DTYPEO "_" #DTYPEI \
|
| 832 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 833 |
+
binary_alpha_dense_scalar<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 834 |
+
device ::c10::metal:: \
|
| 835 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 836 |
+
out_, \
|
| 837 |
+
constant DTYPEI * input_, \
|
| 838 |
+
device DTYPEI * scalar_, \
|
| 839 |
+
constant DTYPEA & alpha, \
|
| 840 |
+
uint tid); \
|
| 841 |
+
template [[host_name(#NAME "_dense_scalar_lhs_" #DTYPEO "_" #DTYPEI \
|
| 842 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 843 |
+
binary_alpha_dense_scalar_lhs<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 844 |
+
device ::c10::metal:: \
|
| 845 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 846 |
+
out_, \
|
| 847 |
+
device DTYPEI * scalar_, \
|
| 848 |
+
constant DTYPEI * input_, \
|
| 849 |
+
constant DTYPEA & alpha, \
|
| 850 |
+
uint tid); \
|
| 851 |
+
template [[host_name(#NAME "_dense_scalar_cast_" #DTYPEI \
|
| 852 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 853 |
+
binary_alpha_dense_scalar_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 854 |
+
device ::c10::metal:: \
|
| 855 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 856 |
+
out_, \
|
| 857 |
+
constant void* input_, \
|
| 858 |
+
device void* scalar_, \
|
| 859 |
+
constant DTYPEA& alpha, \
|
| 860 |
+
constant uint4& sizes_types, \
|
| 861 |
+
uint tid); \
|
| 862 |
+
template [[host_name(#NAME "_dense_scalar_lhs_cast_" #DTYPEI \
|
| 863 |
+
"_" #DTYPEA)]] kernel void ::c10::metal:: \
|
| 864 |
+
binary_alpha_dense_scalar_lhs_cast<DTYPEI, DTYPEA, NAME##_functor>( \
|
| 865 |
+
device ::c10::metal:: \
|
| 866 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEA> * \
|
| 867 |
+
out_, \
|
| 868 |
+
device void* scalar_, \
|
| 869 |
+
constant void* input_, \
|
| 870 |
+
constant DTYPEA& alpha, \
|
| 871 |
+
constant uint4& sizes_types, \
|
| 872 |
+
uint tid)
|
| 873 |
+
|
| 874 |
+
// Ternary elementwise ops kernels
|
| 875 |
+
// Right now there are 4 flavors available:
|
| 876 |
+
// - ternary_dense where both input, other1, other2, and output are dense and
|
| 877 |
+
// share the same type
|
| 878 |
+
// - ternary_strided when all inputs are of the same types, but some elements
|
| 879 |
+
// are strided
|
| 880 |
+
// - ternary_dense_cast - inputs are dense, but of different dtypes
|
| 881 |
+
// - ternary_strided_cast - inputs or output are strided and of different dtypes
|
| 882 |
+
// Note about accuracy (for more info see
|
| 883 |
+
// https://github.com/pytorch/pytorch/issues/152736) Sometimes when kernel is
|
| 884 |
+
// invoked to produce `half` output, but one of the arguments is float arguments
|
| 885 |
+
// should be upcast to float, rather than downcast to half At the moment this is
|
| 886 |
+
// expressed with `om_t` optional argument (which stands for opmath_type) which
|
| 887 |
+
// is identical to output type but could be something else
|
| 888 |
+
|
| 889 |
+
template <typename T, typename F, typename om_t = T>
|
| 890 |
+
kernel void ternary_strided(
|
| 891 |
+
device void* output [[buffer(0)]],
|
| 892 |
+
constant void* input [[buffer(1)]],
|
| 893 |
+
constant void* other1 [[buffer(2)]],
|
| 894 |
+
constant void* other2 [[buffer(3)]],
|
| 895 |
+
constant long* sizes [[buffer(4)]],
|
| 896 |
+
constant long* output_strides [[buffer(5)]],
|
| 897 |
+
constant long* input_strides [[buffer(6)]],
|
| 898 |
+
constant long* other1_strides [[buffer(7)]],
|
| 899 |
+
constant long* other2_strides [[buffer(8)]],
|
| 900 |
+
constant uint& ndim [[buffer(9)]],
|
| 901 |
+
constant uint4& types [[buffer(10)]],
|
| 902 |
+
uint index [[thread_position_in_grid]]) {
|
| 903 |
+
F f;
|
| 904 |
+
using res_t = result_of<F, T, T, T>;
|
| 905 |
+
int pos[max_ndim];
|
| 906 |
+
pos_from_thread_index(int(index), pos, sizes, ndim);
|
| 907 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim);
|
| 908 |
+
const auto other1_offs = offset_from_coord(pos, other1_strides, ndim);
|
| 909 |
+
const auto other2_offs = offset_from_coord(pos, other2_strides, ndim);
|
| 910 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim);
|
| 911 |
+
const auto a = val_at_offs<T>(input, input_offs);
|
| 912 |
+
const auto b = val_at_offs<T>(other1, other1_offs);
|
| 913 |
+
const auto c = val_at_offs<T>(other2, other2_offs);
|
| 914 |
+
ref_at_offs<res_t>(output, output_offs) =
|
| 915 |
+
static_cast<res_t>(f(om_t(a), om_t(b), om_t(c)));
|
| 916 |
+
}
|
| 917 |
+
|
| 918 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 919 |
+
kernel void ternary_strided_cast(
|
| 920 |
+
device void* output [[buffer(0)]],
|
| 921 |
+
constant void* input [[buffer(1)]],
|
| 922 |
+
constant void* other1 [[buffer(2)]],
|
| 923 |
+
constant void* other2 [[buffer(3)]],
|
| 924 |
+
constant long* sizes [[buffer(4)]],
|
| 925 |
+
constant long* output_strides [[buffer(5)]],
|
| 926 |
+
constant long* input_strides [[buffer(6)]],
|
| 927 |
+
constant long* other1_strides [[buffer(7)]],
|
| 928 |
+
constant long* other2_strides [[buffer(8)]],
|
| 929 |
+
constant uint& ndim [[buffer(9)]],
|
| 930 |
+
constant uint4& types [[buffer(10)]],
|
| 931 |
+
uint index [[thread_position_in_grid]]) {
|
| 932 |
+
F f;
|
| 933 |
+
using res_t = result_of<F, T, T, T>;
|
| 934 |
+
int pos[max_ndim];
|
| 935 |
+
pos_from_thread_index(int(index), pos, sizes, ndim);
|
| 936 |
+
const auto input_offs = offset_from_coord(pos, input_strides, ndim);
|
| 937 |
+
const auto other1_offs = offset_from_coord(pos, other1_strides, ndim);
|
| 938 |
+
const auto other2_offs = offset_from_coord(pos, other2_strides, ndim);
|
| 939 |
+
const auto output_offs = offset_from_coord(pos, output_strides, ndim);
|
| 940 |
+
const auto a =
|
| 941 |
+
val_at_offs<om_t>(input, input_offs, static_cast<ScalarType>(types.x));
|
| 942 |
+
const auto b =
|
| 943 |
+
val_at_offs<om_t>(other1, other1_offs, static_cast<ScalarType>(types.y));
|
| 944 |
+
const auto c =
|
| 945 |
+
val_at_offs<om_t>(other2, other2_offs, static_cast<ScalarType>(types.z));
|
| 946 |
+
ref_at_offs<res_t>(output, output_offs) = static_cast<res_t>(f(a, b, c));
|
| 947 |
+
}
|
| 948 |
+
|
| 949 |
+
template <typename T, typename F, typename om_t = opmath_t<T>>
|
| 950 |
+
kernel void ternary_dense(
|
| 951 |
+
device result_of<F, T, T, T>* out [[buffer(0)]],
|
| 952 |
+
constant T* input [[buffer(1)]],
|
| 953 |
+
constant T* other1 [[buffer(2)]],
|
| 954 |
+
constant T* other2 [[buffer(3)]],
|
| 955 |
+
uint tid [[thread_position_in_grid]]) {
|
| 956 |
+
F f;
|
| 957 |
+
using res_t = result_of<F, T, T, T>;
|
| 958 |
+
out[tid] = static_cast<res_t>(
|
| 959 |
+
f(om_t(input[tid]), om_t(other1[tid]), om_t(other2[tid])));
|
| 960 |
+
}
|
| 961 |
+
|
| 962 |
+
template <typename T, typename F, typename om_t = T>
|
| 963 |
+
kernel void ternary_dense_cast(
|
| 964 |
+
device result_of<F, T, T, T>* out [[buffer(0)]],
|
| 965 |
+
constant void* input [[buffer(1)]],
|
| 966 |
+
constant void* other1 [[buffer(2)]],
|
| 967 |
+
constant void* other2 [[buffer(3)]],
|
| 968 |
+
constant uint3& sizes [[buffer(4)]],
|
| 969 |
+
constant uint3& types [[buffer(5)]],
|
| 970 |
+
uint tid [[thread_position_in_grid]]) {
|
| 971 |
+
F f;
|
| 972 |
+
using res_t = result_of<F, T, T, T>;
|
| 973 |
+
const auto a =
|
| 974 |
+
val_at_offs<om_t>(input, tid * sizes.x, static_cast<ScalarType>(types.x));
|
| 975 |
+
const auto b = val_at_offs<om_t>(
|
| 976 |
+
other1, tid * sizes.y, static_cast<ScalarType>(types.y));
|
| 977 |
+
const auto c = val_at_offs<om_t>(
|
| 978 |
+
other2, tid * sizes.z, static_cast<ScalarType>(types.z));
|
| 979 |
+
out[tid] = static_cast<res_t>(f(a, b, c));
|
| 980 |
+
}
|
| 981 |
+
|
| 982 |
+
#define REGISTER_TERNARY_OP_(NAME, DTYPEI, DTYPEO, OMT) \
|
| 983 |
+
static_assert( \
|
| 984 |
+
::metal::is_same_v< \
|
| 985 |
+
DTYPEO, \
|
| 986 |
+
::c10::metal::result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEI>>, \
|
| 987 |
+
"Output dtype mismatch for ternary op " #NAME " and input " #DTYPEI); \
|
| 988 |
+
template [[host_name(#NAME "_strided_" #DTYPEO "_" #DTYPEI)]] kernel void :: \
|
| 989 |
+
c10::metal::ternary_strided<DTYPEI, NAME##_functor, OMT>( \
|
| 990 |
+
device void* out, \
|
| 991 |
+
constant void* input, \
|
| 992 |
+
constant void* other1, \
|
| 993 |
+
constant void* other2, \
|
| 994 |
+
constant long* sizes, \
|
| 995 |
+
constant long* output_strides, \
|
| 996 |
+
constant long* input_strides, \
|
| 997 |
+
constant long* other1_strides, \
|
| 998 |
+
constant long* other2_strides, \
|
| 999 |
+
constant uint& ndim, \
|
| 1000 |
+
constant uint4& types, \
|
| 1001 |
+
uint tid); \
|
| 1002 |
+
template [[host_name(#NAME "_strided_cast_" #DTYPEI)]] kernel void ::c10:: \
|
| 1003 |
+
metal::ternary_strided_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 1004 |
+
device void* out, \
|
| 1005 |
+
constant void* input, \
|
| 1006 |
+
constant void* other1, \
|
| 1007 |
+
constant void* other2, \
|
| 1008 |
+
constant long* sizes, \
|
| 1009 |
+
constant long* output_strides, \
|
| 1010 |
+
constant long* input_strides, \
|
| 1011 |
+
constant long* other1_strides, \
|
| 1012 |
+
constant long* other2_strides, \
|
| 1013 |
+
constant uint& ndim, \
|
| 1014 |
+
constant uint4& types, \
|
| 1015 |
+
uint tid); \
|
| 1016 |
+
template [[host_name(#NAME "_dense_" #DTYPEO "_" #DTYPEI)]] kernel void :: \
|
| 1017 |
+
c10::metal::ternary_dense<DTYPEI, NAME##_functor, OMT>( \
|
| 1018 |
+
device ::c10::metal:: \
|
| 1019 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEI> * \
|
| 1020 |
+
out_, \
|
| 1021 |
+
constant DTYPEI * input_, \
|
| 1022 |
+
constant DTYPEI * other1_, \
|
| 1023 |
+
constant DTYPEI * other2_, \
|
| 1024 |
+
uint tid); \
|
| 1025 |
+
template [[host_name(#NAME "_dense_cast_" #DTYPEI)]] kernel void ::c10:: \
|
| 1026 |
+
metal::ternary_dense_cast<DTYPEI, NAME##_functor, OMT>( \
|
| 1027 |
+
device ::c10::metal:: \
|
| 1028 |
+
result_of<NAME##_functor, DTYPEI, DTYPEI, DTYPEI> * \
|
| 1029 |
+
out_, \
|
| 1030 |
+
constant void* input, \
|
| 1031 |
+
constant void* other1, \
|
| 1032 |
+
constant void* other2, \
|
| 1033 |
+
constant uint3& sizes, \
|
| 1034 |
+
constant uint3& types, \
|
| 1035 |
+
uint tid)
|
| 1036 |
+
|
| 1037 |
+
// OpMath ternary Op promotes inputs to higher precision type before Functor
|
| 1038 |
+
// call
|
| 1039 |
+
#define REGISTER_OPMATH_TERNARY_OP(NAME, DTYPEI, DTYPEO) \
|
| 1040 |
+
REGISTER_TERNARY_OP_(NAME, DTYPEI, DTYPEO, ::c10::metal::opmath_t<DTYPEI>)
|
| 1041 |
+
|
| 1042 |
+
#define REGISTER_TERNARY_OP(NAME, DTYPEI, DTYPEO) \
|
| 1043 |
+
REGISTER_TERNARY_OP_(NAME, DTYPEI, DTYPEO, DTYPEI)
|
| 1044 |
+
|
| 1045 |
+
} // namespace metal
|
| 1046 |
+
} // namespace c10
|
| 1047 |
+
|
| 1048 |
+
#else
|
| 1049 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 1050 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/random.h
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Philox Counter based RNG implementation for Metal
|
| 3 |
+
// Borrowed from aten/src/ATen/core/PhiloxRNGEngine.h
|
| 4 |
+
// Which in turn borrowed from
|
| 5 |
+
// http://www.thesalmons.org/john/random123/papers/random123sc11.pdf
|
| 6 |
+
#pragma once
|
| 7 |
+
#include <metal_stdlib>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
namespace metal {
|
| 11 |
+
|
| 12 |
+
namespace detail {
|
| 13 |
+
|
| 14 |
+
constexpr float uint32_to_uniform_float(uint32_t value) {
|
| 15 |
+
// maximum value such that `MAX_INT * scale < 1.0` (with float rounding)
|
| 16 |
+
constexpr float scale = 4.6566127342e-10;
|
| 17 |
+
return static_cast<float>(value & 0x7FFFFFFF) * scale;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
inline uint2 splitlong(ulong v) {
|
| 21 |
+
return uint2(v >> 32, v & 0xffffffff);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
} // namespace detail
|
| 25 |
+
|
| 26 |
+
namespace philox4 {
|
| 27 |
+
|
| 28 |
+
uint2 mulhilo(uint a, uint b) {
|
| 29 |
+
auto rc = static_cast<ulong>(a) * b;
|
| 30 |
+
return detail::splitlong(rc);
|
| 31 |
+
}
|
| 32 |
+
uint4 single_round(uint4 ctr, uint2 key) {
|
| 33 |
+
constexpr uint kPhiloxSA = 0xD2511F53;
|
| 34 |
+
constexpr uint kPhiloxSB = 0xCD9E8D57;
|
| 35 |
+
auto rc0 = mulhilo(kPhiloxSA, ctr.x);
|
| 36 |
+
auto rc1 = mulhilo(kPhiloxSB, ctr.z);
|
| 37 |
+
return uint4(rc1.y ^ ctr.y ^ key.x, rc1.x, rc0.y ^ ctr.w ^ key.y, rc0.x);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
uint4 multiple_rounds(uint4 ctr, uint2 key, uint rounds) {
|
| 41 |
+
constexpr uint2 kPhilox10 = {0x9E3779B9, 0xBB67AE85};
|
| 42 |
+
for (uint round = 0; round < rounds - 1; ++round) {
|
| 43 |
+
ctr = single_round(ctr, key);
|
| 44 |
+
key += kPhilox10;
|
| 45 |
+
}
|
| 46 |
+
return ctr;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
uint4 rand(long seed, long index) {
|
| 50 |
+
uint4 ctr = 0;
|
| 51 |
+
ctr.zw = detail::splitlong(index);
|
| 52 |
+
return multiple_rounds(ctr, detail::splitlong(seed), 10);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
} // namespace philox4
|
| 56 |
+
|
| 57 |
+
float randn(long seed, long index) {
|
| 58 |
+
auto value = philox4::rand(seed, index);
|
| 59 |
+
float u1 = 1.0 - detail::uint32_to_uniform_float(value.x);
|
| 60 |
+
float u2 = 1.0 - detail::uint32_to_uniform_float(value.y);
|
| 61 |
+
return ::metal::sqrt(-2.0 * ::metal::log(u1)) *
|
| 62 |
+
::metal::cos(2.0 * M_PI_F * u2);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
float rand(long seed, long index) {
|
| 66 |
+
auto value = philox4::rand(seed, index);
|
| 67 |
+
return detail::uint32_to_uniform_float(value.x);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
long randint64(long seed, long index, long low, long high) {
|
| 71 |
+
auto range = high - low;
|
| 72 |
+
auto value = philox4::rand(seed, index);
|
| 73 |
+
// TODO: Implement better algorithm for large ranges
|
| 74 |
+
return low +
|
| 75 |
+
static_cast<long>(detail::uint32_to_uniform_float(value.x) * range);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
} // namespace metal
|
| 79 |
+
} // namespace c10
|
| 80 |
+
|
| 81 |
+
#else
|
| 82 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or T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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/metal/utils.h>
|
| 5 |
+
#include <metal_compute>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
namespace metal {
|
| 9 |
+
namespace detail {
|
| 10 |
+
template <typename T>
|
| 11 |
+
struct simd_type {
|
| 12 |
+
using t = T;
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
// Helper that allows one to run simd ops over bfl16 by upcasting them to fp32
|
| 16 |
+
template <typename T>
|
| 17 |
+
using simd_type_t = typename simd_type<T>::t;
|
| 18 |
+
|
| 19 |
+
template <>
|
| 20 |
+
struct simd_type<bfloat> {
|
| 21 |
+
using t = float;
|
| 22 |
+
};
|
| 23 |
+
} // namespace detail
|
| 24 |
+
|
| 25 |
+
template <typename T>
|
| 26 |
+
inline ::metal::enable_if_t<!::metal::is_same_v<T, long>, T> simd_sum(T val) {
|
| 27 |
+
return T(::metal::simd_sum(detail::simd_type_t<T>(val)));
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
template <typename T>
|
| 31 |
+
inline ::metal::enable_if_t<!::metal::is_same_v<T, long>, T> simd_prod(T val) {
|
| 32 |
+
return T(::metal::simd_product(detail::simd_type_t<T>(val)));
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Extend simd_broadcast to 64-bit integral types using int2 trick
|
| 36 |
+
template <
|
| 37 |
+
typename T,
|
| 38 |
+
::metal::enable_if_t<::metal::is_integral_v<T> && sizeof(T) == 8, bool> =
|
| 39 |
+
true>
|
| 40 |
+
inline T simd_broadcast(T val, ushort lane_id) {
|
| 41 |
+
return as_type<T>(::metal::simd_broadcast(as_type<int2>(val), lane_id));
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
template <
|
| 45 |
+
typename T,
|
| 46 |
+
::metal::enable_if_t<!::metal::is_integral_v<T> || sizeof(T) != 8, bool> =
|
| 47 |
+
true>
|
| 48 |
+
inline T simd_broadcast(T val, ushort lane_id) {
|
| 49 |
+
return ::metal::simd_broadcast(val, lane_id);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// Floating simd_min/max with nan propagation
|
| 53 |
+
template <
|
| 54 |
+
typename T,
|
| 55 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, bool> = true>
|
| 56 |
+
inline T simd_max(T val) {
|
| 57 |
+
if (::metal::simd_any(::metal::isnan(val))) {
|
| 58 |
+
return ::metal::numeric_limits<T>::quiet_NaN();
|
| 59 |
+
}
|
| 60 |
+
return T(::metal::simd_max(detail::simd_type_t<T>(val)));
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
template <
|
| 64 |
+
typename T,
|
| 65 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, bool> = true>
|
| 66 |
+
inline T simd_min(T val) {
|
| 67 |
+
if (::metal::simd_any(::metal::isnan(val))) {
|
| 68 |
+
return ::metal::numeric_limits<T>::quiet_NaN();
|
| 69 |
+
}
|
| 70 |
+
return T(::metal::simd_min(detail::simd_type_t<T>(val)));
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
template <
|
| 74 |
+
typename T,
|
| 75 |
+
::metal::enable_if_t<::metal::is_integral_v<T> && sizeof(T) != 8, bool> =
|
| 76 |
+
true>
|
| 77 |
+
inline T simd_max(T val) {
|
| 78 |
+
return ::metal::simd_max(val);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <
|
| 82 |
+
typename T,
|
| 83 |
+
::metal::enable_if_t<::metal::is_integral_v<T> && sizeof(T) != 8, bool> =
|
| 84 |
+
true>
|
| 85 |
+
inline T simd_min(T val) {
|
| 86 |
+
return ::metal::simd_min(val);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// Metal does not support SIMD reductions over 64-bit types, but it could be
|
| 90 |
+
// implement using simd_shuffle_down, that yields result in log2(simdgroup_size)
|
| 91 |
+
// iterations Use fill variant, as shuffle down returns garbage if inactive
|
| 92 |
+
// thread is referenced (on M1/M2, works fine on M4) and broadcast result to all
|
| 93 |
+
// threads in the end. Implementation heavily borrows from
|
| 94 |
+
// https://github.com/ml-explore/mlx/blob/86389bf9707f46101af45d90510e8e97c8a90b93/mlx/backend/metal/kernels/reduction/ops.h#L16
|
| 95 |
+
template <typename T>
|
| 96 |
+
inline ::metal::enable_if_t<::metal::is_same_v<T, long>, T> simd_sum(T val) {
|
| 97 |
+
for (ushort i = simdgroup_size / 2; i > 0; i /= 2) {
|
| 98 |
+
val += as_type<T>(
|
| 99 |
+
::metal::simd_shuffle_and_fill_down(as_type<int2>(val), int2(0), i));
|
| 100 |
+
}
|
| 101 |
+
return simd_broadcast(val, 0);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
template <typename T>
|
| 105 |
+
inline ::metal::enable_if_t<::metal::is_same_v<T, long>, T> simd_prod(T val) {
|
| 106 |
+
for (ushort i = simdgroup_size / 2; i > 0; i /= 2) {
|
| 107 |
+
val *= as_type<T>(
|
| 108 |
+
::metal::simd_shuffle_and_fill_down(as_type<int2>(val), int2(0), i));
|
| 109 |
+
}
|
| 110 |
+
return simd_broadcast(val, 0);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
template <typename T>
|
| 114 |
+
inline ::metal::enable_if_t<::metal::is_same_v<T, long>, T> simd_max(T val) {
|
| 115 |
+
for (ushort i = simdgroup_size / 2; i > 0; i /= 2) {
|
| 116 |
+
val = ::metal::max(
|
| 117 |
+
val,
|
| 118 |
+
as_type<T>(::metal::simd_shuffle_and_fill_down(
|
| 119 |
+
as_type<int2>(val), int2(0), i)));
|
| 120 |
+
}
|
| 121 |
+
return simd_broadcast(val, 0);
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
template <typename T>
|
| 125 |
+
inline ::metal::enable_if_t<::metal::is_same_v<T, long>, T> simd_min(T val) {
|
| 126 |
+
for (ushort i = simdgroup_size / 2; i > 0; i /= 2) {
|
| 127 |
+
val = ::metal::min(
|
| 128 |
+
val,
|
| 129 |
+
as_type<T>(::metal::simd_shuffle_and_fill_down(
|
| 130 |
+
as_type<int2>(val), int2(0), i)));
|
| 131 |
+
}
|
| 132 |
+
return simd_broadcast(val, 0);
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// argmin/argmax helpers using simd_ballot
|
| 136 |
+
template <
|
| 137 |
+
typename T,
|
| 138 |
+
::metal::enable_if_t<::metal::is_integral_v<T>, bool> = true>
|
| 139 |
+
inline ::c10::metal::pair<T, ushort> simd_argmin(T val) {
|
| 140 |
+
const auto rc = simd_min(val);
|
| 141 |
+
const auto vote = ::metal::simd_ballot(val == rc);
|
| 142 |
+
return {rc, static_cast<ushort>(::metal::ctz(static_cast<ulong>(vote)))};
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
template <
|
| 146 |
+
typename T,
|
| 147 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, bool> = true>
|
| 148 |
+
inline ::c10::metal::pair<T, ushort> simd_argmin(T val) {
|
| 149 |
+
const auto rc = simd_min(val);
|
| 150 |
+
const auto vote = ::metal::simd_ballot(val == rc || ::metal::isnan(val));
|
| 151 |
+
return {rc, static_cast<ushort>(::metal::ctz(static_cast<ulong>(vote)))};
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
template <
|
| 155 |
+
typename T,
|
| 156 |
+
::metal::enable_if_t<::metal::is_integral_v<T>, bool> = true>
|
| 157 |
+
inline ::c10::metal::pair<T, ushort> simd_argmax(T val) {
|
| 158 |
+
const auto rc = simd_max(val);
|
| 159 |
+
const auto vote = ::metal::simd_ballot(val == rc);
|
| 160 |
+
return {rc, static_cast<ushort>(::metal::ctz(static_cast<ulong>(vote)))};
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
template <
|
| 164 |
+
typename T,
|
| 165 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, bool> = true>
|
| 166 |
+
inline ::c10::metal::pair<T, ushort> simd_argmax(T val) {
|
| 167 |
+
const auto rc = simd_max(val);
|
| 168 |
+
const auto vote = ::metal::simd_ballot(val == rc || ::metal::isnan(val));
|
| 169 |
+
return {rc, static_cast<ushort>(::metal::ctz(static_cast<ulong>(vote)))};
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
template <typename ARG_T, typename IDX_T>
|
| 173 |
+
inline c10::metal::pair<ARG_T, IDX_T> simd_argmin(ARG_T val, IDX_T idx_val) {
|
| 174 |
+
auto rc = simd_argmin(val);
|
| 175 |
+
return {rc.first, simd_broadcast(idx_val, rc.second)};
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
template <typename ARG_T, typename IDX_T>
|
| 179 |
+
inline c10::metal::pair<ARG_T, IDX_T> simd_argmax(ARG_T val, IDX_T idx_val) {
|
| 180 |
+
auto rc = simd_argmax(val);
|
| 181 |
+
return {rc.first, simd_broadcast(idx_val, rc.second)};
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// Below algorithms are written with hardcoded assumption that simdgroup is 32
|
| 185 |
+
// and threadgroup_max is 1024, i.e. reduction can be done in two stages max
|
| 186 |
+
template <typename T>
|
| 187 |
+
opmath_t<T> threadgroup_sum(
|
| 188 |
+
threadgroup opmath_t<T>* data,
|
| 189 |
+
T val,
|
| 190 |
+
unsigned idx,
|
| 191 |
+
unsigned size) {
|
| 192 |
+
auto rc = simd_sum(static_cast<opmath_t<T>>(val));
|
| 193 |
+
if (idx % simdgroup_size == 0) {
|
| 194 |
+
data[idx / simdgroup_size] = rc;
|
| 195 |
+
}
|
| 196 |
+
if (size > simdgroup_size) {
|
| 197 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 198 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 199 |
+
auto rc1 = simd_sum(data[idx]);
|
| 200 |
+
if (idx == 0) {
|
| 201 |
+
data[0] = rc1;
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 206 |
+
return data[0];
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
template <typename T>
|
| 210 |
+
opmath_t<T> threadgroup_prod(
|
| 211 |
+
threadgroup opmath_t<T>* data,
|
| 212 |
+
T val,
|
| 213 |
+
unsigned idx,
|
| 214 |
+
unsigned size) {
|
| 215 |
+
auto rc = simd_prod(static_cast<opmath_t<T>>(val));
|
| 216 |
+
if (idx % simdgroup_size == 0) {
|
| 217 |
+
data[idx / simdgroup_size] = rc;
|
| 218 |
+
}
|
| 219 |
+
if (size > simdgroup_size) {
|
| 220 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 221 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 222 |
+
auto rc1 = simd_prod(data[idx]);
|
| 223 |
+
if (idx == 0) {
|
| 224 |
+
data[0] = rc1;
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 229 |
+
return data[0];
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
template <typename T>
|
| 233 |
+
T threadgroup_max(threadgroup T* data, T val, unsigned idx, unsigned size) {
|
| 234 |
+
auto rc = simd_max(val);
|
| 235 |
+
if (idx % simdgroup_size == 0) {
|
| 236 |
+
data[idx / simdgroup_size] = rc;
|
| 237 |
+
}
|
| 238 |
+
if (size > simdgroup_size) {
|
| 239 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 240 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 241 |
+
auto rc1 = simd_max(data[idx]);
|
| 242 |
+
if (idx == 0) {
|
| 243 |
+
data[0] = rc1;
|
| 244 |
+
}
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 248 |
+
return data[0];
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
template <typename T>
|
| 252 |
+
T threadgroup_min(threadgroup T* data, T val, unsigned idx, unsigned size) {
|
| 253 |
+
auto rc = simd_min(val);
|
| 254 |
+
if (idx % simdgroup_size == 0) {
|
| 255 |
+
data[idx / simdgroup_size] = rc;
|
| 256 |
+
}
|
| 257 |
+
if (size > simdgroup_size) {
|
| 258 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 259 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 260 |
+
auto rc1 = simd_min(data[idx]);
|
| 261 |
+
if (idx == 0) {
|
| 262 |
+
data[0] = rc1;
|
| 263 |
+
}
|
| 264 |
+
}
|
| 265 |
+
}
|
| 266 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 267 |
+
return data[0];
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
template <typename T>
|
| 271 |
+
float3 threadgroup_welford_reduce(threadgroup T* data, unsigned size) {
|
| 272 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 273 |
+
float m = data[0];
|
| 274 |
+
float m2 = 0;
|
| 275 |
+
for (unsigned idx = 1; idx < size; ++idx) {
|
| 276 |
+
float delta = data[idx] - m;
|
| 277 |
+
m += delta / (idx + 1);
|
| 278 |
+
m2 += delta * (data[idx] - m);
|
| 279 |
+
}
|
| 280 |
+
return float3(m, m2, size);
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
// Each vec3type is tuple of mean, m2 and weight
|
| 284 |
+
template <typename T>
|
| 285 |
+
float3 welford_combine(T a, T b) {
|
| 286 |
+
float delta = b.x - a.x;
|
| 287 |
+
float new_weight = a.z + b.z;
|
| 288 |
+
auto w2_over_w = new_weight != 0 ? b.z / new_weight : 0.0;
|
| 289 |
+
return float3(
|
| 290 |
+
a.x + delta * w2_over_w,
|
| 291 |
+
a.y + b.y + delta * delta * a.z * w2_over_w,
|
| 292 |
+
new_weight);
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
template <typename T>
|
| 296 |
+
float3 threadgroup_welford_combine(threadgroup T* data, unsigned size) {
|
| 297 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 298 |
+
float3 rc = data[0];
|
| 299 |
+
for (unsigned idx = 1; idx < size; ++idx) {
|
| 300 |
+
rc = welford_combine(rc, data[idx]);
|
| 301 |
+
}
|
| 302 |
+
return rc;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
template <typename ARG_T, typename IDX_T>
|
| 306 |
+
IDX_T threadgroup_argmax(
|
| 307 |
+
threadgroup ARG_T* arg_data,
|
| 308 |
+
threadgroup IDX_T* idx_data,
|
| 309 |
+
ARG_T val,
|
| 310 |
+
IDX_T idx_val,
|
| 311 |
+
unsigned idx,
|
| 312 |
+
unsigned size) {
|
| 313 |
+
auto rc = simd_argmax(val, idx_val);
|
| 314 |
+
if (size <= simdgroup_size) {
|
| 315 |
+
return rc.second;
|
| 316 |
+
}
|
| 317 |
+
if (idx % simdgroup_size == 0) {
|
| 318 |
+
arg_data[idx / simdgroup_size] = rc.first;
|
| 319 |
+
idx_data[idx / simdgroup_size] = rc.second;
|
| 320 |
+
}
|
| 321 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 322 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 323 |
+
auto rc1 = simd_argmax(arg_data[idx], idx_data[idx]);
|
| 324 |
+
if (idx == 0) {
|
| 325 |
+
idx_data[0] = rc1.second;
|
| 326 |
+
}
|
| 327 |
+
}
|
| 328 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 329 |
+
return idx_data[0];
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
template <typename ARG_T, typename IDX_T>
|
| 333 |
+
IDX_T threadgroup_argmin(
|
| 334 |
+
threadgroup ARG_T* arg_data,
|
| 335 |
+
threadgroup IDX_T* idx_data,
|
| 336 |
+
ARG_T val,
|
| 337 |
+
IDX_T idx_val,
|
| 338 |
+
unsigned idx,
|
| 339 |
+
unsigned size) {
|
| 340 |
+
auto rc = simd_argmin(val, idx_val);
|
| 341 |
+
if (size <= simdgroup_size) {
|
| 342 |
+
return rc.second;
|
| 343 |
+
}
|
| 344 |
+
if (idx % simdgroup_size == 0) {
|
| 345 |
+
arg_data[idx / simdgroup_size] = rc.first;
|
| 346 |
+
idx_data[idx / simdgroup_size] = rc.second;
|
| 347 |
+
}
|
| 348 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 349 |
+
if (idx < ((size + simdgroup_size - 1) / simdgroup_size)) {
|
| 350 |
+
auto rc1 = simd_argmin(arg_data[idx], idx_data[idx]);
|
| 351 |
+
if (idx == 0) {
|
| 352 |
+
idx_data[0] = rc1.second;
|
| 353 |
+
}
|
| 354 |
+
}
|
| 355 |
+
::metal::threadgroup_barrier(::metal::mem_flags::mem_threadgroup);
|
| 356 |
+
return idx_data[0];
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
} // namespace metal
|
| 360 |
+
} // namespace c10
|
| 361 |
+
|
| 362 |
+
#else
|
| 363 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 364 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Implementation of special math functions for Metal
|
| 3 |
+
#pragma once
|
| 4 |
+
#include <c10/metal/expm1f.h>
|
| 5 |
+
#include <c10/metal/igamma.h>
|
| 6 |
+
#include <c10/metal/utils.h>
|
| 7 |
+
#include <metal_stdlib>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
namespace metal {
|
| 11 |
+
|
| 12 |
+
/*
|
| 13 |
+
* Approximation to the error function.
|
| 14 |
+
* Based on code from:
|
| 15 |
+
* https://stackoverflow.com/questions/35148198/efficient-faithfully-rounded-implementation-of-error-function-erff#answer-35148199
|
| 16 |
+
* Copy-n-pasted from
|
| 17 |
+
* https://github.com/ml-explore/mlx/blob/2e8cf0b4506c200a5c2d199ecbbf655fdf4c2ce2/mlx/backend/metal/kernels/erf.h#L11
|
| 18 |
+
*/
|
| 19 |
+
template <typename T>
|
| 20 |
+
inline float erf(T x) {
|
| 21 |
+
const auto a = static_cast<float>(x);
|
| 22 |
+
const auto t = ::metal::abs(a);
|
| 23 |
+
const auto s = a * a;
|
| 24 |
+
if (t > 0.927734375f) {
|
| 25 |
+
// maximum error 0.99527 ulp
|
| 26 |
+
auto r = ::metal::fma(
|
| 27 |
+
-1.72853470e-5f, t, 3.83197126e-4f); // -0x1.220000p-16,0x1.91cfb2p-12
|
| 28 |
+
const auto u = ::metal::fma(
|
| 29 |
+
-3.88396438e-3f, t, 2.42546219e-2f); // -0x1.fd1438p-9, 0x1.8d6342p-6
|
| 30 |
+
r = ::metal::fma(r, s, u);
|
| 31 |
+
r = ::metal::fma(r, t, -1.06777877e-1f); // -0x1.b55cb8p-4
|
| 32 |
+
r = ::metal::fma(r, t, -6.34846687e-1f); // -0x1.450aa0p-1
|
| 33 |
+
r = ::metal::fma(r, t, -1.28717512e-1f); // -0x1.079d0cp-3
|
| 34 |
+
r = ::metal::fma(r, t, -t);
|
| 35 |
+
// TODO, replace with expm1 when implemented
|
| 36 |
+
r = 1.0f - ::metal::exp(r);
|
| 37 |
+
r = ::metal::copysign(r, a);
|
| 38 |
+
return r;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// maximum error 0.98929 ulp
|
| 42 |
+
auto r = -5.96761703e-4f; // -0x1.38e000p-11
|
| 43 |
+
r = ::metal::fma(r, s, 4.99119423e-3f); // 0x1.471a58p-8
|
| 44 |
+
r = ::metal::fma(r, s, -2.67681349e-2f); // -0x1.b691b2p-6
|
| 45 |
+
r = ::metal::fma(r, s, 1.12819925e-1f); // 0x1.ce1c44p-4
|
| 46 |
+
r = ::metal::fma(r, s, -3.76125336e-1f); // -0x1.812700p-2
|
| 47 |
+
r = ::metal::fma(r, s, 1.28379166e-1f); // 0x1.06eba8p-3
|
| 48 |
+
r = ::metal::fma(r, a, a);
|
| 49 |
+
return r;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
template <typename T>
|
| 53 |
+
float erfc(T x) {
|
| 54 |
+
return 1.0 - erf(x);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
template <typename T>
|
| 58 |
+
inline float erfinv(T y) {
|
| 59 |
+
/* coefficients in rational expansion */
|
| 60 |
+
constexpr float a[4] = {0.886226899, -1.645349621, 0.914624893, -0.140543331};
|
| 61 |
+
constexpr float b[4] = {-2.118377725, 1.442710462, -0.329097515, 0.012229801};
|
| 62 |
+
constexpr float c[4] = {-1.970840454, -1.624906493, 3.429567803, 1.641345311};
|
| 63 |
+
constexpr float d[2] = {3.543889200, 1.637067800};
|
| 64 |
+
|
| 65 |
+
float x, z, num, dem; /*working variables */
|
| 66 |
+
|
| 67 |
+
float y_abs = ::metal::abs(static_cast<float>(y));
|
| 68 |
+
if (y_abs >= 1.0f) {
|
| 69 |
+
return y_abs > 1.0f ? NAN
|
| 70 |
+
: ::metal::copysign(INFINITY, static_cast<float>(y));
|
| 71 |
+
}
|
| 72 |
+
if (y_abs <= 0.7f) {
|
| 73 |
+
z = y * y;
|
| 74 |
+
num = ((a[3] * z + a[2]) * z + a[1]) * z + a[0];
|
| 75 |
+
dem = (((b[3] * z + b[2]) * z + b[1]) * z + b[0]) * z + 1.0f;
|
| 76 |
+
x = y * num / dem;
|
| 77 |
+
} else {
|
| 78 |
+
z = ::metal::sqrt(-1.0f * ::metal::log((1.0 - y_abs) / 2.0));
|
| 79 |
+
num = ((c[3] * z + c[2]) * z + c[1]) * z + c[0];
|
| 80 |
+
dem = (d[1] * z + d[0]) * z + 1.0f;
|
| 81 |
+
x = ::metal::copysign(num, static_cast<float>(y)) / dem;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return x;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/*
|
| 88 |
+
* For licensing information and documentation, please refer to the cpu
|
| 89 |
+
* implementation located in "ATen/native/Math.h".
|
| 90 |
+
*/
|
| 91 |
+
|
| 92 |
+
template <typename T>
|
| 93 |
+
inline T chbevl(T x, const float array[], const int len) {
|
| 94 |
+
T b0, b1, b2;
|
| 95 |
+
|
| 96 |
+
b0 = array[0];
|
| 97 |
+
b1 = 0;
|
| 98 |
+
|
| 99 |
+
for (int i = 1; i < len; ++i) {
|
| 100 |
+
b2 = b1;
|
| 101 |
+
b1 = b0;
|
| 102 |
+
b0 = x * b1 - b2 + array[i];
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
return T{0.5} * (b0 - b2);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// Copied from
|
| 109 |
+
// https://github.com/pytorch/pytorch/blob/58b661cda2c002a8e1ac3bee494bfe1f7420437c/aten/src/ATen/native/cuda/Math.cuh#L502
|
| 110 |
+
|
| 111 |
+
template <typename T>
|
| 112 |
+
inline T i0(T _x) {
|
| 113 |
+
auto x = ::metal::fabs(_x);
|
| 114 |
+
|
| 115 |
+
if (x <= 8.0) {
|
| 116 |
+
/* Chebyshev coefficients for exp(-x) I0(x)
|
| 117 |
+
* in the interval [0,8].
|
| 118 |
+
*
|
| 119 |
+
* lim(x->0){ exp(-x) I0(x) } = 1.
|
| 120 |
+
*/
|
| 121 |
+
constexpr float A[] = {
|
| 122 |
+
-4.41534164647933937950E-18, 3.33079451882223809783E-17,
|
| 123 |
+
-2.43127984654795469359E-16, 1.71539128555513303061E-15,
|
| 124 |
+
-1.16853328779934516808E-14, 7.67618549860493561688E-14,
|
| 125 |
+
-4.85644678311192946090E-13, 2.95505266312963983461E-12,
|
| 126 |
+
-1.72682629144155570723E-11, 9.67580903537323691224E-11,
|
| 127 |
+
-5.18979560163526290666E-10, 2.65982372468238665035E-9,
|
| 128 |
+
-1.30002500998624804212E-8, 6.04699502254191894932E-8,
|
| 129 |
+
-2.67079385394061173391E-7, 1.11738753912010371815E-6,
|
| 130 |
+
-4.41673835845875056359E-6, 1.64484480707288970893E-5,
|
| 131 |
+
-5.75419501008210370398E-5, 1.88502885095841655729E-4,
|
| 132 |
+
-5.76375574538582365885E-4, 1.63947561694133579842E-3,
|
| 133 |
+
-4.32430999505057594430E-3, 1.05464603945949983183E-2,
|
| 134 |
+
-2.37374148058994688156E-2, 4.93052842396707084878E-2,
|
| 135 |
+
-9.49010970480476444210E-2, 1.71620901522208775349E-1,
|
| 136 |
+
-3.04682672343198398683E-1, 6.76795274409476084995E-1};
|
| 137 |
+
|
| 138 |
+
auto y = (x / 2.0) - 2.0;
|
| 139 |
+
return static_cast<T>(::metal::exp(x) * chbevl(y, A, 30));
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Handles x > 8 case
|
| 143 |
+
/* Chebyshev coefficients for exp(-x) sqrt(x) I0(x)
|
| 144 |
+
* in the inverted interval [8,infinity].
|
| 145 |
+
*
|
| 146 |
+
* lim(x->inf){ exp(-x) sqrt(x) I0(x) } = 1/sqrt(2pi).
|
| 147 |
+
*/
|
| 148 |
+
constexpr float B[] = {
|
| 149 |
+
-7.23318048787475395456E-18, -4.83050448594418207126E-18,
|
| 150 |
+
4.46562142029675999901E-17, 3.46122286769746109310E-17,
|
| 151 |
+
-2.82762398051658348494E-16, -3.42548561967721913462E-16,
|
| 152 |
+
1.77256013305652638360E-15, 3.81168066935262242075E-15,
|
| 153 |
+
-9.55484669882830764870E-15, -4.15056934728722208663E-14,
|
| 154 |
+
1.54008621752140982691E-14, 3.85277838274214270114E-13,
|
| 155 |
+
7.18012445138366623367E-13, -1.79417853150680611778E-12,
|
| 156 |
+
-1.32158118404477131188E-11, -3.14991652796324136454E-11,
|
| 157 |
+
1.18891471078464383424E-11, 4.94060238822496958910E-10,
|
| 158 |
+
3.39623202570838634515E-9, 2.26666899049817806459E-8,
|
| 159 |
+
2.04891858946906374183E-7, 2.89137052083475648297E-6,
|
| 160 |
+
6.88975834691682398426E-5, 3.36911647825569408990E-3,
|
| 161 |
+
8.04490411014108831608E-1};
|
| 162 |
+
|
| 163 |
+
return static_cast<T>(
|
| 164 |
+
(::metal::exp(x) * chbevl(32.0 / x - 2.0, B, 25)) / ::metal::sqrt(x));
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
template <typename T>
|
| 168 |
+
inline T i0e(T _x) {
|
| 169 |
+
auto x = ::metal::fabs(_x);
|
| 170 |
+
|
| 171 |
+
if (x <= 8.0) {
|
| 172 |
+
constexpr float coefficients[] = {
|
| 173 |
+
-4.41534164647933937950E-18, 3.33079451882223809783E-17,
|
| 174 |
+
-2.43127984654795469359E-16, 1.71539128555513303061E-15,
|
| 175 |
+
-1.16853328779934516808E-14, 7.67618549860493561688E-14,
|
| 176 |
+
-4.85644678311192946090E-13, 2.95505266312963983461E-12,
|
| 177 |
+
-1.72682629144155570723E-11, 9.67580903537323691224E-11,
|
| 178 |
+
-5.18979560163526290666E-10, 2.65982372468238665035E-9,
|
| 179 |
+
-1.30002500998624804212E-8, 6.04699502254191894932E-8,
|
| 180 |
+
-2.67079385394061173391E-7, 1.11738753912010371815E-6,
|
| 181 |
+
-4.41673835845875056359E-6, 1.64484480707288970893E-5,
|
| 182 |
+
-5.75419501008210370398E-5, 1.88502885095841655729E-4,
|
| 183 |
+
-5.76375574538582365885E-4, 1.63947561694133579842E-3,
|
| 184 |
+
-4.32430999505057594430E-3, 1.05464603945949983183E-2,
|
| 185 |
+
-2.37374148058994688156E-2, 4.93052842396707084878E-2,
|
| 186 |
+
-9.49010970480476444210E-2, 1.71620901522208775349E-1,
|
| 187 |
+
-3.04682672343198398683E-1, 6.76795274409476084995E-1};
|
| 188 |
+
|
| 189 |
+
auto y = (x / 2.0) - 2.0;
|
| 190 |
+
return static_cast<T>(chbevl(y, coefficients, int{30}));
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// x > 8
|
| 194 |
+
constexpr float coefficients[] = {
|
| 195 |
+
-7.23318048787475395456E-18, -4.83050448594418207126E-18,
|
| 196 |
+
4.46562142029675999901E-17, 3.46122286769746109310E-17,
|
| 197 |
+
-2.82762398051658348494E-16, -3.42548561967721913462E-16,
|
| 198 |
+
1.77256013305652638360E-15, 3.81168066935262242075E-15,
|
| 199 |
+
-9.55484669882830764870E-15, -4.15056934728722208663E-14,
|
| 200 |
+
1.54008621752140982691E-14, 3.85277838274214270114E-13,
|
| 201 |
+
7.18012445138366623367E-13, -1.79417853150680611778E-12,
|
| 202 |
+
-1.32158118404477131188E-11, -3.14991652796324136454E-11,
|
| 203 |
+
1.18891471078464383424E-11, 4.94060238822496958910E-10,
|
| 204 |
+
3.39623202570838634515E-9, 2.26666899049817806459E-8,
|
| 205 |
+
2.04891858946906374183E-7, 2.89137052083475648297E-6,
|
| 206 |
+
6.88975834691682398426E-5, 3.36911647825569408990E-3,
|
| 207 |
+
8.04490411014108831608E-1};
|
| 208 |
+
|
| 209 |
+
return static_cast<T>(
|
| 210 |
+
chbevl(32.0 / x - 2.0, coefficients, 25) / ::metal::sqrt(x));
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// Copied from
|
| 214 |
+
// https://github.com/pytorch/pytorch/blob/58b661cda2c002a8e1ac3bee494bfe1f7420437c/aten/src/ATen/native/cuda/Math.cuh#L576
|
| 215 |
+
|
| 216 |
+
template <typename T>
|
| 217 |
+
inline T i1(T _x) {
|
| 218 |
+
const auto x = ::metal::fabs(_x);
|
| 219 |
+
|
| 220 |
+
if (x <= 8.0) {
|
| 221 |
+
// Chebyshev coefficients for exp(-x) i1(x) in the internal [0, 8]
|
| 222 |
+
// lim(x->0){ exp(-x) i1(x) / x } = 1/2
|
| 223 |
+
constexpr float coefficients[] = {
|
| 224 |
+
2.77791411276104639959E-18, -2.11142121435816608115E-17,
|
| 225 |
+
1.55363195773620046921E-16, -1.10559694773538630805E-15,
|
| 226 |
+
7.60068429473540693410E-15, -5.04218550472791168711E-14,
|
| 227 |
+
3.22379336594557470981E-13, -1.98397439776494371520E-12,
|
| 228 |
+
1.17361862988909016308E-11, -6.66348972350202774223E-11,
|
| 229 |
+
3.62559028155211703701E-10, -1.88724975172282928790E-9,
|
| 230 |
+
9.38153738649577178388E-9, -4.44505912879632808065E-8,
|
| 231 |
+
2.00329475355213526229E-7, -8.56872026469545474066E-7,
|
| 232 |
+
3.47025130813767847674E-6, -1.32731636560394358279E-5,
|
| 233 |
+
4.78156510755005422638E-5, -1.61760815825896745588E-4,
|
| 234 |
+
5.12285956168575772895E-4, -1.51357245063125314899E-3,
|
| 235 |
+
4.15642294431288815669E-3, -1.05640848946261981558E-2,
|
| 236 |
+
2.47264490306265168283E-2, -5.29459812080949914269E-2,
|
| 237 |
+
1.02643658689847095384E-1, -1.76416518357834055153E-1,
|
| 238 |
+
2.52587186443633654823E-1};
|
| 239 |
+
const auto y = x / 2.0 - 2.0;
|
| 240 |
+
const auto out = ::metal::exp(x) * x * chbevl(y, coefficients, 29);
|
| 241 |
+
return static_cast<T>(_x < T(0.) ? -out : out);
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
// Chebyshev coefficients for exp(-x) sqrt(x) i1(x)
|
| 245 |
+
// in the inverted interval [8, infinity]
|
| 246 |
+
// lim(x->inf){ exp(-x) sqrt(x) i1(x) } = 1/sqrt(2pi)
|
| 247 |
+
constexpr float coefficients[] = {
|
| 248 |
+
7.51729631084210481353E-18, 4.41434832307170791151E-18,
|
| 249 |
+
-4.65030536848935832153E-17, -3.20952592199342395980E-17,
|
| 250 |
+
2.96262899764595013876E-16, 3.30820231092092828324E-16,
|
| 251 |
+
-1.88035477551078244854E-15, -3.81440307243700780478E-15,
|
| 252 |
+
1.04202769841288027642E-14, 4.27244001671195135429E-14,
|
| 253 |
+
-2.10154184277266431302E-14, -4.08355111109219731823E-13,
|
| 254 |
+
-7.19855177624590851209E-13, 2.03562854414708950722E-12,
|
| 255 |
+
1.41258074366137813316E-11, 3.25260358301548823856E-11,
|
| 256 |
+
-1.89749581235054123450E-11, -5.58974346219658380687E-10,
|
| 257 |
+
-3.83538038596423702205E-9, -2.63146884688951950684E-8,
|
| 258 |
+
-2.51223623787020892529E-7, -3.88256480887769039346E-6,
|
| 259 |
+
-1.10588938762623716291E-4, -9.76109749136146840777E-3,
|
| 260 |
+
7.78576235018280120474E-1};
|
| 261 |
+
const auto out = (::metal::exp(x) * chbevl(32. / x - 2., coefficients, 25)) /
|
| 262 |
+
::metal::sqrt(x);
|
| 263 |
+
return static_cast<T>(_x < T(0.) ? -out : out);
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
template <typename T>
|
| 267 |
+
inline T i1e(T _x) {
|
| 268 |
+
const auto x = ::metal::fabs(_x);
|
| 269 |
+
if (x <= 8.0) {
|
| 270 |
+
// Chebyshev double coefficients for exp(-x) i1(x) in the interval [0,8].
|
| 271 |
+
// Note: lim(x->0){ exp(-x) i1(x) / x } = 1/2.
|
| 272 |
+
constexpr float coefficients[] = {
|
| 273 |
+
9.38153738649577178388E-9f,
|
| 274 |
+
-4.44505912879632808065E-8f,
|
| 275 |
+
2.00329475355213526229E-7f,
|
| 276 |
+
-8.56872026469545474066E-7f,
|
| 277 |
+
3.47025130813767847674E-6f,
|
| 278 |
+
-1.32731636560394358279E-5f,
|
| 279 |
+
4.78156510755005422638E-5f,
|
| 280 |
+
-1.61760815825896745588E-4f,
|
| 281 |
+
5.12285956168575772895E-4f,
|
| 282 |
+
-1.51357245063125314899E-3f,
|
| 283 |
+
4.15642294431288815669E-3f,
|
| 284 |
+
-1.05640848946261981558E-2f,
|
| 285 |
+
2.47264490306265168283E-2f,
|
| 286 |
+
-5.29459812080949914269E-2f,
|
| 287 |
+
1.02643658689847095384E-1f,
|
| 288 |
+
-1.76416518357834055153E-1f,
|
| 289 |
+
2.52587186443633654823E-1f};
|
| 290 |
+
const auto y = x / 2.0 - 2.0;
|
| 291 |
+
const auto out = chbevl(y, coefficients, 17) * x;
|
| 292 |
+
return static_cast<T>(_x < 0. ? -out : out);
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
// Chebyshev coefficients for exp(-x) sqrt(x) i1(x)
|
| 296 |
+
// in the inverted interval (8, infinity].
|
| 297 |
+
// Note: lim(x->inf){ exp(-x) sqrt(x) i1(x) } = 1/sqrt(2pi).
|
| 298 |
+
// TODO: what's an "inverted interval"? Open on the left
|
| 299 |
+
// and closed on the right?
|
| 300 |
+
constexpr float coefficients[] = {
|
| 301 |
+
-3.83538038596423702205E-9f,
|
| 302 |
+
-2.63146884688951950684E-8f,
|
| 303 |
+
-2.51223623787020892529E-7f,
|
| 304 |
+
-3.88256480887769039346E-6f,
|
| 305 |
+
-1.10588938762623716291E-4f,
|
| 306 |
+
-9.76109749136146840777E-3f,
|
| 307 |
+
7.78576235018280120474E-1f};
|
| 308 |
+
|
| 309 |
+
const auto out =
|
| 310 |
+
chbevl(32. / x - 2., coefficients, 7) / ::metal::precise::sqrt(x);
|
| 311 |
+
return static_cast<T>(_x < 0. ? -out : out);
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
// gamma, lgamma
|
| 315 |
+
template <typename T>
|
| 316 |
+
inline float log_gamma(const T);
|
| 317 |
+
|
| 318 |
+
template <typename T>
|
| 319 |
+
inline float gamma(const T x) {
|
| 320 |
+
if (x < 0.001) {
|
| 321 |
+
constexpr float EULER_MASCHERONI = 0.577215664901532860606512090;
|
| 322 |
+
// For small x, 1/gamma(x) has power series x + gamma x^2 - ...
|
| 323 |
+
// So in this range, 1/gamma(x) = x + gamma x^2 with error on the order of
|
| 324 |
+
// x^3. The relative error over this interval is less than 6e-7.
|
| 325 |
+
|
| 326 |
+
return 1.0 / (x * (1.0 + EULER_MASCHERONI * x));
|
| 327 |
+
}
|
| 328 |
+
if (x >= 12.0) {
|
| 329 |
+
return ::metal::exp(log_gamma(x));
|
| 330 |
+
}
|
| 331 |
+
// The algorithm directly approximates gamma over (1,2) and uses
|
| 332 |
+
// reduction identities to reduce other arguments to this interval.
|
| 333 |
+
// numerator coefficients for gamma approximation over the interval (1,2)
|
| 334 |
+
constexpr float GAMMA_NUMERATOR_COEF[8] = {
|
| 335 |
+
-1.71618513886549492533811E+0,
|
| 336 |
+
2.47656508055759199108314E+1,
|
| 337 |
+
-3.79804256470945635097577E+2,
|
| 338 |
+
6.29331155312818442661052E+2,
|
| 339 |
+
8.66966202790413211295064E+2,
|
| 340 |
+
-3.14512729688483675254357E+4,
|
| 341 |
+
-3.61444134186911729807069E+4,
|
| 342 |
+
6.64561438202405440627855E+4};
|
| 343 |
+
|
| 344 |
+
// denominator coefficients for gamma approximation over the interval (1,2)
|
| 345 |
+
constexpr float GAMMA_DENOMINATOR_COEF[8] = {
|
| 346 |
+
-3.08402300119738975254353E+1,
|
| 347 |
+
3.15350626979604161529144E+2,
|
| 348 |
+
-1.01515636749021914166146E+3,
|
| 349 |
+
-3.10777167157231109440444E+3,
|
| 350 |
+
2.25381184209801510330112E+4,
|
| 351 |
+
4.75584627752788110767815E+3,
|
| 352 |
+
-1.34659959864969306392456E+5,
|
| 353 |
+
-1.15132259675553483497211E+5};
|
| 354 |
+
|
| 355 |
+
// Add or subtract integers as necessary to bring y into (1,2)
|
| 356 |
+
float y = 1.0 + ::metal::fract(x);
|
| 357 |
+
|
| 358 |
+
float num = 0.0;
|
| 359 |
+
float den = 1.0;
|
| 360 |
+
|
| 361 |
+
float z = y - 1;
|
| 362 |
+
for (int i = 0; i < 8; i++) {
|
| 363 |
+
num = (num + GAMMA_NUMERATOR_COEF[i]) * z;
|
| 364 |
+
den = den * z + GAMMA_DENOMINATOR_COEF[i];
|
| 365 |
+
}
|
| 366 |
+
float result = num / den + 1.0;
|
| 367 |
+
|
| 368 |
+
// Apply correction if argument was not initially in (1,2)
|
| 369 |
+
if (x < 1.0) {
|
| 370 |
+
// identity gamma(z) = gamma(z+1)/z
|
| 371 |
+
result /= (y - 1.0);
|
| 372 |
+
} else {
|
| 373 |
+
// identity gamma(z+n) = z*(z+1)* ... *(z+n-1)*gamma(z)
|
| 374 |
+
auto n = static_cast<int>(::metal::floor(x));
|
| 375 |
+
for (int i = 1; i < n; i++) {
|
| 376 |
+
result *= y++;
|
| 377 |
+
}
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
return result;
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
template <typename T>
|
| 384 |
+
inline float log_gamma(const T x) {
|
| 385 |
+
constexpr float LOG_PI = 1.14472988584940017414342735135305;
|
| 386 |
+
constexpr float HALF_LOG_TWO_PI = 0.91893853320467274178032973640562;
|
| 387 |
+
constexpr float LGAMMA_EXPANSION_COEF[8] = {
|
| 388 |
+
1.0 / 12.0,
|
| 389 |
+
-1.0 / 360.0,
|
| 390 |
+
1.0 / 1260.0,
|
| 391 |
+
-1.0 / 1680.0,
|
| 392 |
+
1.0 / 1188.0,
|
| 393 |
+
-691.0 / 360360.0,
|
| 394 |
+
1.0 / 156.0,
|
| 395 |
+
-3617.0 / 122400.0};
|
| 396 |
+
|
| 397 |
+
float rc;
|
| 398 |
+
|
| 399 |
+
const auto abs_x = ::metal::abs(static_cast<float>(x));
|
| 400 |
+
if (abs_x == 0) {
|
| 401 |
+
return INFINITY;
|
| 402 |
+
}
|
| 403 |
+
if (abs_x < 12.0) {
|
| 404 |
+
rc = ::metal::log(::metal::abs(gamma(abs_x)));
|
| 405 |
+
} else {
|
| 406 |
+
// Abramowitz and Stegun 6.1.41
|
| 407 |
+
// Asymptotic series should be good to at least 11 or 12 figures
|
| 408 |
+
// For error analysis, see Whittiker and Watson
|
| 409 |
+
// A Course in Modern Analysis (1927), page 252
|
| 410 |
+
|
| 411 |
+
float z = 1.0 / (abs_x * abs_x);
|
| 412 |
+
float sum = LGAMMA_EXPANSION_COEF[7];
|
| 413 |
+
|
| 414 |
+
for (int i = 6; i >= 0; i--) {
|
| 415 |
+
sum *= z;
|
| 416 |
+
sum += LGAMMA_EXPANSION_COEF[i];
|
| 417 |
+
}
|
| 418 |
+
float series = sum / abs_x;
|
| 419 |
+
|
| 420 |
+
rc = (abs_x - 0.5) * ::metal::log(abs_x) - abs_x + HALF_LOG_TWO_PI + series;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
if (x >= 0) {
|
| 424 |
+
return rc;
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
// Reflection formula
|
| 428 |
+
// Compute arg first to workaround Metal compiler bgg of sorts on M4
|
| 429 |
+
// See https://github.com/pytorch/pytorch/pull/145740 for more details
|
| 430 |
+
auto log_arg = abs_x * ::metal::abs(::metal::sinpi(abs_x));
|
| 431 |
+
return LOG_PI - rc - ::metal::log(log_arg);
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
inline float zeta(float x, float q) {
|
| 435 |
+
constexpr float MACHEP = 1.11022302462515654042E-16;
|
| 436 |
+
constexpr float ZETA_EXPANSION[] = {
|
| 437 |
+
12.0,
|
| 438 |
+
-720.0,
|
| 439 |
+
30240.0,
|
| 440 |
+
-1209600.0,
|
| 441 |
+
47900160.0,
|
| 442 |
+
-1.8924375803183791606e9,
|
| 443 |
+
7.47242496e10,
|
| 444 |
+
-2.950130727918164224e12,
|
| 445 |
+
1.1646782814350067249e14,
|
| 446 |
+
-4.5979787224074726105e15,
|
| 447 |
+
1.8152105401943546773e17,
|
| 448 |
+
-7.1661652561756670113e18};
|
| 449 |
+
if (x == 1.0f) {
|
| 450 |
+
return INFINITY;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
if (x < 1.0f) {
|
| 454 |
+
return NAN;
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
if (q <= 0.0f) {
|
| 458 |
+
if (q == ::metal::trunc(q)) {
|
| 459 |
+
return INFINITY;
|
| 460 |
+
}
|
| 461 |
+
if (x != ::metal::trunc(x)) {
|
| 462 |
+
return NAN;
|
| 463 |
+
}
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
float s = ::metal::pow(q, -x);
|
| 467 |
+
float a = q;
|
| 468 |
+
int i = 0;
|
| 469 |
+
float b = 0.0f;
|
| 470 |
+
while ((i < 9) || (a <= 9.0f)) {
|
| 471 |
+
i += 1;
|
| 472 |
+
a += 1.0f;
|
| 473 |
+
b = ::metal::pow(a, -x);
|
| 474 |
+
s += b;
|
| 475 |
+
if ((-MACHEP * s < b) && (b < MACHEP * s)) {
|
| 476 |
+
return s;
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
float w = a;
|
| 481 |
+
s += b * w / (x - 1.0f);
|
| 482 |
+
s -= 0.5f * b;
|
| 483 |
+
a = 1.0f;
|
| 484 |
+
float t;
|
| 485 |
+
float k = 0.0f;
|
| 486 |
+
for (int i = 0; i < 12; i++) {
|
| 487 |
+
a *= x + k;
|
| 488 |
+
b /= w;
|
| 489 |
+
t = a * b / ZETA_EXPANSION[i];
|
| 490 |
+
s += t;
|
| 491 |
+
t = ::metal::fabs(t / s);
|
| 492 |
+
if (t < MACHEP) {
|
| 493 |
+
return s;
|
| 494 |
+
}
|
| 495 |
+
k += 1.0f;
|
| 496 |
+
a *= x + k;
|
| 497 |
+
b /= w;
|
| 498 |
+
k += 1.0f;
|
| 499 |
+
}
|
| 500 |
+
return s;
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
inline float calc_digamma_positive_domain(float x) {
|
| 504 |
+
constexpr float DIGAMMA_COEF[7] = {
|
| 505 |
+
8.33333333333333333333E-2,
|
| 506 |
+
-2.10927960927960927961E-2,
|
| 507 |
+
7.57575757575757575758E-3,
|
| 508 |
+
-4.16666666666666666667E-3,
|
| 509 |
+
3.96825396825396825397E-3,
|
| 510 |
+
-8.33333333333333333333E-3,
|
| 511 |
+
8.33333333333333333333E-2,
|
| 512 |
+
};
|
| 513 |
+
|
| 514 |
+
// Push x to be >= 10
|
| 515 |
+
float result = 0;
|
| 516 |
+
while (x < 10) {
|
| 517 |
+
result -= 1 / x;
|
| 518 |
+
x += 1;
|
| 519 |
+
}
|
| 520 |
+
if (x == 10) {
|
| 521 |
+
constexpr float PSI_10 = 2.25175258906672110764;
|
| 522 |
+
return result + PSI_10;
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
// Compute asymptotic digamma
|
| 526 |
+
float y = 0;
|
| 527 |
+
if (x < 1.0E+17) {
|
| 528 |
+
float z = 1.0 / (x * x);
|
| 529 |
+
for (int i = 0; i <= 6; i++) {
|
| 530 |
+
y += ::metal::pow(z, i) * DIGAMMA_COEF[i];
|
| 531 |
+
}
|
| 532 |
+
y *= z;
|
| 533 |
+
}
|
| 534 |
+
return result + ::metal::log(x) - (0.5 / x) - y;
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
template <typename T0>
|
| 538 |
+
inline float digamma(T0 x) {
|
| 539 |
+
if (x < 0.0f) {
|
| 540 |
+
if (x == ::metal::trunc(x)) {
|
| 541 |
+
// As per C++ standard for gamma related functions and SciPy,
|
| 542 |
+
// If the argument is a negative integer, NaN is returned
|
| 543 |
+
return NAN;
|
| 544 |
+
} else {
|
| 545 |
+
// Extracts the fractional part of x as r, since tan(pi * r) is more
|
| 546 |
+
// numerically accurate than tan(pi * x). While these operations are
|
| 547 |
+
// mathematically equivalent since both x and r are in radians and tan()
|
| 548 |
+
// has a periodicity of pi, in practice the computation of pi * x is a
|
| 549 |
+
// source of error (when |x| > 1).
|
| 550 |
+
float r = ::metal::fract(x);
|
| 551 |
+
return calc_digamma_positive_domain(1.0f - x) -
|
| 552 |
+
M_PI_F / ::metal::tan(M_PI_F * r);
|
| 553 |
+
}
|
| 554 |
+
} else if (x == 0.0f) {
|
| 555 |
+
// As per C++ standard for gamma related functions and SciPy,
|
| 556 |
+
// If the argument is ±0, ±∞ is returned
|
| 557 |
+
return ::metal::copysign(INFINITY, static_cast<float>(-x));
|
| 558 |
+
} else {
|
| 559 |
+
return calc_digamma_positive_domain(x);
|
| 560 |
+
}
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
template <typename T0>
|
| 564 |
+
inline float polygamma(const int64_t order, const T0 input) {
|
| 565 |
+
// Filter out n == 0.
|
| 566 |
+
if (order == 0) {
|
| 567 |
+
return digamma(input);
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
float x = input;
|
| 571 |
+
float n = order;
|
| 572 |
+
float sgn = ((order % 2) ? 1 : -1);
|
| 573 |
+
return sgn * gamma(n + 1) * zeta(n + 1, x);
|
| 574 |
+
}
|
| 575 |
+
|
| 576 |
+
template <typename T>
|
| 577 |
+
inline ::metal::enable_if_t<is_scalar_floating_point_v<T>, T> sinc(T a) {
|
| 578 |
+
if (a == static_cast<T>(0)) {
|
| 579 |
+
return static_cast<T>(1);
|
| 580 |
+
}
|
| 581 |
+
auto product = M_PI_F * static_cast<float>(a);
|
| 582 |
+
return static_cast<T>(::metal::precise::sin(product) / product);
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
// Complex sinc2 implementation
|
| 586 |
+
template <typename T>
|
| 587 |
+
inline ::metal::enable_if_t<is_complex_v<T>, T> sinc(T inp) {
|
| 588 |
+
auto a = static_cast<float2>(inp) * M_PI_F;
|
| 589 |
+
const float a2 = a.x * a.x + a.y * a.y;
|
| 590 |
+
if (a2 == 0) {
|
| 591 |
+
return 0;
|
| 592 |
+
}
|
| 593 |
+
float cosx;
|
| 594 |
+
float sinx = ::metal::sincos(a.x, cosx);
|
| 595 |
+
float sinhy = ::metal::sinh(a.y);
|
| 596 |
+
float coshy = ::metal::cosh(a.y);
|
| 597 |
+
auto re = sinx * coshy * a.x + cosx * sinhy * a.y;
|
| 598 |
+
auto im = cosx * sinhy * a.x - sinx * coshy * a.y;
|
| 599 |
+
return T(re, im) / a2;
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
template <typename T>
|
| 603 |
+
inline T spherical_bessel_j0(T x) {
|
| 604 |
+
if (::metal::isinf(x))
|
| 605 |
+
return T(0.0);
|
| 606 |
+
T x2 = x * x;
|
| 607 |
+
T k1 = static_cast<T>(-1.0);
|
| 608 |
+
T k2 = static_cast<T>(1.0);
|
| 609 |
+
|
| 610 |
+
if (::metal::fabs(static_cast<T>(x)) < T(0.5)) {
|
| 611 |
+
return T(1.0) +
|
| 612 |
+
x2 *
|
| 613 |
+
(k1 / T(6.0) +
|
| 614 |
+
x2 *
|
| 615 |
+
(k2 / T(120.0) +
|
| 616 |
+
x2 *
|
| 617 |
+
(k1 / T(5040.0) +
|
| 618 |
+
x2 *
|
| 619 |
+
(k2 / T(362880.0) +
|
| 620 |
+
x2 *
|
| 621 |
+
(k1 / T(39916800.0) +
|
| 622 |
+
x2 * (k2 / T(6227020800.0)))))));
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
return static_cast<T>(::metal::sin(x) / x);
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
template <typename T>
|
| 629 |
+
inline ::metal::enable_if_t<is_scalar_floating_point_v<T>, T> logaddexp(
|
| 630 |
+
T a,
|
| 631 |
+
T b) {
|
| 632 |
+
float a0 = static_cast<float>(a);
|
| 633 |
+
float b0 = static_cast<float>(b);
|
| 634 |
+
if (::metal::isinf(a0) && a0 == b0) {
|
| 635 |
+
return static_cast<T>(a0);
|
| 636 |
+
} else {
|
| 637 |
+
float m0 = ::metal::max(a0, b0);
|
| 638 |
+
return static_cast<T>(
|
| 639 |
+
m0 + ::c10::metal::log1p(::metal::exp(-::metal::abs(a0 - b0))));
|
| 640 |
+
}
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
// The function is ported from mlx
|
| 644 |
+
template <typename T>
|
| 645 |
+
inline ::metal::enable_if_t<is_complex_v<T>, T> logaddexp(T a, T b) {
|
| 646 |
+
if (::metal::isnan(a.x) || ::metal::isnan(a.y) || ::metal::isnan(b.x) ||
|
| 647 |
+
::metal::isnan(b.y)) {
|
| 648 |
+
return T(NAN, NAN);
|
| 649 |
+
}
|
| 650 |
+
|
| 651 |
+
T maxval = a.x > b.x ? a : b;
|
| 652 |
+
T minval = a.x < b.x ? a : b;
|
| 653 |
+
constexpr auto inf = ::metal::numeric_limits<T>::infinity().x;
|
| 654 |
+
|
| 655 |
+
if (minval.x == -inf || maxval.x == inf) {
|
| 656 |
+
return maxval;
|
| 657 |
+
}
|
| 658 |
+
|
| 659 |
+
float2 maxval_ = static_cast<float2>(maxval);
|
| 660 |
+
float2 minval_ = static_cast<float2>(minval);
|
| 661 |
+
float m = ::metal::exp(minval_.x - maxval_.x);
|
| 662 |
+
float2 dexp{
|
| 663 |
+
m * ::metal::cos(minval_.y - maxval_.y),
|
| 664 |
+
m * ::metal::sin(minval_.y - maxval_.y),
|
| 665 |
+
};
|
| 666 |
+
return static_cast<T>(maxval_ + ::c10::metal::log1p(dexp));
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
template <typename T>
|
| 670 |
+
inline T logaddexp2(T a, T b) {
|
| 671 |
+
constexpr auto log_2 = float(0.693147180559945309417232121458176);
|
| 672 |
+
constexpr auto inv_log_2 = float(1) / log_2;
|
| 673 |
+
float a0 = static_cast<float>(a);
|
| 674 |
+
float b0 = static_cast<float>(b);
|
| 675 |
+
if (::metal::isinf(a0) && a0 == b0) {
|
| 676 |
+
return static_cast<T>(a0);
|
| 677 |
+
} else {
|
| 678 |
+
float m0 = ::metal::max(a0, b0);
|
| 679 |
+
return static_cast<T>(
|
| 680 |
+
m0 +
|
| 681 |
+
::c10::metal::log1p(::metal::pow(float(2), -::metal::abs(a0 - b0))) *
|
| 682 |
+
inv_log_2);
|
| 683 |
+
}
|
| 684 |
+
}
|
| 685 |
+
|
| 686 |
+
template <typename T>
|
| 687 |
+
inline float xlog1py(T x, T y) {
|
| 688 |
+
if (::metal::isnan(y)) {
|
| 689 |
+
return NAN;
|
| 690 |
+
}
|
| 691 |
+
|
| 692 |
+
if (x == 0) {
|
| 693 |
+
return x;
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
return x * ::c10::metal::log1p(y);
|
| 697 |
+
}
|
| 698 |
+
|
| 699 |
+
template <typename T>
|
| 700 |
+
inline T entr(T a) {
|
| 701 |
+
if (a != a) {
|
| 702 |
+
return a;
|
| 703 |
+
}
|
| 704 |
+
|
| 705 |
+
if (a > 0) {
|
| 706 |
+
return static_cast<T>(-a * ::metal::log(a));
|
| 707 |
+
}
|
| 708 |
+
|
| 709 |
+
if (a == 0) {
|
| 710 |
+
return 0;
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
+
return static_cast<T>(-INFINITY);
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
// Copy-n-paste from aten/src/ATen/native/cuda/Math.cuh lines 1463-1915
|
| 717 |
+
template <typename T>
|
| 718 |
+
inline float bessel_j0_forward(T x) {
|
| 719 |
+
constexpr float PP[] = {
|
| 720 |
+
+7.96936729297347051624e-04,
|
| 721 |
+
+8.28352392107440799803e-02,
|
| 722 |
+
+1.23953371646414299388e+00,
|
| 723 |
+
+5.44725003058768775090e+00,
|
| 724 |
+
+8.74716500199817011941e+00,
|
| 725 |
+
+5.30324038235394892183e+00,
|
| 726 |
+
+9.99999999999999997821e-01,
|
| 727 |
+
};
|
| 728 |
+
|
| 729 |
+
constexpr float PQ[] = {
|
| 730 |
+
+9.24408810558863637013e-04,
|
| 731 |
+
+8.56288474354474431428e-02,
|
| 732 |
+
+1.25352743901058953537e+00,
|
| 733 |
+
+5.47097740330417105182e+00,
|
| 734 |
+
+8.76190883237069594232e+00,
|
| 735 |
+
+5.30605288235394617618e+00,
|
| 736 |
+
+1.00000000000000000218e+00,
|
| 737 |
+
};
|
| 738 |
+
|
| 739 |
+
constexpr float QP[] = {
|
| 740 |
+
-1.13663838898469149931e-02,
|
| 741 |
+
-1.28252718670509318512e+00,
|
| 742 |
+
-1.95539544257735972385e+01,
|
| 743 |
+
-9.32060152123768231369e+01,
|
| 744 |
+
-1.77681167980488050595e+02,
|
| 745 |
+
-1.47077505154951170175e+02,
|
| 746 |
+
-5.14105326766599330220e+01,
|
| 747 |
+
-6.05014350600728481186e+00,
|
| 748 |
+
};
|
| 749 |
+
|
| 750 |
+
constexpr float QQ[] = {
|
| 751 |
+
+6.43178256118178023184e+01,
|
| 752 |
+
+8.56430025976980587198e+02,
|
| 753 |
+
+3.88240183605401609683e+03,
|
| 754 |
+
+7.24046774195652478189e+03,
|
| 755 |
+
+5.93072701187316984827e+03,
|
| 756 |
+
+2.06209331660327847417e+03,
|
| 757 |
+
+2.42005740240291393179e+02,
|
| 758 |
+
};
|
| 759 |
+
|
| 760 |
+
constexpr float RP[] = {
|
| 761 |
+
-4.79443220978201773821e+09,
|
| 762 |
+
+1.95617491946556577543e+12,
|
| 763 |
+
-2.49248344360967716204e+14,
|
| 764 |
+
+9.70862251047306323952e+15,
|
| 765 |
+
};
|
| 766 |
+
|
| 767 |
+
constexpr float RQ[] = {
|
| 768 |
+
+4.99563147152651017219e+02,
|
| 769 |
+
+1.73785401676374683123e+05,
|
| 770 |
+
+4.84409658339962045305e+07,
|
| 771 |
+
+1.11855537045356834862e+10,
|
| 772 |
+
+2.11277520115489217587e+12,
|
| 773 |
+
+3.10518229857422583814e+14,
|
| 774 |
+
+3.18121955943204943306e+16,
|
| 775 |
+
+1.71086294081043136091e+18,
|
| 776 |
+
};
|
| 777 |
+
|
| 778 |
+
if (x < T(0)) {
|
| 779 |
+
x = -x;
|
| 780 |
+
}
|
| 781 |
+
|
| 782 |
+
if (x <= T(5.0)) {
|
| 783 |
+
if (x < T(0.00001)) {
|
| 784 |
+
return 1.0 - x * x / 4.0;
|
| 785 |
+
}
|
| 786 |
+
|
| 787 |
+
float rp = 0.0;
|
| 788 |
+
|
| 789 |
+
for (auto index = 0; index <= 3; index++) {
|
| 790 |
+
rp = rp * (x * x) + RP[index];
|
| 791 |
+
}
|
| 792 |
+
|
| 793 |
+
float rq = 0.0;
|
| 794 |
+
|
| 795 |
+
for (auto index = 0; index <= 7; index++) {
|
| 796 |
+
rq = rq * (x * x) + RQ[index];
|
| 797 |
+
}
|
| 798 |
+
|
| 799 |
+
return (x * x - 5.78318596294678452118e+00) *
|
| 800 |
+
(x * x - T(3.04712623436620863991e+01)) * rp / rq;
|
| 801 |
+
}
|
| 802 |
+
|
| 803 |
+
float pp = 0.0;
|
| 804 |
+
|
| 805 |
+
for (auto index = 0; index <= 6; index++) {
|
| 806 |
+
pp = pp * (25.0 / (x * x)) + PP[index];
|
| 807 |
+
}
|
| 808 |
+
|
| 809 |
+
float pq = 0.0;
|
| 810 |
+
|
| 811 |
+
for (auto index = 0; index <= 6; index++) {
|
| 812 |
+
pq = pq * (25.0 / (x * x)) + PQ[index];
|
| 813 |
+
}
|
| 814 |
+
|
| 815 |
+
float qp = 0.0;
|
| 816 |
+
|
| 817 |
+
for (auto index = 0; index <= 7; index++) {
|
| 818 |
+
qp = qp * (25.0 / (x * x)) + QP[index];
|
| 819 |
+
}
|
| 820 |
+
|
| 821 |
+
float qq = 0.0;
|
| 822 |
+
|
| 823 |
+
for (auto index = 0; index <= 6; index++) {
|
| 824 |
+
qq = qq * (25.0 / (x * x)) + QQ[index];
|
| 825 |
+
}
|
| 826 |
+
|
| 827 |
+
return (pp / pq *
|
| 828 |
+
::metal::precise::cos(
|
| 829 |
+
x - T(0.785398163397448309615660845819875721)) -
|
| 830 |
+
5.0 / x * (qp / qq) *
|
| 831 |
+
::metal::precise::sin(
|
| 832 |
+
x - 0.785398163397448309615660845819875721)) *
|
| 833 |
+
0.797884560802865355879892119868763737 / ::metal::precise::sqrt(x);
|
| 834 |
+
} // bessel_j0_forward(T x)
|
| 835 |
+
|
| 836 |
+
template <typename T>
|
| 837 |
+
inline float bessel_y0_forward(T x) {
|
| 838 |
+
constexpr float PP[] = {
|
| 839 |
+
+7.96936729297347051624e-04,
|
| 840 |
+
+8.28352392107440799803e-02,
|
| 841 |
+
+1.23953371646414299388e+00,
|
| 842 |
+
+5.44725003058768775090e+00,
|
| 843 |
+
+8.74716500199817011941e+00,
|
| 844 |
+
+5.30324038235394892183e+00,
|
| 845 |
+
+9.99999999999999997821e-01,
|
| 846 |
+
};
|
| 847 |
+
|
| 848 |
+
constexpr float PQ[] = {
|
| 849 |
+
+9.24408810558863637013e-04,
|
| 850 |
+
+8.56288474354474431428e-02,
|
| 851 |
+
+1.25352743901058953537e+00,
|
| 852 |
+
+5.47097740330417105182e+00,
|
| 853 |
+
+8.76190883237069594232e+00,
|
| 854 |
+
+5.30605288235394617618e+00,
|
| 855 |
+
+1.00000000000000000218e+00,
|
| 856 |
+
};
|
| 857 |
+
|
| 858 |
+
constexpr float QP[] = {
|
| 859 |
+
-1.13663838898469149931e-02,
|
| 860 |
+
-1.28252718670509318512e+00,
|
| 861 |
+
-1.95539544257735972385e+01,
|
| 862 |
+
-9.32060152123768231369e+01,
|
| 863 |
+
-1.77681167980488050595e+02,
|
| 864 |
+
-1.47077505154951170175e+02,
|
| 865 |
+
-5.14105326766599330220e+01,
|
| 866 |
+
-6.05014350600728481186e+00,
|
| 867 |
+
};
|
| 868 |
+
|
| 869 |
+
constexpr float QQ[] = {
|
| 870 |
+
+6.43178256118178023184e+01,
|
| 871 |
+
+8.56430025976980587198e+02,
|
| 872 |
+
+3.88240183605401609683e+03,
|
| 873 |
+
+7.24046774195652478189e+03,
|
| 874 |
+
+5.93072701187316984827e+03,
|
| 875 |
+
+2.06209331660327847417e+03,
|
| 876 |
+
+2.42005740240291393179e+02,
|
| 877 |
+
};
|
| 878 |
+
|
| 879 |
+
constexpr float YP[] = {
|
| 880 |
+
+1.55924367855235737965e+04,
|
| 881 |
+
-1.46639295903971606143e+07,
|
| 882 |
+
+5.43526477051876500413e+09,
|
| 883 |
+
-9.82136065717911466409e+11,
|
| 884 |
+
+8.75906394395366999549e+13,
|
| 885 |
+
-3.46628303384729719441e+15,
|
| 886 |
+
+4.42733268572569800351e+16,
|
| 887 |
+
-1.84950800436986690637e+16,
|
| 888 |
+
};
|
| 889 |
+
|
| 890 |
+
constexpr float YQ[] = {
|
| 891 |
+
+1.04128353664259848412e+03,
|
| 892 |
+
+6.26107330137134956842e+05,
|
| 893 |
+
+2.68919633393814121987e+08,
|
| 894 |
+
+8.64002487103935000337e+10,
|
| 895 |
+
+2.02979612750105546709e+13,
|
| 896 |
+
+3.17157752842975028269e+15,
|
| 897 |
+
+2.50596256172653059228e+17,
|
| 898 |
+
};
|
| 899 |
+
|
| 900 |
+
if (x <= T(5.0)) {
|
| 901 |
+
if (x == T(0.0)) {
|
| 902 |
+
return -INFINITY;
|
| 903 |
+
}
|
| 904 |
+
|
| 905 |
+
if (x < T(0.0)) {
|
| 906 |
+
return NAN;
|
| 907 |
+
}
|
| 908 |
+
|
| 909 |
+
float yp = 0.0;
|
| 910 |
+
|
| 911 |
+
for (auto index = 0; index <= 7; index++) {
|
| 912 |
+
yp = yp * (x * x) + YP[index];
|
| 913 |
+
}
|
| 914 |
+
|
| 915 |
+
float yq = 0.0;
|
| 916 |
+
|
| 917 |
+
for (auto index = 0; index <= 6; index++) {
|
| 918 |
+
yq = yq * (x * x) + YQ[index];
|
| 919 |
+
}
|
| 920 |
+
|
| 921 |
+
return yp / yq +
|
| 922 |
+
(0.636619772367581343075535053490057448 * ::metal::precise::log(x) *
|
| 923 |
+
bessel_j0_forward(x));
|
| 924 |
+
}
|
| 925 |
+
|
| 926 |
+
float pp = 0.0;
|
| 927 |
+
|
| 928 |
+
for (auto index = 0; index <= 6; index++) {
|
| 929 |
+
pp = pp * (25.0 / (x * x)) + PP[index];
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
float pq = 0.0;
|
| 933 |
+
|
| 934 |
+
for (auto index = 0; index <= 6; index++) {
|
| 935 |
+
pq = pq * (25.0 / (x * x)) + PQ[index];
|
| 936 |
+
}
|
| 937 |
+
|
| 938 |
+
float qp = 0.0;
|
| 939 |
+
|
| 940 |
+
for (auto index = 0; index <= 7; index++) {
|
| 941 |
+
qp = qp * (25.0 / (x * x)) + QP[index];
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
float qq = 0.0;
|
| 945 |
+
|
| 946 |
+
for (auto index = 0; index <= 6; index++) {
|
| 947 |
+
qq = qq * (25.0 / (x * x)) + QQ[index];
|
| 948 |
+
}
|
| 949 |
+
|
| 950 |
+
return (pp / pq *
|
| 951 |
+
::metal::precise::sin(
|
| 952 |
+
x - 0.785398163397448309615660845819875721) +
|
| 953 |
+
5.0 / x * (qp / qq) *
|
| 954 |
+
::metal::precise::cos(
|
| 955 |
+
x - 0.785398163397448309615660845819875721)) *
|
| 956 |
+
0.797884560802865355879892119868763737 / ::metal::precise::sqrt(x);
|
| 957 |
+
} // bessel_y0_forward(T x)
|
| 958 |
+
|
| 959 |
+
template <typename T>
|
| 960 |
+
inline float bessel_j1_forward(T x) {
|
| 961 |
+
constexpr float PP[] = {
|
| 962 |
+
+7.62125616208173112003e-04,
|
| 963 |
+
+7.31397056940917570436e-02,
|
| 964 |
+
+1.12719608129684925192e+00,
|
| 965 |
+
+5.11207951146807644818e+00,
|
| 966 |
+
+8.42404590141772420927e+00,
|
| 967 |
+
+5.21451598682361504063e+00,
|
| 968 |
+
+1.00000000000000000254e+00,
|
| 969 |
+
};
|
| 970 |
+
|
| 971 |
+
constexpr float PQ[] = {
|
| 972 |
+
+5.71323128072548699714e-04,
|
| 973 |
+
+6.88455908754495404082e-02,
|
| 974 |
+
+1.10514232634061696926e+00,
|
| 975 |
+
+5.07386386128601488557e+00,
|
| 976 |
+
+8.39985554327604159757e+00,
|
| 977 |
+
+5.20982848682361821619e+00,
|
| 978 |
+
+9.99999999999999997461e-01,
|
| 979 |
+
};
|
| 980 |
+
|
| 981 |
+
constexpr float QP[] = {
|
| 982 |
+
+5.10862594750176621635e-02,
|
| 983 |
+
+4.98213872951233449420e+00,
|
| 984 |
+
+7.58238284132545283818e+01,
|
| 985 |
+
+3.66779609360150777800e+02,
|
| 986 |
+
+7.10856304998926107277e+02,
|
| 987 |
+
+5.97489612400613639965e+02,
|
| 988 |
+
+2.11688757100572135698e+02,
|
| 989 |
+
+2.52070205858023719784e+01,
|
| 990 |
+
};
|
| 991 |
+
|
| 992 |
+
constexpr float QQ[] = {
|
| 993 |
+
+7.42373277035675149943e+01,
|
| 994 |
+
+1.05644886038262816351e+03,
|
| 995 |
+
+4.98641058337653607651e+03,
|
| 996 |
+
+9.56231892404756170795e+03,
|
| 997 |
+
+7.99704160447350683650e+03,
|
| 998 |
+
+2.82619278517639096600e+03,
|
| 999 |
+
+3.36093607810698293419e+02,
|
| 1000 |
+
};
|
| 1001 |
+
|
| 1002 |
+
constexpr float RP[] = {
|
| 1003 |
+
-8.99971225705559398224e+08,
|
| 1004 |
+
+4.52228297998194034323e+11,
|
| 1005 |
+
-7.27494245221818276015e+13,
|
| 1006 |
+
+3.68295732863852883286e+15,
|
| 1007 |
+
};
|
| 1008 |
+
|
| 1009 |
+
constexpr float RQ[] = {
|
| 1010 |
+
+6.20836478118054335476e+02,
|
| 1011 |
+
+2.56987256757748830383e+05,
|
| 1012 |
+
+8.35146791431949253037e+07,
|
| 1013 |
+
+2.21511595479792499675e+10,
|
| 1014 |
+
+4.74914122079991414898e+12,
|
| 1015 |
+
+7.84369607876235854894e+14,
|
| 1016 |
+
+8.95222336184627338078e+16,
|
| 1017 |
+
+5.32278620332680085395e+18,
|
| 1018 |
+
};
|
| 1019 |
+
|
| 1020 |
+
if (x < T(0.0)) {
|
| 1021 |
+
return -bessel_j1_forward(-x);
|
| 1022 |
+
}
|
| 1023 |
+
|
| 1024 |
+
if (x <= T(5.0)) {
|
| 1025 |
+
float rp = 0.0;
|
| 1026 |
+
|
| 1027 |
+
for (auto index = 0; index <= 3; index++) {
|
| 1028 |
+
rp = rp * (x * x) + RP[index];
|
| 1029 |
+
}
|
| 1030 |
+
|
| 1031 |
+
float rq = 0.0;
|
| 1032 |
+
|
| 1033 |
+
for (auto index = 0; index <= 7; index++) {
|
| 1034 |
+
rq = rq * (x * x) + RQ[index];
|
| 1035 |
+
}
|
| 1036 |
+
|
| 1037 |
+
return rp / rq * x * (x * x - 1.46819706421238932572e+01) *
|
| 1038 |
+
(x * x - 4.92184563216946036703e+01);
|
| 1039 |
+
}
|
| 1040 |
+
|
| 1041 |
+
float pp = 0.0;
|
| 1042 |
+
|
| 1043 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1044 |
+
pp = pp * (5.0 / x * (5.0 / x)) + PP[index];
|
| 1045 |
+
}
|
| 1046 |
+
|
| 1047 |
+
float pq = 0.0;
|
| 1048 |
+
|
| 1049 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1050 |
+
pq = pq * (5.0 / x * (5.0 / x)) + PQ[index];
|
| 1051 |
+
}
|
| 1052 |
+
|
| 1053 |
+
float qp = 0.0;
|
| 1054 |
+
|
| 1055 |
+
for (auto index = 0; index <= 7; index++) {
|
| 1056 |
+
qp = qp * (5.0 / x * (5.0 / x)) + QP[index];
|
| 1057 |
+
}
|
| 1058 |
+
|
| 1059 |
+
float qq = 0.0;
|
| 1060 |
+
|
| 1061 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1062 |
+
qq = qq * (5.0 / x * (5.0 / x)) + QQ[index];
|
| 1063 |
+
}
|
| 1064 |
+
|
| 1065 |
+
return (pp / pq *
|
| 1066 |
+
::metal::precise::cos(
|
| 1067 |
+
x - 2.356194490192344928846982537459627163) -
|
| 1068 |
+
5.0 / x * (qp / qq) *
|
| 1069 |
+
::metal::precise::sin(
|
| 1070 |
+
x - 2.356194490192344928846982537459627163)) *
|
| 1071 |
+
0.797884560802865355879892119868763737 / ::metal::precise::sqrt(x);
|
| 1072 |
+
} // bessel_j1_forward(T x)
|
| 1073 |
+
|
| 1074 |
+
template <typename T>
|
| 1075 |
+
inline float bessel_y1_forward(T x) {
|
| 1076 |
+
constexpr float PP[] = {
|
| 1077 |
+
+7.62125616208173112003e-04,
|
| 1078 |
+
+7.31397056940917570436e-02,
|
| 1079 |
+
+1.12719608129684925192e+00,
|
| 1080 |
+
+5.11207951146807644818e+00,
|
| 1081 |
+
+8.42404590141772420927e+00,
|
| 1082 |
+
+5.21451598682361504063e+00,
|
| 1083 |
+
+1.00000000000000000254e+00,
|
| 1084 |
+
};
|
| 1085 |
+
|
| 1086 |
+
constexpr float PQ[] = {
|
| 1087 |
+
+5.71323128072548699714e-04,
|
| 1088 |
+
+6.88455908754495404082e-02,
|
| 1089 |
+
+1.10514232634061696926e+00,
|
| 1090 |
+
+5.07386386128601488557e+00,
|
| 1091 |
+
+8.39985554327604159757e+00,
|
| 1092 |
+
+5.20982848682361821619e+00,
|
| 1093 |
+
+9.99999999999999997461e-01,
|
| 1094 |
+
};
|
| 1095 |
+
|
| 1096 |
+
constexpr float QP[] = {
|
| 1097 |
+
+5.10862594750176621635e-02,
|
| 1098 |
+
+4.98213872951233449420e+00,
|
| 1099 |
+
+7.58238284132545283818e+01,
|
| 1100 |
+
+3.66779609360150777800e+02,
|
| 1101 |
+
+7.10856304998926107277e+02,
|
| 1102 |
+
+5.97489612400613639965e+02,
|
| 1103 |
+
+2.11688757100572135698e+02,
|
| 1104 |
+
+2.52070205858023719784e+01,
|
| 1105 |
+
};
|
| 1106 |
+
|
| 1107 |
+
constexpr float QQ[] = {
|
| 1108 |
+
+7.42373277035675149943e+01,
|
| 1109 |
+
+1.05644886038262816351e+03,
|
| 1110 |
+
+4.98641058337653607651e+03,
|
| 1111 |
+
+9.56231892404756170795e+03,
|
| 1112 |
+
+7.99704160447350683650e+03,
|
| 1113 |
+
+2.82619278517639096600e+03,
|
| 1114 |
+
+3.36093607810698293419e+02,
|
| 1115 |
+
};
|
| 1116 |
+
|
| 1117 |
+
constexpr float YP[] = {
|
| 1118 |
+
+1.26320474790178026440e+09,
|
| 1119 |
+
-6.47355876379160291031e+11,
|
| 1120 |
+
+1.14509511541823727583e+14,
|
| 1121 |
+
-8.12770255501325109621e+15,
|
| 1122 |
+
+2.02439475713594898196e+17,
|
| 1123 |
+
-7.78877196265950026825e+17,
|
| 1124 |
+
};
|
| 1125 |
+
|
| 1126 |
+
constexpr float YQ[] = {
|
| 1127 |
+
+5.94301592346128195359e+02,
|
| 1128 |
+
+2.35564092943068577943e+05,
|
| 1129 |
+
+7.34811944459721705660e+07,
|
| 1130 |
+
+1.87601316108706159478e+10,
|
| 1131 |
+
+3.88231277496238566008e+12,
|
| 1132 |
+
+6.20557727146953693363e+14,
|
| 1133 |
+
+6.87141087355300489866e+16,
|
| 1134 |
+
+3.97270608116560655612e+18,
|
| 1135 |
+
};
|
| 1136 |
+
|
| 1137 |
+
if (x <= T(5.0)) {
|
| 1138 |
+
if (x == T(0.0)) {
|
| 1139 |
+
return -INFINITY;
|
| 1140 |
+
}
|
| 1141 |
+
|
| 1142 |
+
if (x <= T(0.0)) {
|
| 1143 |
+
return NAN;
|
| 1144 |
+
}
|
| 1145 |
+
|
| 1146 |
+
float yp = 0.0;
|
| 1147 |
+
|
| 1148 |
+
for (auto index = 0; index <= 5; index++) {
|
| 1149 |
+
yp = yp * (x * x) + YP[index];
|
| 1150 |
+
}
|
| 1151 |
+
|
| 1152 |
+
float yq = 0.0;
|
| 1153 |
+
|
| 1154 |
+
for (auto index = 0; index <= 7; index++) {
|
| 1155 |
+
yq = yq * (x * x) + YQ[index];
|
| 1156 |
+
}
|
| 1157 |
+
|
| 1158 |
+
return x * (yp / yq) +
|
| 1159 |
+
(0.636619772367581343075535053490057448 *
|
| 1160 |
+
(bessel_j1_forward(x) * ::metal::precise::log(x) - 1.0 / x));
|
| 1161 |
+
}
|
| 1162 |
+
|
| 1163 |
+
float pp = 0.0;
|
| 1164 |
+
|
| 1165 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1166 |
+
pp = pp * (5.0 / x * (5.0 / x)) + PP[index];
|
| 1167 |
+
}
|
| 1168 |
+
|
| 1169 |
+
float pq = 0.0;
|
| 1170 |
+
|
| 1171 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1172 |
+
pq = pq * (5.0 / x * (5.0 / x)) + PQ[index];
|
| 1173 |
+
}
|
| 1174 |
+
|
| 1175 |
+
float qp = 0.0;
|
| 1176 |
+
|
| 1177 |
+
for (auto index = 0; index <= 7; index++) {
|
| 1178 |
+
qp = qp * (5.0 / x * (5.0 / x)) + QP[index];
|
| 1179 |
+
}
|
| 1180 |
+
|
| 1181 |
+
float qq = 0.0;
|
| 1182 |
+
|
| 1183 |
+
for (auto index = 0; index <= 6; index++) {
|
| 1184 |
+
qq = qq * (5.0 / x * (5.0 / x)) + QQ[index];
|
| 1185 |
+
}
|
| 1186 |
+
|
| 1187 |
+
return (pp / pq *
|
| 1188 |
+
::metal::precise::sin(
|
| 1189 |
+
x - 2.356194490192344928846982537459627163) +
|
| 1190 |
+
5.0 / x * (qp / qq) *
|
| 1191 |
+
::metal::precise::cos(
|
| 1192 |
+
x - 2.356194490192344928846982537459627163)) *
|
| 1193 |
+
0.797884560802865355879892119868763737 / ::metal::precise::sqrt(x);
|
| 1194 |
+
} // bessel_y1_forward(T x)
|
| 1195 |
+
|
| 1196 |
+
template <typename T>
|
| 1197 |
+
inline float modified_bessel_i0_forward(T x) {
|
| 1198 |
+
constexpr float A[] = {
|
| 1199 |
+
-4.41534164647933937950e-18, +3.33079451882223809783e-17,
|
| 1200 |
+
-2.43127984654795469359e-16, +1.71539128555513303061e-15,
|
| 1201 |
+
-1.16853328779934516808e-14, +7.67618549860493561688e-14,
|
| 1202 |
+
-4.85644678311192946090e-13, +2.95505266312963983461e-12,
|
| 1203 |
+
-1.72682629144155570723e-11, +9.67580903537323691224e-11,
|
| 1204 |
+
-5.18979560163526290666e-10, +2.65982372468238665035e-09,
|
| 1205 |
+
-1.30002500998624804212e-08, +6.04699502254191894932e-08,
|
| 1206 |
+
-2.67079385394061173391e-07, +1.11738753912010371815e-06,
|
| 1207 |
+
-4.41673835845875056359e-06, +1.64484480707288970893e-05,
|
| 1208 |
+
-5.75419501008210370398e-05, +1.88502885095841655729e-04,
|
| 1209 |
+
-5.76375574538582365885e-04, +1.63947561694133579842e-03,
|
| 1210 |
+
-4.32430999505057594430e-03, +1.05464603945949983183e-02,
|
| 1211 |
+
-2.37374148058994688156e-02, +4.93052842396707084878e-02,
|
| 1212 |
+
-9.49010970480476444210e-02, +1.71620901522208775349e-01,
|
| 1213 |
+
-3.04682672343198398683e-01, +6.76795274409476084995e-01,
|
| 1214 |
+
};
|
| 1215 |
+
|
| 1216 |
+
constexpr float B[] = {
|
| 1217 |
+
-7.23318048787475395456e-18, -4.83050448594418207126e-18,
|
| 1218 |
+
+4.46562142029675999901e-17, +3.46122286769746109310e-17,
|
| 1219 |
+
-2.82762398051658348494e-16, -3.42548561967721913462e-16,
|
| 1220 |
+
+1.77256013305652638360e-15, +3.81168066935262242075e-15,
|
| 1221 |
+
-9.55484669882830764870e-15, -4.15056934728722208663e-14,
|
| 1222 |
+
+1.54008621752140982691e-14, +3.85277838274214270114e-13,
|
| 1223 |
+
+7.18012445138366623367e-13, -1.79417853150680611778e-12,
|
| 1224 |
+
-1.32158118404477131188e-11, -3.14991652796324136454e-11,
|
| 1225 |
+
+1.18891471078464383424e-11, +4.94060238822496958910e-10,
|
| 1226 |
+
+3.39623202570838634515e-09, +2.26666899049817806459e-08,
|
| 1227 |
+
+2.04891858946906374183e-07, +2.89137052083475648297e-06,
|
| 1228 |
+
+6.88975834691682398426e-05, +3.36911647825569408990e-03,
|
| 1229 |
+
+8.04490411014108831608e-01,
|
| 1230 |
+
};
|
| 1231 |
+
|
| 1232 |
+
float p;
|
| 1233 |
+
float q = 0.0;
|
| 1234 |
+
|
| 1235 |
+
if (::metal::fabs(x) <= 8.0) {
|
| 1236 |
+
float a = A[0];
|
| 1237 |
+
|
| 1238 |
+
for (uint8_t index = 1; index < 30; index++) {
|
| 1239 |
+
p = q;
|
| 1240 |
+
q = a;
|
| 1241 |
+
a = (.5 * ::metal::fabs(x) - 2.0) * q - p + A[index];
|
| 1242 |
+
}
|
| 1243 |
+
|
| 1244 |
+
return ::metal::exp(::metal::fabs(x)) * (T(0.5) * (a - p));
|
| 1245 |
+
}
|
| 1246 |
+
|
| 1247 |
+
float b = B[0];
|
| 1248 |
+
|
| 1249 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1250 |
+
p = q;
|
| 1251 |
+
q = b;
|
| 1252 |
+
b = (32.0 / ::metal::fabs(x) - 2.0) * q - p + B[index];
|
| 1253 |
+
}
|
| 1254 |
+
|
| 1255 |
+
return ::metal::exp(::metal::fabs(x)) * (.5 * (b - p)) /
|
| 1256 |
+
::metal::precise::sqrt(::metal::fabs(x));
|
| 1257 |
+
} // modified_bessel_i0_forward(T x)
|
| 1258 |
+
|
| 1259 |
+
template <typename T>
|
| 1260 |
+
inline float modified_bessel_i1_forward(T x) {
|
| 1261 |
+
constexpr float A[] = {
|
| 1262 |
+
+2.77791411276104639959e-18, -2.11142121435816608115e-17,
|
| 1263 |
+
+1.55363195773620046921e-16, -1.10559694773538630805e-15,
|
| 1264 |
+
+7.60068429473540693410e-15, -5.04218550472791168711e-14,
|
| 1265 |
+
+3.22379336594557470981e-13, -1.98397439776494371520e-12,
|
| 1266 |
+
+1.17361862988909016308e-11, -6.66348972350202774223e-11,
|
| 1267 |
+
+3.62559028155211703701e-10, -1.88724975172282928790e-09,
|
| 1268 |
+
+9.38153738649577178388e-09, -4.44505912879632808065e-08,
|
| 1269 |
+
+2.00329475355213526229e-07, -8.56872026469545474066e-07,
|
| 1270 |
+
+3.47025130813767847674e-06, -1.32731636560394358279e-05,
|
| 1271 |
+
+4.78156510755005422638e-05, -1.61760815825896745588e-04,
|
| 1272 |
+
+5.12285956168575772895e-04, -1.51357245063125314899e-03,
|
| 1273 |
+
+4.15642294431288815669e-03, -1.05640848946261981558e-02,
|
| 1274 |
+
+2.47264490306265168283e-02, -5.29459812080949914269e-02,
|
| 1275 |
+
+1.02643658689847095384e-01, -1.76416518357834055153e-01,
|
| 1276 |
+
+2.52587186443633654823e-01,
|
| 1277 |
+
};
|
| 1278 |
+
|
| 1279 |
+
constexpr float B[] = {
|
| 1280 |
+
+7.51729631084210481353e-18, +4.41434832307170791151e-18,
|
| 1281 |
+
-4.65030536848935832153e-17, -3.20952592199342395980e-17,
|
| 1282 |
+
+2.96262899764595013876e-16, +3.30820231092092828324e-16,
|
| 1283 |
+
-1.88035477551078244854e-15, -3.81440307243700780478e-15,
|
| 1284 |
+
+1.04202769841288027642e-14, +4.27244001671195135429e-14,
|
| 1285 |
+
-2.10154184277266431302e-14, -4.08355111109219731823e-13,
|
| 1286 |
+
-7.19855177624590851209e-13, +2.03562854414708950722e-12,
|
| 1287 |
+
+1.41258074366137813316e-11, +3.25260358301548823856e-11,
|
| 1288 |
+
-1.89749581235054123450e-11, -5.58974346219658380687e-10,
|
| 1289 |
+
-3.83538038596423702205e-09, -2.63146884688951950684e-08,
|
| 1290 |
+
-2.51223623787020892529e-07, -3.88256480887769039346e-06,
|
| 1291 |
+
-1.10588938762623716291e-04, -9.76109749136146840777e-03,
|
| 1292 |
+
+7.78576235018280120474e-01,
|
| 1293 |
+
};
|
| 1294 |
+
|
| 1295 |
+
float p;
|
| 1296 |
+
float q = 0.0;
|
| 1297 |
+
|
| 1298 |
+
if (::metal::fabs(x) <= T(8.0)) {
|
| 1299 |
+
float a = A[0];
|
| 1300 |
+
|
| 1301 |
+
for (uint8_t index = 1; index < 29; index++) {
|
| 1302 |
+
p = q;
|
| 1303 |
+
q = a;
|
| 1304 |
+
a = (.5 * ::metal::fabs(x) - 2.0) * q - p + A[index];
|
| 1305 |
+
}
|
| 1306 |
+
|
| 1307 |
+
return .5 * (a - p) * x * ::metal::precise::exp(::metal::fabs(x));
|
| 1308 |
+
}
|
| 1309 |
+
|
| 1310 |
+
float b = B[0];
|
| 1311 |
+
|
| 1312 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1313 |
+
p = q;
|
| 1314 |
+
q = b;
|
| 1315 |
+
b = (32.0 / ::metal::fabs(x) - 2.0) * q - p + B[index];
|
| 1316 |
+
}
|
| 1317 |
+
|
| 1318 |
+
if (x < 0.0) {
|
| 1319 |
+
return -(
|
| 1320 |
+
::metal::precise::exp(::metal::fabs(x)) * (0.5 * (b - p)) /
|
| 1321 |
+
::metal::precise::sqrt(::metal::fabs(x)));
|
| 1322 |
+
}
|
| 1323 |
+
|
| 1324 |
+
return ::metal::precise::exp(::metal::fabs(x)) * (0.5 * (b - p)) /
|
| 1325 |
+
::metal::precise::sqrt(::metal::fabs(x));
|
| 1326 |
+
} // modified_bessel_i1_forward(T x)
|
| 1327 |
+
|
| 1328 |
+
template <typename T>
|
| 1329 |
+
inline float modified_bessel_k0_forward(T x) {
|
| 1330 |
+
constexpr float A[] = {
|
| 1331 |
+
+1.37446543561352307156e-16,
|
| 1332 |
+
+4.25981614279661018399e-14,
|
| 1333 |
+
+1.03496952576338420167e-11,
|
| 1334 |
+
+1.90451637722020886025e-09,
|
| 1335 |
+
+2.53479107902614945675e-07,
|
| 1336 |
+
+2.28621210311945178607e-05,
|
| 1337 |
+
+1.26461541144692592338e-03,
|
| 1338 |
+
+3.59799365153615016266e-02,
|
| 1339 |
+
+3.44289899924628486886e-01,
|
| 1340 |
+
-5.35327393233902768720e-01,
|
| 1341 |
+
};
|
| 1342 |
+
|
| 1343 |
+
constexpr float B[] = {
|
| 1344 |
+
+5.30043377268626276149e-18, -1.64758043015242134646e-17,
|
| 1345 |
+
+5.21039150503902756861e-17, -1.67823109680541210385e-16,
|
| 1346 |
+
+5.51205597852431940784e-16, -1.84859337734377901440e-15,
|
| 1347 |
+
+6.34007647740507060557e-15, -2.22751332699166985548e-14,
|
| 1348 |
+
+8.03289077536357521100e-14, -2.98009692317273043925e-13,
|
| 1349 |
+
+1.14034058820847496303e-12, -4.51459788337394416547e-12,
|
| 1350 |
+
+1.85594911495471785253e-11, -7.95748924447710747776e-11,
|
| 1351 |
+
+3.57739728140030116597e-10, -1.69753450938905987466e-09,
|
| 1352 |
+
+8.57403401741422608519e-09, -4.66048989768794782956e-08,
|
| 1353 |
+
+2.76681363944501510342e-07, -1.83175552271911948767e-06,
|
| 1354 |
+
+1.39498137188764993662e-05, -1.28495495816278026384e-04,
|
| 1355 |
+
+1.56988388573005337491e-03, -3.14481013119645005427e-02,
|
| 1356 |
+
+2.44030308206595545468e+00,
|
| 1357 |
+
};
|
| 1358 |
+
|
| 1359 |
+
if (x == 0.0) {
|
| 1360 |
+
return INFINITY;
|
| 1361 |
+
}
|
| 1362 |
+
|
| 1363 |
+
if (x < 0.0) {
|
| 1364 |
+
return NAN;
|
| 1365 |
+
}
|
| 1366 |
+
|
| 1367 |
+
float p;
|
| 1368 |
+
float q = 0.0;
|
| 1369 |
+
|
| 1370 |
+
if (x <= 2.0) {
|
| 1371 |
+
float a = A[0];
|
| 1372 |
+
|
| 1373 |
+
for (uint8_t index = 1; index < 10; index++) {
|
| 1374 |
+
p = q;
|
| 1375 |
+
q = a;
|
| 1376 |
+
a = (x * x - 2.0) * q - p + A[index];
|
| 1377 |
+
}
|
| 1378 |
+
|
| 1379 |
+
return 0.5 * (a - p) -
|
| 1380 |
+
::metal::log(0.5 * x) * modified_bessel_i0_forward(x);
|
| 1381 |
+
}
|
| 1382 |
+
|
| 1383 |
+
float b = B[0];
|
| 1384 |
+
|
| 1385 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1386 |
+
p = q;
|
| 1387 |
+
q = b;
|
| 1388 |
+
b = (8.0 / x - 2.0) * q - p + B[index];
|
| 1389 |
+
}
|
| 1390 |
+
|
| 1391 |
+
return ::metal::exp(-x) * (0.5 * (b - p)) / ::metal::sqrt(x);
|
| 1392 |
+
} // modified_bessel_k0_forward(T x)
|
| 1393 |
+
|
| 1394 |
+
template <typename T>
|
| 1395 |
+
inline float modified_bessel_k1_forward(T x) {
|
| 1396 |
+
constexpr float A[] = {
|
| 1397 |
+
-7.02386347938628759343e-18,
|
| 1398 |
+
-2.42744985051936593393e-15,
|
| 1399 |
+
-6.66690169419932900609e-13,
|
| 1400 |
+
-1.41148839263352776110e-10,
|
| 1401 |
+
-2.21338763073472585583e-08,
|
| 1402 |
+
-2.43340614156596823496e-06,
|
| 1403 |
+
-1.73028895751305206302e-04,
|
| 1404 |
+
-6.97572385963986435018e-03,
|
| 1405 |
+
-1.22611180822657148235e-01,
|
| 1406 |
+
-3.53155960776544875667e-01,
|
| 1407 |
+
+1.52530022733894777053e+00,
|
| 1408 |
+
};
|
| 1409 |
+
|
| 1410 |
+
constexpr float B[] = {
|
| 1411 |
+
-5.75674448366501715755e-18, +1.79405087314755922667e-17,
|
| 1412 |
+
-5.68946255844285935196e-17, +1.83809354436663880070e-16,
|
| 1413 |
+
-6.05704724837331885336e-16, +2.03870316562433424052e-15,
|
| 1414 |
+
-7.01983709041831346144e-15, +2.47715442448130437068e-14,
|
| 1415 |
+
-8.97670518232499435011e-14, +3.34841966607842919884e-13,
|
| 1416 |
+
-1.28917396095102890680e-12, +5.13963967348173025100e-12,
|
| 1417 |
+
-2.12996783842756842877e-11, +9.21831518760500529508e-11,
|
| 1418 |
+
-4.19035475934189648750e-10, +2.01504975519703286596e-09,
|
| 1419 |
+
-1.03457624656780970260e-08, +5.74108412545004946722e-08,
|
| 1420 |
+
-3.50196060308781257119e-07, +2.40648494783721712015e-06,
|
| 1421 |
+
-1.93619797416608296024e-05, +1.95215518471351631108e-04,
|
| 1422 |
+
-2.85781685962277938680e-03, +1.03923736576817238437e-01,
|
| 1423 |
+
+2.72062619048444266945e+00,
|
| 1424 |
+
};
|
| 1425 |
+
|
| 1426 |
+
if (x == 0.0) {
|
| 1427 |
+
return INFINITY;
|
| 1428 |
+
}
|
| 1429 |
+
|
| 1430 |
+
if (x < 0.0) {
|
| 1431 |
+
return NAN;
|
| 1432 |
+
}
|
| 1433 |
+
|
| 1434 |
+
float p;
|
| 1435 |
+
float q = 0.0;
|
| 1436 |
+
|
| 1437 |
+
if (x <= 2.0) {
|
| 1438 |
+
float a = A[0];
|
| 1439 |
+
|
| 1440 |
+
for (uint8_t index = 1; index < 11; index++) {
|
| 1441 |
+
p = q;
|
| 1442 |
+
q = a;
|
| 1443 |
+
a = (x * x - T(2.0)) * q - p + A[index];
|
| 1444 |
+
}
|
| 1445 |
+
|
| 1446 |
+
return ::metal::precise::log(T(0.5) * x) * modified_bessel_i1_forward(x) +
|
| 1447 |
+
0.5 * (a - p) / x;
|
| 1448 |
+
}
|
| 1449 |
+
|
| 1450 |
+
float b = B[0];
|
| 1451 |
+
|
| 1452 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1453 |
+
p = q;
|
| 1454 |
+
q = b;
|
| 1455 |
+
b = (8.0 / x - 2.0) * q - p + B[index];
|
| 1456 |
+
}
|
| 1457 |
+
|
| 1458 |
+
return ::metal::precise::exp(-x) * (0.5 * (b - p)) /
|
| 1459 |
+
::metal::precise::sqrt(x);
|
| 1460 |
+
}
|
| 1461 |
+
|
| 1462 |
+
template <typename T>
|
| 1463 |
+
inline float scaled_modified_bessel_k0_forward(T x) {
|
| 1464 |
+
constexpr float A[] = {
|
| 1465 |
+
+1.37446543561352307156e-16,
|
| 1466 |
+
+4.25981614279661018399e-14,
|
| 1467 |
+
+1.03496952576338420167e-11,
|
| 1468 |
+
+1.90451637722020886025e-09,
|
| 1469 |
+
+2.53479107902614945675e-07,
|
| 1470 |
+
+2.28621210311945178607e-05,
|
| 1471 |
+
+1.26461541144692592338e-03,
|
| 1472 |
+
+3.59799365153615016266e-02,
|
| 1473 |
+
+3.44289899924628486886e-01,
|
| 1474 |
+
-5.35327393233902768720e-01,
|
| 1475 |
+
};
|
| 1476 |
+
|
| 1477 |
+
constexpr float B[] = {
|
| 1478 |
+
+5.30043377268626276149e-18, -1.64758043015242134646e-17,
|
| 1479 |
+
+5.21039150503902756861e-17, -1.67823109680541210385e-16,
|
| 1480 |
+
+5.51205597852431940784e-16, -1.84859337734377901440e-15,
|
| 1481 |
+
+6.34007647740507060557e-15, -2.22751332699166985548e-14,
|
| 1482 |
+
+8.03289077536357521100e-14, -2.98009692317273043925e-13,
|
| 1483 |
+
+1.14034058820847496303e-12, -4.51459788337394416547e-12,
|
| 1484 |
+
+1.85594911495471785253e-11, -7.95748924447710747776e-11,
|
| 1485 |
+
+3.57739728140030116597e-10, -1.69753450938905987466e-09,
|
| 1486 |
+
+8.57403401741422608519e-09, -4.66048989768794782956e-08,
|
| 1487 |
+
+2.76681363944501510342e-07, -1.83175552271911948767e-06,
|
| 1488 |
+
+1.39498137188764993662e-05, -1.28495495816278026384e-04,
|
| 1489 |
+
+1.56988388573005337491e-03, -3.14481013119645005427e-02,
|
| 1490 |
+
+2.44030308206595545468e+00,
|
| 1491 |
+
};
|
| 1492 |
+
|
| 1493 |
+
if (x == 0.0) {
|
| 1494 |
+
return INFINITY;
|
| 1495 |
+
}
|
| 1496 |
+
|
| 1497 |
+
if (x < 0.0) {
|
| 1498 |
+
return NAN;
|
| 1499 |
+
}
|
| 1500 |
+
|
| 1501 |
+
float p;
|
| 1502 |
+
float q = 0.0;
|
| 1503 |
+
|
| 1504 |
+
if (x <= 2.0) {
|
| 1505 |
+
float a = A[0];
|
| 1506 |
+
|
| 1507 |
+
for (uint8_t index = 1; index < 10; index++) {
|
| 1508 |
+
p = q;
|
| 1509 |
+
q = a;
|
| 1510 |
+
a = (x * x - T(2.0)) * q - p + A[index];
|
| 1511 |
+
}
|
| 1512 |
+
|
| 1513 |
+
return (0.5 * (a - p) -
|
| 1514 |
+
::metal::precise::log(0.5 * x) * modified_bessel_i0_forward(x)) *
|
| 1515 |
+
::metal::precise::exp(x);
|
| 1516 |
+
}
|
| 1517 |
+
|
| 1518 |
+
float b = B[0];
|
| 1519 |
+
|
| 1520 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1521 |
+
p = q;
|
| 1522 |
+
q = b;
|
| 1523 |
+
b = (8.0 / x - 2.0) * q - p + B[index];
|
| 1524 |
+
}
|
| 1525 |
+
|
| 1526 |
+
return 0.5 * (b - p) / ::metal::precise::sqrt(x);
|
| 1527 |
+
}
|
| 1528 |
+
|
| 1529 |
+
template <typename T>
|
| 1530 |
+
inline float scaled_modified_bessel_k1_forward(T x) {
|
| 1531 |
+
constexpr float A[] = {
|
| 1532 |
+
-7.02386347938628759343e-18,
|
| 1533 |
+
-2.42744985051936593393e-15,
|
| 1534 |
+
-6.66690169419932900609e-13,
|
| 1535 |
+
-1.41148839263352776110e-10,
|
| 1536 |
+
-2.21338763073472585583e-08,
|
| 1537 |
+
-2.43340614156596823496e-06,
|
| 1538 |
+
-1.73028895751305206302e-04,
|
| 1539 |
+
-6.97572385963986435018e-03,
|
| 1540 |
+
-1.22611180822657148235e-01,
|
| 1541 |
+
-3.53155960776544875667e-01,
|
| 1542 |
+
+1.52530022733894777053e+00,
|
| 1543 |
+
};
|
| 1544 |
+
|
| 1545 |
+
constexpr float B[] = {
|
| 1546 |
+
-5.75674448366501715755e-18, +1.79405087314755922667e-17,
|
| 1547 |
+
-5.68946255844285935196e-17, +1.83809354436663880070e-16,
|
| 1548 |
+
-6.05704724837331885336e-16, +2.03870316562433424052e-15,
|
| 1549 |
+
-7.01983709041831346144e-15, +2.47715442448130437068e-14,
|
| 1550 |
+
-8.97670518232499435011e-14, +3.34841966607842919884e-13,
|
| 1551 |
+
-1.28917396095102890680e-12, +5.13963967348173025100e-12,
|
| 1552 |
+
-2.12996783842756842877e-11, +9.21831518760500529508e-11,
|
| 1553 |
+
-4.19035475934189648750e-10, +2.01504975519703286596e-09,
|
| 1554 |
+
-1.03457624656780970260e-08, +5.74108412545004946722e-08,
|
| 1555 |
+
-3.50196060308781257119e-07, +2.40648494783721712015e-06,
|
| 1556 |
+
-1.93619797416608296024e-05, +1.95215518471351631108e-04,
|
| 1557 |
+
-2.85781685962277938680e-03, +1.03923736576817238437e-01,
|
| 1558 |
+
+2.72062619048444266945e+00,
|
| 1559 |
+
};
|
| 1560 |
+
|
| 1561 |
+
if (x == 0.0) {
|
| 1562 |
+
return INFINITY;
|
| 1563 |
+
}
|
| 1564 |
+
|
| 1565 |
+
if (x < 0.0) {
|
| 1566 |
+
return NAN;
|
| 1567 |
+
}
|
| 1568 |
+
|
| 1569 |
+
float p;
|
| 1570 |
+
float q = 0.0;
|
| 1571 |
+
|
| 1572 |
+
if (x <= 2.0) {
|
| 1573 |
+
float a = A[0];
|
| 1574 |
+
|
| 1575 |
+
for (uint8_t index = 1; index < 11; index++) {
|
| 1576 |
+
p = q;
|
| 1577 |
+
q = a;
|
| 1578 |
+
a = (x * x - 2.0) * q - p + A[index];
|
| 1579 |
+
}
|
| 1580 |
+
|
| 1581 |
+
return (::metal::precise::log(0.5 * x) * modified_bessel_i1_forward(x) +
|
| 1582 |
+
0.5 * (a - p) / x) *
|
| 1583 |
+
::metal::precise::exp(x);
|
| 1584 |
+
}
|
| 1585 |
+
|
| 1586 |
+
float b = B[0];
|
| 1587 |
+
|
| 1588 |
+
for (uint8_t index = 1; index < 25; index++) {
|
| 1589 |
+
p = q;
|
| 1590 |
+
q = b;
|
| 1591 |
+
b = (8.0 / x - 2.0) * q - p + B[index];
|
| 1592 |
+
}
|
| 1593 |
+
|
| 1594 |
+
return (0.5 * (b - p) / ::metal::precise::sqrt(x));
|
| 1595 |
+
}
|
| 1596 |
+
|
| 1597 |
+
template <typename T>
|
| 1598 |
+
float chebyshev_polynomial_t_forward(T x, int64_t n) {
|
| 1599 |
+
if (n < 0) {
|
| 1600 |
+
return 0.0;
|
| 1601 |
+
}
|
| 1602 |
+
|
| 1603 |
+
if (::metal::fabs(x) == 1.0) {
|
| 1604 |
+
if (x > 0.0 || n % 2 == 0) {
|
| 1605 |
+
return 1.0;
|
| 1606 |
+
}
|
| 1607 |
+
|
| 1608 |
+
return -1.0;
|
| 1609 |
+
}
|
| 1610 |
+
|
| 1611 |
+
if ((n > 6) && (::metal::precise::fabs(x) < 1.0)) {
|
| 1612 |
+
return ::metal::precise::cos(n * ::metal::precise::acos(x));
|
| 1613 |
+
}
|
| 1614 |
+
|
| 1615 |
+
if (n == 0) {
|
| 1616 |
+
return 1.0;
|
| 1617 |
+
}
|
| 1618 |
+
|
| 1619 |
+
if (n == 1) {
|
| 1620 |
+
return x;
|
| 1621 |
+
}
|
| 1622 |
+
|
| 1623 |
+
float p = 1.0;
|
| 1624 |
+
float q = x;
|
| 1625 |
+
float r;
|
| 1626 |
+
|
| 1627 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1628 |
+
r = (x + x) * q - p;
|
| 1629 |
+
p = q;
|
| 1630 |
+
q = r;
|
| 1631 |
+
}
|
| 1632 |
+
return r;
|
| 1633 |
+
}
|
| 1634 |
+
|
| 1635 |
+
template <typename T>
|
| 1636 |
+
float chebyshev_polynomial_u_forward(T x, int64_t n) {
|
| 1637 |
+
if (n < 0) {
|
| 1638 |
+
return 0.0;
|
| 1639 |
+
}
|
| 1640 |
+
|
| 1641 |
+
if (::metal::fabs(x) == 1.0) {
|
| 1642 |
+
if (x > 0.0 || n % 2 == 0) {
|
| 1643 |
+
return n + 1;
|
| 1644 |
+
}
|
| 1645 |
+
|
| 1646 |
+
return -(n + 1);
|
| 1647 |
+
}
|
| 1648 |
+
|
| 1649 |
+
if ((n > 8) && (::metal::fabs(x) < 1.0)) {
|
| 1650 |
+
const auto acos_x = ::metal::precise::acos(x);
|
| 1651 |
+
if (::metal::precise::sin(acos_x) != 0.0) {
|
| 1652 |
+
return ::metal::precise::sin((n + 1) * acos_x) /
|
| 1653 |
+
::metal::precise::sin(acos_x);
|
| 1654 |
+
}
|
| 1655 |
+
|
| 1656 |
+
return (n + 1) * ::metal::precise::cos((n + 1) * acos_x) / x;
|
| 1657 |
+
}
|
| 1658 |
+
|
| 1659 |
+
if (n == 0) {
|
| 1660 |
+
return 1.0;
|
| 1661 |
+
}
|
| 1662 |
+
|
| 1663 |
+
auto q = 2.0 * x;
|
| 1664 |
+
if (n == 1) {
|
| 1665 |
+
return q;
|
| 1666 |
+
}
|
| 1667 |
+
|
| 1668 |
+
auto p = 1.0;
|
| 1669 |
+
float r;
|
| 1670 |
+
|
| 1671 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1672 |
+
r = 2 * x * q - p;
|
| 1673 |
+
p = q;
|
| 1674 |
+
q = r;
|
| 1675 |
+
}
|
| 1676 |
+
|
| 1677 |
+
return r;
|
| 1678 |
+
}
|
| 1679 |
+
|
| 1680 |
+
template <typename T>
|
| 1681 |
+
float chebyshev_polynomial_v_forward(T x, int64_t n) {
|
| 1682 |
+
if (n < 0) {
|
| 1683 |
+
return 0.0;
|
| 1684 |
+
}
|
| 1685 |
+
|
| 1686 |
+
if (::metal::fabs(x) == 1.0) {
|
| 1687 |
+
if (x > 0.0) {
|
| 1688 |
+
return 1.0;
|
| 1689 |
+
}
|
| 1690 |
+
|
| 1691 |
+
if (n % 2 == 0) {
|
| 1692 |
+
return n + n + 1;
|
| 1693 |
+
}
|
| 1694 |
+
|
| 1695 |
+
return -(n + n + 1);
|
| 1696 |
+
}
|
| 1697 |
+
|
| 1698 |
+
if ((n > 8) && (::metal::fabs(x) < 1.0)) {
|
| 1699 |
+
const auto acos_x = ::metal::precise::acos(x);
|
| 1700 |
+
if (::metal::precise::sin(.5 * acos_x) != 1.0) {
|
| 1701 |
+
return ::metal::precise::cos((n + 0.5) * acos_x) /
|
| 1702 |
+
::metal::precise::cos(.5 * acos_x);
|
| 1703 |
+
}
|
| 1704 |
+
|
| 1705 |
+
if (n % 2 == 0) {
|
| 1706 |
+
return n + n + 1;
|
| 1707 |
+
}
|
| 1708 |
+
|
| 1709 |
+
return -(n + n + 1);
|
| 1710 |
+
}
|
| 1711 |
+
|
| 1712 |
+
if (n == 0) {
|
| 1713 |
+
return 1.0;
|
| 1714 |
+
}
|
| 1715 |
+
|
| 1716 |
+
auto q = 2.0 * x - 1.0;
|
| 1717 |
+
if (n == 1) {
|
| 1718 |
+
return q;
|
| 1719 |
+
}
|
| 1720 |
+
|
| 1721 |
+
auto p = 1.0;
|
| 1722 |
+
float r;
|
| 1723 |
+
|
| 1724 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1725 |
+
r = 2 * x * q - p;
|
| 1726 |
+
p = q;
|
| 1727 |
+
q = r;
|
| 1728 |
+
}
|
| 1729 |
+
|
| 1730 |
+
return r;
|
| 1731 |
+
} // chebyshev_polynomial_v_forward(T x, int64_t n)
|
| 1732 |
+
|
| 1733 |
+
template <typename T>
|
| 1734 |
+
float chebyshev_polynomial_w_forward(T x, int64_t n) {
|
| 1735 |
+
if (n < 0) {
|
| 1736 |
+
return 0.0;
|
| 1737 |
+
}
|
| 1738 |
+
|
| 1739 |
+
if (::metal::fabs(x) == 1.0) {
|
| 1740 |
+
if (x > 0.0) {
|
| 1741 |
+
return n + n + 1;
|
| 1742 |
+
}
|
| 1743 |
+
|
| 1744 |
+
if (n % 2 == 0) {
|
| 1745 |
+
return 1.0;
|
| 1746 |
+
}
|
| 1747 |
+
|
| 1748 |
+
return -1.0;
|
| 1749 |
+
}
|
| 1750 |
+
|
| 1751 |
+
if ((n > 8) && (::metal::fabs(x) < 1.0)) {
|
| 1752 |
+
const auto acos_x = ::metal::precise::acos(x);
|
| 1753 |
+
if (::metal::precise::cos(.5 * acos_x) != 1.0) {
|
| 1754 |
+
return ::metal::precise::sin((n + 0.5) * acos_x) /
|
| 1755 |
+
::metal::precise::sin(.5 * acos_x);
|
| 1756 |
+
}
|
| 1757 |
+
|
| 1758 |
+
if (x > 0.0) {
|
| 1759 |
+
return n + n + 1;
|
| 1760 |
+
}
|
| 1761 |
+
|
| 1762 |
+
if (n % 2 == 0) {
|
| 1763 |
+
return 1.0;
|
| 1764 |
+
}
|
| 1765 |
+
|
| 1766 |
+
return -1.0;
|
| 1767 |
+
}
|
| 1768 |
+
|
| 1769 |
+
if (n == 0) {
|
| 1770 |
+
return 1.0;
|
| 1771 |
+
}
|
| 1772 |
+
|
| 1773 |
+
auto q = 2.0 * x + 1.0;
|
| 1774 |
+
if (n == 1) {
|
| 1775 |
+
return q;
|
| 1776 |
+
}
|
| 1777 |
+
|
| 1778 |
+
auto p = 1.0;
|
| 1779 |
+
float r;
|
| 1780 |
+
|
| 1781 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1782 |
+
r = 2.0 * x * q - p;
|
| 1783 |
+
p = q;
|
| 1784 |
+
q = r;
|
| 1785 |
+
}
|
| 1786 |
+
|
| 1787 |
+
return r;
|
| 1788 |
+
} // chebyshev_polynomial_w_forward(T x, int64_t n)
|
| 1789 |
+
|
| 1790 |
+
template <typename T>
|
| 1791 |
+
float shifted_chebyshev_polynomial_t_forward(T x, int64_t n) {
|
| 1792 |
+
if (n < 0) {
|
| 1793 |
+
return 0.0;
|
| 1794 |
+
}
|
| 1795 |
+
|
| 1796 |
+
if (x == T(1.0)) {
|
| 1797 |
+
return 1.0;
|
| 1798 |
+
}
|
| 1799 |
+
|
| 1800 |
+
if (x == 0.0) {
|
| 1801 |
+
if (n % 2 == 0) {
|
| 1802 |
+
return 1.0;
|
| 1803 |
+
}
|
| 1804 |
+
|
| 1805 |
+
return -1.0;
|
| 1806 |
+
}
|
| 1807 |
+
|
| 1808 |
+
const float xpxm1 = x + x - 1.0;
|
| 1809 |
+
if ((n > 6) && (::metal::abs(xpxm1) < 1.0)) {
|
| 1810 |
+
return ::metal::precise::cos(n * ::metal::precise::acos(xpxm1));
|
| 1811 |
+
}
|
| 1812 |
+
|
| 1813 |
+
if (n == 0) {
|
| 1814 |
+
return 1.0;
|
| 1815 |
+
}
|
| 1816 |
+
|
| 1817 |
+
if (n == 1) {
|
| 1818 |
+
return xpxm1;
|
| 1819 |
+
}
|
| 1820 |
+
|
| 1821 |
+
float p = 1.0;
|
| 1822 |
+
float q = xpxm1;
|
| 1823 |
+
float r;
|
| 1824 |
+
|
| 1825 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1826 |
+
r = (xpxm1 + xpxm1) * q - p;
|
| 1827 |
+
p = q;
|
| 1828 |
+
q = r;
|
| 1829 |
+
}
|
| 1830 |
+
|
| 1831 |
+
return r;
|
| 1832 |
+
} // shifted_chebyshev_polynomial_t_forward(T x, int64_t n)
|
| 1833 |
+
|
| 1834 |
+
template <typename T>
|
| 1835 |
+
float shifted_chebyshev_polynomial_u_forward(T x, int64_t n) {
|
| 1836 |
+
if (n < 0) {
|
| 1837 |
+
return 0.0;
|
| 1838 |
+
}
|
| 1839 |
+
|
| 1840 |
+
if (x == 1.0) {
|
| 1841 |
+
return n + 1;
|
| 1842 |
+
}
|
| 1843 |
+
|
| 1844 |
+
if (x == 0.0) {
|
| 1845 |
+
if (n % 2 == 0) {
|
| 1846 |
+
return n + 1;
|
| 1847 |
+
}
|
| 1848 |
+
|
| 1849 |
+
return -(n + 1);
|
| 1850 |
+
}
|
| 1851 |
+
const float xpxm1 = x + x - 1.0;
|
| 1852 |
+
if ((n > 6) && (::metal::abs(xpxm1) < 1.0)) {
|
| 1853 |
+
const float acos_2xm1 = ::metal::precise::acos(xpxm1);
|
| 1854 |
+
const float divisor = ::metal::precise::sin(acos_2xm1);
|
| 1855 |
+
if (divisor != 0.0) {
|
| 1856 |
+
return ::metal::precise::sin((n + 1) * acos_2xm1) / divisor;
|
| 1857 |
+
}
|
| 1858 |
+
|
| 1859 |
+
return (n + 1) * ::metal::precise::cos((n + 1) * acos_2xm1) / xpxm1;
|
| 1860 |
+
}
|
| 1861 |
+
|
| 1862 |
+
if (n == 0) {
|
| 1863 |
+
return 1.0;
|
| 1864 |
+
}
|
| 1865 |
+
|
| 1866 |
+
if (n == 1) {
|
| 1867 |
+
return xpxm1 + xpxm1;
|
| 1868 |
+
}
|
| 1869 |
+
|
| 1870 |
+
float p = 1.0;
|
| 1871 |
+
float q = xpxm1 + xpxm1;
|
| 1872 |
+
float r;
|
| 1873 |
+
|
| 1874 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1875 |
+
r = (xpxm1 + xpxm1) * q - p;
|
| 1876 |
+
p = q;
|
| 1877 |
+
q = r;
|
| 1878 |
+
}
|
| 1879 |
+
|
| 1880 |
+
return r;
|
| 1881 |
+
} // shifted_chebyshev_polynomial_u_forward(T x, int64_t n)
|
| 1882 |
+
|
| 1883 |
+
template <typename T>
|
| 1884 |
+
float shifted_chebyshev_polynomial_v_forward(T x, int64_t n) {
|
| 1885 |
+
if (n < 0) {
|
| 1886 |
+
return 0.0;
|
| 1887 |
+
}
|
| 1888 |
+
|
| 1889 |
+
if (x == 1.0) {
|
| 1890 |
+
return 1.0;
|
| 1891 |
+
}
|
| 1892 |
+
|
| 1893 |
+
if (x == 0.0) {
|
| 1894 |
+
if (n % 2 == 0) {
|
| 1895 |
+
return (n + n + 1);
|
| 1896 |
+
}
|
| 1897 |
+
|
| 1898 |
+
return -(n + n + 1);
|
| 1899 |
+
}
|
| 1900 |
+
|
| 1901 |
+
const float xpxm1 = x + x - 1.0;
|
| 1902 |
+
if ((n > 6) && (::metal::abs(xpxm1) < 1.0)) {
|
| 1903 |
+
const float acos_2xm1 = ::metal::precise::acos(xpxm1);
|
| 1904 |
+
if (::metal::precise::sin(acos_2xm1 / 2.0) != 1.0) {
|
| 1905 |
+
return ::metal::precise::cos((n + 0.5) * acos_2xm1) /
|
| 1906 |
+
::metal::precise::cos(acos_2xm1 / 2.0);
|
| 1907 |
+
}
|
| 1908 |
+
|
| 1909 |
+
if (n % 2 == 0) {
|
| 1910 |
+
return n + n + 1;
|
| 1911 |
+
}
|
| 1912 |
+
|
| 1913 |
+
return -(n + n + 1);
|
| 1914 |
+
}
|
| 1915 |
+
|
| 1916 |
+
if (n == 0) {
|
| 1917 |
+
return T(1.0);
|
| 1918 |
+
}
|
| 1919 |
+
|
| 1920 |
+
if (n == 1) {
|
| 1921 |
+
return xpxm1 + xpxm1 - 1.0;
|
| 1922 |
+
}
|
| 1923 |
+
|
| 1924 |
+
float p = 1.0;
|
| 1925 |
+
float q = xpxm1 + xpxm1 - 1.0;
|
| 1926 |
+
float r;
|
| 1927 |
+
|
| 1928 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1929 |
+
r = (xpxm1 + xpxm1) * q - p;
|
| 1930 |
+
p = q;
|
| 1931 |
+
q = r;
|
| 1932 |
+
}
|
| 1933 |
+
|
| 1934 |
+
return r;
|
| 1935 |
+
} // shifted_chebyshev_polynomial_v_forward(T x, int64_t n)
|
| 1936 |
+
|
| 1937 |
+
template <typename T>
|
| 1938 |
+
float shifted_chebyshev_polynomial_w_forward(T x, int64_t n) {
|
| 1939 |
+
if (n < 0) {
|
| 1940 |
+
return 0.0;
|
| 1941 |
+
}
|
| 1942 |
+
|
| 1943 |
+
if (x == 1.0) {
|
| 1944 |
+
return n + n + 1;
|
| 1945 |
+
}
|
| 1946 |
+
|
| 1947 |
+
if (x == 0.0) {
|
| 1948 |
+
if (n % 2 == 0) {
|
| 1949 |
+
return 1.0;
|
| 1950 |
+
}
|
| 1951 |
+
|
| 1952 |
+
return -1.0;
|
| 1953 |
+
}
|
| 1954 |
+
|
| 1955 |
+
const float xpxm1 = x + x - 1.0;
|
| 1956 |
+
if ((n > 4) && (::metal::abs(xpxm1) < 1.0)) {
|
| 1957 |
+
const float acos_2xm1 = ::metal::precise::acos(xpxm1);
|
| 1958 |
+
if (::metal::precise::cos(acos_2xm1 / 2.0) != 1.0) {
|
| 1959 |
+
return ::metal::precise::sin((n + 0.5) * acos_2xm1) /
|
| 1960 |
+
::metal::precise::sin(acos_2xm1 / 2.0);
|
| 1961 |
+
}
|
| 1962 |
+
|
| 1963 |
+
if (n % 2 == 0) {
|
| 1964 |
+
return 1.0;
|
| 1965 |
+
}
|
| 1966 |
+
|
| 1967 |
+
return -1.0;
|
| 1968 |
+
}
|
| 1969 |
+
|
| 1970 |
+
if (n == 0) {
|
| 1971 |
+
return 1.0;
|
| 1972 |
+
}
|
| 1973 |
+
|
| 1974 |
+
if (n == 1) {
|
| 1975 |
+
return xpxm1 + xpxm1 + 1.0;
|
| 1976 |
+
}
|
| 1977 |
+
|
| 1978 |
+
float p = 1.0;
|
| 1979 |
+
float q = xpxm1 + xpxm1 + 1.0;
|
| 1980 |
+
float r;
|
| 1981 |
+
|
| 1982 |
+
for (int64_t k = 2; (k <= n) && !::metal::isnan(q); k++) {
|
| 1983 |
+
r = (xpxm1 + xpxm1) * q - p;
|
| 1984 |
+
p = q;
|
| 1985 |
+
q = r;
|
| 1986 |
+
}
|
| 1987 |
+
|
| 1988 |
+
return r;
|
| 1989 |
+
} // shifted_chebyshev_polynomial_w_forward(T x, int64_t n)
|
| 1990 |
+
|
| 1991 |
+
template <typename T>
|
| 1992 |
+
// TODO: Add 512 if/when double will be supported in Metal
|
| 1993 |
+
inline constexpr int getHermitianLimit() {
|
| 1994 |
+
return 128;
|
| 1995 |
+
}
|
| 1996 |
+
|
| 1997 |
+
template <typename T>
|
| 1998 |
+
inline float hermite_polynomial_h_forward(T x, int64_t n) {
|
| 1999 |
+
if (n < 0) {
|
| 2000 |
+
return 0.0;
|
| 2001 |
+
}
|
| 2002 |
+
|
| 2003 |
+
if (n == 0) {
|
| 2004 |
+
return 1.0;
|
| 2005 |
+
}
|
| 2006 |
+
|
| 2007 |
+
if (n == 1) {
|
| 2008 |
+
return x + x;
|
| 2009 |
+
}
|
| 2010 |
+
|
| 2011 |
+
if (n > getHermitianLimit<T>()) {
|
| 2012 |
+
return NAN;
|
| 2013 |
+
}
|
| 2014 |
+
|
| 2015 |
+
float p = 1.0;
|
| 2016 |
+
float q = x + x;
|
| 2017 |
+
float r = 0.0;
|
| 2018 |
+
|
| 2019 |
+
for (int64_t k = 2; k < n + n; k += 2) {
|
| 2020 |
+
r = (x + x) * q - k * p;
|
| 2021 |
+
p = q;
|
| 2022 |
+
q = r;
|
| 2023 |
+
}
|
| 2024 |
+
|
| 2025 |
+
return r;
|
| 2026 |
+
} // hermite_polynomial_h_forward(T x, int64_t n)
|
| 2027 |
+
|
| 2028 |
+
template <typename T>
|
| 2029 |
+
inline float hermite_polynomial_he_forward(T x, int64_t n) {
|
| 2030 |
+
if (n < 0) {
|
| 2031 |
+
return 0.0;
|
| 2032 |
+
}
|
| 2033 |
+
|
| 2034 |
+
if (n == 0) {
|
| 2035 |
+
return 1.0;
|
| 2036 |
+
}
|
| 2037 |
+
|
| 2038 |
+
if (n == 1) {
|
| 2039 |
+
return x;
|
| 2040 |
+
}
|
| 2041 |
+
|
| 2042 |
+
if (n > getHermitianLimit<T>()) {
|
| 2043 |
+
return NAN;
|
| 2044 |
+
}
|
| 2045 |
+
|
| 2046 |
+
float p = 1.0;
|
| 2047 |
+
float q = x;
|
| 2048 |
+
float r;
|
| 2049 |
+
|
| 2050 |
+
for (int64_t k = 1; k < n; k++) {
|
| 2051 |
+
r = x * q - k * p;
|
| 2052 |
+
p = q;
|
| 2053 |
+
q = r;
|
| 2054 |
+
}
|
| 2055 |
+
|
| 2056 |
+
return r;
|
| 2057 |
+
} // hermite_polynomial_he_forward(T x, int64_t n)
|
| 2058 |
+
|
| 2059 |
+
} // namespace metal
|
| 2060 |
+
} // namespace c10
|
| 2061 |
+
|
| 2062 |
+
#else
|
| 2063 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 2064 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/metal/utils.h
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Metal helper functions
|
| 3 |
+
#pragma once
|
| 4 |
+
#include <c10/metal/common.h>
|
| 5 |
+
#include <metal_stdlib>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
namespace metal {
|
| 9 |
+
|
| 10 |
+
namespace detail {
|
| 11 |
+
template <typename T>
|
| 12 |
+
struct vectypes {};
|
| 13 |
+
|
| 14 |
+
template <>
|
| 15 |
+
struct vectypes<float> {
|
| 16 |
+
using type4 = float4;
|
| 17 |
+
using type3 = float3;
|
| 18 |
+
using type2 = float2;
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
template <>
|
| 22 |
+
struct vectypes<half> {
|
| 23 |
+
using type4 = half4;
|
| 24 |
+
using type3 = half3;
|
| 25 |
+
using type2 = half2;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
template <>
|
| 29 |
+
struct vectypes<bfloat> {
|
| 30 |
+
using type4 = bfloat4;
|
| 31 |
+
using type3 = bfloat3;
|
| 32 |
+
using type2 = bfloat2;
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
template <>
|
| 36 |
+
struct vectypes<short> {
|
| 37 |
+
using type4 = short4;
|
| 38 |
+
using type3 = short3;
|
| 39 |
+
using type2 = short2;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
template <>
|
| 43 |
+
struct vectypes<int> {
|
| 44 |
+
using type4 = int4;
|
| 45 |
+
using type3 = int3;
|
| 46 |
+
using type2 = int2;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
template <>
|
| 50 |
+
struct vectypes<long> {
|
| 51 |
+
using type4 = short4;
|
| 52 |
+
using type3 = short3;
|
| 53 |
+
using type2 = short2;
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
template <typename T>
|
| 57 |
+
struct OpMathType {
|
| 58 |
+
using type = T;
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
template <>
|
| 62 |
+
struct OpMathType<half> {
|
| 63 |
+
using type = float;
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
template <>
|
| 67 |
+
struct OpMathType<short> {
|
| 68 |
+
using type = int;
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
template <>
|
| 72 |
+
struct OpMathType<char> {
|
| 73 |
+
using type = int;
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
template <>
|
| 77 |
+
struct OpMathType<uchar> {
|
| 78 |
+
using type = int;
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
template <>
|
| 82 |
+
struct OpMathType<bfloat> {
|
| 83 |
+
using type = float;
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
// Type promotion structure for higher precision accumulation
|
| 87 |
+
template <typename T>
|
| 88 |
+
struct AccumulationType {
|
| 89 |
+
using type = T;
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
// Specialization for half - promote to float for accumulation
|
| 93 |
+
template <>
|
| 94 |
+
struct AccumulationType<half> {
|
| 95 |
+
using type = float;
|
| 96 |
+
};
|
| 97 |
+
|
| 98 |
+
// Specialization for bfloat - promote to float for accumulation
|
| 99 |
+
template <>
|
| 100 |
+
struct AccumulationType<bfloat> {
|
| 101 |
+
using type = float;
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
} // namespace detail
|
| 105 |
+
|
| 106 |
+
template <typename T>
|
| 107 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, T> max(T a, T b) {
|
| 108 |
+
return ::metal::isunordered(a, b) ? NAN : ::metal::max(a, b);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
template <typename T, typename U>
|
| 112 |
+
::metal::enable_if_t<::metal::is_integral_v<T>&& ::metal::is_integral_v<U>, T>
|
| 113 |
+
max(T a, U b) {
|
| 114 |
+
return ::metal::max(a, static_cast<T>(b));
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
template <typename T>
|
| 118 |
+
::metal::enable_if_t<::metal::is_floating_point_v<T>, T> min(T a, T b) {
|
| 119 |
+
return ::metal::isunordered(a, b) ? NAN : ::metal::min(a, b);
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
template <typename T, typename U>
|
| 123 |
+
::metal::enable_if_t<::metal::is_integral_v<T>&& ::metal::is_integral_v<U>, T>
|
| 124 |
+
min(T a, U b) {
|
| 125 |
+
return ::metal::min(a, static_cast<T>(b));
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
template <>
|
| 129 |
+
inline bfloat min(bfloat a, bfloat b) {
|
| 130 |
+
return bfloat(
|
| 131 |
+
::metal::isunordered(a, b) ? NAN : ::metal::min(float(a), float(b)));
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
template <>
|
| 135 |
+
inline bfloat max(bfloat a, bfloat b) {
|
| 136 |
+
return bfloat(
|
| 137 |
+
::metal::isunordered(a, b) ? NAN : ::metal::max(float(a), float(b)));
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
template <typename T>
|
| 141 |
+
using vec2type_t = typename detail::vectypes<T>::type2;
|
| 142 |
+
|
| 143 |
+
template <typename T>
|
| 144 |
+
using vec4type_t = typename detail::vectypes<T>::type4;
|
| 145 |
+
|
| 146 |
+
template <typename T>
|
| 147 |
+
using opmath_t = typename detail::OpMathType<T>::type;
|
| 148 |
+
|
| 149 |
+
template <typename T>
|
| 150 |
+
using accum_t = typename detail::AccumulationType<T>::type;
|
| 151 |
+
|
| 152 |
+
// TODO: Move it to type_traits header may be
|
| 153 |
+
template <typename F, typename... Args>
|
| 154 |
+
using result_of = decltype(::metal::declval<F>()(::metal::declval<Args>()...));
|
| 155 |
+
|
| 156 |
+
template <typename T>
|
| 157 |
+
constexpr constant bool is_complex_v =
|
| 158 |
+
::metal::is_same_v<T, float2> || ::metal::is_same_v<T, half2>;
|
| 159 |
+
|
| 160 |
+
template <typename T>
|
| 161 |
+
constexpr constant bool is_scalar_floating_point_v =
|
| 162 |
+
::metal::is_floating_point_v<T> && ::metal::is_scalar_v<T>;
|
| 163 |
+
|
| 164 |
+
template <typename T>
|
| 165 |
+
constexpr constant bool is_scalar_integral_v =
|
| 166 |
+
::metal::is_integral_v<T> && ::metal::is_scalar_v<T>;
|
| 167 |
+
|
| 168 |
+
template <typename U, typename V>
|
| 169 |
+
using common_dtype = decltype(U(0) + V(0));
|
| 170 |
+
|
| 171 |
+
// floor_divide
|
| 172 |
+
template <
|
| 173 |
+
typename T,
|
| 174 |
+
typename U,
|
| 175 |
+
::metal::enable_if_t<
|
| 176 |
+
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
|
| 177 |
+
bool> = true>
|
| 178 |
+
inline common_dtype<T, U> floor_divide(T x, U y) {
|
| 179 |
+
const auto quot = x / y;
|
| 180 |
+
return (x < 0) == (y < 0) ? quot : (x % y != 0) ? quot - 1 : quot;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
template <
|
| 184 |
+
typename T,
|
| 185 |
+
typename U,
|
| 186 |
+
::metal::enable_if_t<
|
| 187 |
+
is_scalar_floating_point_v<T> && is_scalar_floating_point_v<U>,
|
| 188 |
+
bool> = true>
|
| 189 |
+
inline common_dtype<T, U> floor_divide(T x, U y) {
|
| 190 |
+
return ::metal::floor(x / y);
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// fmod
|
| 194 |
+
template <
|
| 195 |
+
typename T,
|
| 196 |
+
typename U,
|
| 197 |
+
::metal::enable_if_t<
|
| 198 |
+
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
|
| 199 |
+
bool> = true>
|
| 200 |
+
inline common_dtype<T, U> fmod(T x, U y) {
|
| 201 |
+
return x % y;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
template <
|
| 205 |
+
typename T,
|
| 206 |
+
typename U,
|
| 207 |
+
::metal::enable_if_t<
|
| 208 |
+
is_scalar_floating_point_v<T> && is_scalar_floating_point_v<U>,
|
| 209 |
+
bool> = true>
|
| 210 |
+
inline common_dtype<T, U> fmod(T x, U y) {
|
| 211 |
+
return ::metal::fmod(x, y);
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
// cast_to primitives
|
| 215 |
+
// - No-op if types as the same
|
| 216 |
+
template <
|
| 217 |
+
typename T,
|
| 218 |
+
typename U,
|
| 219 |
+
::metal::enable_if_t<::metal::is_same_v<U, T>, bool> = true>
|
| 220 |
+
inline T cast_to(const U from) {
|
| 221 |
+
return from;
|
| 222 |
+
}
|
| 223 |
+
// - Simple cast between scalar and complex dtypes
|
| 224 |
+
template <
|
| 225 |
+
typename T,
|
| 226 |
+
typename U,
|
| 227 |
+
::metal::enable_if_t<
|
| 228 |
+
!::metal::is_same_v<U, T> && (is_complex_v<T> == is_complex_v<U>),
|
| 229 |
+
bool> = true>
|
| 230 |
+
inline T cast_to(const U from) {
|
| 231 |
+
return static_cast<T>(from);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
// - Scalar to complex
|
| 235 |
+
template <
|
| 236 |
+
typename T,
|
| 237 |
+
typename U,
|
| 238 |
+
::metal::enable_if_t<is_complex_v<T> && !is_complex_v<U>, bool> = true>
|
| 239 |
+
inline T cast_to(const U from) {
|
| 240 |
+
return T(float(from), 0.0);
|
| 241 |
+
}
|
| 242 |
+
// - Complex to scalar (should not really be used, but exists for compliteness)
|
| 243 |
+
template <
|
| 244 |
+
typename T,
|
| 245 |
+
typename U,
|
| 246 |
+
::metal::enable_if_t<!is_complex_v<T> && is_complex_v<U>, bool> = true>
|
| 247 |
+
inline T cast_to(const U from) {
|
| 248 |
+
return static_cast<T>(from.x);
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
// Generalizable math operators (used for both scalar and complex)
|
| 252 |
+
|
| 253 |
+
template <
|
| 254 |
+
typename T,
|
| 255 |
+
typename U,
|
| 256 |
+
::metal::enable_if_t<!is_complex_v<T>, bool> = true>
|
| 257 |
+
inline common_dtype<T, U> mul(const T x, const U y) {
|
| 258 |
+
return x * y;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
template <
|
| 262 |
+
typename T,
|
| 263 |
+
typename U,
|
| 264 |
+
::metal::enable_if_t<is_complex_v<T> && is_complex_v<U>, bool> = true>
|
| 265 |
+
inline common_dtype<T, U> mul(const T x, const U y) {
|
| 266 |
+
return T(x.x * y.x - x.y * y.y, x.x * y.y + x.y * y.x);
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
template <
|
| 270 |
+
typename T,
|
| 271 |
+
typename U,
|
| 272 |
+
::metal::enable_if_t<!is_complex_v<T>, bool> = true>
|
| 273 |
+
inline common_dtype<T, U> div(const T x, const U y) {
|
| 274 |
+
return x / y;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
template <
|
| 278 |
+
typename T,
|
| 279 |
+
typename U,
|
| 280 |
+
::metal::enable_if_t<is_complex_v<T> && is_complex_v<U>, bool> = true>
|
| 281 |
+
inline common_dtype<T, U> div(const T x, const U y) {
|
| 282 |
+
return T(::metal::dot(x, y), x.y * y.x - x.x * y.y) / ::metal::dot(y, y);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
// Remainder operator
|
| 286 |
+
template <
|
| 287 |
+
typename T,
|
| 288 |
+
typename U,
|
| 289 |
+
::metal::enable_if_t<
|
| 290 |
+
is_scalar_floating_point_v<T> || is_scalar_floating_point_v<U>,
|
| 291 |
+
bool> = true>
|
| 292 |
+
inline float remainder(const T x, const U y) {
|
| 293 |
+
const auto x_f = static_cast<float>(x);
|
| 294 |
+
const auto y_f = static_cast<float>(y);
|
| 295 |
+
return x_f - y_f * floor_divide(x_f, y_f);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
template <
|
| 299 |
+
typename T,
|
| 300 |
+
typename U,
|
| 301 |
+
::metal::enable_if_t<
|
| 302 |
+
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
|
| 303 |
+
bool> = true>
|
| 304 |
+
inline common_dtype<T, U> remainder(const T x, const U y) {
|
| 305 |
+
auto rc = x % y;
|
| 306 |
+
return rc == 0 || (x ^ y) > 0 ? rc : rc + y;
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
// Based on algorithm described in
|
| 310 |
+
// https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#1202
|
| 311 |
+
inline float log1p(float x) {
|
| 312 |
+
const auto xp1 = 1.0f + x;
|
| 313 |
+
// First two elements of Taylor series for log(1+x) in Horner's form are:
|
| 314 |
+
// log(1+x) = x * (1 - x * (.5 ...)), but if 1 + x == x, then it's just x
|
| 315 |
+
if (xp1 == 1.0f) {
|
| 316 |
+
return x;
|
| 317 |
+
}
|
| 318 |
+
auto rc = ::metal::precise::log(xp1);
|
| 319 |
+
if (x > -.5 && x < .5) {
|
| 320 |
+
// Order of operations is important here for higher precision
|
| 321 |
+
rc *= x / (xp1 - 1.0f);
|
| 322 |
+
}
|
| 323 |
+
return rc;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
// The function is ported from mlx
|
| 327 |
+
inline float2 log1p(float2 in) {
|
| 328 |
+
float x = in.x;
|
| 329 |
+
float y = in.y;
|
| 330 |
+
float zabs = ::metal::precise::sqrt(x * x + y * y);
|
| 331 |
+
float theta = ::metal::atan2(y, x + 1);
|
| 332 |
+
if (zabs < 0.5f) {
|
| 333 |
+
float r = x * (2 + x) + y * y;
|
| 334 |
+
if (r == 0) { // handle underflow
|
| 335 |
+
return {x, theta};
|
| 336 |
+
}
|
| 337 |
+
return {0.5f * log1p(r), theta};
|
| 338 |
+
} else {
|
| 339 |
+
auto z0 = ::metal::sqrt((x + 1) * (x + 1) + y * y);
|
| 340 |
+
return {::metal::log(z0), theta};
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
template <typename T1, typename T2 = T1>
|
| 345 |
+
struct pair {
|
| 346 |
+
T1 first;
|
| 347 |
+
T2 second;
|
| 348 |
+
};
|
| 349 |
+
|
| 350 |
+
template <typename T>
|
| 351 |
+
inline T conj(T a) {
|
| 352 |
+
return a;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
template <>
|
| 356 |
+
inline half2 conj(half2 a) {
|
| 357 |
+
return half2(a.x, -a.y);
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
template <>
|
| 361 |
+
inline float2 conj(float2 a) {
|
| 362 |
+
return float2(a.x, -a.y);
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
#define INSTANTIATE_FOR_ALL_TYPES(MACRO) \
|
| 366 |
+
MACRO(float); \
|
| 367 |
+
MACRO(half); \
|
| 368 |
+
MACRO(bfloat); \
|
| 369 |
+
MACRO(float2); \
|
| 370 |
+
MACRO(long); \
|
| 371 |
+
MACRO(char); \
|
| 372 |
+
MACRO(uchar); \
|
| 373 |
+
MACRO(short); \
|
| 374 |
+
MACRO(int);
|
| 375 |
+
|
| 376 |
+
#define INSTANTIATE_FOR_FLOAT_TYPES(MACRO) \
|
| 377 |
+
MACRO(float); \
|
| 378 |
+
MACRO(half); \
|
| 379 |
+
MACRO(bfloat);
|
| 380 |
+
|
| 381 |
+
} // namespace metal
|
| 382 |
+
} // namespace c10
|
| 383 |
+
|
| 384 |
+
#else
|
| 385 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 386 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/mobile/CPUCachingAllocator.h
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <cstddef>
|
| 5 |
+
#include <mutex>
|
| 6 |
+
|
| 7 |
+
#include <c10/macros/Export.h>
|
| 8 |
+
#include <c10/util/SmallVector.h>
|
| 9 |
+
#include <c10/util/flat_hash_map.h>
|
| 10 |
+
|
| 11 |
+
/*
|
| 12 |
+
* CPUCachingAllocator:
|
| 13 |
+
* DISCLAIMER:
|
| 14 |
+
* This is subject to change (beta) and only supported on mobile builds.
|
| 15 |
+
* If code snippet such as in 'Usage pattern' is used outside of mobile
|
| 16 |
+
* build you will not observe the intended behavior.
|
| 17 |
+
* See below for more information.
|
| 18 |
+
* Why?
|
| 19 |
+
* It has been observed that some mobile platforms, such as pixel 3, return
|
| 20 |
+
* memory aggressively to the system. This results in page faults in some
|
| 21 |
+
* cases and ends up hurting performance. This caching allocator aims to address
|
| 22 |
+
* that. Furthermore it also allows users to specify their own allocator by
|
| 23 |
+
* implementing allocate/free virtual interfaces. What are the cons? There are
|
| 24 |
+
* some cons that were observed where use of caching allocator led to worse
|
| 25 |
+
* performance on some platforms. Reason being that the caching mechanism used
|
| 26 |
+
* by this allocator left us worse off compared to the corresponding platform's
|
| 27 |
+
* tuned memory allocator. In that case it seemed better to not use this
|
| 28 |
+
* allocator. Note there are some ideas to fix this in the works.
|
| 29 |
+
*
|
| 30 |
+
* Usage:
|
| 31 |
+
* Usage pattern:
|
| 32 |
+
* Instantiate and own the caching allocator.
|
| 33 |
+
* std::unique_ptr<c10::CPUCachingAllocator> caching_allocator =
|
| 34 |
+
* std::make_unique<c10::CPUCachingAllocator>();
|
| 35 |
+
* Use caching allocator with a scoped guard at inference time.
|
| 36 |
+
* {
|
| 37 |
+
* WithCPUCachingAllocatorGuard(caching_allocator.get());
|
| 38 |
+
* ... model.forward(...);
|
| 39 |
+
* }
|
| 40 |
+
*/
|
| 41 |
+
|
| 42 |
+
namespace c10 {
|
| 43 |
+
|
| 44 |
+
class C10_API CPUCachingAllocator {
|
| 45 |
+
/*
|
| 46 |
+
* What it does:
|
| 47 |
+
* Caches all the allocations carried out by this allocator.
|
| 48 |
+
* Cache key is the size of the allocation.
|
| 49 |
+
* If requested size is found in the cache returns the cached pointer.
|
| 50 |
+
* What it does not do:
|
| 51 |
+
* No speculative allocation for any future allocations.
|
| 52 |
+
*/
|
| 53 |
+
private:
|
| 54 |
+
inline void* allocate_and_cache(const size_t bytes);
|
| 55 |
+
void free_cached();
|
| 56 |
+
|
| 57 |
+
protected:
|
| 58 |
+
// Invariants.
|
| 59 |
+
// 1. If memory is ever allocated via this allocator then
|
| 60 |
+
// the pointer will exist in allocation_map_, unless the allocator
|
| 61 |
+
// returned the memory to OS via free_cached.
|
| 62 |
+
// 1.1. Therefore even when the said memory is "freed" via this
|
| 63 |
+
// allocator (and thus cached), it will continue to stay
|
| 64 |
+
// in allocation_map_. Furthermore it will also exist in
|
| 65 |
+
// available_map_. Thus an allocated memory pointer can be in both
|
| 66 |
+
// allocation_map_ and available_map_ simultaneously.
|
| 67 |
+
// 2. Memory pointer maybe removed from allocation_map_, when it
|
| 68 |
+
// is freed outside of the scope of this allocator, but was allocated
|
| 69 |
+
// by this allocator.
|
| 70 |
+
// 3. Available map only contains that memory which was allocated
|
| 71 |
+
// by this allocator and subsequently freed by this allocator.
|
| 72 |
+
// As a result of above invariants, allocated memory ptr cannot be in
|
| 73 |
+
// available_map_ unless it is in allocation_map_ as well.
|
| 74 |
+
ska::flat_hash_map<size_t, c10::SmallVector<void*, 16>> available_map_;
|
| 75 |
+
static ska::flat_hash_map<void*, size_t> allocation_map_;
|
| 76 |
+
// Since allocation_map, which is a global instance, is mutated/read via
|
| 77 |
+
// all public APIs we need a global mutex.
|
| 78 |
+
static std::mutex mutex_;
|
| 79 |
+
|
| 80 |
+
public:
|
| 81 |
+
static void record_free(void* ptr);
|
| 82 |
+
virtual ~CPUCachingAllocator();
|
| 83 |
+
// Checks the cache to see if allocation of size bytes can be found.
|
| 84 |
+
// If so return cached memory, else
|
| 85 |
+
// allocates memory, records it for caching and returns.
|
| 86 |
+
virtual void* allocate(const size_t bytes);
|
| 87 |
+
// Checks if the memory being freed is was marked for allocation by
|
| 88 |
+
// an earlier call to allocate. If so cache the allocation.
|
| 89 |
+
// Otherwise free.
|
| 90 |
+
virtual void free(void* ptr);
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
CPUCachingAllocator* GetDefaultCPUCachingAllocator();
|
| 94 |
+
|
| 95 |
+
bool ThreadLocalCachingAllocatorEnabled();
|
| 96 |
+
CPUCachingAllocator* GetThreadLocalCachingAllocator();
|
| 97 |
+
|
| 98 |
+
class C10_API WithCPUCachingAllocatorGuard {
|
| 99 |
+
public:
|
| 100 |
+
WithCPUCachingAllocatorGuard(CPUCachingAllocator* allocator);
|
| 101 |
+
~WithCPUCachingAllocatorGuard();
|
| 102 |
+
|
| 103 |
+
private:
|
| 104 |
+
CPUCachingAllocator* prev_caching_allocator_ptr_{nullptr};
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
} // namespace c10
|
| 108 |
+
|
| 109 |
+
#else
|
| 110 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 111 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/mobile/CPUProfilingAllocator.h
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/macros/Export.h>
|
| 5 |
+
#include <c10/util/flat_hash_map.h>
|
| 6 |
+
#include <cstddef>
|
| 7 |
+
#include <cstdint>
|
| 8 |
+
#include <memory>
|
| 9 |
+
#include <vector>
|
| 10 |
+
|
| 11 |
+
namespace c10 {
|
| 12 |
+
|
| 13 |
+
/*
|
| 14 |
+
* Given a sequence of allocations in a thread, AllocationPlan records
|
| 15 |
+
* 1. size of each allocation
|
| 16 |
+
* 2. Lifetime of each allocation.
|
| 17 |
+
* 3. allocation offsets: Memory offset for each allocation in a single blob of
|
| 18 |
+
* memory
|
| 19 |
+
* 4. Total size of a blob of memory required to satisfy all the allocations.
|
| 20 |
+
*/
|
| 21 |
+
class C10_API AllocationPlan {
|
| 22 |
+
private:
|
| 23 |
+
// Records size of each allocation by their sequential allocation ids.
|
| 24 |
+
std::vector<uint64_t> allocation_sizes;
|
| 25 |
+
// This maps one allocation id (X) to another allocation id (Y).
|
| 26 |
+
// Allocation X is alive until allocation Y. From allocation Y onwards
|
| 27 |
+
// allocation X is not referenced.
|
| 28 |
+
// Thus Y is the id of the first allocation after X is freed.
|
| 29 |
+
// NB: When an allocation is recorded, along with recording its size,
|
| 30 |
+
// we also set the lifetime to be numeric_limits::max()
|
| 31 |
+
// This is to track allocations that are made during the scope of
|
| 32 |
+
// profiling but were not freed until after the scope ended.
|
| 33 |
+
// Such allocations are not managed by profiling allocator.
|
| 34 |
+
std::vector<uint64_t> allocation_lifetimes;
|
| 35 |
+
// Maps an allocation to some offset in a blob of memory.
|
| 36 |
+
std::vector<uint64_t> allocation_offsets;
|
| 37 |
+
uint64_t total_size{0};
|
| 38 |
+
void clear();
|
| 39 |
+
friend class AllocationPlanner;
|
| 40 |
+
friend class CPUProfilingAllocator;
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
/*
|
| 44 |
+
* Map of memory ptr to allocation id. This is auxiliary information only
|
| 45 |
+
* used to establish lifetime of allocations.
|
| 46 |
+
*/
|
| 47 |
+
class C10_API AllocationPlanner {
|
| 48 |
+
private:
|
| 49 |
+
AllocationPlan* allocation_plan_{nullptr};
|
| 50 |
+
// Maps allocated ptr to its allocation id.
|
| 51 |
+
// This is used when freeing the memory to look up the allocation id
|
| 52 |
+
// in order to establish the lifetime of a particular allocation.
|
| 53 |
+
ska::flat_hash_map<const void*, uint64_t> allocation_ptr_to_id_;
|
| 54 |
+
uint64_t allocation_id_{0};
|
| 55 |
+
bool validation_mode_{false};
|
| 56 |
+
|
| 57 |
+
bool validate_allocation(const uint64_t size, const void* ptr);
|
| 58 |
+
bool validate_free(const void* ptr);
|
| 59 |
+
|
| 60 |
+
public:
|
| 61 |
+
bool validation_success{true};
|
| 62 |
+
|
| 63 |
+
AllocationPlanner() = delete;
|
| 64 |
+
AllocationPlanner(AllocationPlan* plan, bool validate = false)
|
| 65 |
+
: allocation_plan_(plan), validation_mode_(validate) {}
|
| 66 |
+
void record_allocation(const uint64_t size, const void* ptr);
|
| 67 |
+
void record_free(const void* ptr);
|
| 68 |
+
void formulate_plan();
|
| 69 |
+
void clear();
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
// NOT THREAD SAFE profiling allocator.
|
| 73 |
+
class C10_API CPUProfilingAllocator {
|
| 74 |
+
private:
|
| 75 |
+
const AllocationPlan* plan_{nullptr};
|
| 76 |
+
uint64_t allocation_id_{0};
|
| 77 |
+
uint64_t current_size_{0};
|
| 78 |
+
void* blob_{nullptr};
|
| 79 |
+
ska::flat_hash_map<const void*, uint64_t> allocation_ptr_to_id_;
|
| 80 |
+
|
| 81 |
+
public:
|
| 82 |
+
~CPUProfilingAllocator();
|
| 83 |
+
void set_plan(const AllocationPlan* plan);
|
| 84 |
+
void unset_plan();
|
| 85 |
+
void* allocate(const size_t bytes);
|
| 86 |
+
void free(void* const ptr);
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
/*
|
| 90 |
+
* Usage: Profile allocations made by one run of the model.
|
| 91 |
+
* AllocationPlan plan;
|
| 92 |
+
* {
|
| 93 |
+
* WithProfileAllocationGuard profile_guard(&plan);
|
| 94 |
+
* module.forward(...);
|
| 95 |
+
* }
|
| 96 |
+
* plan now contains allocation plan.
|
| 97 |
+
*/
|
| 98 |
+
class C10_API WithProfileAllocationsGuard {
|
| 99 |
+
public:
|
| 100 |
+
WithProfileAllocationsGuard(AllocationPlan* plan);
|
| 101 |
+
~WithProfileAllocationsGuard();
|
| 102 |
+
|
| 103 |
+
private:
|
| 104 |
+
std::unique_ptr<AllocationPlanner> planner_;
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
/*
|
| 108 |
+
* Usage: Validate allocation plan made with WithProfileAllocationGuard
|
| 109 |
+
* bool plan_validation_success, success = true;
|
| 110 |
+
* for (some number of representative inputs)
|
| 111 |
+
* {
|
| 112 |
+
* WithValidateAllocationPlanGuard(&plan, &plan_validation_success);
|
| 113 |
+
* module.forward(...);
|
| 114 |
+
* success = success && plan_validation_success;
|
| 115 |
+
* }
|
| 116 |
+
* success == true means allocations are according to plan
|
| 117 |
+
* else for some inputs allocation pattern changed.
|
| 118 |
+
*/
|
| 119 |
+
class C10_API WithValidateAllocationPlanGuard {
|
| 120 |
+
public:
|
| 121 |
+
WithValidateAllocationPlanGuard(AllocationPlan* plan, bool* success);
|
| 122 |
+
~WithValidateAllocationPlanGuard();
|
| 123 |
+
|
| 124 |
+
private:
|
| 125 |
+
std::unique_ptr<AllocationPlanner> planner_;
|
| 126 |
+
bool* success_;
|
| 127 |
+
};
|
| 128 |
+
|
| 129 |
+
AllocationPlanner* GetThreadLocalAllocationPlanner();
|
| 130 |
+
|
| 131 |
+
/*
|
| 132 |
+
* Usage: Allocate tensors accordingly to allocation plan
|
| 133 |
+
* First make allocation plan.
|
| 134 |
+
* See WithProfileAllocationsGuard usage.
|
| 135 |
+
* Second validate allocation plan.
|
| 136 |
+
* See WithValidateAllocationPlanGuard usage.
|
| 137 |
+
* CPUProfilingAllocator profiling_allocator;
|
| 138 |
+
* {
|
| 139 |
+
* WithProfilingAllocatorGuard allocator_guard(&profiling_allocator, &plan);
|
| 140 |
+
* module.forward(...);
|
| 141 |
+
* }
|
| 142 |
+
*/
|
| 143 |
+
class C10_API WithProfilingAllocatorGuard {
|
| 144 |
+
public:
|
| 145 |
+
WithProfilingAllocatorGuard(
|
| 146 |
+
CPUProfilingAllocator* allocator,
|
| 147 |
+
const AllocationPlan* plan);
|
| 148 |
+
~WithProfilingAllocatorGuard();
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
CPUProfilingAllocator* GetThreadLocalProfilingAllocator();
|
| 152 |
+
|
| 153 |
+
} // namespace c10
|
| 154 |
+
|
| 155 |
+
#else
|
| 156 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 157 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/test/util/Macros.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef C10_TEST_CORE_MACROS_MACROS_H_
|
| 3 |
+
|
| 4 |
+
#ifdef _WIN32
|
| 5 |
+
#define DISABLED_ON_WINDOWS(x) DISABLED_##x
|
| 6 |
+
#else
|
| 7 |
+
#define DISABLED_ON_WINDOWS(x) x
|
| 8 |
+
#endif
|
| 9 |
+
|
| 10 |
+
#endif // C10_MACROS_MACROS_H_
|
| 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/c10/test/util/complex_math_test_common.h
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Warning: this file is included twice in
|
| 3 |
+
// aten/src/ATen/test/cuda_complex_math_test.cu
|
| 4 |
+
|
| 5 |
+
#include <c10/util/complex.h>
|
| 6 |
+
#include <gtest/gtest.h>
|
| 7 |
+
|
| 8 |
+
#ifndef PI
|
| 9 |
+
#define PI 3.141592653589793238463
|
| 10 |
+
#endif
|
| 11 |
+
|
| 12 |
+
#ifndef tol
|
| 13 |
+
#define tol 1e-6
|
| 14 |
+
#endif
|
| 15 |
+
|
| 16 |
+
// Exponential functions
|
| 17 |
+
|
| 18 |
+
C10_DEFINE_TEST(TestExponential, IPi) {
|
| 19 |
+
// exp(i*pi) = -1
|
| 20 |
+
{
|
| 21 |
+
c10::complex<float> e_i_pi = std::exp(c10::complex<float>(0, float(PI)));
|
| 22 |
+
C10_ASSERT_NEAR(e_i_pi.real(), -1, tol);
|
| 23 |
+
C10_ASSERT_NEAR(e_i_pi.imag(), 0, tol);
|
| 24 |
+
}
|
| 25 |
+
{
|
| 26 |
+
c10::complex<float> e_i_pi = ::exp(c10::complex<float>(0, float(PI)));
|
| 27 |
+
C10_ASSERT_NEAR(e_i_pi.real(), -1, tol);
|
| 28 |
+
C10_ASSERT_NEAR(e_i_pi.imag(), 0, tol);
|
| 29 |
+
}
|
| 30 |
+
{
|
| 31 |
+
c10::complex<double> e_i_pi = std::exp(c10::complex<double>(0, PI));
|
| 32 |
+
C10_ASSERT_NEAR(e_i_pi.real(), -1, tol);
|
| 33 |
+
C10_ASSERT_NEAR(e_i_pi.imag(), 0, tol);
|
| 34 |
+
}
|
| 35 |
+
{
|
| 36 |
+
c10::complex<double> e_i_pi = ::exp(c10::complex<double>(0, PI));
|
| 37 |
+
C10_ASSERT_NEAR(e_i_pi.real(), -1, tol);
|
| 38 |
+
C10_ASSERT_NEAR(e_i_pi.imag(), 0, tol);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
C10_DEFINE_TEST(TestExponential, EulerFormula) {
|
| 43 |
+
// exp(ix) = cos(x) + i * sin(x)
|
| 44 |
+
{
|
| 45 |
+
c10::complex<float> x(0.1, 1.2);
|
| 46 |
+
c10::complex<float> e = std::exp(x);
|
| 47 |
+
float expected_real = std::exp(x.real()) * std::cos(x.imag());
|
| 48 |
+
float expected_imag = std::exp(x.real()) * std::sin(x.imag());
|
| 49 |
+
C10_ASSERT_NEAR(e.real(), expected_real, tol);
|
| 50 |
+
C10_ASSERT_NEAR(e.imag(), expected_imag, tol);
|
| 51 |
+
}
|
| 52 |
+
{
|
| 53 |
+
c10::complex<float> x(0.1, 1.2);
|
| 54 |
+
c10::complex<float> e = ::exp(x);
|
| 55 |
+
float expected_real = ::exp(x.real()) * ::cos(x.imag());
|
| 56 |
+
float expected_imag = ::exp(x.real()) * ::sin(x.imag());
|
| 57 |
+
C10_ASSERT_NEAR(e.real(), expected_real, tol);
|
| 58 |
+
C10_ASSERT_NEAR(e.imag(), expected_imag, tol);
|
| 59 |
+
}
|
| 60 |
+
{
|
| 61 |
+
c10::complex<double> x(0.1, 1.2);
|
| 62 |
+
c10::complex<double> e = std::exp(x);
|
| 63 |
+
float expected_real = std::exp(x.real()) * std::cos(x.imag());
|
| 64 |
+
float expected_imag = std::exp(x.real()) * std::sin(x.imag());
|
| 65 |
+
C10_ASSERT_NEAR(e.real(), expected_real, tol);
|
| 66 |
+
C10_ASSERT_NEAR(e.imag(), expected_imag, tol);
|
| 67 |
+
}
|
| 68 |
+
{
|
| 69 |
+
c10::complex<double> x(0.1, 1.2);
|
| 70 |
+
c10::complex<double> e = ::exp(x);
|
| 71 |
+
float expected_real = ::exp(x.real()) * ::cos(x.imag());
|
| 72 |
+
float expected_imag = ::exp(x.real()) * ::sin(x.imag());
|
| 73 |
+
C10_ASSERT_NEAR(e.real(), expected_real, tol);
|
| 74 |
+
C10_ASSERT_NEAR(e.imag(), expected_imag, tol);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
C10_DEFINE_TEST(TestExpm1, Normal) {
|
| 79 |
+
// expm1(x) = exp(x) - 1
|
| 80 |
+
{
|
| 81 |
+
c10::complex<float> x(0.1, 1.2);
|
| 82 |
+
c10::complex<float> l1 = std::expm1(x);
|
| 83 |
+
c10::complex<float> l2 = std::exp(x) - 1.0f;
|
| 84 |
+
C10_ASSERT_NEAR(l1.real(), l2.real(), tol);
|
| 85 |
+
C10_ASSERT_NEAR(l1.imag(), l2.imag(), tol);
|
| 86 |
+
}
|
| 87 |
+
{
|
| 88 |
+
c10::complex<double> x(0.1, 1.2);
|
| 89 |
+
c10::complex<double> l1 = std::expm1(x);
|
| 90 |
+
c10::complex<double> l2 = std::exp(x) - 1.0;
|
| 91 |
+
C10_ASSERT_NEAR(l1.real(), l2.real(), tol);
|
| 92 |
+
C10_ASSERT_NEAR(l1.imag(), l2.imag(), tol);
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
C10_DEFINE_TEST(TestExpm1, Small) {
|
| 97 |
+
// expm1(x) = exp(x) - 1
|
| 98 |
+
// expm1(x) provides greater precision than exp(x) - 1 for small values of x
|
| 99 |
+
{
|
| 100 |
+
c10::complex<float> x(1e-30, 1e-30);
|
| 101 |
+
c10::complex<float> l1 = std::expm1(x);
|
| 102 |
+
C10_ASSERT_NEAR(l1.real(), 1e-30, tol);
|
| 103 |
+
C10_ASSERT_NEAR(l1.imag(), 1e-30, tol);
|
| 104 |
+
}
|
| 105 |
+
{
|
| 106 |
+
c10::complex<double> x(1e-100, 1e-100);
|
| 107 |
+
c10::complex<double> l1 = std::expm1(x);
|
| 108 |
+
C10_ASSERT_NEAR(l1.real(), 1e-30, tol);
|
| 109 |
+
C10_ASSERT_NEAR(l1.imag(), 1e-30, tol);
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
C10_DEFINE_TEST(TestLog, Definition) {
|
| 114 |
+
// log(x) = log(r) + i*theta
|
| 115 |
+
{
|
| 116 |
+
c10::complex<float> x(1.2, 3.4);
|
| 117 |
+
c10::complex<float> l = std::log(x);
|
| 118 |
+
float expected_real = std::log(std::abs(x));
|
| 119 |
+
float expected_imag = std::arg(x);
|
| 120 |
+
C10_ASSERT_NEAR(l.real(), expected_real, tol);
|
| 121 |
+
C10_ASSERT_NEAR(l.imag(), expected_imag, tol);
|
| 122 |
+
}
|
| 123 |
+
{
|
| 124 |
+
c10::complex<float> x(1.2, 3.4);
|
| 125 |
+
c10::complex<float> l = ::log(x);
|
| 126 |
+
float expected_real = ::log(std::abs(x));
|
| 127 |
+
float expected_imag = std::arg(x);
|
| 128 |
+
C10_ASSERT_NEAR(l.real(), expected_real, tol);
|
| 129 |
+
C10_ASSERT_NEAR(l.imag(), expected_imag, tol);
|
| 130 |
+
}
|
| 131 |
+
{
|
| 132 |
+
c10::complex<double> x(1.2, 3.4);
|
| 133 |
+
c10::complex<double> l = std::log(x);
|
| 134 |
+
float expected_real = std::log(std::abs(x));
|
| 135 |
+
float expected_imag = std::arg(x);
|
| 136 |
+
C10_ASSERT_NEAR(l.real(), expected_real, tol);
|
| 137 |
+
C10_ASSERT_NEAR(l.imag(), expected_imag, tol);
|
| 138 |
+
}
|
| 139 |
+
{
|
| 140 |
+
c10::complex<double> x(1.2, 3.4);
|
| 141 |
+
c10::complex<double> l = ::log(x);
|
| 142 |
+
float expected_real = ::log(std::abs(x));
|
| 143 |
+
float expected_imag = std::arg(x);
|
| 144 |
+
C10_ASSERT_NEAR(l.real(), expected_real, tol);
|
| 145 |
+
C10_ASSERT_NEAR(l.imag(), expected_imag, tol);
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
C10_DEFINE_TEST(TestLog10, Rev) {
|
| 150 |
+
// log10(10^x) = x
|
| 151 |
+
{
|
| 152 |
+
c10::complex<float> x(0.1, 1.2);
|
| 153 |
+
c10::complex<float> l = std::log10(std::pow(float(10), x));
|
| 154 |
+
C10_ASSERT_NEAR(l.real(), float(0.1), tol);
|
| 155 |
+
C10_ASSERT_NEAR(l.imag(), float(1.2), tol);
|
| 156 |
+
}
|
| 157 |
+
{
|
| 158 |
+
c10::complex<float> x(0.1, 1.2);
|
| 159 |
+
c10::complex<float> l = ::log10(::pow(float(10), x));
|
| 160 |
+
C10_ASSERT_NEAR(l.real(), float(0.1), tol);
|
| 161 |
+
C10_ASSERT_NEAR(l.imag(), float(1.2), tol);
|
| 162 |
+
}
|
| 163 |
+
{
|
| 164 |
+
c10::complex<double> x(0.1, 1.2);
|
| 165 |
+
c10::complex<double> l = std::log10(std::pow(double(10), x));
|
| 166 |
+
C10_ASSERT_NEAR(l.real(), double(0.1), tol);
|
| 167 |
+
C10_ASSERT_NEAR(l.imag(), double(1.2), tol);
|
| 168 |
+
}
|
| 169 |
+
{
|
| 170 |
+
c10::complex<double> x(0.1, 1.2);
|
| 171 |
+
c10::complex<double> l = ::log10(::pow(double(10), x));
|
| 172 |
+
C10_ASSERT_NEAR(l.real(), double(0.1), tol);
|
| 173 |
+
C10_ASSERT_NEAR(l.imag(), double(1.2), tol);
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
C10_DEFINE_TEST(TestLog2, Rev) {
|
| 178 |
+
// log2(2^x) = x
|
| 179 |
+
{
|
| 180 |
+
c10::complex<float> x(0.1, 1.2);
|
| 181 |
+
c10::complex<float> l = std::log2(std::pow(float(2), x));
|
| 182 |
+
C10_ASSERT_NEAR(l.real(), float(0.1), tol);
|
| 183 |
+
C10_ASSERT_NEAR(l.imag(), float(1.2), tol);
|
| 184 |
+
}
|
| 185 |
+
{
|
| 186 |
+
c10::complex<float> x(0.1, 1.2);
|
| 187 |
+
c10::complex<float> l = ::log2(std::pow(float(2), x));
|
| 188 |
+
C10_ASSERT_NEAR(l.real(), float(0.1), tol);
|
| 189 |
+
C10_ASSERT_NEAR(l.imag(), float(1.2), tol);
|
| 190 |
+
}
|
| 191 |
+
{
|
| 192 |
+
c10::complex<double> x(0.1, 1.2);
|
| 193 |
+
c10::complex<double> l = std::log2(std::pow(double(2), x));
|
| 194 |
+
C10_ASSERT_NEAR(l.real(), double(0.1), tol);
|
| 195 |
+
C10_ASSERT_NEAR(l.imag(), double(1.2), tol);
|
| 196 |
+
}
|
| 197 |
+
{
|
| 198 |
+
c10::complex<double> x(0.1, 1.2);
|
| 199 |
+
c10::complex<double> l = ::log2(std::pow(double(2), x));
|
| 200 |
+
C10_ASSERT_NEAR(l.real(), double(0.1), tol);
|
| 201 |
+
C10_ASSERT_NEAR(l.imag(), double(1.2), tol);
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
C10_DEFINE_TEST(TestLog1p, Normal) {
|
| 206 |
+
// log1p(x) = log(1 + x)
|
| 207 |
+
{
|
| 208 |
+
c10::complex<float> x(0.1, 1.2);
|
| 209 |
+
c10::complex<float> l1 = std::log1p(x);
|
| 210 |
+
c10::complex<float> l2 = std::log(1.0f + x);
|
| 211 |
+
C10_ASSERT_NEAR(l1.real(), l2.real(), tol);
|
| 212 |
+
C10_ASSERT_NEAR(l1.imag(), l2.imag(), tol);
|
| 213 |
+
}
|
| 214 |
+
{
|
| 215 |
+
c10::complex<double> x(0.1, 1.2);
|
| 216 |
+
c10::complex<double> l1 = std::log1p(x);
|
| 217 |
+
c10::complex<double> l2 = std::log(1.0 + x);
|
| 218 |
+
C10_ASSERT_NEAR(l1.real(), l2.real(), tol);
|
| 219 |
+
C10_ASSERT_NEAR(l1.imag(), l2.imag(), tol);
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
C10_DEFINE_TEST(TestLog1p, Small) {
|
| 224 |
+
// log(1 + x) ~ x for |x| << 1
|
| 225 |
+
{
|
| 226 |
+
c10::complex<float> x(1e-9, 2e-9);
|
| 227 |
+
c10::complex<float> l = std::log1p(x);
|
| 228 |
+
C10_ASSERT_NEAR(l.real() / x.real(), 1, tol);
|
| 229 |
+
C10_ASSERT_NEAR(l.imag() / x.imag(), 1, tol);
|
| 230 |
+
}
|
| 231 |
+
{
|
| 232 |
+
c10::complex<double> x(1e-100, 2e-100);
|
| 233 |
+
c10::complex<double> l = std::log1p(x);
|
| 234 |
+
C10_ASSERT_NEAR(l.real() / x.real(), 1, tol);
|
| 235 |
+
C10_ASSERT_NEAR(l.imag() / x.imag(), 1, tol);
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
C10_DEFINE_TEST(TestLog1p, Extreme) {
|
| 240 |
+
// log(1 + x) ~ x for |x| << 1 and in the brink of overflow / underflow
|
| 241 |
+
{
|
| 242 |
+
c10::complex<float> x(-1, 1e-30);
|
| 243 |
+
c10::complex<float> l = std::log1p(x);
|
| 244 |
+
C10_ASSERT_NEAR(l.real(), -69.07755278982137, tol);
|
| 245 |
+
C10_ASSERT_NEAR(l.imag(), 1.5707963267948966, tol);
|
| 246 |
+
}
|
| 247 |
+
{
|
| 248 |
+
c10::complex<float> x(-1, 1e30);
|
| 249 |
+
c10::complex<float> l = std::log1p(x);
|
| 250 |
+
C10_ASSERT_NEAR(l.real(), 69.07755278982137, tol);
|
| 251 |
+
C10_ASSERT_NEAR(l.imag(), 1.5707963267948966, tol);
|
| 252 |
+
}
|
| 253 |
+
{
|
| 254 |
+
c10::complex<float> x(1e30, 1);
|
| 255 |
+
c10::complex<float> l = std::log1p(x);
|
| 256 |
+
C10_ASSERT_NEAR(l.real(), 69.07755278982137, tol);
|
| 257 |
+
C10_ASSERT_NEAR(l.imag(), 1e-30, tol);
|
| 258 |
+
}
|
| 259 |
+
{
|
| 260 |
+
c10::complex<float> x(1e-30, 1);
|
| 261 |
+
c10::complex<float> l = std::log1p(x);
|
| 262 |
+
C10_ASSERT_NEAR(l.real(), 0.34657359027997264, tol);
|
| 263 |
+
C10_ASSERT_NEAR(l.imag(), 0.7853981633974483, tol);
|
| 264 |
+
}
|
| 265 |
+
{
|
| 266 |
+
c10::complex<float> x(1e30, 1e30);
|
| 267 |
+
c10::complex<float> l = std::log1p(x);
|
| 268 |
+
C10_ASSERT_NEAR(l.real(), 69.42412638010134, tol);
|
| 269 |
+
C10_ASSERT_NEAR(l.imag(), 0.7853981633974483, tol);
|
| 270 |
+
}
|
| 271 |
+
{
|
| 272 |
+
c10::complex<float> x(1e-38, 1e-38);
|
| 273 |
+
c10::complex<float> l = std::log1p(x);
|
| 274 |
+
C10_ASSERT_NEAR(l.real(), 1e-38, tol);
|
| 275 |
+
C10_ASSERT_NEAR(l.imag(), 1e-38, tol);
|
| 276 |
+
}
|
| 277 |
+
{
|
| 278 |
+
c10::complex<float> x(1e-38, 2e-30);
|
| 279 |
+
c10::complex<float> l = std::log1p(x);
|
| 280 |
+
C10_ASSERT_NEAR(l.real(), 1e-30, tol);
|
| 281 |
+
C10_ASSERT_NEAR(l.imag(), 2e-30, tol);
|
| 282 |
+
}
|
| 283 |
+
{
|
| 284 |
+
c10::complex<double> x(-1, 1e-250);
|
| 285 |
+
c10::complex<double> l = std::log1p(x);
|
| 286 |
+
C10_ASSERT_NEAR(l.real(), -575.6462732485114, tol);
|
| 287 |
+
C10_ASSERT_NEAR(l.imag(), 1.5707963267948966, tol);
|
| 288 |
+
}
|
| 289 |
+
{
|
| 290 |
+
c10::complex<double> x(-1, 1e250);
|
| 291 |
+
c10::complex<double> l = std::log1p(x);
|
| 292 |
+
C10_ASSERT_NEAR(l.real(), 575.6462732485114, tol);
|
| 293 |
+
C10_ASSERT_NEAR(l.imag(), 1.5707963267948966, tol);
|
| 294 |
+
}
|
| 295 |
+
{
|
| 296 |
+
c10::complex<double> x(1e250, 1);
|
| 297 |
+
c10::complex<double> l = std::log1p(x);
|
| 298 |
+
C10_ASSERT_NEAR(l.real(), 575.6462732485114, tol);
|
| 299 |
+
C10_ASSERT_NEAR(l.imag(), 1e-250, tol);
|
| 300 |
+
}
|
| 301 |
+
{
|
| 302 |
+
c10::complex<double> x(1e-250, 1);
|
| 303 |
+
c10::complex<double> l = std::log1p(x);
|
| 304 |
+
C10_ASSERT_NEAR(l.real(), 0.34657359027997264, tol);
|
| 305 |
+
C10_ASSERT_NEAR(l.imag(), 0.7853981633974483, tol);
|
| 306 |
+
}
|
| 307 |
+
{
|
| 308 |
+
c10::complex<double> x(1e250, 1e250);
|
| 309 |
+
c10::complex<double> l = std::log1p(x);
|
| 310 |
+
C10_ASSERT_NEAR(l.real(), 575.9928468387914, tol);
|
| 311 |
+
C10_ASSERT_NEAR(l.imag(), 0.7853981633974483, tol);
|
| 312 |
+
}
|
| 313 |
+
{
|
| 314 |
+
c10::complex<double> x(1e-250, 1e-250);
|
| 315 |
+
c10::complex<double> l = std::log1p(x);
|
| 316 |
+
C10_ASSERT_NEAR(l.real(), 1e-250, tol);
|
| 317 |
+
C10_ASSERT_NEAR(l.imag(), 1e-250, tol);
|
| 318 |
+
}
|
| 319 |
+
{
|
| 320 |
+
c10::complex<double> x(1e-250, 2e-250);
|
| 321 |
+
c10::complex<double> l = std::log1p(x);
|
| 322 |
+
C10_ASSERT_NEAR(l.real(), 1e-250, tol);
|
| 323 |
+
C10_ASSERT_NEAR(l.imag(), 2e-250, tol);
|
| 324 |
+
}
|
| 325 |
+
{
|
| 326 |
+
c10::complex<double> x(2e-308, 1.5e-250);
|
| 327 |
+
c10::complex<double> l = std::log1p(x);
|
| 328 |
+
C10_ASSERT_NEAR(l.real(), 2e-308, tol);
|
| 329 |
+
C10_ASSERT_NEAR(l.imag(), 1.5e-308, tol);
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// Power functions
|
| 334 |
+
|
| 335 |
+
C10_DEFINE_TEST(TestPowSqrt, Equal) {
|
| 336 |
+
// x^0.5 = sqrt(x)
|
| 337 |
+
{
|
| 338 |
+
c10::complex<float> x(0.1, 1.2);
|
| 339 |
+
c10::complex<float> y = std::pow(x, float(0.5));
|
| 340 |
+
c10::complex<float> z = std::sqrt(x);
|
| 341 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 342 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 343 |
+
}
|
| 344 |
+
{
|
| 345 |
+
c10::complex<float> x(0.1, 1.2);
|
| 346 |
+
c10::complex<float> y = ::pow(x, float(0.5));
|
| 347 |
+
c10::complex<float> z = ::sqrt(x);
|
| 348 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 349 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 350 |
+
}
|
| 351 |
+
{
|
| 352 |
+
c10::complex<double> x(0.1, 1.2);
|
| 353 |
+
c10::complex<double> y = std::pow(x, double(0.5));
|
| 354 |
+
c10::complex<double> z = std::sqrt(x);
|
| 355 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 356 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 357 |
+
}
|
| 358 |
+
{
|
| 359 |
+
c10::complex<double> x(0.1, 1.2);
|
| 360 |
+
c10::complex<double> y = ::pow(x, double(0.5));
|
| 361 |
+
c10::complex<double> z = ::sqrt(x);
|
| 362 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 363 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
C10_DEFINE_TEST(TestPow, Square) {
|
| 368 |
+
// x^2 = x * x
|
| 369 |
+
{
|
| 370 |
+
c10::complex<float> x(0.1, 1.2);
|
| 371 |
+
c10::complex<float> y = std::pow(x, float(2));
|
| 372 |
+
c10::complex<float> z = x * x;
|
| 373 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 374 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 375 |
+
}
|
| 376 |
+
{
|
| 377 |
+
c10::complex<float> x(0.1, 1.2);
|
| 378 |
+
c10::complex<float> y = ::pow(x, float(2));
|
| 379 |
+
c10::complex<float> z = x * x;
|
| 380 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 381 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 382 |
+
}
|
| 383 |
+
{
|
| 384 |
+
c10::complex<double> x(0.1, 1.2);
|
| 385 |
+
c10::complex<double> y = std::pow(x, double(2));
|
| 386 |
+
c10::complex<double> z = x * x;
|
| 387 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 388 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 389 |
+
}
|
| 390 |
+
{
|
| 391 |
+
c10::complex<double> x(0.1, 1.2);
|
| 392 |
+
c10::complex<double> y = ::pow(x, double(2));
|
| 393 |
+
c10::complex<double> z = x * x;
|
| 394 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 395 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 396 |
+
}
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
// Trigonometric functions and hyperbolic functions
|
| 400 |
+
|
| 401 |
+
C10_DEFINE_TEST(TestSinCosSinhCosh, Identity) {
|
| 402 |
+
// sin(x + i * y) = sin(x) * cosh(y) + i * cos(x) * sinh(y)
|
| 403 |
+
// cos(x + i * y) = cos(x) * cosh(y) - i * sin(x) * sinh(y)
|
| 404 |
+
{
|
| 405 |
+
c10::complex<float> x(0.1, 1.2);
|
| 406 |
+
c10::complex<float> y = std::sin(x);
|
| 407 |
+
float expected_real = std::sin(x.real()) * std::cosh(x.imag());
|
| 408 |
+
float expected_imag = std::cos(x.real()) * std::sinh(x.imag());
|
| 409 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 410 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 411 |
+
}
|
| 412 |
+
{
|
| 413 |
+
c10::complex<float> x(0.1, 1.2);
|
| 414 |
+
c10::complex<float> y = ::sin(x);
|
| 415 |
+
float expected_real = ::sin(x.real()) * ::cosh(x.imag());
|
| 416 |
+
float expected_imag = ::cos(x.real()) * ::sinh(x.imag());
|
| 417 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 418 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 419 |
+
}
|
| 420 |
+
{
|
| 421 |
+
c10::complex<float> x(0.1, 1.2);
|
| 422 |
+
c10::complex<float> y = std::cos(x);
|
| 423 |
+
float expected_real = std::cos(x.real()) * std::cosh(x.imag());
|
| 424 |
+
float expected_imag = -std::sin(x.real()) * std::sinh(x.imag());
|
| 425 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 426 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 427 |
+
}
|
| 428 |
+
{
|
| 429 |
+
c10::complex<float> x(0.1, 1.2);
|
| 430 |
+
c10::complex<float> y = ::cos(x);
|
| 431 |
+
float expected_real = ::cos(x.real()) * ::cosh(x.imag());
|
| 432 |
+
float expected_imag = -::sin(x.real()) * ::sinh(x.imag());
|
| 433 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 434 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 435 |
+
}
|
| 436 |
+
{
|
| 437 |
+
c10::complex<double> x(0.1, 1.2);
|
| 438 |
+
c10::complex<double> y = std::sin(x);
|
| 439 |
+
float expected_real = std::sin(x.real()) * std::cosh(x.imag());
|
| 440 |
+
float expected_imag = std::cos(x.real()) * std::sinh(x.imag());
|
| 441 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 442 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 443 |
+
}
|
| 444 |
+
{
|
| 445 |
+
c10::complex<double> x(0.1, 1.2);
|
| 446 |
+
c10::complex<double> y = ::sin(x);
|
| 447 |
+
float expected_real = ::sin(x.real()) * ::cosh(x.imag());
|
| 448 |
+
float expected_imag = ::cos(x.real()) * ::sinh(x.imag());
|
| 449 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 450 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 451 |
+
}
|
| 452 |
+
{
|
| 453 |
+
c10::complex<double> x(0.1, 1.2);
|
| 454 |
+
c10::complex<double> y = std::cos(x);
|
| 455 |
+
float expected_real = std::cos(x.real()) * std::cosh(x.imag());
|
| 456 |
+
float expected_imag = -std::sin(x.real()) * std::sinh(x.imag());
|
| 457 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 458 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 459 |
+
}
|
| 460 |
+
{
|
| 461 |
+
c10::complex<double> x(0.1, 1.2);
|
| 462 |
+
c10::complex<double> y = ::cos(x);
|
| 463 |
+
float expected_real = ::cos(x.real()) * ::cosh(x.imag());
|
| 464 |
+
float expected_imag = -::sin(x.real()) * ::sinh(x.imag());
|
| 465 |
+
C10_ASSERT_NEAR(y.real(), expected_real, tol);
|
| 466 |
+
C10_ASSERT_NEAR(y.imag(), expected_imag, tol);
|
| 467 |
+
}
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
C10_DEFINE_TEST(TestTan, Identity) {
|
| 471 |
+
// tan(x) = sin(x) / cos(x)
|
| 472 |
+
{
|
| 473 |
+
c10::complex<float> x(0.1, 1.2);
|
| 474 |
+
c10::complex<float> y = std::tan(x);
|
| 475 |
+
c10::complex<float> z = std::sin(x) / std::cos(x);
|
| 476 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 477 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 478 |
+
}
|
| 479 |
+
{
|
| 480 |
+
c10::complex<float> x(0.1, 1.2);
|
| 481 |
+
c10::complex<float> y = ::tan(x);
|
| 482 |
+
c10::complex<float> z = ::sin(x) / ::cos(x);
|
| 483 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 484 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 485 |
+
}
|
| 486 |
+
{
|
| 487 |
+
c10::complex<double> x(0.1, 1.2);
|
| 488 |
+
c10::complex<double> y = std::tan(x);
|
| 489 |
+
c10::complex<double> z = std::sin(x) / std::cos(x);
|
| 490 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 491 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 492 |
+
}
|
| 493 |
+
{
|
| 494 |
+
c10::complex<double> x(0.1, 1.2);
|
| 495 |
+
c10::complex<double> y = ::tan(x);
|
| 496 |
+
c10::complex<double> z = ::sin(x) / ::cos(x);
|
| 497 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 498 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 499 |
+
}
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
C10_DEFINE_TEST(TestTanh, Identity) {
|
| 503 |
+
// tanh(x) = sinh(x) / cosh(x)
|
| 504 |
+
{
|
| 505 |
+
c10::complex<float> x(0.1, 1.2);
|
| 506 |
+
c10::complex<float> y = std::tanh(x);
|
| 507 |
+
c10::complex<float> z = std::sinh(x) / std::cosh(x);
|
| 508 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 509 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 510 |
+
}
|
| 511 |
+
{
|
| 512 |
+
c10::complex<float> x(0.1, 1.2);
|
| 513 |
+
c10::complex<float> y = ::tanh(x);
|
| 514 |
+
c10::complex<float> z = ::sinh(x) / ::cosh(x);
|
| 515 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 516 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 517 |
+
}
|
| 518 |
+
{
|
| 519 |
+
c10::complex<double> x(0.1, 1.2);
|
| 520 |
+
c10::complex<double> y = std::tanh(x);
|
| 521 |
+
c10::complex<double> z = std::sinh(x) / std::cosh(x);
|
| 522 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 523 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 524 |
+
}
|
| 525 |
+
{
|
| 526 |
+
c10::complex<double> x(0.1, 1.2);
|
| 527 |
+
c10::complex<double> y = ::tanh(x);
|
| 528 |
+
c10::complex<double> z = ::sinh(x) / ::cosh(x);
|
| 529 |
+
C10_ASSERT_NEAR(y.real(), z.real(), tol);
|
| 530 |
+
C10_ASSERT_NEAR(y.imag(), z.imag(), tol);
|
| 531 |
+
}
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
// Rev trigonometric functions
|
| 535 |
+
|
| 536 |
+
C10_DEFINE_TEST(TestRevTrigonometric, Rev) {
|
| 537 |
+
// asin(sin(x)) = x
|
| 538 |
+
// acos(cos(x)) = x
|
| 539 |
+
// atan(tan(x)) = x
|
| 540 |
+
{
|
| 541 |
+
c10::complex<float> x(0.5, 0.6);
|
| 542 |
+
c10::complex<float> s = std::sin(x);
|
| 543 |
+
c10::complex<float> ss = std::asin(s);
|
| 544 |
+
c10::complex<float> c = std::cos(x);
|
| 545 |
+
c10::complex<float> cc = std::acos(c);
|
| 546 |
+
c10::complex<float> t = std::tan(x);
|
| 547 |
+
c10::complex<float> tt = std::atan(t);
|
| 548 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 549 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 550 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 551 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 552 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 553 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 554 |
+
}
|
| 555 |
+
{
|
| 556 |
+
c10::complex<float> x(0.5, 0.6);
|
| 557 |
+
c10::complex<float> s = ::sin(x);
|
| 558 |
+
c10::complex<float> ss = ::asin(s);
|
| 559 |
+
c10::complex<float> c = ::cos(x);
|
| 560 |
+
c10::complex<float> cc = ::acos(c);
|
| 561 |
+
c10::complex<float> t = ::tan(x);
|
| 562 |
+
c10::complex<float> tt = ::atan(t);
|
| 563 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 564 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 565 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 566 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 567 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 568 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 569 |
+
}
|
| 570 |
+
{
|
| 571 |
+
c10::complex<double> x(0.5, 0.6);
|
| 572 |
+
c10::complex<double> s = std::sin(x);
|
| 573 |
+
c10::complex<double> ss = std::asin(s);
|
| 574 |
+
c10::complex<double> c = std::cos(x);
|
| 575 |
+
c10::complex<double> cc = std::acos(c);
|
| 576 |
+
c10::complex<double> t = std::tan(x);
|
| 577 |
+
c10::complex<double> tt = std::atan(t);
|
| 578 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 579 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 580 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 581 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 582 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 583 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 584 |
+
}
|
| 585 |
+
{
|
| 586 |
+
c10::complex<double> x(0.5, 0.6);
|
| 587 |
+
c10::complex<double> s = ::sin(x);
|
| 588 |
+
c10::complex<double> ss = ::asin(s);
|
| 589 |
+
c10::complex<double> c = ::cos(x);
|
| 590 |
+
c10::complex<double> cc = ::acos(c);
|
| 591 |
+
c10::complex<double> t = ::tan(x);
|
| 592 |
+
c10::complex<double> tt = ::atan(t);
|
| 593 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 594 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 595 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 596 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 597 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 598 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 599 |
+
}
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
// Rev hyperbolic functions
|
| 603 |
+
|
| 604 |
+
C10_DEFINE_TEST(TestRevHyperbolic, Rev) {
|
| 605 |
+
// asinh(sinh(x)) = x
|
| 606 |
+
// acosh(cosh(x)) = x
|
| 607 |
+
// atanh(tanh(x)) = x
|
| 608 |
+
{
|
| 609 |
+
c10::complex<float> x(0.5, 0.6);
|
| 610 |
+
c10::complex<float> s = std::sinh(x);
|
| 611 |
+
c10::complex<float> ss = std::asinh(s);
|
| 612 |
+
c10::complex<float> c = std::cosh(x);
|
| 613 |
+
c10::complex<float> cc = std::acosh(c);
|
| 614 |
+
c10::complex<float> t = std::tanh(x);
|
| 615 |
+
c10::complex<float> tt = std::atanh(t);
|
| 616 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 617 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 618 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 619 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 620 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 621 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 622 |
+
}
|
| 623 |
+
{
|
| 624 |
+
c10::complex<float> x(0.5, 0.6);
|
| 625 |
+
c10::complex<float> s = ::sinh(x);
|
| 626 |
+
c10::complex<float> ss = ::asinh(s);
|
| 627 |
+
c10::complex<float> c = ::cosh(x);
|
| 628 |
+
c10::complex<float> cc = ::acosh(c);
|
| 629 |
+
c10::complex<float> t = ::tanh(x);
|
| 630 |
+
c10::complex<float> tt = ::atanh(t);
|
| 631 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 632 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 633 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 634 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 635 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 636 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 637 |
+
}
|
| 638 |
+
{
|
| 639 |
+
c10::complex<double> x(0.5, 0.6);
|
| 640 |
+
c10::complex<double> s = std::sinh(x);
|
| 641 |
+
c10::complex<double> ss = std::asinh(s);
|
| 642 |
+
c10::complex<double> c = std::cosh(x);
|
| 643 |
+
c10::complex<double> cc = std::acosh(c);
|
| 644 |
+
c10::complex<double> t = std::tanh(x);
|
| 645 |
+
c10::complex<double> tt = std::atanh(t);
|
| 646 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 647 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 648 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 649 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 650 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 651 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 652 |
+
}
|
| 653 |
+
{
|
| 654 |
+
c10::complex<double> x(0.5, 0.6);
|
| 655 |
+
c10::complex<double> s = ::sinh(x);
|
| 656 |
+
c10::complex<double> ss = ::asinh(s);
|
| 657 |
+
c10::complex<double> c = ::cosh(x);
|
| 658 |
+
c10::complex<double> cc = ::acosh(c);
|
| 659 |
+
c10::complex<double> t = ::tanh(x);
|
| 660 |
+
c10::complex<double> tt = ::atanh(t);
|
| 661 |
+
C10_ASSERT_NEAR(x.real(), ss.real(), tol);
|
| 662 |
+
C10_ASSERT_NEAR(x.imag(), ss.imag(), tol);
|
| 663 |
+
C10_ASSERT_NEAR(x.real(), cc.real(), tol);
|
| 664 |
+
C10_ASSERT_NEAR(x.imag(), cc.imag(), tol);
|
| 665 |
+
C10_ASSERT_NEAR(x.real(), tt.real(), tol);
|
| 666 |
+
C10_ASSERT_NEAR(x.imag(), tt.imag(), tol);
|
| 667 |
+
}
|
| 668 |
+
}
|
| 669 |
+
|
| 670 |
+
#else
|
| 671 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 672 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/test/util/complex_test_common.h
ADDED
|
@@ -0,0 +1,663 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <c10/macros/Macros.h>
|
| 3 |
+
#include <c10/util/complex.h>
|
| 4 |
+
#include <c10/util/hash.h>
|
| 5 |
+
#include <gtest/gtest.h>
|
| 6 |
+
#include <sstream>
|
| 7 |
+
#include <tuple>
|
| 8 |
+
#include <type_traits>
|
| 9 |
+
#include <unordered_map>
|
| 10 |
+
|
| 11 |
+
#if (defined(__CUDACC__) || defined(__HIPCC__))
|
| 12 |
+
#define MAYBE_GLOBAL __global__
|
| 13 |
+
#else
|
| 14 |
+
#define MAYBE_GLOBAL
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
#define PI 3.141592653589793238463
|
| 18 |
+
|
| 19 |
+
namespace memory {
|
| 20 |
+
|
| 21 |
+
MAYBE_GLOBAL void test_size() {
|
| 22 |
+
static_assert(sizeof(c10::complex<float>) == 2 * sizeof(float), "");
|
| 23 |
+
static_assert(sizeof(c10::complex<double>) == 2 * sizeof(double), "");
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
MAYBE_GLOBAL void test_align() {
|
| 27 |
+
static_assert(alignof(c10::complex<float>) == 2 * sizeof(float), "");
|
| 28 |
+
static_assert(alignof(c10::complex<double>) == 2 * sizeof(double), "");
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
MAYBE_GLOBAL void test_pod() {
|
| 32 |
+
static_assert(std::is_standard_layout<c10::complex<float>>::value, "");
|
| 33 |
+
static_assert(std::is_standard_layout<c10::complex<double>>::value, "");
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
TEST(TestMemory, ReinterpretCast) {
|
| 37 |
+
{
|
| 38 |
+
std::complex<float> z(1, 2);
|
| 39 |
+
c10::complex<float> zz = *reinterpret_cast<c10::complex<float>*>(&z);
|
| 40 |
+
ASSERT_EQ(zz.real(), float(1));
|
| 41 |
+
ASSERT_EQ(zz.imag(), float(2));
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
{
|
| 45 |
+
c10::complex<float> z(3, 4);
|
| 46 |
+
std::complex<float> zz = *reinterpret_cast<std::complex<float>*>(&z);
|
| 47 |
+
ASSERT_EQ(zz.real(), float(3));
|
| 48 |
+
ASSERT_EQ(zz.imag(), float(4));
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
{
|
| 52 |
+
std::complex<double> z(1, 2);
|
| 53 |
+
c10::complex<double> zz = *reinterpret_cast<c10::complex<double>*>(&z);
|
| 54 |
+
ASSERT_EQ(zz.real(), double(1));
|
| 55 |
+
ASSERT_EQ(zz.imag(), double(2));
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
{
|
| 59 |
+
c10::complex<double> z(3, 4);
|
| 60 |
+
std::complex<double> zz = *reinterpret_cast<std::complex<double>*>(&z);
|
| 61 |
+
ASSERT_EQ(zz.real(), double(3));
|
| 62 |
+
ASSERT_EQ(zz.imag(), double(4));
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
#if defined(__CUDACC__) || defined(__HIPCC__)
|
| 67 |
+
TEST(TestMemory, ThrustReinterpretCast) {
|
| 68 |
+
{
|
| 69 |
+
thrust::complex<float> z(1, 2);
|
| 70 |
+
c10::complex<float> zz = *reinterpret_cast<c10::complex<float>*>(&z);
|
| 71 |
+
ASSERT_EQ(zz.real(), float(1));
|
| 72 |
+
ASSERT_EQ(zz.imag(), float(2));
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
{
|
| 76 |
+
c10::complex<float> z(3, 4);
|
| 77 |
+
thrust::complex<float> zz = *reinterpret_cast<thrust::complex<float>*>(&z);
|
| 78 |
+
ASSERT_EQ(zz.real(), float(3));
|
| 79 |
+
ASSERT_EQ(zz.imag(), float(4));
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
{
|
| 83 |
+
thrust::complex<double> z(1, 2);
|
| 84 |
+
c10::complex<double> zz = *reinterpret_cast<c10::complex<double>*>(&z);
|
| 85 |
+
ASSERT_EQ(zz.real(), double(1));
|
| 86 |
+
ASSERT_EQ(zz.imag(), double(2));
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
{
|
| 90 |
+
c10::complex<double> z(3, 4);
|
| 91 |
+
thrust::complex<double> zz =
|
| 92 |
+
*reinterpret_cast<thrust::complex<double>*>(&z);
|
| 93 |
+
ASSERT_EQ(zz.real(), double(3));
|
| 94 |
+
ASSERT_EQ(zz.imag(), double(4));
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
#endif
|
| 98 |
+
|
| 99 |
+
} // namespace memory
|
| 100 |
+
|
| 101 |
+
namespace constructors {
|
| 102 |
+
|
| 103 |
+
template <typename scalar_t>
|
| 104 |
+
C10_HOST_DEVICE void test_construct_from_scalar() {
|
| 105 |
+
constexpr scalar_t num1 = scalar_t(1.23);
|
| 106 |
+
constexpr scalar_t num2 = scalar_t(4.56);
|
| 107 |
+
constexpr scalar_t zero = scalar_t();
|
| 108 |
+
static_assert(c10::complex<scalar_t>(num1, num2).real() == num1, "");
|
| 109 |
+
static_assert(c10::complex<scalar_t>(num1, num2).imag() == num2, "");
|
| 110 |
+
static_assert(c10::complex<scalar_t>(num1).real() == num1, "");
|
| 111 |
+
static_assert(c10::complex<scalar_t>(num1).imag() == zero, "");
|
| 112 |
+
static_assert(c10::complex<scalar_t>().real() == zero, "");
|
| 113 |
+
static_assert(c10::complex<scalar_t>().imag() == zero, "");
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
template <typename scalar_t, typename other_t>
|
| 117 |
+
C10_HOST_DEVICE void test_construct_from_other() {
|
| 118 |
+
constexpr other_t num1 = other_t(1.23);
|
| 119 |
+
constexpr other_t num2 = other_t(4.56);
|
| 120 |
+
constexpr scalar_t num3 = scalar_t(num1);
|
| 121 |
+
constexpr scalar_t num4 = scalar_t(num2);
|
| 122 |
+
static_assert(
|
| 123 |
+
c10::complex<scalar_t>(c10::complex<other_t>(num1, num2)).real() == num3,
|
| 124 |
+
"");
|
| 125 |
+
static_assert(
|
| 126 |
+
c10::complex<scalar_t>(c10::complex<other_t>(num1, num2)).imag() == num4,
|
| 127 |
+
"");
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
MAYBE_GLOBAL void test_convert_constructors() {
|
| 131 |
+
test_construct_from_scalar<float>();
|
| 132 |
+
test_construct_from_scalar<double>();
|
| 133 |
+
|
| 134 |
+
static_assert(
|
| 135 |
+
std::is_convertible<c10::complex<float>, c10::complex<float>>::value, "");
|
| 136 |
+
static_assert(
|
| 137 |
+
!std::is_convertible<c10::complex<double>, c10::complex<float>>::value,
|
| 138 |
+
"");
|
| 139 |
+
static_assert(
|
| 140 |
+
std::is_convertible<c10::complex<float>, c10::complex<double>>::value,
|
| 141 |
+
"");
|
| 142 |
+
static_assert(
|
| 143 |
+
std::is_convertible<c10::complex<double>, c10::complex<double>>::value,
|
| 144 |
+
"");
|
| 145 |
+
|
| 146 |
+
static_assert(
|
| 147 |
+
std::is_constructible<c10::complex<float>, c10::complex<float>>::value,
|
| 148 |
+
"");
|
| 149 |
+
static_assert(
|
| 150 |
+
std::is_constructible<c10::complex<double>, c10::complex<float>>::value,
|
| 151 |
+
"");
|
| 152 |
+
static_assert(
|
| 153 |
+
std::is_constructible<c10::complex<float>, c10::complex<double>>::value,
|
| 154 |
+
"");
|
| 155 |
+
static_assert(
|
| 156 |
+
std::is_constructible<c10::complex<double>, c10::complex<double>>::value,
|
| 157 |
+
"");
|
| 158 |
+
|
| 159 |
+
test_construct_from_other<float, float>();
|
| 160 |
+
test_construct_from_other<float, double>();
|
| 161 |
+
test_construct_from_other<double, float>();
|
| 162 |
+
test_construct_from_other<double, double>();
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
template <typename scalar_t>
|
| 166 |
+
C10_HOST_DEVICE void test_construct_from_std() {
|
| 167 |
+
constexpr scalar_t num1 = scalar_t(1.23);
|
| 168 |
+
constexpr scalar_t num2 = scalar_t(4.56);
|
| 169 |
+
static_assert(
|
| 170 |
+
c10::complex<scalar_t>(std::complex<scalar_t>(num1, num2)).real() == num1,
|
| 171 |
+
"");
|
| 172 |
+
static_assert(
|
| 173 |
+
c10::complex<scalar_t>(std::complex<scalar_t>(num1, num2)).imag() == num2,
|
| 174 |
+
"");
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
MAYBE_GLOBAL void test_std_conversion() {
|
| 178 |
+
test_construct_from_std<float>();
|
| 179 |
+
test_construct_from_std<double>();
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
#if defined(__CUDACC__) || defined(__HIPCC__)
|
| 183 |
+
template <typename scalar_t>
|
| 184 |
+
void test_construct_from_thrust() {
|
| 185 |
+
constexpr scalar_t num1 = scalar_t(1.23);
|
| 186 |
+
constexpr scalar_t num2 = scalar_t(4.56);
|
| 187 |
+
ASSERT_EQ(
|
| 188 |
+
c10::complex<scalar_t>(thrust::complex<scalar_t>(num1, num2)).real(),
|
| 189 |
+
num1);
|
| 190 |
+
ASSERT_EQ(
|
| 191 |
+
c10::complex<scalar_t>(thrust::complex<scalar_t>(num1, num2)).imag(),
|
| 192 |
+
num2);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
TEST(TestConstructors, FromThrust) {
|
| 196 |
+
test_construct_from_thrust<float>();
|
| 197 |
+
test_construct_from_thrust<double>();
|
| 198 |
+
}
|
| 199 |
+
#endif
|
| 200 |
+
|
| 201 |
+
TEST(TestConstructors, UnorderedMap) {
|
| 202 |
+
std::unordered_map<
|
| 203 |
+
c10::complex<double>,
|
| 204 |
+
c10::complex<double>,
|
| 205 |
+
c10::hash<c10::complex<double>>>
|
| 206 |
+
m;
|
| 207 |
+
auto key1 = c10::complex<double>(2.5, 3);
|
| 208 |
+
auto key2 = c10::complex<double>(2, 0);
|
| 209 |
+
auto val1 = c10::complex<double>(2, -3.2);
|
| 210 |
+
auto val2 = c10::complex<double>(0, -3);
|
| 211 |
+
m[key1] = val1;
|
| 212 |
+
m[key2] = val2;
|
| 213 |
+
ASSERT_EQ(m[key1], val1);
|
| 214 |
+
ASSERT_EQ(m[key2], val2);
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
} // namespace constructors
|
| 218 |
+
|
| 219 |
+
namespace assignment {
|
| 220 |
+
|
| 221 |
+
template <typename scalar_t>
|
| 222 |
+
constexpr c10::complex<scalar_t> one() {
|
| 223 |
+
c10::complex<scalar_t> result(3, 4);
|
| 224 |
+
result = scalar_t(1);
|
| 225 |
+
return result;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
MAYBE_GLOBAL void test_assign_real() {
|
| 229 |
+
static_assert(one<float>().real() == float(1), "");
|
| 230 |
+
static_assert(one<float>().imag() == float(), "");
|
| 231 |
+
static_assert(one<double>().real() == double(1), "");
|
| 232 |
+
static_assert(one<double>().imag() == double(), "");
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
constexpr std::tuple<c10::complex<double>, c10::complex<float>> one_two() {
|
| 236 |
+
constexpr c10::complex<float> src(1, 2);
|
| 237 |
+
c10::complex<double> ret0;
|
| 238 |
+
c10::complex<float> ret1;
|
| 239 |
+
ret0 = ret1 = src;
|
| 240 |
+
return std::make_tuple(ret0, ret1);
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
MAYBE_GLOBAL void test_assign_other() {
|
| 244 |
+
constexpr auto tup = one_two();
|
| 245 |
+
static_assert(std::get<c10::complex<double>>(tup).real() == double(1), "");
|
| 246 |
+
static_assert(std::get<c10::complex<double>>(tup).imag() == double(2), "");
|
| 247 |
+
static_assert(std::get<c10::complex<float>>(tup).real() == float(1), "");
|
| 248 |
+
static_assert(std::get<c10::complex<float>>(tup).imag() == float(2), "");
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
constexpr std::tuple<c10::complex<double>, c10::complex<float>> one_two_std() {
|
| 252 |
+
constexpr std::complex<float> src(1, 1);
|
| 253 |
+
c10::complex<double> ret0;
|
| 254 |
+
c10::complex<float> ret1;
|
| 255 |
+
ret0 = ret1 = src;
|
| 256 |
+
return std::make_tuple(ret0, ret1);
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
MAYBE_GLOBAL void test_assign_std() {
|
| 260 |
+
constexpr auto tup = one_two();
|
| 261 |
+
static_assert(std::get<c10::complex<double>>(tup).real() == double(1), "");
|
| 262 |
+
static_assert(std::get<c10::complex<double>>(tup).imag() == double(2), "");
|
| 263 |
+
static_assert(std::get<c10::complex<float>>(tup).real() == float(1), "");
|
| 264 |
+
static_assert(std::get<c10::complex<float>>(tup).imag() == float(2), "");
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
#if defined(__CUDACC__) || defined(__HIPCC__)
|
| 268 |
+
C10_HOST_DEVICE std::tuple<c10::complex<double>, c10::complex<float>>
|
| 269 |
+
one_two_thrust() {
|
| 270 |
+
thrust::complex<float> src(1, 2);
|
| 271 |
+
c10::complex<double> ret0;
|
| 272 |
+
c10::complex<float> ret1;
|
| 273 |
+
ret0 = ret1 = src;
|
| 274 |
+
return std::make_tuple(ret0, ret1);
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
TEST(TestAssignment, FromThrust) {
|
| 278 |
+
auto tup = one_two_thrust();
|
| 279 |
+
ASSERT_EQ(std::get<c10::complex<double>>(tup).real(), double(1));
|
| 280 |
+
ASSERT_EQ(std::get<c10::complex<double>>(tup).imag(), double(2));
|
| 281 |
+
ASSERT_EQ(std::get<c10::complex<float>>(tup).real(), float(1));
|
| 282 |
+
ASSERT_EQ(std::get<c10::complex<float>>(tup).imag(), float(2));
|
| 283 |
+
}
|
| 284 |
+
#endif
|
| 285 |
+
|
| 286 |
+
} // namespace assignment
|
| 287 |
+
|
| 288 |
+
namespace literals {
|
| 289 |
+
|
| 290 |
+
MAYBE_GLOBAL void test_complex_literals() {
|
| 291 |
+
using namespace c10::complex_literals;
|
| 292 |
+
static_assert(std::is_same<decltype(0.5_if), c10::complex<float>>::value, "");
|
| 293 |
+
static_assert((0.5_if).real() == float(), "");
|
| 294 |
+
static_assert((0.5_if).imag() == float(0.5), "");
|
| 295 |
+
static_assert(
|
| 296 |
+
std::is_same<decltype(0.5_id), c10::complex<double>>::value, "");
|
| 297 |
+
static_assert((0.5_id).real() == float(), "");
|
| 298 |
+
static_assert((0.5_id).imag() == float(0.5), "");
|
| 299 |
+
|
| 300 |
+
static_assert(std::is_same<decltype(1_if), c10::complex<float>>::value, "");
|
| 301 |
+
static_assert((1_if).real() == float(), "");
|
| 302 |
+
static_assert((1_if).imag() == float(1), "");
|
| 303 |
+
static_assert(std::is_same<decltype(1_id), c10::complex<double>>::value, "");
|
| 304 |
+
static_assert((1_id).real() == double(), "");
|
| 305 |
+
static_assert((1_id).imag() == double(1), "");
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
} // namespace literals
|
| 309 |
+
|
| 310 |
+
namespace real_imag {
|
| 311 |
+
|
| 312 |
+
template <typename scalar_t>
|
| 313 |
+
constexpr c10::complex<scalar_t> zero_one() {
|
| 314 |
+
c10::complex<scalar_t> result;
|
| 315 |
+
result.imag(scalar_t(1));
|
| 316 |
+
return result;
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
template <typename scalar_t>
|
| 320 |
+
constexpr c10::complex<scalar_t> one_zero() {
|
| 321 |
+
c10::complex<scalar_t> result;
|
| 322 |
+
result.real(scalar_t(1));
|
| 323 |
+
return result;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
MAYBE_GLOBAL void test_real_imag_modify() {
|
| 327 |
+
static_assert(zero_one<float>().real() == float(0), "");
|
| 328 |
+
static_assert(zero_one<float>().imag() == float(1), "");
|
| 329 |
+
static_assert(zero_one<double>().real() == double(0), "");
|
| 330 |
+
static_assert(zero_one<double>().imag() == double(1), "");
|
| 331 |
+
|
| 332 |
+
static_assert(one_zero<float>().real() == float(1), "");
|
| 333 |
+
static_assert(one_zero<float>().imag() == float(0), "");
|
| 334 |
+
static_assert(one_zero<double>().real() == double(1), "");
|
| 335 |
+
static_assert(one_zero<double>().imag() == double(0), "");
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
} // namespace real_imag
|
| 339 |
+
|
| 340 |
+
namespace arithmetic_assign {
|
| 341 |
+
|
| 342 |
+
template <typename scalar_t>
|
| 343 |
+
constexpr c10::complex<scalar_t> p(scalar_t value) {
|
| 344 |
+
c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
|
| 345 |
+
result += value;
|
| 346 |
+
return result;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
template <typename scalar_t>
|
| 350 |
+
constexpr c10::complex<scalar_t> m(scalar_t value) {
|
| 351 |
+
c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
|
| 352 |
+
result -= value;
|
| 353 |
+
return result;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
template <typename scalar_t>
|
| 357 |
+
constexpr c10::complex<scalar_t> t(scalar_t value) {
|
| 358 |
+
c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
|
| 359 |
+
result *= value;
|
| 360 |
+
return result;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
template <typename scalar_t>
|
| 364 |
+
constexpr c10::complex<scalar_t> d(scalar_t value) {
|
| 365 |
+
c10::complex<scalar_t> result(scalar_t(2), scalar_t(2));
|
| 366 |
+
result /= value;
|
| 367 |
+
return result;
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
template <typename scalar_t>
|
| 371 |
+
C10_HOST_DEVICE void test_arithmetic_assign_scalar() {
|
| 372 |
+
constexpr c10::complex<scalar_t> x = p(scalar_t(1));
|
| 373 |
+
static_assert(x.real() == scalar_t(3), "");
|
| 374 |
+
static_assert(x.imag() == scalar_t(2), "");
|
| 375 |
+
constexpr c10::complex<scalar_t> y = m(scalar_t(1));
|
| 376 |
+
static_assert(y.real() == scalar_t(1), "");
|
| 377 |
+
static_assert(y.imag() == scalar_t(2), "");
|
| 378 |
+
constexpr c10::complex<scalar_t> z = t(scalar_t(2));
|
| 379 |
+
static_assert(z.real() == scalar_t(4), "");
|
| 380 |
+
static_assert(z.imag() == scalar_t(4), "");
|
| 381 |
+
constexpr c10::complex<scalar_t> t = d(scalar_t(2));
|
| 382 |
+
static_assert(t.real() == scalar_t(1), "");
|
| 383 |
+
static_assert(t.imag() == scalar_t(1), "");
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
template <typename scalar_t, typename rhs_t>
|
| 387 |
+
constexpr c10::complex<scalar_t> p(
|
| 388 |
+
scalar_t real,
|
| 389 |
+
scalar_t imag,
|
| 390 |
+
c10::complex<rhs_t> rhs) {
|
| 391 |
+
c10::complex<scalar_t> result(real, imag);
|
| 392 |
+
result += rhs;
|
| 393 |
+
return result;
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
template <typename scalar_t, typename rhs_t>
|
| 397 |
+
constexpr c10::complex<scalar_t> m(
|
| 398 |
+
scalar_t real,
|
| 399 |
+
scalar_t imag,
|
| 400 |
+
c10::complex<rhs_t> rhs) {
|
| 401 |
+
c10::complex<scalar_t> result(real, imag);
|
| 402 |
+
result -= rhs;
|
| 403 |
+
return result;
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
template <typename scalar_t, typename rhs_t>
|
| 407 |
+
constexpr c10::complex<scalar_t> t(
|
| 408 |
+
scalar_t real,
|
| 409 |
+
scalar_t imag,
|
| 410 |
+
c10::complex<rhs_t> rhs) {
|
| 411 |
+
c10::complex<scalar_t> result(real, imag);
|
| 412 |
+
result *= rhs;
|
| 413 |
+
return result;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
template <typename scalar_t, typename rhs_t>
|
| 417 |
+
constexpr c10::complex<scalar_t> d(
|
| 418 |
+
scalar_t real,
|
| 419 |
+
scalar_t imag,
|
| 420 |
+
c10::complex<rhs_t> rhs) {
|
| 421 |
+
c10::complex<scalar_t> result(real, imag);
|
| 422 |
+
result /= rhs;
|
| 423 |
+
return result;
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
template <typename scalar_t>
|
| 427 |
+
C10_HOST_DEVICE void test_arithmetic_assign_complex() {
|
| 428 |
+
using namespace c10::complex_literals;
|
| 429 |
+
constexpr c10::complex<scalar_t> x2 = p(scalar_t(2), scalar_t(2), 1.0_if);
|
| 430 |
+
static_assert(x2.real() == scalar_t(2), "");
|
| 431 |
+
static_assert(x2.imag() == scalar_t(3), "");
|
| 432 |
+
constexpr c10::complex<scalar_t> x3 = p(scalar_t(2), scalar_t(2), 1.0_id);
|
| 433 |
+
static_assert(x3.real() == scalar_t(2), "");
|
| 434 |
+
|
| 435 |
+
// this test is skipped due to a bug in constexpr evaluation
|
| 436 |
+
// in nvcc. This bug has already been fixed since CUDA 11.2
|
| 437 |
+
#if !defined(__CUDACC__) || (defined(CUDA_VERSION) && CUDA_VERSION >= 11020)
|
| 438 |
+
static_assert(x3.imag() == scalar_t(3), "");
|
| 439 |
+
#endif
|
| 440 |
+
|
| 441 |
+
constexpr c10::complex<scalar_t> y2 = m(scalar_t(2), scalar_t(2), 1.0_if);
|
| 442 |
+
static_assert(y2.real() == scalar_t(2), "");
|
| 443 |
+
static_assert(y2.imag() == scalar_t(1), "");
|
| 444 |
+
constexpr c10::complex<scalar_t> y3 = m(scalar_t(2), scalar_t(2), 1.0_id);
|
| 445 |
+
static_assert(y3.real() == scalar_t(2), "");
|
| 446 |
+
|
| 447 |
+
// this test is skipped due to a bug in constexpr evaluation
|
| 448 |
+
// in nvcc. This bug has already been fixed since CUDA 11.2
|
| 449 |
+
#if !defined(__CUDACC__) || (defined(CUDA_VERSION) && CUDA_VERSION >= 11020)
|
| 450 |
+
static_assert(y3.imag() == scalar_t(1), "");
|
| 451 |
+
#endif
|
| 452 |
+
|
| 453 |
+
constexpr c10::complex<scalar_t> z2 = t(scalar_t(1), scalar_t(-2), 1.0_if);
|
| 454 |
+
static_assert(z2.real() == scalar_t(2), "");
|
| 455 |
+
static_assert(z2.imag() == scalar_t(1), "");
|
| 456 |
+
constexpr c10::complex<scalar_t> z3 = t(scalar_t(1), scalar_t(-2), 1.0_id);
|
| 457 |
+
static_assert(z3.real() == scalar_t(2), "");
|
| 458 |
+
static_assert(z3.imag() == scalar_t(1), "");
|
| 459 |
+
|
| 460 |
+
constexpr c10::complex<scalar_t> t2 = d(scalar_t(-1), scalar_t(2), 1.0_if);
|
| 461 |
+
static_assert(t2.real() == scalar_t(2), "");
|
| 462 |
+
static_assert(t2.imag() == scalar_t(1), "");
|
| 463 |
+
constexpr c10::complex<scalar_t> t3 = d(scalar_t(-1), scalar_t(2), 1.0_id);
|
| 464 |
+
static_assert(t3.real() == scalar_t(2), "");
|
| 465 |
+
static_assert(t3.imag() == scalar_t(1), "");
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
MAYBE_GLOBAL void test_arithmetic_assign() {
|
| 469 |
+
test_arithmetic_assign_scalar<float>();
|
| 470 |
+
test_arithmetic_assign_scalar<double>();
|
| 471 |
+
test_arithmetic_assign_complex<float>();
|
| 472 |
+
test_arithmetic_assign_complex<double>();
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
} // namespace arithmetic_assign
|
| 476 |
+
|
| 477 |
+
namespace arithmetic {
|
| 478 |
+
|
| 479 |
+
template <typename scalar_t>
|
| 480 |
+
C10_HOST_DEVICE void test_arithmetic_() {
|
| 481 |
+
static_assert(
|
| 482 |
+
c10::complex<scalar_t>(1, 2) == +c10::complex<scalar_t>(1, 2), "");
|
| 483 |
+
static_assert(
|
| 484 |
+
c10::complex<scalar_t>(-1, -2) == -c10::complex<scalar_t>(1, 2), "");
|
| 485 |
+
|
| 486 |
+
static_assert(
|
| 487 |
+
c10::complex<scalar_t>(1, 2) + c10::complex<scalar_t>(3, 4) ==
|
| 488 |
+
c10::complex<scalar_t>(4, 6),
|
| 489 |
+
"");
|
| 490 |
+
static_assert(
|
| 491 |
+
c10::complex<scalar_t>(1, 2) + scalar_t(3) ==
|
| 492 |
+
c10::complex<scalar_t>(4, 2),
|
| 493 |
+
"");
|
| 494 |
+
static_assert(
|
| 495 |
+
scalar_t(3) + c10::complex<scalar_t>(1, 2) ==
|
| 496 |
+
c10::complex<scalar_t>(4, 2),
|
| 497 |
+
"");
|
| 498 |
+
|
| 499 |
+
static_assert(
|
| 500 |
+
c10::complex<scalar_t>(1, 2) - c10::complex<scalar_t>(3, 4) ==
|
| 501 |
+
c10::complex<scalar_t>(-2, -2),
|
| 502 |
+
"");
|
| 503 |
+
static_assert(
|
| 504 |
+
c10::complex<scalar_t>(1, 2) - scalar_t(3) ==
|
| 505 |
+
c10::complex<scalar_t>(-2, 2),
|
| 506 |
+
"");
|
| 507 |
+
static_assert(
|
| 508 |
+
scalar_t(3) - c10::complex<scalar_t>(1, 2) ==
|
| 509 |
+
c10::complex<scalar_t>(2, -2),
|
| 510 |
+
"");
|
| 511 |
+
|
| 512 |
+
static_assert(
|
| 513 |
+
c10::complex<scalar_t>(1, 2) * c10::complex<scalar_t>(3, 4) ==
|
| 514 |
+
c10::complex<scalar_t>(-5, 10),
|
| 515 |
+
"");
|
| 516 |
+
static_assert(
|
| 517 |
+
c10::complex<scalar_t>(1, 2) * scalar_t(3) ==
|
| 518 |
+
c10::complex<scalar_t>(3, 6),
|
| 519 |
+
"");
|
| 520 |
+
static_assert(
|
| 521 |
+
scalar_t(3) * c10::complex<scalar_t>(1, 2) ==
|
| 522 |
+
c10::complex<scalar_t>(3, 6),
|
| 523 |
+
"");
|
| 524 |
+
|
| 525 |
+
static_assert(
|
| 526 |
+
c10::complex<scalar_t>(-5, 10) / c10::complex<scalar_t>(3, 4) ==
|
| 527 |
+
c10::complex<scalar_t>(1, 2),
|
| 528 |
+
"");
|
| 529 |
+
static_assert(
|
| 530 |
+
c10::complex<scalar_t>(5, 10) / scalar_t(5) ==
|
| 531 |
+
c10::complex<scalar_t>(1, 2),
|
| 532 |
+
"");
|
| 533 |
+
static_assert(
|
| 534 |
+
scalar_t(25) / c10::complex<scalar_t>(3, 4) ==
|
| 535 |
+
c10::complex<scalar_t>(3, -4),
|
| 536 |
+
"");
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
MAYBE_GLOBAL void test_arithmetic() {
|
| 540 |
+
test_arithmetic_<float>();
|
| 541 |
+
test_arithmetic_<double>();
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
template <typename T, typename int_t>
|
| 545 |
+
void test_binary_ops_for_int_type_(T real, T img, int_t num) {
|
| 546 |
+
c10::complex<T> c(real, img);
|
| 547 |
+
ASSERT_EQ(c + num, c10::complex<T>(real + num, img));
|
| 548 |
+
ASSERT_EQ(num + c, c10::complex<T>(num + real, img));
|
| 549 |
+
ASSERT_EQ(c - num, c10::complex<T>(real - num, img));
|
| 550 |
+
ASSERT_EQ(num - c, c10::complex<T>(num - real, -img));
|
| 551 |
+
ASSERT_EQ(c * num, c10::complex<T>(real * num, img * num));
|
| 552 |
+
ASSERT_EQ(num * c, c10::complex<T>(num * real, num * img));
|
| 553 |
+
ASSERT_EQ(c / num, c10::complex<T>(real / num, img / num));
|
| 554 |
+
ASSERT_EQ(
|
| 555 |
+
num / c,
|
| 556 |
+
c10::complex<T>(num * real / std::norm(c), -num * img / std::norm(c)));
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
template <typename T>
|
| 560 |
+
void test_binary_ops_for_all_int_types_(T real, T img, int8_t i) {
|
| 561 |
+
test_binary_ops_for_int_type_<T, int8_t>(real, img, i);
|
| 562 |
+
test_binary_ops_for_int_type_<T, int16_t>(real, img, i);
|
| 563 |
+
test_binary_ops_for_int_type_<T, int32_t>(real, img, i);
|
| 564 |
+
test_binary_ops_for_int_type_<T, int64_t>(real, img, i);
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
TEST(TestArithmeticIntScalar, All) {
|
| 568 |
+
test_binary_ops_for_all_int_types_<float>(1.0, 0.1, 1);
|
| 569 |
+
test_binary_ops_for_all_int_types_<double>(-1.3, -0.2, -2);
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
} // namespace arithmetic
|
| 573 |
+
|
| 574 |
+
namespace equality {
|
| 575 |
+
|
| 576 |
+
template <typename scalar_t>
|
| 577 |
+
C10_HOST_DEVICE void test_equality_() {
|
| 578 |
+
static_assert(
|
| 579 |
+
c10::complex<scalar_t>(1, 2) == c10::complex<scalar_t>(1, 2), "");
|
| 580 |
+
static_assert(c10::complex<scalar_t>(1, 0) == scalar_t(1), "");
|
| 581 |
+
static_assert(scalar_t(1) == c10::complex<scalar_t>(1, 0), "");
|
| 582 |
+
static_assert(
|
| 583 |
+
c10::complex<scalar_t>(1, 2) != c10::complex<scalar_t>(3, 4), "");
|
| 584 |
+
static_assert(c10::complex<scalar_t>(1, 2) != scalar_t(1), "");
|
| 585 |
+
static_assert(scalar_t(1) != c10::complex<scalar_t>(1, 2), "");
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
MAYBE_GLOBAL void test_equality() {
|
| 589 |
+
test_equality_<float>();
|
| 590 |
+
test_equality_<double>();
|
| 591 |
+
}
|
| 592 |
+
|
| 593 |
+
} // namespace equality
|
| 594 |
+
|
| 595 |
+
namespace io {
|
| 596 |
+
|
| 597 |
+
template <typename scalar_t>
|
| 598 |
+
void test_io_() {
|
| 599 |
+
std::stringstream ss;
|
| 600 |
+
c10::complex<scalar_t> a(1, 2);
|
| 601 |
+
ss << a;
|
| 602 |
+
ASSERT_EQ(ss.str(), "(1,2)");
|
| 603 |
+
ss.str("(3,4)");
|
| 604 |
+
ss >> a;
|
| 605 |
+
ASSERT_TRUE(a == c10::complex<scalar_t>(3, 4));
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
TEST(TestIO, All) {
|
| 609 |
+
test_io_<float>();
|
| 610 |
+
test_io_<double>();
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
} // namespace io
|
| 614 |
+
|
| 615 |
+
namespace test_std {
|
| 616 |
+
|
| 617 |
+
template <typename scalar_t>
|
| 618 |
+
C10_HOST_DEVICE void test_callable_() {
|
| 619 |
+
static_assert(std::real(c10::complex<scalar_t>(1, 2)) == scalar_t(1), "");
|
| 620 |
+
static_assert(std::imag(c10::complex<scalar_t>(1, 2)) == scalar_t(2), "");
|
| 621 |
+
std::abs(c10::complex<scalar_t>(1, 2));
|
| 622 |
+
std::arg(c10::complex<scalar_t>(1, 2));
|
| 623 |
+
static_assert(std::norm(c10::complex<scalar_t>(3, 4)) == scalar_t(25), "");
|
| 624 |
+
static_assert(
|
| 625 |
+
std::conj(c10::complex<scalar_t>(3, 4)) == c10::complex<scalar_t>(3, -4),
|
| 626 |
+
"");
|
| 627 |
+
c10::polar(float(1), float(PI / 2));
|
| 628 |
+
c10::polar(double(1), double(PI / 2));
|
| 629 |
+
}
|
| 630 |
+
|
| 631 |
+
MAYBE_GLOBAL void test_callable() {
|
| 632 |
+
test_callable_<float>();
|
| 633 |
+
test_callable_<double>();
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
template <typename scalar_t>
|
| 637 |
+
void test_values_() {
|
| 638 |
+
ASSERT_EQ(std::abs(c10::complex<scalar_t>(3, 4)), scalar_t(5));
|
| 639 |
+
ASSERT_LT(std::abs(std::arg(c10::complex<scalar_t>(0, 1)) - PI / 2), 1e-6);
|
| 640 |
+
ASSERT_LT(
|
| 641 |
+
std::abs(
|
| 642 |
+
c10::polar(scalar_t(1), scalar_t(PI / 2)) -
|
| 643 |
+
c10::complex<scalar_t>(0, 1)),
|
| 644 |
+
1e-6);
|
| 645 |
+
}
|
| 646 |
+
|
| 647 |
+
TEST(TestStd, BasicFunctions) {
|
| 648 |
+
test_values_<float>();
|
| 649 |
+
test_values_<double>();
|
| 650 |
+
// CSQRT edge cases: checks for overflows which are likely to occur
|
| 651 |
+
// if square root is computed using polar form
|
| 652 |
+
ASSERT_LT(
|
| 653 |
+
std::abs(std::sqrt(c10::complex<float>(-1e20, -4988429.2)).real()), 3e-4);
|
| 654 |
+
ASSERT_LT(
|
| 655 |
+
std::abs(std::sqrt(c10::complex<double>(-1e60, -4988429.2)).real()),
|
| 656 |
+
3e-4);
|
| 657 |
+
}
|
| 658 |
+
|
| 659 |
+
} // namespace test_std
|
| 660 |
+
|
| 661 |
+
#else
|
| 662 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 663 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/AbortHandler.h
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <c10/macros/Macros.h>
|
| 3 |
+
#include <c10/util/Backtrace.h>
|
| 4 |
+
#include <c10/util/env.h>
|
| 5 |
+
#include <cstdlib>
|
| 6 |
+
#include <exception>
|
| 7 |
+
#include <iostream>
|
| 8 |
+
#include <mutex>
|
| 9 |
+
#include <optional>
|
| 10 |
+
|
| 11 |
+
namespace c10 {
|
| 12 |
+
class AbortHandlerHelper {
|
| 13 |
+
public:
|
| 14 |
+
static AbortHandlerHelper& getInstance() {
|
| 15 |
+
#ifdef _WIN32
|
| 16 |
+
thread_local
|
| 17 |
+
#endif // _WIN32
|
| 18 |
+
static AbortHandlerHelper instance;
|
| 19 |
+
return instance;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
void set(std::terminate_handler handler) {
|
| 23 |
+
std::lock_guard<std::mutex> lk(mutex);
|
| 24 |
+
if (!inited) {
|
| 25 |
+
prev = std::set_terminate(handler);
|
| 26 |
+
curr = std::get_terminate();
|
| 27 |
+
inited = true;
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
std::terminate_handler getPrev() const {
|
| 32 |
+
return prev;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
private:
|
| 36 |
+
std::terminate_handler prev = nullptr;
|
| 37 |
+
std::terminate_handler curr = nullptr;
|
| 38 |
+
bool inited = false;
|
| 39 |
+
std::mutex mutex;
|
| 40 |
+
AbortHandlerHelper() = default;
|
| 41 |
+
~AbortHandlerHelper() {
|
| 42 |
+
// Only restore the handler if we are the current one
|
| 43 |
+
if (inited && curr == std::get_terminate()) {
|
| 44 |
+
std::set_terminate(prev);
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
public:
|
| 49 |
+
AbortHandlerHelper(AbortHandlerHelper const&) = delete;
|
| 50 |
+
void operator=(AbortHandlerHelper const&) = delete;
|
| 51 |
+
AbortHandlerHelper(AbortHandlerHelper&&) = delete;
|
| 52 |
+
void operator=(AbortHandlerHelper&&) = delete;
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
namespace detail {
|
| 56 |
+
C10_ALWAYS_INLINE void terminate_handler() {
|
| 57 |
+
std::cout << "Unhandled exception caught in c10/util/AbortHandler.h" << '\n';
|
| 58 |
+
auto backtrace = get_backtrace();
|
| 59 |
+
std::cout << backtrace << '\n' << std::flush;
|
| 60 |
+
auto prev_handler = AbortHandlerHelper::getInstance().getPrev();
|
| 61 |
+
if (prev_handler) {
|
| 62 |
+
prev_handler();
|
| 63 |
+
} else {
|
| 64 |
+
std::abort();
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
} // namespace detail
|
| 68 |
+
|
| 69 |
+
C10_ALWAYS_INLINE void set_terminate_handler() {
|
| 70 |
+
bool use_custom_terminate = false;
|
| 71 |
+
// On Windows it is enabled by default based on
|
| 72 |
+
// https://github.com/pytorch/pytorch/pull/50320#issuecomment-763147062
|
| 73 |
+
#ifdef _WIN32
|
| 74 |
+
use_custom_terminate = true;
|
| 75 |
+
#endif // _WIN32
|
| 76 |
+
auto result = c10::utils::check_env("TORCH_CUSTOM_TERMINATE");
|
| 77 |
+
if (result != std::nullopt) {
|
| 78 |
+
use_custom_terminate = result.value();
|
| 79 |
+
}
|
| 80 |
+
if (use_custom_terminate) {
|
| 81 |
+
AbortHandlerHelper::getInstance().set(detail::terminate_handler);
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
} // namespace c10
|
| 85 |
+
|
| 86 |
+
#else
|
| 87 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 88 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/AlignOf.h
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
|
| 3 |
+
//
|
| 4 |
+
// The LLVM Compiler Infrastructure
|
| 5 |
+
//
|
| 6 |
+
// This file is distributed under the University of Illinois Open Source
|
| 7 |
+
// License. See LICENSE.TXT for details.
|
| 8 |
+
//
|
| 9 |
+
//===----------------------------------------------------------------------===//
|
| 10 |
+
//
|
| 11 |
+
// This file defines the AlignedCharArray and AlignedCharArrayUnion classes.
|
| 12 |
+
//
|
| 13 |
+
//===----------------------------------------------------------------------===//
|
| 14 |
+
|
| 15 |
+
// ATen: modified from llvm::AlignOf
|
| 16 |
+
// replaced LLVM_ALIGNAS with alignas
|
| 17 |
+
|
| 18 |
+
#pragma once
|
| 19 |
+
|
| 20 |
+
#include <cstddef>
|
| 21 |
+
|
| 22 |
+
namespace c10 {
|
| 23 |
+
|
| 24 |
+
/// \struct AlignedCharArray
|
| 25 |
+
/// \brief Helper for building an aligned character array type.
|
| 26 |
+
///
|
| 27 |
+
/// This template is used to explicitly build up a collection of aligned
|
| 28 |
+
/// character array types. We have to build these up using a macro and explicit
|
| 29 |
+
/// specialization to cope with MSVC (at least till 2015) where only an
|
| 30 |
+
/// integer literal can be used to specify an alignment constraint. Once built
|
| 31 |
+
/// up here, we can then begin to indirect between these using normal C++
|
| 32 |
+
/// template parameters.
|
| 33 |
+
|
| 34 |
+
// MSVC requires special handling here.
|
| 35 |
+
#ifndef _MSC_VER
|
| 36 |
+
|
| 37 |
+
template <size_t Alignment, size_t Size>
|
| 38 |
+
struct AlignedCharArray {
|
| 39 |
+
// NOLINTNEXTLINE(*c-arrays)
|
| 40 |
+
alignas(Alignment) char buffer[Size];
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
#else // _MSC_VER
|
| 44 |
+
|
| 45 |
+
/// \brief Create a type with an aligned char buffer.
|
| 46 |
+
template <size_t Alignment, size_t Size>
|
| 47 |
+
struct AlignedCharArray;
|
| 48 |
+
|
| 49 |
+
// We provide special variations of this template for the most common
|
| 50 |
+
// alignments because __declspec(align(...)) doesn't actually work when it is
|
| 51 |
+
// a member of a by-value function argument in MSVC, even if the alignment
|
| 52 |
+
// request is something reasonably like 8-byte or 16-byte. Note that we can't
|
| 53 |
+
// even include the declspec with the union that forces the alignment because
|
| 54 |
+
// MSVC warns on the existence of the declspec despite the union member forcing
|
| 55 |
+
// proper alignment.
|
| 56 |
+
|
| 57 |
+
template <size_t Size>
|
| 58 |
+
struct AlignedCharArray<1, Size> {
|
| 59 |
+
union {
|
| 60 |
+
char aligned;
|
| 61 |
+
char buffer[Size];
|
| 62 |
+
};
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
template <size_t Size>
|
| 66 |
+
struct AlignedCharArray<2, Size> {
|
| 67 |
+
union {
|
| 68 |
+
short aligned;
|
| 69 |
+
char buffer[Size];
|
| 70 |
+
};
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
template <size_t Size>
|
| 74 |
+
struct AlignedCharArray<4, Size> {
|
| 75 |
+
union {
|
| 76 |
+
int aligned;
|
| 77 |
+
char buffer[Size];
|
| 78 |
+
};
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
template <size_t Size>
|
| 82 |
+
struct AlignedCharArray<8, Size> {
|
| 83 |
+
union {
|
| 84 |
+
double aligned;
|
| 85 |
+
char buffer[Size];
|
| 86 |
+
};
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
// The rest of these are provided with a __declspec(align(...)) and we simply
|
| 90 |
+
// can't pass them by-value as function arguments on MSVC.
|
| 91 |
+
|
| 92 |
+
#define AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
|
| 93 |
+
template <size_t Size> \
|
| 94 |
+
struct AlignedCharArray<x, Size> { \
|
| 95 |
+
__declspec(align(x)) char buffer[Size]; \
|
| 96 |
+
};
|
| 97 |
+
|
| 98 |
+
AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
|
| 99 |
+
AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
|
| 100 |
+
AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
|
| 101 |
+
AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
|
| 102 |
+
|
| 103 |
+
#undef AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
|
| 104 |
+
|
| 105 |
+
#endif // _MSC_VER
|
| 106 |
+
|
| 107 |
+
namespace detail {
|
| 108 |
+
template <
|
| 109 |
+
typename T1,
|
| 110 |
+
typename T2 = char,
|
| 111 |
+
typename T3 = char,
|
| 112 |
+
typename T4 = char,
|
| 113 |
+
typename T5 = char,
|
| 114 |
+
typename T6 = char,
|
| 115 |
+
typename T7 = char,
|
| 116 |
+
typename T8 = char,
|
| 117 |
+
typename T9 = char,
|
| 118 |
+
typename T10 = char>
|
| 119 |
+
class AlignerImpl {
|
| 120 |
+
T1 t1;
|
| 121 |
+
T2 t2;
|
| 122 |
+
T3 t3;
|
| 123 |
+
T4 t4;
|
| 124 |
+
T5 t5;
|
| 125 |
+
T6 t6;
|
| 126 |
+
T7 t7;
|
| 127 |
+
T8 t8;
|
| 128 |
+
T9 t9;
|
| 129 |
+
T10 t10;
|
| 130 |
+
|
| 131 |
+
public:
|
| 132 |
+
AlignerImpl() = delete;
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
template <
|
| 136 |
+
typename T1,
|
| 137 |
+
typename T2 = char,
|
| 138 |
+
typename T3 = char,
|
| 139 |
+
typename T4 = char,
|
| 140 |
+
typename T5 = char,
|
| 141 |
+
typename T6 = char,
|
| 142 |
+
typename T7 = char,
|
| 143 |
+
typename T8 = char,
|
| 144 |
+
typename T9 = char,
|
| 145 |
+
typename T10 = char>
|
| 146 |
+
union SizerImpl {
|
| 147 |
+
// NOLINTNEXTLINE(*c-arrays)
|
| 148 |
+
char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
|
| 149 |
+
arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
|
| 150 |
+
arr9[sizeof(T9)], arr10[sizeof(T10)];
|
| 151 |
+
};
|
| 152 |
+
} // end namespace detail
|
| 153 |
+
|
| 154 |
+
/// \brief This union template exposes a suitably aligned and sized character
|
| 155 |
+
/// array member which can hold elements of any of up to ten types.
|
| 156 |
+
///
|
| 157 |
+
/// These types may be arrays, structs, or any other types. The goal is to
|
| 158 |
+
/// expose a char array buffer member which can be used as suitable storage for
|
| 159 |
+
/// a placement new of any of these types. Support for more than ten types can
|
| 160 |
+
/// be added at the cost of more boilerplate.
|
| 161 |
+
template <
|
| 162 |
+
typename T1,
|
| 163 |
+
typename T2 = char,
|
| 164 |
+
typename T3 = char,
|
| 165 |
+
typename T4 = char,
|
| 166 |
+
typename T5 = char,
|
| 167 |
+
typename T6 = char,
|
| 168 |
+
typename T7 = char,
|
| 169 |
+
typename T8 = char,
|
| 170 |
+
typename T9 = char,
|
| 171 |
+
typename T10 = char>
|
| 172 |
+
struct AlignedCharArrayUnion
|
| 173 |
+
: AlignedCharArray<
|
| 174 |
+
alignof(detail::AlignerImpl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>),
|
| 175 |
+
sizeof(::c10::detail::
|
| 176 |
+
SizerImpl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>)> {};
|
| 177 |
+
} // end namespace c10
|
| 178 |
+
|
| 179 |
+
#else
|
| 180 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 181 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/ApproximateClock.h
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
// Copyright 2023-present Facebook. All Rights Reserved.
|
| 3 |
+
|
| 4 |
+
#pragma once
|
| 5 |
+
|
| 6 |
+
#include <c10/macros/Export.h>
|
| 7 |
+
#include <array>
|
| 8 |
+
#include <chrono>
|
| 9 |
+
#include <cstddef>
|
| 10 |
+
#include <cstdint>
|
| 11 |
+
#include <ctime>
|
| 12 |
+
#include <functional>
|
| 13 |
+
#include <type_traits>
|
| 14 |
+
|
| 15 |
+
#if defined(C10_IOS) && defined(C10_MOBILE)
|
| 16 |
+
#include <sys/time.h> // for gettimeofday()
|
| 17 |
+
#endif
|
| 18 |
+
|
| 19 |
+
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
|
| 20 |
+
#define C10_RDTSC
|
| 21 |
+
#if defined(_MSC_VER)
|
| 22 |
+
#include <intrin.h>
|
| 23 |
+
#elif defined(__CUDACC__) || defined(__HIPCC__)
|
| 24 |
+
#undef C10_RDTSC
|
| 25 |
+
#elif defined(__clang__)
|
| 26 |
+
// `__rdtsc` is available by default.
|
| 27 |
+
// NB: This has to be first, because Clang will also define `__GNUC__`
|
| 28 |
+
#elif defined(__GNUC__)
|
| 29 |
+
#include <x86intrin.h>
|
| 30 |
+
#else
|
| 31 |
+
#undef C10_RDTSC
|
| 32 |
+
#endif
|
| 33 |
+
#endif
|
| 34 |
+
|
| 35 |
+
namespace c10 {
|
| 36 |
+
|
| 37 |
+
using time_t = int64_t;
|
| 38 |
+
using steady_clock_t = std::conditional_t<
|
| 39 |
+
std::chrono::high_resolution_clock::is_steady,
|
| 40 |
+
std::chrono::high_resolution_clock,
|
| 41 |
+
std::chrono::steady_clock>;
|
| 42 |
+
|
| 43 |
+
inline time_t getTimeSinceEpoch() {
|
| 44 |
+
auto now = std::chrono::system_clock::now().time_since_epoch();
|
| 45 |
+
return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
inline time_t getTime(bool allow_monotonic = false) {
|
| 49 |
+
#if defined(C10_IOS) && defined(C10_MOBILE)
|
| 50 |
+
// clock_gettime is only available on iOS 10.0 or newer. Unlike OS X, iOS
|
| 51 |
+
// can't rely on CLOCK_REALTIME, as it is defined no matter if clock_gettime
|
| 52 |
+
// is implemented or not
|
| 53 |
+
struct timeval now;
|
| 54 |
+
gettimeofday(&now, NULL);
|
| 55 |
+
return static_cast<time_t>(now.tv_sec) * 1000000000 +
|
| 56 |
+
static_cast<time_t>(now.tv_usec) * 1000;
|
| 57 |
+
#elif defined(_WIN32) || defined(__MACH__)
|
| 58 |
+
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
| 59 |
+
steady_clock_t::now().time_since_epoch())
|
| 60 |
+
.count();
|
| 61 |
+
#else
|
| 62 |
+
// clock_gettime is *much* faster than std::chrono implementation on Linux
|
| 63 |
+
struct timespec t{};
|
| 64 |
+
auto mode = CLOCK_REALTIME;
|
| 65 |
+
if (allow_monotonic) {
|
| 66 |
+
mode = CLOCK_MONOTONIC;
|
| 67 |
+
}
|
| 68 |
+
clock_gettime(mode, &t);
|
| 69 |
+
return static_cast<time_t>(t.tv_sec) * 1000000000 +
|
| 70 |
+
static_cast<time_t>(t.tv_nsec);
|
| 71 |
+
#endif
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// We often do not need to capture true wall times. If a fast mechanism such
|
| 75 |
+
// as TSC is available we can use that instead and convert back to epoch time
|
| 76 |
+
// during post processing. This greatly reduce the clock's contribution to
|
| 77 |
+
// profiling.
|
| 78 |
+
// http://btorpey.github.io/blog/2014/02/18/clock-sources-in-linux/
|
| 79 |
+
// https://quick-bench.com/q/r8opkkGZSJMu9wM_XTbDouq-0Io
|
| 80 |
+
// TODO: We should use
|
| 81 |
+
// `https://github.com/google/benchmark/blob/main/src/cycleclock.h`
|
| 82 |
+
inline auto getApproximateTime() {
|
| 83 |
+
#if defined(C10_RDTSC)
|
| 84 |
+
return static_cast<uint64_t>(__rdtsc());
|
| 85 |
+
#else
|
| 86 |
+
return getTime();
|
| 87 |
+
#endif
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
using approx_time_t = decltype(getApproximateTime());
|
| 91 |
+
static_assert(
|
| 92 |
+
std::is_same_v<approx_time_t, int64_t> ||
|
| 93 |
+
std::is_same_v<approx_time_t, uint64_t>,
|
| 94 |
+
"Expected either int64_t (`getTime`) or uint64_t (some TSC reads).");
|
| 95 |
+
|
| 96 |
+
// Convert `getCount` results to Nanoseconds since unix epoch.
|
| 97 |
+
class C10_API ApproximateClockToUnixTimeConverter final {
|
| 98 |
+
public:
|
| 99 |
+
ApproximateClockToUnixTimeConverter();
|
| 100 |
+
std::function<time_t(approx_time_t)> makeConverter();
|
| 101 |
+
|
| 102 |
+
struct UnixAndApproximateTimePair {
|
| 103 |
+
time_t t_;
|
| 104 |
+
approx_time_t approx_t_;
|
| 105 |
+
};
|
| 106 |
+
static UnixAndApproximateTimePair measurePair();
|
| 107 |
+
|
| 108 |
+
private:
|
| 109 |
+
static constexpr size_t replicates = 1001;
|
| 110 |
+
using time_pairs = std::array<UnixAndApproximateTimePair, replicates>;
|
| 111 |
+
time_pairs measurePairs();
|
| 112 |
+
|
| 113 |
+
time_pairs start_times_;
|
| 114 |
+
};
|
| 115 |
+
|
| 116 |
+
} // namespace c10
|
| 117 |
+
|
| 118 |
+
#else
|
| 119 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 120 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/Array.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <array>
|
| 5 |
+
#include <utility>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
|
| 9 |
+
// This helper function creates a constexpr std::array
|
| 10 |
+
// From a compile time list of values, without requiring you to explicitly
|
| 11 |
+
// write out the length.
|
| 12 |
+
//
|
| 13 |
+
// See also https://stackoverflow.com/a/26351760/23845
|
| 14 |
+
template <typename V, typename... T>
|
| 15 |
+
inline constexpr auto array_of(T&&... t) -> std::array<V, sizeof...(T)> {
|
| 16 |
+
return {{std::forward<T>(t)...}};
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
} // namespace c10
|
| 20 |
+
|
| 21 |
+
#else
|
| 22 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 23 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/ArrayRef.h
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
//===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
|
| 3 |
+
//
|
| 4 |
+
// The LLVM Compiler Infrastructure
|
| 5 |
+
//
|
| 6 |
+
// This file is distributed under the University of Illinois Open Source
|
| 7 |
+
// License. See LICENSE.TXT for details.
|
| 8 |
+
//
|
| 9 |
+
//===----------------------------------------------------------------------===//
|
| 10 |
+
|
| 11 |
+
// ATen: modified from llvm::ArrayRef.
|
| 12 |
+
// removed llvm-specific functionality
|
| 13 |
+
// removed some implicit const -> non-const conversions that rely on
|
| 14 |
+
// complicated std::enable_if meta-programming
|
| 15 |
+
// removed a bunch of slice variants for simplicity...
|
| 16 |
+
|
| 17 |
+
#pragma once
|
| 18 |
+
|
| 19 |
+
#include <c10/macros/Macros.h>
|
| 20 |
+
#include <c10/util/Exception.h>
|
| 21 |
+
#include <c10/util/SmallVector.h>
|
| 22 |
+
#include <torch/headeronly/util/HeaderOnlyArrayRef.h>
|
| 23 |
+
|
| 24 |
+
#include <array>
|
| 25 |
+
#include <cstddef>
|
| 26 |
+
#include <cstdint>
|
| 27 |
+
#include <initializer_list>
|
| 28 |
+
#include <iterator>
|
| 29 |
+
#include <ostream>
|
| 30 |
+
#include <type_traits>
|
| 31 |
+
#include <vector>
|
| 32 |
+
|
| 33 |
+
namespace c10 {
|
| 34 |
+
/// ArrayRef - Represent a constant reference to an array (0 or more elements
|
| 35 |
+
/// consecutively in memory), i.e. a start pointer and a length. It allows
|
| 36 |
+
/// various APIs to take consecutive elements easily and conveniently.
|
| 37 |
+
///
|
| 38 |
+
/// This class does not own the underlying data, it is expected to be used in
|
| 39 |
+
/// situations where the data resides in some other buffer, whose lifetime
|
| 40 |
+
/// extends past that of the ArrayRef. For this reason, it is not in general
|
| 41 |
+
/// safe to store an ArrayRef.
|
| 42 |
+
///
|
| 43 |
+
/// This is intended to be trivially copyable, so it should be passed by
|
| 44 |
+
/// value.
|
| 45 |
+
///
|
| 46 |
+
/// NOTE: We have refactored out the headeronly parts of the ArrayRef struct
|
| 47 |
+
/// into HeaderOnlyArrayRef. As adding `virtual` would change the performance of
|
| 48 |
+
/// the underlying constexpr calls, we rely on apparent-type dispatch for
|
| 49 |
+
/// inheritance. This should be fine because their memory format is the same,
|
| 50 |
+
/// and it is never incorrect for ArrayRef to call HeaderOnlyArrayRef methods.
|
| 51 |
+
/// However, you should prefer to use ArrayRef when possible, because its use
|
| 52 |
+
/// of TORCH_CHECK will lead to better user-facing error messages.
|
| 53 |
+
template <typename T>
|
| 54 |
+
// ArrayRef cannot be derived from. Normally, we would use `final`
|
| 55 |
+
// specifier to force this constraint at compile time. However, Intel
|
| 56 |
+
// compiler does not recognize ArrayRef as a class template (which is
|
| 57 |
+
// required in the definition of at::TensorAccessor, for instance)
|
| 58 |
+
// when `final` specifier is used. So, we cannot define ArrayRef as
|
| 59 |
+
// final because of the Intel compiler issue.
|
| 60 |
+
class ArrayRef : public HeaderOnlyArrayRef<T> {
|
| 61 |
+
public:
|
| 62 |
+
/// @name Constructors, all inherited from HeaderOnlyArrayRef except for
|
| 63 |
+
/// SmallVector. As inherited constructors won't work with class template
|
| 64 |
+
/// argument deduction (CTAD) until C++23, we add deduction guides after
|
| 65 |
+
/// the class definition to enable CTAD.
|
| 66 |
+
/// @{
|
| 67 |
+
|
| 68 |
+
using HeaderOnlyArrayRef<T>::HeaderOnlyArrayRef;
|
| 69 |
+
|
| 70 |
+
/// Construct an ArrayRef from a SmallVector. This is templated in order to
|
| 71 |
+
/// avoid instantiating SmallVectorTemplateCommon<T> whenever we
|
| 72 |
+
/// copy-construct an ArrayRef.
|
| 73 |
+
/// NOTE: this is the only constructor that is not inherited from
|
| 74 |
+
/// HeaderOnlyArrayRef.
|
| 75 |
+
template <typename U>
|
| 76 |
+
/* implicit */ ArrayRef(const SmallVectorTemplateCommon<T, U>& Vec)
|
| 77 |
+
: HeaderOnlyArrayRef<T>(Vec.data(), Vec.size()) {}
|
| 78 |
+
|
| 79 |
+
/// @}
|
| 80 |
+
/// @name Simple Operations, mostly inherited from HeaderOnlyArrayRef
|
| 81 |
+
/// @{
|
| 82 |
+
|
| 83 |
+
/// front - Get the first element.
|
| 84 |
+
/// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of
|
| 85 |
+
/// STD_TORCH_CHECK
|
| 86 |
+
constexpr const T& front() const {
|
| 87 |
+
TORCH_CHECK(
|
| 88 |
+
!this->empty(), "ArrayRef: attempted to access front() of empty list");
|
| 89 |
+
return this->Data[0];
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/// back - Get the last element.
|
| 93 |
+
/// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of
|
| 94 |
+
/// STD_TORCH_CHECK
|
| 95 |
+
constexpr const T& back() const {
|
| 96 |
+
TORCH_CHECK(
|
| 97 |
+
!this->empty(), "ArrayRef: attempted to access back() of empty list");
|
| 98 |
+
return this->Data[this->Length - 1];
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/// slice(n, m) - Take M elements of the array starting at element N
|
| 102 |
+
/// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of
|
| 103 |
+
/// STD_TORCH_CHECK
|
| 104 |
+
constexpr ArrayRef<T> slice(size_t N, size_t M) const {
|
| 105 |
+
TORCH_CHECK(
|
| 106 |
+
N + M <= this->size(),
|
| 107 |
+
"ArrayRef: invalid slice, N = ",
|
| 108 |
+
N,
|
| 109 |
+
"; M = ",
|
| 110 |
+
M,
|
| 111 |
+
"; size = ",
|
| 112 |
+
this->size());
|
| 113 |
+
return ArrayRef<T>(this->data() + N, M);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/// slice(n) - Chop off the first N elements of the array.
|
| 117 |
+
/// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of
|
| 118 |
+
/// STD_TORCH_CHECK
|
| 119 |
+
constexpr ArrayRef<T> slice(size_t N) const {
|
| 120 |
+
TORCH_CHECK(
|
| 121 |
+
N <= this->size(),
|
| 122 |
+
"ArrayRef: invalid slice, N = ",
|
| 123 |
+
N,
|
| 124 |
+
"; size = ",
|
| 125 |
+
this->size());
|
| 126 |
+
return slice(N, this->size() - N); // should this slice be this->slice?
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/// @}
|
| 130 |
+
/// @name Operator Overloads
|
| 131 |
+
/// @{
|
| 132 |
+
|
| 133 |
+
/// Vector compatibility
|
| 134 |
+
/// We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of
|
| 135 |
+
/// STD_TORCH_CHECK
|
| 136 |
+
constexpr const T& at(size_t Index) const {
|
| 137 |
+
TORCH_CHECK(
|
| 138 |
+
Index < this->Length,
|
| 139 |
+
"ArrayRef: invalid index Index = ",
|
| 140 |
+
Index,
|
| 141 |
+
"; Length = ",
|
| 142 |
+
this->Length);
|
| 143 |
+
return this->Data[Index];
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
/// Disallow accidental assignment from a temporary.
|
| 147 |
+
///
|
| 148 |
+
/// The declaration here is extra complicated so that "arrayRef = {}"
|
| 149 |
+
/// continues to select the move assignment operator.
|
| 150 |
+
template <typename U>
|
| 151 |
+
std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(
|
| 152 |
+
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
|
| 153 |
+
U&& Temporary) = delete;
|
| 154 |
+
|
| 155 |
+
/// Disallow accidental assignment from a temporary.
|
| 156 |
+
///
|
| 157 |
+
/// The declaration here is extra complicated so that "arrayRef = {}"
|
| 158 |
+
/// continues to select the move assignment operator.
|
| 159 |
+
template <typename U>
|
| 160 |
+
std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(
|
| 161 |
+
std::initializer_list<U>) = delete;
|
| 162 |
+
|
| 163 |
+
/// @}
|
| 164 |
+
};
|
| 165 |
+
|
| 166 |
+
/// Deduction guides for ArrayRef to support CTAD with inherited constructors
|
| 167 |
+
/// These mirror the constructors inherited from HeaderOnlyArrayRef
|
| 168 |
+
/// @{
|
| 169 |
+
|
| 170 |
+
// Single element constructor
|
| 171 |
+
template <typename T>
|
| 172 |
+
ArrayRef(const T&) -> ArrayRef<T>;
|
| 173 |
+
|
| 174 |
+
// Pointer and length constructor
|
| 175 |
+
template <typename T>
|
| 176 |
+
ArrayRef(const T*, size_t) -> ArrayRef<T>;
|
| 177 |
+
|
| 178 |
+
// Range constructor (begin, end)
|
| 179 |
+
template <typename T>
|
| 180 |
+
ArrayRef(const T*, const T*) -> ArrayRef<T>;
|
| 181 |
+
|
| 182 |
+
// Generic container constructor (anything with .data() and .size())
|
| 183 |
+
template <typename Container>
|
| 184 |
+
ArrayRef(const Container&) -> ArrayRef<
|
| 185 |
+
std::remove_pointer_t<decltype(std::declval<Container>().data())>>;
|
| 186 |
+
|
| 187 |
+
// std::vector constructor
|
| 188 |
+
template <typename T, typename A>
|
| 189 |
+
ArrayRef(const std::vector<T, A>&) -> ArrayRef<T>;
|
| 190 |
+
|
| 191 |
+
// std::array constructor
|
| 192 |
+
template <typename T, size_t N>
|
| 193 |
+
ArrayRef(const std::array<T, N>&) -> ArrayRef<T>;
|
| 194 |
+
|
| 195 |
+
// C array constructor
|
| 196 |
+
template <typename T, size_t N>
|
| 197 |
+
ArrayRef(const T (&)[N]) -> ArrayRef<T>;
|
| 198 |
+
|
| 199 |
+
// std::initializer_list constructor
|
| 200 |
+
template <typename T>
|
| 201 |
+
ArrayRef(const std::initializer_list<T>&) -> ArrayRef<T>;
|
| 202 |
+
|
| 203 |
+
/// @}
|
| 204 |
+
|
| 205 |
+
template <typename T>
|
| 206 |
+
std::ostream& operator<<(std::ostream& out, ArrayRef<T> list) {
|
| 207 |
+
int i = 0;
|
| 208 |
+
out << '[';
|
| 209 |
+
for (const auto& e : list) {
|
| 210 |
+
if (i++ > 0)
|
| 211 |
+
out << ", ";
|
| 212 |
+
out << e;
|
| 213 |
+
}
|
| 214 |
+
out << ']';
|
| 215 |
+
return out;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
/// @name ArrayRef Convenience constructors
|
| 219 |
+
/// @{
|
| 220 |
+
|
| 221 |
+
/// Construct an ArrayRef from a single element.
|
| 222 |
+
template <typename T>
|
| 223 |
+
ArrayRef<T> makeArrayRef(const T& OneElt) {
|
| 224 |
+
return OneElt;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
/// Construct an ArrayRef from a pointer and length.
|
| 228 |
+
template <typename T>
|
| 229 |
+
ArrayRef<T> makeArrayRef(const T* data, size_t length) {
|
| 230 |
+
return ArrayRef<T>(data, length);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/// Construct an ArrayRef from a range.
|
| 234 |
+
template <typename T>
|
| 235 |
+
ArrayRef<T> makeArrayRef(const T* begin, const T* end) {
|
| 236 |
+
return ArrayRef<T>(begin, end);
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
/// Construct an ArrayRef from a SmallVector.
|
| 240 |
+
template <typename T>
|
| 241 |
+
ArrayRef<T> makeArrayRef(const SmallVectorImpl<T>& Vec) {
|
| 242 |
+
return Vec;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
/// Construct an ArrayRef from a SmallVector.
|
| 246 |
+
template <typename T, unsigned N>
|
| 247 |
+
ArrayRef<T> makeArrayRef(const SmallVector<T, N>& Vec) {
|
| 248 |
+
return Vec;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
/// Construct an ArrayRef from a std::vector.
|
| 252 |
+
template <typename T>
|
| 253 |
+
ArrayRef<T> makeArrayRef(const std::vector<T>& Vec) {
|
| 254 |
+
return Vec;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
/// Construct an ArrayRef from a std::array.
|
| 258 |
+
template <typename T, std::size_t N>
|
| 259 |
+
ArrayRef<T> makeArrayRef(const std::array<T, N>& Arr) {
|
| 260 |
+
return Arr;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
/// Construct an ArrayRef from an ArrayRef (no-op) (const)
|
| 264 |
+
template <typename T>
|
| 265 |
+
ArrayRef<T> makeArrayRef(const ArrayRef<T>& Vec) {
|
| 266 |
+
return Vec;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
/// Construct an ArrayRef from an ArrayRef (no-op)
|
| 270 |
+
template <typename T>
|
| 271 |
+
ArrayRef<T>& makeArrayRef(ArrayRef<T>& Vec) {
|
| 272 |
+
return Vec;
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
/// Construct an ArrayRef from a C array.
|
| 276 |
+
template <typename T, size_t N>
|
| 277 |
+
// NOLINTNEXTLINE(*c-arrays*)
|
| 278 |
+
ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
|
| 279 |
+
return ArrayRef<T>(Arr);
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// WARNING: Template instantiation will NOT be willing to do an implicit
|
| 283 |
+
// conversions to get you to an c10::ArrayRef, which is why we need so
|
| 284 |
+
// many overloads.
|
| 285 |
+
|
| 286 |
+
template <typename T>
|
| 287 |
+
bool operator==(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
|
| 288 |
+
return a1.equals(a2);
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
template <typename T>
|
| 292 |
+
bool operator!=(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
|
| 293 |
+
return !a1.equals(a2);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
template <typename T>
|
| 297 |
+
bool operator==(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
|
| 298 |
+
return c10::ArrayRef<T>(a1).equals(a2);
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
template <typename T>
|
| 302 |
+
bool operator!=(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
|
| 303 |
+
return !c10::ArrayRef<T>(a1).equals(a2);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
template <typename T>
|
| 307 |
+
bool operator==(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
|
| 308 |
+
return a1.equals(c10::ArrayRef<T>(a2));
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
template <typename T>
|
| 312 |
+
bool operator!=(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
|
| 313 |
+
return !a1.equals(c10::ArrayRef<T>(a2));
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
using IntArrayRef = ArrayRef<int64_t>;
|
| 317 |
+
|
| 318 |
+
using IntList [[deprecated(
|
| 319 |
+
"This alias is deprecated because it doesn't make ownership semantics obvious. Use IntArrayRef instead!")]] =
|
| 320 |
+
ArrayRef<int64_t>;
|
| 321 |
+
|
| 322 |
+
} // namespace c10
|
| 323 |
+
|
| 324 |
+
#else
|
| 325 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 326 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16-inl.h
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <torch/headeronly/util/BFloat16.h>
|
| 3 |
+
|
| 4 |
+
#else
|
| 5 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 6 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16-math.h
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/util/BFloat16.h>
|
| 5 |
+
#include <c10/util/Half.h>
|
| 6 |
+
|
| 7 |
+
C10_CLANG_DIAGNOSTIC_PUSH()
|
| 8 |
+
#if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
|
| 9 |
+
C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
|
| 10 |
+
#endif
|
| 11 |
+
|
| 12 |
+
namespace c10 {
|
| 13 |
+
template <typename T>
|
| 14 |
+
struct is_reduced_floating_point
|
| 15 |
+
: std::integral_constant<
|
| 16 |
+
bool,
|
| 17 |
+
std::is_same_v<T, c10::Half> || std::is_same_v<T, c10::BFloat16>> {};
|
| 18 |
+
|
| 19 |
+
template <typename T>
|
| 20 |
+
constexpr bool is_reduced_floating_point_v =
|
| 21 |
+
is_reduced_floating_point<T>::value;
|
| 22 |
+
} // namespace c10
|
| 23 |
+
|
| 24 |
+
namespace std {
|
| 25 |
+
|
| 26 |
+
#if !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
|
| 27 |
+
using c10::is_reduced_floating_point;
|
| 28 |
+
using c10::is_reduced_floating_point_v;
|
| 29 |
+
#endif // !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
|
| 30 |
+
|
| 31 |
+
template <
|
| 32 |
+
typename T,
|
| 33 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 34 |
+
inline T acos(T a) {
|
| 35 |
+
return std::acos(float(a));
|
| 36 |
+
}
|
| 37 |
+
template <
|
| 38 |
+
typename T,
|
| 39 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 40 |
+
inline T asin(T a) {
|
| 41 |
+
return std::asin(float(a));
|
| 42 |
+
}
|
| 43 |
+
template <
|
| 44 |
+
typename T,
|
| 45 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 46 |
+
inline T atan(T a) {
|
| 47 |
+
return std::atan(float(a));
|
| 48 |
+
}
|
| 49 |
+
template <
|
| 50 |
+
typename T,
|
| 51 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 52 |
+
inline T atanh(T a) {
|
| 53 |
+
return std::atanh(float(a));
|
| 54 |
+
}
|
| 55 |
+
template <
|
| 56 |
+
typename T,
|
| 57 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 58 |
+
inline T erf(T a) {
|
| 59 |
+
return std::erf(float(a));
|
| 60 |
+
}
|
| 61 |
+
template <
|
| 62 |
+
typename T,
|
| 63 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 64 |
+
inline T erfc(T a) {
|
| 65 |
+
return std::erfc(float(a));
|
| 66 |
+
}
|
| 67 |
+
template <
|
| 68 |
+
typename T,
|
| 69 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 70 |
+
inline T exp(T a) {
|
| 71 |
+
return std::exp(float(a));
|
| 72 |
+
}
|
| 73 |
+
template <
|
| 74 |
+
typename T,
|
| 75 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 76 |
+
inline T expm1(T a) {
|
| 77 |
+
return std::expm1(float(a));
|
| 78 |
+
}
|
| 79 |
+
template <
|
| 80 |
+
typename T,
|
| 81 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 82 |
+
inline bool isfinite(T a) {
|
| 83 |
+
return std::isfinite(float(a));
|
| 84 |
+
}
|
| 85 |
+
template <
|
| 86 |
+
typename T,
|
| 87 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 88 |
+
inline T log(T a) {
|
| 89 |
+
return std::log(float(a));
|
| 90 |
+
}
|
| 91 |
+
template <
|
| 92 |
+
typename T,
|
| 93 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 94 |
+
inline T log10(T a) {
|
| 95 |
+
return std::log10(float(a));
|
| 96 |
+
}
|
| 97 |
+
template <
|
| 98 |
+
typename T,
|
| 99 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 100 |
+
inline T log1p(T a) {
|
| 101 |
+
return std::log1p(float(a));
|
| 102 |
+
}
|
| 103 |
+
template <
|
| 104 |
+
typename T,
|
| 105 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 106 |
+
inline T log2(T a) {
|
| 107 |
+
return std::log2(float(a));
|
| 108 |
+
}
|
| 109 |
+
template <
|
| 110 |
+
typename T,
|
| 111 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 112 |
+
inline T ceil(T a) {
|
| 113 |
+
return std::ceil(float(a));
|
| 114 |
+
}
|
| 115 |
+
template <
|
| 116 |
+
typename T,
|
| 117 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 118 |
+
inline T cos(T a) {
|
| 119 |
+
return std::cos(float(a));
|
| 120 |
+
}
|
| 121 |
+
template <
|
| 122 |
+
typename T,
|
| 123 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 124 |
+
inline T floor(T a) {
|
| 125 |
+
return std::floor(float(a));
|
| 126 |
+
}
|
| 127 |
+
template <
|
| 128 |
+
typename T,
|
| 129 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 130 |
+
inline T nearbyint(T a) {
|
| 131 |
+
return std::nearbyint(float(a));
|
| 132 |
+
}
|
| 133 |
+
template <
|
| 134 |
+
typename T,
|
| 135 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 136 |
+
inline T sin(T a) {
|
| 137 |
+
return std::sin(float(a));
|
| 138 |
+
}
|
| 139 |
+
template <
|
| 140 |
+
typename T,
|
| 141 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 142 |
+
inline T tan(T a) {
|
| 143 |
+
return std::tan(float(a));
|
| 144 |
+
}
|
| 145 |
+
template <
|
| 146 |
+
typename T,
|
| 147 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 148 |
+
inline T sinh(T a) {
|
| 149 |
+
return std::sinh(float(a));
|
| 150 |
+
}
|
| 151 |
+
template <
|
| 152 |
+
typename T,
|
| 153 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 154 |
+
inline T cosh(T a) {
|
| 155 |
+
return std::cosh(float(a));
|
| 156 |
+
}
|
| 157 |
+
template <
|
| 158 |
+
typename T,
|
| 159 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 160 |
+
inline T tanh(T a) {
|
| 161 |
+
return std::tanh(float(a));
|
| 162 |
+
}
|
| 163 |
+
template <
|
| 164 |
+
typename T,
|
| 165 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 166 |
+
inline T trunc(T a) {
|
| 167 |
+
return std::trunc(float(a));
|
| 168 |
+
}
|
| 169 |
+
template <
|
| 170 |
+
typename T,
|
| 171 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 172 |
+
inline T lgamma(T a) {
|
| 173 |
+
return std::lgamma(float(a));
|
| 174 |
+
}
|
| 175 |
+
template <
|
| 176 |
+
typename T,
|
| 177 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 178 |
+
inline T sqrt(T a) {
|
| 179 |
+
return std::sqrt(float(a));
|
| 180 |
+
}
|
| 181 |
+
template <
|
| 182 |
+
typename T,
|
| 183 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 184 |
+
inline T rsqrt(T a) {
|
| 185 |
+
return 1.0 / std::sqrt(float(a));
|
| 186 |
+
}
|
| 187 |
+
template <
|
| 188 |
+
typename T,
|
| 189 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 190 |
+
inline T abs(T a) {
|
| 191 |
+
return std::abs(float(a));
|
| 192 |
+
}
|
| 193 |
+
#if defined(_MSC_VER) && defined(__CUDACC__)
|
| 194 |
+
template <
|
| 195 |
+
typename T,
|
| 196 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 197 |
+
inline T pow(T a, double b) {
|
| 198 |
+
return std::pow(float(a), float(b));
|
| 199 |
+
}
|
| 200 |
+
#else
|
| 201 |
+
template <
|
| 202 |
+
typename T,
|
| 203 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 204 |
+
inline T pow(T a, double b) {
|
| 205 |
+
return std::pow(float(a), b);
|
| 206 |
+
}
|
| 207 |
+
#endif
|
| 208 |
+
template <
|
| 209 |
+
typename T,
|
| 210 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 211 |
+
inline T pow(T a, T b) {
|
| 212 |
+
return std::pow(float(a), float(b));
|
| 213 |
+
}
|
| 214 |
+
template <
|
| 215 |
+
typename T,
|
| 216 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 217 |
+
inline T fmod(T a, T b) {
|
| 218 |
+
return std::fmod(float(a), float(b));
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
/*
|
| 222 |
+
The following function is inspired from the implementation in `musl`
|
| 223 |
+
Link to License: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
| 224 |
+
----------------------------------------------------------------------
|
| 225 |
+
Copyright © 2005-2020 Rich Felker, et al.
|
| 226 |
+
|
| 227 |
+
Permission is hereby granted, free of charge, to any person obtaining
|
| 228 |
+
a copy of this software and associated documentation files (the
|
| 229 |
+
"Software"), to deal in the Software without restriction, including
|
| 230 |
+
without limitation the rights to use, copy, modify, merge, publish,
|
| 231 |
+
distribute, sublicense, and/or sell copies of the Software, and to
|
| 232 |
+
permit persons to whom the Software is furnished to do so, subject to
|
| 233 |
+
the following conditions:
|
| 234 |
+
|
| 235 |
+
The above copyright notice and this permission notice shall be
|
| 236 |
+
included in all copies or substantial portions of the Software.
|
| 237 |
+
|
| 238 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 239 |
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 240 |
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
| 241 |
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
| 242 |
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
| 243 |
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
| 244 |
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
| 245 |
+
----------------------------------------------------------------------
|
| 246 |
+
*/
|
| 247 |
+
template <
|
| 248 |
+
typename T,
|
| 249 |
+
typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
|
| 250 |
+
C10_HOST_DEVICE inline T nextafter(T from, T to) {
|
| 251 |
+
// Reference:
|
| 252 |
+
// https://git.musl-libc.org/cgit/musl/tree/src/math/nextafter.c
|
| 253 |
+
using int_repr_t = uint16_t;
|
| 254 |
+
constexpr uint8_t bits = 16;
|
| 255 |
+
union {
|
| 256 |
+
T f;
|
| 257 |
+
int_repr_t i;
|
| 258 |
+
} ufrom = {from}, uto = {to};
|
| 259 |
+
|
| 260 |
+
// get a mask to get the sign bit i.e. MSB
|
| 261 |
+
int_repr_t sign_mask = int_repr_t{1} << (bits - 1);
|
| 262 |
+
|
| 263 |
+
// short-circuit: if either is NaN, return NaN
|
| 264 |
+
if (from != from || to != to) {
|
| 265 |
+
return from + to;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
// short-circuit: if they are exactly the same.
|
| 269 |
+
if (ufrom.i == uto.i) {
|
| 270 |
+
return from;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
// mask the sign-bit to zero i.e. positive
|
| 274 |
+
// equivalent to abs(x)
|
| 275 |
+
int_repr_t abs_from = ufrom.i & ~sign_mask;
|
| 276 |
+
int_repr_t abs_to = uto.i & ~sign_mask;
|
| 277 |
+
if (abs_from == 0) {
|
| 278 |
+
// if both are zero but with different sign,
|
| 279 |
+
// preserve the sign of `to`.
|
| 280 |
+
if (abs_to == 0) {
|
| 281 |
+
return to;
|
| 282 |
+
}
|
| 283 |
+
// smallest subnormal with sign of `to`.
|
| 284 |
+
ufrom.i = (uto.i & sign_mask) | int_repr_t{1};
|
| 285 |
+
return ufrom.f;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// if abs(from) > abs(to) or sign(from) != sign(to)
|
| 289 |
+
if (abs_from > abs_to || ((ufrom.i ^ uto.i) & sign_mask)) {
|
| 290 |
+
ufrom.i--;
|
| 291 |
+
} else {
|
| 292 |
+
ufrom.i++;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
return ufrom.f;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
} // namespace std
|
| 299 |
+
|
| 300 |
+
C10_CLANG_DIAGNOSTIC_POP()
|
| 301 |
+
|
| 302 |
+
#else
|
| 303 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 304 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/c10/util/BFloat16.h
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <torch/headeronly/util/BFloat16.h>
|
| 3 |
+
|
| 4 |
+
#else
|
| 5 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 6 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|