Skip to content

Commit

Permalink
[core]Correct Constant creation from string (#21099)
Browse files Browse the repository at this point in the history
* Improve Constant creation from string values

* Optimize Constant creation from vector<T>
binary size reduction

* Fix code style

* Add tests to check there is no precision los

* Fix conversion for string -> integral numbers

---------

Co-authored-by: Michal Lukaszewski <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jun 12, 2024
1 parent 3c5f575 commit 1545465
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 28 deletions.
25 changes: 19 additions & 6 deletions src/core/include/openvino/op/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ class OPENVINO_API Constant : public Op {
template <typename T>
Constant(const element::Type& type, const Shape& shape, const std::vector<T>& values)
: Constant(false, type, shape) {
const auto this_shape_size = shape_size(m_shape);
const auto values_size = values.size();
const auto has_single_value = (values_size == 1);
NODE_VALIDATION_CHECK(this,
values.size() == 1 || values.size() == shape_size(m_shape),
has_single_value || values_size == this_shape_size,
"Did not get the expected number of literals for a constant of shape ",
m_shape,
" (got ",
values.size(),
values_size,
", expected ",
(shape_size(m_shape) == 1 ? "" : "1 or "),
shape_size(m_shape),
(this_shape_size == 1 ? "" : "1 or "),
this_shape_size,
").");

if (values.size() == 1) {
fill_data(type, values.front());
if (has_single_value) {
fill_data(type, values[0]);
} else {
write_values(values);
}
Expand Down Expand Up @@ -890,6 +893,16 @@ class OPENVINO_API Constant : public Op {
# pragma GCC diagnostic pop
#endif
}

template <class T>
void fill_or_write(const bool fill, const element::Type& et, const std::vector<T>& values) {
if (fill) {
fill_data(et, values[0]);
} else {
write_values(values);
}
}

template <ov::element::Type_t Type,
typename ValueT,
typename std::enable_if<Type == ov::element::Type_t::u4 || Type == ov::element::Type_t::u4 ||
Expand Down
53 changes: 31 additions & 22 deletions src/core/src/op/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ std::string to_cpp_string(T value) {
}
}

std::vector<double> from_string_vector(const std::vector<std::string>& str_values) {
std::vector<double> values;
template <class T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
T str_to_value(const std::string& s, size_t* pos) {
return static_cast<T>(std::is_signed<T>::value ? std::stoll(s, pos) : std::stoull(s, pos));
}

template <class T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
T str_to_value(const std::string& s, size_t* pos) {
return static_cast<T>(std::stod(s, pos));
}

template <class T>
std::vector<T> from_string_vector(const std::vector<std::string>& str_values) {
std::vector<T> values;
values.reserve(str_values.size());
std::transform(str_values.cbegin(), str_values.cend(), std::back_inserter(values), [](const std::string& s) {
size_t pos;
auto v = std::stold(s, &pos);
auto v = str_to_value<T>(s, &pos);
OPENVINO_ASSERT(s.size() == pos, "Could not parse literal '", s, "'");
return v;
});
Expand Down Expand Up @@ -97,33 +108,30 @@ Constant::Constant(const Tensor& tensor)

Constant::Constant(const element::Type& type, const Shape& shape, const std::vector<std::string>& values)
: Constant(false, type, shape) {
const auto this_shape_size = shape_size(m_shape);
const auto values_size = values.size();
const auto has_single_value = (values_size == 1);
NODE_VALIDATION_CHECK(this,
values.size() == 1 || values.size() == shape_size(m_shape),
has_single_value || values_size == this_shape_size,
"Did not get the expected number of literals for a constant of shape ",
m_shape,
" (got ",
values.size(),
values_size,
", expected ",
(shape_size(m_shape) == 1 ? "" : "1 or "),
shape_size(m_shape),
(this_shape_size == 1 ? "" : "1 or "),
this_shape_size,
").");
const auto is_checked_and_identical = has_single_value && (this_shape_size != 1);

if (type == element::string) {
if (values.size() == 1) {
fill_data(type, values.front());
} else {
write_values(values);
}
fill_or_write(is_checked_and_identical, type, values);
} else if (type.is_real()) {
fill_or_write(is_checked_and_identical, type, from_string_vector<double>(values));
} else if (type.is_signed()) {
fill_or_write(is_checked_and_identical, type, from_string_vector<int64_t>(values));
} else {
auto parsed_values = from_string_vector(values);
if (parsed_values.size() == 1) {
fill_data(type, parsed_values.front());
} else {
write_values(parsed_values);
}
fill_or_write(is_checked_and_identical, type, from_string_vector<uint64_t>(values));
}
const auto is_checked_and_identical = (values.size() == 1) && (shape_size(m_shape) != 1);
update_identical_flags(is_checked_and_identical, is_checked_and_identical);
}

Constant::Constant(const element::Type& type, const Shape& shape) : Constant(true, type, shape) {}
Expand Down Expand Up @@ -385,10 +393,11 @@ bool Constant::evaluate(TensorVector& outputs, const TensorVector& inputs) const
outputs.emplace_back(m_element_type, m_shape);
else
outputs[0].set_shape(m_shape);

if (m_element_type == ov::element::string) {
auto num_elements = shape_size(m_shape);
const std::string* src_strings = static_cast<const std::string*>(get_data_ptr());
std::string* dst_strings = static_cast<std::string*>(outputs[0].data());
auto src_strings = static_cast<const std::string*>(get_data_ptr());
auto dst_strings = static_cast<std::string*>(outputs[0].data());
std::copy_n(src_strings, num_elements, dst_strings);
} else {
std::memcpy(outputs[0].data(), get_data_ptr(), outputs[0].get_byte_size());
Expand Down
48 changes: 48 additions & 0 deletions src/core/tests/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,29 @@ TEST(constant, int64_vector_broadcast) {
EXPECT_EQ(p[3], 1);
}

TEST(constant, int64_string_max) {
Shape shape{4};
vector<string> input{"9223372036854775807", "9223372036854775807", "9223372036854775807", "9223372036854775807"};

constexpr auto exp_value = std::numeric_limits<int64_t>::max();
ov::op::v0::Constant c(element::i64, shape, input);
auto v = c.get_vector<int64_t>();
ASSERT_EQ(v.size(), shape_size(shape));
EXPECT_THAT(v, testing::Each(exp_value));

const auto p = c.get_data_ptr<int64_t>();
EXPECT_EQ(p[0], exp_value);
EXPECT_EQ(p[1], exp_value);
EXPECT_EQ(p[2], exp_value);
EXPECT_EQ(p[3], exp_value);

EXPECT_EQ(input, c.get_value_strings());

for (unsigned i = 0; i != input.size(); ++i) {
EXPECT_EQ(input[i], c.convert_value_to_string(i));
}
}

//
// uint1
//
Expand Down Expand Up @@ -1184,6 +1207,31 @@ TEST(constant, uint64_vector_broadcast) {
EXPECT_EQ(p[3], 1);
}

TEST(constant, uint64_string_max) {
Shape shape{4};
vector<string> input{"18446744073709551615",
"18446744073709551615",
"18446744073709551615",
"18446744073709551615"};
ov::op::v0::Constant c(element::u64, shape, input);
constexpr auto exp_value = std::numeric_limits<uint64_t>::max();
auto v = c.get_vector<uint64_t>();
ASSERT_EQ(v.size(), shape_size(shape));
EXPECT_THAT(v, testing::Each(exp_value));

const auto p = c.get_data_ptr<uint64_t>();
EXPECT_EQ(p[0], exp_value);
EXPECT_EQ(p[1], exp_value);
EXPECT_EQ(p[2], exp_value);
EXPECT_EQ(p[3], exp_value);

EXPECT_EQ(input, c.get_value_strings());

for (unsigned i = 0; i != input.size(); ++i) {
EXPECT_EQ(input[i], c.convert_value_to_string(i));
}
}

//
// bfloat16
//
Expand Down

0 comments on commit 1545465

Please sign in to comment.