mindspore.load_obf_params_into_net
- mindspore.load_obf_params_into_net(network, target_modules=None, obf_ratios=None, obf_config=None, data_parallel_num=1, **kwargs)[source]
Modify model structure according to obfuscation config and load obfuscated checkpoint into obfuscated network.
- Parameters
network (nn.Cell) – The original network that need to be obfuscated.
target_modules (list[str]) – The target ops that need to be obfuscated in the network. The first string represents the network path of the target ops in the original network, which should be in form of
"A/B/C"
. The second string represents the names of multiple target ops in the same path, which should be in form of"D|E|F"
. For example, thr target_modules of GPT2 can be['backbone /blocks/attention', 'dense1|dense2|dense3']
. If target_modules has the third value, it should be in the format of 'obfuscate_layers:all' or 'obfuscate_layers:int', which represents the number of layers need to be obfuscated of duplicate layers (such as transformer layers or resnet blocks). Default:None
.obf_ratios (Tensor) – The obf ratios generated when execute
mindspore.obfuscate_ckpt()
. Default:None
.obf_config (dict) – The configuration of model obfuscation polices. Default:
None
.data_parallel_num (int) – The data parallel number of parallel training. Default: 1.
kwargs (dict) –
Configuration options dictionary.
ignored_func_decorators (list[str]): The name list of function decorators in network's python code.
ignored_class_decorators (list[str]): The name list of class decorators in network's python code.
- Returns
nn.Cell, new_net, which is the obfuscated network.
- Raises
TypeError – If network is not nn.Cell.
TypeError – If obf_ratios is not Tensor.
TypeError – If target_modules is not list.
TypeError – If obf_config is not dict.
TypeError – If target_modules's elements are not string.
ValueError – If the number of elements of target_modules is less than
2
.ValueError – If obf_ratios is empty Tensor.
ValueError – If the first string of target_modules contains characters other than uppercase and lowercase letters, numbers,
'_'
and'/'
.ValueError – If the second string of target_modules is empty or contains characters other than uppercase and lowercase letters, numbers,
'_'
and'|'
.ValueError – If the third string of target_modules is not in the format of 'obfuscate_layers:all' or 'obfuscate_layers:int'.
TypeError – If ignored_func_decorators is not list[str] or ignored_class_decorators is not list[str].
Examples
>>> from mindspore import obfuscate_ckpt, save_checkpoint, load_checkpoint, Tensor >>> import mindspore.common.dtype as mstype >>> import numpy as np >>> # Refer to https://gitee.com/mindspore/docs/blob/master/docs/mindspore/code/lenet.py >>> net = LeNet5() >>> save_checkpoint(net, './test_net.ckpt') >>> target_modules = ['', 'fc1|fc2'] >>> # obfuscate ckpt files >>> obfuscate_ckpt(net, './', target_modules=target_modules, saved_path='./') >>> # load obf ckpt into network >>> new_net = LeNet5() >>> load_checkpoint('./test_net_obf.ckpt', new_net) >>> obf_net = load_obf_params_into_net(new_net, target_modules)