pytorch/caffe2/python/models/seq2seq/seq2seq_model_helper.py
Xuehai Pan 8d45f555d7 [BE] [1/3] Rewrite super() calls in caffe2 and benchmarks (#94587)
Rewrite Python built-in class `super()` calls. Only non-semantic changes should be applied.

- #94587
- #94588
- #94592

Also, methods with only a `super()` call are removed:

```diff
class MyModule(nn.Module):
-   def __init__(self):
-       super().__init__()
-
    def forward(self, ...):
        ...
```

Some cases that change the semantics should be kept unchanged. E.g.:

f152a79be9/caffe2/python/net_printer.py (L184-L190)

f152a79be9/test/test_jit_fuser_te.py (L2628-L2635)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94587
Approved by: https://github.com/ezyang
2023-02-11 18:19:48 +00:00

82 lines
2.5 KiB
Python

## @package seq2seq_model_helper
# Module caffe2.python.models.seq2seq.seq2seq_model_helper
from caffe2.python import scope
from caffe2.python.model_helper import ModelHelper
class Seq2SeqModelHelper(ModelHelper):
def __init__(self, init_params=True, **kwargs):
arg_scope = {
'use_cudnn': kwargs.pop('use_cudnn', True),
'cudnn_exhaustive_search': kwargs.pop('cudnn_exhaustive_search', False),
'order': 'NHWC',
}
if kwargs.get('ws_nbytes_limit', None):
arg_scope['ws_nbytes_limit'] = kwargs.pop('ws_nbytes_limit')
super().__init__(init_params=init_params, arg_scope=arg_scope, **kwargs)
self.non_trainable_params = []
def AddParam(self, name, init=None, init_value=None, trainable=True):
"""Adds a parameter to the model's net and it's initializer if needed
Args:
init: a tuple (<initialization_op_name>, <initialization_op_kwargs>)
init_value: int, float or str. Can be used instead of `init` as a
simple constant initializer
trainable: bool, whether to compute gradient for this param or not
"""
if init_value is not None:
assert init is None
assert type(init_value) in [int, float, str]
init = ('ConstantFill', dict(
shape=[1],
value=init_value,
))
if self.init_params:
param = self.param_init_net.__getattr__(init[0])(
[],
name,
**init[1]
)
else:
param = self.net.AddExternalInput(name)
if trainable:
self.params.append(param)
else:
self.non_trainable_params.append(param)
return param
def GetNonTrainableParams(self, namescope=None):
'''
Returns the params in current namescope
'''
if namescope is None:
namescope = scope.CurrentNameScope()
else:
if not namescope.endswith(scope._NAMESCOPE_SEPARATOR):
namescope += scope._NAMESCOPE_SEPARATOR
if namescope == '':
return self.non_trainable_params[:]
else:
return [
p for p in self.non_trainable_params
if p.GetNameScope() == namescope
]
def GetAllParams(self, namescope=None):
return (
self.GetParams(namescope) +
self.GetComputedParams(namescope) +
self.GetNonTrainableParams(namescope)
)