GithubHelp home page GithubHelp logo

transformer-cpp's Introduction

transformer-cpp

a simple example for torch transformer english to french translate.
implement inference function in linux c++ environment using libtorch and tensorrt.

env

ubuntu18.04
python == 3.9.12
torch == 1.11.0
cuda == 11.3
cudnn == cudnn-11.3-linux-x64-v8.2.1.32
libtorch == libtorch-cxx11-abi-shared-with-deps-1.11.0+cu113 or libtorch-cxx11-abi-shared-with-deps-1.11.0+cpu (change device=cpu in config/config.ini)
tensorrt == TensorRT-8.4.1.5.Linux.x86_64-gnu.cuda-11.6.cudnn8.4

run

cd path_to_your_workspace
change configs in config/config.ini if you want

train

python transformer/trainer.py will save the final model file in saved_models/

inference

  1. run python transformer/save_model_trace.py, you will get a trace model file in saved_models/
  2. compile c++ program
    • you should firstly change the path to your libtorch path and tensorrt path in CMakeLists.txt,
    • mkdir build && cd build && cmake .. && make, you get the bin file in path_to_your_workspace/bin
    • cd/path_to_your_workspace and run ./bin/infer_xxx,input english sentence, press enter to inference, press q to quit.

bugs

tensor.data may cause unexpected results in traced model.

pad_mask = seq_k.data.eq(self.pad_idx).unsqueeze(1).expand(batch_size, len_q, len_k)

this will cause inference diff between torch.save model and torch.jit.trace model.
reference: https://pytorch.org/docs/stable/onnx.html#avoid-tensor-data

onnx-tensorrt does not support torch.triu. (supported in tensorrt-8.5.x)

cp ${tensorrt_dir}/lib/libnvinfer_builder_resource.so.8.4.1 ${workspaceFolder}
add export LD_LIBRARY_PATH="${path_of_TensorRT}/lib:$LD_LIBRARY_PATH" to ~/.zshrc and source ~/.zshrc

transformer-cpp's People

Contributors

sunjiabin17 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.