Skip to content

Commit 6490bc5

Browse files
authored
merge from dev branch (#101)
* remove tensor.h * rm src/tensor.cpp * new api :: consistent_variable * update basic_flat_shape * check bound * fix windows build
1 parent 5c092a7 commit 6490bc5

16 files changed

+161
-120
lines changed

.github/workflows/windows.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v1
1313

14-
- run: cmake . -DBUILD_TESTS=1 -DBUILD_GTEST=1 -DUSE_STRICT=0 -DBUILD_LIB=1
14+
- run: cmake . -DBUILD_TESTS=1 -DBUILD_GTEST=1 -DUSE_STRICT=0 -DBUILD_LIB=0
1515
- run: cmake --build . --config Release
1616
- run: ctest -C Release

CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
2525

2626
OPTION(BUILD_LIB "Build c lib." OFF)
2727
IF(BUILD_LIB)
28-
ADD_LIBRARY(stdtensor src/tensor.cpp)
29-
INSTALL(TARGETS stdtensor ARCHIVE DESTINATION lib)
28+
INCLUDE(cmake/lib.cmake)
29+
BUILD_STDML_TENSOR_LIB(tensor)
3030
ENDIF()
3131

3232
INSTALL(DIRECTORY include DESTINATION .)
@@ -55,3 +55,7 @@ ENDIF()
5555
IF(BUILD_EXAMPLES)
5656
INCLUDE(cmake/examples.cmake)
5757
ENDIF()
58+
59+
IF(BUILD_RELEASE)
60+
INCLUDE(cmake/pack.cmake)
61+
ENDIF()

cmake/examples.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ FUNCTION(ADD_C_EXAMPLE target)
88
ENDFUNCTION()
99

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

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

cmake/lib.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FUNCTION(BUILD_STDML_TENSOR_LIB target)
2+
# libtensor will be shared by python/haskell bindings
3+
FILE(GLOB_RECURSE srcs ${CMAKE_SOURCE_DIR}/src/stdml/*)
4+
ADD_LIBRARY(${target} ${srcs})
5+
SET_TARGET_PROPERTIES(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
6+
TARGET_LINK_LIBRARIES(${target} dl)
7+
SET_PROPERTY(TARGET ${target} PROPERTY CXX_STANDARD 17)
8+
INSTALL(TARGETS ${target} ARCHIVE DESTINATION lib)
9+
ENDFUNCTION()

cmake/pack.cmake

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "[email protected]")
2+
SET(CPACK_GENERATOR "TGZ")
3+
SET(CPACK_PACKAGE_DIRECTORY ${CMAKE_SOURCE_DIR}/release)
4+
SET(CPACK_PACKAGE_VERSION
5+
"latest"
6+
CACHE STRING "Release version.")
7+
IF(BUILD_DEB)
8+
SET(CPACK_GENERATOR "TGZ;DEB")
9+
# SET(CPACK_GENERATOR "TGZ;RPM")
10+
ENDIF()
11+
INCLUDE(CPack)

cmake/tests.cmake

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ FUNCTION(ADD_UNIT_TEST target)
55
TARGET_USE_GTEST(${target})
66
TARGET_INCLUDE_DIRECTORIES(${target}
77
PRIVATE ${CMAKE_SOURCE_DIR}/tests/include)
8-
IF(BUILD_LIB)
9-
TARGET_LINK_LIBRARIES(${target} stdtensor)
10-
ENDIF()
118
IF(HAVE_CUDA)
129
TARGET_LINK_LIBRARIES(${target} cudart)
1310
ENDIF()

configure

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BUILD_BENCHMARKS=0
88
BUILD_EXAMPLES=0
99
BUILD_GBENCH=0
1010
BUILD_GTEST=0
11-
BUILD_LIB=1 # FIXME: make it false
11+
BUILD_LIB=0
1212
BUILD_TESTS=0
1313

1414
HAVE_CUDA=0
@@ -55,6 +55,9 @@ parse_args() {
5555
CUDA_HOME="${i#*=}"
5656
echo "configure --with-cuda=$CUDA_HOME"
5757
;;
58+
--release=*)
59+
RELEASE="${i#*=}"
60+
;;
5861
--verbose)
5962
VERBOSE=1
6063
;;
@@ -119,6 +122,11 @@ add_cmake_flags() {
119122

120123
# add_cmake_flag BUILD_DOCS 1
121124
# add_cmake_flag CMAKE_FIND_DEBUG_MODE 1
125+
126+
if [ ! -z "$RELEASE" ]; then
127+
add_cmake_flag CPACK_PACKAGE_VERSION $RELEASE
128+
add_cmake_flag BUILD_RELEASE 1
129+
fi
122130
}
123131

124132
main() {

examples/example_c_api.c

-9
This file was deleted.

include/tensor.h

-30
This file was deleted.
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
#include <stdexcept>
3+
#include <string>
4+
5+
namespace ttl
6+
{
7+
namespace internal
8+
{
9+
struct std_to_string {
10+
template <typename T>
11+
std::string operator()(const T &x) const
12+
{
13+
return std::to_string(x);
14+
}
15+
};
16+
17+
template <typename T, typename Str = std_to_string>
18+
class consistent_variable
19+
{
20+
T value_;
21+
bool assigned_;
22+
23+
public:
24+
consistent_variable() : assigned_(false) {}
25+
26+
consistent_variable(T value) : value_(std::move(value)), assigned_(true) {}
27+
28+
void operator=(T v)
29+
{
30+
if (assigned_) {
31+
if (value_ != v) {
32+
// TODO: customized exception
33+
throw std::invalid_argument(
34+
"consistent_variable was assigned: " + Str()(value_) +
35+
" now assigned: " + Str()(v));
36+
}
37+
} else {
38+
value_ = std::move(v);
39+
assigned_ = true;
40+
}
41+
}
42+
43+
operator T() const
44+
{
45+
if (!assigned_) {
46+
// TODO: customized exception
47+
throw std::runtime_error("consistent_variable not assigned");
48+
}
49+
return value_;
50+
}
51+
};
52+
} // namespace internal
53+
} // namespace ttl

include/ttl/bits/flat_shape.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ template <typename Dim = uint32_t>
2424
class basic_flat_shape
2525
{
2626
using dim_t = Dim;
27-
const std::vector<dim_t> dims_;
27+
28+
std::vector<dim_t> dims_;
2829

2930
public:
3031
using dimension_type = Dim;
@@ -58,6 +59,15 @@ class basic_flat_shape
5859
return basic_flat_shape(std::move(sub_dims));
5960
}
6061

62+
std::pair<dim_t, basic_flat_shape> uncons() const
63+
{
64+
if (dims_.size() < 1) {
65+
throw std::logic_error("scalar shape has no sub shape");
66+
}
67+
std::vector<dim_t> sub_dims(dims_.begin() + 1, dims_.end());
68+
return std::make_pair(dims_[0], basic_flat_shape(std::move(sub_dims)));
69+
}
70+
6171
// experimental API!
6272
basic_flat_shape batch_shape(Dim n) const
6373
{

include/ttl/bits/raw_tensor_mixin.hpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,24 @@ class raw_tensor_mixin
125125

126126
slice_type slice(Dim i, Dim j) const
127127
{
128-
const auto sub_shape = shape_.subshape();
128+
Dim n;
129+
S sub_shape;
130+
std::tie(n, sub_shape) = shape_.uncons();
131+
if (i > j) { throw std::invalid_argument("invalid slice"); }
132+
if (i < 0 || j > n) { throw std::invalid_argument("out of bound"); }
133+
129134
char *offset = (char *)(data_.get()) +
130135
i * sub_shape.size() * Encoder::size(value_type_);
131136
return slice_type(offset, value_type_, sub_shape.batch_shape(j - i));
132137
}
133138

134139
slice_type operator[](Dim i) const
135140
{
136-
const auto sub_shape = shape_.subshape();
141+
Dim n;
142+
S sub_shape;
143+
std::tie(n, sub_shape) = shape_.uncons();
144+
if (i < 0 || i >= n) { throw std::invalid_argument("out of bound"); }
145+
137146
char *offset = (char *)(data_.get()) +
138147
i * sub_shape.size() * Encoder::size(value_type_);
139148
return slice_type(offset, value_type_, std::move(sub_shape));

include/ttl/consistent_variable

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <ttl/bits/consistent_variable.hpp>
3+
4+
namespace ttl
5+
{
6+
using ttl::internal::consistent_variable;
7+
}

src/tensor.cpp

-40
This file was deleted.

tests/test_consistent_variable.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "testing.hpp"
2+
3+
#include <ttl/consistent_variable>
4+
5+
TEST(consistent_variable_test, test_1)
6+
{
7+
ttl::consistent_variable<int> i;
8+
i = 1;
9+
try {
10+
i = 2;
11+
ASSERT_TRUE(false);
12+
} catch (...) {
13+
ASSERT_TRUE(true);
14+
}
15+
16+
try {
17+
i = 1;
18+
ASSERT_TRUE(true);
19+
} catch (...) {
20+
ASSERT_TRUE(false);
21+
}
22+
}
23+
24+
TEST(consistent_variable_test, test_2)
25+
{
26+
ttl::consistent_variable<int> i;
27+
try {
28+
int j = i;
29+
ASSERT_TRUE(false);
30+
ASSERT_EQ(j, 0);
31+
} catch (...) {
32+
ASSERT_TRUE(true);
33+
}
34+
35+
i = 1;
36+
try {
37+
int j = i;
38+
ASSERT_TRUE(true);
39+
ASSERT_EQ(j, 1);
40+
} catch (...) {
41+
ASSERT_TRUE(false);
42+
}
43+
}

tests/test_tensor_c_api.cpp

-30
This file was deleted.

0 commit comments

Comments
 (0)