diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 7876f6e8..51295121 100644 --- a/.github/workflows/cmake-windows.yml +++ b/.github/workflows/cmake-windows.yml @@ -24,4 +24,4 @@ jobs: - name: Build run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - name: Test - run: ${{github.workspace}}\build\src\libllm\${{env.BUILD_TYPE}}\unittest.exe + run: ${{github.workspace}}\build\${{env.BUILD_TYPE}}\unittest.exe diff --git a/CMakeLists.txt b/CMakeLists.txt index b9a2c636..d74a7658 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,8 @@ option(WITH_OPENMP "Build with OpenMP." ON) option(WITH_CUTLASS "build MatMul operators with CUTLASS." OFF) option(MKL_PREFIX "Prefix for MKL headers and libraries." "/opt/intel/mkl") +find_package(OpenMP) + set(CMAKE_CUDA_RUNTIME_LIBRARY Static) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") @@ -81,4 +83,64 @@ if(HAVE_FP16) add_compile_definitions(LIBLLM_HAVE_FP16) endif() -add_subdirectory("src/libllm") +add_subdirectory("src") + +set(libllm_LIBADD lut ${CMAKE_DL_LIBS}) + +if(WITH_OPENMP) + set(libllm_LIBADD ${libllm_LIBADD} OpenMP::OpenMP_CXX) +endif() + +add_library(libllm SHARED $) +target_link_libraries(libllm ${libllm_LIBADD} ) +set_property(TARGET libllm PROPERTY OUTPUT_NAME llm) +if(UNIX AND NOT APPLE) + target_link_options(libllm PUBLIC "-Wl,--no-undefined") +endif() + +set(unittest_LIBADD + libllm_static + libllm_test + lut_test + ${libllm_LIBADD} + catch2) +if (WITH_MKL) + set(unittest_LIBADD + ${unittest_LIBADD} + mkl_intel_lp64 + mkl_intel_thread + mkl_core + iomp5) +endif() + +set(benchmark_LIBADD + libllm_static + ${libllm_LIBADD}) + +add_library(catch2 STATIC "../third_party/catch2/catch_amalgamated.cpp") +add_executable(unittest "src/libllm/test_main.cc") +target_include_directories(unittest PRIVATE "src") + +target_link_libraries(unittest ${unittest_LIBADD}) + +add_executable(benchmark "src/libllm/benchmark_main.cc") +target_include_directories(benchmark PRIVATE "src") +target_link_libraries(benchmark ${benchmark_LIBADD}) + +if (WITH_CUDA) + add_library(llmplugincublas SHARED $) + target_include_directories(llmplugincublas PRIVATE ${libllm_INCDIR}) + target_link_libraries(llmplugincublas lut CUDA::cublas) + if(UNIX) + target_link_options(llmplugincublas PUBLIC "-Wl,--no-undefined") + endif(UNIX) +endif() + +enable_testing() +add_test(NAME unittest COMMAND $) + +add_custom_target(llm + ALL + DEPENDS libllm + COMMAND go build -o $>/llm${CMAKE_EXECUTABLE_SUFFIX} ${CMAKE_SOURCE_DIR}/go/bin + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/go/bin) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..ad66360c --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory("lut") +add_subdirectory("libllm") diff --git a/src/libllm/CMakeLists.txt b/src/libllm/CMakeLists.txt index fa8a53db..2f9ad3e4 100644 --- a/src/libllm/CMakeLists.txt +++ b/src/libllm/CMakeLists.txt @@ -1,26 +1,8 @@ -find_package(OpenMP) if (OPENMP_FOUND) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") endif() -set(lut_SOURCES - "lut/internal/log.cc" - "lut/internal/sprintf.cc" - "lut/base64.cc" - "lut/error.cc" - "lut/flags.cc" - "lut/half.cc" - "lut/ini_config.cc" - "lut/is_debug.cc" - "lut/path.cc" - "lut/random.cc" - "lut/reader.cc" - "lut/strings.cc" - "lut/time.cc" - "lut/thread_pool.cc" - "lut/zip_file.cc") - set(libllm_SOURCES "cpu/kernel/fallback.cc" "cpu/kernel/interface.cc" @@ -72,29 +54,19 @@ set(libllm_SOURCES "whisper.cc" "../../third_party/ruapu/ruapu.cc") -set(unittest_SOURCES +set(libllm_test_SOURCES "cpu/kernel/benchmark.cc" "cpu/kernel/interface_test.cc" "cpu/log_mel_spectrogram_test.cc" "cpu/test.cc" - "lut/path_test.cc" - "lut/strings_test.cc" "llama_test.cc" "module_test.cc" "operator_tester.cc" "tensor_test.cc" - "test_helper.cc" - "test_main.cc") - -set(llm_SOURCES - "dialog_manager.cc" - "llm_main.cc") + "test_helper.cc") -set(benchmark_SOURCES "benchmark_main.cc") - set(libllm_INCDIR ".." "../../third_party") -set(libllm_LIBADD lut ${CMAKE_DL_LIBS}) if(WITH_OPENMP) if(NOT OPENMP_FOUND) @@ -102,7 +74,6 @@ if(WITH_OPENMP) endif() set(libllm_SOURCES ${libllm_SOURCES} "mp_openmp.cc") set(libllm_INCDIR ${libllm_INCDIR} ${OpenMP_CXX_INCLUDE_DIRS}) - set(libllm_LIBADD ${libllm_LIBADD} OpenMP::OpenMP_CXX) else() set(libllm_SOURCES ${libllm_SOURCES} "mp_thread_pool.cc") endif() @@ -135,40 +106,15 @@ if (WITH_CUDA) "cuda/transform.cu" "cuda/unfold.cu") - set(unittest_SOURCES ${unittest_SOURCES} "cuda/test.cc") + set(libllm_test_SOURCES ${libllm_test_SOURCES} "cuda/test.cc") if (WITH_CUTLASS) set(libllm_SOURCES ${libllm_SOURCES} "cuda/gemm_cutlass.cu") endif(WITH_CUTLASS) - set(llmextcublas_SOURCES + set(llmplugincublas_SOURCES "cuda/gemm_cublas.cc" - "lut/internal/log.cc") -endif() - -# OS specific code -if(WIN32) - set(libllm_SOURCES - ${libllm_SOURCES} - "lut/path_windows.cc" - "lut/platform_windows.cc" - "lut/shared_library_windows.cc") -endif() -if(UNIX) - set(libllm_SOURCES - ${libllm_SOURCES} - "lut/platform_linux.cc" - "lut/shared_library_linux.cc") -endif() -if(UNIX AND APPLE) - set(libllm_SOURCES - ${libllm_SOURCES} - "lut/path_darwin.cc") -endif() -if(UNIX AND NOT APPLE) - set(libllm_SOURCES - ${libllm_SOURCES} - "lut/path_linux.cc") + "../lut/internal/log.cc") endif() # CPU specific code @@ -193,22 +139,25 @@ if(LIBLLM_KERNEL_X86_64) "cpu/kernel/avx2.cc" PROPERTIES COMPILE_FLAGS "-mavx2 -mfma -mf16c") endif(UNIX) - set(unittest_SOURCES - ${unittest_SOURCES} + set(libllm_test_SOURCES + ${libllm_test_SOURCES} "cpu/kernel/avx2_test.cc" "cpu/kernel/avx512_test.cc") endif() + if(LIBLLM_KERNEL_AARCH64) set(libllm_SOURCES ${libllm_SOURCES} "cpu/kernel/asimdhp.cc") - set(unittest_SOURCES - ${unittest_SOURCES} + set(libllm_test_SOURCES + ${libllm_test_SOURCES} "cpu/test_float16.cc" "cpu/kernel/asimdhp_test.cc") endif() -add_library(lut STATIC ${lut_SOURCES}) -set_target_properties(lut PROPERTIES CXX_VISIBILITY_PRESET hidden) -target_include_directories(lut PRIVATE ${libllm_INCDIR}) +if (WITH_CUDA) + add_library(llmplugincublas_static OBJECT ${llmplugincublas_SOURCES}) + target_compile_options(llmplugincublas_static PRIVATE "-DLIBLLM_EXPORTS") + target_include_directories(llmplugincublas_static PRIVATE ${libllm_INCDIR}) +endif() add_library(libllm_static OBJECT ${libllm_SOURCES}) target_compile_options(libllm_static PRIVATE "-DLIBLLM_EXPORTS") @@ -216,53 +165,5 @@ set_target_properties(libllm_static PROPERTIES CXX_VISIBILITY_PRESET hidden) set_target_properties(libllm_static PROPERTIES CUDA_VISIBILITY_PRESET hidden) target_include_directories(libllm_static PRIVATE ${libllm_INCDIR}) -add_library(libllm SHARED $) -target_link_libraries(libllm ${libllm_LIBADD} ) -set_property(TARGET libllm PROPERTY OUTPUT_NAME llm) -if(UNIX AND NOT APPLE) - target_link_options(libllm PUBLIC "-Wl,--no-undefined") -endif() - -set(unittest_LIBADD - libllm_static - ${libllm_LIBADD} - catch2) -if (WITH_MKL) - set(unittest_LIBADD - ${unittest_LIBADD} - mkl_intel_lp64 - mkl_intel_thread - mkl_core - iomp5) -endif() - -set(benchmark_LIBADD - libllm_static - ${libllm_LIBADD}) - -add_library(catch2 STATIC "../../third_party/catch2/catch_amalgamated.cpp") -add_executable(unittest ${unittest_SOURCES}) -target_include_directories(unittest PRIVATE ${libllm_INCDIR}) -target_link_libraries(unittest ${unittest_LIBADD}) - -add_executable(benchmark ${benchmark_SOURCES}) -target_include_directories(benchmark PRIVATE ${libllm_INCDIR}) -target_link_libraries(benchmark ${benchmark_LIBADD}) - -if (WITH_CUDA) - add_library(llmextcublas SHARED ${llmextcublas_SOURCES}) - target_include_directories(llmextcublas PRIVATE ${libllm_INCDIR}) - target_link_libraries(llmextcublas lut CUDA::cublas) - if(UNIX) - target_link_options(llmextcublas PUBLIC "-Wl,--no-undefined") - endif(UNIX) -endif() - -enable_testing() -add_test(NAME unittest COMMAND $) - -add_custom_target(llmbin - ALL - DEPENDS libllm - COMMAND go build -o $>/llm${CMAKE_EXECUTABLE_SUFFIX} ${CMAKE_SOURCE_DIR}/go/bin - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/go/bin) +add_library(libllm_test OBJECT ${libllm_test_SOURCES}) +target_include_directories(libllm_test PRIVATE ${libllm_INCDIR}) diff --git a/src/libllm/benchmark_main.cc b/src/libllm/benchmark_main.cc index f7a0a3e0..ebfb2f69 100644 --- a/src/libllm/benchmark_main.cc +++ b/src/libllm/benchmark_main.cc @@ -26,12 +26,12 @@ #include "libllm/functional.h" #include "libllm/llama.h" #include "libllm/llm.h" -#include "libllm/lut/error.h" -#include "libllm/lut/flags.h" -#include "libllm/lut/random.h" -#include "libllm/lut/time.h" #include "libllm/model_for_generation.h" #include "libllm/operators.h" +#include "lut/error.h" +#include "lut/flags.h" +#include "lut/random.h" +#include "lut/time.h" constexpr int MagicNumber = 0x55aa; constexpr double MaxWait = 10; diff --git a/src/libllm/bpe_config.h b/src/libllm/bpe_config.h index 84e017fd..a5ccb607 100644 --- a/src/libllm/bpe_config.h +++ b/src/libllm/bpe_config.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -21,7 +21,8 @@ #include #include -#include "libllm/lut/ini_config.h" + +#include "lut/ini_config.h" namespace libllm { diff --git a/src/libllm/bpe_encoder.cc b/src/libllm/bpe_encoder.cc index 290121ff..dac6aceb 100644 --- a/src/libllm/bpe_encoder.cc +++ b/src/libllm/bpe_encoder.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,18 +19,18 @@ #include "libllm/bpe_encoder.h" -#include "libllm/lut/strings.h" +#include "lut/strings.h" namespace libllm { BPEEncoder::BPEEncoder(const BPEModel *model, const BPEConfig &config) : _model(model), _config(&config), - _header(nullptr) {} + _header(nullptr) { +} void BPEEncoder::initQueue() { - Symbol *p = _header->next, - *q = p->next; + Symbol *p = _header->next, *q = p->next; while (q) { addBigramIfExist(p, q); p = q; @@ -69,8 +69,7 @@ std::vector BPEEncoder::encode(const std::string &s) { } void BPEEncoder::addBigramIfExist(Symbol *left, Symbol *right) { - if (left == _header || right == nullptr || - _model->isSpecialToken(right->tokenId) || + if (left == _header || right == nullptr || _model->isSpecialToken(right->tokenId) || _model->isSpecialToken(left->tokenId)) { return; } @@ -119,7 +118,7 @@ BPEEncoder::Symbol *BPEEncoder::mergeBigram(const Bigram &bigram) { std::vector BPEEncoder::splitBytes(const std::string &s) { std::vector l; - + char buffer[2] = " "; for (char ch : s) { buffer[0] = ch; @@ -137,7 +136,7 @@ BPEEncoder::Symbol *BPEEncoder::appendToken(Symbol *tail, int tokenId) { symbol->next = nullptr; tail->next = symbol; - + return symbol; } diff --git a/src/libllm/bpe_encoder.h b/src/libllm/bpe_encoder.h index 5adcc7c0..26ec351a 100644 --- a/src/libllm/bpe_encoder.h +++ b/src/libllm/bpe_encoder.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -21,20 +21,20 @@ #include #include -#include "libllm/lut/noncopyable.h" -#include "libllm/lut/pool.h" + #include "libllm/bpe_config.h" #include "libllm/bpe_model.h" +#include "lut/noncopyable.h" +#include "lut/pool.h" namespace libllm { - // String to BPE token-ids encoder. class BPEEncoder : private lut::NonCopyable { public: BPEEncoder(const BPEModel *model, const BPEConfig &config); - // encode string to token ids. + // encode string to token ids. std::vector encode(const std::string &s); private: @@ -46,7 +46,9 @@ class BPEEncoder : private lut::NonCopyable { Symbol *next; int tokenId; - bool valid() const { return tokenId != Vocab::kInvalidToken; } + bool valid() const { + return tokenId != Vocab::kInvalidToken; + } }; struct Bigram { diff --git a/src/libllm/bpe_model.cc b/src/libllm/bpe_model.cc index 66ef6bf9..53358909 100644 --- a/src/libllm/bpe_model.cc +++ b/src/libllm/bpe_model.cc @@ -21,9 +21,9 @@ #include -#include "libllm/lut/error.h" -#include "libllm/lut/reader.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/reader.h" +#include "lut/strings.h" namespace libllm { diff --git a/src/libllm/bpe_model.h b/src/libllm/bpe_model.h index 774d69f7..33bc1c26 100644 --- a/src/libllm/bpe_model.h +++ b/src/libllm/bpe_model.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,14 +20,14 @@ #pragma once #include -#include "libllm/lut/reader.h" + #include "libllm/vocab.h" +#include "lut/reader.h" namespace libllm { // Store tne data from sentence-piece BPE model. -class BPEModel : public Vocab, - private lut::NonCopyable { +class BPEModel : public Vocab, private lut::NonCopyable { public: // token flags. static constexpr int kUnknown = 1; @@ -66,7 +66,9 @@ class BPEModel : public Vocab, // get token-id for a single byte. int getByteId(int byte) const; - bool isByteTokenAvailable() const { return _isByteTokenAvailable; } + bool isByteTokenAvailable() const { + return _isByteTokenAvailable; + } private: struct TokenInfo; @@ -83,7 +85,7 @@ class BPEModel : public Vocab, int _unkId; int _spaceId; - + BPEModel(); // read model from fp @@ -101,7 +103,9 @@ struct BPEModel::TokenInfo { std::string tokenString; int8_t flag; - constexpr bool isSpecialToken() const { return flag != 0; } + constexpr bool isSpecialToken() const { + return flag != 0; + } }; } // namespace libllm \ No newline at end of file diff --git a/src/libllm/context.cc b/src/libllm/context.cc index f93095b1..16034c18 100644 --- a/src/libllm/context.cc +++ b/src/libllm/context.cc @@ -20,9 +20,9 @@ #include "libllm/context.h" #include "libllm/device.h" -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/log.h" +#include "lut/strings.h" namespace libllm { diff --git a/src/libllm/cpu/accessor.h b/src/libllm/cpu/accessor.h index fe22fd0e..ceedaf7b 100644 --- a/src/libllm/cpu/accessor.h +++ b/src/libllm/cpu/accessor.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,8 +20,9 @@ #pragma once #include -#include "libllm/lut/span.h" + #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { @@ -51,7 +52,10 @@ class TensorAccessorBase { return _data; } - TensorAccessorBase(const TensorShape::Elem *size, T *data) : _size(size), _data(data) {} + TensorAccessorBase(const TensorShape::Elem *size, T *data) + : _size(size), + _data(data) { + } protected: const TensorShape::Elem *_size; @@ -61,11 +65,16 @@ class TensorAccessorBase { template class TensorAccessor : public TensorAccessorBase { public: - TensorAccessor(Tensor &tensor) : TensorAccessorBase(tensor) {} - TensorAccessor(const Tensor &tensor) : TensorAccessorBase(tensor) {} + TensorAccessor(Tensor &tensor) + : TensorAccessorBase(tensor) { + } + TensorAccessor(const Tensor &tensor) + : TensorAccessorBase(tensor) { + } - TensorAccessor(const TensorShape::Elem *size, T *data) : - TensorAccessorBase(size, data) {} + TensorAccessor(const TensorShape::Elem *size, T *data) + : TensorAccessorBase(size, data) { + } TensorAccessor operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -80,11 +89,16 @@ class TensorAccessor : public TensorAccessorBase { template class TensorAccessor : public TensorAccessorBase { public: - TensorAccessor(Tensor &tensor) : TensorAccessorBase(tensor) {} - TensorAccessor(const Tensor &tensor) : TensorAccessorBase(tensor) {} + TensorAccessor(Tensor &tensor) + : TensorAccessorBase(tensor) { + } + TensorAccessor(const Tensor &tensor) + : TensorAccessorBase(tensor) { + } - TensorAccessor(const TensorShape::Elem *size, T *data) : - TensorAccessorBase(size, data) {} + TensorAccessor(const TensorShape::Elem *size, T *data) + : TensorAccessorBase(size, data) { + } T &operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -101,13 +115,19 @@ class TensorList { public: static TensorList fromTensor(const Tensor &src); static TensorList fromTensor(Tensor &src); - + lut::Span getShape() const { - return lut::Span(_shape, DIM); + return lut::Span(_shape, DIM); + } + int getShape(int d) const { + return _shape[d].shape; + } + int getLength() const { + return static_cast(_pointerList.size()); + } + lut::Span getDataPtrList() const { + return lut::makeConstSpan(_pointerList); } - int getShape(int d) const { return _shape[d].shape; } - int getLength() const { return static_cast(_pointerList.size()); } - lut::Span getDataPtrList() const { return lut::makeConstSpan(_pointerList); } TensorAccessor getTensor(int index) const { return TensorAccessor(_shape, _pointerList[index]); @@ -117,9 +137,10 @@ class TensorList { const TensorShape::Elem *_shape; std::vector _pointerList; - TensorList(const TensorShape::Elem *shape, std::vector &&pointerList): - _shape(shape), - _pointerList(std::move(pointerList)) {} + TensorList(const TensorShape::Elem *shape, std::vector &&pointerList) + : _shape(shape), + _pointerList(std::move(pointerList)) { + } }; template @@ -167,7 +188,6 @@ TensorList TensorList::fromTensor(Tensor &src) { return TensorList(tensorShape, std::move(pointerList)); } -} // cpu -} // op -} // libllm - +} // namespace cpu +} // namespace op +} // namespace libllm diff --git a/src/libllm/cpu/binary_op.cc b/src/libllm/cpu/binary_op.cc index 0d4760d9..46662072 100644 --- a/src/libllm/cpu/binary_op.cc +++ b/src/libllm/cpu/binary_op.cc @@ -22,9 +22,9 @@ #include "libllm/cpu/accessor.h" #include "libllm/cpu/common.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/attributes.h" #include "libllm/mp.h" #include "libllm/tensor.h" +#include "lut/attributes.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/cast.cc b/src/libllm/cpu/cast.cc index 38a2b462..70eaa6e2 100644 --- a/src/libllm/cpu/cast.cc +++ b/src/libllm/cpu/cast.cc @@ -29,8 +29,8 @@ #include "libllm/cpu/kernel/interface.h" #include "libllm/cpu/lookup.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/half.h" #include "libllm/tensor.h" +#include "lut/half.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/common.h b/src/libllm/cpu/common.h index e39f4369..30f064ad 100644 --- a/src/libllm/cpu/common.h +++ b/src/libllm/cpu/common.h @@ -21,8 +21,8 @@ #include "libllm/cpu/accessor.h" #include "libllm/cpu/kernel/interface.h" -#include "libllm/lut/span.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/cpu_tensor_data.cc b/src/libllm/cpu/cpu_tensor_data.cc index 223cce47..8c027356 100644 --- a/src/libllm/cpu/cpu_tensor_data.cc +++ b/src/libllm/cpu/cpu_tensor_data.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -21,9 +21,9 @@ #include "libllm/device.h" #include "libllm/dtype.h" -#include "libllm/lut/error.h" -#include "libllm/lut/platform.h" -#include "libllm/lut/span.h" +#include "lut/error.h" +#include "lut/platform.h" +#include "lut/span.h" namespace libllm { namespace op { @@ -32,7 +32,8 @@ namespace cpu { CpuTensorData::Slot::Slot() : data(nullptr), numel(0), - dtype(DType::kUnknown) {} + dtype(DType::kUnknown) { +} int64_t CpuTensorData::Slot::getNumEl() const { return numel; @@ -44,7 +45,9 @@ Byte *CpuTensorData::Slot::getRawData() const { return data; } -CpuTensorData::CpuTensorData() : _numSlot(0) {} +CpuTensorData::CpuTensorData() + : _numSlot(0) { +} void CpuTensorData::readSlot(lut::Reader *fp, int slotIdx) { CHECK(slotIdx < MaxSlot); @@ -53,19 +56,16 @@ void CpuTensorData::readSlot(lut::Reader *fp, int slotIdx) { CHECK(slot.data == nullptr); slot.dtype = fp->readValue(); - if (!slot.dtype.isValid()) - THROW(Aborted, "invalid dtype."); + if (!slot.dtype.isValid()) THROW(Aborted, "invalid dtype."); slot.numel = fp->readValue(); - if (slot.numel > MaxNumEl) - throw lut::AbortedError("tensor too big"); - + if (slot.numel > MaxNumEl) throw lut::AbortedError("tensor too big"); + int64_t size = slot.dtype.getTotalSize(slot.numel); slot.data = reinterpret_cast(lut::alloc32ByteAlignedMem(size)); fp->readSpan(lut::makeSpan(reinterpret_cast(slot.data), size)); int magicNumber = fp->readValue(); - if (magicNumber != 0x55aa) - throw lut::AbortedError("bad tensor data format (magic number)."); + if (magicNumber != 0x55aa) throw lut::AbortedError("bad tensor data format (magic number)."); } std::shared_ptr CpuTensorData::create(int64_t numel, DType dtype) { @@ -97,13 +97,11 @@ std::shared_ptr CpuTensorData::create( std::shared_ptr CpuTensorData::read(lut::Reader *fp) { std::shared_ptr tensorData = std::make_shared(); - if (fp->readString(4) != "tdat") - throw lut::AbortedError("bad tensor data format."); + if (fp->readString(4) != "tdat") throw lut::AbortedError("bad tensor data format."); int32_t numSlot = fp->readValue(); - if (numSlot <= 0 || numSlot > 3) - throw lut::AbortedError("invalid num slot."); - + if (numSlot <= 0 || numSlot > 3) throw lut::AbortedError("invalid num slot."); + // slot 0 tensorData->readSlot(fp, 0); diff --git a/src/libllm/cpu/cpu_tensor_data.h b/src/libllm/cpu/cpu_tensor_data.h index dc30b6bf..7e3ada28 100644 --- a/src/libllm/cpu/cpu_tensor_data.h +++ b/src/libllm/cpu/cpu_tensor_data.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,10 +19,9 @@ #pragma once - -#include "libllm/lut/span.h" #include "libllm/device.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/gelu.cc b/src/libllm/cpu/gelu.cc index 7f552717..5d80740d 100644 --- a/src/libllm/cpu/gelu.cc +++ b/src/libllm/cpu/gelu.cc @@ -23,8 +23,8 @@ #include "libllm/cpu/accessor.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/thread_pool.h" #include "libllm/mp.h" +#include "lut/thread_pool.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/asimdhp.h b/src/libllm/cpu/kernel/asimdhp.h index 795aa5c5..ab811b18 100644 --- a/src/libllm/cpu/kernel/asimdhp.h +++ b/src/libllm/cpu/kernel/asimdhp.h @@ -22,7 +22,7 @@ #include #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/log.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/asimdhp_test.cc b/src/libllm/cpu/kernel/asimdhp_test.cc index d31614bd..272ee116 100644 --- a/src/libllm/cpu/kernel/asimdhp_test.cc +++ b/src/libllm/cpu/kernel/asimdhp_test.cc @@ -25,9 +25,9 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/test_common.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" -#include "libllm/lut/random.h" +#include "lut/half.h" +#include "lut/log.h" +#include "lut/random.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/avx2.h b/src/libllm/cpu/kernel/avx2.h index e09f7717..b6f19a26 100644 --- a/src/libllm/cpu/kernel/avx2.h +++ b/src/libllm/cpu/kernel/avx2.h @@ -22,7 +22,7 @@ #include #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/log.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/avx2_test.cc b/src/libllm/cpu/kernel/avx2_test.cc index bfcfd658..39a949ba 100644 --- a/src/libllm/cpu/kernel/avx2_test.cc +++ b/src/libllm/cpu/kernel/avx2_test.cc @@ -23,9 +23,9 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/test_common.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" -#include "libllm/lut/random.h" +#include "lut/half.h" +#include "lut/log.h" +#include "lut/random.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/avx512.h b/src/libllm/cpu/kernel/avx512.h index 2320f6db..7ed9de9f 100644 --- a/src/libllm/cpu/kernel/avx512.h +++ b/src/libllm/cpu/kernel/avx512.h @@ -23,7 +23,7 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/avx2.h" -#include "libllm/lut/log.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/avx512_test.cc b/src/libllm/cpu/kernel/avx512_test.cc index 5d6f2415..5c69e111 100644 --- a/src/libllm/cpu/kernel/avx512_test.cc +++ b/src/libllm/cpu/kernel/avx512_test.cc @@ -23,9 +23,9 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/test_common.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" -#include "libllm/lut/random.h" +#include "lut/half.h" +#include "lut/log.h" +#include "lut/random.h" #include "ruapu/ruapu.h" namespace libllm { diff --git a/src/libllm/cpu/kernel/benchmark.cc b/src/libllm/cpu/kernel/benchmark.cc index ea274b22..be88ab3e 100644 --- a/src/libllm/cpu/kernel/benchmark.cc +++ b/src/libllm/cpu/kernel/benchmark.cc @@ -17,7 +17,7 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "catch2/catch_amalgamated.hpp" +#include "../../../../third_party/catch2/catch_amalgamated.hpp" #ifdef MKL_ENABLED #include @@ -29,10 +29,10 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/gemm.h" #include "libllm/cpu/kernel/interface.h" -#include "libllm/lut/attributes.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/time.h" +#include "lut/attributes.h" +#include "lut/log.h" +#include "lut/strings.h" +#include "lut/time.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/block.h b/src/libllm/cpu/kernel/block.h index c5082ed5..2ecc5219 100644 --- a/src/libllm/cpu/kernel/block.h +++ b/src/libllm/cpu/kernel/block.h @@ -20,9 +20,9 @@ #pragma once #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/log.h" -#include "libllm/lut/time.h" #include "libllm/mp.h" +#include "lut/log.h" +#include "lut/time.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/cvt.h b/src/libllm/cpu/kernel/cvt.h index 9c1506f5..553e484c 100644 --- a/src/libllm/cpu/kernel/cvt.h +++ b/src/libllm/cpu/kernel/cvt.h @@ -20,8 +20,8 @@ #pragma once #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/log.h" #include "libllm/mp.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/fallback.cc b/src/libllm/cpu/kernel/fallback.cc index 4d273e40..82402103 100644 --- a/src/libllm/cpu/kernel/fallback.cc +++ b/src/libllm/cpu/kernel/fallback.cc @@ -27,9 +27,9 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/attributes.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" +#include "lut/attributes.h" +#include "lut/half.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/fallback.h b/src/libllm/cpu/kernel/fallback.h index efdd4f69..ac4606ca 100644 --- a/src/libllm/cpu/kernel/fallback.h +++ b/src/libllm/cpu/kernel/fallback.h @@ -22,7 +22,7 @@ #include #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/log.h" +#include "lut/log.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/gemm.h b/src/libllm/cpu/kernel/gemm.h index 54dded1b..ada3a28e 100644 --- a/src/libllm/cpu/kernel/gemm.h +++ b/src/libllm/cpu/kernel/gemm.h @@ -23,9 +23,9 @@ #include "libllm/cpu/kernel/block.h" #include "libllm/cpu/kernel/cvt.h" #include "libllm/cpu/kernel/gemv.h" -#include "libllm/lut/log.h" -#include "libllm/lut/time.h" #include "libllm/mp.h" +#include "lut/log.h" +#include "lut/time.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/gemv.h b/src/libllm/cpu/kernel/gemv.h index e3d26964..29391762 100644 --- a/src/libllm/cpu/kernel/gemv.h +++ b/src/libllm/cpu/kernel/gemv.h @@ -26,8 +26,8 @@ #include "libllm/cpu/kernel/abstract.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/c_ptr.h" #include "libllm/mp.h" +#include "lut/c_ptr.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/interface.cc b/src/libllm/cpu/kernel/interface.cc index 61c17086..2cb0f5f4 100644 --- a/src/libllm/cpu/kernel/interface.cc +++ b/src/libllm/cpu/kernel/interface.cc @@ -33,10 +33,10 @@ #include "libllm/cpu/kernel/gemm.h" #include "libllm/cpu/kernel/gemv.h" #include "libllm/cpu/kernel/interface.h" -#include "libllm/lut/is_debug.h" -#include "libllm/lut/log.h" -#include "libllm/lut/platform.h" -#include "libllm/lut/strings.h" +#include "lut/is_debug.h" +#include "lut/log.h" +#include "lut/platform.h" +#include "lut/strings.h" #include "ruapu/ruapu.h" namespace libllm { diff --git a/src/libllm/cpu/kernel/interface.h b/src/libllm/cpu/kernel/interface.h index 479f5ee4..c3af2c05 100644 --- a/src/libllm/cpu/kernel/interface.h +++ b/src/libllm/cpu/kernel/interface.h @@ -21,7 +21,7 @@ #include -#include "libllm/lut/attributes.h" +#include "lut/attributes.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/interface_test.cc b/src/libllm/cpu/kernel/interface_test.cc index 22323bd2..b2a30a73 100644 --- a/src/libllm/cpu/kernel/interface_test.cc +++ b/src/libllm/cpu/kernel/interface_test.cc @@ -24,9 +24,9 @@ #include "catch2/catch_amalgamated.hpp" #include "libllm/cpu/kernel/test_common.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" -#include "libllm/lut/random.h" +#include "lut/half.h" +#include "lut/log.h" +#include "lut/random.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/test_common.h b/src/libllm/cpu/kernel/test_common.h index 3b2d0851..a418f36d 100644 --- a/src/libllm/cpu/kernel/test_common.h +++ b/src/libllm/cpu/kernel/test_common.h @@ -28,9 +28,9 @@ #include "libllm/cpu/kernel/avx512.h" #include "libllm/cpu/kernel/fallback.h" #include "libllm/cpu/kernel/util.h" -#include "libllm/lut/half.h" -#include "libllm/lut/log.h" -#include "libllm/lut/random.h" +#include "lut/half.h" +#include "lut/log.h" +#include "lut/random.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/kernel/util.cc b/src/libllm/cpu/kernel/util.cc index 829bf893..18e36a8b 100644 --- a/src/libllm/cpu/kernel/util.cc +++ b/src/libllm/cpu/kernel/util.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,10 +20,12 @@ #include "libllm/cpu/kernel/util.h" #include + #include + #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/half.h" -#include "libllm/lut/platform.h" +#include "lut/half.h" +#include "lut/platform.h" #ifdef LUT_ARCH_AARCH64 #include @@ -34,7 +36,6 @@ namespace op { namespace cpu { namespace kernel { - float cvt_h2s(Float16 vh) { #ifdef LUT_ARCH_AARCH64 float16x4_t a00 = vld1_dup_f16(&vh); @@ -59,4 +60,3 @@ Float16 cvt_s2h(float vf) { } // namespace cpu } // namespace op } // namespace libllm - diff --git a/src/libllm/cpu/kernel/util.h b/src/libllm/cpu/kernel/util.h index 2a23a9ce..00bd96bc 100644 --- a/src/libllm/cpu/kernel/util.h +++ b/src/libllm/cpu/kernel/util.h @@ -24,9 +24,9 @@ #include #include "libllm/cpu/kernel/abstract.h" -#include "libllm/lut/c_ptr.h" -#include "libllm/lut/platform.h" -#include "libllm/lut/span.h" +#include "lut/c_ptr.h" +#include "lut/platform.h" +#include "lut/span.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/log_mel_spectrogram.cc b/src/libllm/cpu/log_mel_spectrogram.cc index e006ec87..f5caa5d9 100644 --- a/src/libllm/cpu/log_mel_spectrogram.cc +++ b/src/libllm/cpu/log_mel_spectrogram.cc @@ -27,8 +27,8 @@ #include "libllm/cpu/accessor.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/thread_pool.h" #include "libllm/mp.h" +#include "lut/thread_pool.h" #include "pocketfft/pocketfft_hdronly.h" namespace libllm { diff --git a/src/libllm/cpu/log_mel_spectrogram_test.cc b/src/libllm/cpu/log_mel_spectrogram_test.cc index 1ee3a378..a4986c63 100644 --- a/src/libllm/cpu/log_mel_spectrogram_test.cc +++ b/src/libllm/cpu/log_mel_spectrogram_test.cc @@ -22,8 +22,8 @@ #include "catch2/catch_amalgamated.hpp" #include "libllm/cpu/fingerprint.h" #include "libllm/functional.h" -#include "libllm/lut/base64.h" #include "libllm/wave.h" +#include "lut/base64.h" namespace libllm { namespace op { @@ -144,8 +144,6 @@ CATCH_TEST_CASE("test logMelSpectrogram", "[op][cpu][logmelspectrogram]") { Tensor wave = Wave::read(pcmSpan, WaveFormat::Wave16kHz16bitMonoPCM); Tensor features = logMelSpectrogram(wave); - F::print(op::cpu::fingerprint(features)); - CATCH_REQUIRE(F::allClose( op::cpu::fingerprint(features), Tensor::create( diff --git a/src/libllm/cpu/matmul.cc b/src/libllm/cpu/matmul.cc index c0671400..0aafa8f9 100644 --- a/src/libllm/cpu/matmul.cc +++ b/src/libllm/cpu/matmul.cc @@ -23,8 +23,8 @@ #include "libllm/cpu/common.h" #include "libllm/cpu/kernel/interface.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/strings.h" #include "libllm/mp.h" +#include "lut/strings.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/rand.cc b/src/libllm/cpu/rand.cc index 351c7ebd..cc19713b 100644 --- a/src/libllm/cpu/rand.cc +++ b/src/libllm/cpu/rand.cc @@ -26,11 +26,11 @@ #include "libllm/cpu/cast.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/half.h" -#include "libllm/lut/random.h" -#include "libllm/lut/time.h" #include "libllm/mp.h" #include "libllm/tensor.h" +#include "lut/half.h" +#include "lut/random.h" +#include "lut/time.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/rand.h b/src/libllm/cpu/rand.h index 92bbae7a..0f25a513 100644 --- a/src/libllm/cpu/rand.h +++ b/src/libllm/cpu/rand.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,7 +20,7 @@ #pragma once #include "libllm/tensor.h" -#include "libllm/lut/random.h" +#include "lut/random.h" namespace libllm { namespace op { @@ -30,6 +30,6 @@ Tensor rand(lut::Span shape, DType dtype, lut::Random *generator, flo Tensor randFp32(lut::Span shape, lut::Random *generator, float min, float max); Tensor randQ4(lut::Span shape, lut::Random *generator, float min, float max); -} // cpu -} // op -} // ly +} // namespace cpu +} // namespace op +} // namespace libllm diff --git a/src/libllm/cpu/swiglu.cc b/src/libllm/cpu/swiglu.cc index 0ab9790d..65657086 100644 --- a/src/libllm/cpu/swiglu.cc +++ b/src/libllm/cpu/swiglu.cc @@ -23,8 +23,8 @@ #include "libllm/cpu/accessor.h" #include "libllm/cpu/tensor.h" -#include "libllm/lut/thread_pool.h" #include "libllm/mp.h" +#include "lut/thread_pool.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/unfold.h b/src/libllm/cpu/unfold.h index 36246c6c..25614315 100644 --- a/src/libllm/cpu/unfold.h +++ b/src/libllm/cpu/unfold.h @@ -19,8 +19,8 @@ #pragma once -#include "libllm/lut/span.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { diff --git a/src/libllm/cpu/view.h b/src/libllm/cpu/view.h index 1fd4b47f..67be9aff 100644 --- a/src/libllm/cpu/view.h +++ b/src/libllm/cpu/view.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,7 +20,7 @@ #pragma once #include "libllm/tensor.h" -#include "libllm/lut/span.h" +#include "lut/span.h" namespace libllm { namespace op { @@ -32,12 +32,11 @@ Tensor view(const Tensor &src, lut::Span view); std::vector getRealShape(int64_t numEl, lut::Span view); // infer the stride for new view, according to the original stride. -std::vector getViewShapeStride( - const Tensor &src, lut::Span view); +std::vector getViewShapeStride(const Tensor &src, lut::Span view); // merge contiguous dimensions in the shape of `src`. std::vector mergeContigShape(const Tensor &src); -} // cpu -} // op -} // ly +} // namespace cpu +} // namespace op +} // namespace libllm diff --git a/src/libllm/cuda/binary_op.cu b/src/libllm/cuda/binary_op.cu index f3277925..d5868b48 100644 --- a/src/libllm/cuda/binary_op.cu +++ b/src/libllm/cuda/binary_op.cu @@ -22,8 +22,8 @@ #include "libllm/cpu/common.h" #include "libllm/cuda/binary_op.h" #include "libllm/cuda/common.h" -#include "libllm/lut/span.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { diff --git a/src/libllm/cuda/common.h b/src/libllm/cuda/common.h index 8329694f..14f7775f 100644 --- a/src/libllm/cuda/common.h +++ b/src/libllm/cuda/common.h @@ -27,10 +27,10 @@ #include "libllm/cuda/subtensor.h" #include "libllm/dtype.h" -#include "libllm/lut/c_ptr.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" #include "libllm/tensor.h" +#include "lut/c_ptr.h" +#include "lut/error.h" +#include "lut/strings.h" #define LL_CHECK_CONTIGUOUS(x) \ { \ diff --git a/src/libllm/cuda/cuda_tensor_data.cc b/src/libllm/cuda/cuda_tensor_data.cc index 33d32e20..f0af933a 100644 --- a/src/libllm/cuda/cuda_tensor_data.cc +++ b/src/libllm/cuda/cuda_tensor_data.cc @@ -24,10 +24,10 @@ #include "libllm/cuda/common.h" #include "libllm/device.h" #include "libllm/dtype.h" -#include "libllm/lut/error.h" -#include "libllm/lut/platform.h" -#include "libllm/lut/span.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/platform.h" +#include "lut/span.h" +#include "lut/strings.h" namespace libllm { namespace op { diff --git a/src/libllm/cuda/cuda_tensor_data.h b/src/libllm/cuda/cuda_tensor_data.h index cd0b252e..44808ac1 100644 --- a/src/libllm/cuda/cuda_tensor_data.h +++ b/src/libllm/cuda/cuda_tensor_data.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,9 +20,10 @@ #pragma once #include -#include "libllm/lut/span.h" + #include "libllm/device.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { namespace op { @@ -31,8 +32,7 @@ namespace cuda { class CudaTensorData : public TensorData { public: static std::shared_ptr create(int64_t numel, DType dtype); - static std::shared_ptr create( - lut::Span> slots); + static std::shared_ptr create(lut::Span> slots); CudaTensorData(); ~CudaTensorData(); @@ -58,6 +58,6 @@ class CudaTensorData : public TensorData { int _numSlot; }; -} // cuda -} // op -} // ly +} // namespace cuda +} // namespace op +} // namespace libllm diff --git a/src/libllm/cuda/gemm_cutlass.cu b/src/libllm/cuda/gemm_cutlass.cu index 9b03258d..a0ea8389 100644 --- a/src/libllm/cuda/gemm_cutlass.cu +++ b/src/libllm/cuda/gemm_cutlass.cu @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,23 +17,23 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/cuda/gemm_cutlass.h" - #include #include #include -#include "libllm/dtype.h" + #include "libllm/cpu/common.h" #include "libllm/cpu/matmul.h" #include "libllm/cuda/common.h" -#include "libllm/lut/error.h" +#include "libllm/cuda/gemm_cutlass.h" +#include "libllm/dtype.h" +#include "lut/error.h" namespace libllm { namespace op { namespace cuda { -using cutlass::layout::RowMajor; using cutlass::layout::ColumnMajor; +using cutlass::layout::RowMajor; std::shared_ptr CutlassGemm::create() { std::shared_ptr mm = std::make_shared(); @@ -54,20 +54,18 @@ lut::ErrorCode hgemmT( cutlass::half_t *C, int ldc) { using CutlassGemm = cutlass::gemm::device::Gemm< - cutlass::half_t, LayoutA, - cutlass::half_t, layoutB, - cutlass::half_t, RowMajor, + cutlass::half_t, + LayoutA, + cutlass::half_t, + layoutB, + cutlass::half_t, + RowMajor, float, cutlass::arch::OpClassSimt, cutlass::arch::Sm61>; CutlassGemm gemmOperator; - typename CutlassGemm::Arguments args( - {m, n, k}, - {A, lda}, - {B, ldb}, - {C, ldc}, - {C, ldc}, - {alpha, beta}); + typename CutlassGemm::Arguments + args({m, n, k}, {A, lda}, {B, ldb}, {C, ldc}, {C, ldc}, {alpha, beta}); cutlass::Status status = gemmOperator(args); if (status != cutlass::Status::kSuccess) { return lut::ErrorCode::Aborted; @@ -118,22 +116,19 @@ lut::ErrorCode hgemmArrayT( int ldc, int batchSize) { using CutlassGemm = cutlass::gemm::device::GemmArray< - cutlass::half_t, LayoutA, - cutlass::half_t, layoutB, - cutlass::half_t, RowMajor, + cutlass::half_t, + LayoutA, + cutlass::half_t, + layoutB, + cutlass::half_t, + RowMajor, float, cutlass::arch::OpClassSimt, cutlass::arch::Sm61>; CutlassGemm gemmOperator; - typename CutlassGemm::Arguments args( - {m, n, k}, - A, lda, - B, ldb, - C, ldc, - C, ldc, - {alpha, beta}, - batchSize); + typename CutlassGemm::Arguments + args({m, n, k}, A, lda, B, ldb, C, ldc, C, ldc, {alpha, beta}, batchSize); cutlass::Status status = gemmOperator(args); if (status != cutlass::Status::kSuccess) { return lut::ErrorCode::Aborted; @@ -168,25 +163,23 @@ lut::ErrorCode cutlassHgemmArray( return hgemmArrayT(m, n, k, alpha, A, lda, B, ldb, beta, C, ldc, bs); } - return lut::ErrorCode::Aborted; } - lut::ErrorCode CutlassGemm::hgemm( - bool transA, - bool transB, - int m, - int n, - int k, - __half alpha, - const __half *A, - int lda, - const __half *B, - int ldb, - __half beta, - __half *C, - int ldc) { + bool transA, + bool transB, + int m, + int n, + int k, + __half alpha, + const __half *A, + int lda, + const __half *B, + int ldb, + __half beta, + __half *C, + int ldc) { cutlass::half_t alphaH = *reinterpret_cast(&alpha); cutlass::half_t betaH = *reinterpret_cast(&beta); return cutlassHgemm( @@ -205,7 +198,6 @@ lut::ErrorCode CutlassGemm::hgemm( ldc); } - lut::ErrorCode CutlassGemm::hgemmArray( bool transA, bool transB, @@ -240,6 +232,6 @@ lut::ErrorCode CutlassGemm::hgemmArray( batchSize); } -} // cuda -} // op -} // ly +} // namespace cuda +} // namespace op +} // namespace libllm diff --git a/src/libllm/cuda/matmul.cc b/src/libllm/cuda/matmul.cc index 24455c51..21cb3df2 100644 --- a/src/libllm/cuda/matmul.cc +++ b/src/libllm/cuda/matmul.cc @@ -28,7 +28,7 @@ #include "libllm/cuda/gemm_cutlass.h" #include "libllm/cuda/matvec.h" #include "libllm/dtype.h" -#include "libllm/lut/strings.h" +#include "lut/strings.h" namespace libllm { namespace op { @@ -62,7 +62,7 @@ std::shared_ptr MatMul::create() { std::shared_ptr MatMul::createCublas() { std::shared_ptr mm{new MatMul()}; - mm->_gemmExtLib = lut::SharedLibrary::open("llmextcublas"); + mm->_gemmExtLib = lut::SharedLibrary::open("llmplugincublas"); std::function factory; std::function deleter; diff --git a/src/libllm/cuda/matmul.h b/src/libllm/cuda/matmul.h index 1fcefc93..1bc2f97b 100644 --- a/src/libllm/cuda/matmul.h +++ b/src/libllm/cuda/matmul.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,12 +19,13 @@ #pragma once -#include #include -#include "libllm/tensor.h" +#include + #include "libllm/cuda/common.h" #include "libllm/cuda/gemm.h" -#include "libllm/lut/shared_library.h" +#include "libllm/tensor.h" +#include "lut/shared_library.h" namespace libllm { namespace op { @@ -56,6 +57,6 @@ class MatMul { std::vector getBatch(const Tensor &A, int nBatchDim); }; -} // cuda -} // op -} // ly +} // namespace cuda +} // namespace op +} // namespace libllm diff --git a/src/libllm/cuda/subtensor.h b/src/libllm/cuda/subtensor.h index 4ede79c0..e4f477af 100644 --- a/src/libllm/cuda/subtensor.h +++ b/src/libllm/cuda/subtensor.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -22,11 +22,13 @@ #include #include #include + #include -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" -#include "libllm/tensor.h" + #include "libllm/cuda/cuda_tensor_data.h" +#include "libllm/tensor.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { namespace op { @@ -40,7 +42,10 @@ struct Size { template class SubtensorBase { public: - __device__ SubtensorBase(const Size *size, T *data) : _size(size), _data(data) {} + __device__ SubtensorBase(const Size *size, T *data) + : _size(size), + _data(data) { + } protected: const Size *_size; @@ -50,7 +55,9 @@ class SubtensorBase { template class Subtensor : public SubtensorBase { public: - __device__ Subtensor(const Size *size, T *data) : SubtensorBase(size, data) {} + __device__ Subtensor(const Size *size, T *data) + : SubtensorBase(size, data) { + } __device__ Subtensor operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -65,7 +72,9 @@ class Subtensor : public SubtensorBase { template class Subtensor : public SubtensorBase { public: - __device__ Subtensor(const Size *size, T *data) : SubtensorBase(size, data) {} + __device__ Subtensor(const Size *size, T *data) + : SubtensorBase(size, data) { + } __device__ T &operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -77,7 +86,7 @@ class Subtensor : public SubtensorBase { } }; -/// @brief A packed tensor accessor. `Packed` means the subtensor also packed with the tensor +/// @brief A packed tensor accessor. `Packed` means the subtensor also packed with the tensor /// metadata. /// @tparam T Tensor data type. /// @tparam DIM Dimension of this tensor. @@ -100,9 +109,15 @@ class PackedSubtensorBase { } } - __device__ int getShape(int dim) const { return this->_size[dim].shape; } - __device__ const Size *getSize() const { return _size; } - __device__ T *getData() const { return _data; } + __device__ int getShape(int dim) const { + return this->_size[dim].shape; + } + __device__ const Size *getSize() const { + return _size; + } + __device__ T *getData() const { + return _data; + } protected: Size _size[DIM]; @@ -112,8 +127,12 @@ class PackedSubtensorBase { template class PackedSubtensor : public PackedSubtensorBase { public: - __host__ PackedSubtensor(Tensor &tensor) : PackedSubtensorBase(tensor) {} - __host__ PackedSubtensor(const Tensor &tensor) : PackedSubtensorBase(tensor) {} + __host__ PackedSubtensor(Tensor &tensor) + : PackedSubtensorBase(tensor) { + } + __host__ PackedSubtensor(const Tensor &tensor) + : PackedSubtensorBase(tensor) { + } __device__ Subtensor operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -128,8 +147,12 @@ class PackedSubtensor : public PackedSubtensorBase { template class PackedSubtensor : public PackedSubtensorBase { public: - __host__ PackedSubtensor(Tensor &tensor) : PackedSubtensorBase(tensor) {} - __host__ PackedSubtensor(const Tensor &tensor) : PackedSubtensorBase(tensor) {} + __host__ PackedSubtensor(Tensor &tensor) + : PackedSubtensorBase(tensor) { + } + __host__ PackedSubtensor(const Tensor &tensor) + : PackedSubtensorBase(tensor) { + } __device__ T &operator[](int index) { int64_t offset = index * this->_size[0].stride; @@ -141,6 +164,6 @@ class PackedSubtensor : public PackedSubtensorBase { } }; -} // cuda -} // op -} // ly +} // namespace cuda +} // namespace op +} // namespace libllm diff --git a/src/libllm/cuda/test.cc b/src/libllm/cuda/test.cc index 5c0e1a34..f342f028 100644 --- a/src/libllm/cuda/test.cc +++ b/src/libllm/cuda/test.cc @@ -29,10 +29,10 @@ #include "libllm/cuda/matvec.h" #include "libllm/device.h" #include "libllm/functional.h" -#include "libllm/lut/half.h" -#include "libllm/lut/random.h" -#include "libllm/lut/time.h" #include "libllm/operator_tester.h" +#include "lut/half.h" +#include "lut/random.h" +#include "lut/time.h" #define CONCAT2(l, r) l##r #define CONCAT(l, r) CONCAT2(l, r) diff --git a/src/libllm/device.cc b/src/libllm/device.cc index d9936d2a..20644ceb 100644 --- a/src/libllm/device.cc +++ b/src/libllm/device.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,13 +19,17 @@ #include "libllm/device.h" -#include "libllm/lut/log.h" #include "libllm/cuda/cuda_operators.h" +#include "lut/log.h" namespace libllm { -Device::Device() : _type(Type::kUnknown) {} -Device::Device(Type type) : _type(type) {} +Device::Device() + : _type(Type::kUnknown) { +} +Device::Device(Type type) + : _type(type) { +} Device Device::getCpu() { return Device(Type::kCpu); diff --git a/src/libllm/dtype.cc b/src/libllm/dtype.cc index 1c4dde64..e4361e62 100644 --- a/src/libllm/dtype.cc +++ b/src/libllm/dtype.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,8 +19,8 @@ #include "libllm/dtype.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" +#include "lut/log.h" +#include "lut/strings.h" #ifdef LIBLLM_CUDA_ENABLED #include @@ -36,7 +36,9 @@ constexpr int16_t DType::kFloat16; constexpr int16_t DType::kQInt4x32; constexpr int16_t DType::kInt8; -DType::DType(int16_t dtype) : _dtype(dtype) {} +DType::DType(int16_t dtype) + : _dtype(dtype) { +} template<> DType DType::getTypeImpl() { @@ -69,7 +71,6 @@ DType DType::getTypeImpl() { } #endif - int64_t DType::getTotalSize(int64_t numel) const { switch (_dtype) { case DType::kFloat: diff --git a/src/libllm/dtype.h b/src/libllm/dtype.h index 131832f2..9882b373 100644 --- a/src/libllm/dtype.h +++ b/src/libllm/dtype.h @@ -24,7 +24,7 @@ #include #include -#include "libllm/lut/attributes.h" +#include "lut/attributes.h" namespace libllm { diff --git a/src/libllm/functional.cc b/src/libllm/functional.cc index 5701d1e6..52396b78 100644 --- a/src/libllm/functional.cc +++ b/src/libllm/functional.cc @@ -21,10 +21,10 @@ #include -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" #include "libllm/operators.h" #include "libllm/tensor.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { namespace F { diff --git a/src/libllm/functional.h b/src/libllm/functional.h index 6ecc9235..a339c357 100644 --- a/src/libllm/functional.h +++ b/src/libllm/functional.h @@ -19,9 +19,9 @@ #pragma once -#include "libllm/lut/random.h" -#include "libllm/lut/span.h" #include "libllm/tensor.h" +#include "lut/random.h" +#include "lut/span.h" namespace libllm { namespace F { diff --git a/src/libllm/generator.cc b/src/libllm/generator.cc index 3db213a6..9213256b 100644 --- a/src/libllm/generator.cc +++ b/src/libllm/generator.cc @@ -24,9 +24,9 @@ #include #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" #include "libllm/whisper.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { diff --git a/src/libllm/generator.h b/src/libllm/generator.h index b92addf0..552eedbe 100644 --- a/src/libllm/generator.h +++ b/src/libllm/generator.h @@ -23,9 +23,9 @@ #include #include -#include "libllm/lut/random.h" #include "libllm/model_for_generation.h" #include "libllm/prompt.h" +#include "lut/random.h" namespace libllm { diff --git a/src/libllm/llama.cc b/src/libllm/llama.cc index 32904efe..b4c7a1e9 100644 --- a/src/libllm/llama.cc +++ b/src/libllm/llama.cc @@ -25,11 +25,11 @@ #include "libllm/constants.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/ini_config.h" -#include "libllm/lut/strings.h" #include "libllm/model_for_generation.h" #include "libllm/module.h" +#include "lut/error.h" +#include "lut/ini_config.h" +#include "lut/strings.h" namespace libllm { namespace llama { diff --git a/src/libllm/llama.h b/src/libllm/llama.h index 488ed616..dfad59c4 100644 --- a/src/libllm/llama.h +++ b/src/libllm/llama.h @@ -23,10 +23,10 @@ #include "libllm/constants.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/ini_config.h" #include "libllm/model_for_generation.h" #include "libllm/module.h" +#include "lut/error.h" +#include "lut/ini_config.h" namespace libllm { namespace llama { diff --git a/src/libllm/llama_test.cc b/src/libllm/llama_test.cc index 81f799f7..0241bde9 100644 --- a/src/libllm/llama_test.cc +++ b/src/libllm/llama_test.cc @@ -23,10 +23,10 @@ #include "catch2/catch_amalgamated.hpp" #include "libllm/cpu/fingerprint.h" -#include "libllm/lut/random.h" -#include "libllm/lut/span.h" #include "libllm/tensor.h" #include "libllm/test_helper.h" +#include "lut/random.h" +#include "lut/span.h" namespace libllm { namespace llama { diff --git a/src/libllm/llm.cc b/src/libllm/llm.cc index afef38d3..ed463aaf 100644 --- a/src/libllm/llm.cc +++ b/src/libllm/llm.cc @@ -12,14 +12,14 @@ #include "libllm/dtype.h" #include "libllm/functional.h" #include "libllm/generator.h" -#include "libllm/lut/error.h" -#include "libllm/lut/ini_config.h" -#include "libllm/lut/log.h" -#include "libllm/lut/zip_file.h" #include "libllm/model_for_generation.h" #include "libllm/operators.h" #include "libllm/prompt.h" #include "libllm/tokenizer.h" +#include "lut/error.h" +#include "lut/ini_config.h" +#include "lut/log.h" +#include "lut/zip_file.h" using libllm::Context; using libllm::GenerationConfig; diff --git a/src/libllm/lut/is_debug.cc b/src/libllm/lut/is_debug.cc deleted file mode 100644 index 05935633..00000000 --- a/src/libllm/lut/is_debug.cc +++ /dev/null @@ -1,80 +0,0 @@ -// original file: https://github.com/Tencent/ncnn/blob/056509a/src/cpu.cpp - -#include "libllm/lut/is_debug.h" - -#include -#include -#include - -#if defined _WIN32 -#include -#endif - -#if defined __ANDROID__ || defined __linux__ -#include -#include -#include -#endif - -#if defined __APPLE__ -#include -#include -#endif - -namespace lut { - -bool isDebug() -{ -#if defined _WIN32 - return IsDebuggerPresent(); -#elif defined __ANDROID__ || defined __linux__ - // https://stackoverflow.com/questions/3596781/how-to-detect-if-the-current-process-is-being-run-by-gdb - int status_fd = open("/proc/self/status", O_RDONLY); - if (status_fd == -1) - return false; - - char buf[4096]; - ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1); - close(status_fd); - - if (num_read <= 0) - return false; - - buf[num_read] = '\0'; - const char tracerPidString[] = "TracerPid:"; - const char* tracer_pid_ptr = strstr(buf, tracerPidString); - if (!tracer_pid_ptr) - return false; - - for (const char* ch = tracer_pid_ptr + sizeof(tracerPidString) - 1; ch <= buf + num_read; ++ch) - { - if (isspace(*ch)) - continue; - - return isdigit(*ch) != 0 && *ch != '0'; - } - - return false; -#elif defined __APPLE__ - // https://stackoverflow.com/questions/2200277/detecting-debugger-on-mac-os-x - struct kinfo_proc info; - info.kp_proc.p_flag = 0; - - int mib[4]; - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PID; - mib[3] = getpid(); - - size_t size = sizeof(info); - sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); - - return ((info.kp_proc.p_flag & P_TRACED) != 0); -#else - // unknown platform :( - fprintf(stderr, "unknown platform!\n"); - return false; -#endif -} - -} // namespace lut \ No newline at end of file diff --git a/src/libllm/model_for_generation.cc b/src/libllm/model_for_generation.cc index 74b56b7e..db57f084 100644 --- a/src/libllm/model_for_generation.cc +++ b/src/libllm/model_for_generation.cc @@ -21,12 +21,12 @@ #include "libllm/constants.h" #include "libllm/llama.h" -#include "libllm/lut/error.h" -#include "libllm/lut/path.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/zip_file.h" #include "libllm/qwen.h" #include "libllm/whisper.h" +#include "lut/error.h" +#include "lut/path.h" +#include "lut/strings.h" +#include "lut/zip_file.h" namespace libllm { diff --git a/src/libllm/model_for_generation.h b/src/libllm/model_for_generation.h index 74c3dc3c..5aa06eac 100644 --- a/src/libllm/model_for_generation.h +++ b/src/libllm/model_for_generation.h @@ -20,11 +20,11 @@ #pragma once #include "libllm/context.h" -#include "libllm/lut/zip_file.h" #include "libllm/prompt.h" #include "libllm/state_map.h" #include "libllm/tensor.h" #include "libllm/tokenizer.h" +#include "lut/zip_file.h" namespace libllm { diff --git a/src/libllm/module.cc b/src/libllm/module.cc index 345f8cb6..2d37e66c 100644 --- a/src/libllm/module.cc +++ b/src/libllm/module.cc @@ -22,8 +22,8 @@ #include #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { @@ -318,9 +318,6 @@ void Conv1D::initParameters(lut::Random *generator, DType weightType) { Tensor Conv1D::forward(const Tensor &input) const { Tensor x = F::unfold(input, _kernelSize, _stride); - if (getCtx().getDebugMode()) { - F::print(x); - } if (input.getDim() >= 2) { x = F::matmul(x, _w.transpose(0, 1)); } else { diff --git a/src/libllm/module.h b/src/libllm/module.h index 1d20e76c..decca908 100644 --- a/src/libllm/module.h +++ b/src/libllm/module.h @@ -22,9 +22,9 @@ #include #include "libllm/context.h" -#include "libllm/lut/random.h" #include "libllm/state_map.h" #include "libllm/tensor.h" +#include "lut/random.h" #include "tensor.h" namespace libllm { diff --git a/src/libllm/module_test.cc b/src/libllm/module_test.cc index a19f14b8..95deb0d1 100644 --- a/src/libllm/module_test.cc +++ b/src/libllm/module_test.cc @@ -21,10 +21,10 @@ #include "catch2/catch_amalgamated.hpp" #include "libllm/cpu/fingerprint.h" -#include "libllm/lut/random.h" -#include "libllm/lut/span.h" #include "libllm/tensor.h" #include "libllm/test_helper.h" +#include "lut/random.h" +#include "lut/span.h" namespace libllm { diff --git a/src/libllm/mp.cc b/src/libllm/mp.cc index e932e0c2..b98d7f72 100644 --- a/src/libllm/mp.cc +++ b/src/libllm/mp.cc @@ -22,7 +22,7 @@ #include #include -#include "libllm/lut/range.h" +#include "lut/range.h" namespace libllm { diff --git a/src/libllm/mp.h b/src/libllm/mp.h index 7fec8f45..f667d6a9 100644 --- a/src/libllm/mp.h +++ b/src/libllm/mp.h @@ -21,7 +21,7 @@ #include -#include "libllm/lut/range.h" +#include "lut/range.h" namespace libllm { diff --git a/src/libllm/mp_openmp.cc b/src/libllm/mp_openmp.cc index 78e6c7a5..cb74a468 100644 --- a/src/libllm/mp_openmp.cc +++ b/src/libllm/mp_openmp.cc @@ -27,9 +27,9 @@ #include #include -#include "libllm/lut/log.h" -#include "libllm/lut/range.h" -#include "libllm/lut/thread_pool.h" +#include "lut/log.h" +#include "lut/range.h" +#include "lut/thread_pool.h" namespace libllm { diff --git a/src/libllm/mp_thread_pool.cc b/src/libllm/mp_thread_pool.cc index 6d767a19..60ff45ed 100644 --- a/src/libllm/mp_thread_pool.cc +++ b/src/libllm/mp_thread_pool.cc @@ -23,10 +23,10 @@ #include #include -#include "libllm/lut/log.h" -#include "libllm/lut/range.h" -#include "libllm/lut/thread_pool.h" #include "libllm/mp.h" +#include "lut/log.h" +#include "lut/range.h" +#include "lut/thread_pool.h" namespace libllm { diff --git a/src/libllm/operator_tester.cc b/src/libllm/operator_tester.cc index 8b84ab72..4cf08099 100644 --- a/src/libllm/operator_tester.cc +++ b/src/libllm/operator_tester.cc @@ -23,10 +23,10 @@ #include "libllm/device.h" #include "libllm/functional.h" -#include "libllm/lut/attributes.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/time.h" #include "libllm/operators.h" +#include "lut/attributes.h" +#include "lut/strings.h" +#include "lut/time.h" #define CONCAT2(l, r) l##r #define CONCAT(l, r) CONCAT2(l, r) @@ -421,9 +421,6 @@ bool OperatorTester::testUnfold() { x = _op->cast(x, DType::kFloat); x = _op->to(Device::getCpu(), x); - F::print(x); - F::print(xr); - return F::allClose(x, xr, _rtol, _atol); } diff --git a/src/libllm/operator_tester.h b/src/libllm/operator_tester.h index 822b46fc..353ebf1a 100644 --- a/src/libllm/operator_tester.h +++ b/src/libllm/operator_tester.h @@ -20,8 +20,8 @@ #pragma once #include "libllm/device.h" -#include "libllm/lut/attributes.h" #include "libllm/operators.h" +#include "lut/attributes.h" namespace libllm { diff --git a/src/libllm/operators.cc b/src/libllm/operators.cc index 111c4b57..d477b651 100644 --- a/src/libllm/operators.cc +++ b/src/libllm/operators.cc @@ -26,10 +26,10 @@ #include "libllm/cpu/cpu_operators.h" #include "libllm/cpu/kernel/interface.h" #include "libllm/cuda/cuda_operators.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/thread_pool.h" #include "libllm/mp.h" +#include "lut/error.h" +#include "lut/strings.h" +#include "lut/thread_pool.h" namespace libllm { diff --git a/src/libllm/operators.h b/src/libllm/operators.h index 8594989f..4ed7af07 100644 --- a/src/libllm/operators.h +++ b/src/libllm/operators.h @@ -22,9 +22,9 @@ #include #include "libllm/device.h" -#include "libllm/lut/random.h" -#include "libllm/lut/thread_pool.h" #include "libllm/tensor.h" +#include "lut/random.h" +#include "lut/thread_pool.h" namespace libllm { diff --git a/src/libllm/qwen.h b/src/libllm/qwen.h index dcdc53e8..8e4f80d2 100644 --- a/src/libllm/qwen.h +++ b/src/libllm/qwen.h @@ -22,8 +22,8 @@ #include #include "libllm/llama.h" -#include "libllm/lut/ini_config.h" #include "libllm/model_for_generation.h" +#include "lut/ini_config.h" namespace libllm { namespace qwen { diff --git a/src/libllm/state_map.cc b/src/libllm/state_map.cc index fcddc5e3..c1d437fe 100644 --- a/src/libllm/state_map.cc +++ b/src/libllm/state_map.cc @@ -20,9 +20,9 @@ #include "libllm/state_map.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/time.h" +#include "lut/error.h" +#include "lut/strings.h" +#include "lut/time.h" namespace libllm { diff --git a/src/libllm/state_map.h b/src/libllm/state_map.h index 2b553eef..3ab33770 100644 --- a/src/libllm/state_map.h +++ b/src/libllm/state_map.h @@ -23,8 +23,8 @@ #include #include -#include "libllm/lut/reader.h" #include "libllm/tensor.h" +#include "lut/reader.h" namespace libllm { diff --git a/src/libllm/tensor.cc b/src/libllm/tensor.cc index 68c2098f..7327a027 100644 --- a/src/libllm/tensor.cc +++ b/src/libllm/tensor.cc @@ -26,8 +26,8 @@ #include "libllm/cpu/cpu_tensor_data.h" #include "libllm/cpu/view.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { diff --git a/src/libllm/tensor.h b/src/libllm/tensor.h index ee99c47a..6ca30e3a 100644 --- a/src/libllm/tensor.h +++ b/src/libllm/tensor.h @@ -26,9 +26,9 @@ #include "libllm/device.h" #include "libllm/dtype.h" -#include "libllm/lut/fixed_array.h" -#include "libllm/lut/reader.h" -#include "libllm/lut/span.h" +#include "lut/fixed_array.h" +#include "lut/reader.h" +#include "lut/span.h" namespace libllm { @@ -51,8 +51,10 @@ class Tensor { /// @param shape pointer to TensorShape. /// @param data pointer to TensorData. /// @return The Tensor created. - static Tensor - create(std::shared_ptr shape, std::shared_ptr data, int64_t offset = 0); + static Tensor create( + std::shared_ptr shape, + std::shared_ptr data, + int64_t offset = 0); // constructor and destructor. Tensor(); diff --git a/src/libllm/test_helper.h b/src/libllm/test_helper.h index 7d7e6010..e1e0c597 100644 --- a/src/libllm/test_helper.h +++ b/src/libllm/test_helper.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,12 +19,12 @@ #pragma once -#include "libllm/tensor.h" +#include "libllm/cpu/fingerprint.h" #include "libllm/module.h" +#include "libllm/tensor.h" #include "libllm/test_helper.h" -#include "libllm/cpu/fingerprint.h" -#include "libllm/lut/random.h" -#include "libllm/lut/span.h" +#include "lut/random.h" +#include "lut/span.h" namespace libllm { @@ -72,9 +72,12 @@ class ModuleTester { /// @param ref the reference data. bool allClose(Tensor a, lut::Span ref) const; - - DType getWeightType() const { return _weightType; } - Device getDevice() const { return _device; } + DType getWeightType() const { + return _weightType; + } + Device getDevice() const { + return _device; + } protected: Device _device; diff --git a/src/libllm/test_main.cc b/src/libllm/test_main.cc index 92e51cb5..c95949c9 100644 --- a/src/libllm/test_main.cc +++ b/src/libllm/test_main.cc @@ -20,8 +20,8 @@ #include "../../third_party/catch2/catch_amalgamated.hpp" #include "libllm/cpu/kernel/interface.h" #include "libllm/operators.h" -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" +#include "lut/error.h" +#include "lut/log.h" int main(int argc, char **argv) { libllm::initOperators(); diff --git a/src/libllm/tokenizer.cc b/src/libllm/tokenizer.cc index d1a9757f..e071a47b 100644 --- a/src/libllm/tokenizer.cc +++ b/src/libllm/tokenizer.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,12 +19,11 @@ #include "libllm/tokenizer.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" #include "libllm/bpe_config.h" #include "libllm/bpe_encoder.h" #include "libllm/bpe_model.h" - +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { @@ -47,7 +46,8 @@ class BPETokenizer : public Tokenizer { BPETokenizer(); }; -BPETokenizer::BPETokenizer() {} +BPETokenizer::BPETokenizer() { +} std::unique_ptr BPETokenizer::fromStream(lut::Reader *reader, BPEConfig config) { std::unique_ptr tokenizer{new BPETokenizer()}; @@ -69,8 +69,7 @@ const Vocab *BPETokenizer::getVocab() const { // -- class Tokenizer ---------- std::shared_ptr Tokenizer::fromPackage(lut::ZipFile *package) { - std::shared_ptr ini = lut::IniConfig::fromStream( - package->open(ConfigFile).get()); + std::shared_ptr ini = lut::IniConfig::fromStream(package->open(ConfigFile).get()); lut::IniSection tokenizerSection = ini->getSection("tokenizer"); std::string type = tokenizerSection.getString("type"); diff --git a/src/libllm/tokenizer.h b/src/libllm/tokenizer.h index 0199de56..7f8cc26a 100644 --- a/src/libllm/tokenizer.h +++ b/src/libllm/tokenizer.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,8 +20,8 @@ #pragma once #include "libllm/vocab.h" -#include "libllm/lut/ini_config.h" -#include "libllm/lut/zip_file.h" +#include "lut/ini_config.h" +#include "lut/zip_file.h" namespace libllm { @@ -32,9 +32,9 @@ class Tokenizer { // create an instance of Tokenizer from model package. static std::shared_ptr fromPackage(lut::ZipFile *package); - + virtual ~Tokenizer() = default; - + // encode the input string to a list of token_ids. virtual std::vector encode(const std::string &s) const = 0; diff --git a/src/libllm/wave.cc b/src/libllm/wave.cc index b05a8363..75486a54 100644 --- a/src/libllm/wave.cc +++ b/src/libllm/wave.cc @@ -20,7 +20,7 @@ #include "libllm/wave.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" +#include "lut/error.h" namespace libllm { diff --git a/src/libllm/wave.h b/src/libllm/wave.h index 5eb1b044..c2d5bf2c 100644 --- a/src/libllm/wave.h +++ b/src/libllm/wave.h @@ -21,8 +21,8 @@ #include -#include "libllm/lut/span.h" #include "libllm/tensor.h" +#include "lut/span.h" namespace libllm { diff --git a/src/libllm/whisper.cc b/src/libllm/whisper.cc index 6ad77fe6..abbfc88a 100644 --- a/src/libllm/whisper.cc +++ b/src/libllm/whisper.cc @@ -23,8 +23,8 @@ #include "libllm/constants.h" #include "libllm/functional.h" -#include "libllm/lut/error.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/strings.h" namespace libllm { namespace whisper { diff --git a/src/libllm/whisper.h b/src/libllm/whisper.h index 263d0465..4627840e 100644 --- a/src/libllm/whisper.h +++ b/src/libllm/whisper.h @@ -21,9 +21,9 @@ #include -#include "libllm/lut/ini_config.h" #include "libllm/model_for_generation.h" #include "libllm/module.h" +#include "lut/ini_config.h" namespace libllm { namespace whisper { diff --git a/src/lut/CMakeLists.txt b/src/lut/CMakeLists.txt new file mode 100644 index 00000000..4ff8983e --- /dev/null +++ b/src/lut/CMakeLists.txt @@ -0,0 +1,48 @@ +set(lut_SOURCES + "internal/log.cc" + "internal/sprintf.cc" + "base64.cc" + "error.cc" + "flags.cc" + "half.cc" + "ini_config.cc" + "is_debug.cc" + "path.cc" + "random.cc" + "reader.cc" + "strings.cc" + "time.cc" + "thread_pool.cc" + "zip_file.cc") + +set(lut_test_SOURCES + "path_test.cc" + "strings_test.cc") + +# OS specific code +if(WIN32) + set(lut_SOURCES + ${lut_SOURCES} + "path_windows.cc" + "platform_windows.cc" + "shared_library_windows.cc") +endif() +if(UNIX) + set(lut_SOURCES + ${lut_SOURCES} + "platform_linux.cc" + "shared_library_linux.cc") +endif() +if(UNIX AND APPLE) + set(lut_SOURCES ${lut_SOURCES} "path_darwin.cc") +endif() +if(UNIX AND NOT APPLE) + set(lut_SOURCES ${lut_SOURCES} "path_linux.cc") +endif() + +add_library(lut STATIC ${lut_SOURCES}) +set_target_properties(lut PROPERTIES CXX_VISIBILITY_PRESET hidden) +target_include_directories(lut PRIVATE "..") + +add_library(lut_test OBJECT ${lut_test_SOURCES}) +target_include_directories(lut_test PRIVATE "..") diff --git a/src/libllm/lut/attributes.h b/src/lut/attributes.h similarity index 100% rename from src/libllm/lut/attributes.h rename to src/lut/attributes.h diff --git a/src/libllm/lut/base64.cc b/src/lut/base64.cc similarity index 99% rename from src/libllm/lut/base64.cc rename to src/lut/base64.cc index 846fee4b..ac523355 100644 --- a/src/libllm/lut/base64.cc +++ b/src/lut/base64.cc @@ -15,15 +15,15 @@ // original file: // https://android.googlesource.com/platform/external/wpa_supplicant/+/4d8c3c1ca334d1319decf3e2c5d2be0cf472e3f9/base64.c -#include #include +#include #include #include #include #include -#include "libllm/lut/span.h" +#include "lut/span.h" static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/src/libllm/lut/base64.h b/src/lut/base64.h similarity index 97% rename from src/libllm/lut/base64.h rename to src/lut/base64.h index 4cec405b..c0735f01 100644 --- a/src/libllm/lut/base64.h +++ b/src/lut/base64.h @@ -24,7 +24,7 @@ #include #include -#include "libllm/lut/span.h" +#include "lut/span.h" namespace lut { diff --git a/src/libllm/lut/c_ptr.h b/src/lut/c_ptr.h similarity index 83% rename from src/libllm/lut/c_ptr.h rename to src/lut/c_ptr.h index 9ec67618..4031c66e 100644 --- a/src/libllm/lut/c_ptr.h +++ b/src/lut/c_ptr.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,8 +20,9 @@ #pragma once #include -#include "libllm/lut/noncopyable.h" -#include "libllm/lut/log.h" + +#include "lut/log.h" +#include "lut/noncopyable.h" namespace lut { @@ -40,28 +41,38 @@ class c_ptr : private NonCopyable { T *Release(); // get the pointer - T *get() { return _ptr; } - const T *get() const { return _ptr; } + T *get() { + return _ptr; + } + const T *get() const { + return _ptr; + } // get the pointer to this pointer. - T **get_pp() { CHECK(_deleter); return &_ptr; } + T **get_pp() { + CHECK(_deleter); + return &_ptr; + } private: T *_ptr; std::function _deleter; }; - template -inline c_ptr::c_ptr(): _ptr(nullptr), _deleter(nullptr) {} +inline c_ptr::c_ptr() + : _ptr(nullptr), + _deleter(nullptr) { +} template -inline c_ptr::c_ptr(T *ptr, std::function deleter): - _ptr(ptr), - _deleter(deleter) {} +inline c_ptr::c_ptr(T *ptr, std::function deleter) + : _ptr(ptr), + _deleter(deleter) { +} template -inline c_ptr::c_ptr(c_ptr &&auto_cptr) noexcept : - _ptr(auto_cptr._ptr), - _deleter(auto_cptr._deleter) { +inline c_ptr::c_ptr(c_ptr &&auto_cptr) noexcept + : _ptr(auto_cptr._ptr), + _deleter(auto_cptr._deleter) { auto_cptr._ptr = nullptr; } template @@ -92,4 +103,4 @@ inline T *c_ptr::Release() { return ptr; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/error.cc b/src/lut/error.cc similarity index 98% rename from src/libllm/lut/error.cc rename to src/lut/error.cc index 22baa452..17db6338 100644 --- a/src/libllm/lut/error.cc +++ b/src/lut/error.cc @@ -17,7 +17,7 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/error.h" +#include "lut/error.h" namespace lut { diff --git a/src/libllm/lut/error.h b/src/lut/error.h similarity index 100% rename from src/libllm/lut/error.h rename to src/lut/error.h diff --git a/src/libllm/lut/fixed_array.h b/src/lut/fixed_array.h similarity index 90% rename from src/libllm/lut/fixed_array.h rename to src/lut/fixed_array.h index c2b2dba6..c41e8c4c 100644 --- a/src/libllm/lut/fixed_array.h +++ b/src/lut/fixed_array.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,16 +19,19 @@ #pragma once -#include "libllm/lut/internal/base_array.h" +#include "lut/internal/base_array.h" namespace lut { template class FixedArray : public internal::BaseArray { public: - FixedArray() noexcept : internal::BaseArray() {} - FixedArray(int64_t size) noexcept : - internal::BaseArray(size ? new T[size] : nullptr, size) {} + FixedArray() noexcept + : internal::BaseArray() { + } + FixedArray(int64_t size) noexcept + : internal::BaseArray(size ? new T[size] : nullptr, size) { + } ~FixedArray() noexcept { delete[] internal::BaseArray::_ptr; internal::BaseArray::_ptr = nullptr; @@ -67,4 +70,4 @@ class FixedArray : public internal::BaseArray { } }; -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/flags.cc b/src/lut/flags.cc similarity index 96% rename from src/libllm/lut/flags.cc rename to src/lut/flags.cc index 87bbc593..b414a692 100644 --- a/src/libllm/lut/flags.cc +++ b/src/lut/flags.cc @@ -17,11 +17,11 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/flags.h" +#include "lut/flags.h" -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/log.h" +#include "lut/strings.h" namespace lut { diff --git a/src/libllm/lut/flags.h b/src/lut/flags.h similarity index 95% rename from src/libllm/lut/flags.h rename to src/lut/flags.h index 77dfb84a..5bd727d2 100644 --- a/src/libllm/lut/flags.h +++ b/src/lut/flags.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -23,12 +23,12 @@ #include #include #include -#include "libllm/lut/span.h" -#include "libllm/lut/strings.h" -#include "libllm/lut/log.h" -namespace lut { +#include "lut/log.h" +#include "lut/span.h" +#include "lut/strings.h" +namespace lut { // command line option parser. // Example: @@ -77,5 +77,4 @@ class Flags::Parser { virtual std::string getType() const = 0; }; -} // namespace lut - +} // namespace lut diff --git a/src/libllm/lut/half.cc b/src/lut/half.cc similarity index 67% rename from src/libllm/lut/half.cc rename to src/lut/half.cc index 1722a04e..8b404c7a 100644 --- a/src/libllm/lut/half.cc +++ b/src/lut/half.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,7 +17,7 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/half.h" +#include "lut/half.h" namespace lut { @@ -46,12 +46,12 @@ union FP16 { }; FP16 float_to_half_fast3(FP32 f) { - constexpr FP32 f32infty = { 255 << 23 }; - constexpr FP32 f16infty = { 31 << 23 }; - constexpr FP32 magic = { 15 << 23 }; + constexpr FP32 f32infty = {255 << 23}; + constexpr FP32 f16infty = {31 << 23}; + constexpr FP32 magic = {15 << 23}; constexpr uint sign_mask = 0x80000000u; - constexpr uint round_mask = ~0xfffu; - FP16 o = { 0 }; + constexpr uint round_mask = ~0xfffu; + FP16 o = {0}; uint sign = f.u & sign_mask; f.u ^= sign; @@ -61,15 +61,15 @@ FP16 float_to_half_fast3(FP32 f) { // 0x80000000. Important if you want fast straight SSE2 code // (since there's no unsigned PCMPGTD). - if (f.u >= f32infty.u) { // Inf or NaN (all exponent bits set) - o.u = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf - } else { // (De)normalized number or zero + if (f.u >= f32infty.u) { // Inf or NaN (all exponent bits set) + o.u = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf + } else { // (De)normalized number or zero f.u &= round_mask; f.f *= magic.f; f.u -= round_mask; - if (f.u > f16infty.u) f.u = f16infty.u; // Clamp to signed infinity if overflowed + if (f.u > f16infty.u) f.u = f16infty.u; // Clamp to signed infinity if overflowed - o.u = f.u >> 13; // Take the bits! + o.u = f.u >> 13; // Take the bits! } o.u |= sign >> 16; @@ -78,23 +78,23 @@ FP16 float_to_half_fast3(FP32 f) { // from half->float code - just for verification. FP32 half_to_float(FP16 h) { - constexpr FP32 magic = { 113 << 23 }; - constexpr uint shifted_exp = 0x7c00 << 13; // exponent mask after shift + constexpr FP32 magic = {113 << 23}; + constexpr uint shifted_exp = 0x7c00 << 13; // exponent mask after shift FP32 o; - o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits - uint exp = shifted_exp & o.u; // just the exponent - o.u += (127 - 15) << 23; // exponent adjust + o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits + uint exp = shifted_exp & o.u; // just the exponent + o.u += (127 - 15) << 23; // exponent adjust // handle exponent special cases - if (exp == shifted_exp) { // Inf/NaN? - o.u += (128 - 16) << 23; // extra exp adjust - } else if (exp == 0) { // Zero/Denormal? - o.u += 1 << 23; // extra exp adjust - o.f -= magic.f; // renormalize + if (exp == shifted_exp) { // Inf/NaN? + o.u += (128 - 16) << 23; // extra exp adjust + } else if (exp == 0) { // Zero/Denormal? + o.u += 1 << 23; // extra exp adjust + o.f -= magic.f; // renormalize } - o.u |= (h.u & 0x8000) << 16; // sign bit + o.u |= (h.u & 0x8000) << 16; // sign bit return o; } @@ -108,4 +108,4 @@ float cvtsh_ss(uint16_t v) { return half_to_float({v}).f; } -} // namespace libllm \ No newline at end of file +} // namespace lut \ No newline at end of file diff --git a/src/libllm/lut/half.h b/src/lut/half.h similarity index 100% rename from src/libllm/lut/half.h rename to src/lut/half.h diff --git a/src/libllm/lut/ini_config.cc b/src/lut/ini_config.cc similarity index 95% rename from src/libllm/lut/ini_config.cc rename to src/lut/ini_config.cc index 114d0b3e..a7338441 100644 --- a/src/libllm/lut/ini_config.cc +++ b/src/lut/ini_config.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,19 +17,21 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/ini_config.h" +#include "lut/ini_config.h" #include -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/reader.h" -#include "libllm/lut/strings.h" + +#include "lut/error.h" +#include "lut/log.h" +#include "lut/reader.h" +#include "lut/strings.h" namespace lut { // -- class IniConfig ---------------------------------------------------------- -IniConfig::IniConfig() {} +IniConfig::IniConfig() { +} std::shared_ptr IniConfig::fromFile(const std::string &filename) { std::shared_ptr config{new IniConfig()}; @@ -129,7 +131,7 @@ std::string IniConfig::parseHeader(const std::string &s) { if (!isHeader(s)) { throw AbortedError(lut::sprintf("invalid line: %s", s)); } - + std::string name = s.substr(1, s.size() - 2); name = lut::trim(name); if (name.empty()) { @@ -153,10 +155,11 @@ std::pair IniConfig::parseKeyValue(const std::string & return std::make_pair(key, value); } - // -- class IniSection --------------------------------------------------------- -IniSection::IniSection(const Path &iniDir) : _iniDir(iniDir) {} +IniSection::IniSection(const Path &iniDir) + : _iniDir(iniDir) { +} std::string IniSection::getString(const std::string &key) const { auto it = _kvTable.find(key); @@ -214,4 +217,4 @@ std::vector IniSection::getIntArray(const std::string &key) const { return intValues; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/ini_config.h b/src/lut/ini_config.h similarity index 93% rename from src/libllm/lut/ini_config.h rename to src/lut/ini_config.h index 5e6c112e..47627077 100644 --- a/src/libllm/lut/ini_config.h +++ b/src/lut/ini_config.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -24,8 +24,9 @@ #include #include #include -#include "libllm/lut/path.h" -#include "libllm/lut/reader.h" + +#include "lut/path.h" +#include "lut/reader.h" namespace lut { @@ -49,14 +50,16 @@ class IniConfig { bool hasSection(const std::string §ion) const; // Get filename. - const std::string &getFilename() const { return _filename; } + const std::string &getFilename() const { + return _filename; + } - private: + private: std::string _filename; // map (section, key) -> value std::map _table; - + IniConfig(); void readStream(lut::Reader *reader); @@ -92,7 +95,9 @@ class IniSection { bool hasKey(const std::string &key) const; // name of the section. - const std::string &getName() const { return _name; } + const std::string &getName() const { + return _name; + } private: std::unordered_map _kvTable; @@ -102,4 +107,4 @@ class IniSection { IniSection(const Path &iniDir); }; -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/internal/base_array.h b/src/lut/internal/base_array.h similarity index 100% rename from src/libllm/lut/internal/base_array.h rename to src/lut/internal/base_array.h diff --git a/src/libllm/lut/internal/log.cc b/src/lut/internal/log.cc similarity index 87% rename from src/libllm/lut/internal/log.cc rename to src/lut/internal/log.cc index 70a0b760..c48a0334 100644 --- a/src/libllm/lut/internal/log.cc +++ b/src/lut/internal/log.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,11 +17,12 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/internal/log.h" +#include "lut/internal/log.h" #include #include #include + #include #include @@ -30,9 +31,7 @@ namespace internal { LogSeverity gLogLevel = LogSeverity::kINFO; -LogWrapper::LogWrapper(LogSeverity severity, - const char *source_file, - int source_line) +LogWrapper::LogWrapper(LogSeverity severity, const char *source_file, int source_line) : severity_(severity), source_line_(source_line) { const char *s = strrchr(source_file, '/'); @@ -51,12 +50,7 @@ LogWrapper::~LogWrapper() { std::string message = os_.str(); if (message.empty()) message = default_message_; - printf("%s %s %s:%d] %s\n", - Severity(), - Time(), - source_file_, - source_line_, - message.c_str()); + printf("%s %s %s:%d] %s\n", Severity(), Time(), source_file_, source_line_, message.c_str()); if (severity_ == LogSeverity::kFATAL) { abort(); @@ -65,7 +59,7 @@ LogWrapper::~LogWrapper() { const char *LogWrapper::Time() { time_t now = time(nullptr); - + std::strftime(time_, sizeof(time_), "%FT%TZ", std::gmtime(&now)); return time_; } @@ -94,8 +88,7 @@ LogWrapper &LogWrapper::DefaultMessage(const char *message) { } } // namespace internal -} // namespace lut - +} // namespace lut namespace lut { @@ -103,5 +96,4 @@ void setLogLevel(LogSeverity level) { internal::gLogLevel = level; } -} // namespace lut - +} // namespace lut diff --git a/src/libllm/lut/internal/log.h b/src/lut/internal/log.h similarity index 72% rename from src/libllm/lut/internal/log.h rename to src/lut/internal/log.h index dd3b4197..83d6a678 100644 --- a/src/libllm/lut/internal/log.h +++ b/src/lut/internal/log.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,9 +17,9 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#pragma once +#pragma once -#include "libllm/lut/log.h" +#include "lut/log.h" namespace lut { namespace internal { @@ -34,8 +34,11 @@ class LogWrapper { LogWrapper(LogWrapper &) = delete; LogWrapper &operator=(LogWrapper &) = delete; - template - LogWrapper& operator<<(const T &value) { os_ << value; return *this; } + template + LogWrapper &operator<<(const T &value) { + os_ << value; + return *this; + } // set the default message to LogWrapper. If no message appended, it will // log the `message` instead @@ -57,30 +60,34 @@ class LogWrapper { // log wrappers for each severity class LogWrapperkDEBUG : public LogWrapper { public: - LogWrapperkDEBUG(const char *source_file, int source_line) : - LogWrapper(LogSeverity::kDEBUG, source_file, source_line) {} + LogWrapperkDEBUG(const char *source_file, int source_line) + : LogWrapper(LogSeverity::kDEBUG, source_file, source_line) { + } }; class LogWrapperkINFO : public LogWrapper { public: - LogWrapperkINFO(const char *source_file, int source_line) : - LogWrapper(LogSeverity::kINFO, source_file, source_line) {} + LogWrapperkINFO(const char *source_file, int source_line) + : LogWrapper(LogSeverity::kINFO, source_file, source_line) { + } }; class LogWrapperkWARN : public LogWrapper { public: - LogWrapperkWARN(const char *source_file, int source_line) : - LogWrapper(LogSeverity::kWARN, source_file, source_line) {} + LogWrapperkWARN(const char *source_file, int source_line) + : LogWrapper(LogSeverity::kWARN, source_file, source_line) { + } }; class LogWrapperkERROR : public LogWrapper { public: - LogWrapperkERROR(const char *source_file, int source_line) : - LogWrapper(LogSeverity::kERROR, source_file, source_line) {} + LogWrapperkERROR(const char *source_file, int source_line) + : LogWrapper(LogSeverity::kERROR, source_file, source_line) { + } }; class LogWrapperkFATAL : public LogWrapper { public: - LogWrapperkFATAL(const char *source_file, int source_line) : - LogWrapper(LogSeverity::kFATAL, source_file, source_line) {} + LogWrapperkFATAL(const char *source_file, int source_line) + : LogWrapper(LogSeverity::kFATAL, source_file, source_line) { + } }; } // namespace internal -} // namespace lut - +} // namespace lut diff --git a/src/libllm/lut/internal/sprintf.cc b/src/lut/internal/sprintf.cc similarity index 96% rename from src/libllm/lut/internal/sprintf.cc rename to src/lut/internal/sprintf.cc index 7e21bd4c..c0523348 100644 --- a/src/libllm/lut/internal/sprintf.cc +++ b/src/lut/internal/sprintf.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,11 +17,13 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/internal/sprintf.h" +#include "lut/internal/sprintf.h" -#include #include -#include "libllm/lut/log.h" + +#include + +#include "lut/log.h" namespace lut { namespace internal { @@ -49,10 +51,9 @@ int readDigit(const char **ppch, char *buf, int buf_size) { *pbuf = '\0'; *ppch = pch; - if (strlen(buf) > 3) - return kSprintfMaxWeight; + if (strlen(buf) > 3) return kSprintfMaxWeight; int n = atoi(buf); - + return n < kSprintfMaxWeight ? n : kSprintfMaxWeight; } @@ -160,4 +161,4 @@ char sprintfParseFormat(const char **pp_string, std::stringstream &ss) { } } // namespace internal -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/internal/sprintf.h b/src/lut/internal/sprintf.h similarity index 100% rename from src/libllm/lut/internal/sprintf.h rename to src/lut/internal/sprintf.h diff --git a/src/lut/is_debug.cc b/src/lut/is_debug.cc new file mode 100644 index 00000000..6aa1a3db --- /dev/null +++ b/src/lut/is_debug.cc @@ -0,0 +1,74 @@ +// original file: https://github.com/Tencent/ncnn/blob/056509a/src/cpu.cpp + +#include "lut/is_debug.h" + +#include +#include +#include + +#if defined _WIN32 +#include +#endif + +#if defined __ANDROID__ || defined __linux__ +#include +#include +#include +#endif + +#if defined __APPLE__ +#include +#include +#endif + +namespace lut { + +bool isDebug() { +#if defined _WIN32 + return IsDebuggerPresent(); +#elif defined __ANDROID__ || defined __linux__ + // https://stackoverflow.com/questions/3596781/how-to-detect-if-the-current-process-is-being-run-by-gdb + int status_fd = open("/proc/self/status", O_RDONLY); + if (status_fd == -1) return false; + + char buf[4096]; + ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1); + close(status_fd); + + if (num_read <= 0) return false; + + buf[num_read] = '\0'; + const char tracerPidString[] = "TracerPid:"; + const char* tracer_pid_ptr = strstr(buf, tracerPidString); + if (!tracer_pid_ptr) return false; + + for (const char* ch = tracer_pid_ptr + sizeof(tracerPidString) - 1; ch <= buf + num_read; ++ch) { + if (isspace(*ch)) continue; + + return isdigit(*ch) != 0 && *ch != '0'; + } + + return false; +#elif defined __APPLE__ + // https://stackoverflow.com/questions/2200277/detecting-debugger-on-mac-os-x + struct kinfo_proc info; + info.kp_proc.p_flag = 0; + + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + + size_t size = sizeof(info); + sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); + + return ((info.kp_proc.p_flag & P_TRACED) != 0); +#else + // unknown platform :( + fprintf(stderr, "unknown platform!\n"); + return false; +#endif +} + +} // namespace lut \ No newline at end of file diff --git a/src/libllm/lut/is_debug.h b/src/lut/is_debug.h similarity index 100% rename from src/libllm/lut/is_debug.h rename to src/lut/is_debug.h diff --git a/src/libllm/lut/log.h b/src/lut/log.h similarity index 65% rename from src/libllm/lut/log.h rename to src/lut/log.h index 580423b9..10c8681f 100644 --- a/src/libllm/lut/log.h +++ b/src/lut/log.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -21,27 +21,28 @@ #include -#define LOG(severity) \ - if (lut::internal::gLogLevel > lut::LogSeverity::k ## severity) {} \ - else lut::internal::LogWrapperk ## severity(__FILE__, __LINE__) -#define NOT_IMPL() { LOG(FATAL) << "not implemented"; abort(); } +#define LOG(severity) \ + if (lut::internal::gLogLevel > lut::LogSeverity::k##severity) { \ + } else \ + lut::internal::LogWrapperk##severity(__FILE__, __LINE__) +#define NOT_IMPL() \ + { \ + LOG(FATAL) << "not implemented"; \ + abort(); \ + } // CHECK macro conflicts with catch2 #define CHECK(cond) \ - if (cond) {} else LOG(FATAL).DefaultMessage("Check " #cond " failed.") + if (cond) { \ + } else \ + LOG(FATAL).DefaultMessage("Check " #cond " failed.") namespace lut { -enum class LogSeverity { - kDEBUG = 0, - kINFO = 1, - kWARN = 2, - kERROR = 4, - kFATAL = 3 -}; +enum class LogSeverity { kDEBUG = 0, kINFO = 1, kWARN = 2, kERROR = 4, kFATAL = 3 }; void setLogLevel(LogSeverity level); -} // namespace lut +} // namespace lut -#include "libllm/lut/internal/log.h" +#include "lut/internal/log.h" diff --git a/src/libllm/lut/noncopyable.h b/src/lut/noncopyable.h similarity index 100% rename from src/libllm/lut/noncopyable.h rename to src/lut/noncopyable.h diff --git a/src/libllm/lut/optional.h b/src/lut/optional.h similarity index 100% rename from src/libllm/lut/optional.h rename to src/lut/optional.h diff --git a/src/libllm/lut/path.cc b/src/lut/path.cc similarity index 91% rename from src/libllm/lut/path.cc rename to src/lut/path.cc index 1a2ab467..7d4e58a5 100644 --- a/src/libllm/lut/path.cc +++ b/src/lut/path.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,16 +17,20 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/path.h" +#include "lut/path.h" -#include "libllm/lut/log.h" -#include "libllm/lut/platform.h" -#include "libllm/lut/strings.h" +#include "lut/log.h" +#include "lut/platform.h" +#include "lut/strings.h" namespace lut { -Path::Path(const std::string &path) : _path(normPath(path)) {} -Path::Path(std::string &&path): _path(normPath(path)) {} +Path::Path(const std::string &path) + : _path(normPath(path)) { +} +Path::Path(std::string &&path) + : _path(normPath(path)) { +} Path Path::dirname() const { const char *delim = getPathDelim(); @@ -99,4 +103,4 @@ std::wstring Path::wstring() const { return toWide(_path); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/path.h b/src/lut/path.h similarity index 100% rename from src/libllm/lut/path.h rename to src/lut/path.h diff --git a/src/libllm/lut/path_darwin.cc b/src/lut/path_darwin.cc similarity index 92% rename from src/libllm/lut/path_darwin.cc rename to src/lut/path_darwin.cc index 9d1b4169..fef4d560 100644 --- a/src/libllm/lut/path_darwin.cc +++ b/src/lut/path_darwin.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,14 +17,14 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/path.h" - #include #include #include -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" + +#include "lut/error.h" +#include "lut/log.h" +#include "lut/path.h" +#include "lut/strings.h" namespace lut { @@ -58,5 +58,4 @@ std::string Path::normPath(const std::string &path) { return path; } - -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/path_linux.cc b/src/lut/path_linux.cc similarity index 93% rename from src/libllm/lut/path_linux.cc rename to src/lut/path_linux.cc index df37c425..6247291f 100644 --- a/src/libllm/lut/path_linux.cc +++ b/src/lut/path_linux.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,15 +17,16 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/path.h" +#include "lut/path.h" #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" + +#include "lut/log.h" +#include "lut/strings.h" extern char *program_invocation_name; @@ -54,4 +55,4 @@ std::string Path::normPath(const std::string &path) { return path; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/path_test.cc b/src/lut/path_test.cc similarity index 96% rename from src/libllm/lut/path_test.cc rename to src/lut/path_test.cc index c5d41e37..36573ad5 100644 --- a/src/libllm/lut/path_test.cc +++ b/src/lut/path_test.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,9 +17,10 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "catch2/catch_amalgamated.hpp" -#include "libllm/lut/path.h" -#include "libllm/lut/platform.h" +#include "lut/path.h" + +#include "../../third_party/catch2/catch_amalgamated.hpp" +#include "lut/platform.h" namespace lut { diff --git a/src/libllm/lut/path_windows.cc b/src/lut/path_windows.cc similarity index 84% rename from src/libllm/lut/path_windows.cc rename to src/lut/path_windows.cc index 7bbf3c92..67e7b81a 100644 --- a/src/libllm/lut/path_windows.cc +++ b/src/lut/path_windows.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,10 +19,9 @@ #include -#include "libllm/lut/path.h" - -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" +#include "lut/log.h" +#include "lut/path.h" +#include "lut/strings.h" namespace lut { @@ -30,10 +29,10 @@ Path Path::currentModulePath() { char filename[MAX_PATH + 1]; HMODULE hm = NULL; - BOOL b = GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (LPCWSTR)¤tModulePath, - &hm); + BOOL b = GetModuleHandleExW( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCWSTR)¤tModulePath, + &hm); CHECK(b); DWORD dw = GetModuleFileNameA(hm, filename, sizeof(filename)); @@ -70,4 +69,4 @@ std::string Path::normPath(const std::string &path) { return replace(path, "/", "\\"); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/platform.h b/src/lut/platform.h similarity index 95% rename from src/libllm/lut/platform.h rename to src/lut/platform.h index 78cfcb66..292c6df5 100644 --- a/src/libllm/lut/platform.h +++ b/src/lut/platform.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -21,7 +21,8 @@ #pragma once #include -#include "libllm/lut/attributes.h" + +#include "lut/attributes.h" namespace lut { @@ -29,4 +30,4 @@ void *alloc32ByteAlignedMem(int64_t nbytes); void free32ByteAlignedMem(void *); const char *getPathDelim(); -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/platform_linux.cc b/src/lut/platform_linux.cc similarity index 96% rename from src/libllm/lut/platform_linux.cc rename to src/lut/platform_linux.cc index a25072d0..61968b73 100644 --- a/src/libllm/lut/platform_linux.cc +++ b/src/lut/platform_linux.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,10 +17,10 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/platform.h" - #include +#include "lut/platform.h" + namespace lut { void *alloc32ByteAlignedMem(int64_t size) { @@ -38,4 +38,4 @@ const char *getPathDelim() { return "/"; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/platform_windows.cc b/src/lut/platform_windows.cc similarity index 96% rename from src/libllm/lut/platform_windows.cc rename to src/lut/platform_windows.cc index 7e3e9bcd..a6b9e180 100644 --- a/src/libllm/lut/platform_windows.cc +++ b/src/lut/platform_windows.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,10 +17,10 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/platform.h" - #include +#include "lut/platform.h" + namespace lut { bool isAvx512Available() { @@ -43,4 +43,4 @@ const char *getPathDelim() { return "\\"; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/pool.h b/src/lut/pool.h similarity index 93% rename from src/libllm/lut/pool.h rename to src/lut/pool.h index 1b5dc715..924e8145 100644 --- a/src/libllm/lut/pool.h +++ b/src/lut/pool.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,7 +20,8 @@ #pragma once #include -#include "libllm/lut/log.h" + +#include "lut/log.h" namespace lut { @@ -34,10 +35,10 @@ class Pool { Pool(); ~Pool(); - // Allocate a class T with constructor parameter args + // Allocate a class T with constructor parameter args T *alloc(); - // Allocate a class T with constructor parameter args + // Allocate a class T with constructor parameter args void free(T *pointer); void free(const T *pointer); @@ -58,9 +59,10 @@ class Pool { int _currentOffset; }; - template -Pool::Pool() : _currentBlock(0), _currentOffset(0) { +Pool::Pool() + : _currentBlock(0), + _currentOffset(0) { T *block = reinterpret_cast(::operator new(sizeof(T) * BLOCK_SIZE)); _blocks.push_back(block); } @@ -120,4 +122,4 @@ int Pool::getNumAllocated() const { return _currentBlock * BLOCK_SIZE + _currentOffset - _free.size(); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/random.cc b/src/lut/random.cc similarity index 97% rename from src/libllm/lut/random.cc rename to src/lut/random.cc index e2a3151f..b0237fd6 100644 --- a/src/libllm/lut/random.cc +++ b/src/lut/random.cc @@ -17,11 +17,11 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/random.h" +#include "lut/random.h" #include -#include "libllm/lut/log.h" +#include "lut/log.h" namespace lut { diff --git a/src/libllm/lut/random.h b/src/lut/random.h similarity index 98% rename from src/libllm/lut/random.h rename to src/lut/random.h index 3daa57ab..c79ed630 100644 --- a/src/libllm/lut/random.h +++ b/src/lut/random.h @@ -21,7 +21,7 @@ #include -#include "libllm/lut/span.h" +#include "lut/span.h" namespace lut { diff --git a/src/libllm/lut/range.h b/src/lut/range.h similarity index 100% rename from src/libllm/lut/range.h rename to src/lut/range.h diff --git a/src/libllm/lut/reader.cc b/src/lut/reader.cc similarity index 92% rename from src/libllm/lut/reader.cc rename to src/lut/reader.cc index b83dc064..6d7c4ed1 100644 --- a/src/libllm/lut/reader.cc +++ b/src/lut/reader.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,20 +17,21 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/reader.h" +#include "lut/reader.h" -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/log.h" +#include "lut/strings.h" namespace lut { // -- class Reader ------------------------------------------------------------------------- Reader::Reader(int buffer_size) - : _buffer(buffer_size), + : _buffer(buffer_size), _w(0), - _r(0) {} + _r(0) { +} int64_t Reader::readFromBuffer(Span dest) { int64_t n = std::min(static_cast(dest.size()), _w - _r); @@ -55,7 +56,7 @@ void Reader::readSpan(Span span) { Span::iterator it = span.begin(); int64_t bytesRead = readFromBuffer(span); - + while (bytesRead < static_cast(span.size())) { int64_t n = readNextBuffer(); if (!n) { @@ -80,7 +81,7 @@ std::string Reader::readLine() { std::string line; int8_t ch; - for (; ; ) { + for (;;) { if (_w - _r == 0) { int64_t n = readNextBuffer(); @@ -99,7 +100,9 @@ std::string Reader::readLine() { // -- class ReadableFile --------------------------------------------------------------------------- -ReadableFile::ReadableFile() : _fp(nullptr) {} +ReadableFile::ReadableFile() + : _fp(nullptr) { +} ReadableFile::~ReadableFile() { if (_fp) { fclose(_fp); @@ -128,4 +131,4 @@ int64_t ReadableFile::readData(Span buffer) { return n; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/reader.h b/src/lut/reader.h similarity index 97% rename from src/libllm/lut/reader.h rename to src/lut/reader.h index ca1a1297..268b6145 100644 --- a/src/libllm/lut/reader.h +++ b/src/lut/reader.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,15 +19,16 @@ #pragma once -#include #include +#include + #include #include #include #include -#include "libllm/lut/noncopyable.h" -#include "libllm/lut/span.h" +#include "lut/noncopyable.h" +#include "lut/span.h" namespace lut { @@ -41,7 +42,7 @@ class Reader { // create the BufferedReader instance by Reader and the buffer size. Reader(int buffer_size = kDefaultBufferSize); - + // read `buffer.size()` bytes and fill the `buffer`. Throw if EOF reached or other errors occured. void readSpan(Span buffer); @@ -80,7 +81,7 @@ template T Reader::readValue() { T value; readSpan(lut::makeSpan(reinterpret_cast(&value), sizeof(T))); - + return value; } @@ -107,6 +108,4 @@ class ReadableFile : public Reader { ReadableFile(); }; -} // namespace lut - - +} // namespace lut diff --git a/src/libllm/lut/shared_library.h b/src/lut/shared_library.h similarity index 93% rename from src/libllm/lut/shared_library.h rename to src/lut/shared_library.h index e75c9354..20827813 100644 --- a/src/libllm/lut/shared_library.h +++ b/src/lut/shared_library.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -19,13 +19,14 @@ #pragma once -#include #include -#include "libllm/lut/noncopyable.h" +#include + +#include "lut/noncopyable.h" namespace lut { -// Dynamic library loader. It stores the instance of dynamic library and +// Dynamic library loader. It stores the instance of dynamic library and // supports get function from it. // // Example: @@ -45,7 +46,7 @@ class SharedLibrary : private NonCopyable { // get function by name. return nullptr if function not found template - T *getFunc(const std::string& name) { + T *getFunc(const std::string &name) { return reinterpret_cast(getFuncPtr(name)); } @@ -56,7 +57,7 @@ class SharedLibrary : private NonCopyable { // get raw function pointer by name. throw error if function not exist or // other errors occured - void *getFuncPtr(const std::string& name); + void *getFuncPtr(const std::string &name); }; -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/shared_library_linux.cc b/src/lut/shared_library_linux.cc similarity index 88% rename from src/libllm/lut/shared_library_linux.cc rename to src/lut/shared_library_linux.cc index b1581868..2d195847 100644 --- a/src/libllm/lut/shared_library_linux.cc +++ b/src/lut/shared_library_linux.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,16 +17,16 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/shared_library.h" +#include #include #include -#include -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/path.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/log.h" +#include "lut/path.h" +#include "lut/shared_library.h" +#include "lut/strings.h" namespace lut { @@ -43,7 +43,9 @@ class SharedLibrary::Impl { Impl(); }; -SharedLibrary::Impl::Impl() : _module(nullptr) {} +SharedLibrary::Impl::Impl() + : _module(nullptr) { +} SharedLibrary::Impl::~Impl() { if (_module) { dlclose(_module); @@ -69,7 +71,9 @@ std::unique_ptr SharedLibrary::Impl::open(const std::string impl->_module = dlopen(filename.string().c_str(), RTLD_NOW); if (!impl->_module) { throw AbortedError(lut::sprintf( - "Load library %s failed with message \"%s\"", absFilename.string(), dlerror())); + "Load library %s failed with message \"%s\"", + absFilename.string(), + dlerror())); } } @@ -84,8 +88,10 @@ void *SharedLibrary::Impl::getFuncPtr(const std::string &func_name) { // -- class SharedLibrary ---------- -SharedLibrary::SharedLibrary() {} -SharedLibrary::~SharedLibrary() {} +SharedLibrary::SharedLibrary() { +} +SharedLibrary::~SharedLibrary() { +} std::unique_ptr SharedLibrary::open(const std::string &name) { std::unique_ptr library{new SharedLibrary()}; @@ -96,5 +102,4 @@ void *SharedLibrary::getFuncPtr(const std::string &name) { return _impl->getFuncPtr(name); } - -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/shared_library_windows.cc b/src/lut/shared_library_windows.cc similarity index 86% rename from src/libllm/lut/shared_library_windows.cc rename to src/lut/shared_library_windows.cc index f324c8dd..412c7650 100644 --- a/src/libllm/lut/shared_library_windows.cc +++ b/src/lut/shared_library_windows.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,16 +17,16 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/shared_library.h" +#include #include #include -#include -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" -#include "libllm/lut/path.h" -#include "libllm/lut/strings.h" +#include "lut/error.h" +#include "lut/log.h" +#include "lut/path.h" +#include "lut/shared_library.h" +#include "lut/strings.h" namespace lut { @@ -43,7 +43,9 @@ class SharedLibrary::Impl { Impl(); }; -SharedLibrary::Impl::Impl() : _module(nullptr) {} +SharedLibrary::Impl::Impl() + : _module(nullptr) { +} SharedLibrary::Impl::~Impl() { if (_module) { FreeLibrary(_module); @@ -60,7 +62,7 @@ std::unique_ptr SharedLibrary::Impl::open(const std::string modulePath = modulePath.dirname(); Path absFilename = modulePath / filename; - DWORD code = S_OK; + DWORD code = S_OK; impl->_module = LoadLibraryW(absFilename.wstring().c_str()); if (!impl->_module) { code = GetLastError(); @@ -71,8 +73,8 @@ std::unique_ptr SharedLibrary::Impl::open(const std::string impl->_module = LoadLibraryW(filename.wstring().c_str()); if (!impl->_module) { code = GetLastError(); - throw AbortedError(lut::sprintf( - "Load library %s failed with code 0x%x.", absFilename.string(), code)); + throw AbortedError( + lut::sprintf("Load library %s failed with code 0x%x.", absFilename.string(), code)); } } @@ -87,8 +89,10 @@ void *SharedLibrary::Impl::getFuncPtr(const std::string &func_name) { // -- class SharedLibrary ---------- -SharedLibrary::SharedLibrary() {} -SharedLibrary::~SharedLibrary() {} +SharedLibrary::SharedLibrary() { +} +SharedLibrary::~SharedLibrary() { +} std::unique_ptr SharedLibrary::open(const std::string &name) { std::unique_ptr library{new SharedLibrary()}; @@ -99,4 +103,4 @@ void *SharedLibrary::getFuncPtr(const std::string &name) { return _impl->getFuncPtr(name); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/span.h b/src/lut/span.h similarity index 74% rename from src/libllm/lut/span.h rename to src/lut/span.h index 89cb4b56..a4356fc8 100644 --- a/src/libllm/lut/span.h +++ b/src/lut/span.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,51 +20,56 @@ #pragma once #include -#include "libllm/lut/internal/base_array.h" -#include "libllm/lut/attributes.h" -#include "libllm/lut/fixed_array.h" -#include "libllm/lut/log.h" + +#include "lut/attributes.h" +#include "lut/fixed_array.h" +#include "lut/internal/base_array.h" +#include "lut/log.h" namespace lut { template class Span : public internal::BaseArray { public: - constexpr Span() noexcept : internal::BaseArray() {} + constexpr Span() noexcept + : internal::BaseArray() { + } constexpr Span(T *ptr, typename internal::BaseArray::size_type size) - : internal::BaseArray(ptr, size) {} + : internal::BaseArray(ptr, size) { + } // automatic convert initializer_list to Span. // NOTE: initializer_list should outlives span when using this constructor. // Examples: // Span v = {1, 2, 3}; // WRONG: lifetime of initializer_list is shorter than v; - template ::value, U>::type> - constexpr Span(std::initializer_list< - typename internal::BaseArray::value_type - > v LUT_LIFETIME_BOUND) noexcept - : Span(v.begin(), v.size()) {} + template::value, U>::type> + constexpr Span(std::initializer_list::value_type> v + LUT_LIFETIME_BOUND) noexcept + : Span(v.begin(), v.size()) { + } // automatic convert std::vector to Span. // NOTE: initializer_list should outlives span when using this constructor. // Examples: // Span v = {1, 2, 3}; // WRONG: lifetime of initializer_list is shorter than v; - template ::value, U>::type> + template::value, U>::type> constexpr Span( const std::vector::value_type> &v LUT_LIFETIME_BOUND) noexcept - : Span(v.data(), v.size()) {} + : Span(v.data(), v.size()) { + } // automatic convert std::array to Span. // NOTE: initializer_list should outlives span when using this constructor. // Examples: // Span v = {1, 2, 3}; // WRONG: lifetime of initializer_list is shorter than v; - template ::value, U>::type> - constexpr Span(const std::array< - typename internal::BaseArray::value_type, N> &v LUT_LIFETIME_BOUND) noexcept - : Span(v.data(), v.size()) {} + template< + typename U = T, + std::size_t N, + typename = typename std::enable_if::value, U>::type> + constexpr Span(const std::array::value_type, N> &v + LUT_LIFETIME_BOUND) noexcept + : Span(v.data(), v.size()) { + } constexpr Span subspan( typename internal::BaseArray::size_type pos = 0, @@ -76,15 +81,11 @@ class Span : public internal::BaseArray { }; template -constexpr Span makeSpan( - T *ptr, - typename Span::size_type size) { +constexpr Span makeSpan(T *ptr, typename Span::size_type size) { return Span(ptr, size); } template -constexpr Span makeConstSpan( - const T *ptr, - typename Span::size_type size) { +constexpr Span makeConstSpan(const T *ptr, typename Span::size_type size) { return Span(ptr, size); } template @@ -113,4 +114,4 @@ constexpr Span makeConstSpan(Span v) { return Span(v.data(), v.size()); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/strings.cc b/src/lut/strings.cc similarity index 96% rename from src/libllm/lut/strings.cc rename to src/lut/strings.cc index dc979bf8..09e81d6a 100644 --- a/src/libllm/lut/strings.cc +++ b/src/lut/strings.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,14 +17,16 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/strings.h" +#include "lut/strings.h" #include + #include #include #include -#include "../../../third_party/utfcpp/utfcpp.h" -#include "libllm/lut/log.h" + +#include "../../third_party/utfcpp/utfcpp.h" +#include "lut/log.h" namespace lut { @@ -51,8 +53,6 @@ I findFirstNotMatch(I begin, I end, const char *chars) { } // namespace - - #if !defined(WCHAR_MAX) #error WCHAR_MAX not defined! #endif @@ -149,7 +149,7 @@ std::vector split(const std::string &str, const std::string &delim) fields.emplace_back(str.cbegin() + start, str.cbegin() + pos); start = pos + delim.size(); } - + fields.emplace_back(str.cbegin() + start, str.cend()); return fields; } @@ -197,7 +197,7 @@ bool parseBool(const std::string &s) { std::string replace(const std::string &from, const std::string &old, const std::string &repl) { size_t pos = 0; std::string s = from; - while((pos = s.find(old, pos)) != std::string::npos) { + while ((pos = s.find(old, pos)) != std::string::npos) { s.replace(pos, old.length(), repl); pos += repl.length(); } @@ -227,4 +227,4 @@ std::vector splitUtf8(const std::string &s) { return utf8Chars; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/strings.h b/src/lut/strings.h similarity index 97% rename from src/libllm/lut/strings.h rename to src/lut/strings.h index f3e1b499..1d38ee81 100644 --- a/src/libllm/lut/strings.h +++ b/src/lut/strings.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -24,7 +24,7 @@ #include #include -#include "libllm/lut/internal/sprintf.h" +#include "lut/internal/sprintf.h" namespace lut { @@ -58,4 +58,4 @@ inline std::string sprintf(const std::string &fmt, Args &&...args) { return internal::sprintf0(std::stringstream(), fmt.c_str(), std::forward(args)...); } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/strings_test.cc b/src/lut/strings_test.cc similarity index 98% rename from src/libllm/lut/strings_test.cc rename to src/lut/strings_test.cc index 2a2aafe7..1e7855ff 100644 --- a/src/libllm/lut/strings_test.cc +++ b/src/lut/strings_test.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,8 +17,9 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "catch2/catch_amalgamated.hpp" -#include "libllm/lut/strings.h" +#include "lut/strings.h" + +#include "../../third_party/catch2/catch_amalgamated.hpp" namespace lut { diff --git a/src/libllm/lut/thread_pool.cc b/src/lut/thread_pool.cc similarity index 95% rename from src/libllm/lut/thread_pool.cc rename to src/lut/thread_pool.cc index 23eb9059..b0198a16 100644 --- a/src/libllm/lut/thread_pool.cc +++ b/src/lut/thread_pool.cc @@ -17,7 +17,7 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/thread_pool.h" +#include "lut/thread_pool.h" #include #include @@ -26,9 +26,9 @@ #include #include -#include "concurrentqueue/blockingconcurrentqueue.h" -#include "libllm/lut/error.h" -#include "libllm/lut/log.h" +#include "../../third_party/concurrentqueue/blockingconcurrentqueue.h" +#include "lut/error.h" +#include "lut/log.h" namespace lut { diff --git a/src/libllm/lut/thread_pool.h b/src/lut/thread_pool.h similarity index 98% rename from src/libllm/lut/thread_pool.h rename to src/lut/thread_pool.h index 17c8e80d..61851961 100644 --- a/src/libllm/lut/thread_pool.h +++ b/src/lut/thread_pool.h @@ -22,7 +22,7 @@ #include #include -#include "libllm/lut/range.h" +#include "lut/range.h" namespace lut { diff --git a/src/libllm/lut/time.cc b/src/lut/time.cc similarity index 96% rename from src/libllm/lut/time.cc rename to src/lut/time.cc index 18016e46..3c2dea8a 100644 --- a/src/libllm/lut/time.cc +++ b/src/lut/time.cc @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -17,10 +17,12 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#include "libllm/lut/time.h" -#include +#include "lut/time.h" + #include +#include + namespace lut { double now() { @@ -29,4 +31,4 @@ double now() { return ns / 1000000000.0; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/time.h b/src/lut/time.h similarity index 100% rename from src/libllm/lut/time.h rename to src/lut/time.h diff --git a/src/libllm/lut/zip_file.cc b/src/lut/zip_file.cc similarity index 89% rename from src/libllm/lut/zip_file.cc rename to src/lut/zip_file.cc index e51a0116..d3bb4df2 100644 --- a/src/libllm/lut/zip_file.cc +++ b/src/lut/zip_file.cc @@ -15,15 +15,17 @@ // This file is modified from StoreZipReader in Tencent/ncnn project. // Original file: https://github.com/Tencent/ncnn/blob/master/tools/pnnx/src/storezip.h -#include "libllm/lut/zip_file.h" +#include "lut/zip_file.h" #include + #include #include #include -#include "libllm/lut/error.h" -#include "libllm/lut/reader.h" -#include "libllm/lut/strings.h" + +#include "lut/error.h" +#include "lut/reader.h" +#include "lut/strings.h" #ifdef _MSC_VER #define FSEEK64 _fseeki64 @@ -165,7 +167,7 @@ ZipFile::Impl::~Impl() { close(); } -void ZipFile::Impl::open(const std::string& path) { +void ZipFile::Impl::open(const std::string &path) { close(); _fp = fopen(path.c_str(), "rb"); @@ -176,14 +178,14 @@ void ZipFile::Impl::open(const std::string& path) { while (!feof(_fp)) { // peek signature uint32_t signature; - int nread = fread((char*)&signature, sizeof(signature), 1, _fp); + int nread = fread((char *)&signature, sizeof(signature), 1, _fp); if (nread != 1) break; // fprintf(stderr, "signature = %x\n", signature); if (signature == 0x04034b50) { local_file_header lfh; - size_t n = fread((char*)&lfh, sizeof(lfh), 1, _fp); + size_t n = fread((char *)&lfh, sizeof(lfh), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -199,7 +201,7 @@ void ZipFile::Impl::open(const std::string& path) { // file name std::string name; name.resize(lfh.file_name_length); - n = fread((char*)name.data(), name.size(), 1, _fp); + n = fread((char *)name.data(), name.size(), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -211,12 +213,12 @@ void ZipFile::Impl::open(const std::string& path) { while (extra_offset < lfh.extra_field_length) { uint16_t extra_id; uint16_t extra_size; - size_t n = fread((char*)&extra_id, sizeof(extra_id), 1, _fp); + size_t n = fread((char *)&extra_id, sizeof(extra_id), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } - n = fread((char*)&extra_size, sizeof(extra_size), 1, _fp); + n = fread((char *)&extra_size, sizeof(extra_size), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -230,7 +232,7 @@ void ZipFile::Impl::open(const std::string& path) { // zip64 extra field zip64_extended_extra_field zip64_eef; - n = fread((char*)&zip64_eef, sizeof(zip64_eef), 1, _fp); + n = fread((char *)&zip64_eef, sizeof(zip64_eef), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -258,7 +260,7 @@ void ZipFile::Impl::open(const std::string& path) { fseek(_fp, compressed_size, SEEK_CUR); } else if (signature == 0x02014b50) { central_directory_file_header cdfh; - size_t n = fread((char*)&cdfh, sizeof(cdfh), 1, _fp); + size_t n = fread((char *)&cdfh, sizeof(cdfh), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -273,7 +275,7 @@ void ZipFile::Impl::open(const std::string& path) { fseek(_fp, cdfh.file_comment_length, SEEK_CUR); } else if (signature == 0x06054b50) { end_of_central_directory_record eocdr; - size_t n = fread((char*)&eocdr, sizeof(eocdr), 1, _fp); + size_t n = fread((char *)&eocdr, sizeof(eocdr), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -282,7 +284,7 @@ void ZipFile::Impl::open(const std::string& path) { fseek(_fp, eocdr.comment_length, SEEK_CUR); } else if (signature == 0x06064b50) { zip64_end_of_central_directory_record eocdr64; - size_t n = fread((char*)&eocdr64, sizeof(eocdr64), 1, _fp); + size_t n = fread((char *)&eocdr64, sizeof(eocdr64), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -291,7 +293,7 @@ void ZipFile::Impl::open(const std::string& path) { fseek(_fp, eocdr64.size_of_eocd64_m12 - 44, SEEK_CUR); } else if (signature == 0x07064b50) { zip64_end_of_central_directory_locator eocdl64; - size_t n = fread((char*)&eocdl64, sizeof(eocdl64), 1, _fp); + size_t n = fread((char *)&eocdl64, sizeof(eocdl64), 1, _fp); if (!n) { THROW(Aborted, lut::sprintf("unable to read file %s", path)); } @@ -304,8 +306,8 @@ void ZipFile::Impl::open(const std::string& path) { std::vector ZipFile::Impl::getList() const { std::vector names; for (std::map::const_iterator it = _filemetas.begin(); - it != _filemetas.end(); - ++it) { + it != _filemetas.end(); + ++it) { names.push_back(it->first); } @@ -313,7 +315,7 @@ std::vector ZipFile::Impl::getList() const { } ZipFile::Impl::StoreZipMeta ZipFile::Impl::getFileMeta(const std::string &name) const { - if (_filemetas.find(name) == _filemetas.end()){ + if (_filemetas.find(name) == _filemetas.end()) { THROW(Aborted, lut::sprintf("file \"%s\" not exist in zip.", name)); } @@ -337,11 +339,16 @@ FILE *ZipFile::Impl::getFp() { // class ZipFile::FileReader | // -----------------------------------------------------------------------------------------------+ -ZipFile::FileReader::FileReader() : _fp(nullptr), _pos(0) {} -ZipFile::FileReader::~FileReader() {} +ZipFile::FileReader::FileReader() + : _fp(nullptr), + _pos(0) { +} +ZipFile::FileReader::~FileReader() { +} -void ZipFile::FileReader::init(std::shared_ptr zipImpl, - const std::string &filename) { +void ZipFile::FileReader::init( + std::shared_ptr zipImpl, + const std::string &filename) { _fileMeta = zipImpl->getFileMeta(filename); _fp = zipImpl->getFp(); } @@ -381,4 +388,4 @@ std::shared_ptr ZipFile::open(const std::string &filename) const { return reader; } -} // namespace lut +} // namespace lut diff --git a/src/libllm/lut/zip_file.h b/src/lut/zip_file.h similarity index 96% rename from src/libllm/lut/zip_file.h rename to src/lut/zip_file.h index f87b2466..693d03a8 100644 --- a/src/libllm/lut/zip_file.h +++ b/src/lut/zip_file.h @@ -7,7 +7,7 @@ // restriction, including without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // @@ -20,7 +20,8 @@ #pragma once #include -#include "libllm/lut/reader.h" + +#include "lut/reader.h" namespace lut { @@ -41,4 +42,4 @@ class ZipFile { ZipFile() = default; }; -} // namespace lut +} // namespace lut diff --git a/third_party/utfcpp/utfcpp.h b/third_party/utfcpp/utfcpp.h index df1188ac..92235ad5 100644 --- a/third_party/utfcpp/utfcpp.h +++ b/third_party/utfcpp/utfcpp.h @@ -22,411 +22,383 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - #ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #include + #include #include -#include "../../src/libllm/lut/error.h" -namespace lut -{ -namespace utf8 -{ +#include "lut/error.h" + +namespace lut { +namespace utf8 { // Helper code - not intended to be directly called by the library users. May be changed at any time -namespace internal -{ +namespace internal { // Unicode constants // Leading (high) surrogates: 0xd800 - 0xdbff // Trailing (low) surrogates: 0xdc00 - 0xdfff -const uint16_t LEAD_SURROGATE_MIN = 0xd800u; -const uint16_t LEAD_SURROGATE_MAX = 0xdbffu; +const uint16_t LEAD_SURROGATE_MIN = 0xd800u; +const uint16_t LEAD_SURROGATE_MAX = 0xdbffu; const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u; const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu; -const uint16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) -const uint32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN +const uint16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10) +const uint32_t + SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN // Maximum valid value for a Unicode code point -const uint32_t CODE_POINT_MAX = 0x0010ffffu; +const uint32_t CODE_POINT_MAX = 0x0010ffffu; template -inline uint8_t mask8(octet_type oc) -{ - return static_cast(0xff & oc); +inline uint8_t mask8(octet_type oc) { + return static_cast(0xff & oc); } template -inline uint16_t mask16(u16_type oc) -{ - return static_cast(0xffff & oc); +inline uint16_t mask16(u16_type oc) { + return static_cast(0xffff & oc); } template -inline bool is_trail(octet_type oc) -{ - return ((utf8::internal::mask8(oc) >> 6) == 0x2); +inline bool is_trail(octet_type oc) { + return ((utf8::internal::mask8(oc) >> 6) == 0x2); } -template -inline bool is_lead_surrogate(u16 cp) -{ - return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX); +template +inline bool is_lead_surrogate(u16 cp) { + return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX); } -template -inline bool is_trail_surrogate(u16 cp) -{ - return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); +template +inline bool is_trail_surrogate(u16 cp) { + return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); } -template -inline bool is_surrogate(u16 cp) -{ - return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); +template +inline bool is_surrogate(u16 cp) { + return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); } -template -inline bool is_code_point_valid(u32 cp) -{ - return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp)); +template +inline bool is_code_point_valid(u32 cp) { + return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp)); } -template -inline typename std::iterator_traits::difference_type -sequence_length(octet_iterator lead_it) -{ - uint8_t lead = utf8::internal::mask8(*lead_it); - if (lead < 0x80) - return 1; - else if ((lead >> 5) == 0x6) - return 2; - else if ((lead >> 4) == 0xe) - return 3; - else if ((lead >> 3) == 0x1e) - return 4; - else - return 0; +template +inline typename std::iterator_traits::difference_type sequence_length( + octet_iterator lead_it) { + uint8_t lead = utf8::internal::mask8(*lead_it); + if (lead < 0x80) + return 1; + else if ((lead >> 5) == 0x6) + return 2; + else if ((lead >> 4) == 0xe) + return 3; + else if ((lead >> 3) == 0x1e) + return 4; + else + return 0; } -template -inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) -{ - if (cp < 0x80) { - if (length != 1) - return true; - } - else if (cp < 0x800) { - if (length != 2) - return true; - } - else if (cp < 0x10000) { - if (length != 3) - return true; - } - - return false; +template +inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) { + if (cp < 0x80) { + if (length != 1) return true; + } else if (cp < 0x800) { + if (length != 2) return true; + } else if (cp < 0x10000) { + if (length != 3) return true; + } + + return false; } -enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; +enum utf_error { + UTF8_OK, + NOT_ENOUGH_ROOM, + INVALID_LEAD, + INCOMPLETE_SEQUENCE, + OVERLONG_SEQUENCE, + INVALID_CODE_POINT +}; /// Helper for get_sequence_x -template -utf_error increase_safely(octet_iterator& it, octet_iterator end) -{ - if (++it == end) - return NOT_ENOUGH_ROOM; +template +utf_error increase_safely(octet_iterator& it, octet_iterator end) { + if (++it == end) return NOT_ENOUGH_ROOM; - if (!utf8::internal::is_trail(*it)) - return INCOMPLETE_SEQUENCE; + if (!utf8::internal::is_trail(*it)) return INCOMPLETE_SEQUENCE; - return UTF8_OK; + return UTF8_OK; } -#define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;} +#define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) \ + { \ + utf_error ret = increase_safely(IT, END); \ + if (ret != UTF8_OK) return ret; \ + } /// get_sequence_x functions decode utf-8 sequences of the length x -template -utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point) -{ - if (it == end) - return NOT_ENOUGH_ROOM; +template +utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point) { + if (it == end) return NOT_ENOUGH_ROOM; - code_point = utf8::internal::mask8(*it); + code_point = utf8::internal::mask8(*it); - return UTF8_OK; + return UTF8_OK; } -template -utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point) -{ - if (it == end) - return NOT_ENOUGH_ROOM; +template +utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point) { + if (it == end) return NOT_ENOUGH_ROOM; - code_point = utf8::internal::mask8(*it); + code_point = utf8::internal::mask8(*it); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); + code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); - return UTF8_OK; + return UTF8_OK; } -template -utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point) -{ - if (it == end) - return NOT_ENOUGH_ROOM; - - code_point = utf8::internal::mask8(*it); +template +utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point) { + if (it == end) return NOT_ENOUGH_ROOM; - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + code_point = utf8::internal::mask8(*it); - code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); - code_point += (*it) & 0x3f; + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - return UTF8_OK; + code_point += (*it) & 0x3f; + + return UTF8_OK; } -template -utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point) -{ - if (it == end) - return NOT_ENOUGH_ROOM; +template +utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point) { + if (it == end) return NOT_ENOUGH_ROOM; - code_point = utf8::internal::mask8(*it); + code_point = utf8::internal::mask8(*it); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); + code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point += (utf8::internal::mask8(*it) << 6) & 0xfff; + code_point += (utf8::internal::mask8(*it) << 6) & 0xfff; - UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) + UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end) - code_point += (*it) & 0x3f; + code_point += (*it) & 0x3f; - return UTF8_OK; + return UTF8_OK; } #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR -template -utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point) -{ - if (it == end) - return NOT_ENOUGH_ROOM; - - // Save the original value of it so we can go back in case of failure - // Of course, it does not make much sense with i.e. stream iterators - octet_iterator original_it = it; - - uint32_t cp = 0; - // Determine the sequence length based on the lead octet - typedef typename std::iterator_traits::difference_type octet_difference_type; - const octet_difference_type length = utf8::internal::sequence_length(it); - - // Get trail octets and calculate the code point - utf_error err = UTF8_OK; - switch (length) { - case 0: - return INVALID_LEAD; - case 1: - err = utf8::internal::get_sequence_1(it, end, cp); - break; - case 2: - err = utf8::internal::get_sequence_2(it, end, cp); - break; - case 3: - err = utf8::internal::get_sequence_3(it, end, cp); - break; - case 4: - err = utf8::internal::get_sequence_4(it, end, cp); - break; - } - - if (err == UTF8_OK) { - // Decoding succeeded. Now, security checks... - if (utf8::internal::is_code_point_valid(cp)) { - if (!utf8::internal::is_overlong_sequence(cp, length)){ - // Passed! Return here. - code_point = cp; - ++it; - return UTF8_OK; - } - else - err = OVERLONG_SEQUENCE; - } - else - err = INVALID_CODE_POINT; - } - - // Failure branch - restore the original value of the iterator - it = original_it; - return err; +template +utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point) { + if (it == end) return NOT_ENOUGH_ROOM; + + // Save the original value of it so we can go back in case of failure + // Of course, it does not make much sense with i.e. stream iterators + octet_iterator original_it = it; + + uint32_t cp = 0; + // Determine the sequence length based on the lead octet + typedef typename std::iterator_traits::difference_type octet_difference_type; + const octet_difference_type length = utf8::internal::sequence_length(it); + + // Get trail octets and calculate the code point + utf_error err = UTF8_OK; + switch (length) { + case 0: + return INVALID_LEAD; + case 1: + err = utf8::internal::get_sequence_1(it, end, cp); + break; + case 2: + err = utf8::internal::get_sequence_2(it, end, cp); + break; + case 3: + err = utf8::internal::get_sequence_3(it, end, cp); + break; + case 4: + err = utf8::internal::get_sequence_4(it, end, cp); + break; + } + + if (err == UTF8_OK) { + // Decoding succeeded. Now, security checks... + if (utf8::internal::is_code_point_valid(cp)) { + if (!utf8::internal::is_overlong_sequence(cp, length)) { + // Passed! Return here. + code_point = cp; + ++it; + return UTF8_OK; + } else + err = OVERLONG_SEQUENCE; + } else + err = INVALID_CODE_POINT; + } + + // Failure branch - restore the original value of the iterator + it = original_it; + return err; } -template +template inline utf_error validate_next(octet_iterator& it, octet_iterator end) { - uint32_t ignored; - return utf8::internal::validate_next(it, end, ignored); + uint32_t ignored; + return utf8::internal::validate_next(it, end, ignored); } // Internal implementation of both checked and unchecked append() function // This function will be invoked by the overloads below, as they will know // the octet_type. -template +template octet_iterator append(uint32_t cp, octet_iterator result) { - if (cp < 0x80) // one octet - *(result++) = static_cast(cp); - else if (cp < 0x800) { // two octets - *(result++) = static_cast((cp >> 6) | 0xc0); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else if (cp < 0x10000) { // three octets - *(result++) = static_cast((cp >> 12) | 0xe0); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - else { // four octets - *(result++) = static_cast((cp >> 18) | 0xf0); - *(result++) = static_cast(((cp >> 12) & 0x3f)| 0x80); - *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); - *(result++) = static_cast((cp & 0x3f) | 0x80); - } - return result; + if (cp < 0x80) // one octet + *(result++) = static_cast(cp); + else if (cp < 0x800) { // two octets + *(result++) = static_cast((cp >> 6) | 0xc0); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } else if (cp < 0x10000) { // three octets + *(result++) = static_cast((cp >> 12) | 0xe0); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } else { // four octets + *(result++) = static_cast((cp >> 18) | 0xf0); + *(result++) = static_cast(((cp >> 12) & 0x3f) | 0x80); + *(result++) = static_cast(((cp >> 6) & 0x3f) | 0x80); + *(result++) = static_cast((cp & 0x3f) | 0x80); + } + return result; } // One of the following overloads will be invoked from the API calls // A simple (but dangerous) case: the caller appends byte(s) to a char array inline char* append(uint32_t cp, char* result) { - return append(cp, result); + return append(cp, result); } // Hopefully, most common case: the caller uses back_inserter // i.e. append(cp, std::back_inserter(str)); template -std::back_insert_iterator append - (uint32_t cp, std::back_insert_iterator result) { - return append, - typename container_type::value_type>(cp, result); +std::back_insert_iterator append( + uint32_t cp, + std::back_insert_iterator result) { + return append, typename container_type::value_type>( + cp, + result); } // The caller uses some other kind of output operator - not covered above // Note that in this case we are not able to determine octet_type // so we assume it's uint_8; that can cause a conversion warning if we are wrong. -template +template octet_iterator append(uint32_t cp, octet_iterator result) { - return append(cp, result); + return append(cp, result); } -} // namespace internal -} // namespace utf8 +} // namespace internal +} // namespace utf8 -namespace utf8 -{ +namespace utf8 { /// The library API - functions intended to be called by the users -template -octet_iterator append(uint32_t cp, octet_iterator result) -{ - if (!utf8::internal::is_code_point_valid(cp)) { - throw AbortedError(sprintf("invalid code point %d", cp)); - } +template +octet_iterator append(uint32_t cp, octet_iterator result) { + if (!utf8::internal::is_code_point_valid(cp)) { + throw AbortedError(sprintf("invalid code point %d", cp)); + } - return internal::append(cp, result); + return internal::append(cp, result); } -template -uint32_t next(octet_iterator& it, octet_iterator end) -{ - uint32_t cp = 0; - internal::utf_error err_code = utf8::internal::validate_next(it, end, cp); - switch (err_code) { - case internal::UTF8_OK : - break; - case internal::NOT_ENOUGH_ROOM : - throw AbortedError("not enough space"); - case internal::INVALID_LEAD : - case internal::INCOMPLETE_SEQUENCE : - case internal::OVERLONG_SEQUENCE : - throw AbortedError(sprintf("invalid utf-8 %d", int(*it))); - case internal::INVALID_CODE_POINT : - throw AbortedError(sprintf("invalid code point %d", cp)); - } - return cp; +template +uint32_t next(octet_iterator& it, octet_iterator end) { + uint32_t cp = 0; + internal::utf_error err_code = utf8::internal::validate_next(it, end, cp); + switch (err_code) { + case internal::UTF8_OK: + break; + case internal::NOT_ENOUGH_ROOM: + throw AbortedError("not enough space"); + case internal::INVALID_LEAD: + case internal::INCOMPLETE_SEQUENCE: + case internal::OVERLONG_SEQUENCE: + throw AbortedError(sprintf("invalid utf-8 %d", int(*it))); + case internal::INVALID_CODE_POINT: + throw AbortedError(sprintf("invalid code point %d", cp)); + } + return cp; } -template -void utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) -{ - u16bit_iterator it = start; - octet_iterator next_result = result; - while (it != end) { - uint32_t cp = utf8::internal::mask16(*it++); - // Take care of surrogate pairs first - if (utf8::internal::is_lead_surrogate(cp)) { - if (it != end) { - uint32_t trail_surrogate = utf8::internal::mask16(*it++); - if (utf8::internal::is_trail_surrogate(trail_surrogate)) { - cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; - } else { - throw AbortedError(lut::sprintf( - "invalid utf16 trail surrogate %d in position %d", - trail_surrogate, - it - start)); - } - } else { - throw AbortedError(lut::sprintf( - "invalid utf16 lead surrogate %d in position %d", cp, it - start)); - } - } else if (utf8::internal::is_trail_surrogate(cp)) { - throw AbortedError(lut::sprintf( - "lone trail surrogate %d in position %d", cp, it - start)); +template +void utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) { + u16bit_iterator it = start; + octet_iterator next_result = result; + while (it != end) { + uint32_t cp = utf8::internal::mask16(*it++); + // Take care of surrogate pairs first + if (utf8::internal::is_lead_surrogate(cp)) { + if (it != end) { + uint32_t trail_surrogate = utf8::internal::mask16(*it++); + if (utf8::internal::is_trail_surrogate(trail_surrogate)) { + cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; + } else { + throw AbortedError(lut::sprintf( + "invalid utf16 trail surrogate %d in position %d", + trail_surrogate, + it - start)); } - - result = utf8::append(cp, result); + } else { + throw AbortedError( + lut::sprintf("invalid utf16 lead surrogate %d in position %d", cp, it - start)); + } + } else if (utf8::internal::is_trail_surrogate(cp)) { + throw AbortedError(lut::sprintf("lone trail surrogate %d in position %d", cp, it - start)); } + + result = utf8::append(cp, result); + } } -template -void utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) -{ - uint32_t cp = 0; - while (start < end) { - uint32_t cp = utf8::next(start, end); - if (cp > 0xffff) { //make a surrogate pair - *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); - *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); - } - else - *result++ = static_cast(cp); - } +template +void utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) { + uint32_t cp = 0; + while (start < end) { + uint32_t cp = utf8::next(start, end); + if (cp > 0xffff) { // make a surrogate pair + *result++ = static_cast((cp >> 10) + internal::LEAD_OFFSET); + *result++ = static_cast((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); + } else + *result++ = static_cast(cp); + } } -template -void utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result) -{ - while (start != end) { - result = utf8::append(*(start++), result); - } +template +void utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result) { + while (start != end) { + result = utf8::append(*(start++), result); + } } -template -void utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result) -{ - for (; start < end; ++result) { - uint32_t c = utf8::next(start, end); - *result = c; - } +template +void utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result) { + for (; start < end; ++result) { + uint32_t c = utf8::next(start, end); + *result = c; + } } } // namespace utf8 } // namespace lut -#endif //header guard +#endif // header guard