Skip to content

E2E SymInt example narrow_copy#75759

Closed
Krovatkin wants to merge 4 commits intopytorch:masterfrom
Krovatkin:krovatkin/symbolic_int_node
Closed

E2E SymInt example narrow_copy#75759
Krovatkin wants to merge 4 commits intopytorch:masterfrom
Krovatkin:krovatkin/symbolic_int_node

Conversation

@Krovatkin
Copy link
Copy Markdown
Contributor

@Krovatkin Krovatkin commented Apr 13, 2022

This roughly corresponds to Goal 3.2 in https://docs.google.com/document/d/1iiLNwR5ohAsw_ymfnOpDsyF6L9RTUaHMpD8YLw-jxEw/edit#

Namely:

It adds the following:

  • SymbolicIntNode interface
  • LazySymbolicIntNode implementation
  • Lazy narrow_copy implementation
  • Need add support for SymInt in codegen
  • Test (below)
TEST(LazyDynamicOpsTest, NarrowCopy) {
  auto x = torch::rand({5, 10, 10}).to(kLazy);
  const size_t Y_DIM = 3;
  const size_t X_DIM_INDEX = 2;
  auto y = torch::rand({Y_DIM}).to(kLazy);
  auto ly = torch::lazy::TryGetLtcTensor(y);
  auto dim_node = MakeNode<SizeNode>(ly->GetIrValue(), 0);
  auto lmn = new torch::lazy::SymbolicIntNode(dim_node);
  auto z = x.narrow_copy(X_DIM_INDEX, 0, lmn->toSymInt());
  AllClose(z.cpu(), x.cpu().narrow_copy(X_DIM_INDEX, 0, Y_DIM));
}

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Apr 13, 2022

🔗 Helpful links

💊 CI failures summary and remediations

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

@Krovatkin Krovatkin changed the title E2E SymInt example narrow_copy E2E SymInt example narrow_copy [WIP] Apr 13, 2022
@Krovatkin Krovatkin requested review from miladm and wconstab and removed request for wconstab April 13, 2022 20:38
@Krovatkin Krovatkin changed the title E2E SymInt example narrow_copy [WIP] E2E SymInt example narrow_copy Apr 14, 2022
@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch 2 times, most recently from 7e5bdfb to c5371ed Compare April 15, 2022 21:36
Comment thread aten/src/ATen/core/SymbolicIntNode.h 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.

I am confused. SymInt is the thing that fits in int64_t (and is where all the bitmasking logic ought to live). SymbolicIntNode is supposed to be the boxed thing that you can put all of the random extra IR bits you need to identify the object in question!

Comment thread aten/src/ATen/core/SymInt.h 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.

We shouldn't expose data directly, since it isn't really an int64_t, it might also be some masked nonsense. Also, standard practice in C++ is when we're doing bit-masky stuff, we should represent it as a uint64_t, not a signed integer; signed integers are undefined with bitwise operators, see https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can switch to uint64_t. I was trying to avoid conversions from int64_t to uint64_t since that's the type we are using for dimension values and we aren't doing any "dangerous" bit-wise stuff as shifting.

Comment thread aten/src/ATen/core/SymbolicIntNode.h 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.

instead of doing this make the vector store an owning pointer (either unique_ptr or shared_ptr) so that you don't need an explicit destructor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was thinking we could save some overhead here, but it may not worth it.

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.

unique_ptr is supposed to be zero overhead on top of a regular raw pointer (due to ABI shenanigans this is not strictly true but you are mostly definitely not thinking about that here.)

Comment thread c10/util/SymIntTable.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.

I wouldn't call the transformation you need to do here memoization; instead, the invariant is that SymbolicIntNodes should only ever be allocated SIMULTANEOUSLY with assigning them in the table, so they know their index in that case.

Comment thread test/cpp/lazy/test_lazy_ops.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.

So this tests that a symint can behave correctly in a static condition, and serves as a smoke test. But i guess it doesn't really cover that dynamic aspects?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, though I could extend this and see how it behaves with different values, but this wasn't the goal of the milestone :)

Comment thread c10/util/SymIntTable.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.

hm shouldn't you lock before you check?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hehe, yes

Comment thread tools/codegen/api/lazy.py Outdated
Comment thread tools/codegen/dest/lazy_ir.py 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.

does 'or_list' mean list of symint in this case?

is this generated code correct for both cases? (maybe, as it is a base class)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, I'm thinking of reusing this flag for both symints and list of symints, we don't quite yet have lists of symints if it's confusing please let me know I'll rename the flag latter.

Comment thread torch/csrc/lazy/core/dynamic_ir.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.

i guess this works for all cases (where operand(0) is a constant, a list of constants, a Value, etc.?)

it's cool to see this E2E finally!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Err I think operand(0) should be a tensor otherwise JIT would burp.

Copy link
Copy Markdown
Collaborator

@miladm miladm left a comment

Choose a reason for hiding this comment

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

