Pytorch添加自定义算子之(10)-mmdeploy编译流程

整体参考
在这里插入图片描述

一、环境的安装

参考

conda create -n mmlab python=3.8
conda activate mmlab
conda install pytorch==2.0.0 torchvision==0.15.0 torchaudio==2.0.0 pytorch-cuda=11.8 -c pytorch 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
发布自定义的C++算子,需要完成以下步骤: 1. 编写C++代码:编写自定义算子的C++代码,包括前向计算和反向传播函数等。 2. 编写Python绑定代码:编写Python绑定代码,将C++算子封装为PyTorch模块,方便在Python中使用。 3. 编译C++代码:使用CMake或者Makefile等工具,将C++代码编译成共享库(.so或.dll文件)。 4. 使用PyTorch C++扩展API:使用PyTorch C++扩展API,将共享库加载到PyTorch中,并注册自定义算子。 5. 测试自定义算子:在Python中测试自定义算子是否能够正常工作。 这里提供一个简单的示例: 1. 编写C++代码 ```cpp #include <torch/extension.h> torch::Tensor my_add_forward(const torch::Tensor& input1, const torch::Tensor& input2) { return input1 + input2; } std::vector<torch::Tensor> my_add_backward(const torch::Tensor& grad_output) { return {grad_output, grad_output}; } PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("forward", &my_add_forward, "MyAdd forward"); m.def("backward", &my_add_backward, "MyAdd backward"); } ``` 2. 编写Python绑定代码 ```python import torch my_add = torch.utils.cpp_extension.load(name='my_add', sources=['my_add.cpp'], verbose=True) def my_add_op(input1, input2): return my_add.forward(input1, input2) class MyAddFunction(torch.autograd.Function): @staticmethod def forward(ctx, input1, input2): output = my_add_op(input1, input2) ctx.save_for_backward(input1, input2) return output @staticmethod def backward(ctx, grad_output): input1, input2 = ctx.saved_tensors grad_input = my_add.backward(grad_output) return grad_input[0], grad_input[1] my_add_function = MyAddFunction.apply ``` 3. 编译C++代码 使用以下命令编译C++代码: ```sh g++ -o my_add.so -shared -fPIC my_add.cpp $(python3 -m pybind11 --includes) -I/path/to/torch/include -I/path/to/torch/include/torch/csrc/api/include -L/path/to/torch/lib -ltorch -lc10 ``` 4. 使用PyTorch C++扩展API ```cpp #include <torch/script.h> #include <iostream> int main() { torch::jit::script::Module module = torch::jit::load("model.pt"); module.to(torch::kCPU); std::string code = R"( def forward(x, y): return my_add_function(x, y) )"; torch::jit::script::Module new_module = module.clone(); new_module.define(code); // Test the new module torch::Tensor x = torch::ones({2, 3}); torch::Tensor y = torch::ones({2, 3}); torch::Tensor output = new_module.forward({x, y}).toTensor(); std::cout << output << std::endl; return 0; } ``` 5. 测试自定义算子 在Python中测试自定义算子: ```python import torch my_add = torch.utils.cpp_extension.load(name='my_add', sources=['my_add.cpp'], verbose=True) def my_add_op(input1, input2): return my_add.forward(input1, input2) class MyAddFunction(torch.autograd.Function): @staticmethod def forward(ctx, input1, input2): output = my_add_op(input1, input2) ctx.save_for_backward(input1, input2) return output @staticmethod def backward(ctx, grad_output): input1, input2 = ctx.saved_tensors grad_input = my_add.backward(grad_output) return grad_input[0], grad_input[1] my_add_function = MyAddFunction.apply # Test the custom operator x = torch.ones(2, 3, requires_grad=True) y = torch.ones(2, 3, requires_grad=True) z = my_add_function(x, y) z.sum().backward() print(x.grad) # tensor([[1., 1., 1.],[1., 1., 1.]]) print(y.grad) # tensor([[1., 1., 1.],[1., 1., 1.]]) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

誓天断发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值