Skip to content

Commit

Permalink
merge from dev branch (#101)
Browse files Browse the repository at this point in the history
* remove tensor.h

* rm src/tensor.cpp

* new api :: consistent_variable

* update basic_flat_shape

* check bound

* fix windows build
  • Loading branch information
lgarithm authored Jul 24, 2021
1 parent 5c092a7 commit 6490bc5
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
steps:
- uses: actions/checkout@v1

- run: cmake . -DBUILD_TESTS=1 -DBUILD_GTEST=1 -DUSE_STRICT=0 -DBUILD_LIB=1
- run: cmake . -DBUILD_TESTS=1 -DBUILD_GTEST=1 -DUSE_STRICT=0 -DBUILD_LIB=0
- run: cmake --build . --config Release
- run: ctest -C Release
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)

OPTION(BUILD_LIB "Build c lib." OFF)
IF(BUILD_LIB)
ADD_LIBRARY(stdtensor src/tensor.cpp)
INSTALL(TARGETS stdtensor ARCHIVE DESTINATION lib)
INCLUDE(cmake/lib.cmake)
BUILD_STDML_TENSOR_LIB(tensor)
ENDIF()

INSTALL(DIRECTORY include DESTINATION .)
Expand Down Expand Up @@ -55,3 +55,7 @@ ENDIF()
IF(BUILD_EXAMPLES)
INCLUDE(cmake/examples.cmake)
ENDIF()

IF(BUILD_RELEASE)
INCLUDE(cmake/pack.cmake)
ENDIF()
1 change: 0 additions & 1 deletion cmake/examples.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ FUNCTION(ADD_C_EXAMPLE target)
ENDFUNCTION()

ADD_CPP_EXAMPLE(example-1 examples/example_1.cpp)
ADD_C_EXAMPLE(example-c-api examples/example_c_api.c)

OPTION(USE_OPENCV "Build examples with libopencv-dev" OFF)

