11#pragma GCC diagnostic ignored "-Warray-bounds"
12#pragma GCC diagnostic ignored "-Wstringop-overflow"
13#include <torch/script.h>
23#define CHECK_CUDA(x) \
25 TORCH_CHECK(x.is_cuda(), #x " must be a CUDA tensor") \
28#define CHECK_CONTIGUOUS(x) \
30 TORCH_CHECK(x.is_contiguous(), #x " must be contiguous") \
33#define CHECK_TYPE(x, type) \
35 TORCH_CHECK(x.dtype() == torch::type, #x " must have type " #type) \
38#define CHECK_SAME_DEVICE_TYPE(...) \
40 if (!SameDeviceType({__VA_ARGS__})) { \
44 " must all have the same device type but got " + \
45 TensorInfoStr({__VA_ARGS__})) \
49#define CHECK_SAME_DTYPE(...) \
51 if (!SameDtype({__VA_ARGS__})) { \
54 " must all have the same dtype but got " + \
55 TensorInfoStr({__VA_ARGS__})) \
63 TORCH_CHECK(
false,
"Unsupported type");
87 return torch::kFloat32;
91 return torch::kFloat64;
95template <
class T,
class TDtype>
97 return ToTorchDtype<T>() ==
t;
102 if (tensors.size()) {
103 auto device_type = tensors.begin()->device().type();
104 for (
const auto&
t : tensors) {
105 if (device_type !=
t.device().type()) {
114inline bool SameDtype(std::initializer_list<torch::Tensor> tensors) {
115 if (tensors.size()) {
116 auto dtype = tensors.begin()->dtype();
117 for (
const auto&
t : tensors) {
118 if (dtype !=
t.dtype()) {
126inline std::string
TensorInfoStr(std::initializer_list<torch::Tensor> tensors) {
127 std::stringstream sstr;
129 for (
const auto&
t : tensors) {
130 sstr <<
t.sizes() <<
" " <<
t.toString() <<
" " <<
t.device();
132 if (
count < tensors.size()) sstr <<
", ";
139 const torch::Device& device,
140 void** ptr =
nullptr) {
141 torch::Tensor tensor = torch::empty(
144 *ptr = tensor.data_ptr<uint8_t>();
155 open3d::utility::LogWarning(
156 "allow_tf32 is not supported on this backend; computing in "
157 "full float32 precision instead.");
170 int64_t max_temp_mem_MB,
172 void* temp_ptr =
nullptr;
173 size_t temp_size = 0;
174 size_t max_temp_size = 0;
177 run_fn(temp_ptr, temp_size, max_temp_size);
179 temp_size = std::max(
180 std::min(
size_t(max_temp_mem_MB) * 1024 * 1024, max_temp_size),
186 run_fn(temp_ptr, temp_size, max_temp_size);
190 torch::Tensor tensor) {
193 std::vector<DimValue> shape;
194 const int rank = tensor.dim();
195 for (
int i = 0; i < rank; ++i) {
196 shape.push_back(tensor.size(i));
204std::tuple<bool, std::string>
CheckShape(torch::Tensor tensor,
207 return open3d::ml::op_util::CheckShape<Opt>(
GetShapeVector(tensor),
208 std::forward<TDimX>(dimex),
209 std::forward<TArgs>(args)...);
232#define CHECK_SHAPE(tensor, ...) \
235 std::string cs_errstr_; \
236 std::tie(cs_success_, cs_errstr_) = CheckShape(tensor, __VA_ARGS__); \
237 TORCH_CHECK(cs_success_, \
238 "invalid shape for '" #tensor "', " + cs_errstr_) \
241#define CHECK_SHAPE_COMBINE_FIRST_DIMS(tensor, ...) \
244 std::string cs_errstr_; \
245 std::tie(cs_success_, cs_errstr_) = \
246 CheckShape<CSOpt::COMBINE_FIRST_DIMS>(tensor, __VA_ARGS__); \
247 TORCH_CHECK(cs_success_, \
248 "invalid shape for '" #tensor "', " + cs_errstr_) \
251#define CHECK_SHAPE_IGNORE_FIRST_DIMS(tensor, ...) \
254 std::string cs_errstr_; \
255 std::tie(cs_success_, cs_errstr_) = \
256 CheckShape<CSOpt::IGNORE_FIRST_DIMS>(tensor, __VA_ARGS__); \
257 TORCH_CHECK(cs_success_, \
258 "invalid shape for '" #tensor "', " + cs_errstr_) \
261#define CHECK_SHAPE_COMBINE_LAST_DIMS(tensor, ...) \
264 std::string cs_errstr_; \
265 std::tie(cs_success_, cs_errstr_) = \
266 CheckShape<CSOpt::COMBINE_LAST_DIMS>(tensor, __VA_ARGS__); \
267 TORCH_CHECK(cs_success_, \
268 "invalid shape for '" #tensor "', " + cs_errstr_) \
271#define CHECK_SHAPE_IGNORE_LAST_DIMS(tensor, ...) \
274 std::string cs_errstr_; \
275 std::tie(cs_success_, cs_errstr_) = \
276 CheckShape<CSOpt::IGNORE_LAST_DIMS>(tensor, __VA_ARGS__); \
277 TORCH_CHECK(cs_success_, \
278 "invalid shape for '" #tensor "', " + cs_errstr_) \
double t
Definition SurfaceReconstructionPoisson.cpp:175
TorchDtype_t ToTorchDtype< int64_t >()
Definition TorchHelper.h:82
TorchDtype_t ToTorchDtype< uint8_t >()
Definition TorchHelper.h:66
std::string TensorInfoStr(std::initializer_list< torch::Tensor > tensors)
Definition TorchHelper.h:126
std::vector< open3d::ml::op_util::DimValue > GetShapeVector(torch::Tensor tensor)
Definition TorchHelper.h:189
TorchDtype_t ToTorchDtype< int16_t >()
Definition TorchHelper.h:74
void WarnIfTF32NotSupported(bool allow_tf32)
Definition TorchHelper.h:153
TorchDtype_t ToTorchDtype< int8_t >()
Definition TorchHelper.h:70
TorchDtype_t ToTorchDtype< double >()
Definition TorchHelper.h:90
bool SameDtype(std::initializer_list< torch::Tensor > tensors)
Definition TorchHelper.h:114
bool SameDeviceType(std::initializer_list< torch::Tensor > tensors)
Definition TorchHelper.h:101
std::remove_const< decltype(torch::kInt32)>::type TorchDtype_t
Definition TorchHelper.h:60
TorchDtype_t ToTorchDtype()
Definition TorchHelper.h:62
void RunSYCLWithTempMemory(const torch::Device &device, int64_t max_temp_mem_MB, Fn &&run_fn)
Definition TorchHelper.h:169
torch::Tensor CreateTempTensor(const int64_t size, const torch::Device &device, void **ptr=nullptr)
Definition TorchHelper.h:138
std::tuple< bool, std::string > CheckShape(torch::Tensor tensor, TDimX &&dimex, TArgs &&... args)
Definition TorchHelper.h:204
TorchDtype_t ToTorchDtype< int32_t >()
Definition TorchHelper.h:78
bool CompareTorchDtype(const TDtype &t)
Definition TorchHelper.h:96
TorchDtype_t ToTorchDtype< float >()
Definition TorchHelper.h:86
Definition ShapeChecking.h:16
CSOpt
Check shape options.
Definition ShapeChecking.h:405