Skip to content

Commit f5178bf

Browse files
Walter Shenfacebook-github-bot
authored andcommitted
Revert D25607503: Add base forward grad logic
Test Plan: revert-hammer Differential Revision: D25607503 (fdf02ef) Original commit changeset: f1396290de1d fbshipit-source-id: 057206e28ff48ee288856adfe3ca577d4880789f
1 parent aa2782b commit f5178bf

37 files changed

Lines changed: 153 additions & 1442 deletions

aten/src/ATen/core/Formatting.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,6 @@ std::ostream& print(std::ostream& stream, const Tensor & tensor_, int64_t linesi
292292
stream << ", axis: " << tensor_.q_per_channel_axis();
293293
}
294294
}
295-
296-
auto& fw_grad = tensor.fw_grad(/* level */ 0);
297-
if (fw_grad.defined()) {
298-
stream << ", tangent:" << std::endl << fw_grad;
299-
}
300295
stream << " ]";
301296
}
302297
return stream;

aten/src/ATen/core/NamedRegistrations.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,4 @@ TORCH_LIBRARY_IMPL(aten, Named, m) {
510510
m.impl("_version", CppFunction::makeFallthrough());
511511
m.impl("requires_grad_", CppFunction::makeFallthrough());
512512
m.impl("retain_grad", CppFunction::makeFallthrough());
513-
m.impl("_fw_primal", CppFunction::makeFallthrough());
514513
}

aten/src/ATen/native/AutogradComposite.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

aten/src/ATen/native/VariableMethodStubs.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,5 @@ void retain_grad(Tensor& self) {
4040
AT_ERROR("retain_grad is not implemented for Tensor");
4141
}
4242

43-
Tensor _fw_primal(const Tensor& self, int64_t level) {
44-
AT_ERROR("_fw_primal is not implemented for Tensor");
45-
}
46-
4743
} // namespace native
4844
} // namespace at

aten/src/ATen/native/native_functions.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,6 @@
105105
manual_kernel_registration: True
106106
variants: method
107107

108-
- func: _fw_primal(Tensor(a) self, int level) -> Tensor(a)
109-
use_c10_dispatcher: full
110-
variants: method
111-
dispatch:
112-
DefaultBackend: _fw_primal
113-
114-
- func: make_dual(Tensor(a) primal, Tensor tangent, int level) -> Tensor(a)
115-
use_c10_dispatcher: full
116-
variants: function
117-
118-
- func: unpack_dual(Tensor(a) dual, int level) -> (Tensor(a) primal, Tensor tangent)
119-
use_c10_dispatcher: full
120-
variants: function
121-
122108
- func: rename_(Tensor(a!) self, Dimname[]? names) -> Tensor(a!)
123109
use_c10_dispatcher: full
124110
variants: method

aten/src/ATen/templates/TensorBody.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -599,23 +599,6 @@ class TORCH_API Tensor {
599599
return impl_->grad();
600600
}
601601

602-
// The Forward AD API functions below are low level and are not to be used by end
603-
// users who should use the API provided in torch/csrc/autograd.h
604-
605-
/// This function returns the forward gradient for this Tensor at the given level.
606-
const Tensor& fw_grad(uint64_t level) const {
607-
return impl_->fw_grad(level, *this);
608-
}
609-
610-
/// This function can be used to set the value of the forward grad.
611-
/// Note that the given new_grad might not be used directly if it has different
612-
/// metadata (size/stride/storage offset) compared to this Tensor. In that case,
613-
/// new_grad content will be copied into a new Tensor
614-
void set_fw_grad(const Tensor& new_grad, uint64_t level, bool is_inplace_op) {
615-
impl_->set_fw_grad(new_grad, *this, level, is_inplace_op);
616-
}
617-
618-
619602
// STOP. Thinking of adding a method here, which only makes use
620603
// of other ATen methods? Define it in native_functions.yaml.
621604

c10/core/TensorImpl.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ const at::Tensor& TensorImpl::grad() const {
4444
return autograd_meta_->grad();
4545
}
4646

