Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
apex
Commits
7c36c412
Commit
7c36c412
authored
Feb 25, 2019
by
Michael Carilli
Browse files
Forward+backward compatibility fix around
https://github.com/pytorch/pytorch/pull/15744
parent
1b903852
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
+18
-2
apex/amp/rnn_compat.py
apex/amp/rnn_compat.py
+6
-1
apex/amp/wrap.py
apex/amp/wrap.py
+12
-1
No files found.
apex/amp/rnn_compat.py
View file @
7c36c412
...
@@ -11,10 +11,15 @@ def _gen_VF_wrapper(name):
...
@@ -11,10 +11,15 @@ def _gen_VF_wrapper(name):
# Some python magic to generate an object that has the rnn cell functions
# Some python magic to generate an object that has the rnn cell functions
# defined on it, all of which call into corresponding _VF version.
# defined on it, all of which call into corresponding _VF version.
# Intended to patch torch.nn.modules.rnn._VF (aka, the ref named "_VF"
# imported at module scope within torch.nn.modules.rnn). This should
# not affect third-party importers of _VF.py.
class
VariableFunctionsShim
(
object
):
class
VariableFunctionsShim
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
for
name
in
RNN_NAMES
:
for
name
in
RNN_NAMES
:
setattr
(
self
,
name
+
'_cell'
,
_gen_VF_wrapper
(
name
+
'_cell'
))
for
suffix
in
[
''
,
'_cell'
]:
fn_name
=
name
+
suffix
setattr
(
self
,
fn_name
,
_gen_VF_wrapper
(
fn_name
))
def
has_old_rnns
():
def
has_old_rnns
():
try
:
try
:
...
...
apex/amp/wrap.py
View file @
7c36c412
from
.
import
compat
from
.
import
compat
from
.
import
utils
from
.
import
utils
from
.
import
rnn_compat
import
functools
import
functools
...
@@ -206,7 +207,17 @@ def rnn_cast(backend, fn, handle, verbose=False):
...
@@ -206,7 +207,17 @@ def rnn_cast(backend, fn, handle, verbose=False):
utils
.
set_func_save
(
handle
,
backend
,
fn
,
rnn_wrapper
)
utils
.
set_func_save
(
handle
,
backend
,
fn
,
rnn_wrapper
)
def
new_rnn_cast
(
fn
,
handle
,
verbose
=
False
):
def
new_rnn_cast
(
fn
,
handle
,
verbose
=
False
):
# Forward+backward compatibility around https://github.com/pytorch/pytorch/pull/15744
# For rnn backend calls that route through _rnn_impls, we must patch the ref
# that _rnn_impls stashed. For rnn backend calls that directly invoke
# _VF.<backend>, e.g. _VF.lstm, we can patch onto VariableFunctionsShim,
# which in turn has patched the ref named "_VF" in torch.nn.modules.rnn.
if
utils
.
has_func
(
torch
.
nn
.
modules
.
rnn
.
_rnn_impls
,
fn
):
mod
=
torch
.
nn
.
modules
.
rnn
.
_rnn_impls
mod
=
torch
.
nn
.
modules
.
rnn
.
_rnn_impls
else
:
mod
=
torch
.
nn
.
modules
.
rnn
.
_VF
assert
isinstance
(
mod
,
rnn_compat
.
VariableFunctionsShim
)
fn
=
fn
.
lower
()
orig_fn
=
utils
.
get_func
(
mod
,
fn
)
orig_fn
=
utils
.
get_func
(
mod
,
fn
)
cast_fn
=
utils
.
verbosify
(
utils
.
maybe_half
,
fn
,
verbose
)
cast_fn
=
utils
.
verbosify
(
utils
.
maybe_half
,
fn
,
verbose
)
@
functools
.
wraps
(
orig_fn
)
@
functools
.
wraps
(
orig_fn
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment