I was trying to use the C++ library on an Intel Macbook, following this tutorial – Installing C++ Distributions of PyTorch — PyTorch main documentation
The only difference is that I use the libtorch
installed in Anaconda (pytorch version 2.2.2
).
cmake_minimum_required(VERSION 3.18)
project(vllm_csrc VERSION 0.1.0 LANGUAGES C CXX)
include(CTest)
enable_testing()
set(CMAKE_PREFIX_PATH "/home/anaconda3/envs/vllm/lib/python3.10/site-packages/torch/share/cmake")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(vllm_csrc main.cpp)
target_link_libraries(vllm_csrc "${TORCH_LIBRARIES}")
set_property(TARGET vllm_csrc PROPERTY CXX_STANDARD 17)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
and the main program is a simple hello world
#include <iostream>
#include <torch/torch.h>
int main(int, char**){
std::cout << "hello world" << std::endl;
torch::Tensor tensor = torch::rand({2, 3});
torch::Tensor other_tensor = torch::rand({2, 3});
torch::Tensor final_tensor = tensor + other_tensor;
if (tensor.defined()) {
std::cout << "Tensor size: " << tensor.sizes() << std::endl;
std::cout << tensor << std::endl;
}
}
The project builds successfully. When I run the executable, I see the size, but I get a segmentation fault while printing the tensor (which is the example in the link).
./vllm_csrc
hello world
Tensor size: [2, 3]
zsh: segmentation fault ./vllm_csrc