47-
const at::Tensor& TensorImpl::fw_grad(uint64_t level, const at::Tensor& self) const {
48-
// See TensorImpl::grad() above for explanation about the line below
49-
if (!autograd_meta_) return impl::GetAutogradMetaFactory()->undefined_tensor();
50-
return autograd_meta_->fw_grad(level, self);
51-
}
52-
53-
void TensorImpl::set_fw_grad(const at::Tensor& new_grad, const at::Tensor& self, uint64_t level, bool is_inplace_op) {
54-
if (!autograd_meta_) autograd_meta_ = impl::GetAutogradMetaFactory()->make();
55-
autograd_meta_->set_fw_grad(new_grad, self, level, is_inplace_op);
56-
}
57-
5847
TensorImpl::TensorImpl(
5948
Storage&& storage,
6049
DispatchKeySet key_set,

c10/core/TensorImpl.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ struct C10_API AutogradMetaInterface {
136136
virtual bool requires_grad() const = 0;
137137
virtual at::Tensor& mutable_grad() = 0;
138138
virtual const at::Tensor& grad() const = 0;
139-
virtual const at::Tensor& fw_grad(uint64_t level, const at::Tensor& self) const = 0;
140-
virtual void set_fw_grad(const at::Tensor& new_grad, const at::Tensor& self, uint64_t level, bool is_inplace_op) = 0;
141139
virtual ~AutogradMetaInterface();
142140
};
143141

@@ -600,42 +598,6 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
600598
*/
601599
const at::Tensor& grad() const;
602600

603-
/**
604-
* Return the accumulated gradient of a tensor. This gradient is computed
605-
* using forward mode AD.
606-
*
607-
* This is an internal API that should never be used by end users.
608-
*
609-
* The API is as follows:
610-
* - "level" allows to specify the level of forward AD nesting for which the
611-
* gradient should be returned. Note that since levels are not fully
612-
* supported yet, this argument should be 0. See documentation for
613-
* torch::autograd::enter_dual_level for more details about forward AD nesting.
614-
* - "self" should represent the Tensor whose forward grad is accessed. It is
615-
* required when dealing with view.
616-
*/
617-
const at::Tensor& fw_grad(uint64_t level, const at::Tensor& self) const;
618-
619-
/**
620-
* Sets the forward gradient for this Tensor.
621-
* The given Tensor might not be used directly and its content will be copied.
622-
*
623-
* This is an internal API that should never be used by end users.
624-
*
625-
* The API is as follows:
626-
* - "new_grad" is a Tensor containing the new value of the gradient that should
627-
* be set
628-
* - "self" should reprensent the Tensor whose forward grad is accessed. It is
629-
* required when dealing with view.
630-
* - "level" allows to specify the level of forward AD nesting for which the
631-
* gradient should be set. Note that since levels are not fully supported
632-
* yet, this argument should be 0. See documentation for torch::autograd::enter_dual_level
633-
* for more details about forward AD nesting.
634-
* - "is_inplace_op" is a boolean flag that tells if this gradient was generated
635-
* by an inplace operation or an out of place one. This allows better error checking.
636-
*/
637-
void set_fw_grad(const at::Tensor& new_grad, const at::Tensor& self, uint64_t level, bool is_inplace_op);
638-
639601
/**
640602
* Return a typed data pointer to the actual data which this tensor refers to.
641603
* This checks that the requested type (from the template parameter) matches

test/test_autograd.py

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
IS_WINDOWS, IS_MACOS, CudaMemoryLeakCheck)
3636
from torch.autograd import Variable, Function, detect_anomaly, kineto_available
3737
from torch.autograd.function import InplaceFunction
38-
import torch.autograd.forward_ad as fwAD
3938
from torch.testing import randn_like
4039
from torch.testing._internal.common_methods_invocations import (method_tests,
4140
create_input, unpack_variables,
@@ -5327,26 +5326,6 @@ def fn(a, dim0_size=5):
53275326

53285327
self.assertEqual(x.grad, y.grad)
53295328

5330-
def test_view_with_multi_output(self):
5331-
x = torch.randn(2, 2, 2, dtype=torch.double)
5332-
5333-
x1 = torch.view_as_complex(x)
5334-
# Taking an invalid view should always be allowed as long as it is not
5335-
# modified inplace
5336-
res = x1.unbind(0)
5337-
5338-
with self.assertRaisesRegex(RuntimeError, "output of a function that returns multiple views"):
5339-
res[0] += torch.rand(2, requires_grad=True)
5340-
5341-
x.requires_grad_(True)
5342-
x1 = torch.view_as_complex(x)
5343-
# Taking an invalid view should always be allowed as long as it is not
5344-
# modified inplace
5345-
res = x1.unbind(0)
5346-
5347-
with self.assertRaisesRegex(RuntimeError, "output of a function that returns multiple views"):
5348-
res[0] += torch.rand(2, requires_grad=True)
5349-
53505329
def as_identity(self):
53515330
# view_as_real and view_as_complex behavior should be like an identity
53525331
def func(z):
@@ -6345,66 +6324,6 @@ def foo(a):
63456324
self.assertEqual(hvp, torch.mm(hes, v.unsqueeze(1)).squeeze(1))
63466325
self.assertEqual(vhp, torch.mm(v.unsqueeze(0), hes).squeeze(0))
63476326

6348-
class TestAutogradForwardMode(TestCase):
6349-
def test_forward_level_cleanup(self):
6350-
import weakref
6351-
6352-
def get_tensor_and_weak_ref():
6353-
# Helper function to get a Tensor and a weak ref that tells us
6354-
# if the c++ version of this Tensor is still alive or not.
6355-
#
6356-
# Create the following reference chain to do so:
6357-
# - python Tensor t
6358-
# - c++ Tensor corresponding by t
6359-
# - c++ Node corresponding to t.grad_fn
6360-
# - python dict of metadata from this Node
6361-
# - an object in this dict that we can take a weakref of
6362-
6363-
6364-
# Create a new Tensor and Node
6365-
t = torch.rand(2, requires_grad=True).clone()
6366-
# Create the metadata dict
6367-
meta_dict = t.grad_fn.metadata
6368-
# Create the object in the dict
6369-
6370-
class Foo(object):
6371-
pass
6372-
my_obj = Foo()
6373-
meta_dict[0] = my_obj
6374-
6375-
# After exiting this function, the python Tensor t is the only
6376-
# thing keeping ref alive
6377-
ref = weakref.ref(my_obj)
6378-
return t, ref
6379-
6380-
# Sanity check that the helper function works as expected
6381-
t, t_ref = get_tensor_and_weak_ref()
6382-
self.assertIsNotNone(t_ref())
6383-
6384-
del t
6385-
self.assertIsNone(t_ref())
6386-
6387-
# Main test code
6388-
foo = torch.rand(2)
6389-
6390-
with fwAD.dual_level():
6391-
tangent, tangent_ref = get_tensor_and_weak_ref()
6392-
self.assertIsNotNone(tangent_ref())
6393-
6394-
dual = fwAD.make_dual(foo, tangent)
6395-
self.assertIsNotNone(tangent_ref())
6396-
6397-
# Make sure that the tangent we provided has been re-used as is
6398-
self.assertTrue(fwAD.unpack_dual(dual)[1] is tangent)
6399-
6400-
# Make sure that dual is keeping the tangent alive
6401-
del tangent
6402-
self.assertIsNotNone(tangent_ref())
6403-
6404-
# Make sure that the dual level does not keep the c++
6405-
# version of the tangent alive
6406-
del dual
6407-
self.assertIsNone(tangent_ref())
64086327

64096328
# Generic device type autograd tests.
64106329
class TestAutogradDeviceType(TestCase):

test/test_namedtuple_return_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
all_operators_with_namedtuple_return = {
1313
'max', 'min', 'median', 'nanmedian', 'mode', 'kthvalue', 'svd', 'symeig', 'eig',
1414
'qr', 'geqrf', 'solve', 'slogdet', 'sort', 'topk', 'lstsq',
15-
'triangular_solve', 'cummax', 'cummin', 'linalg_eigh', "unpack_dual"
15+
'triangular_solve', 'cummax', 'cummin', 'linalg_eigh'
1616
}
1717

1818

@@ -65,7 +65,6 @@ def test_namedtuple_return(self):
6565
op(operators=['triangular_solve'], input=(a,), names=('solution', 'cloned_coefficient'), hasout=True),
6666
op(operators=['lstsq'], input=(a,), names=('solution', 'QR'), hasout=True),
6767
op(operators=['linalg_eigh'], input=("L",), names=('eigenvalues', 'eigenvectors'), hasout=True),
68-
op(operators=['unpack_dual'], input=(a, 0), names=('primal', 'tangent'), hasout=False),
6968
]
7069

7170
for op in operators:
@@ -76,9 +75,7 @@ def test_namedtuple_return(self):
7675
for i, name in enumerate(op.names):
7776
self.assertIs(getattr(ret, name), ret[i])
7877
else:
79-
# Handle op that are not methods
80-
func = getattr(a, f) if hasattr(a, f) else getattr(torch, f)
81-
ret = func(*op.input)
78+
ret = getattr(a, f)(*op.input)
8279
for i, name in enumerate(op.names):
8380
self.assertIs(getattr(ret, name), ret[i])
8481
if op.hasout:

0 commit comments

Comments
 (0)