1 #include <torch/csrc/autograd/input_buffer.h> 3 #include <torch/csrc/autograd/functions/basic_ops.h> 4 #include <torch/csrc/autograd/input_metadata.h> 6 #include <ATen/DeviceGuard.h> 12 namespace torch {
namespace autograd {
15 void InputBuffer::add(
size_t pos, Variable var) {
16 AT_ASSERT(pos < buffer.size());
20 auto& old_var = buffer[pos];
21 if (!old_var.defined()) {
22 buffer[pos] = std::move(var);
27 if (old_var.is_sparse()) {
29 if (!var.is_sparse() && var.is_contiguous() && var.storage().use_count() == 1) {
30 buffer[pos] = var.add_(old_var);
32 buffer[pos] = var + old_var;
35 if (var.is_sparse() && !old_var.is_sparse() && old_var.is_contiguous() && old_var.storage().use_count() == 1) {
36 buffer[pos] = old_var.add_(var);
38 buffer[pos] = old_var + var;
44 auto InputBuffer::device() const ->
at::Device {
49 for (
auto& var : buffer) {
51 auto device = var.device();
52 if (device.
type() != at::kCPU) {
62 auto InputBuffer::variables(InputBuffer&& g) -> std::vector<Variable> {
63 std::vector<Variable> result = std::move(g.buffer);
optional< Device > device_of(Tensor t)
Return the Device of a Tensor, if the Tensor is defined.
A OptionalDeviceGuard is an RAII class that sets a device to some value on initialization, and resets the device to its original value on destruction.
Flush-To-Zero and Denormals-Are-Zero mode.
DeviceType type() const noexcept
Returns the type of device this is.