Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less GIL locks speeds things up apparently ? #31

Merged
merged 5 commits into from
Oct 21, 2022
Merged

Conversation

Narsil
Copy link
Collaborator

@Narsil Narsil commented Oct 20, 2022

This PR reduces the locations where the GIL is taken, which at least in appearance speeds up significantly the load speed for PT.
I am now on my local computer ~3x faster to load on CPU.

The PR stems from realizing that trying to load on GPU directly, was consuming a LOT of CPU (it still does).
GPU loading was much slower to load everything on CPU then moving to GPU than directly loading on GPU (tensor per tensor).

GIL definitely seems at play here.

So safe_open now receives a device argument to that the various functons can return tensors directly on the correct location.

No check or concern for TF or Flax has been taken yet (numpy cannot allocate on GPU).
When we do we'll probably have to make Device uniform across library.

@Narsil Narsil requested review from sgugger and mishig25 October 20, 2022 16:02
Copy link
Contributor

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Nice add!

self.assertTrue(torch.allclose(v, tv))
self.assertEqual(v.device, torch.device("cpu"))

def test_deserialization_safe_gpu(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should have some kind of decorator to skip it if there is no GPU (will be necessary to get the CI green).

@Narsil Narsil merged commit 634decc into main Oct 21, 2022
@Narsil Narsil deleted the speedup_torch_gil branch October 21, 2022 08:57
}
}

fn create_tensor(
py: Python,
Copy link
Collaborator

@mishig25 mishig25 Oct 21, 2022

Choose a reason for hiding this comment

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

what is the advantage/reason of removing py arg & using Python::with_gil(|py| { ?
Was it required for the new changes to work OR it was some clean up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is the core of the thing.
I must say I don't completely understand but it comes from here:

PyO3/pyo3#1056
PyO3/pyo3#1547

Basically you want to hold the gil as little as possible to give a chance to pyo3 to release it's objects (which then makes the memory available for follow up tensor creation)

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh I see 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants