Skip to content

Conversation

laithsakka
Copy link
Contributor

@laithsakka laithsakka commented Oct 1, 2025

Stack from ghstack (oldest at bottom):

Three fixes:

  1. When doing t[u0] +=1 if u0 is unbacked we could allocate a new unbacked symbol during the the indexing of t[u0] (when we fake trace setitem), namely because meta_select does allocate a new unbacked symbol for the storage offset when we do not know if u0>=0 or u0<0. but the output size/stride of setitem(), does not depend on that new symbol. it's self consumed in setitem so we shall ignore it.

  2. Also when we trace through generalized_scatter the applications of the views could allocate unbacked symints
    but those do not effect final output, we also shall ignore them.

3.Before accessing strides in lowering we shall materialize.

Address #114293 and #131911

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @chenyang78 @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @Lucaskabela

Copy link

pytorch-bot bot commented Oct 1, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/164341

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 95f2e2d with merge base 219fb6a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

laithsakka added a commit that referenced this pull request Oct 1, 2025
ghstack-source-id: 99f017a
Pull Request resolved: #164341
@laithsakka laithsakka changed the title Fix issues with selec_scatter on unbacked Fix issues with select_scatter on unbacked and item allocated unbacked symbols. Oct 1, 2025
@laithsakka laithsakka requested a review from eellison October 1, 2025 07:20
…ted unbacked symbols."



Address #162110 and #114293

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben Lucaskabela

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Oct 1, 2025
ghstack-source-id: b675d4a
Pull Request resolved: #164341
@laithsakka laithsakka added the topic: not user facing topic category label Oct 2, 2025
@laithsakka laithsakka marked this pull request as ready for review October 2, 2025 03:48
@laithsakka laithsakka requested review from aorenste and zou3519 October 2, 2025 03:56
…ted unbacked symbols."


Three fixes:
1. When doing t[x] +=1  if x is unbacked we could allocate a new unbacked symbol during the the indexing of t[x]  but 
the output size/stride does not depend on it in this case. it's self consumed during meta tracing so we shall ignore it. 

2. Also when we trace through generalized_scatter the applications of the views could allocate unbacked symints
but those do not effect final output, we also shall ignore them.

3.Before accessing strides in lowering we shall materialize. 

Address #162110 and #114293

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben Lucaskabela

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Oct 2, 2025
ghstack-source-id: bdd47ed
Pull Request resolved: #164341
@laithsakka laithsakka requested a review from pianpwk October 2, 2025 05:57
@laithsakka laithsakka changed the title Fix issues with select_scatter on unbacked and item allocated unbacked symbols. Fix issues with select_scatter and update_item allocated unbacked symbols. Oct 2, 2025
@laithsakka laithsakka requested a review from ezyang October 2, 2025 15:13
…nbacked symbols."


Three fixes:
1. When doing t[x] +=1  if x is unbacked we could allocate a new unbacked symbol during the the indexing of t[x]  but 
the output size/stride does not depend on it in this case. it's self consumed during meta tracing so we shall ignore it. 

2. Also when we trace through generalized_scatter the applications of the views could allocate unbacked symints
but those do not effect final output, we also shall ignore them.

3.Before accessing strides in lowering we shall materialize. 

Address  #114293 and #131911

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben Lucaskabela

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Oct 2, 2025
ghstack-source-id: 216973d
Pull Request resolved: #164341
@laithsakka
Copy link
Contributor Author

laithsakka commented Oct 6, 2025

I don't understand the pr description for (1). What does it mean that you get a new unbacked symbol if x is unbacked?

@ezyang
if we have
u0 = ...
and then have
t[u0] +=1

during the meta_select we allocate new unbacked symint to represent the storage offset in the output of the select op. (because we do not know if u0>=0 or u0<0)
the problem is that when we trace setitem its meta call meta_select, but the final output of setitem does not use that symbol.

updated summary.

…unbacked symbols."


Three fixes:
1. When doing t[u0] +=1  if x is unbacked we could allocate a new unbacked symbol during the the indexing of t[u0] (when we fake trace setitem), namely because meta_select does allocate a new unbacked symbol for the storage offset when we do not know if u0>=0 or u0<0.  but the output size/stride of setitem(), does not depend on that new symbol. it's self consumed in setitem so we shall ignore it. 

2. Also when we trace through generalized_scatter the applications of the views could allocate unbacked symints
but those do not effect final output, we also shall ignore them.

3.Before accessing strides in lowering we shall materialize. 

Address  #114293 and #131911

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben Lucaskabela

