12namespace torch {
namespace autograd {
18 auto& old_var = buffer[pos];
21 if (old_var.is_sparse()) {
23 if (!
var.is_sparse() &&
var.is_contiguous() &&
var.storage().use_count() == 1) {
24 buffer[pos] =
var.add_(old_var);
26 buffer[pos] =
var + old_var;
29 if (
var.is_sparse() && !old_var.is_sparse() && old_var.is_contiguous() && old_var.storage().use_count() == 1) {
30 buffer[pos] = old_var.add_(
var);
32 buffer[pos] = old_var +
var;
67 const auto on_producer = opt_producer_stream
69 const auto on_consumer = opt_consumer_stream
71 if (on_producer && on_consumer) {
73 opt_accumulate_stream = opt_consumer_stream;
74 if (opt_accumulate_stream != opt_producer_stream) {
77 event.record(*opt_producer_stream);
78 opt_accumulate_stream->wait(event);
83 if (on_consumer && !on_producer) {
85 opt_accumulate_stream = opt_consumer_stream;
86 opt_sync_stream = guard.getDefaultStream(opt_consumer_stream->device());
87 }
else if (on_producer && !on_consumer) {
89 opt_accumulate_stream = guard.getDefaultStream(opt_producer_stream->device());
90 opt_sync_stream = opt_producer_stream;
93 opt_accumulate_stream = guard.getDefaultStream(*
device_of(
var));
95 if (opt_sync_stream && (opt_accumulate_stream != opt_sync_stream)) {
99 event.record(*opt_sync_stream);
100 opt_accumulate_stream->wait(event);
105 auto& old_var =
buffer[pos];
106 if (!old_var.defined()) {
109 if (opt_accumulate_stream) {
140 std::vector<Variable> result =
std::move(g.buffer);
#define TORCH_INTERNAL_ASSERT(cond,...)
A OptionalDeviceGuard is an RAII class that sets a device to some value on initialization,...
An implementation of DeviceGuardImplInterface which delegates to virtual dispatch on the DeviceGuardI...
Distributions kernel adapted from THRandom.cpp The kernels try to follow std::random distributions si...
optional< Device > device_of(const Tensor &t)
Return the Device of a Tensor, if the Tensor is defined.
constexpr remove_reference_t< T > && move(T &&t) noexcept
constexpr Symbol is_cuda(static_cast< unique_t >(_keys::prim_is_cuda))
constexpr nullopt_t nullopt
constexpr DeviceType kCPU
default The input dimensional tensor of shape $NCHW$ or $NHWC$ depending on the order parameter The bias as a dimensional tensor of size $C$ to be applied to the output var
static void accumulate(std::vector< Variable > &buffer, const size_t pos, Variable &&var)
Copyright (c) 2016-present, Facebook, Inc.
Represents a a compute device on which a tensor is located.
DeviceType type() const noexcept
Returns the type of device this is.
A backend-generic movable, not copyable, not thread-safe event.
An OptionalStreamGuard is an RAII class that sets a device to some value on initialization,...