Skip to content

Acquire GIL when constructing/destructing ConcretePyObjectHolder#37870

Closed
mrshenli wants to merge 7 commits intogh/mrshenli/167/basefrom
gh/mrshenli/167/head
Closed

Acquire GIL when constructing/destructing ConcretePyObjectHolder#37870
mrshenli wants to merge 7 commits intogh/mrshenli/167/basefrom
gh/mrshenli/167/head

Conversation

@mrshenli
Copy link
Copy Markdown
Contributor

@mrshenli mrshenli commented May 5, 2020

Stack from ghstack:

Differential Revision: D21410785

@mrshenli mrshenli requested a review from apaszke as a code owner May 5, 2020 20:13
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 5, 2020
@mrshenli mrshenli requested a review from wanchaol May 5, 2020 20:51
@mrshenli
Copy link
Copy Markdown
Contributor Author

mrshenli commented May 5, 2020

Hey @wanchaol, does the change look reasonable to you?

@mrshenli mrshenli requested a review from xush6528 May 5, 2020 22:03
Comment thread torch/csrc/jit/python/python_ivalue.h
static c10::intrusive_ptr<PyObjectHolder> create(py::object py_obj) {
return c10::make_intrusive<ConcretePyObjectHolder>(py_obj);
return c10::make_intrusive<ConcretePyObjectHolder>(std::move(py_obj));
}
Copy link
Copy Markdown
Contributor

@xush6528 xush6528 May 5, 2020

Choose a reason for hiding this comment

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

Before this change, I think when creating a PyObjectHolder, the passed py obj ref count +2 and then -1. This change makes it only +1 one time.

@@ -11,17 +11,21 @@ namespace ivalue {
struct C10_EXPORT ConcretePyObjectHolder final : PyObjectHolder {
public:
static c10::intrusive_ptr<PyObjectHolder> create(py::object py_obj) {
Copy link
Copy Markdown
Contributor

@xush6528 xush6528 May 5, 2020

Choose a reason for hiding this comment

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

Since you are making this data wrapper take care of acquiring GIL on destruction.

I wonder can it also acquires GIL on creation?

So that you don't have to do it in upper level, as in #37871.

    case TypeKind::PyObjectType:
      // convert a py::handle to the IValue that holds the py::object
      py::object py_obj;
      {
        py::gil_scoped_acquire ag;
        py_obj = obj.cast<py::object>();
      }}
      return c10::ivalue::ConcretePyObjectHolder::create(
          std::move(py_obj));

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.

good point! I will make the change if this looks good to @wanchaol too.

@mrshenli mrshenli changed the title Acquire GIL when destructing ConcretePyObjectHolder Acquire GIL when constructing/destructing ConcretePyObjectHolder May 6, 2020
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented May 6, 2020

💊 CI failures summary and remediations

As of commit dc28784 (more details on the Dr. CI page):


None of the CI failures appear to be your fault 💚



🚧 1 ongoing upstream failure:

These were probably caused by upstream breakages that are not fixed yet:


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 9 times.

Copy link
Copy Markdown
Collaborator

@wanchaol wanchaol left a comment

Choose a reason for hiding this comment

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

Some nits, generally looks good to me, thanks!

Comment thread torch/csrc/jit/python/pybind_utils.h Outdated
public:
static c10::intrusive_ptr<PyObjectHolder> create(py::object py_obj) {
return c10::make_intrusive<ConcretePyObjectHolder>(py_obj);
return c10::make_intrusive<ConcretePyObjectHolder>(std::move(py_obj));
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.

can we move the gil acquisition to the constructor as well (i.e. move the logic from toIValue to here)?

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 it's to change the constructor takes py::handle, and cast it to py::object with GIL

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.

Yes :)

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.

Actually, I would prefer adding a new constructor that takes a py::handle, and move the logic to there. We should still keep the constructor that takes py::object without acquire gil for C++ usage.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in ee1ddce.

xuezhou1998 pushed a commit to xuezhou1998/new_pytorch that referenced this pull request May 9, 2020
@facebook-github-bot facebook-github-bot deleted the gh/mrshenli/167/head branch May 11, 2020 14:20
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…orch#37870)

Summary: Pull Request resolved: pytorch#37870

Test Plan: Imported from OSS

Differential Revision: D21410785

fbshipit-source-id: 374d5f40fbdfec98262aa4c84ec4ccdc40fb2ac1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants