Upload folder using huggingface_hub (part 44)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +51 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Layout.h +31 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/MemoryFormat.h +34 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Module.h +11 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/PyInterpreter.h +18 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/PyInterpreterHooks.h +20 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/PyObjectConversion.h +60 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/QScheme.h +32 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Size.h +21 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Storage.h +62 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/StorageMethods.h +13 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/StorageSharing.h +13 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Stream.h +33 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/THConcat.h +24 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/THP.h +25 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/TensorIterator.h +14 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/TypeInfo.h +33 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/Types.h +18 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/pickler.h +190 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/pickler_helper.h +239 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/python_print.h +61 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/source_range_serialization.h +71 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/source_range_serialization_impl.h +33 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/storage_context.h +88 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/type_name_uniquer.h +36 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/unpickler.h +211 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/analysis.h +403 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/block_codegen.h +151 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/bounds_inference.h +80 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/bounds_overlap.h +126 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/codegen.h +282 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cpp_codegen.h +103 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cpp_intrinsics.h +37 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cuda_codegen.h +291 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cuda_random.h +105 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/eval.h +330 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/exceptions.h +90 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/expr.h +498 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions.h +116 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions_core.h +30 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions_registry.h +62 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/fwd_decls.h +130 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/graph_opt.h +116 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/half_support.h +217 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/hash_provider.h +287 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/intrinsic_symbols.h +27 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir.h +921 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_cloner.h +67 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_mutator.h +67 -0
- .venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_printer.h +137 -0
.gitattributes
CHANGED
|
@@ -639,3 +639,54 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 639 |
.venv/Lib/site-packages/torch/fx/__pycache__/graph.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 640 |
.venv/Lib/site-packages/torch/fx/experimental/__pycache__/proxy_tensor.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 641 |
.venv/Lib/site-packages/torch/fx/experimental/__pycache__/symbolic_shapes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
.venv/Lib/site-packages/torch/fx/__pycache__/graph.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 640 |
.venv/Lib/site-packages/torch/fx/experimental/__pycache__/proxy_tensor.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 641 |
.venv/Lib/site-packages/torch/fx/experimental/__pycache__/symbolic_shapes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 642 |
+
.venv/Lib/site-packages/torch/lib/c10.dll filter=lfs diff=lfs merge=lfs -text
|
| 643 |
+
.venv/Lib/site-packages/torch/lib/c10.lib filter=lfs diff=lfs merge=lfs -text
|
| 644 |
+
.venv/Lib/site-packages/torch/lib/c10_cuda.dll filter=lfs diff=lfs merge=lfs -text
|
| 645 |
+
.venv/Lib/site-packages/torch/lib/cpuinfo.lib filter=lfs diff=lfs merge=lfs -text
|
| 646 |
+
.venv/Lib/site-packages/torch/lib/cublas64_13.dll filter=lfs diff=lfs merge=lfs -text
|
| 647 |
+
.venv/Lib/site-packages/torch/lib/cublasLt64_13.dll filter=lfs diff=lfs merge=lfs -text
|
| 648 |
+
.venv/Lib/site-packages/torch/lib/cudart64_13.dll filter=lfs diff=lfs merge=lfs -text
|
| 649 |
+
.venv/Lib/site-packages/torch/lib/cudnn64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 650 |
+
.venv/Lib/site-packages/torch/lib/cudnn_adv64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 651 |
+
.venv/Lib/site-packages/torch/lib/cudnn_cnn64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 652 |
+
.venv/Lib/site-packages/torch/lib/cudnn_engines_precompiled64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 653 |
+
.venv/Lib/site-packages/torch/lib/cudnn_engines_runtime_compiled64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 654 |
+
.venv/Lib/site-packages/torch/lib/cudnn_engines_tensor_ir64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 655 |
+
.venv/Lib/site-packages/torch/lib/cudnn_ext64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 656 |
+
.venv/Lib/site-packages/torch/lib/cudnn_graph64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 657 |
+
.venv/Lib/site-packages/torch/lib/cudnn_heuristic64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 658 |
+
.venv/Lib/site-packages/torch/lib/cudnn_ops64_9.dll filter=lfs diff=lfs merge=lfs -text
|
| 659 |
+
.venv/Lib/site-packages/torch/lib/cufft64_12.dll filter=lfs diff=lfs merge=lfs -text
|
| 660 |
+
.venv/Lib/site-packages/torch/lib/cufftw64_12.dll filter=lfs diff=lfs merge=lfs -text
|
| 661 |
+
.venv/Lib/site-packages/torch/lib/cupti64_2025.3.0.dll filter=lfs diff=lfs merge=lfs -text
|
| 662 |
+
.venv/Lib/site-packages/torch/lib/curand64_10.dll filter=lfs diff=lfs merge=lfs -text
|
| 663 |
+
.venv/Lib/site-packages/torch/lib/cusolver64_12.dll filter=lfs diff=lfs merge=lfs -text
|
| 664 |
+
.venv/Lib/site-packages/torch/lib/cusolverMg64_12.dll filter=lfs diff=lfs merge=lfs -text
|
| 665 |
+
.venv/Lib/site-packages/torch/lib/cusparse64_12.dll filter=lfs diff=lfs merge=lfs -text
|
| 666 |
+
.venv/Lib/site-packages/torch/lib/fmt-module.lib filter=lfs diff=lfs merge=lfs -text
|
| 667 |
+
.venv/Lib/site-packages/torch/lib/fmt.lib filter=lfs diff=lfs merge=lfs -text
|
| 668 |
+
.venv/Lib/site-packages/torch/lib/libiomp5md.dll filter=lfs diff=lfs merge=lfs -text
|
| 669 |
+
.venv/Lib/site-packages/torch/lib/libittnotify.lib filter=lfs diff=lfs merge=lfs -text
|
| 670 |
+
.venv/Lib/site-packages/torch/lib/mimalloc-2.4/mimalloc.lib filter=lfs diff=lfs merge=lfs -text
|
| 671 |
+
.venv/Lib/site-packages/torch/lib/nvJitLink_130_0.dll filter=lfs diff=lfs merge=lfs -text
|
| 672 |
+
.venv/Lib/site-packages/torch/lib/nvperf_host.dll filter=lfs diff=lfs merge=lfs -text
|
| 673 |
+
.venv/Lib/site-packages/torch/lib/nvrtc-builtins64_130.dll filter=lfs diff=lfs merge=lfs -text
|
| 674 |
+
.venv/Lib/site-packages/torch/lib/nvrtc64_130_0.alt.dll filter=lfs diff=lfs merge=lfs -text
|
| 675 |
+
.venv/Lib/site-packages/torch/lib/nvrtc64_130_0.dll filter=lfs diff=lfs merge=lfs -text
|
| 676 |
+
.venv/Lib/site-packages/torch/lib/pthreadpool.lib filter=lfs diff=lfs merge=lfs -text
|
| 677 |
+
.venv/Lib/site-packages/torch/lib/sleef.lib filter=lfs diff=lfs merge=lfs -text
|
| 678 |
+
.venv/Lib/site-packages/torch/lib/torch_cpu.dll filter=lfs diff=lfs merge=lfs -text
|
| 679 |
+
.venv/Lib/site-packages/torch/lib/torch_cpu.lib filter=lfs diff=lfs merge=lfs -text
|
| 680 |
+
.venv/Lib/site-packages/torch/lib/torch_cuda.dll filter=lfs diff=lfs merge=lfs -text
|
| 681 |
+
.venv/Lib/site-packages/torch/lib/torch_cuda.lib filter=lfs diff=lfs merge=lfs -text
|
| 682 |
+
.venv/Lib/site-packages/torch/lib/torch_python.dll filter=lfs diff=lfs merge=lfs -text
|
| 683 |
+
.venv/Lib/site-packages/torch/lib/torch_python.lib filter=lfs diff=lfs merge=lfs -text
|
| 684 |
+
.venv/Lib/site-packages/torch/lib/uv.dll filter=lfs diff=lfs merge=lfs -text
|
| 685 |
+
.venv/Lib/site-packages/torch/linalg/__pycache__/__init__.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 686 |
+
.venv/Lib/site-packages/torch/nn/__pycache__/functional.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 687 |
+
.venv/Lib/site-packages/torch/nn/attention/__pycache__/flex_attention.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 688 |
+
.venv/Lib/site-packages/torch/nn/modules/__pycache__/loss.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 689 |
+
.venv/Lib/site-packages/torch/nn/modules/__pycache__/module.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 690 |
+
.venv/Lib/site-packages/torch/nn/parallel/__pycache__/distributed.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 691 |
+
.venv/Lib/site-packages/torch/onnx/_internal/torchscript_exporter/__pycache__/symbolic_opset9.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 692 |
+
.venv/Lib/site-packages/torch/optim/__pycache__/lr_scheduler.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
.venv/Lib/site-packages/torch/include/torch/csrc/Layout.h
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <torch/csrc/python_headers.h>
|
| 5 |
+
|
| 6 |
+
#include <ATen/Layout.h>
|
| 7 |
+
|
| 8 |
+
#include <string>
|
| 9 |
+
|
| 10 |
+
const int LAYOUT_NAME_LEN = 64;
|
| 11 |
+
|
| 12 |
+
struct THPLayout {
|
| 13 |
+
PyObject_HEAD
|
| 14 |
+
at::Layout layout;
|
| 15 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
|
| 16 |
+
char name[LAYOUT_NAME_LEN + 1];
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
TORCH_PYTHON_API extern PyTypeObject THPLayoutType;
|
| 20 |
+
|
| 21 |
+
inline bool THPLayout_Check(PyObject* obj) {
|
| 22 |
+
return Py_TYPE(obj) == &THPLayoutType;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
PyObject* THPLayout_New(at::Layout layout, const std::string& name);
|
| 26 |
+
|
| 27 |
+
void THPLayout_init(PyObject* module);
|
| 28 |
+
|
| 29 |
+
#else
|
| 30 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 31 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/MemoryFormat.h
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/python_headers.h>
|
| 6 |
+
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
|
| 9 |
+
#include <string>
|
| 10 |
+
|
| 11 |
+
const int MEMORY_FORMAT_NAME_LEN = 64;
|
| 12 |
+
|
| 13 |
+
struct THPMemoryFormat {
|
| 14 |
+
PyObject_HEAD
|
| 15 |
+
at::MemoryFormat memory_format;
|
| 16 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
|
| 17 |
+
char name[MEMORY_FORMAT_NAME_LEN + 1];
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
+
TORCH_PYTHON_API extern PyTypeObject THPMemoryFormatType;
|
| 21 |
+
|
| 22 |
+
inline bool THPMemoryFormat_Check(PyObject* obj) {
|
| 23 |
+
return Py_TYPE(obj) == &THPMemoryFormatType;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
PyObject* THPMemoryFormat_New(
|
| 27 |
+
at::MemoryFormat memory_format,
|
| 28 |
+
const std::string& name);
|
| 29 |
+
|
| 30 |
+
void THPMemoryFormat_init(PyObject* module);
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/Module.h
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_MODULE_INC
|
| 3 |
+
#define THP_MODULE_INC
|
| 4 |
+
|
| 5 |
+
#define THP_STATELESS_ATTRIBUTE_NAME "_torch"
|
| 6 |
+
|
| 7 |
+
#endif
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/PyInterpreter.h
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 5 |
+
#include <torch/csrc/Export.h>
|
| 6 |
+
#include <torch/csrc/utils/pybind.h>
|
| 7 |
+
|
| 8 |
+
namespace torch::detail {
|
| 9 |
+
TORCH_PYTHON_API py::handle getTorchApiFunction(const c10::OperatorHandle& op);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
// TODO: Move these to a proper namespace
|
| 13 |
+
TORCH_PYTHON_API c10::impl::PyInterpreter* getPyInterpreter();
|
| 14 |
+
TORCH_PYTHON_API void initializeGlobalPyInterpreter();
|
| 15 |
+
|
| 16 |
+
#else
|
| 17 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 18 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/PyInterpreterHooks.h
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/PyInterpreterHooks.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::detail {
|
| 7 |
+
|
| 8 |
+
// Concrete implementation of PyInterpreterHooks
|
| 9 |
+
class PyInterpreterHooks : public c10::impl::PyInterpreterHooksInterface {
|
| 10 |
+
public:
|
| 11 |
+
PyInterpreterHooks();
|
| 12 |
+
|
| 13 |
+
c10::impl::PyInterpreter* getPyInterpreter() const override;
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
} // namespace torch::detail
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/PyObjectConversion.h
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/Tensor.h>
|
| 5 |
+
#include <c10/util/python_stub.h>
|
| 6 |
+
#include <torch/csrc/Export.h>
|
| 7 |
+
|
| 8 |
+
// Indirection that lets the libtorch-only Python-interop stable shims call into
|
| 9 |
+
// code that only libtorch_python can provide (THPVariable_* &co) without
|
| 10 |
+
// libtorch (or the user extension) linking libtorch_python. Today this backs
|
| 11 |
+
// PyObject <-> Tensor conversion; other conversions between Python objects and
|
| 12 |
+
// libtorch types that need libtorch_python can be added as further methods.
|
| 13 |
+
//
|
| 14 |
+
// This mirrors c10's PyInterpreterVTable: an abstract interface declared in the
|
| 15 |
+
// lower library, a no-op default that errors, and a concrete implementation
|
| 16 |
+
// registered by libtorch_python at load time. Unlike PyInterpreterVTable this
|
| 17 |
+
// is a single process-global (there is no tagged tensor to route through when
|
| 18 |
+
// converting a raw PyObject*), so it assumes a single Python interpreter.
|
| 19 |
+
//
|
| 20 |
+
// How the pieces fit together:
|
| 21 |
+
// 1. torch/csrc/PyObjectConversion.h (this file) - the vtable interface; in
|
| 22 |
+
// libtorch.
|
| 23 |
+
// 2. torch/csrc/PyObjectConversion.cpp - a NoopPyObjectConversion that errors
|
| 24 |
+
// by default; in libtorch. This fallback fires when libtorch_python was
|
| 25 |
+
// never loaded to register a real implementation.
|
| 26 |
+
// 3. torch/csrc/PyObjectConversionPythonImpl.cpp - the concrete impl using
|
| 27 |
+
// THPVariable_*; in libtorch_python, registers itself at load.
|
| 28 |
+
// 4. torch/csrc/shim_common.cpp - the stable C shims that call through the
|
| 29 |
+
// vtable; in libtorch.
|
| 30 |
+
|
| 31 |
+
namespace torch::detail {
|
| 32 |
+
|
| 33 |
+
struct TORCH_API PyObjectConversionInterface {
|
| 34 |
+
virtual ~PyObjectConversionInterface() = default;
|
| 35 |
+
|
| 36 |
+
// Unpack a Python torch.Tensor (PyObject*) into an at::Tensor that shares the
|
| 37 |
+
// underlying TensorImpl. The GIL must be held.
|
| 38 |
+
virtual at::Tensor tensor_from_pyobject(PyObject* obj) const = 0;
|
| 39 |
+
|
| 40 |
+
// Wrap an at::Tensor as a new-reference Python torch.Tensor. py_type, if
|
| 41 |
+
// non-null, is the result's exact PyTypeObject* (e.g. torch.nn.Parameter);
|
| 42 |
+
// null means the default torch.Tensor type. The GIL must be held.
|
| 43 |
+
virtual PyObject* tensor_to_pyobject(const at::Tensor& t, PyObject* py_type)
|
| 44 |
+
const = 0;
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
// Install the conversion implementation. Called once by libtorch_python when it
|
| 48 |
+
// is loaded. Passing nullptr restores the default no-op implementation.
|
| 49 |
+
TORCH_API void setPyObjectConversionImpl(
|
| 50 |
+
const PyObjectConversionInterface* impl);
|
| 51 |
+
|
| 52 |
+
// Return the registered implementation. Before libtorch_python registers one
|
| 53 |
+
// this is a no-op that raises a clear error when its methods are called.
|
| 54 |
+
TORCH_API const PyObjectConversionInterface& getPyObjectConversionImpl();
|
| 55 |
+
|
| 56 |
+
} // namespace torch::detail
|
| 57 |
+
|
| 58 |
+
#else
|
| 59 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 60 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/QScheme.h
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/python_headers.h>
|
| 6 |
+
|
| 7 |
+
#include <c10/core/QScheme.h>
|
| 8 |
+
|
| 9 |
+
#include <string>
|
| 10 |
+
|
| 11 |
+
constexpr int QSCHEME_NAME_LEN = 64;
|
| 12 |
+
|
| 13 |
+
struct THPQScheme {
|
| 14 |
+
PyObject_HEAD
|
| 15 |
+
at::QScheme qscheme;
|
| 16 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
|
| 17 |
+
char name[QSCHEME_NAME_LEN + 1];
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
+
TORCH_PYTHON_API extern PyTypeObject THPQSchemeType;
|
| 21 |
+
|
| 22 |
+
inline bool THPQScheme_Check(PyObject* obj) {
|
| 23 |
+
return Py_TYPE(obj) == &THPQSchemeType;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
PyObject* THPQScheme_New(at::QScheme qscheme, const std::string& name);
|
| 27 |
+
|
| 28 |
+
void THPQScheme_init(PyObject* module);
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/Size.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/autograd/variable.h>
|
| 6 |
+
#include <torch/csrc/python_headers.h>
|
| 7 |
+
#include <cstdint>
|
| 8 |
+
|
| 9 |
+
TORCH_PYTHON_API extern PyTypeObject THPSizeType;
|
| 10 |
+
|
| 11 |
+
#define THPSize_Check(obj) (Py_TYPE(obj) == &THPSizeType)
|
| 12 |
+
|
| 13 |
+
PyObject* THPSize_New(const torch::autograd::Variable& t);
|
| 14 |
+
PyObject* THPSize_NewFromSizes(int64_t dim, const int64_t* sizes);
|
| 15 |
+
PyObject* THPSize_NewFromSymSizes(const at::Tensor& t);
|
| 16 |
+
|
| 17 |
+
void THPSize_init(PyObject* module);
|
| 18 |
+
|
| 19 |
+
#else
|
| 20 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 21 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/Storage.h
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_STORAGE_INC
|
| 3 |
+
#define THP_STORAGE_INC
|
| 4 |
+
|
| 5 |
+
#include <Python.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <torch/csrc/Exceptions.h>
|
| 8 |
+
#include <torch/csrc/Export.h>
|
| 9 |
+
#include <torch/csrc/Types.h>
|
| 10 |
+
|
| 11 |
+
#define THPStorageStr "torch.UntypedStorage"
|
| 12 |
+
|
| 13 |
+
struct THPStorage {
|
| 14 |
+
PyObject_HEAD
|
| 15 |
+
c10::Storage cdata;
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
TORCH_PYTHON_API PyObject* THPStorage_Wrap(c10::Storage storage);
|
| 19 |
+
TORCH_PYTHON_API PyObject* THPStorage_NewWithStorage(
|
| 20 |
+
PyTypeObject* type,
|
| 21 |
+
c10::Storage _storage);
|
| 22 |
+
TORCH_PYTHON_API extern PyTypeObject* THPStorageClass;
|
| 23 |
+
|
| 24 |
+
inline bool THPStorage_CheckTypeExact(PyTypeObject* tp) {
|
| 25 |
+
return tp == THPStorageClass;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
inline bool THPStorage_CheckExact(PyObject* obj) {
|
| 29 |
+
return THPStorage_CheckTypeExact(Py_TYPE(obj));
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
inline bool THPStorage_Check(PyObject* obj) {
|
| 33 |
+
if (!THPStorageClass)
|
| 34 |
+
return false;
|
| 35 |
+
|
| 36 |
+
const auto result = PyObject_IsInstance(obj, (PyObject*)THPStorageClass);
|
| 37 |
+
if (result == -1)
|
| 38 |
+
throw python_error();
|
| 39 |
+
return result;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
bool THPStorage_init(PyObject* module);
|
| 43 |
+
void THPStorage_postInit(PyObject* module);
|
| 44 |
+
|
| 45 |
+
void THPStorage_assertNotNull(THPStorage* storage);
|
| 46 |
+
TORCH_PYTHON_API void THPStorage_assertNotNull(PyObject* obj);
|
| 47 |
+
|
| 48 |
+
TORCH_PYTHON_API extern PyTypeObject THPStorageType;
|
| 49 |
+
|
| 50 |
+
inline const c10::Storage& THPStorage_Unpack(THPStorage* storage) {
|
| 51 |
+
return storage->cdata;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
inline const c10::Storage& THPStorage_Unpack(PyObject* obj) {
|
| 55 |
+
return THPStorage_Unpack(reinterpret_cast<THPStorage*>(obj));
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
#endif
|
| 59 |
+
|
| 60 |
+
#else
|
| 61 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 62 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/StorageMethods.h
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_STORAGE_METHODS_INC
|
| 3 |
+
#define THP_STORAGE_METHODS_INC
|
| 4 |
+
|
| 5 |
+
#include <Python.h>
|
| 6 |
+
|
| 7 |
+
PyMethodDef* THPStorage_getMethods();
|
| 8 |
+
|
| 9 |
+
#endif
|
| 10 |
+
|
| 11 |
+
#else
|
| 12 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 13 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/StorageSharing.h
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_STORAGE_SHARING_INC
|
| 3 |
+
#define THP_STORAGE_SHARING_INC
|
| 4 |
+
|
| 5 |
+
#include <Python.h>
|
| 6 |
+
|
| 7 |
+
PyMethodDef* THPStorage_getSharingMethods();
|
| 8 |
+
|
| 9 |
+
#endif
|
| 10 |
+
|
| 11 |
+
#else
|
| 12 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 13 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/Stream.h
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_STREAM_INC
|
| 3 |
+
#define THP_STREAM_INC
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Stream.h>
|
| 6 |
+
#include <torch/csrc/Export.h>
|
| 7 |
+
#include <torch/csrc/python_headers.h>
|
| 8 |
+
|
| 9 |
+
struct TORCH_PYTHON_API THPStream {
|
| 10 |
+
PyObject_HEAD
|
| 11 |
+
int64_t stream_id;
|
| 12 |
+
int64_t device_type;
|
| 13 |
+
int64_t device_index;
|
| 14 |
+
// Used to switch stream context management, initialized lazily.
|
| 15 |
+
PyObject* context;
|
| 16 |
+
PyObject* weakreflist;
|
| 17 |
+
};
|
| 18 |
+
extern TORCH_PYTHON_API PyTypeObject* THPStreamClass;
|
| 19 |
+
|
| 20 |
+
TORCH_PYTHON_API void THPStream_init(PyObject* module);
|
| 21 |
+
TORCH_PYTHON_API void THPStream_dealloc_common(THPStream* self);
|
| 22 |
+
|
| 23 |
+
inline bool THPStream_Check(PyObject* obj) {
|
| 24 |
+
return THPStreamClass && PyObject_IsInstance(obj, (PyObject*)THPStreamClass);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
TORCH_PYTHON_API PyObject* THPStream_Wrap(const c10::Stream& stream);
|
| 28 |
+
|
| 29 |
+
#endif // THP_STREAM_INC
|
| 30 |
+
|
| 31 |
+
#else
|
| 32 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 33 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/THConcat.h
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#define TH_CONCAT_STRING_2(x, y) TH_CONCAT_STRING_2_EXPAND(x, y)
|
| 5 |
+
#define TH_CONCAT_STRING_2_EXPAND(x, y) #x #y
|
| 6 |
+
|
| 7 |
+
#define TH_CONCAT_STRING_3(x, y, z) TH_CONCAT_STRING_3_EXPAND(x, y, z)
|
| 8 |
+
#define TH_CONCAT_STRING_3_EXPAND(x, y, z) #x #y #z
|
| 9 |
+
|
| 10 |
+
#define TH_CONCAT_STRING_4(x, y, z, w) TH_CONCAT_STRING_4_EXPAND(x, y, z, w)
|
| 11 |
+
#define TH_CONCAT_STRING_4_EXPAND(x, y, z, w) #x #y #z #w
|
| 12 |
+
|
| 13 |
+
#define TH_CONCAT_2(x, y) TH_CONCAT_2_EXPAND(x, y)
|
| 14 |
+
#define TH_CONCAT_2_EXPAND(x, y) x##y
|
| 15 |
+
|
| 16 |
+
#define TH_CONCAT_3(x, y, z) TH_CONCAT_3_EXPAND(x, y, z)
|
| 17 |
+
#define TH_CONCAT_3_EXPAND(x, y, z) x##y##z
|
| 18 |
+
|
| 19 |
+
#define TH_CONCAT_4_EXPAND(x, y, z, w) x##y##z##w
|
| 20 |
+
#define TH_CONCAT_4(x, y, z, w) TH_CONCAT_4_EXPAND(x, y, z, w)
|
| 21 |
+
|
| 22 |
+
#else
|
| 23 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 24 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/THP.h
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_H
|
| 3 |
+
#define THP_H
|
| 4 |
+
|
| 5 |
+
#include <torch/csrc/Export.h>
|
| 6 |
+
#include <torch/csrc/python_headers.h>
|
| 7 |
+
|
| 8 |
+
#include <torch/csrc/Exceptions.h>
|
| 9 |
+
#include <torch/csrc/Generator.h>
|
| 10 |
+
#include <torch/csrc/Module.h>
|
| 11 |
+
#include <torch/csrc/Size.h>
|
| 12 |
+
#include <torch/csrc/Storage.h>
|
| 13 |
+
#include <torch/csrc/Types.h>
|
| 14 |
+
#include <torch/csrc/utils.h> // This requires defined Storage and Tensor types
|
| 15 |
+
#include <torch/csrc/utils/byte_order.h>
|
| 16 |
+
|
| 17 |
+
#include <torch/csrc/serialization.h>
|
| 18 |
+
|
| 19 |
+
#include <torch/csrc/autograd/python_autograd.h>
|
| 20 |
+
|
| 21 |
+
#endif
|
| 22 |
+
|
| 23 |
+
#else
|
| 24 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 25 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/TensorIterator.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/python_headers.h>
|
| 5 |
+
|
| 6 |
+
namespace torch {
|
| 7 |
+
|
| 8 |
+
void initTensorIteratorBindings(PyObject* module);
|
| 9 |
+
|
| 10 |
+
} // namespace torch
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/TypeInfo.h
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/python_headers.h>
|
| 6 |
+
|
| 7 |
+
#include <ATen/ATen.h>
|
| 8 |
+
|
| 9 |
+
struct THPDTypeInfo {
|
| 10 |
+
PyObject_HEAD
|
| 11 |
+
at::ScalarType type;
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
struct THPFInfo : THPDTypeInfo {};
|
| 15 |
+
|
| 16 |
+
struct THPIInfo : THPDTypeInfo {};
|
| 17 |
+
|
| 18 |
+
TORCH_PYTHON_API extern PyTypeObject THPFInfoType;
|
| 19 |
+
TORCH_PYTHON_API extern PyTypeObject THPIInfoType;
|
| 20 |
+
|
| 21 |
+
inline bool THPFInfo_Check(PyObject* obj) {
|
| 22 |
+
return Py_TYPE(obj) == &THPFInfoType;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
inline bool THPIInfo_Check(PyObject* obj) {
|
| 26 |
+
return Py_TYPE(obj) == &THPIInfoType;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
void THPDTypeInfo_init(PyObject* module);
|
| 30 |
+
|
| 31 |
+
#else
|
| 32 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 33 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/Types.h
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#ifndef THP_TYPES_INC
|
| 3 |
+
#define THP_TYPES_INC
|
| 4 |
+
|
| 5 |
+
#include <cstddef>
|
| 6 |
+
|
| 7 |
+
#ifndef INT64_MAX
|
| 8 |
+
#include <cstdint>
|
| 9 |
+
#endif
|
| 10 |
+
|
| 11 |
+
template <typename T>
|
| 12 |
+
struct THPTypeInfo {};
|
| 13 |
+
|
| 14 |
+
#endif
|
| 15 |
+
|
| 16 |
+
#else
|
| 17 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 18 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/pickler.h
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <string>
|
| 5 |
+
#include <string_view>
|
| 6 |
+
#include <utility>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
#include <ATen/Utils.h>
|
| 10 |
+
#include <ATen/core/ivalue.h>
|
| 11 |
+
#include <ATen/core/jit_type.h>
|
| 12 |
+
#include <ATen/core/qualified_name.h>
|
| 13 |
+
#include <c10/util/ArrayRef.h>
|
| 14 |
+
#include <c10/util/FbcodeMaps.h>
|
| 15 |
+
#include <c10/util/intrusive_ptr.h>
|
| 16 |
+
#include <torch/csrc/Export.h>
|
| 17 |
+
#include <torch/csrc/jit/serialization/pickler_helper.h>
|
| 18 |
+
|
| 19 |
+
namespace torch::jit {
|
| 20 |
+
|
| 21 |
+
using ::c10::IValue;
|
| 22 |
+
|
| 23 |
+
class TORCH_API Pickler {
|
| 24 |
+
AT_DISALLOW_COPY_AND_ASSIGN(Pickler);
|
| 25 |
+
|
| 26 |
+
public:
|
| 27 |
+
Pickler(std::function<void(const char*, size_t)> writer)
|
| 28 |
+
: Pickler(std::move(writer), nullptr, nullptr, nullptr) {}
|
| 29 |
+
|
| 30 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 31 |
+
Pickler(
|
| 32 |
+
std::function<void(const char*, size_t)> writer,
|
| 33 |
+
std::vector<at::Tensor>* tensor_table,
|
| 34 |
+
std::function<c10::QualifiedName(const c10::ClassTypePtr&)> type_renamer,
|
| 35 |
+
std::vector<c10::ClassTypePtr>* memoized_class_types,
|
| 36 |
+
std::function<std::string(const at::Tensor&)> get_tensor_id = nullptr,
|
| 37 |
+
bool tag_aggregates = true)
|
| 38 |
+
: writer_(std::move(writer)),
|
| 39 |
+
tensor_table_(tensor_table),
|
| 40 |
+
type_renamer_(std::move(type_renamer)),
|
| 41 |
+
memoized_class_types_(memoized_class_types),
|
| 42 |
+
get_tensor_id_(std::move(get_tensor_id)),
|
| 43 |
+
tag_aggregates_(tag_aggregates) {}
|
| 44 |
+
~Pickler();
|
| 45 |
+
|
| 46 |
+
// Push protocol onto the stack
|
| 47 |
+
void protocol();
|
| 48 |
+
|
| 49 |
+
// Push STOP PickleOpCode onto the stack
|
| 50 |
+
void stop();
|
| 51 |
+
|
| 52 |
+
void pushIValue(const IValue& ivalue);
|
| 53 |
+
|
| 54 |
+
void startTuple();
|
| 55 |
+
void endTuple();
|
| 56 |
+
|
| 57 |
+
const std::vector<at::Tensor>& tensorData() {
|
| 58 |
+
return tensor_data_;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
void pushEmptyDict();
|
| 62 |
+
void pushDict(const IValue& ivalue);
|
| 63 |
+
void pushInt(int64_t value);
|
| 64 |
+
void pushLong(const std::string& data);
|
| 65 |
+
|
| 66 |
+
private:
|
| 67 |
+
void pushIValueImpl(const IValue& ivalue);
|
| 68 |
+
void startTypeTag();
|
| 69 |
+
void endTypeTag(const IValue& value);
|
| 70 |
+
void pushBool(bool value);
|
| 71 |
+
void pushDouble(double value);
|
| 72 |
+
void pushComplexDouble(const IValue& value);
|
| 73 |
+
void pushGenericList(const IValue& ivalue);
|
| 74 |
+
void pushIntList(const IValue& ivalue);
|
| 75 |
+
void pushList(const IValue& ivalue);
|
| 76 |
+
void pushTensor(const IValue& ivalue);
|
| 77 |
+
void pushTensorReference(const IValue& ivalue);
|
| 78 |
+
void pushLiteralTensor(const IValue& ivalue);
|
| 79 |
+
void pushLiteralSparseTensor(const at::Tensor& tensor);
|
| 80 |
+
void pushTuple(const IValue& ivalue);
|
| 81 |
+
void pushString(const std::string& string);
|
| 82 |
+
void pushDevice(const IValue& ivalue);
|
| 83 |
+
#ifdef USE_DISTRIBUTED
|
| 84 |
+
void pushRRef(const IValue& ivalue);
|
| 85 |
+
#endif
|
| 86 |
+
// unmemoized version
|
| 87 |
+
void pushStringImpl(const std::string& string);
|
| 88 |
+
void pushStorageOfTensor(const at::Tensor& tensor);
|
| 89 |
+
|
| 90 |
+
void pushBinGet(uint32_t memo_id);
|
| 91 |
+
void pushSpecializedList(
|
| 92 |
+
const IValue& ivalue,
|
| 93 |
+
const char* list_name,
|
| 94 |
+
const std::function<void(const IValue&)>& item_pusher);
|
| 95 |
+
void pushGlobal(std::string_view module_name, std::string_view class_name);
|
| 96 |
+
// raw string data is appended directly to the byte stream
|
| 97 |
+
void pushBytes(const std::string& string);
|
| 98 |
+
void pushTensorData(const at::Tensor& tensor);
|
| 99 |
+
|
| 100 |
+
// Add a BINPUT op and return the memoization id used
|
| 101 |
+
size_t pushNextBinPut();
|
| 102 |
+
|
| 103 |
+
const void* getPointer(const IValue& ivalue);
|
| 104 |
+
|
| 105 |
+
// Caller checks that bufferPos_ > 0
|
| 106 |
+
void flushNonEmpty() {
|
| 107 |
+
writer_(buffer_.data(), bufferPos_);
|
| 108 |
+
bufferPos_ = 0;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
void flush() {
|
| 112 |
+
if (bufferPos_ != 0) {
|
| 113 |
+
flushNonEmpty();
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// These convert values to bytes and add them to the stack (NB: since T is to
|
| 118 |
+
// the left of a '::', its type cannot be deduced by the compiler so one must
|
| 119 |
+
// explicitly instantiate the template, i.e. push<int>(int) works, push(int)
|
| 120 |
+
// does not)
|
| 121 |
+
static constexpr size_t kBufferSize = 256;
|
| 122 |
+
template <typename T>
|
| 123 |
+
void push(std::common_type_t<T> value) {
|
| 124 |
+
const char* begin = reinterpret_cast<const char*>(&value);
|
| 125 |
+
if (bufferPos_ + sizeof(T) > buffer_.size()) {
|
| 126 |
+
flushNonEmpty();
|
| 127 |
+
}
|
| 128 |
+
static_assert(sizeof(T) <= kBufferSize, "Buffer size assumption");
|
| 129 |
+
memcpy(buffer_.data() + bufferPos_, begin, sizeof(T));
|
| 130 |
+
bufferPos_ += sizeof(T);
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Stream to write binary data to
|
| 134 |
+
// Code shouldn't call writer_ directly without first flushing.
|
| 135 |
+
std::function<void(const char*, size_t)> writer_;
|
| 136 |
+
|
| 137 |
+
// Buffer to avoid calling a writer_ on a per-byte basis.
|
| 138 |
+
std::array<char, kBufferSize> buffer_;
|
| 139 |
+
size_t bufferPos_{0};
|
| 140 |
+
|
| 141 |
+
// Stack of opcodes/data
|
| 142 |
+
std::vector<char> stack_;
|
| 143 |
+
|
| 144 |
+
// External table of tensors to serialize. If this is missing, then tensors
|
| 145 |
+
// are serialized directly into the pickle
|
| 146 |
+
std::vector<at::Tensor>* tensor_table_;
|
| 147 |
+
|
| 148 |
+
// TODO: only use this if necessary (add a pass to find all shared ivalues,
|
| 149 |
+
// and only memoize those)
|
| 150 |
+
uint32_t memo_id_ = 0;
|
| 151 |
+
|
| 152 |
+
// Memoization of IValues that have been written (index in table is used for
|
| 153 |
+
// BINPUT opcodes) to enable shared references
|
| 154 |
+
c10::FastMap<const void*, uint32_t> memoized_ivalue_map_;
|
| 155 |
+
|
| 156 |
+
// because we de-dup ivalues based on their raw pointer address in the above
|
| 157 |
+
// map we need to keep all the memoized values alive during the pickle.
|
| 158 |
+
// Otherwise, it is possible that a raw address gets reused for another
|
| 159 |
+
// object, and we will alias it to the old object at that address.
|
| 160 |
+
std::vector<IValue> memoized_ivalues_;
|
| 161 |
+
|
| 162 |
+
std::function<c10::QualifiedName(const c10::ClassTypePtr&)> type_renamer_;
|
| 163 |
+
|
| 164 |
+
// List of all the types that it wrote, inspect from the IValues it wrote.
|
| 165 |
+
std::vector<c10::ClassTypePtr>* memoized_class_types_;
|
| 166 |
+
|
| 167 |
+
// Function to grab next id_name for tensor storage, function is responsible
|
| 168 |
+
// for returning unique ids
|
| 169 |
+
std::function<std::string(const at::Tensor&)> get_tensor_id_;
|
| 170 |
+
|
| 171 |
+
// List of tensor storages to serialize in the same binary as the pickle data
|
| 172 |
+
// similar to ivalues, they are memoized using BINPUT
|
| 173 |
+
std::vector<at::Tensor> tensor_data_;
|
| 174 |
+
c10::FastMap<const void*, uint32_t> memoized_storage_map_;
|
| 175 |
+
|
| 176 |
+
c10::FastMap<std::string, uint32_t> memoized_globals_map_;
|
| 177 |
+
c10::FastMap<std::string, uint32_t> memoized_strings_map_;
|
| 178 |
+
c10::FastMap<std::string, uint32_t> memoized_devices_map_;
|
| 179 |
+
// when true, List and Dict objects will be wrapped in a
|
| 180 |
+
// torch.jit._pickle.restore_type_tag call to correctly set the dynamic
|
| 181 |
+
// TorchScript type for the object. When true the thing unpickling must have
|
| 182 |
+
// torch installed.
|
| 183 |
+
bool tag_aggregates_;
|
| 184 |
+
};
|
| 185 |
+
|
| 186 |
+
} // namespace torch::jit
|
| 187 |
+
|
| 188 |
+
#else
|
| 189 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 190 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/pickler_helper.h
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <string>
|
| 5 |
+
|
| 6 |
+
#include <ATen/Utils.h>
|
| 7 |
+
#include <ATen/core/ivalue.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::jit {
|
| 10 |
+
|
| 11 |
+
// See Python's pickletools.py for a detailed description of each of these codes
|
| 12 |
+
enum class PickleOpCode : char {
|
| 13 |
+
MARK = '(',
|
| 14 |
+
STOP = '.',
|
| 15 |
+
POP = '0',
|
| 16 |
+
POP_MARK = '1',
|
| 17 |
+
DUP = '2',
|
| 18 |
+
FLOAT = 'F',
|
| 19 |
+
INT = 'I',
|
| 20 |
+
BININT = 'J',
|
| 21 |
+
BININT1 = 'K',
|
| 22 |
+
LONG = 'L',
|
| 23 |
+
BININT2 = 'M',
|
| 24 |
+
NONE = 'N',
|
| 25 |
+
PERSID = 'P',
|
| 26 |
+
BINPERSID = 'Q',
|
| 27 |
+
REDUCE = 'R',
|
| 28 |
+
STRING = 'S',
|
| 29 |
+
BINSTRING = 'T',
|
| 30 |
+
SHORT_BINSTRING = 'U',
|
| 31 |
+
// NB: Avoid using UNICODE as it is a macro in the Windows API
|
| 32 |
+
UNICODE_ = 'V',
|
| 33 |
+
BINUNICODE = 'X',
|
| 34 |
+
APPEND = 'a',
|
| 35 |
+
BUILD = 'b',
|
| 36 |
+
GLOBAL = 'c',
|
| 37 |
+
DICT = 'd',
|
| 38 |
+
EMPTY_DICT = '}',
|
| 39 |
+
APPENDS = 'e',
|
| 40 |
+
GET = 'g',
|
| 41 |
+
BINGET = 'h',
|
| 42 |
+
INST = 'i',
|
| 43 |
+
LONG_BINGET = 'j',
|
| 44 |
+
LIST = 'l',
|
| 45 |
+
EMPTY_LIST = ']',
|
| 46 |
+
OBJ = 'o',
|
| 47 |
+
PUT = 'p',
|
| 48 |
+
BINPUT = 'q',
|
| 49 |
+
LONG_BINPUT = 'r',
|
| 50 |
+
SETITEM = 's',
|
| 51 |
+
TUPLE = 't',
|
| 52 |
+
EMPTY_TUPLE = ')',
|
| 53 |
+
SETITEMS = 'u',
|
| 54 |
+
BINFLOAT = 'G',
|
| 55 |
+
|
| 56 |
+
// Protocol 2
|
| 57 |
+
// NOLINTNEXTLINE(readability-redundant-inline-specifier)
|
| 58 |
+
PROTO = char('\x80'),
|
| 59 |
+
NEWOBJ = '\x81',
|
| 60 |
+
EXT1 = '\x82',
|
| 61 |
+
EXT2 = '\x83',
|
| 62 |
+
EXT4 = '\x84',
|
| 63 |
+
TUPLE1 = '\x85',
|
| 64 |
+
TUPLE2 = '\x86',
|
| 65 |
+
TUPLE3 = '\x87',
|
| 66 |
+
NEWTRUE = '\x88',
|
| 67 |
+
NEWFALSE = '\x89',
|
| 68 |
+
LONG1 = '\x8a',
|
| 69 |
+
LONG4 = '\x8b',
|
| 70 |
+
|
| 71 |
+
// Protocol 3 (Python 3.x)
|
| 72 |
+
BINBYTES = 'B',
|
| 73 |
+
SHORT_BINBYTES = 'C',
|
| 74 |
+
|
| 75 |
+
// Protocol 4
|
| 76 |
+
// NOLINTNEXTLINE(readability-redundant-inline-specifier)
|
| 77 |
+
SHORT_BINUNICODE = char('\x8c'),
|
| 78 |
+
BINUNICODE8 = '\x8d',
|
| 79 |
+
BINBYTES8 = '\x8e',
|
| 80 |
+
EMPTY_SET = '\x8f',
|
| 81 |
+
ADDITEMS = '\x90',
|
| 82 |
+
FROZENSET = '\x91',
|
| 83 |
+
NEWOBJ_EX = '\x92',
|
| 84 |
+
STACK_GLOBAL = '\x93',
|
| 85 |
+
MEMOIZE = '\x94',
|
| 86 |
+
FRAME = '\x95'
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
struct WriteableTensorData {
|
| 90 |
+
const char* data() const {
|
| 91 |
+
return static_cast<const char*>(tensor_.storage().data());
|
| 92 |
+
}
|
| 93 |
+
size_t sizeInBytes() const {
|
| 94 |
+
return size_;
|
| 95 |
+
}
|
| 96 |
+
size_t nbytes() const {
|
| 97 |
+
return tensor_.storage().nbytes();
|
| 98 |
+
}
|
| 99 |
+
bool storageHasDeleter() const {
|
| 100 |
+
return tensor_.storage().data_ptr().get_context() != nullptr;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
private:
|
| 104 |
+
friend TORCH_API WriteableTensorData
|
| 105 |
+
getWriteableTensorData(const at::Tensor& tensor, bool to_cpu);
|
| 106 |
+
at::Tensor tensor_;
|
| 107 |
+
uint64_t size_;
|
| 108 |
+
};
|
| 109 |
+
|
| 110 |
+
// returns a (tensor, record_size) for a tensor, converting it to a CPU tensor
|
| 111 |
+
// if it was CUDA and to_cpu is True.
|
| 112 |
+
TORCH_API WriteableTensorData
|
| 113 |
+
getWriteableTensorData(const at::Tensor& tensor, bool to_cpu = true);
|
| 114 |
+
|
| 115 |
+
// if the cls has __getstate__/__setstate__
|
| 116 |
+
// assert they have the right schema and return true,
|
| 117 |
+
// otherwise return false
|
| 118 |
+
bool checkHasValidSetGetState(const c10::ClassType& cls);
|
| 119 |
+
|
| 120 |
+
// Declare BackendMeta serialization and deserialization function pointer types.
|
| 121 |
+
using BackendMetaPtr = std::function<
|
| 122 |
+
void(const at::Tensor&, std::unordered_map<std::string, bool>&)>;
|
| 123 |
+
|
| 124 |
+
// A allowlist of device type, currently available is PrivateUse1
|
| 125 |
+
TORCH_API std::unordered_set<c10::DeviceType>& GetBackendMetaAllowlist();
|
| 126 |
+
|
| 127 |
+
// Dynamically obtain serialization function pairs
|
| 128 |
+
// that require the corresponding backend.
|
| 129 |
+
TORCH_API std::array<
|
| 130 |
+
std::optional<std::pair<BackendMetaPtr, BackendMetaPtr>>,
|
| 131 |
+
at::COMPILE_TIME_MAX_DEVICE_TYPES>&
|
| 132 |
+
GetBackendMetaSerialization();
|
| 133 |
+
|
| 134 |
+
// Return a map of Tensor Metadata which including BackendMetaData for
|
| 135 |
+
// serialization. For now, it only takes care of `conj` and `neg` bit.
|
| 136 |
+
inline std::unordered_map<std::string, bool> getTensorMetadata(
|
| 137 |
+
const at::Tensor& t) {
|
| 138 |
+
// We don't support serializing `ZeroTensor` as it is not public
|
| 139 |
+
// facing yet.
|
| 140 |
+
TORCH_CHECK(
|
| 141 |
+
!t._is_zerotensor(),
|
| 142 |
+
"ZeroTensor is not serializable,",
|
| 143 |
+
" please file an issue if required.");
|
| 144 |
+
std::unordered_map<std::string, bool> metadata{};
|
| 145 |
+
|
| 146 |
+
// Only add meta-data if the value is not default.
|
| 147 |
+
if (t.is_conj()) {
|
| 148 |
+
metadata["conj"] = true;
|
| 149 |
+
}
|
| 150 |
+
if (t.is_neg()) {
|
| 151 |
+
metadata["neg"] = true;
|
| 152 |
+
}
|
| 153 |
+
// Only add BackendMetaData for custom backend if the function pointer is
|
| 154 |
+
// registered.
|
| 155 |
+
int device_type = static_cast<int>(t.device().type());
|
| 156 |
+
const auto& BackendMetaSerialization = GetBackendMetaSerialization();
|
| 157 |
+
if (BackendMetaSerialization[device_type].has_value()) {
|
| 158 |
+
// Pass the tensor and metadata map references as parameters to the custom
|
| 159 |
+
// serialization function.
|
| 160 |
+
BackendMetaPtr fptr = BackendMetaSerialization[device_type].value().first;
|
| 161 |
+
fptr(t, metadata);
|
| 162 |
+
}
|
| 163 |
+
return metadata;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
// set Tensor Metadata based on the map.
|
| 167 |
+
// Refer: getTensorMetadata
|
| 168 |
+
inline void setTensorMetadata(
|
| 169 |
+
const at::Tensor& t,
|
| 170 |
+
std::unordered_map<std::string, bool> metadata) {
|
| 171 |
+
auto iter_end = metadata.end();
|
| 172 |
+
auto iter_temp = metadata.find("conj");
|
| 173 |
+
if (iter_temp != iter_end) {
|
| 174 |
+
t._set_conj(true);
|
| 175 |
+
metadata.erase(iter_temp);
|
| 176 |
+
}
|
| 177 |
+
iter_temp = metadata.find("neg");
|
| 178 |
+
if (iter_temp != iter_end) {
|
| 179 |
+
t._set_neg(true);
|
| 180 |
+
metadata.erase(iter_temp);
|
| 181 |
+
}
|
| 182 |
+
// Only set BackendMetaData for custom backend if the function pointer is
|
| 183 |
+
// registered.
|
| 184 |
+
int device_type = static_cast<int>(t.device().type());
|
| 185 |
+
const auto& BackendMetaSerialization = GetBackendMetaSerialization();
|
| 186 |
+
if (BackendMetaSerialization[device_type].has_value()) {
|
| 187 |
+
// Pass the tensor and metadata map references as parameters to the custom
|
| 188 |
+
// deserialization function.
|
| 189 |
+
BackendMetaPtr fptr = BackendMetaSerialization[device_type].value().second;
|
| 190 |
+
fptr(t, metadata);
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// set Tensor metadata based on the map.
|
| 195 |
+
// NOTE: This overload is required by unpickler.cpp
|
| 196 |
+
inline void setTensorMetadata(
|
| 197 |
+
const at::Tensor& t,
|
| 198 |
+
const c10::Dict<c10::IValue, c10::IValue>& metadata_idict) {
|
| 199 |
+
std::unordered_map<std::string, bool> metadata;
|
| 200 |
+
for (auto& pair : metadata_idict) {
|
| 201 |
+
auto key = *pair.key().toString();
|
| 202 |
+
metadata[key] = pair.value().toBool();
|
| 203 |
+
}
|
| 204 |
+
setTensorMetadata(t, std::move(metadata));
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// Register function pointer of Tensor BackendMetadata for serialization.
|
| 208 |
+
inline void TensorBackendMetaRegistry(
|
| 209 |
+
c10::DeviceType t,
|
| 210 |
+
const BackendMetaPtr& get_fptr,
|
| 211 |
+
const BackendMetaPtr& set_fptr) {
|
| 212 |
+
// allowlist verification
|
| 213 |
+
// Only if the devicetype is in the allowlist,
|
| 214 |
+
// we allow the serialization extension to be registered for backendmeta data.
|
| 215 |
+
const auto& DeviceTypeAllowlist = GetBackendMetaAllowlist();
|
| 216 |
+
TORCH_CHECK(
|
| 217 |
+
DeviceTypeAllowlist.find(t) != DeviceTypeAllowlist.end(),
|
| 218 |
+
"It is not allowed to register the serialization method ",
|
| 219 |
+
"of backendMeta data for PrivateUse1. ",
|
| 220 |
+
"If you have related serialization requirements, ",
|
| 221 |
+
"please expand the allowlist");
|
| 222 |
+
// Register function pointer
|
| 223 |
+
int device_type = static_cast<int>(t);
|
| 224 |
+
auto& BackendMetaSerialization = GetBackendMetaSerialization();
|
| 225 |
+
TORCH_CHECK(
|
| 226 |
+
!BackendMetaSerialization[device_type].has_value(),
|
| 227 |
+
"The tensor BackendMeta serialization function pointer for ",
|
| 228 |
+
t,
|
| 229 |
+
" has been registered.");
|
| 230 |
+
BackendMetaSerialization[device_type] =
|
| 231 |
+
std::optional<std::pair<BackendMetaPtr, BackendMetaPtr>>(
|
| 232 |
+
std::make_pair(get_fptr, set_fptr));
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
} // namespace torch::jit
|
| 236 |
+
|
| 237 |
+
#else
|
| 238 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 239 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/python_print.h
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <torch/csrc/jit/api/module.h>
|
| 5 |
+
#include <torch/csrc/jit/ir/ir.h>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
namespace torch::jit {
|
| 9 |
+
|
| 10 |
+
struct Method;
|
| 11 |
+
struct Module;
|
| 12 |
+
struct PythonPrintImpl;
|
| 13 |
+
|
| 14 |
+
struct PrintDepsTable {
|
| 15 |
+
void add(const c10::NamedTypePtr& type);
|
| 16 |
+
|
| 17 |
+
size_t size() const {
|
| 18 |
+
return table_.size();
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const c10::NamedTypePtr& operator[](size_t index) const {
|
| 22 |
+
return table_[index];
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
private:
|
| 26 |
+
std::vector<c10::NamedTypePtr> table_;
|
| 27 |
+
std::unordered_set<c10::NamedTypePtr> non_unique_;
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
struct TORCH_API PythonPrint {
|
| 31 |
+
PythonPrint(
|
| 32 |
+
std::vector<IValue>& constant_table,
|
| 33 |
+
PrintDepsTable& deps_table,
|
| 34 |
+
c10::TypePrinter type_printer = nullptr,
|
| 35 |
+
bool enforce_importable = false);
|
| 36 |
+
|
| 37 |
+
void printNamedType(const c10::NamedTypePtr& classType);
|
| 38 |
+
void printFunction(const Function& callee);
|
| 39 |
+
void printMethod(const Function& callee);
|
| 40 |
+
|
| 41 |
+
std::string str() const;
|
| 42 |
+
const SourceRangeRecords& ranges() const;
|
| 43 |
+
uint64_t minVersion() const;
|
| 44 |
+
|
| 45 |
+
private:
|
| 46 |
+
std::shared_ptr<PythonPrintImpl> pImpl;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
TORCH_API bool printerHasSpecialCaseFor(c10::Symbol sym);
|
| 50 |
+
|
| 51 |
+
TORCH_API void jitModuleToPythonCodeAndConstants(
|
| 52 |
+
const Module& module,
|
| 53 |
+
ExtraFilesMap* jit_sources, // output
|
| 54 |
+
std::vector<IValue>* constants // output
|
| 55 |
+
);
|
| 56 |
+
|
| 57 |
+
} // namespace torch::jit
|
| 58 |
+
|
| 59 |
+
#else
|
| 60 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 61 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/source_range_serialization.h
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/Allocator.h>
|
| 5 |
+
#include <torch/csrc/jit/frontend/source_range.h>
|
| 6 |
+
|
| 7 |
+
#include <ATen/core/ivalue.h>
|
| 8 |
+
|
| 9 |
+
#include <unordered_map>
|
| 10 |
+
#include <vector>
|
| 11 |
+
|
| 12 |
+
namespace c10 {
|
| 13 |
+
struct IValue;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
namespace torch::jit {
|
| 17 |
+
|
| 18 |
+
class Pickler;
|
| 19 |
+
class SourceRangeSerializer;
|
| 20 |
+
static constexpr size_t kByteOffsetIndex = 0;
|
| 21 |
+
static constexpr size_t kSourceRangeIndex = 1;
|
| 22 |
+
static constexpr size_t kSourceRangeTagIndex = 2;
|
| 23 |
+
constexpr std::string_view kFormatWithStringTable = "FORMAT_WITH_STRING_TABLE";
|
| 24 |
+
|
| 25 |
+
class SourceRangePickler {
|
| 26 |
+
public:
|
| 27 |
+
SourceRangePickler();
|
| 28 |
+
|
| 29 |
+
std::vector<char> pickle(
|
| 30 |
+
const SourceRangeRecords& ranges,
|
| 31 |
+
const SourceRangeTagMap& source_range_tags);
|
| 32 |
+
|
| 33 |
+
private:
|
| 34 |
+
std::shared_ptr<SourceRangeSerializer> srs;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
class SourceRangeDeserializer {
|
| 38 |
+
public:
|
| 39 |
+
SourceRangeDeserializer() = default;
|
| 40 |
+
explicit SourceRangeDeserializer(const c10::IValue& text_table) {
|
| 41 |
+
for (const auto& x : text_table.toTuple()->elements()) {
|
| 42 |
+
text_table_.emplace_back(std::make_shared<std::string>(x.toStringRef()));
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
SourceRange deserialize(const c10::IValue& iv);
|
| 46 |
+
|
| 47 |
+
private:
|
| 48 |
+
std::shared_ptr<Source> deserialize_source(const c10::IValue& iv);
|
| 49 |
+
std::unordered_map<
|
| 50 |
+
c10::intrusive_ptr<c10::ivalue::Tuple>,
|
| 51 |
+
std::shared_ptr<Source>>
|
| 52 |
+
cached_sources;
|
| 53 |
+
std::vector<std::shared_ptr<std::string>> text_table_;
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
class SourceRangeUnpickler {
|
| 57 |
+
public:
|
| 58 |
+
virtual std::optional<SourceRange> findSourceRangeThatGenerated(
|
| 59 |
+
const SourceRange& range) = 0;
|
| 60 |
+
|
| 61 |
+
virtual ~SourceRangeUnpickler() = default;
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
TORCH_API void setShouldUseFormatWithStringTable(
|
| 65 |
+
bool should_use_format_with_string_table);
|
| 66 |
+
|
| 67 |
+
} // namespace torch::jit
|
| 68 |
+
|
| 69 |
+
#else
|
| 70 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 71 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/source_range_serialization_impl.h
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/serialization/source_range_serialization.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::jit {
|
| 7 |
+
|
| 8 |
+
// Do this clownyness with virtual functions because of the split
|
| 9 |
+
// between ATen core and torch
|
| 10 |
+
|
| 11 |
+
class ConcreteSourceRangeUnpickler : public SourceRangeUnpickler {
|
| 12 |
+
public:
|
| 13 |
+
ConcreteSourceRangeUnpickler(at::DataPtr&& data, size_t size);
|
| 14 |
+
|
| 15 |
+
std::optional<SourceRange> findSourceRangeThatGenerated(
|
| 16 |
+
const SourceRange& range) override;
|
| 17 |
+
|
| 18 |
+
private:
|
| 19 |
+
at::DataPtr data;
|
| 20 |
+
size_t size;
|
| 21 |
+
|
| 22 |
+
void unpickle();
|
| 23 |
+
|
| 24 |
+
std::mutex mutex;
|
| 25 |
+
std::shared_ptr<SourceRangeDeserializer> deserializer;
|
| 26 |
+
std::shared_ptr<SourceRangeRecords> unpickled_records;
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
} // namespace torch::jit
|
| 30 |
+
|
| 31 |
+
#else
|
| 32 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 33 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/storage_context.h
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::jit {
|
| 7 |
+
|
| 8 |
+
// Used in torch.package and TorchScript serialization to coordinate
|
| 9 |
+
// sharing of storages between models. Also used to create deterministic
|
| 10 |
+
// naming for storages.
|
| 11 |
+
class TORCH_API SerializationStorageContext {
|
| 12 |
+
public:
|
| 13 |
+
explicit SerializationStorageContext() = default;
|
| 14 |
+
SerializationStorageContext operator=(const SerializationStorageContext&) =
|
| 15 |
+
delete;
|
| 16 |
+
SerializationStorageContext(const SerializationStorageContext&) = delete;
|
| 17 |
+
|
| 18 |
+
uint64_t getOrAddStorage(const c10::Storage& storage) {
|
| 19 |
+
if (!hasStorage(storage)) {
|
| 20 |
+
uint64_t size = storage_id_map_.size();
|
| 21 |
+
storage_id_map_[storage] = size;
|
| 22 |
+
}
|
| 23 |
+
return storage_id_map_[storage];
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
bool hasStorage(const c10::Storage& storage) {
|
| 27 |
+
return storage_id_map_.find(storage) != storage_id_map_.end();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
~SerializationStorageContext() = default;
|
| 31 |
+
|
| 32 |
+
private:
|
| 33 |
+
class StorageSerializationHash {
|
| 34 |
+
public:
|
| 35 |
+
size_t operator()(const c10::Storage& storage) const {
|
| 36 |
+
return std::hash<void*>()(
|
| 37 |
+
reinterpret_cast<void*>(storage.unsafeGetStorageImpl()));
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
class StorageSerializationEqual {
|
| 42 |
+
public:
|
| 43 |
+
bool operator()(const c10::Storage& lhs, const c10::Storage& rhs) const {
|
| 44 |
+
return lhs.unsafeGetStorageImpl() == rhs.unsafeGetStorageImpl();
|
| 45 |
+
}
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
std::unordered_map<
|
| 49 |
+
c10::Storage,
|
| 50 |
+
uint64_t,
|
| 51 |
+
StorageSerializationHash,
|
| 52 |
+
StorageSerializationEqual>
|
| 53 |
+
storage_id_map_;
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
// Used in torch.package and TorchScript deserialization to coordinate
|
| 57 |
+
// sharing of storages between models.
|
| 58 |
+
class TORCH_API DeserializationStorageContext {
|
| 59 |
+
public:
|
| 60 |
+
explicit DeserializationStorageContext() = default;
|
| 61 |
+
DeserializationStorageContext operator=(
|
| 62 |
+
const DeserializationStorageContext&) = delete;
|
| 63 |
+
DeserializationStorageContext(const DeserializationStorageContext&) = delete;
|
| 64 |
+
|
| 65 |
+
void addStorage(std::string name, c10::Storage storage) {
|
| 66 |
+
TORCH_INTERNAL_ASSERT(!hasStorage(name));
|
| 67 |
+
name_storage_map_.emplace(std::move(name), std::move(storage));
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
bool hasStorage(const std::string& name) {
|
| 71 |
+
return name_storage_map_.find(name) != name_storage_map_.end();
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
c10::Storage getStorage(const std::string& name) {
|
| 75 |
+
TORCH_INTERNAL_ASSERT(hasStorage(name));
|
| 76 |
+
return name_storage_map_.find(name)->second;
|
| 77 |
+
}
|
| 78 |
+
~DeserializationStorageContext() = default;
|
| 79 |
+
|
| 80 |
+
private:
|
| 81 |
+
std::unordered_map<std::string, c10::Storage> name_storage_map_;
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
} // namespace torch::jit
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/type_name_uniquer.h
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/frontend/name_mangler.h>
|
| 5 |
+
#include <torch/csrc/jit/ir/type_hashing.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::jit {
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* class TypeNameUniquer
|
| 11 |
+
*
|
| 12 |
+
* Generates a unique name for every type `t` passed in. Types that compare
|
| 13 |
+
* equal with EqualType will receive the same unique name.
|
| 14 |
+
*
|
| 15 |
+
* This is used during Module::save(), to resolve type name collisions during
|
| 16 |
+
* serialization.
|
| 17 |
+
*/
|
| 18 |
+
class TORCH_API TypeNameUniquer {
|
| 19 |
+
public:
|
| 20 |
+
c10::QualifiedName getUniqueName(c10::ConstNamedTypePtr t);
|
| 21 |
+
|
| 22 |
+
private:
|
| 23 |
+
NameMangler mangler_;
|
| 24 |
+
std::unordered_set<c10::QualifiedName> used_names_;
|
| 25 |
+
std::unordered_map<
|
| 26 |
+
c10::ConstNamedTypePtr,
|
| 27 |
+
c10::QualifiedName,
|
| 28 |
+
HashType,
|
| 29 |
+
EqualType>
|
| 30 |
+
name_map_;
|
| 31 |
+
};
|
| 32 |
+
} // namespace torch::jit
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/serialization/unpickler.h
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
#include <c10/util/ArrayRef.h>
|
| 6 |
+
#include <caffe2/serialize/inline_container.h>
|
| 7 |
+
|
| 8 |
+
#include <torch/csrc/Export.h>
|
| 9 |
+
#include <torch/csrc/jit/frontend/script_type_parser.h>
|
| 10 |
+
#include <torch/csrc/jit/serialization/pickler_helper.h>
|
| 11 |
+
|
| 12 |
+
namespace torch::jit {
|
| 13 |
+
|
| 14 |
+
using TypeResolver =
|
| 15 |
+
std::function<c10::StrongTypePtr(const c10::QualifiedName&)>;
|
| 16 |
+
|
| 17 |
+
using ObjLoader = std::function<
|
| 18 |
+
c10::intrusive_ptr<c10::ivalue::Object>(const at::StrongTypePtr&, IValue)>;
|
| 19 |
+
|
| 20 |
+
class DeserializationStorageContext;
|
| 21 |
+
|
| 22 |
+
// [unpickler refactor] there is some cruft around PickleOpCode::BUILD,
|
| 23 |
+
// PickleOpCode::NEWOBJ, and the last_opcode_ member below that should be
|
| 24 |
+
// deleted at some point, the Pickler doesn't produce it and it's only around to
|
| 25 |
+
// support models saved before 1.1
|
| 26 |
+
class TORCH_API Unpickler {
|
| 27 |
+
AT_DISALLOW_COPY_AND_ASSIGN(Unpickler);
|
| 28 |
+
|
| 29 |
+
using TypeParserT = c10::TypePtr (*)(const std::string&);
|
| 30 |
+
|
| 31 |
+
public:
|
| 32 |
+
// tensors inside the pickle are references to the tensor_table.
|
| 33 |
+
// class_resolver is to resolve strong class type, type_resolver_ is
|
| 34 |
+
// to resolve any JIT type. class_resolver and type_resolver are not merged
|
| 35 |
+
// here because some use cases need to get strong class type that
|
| 36 |
+
// type_resolver_ can not return.
|
| 37 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 38 |
+
Unpickler(
|
| 39 |
+
std::function<size_t(char*, size_t)> reader,
|
| 40 |
+
TypeResolver type_resolver,
|
| 41 |
+
c10::ArrayRef<at::Tensor> tensor_table,
|
| 42 |
+
TypeParserT type_parser = defaultTypeParser)
|
| 43 |
+
: reader_(std::move(reader)),
|
| 44 |
+
tensor_table_(tensor_table),
|
| 45 |
+
type_resolver_(std::move(type_resolver)),
|
| 46 |
+
use_storage_device_(false),
|
| 47 |
+
type_parser_(type_parser),
|
| 48 |
+
version_(caffe2::serialize::kProducedFileFormatVersion) {}
|
| 49 |
+
|
| 50 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 51 |
+
Unpickler(
|
| 52 |
+
std::function<size_t(char*, size_t)> reader,
|
| 53 |
+
TypeResolver type_resolver,
|
| 54 |
+
c10::ArrayRef<at::Tensor> tensor_table,
|
| 55 |
+
ObjLoader obj_loader,
|
| 56 |
+
TypeParserT type_parser = defaultTypeParser)
|
| 57 |
+
: reader_(std::move(reader)),
|
| 58 |
+
tensor_table_(tensor_table),
|
| 59 |
+
type_resolver_(std::move(type_resolver)),
|
| 60 |
+
obj_loader_(std::move(obj_loader)),
|
| 61 |
+
use_storage_device_(false),
|
| 62 |
+
type_parser_(type_parser),
|
| 63 |
+
version_(caffe2::serialize::kProducedFileFormatVersion) {}
|
| 64 |
+
|
| 65 |
+
// tensors inside the pickle contain meta-data, the raw tensor
|
| 66 |
+
// dead is retrieved by calling `read_record`.
|
| 67 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 68 |
+
Unpickler(
|
| 69 |
+
std::function<size_t(char*, size_t)> reader,
|
| 70 |
+
TypeResolver type_resolver,
|
| 71 |
+
ObjLoader obj_loader,
|
| 72 |
+
std::function<at::DataPtr(const std::string&)> read_record,
|
| 73 |
+
std::optional<at::Device> device,
|
| 74 |
+
bool use_storage_device = false,
|
| 75 |
+
TypeParserT type_parser = defaultTypeParser,
|
| 76 |
+
std::shared_ptr<DeserializationStorageContext> storage_context = nullptr)
|
| 77 |
+
: reader_(std::move(reader)),
|
| 78 |
+
type_resolver_(std::move(type_resolver)),
|
| 79 |
+
obj_loader_(std::move(obj_loader)),
|
| 80 |
+
read_record_(std::move(read_record)),
|
| 81 |
+
device_(device),
|
| 82 |
+
use_storage_device_(use_storage_device),
|
| 83 |
+
type_parser_(type_parser),
|
| 84 |
+
storage_context_(std::move(storage_context)),
|
| 85 |
+
version_(caffe2::serialize::kProducedFileFormatVersion) {}
|
| 86 |
+
|
| 87 |
+
Unpickler(Unpickler&&) = delete;
|
| 88 |
+
Unpickler& operator=(Unpickler&&) = delete;
|
| 89 |
+
~Unpickler() = default;
|
| 90 |
+
|
| 91 |
+
// consume the pickle stream, producing an IValue from the contents.
|
| 92 |
+
// Type Tags: the pickler will restore the type tags on
|
| 93 |
+
// List and Dict objects when possible IValue is an Object.
|
| 94 |
+
// Otherwise, Dict and List objects will end up with Any as their tag.
|
| 95 |
+
// If you know the type of the ivalue, tags can be restored with
|
| 96 |
+
// restoreAccurateTypeTags
|
| 97 |
+
IValue parse_ivalue();
|
| 98 |
+
|
| 99 |
+
// [type tag serialization]
|
| 100 |
+
// This is used to determine whether to restore type tags be recursively
|
| 101 |
+
// descending into the returned stack object (if version_number <= 2), or
|
| 102 |
+
// if version_number >= 3, to use the type strings included in the pickle
|
| 103 |
+
// archive for container types. By default this is set to
|
| 104 |
+
// `kProducedFileFormatVersion` so unless you're loading a pickle file
|
| 105 |
+
// from alongside a corresponding `version` file, you don't need to set
|
| 106 |
+
// the version manually.
|
| 107 |
+
void set_version(uint64_t version_number) {
|
| 108 |
+
version_ = version_number;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
static c10::TypePtr defaultTypeParser(const std::string& str) {
|
| 112 |
+
ScriptTypeParser parser;
|
| 113 |
+
return parser.parseType(str);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
private:
|
| 117 |
+
// No arguments ensures that a template argument must be specified
|
| 118 |
+
// so that the number of bytes read / type read is explicit
|
| 119 |
+
template <typename T>
|
| 120 |
+
T read() {
|
| 121 |
+
T item;
|
| 122 |
+
if (sizeof(T) <= buffer_remaining_) {
|
| 123 |
+
// Fast path: entirely from buffer.
|
| 124 |
+
memcpy(&item, buffer_.data() + buffer_pos_, sizeof(T));
|
| 125 |
+
buffer_remaining_ -= sizeof(T);
|
| 126 |
+
buffer_pos_ += sizeof(T);
|
| 127 |
+
} else {
|
| 128 |
+
// Don't over-template the slow path, to avoid code size bloat.
|
| 129 |
+
readSlowWithBuffer(reinterpret_cast<char*>(&item), sizeof(T));
|
| 130 |
+
}
|
| 131 |
+
return item;
|
| 132 |
+
}
|
| 133 |
+
void readSlowWithBuffer(char* dest, size_t sz);
|
| 134 |
+
std::string readBytes(size_t num_bytes);
|
| 135 |
+
|
| 136 |
+
double readFloat();
|
| 137 |
+
void readGlobal(
|
| 138 |
+
const std::string& module_name,
|
| 139 |
+
const std::string& class_name);
|
| 140 |
+
void rebuildTensor(bool quantized);
|
| 141 |
+
void rebuildParameter();
|
| 142 |
+
void rebuildTensorFromTypeV2();
|
| 143 |
+
void rebuildSparseTensor();
|
| 144 |
+
#ifdef USE_DISTRIBUTED
|
| 145 |
+
void rebuildRRef();
|
| 146 |
+
#endif
|
| 147 |
+
PickleOpCode readInstruction();
|
| 148 |
+
PickleOpCode readOpCode() {
|
| 149 |
+
return static_cast<PickleOpCode>(read<uint8_t>());
|
| 150 |
+
}
|
| 151 |
+
std::string readString();
|
| 152 |
+
void readList(IValue list_ivalue);
|
| 153 |
+
void readListElements(IValue list_ivalue, size_t start);
|
| 154 |
+
void setInput(size_t memo_id);
|
| 155 |
+
void run();
|
| 156 |
+
|
| 157 |
+
// Returns the number of bytes read. This should statefully
|
| 158 |
+
// remember the position. Don't call reader_ directly.
|
| 159 |
+
std::function<size_t(char*, size_t)> reader_;
|
| 160 |
+
// Small buffer to avoid calling reader_ on a per-byte basis.
|
| 161 |
+
std::array<char, 256> buffer_;
|
| 162 |
+
size_t buffer_pos_{0};
|
| 163 |
+
size_t buffer_remaining_{0};
|
| 164 |
+
|
| 165 |
+
std::vector<IValue> stack_;
|
| 166 |
+
|
| 167 |
+
// globals are represented on the stack as IValue integer indices
|
| 168 |
+
// into this list
|
| 169 |
+
std::vector<std::function<void(void)>> globals_;
|
| 170 |
+
std::vector<IValue> memo_table_;
|
| 171 |
+
std::vector<size_t> marks_;
|
| 172 |
+
c10::ArrayRef<at::Tensor> tensor_table_;
|
| 173 |
+
|
| 174 |
+
// When deserializing types on lists and dicts, cache the type here
|
| 175 |
+
// so we don't have to parse the same type multiple times. Strings
|
| 176 |
+
// are already de-duplicated and replaced with BINGETs in the
|
| 177 |
+
// pickler, so we can just use the actual data pointer of each string.
|
| 178 |
+
std::unordered_map<std::string, c10::TypePtr> type_cache_;
|
| 179 |
+
|
| 180 |
+
// optionally nullptr, needs to be present for creating classes
|
| 181 |
+
TypeResolver type_resolver_;
|
| 182 |
+
ObjLoader obj_loader_;
|
| 183 |
+
IValue empty_tuple_;
|
| 184 |
+
|
| 185 |
+
std::function<at::DataPtr(const std::string&)> read_record_;
|
| 186 |
+
std::optional<at::Device> device_;
|
| 187 |
+
// When set to true, Unpickler will ignore the pickled device and use the
|
| 188 |
+
// device of the DataPtr returned by the read_record_ function. The default
|
| 189 |
+
// value of this flag is false.
|
| 190 |
+
const bool use_storage_device_;
|
| 191 |
+
|
| 192 |
+
TypeParserT type_parser_{defaultTypeParser};
|
| 193 |
+
|
| 194 |
+
// Used for torch.package to enable sharing of storages across
|
| 195 |
+
// ScriptModules and eager modules
|
| 196 |
+
std::shared_ptr<DeserializationStorageContext> storage_context_;
|
| 197 |
+
|
| 198 |
+
// See [type tag serialization]
|
| 199 |
+
uint64_t version_;
|
| 200 |
+
|
| 201 |
+
// See [NOTE] skip_next_read_global
|
| 202 |
+
uint8_t skip_next_read_global = 0;
|
| 203 |
+
};
|
| 204 |
+
|
| 205 |
+
void restoreAccurateTypeTags(const IValue& root, const c10::TypePtr& type_tag);
|
| 206 |
+
|
| 207 |
+
} // namespace torch::jit
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/analysis.h
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 6 |
+
#include <torch/csrc/jit/tensorexpr/stmt.h>
|
| 7 |
+
#include <torch/csrc/jit/tensorexpr/tensor.h>
|
| 8 |
+
|
| 9 |
+
#include <utility>
|
| 10 |
+
|
| 11 |
+
namespace torch::jit::tensorexpr {
|
| 12 |
+
class HasRand : public IRVisitor {
|
| 13 |
+
public:
|
| 14 |
+
HasRand(StmtPtr stmt) : stmt_(std::move(stmt)) {
|
| 15 |
+
stmt_->accept(this);
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
bool has_rand() const {
|
| 19 |
+
return has_rand_;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
private:
|
| 23 |
+
void visit(const IntrinsicsPtr& v) override {
|
| 24 |
+
if (v->op_type() == IntrinsicsOp::kRand) {
|
| 25 |
+
has_rand_ = true;
|
| 26 |
+
} else {
|
| 27 |
+
IRVisitor::visit(v);
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
StmtPtr stmt_;
|
| 31 |
+
bool has_rand_ = false;
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
template <typename Op>
|
| 35 |
+
class NodeFinder : public IRVisitor {
|
| 36 |
+
public:
|
| 37 |
+
void visit(const NodePtr<Op>& v) override {
|
| 38 |
+
nodes.push_back((NodePtr<Op>)v);
|
| 39 |
+
IRVisitor::visit(v);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
static std::vector<NodePtr<Op>> find(const StmtPtr& s) {
|
| 43 |
+
NodeFinder<Op> nf;
|
| 44 |
+
s->accept(&nf);
|
| 45 |
+
return nf.nodes;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
static std::vector<NodePtr<Op>> find(const ExprPtr& e) {
|
| 49 |
+
NodeFinder<Op> nf;
|
| 50 |
+
e->accept(&nf);
|
| 51 |
+
return nf.nodes;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
std::vector<NodePtr<Op>> nodes;
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
class VarFinder : public IRVisitor {
|
| 58 |
+
public:
|
| 59 |
+
void visit(const VarPtr& v) override {
|
| 60 |
+
vars_.insert(v);
|
| 61 |
+
IRVisitor::visit(v);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
static std::unordered_set<VarPtr> find(const StmtPtr& s) {
|
| 65 |
+
VarFinder nf;
|
| 66 |
+
s->accept(&nf);
|
| 67 |
+
return nf.vars();
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
static std::unordered_set<VarPtr> find(const ExprPtr& e) {
|
| 71 |
+
VarFinder nf;
|
| 72 |
+
e->accept(&nf);
|
| 73 |
+
return nf.vars();
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
const std::unordered_set<VarPtr>& vars() {
|
| 77 |
+
return vars_;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
private:
|
| 81 |
+
std::unordered_set<VarPtr> vars_;
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
class BufFinder : public IRVisitor {
|
| 85 |
+
public:
|
| 86 |
+
void visit(const BufPtr& v) override {
|
| 87 |
+
bufs_.insert(v);
|
| 88 |
+
IRVisitor::visit(v);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
static std::unordered_set<BufPtr> find(const StmtPtr& s) {
|
| 92 |
+
BufFinder nf;
|
| 93 |
+
s->accept(&nf);
|
| 94 |
+
return nf.bufs();
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
static std::unordered_set<BufPtr> find(const ExprPtr& e) {
|
| 98 |
+
BufFinder nf;
|
| 99 |
+
e->accept(&nf);
|
| 100 |
+
return nf.bufs();
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
const std::unordered_set<BufPtr>& bufs() {
|
| 104 |
+
return bufs_;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
private:
|
| 108 |
+
std::unordered_set<BufPtr> bufs_;
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
// Finds all kinds of write operations to the provided Buf.
|
| 112 |
+
class WritesToBuf : public IRVisitor {
|
| 113 |
+
public:
|
| 114 |
+
WritesToBuf(BufPtr target) : target_(std::move(target)) {}
|
| 115 |
+
|
| 116 |
+
std::vector<StmtPtr> writes() {
|
| 117 |
+
return writes_;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
static std::vector<StmtPtr> find(const StmtPtr& s, BufPtr b) {
|
| 121 |
+
WritesToBuf finder(std::move(b));
|
| 122 |
+
s->accept(&finder);
|
| 123 |
+
return finder.writes();
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
private:
|
| 127 |
+
void visit(const StorePtr& v) override {
|
| 128 |
+
if (v->buf() == target_) {
|
| 129 |
+
writes_.push_back(v);
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
void visit(const AtomicAddPtr& v) override {
|
| 134 |
+
if (v->buf() == target_) {
|
| 135 |
+
writes_.push_back(v);
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
BufPtr target_;
|
| 140 |
+
std::vector<StmtPtr> writes_;
|
| 141 |
+
};
|
| 142 |
+
|
| 143 |
+
class StmtsReadingBuf : public IRVisitor {
|
| 144 |
+
public:
|
| 145 |
+
StmtsReadingBuf(BufPtr target) : target_(std::move(target)) {}
|
| 146 |
+
|
| 147 |
+
std::vector<StmtPtr> reads() {
|
| 148 |
+
return reads_;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
static std::vector<StmtPtr> find(const StmtPtr& s, BufPtr b) {
|
| 152 |
+
StmtsReadingBuf finder(std::move(b));
|
| 153 |
+
s->accept(&finder);
|
| 154 |
+
return finder.reads();
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
private:
|
| 158 |
+
bool readsBuffer(const StmtPtr& s) {
|
| 159 |
+
auto loads = NodeFinder<Load>::find(s);
|
| 160 |
+
for (const auto& l : loads) {
|
| 161 |
+
if (l->buf() == target_) {
|
| 162 |
+
return true;
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
return false;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
void visit(const StorePtr& v) override {
|
| 169 |
+
if (readsBuffer(v)) {
|
| 170 |
+
reads_.push_back(v);
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
void visit(const LetPtr& v) override {
|
| 175 |
+
if (readsBuffer(v)) {
|
| 176 |
+
reads_.push_back(v);
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
void visit(const CondPtr& v) override {
|
| 181 |
+
if (readsBuffer(v)) {
|
| 182 |
+
reads_.push_back(v);
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
void visit(const AtomicAddPtr& v) override {
|
| 187 |
+
if (readsBuffer(v)) {
|
| 188 |
+
reads_.push_back(v);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
BufPtr target_;
|
| 193 |
+
std::vector<StmtPtr> reads_;
|
| 194 |
+
};
|
| 195 |
+
|
| 196 |
+
class ExternalAllocBufFinder : public IRVisitor {
|
| 197 |
+
public:
|
| 198 |
+
void visit(const ExternalCallWithAllocPtr& v) override {
|
| 199 |
+
const auto& bufs_out = v->buf_out_args();
|
| 200 |
+
bufs_.insert(bufs_out.begin(), bufs_out.end());
|
| 201 |
+
IRVisitor::visit(v);
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
static std::unordered_set<BufPtr> find(const StmtPtr& s) {
|
| 205 |
+
ExternalAllocBufFinder f;
|
| 206 |
+
s->accept(&f);
|
| 207 |
+
return f.bufs();
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static std::unordered_set<BufPtr> find(const ExprPtr& e) {
|
| 211 |
+
ExternalAllocBufFinder f;
|
| 212 |
+
e->accept(&f);
|
| 213 |
+
return f.bufs();
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
const std::unordered_set<BufPtr>& bufs() {
|
| 217 |
+
return bufs_;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
private:
|
| 221 |
+
std::unordered_set<BufPtr> bufs_;
|
| 222 |
+
};
|
| 223 |
+
|
| 224 |
+
// Traverses the IR to determine if a particular Var is modified within it.
|
| 225 |
+
class ModifiesVarChecker : public IRVisitor {
|
| 226 |
+
public:
|
| 227 |
+
ModifiesVarChecker(VarPtr v) : var_(std::move(v)) {}
|
| 228 |
+
|
| 229 |
+
static bool check(const StmtPtr& s, VarPtr v) {
|
| 230 |
+
ModifiesVarChecker checker(std::move(v));
|
| 231 |
+
s->accept(&checker);
|
| 232 |
+
return checker.found();
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
bool found() {
|
| 236 |
+
return found_;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
private:
|
| 240 |
+
void visit(const StorePtr& v) override {
|
| 241 |
+
if (v->buf()->base_handle() == var_) {
|
| 242 |
+
found_ = true;
|
| 243 |
+
return;
|
| 244 |
+
}
|
| 245 |
+
IRVisitor::visit(v);
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
void visit(const AtomicAddPtr& v) override {
|
| 249 |
+
if (v->buf()->base_handle() == var_) {
|
| 250 |
+
found_ = true;
|
| 251 |
+
return;
|
| 252 |
+
}
|
| 253 |
+
IRVisitor::visit(v);
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
void visit(const LetPtr& v) override {
|
| 257 |
+
if (v->var() == var_) {
|
| 258 |
+
found_ = true;
|
| 259 |
+
return;
|
| 260 |
+
}
|
| 261 |
+
IRVisitor::visit(v);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
void visit(const ForPtr& v) override {
|
| 265 |
+
if (v->var() == var_) {
|
| 266 |
+
found_ = true;
|
| 267 |
+
return;
|
| 268 |
+
}
|
| 269 |
+
IRVisitor::visit(v);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
VarPtr var_;
|
| 273 |
+
bool found_{false};
|
| 274 |
+
};
|
| 275 |
+
|
| 276 |
+
// Traverse the Block stmt to identify the live range of the specified buf. The
|
| 277 |
+
// live range, indicated by a pair of integers, specifies the first and last
|
| 278 |
+
// stmt in block stmts that access to the buf.
|
| 279 |
+
class BufLiveRange : public IRVisitor {
|
| 280 |
+
public:
|
| 281 |
+
BufLiveRange(BufPtr b) : buf_(std::move(b)) {}
|
| 282 |
+
|
| 283 |
+
static std::tuple<int32_t, int32_t> liveRange(const StmtPtr& s, BufPtr b) {
|
| 284 |
+
BlockPtr block = to<Block>(s);
|
| 285 |
+
// We Only analyze buffer live ranges for block stmts.
|
| 286 |
+
if (!block) {
|
| 287 |
+
return std::make_tuple(0, 0);
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
BufLiveRange analyzer(std::move(b));
|
| 291 |
+
block->accept(&analyzer);
|
| 292 |
+
return analyzer.getLiveRange();
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
private:
|
| 296 |
+
std::tuple<int32_t, int32_t> getLiveRange() {
|
| 297 |
+
return std::make_tuple(begin_, end_);
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
bool hasBufReads(const StmtPtr& s) {
|
| 301 |
+
auto loads1 = NodeFinder<Load>::find(s);
|
| 302 |
+
for (const auto& l : loads1) {
|
| 303 |
+
if (l->buf() == buf_) {
|
| 304 |
+
return true;
|
| 305 |
+
}
|
| 306 |
+
}
|
| 307 |
+
auto loads2 = NodeFinder<ExternalCall>::find(s);
|
| 308 |
+
for (const auto& l : loads2) {
|
| 309 |
+
for (const auto& lb : l->buf_args()) {
|
| 310 |
+
if (lb == buf_) {
|
| 311 |
+
return true;
|
| 312 |
+
}
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
auto loads3 = NodeFinder<ExternalCallWithAlloc>::find(s);
|
| 316 |
+
for (const auto& l : loads3) {
|
| 317 |
+
for (const auto& lb : l->buf_args()) {
|
| 318 |
+
if (lb == buf_) {
|
| 319 |
+
return true;
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
return false;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
bool hasBufWrites(const StmtPtr& s) {
|
| 327 |
+
auto writes1 = NodeFinder<Store>::find(s);
|
| 328 |
+
for (const auto& w : writes1) {
|
| 329 |
+
if (w->buf() == buf_) {
|
| 330 |
+
return true;
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
auto writes2 = NodeFinder<ExternalCall>::find(s);
|
| 334 |
+
for (const auto& w : writes2) {
|
| 335 |
+
if (w->buf() == buf_) {
|
| 336 |
+
return true;
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
auto writes3 = NodeFinder<ExternalCallWithAlloc>::find(s);
|
| 340 |
+
for (const auto& w : writes3) {
|
| 341 |
+
for (const auto& wb : w->buf_out_args()) {
|
| 342 |
+
if (wb == buf_) {
|
| 343 |
+
return true;
|
| 344 |
+
}
|
| 345 |
+
}
|
| 346 |
+
}
|
| 347 |
+
return false;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
void findAccAndUpdateLiveRange(const StmtPtr& s) {
|
| 351 |
+
bool has_reads = hasBufReads(s), has_writes = hasBufWrites(s);
|
| 352 |
+
if (has_reads || has_writes) {
|
| 353 |
+
if (begin_ == -1) {
|
| 354 |
+
begin_ = curr_index_;
|
| 355 |
+
};
|
| 356 |
+
end_ = curr_index_;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
void visit(const BlockPtr& v) override {
|
| 361 |
+
for (const StmtPtr& s : *v) {
|
| 362 |
+
curr_index_ += 1;
|
| 363 |
+
findAccAndUpdateLiveRange(s);
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
BufPtr buf_;
|
| 368 |
+
int32_t begin_ = -1;
|
| 369 |
+
int32_t end_ = -1;
|
| 370 |
+
int32_t curr_index_ = -1;
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
// A class that analyzes the given program relevant for Block backend
|
| 374 |
+
// It creates a map of multi dim buffers and their flat versions
|
| 375 |
+
class CreateBufferMap : public IRVisitor {
|
| 376 |
+
public:
|
| 377 |
+
const std::unordered_map<std::string, BufPtr>& getBufferMap() const {
|
| 378 |
+
return map_input_to_tensor_bufs_;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
private:
|
| 382 |
+
void visit(const StorePtr& v) override {
|
| 383 |
+
auto load_node = to<Load>(v->value());
|
| 384 |
+
if (load_node) {
|
| 385 |
+
auto t_buf = load_node->buf();
|
| 386 |
+
map_input_to_tensor_bufs_.emplace(t_buf->name_hint(), v->buf());
|
| 387 |
+
} else {
|
| 388 |
+
auto add_node = to<Add>(v->value());
|
| 389 |
+
auto mul_node = to<Mul>(v->value());
|
| 390 |
+
// This means for now, v->value() can be Add or Mul
|
| 391 |
+
TORCH_INTERNAL_ASSERT(add_node || mul_node, buildErrorMessage());
|
| 392 |
+
map_input_to_tensor_bufs_.emplace(v->buf()->name_hint(), v->buf());
|
| 393 |
+
}
|
| 394 |
+
v->value()->accept(this);
|
| 395 |
+
}
|
| 396 |
+
std::unordered_map<std::string, BufPtr> map_input_to_tensor_bufs_;
|
| 397 |
+
};
|
| 398 |
+
|
| 399 |
+
} // namespace torch::jit::tensorexpr
|
| 400 |
+
|
| 401 |
+
#else
|
| 402 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 403 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/block_codegen.h
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <string>
|
| 5 |
+
#include <unordered_map>
|
| 6 |
+
#include <unordered_set>
|
| 7 |
+
#include <utility>
|
| 8 |
+
|
| 9 |
+
#include <ATen/ATen.h>
|
| 10 |
+
#include <torch/csrc/jit/resource_guard.h>
|
| 11 |
+
#include <torch/csrc/jit/tensorexpr/analysis.h>
|
| 12 |
+
#include <torch/csrc/jit/tensorexpr/codegen.h>
|
| 13 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 14 |
+
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
|
| 15 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 16 |
+
#include <torch/csrc/jit/tensorexpr/unique_name_manager.h>
|
| 17 |
+
|
| 18 |
+
namespace torch::jit::tensorexpr {
|
| 19 |
+
|
| 20 |
+
// A class that analyzes the given program relevant for Block backend.
|
| 21 |
+
class BlockAnalysis : public IRVisitor {
|
| 22 |
+
public:
|
| 23 |
+
bool is_buf_store_target(const BufPtr& buf) const {
|
| 24 |
+
return store_targets_.count(buf) > 0;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const std::unordered_set<BufPtr>& loads() const {
|
| 28 |
+
return loads_;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
const std::unordered_set<BufPtr>& stores() const {
|
| 32 |
+
return store_targets_;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
int64_t block_size() const {
|
| 36 |
+
return block_size_;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
bool areBufsInMap(const std::unordered_set<BufPtr>& bufs) const;
|
| 40 |
+
|
| 41 |
+
BufPtr getMultiDimBuf(const BufPtr& buf) const;
|
| 42 |
+
|
| 43 |
+
std::string getInputName(const BufPtr& buf) const;
|
| 44 |
+
|
| 45 |
+
std::string getFlatInputName(const BufPtr& buf) const {
|
| 46 |
+
return getInputName(buf) + "_flat";
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
std::unordered_map<std::string, BufPtr> getBufferMap() const {
|
| 50 |
+
return map_input_to_tensor_bufs_;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
private:
|
| 54 |
+
void visit(const StorePtr& v) override;
|
| 55 |
+
void visit(const LoadPtr& v) override;
|
| 56 |
+
void visit(const ForPtr& v) override;
|
| 57 |
+
|
| 58 |
+
std::unordered_map<std::string, BufPtr> map_input_to_tensor_bufs_;
|
| 59 |
+
std::unordered_set<BufPtr> store_targets_;
|
| 60 |
+
std::unordered_set<BufPtr> loads_;
|
| 61 |
+
int64_t block_size_ = 32;
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
// A class that overrides the underlying IRPrinter to produce Block.
|
| 65 |
+
class BlockPrinter : public IRPrinter {
|
| 66 |
+
public:
|
| 67 |
+
BlockPrinter(std::ostream* os, BlockAnalysis* block_analysis)
|
| 68 |
+
: IRPrinter(*os), block_analysis_(block_analysis) {}
|
| 69 |
+
|
| 70 |
+
using IRPrinter::name_manager;
|
| 71 |
+
using IRPrinter::visit;
|
| 72 |
+
|
| 73 |
+
private:
|
| 74 |
+
BlockAnalysis* block_analysis_;
|
| 75 |
+
std::unordered_map<std::string, int> dim_values_map;
|
| 76 |
+
std::vector<std::string> dim_names = {"N", "H", "W", "C"};
|
| 77 |
+
std::vector<std::string> flat_dim_names = {"N", "NH", "NHW", "NHWC"};
|
| 78 |
+
void PrintTensorInfo(const std::unordered_set<BufPtr>& bufs);
|
| 79 |
+
void PrintArguments(const std::unordered_set<BufPtr>& bufs);
|
| 80 |
+
void PrintBufferInfo(const std::unordered_set<BufPtr>& bufs);
|
| 81 |
+
void PrintDistribution(const std::unordered_set<BufPtr>& bufs);
|
| 82 |
+
void PrintLoop(const std::unordered_set<BufPtr>& bufs, bool block_idx = true);
|
| 83 |
+
void PrintReshapeInfo(
|
| 84 |
+
const std::unordered_set<BufPtr>& bufs,
|
| 85 |
+
bool reverse = false);
|
| 86 |
+
void PrintDMAs(const std::unordered_set<BufPtr>& bufs);
|
| 87 |
+
void PrintAdjustBuffers(const std::unordered_set<BufPtr>& bufs);
|
| 88 |
+
|
| 89 |
+
void visit(const ForPtr& v) override;
|
| 90 |
+
void visit(const LoadPtr& v) override;
|
| 91 |
+
void visit(const StorePtr& v) override;
|
| 92 |
+
void visit(const BlockPtr& v) override;
|
| 93 |
+
void visit(const AddPtr& v) override;
|
| 94 |
+
void visit(const MulPtr& v) override;
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
class TORCH_API BlockCodeGen : public CodeGen {
|
| 98 |
+
public:
|
| 99 |
+
template <typename... Ts>
|
| 100 |
+
/* implicit */
|
| 101 |
+
BlockCodeGen(StmtPtr stmt, Ts... ts)
|
| 102 |
+
: CodeGen(
|
| 103 |
+
stmt,
|
| 104 |
+
std::vector<BufferArg>({BufferArg(ts)...}),
|
| 105 |
+
at::Device(at::kCPU)) {
|
| 106 |
+
Initialize();
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
BlockCodeGen(
|
| 110 |
+
StmtPtr stmt,
|
| 111 |
+
const std::vector<BufferArg>& buffer_args,
|
| 112 |
+
at::Device device = at::Device(at::kCPU),
|
| 113 |
+
const std::string& kernel_func_name = "func")
|
| 114 |
+
: CodeGen(std::move(stmt), buffer_args, device, kernel_func_name) {
|
| 115 |
+
Initialize();
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
~BlockCodeGen() override;
|
| 119 |
+
|
| 120 |
+
void call(const std::vector<CallArg>& args) override;
|
| 121 |
+
void call_raw(const std::vector<void*>& args) override;
|
| 122 |
+
|
| 123 |
+
void Initialize();
|
| 124 |
+
|
| 125 |
+
std::string getCodeText(const std::string& attr = "") override {
|
| 126 |
+
return oss_.str();
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
private:
|
| 130 |
+
UniqueNameManager* name_manager() {
|
| 131 |
+
if (!printer_) {
|
| 132 |
+
throw std::runtime_error("Null IRPrinter is not expected");
|
| 133 |
+
}
|
| 134 |
+
return printer_->name_manager();
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
std::ostream& os() {
|
| 138 |
+
return printer_->os();
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
std::ostringstream oss_;
|
| 142 |
+
std::unique_ptr<BlockPrinter> printer_;
|
| 143 |
+
std::unique_ptr<BlockAnalysis> block_analysis_;
|
| 144 |
+
|
| 145 |
+
std::string GetUniqueFuncName(const std::string& func_prefix);
|
| 146 |
+
};
|
| 147 |
+
} // namespace torch::jit::tensorexpr
|
| 148 |
+
|
| 149 |
+
#else
|
| 150 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 151 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/bounds_inference.h
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <unordered_map>
|
| 5 |
+
#include <vector>
|
| 6 |
+
|
| 7 |
+
#include <torch/csrc/Export.h>
|
| 8 |
+
#include <torch/csrc/jit/tensorexpr/mem_dependency_checker.h>
|
| 9 |
+
|
| 10 |
+
namespace torch::jit::tensorexpr {
|
| 11 |
+
|
| 12 |
+
class Expr;
|
| 13 |
+
class Buf;
|
| 14 |
+
class Stmt;
|
| 15 |
+
|
| 16 |
+
enum C10_API_ENUM TensorAccessKind { kLoad, kStore, kMutate };
|
| 17 |
+
|
| 18 |
+
struct TORCH_API TensorAccessBoundsInfo {
|
| 19 |
+
TensorAccessKind kind;
|
| 20 |
+
std::vector<ExprPtr> start;
|
| 21 |
+
std::vector<ExprPtr> stop;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
using BoundsInfo =
|
| 25 |
+
std::unordered_map<BufPtr, std::vector<TensorAccessBoundsInfo>>;
|
| 26 |
+
|
| 27 |
+
TORCH_API BoundsInfo
|
| 28 |
+
inferBounds(const StmtPtr& s, bool distinctAccessKinds = true);
|
| 29 |
+
|
| 30 |
+
// Bounds inference caching the analysis. The MemDependencyChecker must already
|
| 31 |
+
// have been run.
|
| 32 |
+
TORCH_API BoundsInfo getInferredBounds(
|
| 33 |
+
analysis::MemDependencyChecker& analyzer,
|
| 34 |
+
const StmtPtr& s,
|
| 35 |
+
bool distinctAccessKinds = true);
|
| 36 |
+
TORCH_API BoundsInfo getInferredBounds(
|
| 37 |
+
analysis::MemDependencyChecker& analyzer,
|
| 38 |
+
const ExprPtr& e,
|
| 39 |
+
bool distinctAccessKinds = true);
|
| 40 |
+
|
| 41 |
+
TORCH_API void printBoundsInfo(const BoundsInfo& v);
|
| 42 |
+
|
| 43 |
+
TORCH_API std::vector<ExprPtr> getBoundExtents(
|
| 44 |
+
const std::vector<TensorAccessBoundsInfo>& infos);
|
| 45 |
+
|
| 46 |
+
// The kind of dependency found, in increasing order of exclusivity.
|
| 47 |
+
enum class HazardKind {
|
| 48 |
+
ReadAfterWrite,
|
| 49 |
+
WriteAfterRead,
|
| 50 |
+
WriteAfterWrite,
|
| 51 |
+
NoDependency,
|
| 52 |
+
};
|
| 53 |
+
TORCH_API HazardKind getPotentialHazards(
|
| 54 |
+
analysis::MemDependencyChecker& analyzer,
|
| 55 |
+
const StmtPtr& A,
|
| 56 |
+
const StmtPtr& B);
|
| 57 |
+
|
| 58 |
+
// Returns true if there is a conflicting overlap between accesses in
|
| 59 |
+
// statements A and B. A conflicting overlap is an overlap in buffer accesses
|
| 60 |
+
// where at least one of the accesses is a Store.
|
| 61 |
+
TORCH_API bool hasConflictingOverlap(
|
| 62 |
+
analysis::MemDependencyChecker& analyzer,
|
| 63 |
+
const StmtPtr& A,
|
| 64 |
+
const StmtPtr& B);
|
| 65 |
+
// Same as above, between accesses in stores S1 and S2.
|
| 66 |
+
TORCH_API bool isOverlapping(
|
| 67 |
+
analysis::MemDependencyChecker& analyzer,
|
| 68 |
+
const StorePtr& S1,
|
| 69 |
+
const StorePtr& S2);
|
| 70 |
+
// Same as above, between accesses in store S and load L.
|
| 71 |
+
TORCH_API bool isOverlapping(
|
| 72 |
+
analysis::MemDependencyChecker& analyzer,
|
| 73 |
+
const StorePtr& S,
|
| 74 |
+
const LoadPtr& L);
|
| 75 |
+
|
| 76 |
+
} // namespace torch::jit::tensorexpr
|
| 77 |
+
|
| 78 |
+
#else
|
| 79 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 80 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/bounds_overlap.h
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/tensorexpr/expr.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 6 |
+
|
| 7 |
+
#include <utility>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch::jit::tensorexpr::analysis {
|
| 11 |
+
|
| 12 |
+
// A simple class containing the start and end of a range in a single dimension.
|
| 13 |
+
struct TORCH_API Bound {
|
| 14 |
+
ExprPtr start{nullptr};
|
| 15 |
+
ExprPtr end{nullptr};
|
| 16 |
+
|
| 17 |
+
// This stores whether or not the start and end of this Bound have previously
|
| 18 |
+
// been swapped. This occurs when the bound is in a loop with a negative
|
| 19 |
+
// stride.
|
| 20 |
+
bool swapped{false};
|
| 21 |
+
|
| 22 |
+
Bound() = default;
|
| 23 |
+
Bound(ExprPtr s, ExprPtr e) : start(std::move(s)), end(std::move(e)) {}
|
| 24 |
+
|
| 25 |
+
void print() const;
|
| 26 |
+
bool equals(const Bound& other) const;
|
| 27 |
+
|
| 28 |
+
// The comparison operators are conservative. If the compare operator returns
|
| 29 |
+
// true, it means that all the elements satisfy the logical expression. But
|
| 30 |
+
// the false does not mean the opposite comparison is satisfied. It could be
|
| 31 |
+
// but not always.
|
| 32 |
+
bool operator==(const Bound& other) const;
|
| 33 |
+
bool operator!=(const Bound& other) const;
|
| 34 |
+
bool operator<(const Bound& other) const;
|
| 35 |
+
bool operator<=(const Bound& other) const;
|
| 36 |
+
bool operator>(const Bound& other) const;
|
| 37 |
+
bool operator>=(const Bound& other) const;
|
| 38 |
+
|
| 39 |
+
void swap() noexcept {
|
| 40 |
+
std::swap(start, end);
|
| 41 |
+
swapped = !swapped;
|
| 42 |
+
}
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
struct BoundHash {
|
| 46 |
+
size_t operator()(const Bound& b) const {
|
| 47 |
+
return std::hash<ExprPtr>()(b.start) ^ std::hash<ExprPtr>()(b.end);
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
// The type of overlap found. Each condition is true only if none of the
|
| 52 |
+
// previous conditions hold.
|
| 53 |
+
// ContainedOrEqual: All elements in the Bound A are in the Bound B (this
|
| 54 |
+
// includes the case where the bounds are equal).
|
| 55 |
+
// Contains: All elements in the Bound B are in the Bound A.
|
| 56 |
+
// PartialOverlap: Any elements in the Bound B are in the Bound A.
|
| 57 |
+
// NoOverlap: No elements in the Bound A are in the bound B.
|
| 58 |
+
enum class OverlapKind {
|
| 59 |
+
ContainedOrEqual,
|
| 60 |
+
Contains,
|
| 61 |
+
PartialOverlap,
|
| 62 |
+
NoOverlap
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
// The Bound comparison result.
|
| 66 |
+
// True: Every Bound element always satisfies the given comparison operator
|
| 67 |
+
// False: Every Bound element always does NOT satisfy the given comparison
|
| 68 |
+
// operator
|
| 69 |
+
// NotDetermined: Some elements satisfy the given comparison operator and
|
| 70 |
+
// some elements not
|
| 71 |
+
enum class CmpEvalResult { True, False, NotDetermined };
|
| 72 |
+
|
| 73 |
+
// Returns the kind of overlap between Bound A and Bound B in a single
|
| 74 |
+
// dimension.
|
| 75 |
+
OverlapKind TORCH_API boundOverlap(const Bound& A, const Bound& B);
|
| 76 |
+
|
| 77 |
+
// The comparison is conservative and the compare result is deterministic.
|
| 78 |
+
// It means that every element of the Bound to be compared needs to satisfy
|
| 79 |
+
// the given comparison operator.
|
| 80 |
+
CmpEvalResult TORCH_API compareBound(
|
| 81 |
+
const Bound& a,
|
| 82 |
+
const Bound& b,
|
| 83 |
+
const CompareSelectOperation& cmp_op);
|
| 84 |
+
|
| 85 |
+
// A multi dimensional bound representing the bound of a set of indices.
|
| 86 |
+
using IndexBounds = std::vector<Bound>;
|
| 87 |
+
|
| 88 |
+
// Returns true if two IndexBounds are equivalent.
|
| 89 |
+
bool TORCH_API indexBoundsEquals(const IndexBounds& A, const IndexBounds& B);
|
| 90 |
+
|
| 91 |
+
// Flattens a multi dimensional bound to a single dimension. The IndexBounds "a"
|
| 92 |
+
// *must* encapsulate the entire range of the buffer.
|
| 93 |
+
Bound TORCH_API flattenBounds(const IndexBounds& a);
|
| 94 |
+
|
| 95 |
+
// Determines the kind of overlap in X dimensions.
|
| 96 |
+
OverlapKind TORCH_API overlaps(const IndexBounds& a, const IndexBounds& b);
|
| 97 |
+
|
| 98 |
+
// Returns the Bound slices created by subtracting bound B from bound A.
|
| 99 |
+
// Multiple Bounds can be returned in the case where B slices A into two
|
| 100 |
+
// distinct regions with no overlap.
|
| 101 |
+
//
|
| 102 |
+
// For example:
|
| 103 |
+
// subtractBound((0, 10), (2, 4)) => [(0, 1), (5, 10)]
|
| 104 |
+
// bound A: (0, 10)
|
| 105 |
+
// bound B: (2, 4)
|
| 106 |
+
// If we remove slice (2, 4) from the slice (0, 10), we will be left
|
| 107 |
+
// with 2 slices, one at the start (0, 1), and one at the end (5, 10).
|
| 108 |
+
// So, the result of this subtraction is [(0, 1), (5, 10)].
|
| 109 |
+
//
|
| 110 |
+
// Note: this doesn't use IndexBounds because the Bounds returned do not
|
| 111 |
+
// represent multiple different dimensions.
|
| 112 |
+
std::vector<Bound> TORCH_API subtractBound(const Bound& a, const Bound& b);
|
| 113 |
+
|
| 114 |
+
// Returns the bound slices created by subtracting the IndexBounds B from A.
|
| 115 |
+
std::vector<IndexBounds> TORCH_API subtractIndicesBounds(
|
| 116 |
+
const IndexBounds& A,
|
| 117 |
+
const IndexBounds& B,
|
| 118 |
+
OverlapKind overlap);
|
| 119 |
+
std::vector<IndexBounds> TORCH_API
|
| 120 |
+
subtractIndicesBounds(const IndexBounds& A, const IndexBounds& B);
|
| 121 |
+
|
| 122 |
+
} // namespace torch::jit::tensorexpr::analysis
|
| 123 |
+
|
| 124 |
+
#else
|
| 125 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 126 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/codegen.h
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 6 |
+
#include <torch/csrc/jit/tensorexpr/tensor.h>
|
| 7 |
+
|
| 8 |
+
#include <utility>
|
| 9 |
+
|
| 10 |
+
namespace torch::jit::tensorexpr {
|
| 11 |
+
|
| 12 |
+
template <typename T>
|
| 13 |
+
class PaddedBuffer;
|
| 14 |
+
|
| 15 |
+
class TORCH_API CodeGen {
|
| 16 |
+
public:
|
| 17 |
+
class BufferArg;
|
| 18 |
+
class CallArg;
|
| 19 |
+
|
| 20 |
+
template <typename... Ts>
|
| 21 |
+
CodeGen(StmtPtr stmt, Ts... ts)
|
| 22 |
+
: stmt_(std::move(stmt)), buffer_args_({BufferArg(ts)...}) {}
|
| 23 |
+
|
| 24 |
+
CodeGen(
|
| 25 |
+
StmtPtr stmt,
|
| 26 |
+
std::vector<BufferArg> buffer_args,
|
| 27 |
+
at::Device device = at::kCPU,
|
| 28 |
+
std::string kernel_func_name = "func");
|
| 29 |
+
|
| 30 |
+
CodeGen(const CodeGen& rhs);
|
| 31 |
+
|
| 32 |
+
CodeGen(CodeGen&& rhs);
|
| 33 |
+
|
| 34 |
+
virtual ~CodeGen();
|
| 35 |
+
|
| 36 |
+
CodeGen& operator=(const CodeGen& rhs);
|
| 37 |
+
|
| 38 |
+
CodeGen& operator=(CodeGen&& rhs);
|
| 39 |
+
|
| 40 |
+
StmtPtr stmt() const {
|
| 41 |
+
return stmt_;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
void set_stmt(StmtPtr s) {
|
| 45 |
+
stmt_ = std::move(s);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
void apply_mutator(IRMutator* mutator) {
|
| 49 |
+
stmt_ = stmt_->accept_mutator(mutator);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
void apply_visitor(IRVisitor* visitor) {
|
| 53 |
+
stmt_->accept(visitor);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
std::vector<BufferArg>& buffer_args() {
|
| 57 |
+
return buffer_args_;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
const std::vector<BufferArg>& buffer_args() const {
|
| 61 |
+
return buffer_args_;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
at::Device device() {
|
| 65 |
+
return device_;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// This function returns the generated code as
|
| 69 |
+
// a string.
|
| 70 |
+
virtual std::string getCodeText(
|
| 71 |
+
const std::string& attr [[maybe_unused]] = "") {
|
| 72 |
+
return "";
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// TODO: Figure out how to unify these call interfaces.
|
| 76 |
+
|
| 77 |
+
/// Call a function with a vector of CallArgs, which are tagged
|
| 78 |
+
/// unions that properly type the arguments.
|
| 79 |
+
virtual void call(const std::vector<CallArg>& args) = 0;
|
| 80 |
+
|
| 81 |
+
/// Call a function faster than a regular `call` by assuming that
|
| 82 |
+
/// the generated kernel already knows the type of the arguments, so
|
| 83 |
+
/// they can be type-punned with `void*`s.
|
| 84 |
+
virtual void call_raw(const std::vector<void*>& args) = 0;
|
| 85 |
+
|
| 86 |
+
/// Call a function even faster than a regular call, by assuming
|
| 87 |
+
/// that the number of thread blocks can be derived from `numel` via
|
| 88 |
+
/// a simple division, rather than evaluating an expression.
|
| 89 |
+
virtual void call_with_numel(void** args, int64_t numel);
|
| 90 |
+
|
| 91 |
+
virtual at::Tensor empty_strided(
|
| 92 |
+
c10::IntArrayRef size,
|
| 93 |
+
c10::IntArrayRef stride,
|
| 94 |
+
std::optional<c10::ScalarType> dtype_opt,
|
| 95 |
+
std::optional<c10::Layout> layout_opt,
|
| 96 |
+
std::optional<c10::Device> device_opt,
|
| 97 |
+
std::optional<bool> pin_memory_opt) {
|
| 98 |
+
return at::empty_strided(
|
| 99 |
+
size, stride, dtype_opt, layout_opt, device_opt, pin_memory_opt);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
const std::string& kernel_func_name() const {
|
| 103 |
+
return kernel_func_name_;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
void allocIntermediateBufs();
|
| 107 |
+
|
| 108 |
+
protected:
|
| 109 |
+
static void* argToPtr(const BufferArg& bufferArg, const CallArg& callArg);
|
| 110 |
+
|
| 111 |
+
private:
|
| 112 |
+
StmtPtr stmt_;
|
| 113 |
+
std::vector<BufferArg> buffer_args_;
|
| 114 |
+
at::Device device_ = at::kCPU;
|
| 115 |
+
std::string kernel_func_name_ = "func";
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
class TORCH_API ExtCallMemoryReuse : public IRMutator {
|
| 119 |
+
static std::unordered_map<std::string, std::string> makeExtCallFuncNameMap();
|
| 120 |
+
static const std::unordered_map<std::string, std::string> extCallFuncNameMap_;
|
| 121 |
+
|
| 122 |
+
public:
|
| 123 |
+
explicit ExtCallMemoryReuse(
|
| 124 |
+
const std::vector<CodeGen::BufferArg>& bufferArgs);
|
| 125 |
+
~ExtCallMemoryReuse() override = default;
|
| 126 |
+
StmtPtr mutate(const ExternalCallPtr& v) override;
|
| 127 |
+
|
| 128 |
+
private:
|
| 129 |
+
std::unordered_set<BufPtr> bufferArgs_;
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
class CodeGen::BufferArg {
|
| 133 |
+
public:
|
| 134 |
+
BufferArg(const Tensor& tensor) : buf_(tensor.buf()) {}
|
| 135 |
+
BufferArg(const VarHandle& var) : var_(var.node()), isVar_(true) {}
|
| 136 |
+
BufferArg(const BufHandle& buf) : buf_(buf.node()) {}
|
| 137 |
+
BufferArg(BufPtr buf) : buf_(std::move(buf)) {}
|
| 138 |
+
|
| 139 |
+
VarPtr var() const {
|
| 140 |
+
return isVar_ ? var_ : buf_->base_handle();
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
BufPtr buf() const {
|
| 144 |
+
return buf_;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
bool isVar() const {
|
| 148 |
+
return isVar_;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
Dtype dtype() const {
|
| 152 |
+
return isVar_ ? var_->dtype() : buf_->dtype();
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
private:
|
| 156 |
+
VarPtr var_ = nullptr;
|
| 157 |
+
BufPtr buf_ = nullptr;
|
| 158 |
+
bool isVar_ = false;
|
| 159 |
+
};
|
| 160 |
+
|
| 161 |
+
class CodeGen::CallArg {
|
| 162 |
+
public:
|
| 163 |
+
template <typename T>
|
| 164 |
+
CallArg(const PaddedBuffer<T>& buffer);
|
| 165 |
+
|
| 166 |
+
template <typename T>
|
| 167 |
+
CallArg(const std::vector<T>& buffer)
|
| 168 |
+
: data_(const_cast<T*>(buffer.data())) {}
|
| 169 |
+
|
| 170 |
+
CallArg(void* ptr) : data_(ptr) {}
|
| 171 |
+
|
| 172 |
+
#define ARG_TYPE_CTOR(Type, Name) \
|
| 173 |
+
CallArg(Type v) { \
|
| 174 |
+
memcpy(buffer_, &v, sizeof(Type)); \
|
| 175 |
+
data_ = (void*)buffer_; \
|
| 176 |
+
}
|
| 177 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, ARG_TYPE_CTOR)
|
| 178 |
+
#undef ARG_TYPE_CTOR
|
| 179 |
+
|
| 180 |
+
void* data() const {
|
| 181 |
+
return data_;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
CallArg(const CallArg& rhs) {
|
| 185 |
+
if (rhs.data_ == rhs.buffer_) {
|
| 186 |
+
memcpy(this->buffer_, rhs.buffer_, sizeof(rhs.buffer_));
|
| 187 |
+
this->data_ = (void*)(this->buffer_);
|
| 188 |
+
} else {
|
| 189 |
+
this->data_ = rhs.data_;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
CallArg& operator=(const CallArg& rhs) {
|
| 194 |
+
if (this == &rhs) {
|
| 195 |
+
return *this;
|
| 196 |
+
}
|
| 197 |
+
if (rhs.data_ == rhs.buffer_) {
|
| 198 |
+
memcpy(this->buffer_, rhs.buffer_, sizeof(rhs.buffer_));
|
| 199 |
+
this->data_ = (void*)(this->buffer_);
|
| 200 |
+
} else {
|
| 201 |
+
this->data_ = rhs.data_;
|
| 202 |
+
}
|
| 203 |
+
return *this;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
#define ARG_PTR_DEFINE(Type, Name) \
|
| 207 |
+
Type* Name##Ptr() const { \
|
| 208 |
+
TORCH_INTERNAL_ASSERT(data_ == (void*)buffer_); \
|
| 209 |
+
return (Type*)data_; \
|
| 210 |
+
}
|
| 211 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, ARG_PTR_DEFINE)
|
| 212 |
+
#undef ARG_PTR_DEFINE
|
| 213 |
+
|
| 214 |
+
private:
|
| 215 |
+
void* data_;
|
| 216 |
+
// Regarding a scalar value, CallArg uses void**=&data_ to store it. But the
|
| 217 |
+
// bit width of a pointer is 32bit on a 32bit platform. It cannot store the
|
| 218 |
+
// scalar if the bit width of the scalar is larger than 32bit, such as double
|
| 219 |
+
// and long. Hence, we add 8 bytes buffer dedicated to storing the scalar
|
| 220 |
+
// value regardless its bit width is less or greater than 32bits.
|
| 221 |
+
char buffer_[8] = {0}; // 64bits
|
| 222 |
+
};
|
| 223 |
+
|
| 224 |
+
class RegisterCodeGenList {
|
| 225 |
+
public:
|
| 226 |
+
TORCH_API static RegisterCodeGenList& GetInstance();
|
| 227 |
+
|
| 228 |
+
using StmtFactoryMethod = std::function<std::unique_ptr<CodeGen>(
|
| 229 |
+
StmtPtr stmt,
|
| 230 |
+
const std::vector<CodeGen::BufferArg>&,
|
| 231 |
+
at::Device device,
|
| 232 |
+
const std::string& kernel_func_name)>;
|
| 233 |
+
|
| 234 |
+
TORCH_API StmtFactoryMethod FindStmtFactoryMethod(const std::string& name);
|
| 235 |
+
RegisterCodeGenList(const RegisterCodeGenList&) = delete;
|
| 236 |
+
RegisterCodeGenList& operator=(const RegisterCodeGenList&) = delete;
|
| 237 |
+
|
| 238 |
+
private:
|
| 239 |
+
template <class CodeGenType>
|
| 240 |
+
friend class RegisterCodeGen;
|
| 241 |
+
RegisterCodeGenList() = default;
|
| 242 |
+
TORCH_API void AddStmtFactoryMethod(
|
| 243 |
+
const std::string& name,
|
| 244 |
+
const StmtFactoryMethod& stmt_factory_method);
|
| 245 |
+
|
| 246 |
+
std::unordered_map<std::string, StmtFactoryMethod> stmt_factory_methods_;
|
| 247 |
+
};
|
| 248 |
+
|
| 249 |
+
template <class CodeGenType>
|
| 250 |
+
class RegisterCodeGen {
|
| 251 |
+
public:
|
| 252 |
+
explicit RegisterCodeGen(const std::string& name) {
|
| 253 |
+
RegisterCodeGenList& codegen_list = RegisterCodeGenList::GetInstance();
|
| 254 |
+
codegen_list.AddStmtFactoryMethod(
|
| 255 |
+
name,
|
| 256 |
+
[](const StmtPtr& stmt,
|
| 257 |
+
const std::vector<CodeGen::BufferArg>& params,
|
| 258 |
+
at::Device device,
|
| 259 |
+
const std::string& kernel_func_name) {
|
| 260 |
+
return std::make_unique<CodeGenType>(
|
| 261 |
+
stmt, params, device, kernel_func_name);
|
| 262 |
+
});
|
| 263 |
+
}
|
| 264 |
+
};
|
| 265 |
+
|
| 266 |
+
TORCH_API std::unique_ptr<CodeGen> CreateCodeGen(
|
| 267 |
+
const std::string& name,
|
| 268 |
+
StmtPtr stmt,
|
| 269 |
+
const std::vector<CodeGen::BufferArg>& params,
|
| 270 |
+
at::Device device = at::kCPU,
|
| 271 |
+
const std::string& kernel_func_name = "func");
|
| 272 |
+
|
| 273 |
+
class TORCH_API GenericIntrinsicsExpander : public IRMutator {
|
| 274 |
+
protected:
|
| 275 |
+
ExprPtr mutate(const IntrinsicsPtr& v) override;
|
| 276 |
+
};
|
| 277 |
+
|
| 278 |
+
} // namespace torch::jit::tensorexpr
|
| 279 |
+
|
| 280 |
+
#else
|
| 281 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 282 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cpp_codegen.h
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/tensorexpr/codegen.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::jit::tensorexpr {
|
| 8 |
+
|
| 9 |
+
class CppVarNameRewriter;
|
| 10 |
+
|
| 11 |
+
// Generates C++ code from the IR.
|
| 12 |
+
//
|
| 13 |
+
// Vector operations are unrolled.
|
| 14 |
+
// For example:
|
| 15 |
+
// C[Ramp(0, 1, 3)] = A[Ramp(0, 2, 3)] + B[Ramp(0, 3, 3)];
|
| 16 |
+
// is unrolled into:
|
| 17 |
+
// C[0] = A[0] + B[0];
|
| 18 |
+
// C[1] = A[2] + B[3];
|
| 19 |
+
// C[2] = A[4] + B[6];
|
| 20 |
+
class TORCH_API CppPrinter : public IRPrinter {
|
| 21 |
+
public:
|
| 22 |
+
explicit CppPrinter(std::ostream* os);
|
| 23 |
+
~CppPrinter() override;
|
| 24 |
+
|
| 25 |
+
void printPrologue();
|
| 26 |
+
|
| 27 |
+
using IRPrinter::visit;
|
| 28 |
+
|
| 29 |
+
// Binary expressions.
|
| 30 |
+
void visit(const ModPtr& /*v*/) override;
|
| 31 |
+
void visit(const MaxPtr& /*v*/) override;
|
| 32 |
+
void visit(const MinPtr& /*v*/) override;
|
| 33 |
+
|
| 34 |
+
// Conditional expressions.
|
| 35 |
+
void visit(const CompareSelectPtr& /*v*/) override;
|
| 36 |
+
void visit(const IfThenElsePtr& /*v*/) override;
|
| 37 |
+
|
| 38 |
+
// Tensor operations.
|
| 39 |
+
void visit(const AllocatePtr& /*v*/) override;
|
| 40 |
+
void visit(const FreePtr& /*v*/) override;
|
| 41 |
+
void visit(const LoadPtr& /*v*/) override;
|
| 42 |
+
void visit(const StorePtr& /*v*/) override;
|
| 43 |
+
|
| 44 |
+
// Casts.
|
| 45 |
+
void visit(const CastPtr& /*v*/) override;
|
| 46 |
+
void visit(const BitCastPtr& /*v*/) override;
|
| 47 |
+
|
| 48 |
+
// Calls.
|
| 49 |
+
void visit(const IntrinsicsPtr& /*v*/) override;
|
| 50 |
+
void visit(const ExternalCallPtr& /*v*/) override;
|
| 51 |
+
|
| 52 |
+
// Vars.
|
| 53 |
+
void visit(const LetPtr& /*v*/) override;
|
| 54 |
+
void visit(const VarPtr& /*v*/) override;
|
| 55 |
+
|
| 56 |
+
// Vector data types.
|
| 57 |
+
void visit(const RampPtr& /*v*/) override;
|
| 58 |
+
void visit(const BroadcastPtr& /*v*/) override;
|
| 59 |
+
|
| 60 |
+
private:
|
| 61 |
+
int lane_{0};
|
| 62 |
+
std::unordered_map<VarPtr, ExprPtr> vector_vars_;
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
class TORCH_API CppCodeGen : public CodeGen {
|
| 66 |
+
public:
|
| 67 |
+
CppCodeGen(
|
| 68 |
+
StmtPtr stmt,
|
| 69 |
+
const std::vector<BufferArg>& buffer_args,
|
| 70 |
+
at::Device device = at::kCPU,
|
| 71 |
+
const std::string& kernel_func_name = "func");
|
| 72 |
+
|
| 73 |
+
~CppCodeGen() override;
|
| 74 |
+
|
| 75 |
+
void call(const std::vector<CallArg>& args) override;
|
| 76 |
+
void call_raw(const std::vector<void*>& args) override;
|
| 77 |
+
|
| 78 |
+
template <typename... Ts>
|
| 79 |
+
void operator()(const Ts&... ts) {
|
| 80 |
+
call(std::vector<CallArg>({CallArg(ts)...}));
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
std::string getCodeText(const std::string& attr = "") override {
|
| 84 |
+
return oss_.str();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
private:
|
| 88 |
+
void init();
|
| 89 |
+
|
| 90 |
+
std::ostream& os() {
|
| 91 |
+
return printer_->os();
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
std::ostringstream oss_;
|
| 95 |
+
std::unique_ptr<CppPrinter> printer_;
|
| 96 |
+
std::unique_ptr<CppVarNameRewriter> var_name_rewriter_;
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
} // namespace torch::jit::tensorexpr
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cpp_intrinsics.h
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
namespace torch::jit::tensorexpr {
|
| 5 |
+
|
| 6 |
+
constexpr auto cpp_intrinsics_definition = R"(
|
| 7 |
+
namespace std {
|
| 8 |
+
|
| 9 |
+
template <typename T,
|
| 10 |
+
std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
| 11 |
+
T rsqrt(T v) {
|
| 12 |
+
return 1.0f / std::sqrt(v);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
template <typename T,
|
| 16 |
+
std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
| 17 |
+
T frac(T v) {
|
| 18 |
+
T intpart;
|
| 19 |
+
return std::modf(v, &intpart);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
template <typename From, typename To>
|
| 23 |
+
To bitcast(const From& v) {
|
| 24 |
+
assert(sizeof(To) == sizeof(From));
|
| 25 |
+
To res;
|
| 26 |
+
std::memcpy(&res, &v, sizeof(From));
|
| 27 |
+
return res;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
} // namespace std
|
| 31 |
+
)";
|
| 32 |
+
|
| 33 |
+
} // namespace torch::jit::tensorexpr
|
| 34 |
+
|
| 35 |
+
#else
|
| 36 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 37 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cuda_codegen.h
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <unordered_set>
|
| 5 |
+
|
| 6 |
+
#include <ATen/ATen.h>
|
| 7 |
+
#include <ATen/cuda/CUDAContext.h>
|
| 8 |
+
#include <ATen/cuda/nvrtc_stub/ATenNVRTC.h>
|
| 9 |
+
#include <c10/cuda/CUDACachingAllocator.h>
|
| 10 |
+
#include <c10/cuda/CUDAGuard.h>
|
| 11 |
+
#include <torch/csrc/jit/resource_guard.h>
|
| 12 |
+
#include <torch/csrc/jit/tensorexpr/codegen.h>
|
| 13 |
+
#include <torch/csrc/jit/tensorexpr/eval.h>
|
| 14 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 15 |
+
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
|
| 16 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 17 |
+
#include <torch/csrc/jit/tensorexpr/llvm_codegen.h>
|
| 18 |
+
#include <torch/csrc/jit/tensorexpr/unique_name_manager.h>
|
| 19 |
+
|
| 20 |
+
namespace torch::jit::tensorexpr {
|
| 21 |
+
|
| 22 |
+
// A class that analyzes the given program relevant for Cuda backends.
|
| 23 |
+
class CudaAnalysis : public IRVisitor {
|
| 24 |
+
public:
|
| 25 |
+
CudaAnalysis() {
|
| 26 |
+
gpu_block_extents_ = {alloc<IntImm>(1), alloc<IntImm>(1), alloc<IntImm>(1)};
|
| 27 |
+
gpu_thread_extents_ = {
|
| 28 |
+
alloc<IntImm>(1), alloc<IntImm>(1), alloc<IntImm>(1)};
|
| 29 |
+
}
|
| 30 |
+
bool is_buf_store_target(const BufPtr& buf) const {
|
| 31 |
+
return store_targets_.count(buf) > 0;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
const std::unordered_set<VarPtr>& thread_local_bufs() const {
|
| 35 |
+
return thread_local_bufs_;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const std::unordered_set<VarPtr>& cross_block_bufs() const {
|
| 39 |
+
return cross_block_bufs_;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
const std::vector<ExprPtr>& gpu_block_extents() const {
|
| 43 |
+
return gpu_block_extents_;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const std::vector<ExprPtr>& gpu_thread_extents() const {
|
| 47 |
+
return gpu_thread_extents_;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
private:
|
| 51 |
+
void visit(const StorePtr& v) override {
|
| 52 |
+
store_targets_.insert(v->buf());
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
void visit(const AllocatePtr& v) override;
|
| 56 |
+
void visit(const FreePtr& v) override;
|
| 57 |
+
void visit(const PlacementAllocatePtr& v) override;
|
| 58 |
+
void visit(const ForPtr& v) override;
|
| 59 |
+
|
| 60 |
+
std::unordered_set<BufPtr> store_targets_;
|
| 61 |
+
std::unordered_set<VarPtr> thread_local_bufs_;
|
| 62 |
+
std::unordered_set<VarPtr> cross_block_bufs_;
|
| 63 |
+
|
| 64 |
+
std::vector<ExprPtr> gpu_block_extents_;
|
| 65 |
+
std::vector<ExprPtr> gpu_thread_extents_;
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
// An IRMutator that replaces binding loop options with Cuda metavars, and masks
|
| 69 |
+
// statements blocks which should execute with less reach than the launch
|
| 70 |
+
// parameter extent.
|
| 71 |
+
//
|
| 72 |
+
// We do this by segmenting each block into chunks which should have the same
|
| 73 |
+
// execution parameters, then if those params differ from the max mask each dim.
|
| 74 |
+
class GPUMetaVarRewriter : public IRMutator {
|
| 75 |
+
public:
|
| 76 |
+
explicit GPUMetaVarRewriter(const CudaAnalysis* cuda_analysis)
|
| 77 |
+
: cuda_analysis_(cuda_analysis) {
|
| 78 |
+
gpu_block_vars_ = {
|
| 79 |
+
alloc<Var>("blockIdx.x", kInt),
|
| 80 |
+
alloc<Var>("blockIdx.y", kInt),
|
| 81 |
+
alloc<Var>("blockIdx.z", kInt)};
|
| 82 |
+
gpu_thread_vars_ = {
|
| 83 |
+
alloc<Var>("threadIdx.x", kInt),
|
| 84 |
+
alloc<Var>("threadIdx.y", kInt),
|
| 85 |
+
alloc<Var>("threadIdx.z", kInt)};
|
| 86 |
+
|
| 87 |
+
current_block_reach_ = {
|
| 88 |
+
alloc<IntImm>(1), alloc<IntImm>(1), alloc<IntImm>(1)};
|
| 89 |
+
current_thread_reach_ = {
|
| 90 |
+
alloc<IntImm>(1), alloc<IntImm>(1), alloc<IntImm>(1)};
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
StmtPtr mutate(const ForPtr& v) override;
|
| 94 |
+
StmtPtr mutate(const BlockPtr& v) override;
|
| 95 |
+
|
| 96 |
+
const std::vector<VarPtr>& gpu_block_vars() const {
|
| 97 |
+
return gpu_block_vars_;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
const std::vector<VarPtr>& gpu_thread_vars() const {
|
| 101 |
+
return gpu_thread_vars_;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
const std::vector<ExprPtr>& gpu_block_extents() const {
|
| 105 |
+
return cuda_analysis_->gpu_block_extents();
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
const std::vector<ExprPtr>& gpu_thread_extents() const {
|
| 109 |
+
return cuda_analysis_->gpu_thread_extents();
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
private:
|
| 113 |
+
// When processing a block, stores the contents of each sub-segment.
|
| 114 |
+
class Segment {
|
| 115 |
+
public:
|
| 116 |
+
void reset(bool mask) {
|
| 117 |
+
stmts_.clear();
|
| 118 |
+
mask_ = mask;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
bool empty() const {
|
| 122 |
+
return stmts_.empty();
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
std::vector<StmtPtr>& stmts() {
|
| 126 |
+
return stmts_;
|
| 127 |
+
}
|
| 128 |
+
bool mask() {
|
| 129 |
+
return mask_;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
private:
|
| 133 |
+
std::vector<StmtPtr> stmts_;
|
| 134 |
+
bool mask_{true};
|
| 135 |
+
};
|
| 136 |
+
|
| 137 |
+
// Returns true if the current execution scope is equivalent to the launch
|
| 138 |
+
// parameters.
|
| 139 |
+
bool isFullExtent();
|
| 140 |
+
|
| 141 |
+
std::vector<VarPtr> gpu_block_vars_;
|
| 142 |
+
std::vector<VarPtr> gpu_thread_vars_;
|
| 143 |
+
|
| 144 |
+
std::vector<ExprPtr> current_block_reach_;
|
| 145 |
+
std::vector<ExprPtr> current_thread_reach_;
|
| 146 |
+
|
| 147 |
+
const CudaAnalysis* cuda_analysis_;
|
| 148 |
+
};
|
| 149 |
+
|
| 150 |
+
// A class that overrides the underlying IRPrinter to produce Cuda C.
|
| 151 |
+
class CudaPrinter : public IRPrinter {
|
| 152 |
+
public:
|
| 153 |
+
explicit CudaPrinter(
|
| 154 |
+
std::ostream* os,
|
| 155 |
+
const CudaAnalysis* cuda_analysis,
|
| 156 |
+
bool has_random)
|
| 157 |
+
: IRPrinter(*os), cuda_analysis_(cuda_analysis) {
|
| 158 |
+
if (has_random) {
|
| 159 |
+
rand_func_ = alloc<Var>("rand", kHandle);
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
void visit(const CastPtr& v) override;
|
| 164 |
+
void visit(const IntrinsicsPtr& v) override;
|
| 165 |
+
void visit(const ForPtr& v) override;
|
| 166 |
+
|
| 167 |
+
void visit(const LoadPtr& v) override;
|
| 168 |
+
void visit(const StorePtr& v) override;
|
| 169 |
+
void visit(const AtomicAddPtr& v) override;
|
| 170 |
+
void visit(const MaxPtr& v) override;
|
| 171 |
+
void visit(const MinPtr& v) override;
|
| 172 |
+
void visit(const IfThenElsePtr& v) override;
|
| 173 |
+
void visit(const BlockPtr& v) override;
|
| 174 |
+
void visit(const AllocatePtr& v) override;
|
| 175 |
+
void visit(const FreePtr& v) override;
|
| 176 |
+
void visit(const LetPtr& v) override;
|
| 177 |
+
|
| 178 |
+
void visit(const ExternalCallPtr& v) override;
|
| 179 |
+
|
| 180 |
+
VarPtr rand_func() const {
|
| 181 |
+
return rand_func_;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
std::string dtypeToCppString(const Dtype& dtype) override;
|
| 185 |
+
|
| 186 |
+
using IRPrinter::name_manager;
|
| 187 |
+
using IRPrinter::visit;
|
| 188 |
+
|
| 189 |
+
private:
|
| 190 |
+
VarPtr rand_func_;
|
| 191 |
+
const CudaAnalysis* cuda_analysis_;
|
| 192 |
+
|
| 193 |
+
void print_flat_alloc(const AllocatePtr& alloc);
|
| 194 |
+
};
|
| 195 |
+
|
| 196 |
+
// Construct Cuda C from the buffer and tensor input, and invoke the
|
| 197 |
+
// kernel when real arguments are provided.
|
| 198 |
+
class TORCH_CUDA_CU_API CudaCodeGen : public CodeGen {
|
| 199 |
+
public:
|
| 200 |
+
template <typename... Ts>
|
| 201 |
+
CudaCodeGen(StmtPtr stmt, Ts... ts)
|
| 202 |
+
: CodeGen(
|
| 203 |
+
stmt,
|
| 204 |
+
std::vector<BufferArg>({BufferArg(ts)...}),
|
| 205 |
+
at::Device(at::kCUDA, at::cuda::current_device())) {
|
| 206 |
+
Initialize();
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
CudaCodeGen(
|
| 210 |
+
StmtPtr stmt,
|
| 211 |
+
const std::vector<BufferArg>& buffer_args,
|
| 212 |
+
at::Device device = at::Device(at::kCUDA, at::cuda::current_device()),
|
| 213 |
+
const std::string& kernel_func_name = "func")
|
| 214 |
+
: CodeGen(std::move(stmt), buffer_args, device, kernel_func_name) {
|
| 215 |
+
Initialize();
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
~CudaCodeGen() override;
|
| 219 |
+
|
| 220 |
+
void call(const std::vector<CallArg>& args) override;
|
| 221 |
+
void call_raw(const std::vector<void*>& args) override;
|
| 222 |
+
void call_with_numel(void** args, int64_t numel) override;
|
| 223 |
+
|
| 224 |
+
template <typename... Ts>
|
| 225 |
+
void operator()(const Ts&... ts) {
|
| 226 |
+
call(std::vector<CallArg>({CallArg(ts)...}));
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
at::Tensor empty_strided(
|
| 230 |
+
c10::IntArrayRef size,
|
| 231 |
+
c10::IntArrayRef stride,
|
| 232 |
+
std::optional<c10::ScalarType> dtype_opt,
|
| 233 |
+
std::optional<c10::Layout> layout_opt,
|
| 234 |
+
std::optional<c10::Device> device_opt,
|
| 235 |
+
std::optional<bool> pin_memory_opt) override;
|
| 236 |
+
|
| 237 |
+
const std::vector<ExprPtr>& gpu_block_extents() const {
|
| 238 |
+
return cuda_analysis_->gpu_block_extents();
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
const std::vector<ExprPtr>& gpu_thread_extents() const {
|
| 242 |
+
return cuda_analysis_->gpu_thread_extents();
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
std::string getCodeText(const std::string& attr = "") override {
|
| 246 |
+
return oss_.str();
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
private:
|
| 250 |
+
void Initialize();
|
| 251 |
+
|
| 252 |
+
void CompileToNVRTC(const std::string& code, const std::string& func_name);
|
| 253 |
+
|
| 254 |
+
UniqueNameManager* name_manager() {
|
| 255 |
+
if (!printer_) {
|
| 256 |
+
throw std::runtime_error("Null IRPrinter is not expected");
|
| 257 |
+
}
|
| 258 |
+
return printer_->name_manager();
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
std::ostream& os() {
|
| 262 |
+
return printer_->os();
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
std::ostringstream oss_;
|
| 266 |
+
std::unique_ptr<CudaPrinter> printer_;
|
| 267 |
+
std::unique_ptr<CudaAnalysis> cuda_analysis_;
|
| 268 |
+
std::unique_ptr<GPUMetaVarRewriter> metavar_rewriter_;
|
| 269 |
+
std::unordered_set<std::string> taken_func_names;
|
| 270 |
+
std::mutex eval_lock_;
|
| 271 |
+
CUfunction function_{nullptr};
|
| 272 |
+
bool has_random_ = false;
|
| 273 |
+
int thread_block_size_ = -1;
|
| 274 |
+
|
| 275 |
+
std::vector<bool> arg_pos_in_extents_;
|
| 276 |
+
#ifdef TORCH_ENABLE_LLVM
|
| 277 |
+
std::vector<ExprEval<LLVMCodeGen>> block_extents_eval_;
|
| 278 |
+
std::vector<ExprEval<LLVMCodeGen>> thread_extents_eval_;
|
| 279 |
+
#else
|
| 280 |
+
std::vector<ExprEval<SimpleIREvaluator>> block_extents_eval_;
|
| 281 |
+
std::vector<ExprEval<SimpleIREvaluator>> thread_extents_eval_;
|
| 282 |
+
#endif
|
| 283 |
+
|
| 284 |
+
std::string GetUniqueFuncName(const std::string& func_prefix);
|
| 285 |
+
};
|
| 286 |
+
|
| 287 |
+
} // namespace torch::jit::tensorexpr
|
| 288 |
+
|
| 289 |
+
#else
|
| 290 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 291 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/cuda_random.h
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
namespace torch::jit::tensorexpr {
|
| 5 |
+
|
| 6 |
+
constexpr auto philox_random_string = R"(
|
| 7 |
+
|
| 8 |
+
class Philox {
|
| 9 |
+
public:
|
| 10 |
+
__device__ inline Philox(unsigned long long seed,
|
| 11 |
+
unsigned long long subsequence,
|
| 12 |
+
unsigned long long offset) {
|
| 13 |
+
key.x = (unsigned int)seed;
|
| 14 |
+
key.y = (unsigned int)(seed >> 32);
|
| 15 |
+
counter = make_uint4(0, 0, 0, 0);
|
| 16 |
+
counter.z = (unsigned int)(subsequence);
|
| 17 |
+
counter.w = (unsigned int)(subsequence >> 32);
|
| 18 |
+
STATE = 0;
|
| 19 |
+
incr_n(offset / 4);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
__device__ inline unsigned long operator()() {
|
| 23 |
+
if(STATE == 0) {
|
| 24 |
+
uint4 counter_ = counter;
|
| 25 |
+
uint2 key_ = key;
|
| 26 |
+
for(int i = 0; i < 9; i++) {
|
| 27 |
+
counter_ = single_round(counter_, key_);
|
| 28 |
+
key_.x += (kPhilox10A); key_.y += (kPhilox10B);
|
| 29 |
+
}
|
| 30 |
+
output = single_round(counter_, key_);
|
| 31 |
+
incr();
|
| 32 |
+
}
|
| 33 |
+
unsigned long ret;
|
| 34 |
+
switch(STATE) {
|
| 35 |
+
case 0: ret = output.x; break;
|
| 36 |
+
case 1: ret = output.y; break;
|
| 37 |
+
case 2: ret = output.z; break;
|
| 38 |
+
case 3: ret = output.w; break;
|
| 39 |
+
}
|
| 40 |
+
STATE = (STATE + 1) % 4;
|
| 41 |
+
return ret;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
private:
|
| 45 |
+
uint4 counter;
|
| 46 |
+
uint4 output;
|
| 47 |
+
uint2 key;
|
| 48 |
+
unsigned int STATE;
|
| 49 |
+
__device__ inline void incr_n(unsigned long long n) {
|
| 50 |
+
unsigned int nlo = (unsigned int)(n);
|
| 51 |
+
unsigned int nhi = (unsigned int)(n >> 32);
|
| 52 |
+
counter.x += nlo;
|
| 53 |
+
if (counter.x < nlo)
|
| 54 |
+
nhi++;
|
| 55 |
+
counter.y += nhi;
|
| 56 |
+
if (nhi <= counter.y)
|
| 57 |
+
return;
|
| 58 |
+
if (++counter.z)
|
| 59 |
+
return;
|
| 60 |
+
++counter.w;
|
| 61 |
+
}
|
| 62 |
+
__device__ inline void incr() {
|
| 63 |
+
if (++counter.x)
|
| 64 |
+
return;
|
| 65 |
+
if (++counter.y)
|
| 66 |
+
return;
|
| 67 |
+
if (++counter.z)
|
| 68 |
+
return;
|
| 69 |
+
++counter.w;
|
| 70 |
+
}
|
| 71 |
+
__device__ unsigned int mulhilo32(unsigned int a, unsigned int b,
|
| 72 |
+
unsigned int *result_high) {
|
| 73 |
+
*result_high = __umulhi(a, b);
|
| 74 |
+
return a*b;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
__device__ inline uint4 single_round(uint4 ctr, uint2 key) {
|
| 78 |
+
unsigned int hi0;
|
| 79 |
+
unsigned int hi1;
|
| 80 |
+
unsigned int lo0 = mulhilo32(kPhiloxSA, ctr.x, &hi0);
|
| 81 |
+
unsigned int lo1 = mulhilo32(kPhiloxSB, ctr.z, &hi1);
|
| 82 |
+
|
| 83 |
+
uint4 ret = {hi1 ^ ctr.y ^ key.x, lo1, hi0 ^ ctr.w ^ key.y, lo0};
|
| 84 |
+
return ret;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
static const unsigned long kPhilox10A = 0x9E3779B9;
|
| 88 |
+
static const unsigned long kPhilox10B = 0xBB67AE85;
|
| 89 |
+
static const unsigned long kPhiloxSA = 0xD2511F53;
|
| 90 |
+
static const unsigned long kPhiloxSB = 0xCD9E8D57;
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
// Inverse of 2^32.
|
| 94 |
+
#define M_RAN_INVM32 2.3283064e-10f
|
| 95 |
+
__device__ __inline__ float Uint32ToFloat(unsigned int x) {
|
| 96 |
+
return x * M_RAN_INVM32;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
)";
|
| 100 |
+
|
| 101 |
+
} // namespace torch::jit::tensorexpr
|
| 102 |
+
|
| 103 |
+
#else
|
| 104 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 105 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/eval.h
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <cmath>
|
| 5 |
+
#include <cstring>
|
| 6 |
+
#include <utility>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
#include <c10/macros/Macros.h>
|
| 10 |
+
#include <c10/util/Logging.h>
|
| 11 |
+
#include <torch/csrc/jit/tensorexpr/codegen.h>
|
| 12 |
+
#include <torch/csrc/jit/tensorexpr/exceptions.h>
|
| 13 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 14 |
+
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
|
| 15 |
+
#include <torch/csrc/jit/tensorexpr/tensor.h>
|
| 16 |
+
#include <torch/csrc/jit/tensorexpr/types.h>
|
| 17 |
+
#include <torch/csrc/jit/tensorexpr/var_substitutor.h>
|
| 18 |
+
|
| 19 |
+
namespace torch::jit::tensorexpr {
|
| 20 |
+
|
| 21 |
+
class InterpValue {
|
| 22 |
+
public:
|
| 23 |
+
InterpValue() : dtype_(kInt) {
|
| 24 |
+
Intvalues.push_back(0);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
template <typename T>
|
| 28 |
+
InterpValue(Dtype dtype, T v) : dtype_(dtype) {
|
| 29 |
+
#define TYPE_CASE(Type, Name) \
|
| 30 |
+
if (dtype == k##Name) { \
|
| 31 |
+
Name##values.push_back(v); \
|
| 32 |
+
return; \
|
| 33 |
+
}
|
| 34 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE)
|
| 35 |
+
#undef TYPE_CASE
|
| 36 |
+
throw unsupported_dtype();
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
#define VALUE_CTOR(Type, Name) \
|
| 40 |
+
InterpValue(Type v) : dtype_(k##Name) { \
|
| 41 |
+
Name##values.push_back(v); \
|
| 42 |
+
}
|
| 43 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, VALUE_CTOR)
|
| 44 |
+
#undef VALUE_CTOR
|
| 45 |
+
|
| 46 |
+
explicit InterpValue(c10::quint8 v) : dtype_(kQUInt8) {
|
| 47 |
+
QUInt8values.emplace_back(v.val_);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
explicit InterpValue(c10::qint8 v) : dtype_(kQInt8) {
|
| 51 |
+
QInt8values.emplace_back(v.val_);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
#define VALUE_VEC_CTOR(Type, Name) \
|
| 55 |
+
InterpValue(const std::vector<Type>& v) \
|
| 56 |
+
: dtype_(Dtype(k##Name, v.size())), Name##values(v) {}
|
| 57 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, VALUE_VEC_CTOR)
|
| 58 |
+
VALUE_VEC_CTOR(c10::quint8, QUInt8)
|
| 59 |
+
VALUE_VEC_CTOR(c10::qint8, QInt8)
|
| 60 |
+
#undef VALUE_VEC_CTOR
|
| 61 |
+
|
| 62 |
+
template <typename T>
|
| 63 |
+
T as() const;
|
| 64 |
+
|
| 65 |
+
template <typename T>
|
| 66 |
+
const std::vector<T>& as_vec() const;
|
| 67 |
+
|
| 68 |
+
int64_t intValue() const;
|
| 69 |
+
|
| 70 |
+
Dtype dtype() const {
|
| 71 |
+
return dtype_;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
private:
|
| 75 |
+
Dtype dtype_;
|
| 76 |
+
|
| 77 |
+
#define VALUE_STORAGE(Type, Name) std::vector<Type> Name##values;
|
| 78 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, VALUE_STORAGE)
|
| 79 |
+
VALUE_STORAGE(c10::qint8, QInt8)
|
| 80 |
+
VALUE_STORAGE(c10::quint8, QUInt8)
|
| 81 |
+
#undef VALUE_STORAGE
|
| 82 |
+
void* ptr{nullptr};
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
#define VALUE_AS_DISPATCH(Type, Name) \
|
| 86 |
+
template <> \
|
| 87 |
+
inline Type InterpValue::as<Type>() const { \
|
| 88 |
+
if (dtype_ != k##Name) { \
|
| 89 |
+
throw unsupported_dtype(); \
|
| 90 |
+
} \
|
| 91 |
+
return Name##values[0]; \
|
| 92 |
+
}
|
| 93 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, VALUE_AS_DISPATCH)
|
| 94 |
+
VALUE_AS_DISPATCH(c10::quint8, QUInt8)
|
| 95 |
+
VALUE_AS_DISPATCH(c10::qint8, QInt8)
|
| 96 |
+
#undef VALUE_AS_DISPATCH
|
| 97 |
+
|
| 98 |
+
#define VALUE_AS_VEC_DISPATCH(Type, Name) \
|
| 99 |
+
template <> \
|
| 100 |
+
inline const std::vector<Type>& InterpValue::as_vec<Type>() const { \
|
| 101 |
+
if (dtype_.scalar_type() != ScalarType::Name) { \
|
| 102 |
+
throw unsupported_dtype(); \
|
| 103 |
+
} \
|
| 104 |
+
return Name##values; \
|
| 105 |
+
}
|
| 106 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, VALUE_AS_VEC_DISPATCH)
|
| 107 |
+
VALUE_AS_VEC_DISPATCH(c10::quint8, QUInt8)
|
| 108 |
+
VALUE_AS_VEC_DISPATCH(c10::qint8, QInt8)
|
| 109 |
+
#undef VALUE_AS_VEC_DISPATCH
|
| 110 |
+
|
| 111 |
+
template <typename Type>
|
| 112 |
+
auto underlyingValue(Type x) {
|
| 113 |
+
return x;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
template <>
|
| 117 |
+
inline auto underlyingValue<c10::quint8>(c10::quint8 x) {
|
| 118 |
+
return x.val_;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
template <>
|
| 122 |
+
inline auto underlyingValue<c10::qint8>(c10::qint8 x) {
|
| 123 |
+
return x.val_;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
template <typename To, typename From>
|
| 127 |
+
To raw_bitcast(const From& src) {
|
| 128 |
+
TORCH_CHECK(sizeof(To) == sizeof(From), "Invalid bitcast invocation");
|
| 129 |
+
To storage;
|
| 130 |
+
std::memcpy(&storage, &src, sizeof(To));
|
| 131 |
+
return storage;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
class SimpleIREvaluatorImpl;
|
| 135 |
+
class TORCH_API SimpleIREvaluator : public CodeGen {
|
| 136 |
+
public:
|
| 137 |
+
SimpleIREvaluator(
|
| 138 |
+
StmtPtr stmt,
|
| 139 |
+
const std::vector<BufferArg>& buffer_args,
|
| 140 |
+
at::Device device = at::kCPU,
|
| 141 |
+
const std::string& kernel_func_name = "func");
|
| 142 |
+
|
| 143 |
+
~SimpleIREvaluator() override;
|
| 144 |
+
|
| 145 |
+
void call(const std::vector<CallArg>& args) override;
|
| 146 |
+
void call_raw(const std::vector<void*>& args) override;
|
| 147 |
+
|
| 148 |
+
template <typename... Ts>
|
| 149 |
+
void operator()(const Ts&... ts) {
|
| 150 |
+
std::vector<CallArg> args({CallArg(ts)...});
|
| 151 |
+
call(args);
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
void bindVar(const VarPtr& v, const ExprPtr& e);
|
| 155 |
+
InterpValue value() const;
|
| 156 |
+
|
| 157 |
+
private:
|
| 158 |
+
void bindArg(const BufferArg& buf, void* data);
|
| 159 |
+
void expand_intrinsics() {
|
| 160 |
+
GenericIntrinsicsExpander intrinsics_expander;
|
| 161 |
+
apply_mutator(&intrinsics_expander);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
std::unique_ptr<SimpleIREvaluatorImpl> impl_;
|
| 165 |
+
};
|
| 166 |
+
|
| 167 |
+
template <class CodeGenType>
|
| 168 |
+
class ExprEval {
|
| 169 |
+
public:
|
| 170 |
+
using BufferArg = CodeGen::BufferArg;
|
| 171 |
+
using CallArg = CodeGen::CallArg;
|
| 172 |
+
|
| 173 |
+
template <typename... Ts>
|
| 174 |
+
ExprEval(const ExprHandle& expr, Ts... ts)
|
| 175 |
+
: ExprEval(expr, {BufferArg(ts)...}) {}
|
| 176 |
+
|
| 177 |
+
ExprEval(const ExprHandle& expr, const std::vector<BufferArg>& buffer_args)
|
| 178 |
+
: dtype_(expr.dtype()) {
|
| 179 |
+
std::vector<BufferArg> buffer_args_extended = buffer_args;
|
| 180 |
+
BufHandle ret_buf("ret_val", {1}, dtype_);
|
| 181 |
+
std::vector<ExprHandle> indices;
|
| 182 |
+
ExprHandle zero = IntImm::make(0);
|
| 183 |
+
indices.reserve(ret_buf.ndim());
|
| 184 |
+
for (size_t i = 0; i < ret_buf.ndim(); i++) {
|
| 185 |
+
indices.push_back(zero);
|
| 186 |
+
}
|
| 187 |
+
StmtPtr store_stmt = Store::make(ret_buf, indices, expr);
|
| 188 |
+
buffer_args_extended.emplace_back(ret_buf);
|
| 189 |
+
codegen_.reset(new CodeGenType(store_stmt, buffer_args_extended));
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
template <typename... Ts>
|
| 193 |
+
void operator()(Ts... ts) {
|
| 194 |
+
call(ts...);
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
void operator()(const std::vector<CallArg>& call_args) {
|
| 198 |
+
call(call_args);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
void bindVar(VarPtr v, ExprPtr e) {
|
| 202 |
+
codegen_->bindVar(v, e);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
void bindVar(const VarHandle& v, const ExprHandle& e) {
|
| 206 |
+
codegen_->bindVar(v.node(), e.node());
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
template <typename... Ts>
|
| 210 |
+
void call(Ts... ts) {
|
| 211 |
+
call({CallArg(ts)...});
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
void call(const std::vector<CallArg>& call_args) {
|
| 215 |
+
std::vector<CallArg> call_args_extended = call_args;
|
| 216 |
+
switch (dtype_.scalar_type()) {
|
| 217 |
+
#define TYPE_CASE(Type, Name) \
|
| 218 |
+
case ScalarType::Name: { \
|
| 219 |
+
std::vector<Type> ret_val_arg(1); \
|
| 220 |
+
call_args_extended.emplace_back(ret_val_arg); \
|
| 221 |
+
codegen_->call(call_args_extended); \
|
| 222 |
+
ret_value_ = InterpValue(ret_val_arg[0]); \
|
| 223 |
+
} break;
|
| 224 |
+
AT_FORALL_SCALAR_TYPES_AND2(Half, BFloat16, TYPE_CASE);
|
| 225 |
+
TYPE_CASE(c10::quint8, QUInt8);
|
| 226 |
+
TYPE_CASE(c10::qint8, QInt8);
|
| 227 |
+
#undef TYPE_CASE
|
| 228 |
+
case ScalarType::Bool: {
|
| 229 |
+
std::vector<unsigned char> ret_val_arg(1);
|
| 230 |
+
call_args_extended.emplace_back(ret_val_arg.data());
|
| 231 |
+
codegen_->call(call_args_extended);
|
| 232 |
+
ret_value_ = InterpValue((bool)ret_val_arg[0]);
|
| 233 |
+
} break;
|
| 234 |
+
default:
|
| 235 |
+
throw unsupported_dtype();
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
void call_raw(const std::vector<void*>& args) {
|
| 240 |
+
std::vector<void*> args_extended = args;
|
| 241 |
+
switch (dtype_.scalar_type()) {
|
| 242 |
+
#define TYPE_CASE(Type, Name) \
|
| 243 |
+
case ScalarType::Name: { \
|
| 244 |
+
std::vector<Type> ret_val_arg(1); \
|
| 245 |
+
args_extended.push_back(ret_val_arg.data()); \
|
| 246 |
+
codegen_->call_raw(args_extended); \
|
| 247 |
+
ret_value_ = InterpValue(ret_val_arg[0]); \
|
| 248 |
+
} break;
|
| 249 |
+
AT_FORALL_SCALAR_TYPES_AND2(Half, BFloat16, TYPE_CASE);
|
| 250 |
+
TYPE_CASE(c10::quint8, QUInt8);
|
| 251 |
+
TYPE_CASE(c10::qint8, QInt8);
|
| 252 |
+
#undef TYPE_CASE
|
| 253 |
+
case ScalarType::Bool: {
|
| 254 |
+
std::vector<unsigned char> ret_val_arg(1);
|
| 255 |
+
args_extended.push_back(ret_val_arg.data());
|
| 256 |
+
codegen_->call_raw(args_extended);
|
| 257 |
+
ret_value_ = InterpValue((bool)ret_val_arg[0]);
|
| 258 |
+
} break;
|
| 259 |
+
default:
|
| 260 |
+
throw unsupported_dtype();
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
template <typename T>
|
| 265 |
+
T value(const std::vector<void*>& args) {
|
| 266 |
+
call_raw(args);
|
| 267 |
+
return ret_value_.as<T>();
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
template <typename T, typename... Ts>
|
| 271 |
+
T value(Ts... ts) {
|
| 272 |
+
call(std::forward<Ts>(ts)...);
|
| 273 |
+
return ret_value_.as<T>();
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
Dtype dtype() {
|
| 277 |
+
return dtype_;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
private:
|
| 281 |
+
Dtype dtype_;
|
| 282 |
+
std::unique_ptr<CodeGenType> codegen_;
|
| 283 |
+
InterpValue ret_value_;
|
| 284 |
+
};
|
| 285 |
+
|
| 286 |
+
// Evaluates the given expression and returns an int64_t value if the result of
|
| 287 |
+
// the given expression is int64_t.
|
| 288 |
+
std::optional<int64_t> evalInt(ExprPtr e);
|
| 289 |
+
|
| 290 |
+
// Substitutes the given vars with their corresponding expressions in the input
|
| 291 |
+
// expression.
|
| 292 |
+
inline ExprPtr Substitute(const ExprPtr& expr, const VarMapping& var_mapping) {
|
| 293 |
+
VarSubMutator var_sub(var_mapping);
|
| 294 |
+
return expr->accept_mutator(&var_sub);
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// Substitutes the given vars with their corresponding expressions in the input
|
| 298 |
+
// statement.
|
| 299 |
+
inline StmtPtr Substitute(const StmtPtr& stmt, const VarMapping& var_mapping) {
|
| 300 |
+
VarSubMutator var_sub(var_mapping);
|
| 301 |
+
return stmt->accept_mutator(&var_sub);
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// Creates a clone of the input expression and substitutes the given vars with
|
| 305 |
+
// their corresponding expressions in the clone.
|
| 306 |
+
// NOTE: This works because cloning reuses variables and does not create new
|
| 307 |
+
// ones, and `VarMapping` input has variables as the key.
|
| 308 |
+
inline ExprPtr SubstituteInClone(
|
| 309 |
+
const ExprPtr& expr,
|
| 310 |
+
const VarMapping& var_mapping) {
|
| 311 |
+
VarSubMutator var_sub(var_mapping);
|
| 312 |
+
return Expr::clone(expr)->accept_mutator(&var_sub);
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
// Creates a clone of the input statement and substitutes the given vars with
|
| 316 |
+
// their corresponding expressions in the clone.
|
| 317 |
+
// NOTE: This works because cloning reuses variables and does not create new
|
| 318 |
+
// ones, and `VarMapping` input has variables as the key.
|
| 319 |
+
inline StmtPtr SubstituteInClone(
|
| 320 |
+
const StmtPtr& stmt,
|
| 321 |
+
const VarMapping& var_mapping) {
|
| 322 |
+
VarSubMutator var_sub(var_mapping);
|
| 323 |
+
return Stmt::clone(stmt)->accept_mutator(&var_sub);
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
} // namespace torch::jit::tensorexpr
|
| 327 |
+
|
| 328 |
+
#else
|
| 329 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 330 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/exceptions.h
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
|
| 6 |
+
|
| 7 |
+
#include <stdexcept>
|
| 8 |
+
|
| 9 |
+
// Forward declarations of types
|
| 10 |
+
|
| 11 |
+
namespace torch::jit::tensorexpr {
|
| 12 |
+
class Expr;
|
| 13 |
+
class Stmt;
|
| 14 |
+
} // namespace torch::jit::tensorexpr
|
| 15 |
+
|
| 16 |
+
// Forward declarations of functions
|
| 17 |
+
namespace std {
|
| 18 |
+
TORCH_API std::string to_string(
|
| 19 |
+
const torch::jit::tensorexpr::ExprPtr& /*expr*/);
|
| 20 |
+
TORCH_API std::string to_string(
|
| 21 |
+
const torch::jit::tensorexpr::StmtPtr& /*stmt*/);
|
| 22 |
+
} // namespace std
|
| 23 |
+
|
| 24 |
+
namespace torch::jit::tensorexpr {
|
| 25 |
+
|
| 26 |
+
class unsupported_dtype : public std::runtime_error {
|
| 27 |
+
public:
|
| 28 |
+
explicit unsupported_dtype() : std::runtime_error("UNSUPPORTED DTYPE") {}
|
| 29 |
+
explicit unsupported_dtype(const std::string& err)
|
| 30 |
+
: std::runtime_error("UNSUPPORTED DTYPE: " + err) {}
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
class out_of_range_index : public std::runtime_error {
|
| 34 |
+
public:
|
| 35 |
+
explicit out_of_range_index() : std::runtime_error("OUT OF RANGE INDEX") {}
|
| 36 |
+
explicit out_of_range_index(const std::string& err)
|
| 37 |
+
: std::runtime_error("OUT OF RANGE INDEX: " + err) {}
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
class unimplemented_lowering : public std::runtime_error {
|
| 41 |
+
public:
|
| 42 |
+
explicit unimplemented_lowering()
|
| 43 |
+
: std::runtime_error("UNIMPLEMENTED LOWERING") {}
|
| 44 |
+
explicit unimplemented_lowering(const ExprPtr& expr)
|
| 45 |
+
: std::runtime_error("UNIMPLEMENTED LOWERING: " + std::to_string(expr)) {}
|
| 46 |
+
explicit unimplemented_lowering(const StmtPtr& stmt)
|
| 47 |
+
: std::runtime_error("UNIMPLEMENTED LOWERING: " + std::to_string(stmt)) {}
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
class malformed_input : public std::runtime_error {
|
| 51 |
+
public:
|
| 52 |
+
explicit malformed_input() : std::runtime_error("MALFORMED INPUT") {}
|
| 53 |
+
explicit malformed_input(const std::string& err)
|
| 54 |
+
: std::runtime_error("MALFORMED INPUT: " + err) {}
|
| 55 |
+
explicit malformed_input(const ExprPtr& expr)
|
| 56 |
+
: std::runtime_error("MALFORMED INPUT: " + std::to_string(expr)) {}
|
| 57 |
+
explicit malformed_input(const std::string& err, const ExprPtr& expr)
|
| 58 |
+
: std::runtime_error(
|
| 59 |
+
"MALFORMED INPUT: " + err + " - " + std::to_string(expr)) {}
|
| 60 |
+
explicit malformed_input(const StmtPtr& stmt)
|
| 61 |
+
: std::runtime_error("MALFORMED INPUT: " + std::to_string(stmt)) {}
|
| 62 |
+
explicit malformed_input(const std::string& err, const StmtPtr& stmt)
|
| 63 |
+
: std::runtime_error(
|
| 64 |
+
"MALFORMED INPUT: " + err + " - " + std::to_string(stmt)) {}
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
class malformed_ir : public std::runtime_error {
|
| 68 |
+
public:
|
| 69 |
+
explicit malformed_ir() : std::runtime_error("MALFORMED IR") {}
|
| 70 |
+
explicit malformed_ir(const std::string& err)
|
| 71 |
+
: std::runtime_error("MALFORMED IR: " + err) {}
|
| 72 |
+
explicit malformed_ir(const ExprPtr& expr)
|
| 73 |
+
: std::runtime_error("MALFORMED IR: " + std::to_string(expr)) {}
|
| 74 |
+
explicit malformed_ir(const std::string& err, const ExprPtr& expr)
|
| 75 |
+
: std::runtime_error(
|
| 76 |
+
"MALFORMED IR: " + err + " - " + std::to_string(expr)) {}
|
| 77 |
+
explicit malformed_ir(const StmtPtr& stmt)
|
| 78 |
+
: std::runtime_error("MALFORMED IR: " + std::to_string(stmt)) {}
|
| 79 |
+
explicit malformed_ir(const std::string& err, const StmtPtr& stmt)
|
| 80 |
+
: std::runtime_error(
|
| 81 |
+
"MALFORMED IR: " + err + " - " + std::to_string(stmt)) {}
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
TORCH_API std::string buildErrorMessage(const std::string& s = "");
|
| 85 |
+
|
| 86 |
+
} // namespace torch::jit::tensorexpr
|
| 87 |
+
|
| 88 |
+
#else
|
| 89 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 90 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/expr.h
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
/**
|
| 3 |
+
* This file implements the core classes for Tensor Expressions.
|
| 4 |
+
*
|
| 5 |
+
* The structure of the expressions is inspired by Halide/TVM IR.
|
| 6 |
+
*/
|
| 7 |
+
#pragma once
|
| 8 |
+
|
| 9 |
+
#include <c10/core/MemoryFormat.h>
|
| 10 |
+
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
|
| 11 |
+
#include <torch/csrc/jit/tensorexpr/ir_mutator.h>
|
| 12 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 13 |
+
#include <torch/csrc/jit/tensorexpr/types.h>
|
| 14 |
+
#include <optional>
|
| 15 |
+
|
| 16 |
+
#include <utility>
|
| 17 |
+
|
| 18 |
+
namespace torch::jit::tensorexpr {
|
| 19 |
+
|
| 20 |
+
enum IRNodeType {
|
| 21 |
+
kPrimitive,
|
| 22 |
+
kAdd,
|
| 23 |
+
kSub,
|
| 24 |
+
kMul,
|
| 25 |
+
kDiv,
|
| 26 |
+
kMod,
|
| 27 |
+
kMax,
|
| 28 |
+
kMin,
|
| 29 |
+
kAnd,
|
| 30 |
+
kOr,
|
| 31 |
+
kLshift,
|
| 32 |
+
kRshift,
|
| 33 |
+
kXor,
|
| 34 |
+
kCompareSelect,
|
| 35 |
+
kCast,
|
| 36 |
+
kBitCast,
|
| 37 |
+
kOther,
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
// The common base between all expression node.
|
| 41 |
+
class TORCH_API Expr : public std::enable_shared_from_this<Expr> {
|
| 42 |
+
public:
|
| 43 |
+
explicit Expr(Dtype dtype, IRNodeType expr_type = kOther)
|
| 44 |
+
: dtype_(dtype), expr_type_(expr_type) {}
|
| 45 |
+
virtual ~Expr() = default;
|
| 46 |
+
Dtype dtype() const {
|
| 47 |
+
return dtype_;
|
| 48 |
+
}
|
| 49 |
+
virtual void accept(IRVisitor* visitor) = 0;
|
| 50 |
+
virtual ExprPtr accept_mutator(IRMutator* mutator) = 0;
|
| 51 |
+
|
| 52 |
+
IRNodeType expr_type() const {
|
| 53 |
+
return expr_type_;
|
| 54 |
+
}
|
| 55 |
+
// Is this a fixed (constant) immediate value.
|
| 56 |
+
virtual bool isConstant() const {
|
| 57 |
+
return false;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
void set_dtype(Dtype dtype) {
|
| 61 |
+
dtype_ = dtype;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/*
|
| 65 |
+
* Make a deep copy of the given expression.
|
| 66 |
+
*
|
| 67 |
+
* All sub-expressions inside the given expressions are also cloned. Note
|
| 68 |
+
* that the variables are not deep-copied since they are immutable.
|
| 69 |
+
*/
|
| 70 |
+
static ExprPtr clone(const ExprPtr& s);
|
| 71 |
+
|
| 72 |
+
protected:
|
| 73 |
+
std::shared_ptr<Expr> getptr() {
|
| 74 |
+
return shared_from_this();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
private:
|
| 78 |
+
Dtype dtype_;
|
| 79 |
+
IRNodeType expr_type_;
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
// A CRTP pattern to accept visitors for children class,
|
| 83 |
+
// and dispatch back to the children.
|
| 84 |
+
template <class Op, class Base = Expr>
|
| 85 |
+
class ExprNode : public Base {
|
| 86 |
+
public:
|
| 87 |
+
using ExprNodeBase = ExprNode<Op>;
|
| 88 |
+
void accept(IRVisitor* visitor) override {
|
| 89 |
+
visitor->visit(static_to<Op>(Base::getptr()));
|
| 90 |
+
}
|
| 91 |
+
ExprPtr accept_mutator(IRMutator* mutator) override;
|
| 92 |
+
// pass the constructor to the base class
|
| 93 |
+
using Base::Base;
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
// A wrapper object to the underlying ExprNode.
|
| 97 |
+
// Also serves the primary way to build and operate on other expressions.
|
| 98 |
+
class TORCH_API ExprHandle {
|
| 99 |
+
public:
|
| 100 |
+
ExprHandle() = default;
|
| 101 |
+
explicit ExprHandle(ExprPtr node) : base_expr_node_(std::move(node)) {}
|
| 102 |
+
|
| 103 |
+
ExprPtr node() {
|
| 104 |
+
return base_expr_node_;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
ExprPtr node() const {
|
| 108 |
+
return base_expr_node_;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
bool empty() const {
|
| 112 |
+
return base_expr_node_ == nullptr;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
#define IMM_EXPR_DECLARE(Type, Name) ExprHandle(Type v);
|
| 116 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_EXPR_DECLARE)
|
| 117 |
+
#undef IMM_EXPR_DECLARE
|
| 118 |
+
|
| 119 |
+
template <class Op>
|
| 120 |
+
NodePtr<Op> AsNode() {
|
| 121 |
+
return to<Op>(this->node());
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
template <class Op>
|
| 125 |
+
NodePtr<Op> AsNode() const {
|
| 126 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
| 127 |
+
return const_cast<ExprHandle*>(this)->AsNode<Op>();
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
Dtype dtype() const {
|
| 131 |
+
return node()->dtype();
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// Handling the math operators.
|
| 135 |
+
ExprHandle operator+(const ExprHandle& other) const;
|
| 136 |
+
ExprHandle operator-(const ExprHandle& other) const;
|
| 137 |
+
ExprHandle operator*(const ExprHandle& other) const;
|
| 138 |
+
ExprHandle operator/(const ExprHandle& other) const;
|
| 139 |
+
ExprHandle operator%(const ExprHandle& other) const;
|
| 140 |
+
ExprHandle operator==(const ExprHandle& other) const;
|
| 141 |
+
ExprHandle operator!=(const ExprHandle& other) const;
|
| 142 |
+
ExprHandle operator>(const ExprHandle& other) const;
|
| 143 |
+
ExprHandle operator>=(const ExprHandle& other) const;
|
| 144 |
+
ExprHandle operator<(const ExprHandle& other) const;
|
| 145 |
+
ExprHandle operator<=(const ExprHandle& other) const;
|
| 146 |
+
ExprHandle operator&(const ExprHandle& other) const;
|
| 147 |
+
ExprHandle operator|(const ExprHandle& other) const;
|
| 148 |
+
ExprHandle operator&&(const ExprHandle& other) const;
|
| 149 |
+
ExprHandle operator||(const ExprHandle& other) const;
|
| 150 |
+
ExprHandle operator^(const ExprHandle& other) const;
|
| 151 |
+
ExprHandle operator<<(const ExprHandle& other) const;
|
| 152 |
+
ExprHandle operator>>(const ExprHandle& other) const;
|
| 153 |
+
|
| 154 |
+
private:
|
| 155 |
+
ExprPtr base_expr_node_ = nullptr;
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
// The underlying representation node to a Var.
|
| 159 |
+
// Currently, each Var object represents a unique variable, even though the
|
| 160 |
+
// names might be the same. We should consider add a unique_name as well.
|
| 161 |
+
class TORCH_API Var : public ExprNode<Var> {
|
| 162 |
+
public:
|
| 163 |
+
static ExprHandle make(const std::string& name_hint, Dtype dtype) {
|
| 164 |
+
return ExprHandle(alloc<Var>(name_hint, dtype));
|
| 165 |
+
}
|
| 166 |
+
static ExprHandle make(Dtype dtype) {
|
| 167 |
+
return ExprHandle(alloc<Var>("", dtype));
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// TODO: unique_name
|
| 171 |
+
const std::string& name_hint() const {
|
| 172 |
+
return name_hint_;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
void set_name_hint(const std::string& name) {
|
| 176 |
+
name_hint_ = name;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
void set_name_hint(std::string&& name) {
|
| 180 |
+
name_hint_ = std::move(name);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
Var(std::string name_hint, Dtype dtype)
|
| 184 |
+
: ExprNodeBase(dtype, kPrimitive), name_hint_(std::move(name_hint)) {}
|
| 185 |
+
|
| 186 |
+
private:
|
| 187 |
+
std::string name_hint_;
|
| 188 |
+
};
|
| 189 |
+
|
| 190 |
+
TORCH_API std::vector<ExprPtr> make_contiguous_strides(
|
| 191 |
+
const std::vector<ExprHandle>& dims);
|
| 192 |
+
TORCH_API std::vector<ExprPtr> make_channels_last_strides(
|
| 193 |
+
const std::vector<ExprHandle>& dims);
|
| 194 |
+
|
| 195 |
+
class TORCH_API Buf : public ExprNode<Buf> {
|
| 196 |
+
public:
|
| 197 |
+
static BufHandle make(const std::vector<ExprHandle>& dims, Dtype dtype);
|
| 198 |
+
|
| 199 |
+
static BufHandle make(
|
| 200 |
+
const std::string& name_hint,
|
| 201 |
+
const std::vector<ExprHandle>& dims,
|
| 202 |
+
const std::vector<ExprHandle>& strides,
|
| 203 |
+
Dtype dtype);
|
| 204 |
+
|
| 205 |
+
static BufHandle make(
|
| 206 |
+
const std::string& name_hint,
|
| 207 |
+
const std::vector<ExprHandle>& dims,
|
| 208 |
+
Dtype dtype,
|
| 209 |
+
std::optional<ExprHandle> initializer = std::nullopt,
|
| 210 |
+
const std::optional<std::vector<ExprHandle>>& strides = std::nullopt,
|
| 211 |
+
std::optional<ExprHandle> qscale = std::nullopt,
|
| 212 |
+
std::optional<ExprHandle> qzero = std::nullopt);
|
| 213 |
+
|
| 214 |
+
// TODO: unique_name
|
| 215 |
+
VarPtr base_handle() const {
|
| 216 |
+
return base_handle_;
|
| 217 |
+
}
|
| 218 |
+
void set_base_handle(VarPtr base_handle) {
|
| 219 |
+
base_handle_ = std::move(base_handle);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
const std::string& name_hint() const {
|
| 223 |
+
return base_handle_->name_hint();
|
| 224 |
+
}
|
| 225 |
+
void set_name_hint(const std::string& name_hint) {
|
| 226 |
+
base_handle_->set_name_hint(name_hint);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
Buf(const std::string& name_hint,
|
| 230 |
+
const std::vector<ExprPtr>& dims,
|
| 231 |
+
Dtype dtype,
|
| 232 |
+
ExprPtr initializer = nullptr,
|
| 233 |
+
std::optional<std::vector<ExprPtr>> strides = std::nullopt,
|
| 234 |
+
ExprPtr qscale = nullptr,
|
| 235 |
+
ExprPtr qzero = nullptr)
|
| 236 |
+
: Buf(alloc<Var>(name_hint, kHandle),
|
| 237 |
+
dims,
|
| 238 |
+
dtype,
|
| 239 |
+
std::move(initializer),
|
| 240 |
+
std::move(strides),
|
| 241 |
+
std::move(qscale),
|
| 242 |
+
std::move(qzero)) {}
|
| 243 |
+
|
| 244 |
+
Buf(const VarPtr& var,
|
| 245 |
+
std::vector<ExprPtr> dims,
|
| 246 |
+
Dtype dtype,
|
| 247 |
+
ExprPtr initializer = nullptr,
|
| 248 |
+
std::optional<std::vector<ExprPtr>> strides = std::nullopt,
|
| 249 |
+
ExprPtr qscale = nullptr,
|
| 250 |
+
ExprPtr qzero = nullptr);
|
| 251 |
+
|
| 252 |
+
size_t ndim() const {
|
| 253 |
+
return dims_.size();
|
| 254 |
+
}
|
| 255 |
+
ExprPtr dim(size_t index) const {
|
| 256 |
+
if (index >= ndim()) {
|
| 257 |
+
throw out_of_range_index();
|
| 258 |
+
}
|
| 259 |
+
return dims_[index];
|
| 260 |
+
}
|
| 261 |
+
std::vector<ExprPtr> dims() const {
|
| 262 |
+
return dims_;
|
| 263 |
+
}
|
| 264 |
+
void set_dims(std::vector<ExprPtr> dims) {
|
| 265 |
+
dims_ = std::move(dims);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
std::vector<ExprPtr> strides() const {
|
| 269 |
+
return strides_;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
void set_strides(std::vector<ExprPtr> strides) {
|
| 273 |
+
strides_ = std::move(strides);
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
ExprPtr initializer() const {
|
| 277 |
+
return initializer_;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
ExprPtr qzero() const {
|
| 281 |
+
return qzero_;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
ExprPtr qscale() const {
|
| 285 |
+
return qscale_;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
void set_qzero(ExprPtr qzero) {
|
| 289 |
+
qzero_ = std::move(qzero);
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
void set_qscale(ExprPtr qscale) {
|
| 293 |
+
qscale_ = std::move(qscale);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
bool hasConstantDims() const {
|
| 297 |
+
for (const auto& d : dims_) {
|
| 298 |
+
if (!d->isConstant()) {
|
| 299 |
+
return false;
|
| 300 |
+
}
|
| 301 |
+
}
|
| 302 |
+
return true;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
bool is_contiguous(
|
| 306 |
+
at::MemoryFormat memory_format = at::MemoryFormat::Contiguous) const;
|
| 307 |
+
|
| 308 |
+
// The channels-last 1d can benefit the performance of some operators like
|
| 309 |
+
// conv1d. But the MemoryFormat enum has not covered this layout yet. Hence,
|
| 310 |
+
// we abstract a dedicated function to check channels-last 1d contiguous.
|
| 311 |
+
//
|
| 312 |
+
// Channels-last 1d:
|
| 313 |
+
// dims: n c l
|
| 314 |
+
// strides(nlc): c*l 1 c
|
| 315 |
+
bool is_channels_last_1d_contiguous() const {
|
| 316 |
+
if (dims_.size() != 3) {
|
| 317 |
+
return false;
|
| 318 |
+
}
|
| 319 |
+
return is_stride_one(1) && is_cont_with(2, 1) && is_cont_with(0, 2);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
private:
|
| 323 |
+
bool is_cont_with(int cur_dim, int adjacent_dim) const;
|
| 324 |
+
bool is_stride_one(int cur_dim) const;
|
| 325 |
+
|
| 326 |
+
VarPtr base_handle_;
|
| 327 |
+
std::vector<ExprPtr> dims_;
|
| 328 |
+
std::vector<ExprPtr> strides_;
|
| 329 |
+
ExprPtr initializer_;
|
| 330 |
+
// qscale_ and qzero_ are used only for quantized dtypes Bufs: kQUInt8, kQInt8
|
| 331 |
+
ExprPtr qscale_;
|
| 332 |
+
ExprPtr qzero_;
|
| 333 |
+
};
|
| 334 |
+
|
| 335 |
+
class TORCH_API BufHandle : public ExprHandle {
|
| 336 |
+
public:
|
| 337 |
+
BufHandle(
|
| 338 |
+
const std::string& name_hint,
|
| 339 |
+
const std::vector<ExprHandle>& dims,
|
| 340 |
+
Dtype dtype)
|
| 341 |
+
: ExprHandle(Buf::make(name_hint, dims, dtype)) {}
|
| 342 |
+
|
| 343 |
+
BufHandle(
|
| 344 |
+
const std::string& name_hint,
|
| 345 |
+
const std::vector<ExprHandle>& dims,
|
| 346 |
+
const std::vector<ExprHandle>& strides,
|
| 347 |
+
Dtype dtype)
|
| 348 |
+
: ExprHandle(Buf::make(name_hint, dims, strides, dtype)) {}
|
| 349 |
+
|
| 350 |
+
BufHandle(const std::vector<ExprHandle>& dims, Dtype dtype)
|
| 351 |
+
: ExprHandle(Buf::make("_", dims, dtype)) {}
|
| 352 |
+
|
| 353 |
+
explicit BufHandle(Dtype dtype) : ExprHandle(Buf::make("_", {}, dtype)) {}
|
| 354 |
+
|
| 355 |
+
explicit BufHandle(BufPtr node) : ExprHandle(std::move(node)) {}
|
| 356 |
+
BufPtr node() const {
|
| 357 |
+
return static_to<Buf>(ExprHandle::node());
|
| 358 |
+
}
|
| 359 |
+
BufPtr node() {
|
| 360 |
+
return static_to<Buf>(ExprHandle::node());
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
template <typename... Ts>
|
| 364 |
+
inline ExprHandle load(const Ts&... ts) const;
|
| 365 |
+
|
| 366 |
+
template <typename T>
|
| 367 |
+
inline ExprHandle load(const std::vector<T>& args) const;
|
| 368 |
+
|
| 369 |
+
inline ExprHandle load(const std::vector<ExprHandle>& args) const;
|
| 370 |
+
|
| 371 |
+
StorePtr store(const std::vector<ExprHandle>& args, const ExprHandle& val)
|
| 372 |
+
const;
|
| 373 |
+
|
| 374 |
+
bool operator==(const BufHandle& other) const {
|
| 375 |
+
return this->node() == other.node();
|
| 376 |
+
}
|
| 377 |
+
bool operator!=(const BufHandle& other) const {
|
| 378 |
+
return !(*this == other);
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
const std::string& name_hint() const {
|
| 382 |
+
return this->node()->name_hint();
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
bool empty() const {
|
| 386 |
+
return (this->node() == nullptr);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
size_t ndim() const {
|
| 390 |
+
return node()->ndim();
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
std::vector<ExprHandle> dims() const;
|
| 394 |
+
|
| 395 |
+
ExprHandle dim(size_t index) const {
|
| 396 |
+
return ExprHandle(node()->dim(index));
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
bool is_contiguous(
|
| 400 |
+
at::MemoryFormat memory_format = at::MemoryFormat::Contiguous) const {
|
| 401 |
+
return node()->is_contiguous(memory_format);
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
bool is_channels_last_1d_contiguous() const {
|
| 405 |
+
return node()->is_channels_last_1d_contiguous();
|
| 406 |
+
}
|
| 407 |
+
};
|
| 408 |
+
|
| 409 |
+
// An expression to construct the underlying variable node.
|
| 410 |
+
// Note: do not store any info here, since it is often possible to slice this
|
| 411 |
+
// object. For example: VarHandle x('x'); ExprHandle x2 = x;
|
| 412 |
+
class TORCH_API VarHandle : public ExprHandle {
|
| 413 |
+
public:
|
| 414 |
+
// Creates an empty VarHandle whose base Var is set to nullptr.
|
| 415 |
+
VarHandle() = default;
|
| 416 |
+
|
| 417 |
+
explicit VarHandle(Dtype dtype) : ExprHandle(Var::make(dtype)) {}
|
| 418 |
+
|
| 419 |
+
VarHandle(const std::string& name_hint, Dtype dtype)
|
| 420 |
+
: ExprHandle(Var::make(name_hint, dtype)) {}
|
| 421 |
+
|
| 422 |
+
explicit VarHandle(VarPtr node) : ExprHandle(std::move(node)) {}
|
| 423 |
+
|
| 424 |
+
VarPtr node() const {
|
| 425 |
+
return static_to<Var>(ExprHandle::node());
|
| 426 |
+
}
|
| 427 |
+
bool operator==(const VarHandle& other) const {
|
| 428 |
+
return this->node() == other.node();
|
| 429 |
+
}
|
| 430 |
+
bool operator!=(const VarHandle& other) const {
|
| 431 |
+
return !(*this == other);
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
const std::string& name_hint() const {
|
| 435 |
+
return this->node()->name_hint();
|
| 436 |
+
}
|
| 437 |
+
bool empty() const {
|
| 438 |
+
return (this->node() == nullptr);
|
| 439 |
+
}
|
| 440 |
+
};
|
| 441 |
+
|
| 442 |
+
template <class Op, class Base>
|
| 443 |
+
ExprPtr ExprNode<Op, Base>::accept_mutator(IRMutator* mutator) {
|
| 444 |
+
return mutator->mutate(static_to<Op>(Base::getptr()));
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
inline bool same_node(const ExprHandle& expr1, const ExprHandle& expr2) {
|
| 448 |
+
return expr1.AsNode<Expr>() == expr2.AsNode<Expr>();
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
TORCH_API ExprHandle sin(const ExprHandle& v);
|
| 452 |
+
TORCH_API ExprHandle cos(const ExprHandle& v);
|
| 453 |
+
TORCH_API ExprHandle tan(const ExprHandle& v);
|
| 454 |
+
TORCH_API ExprHandle asin(const ExprHandle& v);
|
| 455 |
+
TORCH_API ExprHandle acos(const ExprHandle& v);
|
| 456 |
+
TORCH_API ExprHandle atan(const ExprHandle& v);
|
| 457 |
+
TORCH_API ExprHandle sinh(const ExprHandle& v);
|
| 458 |
+
TORCH_API ExprHandle cosh(const ExprHandle& v);
|
| 459 |
+
TORCH_API ExprHandle tanh(const ExprHandle& v);
|
| 460 |
+
TORCH_API ExprHandle sigmoid(const ExprHandle& v);
|
| 461 |
+
TORCH_API ExprHandle exp(const ExprHandle& v);
|
| 462 |
+
TORCH_API ExprHandle expm1(const ExprHandle& v);
|
| 463 |
+
TORCH_API ExprHandle abs(const ExprHandle& v);
|
| 464 |
+
TORCH_API ExprHandle log(const ExprHandle& v);
|
| 465 |
+
TORCH_API ExprHandle fast_tanh(const ExprHandle& v);
|
| 466 |
+
TORCH_API ExprHandle fast_sigmoid(const ExprHandle& v);
|
| 467 |
+
TORCH_API ExprHandle fast_log(const ExprHandle& v);
|
| 468 |
+
TORCH_API ExprHandle log_vml(const ExprHandle& v);
|
| 469 |
+
TORCH_API ExprHandle log2(const ExprHandle& v);
|
| 470 |
+
TORCH_API ExprHandle log10(const ExprHandle& v);
|
| 471 |
+
TORCH_API ExprHandle log1p(const ExprHandle& v);
|
| 472 |
+
TORCH_API ExprHandle erf(const ExprHandle& v);
|
| 473 |
+
TORCH_API ExprHandle erfc(const ExprHandle& v);
|
| 474 |
+
TORCH_API ExprHandle sqrt(const ExprHandle& v);
|
| 475 |
+
TORCH_API ExprHandle rsqrt(const ExprHandle& v);
|
| 476 |
+
TORCH_API ExprHandle ceil(const ExprHandle& v);
|
| 477 |
+
TORCH_API ExprHandle floor(const ExprHandle& v);
|
| 478 |
+
TORCH_API ExprHandle round(const ExprHandle& v);
|
| 479 |
+
TORCH_API ExprHandle trunc(const ExprHandle& v);
|
| 480 |
+
TORCH_API ExprHandle frac(const ExprHandle& v);
|
| 481 |
+
TORCH_API ExprHandle lgamma(const ExprHandle& v);
|
| 482 |
+
TORCH_API ExprHandle atan2(const ExprHandle& v1, const ExprHandle& v2);
|
| 483 |
+
TORCH_API ExprHandle pow(const ExprHandle& v1, const ExprHandle& v2);
|
| 484 |
+
TORCH_API ExprHandle fmod(const ExprHandle& v1, const ExprHandle& v2);
|
| 485 |
+
TORCH_API ExprHandle remainder(const ExprHandle& v1, const ExprHandle& v2);
|
| 486 |
+
TORCH_API ExprHandle isnan(const ExprHandle& v1);
|
| 487 |
+
TORCH_API ExprHandle Relu(const ExprHandle& v1);
|
| 488 |
+
|
| 489 |
+
TORCH_API ExprHandle
|
| 490 |
+
ifThenElse(const ExprHandle& c, const ExprHandle& t, const ExprHandle& f);
|
| 491 |
+
|
| 492 |
+
TORCH_API ExprHandle expr_to_vec(const ExprHandle& v, int lanes);
|
| 493 |
+
|
| 494 |
+
} // namespace torch::jit::tensorexpr
|
| 495 |
+
|
| 496 |
+
#else
|
| 497 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 498 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions.h
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/Config.h>
|
| 5 |
+
#include <ATen/Functions.h>
|
| 6 |
+
#include <c10/macros/Macros.h>
|
| 7 |
+
#include <torch/csrc/Export.h>
|
| 8 |
+
#include <cstdint>
|
| 9 |
+
#include <vector>
|
| 10 |
+
|
| 11 |
+
#define FOR_ALL_EXTERNAL_FUNCTIONS(_) \
|
| 12 |
+
_(nnc_aten_adaptive_avg_pool2d) \
|
| 13 |
+
_(nnc_aten_addmm) \
|
| 14 |
+
_(nnc_aten_conv2d) \
|
| 15 |
+
_(nnc_aten_conv1d) \
|
| 16 |
+
_(nnc_aten_conv1d_out) \
|
| 17 |
+
_(nnc_aten_dequantize) \
|
| 18 |
+
_(nnc_aten_dequantize_out) \
|
| 19 |
+
_(nnc_aten_embedding) \
|
| 20 |
+
_(nnc_aten_matmul) \
|
| 21 |
+
_(nnc_aten_mv) \
|
| 22 |
+
_(nnc_aten_mm) \
|
| 23 |
+
_(nnc_aten_mean) \
|
| 24 |
+
_(nnc_aten_max_red) \
|
| 25 |
+
_(nnc_aten_max_red_out) \
|
| 26 |
+
_(nnc_aten_quantized_conv1d) \
|
| 27 |
+
_(nnc_aten_quantized_conv1d_out) \
|
| 28 |
+
_(nnc_aten_quantized_conv2d) \
|
| 29 |
+
_(nnc_aten_quantized_conv2d_out) \
|
| 30 |
+
_(nnc_aten_quantized_conv2d_relu) \
|
| 31 |
+
_(nnc_aten_quantized_conv2d_relu_out) \
|
| 32 |
+
_(nnc_aten_quantized_linear) \
|
| 33 |
+
_(nnc_aten_quantized_linear_out) \
|
| 34 |
+
_(nnc_aten_quantized_linear_relu) \
|
| 35 |
+
_(nnc_aten_quantized_add) \
|
| 36 |
+
_(nnc_aten_quantized_cat) \
|
| 37 |
+
_(nnc_aten_quantized_mul) \
|
| 38 |
+
_(nnc_aten_quantized_mul_out) \
|
| 39 |
+
_(nnc_aten_quantized_mul_scalar) \
|
| 40 |
+
_(nnc_aten_quantized_mul_scalar_out) \
|
| 41 |
+
_(nnc_aten_quantized_relu) \
|
| 42 |
+
_(nnc_aten_quantized_sigmoid) \
|
| 43 |
+
_(nnc_aten_quantized_sigmoid_out) \
|
| 44 |
+
_(nnc_aten_quantize_per_tensor) \
|
| 45 |
+
_(nnc_aten_quantize_per_tensor_out) \
|
| 46 |
+
_(nnc_aten_triangular_solve) \
|
| 47 |
+
_(nnc_aten_upsample_nearest2d) \
|
| 48 |
+
_(nnc_aten_upsample_nearest2d_out) \
|
| 49 |
+
_(nnc_prepacked_conv2d_clamp_run) \
|
| 50 |
+
_(nnc_prepacked_linear_clamp_run)
|
| 51 |
+
|
| 52 |
+
#define DECLARE_EXTERNAL_FUNCTION(NAME) \
|
| 53 |
+
TORCH_API void NAME( \
|
| 54 |
+
int64_t bufs_num, \
|
| 55 |
+
void** buf_data, \
|
| 56 |
+
int64_t* buf_ranks, \
|
| 57 |
+
int64_t* buf_dims, \
|
| 58 |
+
int64_t* buf_strides, \
|
| 59 |
+
int8_t* buf_dtypes, \
|
| 60 |
+
int64_t args_num, \
|
| 61 |
+
int64_t* extra_args);
|
| 62 |
+
|
| 63 |
+
namespace torch::jit::tensorexpr {
|
| 64 |
+
struct QIData final {
|
| 65 |
+
double scale;
|
| 66 |
+
int64_t zero;
|
| 67 |
+
c10::ScalarType scalarType;
|
| 68 |
+
};
|
| 69 |
+
std::vector<at::Tensor> constructTensors(
|
| 70 |
+
int64_t bufs_num,
|
| 71 |
+
void** buf_data,
|
| 72 |
+
int64_t* buf_ranks,
|
| 73 |
+
int64_t* buf_dims,
|
| 74 |
+
int64_t* buf_strides,
|
| 75 |
+
int8_t* buf_dtypes,
|
| 76 |
+
std::optional<std::vector<std::pair<size_t, QIData>>> qdataArg =
|
| 77 |
+
std::nullopt);
|
| 78 |
+
|
| 79 |
+
std::vector<at::Tensor> constructTensors2(
|
| 80 |
+
int64_t bufs_in_num,
|
| 81 |
+
void** buf_data,
|
| 82 |
+
int64_t* buf_ranks,
|
| 83 |
+
int64_t* buf_dims,
|
| 84 |
+
int64_t* buf_strides,
|
| 85 |
+
int8_t* buf_dtypes,
|
| 86 |
+
std::optional<std::vector<std::pair<size_t, QIData>>> qdataArg =
|
| 87 |
+
std::nullopt,
|
| 88 |
+
size_t bufs_out_num = 0);
|
| 89 |
+
|
| 90 |
+
#ifdef C10_MOBILE
|
| 91 |
+
extern "C" {
|
| 92 |
+
#endif
|
| 93 |
+
void DispatchParallel(
|
| 94 |
+
int8_t* func,
|
| 95 |
+
int64_t start,
|
| 96 |
+
int64_t stop,
|
| 97 |
+
int8_t* packed_data) noexcept;
|
| 98 |
+
|
| 99 |
+
FOR_ALL_EXTERNAL_FUNCTIONS(DECLARE_EXTERNAL_FUNCTION)
|
| 100 |
+
#if AT_MKLDNN_ENABLED()
|
| 101 |
+
DECLARE_EXTERNAL_FUNCTION(nnc_mkldnn_prepacked_conv_run)
|
| 102 |
+
#endif
|
| 103 |
+
|
| 104 |
+
TORCH_API void nnc_aten_free(size_t bufs_num, void** ptrs) noexcept;
|
| 105 |
+
|
| 106 |
+
#ifdef C10_MOBILE
|
| 107 |
+
} // extern "C"
|
| 108 |
+
#endif
|
| 109 |
+
|
| 110 |
+
} // namespace torch::jit::tensorexpr
|
| 111 |
+
|
| 112 |
+
#undef DECLARE_EXTERNAL_FUNCTION
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions_core.h
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <ATen/Parallel.h>
|
| 6 |
+
#include <torch/csrc/Export.h>
|
| 7 |
+
#include <cstdint>
|
| 8 |
+
|
| 9 |
+
namespace torch::jit::tensorexpr {
|
| 10 |
+
|
| 11 |
+
#ifdef C10_MOBILE
|
| 12 |
+
extern "C" {
|
| 13 |
+
#endif
|
| 14 |
+
void DispatchParallel(
|
| 15 |
+
int8_t* func,
|
| 16 |
+
int64_t start,
|
| 17 |
+
int64_t stop,
|
| 18 |
+
int8_t* packed_data) noexcept;
|
| 19 |
+
|
| 20 |
+
TORCH_API void nnc_aten_free(size_t bufs_num, void** ptrs) noexcept;
|
| 21 |
+
|
| 22 |
+
#ifdef C10_MOBILE
|
| 23 |
+
} // extern "C"
|
| 24 |
+
#endif
|
| 25 |
+
|
| 26 |
+
} // namespace torch::jit::tensorexpr
|
| 27 |
+
|
| 28 |
+
#else
|
| 29 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 30 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/external_functions_registry.h
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <cstdint>
|
| 6 |
+
#include <string>
|
| 7 |
+
#include <unordered_map>
|
| 8 |
+
|
| 9 |
+
namespace torch::jit::tensorexpr {
|
| 10 |
+
|
| 11 |
+
// The external functions that could be called from NNC must have the same
|
| 12 |
+
// signature defined by `NNCExternalFunction`.
|
| 13 |
+
//
|
| 14 |
+
// Why this signature?
|
| 15 |
+
// It was picked for two reasons: 1) it should be generic enough to represent
|
| 16 |
+
// most of the ops we might want to call, 2) it should be possible to generate a
|
| 17 |
+
// code for this call in LLVM codegen.
|
| 18 |
+
// The first 5 parameters allow to pass any number of contiguous CPU tensors in
|
| 19 |
+
// case we need to run aten ops (TODO: support different devices). The first
|
| 20 |
+
// buffer in the array is assumed to be the output buffer. We couldn't use
|
| 21 |
+
// `at::Tensor` (or `c10::IValue`) type there directly as it would mean that
|
| 22 |
+
// we'd need to declare it in LLVM codegen in LLVM IR form, which would be very
|
| 23 |
+
// cumbersome and hard to maintain. Note that the dimensions of all tensors are
|
| 24 |
+
// concatenated into a single array buf_dims. We do not need to pass its length,
|
| 25 |
+
// since it can be deduced from total number of buffers and their ranks.
|
| 26 |
+
//
|
| 27 |
+
// The last 2 arguments allow to pass any non-tensor arguments encoded as an
|
| 28 |
+
// array of int64_t values. The way they are encoded is not specified and could
|
| 29 |
+
// be arbitrary - whatever the most convenient for the specific bridge function
|
| 30 |
+
// is.
|
| 31 |
+
//
|
| 32 |
+
// The bridge functions must not throw exceptions - properly propagating them
|
| 33 |
+
// from the generated code is too cumbersome, and thus all calls to functions
|
| 34 |
+
// that could throw must be wrapped with try-catch blocks.
|
| 35 |
+
using NNCExternalFunction = void (*)(
|
| 36 |
+
int64_t bufs_num,
|
| 37 |
+
void** buf_data,
|
| 38 |
+
int64_t* buf_ranks,
|
| 39 |
+
int64_t* buf_dims,
|
| 40 |
+
int64_t* buf_strides,
|
| 41 |
+
int8_t* buf_dtypes,
|
| 42 |
+
int64_t args_num,
|
| 43 |
+
int64_t* extra_args);
|
| 44 |
+
|
| 45 |
+
// Return a global map "function-name" -> "function-pointer" for all registered
|
| 46 |
+
// in NNC external functions
|
| 47 |
+
TORCH_API std::unordered_map<std::string, NNCExternalFunction>&
|
| 48 |
+
getNNCFunctionRegistry();
|
| 49 |
+
|
| 50 |
+
// To register a new external function in NNC one needs to create an instance of
|
| 51 |
+
// this struct
|
| 52 |
+
struct RegisterNNCExternalFunction {
|
| 53 |
+
RegisterNNCExternalFunction(const std::string& name, NNCExternalFunction fn) {
|
| 54 |
+
getNNCFunctionRegistry()[name] = fn;
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
} // namespace torch::jit::tensorexpr
|
| 59 |
+
|
| 60 |
+
#else
|
| 61 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 62 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/fwd_decls.h
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/ScalarType.h>
|
| 4 |
+
#include <memory>
|
| 5 |
+
|
| 6 |
+
namespace torch::jit::tensorexpr {
|
| 7 |
+
|
| 8 |
+
template <typename Node>
|
| 9 |
+
using NodePtr = std::shared_ptr<Node>;
|
| 10 |
+
|
| 11 |
+
template <typename To, typename From>
|
| 12 |
+
NodePtr<To> to(const NodePtr<From>& x) {
|
| 13 |
+
return std::dynamic_pointer_cast<To>(x);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
template <typename To, typename From>
|
| 17 |
+
NodePtr<To> static_to(NodePtr<From> x) {
|
| 18 |
+
return std::static_pointer_cast<To>(x);
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
template <typename Node, typename... Args>
|
| 22 |
+
NodePtr<Node> alloc(Args&&... args) {
|
| 23 |
+
return std::make_shared<Node>(std::forward<Args>(args)...);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
class Buf;
|
| 27 |
+
class Expr;
|
| 28 |
+
class Stmt;
|
| 29 |
+
class Var;
|
| 30 |
+
|
| 31 |
+
using BufPtr = NodePtr<Buf>;
|
| 32 |
+
using ExprPtr = NodePtr<Expr>;
|
| 33 |
+
using StmtPtr = NodePtr<Stmt>;
|
| 34 |
+
using VarPtr = NodePtr<Var>;
|
| 35 |
+
|
| 36 |
+
class ExprHandle;
|
| 37 |
+
class VarHandle;
|
| 38 |
+
class BufHandle;
|
| 39 |
+
|
| 40 |
+
class Add;
|
| 41 |
+
class And;
|
| 42 |
+
class BitCast;
|
| 43 |
+
class Broadcast;
|
| 44 |
+
class Cast;
|
| 45 |
+
class CompareSelect;
|
| 46 |
+
class Div;
|
| 47 |
+
class IfThenElse;
|
| 48 |
+
class Intrinsics;
|
| 49 |
+
class Let;
|
| 50 |
+
class Load;
|
| 51 |
+
class Lshift;
|
| 52 |
+
class Max;
|
| 53 |
+
class MaxTerm;
|
| 54 |
+
class Min;
|
| 55 |
+
class MinTerm;
|
| 56 |
+
class Mod;
|
| 57 |
+
class Mul;
|
| 58 |
+
class Or;
|
| 59 |
+
class Polynomial;
|
| 60 |
+
class Ramp;
|
| 61 |
+
class ReduceOp;
|
| 62 |
+
class RoundOff;
|
| 63 |
+
class Rshift;
|
| 64 |
+
class Store;
|
| 65 |
+
class Sub;
|
| 66 |
+
class Term;
|
| 67 |
+
class Xor;
|
| 68 |
+
using AddPtr = NodePtr<Add>;
|
| 69 |
+
using AndPtr = NodePtr<And>;
|
| 70 |
+
using BitCastPtr = NodePtr<BitCast>;
|
| 71 |
+
using BroadcastPtr = NodePtr<Broadcast>;
|
| 72 |
+
using CastPtr = NodePtr<Cast>;
|
| 73 |
+
using CompareSelectPtr = NodePtr<CompareSelect>;
|
| 74 |
+
using DivPtr = NodePtr<Div>;
|
| 75 |
+
using IfThenElsePtr = NodePtr<IfThenElse>;
|
| 76 |
+
using IntrinsicsPtr = NodePtr<Intrinsics>;
|
| 77 |
+
using LetPtr = NodePtr<Let>;
|
| 78 |
+
using LoadPtr = NodePtr<Load>;
|
| 79 |
+
using LshiftPtr = NodePtr<Lshift>;
|
| 80 |
+
using MaxPtr = NodePtr<Max>;
|
| 81 |
+
using MaxTermPtr = NodePtr<MaxTerm>;
|
| 82 |
+
using MinPtr = NodePtr<Min>;
|
| 83 |
+
using MinTermPtr = NodePtr<MinTerm>;
|
| 84 |
+
using ModPtr = NodePtr<Mod>;
|
| 85 |
+
using MulPtr = NodePtr<Mul>;
|
| 86 |
+
using OrPtr = NodePtr<Or>;
|
| 87 |
+
using PolynomialPtr = NodePtr<Polynomial>;
|
| 88 |
+
using RampPtr = NodePtr<Ramp>;
|
| 89 |
+
using ReduceOpPtr = NodePtr<ReduceOp>;
|
| 90 |
+
using RoundOffPtr = NodePtr<RoundOff>;
|
| 91 |
+
using RshiftPtr = NodePtr<Rshift>;
|
| 92 |
+
using StorePtr = NodePtr<Store>;
|
| 93 |
+
using SubPtr = NodePtr<Sub>;
|
| 94 |
+
using TermPtr = NodePtr<Term>;
|
| 95 |
+
using XorPtr = NodePtr<Xor>;
|
| 96 |
+
|
| 97 |
+
class Allocate;
|
| 98 |
+
class AtomicAdd;
|
| 99 |
+
class Block;
|
| 100 |
+
class Cond;
|
| 101 |
+
class ExternalCall;
|
| 102 |
+
class ExternalCallWithAlloc;
|
| 103 |
+
class For;
|
| 104 |
+
class Free;
|
| 105 |
+
class FreeExt;
|
| 106 |
+
class PlacementAllocate;
|
| 107 |
+
class SyncThreads;
|
| 108 |
+
using AllocatePtr = NodePtr<Allocate>;
|
| 109 |
+
using AtomicAddPtr = NodePtr<AtomicAdd>;
|
| 110 |
+
using BlockPtr = NodePtr<Block>;
|
| 111 |
+
using CondPtr = NodePtr<Cond>;
|
| 112 |
+
using ExternalCallPtr = NodePtr<ExternalCall>;
|
| 113 |
+
using ExternalCallWithAllocPtr = NodePtr<ExternalCallWithAlloc>;
|
| 114 |
+
using ForPtr = NodePtr<For>;
|
| 115 |
+
using FreePtr = NodePtr<Free>;
|
| 116 |
+
using FreeExtPtr = NodePtr<FreeExt>;
|
| 117 |
+
using PlacementAllocatePtr = NodePtr<PlacementAllocate>;
|
| 118 |
+
using SyncThreadsPtr = NodePtr<SyncThreads>;
|
| 119 |
+
|
| 120 |
+
#define IMM_DECLARE(Type, Name) \
|
| 121 |
+
class Name##Imm; \
|
| 122 |
+
using Name##ImmPtr = NodePtr<Name##Imm>;
|
| 123 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_DECLARE)
|
| 124 |
+
#undef IMM_DECLARE
|
| 125 |
+
|
| 126 |
+
} // namespace torch::jit::tensorexpr
|
| 127 |
+
|
| 128 |
+
#else
|
| 129 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 130 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/graph_opt.h
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/ir/ir.h>
|
| 5 |
+
|
| 6 |
+
namespace torch::jit::tensorexpr {
|
| 7 |
+
|
| 8 |
+
// Optimize aten::cat ops in the given subgraph.
|
| 9 |
+
//
|
| 10 |
+
// Moving users of cat to its inputs.
|
| 11 |
+
// Cat ops get lowered into multiple loops, one per input. When the result
|
| 12 |
+
// of cat is used by some other op, it results in a situation where inlining
|
| 13 |
+
// of cat does not happen. This in turn results in intermediate buffers
|
| 14 |
+
// being created for the result of cat, since it is not inlined.
|
| 15 |
+
//
|
| 16 |
+
// For example, consider the following graph:
|
| 17 |
+
// graph(%x : Float(10, strides=[1], device=cpu),
|
| 18 |
+
// %y : Float(20, strides=[1], device=cpu)):
|
| 19 |
+
// %dim : int = prim::Constant[value=0]()
|
| 20 |
+
// %xy_list : Tensor[] = prim::ListConstruct(%x, %y)
|
| 21 |
+
// %cat : Float(60, strides=[1], device=cpu) = aten::cat(%xy_list, %dim)
|
| 22 |
+
// %5 : Float(60, strides=[1], device=cpu) = aten::log(%cat)
|
| 23 |
+
// return (%5))IR";
|
| 24 |
+
//
|
| 25 |
+
// This will get lowered into:
|
| 26 |
+
// Allocate(aten_cat);
|
| 27 |
+
// for (...)
|
| 28 |
+
// aten_cat[...] = x[...]
|
| 29 |
+
// for (...)
|
| 30 |
+
// aten_cat[...] = y[...]
|
| 31 |
+
// for (...)
|
| 32 |
+
// aten_log[...] = log(aten_cat[...])
|
| 33 |
+
// Free(aten_cat);
|
| 34 |
+
// Note that aten_cat is not inlined into aten_log and it results in
|
| 35 |
+
// an intermediate buffer allocation as well.
|
| 36 |
+
//
|
| 37 |
+
// Optimization:
|
| 38 |
+
// We move the ops that use the result of `cat` into its inputs whenever
|
| 39 |
+
// possible.
|
| 40 |
+
//
|
| 41 |
+
// The graph above will be transformed to:
|
| 42 |
+
// graph(%x : Float(10, strides=[1], device=cpu),
|
| 43 |
+
// %y : Float(20, strides=[1], device=cpu)):
|
| 44 |
+
// %3 : int = prim::Constant[value=0]()
|
| 45 |
+
// %7 : Float(10, strides=[1], device=cpu) = aten::log(%x)
|
| 46 |
+
// %8 : Float(20, strides=[1], device=cpu) = aten::log(%y)
|
| 47 |
+
// %9 : Tensor[] = prim::ListConstruct(%7, %8)
|
| 48 |
+
// %10 : Float(60, strides=[1], device=cpu) = aten::cat(%9, %3)
|
| 49 |
+
// return (%10)
|
| 50 |
+
//
|
| 51 |
+
// This will get lowered into:
|
| 52 |
+
// for (...)
|
| 53 |
+
// aten_cat[...] = log(x[...])
|
| 54 |
+
// for (...)
|
| 55 |
+
// aten_cat[...] = log(y[...])
|
| 56 |
+
// aten_cat is the output buffer here.
|
| 57 |
+
|
| 58 |
+
bool OptimizeCat(const std::shared_ptr<Graph>& graph);
|
| 59 |
+
|
| 60 |
+
TORCH_API void annotateInputShapes(
|
| 61 |
+
const std::shared_ptr<Graph>& graph,
|
| 62 |
+
const std::vector<std::optional<at::Tensor>>& example_inputs);
|
| 63 |
+
TORCH_API std::shared_ptr<Graph> removeUnusedSelfArgument(
|
| 64 |
+
const std::shared_ptr<Graph>& graph);
|
| 65 |
+
TORCH_API std::shared_ptr<Graph> removeGraphOutput(
|
| 66 |
+
const std::shared_ptr<Graph>& graph,
|
| 67 |
+
size_t idx);
|
| 68 |
+
TORCH_API std::shared_ptr<Graph> replaceListOutputWithTuple(
|
| 69 |
+
const std::shared_ptr<Graph>& graph);
|
| 70 |
+
|
| 71 |
+
// Perform \p ITERS rounds of "trimming" for the given \p GRAPH.
|
| 72 |
+
//
|
| 73 |
+
// Trimming means that we try to remove a small portion of the graph while
|
| 74 |
+
// keeping it valid. This is useful for debugging when we try to find a minimal
|
| 75 |
+
// example reproducing the issue at hand. When ITERS is 0, the graph remains
|
| 76 |
+
// unchanged, when ITERS is a big number, the graph usually becomes empty.
|
| 77 |
+
TORCH_API std::shared_ptr<Graph> trimGraph(
|
| 78 |
+
const std::shared_ptr<Graph>& graph,
|
| 79 |
+
int64_t iters);
|
| 80 |
+
|
| 81 |
+
// Scan all values in the given graph and replace each dimension with a size Xi
|
| 82 |
+
// present in \p SIZES with a symbolic shape Yi. Return a vector of symbol
|
| 83 |
+
// values [Y0, Y1, .., Yn].
|
| 84 |
+
//
|
| 85 |
+
// For example:
|
| 86 |
+
// Input:
|
| 87 |
+
// graph(%x : Float(10, 20, 30, 40)):
|
| 88 |
+
// %y : Float(10, 20, 30, 40) = aten::relu(%x)
|
| 89 |
+
// return %y
|
| 90 |
+
//
|
| 91 |
+
// If we run makeShapesSymbolic(graph, {20, 40}), then we'll get:
|
| 92 |
+
//
|
| 93 |
+
// graph(%x : Float(10, SS(-3), 30, SS(-5))):
|
| 94 |
+
// %y : Float(10, SS(-3), 30, SS(-5)) = aten::relu(%x)
|
| 95 |
+
// return %y
|
| 96 |
+
//
|
| 97 |
+
// and get {-3, -5} as the return value.
|
| 98 |
+
TORCH_API std::vector<int64_t> makeShapesSymbolic(
|
| 99 |
+
std::shared_ptr<Graph>& graph,
|
| 100 |
+
const std::vector<int64_t>& sizes);
|
| 101 |
+
|
| 102 |
+
// Inspect the graph and report whether it can be converted to TE IR.
|
| 103 |
+
// TODO: add error reporting for graphs that can't be converted.
|
| 104 |
+
TORCH_API bool isGraphCompilable(const std::shared_ptr<Graph>& graph);
|
| 105 |
+
|
| 106 |
+
// Examine the graph and (hackily) fill in missing tensor type info, such as
|
| 107 |
+
// scalar type, device, and strides. Ideally, this should be done by a proper
|
| 108 |
+
// dtype/device/shape propagation passes, but until they are ready we can use
|
| 109 |
+
// this, not always correct, workaround pass.
|
| 110 |
+
TORCH_API void fixupMissingShapeInfo(const std::shared_ptr<Graph>& graph);
|
| 111 |
+
|
| 112 |
+
} // namespace torch::jit::tensorexpr
|
| 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)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/half_support.h
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/tensorexpr/codegen.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 6 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 7 |
+
#include <torch/csrc/jit/tensorexpr/tensor.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::jit::tensorexpr {
|
| 10 |
+
|
| 11 |
+
// Walk the Statement looking for Half size loads/stores.
|
| 12 |
+
class HalfChecker : public IRVisitor {
|
| 13 |
+
public:
|
| 14 |
+
HalfChecker(const std::vector<CodeGen::BufferArg>& args) {
|
| 15 |
+
for (const auto& BA : args) {
|
| 16 |
+
hasHalf_ |= BA.dtype().scalar_type() == ScalarType::Half;
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
bool hasHalf() const {
|
| 21 |
+
return hasHalf_;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
bool hasBFloat16() const {
|
| 25 |
+
return hasBFloat16_;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
void visit(const LoadPtr& v) override {
|
| 29 |
+
hasHalf_ |= v->dtype().scalar_type() == ScalarType::Half;
|
| 30 |
+
hasBFloat16_ |= v->dtype().scalar_type() == ScalarType::BFloat16;
|
| 31 |
+
IRVisitor::visit(v);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
void visit(const StorePtr& v) override {
|
| 35 |
+
hasHalf_ |= v->buf()->dtype().scalar_type() == ScalarType::Half;
|
| 36 |
+
hasBFloat16_ |= v->buf()->dtype().scalar_type() == ScalarType::BFloat16;
|
| 37 |
+
IRVisitor::visit(v);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
void visit(const HalfImmPtr& v) override {
|
| 41 |
+
hasHalf_ = true;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
void visit(const BFloat16ImmPtr& v) override {
|
| 45 |
+
hasBFloat16_ = true;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
void visit(const CastPtr& v) override {
|
| 49 |
+
hasHalf_ |= v->dtype().scalar_type() == ScalarType::Half;
|
| 50 |
+
hasBFloat16_ |= v->dtype().scalar_type() == ScalarType::BFloat16;
|
| 51 |
+
IRVisitor::visit(v);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
private:
|
| 55 |
+
bool hasHalf_{false};
|
| 56 |
+
bool hasBFloat16_{false};
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
class HalfRewriter : public IRMutator {
|
| 60 |
+
ExprPtr mutate(const LoadPtr& v) override {
|
| 61 |
+
ExprPtr child = IRMutator::mutate(v);
|
| 62 |
+
if (!isHalf(child)) {
|
| 63 |
+
return child;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
ExprPtr ret = alloc<Cast>(
|
| 67 |
+
child->dtype().cloneWithScalarType(ScalarType::Float), child);
|
| 68 |
+
|
| 69 |
+
inserted_half_casts_.insert(ret);
|
| 70 |
+
return ret;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
StmtPtr mutate(const StorePtr& v) override {
|
| 74 |
+
// Since mutation changes the `value()` expression in-place, we need to
|
| 75 |
+
// get the dtype of the `value()` before that is mutated.
|
| 76 |
+
auto newType = v->value()->dtype();
|
| 77 |
+
ExprPtr new_val = v->value()->accept_mutator(this);
|
| 78 |
+
auto bufType = v->buf()->dtype();
|
| 79 |
+
|
| 80 |
+
if (isHalf(newType.scalar_type())) {
|
| 81 |
+
new_val = alloc<Cast>(newType, new_val);
|
| 82 |
+
inserted_half_casts_.insert(new_val);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// The scalar_type of value is not Half while the buf is Half
|
| 86 |
+
if (!isHalf(newType.scalar_type()) && isHalf(bufType.scalar_type())) {
|
| 87 |
+
new_val = alloc<Cast>(
|
| 88 |
+
newType.cloneWithScalarType(bufType.scalar_type()), new_val);
|
| 89 |
+
inserted_half_casts_.insert(new_val);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
v->set_value(new_val);
|
| 93 |
+
return v;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
ExprPtr mutate(const HalfImmPtr& v) override {
|
| 97 |
+
return alloc<Cast>(kFloat, v);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
ExprPtr mutate(const BFloat16ImmPtr& v) override {
|
| 101 |
+
return alloc<Cast>(kFloat, v);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
ExprPtr mutate(const CastPtr& v) override {
|
| 105 |
+
ExprPtr child = v->src_value()->accept_mutator(this);
|
| 106 |
+
|
| 107 |
+
// just don't allow half casts we didn't insert.
|
| 108 |
+
if (isHalf(v)) {
|
| 109 |
+
if (inserted_half_casts_.count(v) < 1) {
|
| 110 |
+
v->set_src_value(child);
|
| 111 |
+
v->set_dtype(v->dtype().cloneWithScalarType(c10::kFloat));
|
| 112 |
+
return v;
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Remove Half(Float()) and friends.
|
| 117 |
+
CastPtr cast_child = to<Cast>(child);
|
| 118 |
+
if (cast_child) {
|
| 119 |
+
auto cast_to_double = v->dtype().scalar_type() == ScalarType::Double;
|
| 120 |
+
auto from_half = isHalf(cast_child->src_value());
|
| 121 |
+
// Cannot simplify the double(float(half)) to double(half) as NNC does
|
| 122 |
+
// not support cast BF16 to double directly.
|
| 123 |
+
auto not_cast_half_to_doulbe = !(cast_to_double && from_half);
|
| 124 |
+
if (v->dtype().is_floating_point() &&
|
| 125 |
+
cast_child->dtype().is_floating_point() && not_cast_half_to_doulbe) {
|
| 126 |
+
return alloc<Cast>(v->dtype(), cast_child->src_value());
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
if (child == v->src_value()) {
|
| 131 |
+
return v;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
return alloc<Cast>(v->dtype(), child);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
StmtPtr mutate(const LetPtr& v) override {
|
| 138 |
+
if (isHalf(v->var()->dtype().scalar_type())) {
|
| 139 |
+
VarPtr load_new_var = alloc<Var>(v->var()->name_hint(), kFloat);
|
| 140 |
+
ExprPtr new_value = alloc<Cast>(
|
| 141 |
+
v->var()->dtype().cloneWithScalarType(ScalarType::Float),
|
| 142 |
+
v->value()->accept_mutator(this));
|
| 143 |
+
var_map[v->var()] = load_new_var;
|
| 144 |
+
|
| 145 |
+
return alloc<Let>(load_new_var, new_value);
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
return IRMutator::mutate(v);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
ExprPtr mutate(const VarPtr& v) override {
|
| 152 |
+
auto it = var_map.find(v);
|
| 153 |
+
if (it != var_map.end()) {
|
| 154 |
+
return it->second;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
return v;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
template <typename T>
|
| 161 |
+
ExprPtr mutateArithmetic(T v) {
|
| 162 |
+
IRMutator::mutate(v);
|
| 163 |
+
if (isHalf(v)) {
|
| 164 |
+
v->set_dtype(v->dtype().cloneWithScalarType(c10::kFloat));
|
| 165 |
+
}
|
| 166 |
+
return v;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
ExprPtr mutate(const AddPtr& v) override {
|
| 170 |
+
return mutateArithmetic(v);
|
| 171 |
+
}
|
| 172 |
+
ExprPtr mutate(const SubPtr& v) override {
|
| 173 |
+
return mutateArithmetic(v);
|
| 174 |
+
}
|
| 175 |
+
ExprPtr mutate(const MulPtr& v) override {
|
| 176 |
+
return mutateArithmetic(v);
|
| 177 |
+
}
|
| 178 |
+
ExprPtr mutate(const DivPtr& v) override {
|
| 179 |
+
return mutateArithmetic(v);
|
| 180 |
+
}
|
| 181 |
+
ExprPtr mutate(const MaxPtr& v) override {
|
| 182 |
+
return mutateArithmetic(v);
|
| 183 |
+
}
|
| 184 |
+
ExprPtr mutate(const MinPtr& v) override {
|
| 185 |
+
return mutateArithmetic(v);
|
| 186 |
+
}
|
| 187 |
+
ExprPtr mutate(const CompareSelectPtr& v) override {
|
| 188 |
+
return mutateArithmetic(v);
|
| 189 |
+
}
|
| 190 |
+
ExprPtr mutate(const BroadcastPtr& v) override {
|
| 191 |
+
return mutateArithmetic(v);
|
| 192 |
+
}
|
| 193 |
+
ExprPtr mutate(const IfThenElsePtr& v) override {
|
| 194 |
+
return mutateArithmetic(v);
|
| 195 |
+
}
|
| 196 |
+
ExprPtr mutate(const IntrinsicsPtr& v) override {
|
| 197 |
+
return mutateArithmetic(v);
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
private:
|
| 201 |
+
static bool isHalf(ScalarType st) {
|
| 202 |
+
return st == ScalarType::Half || st == ScalarType::BFloat16;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
static bool isHalf(const ExprPtr& v) {
|
| 206 |
+
return isHalf(v->dtype().scalar_type());
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
std::unordered_set<ExprPtr> inserted_half_casts_;
|
| 210 |
+
std::unordered_map<VarPtr, VarPtr> var_map;
|
| 211 |
+
};
|
| 212 |
+
|
| 213 |
+
} // namespace torch::jit::tensorexpr
|
| 214 |
+
|
| 215 |
+
#else
|
| 216 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 217 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/hash_provider.h
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
|
| 6 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 7 |
+
#include <torch/csrc/jit/tensorexpr/tensor.h>
|
| 8 |
+
|
| 9 |
+
#include <utility>
|
| 10 |
+
|
| 11 |
+
namespace torch::jit::tensorexpr {
|
| 12 |
+
|
| 13 |
+
struct TORCH_API SimplifierHashType {
|
| 14 |
+
SimplifierHashType() = default;
|
| 15 |
+
explicit SimplifierHashType(size_t s) : _h(s) {}
|
| 16 |
+
|
| 17 |
+
bool operator==(const SimplifierHashType& other) const;
|
| 18 |
+
bool operator!=(const SimplifierHashType& other) const;
|
| 19 |
+
bool operator<(const SimplifierHashType& other) const;
|
| 20 |
+
bool operator==(const size_t other) const;
|
| 21 |
+
bool operator!=(const size_t other) const;
|
| 22 |
+
|
| 23 |
+
size_t _h{0};
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
} // namespace torch::jit::tensorexpr
|
| 27 |
+
|
| 28 |
+
namespace std {
|
| 29 |
+
template <>
|
| 30 |
+
struct hash<torch::jit::tensorexpr::SimplifierHashType> {
|
| 31 |
+
size_t operator()(
|
| 32 |
+
const torch::jit::tensorexpr::SimplifierHashType& k) const noexcept {
|
| 33 |
+
return k._h;
|
| 34 |
+
}
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
} // namespace std
|
| 38 |
+
|
| 39 |
+
namespace torch::jit::tensorexpr {
|
| 40 |
+
|
| 41 |
+
#define CACHE_GUARD() \
|
| 42 |
+
if (cachedHash(v)) { \
|
| 43 |
+
return; \
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
class Term;
|
| 47 |
+
class Polynomial;
|
| 48 |
+
|
| 49 |
+
/* Expression hasher providing comparable values representing sub-exprs.
|
| 50 |
+
* Uses memoization to avoid excessive recursion. */
|
| 51 |
+
class TORCH_API HashProvider : public IRVisitor {
|
| 52 |
+
public:
|
| 53 |
+
template <class T>
|
| 54 |
+
SimplifierHashType hash(T e) {
|
| 55 |
+
e->accept(this);
|
| 56 |
+
return hashOf(e);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
bool cachedHash(const ExprPtr& e) {
|
| 60 |
+
return exprToHash_.find(e) != exprToHash_.end();
|
| 61 |
+
}
|
| 62 |
+
bool cachedHash(const StmtPtr& s) {
|
| 63 |
+
return stmtToHash_.find(s) != stmtToHash_.end();
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
void clearCache() {
|
| 67 |
+
exprToHash_.clear();
|
| 68 |
+
stmtToHash_.clear();
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
void visit(const AddPtr& v) override;
|
| 72 |
+
void visit(const SubPtr& v) override;
|
| 73 |
+
void visit(const MulPtr& v) override;
|
| 74 |
+
void visit(const DivPtr& v) override;
|
| 75 |
+
void visit(const ModPtr& v) override;
|
| 76 |
+
void visit(const RoundOffPtr& v) override;
|
| 77 |
+
void visit(const MaxPtr& v) override;
|
| 78 |
+
void visit(const MinPtr& v) override;
|
| 79 |
+
void visit(const AndPtr& v) override;
|
| 80 |
+
void visit(const OrPtr& v) override;
|
| 81 |
+
void visit(const XorPtr& v) override;
|
| 82 |
+
void visit(const LshiftPtr& v) override;
|
| 83 |
+
void visit(const RshiftPtr& v) override;
|
| 84 |
+
void visit(const CompareSelectPtr& v) override;
|
| 85 |
+
|
| 86 |
+
#define IMM_VISIT(Type, Name) \
|
| 87 |
+
void visit(const Name##ImmPtr& v) override { \
|
| 88 |
+
CACHE_GUARD(); \
|
| 89 |
+
putHash(v, hash_combine(#Name, v->value())); \
|
| 90 |
+
}
|
| 91 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_VISIT)
|
| 92 |
+
#undef IMM_VISIT
|
| 93 |
+
|
| 94 |
+
void visit(const CastPtr& v) override;
|
| 95 |
+
void visit(const VarPtr& v) override;
|
| 96 |
+
void visit(const RampPtr& v) override;
|
| 97 |
+
void visit(const LoadPtr& v) override;
|
| 98 |
+
void visit(const StorePtr& v) override;
|
| 99 |
+
void visit(const BlockPtr& v) override;
|
| 100 |
+
void visit(const ForPtr& v) override;
|
| 101 |
+
void visit(const BroadcastPtr& v) override;
|
| 102 |
+
void visit(const IfThenElsePtr& v) override;
|
| 103 |
+
void visit(const IntrinsicsPtr& v) override;
|
| 104 |
+
void visit(const AllocatePtr& v) override;
|
| 105 |
+
void visit(const FreePtr& v) override;
|
| 106 |
+
void visit(const CondPtr& v) override;
|
| 107 |
+
void visit(const TermPtr& v) override;
|
| 108 |
+
void visit(const PolynomialPtr& v) override;
|
| 109 |
+
void visit(const MaxTermPtr& v) override;
|
| 110 |
+
void visit(const MinTermPtr& v) override;
|
| 111 |
+
|
| 112 |
+
template <typename... Types>
|
| 113 |
+
SimplifierHashType hash_combine(const Types&... args) {
|
| 114 |
+
SimplifierHashType seed;
|
| 115 |
+
_hash_combine(seed, args...);
|
| 116 |
+
return seed;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
private:
|
| 120 |
+
SimplifierHashType hashOf(const ExprPtr& e) {
|
| 121 |
+
auto it = exprToHash_.find(e);
|
| 122 |
+
if (it != exprToHash_.end()) {
|
| 123 |
+
return it->second;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
// As a failsafe fall back to IRPrinter.
|
| 127 |
+
std::stringstream ss;
|
| 128 |
+
IRPrinter printer(ss);
|
| 129 |
+
e->accept(&printer);
|
| 130 |
+
SimplifierHashType hash = SimplifierHashType(te_hash(std::move(ss).str()));
|
| 131 |
+
putHash(e, hash);
|
| 132 |
+
|
| 133 |
+
return hash;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
SimplifierHashType hashOf(const StmtPtr& s) {
|
| 137 |
+
auto it = stmtToHash_.find(s);
|
| 138 |
+
if (it != stmtToHash_.end()) {
|
| 139 |
+
return it->second;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// As a failsafe fall back to IRPrinter.
|
| 143 |
+
std::stringstream ss;
|
| 144 |
+
IRPrinter printer(ss);
|
| 145 |
+
s->accept(&printer);
|
| 146 |
+
SimplifierHashType hash = SimplifierHashType(te_hash(std::move(ss).str()));
|
| 147 |
+
putHash(s, hash);
|
| 148 |
+
|
| 149 |
+
return hash;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Hash funcs for various types, numbers are random.
|
| 153 |
+
template <typename T>
|
| 154 |
+
void _hash_combine(SimplifierHashType& seed, const T& val) {
|
| 155 |
+
seed._h ^= te_hash(val) + 0x1f752c19 + (seed._h << 7) + (seed._h >> 4);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
void _hash_combine(SimplifierHashType& seed, const char* val) {
|
| 159 |
+
seed._h ^= te_hash(val) + 0x1f752c19 + (seed._h << 7) + (seed._h >> 4);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// at:::Half doesn't have a prime_number_hash, so cast to short.
|
| 163 |
+
void _hash_combine(SimplifierHashType& seed, const at::Half& val) {
|
| 164 |
+
seed._h ^=
|
| 165 |
+
te_hash((uint16_t)val) + 0x1f752c19 + (seed._h << 7) + (seed._h >> 4);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
void _hash_combine(SimplifierHashType& seed, const Dtype& val) {
|
| 169 |
+
seed._h ^= te_hash(val.ToCppString()) + 0x1f752c19 + (seed._h << 7) +
|
| 170 |
+
(seed._h >> 4);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
void _hash_combine(SimplifierHashType& seed, ExprPtr e) {
|
| 174 |
+
_hash_combine(seed, hash(std::move(e)));
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
template <typename T, typename... Types>
|
| 178 |
+
void _hash_combine(
|
| 179 |
+
SimplifierHashType& seed,
|
| 180 |
+
const T& val,
|
| 181 |
+
const Types&... args) {
|
| 182 |
+
_hash_combine(seed, val);
|
| 183 |
+
_hash_combine(seed, args...);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
void putHash(const ExprPtr& e, SimplifierHashType h) {
|
| 187 |
+
auto res = exprToHash_.emplace(e, h);
|
| 188 |
+
if (res.second == false) {
|
| 189 |
+
// This is always a logic bug since we should check the cache first.
|
| 190 |
+
throw std::runtime_error("hash collision");
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
void putHash(const StmtPtr& s, SimplifierHashType h) {
|
| 194 |
+
auto res = stmtToHash_.emplace(s, h);
|
| 195 |
+
if (res.second == false) {
|
| 196 |
+
// This is always a logic bug since we should check the cache first.
|
| 197 |
+
throw std::runtime_error("hash collision");
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
std::unordered_map<ExprPtr, SimplifierHashType> exprToHash_;
|
| 202 |
+
std::unordered_map<StmtPtr, SimplifierHashType> stmtToHash_;
|
| 203 |
+
UniqueNameManager name_manager_;
|
| 204 |
+
|
| 205 |
+
size_t te_hash(SimplifierHashType val) {
|
| 206 |
+
return val._h;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
size_t te_hash(int64_t val) {
|
| 210 |
+
// put the thing down.
|
| 211 |
+
size_t h = val ^ 0x647AA4D20C0B;
|
| 212 |
+
// bit flip it.
|
| 213 |
+
size_t h2 = ~h;
|
| 214 |
+
// and reverse byte order.
|
| 215 |
+
size_t h3 = 0;
|
| 216 |
+
for (unsigned int i = 0; i < 64; i += 8) {
|
| 217 |
+
h3 |= ((h2 >> i) & 0xFF) << (64 - i - 8);
|
| 218 |
+
}
|
| 219 |
+
return h3;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
size_t te_hash(int32_t val) {
|
| 223 |
+
int64_t v2 = val;
|
| 224 |
+
return te_hash(v2);
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
size_t te_hash(uint32_t val) {
|
| 228 |
+
int64_t v2 = val;
|
| 229 |
+
return te_hash(v2);
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
size_t te_hash(uint64_t val) {
|
| 233 |
+
int64_t v2 = val;
|
| 234 |
+
return te_hash(v2);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
size_t te_hash(int16_t val) {
|
| 238 |
+
int64_t v2 = val;
|
| 239 |
+
return te_hash(v2);
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
size_t te_hash(std::string val) {
|
| 243 |
+
size_t hash{0};
|
| 244 |
+
int64_t intval{0};
|
| 245 |
+
int64_t s = val.size() - 1;
|
| 246 |
+
while (s >= 0) {
|
| 247 |
+
for (unsigned int i = 0; i < 8; ++i) {
|
| 248 |
+
if (s < 0)
|
| 249 |
+
break;
|
| 250 |
+
int64_t c = val[s];
|
| 251 |
+
intval |= (c << (i * 8));
|
| 252 |
+
|
| 253 |
+
s--;
|
| 254 |
+
}
|
| 255 |
+
hash ^= te_hash(intval);
|
| 256 |
+
intval = 0;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
return hash;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
size_t te_hash(double d) {
|
| 263 |
+
int64_t* n = reinterpret_cast<int64_t*>(&d);
|
| 264 |
+
return te_hash(*n);
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
size_t te_hash(float d) {
|
| 268 |
+
int32_t* n = reinterpret_cast<int32_t*>(&d);
|
| 269 |
+
return te_hash(*n);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
size_t te_hash(at::Half d) {
|
| 273 |
+
int16_t* n = reinterpret_cast<int16_t*>(&d);
|
| 274 |
+
return te_hash(*n);
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
size_t te_hash(at::BFloat16 d) {
|
| 278 |
+
int16_t* n = reinterpret_cast<int16_t*>(&d);
|
| 279 |
+
return te_hash(*n);
|
| 280 |
+
}
|
| 281 |
+
};
|
| 282 |
+
|
| 283 |
+
} // namespace torch::jit::tensorexpr
|
| 284 |
+
|
| 285 |
+
#else
|
| 286 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 287 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/intrinsic_symbols.h
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#ifdef TORCH_ENABLE_LLVM
|
| 5 |
+
#include <c10/util/ArrayRef.h>
|
| 6 |
+
|
| 7 |
+
namespace torch {
|
| 8 |
+
namespace jit {
|
| 9 |
+
namespace tensorexpr {
|
| 10 |
+
|
| 11 |
+
struct SymbolAddress {
|
| 12 |
+
const char* symbol;
|
| 13 |
+
void* address;
|
| 14 |
+
|
| 15 |
+
SymbolAddress(const char* sym, void* addr) : symbol(sym), address(addr) {}
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
c10::ArrayRef<SymbolAddress> getIntrinsicSymbols();
|
| 19 |
+
|
| 20 |
+
} // namespace tensorexpr
|
| 21 |
+
} // namespace jit
|
| 22 |
+
} // namespace torch
|
| 23 |
+
#endif // TORCH_ENABLE_LLVM
|
| 24 |
+
|
| 25 |
+
#else
|
| 26 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 27 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir.h
ADDED
|
@@ -0,0 +1,921 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <string>
|
| 5 |
+
#include <utility>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
#include <torch/csrc/jit/tensorexpr/exceptions.h>
|
| 9 |
+
#include <torch/csrc/jit/tensorexpr/expr.h>
|
| 10 |
+
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
|
| 11 |
+
#include <torch/csrc/jit/tensorexpr/stmt.h>
|
| 12 |
+
|
| 13 |
+
#include <ATen/core/ivalue.h>
|
| 14 |
+
|
| 15 |
+
namespace torch::jit::tensorexpr {
|
| 16 |
+
|
| 17 |
+
enum CompareSelectOperation {
|
| 18 |
+
kEQ = 0,
|
| 19 |
+
kGT,
|
| 20 |
+
kGE,
|
| 21 |
+
kLT,
|
| 22 |
+
kLE,
|
| 23 |
+
kNE,
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
enum CompareSelectBias {
|
| 27 |
+
kUnbiased,
|
| 28 |
+
kLikely,
|
| 29 |
+
kUnlikely,
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
inline int getPrecedence(IRNodeType ty) {
|
| 33 |
+
// Match C++ operator precedence rules, since some pretty-print expressions to
|
| 34 |
+
// C++. SEE: https://en.cppreference.com/w/cpp/language/operator_precedence
|
| 35 |
+
switch (ty) {
|
| 36 |
+
case kPrimitive:
|
| 37 |
+
return 0;
|
| 38 |
+
case kCast:
|
| 39 |
+
case kBitCast:
|
| 40 |
+
return 2;
|
| 41 |
+
case kAdd:
|
| 42 |
+
case kSub:
|
| 43 |
+
return 6;
|
| 44 |
+
case kMul:
|
| 45 |
+
case kDiv:
|
| 46 |
+
case kMod:
|
| 47 |
+
return 5;
|
| 48 |
+
case kMax:
|
| 49 |
+
case kMin:
|
| 50 |
+
return 99;
|
| 51 |
+
case kAnd:
|
| 52 |
+
return 11;
|
| 53 |
+
case kOr:
|
| 54 |
+
return 13;
|
| 55 |
+
case kLshift:
|
| 56 |
+
case kRshift:
|
| 57 |
+
return 7;
|
| 58 |
+
case kXor:
|
| 59 |
+
return 12;
|
| 60 |
+
case kCompareSelect:
|
| 61 |
+
return 16;
|
| 62 |
+
default:
|
| 63 |
+
return 99;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
class TORCH_API Cast : public ExprNode<Cast> {
|
| 68 |
+
public:
|
| 69 |
+
ExprPtr src_value() const {
|
| 70 |
+
return src_value_;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
void set_src_value(ExprPtr src_value) {
|
| 74 |
+
src_value_ = std::move(src_value);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
static ExprHandle make(Dtype dtype, const ExprHandle& src_value) {
|
| 78 |
+
return ExprHandle(alloc<Cast>(dtype, src_value.node()));
|
| 79 |
+
}
|
| 80 |
+
Cast(Dtype dtype, ExprPtr src_value)
|
| 81 |
+
: ExprNodeBase(dtype, kCast), src_value_(std::move(src_value)) {}
|
| 82 |
+
|
| 83 |
+
bool isConstant() const override {
|
| 84 |
+
return src_value_->isConstant();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
private:
|
| 88 |
+
ExprPtr src_value_;
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
template <typename T>
|
| 92 |
+
ExprHandle cast(const ExprHandle& src_value) {
|
| 93 |
+
return Cast::make(Dtype(ToDtype<T>(), src_value.dtype().lanes()), src_value);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// This is a bitwise cast, akin to bitcast in LLVM
|
| 97 |
+
class TORCH_API BitCast : public ExprNode<BitCast> {
|
| 98 |
+
public:
|
| 99 |
+
ExprPtr src_value() const {
|
| 100 |
+
return src_value_;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
void set_src_value(ExprPtr src_value) {
|
| 104 |
+
src_value_ = std::move(src_value);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
static ExprHandle make(Dtype dtype, const ExprHandle& src_value) {
|
| 108 |
+
return ExprHandle(alloc<BitCast>(dtype, src_value.node()));
|
| 109 |
+
}
|
| 110 |
+
BitCast(Dtype dtype, ExprPtr src_value)
|
| 111 |
+
: ExprNodeBase(dtype, kBitCast), src_value_(std::move(src_value)) {
|
| 112 |
+
TORCH_CHECK(src_value_->dtype().byte_size() == dtype.byte_size());
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
bool isConstant() const override {
|
| 116 |
+
return src_value_->isConstant();
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
private:
|
| 120 |
+
ExprPtr src_value_;
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
template <typename T>
|
| 124 |
+
ExprHandle bitcast(const ExprHandle& src_value) {
|
| 125 |
+
return BitCast::make(
|
| 126 |
+
Dtype(ToDtype<T>(), src_value.dtype().lanes()), src_value);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// Represent the expression node for binary operators.
|
| 130 |
+
// A CRTP pattern to share common code among the operators.
|
| 131 |
+
template <typename Op>
|
| 132 |
+
class BinaryOpNode : public ExprNode<Op> {
|
| 133 |
+
public:
|
| 134 |
+
ExprPtr lhs() const {
|
| 135 |
+
return this->lhs_;
|
| 136 |
+
}
|
| 137 |
+
ExprPtr rhs() const {
|
| 138 |
+
return this->rhs_;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
void set_lhs(ExprPtr lhs) {
|
| 142 |
+
lhs_ = std::move(lhs);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
void set_rhs(ExprPtr rhs) {
|
| 146 |
+
rhs_ = std::move(rhs);
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) {
|
| 150 |
+
return ExprHandle(alloc<Op>(lhs.node(), rhs.node()));
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
BinaryOpNode(
|
| 154 |
+
ExprPtr lhs_v,
|
| 155 |
+
ExprPtr rhs_v,
|
| 156 |
+
IRNodeType expr_type,
|
| 157 |
+
ScalarType ret_type = ScalarType::Undefined)
|
| 158 |
+
: ExprNode<Op>(
|
| 159 |
+
BinaryOpDtype(lhs_v->dtype(), rhs_v->dtype(), ret_type),
|
| 160 |
+
expr_type),
|
| 161 |
+
lhs_(CastIfNeeded(std::move(lhs_v), ExprNode<Op>::dtype())),
|
| 162 |
+
rhs_(CastIfNeeded(std::move(rhs_v), ExprNode<Op>::dtype())) {}
|
| 163 |
+
|
| 164 |
+
private:
|
| 165 |
+
static ExprPtr CastIfNeeded(ExprPtr expr, Dtype dst_dtype) {
|
| 166 |
+
if (expr->dtype() == dst_dtype) {
|
| 167 |
+
return expr;
|
| 168 |
+
}
|
| 169 |
+
return Cast::make(dst_dtype, ExprHandle(std::move(expr))).node();
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
ExprPtr lhs_;
|
| 173 |
+
ExprPtr rhs_;
|
| 174 |
+
};
|
| 175 |
+
|
| 176 |
+
namespace detail {
|
| 177 |
+
template <typename T>
|
| 178 |
+
void bin_op_deducer(BinaryOpNode<T>);
|
| 179 |
+
bool bin_op_deducer(...);
|
| 180 |
+
} // namespace detail
|
| 181 |
+
|
| 182 |
+
class TORCH_API Add : public BinaryOpNode<Add> {
|
| 183 |
+
public:
|
| 184 |
+
Add(ExprPtr lhs, ExprPtr rhs)
|
| 185 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kAdd) {}
|
| 186 |
+
};
|
| 187 |
+
|
| 188 |
+
class TORCH_API Sub : public BinaryOpNode<Sub> {
|
| 189 |
+
public:
|
| 190 |
+
Sub(ExprPtr lhs, ExprPtr rhs)
|
| 191 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kSub) {}
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
class TORCH_API Mul : public BinaryOpNode<Mul> {
|
| 195 |
+
public:
|
| 196 |
+
Mul(ExprPtr lhs, ExprPtr rhs)
|
| 197 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kMul) {}
|
| 198 |
+
};
|
| 199 |
+
|
| 200 |
+
class TORCH_API Div : public BinaryOpNode<Div> {
|
| 201 |
+
public:
|
| 202 |
+
Div(ExprPtr lhs, ExprPtr rhs)
|
| 203 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kDiv) {}
|
| 204 |
+
};
|
| 205 |
+
|
| 206 |
+
class TORCH_API Mod : public BinaryOpNode<Mod> {
|
| 207 |
+
public:
|
| 208 |
+
Mod(ExprPtr lhs, ExprPtr rhs)
|
| 209 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kMod) {}
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
+
template <typename Op>
|
| 213 |
+
class BitwiseOpNode : public BinaryOpNode<Op> {
|
| 214 |
+
public:
|
| 215 |
+
BitwiseOpNode(ExprPtr lhs, ExprPtr rhs, IRNodeType type)
|
| 216 |
+
: BinaryOpNode<Op>(std::move(lhs), std::move(rhs), type) {}
|
| 217 |
+
|
| 218 |
+
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) {
|
| 219 |
+
if (!lhs.dtype().is_integral()) {
|
| 220 |
+
throw unsupported_dtype();
|
| 221 |
+
}
|
| 222 |
+
if (lhs.dtype() != rhs.dtype()) {
|
| 223 |
+
throw malformed_input("lhs/rhs dtype mismatch");
|
| 224 |
+
}
|
| 225 |
+
return BinaryOpNode<Op>::make(lhs, rhs);
|
| 226 |
+
}
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
class TORCH_API And : public BitwiseOpNode<And> {
|
| 230 |
+
public:
|
| 231 |
+
And(ExprPtr lhs, ExprPtr rhs)
|
| 232 |
+
: BitwiseOpNode(std::move(lhs), std::move(rhs), IRNodeType::kAnd) {}
|
| 233 |
+
};
|
| 234 |
+
|
| 235 |
+
class TORCH_API Or : public BitwiseOpNode<Or> {
|
| 236 |
+
public:
|
| 237 |
+
Or(ExprPtr lhs, ExprPtr rhs)
|
| 238 |
+
: BitwiseOpNode(std::move(lhs), std::move(rhs), IRNodeType::kOr) {}
|
| 239 |
+
};
|
| 240 |
+
|
| 241 |
+
class TORCH_API Xor : public BitwiseOpNode<Xor> {
|
| 242 |
+
public:
|
| 243 |
+
Xor(ExprPtr lhs, ExprPtr rhs)
|
| 244 |
+
: BitwiseOpNode(std::move(lhs), std::move(rhs), IRNodeType::kXor) {}
|
| 245 |
+
};
|
| 246 |
+
|
| 247 |
+
class TORCH_API Lshift : public BitwiseOpNode<Lshift> {
|
| 248 |
+
public:
|
| 249 |
+
Lshift(ExprPtr lhs, ExprPtr rhs)
|
| 250 |
+
: BitwiseOpNode(std::move(lhs), std::move(rhs), IRNodeType::kLshift) {}
|
| 251 |
+
};
|
| 252 |
+
|
| 253 |
+
class TORCH_API Rshift : public BitwiseOpNode<Rshift> {
|
| 254 |
+
public:
|
| 255 |
+
Rshift(ExprPtr lhs, ExprPtr rhs)
|
| 256 |
+
: BitwiseOpNode(std::move(lhs), std::move(rhs), IRNodeType::kRshift) {}
|
| 257 |
+
};
|
| 258 |
+
|
| 259 |
+
// TODO: add TORCH_API
|
| 260 |
+
// Currently adding it results in a compilation error on Windows
|
| 261 |
+
class Max : public BinaryOpNode<Max> {
|
| 262 |
+
private:
|
| 263 |
+
bool propagate_nans_;
|
| 264 |
+
|
| 265 |
+
public:
|
| 266 |
+
Max(ExprPtr lhs, ExprPtr rhs, bool propagate_nans)
|
| 267 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kMax),
|
| 268 |
+
propagate_nans_(propagate_nans) {}
|
| 269 |
+
|
| 270 |
+
bool propagate_nans() const {
|
| 271 |
+
return propagate_nans_;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) = delete;
|
| 275 |
+
static ExprHandle make(
|
| 276 |
+
const ExprHandle& lhs,
|
| 277 |
+
const ExprHandle& rhs,
|
| 278 |
+
bool propagate_nans) {
|
| 279 |
+
return ExprHandle(alloc<Max>(lhs.node(), rhs.node(), propagate_nans));
|
| 280 |
+
}
|
| 281 |
+
};
|
| 282 |
+
|
| 283 |
+
// TODO: add TORCH_API
|
| 284 |
+
// Currently adding it results in a compilation error on Windows
|
| 285 |
+
class Min : public BinaryOpNode<Min> {
|
| 286 |
+
private:
|
| 287 |
+
bool propagate_nans_;
|
| 288 |
+
|
| 289 |
+
public:
|
| 290 |
+
Min(ExprPtr lhs, ExprPtr rhs, bool propagate_nans)
|
| 291 |
+
: BinaryOpNode(std::move(lhs), std::move(rhs), IRNodeType::kMin),
|
| 292 |
+
propagate_nans_(propagate_nans) {}
|
| 293 |
+
|
| 294 |
+
bool propagate_nans() const {
|
| 295 |
+
return propagate_nans_;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) = delete;
|
| 299 |
+
static ExprHandle make(
|
| 300 |
+
const ExprHandle& lhs,
|
| 301 |
+
const ExprHandle& rhs,
|
| 302 |
+
bool propagate_nans) {
|
| 303 |
+
return ExprHandle(alloc<Min>(lhs.node(), rhs.node(), propagate_nans));
|
| 304 |
+
}
|
| 305 |
+
};
|
| 306 |
+
|
| 307 |
+
// Encode typed immediate values e.g. IntImm, FloatImm.
|
| 308 |
+
#define IMM_DECLARE(Type, Name) \
|
| 309 |
+
class TORCH_API Name##Imm : public ExprNode<Name##Imm> { \
|
| 310 |
+
public: \
|
| 311 |
+
Name##Imm(Type value) \
|
| 312 |
+
: ExprNodeBase(k##Name, kPrimitive), value_(value) {} \
|
| 313 |
+
bool isConstant() const override { \
|
| 314 |
+
return true; \
|
| 315 |
+
} \
|
| 316 |
+
Type value() const { \
|
| 317 |
+
return value_; \
|
| 318 |
+
} \
|
| 319 |
+
static ExprHandle make(Type value) { \
|
| 320 |
+
return ExprHandle(alloc<Name##Imm>(value)); \
|
| 321 |
+
} \
|
| 322 |
+
\
|
| 323 |
+
private: \
|
| 324 |
+
Type value_; \
|
| 325 |
+
};
|
| 326 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_DECLARE)
|
| 327 |
+
#undef IMM_DECLARE
|
| 328 |
+
|
| 329 |
+
// Get immediate by ScalarType.
|
| 330 |
+
template <typename T>
|
| 331 |
+
ExprPtr getImmediateByType(ScalarType immType, T initialVal) {
|
| 332 |
+
switch (immType) {
|
| 333 |
+
#define TYPE_CASE(Type, Name) \
|
| 334 |
+
case ScalarType::Name: \
|
| 335 |
+
return alloc<Name##Imm>(Type(initialVal));
|
| 336 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE)
|
| 337 |
+
#undef TYPE_CASE
|
| 338 |
+
default:
|
| 339 |
+
throw unsupported_dtype();
|
| 340 |
+
}
|
| 341 |
+
return nullptr;
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
template <typename T>
|
| 345 |
+
ExprPtr getImmediateByType(Dtype dtype, T initialVal) {
|
| 346 |
+
return getImmediateByType<T>(dtype.scalar_type(), initialVal);
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
template <typename T>
|
| 350 |
+
ExprPtr immLike(const ExprPtr& e, T v) {
|
| 351 |
+
return getImmediateByType<T>(e->dtype(), v);
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
template <typename T>
|
| 355 |
+
ExprPtr immLike(const ExprHandle& e, T v) {
|
| 356 |
+
return immLike(e.node(), v);
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
inline std::optional<int64_t> intValue(const ExprPtr& e) {
|
| 360 |
+
#define TYPE_CASE(Type, Name) \
|
| 361 |
+
if (auto v = to<Name##Imm>(e)) { \
|
| 362 |
+
return v->value(); \
|
| 363 |
+
}
|
| 364 |
+
AT_FORALL_INT_TYPES(TYPE_CASE);
|
| 365 |
+
#undef TYPE_CASE
|
| 366 |
+
return std::nullopt;
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
inline std::optional<int64_t> intValue(const ExprHandle& e) {
|
| 370 |
+
return intValue(e.node());
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
template <typename T>
|
| 374 |
+
T immediateAs(const ExprPtr& e) {
|
| 375 |
+
#define TYPE_CASE(Type, Name) \
|
| 376 |
+
if (Name##ImmPtr imm = to<Name##Imm>(e)) { \
|
| 377 |
+
return imm->value(); \
|
| 378 |
+
}
|
| 379 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE)
|
| 380 |
+
#undef TYPE_CASE
|
| 381 |
+
throw unsupported_dtype();
|
| 382 |
+
return 0;
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
template <typename T>
|
| 386 |
+
T immediateAs(const ExprHandle& e) {
|
| 387 |
+
return immediateAs<T>(e.node());
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
template <typename T>
|
| 391 |
+
bool immediateEquals(const ExprPtr& e, T val) {
|
| 392 |
+
#define TYPE_CASE(Type, Name) \
|
| 393 |
+
if (Name##ImmPtr imm = to<Name##Imm>(e)) { \
|
| 394 |
+
return imm->value() == val; \
|
| 395 |
+
}
|
| 396 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE)
|
| 397 |
+
#undef TYPE_CASE
|
| 398 |
+
throw unsupported_dtype();
|
| 399 |
+
return false;
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
TORCH_API bool immediateIsNegative(const ExprPtr& e);
|
| 403 |
+
|
| 404 |
+
TORCH_API bool immediateIsPositive(const ExprPtr& e);
|
| 405 |
+
|
| 406 |
+
TORCH_API bool immediateIsZero(const ExprPtr& e);
|
| 407 |
+
|
| 408 |
+
// Represents a ramp vector node:
|
| 409 |
+
// [base, base + 1 * stride, ... , base + (lanes - 1) * stride]
|
| 410 |
+
class TORCH_API Ramp : public ExprNode<Ramp> {
|
| 411 |
+
public:
|
| 412 |
+
ExprPtr base() const {
|
| 413 |
+
return base_;
|
| 414 |
+
}
|
| 415 |
+
ExprPtr stride() const {
|
| 416 |
+
return stride_;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
void set_base(ExprPtr base) {
|
| 420 |
+
base_ = std::move(base);
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
void set_stride(ExprPtr stride) {
|
| 424 |
+
stride_ = std::move(stride);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
static ExprHandle make(
|
| 428 |
+
const ExprHandle& base,
|
| 429 |
+
const ExprHandle& stride,
|
| 430 |
+
int64_t lanes) {
|
| 431 |
+
if (stride.dtype() != base.dtype()) {
|
| 432 |
+
throw malformed_input("Bad stride in Ramp");
|
| 433 |
+
}
|
| 434 |
+
return ExprHandle(alloc<Ramp>(base.node(), stride.node(), lanes));
|
| 435 |
+
}
|
| 436 |
+
int64_t lanes() const {
|
| 437 |
+
return lanes_;
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
Ramp(ExprPtr base, ExprPtr stride, int64_t lanes)
|
| 441 |
+
: ExprNodeBase(Dtype(base->dtype(), lanes)),
|
| 442 |
+
base_(std::move(base)),
|
| 443 |
+
stride_(std::move(stride)),
|
| 444 |
+
lanes_(lanes) {}
|
| 445 |
+
|
| 446 |
+
private:
|
| 447 |
+
ExprPtr base_;
|
| 448 |
+
ExprPtr stride_;
|
| 449 |
+
int64_t lanes_;
|
| 450 |
+
};
|
| 451 |
+
|
| 452 |
+
class TORCH_API Load : public ExprNode<Load> {
|
| 453 |
+
public:
|
| 454 |
+
VarPtr base_handle() const {
|
| 455 |
+
return buf_->base_handle();
|
| 456 |
+
}
|
| 457 |
+
std::vector<ExprPtr> indices() const {
|
| 458 |
+
return indices_;
|
| 459 |
+
}
|
| 460 |
+
ExprPtr flat_index() const {
|
| 461 |
+
TORCH_CHECK(indices_.size() == 1, "Indices haven't been flattened.");
|
| 462 |
+
return indices_[0];
|
| 463 |
+
}
|
| 464 |
+
BufPtr buf() const {
|
| 465 |
+
return buf_;
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
void set_buf(BufPtr buf) {
|
| 469 |
+
buf_ = std::move(buf);
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
void set_indices(std::vector<ExprPtr> indices) {
|
| 473 |
+
indices_ = std::move(indices);
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
static ExprHandle make(
|
| 477 |
+
Dtype dtype,
|
| 478 |
+
const BufHandle& buf,
|
| 479 |
+
const std::vector<ExprHandle>& indices);
|
| 480 |
+
static ExprHandle make(
|
| 481 |
+
const BufHandle& buf,
|
| 482 |
+
const std::vector<ExprHandle>& indices);
|
| 483 |
+
|
| 484 |
+
Load(Dtype dtype, BufPtr base_handle, std::vector<ExprPtr> indices);
|
| 485 |
+
Load(const BufPtr& base_handle, const std::vector<ExprPtr>& indices);
|
| 486 |
+
|
| 487 |
+
private:
|
| 488 |
+
BufPtr buf_;
|
| 489 |
+
std::vector<ExprPtr> indices_;
|
| 490 |
+
};
|
| 491 |
+
|
| 492 |
+
class TORCH_API Broadcast : public ExprNode<Broadcast> {
|
| 493 |
+
public:
|
| 494 |
+
ExprPtr value() const {
|
| 495 |
+
return value_;
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
void set_value(ExprPtr value) {
|
| 499 |
+
value_ = std::move(value);
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
int64_t lanes() const {
|
| 503 |
+
return lanes_;
|
| 504 |
+
}
|
| 505 |
+
static ExprHandle make(const ExprHandle& value, int64_t lanes) {
|
| 506 |
+
return ExprHandle(alloc<Broadcast>(value.node(), lanes));
|
| 507 |
+
}
|
| 508 |
+
Broadcast(ExprPtr value, int64_t lanes)
|
| 509 |
+
: ExprNodeBase(Dtype(value->dtype(), lanes)),
|
| 510 |
+
value_(std::move(value)),
|
| 511 |
+
lanes_(lanes) {}
|
| 512 |
+
|
| 513 |
+
private:
|
| 514 |
+
ExprPtr value_;
|
| 515 |
+
int64_t lanes_;
|
| 516 |
+
};
|
| 517 |
+
|
| 518 |
+
class TORCH_API IfThenElse : public ExprNode<IfThenElse> {
|
| 519 |
+
public:
|
| 520 |
+
ExprPtr condition() const {
|
| 521 |
+
return condition_;
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
// Lazily evaluated only if condition is true
|
| 525 |
+
ExprPtr true_value() const {
|
| 526 |
+
return true_;
|
| 527 |
+
}
|
| 528 |
+
|
| 529 |
+
// Lazily evaluated only if condition is false
|
| 530 |
+
ExprPtr false_value() const {
|
| 531 |
+
return false_;
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
void set_condition(ExprPtr condition) {
|
| 535 |
+
condition_ = std::move(condition);
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
void set_true_value(ExprPtr true_value) {
|
| 539 |
+
true_ = std::move(true_value);
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
void set_false_value(ExprPtr false_value) {
|
| 543 |
+
false_ = std::move(false_value);
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
static ExprHandle make(
|
| 547 |
+
const ExprHandle& c,
|
| 548 |
+
const ExprHandle& t,
|
| 549 |
+
const ExprHandle& f) {
|
| 550 |
+
if (!c.dtype().is_integral()) {
|
| 551 |
+
throw unsupported_dtype();
|
| 552 |
+
}
|
| 553 |
+
if (c.dtype().lanes() != 1) {
|
| 554 |
+
throw unsupported_dtype();
|
| 555 |
+
}
|
| 556 |
+
if (t.dtype() != f.dtype()) {
|
| 557 |
+
throw malformed_input("Bad dtype in IfThenElse");
|
| 558 |
+
}
|
| 559 |
+
return ExprHandle(alloc<IfThenElse>(c.node(), t.node(), f.node()));
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
IfThenElse(ExprPtr c, ExprPtr t, ExprPtr f)
|
| 563 |
+
: ExprNodeBase(t->dtype()),
|
| 564 |
+
condition_(std::move(c)),
|
| 565 |
+
true_(std::move(t)),
|
| 566 |
+
false_(std::move(f)) {}
|
| 567 |
+
|
| 568 |
+
private:
|
| 569 |
+
ExprPtr condition_;
|
| 570 |
+
ExprPtr true_;
|
| 571 |
+
ExprPtr false_;
|
| 572 |
+
};
|
| 573 |
+
|
| 574 |
+
class TORCH_API CompareSelect : public ExprNode<CompareSelect> {
|
| 575 |
+
public:
|
| 576 |
+
CompareSelectOperation compare_select_op() const {
|
| 577 |
+
return compare_op_;
|
| 578 |
+
}
|
| 579 |
+
ExprPtr lhs() const {
|
| 580 |
+
return this->lhs_;
|
| 581 |
+
}
|
| 582 |
+
ExprPtr rhs() const {
|
| 583 |
+
return this->rhs_;
|
| 584 |
+
}
|
| 585 |
+
ExprPtr ret_val1() const {
|
| 586 |
+
return this->ret_val1_;
|
| 587 |
+
}
|
| 588 |
+
ExprPtr ret_val2() const {
|
| 589 |
+
return this->ret_val2_;
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
void set_lhs(ExprPtr lhs) {
|
| 593 |
+
lhs_ = std::move(lhs);
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
void set_rhs(ExprPtr rhs) {
|
| 597 |
+
rhs_ = std::move(rhs);
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
void set_ret_val1(ExprPtr ret_val1) {
|
| 601 |
+
ret_val1_ = std::move(ret_val1);
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
void set_ret_val2(ExprPtr ret_val2) {
|
| 605 |
+
ret_val2_ = std::move(ret_val2);
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
CompareSelectBias bias() const {
|
| 609 |
+
return bias_;
|
| 610 |
+
}
|
| 611 |
+
|
| 612 |
+
static ExprHandle make(
|
| 613 |
+
const ExprHandle& lhs,
|
| 614 |
+
const ExprHandle& rhs,
|
| 615 |
+
CompareSelectOperation cmp_op,
|
| 616 |
+
CompareSelectBias bias = kUnbiased) {
|
| 617 |
+
if (lhs.dtype() != rhs.dtype()) {
|
| 618 |
+
throw malformed_input("bad dtype in CompareSelect");
|
| 619 |
+
}
|
| 620 |
+
return ExprHandle(alloc<CompareSelect>(
|
| 621 |
+
lhs.node(),
|
| 622 |
+
rhs.node(),
|
| 623 |
+
IntImm::make(1).node(),
|
| 624 |
+
IntImm::make(0).node(),
|
| 625 |
+
cmp_op,
|
| 626 |
+
bias));
|
| 627 |
+
}
|
| 628 |
+
|
| 629 |
+
static ExprHandle make(
|
| 630 |
+
const ExprHandle& lhs,
|
| 631 |
+
const ExprHandle& rhs,
|
| 632 |
+
const ExprHandle& ret_val1,
|
| 633 |
+
const ExprHandle& ret_val2,
|
| 634 |
+
CompareSelectOperation cmp_op,
|
| 635 |
+
CompareSelectBias bias = kUnbiased) {
|
| 636 |
+
if (lhs.dtype() != rhs.dtype() || ret_val1.dtype() != ret_val2.dtype()) {
|
| 637 |
+
throw malformed_input("bad dtype in CompareSelect");
|
| 638 |
+
}
|
| 639 |
+
return ExprHandle(alloc<CompareSelect>(
|
| 640 |
+
lhs.node(),
|
| 641 |
+
rhs.node(),
|
| 642 |
+
ret_val1.node(),
|
| 643 |
+
ret_val2.node(),
|
| 644 |
+
cmp_op,
|
| 645 |
+
bias));
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
CompareSelect(
|
| 649 |
+
ExprPtr lhs,
|
| 650 |
+
ExprPtr rhs,
|
| 651 |
+
ExprPtr ret_val1,
|
| 652 |
+
ExprPtr ret_val2,
|
| 653 |
+
CompareSelectOperation cmp_op,
|
| 654 |
+
CompareSelectBias bias = kUnbiased)
|
| 655 |
+
: ExprNodeBase(ret_val1->dtype()),
|
| 656 |
+
lhs_(std::move(lhs)),
|
| 657 |
+
rhs_(std::move(rhs)),
|
| 658 |
+
ret_val1_(std::move(ret_val1)),
|
| 659 |
+
ret_val2_(std::move(ret_val2)),
|
| 660 |
+
compare_op_(cmp_op),
|
| 661 |
+
bias_(bias) {}
|
| 662 |
+
|
| 663 |
+
CompareSelect(
|
| 664 |
+
ExprPtr lhs,
|
| 665 |
+
ExprPtr rhs,
|
| 666 |
+
CompareSelectOperation cmp_op,
|
| 667 |
+
CompareSelectBias bias = kUnbiased)
|
| 668 |
+
: ExprNodeBase(kInt),
|
| 669 |
+
lhs_(std::move(lhs)),
|
| 670 |
+
rhs_(std::move(rhs)),
|
| 671 |
+
ret_val1_(alloc<IntImm>(1)),
|
| 672 |
+
ret_val2_(alloc<IntImm>(0)),
|
| 673 |
+
compare_op_(cmp_op),
|
| 674 |
+
bias_(bias) {}
|
| 675 |
+
|
| 676 |
+
private:
|
| 677 |
+
ExprPtr lhs_;
|
| 678 |
+
ExprPtr rhs_;
|
| 679 |
+
ExprPtr ret_val1_;
|
| 680 |
+
ExprPtr ret_val2_;
|
| 681 |
+
CompareSelectOperation compare_op_;
|
| 682 |
+
CompareSelectBias bias_;
|
| 683 |
+
};
|
| 684 |
+
|
| 685 |
+
enum IntrinsicsOp {
|
| 686 |
+
kSin,
|
| 687 |
+
kCos,
|
| 688 |
+
kTan,
|
| 689 |
+
kAsin,
|
| 690 |
+
kAcos,
|
| 691 |
+
kAtan,
|
| 692 |
+
kAtan2,
|
| 693 |
+
kSinh,
|
| 694 |
+
kCosh,
|
| 695 |
+
kTanh,
|
| 696 |
+
kSigmoid,
|
| 697 |
+
kExp,
|
| 698 |
+
kExpm1,
|
| 699 |
+
kAbs,
|
| 700 |
+
kLog,
|
| 701 |
+
kLog2,
|
| 702 |
+
kLog10,
|
| 703 |
+
kLog1p,
|
| 704 |
+
kErf,
|
| 705 |
+
kErfc,
|
| 706 |
+
kSqrt,
|
| 707 |
+
kRsqrt,
|
| 708 |
+
kPow,
|
| 709 |
+
kCeil,
|
| 710 |
+
kFloor,
|
| 711 |
+
kRound,
|
| 712 |
+
kTrunc,
|
| 713 |
+
kFmod,
|
| 714 |
+
kRemainder,
|
| 715 |
+
kLgamma,
|
| 716 |
+
kFrac,
|
| 717 |
+
kIsNan,
|
| 718 |
+
kRand, // We need more discussions on this. Should we consider stateful?
|
| 719 |
+
kMaxIntrinsicsOp,
|
| 720 |
+
};
|
| 721 |
+
|
| 722 |
+
class TORCH_API Intrinsics : public ExprNode<Intrinsics> {
|
| 723 |
+
public:
|
| 724 |
+
static ExprHandle make(IntrinsicsOp op_type, const ExprHandle& v1) {
|
| 725 |
+
return ExprHandle(alloc<Intrinsics>(op_type, v1.node()));
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
static ExprHandle make(
|
| 729 |
+
IntrinsicsOp op_type,
|
| 730 |
+
const ExprHandle& v1,
|
| 731 |
+
const ExprHandle& v2) {
|
| 732 |
+
return ExprHandle(alloc<Intrinsics>(op_type, v1.node(), v2.node()));
|
| 733 |
+
}
|
| 734 |
+
|
| 735 |
+
static ExprHandle make(
|
| 736 |
+
IntrinsicsOp op_type,
|
| 737 |
+
const std::vector<ExprHandle>& params) {
|
| 738 |
+
std::vector<ExprPtr> params_nodes(params.size());
|
| 739 |
+
for (size_t i = 0; i < params.size(); i++) {
|
| 740 |
+
params_nodes[i] = params[i].node();
|
| 741 |
+
}
|
| 742 |
+
return ExprHandle(alloc<Intrinsics>(op_type, params_nodes));
|
| 743 |
+
}
|
| 744 |
+
|
| 745 |
+
static ExprHandle make(IntrinsicsOp op_type, Dtype dtype) {
|
| 746 |
+
return ExprHandle(alloc<Intrinsics>(op_type, dtype));
|
| 747 |
+
}
|
| 748 |
+
|
| 749 |
+
IntrinsicsOp op_type() const {
|
| 750 |
+
return op_type_;
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
std::string func_name() const {
|
| 754 |
+
switch (op_type()) {
|
| 755 |
+
case kSin:
|
| 756 |
+
return "sin";
|
| 757 |
+
case kCos:
|
| 758 |
+
return "cos";
|
| 759 |
+
case kTan:
|
| 760 |
+
return "tan";
|
| 761 |
+
case kAsin:
|
| 762 |
+
return "asin";
|
| 763 |
+
case kAcos:
|
| 764 |
+
return "acos";
|
| 765 |
+
case kAtan:
|
| 766 |
+
return "atan";
|
| 767 |
+
case kAtan2:
|
| 768 |
+
return "atan2";
|
| 769 |
+
case kSinh:
|
| 770 |
+
return "sinh";
|
| 771 |
+
case kCosh:
|
| 772 |
+
return "cosh";
|
| 773 |
+
case kTanh:
|
| 774 |
+
return "tanh";
|
| 775 |
+
case kSigmoid:
|
| 776 |
+
return "sigmoid";
|
| 777 |
+
case kExp:
|
| 778 |
+
return "exp";
|
| 779 |
+
case kAbs:
|
| 780 |
+
return "abs";
|
| 781 |
+
case kLog:
|
| 782 |
+
return "log";
|
| 783 |
+
case kLog2:
|
| 784 |
+
return "log2";
|
| 785 |
+
case kLog10:
|
| 786 |
+
return "log10";
|
| 787 |
+
case kLog1p:
|
| 788 |
+
return "log1p";
|
| 789 |
+
case kErf:
|
| 790 |
+
return "erf";
|
| 791 |
+
case kSqrt:
|
| 792 |
+
return "sqrt";
|
| 793 |
+
case kRsqrt:
|
| 794 |
+
return "rsqrt";
|
| 795 |
+
case kPow:
|
| 796 |
+
return "pow";
|
| 797 |
+
case kCeil:
|
| 798 |
+
return "ceil";
|
| 799 |
+
case kFloor:
|
| 800 |
+
return "floor";
|
| 801 |
+
case kRound:
|
| 802 |
+
return "round";
|
| 803 |
+
case kTrunc:
|
| 804 |
+
return "trunc";
|
| 805 |
+
case kRand:
|
| 806 |
+
return "rand";
|
| 807 |
+
case kFmod:
|
| 808 |
+
return "fmod";
|
| 809 |
+
case kRemainder:
|
| 810 |
+
return "remainder";
|
| 811 |
+
case kLgamma:
|
| 812 |
+
return "lgamma";
|
| 813 |
+
case kExpm1:
|
| 814 |
+
return "expm1";
|
| 815 |
+
case kErfc:
|
| 816 |
+
return "erfc";
|
| 817 |
+
case kFrac:
|
| 818 |
+
return "frac";
|
| 819 |
+
case kIsNan:
|
| 820 |
+
return "isnan";
|
| 821 |
+
default:
|
| 822 |
+
throw std::runtime_error(
|
| 823 |
+
"invalid op_type: " + std::to_string(op_type()));
|
| 824 |
+
}
|
| 825 |
+
}
|
| 826 |
+
|
| 827 |
+
Intrinsics(IntrinsicsOp op_type, Dtype dtype)
|
| 828 |
+
: ExprNodeBase(IntrinsicsDtype(op_type, dtype)),
|
| 829 |
+
params_({}),
|
| 830 |
+
op_type_(op_type) {
|
| 831 |
+
if (OpArgCount(op_type) != 0) {
|
| 832 |
+
throw malformed_input("bad arg count in Intrinsics");
|
| 833 |
+
}
|
| 834 |
+
}
|
| 835 |
+
|
| 836 |
+
Intrinsics(IntrinsicsOp op_type, ExprPtr v1)
|
| 837 |
+
: ExprNodeBase(IntrinsicsDtype(op_type, v1->dtype())),
|
| 838 |
+
params_({std::move(v1)}),
|
| 839 |
+
op_type_(op_type) {
|
| 840 |
+
if (OpArgCount(op_type) != 1) {
|
| 841 |
+
throw malformed_input("bad arg count in Intrinsics");
|
| 842 |
+
}
|
| 843 |
+
}
|
| 844 |
+
|
| 845 |
+
Intrinsics(IntrinsicsOp op_type, ExprPtr v1, ExprPtr v2)
|
| 846 |
+
: ExprNodeBase(IntrinsicsDtype(op_type, v1->dtype(), v2->dtype())),
|
| 847 |
+
params_({std::move(v1), std::move(v2)}),
|
| 848 |
+
op_type_(op_type) {
|
| 849 |
+
if (OpArgCount(op_type) != 2) {
|
| 850 |
+
throw malformed_input("bad arg count in Intrinsics");
|
| 851 |
+
}
|
| 852 |
+
}
|
| 853 |
+
|
| 854 |
+
Intrinsics(IntrinsicsOp op_type, const std::vector<ExprPtr>& params)
|
| 855 |
+
: ExprNodeBase(IntrinsicsDtype(op_type, params)),
|
| 856 |
+
params_(params),
|
| 857 |
+
op_type_(op_type) {
|
| 858 |
+
if (OpArgCount(op_type) != nparams()) {
|
| 859 |
+
throw malformed_input("bad arg count in Intrinsics");
|
| 860 |
+
}
|
| 861 |
+
}
|
| 862 |
+
|
| 863 |
+
Intrinsics(IntrinsicsOp op_type, Dtype dtype, std::vector<ExprPtr> params)
|
| 864 |
+
: ExprNodeBase(IntrinsicsDtype(op_type, dtype)),
|
| 865 |
+
params_(std::move(params)),
|
| 866 |
+
op_type_(op_type) {
|
| 867 |
+
if (OpArgCount(op_type) != nparams()) {
|
| 868 |
+
throw malformed_input("bad arg count in Intrinsics");
|
| 869 |
+
}
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
bool isPure() const {
|
| 873 |
+
return op_type_ != kRand;
|
| 874 |
+
}
|
| 875 |
+
|
| 876 |
+
size_t nparams() const {
|
| 877 |
+
return params_.size();
|
| 878 |
+
}
|
| 879 |
+
|
| 880 |
+
ExprPtr param(size_t index) const {
|
| 881 |
+
return params_[index];
|
| 882 |
+
}
|
| 883 |
+
const std::vector<ExprPtr>& params() const {
|
| 884 |
+
return params_;
|
| 885 |
+
}
|
| 886 |
+
|
| 887 |
+
void set_params(std::vector<ExprPtr> params) {
|
| 888 |
+
params_ = std::move(params);
|
| 889 |
+
}
|
| 890 |
+
|
| 891 |
+
static size_t OpArgCount(IntrinsicsOp op_type);
|
| 892 |
+
|
| 893 |
+
private:
|
| 894 |
+
static Dtype IntrinsicsDtype(IntrinsicsOp op_type, Dtype dt1);
|
| 895 |
+
static Dtype IntrinsicsDtype(IntrinsicsOp op_type, Dtype dt1, Dtype dt2);
|
| 896 |
+
static Dtype IntrinsicsDtype(
|
| 897 |
+
IntrinsicsOp op_type,
|
| 898 |
+
const std::vector<ExprPtr>& params);
|
| 899 |
+
|
| 900 |
+
std::vector<ExprPtr> params_;
|
| 901 |
+
IntrinsicsOp op_type_;
|
| 902 |
+
};
|
| 903 |
+
|
| 904 |
+
TORCH_API std::vector<ExprPtr> ExprHandleVectorToExprVector(
|
| 905 |
+
const std::vector<ExprHandle>& /*v*/);
|
| 906 |
+
TORCH_API std::vector<ExprHandle> ExprVectorToExprHandleVector(
|
| 907 |
+
const std::vector<ExprPtr>& /*v*/);
|
| 908 |
+
TORCH_API std::vector<VarPtr> VarHandleVectorToVarVector(
|
| 909 |
+
const std::vector<VarHandle>& /*v*/);
|
| 910 |
+
TORCH_API std::vector<VarHandle> VarVectorToVarHandleVector(
|
| 911 |
+
const std::vector<VarPtr>& /*v*/);
|
| 912 |
+
TORCH_API ExprPtr flatten_index(
|
| 913 |
+
const std::vector<ExprPtr>& dims,
|
| 914 |
+
const std::vector<ExprPtr>& indices,
|
| 915 |
+
const std::vector<ExprPtr>& strides);
|
| 916 |
+
|
| 917 |
+
} // namespace torch::jit::tensorexpr
|
| 918 |
+
|
| 919 |
+
#else
|
| 920 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 921 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_cloner.h
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/ScalarType.h>
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <vector>
|
| 6 |
+
|
| 7 |
+
#include <torch/csrc/jit/tensorexpr/ir_mutator.h>
|
| 8 |
+
|
| 9 |
+
namespace torch::jit::tensorexpr {
|
| 10 |
+
|
| 11 |
+
class TORCH_API IRCloner : public IRMutator {
|
| 12 |
+
public:
|
| 13 |
+
~IRCloner() override = default;
|
| 14 |
+
ExprPtr mutate(const AddPtr& v) override;
|
| 15 |
+
ExprPtr mutate(const SubPtr& v) override;
|
| 16 |
+
ExprPtr mutate(const MulPtr& v) override;
|
| 17 |
+
ExprPtr mutate(const DivPtr& v) override;
|
| 18 |
+
ExprPtr mutate(const ModPtr& v) override;
|
| 19 |
+
ExprPtr mutate(const MaxPtr& v) override;
|
| 20 |
+
ExprPtr mutate(const MinPtr& v) override;
|
| 21 |
+
ExprPtr mutate(const AndPtr& v) override;
|
| 22 |
+
ExprPtr mutate(const OrPtr& v) override;
|
| 23 |
+
ExprPtr mutate(const XorPtr& v) override;
|
| 24 |
+
ExprPtr mutate(const LshiftPtr& v) override;
|
| 25 |
+
ExprPtr mutate(const RshiftPtr& v) override;
|
| 26 |
+
ExprPtr mutate(const CompareSelectPtr& v) override;
|
| 27 |
+
#define IMM_MUTATE_DECLARE(Type, Name) \
|
| 28 |
+
ExprPtr mutate(const Name##ImmPtr& v) override;
|
| 29 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_MUTATE_DECLARE)
|
| 30 |
+
#undef IMM_MUTATE_DECLARE
|
| 31 |
+
ExprPtr mutate(const CastPtr& v) override;
|
| 32 |
+
ExprPtr mutate(const BitCastPtr& v) override;
|
| 33 |
+
ExprPtr mutate(const VarPtr& v) override;
|
| 34 |
+
ExprPtr mutate(const BufPtr& v) override;
|
| 35 |
+
ExprPtr mutate(const RampPtr& v) override;
|
| 36 |
+
ExprPtr mutate(const LoadPtr& v) override;
|
| 37 |
+
ExprPtr mutate(const BroadcastPtr& v) override;
|
| 38 |
+
ExprPtr mutate(const IfThenElsePtr& v) override;
|
| 39 |
+
ExprPtr mutate(const IntrinsicsPtr& v) override;
|
| 40 |
+
|
| 41 |
+
ExprPtr mutate(const TermPtr& v) override;
|
| 42 |
+
ExprPtr mutate(const PolynomialPtr& v) override;
|
| 43 |
+
ExprPtr mutate(const RoundOffPtr& v) override;
|
| 44 |
+
ExprPtr mutate(const MaxTermPtr& v) override;
|
| 45 |
+
ExprPtr mutate(const MinTermPtr& v) override;
|
| 46 |
+
|
| 47 |
+
ExprPtr mutate(const ReduceOpPtr& v) override;
|
| 48 |
+
|
| 49 |
+
StmtPtr mutate(const ForPtr& v) override;
|
| 50 |
+
StmtPtr mutate(const BlockPtr& v) override;
|
| 51 |
+
StmtPtr mutate(const StorePtr& v) override;
|
| 52 |
+
StmtPtr mutate(const AtomicAddPtr& v) override;
|
| 53 |
+
StmtPtr mutate(const SyncThreadsPtr& v) override;
|
| 54 |
+
StmtPtr mutate(const ExternalCallPtr& v) override;
|
| 55 |
+
StmtPtr mutate(const ExternalCallWithAllocPtr& v) override;
|
| 56 |
+
|
| 57 |
+
StmtPtr mutate(const AllocatePtr& v) override;
|
| 58 |
+
StmtPtr mutate(const FreePtr& v) override;
|
| 59 |
+
StmtPtr mutate(const LetPtr& v) override;
|
| 60 |
+
StmtPtr mutate(const CondPtr& v) override;
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
} // namespace torch::jit::tensorexpr
|
| 64 |
+
|
| 65 |
+
#else
|
| 66 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 67 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_mutator.h
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/ScalarType.h>
|
| 4 |
+
#include <torch/csrc/Export.h>
|
| 5 |
+
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
|
| 6 |
+
|
| 7 |
+
namespace torch::jit::tensorexpr {
|
| 8 |
+
|
| 9 |
+
class TORCH_API IRMutator {
|
| 10 |
+
public:
|
| 11 |
+
virtual ~IRMutator() = default;
|
| 12 |
+
virtual ExprPtr mutate(const AddPtr& v);
|
| 13 |
+
virtual ExprPtr mutate(const SubPtr& v);
|
| 14 |
+
virtual ExprPtr mutate(const MulPtr& v);
|
| 15 |
+
virtual ExprPtr mutate(const DivPtr& v);
|
| 16 |
+
virtual ExprPtr mutate(const ModPtr& v);
|
| 17 |
+
virtual ExprPtr mutate(const MaxPtr& v);
|
| 18 |
+
virtual ExprPtr mutate(const MinPtr& v);
|
| 19 |
+
virtual ExprPtr mutate(const AndPtr& v);
|
| 20 |
+
virtual ExprPtr mutate(const OrPtr& v);
|
| 21 |
+
virtual ExprPtr mutate(const XorPtr& v);
|
| 22 |
+
virtual ExprPtr mutate(const LshiftPtr& v);
|
| 23 |
+
virtual ExprPtr mutate(const RshiftPtr& v);
|
| 24 |
+
virtual ExprPtr mutate(const CompareSelectPtr& v);
|
| 25 |
+
#define IMM_MUTATE_DECLARE(Type, Name) \
|
| 26 |
+
virtual ExprPtr mutate(const Name##ImmPtr& v);
|
| 27 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_MUTATE_DECLARE)
|
| 28 |
+
#undef IMM_MUTATE_DECLARE
|
| 29 |
+
virtual ExprPtr mutate(const CastPtr& v);
|
| 30 |
+
virtual ExprPtr mutate(const BitCastPtr& v);
|
| 31 |
+
virtual ExprPtr mutate(const VarPtr& v);
|
| 32 |
+
virtual ExprPtr mutate(const BufPtr& v);
|
| 33 |
+
virtual ExprPtr mutate(const RampPtr& v);
|
| 34 |
+
virtual ExprPtr mutate(const LoadPtr& v);
|
| 35 |
+
virtual ExprPtr mutate(const BroadcastPtr& v);
|
| 36 |
+
virtual ExprPtr mutate(const IfThenElsePtr& v);
|
| 37 |
+
virtual ExprPtr mutate(const IntrinsicsPtr& v);
|
| 38 |
+
|
| 39 |
+
virtual ExprPtr mutate(const TermPtr& v);
|
| 40 |
+
virtual ExprPtr mutate(const PolynomialPtr& v);
|
| 41 |
+
virtual ExprPtr mutate(const RoundOffPtr& v);
|
| 42 |
+
virtual ExprPtr mutate(const MaxTermPtr& v);
|
| 43 |
+
virtual ExprPtr mutate(const MinTermPtr& v);
|
| 44 |
+
|
| 45 |
+
virtual ExprPtr mutate(const ReduceOpPtr& v);
|
| 46 |
+
|
| 47 |
+
virtual StmtPtr mutate(const ForPtr& v);
|
| 48 |
+
virtual StmtPtr mutate(const BlockPtr& v);
|
| 49 |
+
virtual StmtPtr mutate(const StorePtr& v);
|
| 50 |
+
virtual StmtPtr mutate(const AtomicAddPtr& v);
|
| 51 |
+
virtual StmtPtr mutate(const SyncThreadsPtr& v);
|
| 52 |
+
virtual StmtPtr mutate(const ExternalCallPtr& v);
|
| 53 |
+
virtual StmtPtr mutate(const ExternalCallWithAllocPtr& v);
|
| 54 |
+
|
| 55 |
+
virtual StmtPtr mutate(const AllocatePtr& v);
|
| 56 |
+
virtual StmtPtr mutate(const FreePtr& v);
|
| 57 |
+
virtual StmtPtr mutate(const FreeExtPtr& v);
|
| 58 |
+
virtual StmtPtr mutate(const PlacementAllocatePtr& v);
|
| 59 |
+
virtual StmtPtr mutate(const LetPtr& v);
|
| 60 |
+
virtual StmtPtr mutate(const CondPtr& v);
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
} // namespace torch::jit::tensorexpr
|
| 64 |
+
|
| 65 |
+
#else
|
| 66 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 67 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
.venv/Lib/site-packages/torch/include/torch/csrc/jit/tensorexpr/ir_printer.h
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ostream>
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
|
| 7 |
+
#include <torch/csrc/jit/tensorexpr/ir.h>
|
| 8 |
+
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
|
| 9 |
+
#include <torch/csrc/jit/tensorexpr/unique_name_manager.h>
|
| 10 |
+
|
| 11 |
+
namespace torch::jit::tensorexpr {
|
| 12 |
+
|
| 13 |
+
class Tensor;
|
| 14 |
+
|
| 15 |
+
class TORCH_API IRPrinter : public IRVisitor {
|
| 16 |
+
public:
|
| 17 |
+
explicit IRPrinter(std::ostream& os) : printer_os_(this, os) {}
|
| 18 |
+
|
| 19 |
+
void print(ExprHandle /*expr*/);
|
| 20 |
+
void print(Expr& /*expr*/);
|
| 21 |
+
void print(Stmt& /*stmt*/);
|
| 22 |
+
void visit(const AddPtr& v) override;
|
| 23 |
+
void visit(const SubPtr& v) override;
|
| 24 |
+
void visit(const MulPtr& v) override;
|
| 25 |
+
void visit(const DivPtr& v) override;
|
| 26 |
+
void visit(const ModPtr& v) override;
|
| 27 |
+
void visit(const MaxPtr& v) override;
|
| 28 |
+
void visit(const MinPtr& v) override;
|
| 29 |
+
void visit(const AndPtr& v) override;
|
| 30 |
+
void visit(const OrPtr& v) override;
|
| 31 |
+
void visit(const XorPtr& v) override;
|
| 32 |
+
void visit(const LshiftPtr& v) override;
|
| 33 |
+
void visit(const RshiftPtr& v) override;
|
| 34 |
+
void visit(const CompareSelectPtr& v) override;
|
| 35 |
+
#define IMM_PRINT_VISIT(Type, Name) void visit(const Name##ImmPtr& v) override;
|
| 36 |
+
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_PRINT_VISIT)
|
| 37 |
+
#undef IMM_PRINT_VISIT
|
| 38 |
+
void visit(const CastPtr& v) override;
|
| 39 |
+
void visit(const BitCastPtr& v) override;
|
| 40 |
+
void visit(const VarPtr& v) override;
|
| 41 |
+
void visit(const BufPtr& v) override;
|
| 42 |
+
void visit(const RampPtr& v) override;
|
| 43 |
+
void visit(const LoadPtr& v) override;
|
| 44 |
+
void visit(const BroadcastPtr& v) override;
|
| 45 |
+
void visit(const IfThenElsePtr& v) override;
|
| 46 |
+
void visit(const IntrinsicsPtr& v) override;
|
| 47 |
+
void visit(const TermPtr& v) override;
|
| 48 |
+
void visit(const PolynomialPtr& v) override;
|
| 49 |
+
void visit(const RoundOffPtr& v) override;
|
| 50 |
+
void visit(const MaxTermPtr& v) override;
|
| 51 |
+
void visit(const MinTermPtr& v) override;
|
| 52 |
+
void visit(const ReduceOpPtr& v) override;
|
| 53 |
+
|
| 54 |
+
void visit(const AtomicAddPtr& v) override;
|
| 55 |
+
void visit(const SyncThreadsPtr& v) override;
|
| 56 |
+
void visit(const ExternalCallPtr& v) override;
|
| 57 |
+
void visit(const ExternalCallWithAllocPtr& v) override;
|
| 58 |
+
void visit(const StorePtr& v) override;
|
| 59 |
+
void visit(const ForPtr& v) override;
|
| 60 |
+
void visit(const CondPtr& v) override;
|
| 61 |
+
void visit(const BlockPtr& v) override;
|
| 62 |
+
void visit(const AllocatePtr& v) override;
|
| 63 |
+
void visit(const FreePtr& v) override;
|
| 64 |
+
void visit(const FreeExtPtr& v) override;
|
| 65 |
+
void visit(const PlacementAllocatePtr& v) override;
|
| 66 |
+
void visit(const LetPtr& v) override;
|
| 67 |
+
|
| 68 |
+
// A child class may have a difference rule for generating dtype
|
| 69 |
+
// string, e.g. CUDA needs int64_t to be generated as long long.
|
| 70 |
+
virtual std::string dtypeToCppString(const Dtype& dtype);
|
| 71 |
+
|
| 72 |
+
std::ostream& os() {
|
| 73 |
+
return printer_os_;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
class PrinterStream : public std::ostream {
|
| 77 |
+
public:
|
| 78 |
+
PrinterStream(IRPrinter* printer, std::ostream& os)
|
| 79 |
+
: std::ostream(os.rdbuf()), printer_(printer) {
|
| 80 |
+
initialize_imbue();
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
void initialize_imbue();
|
| 84 |
+
|
| 85 |
+
IRPrinter* printer() {
|
| 86 |
+
return printer_;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
private:
|
| 90 |
+
IRPrinter* printer_ = nullptr;
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
protected:
|
| 94 |
+
std::string to_string(CompareSelectOperation op);
|
| 95 |
+
|
| 96 |
+
UniqueNameManager* name_manager() {
|
| 97 |
+
return &name_manager_;
|
| 98 |
+
}
|
| 99 |
+
void emitIndent();
|
| 100 |
+
|
| 101 |
+
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
|
| 102 |
+
int indent_ = 0;
|
| 103 |
+
|
| 104 |
+
private:
|
| 105 |
+
PrinterStream printer_os_;
|
| 106 |
+
UniqueNameManager name_manager_;
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
TORCH_API std::ostream& operator<<(std::ostream& stream, const Expr& /*expr*/);
|
| 110 |
+
TORCH_API std::ostream& operator<<(
|
| 111 |
+
std::ostream& stream,
|
| 112 |
+
const ExprHandle& /*expr*/);
|
| 113 |
+
TORCH_API std::ostream& operator<<(std::ostream& stream, const Stmt& /*stmt*/);
|
| 114 |
+
TORCH_API std::ostream& operator<<(std::ostream& stream, const Tensor& /*t*/);
|
| 115 |
+
|
| 116 |
+
TORCH_API void print(const ExprPtr& expr);
|
| 117 |
+
TORCH_API void print(const StmtPtr& stmt);
|
| 118 |
+
TORCH_API void print(const Tensor& t);
|
| 119 |
+
|
| 120 |
+
} // namespace torch::jit::tensorexpr
|
| 121 |
+
|
| 122 |
+
namespace std {
|
| 123 |
+
|
| 124 |
+
using torch::jit::tensorexpr::Expr;
|
| 125 |
+
using torch::jit::tensorexpr::ExprPtr;
|
| 126 |
+
using torch::jit::tensorexpr::Stmt;
|
| 127 |
+
using torch::jit::tensorexpr::StmtPtr;
|
| 128 |
+
using torch::jit::tensorexpr::Tensor;
|
| 129 |
+
|
| 130 |
+
TORCH_API std::string to_string(const ExprPtr& expr);
|
| 131 |
+
TORCH_API std::string to_string(const StmtPtr& stmt);
|
| 132 |
+
TORCH_API std::string to_string(const Tensor& t);
|
| 133 |
+
} // namespace std
|
| 134 |
+
|
| 135 |
+
#else
|
| 136 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 137 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|