Expand Down
9 changes: 9 additions & 0 deletions cmake/lib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FUNCTION(BUILD_STDML_TENSOR_LIB target)
# libtensor will be shared by python/haskell bindings
FILE(GLOB_RECURSE srcs ${CMAKE_SOURCE_DIR}/src/stdml/*)
ADD_LIBRARY(${target} ${srcs})
SET_TARGET_PROPERTIES(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
TARGET_LINK_LIBRARIES(${target} dl)
SET_PROPERTY(TARGET ${target} PROPERTY CXX_STANDARD 17)
INSTALL(TARGETS ${target} ARCHIVE DESTINATION lib)
ENDFUNCTION()
11 changes: 11 additions & 0 deletions cmake/pack.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "lg4869@outlook.com")
SET(CPACK_GENERATOR "TGZ")
SET(CPACK_PACKAGE_DIRECTORY ${CMAKE_SOURCE_DIR}/release)
SET(CPACK_PACKAGE_VERSION
"latest"
CACHE STRING "Release version.")
IF(BUILD_DEB)
SET(CPACK_GENERATOR "TGZ;DEB")
# SET(CPACK_GENERATOR "TGZ;RPM")
ENDIF()
INCLUDE(CPack)
3 changes: 0 additions & 3 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ FUNCTION(ADD_UNIT_TEST target)
TARGET_USE_GTEST(${target})
TARGET_INCLUDE_DIRECTORIES(${target}
PRIVATE ${CMAKE_SOURCE_DIR}/tests/include)
IF(BUILD_LIB)
TARGET_LINK_LIBRARIES(${target} stdtensor)
ENDIF()
IF(HAVE_CUDA)
TARGET_LINK_LIBRARIES(${target} cudart)
ENDIF()
Expand Down
10 changes: 9 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BUILD_BENCHMARKS=0
BUILD_EXAMPLES=0
BUILD_GBENCH=0
BUILD_GTEST=0
BUILD_LIB=1 # FIXME: make it false
BUILD_LIB=0
BUILD_TESTS=0

HAVE_CUDA=0
Expand Down Expand Up @@ -55,6 +55,9 @@ parse_args() {
CUDA_HOME="${i#*=}"
echo "configure --with-cuda=$CUDA_HOME"
;;
--release=*)
RELEASE="${i#*=}"
;;
--verbose)
VERBOSE=1
;;
Expand Down Expand Up @@ -119,6 +122,11 @@ add_cmake_flags() {

# add_cmake_flag BUILD_DOCS 1
# add_cmake_flag CMAKE_FIND_DEBUG_MODE 1

if [ ! -z "$RELEASE" ]; then
add_cmake_flag CPACK_PACKAGE_VERSION $RELEASE
add_cmake_flag BUILD_RELEASE 1
fi
}

main() {
Expand Down
9 changes: 0 additions & 9 deletions examples/example_c_api.c

This file was deleted.

30 changes: 0 additions & 30 deletions include/tensor.h

This file was deleted.

53 changes: 53 additions & 0 deletions include/ttl/bits/consistent_variable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <stdexcept>
#include <string>

namespace ttl
{
namespace internal
{
struct std_to_string {
template <typename T>
std::string operator()(const T &x) const
{
return std::to_string(x);
}
};

template <typename T, typename Str = std_to_string>
class consistent_variable
{
T value_;
bool assigned_;

public:
consistent_variable() : assigned_(false) {}

consistent_variable(T value) : value_(std::move(value)), assigned_(true) {}

void operator=(T v)
{
if (assigned_) {
if (value_ != v) {
// TODO: customized exception
throw std::invalid_argument(
"consistent_variable was assigned: " + Str()(value_) +
" now assigned: " + Str()(v));
}
} else {
value_ = std::move(v);
assigned_ = true;
}
}

operator T() const
{
if (!assigned_) {
// TODO: customized exception
throw std::runtime_error("consistent_variable not assigned");
}
return value_;
}
};
} // namespace internal
} // namespace ttl
12 changes: 11 additions & 1 deletion include/ttl/bits/flat_shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ template <typename Dim = uint32_t>
class basic_flat_shape
{
using dim_t = Dim;
const std::vector<dim_t> dims_;

std::vector<dim_t> dims_;

public:
using dimension_type = Dim;
Expand Down Expand Up @@ -58,6 +59,15 @@ class basic_flat_shape
return basic_flat_shape(std::move(sub_dims));
}

std::pair<dim_t, basic_flat_shape> uncons() const
{
if (dims_.size() < 1) {
throw std::logic_error("scalar shape has no sub shape");
}
std::vector<dim_t> sub_dims(dims_.begin() + 1, dims_.end());
return std::make_pair(dims_[0], basic_flat_shape(std::move(sub_dims)));
}

// experimental API!
basic_flat_shape batch_shape(Dim n) const
{
Expand Down
13 changes: 11 additions & 2 deletions include/ttl/bits/raw_tensor_mixin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,24 @@ class raw_tensor_mixin

slice_type slice(Dim i, Dim j) const
{
const auto sub_shape = shape_.subshape();
Dim n;
S sub_shape;
std::tie(n, sub_shape) = shape_.uncons();
if (i > j) { throw std::invalid_argument("invalid slice"); }
if (i < 0 || j > n) { throw std::invalid_argument("out of bound"); }

char *offset = (char *)(data_.get()) +
i * sub_shape.size() * Encoder::size(value_type_);
return slice_type(offset, value_type_, sub_shape.batch_shape(j - i));
}

slice_type operator[](Dim i) const
{
const auto sub_shape = shape_.subshape();
Dim n;
S sub_shape;
std::tie(n, sub_shape) = shape_.uncons();
if (i < 0 || i >= n) { throw std::invalid_argument("out of bound"); }

char *offset = (char *)(data_.get()) +
i * sub_shape.size() * Encoder::size(value_type_);
return slice_type(offset, value_type_, std::move(sub_shape));
Expand Down
7 changes: 7 additions & 0 deletions include/ttl/consistent_variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <ttl/bits/consistent_variable.hpp>

namespace ttl
{
using ttl::internal::consistent_variable;
}
40 changes: 0 additions & 40 deletions src/tensor.cpp

This file was deleted.

43 changes: 43 additions & 0 deletions tests/test_consistent_variable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "testing.hpp"

#include <ttl/consistent_variable>

TEST(consistent_variable_test, test_1)
{
ttl::consistent_variable<int> i;
i = 1;
try {
i = 2;
ASSERT_TRUE(false);
} catch (...) {
ASSERT_TRUE(true);
}

try {
i = 1;
ASSERT_TRUE(true);
} catch (...) {
ASSERT_TRUE(false);
}
}

TEST(consistent_variable_test, test_2)
{
ttl::consistent_variable<int> i;
try {
int j = i;
ASSERT_TRUE(false);
ASSERT_EQ(j, 0);
} catch (...) {
ASSERT_TRUE(true);
}

i = 1;
try {
int j = i;
ASSERT_TRUE(true);
ASSERT_EQ(j, 1);
} catch (...) {
ASSERT_TRUE(false);
}
}
30 changes: 0 additions & 30 deletions tests/test_tensor_c_api.cpp

This file was deleted.

0 comments on commit 6490bc5

Please sign in to comment.