@Namespace(value="c10") @NoOffset @Properties(inherit=torch.class) public class TensorOptions extends Pointer
torch::
variants of factory functions,
e.g., torch::zeros for at::zeros. These return Variables (while the
stock ATen functions return plain Tensors). If you mix these functions
up, you WILL BE SAD.
Rather than use the constructor of this class directly, you should prefer to
use the constructor functions, and then chain setter methods on top of them.
at::device(at::kCUDA).dtype(kInt)
at::dtype(at::kInt)
Additionally, anywhere a TensorOptions is expected, you can directly
pass at::kCUDA / at::kInt, and it will implicitly convert to a
TensorOptions.
Here are some recommended ways to create a 2x2 tensor of zeros
with certain properties. These all *implicitly* make use of
TensorOptions, even if they don't mention the class explicitly:
at::zeros({2,2}, at::kCUDA);
at::zeros({2,2}, at::kLong);
at::zeros({2,2}, at::device(at::kCUDA).dtype(at::kLong()));
at::zeros({2,2}, at::device({at::kCUDA, 1})); // place on device 1
at::zeros({2,2}, at::requires_grad());
NOTE [ TensorOptions Constructors ]
TensorOptions is like a dictionary with entries from the set:
{requires_grad, device, dtype, layout}, where each entry may be
unspecified (i.e., is optional). It is used to specify the properties of
tensors in many places both in C++ internal and API, e.g., tensor factory
methods like at::empty({10}, options)
, tensor conversions like
tensor.to(...)
, etc.
To provide a simple API that is consistent with Python, where one can do
torch.empty(sizes, X)
with X
being a torch.device
, torch.dtype
, or a
torch.layout
, we want TensorOptions to be implicitly convertible from
ScalarType dtype
, Layout layout
and Device device
. Therefore, we have
three implicit constructors from each of these three types.
This is sufficient for ScalarType
and Layout
as they are simple Enum
classes. However, Device
is an ordinary class with implicit constructors
Device(DeviceType, DeviceIndex = -1)
and Device(std::string)
to be
consistent with Python API, where strings are treated as equivalent with a
torch.device
object (e.g., "cuda:1" can be passed to everywhere a
torch.device("cuda:1")
is accepted). To support the syntax
at::empty({10}, {kCUDA, 1})
and tensor.to(kCUDA)
, we need to make sure
that TensorOptions
is implicitly constructible with any arguments that a
Device
can constructed from. So we have,
/* implicit * / TensorOptions(T&& device) : TensorOptions() {
this->set_device(device);
}
template TensorOptions({kCUDA, 1})
.
Compiler will complain about ambiguity between the copy constructor and the
Device
constructor because {kCUDA, 1}
can be converted to both a
TensorOption
and a Device
.
To get around this, we templatize the Device
constructor. Since overload
resolution is done before template resolution, our problem is solved.
Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter
Constructor and Description |
---|
TensorOptions() |
TensorOptions(byte layout) |
TensorOptions(Device device)
Constructs a
TensorOptions object with the given device. |
TensorOptions(long size)
Native array allocator.
|
TensorOptions(Pointer p)
Pointer cast constructor.
|
TensorOptions(torch.Layout layout)
Constructs a
TensorOptions object with the given layout. |
TensorOptions(torch.MemoryFormat memory_format)
Constructs a
TensorOptions object with the given memory format. |
TensorOptions(torch.ScalarType dtype)
legacy constructor to support ScalarType
|
TensorOptions(TypeMeta dtype)
Constructs a
TensorOptions object with the given dtype. |
Modifier and Type | Method and Description |
---|---|
torch.Backend |
backend() |
torch.DispatchKey |
computeDispatchKey() |
byte |
device_index()
Returns the device index of the
TensorOptions . |
TensorOptions |
device_index(byte device_index)
Return a copy of
TensorOptions , but with device set to CUDA, and the
device index set to the given one. |
DeviceOptional |
device_opt()
Returns the device of the
TensorOptions , or std::nullopt if
device is not specified. |
Device |
device()
Returns the device of the
TensorOptions . |
TensorOptions |
device(DeviceOptional device)
Return a copy of
TensorOptions with device set to the given one, or
cleared if device is nullopt . |
TypeMetaOptional |
dtype_opt()
Returns the dtype of the
TensorOptions , or std::nullopt if
device is not specified. |
TypeMeta |
dtype()
Returns the dtype of the
TensorOptions . |
TensorOptions |
dtype(ScalarTypeOptional dtype) |
TensorOptions |
dtype(TypeMetaOptional dtype)
Return a copy of
TensorOptions with dtype set to the given one. |
TensorOptions |
getPointer(long i) |
boolean |
has_device()
Returns whether the device is specified.
|
boolean |
has_dtype()
Returns whether the dtype is specified.
|
boolean |
has_layout()
Returns whether the layout is specified.
|
boolean |
has_memory_format()
Returns whether the
memory_layout is specified |
boolean |
has_pinned_memory()
Returns whether the
pinned_memory is specified. |
boolean |
has_requires_grad()
Returns whether the
requires_grad is specified. |
boolean |
is_sparse_compressed() |
boolean |
is_sparse_csr()
Returns if the layout is sparse CSR, deprecated, use
is_sparse_compressed() instead
|
boolean |
is_sparse()
Returns if the layout is sparse
|
LayoutOptional |
layout_opt()
Returns the layout of the
TensorOptions , or std::nullopt if
layout is not specified. |
torch.Layout |
layout()
Returns the layout of the
TensorOptions . |
TensorOptions |
layout(LayoutOptional layout)
Sets the layout of the
TensorOptions . |
MemoryFormatOptional |
memory_format_opt()
Returns the
memory_layout property of TensorOptions, or
std::nulloptif memory_format{@code is not specified. |
TensorOptions |
memory_format(MemoryFormatOptional memory_format)
Sets the
memory_format property on TensorOptions . |
TensorOptions |
merge_in(TensorOptions options)
Return the right-biased merge of two TensorOptions.
|
TensorOptions |
merge_memory_format(MemoryFormatOptional optional_memory_format) |
BoolOptional |
pinned_memory_opt()
Returns the
pinned_memory property of the TensorOptions , or
std::nullopt if pinned_memory is not specified. |
boolean |
pinned_memory()
Returns the
pinned_memory property of the TensorOptions . |
TensorOptions |
pinned_memory(BoolOptional pinned_memory)
Sets the
pinned_memory property on the TensorOptions . |
TensorOptions |
position(long position) |
BoolOptional |
requires_grad_opt()
Returns the
requires_grad property of the TensorOptions , or
std::nullopt if requires_grad is not specified. |
boolean |
requires_grad()
Returns the
requires_grad property of the TensorOptions . |
TensorOptions |
requires_grad(BoolOptional requires_grad)
Sets the
requires_grad property of the TensorOptions . |
boolean |
type_equal(TensorOptions other) |
address, asBuffer, asByteBuffer, availablePhysicalBytes, calloc, capacity, capacity, close, deallocate, deallocate, deallocateReferences, deallocator, deallocator, equals, fill, formatBytes, free, getDirectBufferAddress, getPointer, getPointer, getPointer, hashCode, interruptDeallocatorThread, isNull, isNull, limit, limit, malloc, maxBytes, maxPhysicalBytes, memchr, memcmp, memcpy, memmove, memset, offsetAddress, offsetof, offsetof, parseBytes, physicalBytes, physicalBytesInaccurate, position, put, realloc, referenceCount, releaseReference, retainReference, setNull, sizeof, sizeof, toString, totalBytes, totalCount, totalPhysicalBytes, withDeallocator, zero
public TensorOptions(Pointer p)
Pointer(Pointer)
.public TensorOptions(long size)
Pointer.position(long)
.public TensorOptions()
public TensorOptions(torch.Layout layout)
TensorOptions
object with the given layout.public TensorOptions(@ByRef(value=true) Device device)
TensorOptions
object with the given device.
See NOTE [ TensorOptions Constructors ] on why this is templatized.public TensorOptions(@ByVal TypeMeta dtype)
TensorOptions
object with the given dtype.public TensorOptions(torch.ScalarType dtype)
public TensorOptions(torch.MemoryFormat memory_format)
TensorOptions
object with the given memory format.public TensorOptions position(long position)
public TensorOptions getPointer(long i)
getPointer
in class Pointer
@ByVal @NoException(value=true) public TensorOptions device(@ByVal DeviceOptional device)
TensorOptions
with device
set to the given one, or
cleared if device
is nullopt
.@ByVal @NoException(value=true) public TensorOptions device_index(@Cast(value="c10::DeviceIndex") byte device_index)
TensorOptions
, but with device set to CUDA, and the
device index set to the given one.
TODO: This function encourages bad behavior (assuming CUDA is
the only device that matters). Get rid of it / rename it.@ByVal @NoException(value=true) public TensorOptions dtype(@ByVal TypeMetaOptional dtype)
TensorOptions
with dtype
set to the given one.@ByVal @NoException(value=true) public TensorOptions dtype(@ByVal ScalarTypeOptional dtype)
@ByVal @NoException(value=true) public TensorOptions layout(@ByVal LayoutOptional layout)
TensorOptions
.@ByVal @NoException(value=true) public TensorOptions requires_grad(@ByVal BoolOptional requires_grad)
requires_grad
property of the TensorOptions
.@ByVal @NoException(value=true) public TensorOptions pinned_memory(@ByVal BoolOptional pinned_memory)
pinned_memory
property on the TensorOptions
.@ByVal @NoException(value=true) public TensorOptions memory_format(@ByVal MemoryFormatOptional memory_format)
memory_format
property on TensorOptions
.@ByVal @NoException(value=true) public Device device()
TensorOptions
.@Cast(value="bool") @NoException(value=true) public boolean has_device()
@ByVal @NoException(value=true) public DeviceOptional device_opt()
TensorOptions
, or std::nullopt
if
device is not specified.@Cast(value="c10::DeviceIndex") @NoException(value=true) public byte device_index()
TensorOptions
.@ByVal @NoException(value=true) public TypeMeta dtype()
TensorOptions
.@Cast(value="bool") @NoException(value=true) public boolean has_dtype()
@ByVal @NoException(value=true) public TypeMetaOptional dtype_opt()
TensorOptions
, or std::nullopt
if
device is not specified.@NoException(value=true) public torch.Layout layout()
TensorOptions
.@Cast(value="bool") @NoException(value=true) public boolean has_layout()
@ByVal @NoException(value=true) public LayoutOptional layout_opt()
TensorOptions
, or std::nullopt
if
layout is not specified.@Cast(value="bool") @NoException(value=true) public boolean requires_grad()
requires_grad
property of the TensorOptions
.@Cast(value="bool") @NoException(value=true) public boolean has_requires_grad()
requires_grad
is specified.@ByVal @NoException(value=true) public BoolOptional requires_grad_opt()
requires_grad
property of the TensorOptions
, or
std::nullopt
if requires_grad
is not specified.@Cast(value="bool") @NoException(value=true) public boolean pinned_memory()
pinned_memory
property of the TensorOptions
.@Cast(value="bool") @NoException(value=true) public boolean has_pinned_memory()
pinned_memory
is specified.@Cast(value="bool") public boolean is_sparse_csr()
@Cast(value="bool") public boolean type_equal(@Const @ByRef TensorOptions other)
@ByVal @NoException(value=true) public BoolOptional pinned_memory_opt()
pinned_memory
property of the TensorOptions
, or
std::nullopt
if pinned_memory
is not specified.@Cast(value="bool") @NoException(value=true) public boolean has_memory_format()
memory_layout
is specified@ByVal @NoException(value=true) public MemoryFormatOptional memory_format_opt()
memory_layout
property of TensorOptions, or
std::nulloptif
memory_format{@code is not specified.public torch.Backend backend()
@ByVal @NoException(value=true) public TensorOptions merge_in(@ByVal TensorOptions options)
@ByVal @NoException(value=true) public TensorOptions merge_memory_format(@ByVal MemoryFormatOptional optional_memory_format)
public torch.DispatchKey computeDispatchKey()
Copyright © 2024. All rights reserved.