text
stringlengths
0
2.2M
bool non_blocking,
c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(!layout.has_value() || self.layout() == layout.value(),
"to(options) doesn't support converting to a different layout, "
"but got self.layout being ", self.layout(),
" and options.layout set as ", layout.value());
auto options = TensorOptions()
.dtype(dtype)
.layout(layout)
.device(device)
.pinned_memory(pin_memory);
if (options.has_device()) {
options = options.device(ensure_has_index(options.device()));
}
// memory_format is handled separately due to MemoryFormat::Preserve logic
options = self.options().merge_in(options).memory_format(c10::nullopt);
auto memory_format = optional_memory_format.value_or(MemoryFormat::Preserve);
bool pin_out = (non_blocking && self.is_cuda() && options.device().is_cpu() &&
(options.layout() == c10::kStrided));
if (memory_format == MemoryFormat::Preserve) {
if (self.is_non_overlapping_and_dense() && options.device().supports_as_strided()) {
Tensor r;
if (self.is_quantized()) {
r = at::empty_quantized(self.sizes(), self, options);
at::QuantizerPtr quantizer = r.quantizer();
r.copy_(self, non_blocking);
set_quantizer_(r, quantizer);
} else {
r = at::empty_strided(
self.sizes(),
self.strides(),
options.pinned_memory(pin_out));
r.copy_(self, non_blocking);
}
return r;
} else {
memory_format = self.suggest_memory_format();
}
}
// See Note [Explicit nullopt MemoryFormat argument]
auto r = at::empty(self.sizes(),
options.memory_format(memory_format).pinned_memory(pin_out),
c10::nullopt);
r.copy_(self, non_blocking);
return r;
}
template <typename T>
static inline bool is_null_or_equal_to(const c10::optional<T>& test, const T& value) {
if (!test.has_value()) {
return true;
}
return test.value() == value;
}
// NOTE: static runtime's to_maybe_copy_out relies on details of this
// check; if you change how it works, please update static runtime as
// well.
bool to_will_alias(
const Tensor& self,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
bool copy,
c10::optional<c10::MemoryFormat> optional_memory_format) {
auto memory_format = optional_memory_format.value_or(MemoryFormat::Preserve);
return is_null_or_equal_to(dtype, self.dtype().toScalarType()) &&
is_null_or_equal_to(layout, self.layout()) &&
is_null_or_equal_to(device, self.device()) &&
!copy &&
(memory_format == MemoryFormat::Preserve ||
self.suggest_memory_format() == memory_format);
}
static inline Tensor to_impl(
const Tensor& self,
c10::optional<ScalarType> dtype,
c10::optional<Layout> layout,
c10::optional<Device> device,
c10::optional<bool> pin_memory,
bool non_blocking,
bool copy,
c10::optional<c10::MemoryFormat> optional_memory_format) {
// fast path
if (to_will_alias(self, dtype, layout, device, copy, optional_memory_format)) {
return self;
}
return at::_to_copy(
self, dtype, layout, device, pin_memory, non_blocking, optional_memory_format);
}
// If input tensor is fp32, cast it to fp16, otherwise leave it alone.
// (this is intended to be used internally by the JIT autocast implementation)
Tensor _autocast_to_reduced_precision(const Tensor& self, bool cuda_enabled, bool cpu_enabled, ScalarType cuda_dtype, ScalarType cpu_dtype) {
if (self.dtype() == at::ScalarType::Float &&