Skip to content

Added support for expand in LazyTensor shape inference#77830

Closed
miladm wants to merge 9 commits intopytorch:masterfrom
miladm:expand_shape_inf
Closed

Added support for expand in LazyTensor shape inference#77830
miladm wants to merge 9 commits intopytorch:masterfrom
miladm:expand_shape_inf

Conversation

@miladm
Copy link
Copy Markdown
Collaborator

@miladm miladm commented May 19, 2022

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented May 19, 2022

🔗 Helpful links

✅ No Failures (0 Pending)

As of commit df3f90d (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.

Click here to manually regenerate this comment.

@miladm miladm marked this pull request as draft May 19, 2022 06:30
@miladm miladm changed the title Added support for expand in LazyTensor shape inference [POC][WIP] Added support for expand in LazyTensor shape inference May 19, 2022
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Krovatkin wdyt?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion summary: #77882

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Krovatkin wdyt?

@miladm miladm assigned miladm and unassigned miladm May 19, 2022
@miladm miladm requested review from Krovatkin and wconstab May 19, 2022 16:48
@miladm miladm marked this pull request as ready for review May 19, 2022 17:27
@miladm miladm changed the title [POC][WIP] Added support for expand in LazyTensor shape inference [WIP] Added support for expand in LazyTensor shape inference May 19, 2022
@miladm miladm added the lazy Lazy Tensor work items label May 19, 2022
@miladm miladm requested a review from JackCaoG May 19, 2022 23:22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we determine which ops to write SymIntArrayRef versions of? Is it (a) all ops that take size inputs, (b) only ops that we expect to consume outputs of dynamic ops like nonzero in models? or something else

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(b) only ops that we expect to consume outputs of dynamic ops like nonzero in models?

I believe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. relatedly- how does it manifest when one is missing? presumably it would show up as a compile error. but it's not immediately clear to me how that works. Who actually calls the 'compute_shape_expand' variant with symints?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. relatedly- how does it manifest when one is missing? presumably it would show up as a compile error.

I think it would show up as a runtime error when a dispatcher wouldn't be able to find a dispatch that takes a version with SymInts.

Who actually calls the 'compute_shape_expand' variant with symints?

The dispatcher. When a user passes sym ints either in python or c++, the dispatcher should dispatch to the expand overload that takes symints?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. the dispatcher doesn't call compute_shape_* right?

but i think i see your point, that we would specifically implement a .symint version of a nativefunc for lazytensor, and if we did that, then we have to implement this variant of compute_shape.

@miladm
Copy link
Copy Markdown
Collaborator Author

miladm commented May 21, 2022

@Krovatkin I will handle the -1 scenario for expand.symint when I return.
Looks like the PR will be unblocked soon if not already.

@miladm miladm force-pushed the expand_shape_inf branch from be1a8f6 to 1fd5cad Compare June 14, 2022 00:19
@miladm miladm requested a review from wconstab June 16, 2022 04:07
Copy link
Copy Markdown
Contributor

@Krovatkin Krovatkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks gut but there's one thing we need to clarify

Comment thread torch/csrc/lazy/core/shape_inference.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miladm I thought we decided to keep -1 as a plain integer? @suo added support for this case.
I think your check should say:
_sizes[idx].is_symbolic() then do

 auto lazySymIntNode = std::dynamic_pointer_cast<torch::lazy::SymbolicIntNode>(symbolicIntNode);
    auto size_node = lazySymIntNode->node_;
    auto static_value = std::dynamic_pointer_cast<torch::lazy::DimensionNode>(size_node)->getStaticValue();

otherwise just do _sizes[idx].data() .

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

@miladm miladm requested a review from Krovatkin June 17, 2022 02:26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miladm i was confused by this part, what does it mean to convert to a static value, this is a real int?

Copy link
Copy Markdown
Collaborator Author

@miladm miladm Jun 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wconstab this is an int value that returns the static upper bound. (base class ref)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wconstab that's just my unfortunate naming. staticValue == upperBound.

@miladm miladm requested a review from wconstab June 17, 2022 21:12
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miladm are you sure this is correct?

If I run the example below, PyTorch produces an error:

>>> import torch
>>> a = torch.rand(3)
>>> a.expand(-1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: The expanded size of the tensor (-1) isn't allowed in a leading, non-existing dimension 0

while your shape inference would silently estimate target_size[0] as 0?

Copy link
Copy Markdown
Collaborator Author

@miladm miladm Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. When I test on PT and PT/XLA, I don't hit your observation meaning the code errors out as expected. Perhaps my code flow doesn't hit the shape inference the way it does for you? Let me know what you ran to observe the inconsistency.

Sound good. Let's land this and open follow up issues as needed. @Krovatkin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need an extra if in here:

} else {
  if (_sizes[idx].data() == -1) {
      // -1 can't be specified for non-existing dimensions
      TORCH_CHECK(idx >= num_new_dimensions);
       target_size[idx] =  padded_self[idx];
  } else {
     target_size[idx] =  _sizes[idx];
  }
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct.

I think we need to call the .data() method when assigning to target_size[idx]. Let me know if you think differently @Krovatkin.

@Krovatkin Krovatkin self-requested a review June 20, 2022 19:50
Copy link
Copy Markdown
Contributor

@Krovatkin Krovatkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: we will need to clarify this : https://github.com/pytorch/pytorch/pull/77830/files#r901962659 sooner or later, but let's land this version for now.

@Krovatkin Krovatkin removed the request for review from a team June 29, 2022 00:27
@miladm
Copy link
Copy Markdown
Collaborator Author

miladm commented Jun 29, 2022

Thanks for your help @Krovatkin!

@miladm
Copy link
Copy Markdown
Collaborator Author

miladm commented Jun 29, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a merge job. Check the current status here

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

@miladm your PR has been successfully merged.

@github-actions
Copy link
Copy Markdown
Contributor

Hey @miladm.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

facebook-github-bot pushed a commit that referenced this pull request Jun 30, 2022
…7830)

Summary:
Added support for `expand` in LazyTensor shape inference
Fixes #77831

 ---

**Blockers:**

- [x] #77880
- [x] #77882

Pull Request resolved: #77830
Approved by: https://github.com/Krovatkin

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/0922cc024eeafa2158c0d00396494a0ae983f8cb

Reviewed By: b0noI

Differential Revision: D37523035

fbshipit-source-id: 2e88e9a8a85c0a9e504fec92925cec0a05588892
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 25, 2026
Added support for `expand` in LazyTensor shape inference
Fixes pytorch#77831

---

**Blockers:**

- [x] pytorch#77880
- [x] pytorch#77882
Pull Request resolved: pytorch#77830
Approved by: https://github.com/Krovatkin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for expand in LazyTensor shape inference

6 participants