热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:他山之石在C++平台上部署PyTorch模型流程+踩坑实录

篇首语:本文由编程笔记#小编为大家整理,主要介绍了他山之石在C++平台上部署PyTorch模型流程+踩坑实录相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了他山之石在C++平台上部署PyTorch模型流程+踩坑实录相关的知识,希望对你有一定的参考价值。















最近因为工作需要,要把pytorch的模型部署到c++平台上,基本过程主要参照官网的教学示例,期间发现了不少坑,特此记录。








作者:火星少女




01




























模型转换



libtorch不依赖于python,python训练的模型,需要转换为script model才能由libtorch加载,并进行推理。在这一步官网提供了两种方法:


方法一:Tracing


这种方法操作比较简单,只需要给模型一组输入,走一遍推理网络,然后由torch.ji.trace记录一下路径上的信息并保存即可。示例如下:

























import torchimport torchvision
# An instance of your model.model = torchvision.models.resnet18()
# An example input you would normally provide to your model's forward() method.example = torch.rand(1, 3, 224, 224)
# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.traced_script_module = torch.jit.trace(model, example)





缺点是如果模型中存在控制流比如if-else语句,一组输入只能遍历一个分支,这种情况下就没办法完整的把模型信息记录下来。


方法二:Scripting


直接在Torch脚本中编写模型并相应地注释模型,通过torch.jit.script编译模块,将其转换为ScriptModule。示例如下:




























class MyModule(torch.nn.Module): def __init__(self, N, M): super(MyModule, self).__init__() self.weight = torch.nn.Parameter(torch.rand(N, M))
def forward(self, input): if input.sum() > 0: output = self.weight.mv(input) else: output = self.weight + input return output
my_module = MyModule(10,20)sm = torch.jit.script(my_module)







  • forward方法会被默认编译,forward中被调用的方法也会按照被调用的顺序被编译



  • 如果想要编译一个forward以外且未被forward调用的方法,可以添加 @torch.jit.export.



  • 如果想要方法不被编译,可使用@torch.jit.ignore[1] 或者 @torch.jit.unused[2]





































# Same behavior as pre-PyTorch 1.2@torch.jit.scriptdef some_fn(): return 2
# Marks a function as ignored, if nothing# ever calls it then this has no effect@torch.jit.ignoredef some_fn2(): return 2
# As with ignore, if nothing calls it then it has no effect.# If it is called in script it is replaced with an exception.@torch.jit.unuseddef some_fn3(): import pdb; pdb.set_trace() return 4
# Doesn't do anything, this function is already# the main entry point@torch.jit.exportdef some_fn4(): return 2





在这一步遇到好多坑,主要原因可归为一下两点


1. 不支持的操作


TorchScript支持的操作是python的子集,大部分torch中用到的操作都可以找到对应实现,但也存在一些尴尬的不支持操作,详细列表可见unsupported-ops[3],下面列一些我自己遇到的操作:


1)参数/返回值不支持可变个数,例如


















def __init__(self, **kwargs):

或者



















if output_flag == 0: return reshape_logitselse: loss = self.loss(reshape_logits, term_mask, labels_id) return reshape_logits, loss





2)各种iteration操作


eg1.


















layers = [int(a) for a in layers]

报错torch.jit.frontend.UnsupportedNodeError: ListComp aren’t supported



可以改成:
















for k in range(len(layers)): layers[k] = int(layers[k])





eg2.



















seq_iter = enumerate(scores)try: _, inivalues = seq_iter.__next__()except: _, inivalues = seq_iter.next()





eg3.


















line = next(infile)

3)不支持的语句



eg1. 不支持continue


torch.jit.frontend.UnsupportedNodeError: continue statements aren’t supported


eg2. 不支持try-catch


torch.jit.frontend.UnsupportedNodeError: try blocks aren’t supported


eg3. 不支持with语句


4)其他常见op/module


eg1. torch.autograd.Variable


解决:使用torch.ones/torch.randn等初始化+.float()/.long()等指定数据类型。


eg2. torch.Tensor/torch.LongTensor etc.


解决:同上


eg3. requires_grad参数只在torch.tensor中支持,torch.ones/torch.zeros等不可用


eg4. tensor.numpy()


eg5. tensor.bool()


解决:tensor.bool()用tensor>0代替


eg6. self.seg_emb(seg_fea_ids).to(embeds.device)


解决:需要转gpu的地方显示调用.cuda()


总之一句话:除了原生python和pytorch以外的库,比如numpy什么的能不用就不用,尽量用pytorch的各种API。


2.指定数据类型


1)属性,大部分的成员数据类型可以根据值来推断,空的列表/字典则需要预先指定































from typing import Dict
class MyModule(torch.nn.Module): my_dict: Dict[str, int]
def __init__(self): super(MyModule, self).__init__() # This type cannot be inferred and must be specified self.my_dict = {}
# The attribute type here is inferred to be `int` self.my_int = 20
def forward(self): pass
m = torch.jit.script(MyModule())





2)常量,使用Final关键字

































try: from typing_extensions import Finalexcept: # If you don't have `typing_extensions` installed, you can use a # polyfill from `torch.jit`. from torch.jit import Final
class MyModule(torch.nn.Module):
my_constant: Final[int]
def __init__(self): super(MyModule, self).__init__() self.my_cOnstant= 2
def forward(self): pass
m = torch.jit.script(MyModule())





3)变量。默认是tensor类型且不可变,所以非tensor类型必须要指明


















def forward(self, batch_size:int, seq_len:int, use_cuda:bool):

方法三:Tracing and Scriptin混合



一种是在trace模型中调用script,适合模型中只有一小部分需要用到控制流的情况,使用实例如下:





