Thanks @Krovatkin!

I added a couple of comments and questions.

Comment thread aten/src/ATen/core/SymInt.h Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks like data_ is a private member? Do we beed need sci.date() here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we are inside SymInt this and sci is also SymInt. I could switch it to sci_.data() if you think it looks better.

Comment thread test/cpp/lazy/test_lazy_ops.cpp Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Trying to locate how exactly lmn->toSymInt()is propagated to the Lower function. Would be great to discuss it in our meeting tomorrow.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Assuming the SymInt parameter allows a call to the upstream SizeNode object via the toSymbolicIntNode() API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah this is exactly how it works! Hopefully, it's intuitive enough API, but we can work on it.

Comment thread c10/util/SymIntTable.cpp Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit:
We use int64_t and uint64_t for data_. Is it necessary to let size_t propagate as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah I need to do a better job here to reduce implicit conversions

@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch from c5b0f50 to c83bf7e Compare April 20, 2022 17:31
@Krovatkin Krovatkin requested a review from wconstab April 20, 2022 19:03
@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch from c83bf7e to 3199a18 Compare April 20, 2022 19:11
Copy link
Copy Markdown
Contributor

@wconstab wconstab left a comment

Choose a reason for hiding this comment

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

lgtm pending addressing the rest of the review comments!

@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch 2 times, most recently from fe08e31 to 794c08a Compare April 22, 2022 23:45
@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Apr 23, 2022

for the future: could you avoid force pushing, it makes it hard for me to see what differentially changed in the revision. Since you're not using stack diffs it doesn't make a material difference if you rebase or merge, but merging preserves history.

@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch from 794c08a to 5129cf5 Compare April 24, 2022 17:43
@Krovatkin Krovatkin force-pushed the krovatkin/symbolic_int_node branch from 0c85fce to 8d2468f Compare April 25, 2022 17:26
@Krovatkin
Copy link
Copy Markdown
Contributor Author

@pytorchbot merge this

@github-actions
Copy link
Copy Markdown
Contributor

Hey @Krovatkin.
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 Apr 26, 2022
Summary:
This **roughly** corresponds to Goal 3.2 in https://docs.google.com/document/d/1iiLNwR5ohAsw_ymfnOpDsyF6L9RTUaHMpD8 (d843f63f2a49c339fe79c3016a082c60fbf3fed5)YLw-jxEw/edit

Namely:

It adds the following:

* SymbolicIntNode interface
* LazySymbolicIntNode implementation
* Lazy `narrow_copy` implementation
* Need add support for SymInt in codegen
* Test (below)

```cpp
TEST(LazyDynamicOpsTest, NarrowCopy) {
  auto x = torch::rand({5, 10, 10}).to(kLazy);
  const size_t Y_DIM = 3;
  const size_t X_DIM_INDEX = 2;
  auto y = torch::rand({Y_DIM}).to(kLazy);
  auto ly = torch::lazy::TryGetLtcTensor(y);
  auto dim_node = MakeNode<SizeNode>(ly->GetIrValue(), 0);
  auto lmn = new torch::lazy::SymbolicIntNode(dim_node);
  auto z = x.narrow_copy(X_DIM_INDEX, 0, lmn->toSymInt());
  AllClose(z.cpu(), x.cpu().narrow_copy(X_DIM_INDEX, 0, Y_DIM));
}
```

Pull Request resolved: #75759
Approved by: https://github.com/wconstab

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

Reviewed By: osalpekar

Differential Revision: D35938189

Pulled By: Krovatkin

fbshipit-source-id: 59e5371efd27a635b83b4311fa53bb0e11af332b
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 25, 2026
This **roughly** corresponds to Goal 3.2 in https://docs.google.com/document/d/1iiLNwR5ohAsw_ymfnOpDsyF6L9RTUaHMpD8YLw-jxEw/edit#

Namely:

It adds the following:

* SymbolicIntNode interface
* LazySymbolicIntNode implementation
* Lazy `narrow_copy` implementation
* Need add support for SymInt in codegen
* Test (below)

```cpp
TEST(LazyDynamicOpsTest, NarrowCopy) {
  auto x = torch::rand({5, 10, 10}).to(kLazy);
  const size_t Y_DIM = 3;
  const size_t X_DIM_INDEX = 2;
  auto y = torch::rand({Y_DIM}).to(kLazy);
  auto ly = torch::lazy::TryGetLtcTensor(y);
  auto dim_node = MakeNode<SizeNode>(ly->GetIrValue(), 0);
  auto lmn = new torch::lazy::SymbolicIntNode(dim_node);
  auto z = x.narrow_copy(X_DIM_INDEX, 0, lmn->toSymInt());
  AllClose(z.cpu(), x.cpu().narrow_copy(X_DIM_INDEX, 0, Y_DIM));
}
```

Pull Request resolved: pytorch#75759
Approved by: https://github.com/wconstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants