3 #include <c10/core/DefaultDtype.h> 4 #include <c10/core/Backend.h> 5 #include <c10/core/Layout.h> 6 #include <c10/core/ScalarType.h> 7 #include <c10/core/Device.h> 9 #include <c10/util/Optional.h> 10 #include <c10/util/C++17.h> 11 #include <c10/macros/Macros.h> 101 : requires_grad_(
false)
102 , is_variable_(
false)
106 , has_requires_grad_(
false)
107 , has_is_variable_(
false)
112 this->set_layout(layout);
118 typename = c10::guts::enable_if_t<std::is_same<c10::guts::decay_t<T>,
Device>::value>>
120 this->set_device(std::forward<T>(
device));
131 template <
typename... Args,
132 typename = c10::guts::enable_if_t<std::is_constructible<
Device, Args&&...>::value>>
134 : TensorOptions(Device(
std::forward<Args>(args)...)) {}
138 this->set_dtype(dtype);
143 this->set_dtype(dtype);
149 has_dtype_ == other.has_dtype_ &&
150 has_layout_ == other.has_layout_ &&
151 has_device_ == other.has_device_ &&
152 has_requires_grad_ == other.has_requires_grad_ &&
153 has_is_variable_ == other.has_is_variable_ &&
154 (!has_dtype_ || dtype_ == other.dtype_) &&
155 (!has_layout_ || layout_ == other.layout_) &&
156 (!has_device_ || device_ == other.device_) &&
157 (!requires_grad_ || requires_grad_ == other.requires_grad_) &&
158 (!is_variable_ || is_variable_ == other.is_variable_);
164 return !(*
this == other);
170 TensorOptions r = *
this;
178 template<
typename ... Args>
179 C10_NODISCARD TensorOptions
device(Args&&... args) const noexcept {
194 TensorOptions r = *
this;
201 TensorOptions r = *
this;
207 template <
typename T>
208 TensorOptions&
dtype() {
209 dtype_ = caffe2::TypeMeta::Make<T>();
216 TensorOptions r = *
this;
223 TensorOptions r = *
this;
230 TensorOptions r = *
this;
231 r.set_is_variable(is_variable);
237 return has_device_ ? device_ : Device(kCPU);
248 return has_device_ ? c10::make_optional(device_) : c10::nullopt;
258 return has_dtype_ ? dtype_ : get_default_dtype();
269 return has_dtype_ ? c10::make_optional(dtype_) : c10::nullopt;
274 return has_layout_ ? layout_ : kStrided;
285 return has_layout_ ? c10::make_optional(layout_) : c10::nullopt;
290 return has_requires_grad_ ? requires_grad_ :
false;
295 return has_requires_grad_;
301 return has_requires_grad_ ? c10::make_optional(requires_grad_)
307 return has_is_variable_ ? is_variable_ :
false;
312 return has_is_variable_;
318 return has_is_variable_ ? c10::make_optional(is_variable_) : c10::nullopt;
322 Backend backend()
const noexcept {
323 return at::tensorTypeIdToBackend(computeTensorTypeId());
328 case Layout::Strided:
329 switch (
device().type()) {
330 case DeviceType::CPU:
331 return CPUTensorId();
332 case DeviceType::CUDA:
333 return CUDATensorId();
334 case DeviceType::MKLDNN:
335 return MKLDNNTensorId();
336 case DeviceType::OPENGL:
337 return OpenGLTensorId();
338 case DeviceType::OPENCL:
339 return OpenCLTensorId();
340 case DeviceType::IDEEP:
341 return IDEEPTensorId();
342 case DeviceType::HIP:
343 return HIPTensorId();
344 case DeviceType::MSNPU:
345 return MSNPUTensorId();
346 case DeviceType::XLA:
347 return XLATensorId();
349 AT_ERROR(
"Unsupported device type for dense layout: ",
device().type());
352 switch (
device().type()) {
353 case DeviceType::CPU:
354 return SparseCPUTensorId();
355 case DeviceType::CUDA:
356 return SparseCUDATensorId();
357 case DeviceType::HIP:
358 return SparseHIPTensorId();
360 AT_ERROR(
"Unsupported device type for sparse layout: ",
device().type());
363 AT_ERROR(
"Unsupported layout: ",
layout());
404 dtype_ = scalarTypeToTypeMeta(*dtype);
425 has_requires_grad_ =
true;
427 has_requires_grad_ =
false;
434 is_variable_ = *is_variable;
435 has_is_variable_ =
true;
437 has_is_variable_ =
false;
448 Device device_ = at::kCPU;
449 Layout layout_ = at::kStrided;
454 bool requires_grad_ : 1;
455 bool is_variable_ : 1;
457 bool has_device_ : 1;
459 bool has_layout_ : 1;
460 bool has_requires_grad_ : 1;
461 bool has_is_variable_ : 1;
468 "TensorOptions must fit in 128-bits" );
505 C10_API std::ostream& operator<<(
506 std::ostream& stream,
509 template <
typename T>
511 return dtype(caffe2::TypeMeta::Make<T>());
522 return options.computeTensorTypeId();
526 if (tid == CPUTensorId()) {
527 return DeviceType::CPU;
528 }
else if (tid == CUDATensorId()) {
529 return DeviceType::CUDA;
530 }
else if (tid == HIPTensorId()) {
531 return DeviceType::HIP;
532 }
else if (tid == MKLDNNTensorId()) {
533 return DeviceType::MKLDNN;
534 }
else if (tid == OpenGLTensorId()) {
535 return DeviceType::IDEEP;
536 }
else if (tid == OpenCLTensorId()) {
537 return DeviceType::OPENCL;
538 }
else if (tid == IDEEPTensorId()) {
539 return DeviceType::IDEEP;
540 }
else if (tid == HIPTensorId()) {
541 return DeviceType::HIP;
542 }
else if (tid == MSNPUTensorId()) {
543 return DeviceType::MSNPU;
544 }
else if (tid == XLATensorId()) {
545 return DeviceType::XLA;
546 }
else if (tid == SparseCPUTensorId()) {
547 return DeviceType::CPU;
548 }
else if (tid == SparseCUDATensorId()) {
549 return DeviceType::CUDA;
550 }
else if (tid == SparseHIPTensorId()) {
551 return DeviceType::HIP;
553 AT_ASSERTM(
false,
"Unknown TensorTypeId: ", tid);
C10_NODISCARD TensorOptions device(c10::optional< Device > device) const noexcept
Return a copy of TensorOptions with device set to the given one, or cleared if device is nullopt...
C10_NODISCARD TensorOptions device(Args &&...args) const noexcept
Return a copy of TensorOptions with device set to the given one.
TensorOptions device_index(int16_t device_index)
Convenience function that returns a TensorOptions object with the device set to CUDA and the device_i...
TensorOptions(Layout layout)
Constructs a TensorOptions object with the given layout.
c10::optional< bool > requires_grad_opt() const noexcept
Returns the requires_grad property of the TensorOptions, or c10::nullopt if requires_grad is not spec...
bool has_device() const noexcept
Returns whether the device is specified.
bool has_requires_grad() const noexcept
Returns whether the requires_grad is specified.
bool has_layout() const noexcept
Returns whether the layout is specified.
c10::optional< Device > device_opt() const noexcept
Returns the device of the TensorOptions, or c10::nullopt if device is not specified.
C10_NODISCARD TensorOptions device_index(int16_t device_index) const noexcept
Return a copy of TensorOptions, but with device set to CUDA, and the device index set to the given on...
TensorOptions(Args &&...args)
Constructs a TensorOptions object from arguments allowed in Device constructors.
c10::optional< caffe2::TypeMeta > dtype_opt() const noexcept
Returns the dtype of the TensorOptions, or c10::nullopt if device is not specified.
int32_t device_index() const noexcept
Returns the device index of the TensorOptions.
TensorOptions device(Device device)
Convenience function that returns a TensorOptions object with the device set to the given one...
bool has_dtype() const noexcept
Returns whether the dtype is specified.
Represents a a compute device on which a tensor is located.
bool operator!=(const TensorOptions &other) const noexcept
True if any of the elements of this TensorOptions do not match that of the other. ...
TensorOptions(caffe2::TypeMeta dtype)
Constructs a TensorOptions object with the given dtype.
Backend
This legacy enum class defines the set of backends supported by old school, code generated Type-based...
Device device() const noexcept
Returns the device of the TensorOptions.
c10::optional< bool > is_variable_opt() const noexcept
Returns the is_variable property of the TensorOptions, or c10::nullopt if is_variable is not specifie...
Dynamic type ID of a Tensor argument.
TensorOptions(T &&device)
A class to encapsulate construction axes of an Tensor.
Layout layout() const noexcept
Returns the layout of the TensorOptions.
TensorOptions(ScalarType dtype)
legacy constructor to support ScalarType
TensorOptions layout(Layout layout)
Convenience function that returns a TensorOptions object with the layout set to the given one...
C10_NODISCARD TensorOptions dtype(c10::optional< caffe2::TypeMeta > dtype) const noexcept
Return a copy of TensorOptions with dtype set to the given one.
bool is_variable() const noexcept
Returns the is_variable property of the TensorOptions.
C10_NODISCARD TensorOptions is_variable(c10::optional< bool > is_variable) const noexcept
Sets the is_variable property on the TensorOptions.
bool operator==(const TensorOptions &other) const noexcept
True if all elements of the TensorOptions match that of the other.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
c10::optional< Layout > layout_opt() const noexcept
Returns the layout of the TensorOptions, or c10::nullopt if layout is not specified.
TensorOptions requires_grad(bool requires_grad=true)
Convenience function that returns a TensorOptions object with the requires_grad set to the given one...
bool requires_grad() const noexcept
Returns the requires_grad property of the TensorOptions.
bool has_is_variable() const noexcept
Returns whether the is_variable is specified.
caffe2::TypeMeta dtype() const noexcept
Returns the dtype of the TensorOptions.
C10_NODISCARD TensorOptions layout(c10::optional< Layout > layout) const noexcept
Sets the layout of the TensorOptions.
TensorOptions(T &&device)
Constructs a TensorOptions object with the given device.
C10_NODISCARD TensorOptions requires_grad(c10::optional< bool > requires_grad) const noexcept
Sets the requires_grad property of the TensorOptions.
TensorOptions dtype(caffe2::TypeMeta dtype)
Convenience function that returns a TensorOptions object with the dtype set to the given one...