From 1c5a3250cf51f92cda1d0c546f9dd71e82d2a779 Mon Sep 17 00:00:00 2001 From: chinakook Date: Sat, 4 Apr 2020 14:06:14 +0800 Subject: [PATCH 1/8] fix block.export ```net.hybridize``` may optimize out some ops. These ops are alive in nn.Block(also nn.HybridBlock), but its names are not contained in symbol's ```arg_names``` list. So ignore these ops except that their name are end with 'running_mean' or 'running_var'. --- python/mxnet/gluon/block.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 10c11b85ba97..61a6d4a6a968 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1271,8 +1271,9 @@ def export(self, path, epoch=0, remove_amp_cast=True): if name in arg_names: arg_dict['arg:%s'%name] = param._reduce() else: - assert name in aux_names - arg_dict['aux:%s'%name] = param._reduce() + if name.endswith('running_mean') or name.endswith('running_var'): + assert name in aux_names + arg_dict['aux:%s'%name] = param._reduce() save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict) From da1862617d74a7e9376348f484a7e6988e76ef2c Mon Sep 17 00:00:00 2001 From: chinakook Date: Mon, 6 Apr 2020 15:39:39 +0800 Subject: [PATCH 2/8] Update block.py let user can save their extra param. --- python/mxnet/gluon/block.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 61a6d4a6a968..b4fb5b8c168f 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1274,6 +1274,8 @@ def export(self, path, epoch=0, remove_amp_cast=True): if name.endswith('running_mean') or name.endswith('running_var'): assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() + else: + arg_dict['aux:%s'%name] = param._reduce() save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict) From 8e5e618cb3444a13768e0f0a29744ca37b3149f6 Mon Sep 17 00:00:00 2001 From: chinakook Date: Mon, 6 Apr 2020 17:19:29 +0800 Subject: [PATCH 3/8] add allow_extra add allow_extra to let user decide whether to save extra parameters or not. --- python/mxnet/gluon/block.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index b4fb5b8c168f..5711365902c9 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1234,7 +1234,7 @@ def infer_type(self, *args): """Infers data type of Parameters from inputs.""" self._infer_attrs('infer_type', 'dtype', *args) - def export(self, path, epoch=0, remove_amp_cast=True): + def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): """Export HybridBlock to json format that can be loaded by `gluon.SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. @@ -1248,7 +1248,11 @@ def export(self, path, epoch=0, remove_amp_cast=True): will be created, where xxxx is the 4 digits epoch number. epoch : int Epoch number of saved model. - + remove_amp_cast : bool, optional + Whether to remove the amp_cast and amp_multicast operators, before exporting the model. + allow_extra : bool, optional + Whether to save extra parameters whose name not in the result symbol. User can set + allow_extra to True to load these parameters with old mxnet.mod.Module.set_params api. Returns ------- symbol_filename : str @@ -1275,7 +1279,8 @@ def export(self, path, epoch=0, remove_amp_cast=True): assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() else: - arg_dict['aux:%s'%name] = param._reduce() + if allow_extra: + arg_dict['aux:%s'%name] = param._reduce() save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict) From b75bedf484ed8ff7657427f9a75ef7173f452fe1 Mon Sep 17 00:00:00 2001 From: chinakook Date: Fri, 22 May 2020 09:38:17 +0800 Subject: [PATCH 4/8] Update block.py add moving_mean and moving_var when export model with SymbolBlock --- python/mxnet/gluon/block.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 5711365902c9..a845cc9e3cc9 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1275,7 +1275,8 @@ def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): if name in arg_names: arg_dict['arg:%s'%name] = param._reduce() else: - if name.endswith('running_mean') or name.endswith('running_var'): + if name.endswith('running_mean') or name.endswith('running_var') \ + or name.endswith('moving_mean') or name.endswith('moving_var'): assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() else: From e702da9f3863ecbdfd7c0385d8d918e98042c784 Mon Sep 17 00:00:00 2001 From: chinakook Date: Sat, 23 May 2020 09:15:07 +0800 Subject: [PATCH 5/8] Update python/mxnet/gluon/block.py typo Co-authored-by: Sheng Zha --- python/mxnet/gluon/block.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index a845cc9e3cc9..2015ed5aa433 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1251,8 +1251,8 @@ def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): remove_amp_cast : bool, optional Whether to remove the amp_cast and amp_multicast operators, before exporting the model. allow_extra : bool, optional - Whether to save extra parameters whose name not in the result symbol. User can set - allow_extra to True to load these parameters with old mxnet.mod.Module.set_params api. + Whether to save extra parameters whose names are not in the result symbol. + User can set allow_extra to True to load these parameters with old mxnet.mod.Module.set_params API. Returns ------- symbol_filename : str From acb8418cf56b4fd714991866bcdcb709efcf14ca Mon Sep 17 00:00:00 2001 From: chinakook Date: Fri, 21 Aug 2020 10:16:53 +0800 Subject: [PATCH 6/8] Update block.py --- python/mxnet/gluon/block.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 2015ed5aa433..8e1ce5845c2b 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1234,7 +1234,7 @@ def infer_type(self, *args): """Infers data type of Parameters from inputs.""" self._infer_attrs('infer_type', 'dtype', *args) - def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): + def export(self, path, epoch=0, remove_amp_cast=True): """Export HybridBlock to json format that can be loaded by `gluon.SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. @@ -1250,9 +1250,6 @@ def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): Epoch number of saved model. remove_amp_cast : bool, optional Whether to remove the amp_cast and amp_multicast operators, before exporting the model. - allow_extra : bool, optional - Whether to save extra parameters whose names are not in the result symbol. - User can set allow_extra to True to load these parameters with old mxnet.mod.Module.set_params API. Returns ------- symbol_filename : str @@ -1280,8 +1277,9 @@ def export(self, path, epoch=0, remove_amp_cast=True, allow_extra=True): assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() else: - if allow_extra: - arg_dict['aux:%s'%name] = param._reduce() + warnings.warn('Parameter "{name}" is ignored in saving ' + 'as it is not found in the graph. ' + .format(name=name), stacklevel=3) save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict) From 7e8b2e706f536e7b2bc247deeb369dc8ea5645de Mon Sep 17 00:00:00 2001 From: chinakook Date: Tue, 1 Sep 2020 09:00:04 +0800 Subject: [PATCH 7/8] Update block.py --- python/mxnet/gluon/block.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index c0f8915c42a6..317ebda78dca 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1343,18 +1343,22 @@ def export(self, path, epoch=0, remove_amp_cast=True): arg_names = set(sym.list_arguments()) aux_names = set(sym.list_auxiliary_states()) arg_dict = {} - for name, param in self.collect_params().items(): - if name in arg_names: - arg_dict['arg:%s'%name] = param._reduce() - else: - if name.endswith('running_mean') or name.endswith('running_var') \ - or name.endswith('moving_mean') or name.endswith('moving_var'): - assert name in aux_names - arg_dict['aux:%s'%name] = param._reduce() + for is_arg, name, param in self._cached_op_args: + if not is_arg: + if name in arg_names: + arg_dict['arg:{}'.format(name)] = param._reduce() else: - warnings.warn('Parameter "{name}" is ignored in saving ' - 'as it is not found in the graph. ' - .format(name=name), stacklevel=3) + if name.endswith('running_mean') or name.endswith('running_var') \ + or name.endswith('moving_mean') or name.endswith('moving_var'): + if name not in aux_names: + warnings.warn('Parameter "{name}" is not found in ' + 'the graph. ' + .format(name=name), stacklevel=3) + else: + warnings.warn('Parameter "{name}" is not found in ' + 'the graph. ' + .format(name=name), stacklevel=3) + arg_dict['aux:%s'%name] = param._reduce() save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict) From 5df00db79551246fd72de4c3e2c304d37603fab5 Mon Sep 17 00:00:00 2001 From: chinakook Date: Tue, 1 Sep 2020 12:00:35 +0800 Subject: [PATCH 8/8] Update python/mxnet/gluon/block.py Co-authored-by: Leonard Lausen --- python/mxnet/gluon/block.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 317ebda78dca..0a1c758d50ad 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -1348,17 +1348,11 @@ def export(self, path, epoch=0, remove_amp_cast=True): if name in arg_names: arg_dict['arg:{}'.format(name)] = param._reduce() else: - if name.endswith('running_mean') or name.endswith('running_var') \ - or name.endswith('moving_mean') or name.endswith('moving_var'): - if name not in aux_names: - warnings.warn('Parameter "{name}" is not found in ' - 'the graph. ' - .format(name=name), stacklevel=3) - else: - warnings.warn('Parameter "{name}" is not found in ' - 'the graph. ' + if name not in aux_names: + warnings.warn('Parameter "{name}" is not found in the graph. ' .format(name=name), stacklevel=3) - arg_dict['aux:%s'%name] = param._reduce() + else: + arg_dict['aux:%s'%name] = param._reduce() save_fn = _mx_npx.save if is_np_array() else ndarray.save params_filename = '%s-%04d.params'%(path, epoch) save_fn(params_filename, arg_dict)