---
title: restore_prune_retrain_model
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/devaids/amct/atlasamct_16_0109.html
sourcePath: /source/zh/canncommercial/900/devaids/amct/atlasamct_16_0109.html
indexId: ee12e85e47f5d1c9423874552c5b4c2871c94ad770567c7e72f8e4677a17a25b65
---
# restore_prune_retrain_model

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | 通道稀疏：√ 4选2结构化稀疏接口：x |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | 通道稀疏：√ 4选2结构化稀疏接口：√ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | 通道稀疏：√ 4选2结构化稀疏接口：√ |
| Atlas 200I/500 A2 推理产品 | 通道稀疏：√ 4选2结构化稀疏接口：√ |
| Atlas 推理系列产品 | 通道稀疏：√ 4选2结构化稀疏接口：x |
| Atlas 训练系列产品 | 通道稀疏：√ 4选2结构化稀疏接口：x |


注：上述4选2结构化稀疏特性，标记“x”的产品，调用接口不会报错，但是获取不到性能收益。


#### 功能说明

通道稀疏或4选2结构化稀疏接口，两种稀疏特性每次只能使能一个：将输入的待稀疏的图结构按照给定的record_file中稀疏记录进行稀疏，返回修改后可用于稀疏后训练的torch.nn.Module模型。


#### 函数原型

```
prune_retrain_model = restore_prune_retrain_model (model, input_data, record_file, config_defination, pth_file, state_dict_name=None)
```


#### 参数说明

| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| model | 输入 | 含义：待进行稀疏的模型，未加载权重。 数据类型：torch.nn.Module |
| input\_data | 输入 | 含义：模型的输入数据。一个torch.tensor会被等价为tuple(torch.tensor)。 数据类型：tuple |
| record\_file | 输入 | 含义：记录稀疏信息的文件路径及名称，由create\_prune\_retrain\_model生成，确保两个接口生成的模型一致。 数据类型：string |
| config\_defination | 输入 | 含义：简易配置文件。基于retrain\_config\_pytorch.proto文件生成的简易配置文件prune.cfg，\*.proto文件所在路径为：AMCT安装目录/amct\_pytorch/proto/。\*.proto文件参数解释以及生成的prune.cfg简易量化配置文件样例请参见量化感知训练简易配置文件。 数据类型：string |
| pth\_file | 输入 | 含义：训练过程中保存的权重文件。 数据类型：string |
| state\_dict\_name | 输入 | 含义：权重文件中的权重对应的键值。 默认值：None 数据类型：string |


#### 返回值说明

返回修改后可用于稀疏后训练的torch.nn.Module模型。


#### 调用示例

```
import amct_pytorch as amct
# 建立待进行稀疏的网络图结构
config_defination = './prune_cfg.cfg'
model = build_model()
input_data = tuple([torch.randn(input_shape)])
save_pth_path = /your/path/to/save/tmp.pth
model.load_state_dict(torch.load(state_dict_path))
 
# 调用稀疏模型API
record_file = os.path.join(TMP, 'scale_offset_record.txt')
prune_retrain_model = amct.restore_prune_retrain_model(           
               model,
               input_data,
               record_file,
               config_defination,
               save_pth_path,
              'state_dict')
```
