Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
4 : *
5 : * @file base_properties.cpp
6 : * @date 03 May 2021
7 : * @brief Convenient property type definition for automated serialization
8 : * @see https://github.com/nnstreamer/nntrainer
9 : * @author Jihoon Lee <jhoon.it.lee@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 : #include <base_properties.h>
13 :
14 : #include <regex>
15 : #include <sstream>
16 : #include <string>
17 : #include <vector>
18 :
19 : namespace nntrainer {
20 8716 : bool PositiveIntegerProperty::isValid(const unsigned int &value) const {
21 8716 : return value > 0;
22 : }
23 :
24 : template <>
25 : std::string
26 3340 : str_converter<str_prop_tag, std::string>::to_string(const std::string &value) {
27 3340 : return value;
28 : }
29 :
30 : template <>
31 17711 : std::string str_converter<str_prop_tag, std::string>::from_string(
32 : const std::string &value) {
33 17711 : return value;
34 : }
35 :
36 : template <>
37 9603 : std::string str_converter<bool_prop_tag, bool>::to_string(const bool &value) {
38 13516 : return value ? "true" : "false";
39 : }
40 :
41 : template <>
42 11576 : bool str_converter<bool_prop_tag, bool>::from_string(const std::string &value) {
43 11576 : if (value == "true") {
44 : return true;
45 : }
46 4089 : if (value == "false") {
47 : return false;
48 : }
49 1 : std::string error_msg = "converting value to boolean failed, value: " + value;
50 1 : throw std::invalid_argument(error_msg);
51 : }
52 :
53 : template <>
54 12 : std::string str_converter<int_prop_tag, int>::to_string(const int &value) {
55 12 : return std::to_string(value);
56 : }
57 :
58 : template <>
59 739 : int str_converter<int_prop_tag, int>::from_string(const std::string &value) {
60 734 : return std::stoi(value);
61 : }
62 :
63 : template <>
64 2396 : std::string str_converter<uint_prop_tag, unsigned int>::to_string(
65 : const unsigned int &value) {
66 2396 : return std::to_string(value);
67 : }
68 :
69 : template <>
70 9572 : unsigned int str_converter<uint_prop_tag, unsigned int>::from_string(
71 : const std::string &value) {
72 9571 : return std::stoul(value);
73 : }
74 :
75 : template <>
76 : std::string
77 0 : str_converter<size_t_prop_tag, size_t>::to_string(const size_t &value) {
78 0 : return std::to_string(value);
79 : }
80 :
81 : template <>
82 : size_t
83 0 : str_converter<size_t_prop_tag, size_t>::from_string(const std::string &value) {
84 0 : return std::stoul(value);
85 : }
86 :
87 : template <>
88 : std::string
89 10376 : str_converter<float_prop_tag, float>::to_string(const float &value) {
90 10376 : return std::to_string(value);
91 : }
92 :
93 : template <>
94 11527 : float str_converter<float_prop_tag, float>::from_string(
95 : const std::string &value) {
96 11525 : return std::stof(value);
97 : }
98 :
99 : template <>
100 : std::string
101 17 : str_converter<double_prop_tag, double>::to_string(const double &value) {
102 17 : std::ostringstream ss;
103 17 : ss << value;
104 17 : return ss.str();
105 17 : }
106 :
107 : template <>
108 : double
109 314 : str_converter<double_prop_tag, double>::from_string(const std::string &value) {
110 307 : return std::stod(value);
111 : }
112 :
113 : template <>
114 686 : std::string str_converter<dimension_prop_tag, TensorDim>::to_string(
115 : const TensorDim &dimension) {
116 686 : std::stringstream ss;
117 2058 : ss << dimension.batch() << ':' << dimension.channel() << ':'
118 1372 : << dimension.height() << ':' << dimension.width();
119 686 : return ss.str();
120 686 : }
121 :
122 : template <>
123 1983 : TensorDim str_converter<dimension_prop_tag, TensorDim>::from_string(
124 : const std::string &value) {
125 : std::vector<std::string> tokens;
126 : std::string token;
127 1983 : std::istringstream iss(value);
128 :
129 8331 : while (std::getline(iss, token, ':')) {
130 6348 : tokens.push_back(token);
131 : }
132 :
133 1984 : NNTR_THROW_IF(tokens.size() > ml::train::TensorDim::MAXDIM,
134 : std::invalid_argument)
135 : << "More than 4 axes is not supported, target string: " << value;
136 :
137 1982 : TensorDim target;
138 :
139 : int cur_axis = 3;
140 8319 : for (auto iter = tokens.rbegin(); iter != tokens.rend(); iter++) {
141 6339 : target.setTensorDim(cur_axis--, std::stoul(*iter));
142 : }
143 :
144 1979 : return target;
145 3966 : }
146 :
147 : } // namespace nntrainer
|