Skip to content

[Static Runtime] Fix MemoryPlanner dtor crash in debug mode#79942

Closed
mikeiovine wants to merge 1 commit intopytorch:masterfrom
mikeiovine:export-D37303728
Closed

[Static Runtime] Fix MemoryPlanner dtor crash in debug mode#79942
mikeiovine wants to merge 1 commit intopytorch:masterfrom
mikeiovine:export-D37303728

Conversation

@mikeiovine
Copy link
Copy Markdown

Summary:
Memory planner destruction was hitting this assertion in debug mode for a few models.

Here's what was going on:

  1. The set of unmanaged IValues acquires one or more owning refs of a managed StorageImpl
  2. Then, one or more tensors in that storage group have their StorageImpl swapped out during execution
  3. During deallocateManagedTensors, we swap the correct StorageImpl back in, calling unsafe_adapt_non_heap_allocated again and resetting the refcount
  4. The unmanaged IValues are deallocated, decrementing the refcount into the danger zone.

So, we just have to make sure that unmanaged IValues are destructed before we deallocate the managed tensors.

Test Plan: CI

Differential Revision: D37303728

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Jun 21, 2022

🔗 Helpful links

✅ No Failures (0 Pending)

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

@facebook-github-bot facebook-github-bot added oncall: jit Add this issue/PR to JIT oncall triage queue fb-exported labels Jun 21, 2022
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D37303728

…79942)

Summary:
Pull Request resolved: pytorch#79942

Memory planner destruction was hitting [this assertion](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/c10/util/intrusive_ptr.h?lines=117) in debug mode for a few models.

Here's what was going on:

1) The set of unmanaged `IValue`s acquires one or more owning refs of a managed `StorageImpl`
2) Then, one or more tensors in that storage group have their `StorageImpl` swapped out during execution
3) During `deallocateManagedTensors`, we swap the correct `StorageImpl` back in, [calling `unsafe_adapt_non_heap_allocated` again and resetting the refcount](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/torch/csrc/jit/runtime/static/memory_planner.cpp?lines=446-452)
4) The unmanaged `IValues` are deallocated, decrementing the refcount into the danger zone.

So, we just have to make sure that unmanaged `IValue`s are destructed before we deallocate the managed tensors.

Test Plan: CI

Differential Revision: D37303728

fbshipit-source-id: dffc079c84dc92b5dad44758cb2bafbf60dc0d0e
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D37303728

@tenpercent tenpercent self-requested a review June 21, 2022 17:08
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

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

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

@mikeiovine your PR has been successfully merged.

@github-actions
Copy link
Copy Markdown
Contributor

Hey @mikeiovine.
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 21, 2022
Summary:
Pull Request resolved: #79942

Memory planner destruction was hitting [this assertion](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/c10/util/intrusive_ptr.h?lines=117) in debug mode for a few models.

Here's what was going on:

1) The set of unmanaged `IValue`s acquires one or more owning refs of a managed `StorageImpl`
2) Then, one or more tensors in that storage group have their `StorageImpl` swapped out during execution
3) During `deallocateManagedTensors`, we swap the correct `StorageImpl` back in, [calling `unsafe_adapt_non_heap_allocated` again and resetting the refcount](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/torch/csrc/jit/runtime/static/memory_planner.cpp?lines=446-452)
4) The unmanaged `IValues` are deallocated, decrementing the refcount into the danger zone.

So, we just have to make sure that unmanaged `IValue`s are destructed before we deallocate the managed tensors.

Test Plan: CI

Reviewed By: ajyu, tenpercent

Differential Revision: D37303728

fbshipit-source-id: 82b58221d45ab04a30cb7358c8bbeb124f71129d
miladm pushed a commit to miladm/pytorch that referenced this pull request Jun 27, 2022
…79942)

Summary:
Memory planner destruction was hitting [this assertion](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/c10/util/intrusive_ptr.h?lines=117) in debug mode for a few models.

Here's what was going on:

1) The set of unmanaged `IValue`s acquires one or more owning refs of a managed `StorageImpl`
2) Then, one or more tensors in that storage group have their `StorageImpl` swapped out during execution
3) During `deallocateManagedTensors`, we swap the correct `StorageImpl` back in, [calling `unsafe_adapt_non_heap_allocated` again and resetting the refcount](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/torch/csrc/jit/runtime/static/memory_planner.cpp?lines=446-452)
4) The unmanaged `IValues` are deallocated, decrementing the refcount into the danger zone.

So, we just have to make sure that unmanaged `IValue`s are destructed before we deallocate the managed tensors.

Test Plan: CI

Differential Revision: D37303728

Pull Request resolved: pytorch#79942
Approved by: https://github.com/tenpercent
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 25, 2026
…79942)

Summary:
Memory planner destruction was hitting [this assertion](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/c10/util/intrusive_ptr.h?lines=117) in debug mode for a few models.

Here's what was going on:

1) The set of unmanaged `IValue`s acquires one or more owning refs of a managed `StorageImpl`
2) Then, one or more tensors in that storage group have their `StorageImpl` swapped out during execution
3) During `deallocateManagedTensors`, we swap the correct `StorageImpl` back in, [calling `unsafe_adapt_non_heap_allocated` again and resetting the refcount](https://www.internalfb.com/code/fbsource/[f8baf8a0bab462c860d2eb7491a4e3fb40d2907a]/fbcode/caffe2/torch/csrc/jit/runtime/static/memory_planner.cpp?lines=446-452)
4) The unmanaged `IValues` are deallocated, decrementing the refcount into the danger zone.

So, we just have to make sure that unmanaged `IValue`s are destructed before we deallocate the managed tensors.

Test Plan: CI

Differential Revision: D37303728

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

Labels

cla signed fb-exported Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants