Skip to content

Instantly share code, notes, and snippets.

@duskvirkus
Created February 17, 2022 10:35
Show Gist options
  • Save duskvirkus/710a26fc69ea1c3d6b700cd420dfbb8d to your computer and use it in GitHub Desktop.
Save duskvirkus/710a26fc69ea1c3d6b700cd420dfbb8d to your computer and use it in GitHub Desktop.
minimal c++ pytorch
#!/bin/bash
# dir check
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ $PWD != $SCRIPT_DIR ]
then
echo "Changing into script directory."
cd $SCRIPT_DIR
fi
echo "Building from ${PWD}"
if [ $1 ] && [ $1 == "clean" ]
then
echo "Cleaning old build."
if [ -d "build" ]
then
rm -rf build
fi
fi
PYTORCH_DIR="${SCRIPT_DIR}/pytorch"
echo "PYTORCH_DIR=${PYTORCH_DIR}"
if [ ! -d "build" ]
then
mkdir build
fi
cd build
echo "Building from ${PWD}"
cmake -DCMAKE_PREFIX_PATH="${PYTORCH_DIR}" ..
make -j6
cmake_minimum_required(VERSION 3.0)
project(cxxtorch)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(cxxtorch main.cxx)
target_link_libraries(cxxtorch "${TORCH_LIBRARIES}")
set_property(TARGET cxxtorch PROPERTY CXX_STANDARD 14)
#include <torch/torch.h>
#include <iostream>
int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment