Created
February 17, 2022 10:35
-
-
Save duskvirkus/710a26fc69ea1c3d6b700cd420dfbb8d to your computer and use it in GitHub Desktop.
minimal c++ pytorch
This file contains 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
#!/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 |
This file contains 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
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) |
This file contains 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 <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