-
Notifications
You must be signed in to change notification settings - Fork 307
Avoid unintended eager cuda initialization #199
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
Open
dkasapp
wants to merge
21
commits into
master
Choose a base branch
from
dk/fix-import-cuda-init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
38b48ec
fix importing sru loads cuda
hpasapp 07bb9bb
this doesnt work, because of torchscript
hpasapp 8648c1f
use env var to guard initialiaiton
hpasapp 91fff61
Change circleCI to python 3.8
dkasapp 7510373
migraet the init test into modules.py
hpasapp 744a718
Merge branch 'hp/fix-import-cuda-init' of github.com:asappresearch/sr…
hpasapp 35f7322
init function works
hpasapp 9fd778a
automtically initialize, even in presence of env var
hpasapp cbce3e2
Revert "Change circleCI to python 3.8"
dkasapp ce87ed5
Changes for modules only
dkasapp ca4e006
Remove unused import
dkasapp 1f819c5
Add a test for no eager cuda init to be run first of all
dkasapp 3823c4f
Added docstring
dkasapp ca7aa32
Attempt to fix pipeline: Bump orb and python versions
dkasapp edf84c6
Attempt to fix pipeline: Update torch wheel
dkasapp d18d912
Attempt to fix pipeline: Fix cmake installation
dkasapp f54ef3d
Module javadoc
dkasapp 24a6370
Bump version
dkasapp 62f229f
Add additional init in apply_recurrence (multi gpu issue)
dkasapp eb0da19
Revert "Add additional init in apply_recurrence (multi gpu issue)"
dkasapp dabdc1a
Change version to 2.7.0-rc1
dkasapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,6 @@ | |
| from torch import Tensor | ||
| from torch.nn.utils.rnn import PackedSequence | ||
|
|
||
| from sru.ops import (elementwise_recurrence_inference, | ||
| elementwise_recurrence_gpu, | ||
| elementwise_recurrence_naive) | ||
|
|
||
|
|
||
| class SRUCell(nn.Module): | ||
| """ | ||
|
|
@@ -27,6 +23,11 @@ class SRUCell(nn.Module): | |
| scale_x: Tensor | ||
| weight_proj: Optional[Tensor] | ||
|
|
||
| initialized = False | ||
| elementwise_recurrence_inference = None | ||
| elementwise_recurrence_gpu = None | ||
| elementwise_recurrence_naive = None | ||
|
|
||
| def __init__(self, | ||
| input_size: int, | ||
| hidden_size: int, | ||
|
|
@@ -160,6 +161,19 @@ def __init__(self, | |
| self.layer_norm = nn.LayerNorm(self.input_size) | ||
|
|
||
| self.reset_parameters() | ||
| SRUCell.init_elementwise_recurrence_funcs() | ||
|
|
||
| @classmethod | ||
| def init_elementwise_recurrence_funcs(cls): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a docstring to this method please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| if cls.initialized: | ||
| return | ||
| from sru.ops import (elementwise_recurrence_inference, | ||
| elementwise_recurrence_gpu, | ||
| elementwise_recurrence_naive) | ||
| cls.elementwise_recurrence_inference = elementwise_recurrence_inference | ||
| cls.elementwise_recurrence_gpu = elementwise_recurrence_gpu | ||
| cls.elementwise_recurrence_naive = elementwise_recurrence_naive | ||
| cls.initialized = True | ||
|
|
||
| def reset_parameters(self): | ||
| """Properly initialize the weights of SRU, following the same | ||
|
|
@@ -295,27 +309,27 @@ def apply_recurrence(self, | |
| """ | ||
| if not torch.jit.is_scripting(): | ||
| if self.bias.is_cuda: | ||
| return elementwise_recurrence_gpu(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad, | ||
| self.amp_recurrence_fp16) | ||
| return SRUCell.elementwise_recurrence_gpu(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad, | ||
| self.amp_recurrence_fp16) | ||
| else: | ||
| return elementwise_recurrence_naive(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad) | ||
| return SRUCell.elementwise_recurrence_naive(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad) | ||
| else: | ||
| return elementwise_recurrence_inference(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad) | ||
| return SRUCell.elementwise_recurrence_inference(U, residual, V, self.bias, c0, | ||
| self.activation_type, | ||
| self.hidden_size, | ||
| self.bidirectional, | ||
| self.has_skip_term, | ||
| scale_val, mask_c, mask_pad) | ||
|
|
||
|
|
||
| def compute_UV(self, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import torch | ||
|
|
||
|
|
||
| # Run at the beginning of the test suites to ensure no previous use of SRUCells | ||
| def test_no_eager_cuda_init(): | ||
| # Notice the test is expected to pass both with GPU available and without it | ||
| import sru | ||
| assert not torch.cuda.is_initialized() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a note about this function initialization to the SRUCell docstring or the docstring for sru/modules.py (it lacks a module docstring now, it probably should have one)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added