Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PyTorch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
conda activate cpp-conda
mkdir build
cd build
cmake ..
cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'` ..
make

- name: Check Code Format
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ it _might_ work on other systems.
- [xtensor](https://xtensor.readthedocs.io/en/latest/)
- [nlohmann/json](https://github.com/nlohmann/json#serialization--deserialization)
- [protobuf](https://github.com/protocolbuffers/protobuf)
- [PyTorch](https://pytorch.org) -> For CUDA support, you will need to install
the [CUDA toolkit](https://developer.nvidia.com/cuda-toolkit) yourself
- Dev tools
- Formatting (Google style) with
[clang-format](https://clang.llvm.org/docs/ClangFormat.html)
Expand Down Expand Up @@ -77,7 +79,7 @@ conda activate cpp-conda
```bash
mkdir build
cd build
cmake ..
cmake -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'` ..
make
```

Expand Down
2 changes: 2 additions & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: cpp-conda
channels:
- conda-forge
- pytorch
- defaults
dependencies:
- python=3.8.*
Expand All @@ -17,5 +18,6 @@ dependencies:
- xsimd=7.*
- nlohmann_json=3.*
- protobuf=3.* # includes Protobuf compiler, C++ headers, Python libraries
- pytorch=1.7.*
# Development
- cmake_format
17 changes: 15 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ find_package(xtensor REQUIRED)
find_package(absl REQUIRED)
find_package(nlohmann_json REQUIRED)

#
# PyTorch
#

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${TORCH_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")

#
# Protobuf
#
Expand All @@ -23,8 +30,14 @@ message("PROTO_HDRS = ${PROTO_HDRS}")

add_executable(main main.cpp ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(
main PRIVATE xtensor xtensor::optimize xtensor::use_xsimd absl::flat_hash_map
private_library ${PROTOBUF_LIBRARIES})
main
PRIVATE xtensor
xtensor::optimize
xtensor::use_xsimd
absl::flat_hash_map
private_library
${PROTOBUF_LIBRARIES}
${TORCH_LIBRARIES})
target_include_directories(
main PRIVATE . ${PROTOBUF_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR} # In order to capture the *.pb.h
Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <torch/torch.h>
#include <xtensor/xarray.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xview.hpp>
Expand Down Expand Up @@ -50,5 +51,10 @@ int main() {
std::cout << "Protobuf\n";
projectname::AddressBook address_book;

std::cout << "Random torch tensor\n";
torch::Tensor tensor = torch::rand({2, 3}); // NOLINT
std::cout << tensor << std::endl;
std::cout << "cuda is available: " << torch::cuda::is_available() << '\n';

return 0;
}