├── .gitignore ├── test.py ├── README.md ├── CMakeLists.txt ├── LICENSE └── main.cu /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import torch 2 | torch.ops.load_library("build/libpytorch_cmake_example.so") 3 | 4 | shape = (3,3,3) 5 | a = torch.randint(0, 10, shape, dtype=torch.float).cuda() 6 | a_plus_one = torch.ops.pytorch_cmake_example.add_one(a) 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Demonstration of using PyTorch with cmake 2 | ========================================= 3 | 4 | To compile: 5 | 6 | ```bash 7 | cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'` -GNinja .. 8 | ``` 9 | 10 | To run: 11 | 12 | ```bash 13 | ./test.py 14 | ``` -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.9) 2 | 3 | project(pytorch_cmake_example LANGUAGES CXX CUDA) 4 | 5 | find_package(Python REQUIRED COMPONENTS Development) 6 | find_package(Torch REQUIRED) 7 | 8 | if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) 9 | set(CMAKE_CUDA_ARCHITECTURES 61) 10 | endif() 11 | 12 | add_library(pytorch_cmake_example SHARED 13 | main.cu 14 | ) 15 | target_compile_features(pytorch_cmake_example PRIVATE cxx_std_11) 16 | target_link_libraries(pytorch_cmake_example PRIVATE ${TORCH_LIBRARIES} Python::Python) 17 | 18 | # Use if the default GCC version gives issues 19 | target_compile_options(pytorch_cmake_example PRIVATE $<$:-ccbin g++-9>) 20 | 21 | # Use a variant of this if you're on an earlier cmake than 3.18 22 | # target_compile_options(pytorch_cmake_example PRIVATE $<$:-gencode arch=compute_61,code=sm_61>) 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2021 Richard Barnes 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /main.cu: -------------------------------------------------------------------------------- 1 | // #include 2 | // #include 3 | // #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace at; 9 | 10 | 11 | int64_t integer_round(int64_t num, int64_t denom){ 12 | return (num + denom - 1) / denom; 13 | } 14 | 15 | 16 | template 17 | __global__ void add_one_kernel(const T *const input, T *const output, const int64_t N){ 18 | // Grid-strided loop 19 | for(int i=blockDim.x*blockIdx.x+threadIdx.x;i>>( 33 | input.data_ptr(), 34 | output.data_ptr(), 35 | input.numel() 36 | ); 37 | C10_CUDA_KERNEL_LAUNCH_CHECK(); 38 | } 39 | ); 40 | 41 | return output; 42 | } 43 | 44 | 45 | TORCH_LIBRARY(pytorch_cmake_example, m) { 46 | m.def("add_one(Tensor input) -> Tensor"); 47 | m.impl("add_one", c10::DispatchKey::CUDA, TORCH_FN(add_one)); 48 | } 49 | --------------------------------------------------------------------------------