From 6fa483fe02c1a2563ab04ef7042d699cd5a41a5e Mon Sep 17 00:00:00 2001 From: MyungJoo Ham Date: Thu, 31 May 2018 20:49:14 +0900 Subject: [PATCH] [Common] Add dimension property parsing API Let's use dimension parsing API in tensor_filter Signed-off-by: MyungJoo Ham --- common/tensor_common.c | 26 ++++++++++++++++++++++++++ common/test/unittest_common.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ include/tensor_common.h | 8 ++++++++ 3 files changed, 75 insertions(+) diff --git a/common/tensor_common.c b/common/tensor_common.c index d261f5f..81c7406 100644 --- a/common/tensor_common.c +++ b/common/tensor_common.c @@ -135,3 +135,29 @@ int find_key_strv(const gchar **strv, const gchar *key) { return -1; /* Not Found */ } + +/** + * @brief Parse tensor dimension parameter string + * @return The Rank. 0 if error. + * @param param The parameter string in the format of d1:d2:d3:d4, d1:d2:d3, d1:d2, or d1, where dN is a positive integer and d1 is the innermost dimension; i.e., dim[d4][d3][d2][d1]; + */ +int get_tensor_dimension(const gchar* param, uint32_t dim[NNS_TENSOR_RANK_LIMIT]) { + gchar **strv = g_strsplit(param, ":", NNS_TENSOR_RANK_LIMIT); + int i, retval = 0; + guint64 val; + + g_assert(strv != NULL); + + for (i = 0; i < NNS_TENSOR_RANK_LIMIT; i++) { + if (strv[i] == NULL) + break; + val = g_ascii_strtoull(strv[i], NULL, 10); + dim[i] = val; + retval = i + 1; + } + for (; i < NNS_TENSOR_RANK_LIMIT; i++) + dim[i] = 1; + + g_strfreev(strv); + return retval; +} diff --git a/common/test/unittest_common.cpp b/common/test/unittest_common.cpp index f38fa17..d3a0446 100644 --- a/common/test/unittest_common.cpp +++ b/common/test/unittest_common.cpp @@ -199,6 +199,47 @@ TEST(common_find_key_strv, case5) { } +TEST(common_get_tensor_dimension, case1) { + uint32_t dim[NNS_TENSOR_RANK_LIMIT]; + int rank = get_tensor_dimension("345:123:433:177", dim); + EXPECT_EQ(rank, 4); + EXPECT_EQ(dim[0], 345); + EXPECT_EQ(dim[1], 123); + EXPECT_EQ(dim[2], 433); + EXPECT_EQ(dim[3], 177); +} + + +TEST(common_get_tensor_dimension, case2) { + uint32_t dim[NNS_TENSOR_RANK_LIMIT]; + int rank = get_tensor_dimension("345:123:433", dim); + EXPECT_EQ(rank, 3); + EXPECT_EQ(dim[0], 345); + EXPECT_EQ(dim[1], 123); + EXPECT_EQ(dim[2], 433); + EXPECT_EQ(dim[3], 1); +} + +TEST(common_get_tensor_dimension, case3) { + uint32_t dim[NNS_TENSOR_RANK_LIMIT]; + int rank = get_tensor_dimension("345:123", dim); + EXPECT_EQ(rank, 2); + EXPECT_EQ(dim[0], 345); + EXPECT_EQ(dim[1], 123); + EXPECT_EQ(dim[2], 1); + EXPECT_EQ(dim[3], 1); +} + +TEST(common_get_tensor_dimension, case4) { + uint32_t dim[NNS_TENSOR_RANK_LIMIT]; + int rank = get_tensor_dimension("345", dim); + EXPECT_EQ(rank, 1); + EXPECT_EQ(dim[0], 345); + EXPECT_EQ(dim[1], 1); + EXPECT_EQ(dim[2], 1); + EXPECT_EQ(dim[3], 1); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/include/tensor_common.h b/include/tensor_common.h index 77ef284..a2b8b84 100644 --- a/include/tensor_common.h +++ b/include/tensor_common.h @@ -53,6 +53,7 @@ #define __GST_TENSOR_COMMON_H__ #include +#include G_BEGIN_DECLS @@ -124,6 +125,13 @@ extern tensor_type get_tensor_type(const gchar* typestr); */ extern int find_key_strv(const gchar **strv, const gchar *key); +/** + * @brief Parse tensor dimension parameter string + * @return The Rank. + * @param param The parameter string in the format of d1:d2:d3:d4, d1:d2:d3, d1:d2, or d1, where dN is a positive integer and d1 is the innermost dimension; i.e., dim[d4][d3][d2][d1]; + */ +extern int get_tensor_dimension(const gchar* param, uint32_t dim[NNS_TENSOR_RANK_LIMIT]); + G_END_DECLS #endif /* __GST_TENSOR_COMMON_H__ */ -- 2.7.4