mindspore.obfuscate_ckpt
- mindspore.obfuscate_ckpt(network, ckpt_files, target_modules=None, obf_config=None, saved_path='./', obfuscate_scale=100)[source]
Obfuscate the plaintext checkpoint files according to the obfuscation config.
- Parameters
network (nn.Cell) – The original network that need to be obfuscated.
ckpt_files (str) – The directory path of original ckpt files.
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, the 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_config (dict) – The configuration of model obfuscation polices. Default:
None
.saved_path (str) – The directory path for saving obfuscated ckpt files. Default:
'./'
.obfuscate_scale (Union[float, int]) – Obfuscate scale of weights. The generated random obf_ratios will be in range of (1 / obfuscate_scale, obfuscate_scale). Default: 100.
- Returns
dict[str], obf_metadata, which is the necessary data that needs to be load when running obfuscated network.
- Raises
TypeError – If network is not nn.Cell.
TypeError – If ckpt_files is not string or saved_path is not string.
TypeError – If target_modules is not list.
TypeError – If target_modules's elements are not string.
TypeError – If obf_config is not dict.
ValueError – If ckpt_files is not exist or saved_path is not exist.
ValueError – If the number of elements of target_modules is less than
2
.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'.
Examples
>>> from mindspore import obfuscate_ckpt, save_checkpoint >>> # 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(net, './', target_modules=target_modules, saved_path='./')