[ghstack-poisoned]
if fake_mode and fake_mode.shape_env
else nullcontext()
):
tmp = view.target(tmp, *fake_args, **fake_kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also confused here. You are going to have view.target inside of the graph now right? So tmp DOES have the unbacked symbol, and you should resolve unbacked symbols here (instead of suppressing them)

Copy link
Contributor Author

@laithsakka laithsakka Oct 8, 2025

Choose a reason for hiding this comment

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

this is Not the function that does the de-composition ,
this is the function used to trace the _generalized_scatter.

for the function that does the decomposition we need to do what you said right which i am doing on the PR that is right on top of this one on the stack.
#164948

for this function we self consume the views but return return inp on line 98 which would not have a reference to the new symbols in its example input.

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

vc me if you don't agree

@laithsakka
Copy link
Contributor Author

laithsakka commented Oct 15, 2025

We discussed this offline for
set_item, we discussed that the alternative approach is to decompose the set_item during dynamo tracing by writing a pollyfill that mimic the logic of this function. I will do that in a different PR since it requires careful implementation.

inline void set_item(
    const Tensor& self,
    const ArrayRef<TensorIndex>& indices,
    const Tensor& value,
    bool disable_slice_optimization = false) {
  at::Device self_device = self.device();
  SymIntArrayRef self_sizes = self.sym_sizes();

  // handle simple types: integers, slices, ellipsis, bool
  if (indices.size() == 1) {
    const TensorIndex& index = indices[0];
    if (index.is_boolean() && !index.boolean()) {
      // do nothing for false (technically we should check the size, but we
      // don't have real 0-sized shapes.
      return;
    } else if (index.is_ellipsis()) {
      copy_to(self, value);
      return;
    } else if (index.is_none() || (index.is_boolean() && index.boolean())) {
      copy_to(self.unsqueeze(0), value);
      return;
    } else if (index.is_integer()) {
      copy_to(
          impl::applySelect(
              self, 0, index.integer(), 0, self_device, self_sizes),
          value);
      return;
    } else if (index.is_slice()) {
      copy_to(
          impl::applySlice(
              self,
              0,
              index.slice().start(),
              index.slice().stop(),
              index.slice().step(),
              /*disable_slice_optimization=*/disable_slice_optimization,
              self_device,
              self_sizes),
          value);
      return;
    }
  }

  std::vector<Tensor> tensorIndices;
  Tensor sliced = impl::applySlicing(
      self,
      indices,
      tensorIndices,
      disable_slice_optimization,
      self_device,
      self_sizes);
  if (tensorIndices.empty()) {
    copy_to(sliced, value);
    return;
  }

  SymIntArrayRef valueSizes = value.sym_sizes();
  SymIntArrayRef slicedValueSizes = slicePrefix1sSize(valueSizes);
  Tensor valuesSliced;
  if (!valueSizes.equals(slicedValueSizes)) {
    valuesSliced = value.view_symint(slicedValueSizes);
  } else {
    valuesSliced = value;
  }
  dispatch_index_put_(sliced, std::move(tensorIndices), valuesSliced);
  return;
}

for the generalized_scatter the current approach is fine @bobrenjc93 is looking at this problem more generally for High order ops.

@laithsakka laithsakka requested a review from ezyang October 15, 2025 05:50
with torch._dynamo.utils._disable_saved_tensors_hooks_during_tracing():

# Ignore fresh unbacked symbols that could arise from the internal indexing (selection),
# that happen in code like t[idx] += 1 when idx is unabacked. Namely the selection
Copy link
Contributor

Choose a reason for hiding this comment

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

s/unabacked/unbacked

# Additionally, we want to avoid accidental unbacked unsqueeze semantics. To resolve this,
# we use as_strided instead.
# Removing this branch will cause test_unbacked_select_index_with_check to fail.
x.realize()
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment?

assert len(unbacked_bindings) == 1, unbacked_bindings
unbacked_offset_sym, _ = next(iter(unbacked_bindings.items()))

x.realize()
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #164948

…unbacked symbols."


Three fixes:
1. When doing t[u0] +=1  if u0 is unbacked we could allocate a new unbacked symbol during the the indexing of t[u0] (when we fake trace setitem), namely because meta_select does allocate a new unbacked symbol for the storage offset when we do not know if u0>=0 or u0<0.  but the output size/stride of setitem(), does not depend on that new symbol. it's self consumed in setitem so we shall ignore it. 

2. Also when we trace through generalized_scatter the applications of the views could allocate unbacked symints
but those do not effect final output, we also shall ignore them.

3.Before accessing strides in lowering we shall materialize. 

Address  #114293 and #131911

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy chenyang78 kadeng muchulee8 amjames chauhang aakhundov coconutruben Lucaskabela

[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Oct 17, 2025
ghstack-source-id: bf48269
Pull Request resolved: #164341
@laithsakka
Copy link
Contributor Author

address comments and rebase

@laithsakka
Copy link
Contributor Author

@pytorchmergebot merge

Copy link

pytorch-bot bot commented Oct 18, 2025

This PR has pending changes requested. Please address the comments and update the PR before merging.

@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #164948

pytorchmergebot pushed a commit that referenced this pull request Oct 18, 2025
…g generalize_scatter decomp (#164948)

Two fixes:
1. in rein_place pass, set unbacked bindings for newly created nodes.
2. In inductor, ComputeBuffer used to miss detecting some used symbols, fixed that.

Pull Request resolved: #164948
Approved by: https://github.com/bobrenjc93
ghstack dependencies: #164341
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.

4 participants