离线推理场景下可通过dynamo_export接口,导出TorchAir生成的离线图(air格式)。
导出的推理模型不再依赖PyTorch框架,可直接由CANN软件栈加载执行,减少了框架调度带来的性能损耗,方便在不同的部署环境上移植。
dynamo_export接口原型定义如下,详细的参数说明参见dynamo_export。
def dynamo_export(*args, model: torch.nn.Module, export_path: str = "export_file", export_name: str = "export", dynamic: bool = False, config=CompilerConfig(), **kwargs)
关键参数说明如下:
支持的功能 |
功能说明 |
配置说明 |
---|---|---|
auto_atc_config_generated |
前端切分场景下(PyTorch模型导出时包含集合通信逻辑),是否开启自动生成ATC的json配置文件样例模板。 |
|
enable_record_nn_module_stack |
导出的图是否携带nn_module_stack信息,方便后端切分(PyTorch模型导出时不含集合通信逻辑,而由GE添加集合通信逻辑)运用模板。 说明:
|
|
config的配置示例如下:
1 2 3 4 5 6 | import torch_npu, torchair config = torchair.CompilerConfig() # 开启自动生成ATC的json配置文件模板 config.export.experimental.auto_atc_config_generated = True # 携带nn_module_stack信息 config.export.experimental.enable_record_nn_module_stack = True |
导图功能支持在单卡和多卡场景下导图,且支持导出的计算图携带allreduce等通信类算子,导图结果参见产物说明,dynamo_export接口的调用示例参见使用示例。
dynamo_export导图结果文件名缺省为"export_file",支持用户自定义,产物目录如下:
└── export_file // 导出的文件夹,可自定义 ├── dynamo.pbtxt // 导出的模型信息(可读格式) ├── export.air // 导出的模型文件(不可读),文件名缺省值为“export” ├── weight_xx // 导出的权重文件 ├── ...... ├── model_relation_config.json // 使能auto_atc_config_generated生成的文件 └── numa_config.json // 使能auto_atc_config_generated生成的文件
对于导出的权重文件,其可能直接存入export.air,也可能单独存在weight_xx文件中。
对于export.air文件,ATC工具支持转换为可执行的*.om模型文件(CANN能加载运行的格式),用户按需使用。命令形如:
1 | atc --model=/export_file/export.air --framework=1 --output=/export_file/offline_module --soc_version=<soc_version> |
详细工具操作参见《CANN ATC离线模型编译工具用户指南》。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import torch, torch_npu, torchair class Model(torch.nn.Module): def __init__(self): super().__init__() self.linear1 = torch.nn.Linear(2, 2) self.linear2 = torch.nn.Linear(2, 2) for param in self.parameters(): torch.nn.init.ones_(param) def forward(self, x, y): return self.linear1(x) + self.linear2(y) model = Model() x = torch.randn(2, 2) y = torch.randn(2, 2) torchair.dynamo_export(x, y, model=model, dynamic=False) |
执行如下命令查看导出结果:
[root@localhost example_export]# tree
├── example1.py
└── export_file // 指定导出的文件夹,当文件夹不存在时会自动创建
├── dynamo.pbtxt // 导出可读的图信息,用于debug
├── export.air // 导出的模型文件,ATC编译时的输入。其中通过fileconst节点记录了权重所在的路径与文件名
1 directory, 3 files
由于权重文件小于2G,因此权重并未外置而是转为Const节点记录在图中。可以打开dynamo.pbtxt文件查看图接口信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import torch, os, torch_npu, torchair from torchair import CompilerConfig class AllReduceSingeGroup(torch.nn.Module): def __init__(self): super().__init__() self.p1 = torch.nn.Parameter(torch.tensor([[1.1, 1.1], [1.1, 1.1]])) self.p2 = torch.nn.Parameter(torch.tensor([[2.2, 2.2], [3.3, 3.3]])) def forward(self, x, y): x = x + y + self.p1 + self.p2 torch.distributed.all_reduce(x) return x def example(rank, world_size): torch.distributed.init_process_group("gloo", rank=rank, world_size=world_size) x = torch.ones([2, 2], dtype=torch.int32) y = torch.ones([2, 2], dtype=torch.int32) mod = AllReduceSingeGroup() config = CompilerConfig() config.export.experimental.auto_atc_config_generated = True config.export.experimental.enable_record_nn_module_stack = True torchair.dynamo_export(x, y, model=mod, dynamic=True, export_path="./mp", export_name="mp_rank", config=config) def mp(): world_size = 2 torch.multiprocessing.spawn(example, args=(world_size, ), nprocs=world_size, join=True) if __name__ == '__main__': os.environ["MASTER_ADDR"] = "localhost" os.environ["MASTER_PORT"] = "29505" mp() |
执行如下命令查看导出结果:
[root@localhost example_export]# tree ├── example1.py ├── example2.py └── mp ├── model_relation_config.json ├── mp_rank0.air // 第一张卡导出的模型文件 ├── mp_rank1.air // 第二张卡导出的模型文件 ├── numa_config.json ├── rank_0 // 第一张卡子目录 │ ├── dynamo.pbtxt // 导出可读的图信息,用于debug │ ├── p1 // 导出的权重文件 │ └── p2 // 导出的权重文件 └── rank_1 ├── dynamo.pbtxt ├── p1 └── p2 3 directories, 12 files
json中相关字段需要用户根据自己的需求修改,字段参数含义请参考《CANN ATC离线模型编译工具用户指南》中的“--model_relation_config”章节与《CANN ATC离线模型编译工具用户指南》中的“--cluster_config”章节。针对多卡场景,item节点被生成为node_id为0的表中,需要用户根据自己的需求手动划分至不同的node下。