import torch
@torch.jit.scriptdef foo(x, y): if x.max() > y.max(): r = x else: r = y return r

def bar(x, y, z): return foo(x, y) + z
traced_bar = torch.jit.trace(bar, (torch.rand(3), torch.rand(3), torch.rand(3)))





另一种情况是在script module中用tracing生成子模块,对于一些存在script module不支持的python feature的layer,就可以把相关layer封装起来,用trace记录相关layer流,其他layer不用修改。使用示例如下:





























import torchimport torchvision
class MyScriptModule(torch.nn.Module): def __init__(self): super(MyScriptModule, self).__init__() self.means = torch.nn.Parameter(torch.tensor([103.939, 116.779, 123.68]) .resize_(1, 3, 1, 1)) self.resnet = torch.jit.trace(torchvision.models.resnet18(), torch.rand(1, 3, 224, 224))
def forward(self, input): return self.resnet(input - self.means)
my_script_module = torch.jit.script(MyScriptModule())





02




























保存序列化模型



如果上一步的坑都踩完,那么模型保存就非常简单了,只需要调用save并传递一个文件名即可,需要注意的是如果想要在gpu上训练模型,在cpu上做inference,一定要在模型save之前转化,再就是记得调用model.eval(),形如






















gpu_model.eval()cpu_model = gpu_model.cpu()sample_input_cpu = sample_input_gpu.cpu()traced_cpu = torch.jit.trace(traced_cpu, sample_input_cpu)torch.jit.save(traced_cpu, "cpu.pth")
traced_gpu = torch.jit.trace(traced_gpu, sample_input_gpu)torch.jit.save(traced_gpu, "gpu.pth")





03




























C++ load训练好的模型



要在C ++中加载序列化的PyTorch模型,必须依赖于PyTorch C ++ API(也称为LibTorch)。libtorch的安装非常简单,只需要在pytorch官网下载对应版本,解压即可。会得到一个结构如下的文件夹。



















libtorch/ bin/ include/ lib/ share/





然后就可以构建应用程序了,一个简单的示例目录结构如下:

















example-app/ CMakeLists.txt example-app.cpp





example-app.cpp和CMakeLists.txt的示例代码分别如下:





































#include // One-stop header.#include <iostream>#include int main(int argc, const char* argv[]) { if (argc != 2) { std::cerr <<"usage: example-app \n"; return -1;  }
torch::jit::script::Module module; try { // Deserialize the ScriptModule from a file using torch::jit::load(). module = torch::jit::load(argv[1]); } catch (const c10::Error& e) { std::cerr <<"error loading the model\n"; return -1; }
std::cout <<"ok\n";}






















cmake_minimum_required(VERSION 3.0 FATAL_ERROR)project(custom_ops)
find_package(Torch REQUIRED)
add_executable(example-app example-app.cpp)target_link_libraries(example-app "${TORCH_LIBRARIES}")set_property(TARGET example-app PROPERTY CXX_STANDARD 14)





至此,就可以运行以下命令从example-app/文件夹中构建应用程序啦:


















mkdir buildcd buildcmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..cmake --build . --config Release





其中/path/to/libtorch是之前下载后的libtorch文件夹所在的路径。这一步如果顺利能够看到编译完成100%的提示,下一步运行编译生成的可执行文件,会看到“ok”的输出,可喜可贺!


04




























执行Script Module



终于到最后一步啦!下面只需要按照构建输入传给模型,执行forward就可以得到输出啦。一个简单的示例如下:





















// Create a vector of inputs.std::vector inputs;inputs.push_back(torch::ones({1, 3, 224, 224}));
// Execute the model and turn its output into a tensor.at::Tensor output = module.forward(inputs).toTensor();std::cout </*dim=*/1, /*start=*/0, /*end=*/5) <<'\n';





前两行创建一个torch::jit::IValue的向量,并添加单个输入. 使用torch::ones()创建输入张量,等效于C ++ API中的torch.ones。然后,运行script::Module的forward方法,通过调用toTensor()将返回的IValue值转换为张量。C++对torch的各种操作还是比较友好的,通过torch::或者后加_的方法都可以找到对应实现,例如
















torch::tensor(input_list[j]).to(at::kLong).resize_({batch, 128}).clone()//torch::tensor对应pytorch的torch.tensor; at::kLong对应torch.int64;resize_对应resize





最后check一下确保c++端的输出和pytorch是一致的就大功告成啦~


踩了无数坑,薅掉了无数头发,很多东西也是自己一点点摸索的,如果有错误欢迎指正!






参考资料:

[1] https://pytorch.org/docs/master/generated/torch.jit.ignore.html#torch.jit.ignore

[2] https://pytorch.org/docs/master/generated/torch.jit.unused.html#torch.jit.unused

[3] https://pytorch.org/docs/master/jit_unsupported.html#jit-unsupported

https://pytorch.org/cppdocs/

https://pytorch.org/tutorials/advanced/cpp_export.html
























直播预告











【他山之石】在C++平台上部署PyTorch模型流程+踩坑实录










左划查看更多






【他山之石】在C++平台上部署PyTorch模型流程+踩坑实录






【他山之石】在C++平台上部署PyTorch模型流程+踩坑实录


























历史文章推荐











































分享、点赞、在看,给个三连击呗!








推荐阅读
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了多因子选股模型在实际中的构建步骤,包括风险源分析、因子筛选和体系构建,并进行了模拟实证回测。在风险源分析中,从宏观、行业、公司和特殊因素四个角度分析了影响资产价格的因素。具体包括宏观经济运行和宏经济政策对证券市场的影响,以及行业类型、行业生命周期和行业政策对股票价格的影响。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
author-avatar
mobiledu2502883183
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有