A simple example of how to use PyTorch ATen directly using C++.
Last active
July 26, 2020 00:19
-
-
Save adityaiitb/90cc5ed2604e85e38681fb01af65b4dc to your computer and use it in GitHub Desktop.
How to use PyTorch ATen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ATen/ATen.h> | |
| #include <iostream> | |
| int main() { | |
| at::Tensor a = at::rand({512,512}).cuda(); | |
| at::Tensor b = at::rand({512,512}).cuda(); | |
| int c = 0; | |
| for (int i = 0; i < 100; i++) { | |
| //b = at::relu(a); | |
| b += a; | |
| c++; | |
| } | |
| //std::cout << a << std::endl; | |
| //std::cout << b << std::endl; | |
| std::cout << c << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .PHONY: all clean | |
| CFLAGS = -std=c++11 | |
| LFLGAS = -Wl,--no-as-needed | |
| INCLUDE = -I /opt/pytorch/pytorch \ | |
| -I /opt/pytorch/pytorch/torch/csrc/api/include \ | |
| -I /opt/pytorch/pytorch/aten/src \ | |
| -I /opt/conda/lib/python3.6/site-packages/torch/include \ | |
| -I /opt/conda/lib/python3.6/site-packages/torch/include/torch/csrc/api/include | |
| LIB_PATH = -L /opt/conda/lib/python3.6/site-packages/torch/lib/ -L /usr/local/cuda/lib64/ | |
| LIBS = -ltorch -lc10 -lc10_cuda -lcaffe2 -lnvrtc | |
| #LIBS1 = -lOpenCL -laccinj64 -lcublas -lcudart -lcufft -lcufftw \ | |
| # -lcuinj64 -lcurand -lcusolver -lcusparse -lnppc -lnppial -lnppicc \ | |
| # -lnppicom -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc \ | |
| # -lnpps -lnvToolsExt -lnvblas -lnvgraph -lnvjpeg -lnvrtc-builtins -lnvrtc -lcuda | |
| all: a.out | |
| a.out: example.cc | |
| g++ $(CFLAGS) $(INCLUDE) $^ -o $@ $(LIB_PATH) $(LFLGAS) $(LIBS) $(LIBS1) | |
| clean: | |
| \rm a.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment