Conversation
🔗 Helpful links
✅ No Failures (0 Pending)As of commit bcec376 (more details on the Dr. CI page): Expand to see more💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Please report bugs/suggestions to the (internal) Dr. CI Users group. |
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
| dispatch: | ||
| CPU, CUDA: addr | ||
| CompositeImplicitAutograd: math_addr | ||
| CompositeExplicitAutograd: math_addr |
There was a problem hiding this comment.
I thought it had to be CompositeImplicitAutograd for automatic backward?
There was a problem hiding this comment.
For some reason, although we have an explicit addr derivative formula XLA didn't want to use it. I'm forcing XLA to use our old formula now and I want to see what happens
There was a problem hiding this comment.
XLA tests passed so looks like we're good to go here
torch/_refs/__init__.py
Outdated
| check(vec1.ndim == 1, lambda: f"addr: Expected 1-D argument vec1, but got {vec1.ndim}-D") | ||
| check(vec2.ndim == 1, lambda: f"addr: Expected 1-D argument vec2, but got {vec2.ndim}-D") | ||
| self = self.expand(vec1.shape[0], vec2.shape[0]) | ||
| check(self.ndim == 2, lambda: f"2D tensor expected, got {self.ndim}D tensor for input") |
There was a problem hiding this comment.
Wouldn't expand fail if self.ndim wasn't 2?
There was a problem hiding this comment.
I guess we should update the original code lol
torch/_refs/__init__.py
Outdated
| check(vec2.ndim == 1, lambda: f"addr: Expected 1-D argument vec2, but got {vec2.ndim}-D") | ||
| self = self.expand(vec1.shape[0], vec2.shape[0]) | ||
| check(self.ndim == 2, lambda: f"2D tensor expected, got {self.ndim}D tensor for input") | ||
| check(self.shape[0] == vec1.shape[0] and self.shape[1] == vec2.shape[0], |
There was a problem hiding this comment.
same here, isn't it guaranteed by expand?
| check(self.ndim == 2, lambda: f"2D tensor expected, got {self.ndim}D tensor for input") | ||
| check(self.shape[0] == vec1.shape[0] and self.shape[1] == vec2.shape[0], | ||
| lambda: f"size mismatch, input: {self.shape}, v1: {vec1.shape}, v2: {vec2.shape}") | ||
| if utils.is_boolean_dtype(self.dtype): |
There was a problem hiding this comment.
should you also check alpha dtype (it should be of the same type kind as other inputs)?
@mruberry do you want to make a case again for alpha checking being done in the wrapper?
There was a problem hiding this comment.
Integers are OK though
>>> torch.addr(torch.tensor([[False]]), torch.tensor([False]), torch.tensor([True]), beta=1, alpha=2)
tensor([[False]])
There was a problem hiding this comment.
Extending the type promotion wrapper would probably make life easier
I think we should say that integer numbers can interact with bools and it's fine, however
There was a problem hiding this comment.
I tried the obvious thing which is
@elementwise_type_promotion_wrapper(
type_promoting_args=("self", "vec1", "vec2", "beta", "alpha"),
type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,
)
but this doesn't work as an integer alpha causes the entire tensor to promote to int64. I'm not sure what the recommend spelling of this change would be, it seems to have far reaching effects.
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
| vec2.ndim == 1, | ||
| lambda: f"addr: Expected 1-D argument vec2, but got {vec2.ndim}-D", | ||
| ) | ||
| self = self.expand(vec1.shape[0], vec2.shape[0]) |
There was a problem hiding this comment.
This unconditional expand seems odd -- maybe we can use a construct like _maybe_expand_to?
There was a problem hiding this comment.
Here is the original code:
check_1d(vec1, "vec1", "addr");
check_1d(vec2, "vec2", "addr");
const auto vec1_size0 = vec1.sizes()[0];
const auto vec2_size0 = vec2.sizes()[0];
auto self_ = &result == &self
? c10::MaybeOwned<Tensor>::borrowed(self)
: expand_size(self, {vec1_size0, vec2_size0}, "addr");
TORCH_CHECK(
self_->dim() == 2,
"2D tensor expected, got ", self_->dim(), "D tensor for input"
);
TORCH_CHECK(
self_->sizes()[0] == vec1_size0 && self_->sizes()[1] == vec2_size0,
"size mismatch, input: ", self_->sizes(),
", v1: ", vec1.sizes(),
", v2: ", vec2.sizes()
);
It looks like the expand is done unconditionally, so I faithfully represented this. Are we expected to simplify the logic when writing refs?
There was a problem hiding this comment.
It'd be nice if we improve it, sure -- if we don't do it now it's just more time and review later
There was a problem hiding this comment.
_maybe_expand_to is... not a thing?
torch/_refs/__init__.py
Outdated
| self.ndim == 2, lambda: f"2D tensor expected, got {self.ndim}D tensor for input" | ||
| ) | ||
| check( | ||
| self.shape[0] == vec1.shape[0] and self.shape[1] == vec2.shape[0], |
There was a problem hiding this comment.
Won't the expand have already asserted this?
There was a problem hiding this comment.
Once again, faithful representation of the original code :) My rule for porting: first do a slavish port, and then once that passes all tests, then refactor.
There was a problem hiding this comment.
haha, OK I guess -- but some cleanups seem easy to snipe
torch/_refs/__init__.py
Outdated
| torch.outer(vec1, vec2) if alpha else torch.full_like(self, False), | ||
| ) | ||
| else: | ||
| return beta * self + alpha * torch.outer(vec1, vec2) |
There was a problem hiding this comment.
I don't think these decompositions for addr are correct. When beta==0, for example, NaNs in self should not propagate
There was a problem hiding this comment.
(this will require adding reference inputs for addr that test the NaN propagation, too!)
There was a problem hiding this comment.
btw, in the original code, even if alpha==0 the NaNs still propagate
| # No meta support for torch.tensor | ||
| DecorateInfo(unittest.expectedFailure, 'TestCommon', 'test_python_reference_meta_functions',), | ||
| # RuntimeError: no _refs support for torch.outer | ||
| DecorateInfo(unittest.expectedFailure, 'TestCommon', 'test_python_reference_consistency',), |
There was a problem hiding this comment.
Eeeks! We should probably get that mode implemented before we start writing refs with torch functions if they have to skip the reference consistency test
There was a problem hiding this comment.
Well, this is what you asked for, but you are welcome to change your mind ;)
We will have a context manager that will reroute torch.* API calls to torch._ref.* (failing if the ref doesn't exist),
and
do you have a preference for two separate tests (but then I need to dupe all the xfails for the new test name I added) or one test (but then an xfail will prevent us checking that the torch.* version works)
one test if it’s easier
My preferred resolution is to maintain only one test which does NOT test with the mode (so torch goes to torch, refs go to refs), and not test anything else. If we insist on testing the strict mode, we will need to add an extra set of tests, because any ref that calls into a torch function that doesn't have a ref will fail in strict mode. We can also test in non-strict mode, in which case you don't get any signal if all torch functions being called have refs or not.
There was a problem hiding this comment.
Yes I have a PR that will be available soon that separates the tests, though, so we can still test for consistency even without refs
There was a problem hiding this comment.
FYI #78026 has landed updating and separating the tests (may require a rebase)
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
|
I think I hit most PR comments. If we're going to have to do this by hand for every op this is going to take a very long time to do properly. |
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
Signed-off-by: Edward Z. Yang <ezyangfb.com> [ghstack-poisoned]
|
@pytorchbot merge this |
|
Hey @ezyang. |
Summary: Signed-off-by: Edward Z. Yang <ezyangfb.com> Pull Request resolved: #78014 Approved by: https://github.com/ngimel Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/a1765f0176250db2b8ce7b543884ec5600ece14c Reviewed By: mehtanirav Differential Revision: D36668872 Pulled By: ezyang fbshipit-source-id: 35a266e666bf812aa7df9594168ae82cd7d63aa1
Stack from ghstack (oldest at bottom):
Signed-off-by: Edward Z. Yang ezyang@fb.com