Skip to content

Commit

Permalink
Linux build + C++17 compatibility option
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanUnderhill committed Oct 17, 2023
1 parent 4305ffa commit 2e60046
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Files to tell git to ignore

/src/.vs
/src/ort
/src/build
/ort
/build
/src/models/files/*.onnx
38 changes: 23 additions & 15 deletions src/CMakeLists.txt → CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ FetchContent_MakeAvailable(safeint)
endif()

project (Generators LANGUAGES CXX)
set(GENERATORS_ROOT ${PROJECT_SOURCE_DIR})
set(MODELS_ROOT ${PROJECT_SOURCE_DIR}/models)
set(TESTS_ROOT ${PROJECT_SOURCE_DIR}/tests)
set(PYBIND_ROOT ${PROJECT_SOURCE_DIR}/pybind)

set(CMAKE_CXX_STANDARD 20)
# add_compile_definitions(_DUSE_CXX17=1)

set(GENERATORS_ROOT ${PROJECT_SOURCE_DIR}/src)
set(MODELS_ROOT ${PROJECT_SOURCE_DIR}/src/models)
set(TESTS_ROOT ${PROJECT_SOURCE_DIR}/src/tests)
set(PYBIND_ROOT ${PROJECT_SOURCE_DIR}/src/pybind)

set(USE_CUDA 1)

add_subdirectory(${PROJECT_SOURCE_DIR}/../extern/pybind11 ${PROJECT_SOURCE_DIR}/build/pybind)
add_subdirectory(${PROJECT_SOURCE_DIR}/extern/pybind11 ${PROJECT_SOURCE_DIR}/build/pybind)

# CUDA Being enabled will make it not a debug build without this option, so all of the C++ headers will complain
# about a mismatch with the actual debug headers and it'll fail to link. I don't know why this happens, or if this is the best fix.
Expand Down Expand Up @@ -95,15 +99,26 @@ target_include_directories(Models PRIVATE ${CMAKE_SOURCE_DIR}/ort)
add_library (Generators ${generator_srcs})
target_include_directories(Generators PRIVATE ${CMAKE_SOURCE_DIR}/ort)

if(WIN32)
set(ONNXRUNTIME_LIB "onnxruntime.lib")
set(ONNXRUNTIME_FILES "onnxruntime*.dll")
elseif(APPLE)
set(ONNXRUNTIME_LIB "libonnxruntime.dylib")
# TODO: Figure ONNXRUNTIME_FILES for MacOS
else()
set(ONNXRUNTIME_LIB "libonnxruntime.so")
set(ONNXRUNTIME_FILES "libonnxruntime*.so*")
endif()

add_executable (Tests ${test_srcs})
target_include_directories(Tests PRIVATE ${CMAKE_SOURCE_DIR}/ort)
target_link_directories(Tests PRIVATE ${CMAKE_SOURCE_DIR}/ort)
target_link_libraries(Tests PRIVATE Generators Models "onnxruntime.lib")
target_link_libraries(Tests PRIVATE Generators Models ${ONNXRUNTIME_LIB})

pybind11_add_module(ort_generators ${pybind_srcs})
target_include_directories(ort_generators PRIVATE ${CMAKE_SOURCE_DIR}/ort)
target_link_directories(ort_generators PRIVATE ${CMAKE_SOURCE_DIR}/ort)
target_link_libraries(ort_generators PRIVATE Generators Models "onnxruntime.lib")
target_link_libraries(ort_generators PRIVATE Generators Models ${ONNXRUNTIME_LIB})

# Microsoft.GSL::GSL
# target_include_directories(Generators PRIVATE ${safeint_SOURCE_DIR})
Expand All @@ -117,7 +132,7 @@ if(MSVC)
endif()

# Copy the onnxruntime binaries into the build folder so it's found on launch
file(GLOB onnxruntime_libs "${CMAKE_SOURCE_DIR}/ort/onnxruntime*.dll")
file(GLOB onnxruntime_libs "${CMAKE_SOURCE_DIR}/ort/${ONNXRUNTIME_FILES}")
foreach(DLL_FILE ${onnxruntime_libs})
add_custom_command(
TARGET Generators POST_BUILD
Expand All @@ -127,13 +142,6 @@ foreach(DLL_FILE ${onnxruntime_libs})
)
endforeach()

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET Generators PROPERTY CXX_STANDARD 20)
set_property(TARGET Models PROPERTY CXX_STANDARD 20)
set_property(TARGET Tests PROPERTY CXX_STANDARD 20)
set_property(TARGET ort_generators PROPERTY CXX_STANDARD 20)
endif()

# Have visual studio put all files into one single 'Sources' folder vs the default split of header files into a separate folder
source_group("Sources" FILES ${generator_srcs})
source_group("Sources" FILES ${model_srcs})
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

git submodule update --init --recursive

cuda_version="cuda"
cuda_arch=70

export CUDA_HOME="/usr/local/${cuda_version}"
export CUDNN_HOME="/usr/local/${cuda_version}"
cuda_compiler="/usr/local/${cuda_version}/bin/nvcc"

cd src
cmake -S . -B build -DCMAKE_CUDA_ARCHITECTURES=$cuda_arch -DCMAKE_CUDA_COMPILER=$cuda_compiler -DCMAKE_POSITION_INDEPENDENT_CODE=ON
cd build
make
File renamed without changes.
6 changes: 6 additions & 0 deletions install_ort.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

mkdir -p src/ort

cp ~/onnxruntime/include/onnxruntime/core/session/onnxruntime_c*.h src/ort/
cp ~/onnxruntime/build/Linux/Release/libonnxruntime*.so* src/ort/
3 changes: 2 additions & 1 deletion src/beam_search_scorer_cuda.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cuda_runtime.h>
#include <assert.h>
#include <span>
#include <algorithm>
#include "span.h"
#include "beam_search_scorer_cuda.cuh"

namespace Generators
Expand Down
4 changes: 3 additions & 1 deletion src/generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <functional>
#include <span>
#include "span.h"
#include <memory>
#include <numeric>
#include <queue>
Expand Down
10 changes: 5 additions & 5 deletions src/models/gpt_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ void Gpt::Run(std::span<const int32_t> next_tokens, std::span<const int32_t> nex
UpdateInputs(next_tokens, next_indices, current_length);

#if 0
printf("**Inputs:\r\n");
DumpTensors(inputs_.data(), input_names_.data(), input_names_.size(), true);
printf("**Outputs:\r\n");
DumpTensors(outputs_.data(), output_names_.data(), output_names_.size(), false);
printf("**Inputs:\r\n");
DumpTensors(inputs_.data(), input_names_.data(), input_names_.size(), true);
printf("**Outputs:\r\n");
DumpTensors(outputs_.data(), output_names_.data(), output_names_.size(), false);
#endif

try {
try {
session_decode_->Run(nullptr, input_names_.data(), inputs_.data(), input_names_.size(), output_names_.data(), outputs_.data(), output_names_.size());
// session_decode_->Run(nullptr, *io_binding_decode_);
// logits_ = std::move(io_binding_decode_->GetOutputValues()[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/models/onnxruntime_cxx_inline_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct StandardAllocator : OrtAllocator
{
version = ORT_API_VERSION;
OrtAllocator::Alloc = [](OrtAllocator* this_, size_t size) -> void* { return new std::byte[size]; };
OrtAllocator::Free = [](OrtAllocator* this_, void *p) { delete p; };
OrtAllocator::Free = [](OrtAllocator* this_, void *p) { delete reinterpret_cast<std::byte*>(p); };
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/pybind/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ORTCHAR_String {
std::wstring string_;
};
#else
using ORTCHAR_String = std::string;
#define ORTchar_String(string) string
#endif

struct float16 {
Expand Down
2 changes: 1 addition & 1 deletion src/pybind/test_greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from transformers import GPT2Tokenizer

text = "best hotel in bay area"
text = "The best hotel in bay area"

# Generate input tokens from the text prompt
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
Expand Down
Binary file not shown.
Binary file removed src/python/__pycache__/float16.cpython-310.pyc
Binary file not shown.
Binary file removed src/python/__pycache__/fusion_utils.cpython-310.pyc
Binary file not shown.
Binary file removed src/python/__pycache__/onnx_model.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file removed src/python/__pycache__/test_gpt.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/search_cuda.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cuda_runtime.h>
#include <algorithm>
#include <span>
#include "span.h"

using ScoreType = float; // TODO: Move to header includable by cuda

Expand Down
2 changes: 1 addition & 1 deletion src/sequences_cuda.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cuda_runtime.h>
#include <assert.h>
#include <span>
#include "span.h"

namespace Generators {
namespace cuda {
Expand Down
2 changes: 1 addition & 1 deletion src/smartptrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Generators {
template <typename T>
void copy(std::span<const T> source, std::span<T> dest) {
assert(source.size() == dest.size());
copy(source.begin(), source.end(), dest.begin());
std::copy(source.begin(), source.end(), dest.begin());
}

template <typename T>
Expand Down
41 changes: 41 additions & 0 deletions src/span.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include <vector>

#ifndef USE_CXX17
#include <span>
#else
namespace std {

template <typename T>
struct span {
span() = default;
span(T* p, size_t length) : p_{p}, length_{length} {}

span(const span<std::remove_const_t<T>>& s) : p_{const_cast<T*>(s.data())}, length_{s.size()} {}
span(std::vector<std::remove_const_t<T>>& s) : p_{const_cast<T*>(s.data())}, length_{s.size()} {}

bool empty() const { return length_ == 0; }

size_t size() const { return length_; }
size_t size_bytes() const { return length_ * sizeof(T); }

T* data() { return p_; }
const T* data() const { return p_; }

T& operator[](size_t index) { return p_[index]; }

T& back() { return p_[length_ - 1]; }
T back() const { return p_[length_ - 1]; }

T* begin() { return p_; }
T* end() { return p_ + length_; }

span subspan(size_t index, size_t length) { return span(p_ + index, length); }
span<const T> subspan(size_t index, size_t length) const { return span(p_ + index, length); }

private:
T* p_{};
size_t length_{};
};
} // namespace std
#endif
4 changes: 2 additions & 2 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#endif
#include <iostream>

// Our working directory is generators/src/build so one up puts us in the src directory:
#define MODEL_PATH "../models/files/"
// Our working directory is generators/build so one up puts us in the src directory:
#define MODEL_PATH "../src/models/files/"

#define ASSERT_EQ(a, b) assert((a) == (b))
#define ASSERT_TRUE(a) assert(a)
Expand Down

0 comments on commit 2e60046

Please sign in to comment.