Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Update join util functions to use string view and allow in stream join (#35568)

Pawel Raasz588fd0779dfe
diff --git a/src/common/snippets/src/lowered/pass/insert_perf_count_verbose.cpp b/src/common/snippets/src/lowered/pass/insert_perf_count_verbose.cpp
index 3f71c8cf9a1..74ed441df44 100644
--- a/src/common/snippets/src/lowered/pass/insert_perf_count_verbose.cpp
+++ b/src/common/snippets/src/lowered/pass/insert_perf_count_verbose.cpp
@@ -6,6 +6,7 @@
#include <cstdint>
#include <iterator>
#include <memory>
+#include <ostream>
#include <sstream>
#include <string>
#include <vector>
@@ -99,12 +100,12 @@ std::string InsertPerfCountVerbose::collect_params(const ov::snippets::lowered::
std::stringstream ss;
ss << m_subgraph_name << ',';
ss << brgemm_expr->get_node()->get_friendly_name() << ',';
- ss << ov::util::join(input_types, ";") << ',';
- ss << ov::util::join(output_types, ";") << ',';
- ss << ov::util::join(input_shapes, ";") << ',';
- ss << ov::util::join(output_shapes, ";") << ',';
- ss << ov::util::join(input_layouts, ";") << (input_layouts.empty() ? "" : ";") << ',';
- ss << ov::util::join(output_layouts, ";") << (output_layouts.empty() ? "" : ";") << ',';
+ ss << ov::util::join<std::ostream>(input_types, ";") << ',';
+ ss << ov::util::join<std::ostream>(output_types, ";") << ',';
+ ss << ov::util::join<std::ostream>(input_shapes, ";") << ',';
+ ss << ov::util::join<std::ostream>(output_shapes, ";") << ',';
+ ss << ov::util::join<std::ostream>(input_layouts, ";") << (input_layouts.empty() ? "" : ";") << ',';
+ ss << ov::util::join<std::ostream>(output_layouts, ";") << (output_layouts.empty() ? "" : ";") << ',';
const auto& in_0_desc = brgemm_expr->get_input_port_descriptor(0);
const auto& in_1_desc = brgemm_expr->get_input_port_descriptor(1);
diff --git a/src/common/snippets/src/lowered/port_descriptor.cpp b/src/common/snippets/src/lowered/port_descriptor.cpp
index 099d8868a74..2c3fe409db4 100644
--- a/src/common/snippets/src/lowered/port_descriptor.cpp
+++ b/src/common/snippets/src/lowered/port_descriptor.cpp
@@ -7,6 +7,7 @@
#include <cassert>
#include <cstddef>
#include <memory>
+#include <ostream>
#include <snippets/utils/utils.hpp>
#include <sstream>
#include <string>
@@ -120,7 +121,7 @@ std::string PortDescriptor::serialize() const {
const auto serialize_container = [&ss](const auto& container) {
ss << container.size() << " ";
if (!container.empty()) {
- ss << ov::util::join(container, " ") << " ";
+ ss << ov::util::join<std::ostream>(container, " ") << " ";
}
};
serialize_container(*m_tensor_shape);
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hpp
index fbdd592978d..3907068f5d4 100644
--- a/src/common/util/include/openvino/util/common_util.hpp
+++ b/src/common/util/include/openvino/util/common_util.hpp
@@ -19,25 +19,56 @@
namespace ov::util {
/**
- * @brief Join container's elements to string using user string as separator.
+ * @brief Helper struct to join container's elements to string using user string as separator.
+ * The output string is generated in operator<< and can be used in any context where std::string is expected.
+ * Example:
+ * std::vector<int> vec = {1, 2, 3};
+ * std::cout << Joined{vec, ","} << std::endl; // Output: 1,2,3
*
- * @param container Element to make joined string.
- * @param sep User string used as separator. Default ", ".
- * @return Joined elements as string.
+ * @tparam Container
*/
template <typename Container>
-std::string join(const Container& container, const std::string& sep = ", ") {
- std::ostringstream ss;
- auto first = std::begin(container);
- const auto last = std::end(container);
- if (first != last) {
- ss << *first;
- ++first;
- for (; first != last; ++first) {
- ss << sep << *first;
+struct Joined {
+ const Container& c;
+ std::string_view sep;
+
+ friend std::ostream& operator<<(std::ostream& os, const Joined& jv) {
+ auto first = std::begin(jv.c);
+ const auto last = std::end(jv.c);
+ if (first != last) {
+ os << *first;
+ for (++first; first != last; ++first)
+ os << jv.sep << *first;
}
+ return os;
+ }
+
+ operator std::string() const {
+ std::ostringstream ss;
+ ss << *this;
+ return ss.str();
+ }
+};
+
+/**
+ * @brief Util to create a string from a container's elements joined by a separator.
+ *
+ * @note If R is std::string the container's elements are joined and returned as a std::string.
+ * If R is std::ostream the container's elements are joined and returned as a Joined<>
+ * for lazy evaluation in stream context.
+ *
+ * @param c The input container.
+ * @param sep The separator to be used between the elements. Default is ", ".
+ * @return A string representation of the container's elements joined by the specified separator.
+ */
+template <class R = std::string, typename Container>
+auto join(const Container& c, std::string_view sep = ", ") {
+ if constexpr (std::is_same_v<R, std::string>) {
+ return static_cast<std::string>(Joined<Container>{c, sep});
+ } else if constexpr (std::is_same_v<R, std::ostream>) {
+ return Joined<Container>{c, sep};
+ } else {
}
- return ss.str();
}
/**
@@ -49,7 +80,7 @@ std::string join(const Container& container, const std::string& sep = ", ") {
* - std::vector<int>{} -> "[ ]"
*
* @param v Vector to be converted
- * @return String contains
+ * @return String containing the vector elements
*/
template <typename T, typename A>
std::string vector_to_string(const std::vector<T, A>& v) {
diff --git a/src/core/src/axis_set.cpp b/src/core/src/axis_set.cpp
index b8ffe05ed17..9b72785b6f8 100644
--- a/src/core/src/axis_set.cpp
+++ b/src/core/src/axis_set.cpp
@@ -31,10 +31,7 @@ std::vector<int64_t> ov::AxisSet::to_vector() const {
}
std::ostream& ov::operator<<(std::ostream& s, const AxisSet& axis_set) {
- s << "AxisSet{";
- s << ov::util::join(axis_set);
- s << "}";
- return s;
+ return s << "AxisSet{" << ov::util::join<std::ostream>(axis_set) << "}";
}
const std::vector<int64_t>& ov::AttributeAdapter<ov::AxisSet>::get() {
diff --git a/src/core/src/axis_vector.cpp b/src/core/src/axis_vector.cpp
index 661cf66bb29..04d7568cb3a 100644
--- a/src/core/src/axis_vector.cpp
+++ b/src/core/src/axis_vector.cpp
@@ -7,10 +7,7 @@
#include "openvino/util/common_util.hpp"
std::ostream& ov::operator<<(std::ostream& s, const AxisVector& axis_vector) {
- s << "AxisVector{";
- s << ov::util::join(axis_vector);
- s << "}";
- return s;
+ return s << "AxisVector{" << ov::util::join<std::ostream>(axis_vector) << "}";
}
ov::AxisVector::AxisVector(const std::initializer_list<size_t>& axes) : std::vector<size_t>(axes) {}
diff --git a/src/core/src/coordinate.cpp b/src/core/src/coordinate.cpp
index d4a2955887a..fc3d4ae1490 100644
--- a/src/core/src/coordinate.cpp
+++ b/src/core/src/coordinate.cpp
@@ -7,10 +7,7 @@
#include "openvino/util/common_util.hpp"
std::ostream& ov::operator<<(std::ostream& s, const Coordinate& coordinate) {
- s << "Coordinate{";
- s << ov::util::join(coordinate);
- s << "}";
- return s;
+ return s << "Coordinate{" << ov::util::join<std::ostream>(coordinate) << "}";
}
ov::Coordinate::Coordinate() = default;
diff --git a/src/core/src/coordinate_diff.cpp b/src/core/src/coordinate_diff.cpp
index 3b2c92914f6..20c625e25c3 100644
--- a/src/core/src/coordinate_diff.cpp
+++ b/src/core/src/coordinate_diff.cpp
@@ -7,10 +7,7 @@
#include "openvino/util/common_util.hpp"
std::ostream& ov::operator<<(std::ostream& s, const CoordinateDiff& coordinate_diff) {
- s << "CoordinateDiff{";
- s << ov::util::join(coordinate_diff);
- s << "}";
- return s;
+ return s << "CoordinateDiff{" << ov::util::join<std::ostream>(coordinate_diff) << "}";
}
ov::CoordinateDiff::CoordinateDiff(const std::initializer_list<std::ptrdiff_t>& diffs)
diff --git a/src/core/src/descriptor/tensor.cpp b/src/core/src/descriptor/tensor.cpp
index 4ee5ad4c973..0aa4cc3ce50 100644
--- a/src/core/src/descriptor/tensor.cpp
+++ b/src/core/src/descriptor/tensor.cpp
@@ -261,7 +261,7 @@ void copy_tensor_names(Tensor& dst, const Tensor& src) {
}
std::ostream& operator<<(std::ostream& out, const Tensor& tensor) {
- out << "Tensor(" << util::join(tensor.get_names()) << ")";
+ out << "Tensor(" << util::join<std::ostream>(tensor.get_names()) << ")";
return out;
}
} // namespace descriptor
diff --git a/src/core/src/dimension.cpp b/src/core/src/dimension.cpp
index f25f555db2c..5137b9b1e86 100644
--- a/src/core/src/dimension.cpp
+++ b/src/core/src/dimension.cpp
@@ -43,7 +43,7 @@ std::shared_ptr<ov::Symbol> merge_symbols(std::shared_ptr<ov::Symbol> lhs,
bool check_all_digits(std::string_view value) {
return std::all_of(value.begin(), value.end(), [](unsigned char c) {
- return std::isdigit(c);
+ return std::isdigit(c) || (c == '-');
});
}
Dimension::value_type dimension_length(Interval::value_type vt) {
diff --git a/src/core/src/partial_shape.cpp b/src/core/src/partial_shape.cpp
index 1fcab9edac0..75dadf02177 100644
--- a/src/core/src/partial_shape.cpp
+++ b/src/core/src/partial_shape.cpp
@@ -154,16 +154,7 @@ ov::PartialShape ov::operator+(const PartialShape& s1, const PartialShape& s2) {
std::ostream& ov::operator<<(std::ostream& str, const PartialShape& shape) {
if (shape.m_rank_is_static) {
- str << "[";
- bool first = true;
- for (auto& d : shape.m_dimensions) {
- if (!first) {
- str << ",";
- }
- str << d;
- first = false;
- }
- return (str << "]");
+ return str << "[" << ov::util::join<std::ostream>(shape.m_dimensions, ",") << "]";
} else {
return (str << "[...]");
}
diff --git a/src/core/src/pass/visualize_tree.cpp b/src/core/src/pass/visualize_tree.cpp
index cecf37deb85..9d9d8fdf92e 100644
--- a/src/core/src/pass/visualize_tree.cpp
+++ b/src/core/src/pass/visualize_tree.cpp
@@ -612,7 +612,7 @@ std::string ov::pass::VisualizeTree::get_attributes(std::shared_ptr<Node> node)
}
std::stringstream ss;
- ss << " " << node->get_name() << " [" << ov::util::join(attributes, " ") << "]\n";
+ ss << " " << node->get_name() << " [" << ov::util::join<std::ostream>(attributes, " ") << "]\n";
return ss.str();
}
diff --git a/src/core/src/preprocess/preprocess_impls.cpp b/src/core/src/preprocess/preprocess_impls.cpp
index bd348f62539..cc164e54730 100644
--- a/src/core/src/preprocess/preprocess_impls.cpp
+++ b/src/core/src/preprocess/preprocess_impls.cpp
@@ -437,7 +437,7 @@ void OutputInfo::OutputInfoImpl::dump(std::ostream& str) const {
str << "Output ";
if (!start_out_node_names.empty()) {
- str << "\"" << util::join(start_out_node_names) << "\"";
+ str << "\"" << util::join<std::ostream>(start_out_node_names) << "\"";
}
str << ":" << std::endl;
str << " Model's data tensor: ";
diff --git a/src/core/src/shape.cpp b/src/core/src/shape.cpp
index 9323150f735..7eb0b56ceb3 100644
--- a/src/core/src/shape.cpp
+++ b/src/core/src/shape.cpp
@@ -9,10 +9,7 @@
#include "openvino/util/common_util.hpp"
std::ostream& ov::operator<<(std::ostream& s, const Shape& shape) {
- s << "[";
- s << ov::util::join(shape, ",");
- s << "]";
- return s;
+ return s << "[" << ov::util::join<std::ostream>(shape, ",") << "]";
}
ov::Shape::Shape() : std::vector<size_t>() {}
diff --git a/src/core/src/strides.cpp b/src/core/src/strides.cpp
index 804af1b4ca4..5a920cb861d 100644
--- a/src/core/src/strides.cpp
+++ b/src/core/src/strides.cpp
@@ -7,10 +7,7 @@
#include "openvino/util/common_util.hpp"
std::ostream& ov::operator<<(std::ostream& s, const ov::Strides& strides) {
- s << "Strides{";
- s << ov::util::join(strides);
- s << "}";
- return s;
+ return s << "Strides{" << ov::util::join<std::ostream>(strides) << "}";
}
ov::Strides::Strides() : std::vector<size_t>() {}
diff --git a/src/plugins/intel_cpu/src/shape_inference/static_shape.hpp b/src/plugins/intel_cpu/src/shape_inference/static_shape.hpp
index 8e3ddfe58be..6facb41c75d 100644
--- a/src/plugins/intel_cpu/src/shape_inference/static_shape.hpp
+++ b/src/plugins/intel_cpu/src/shape_inference/static_shape.hpp
@@ -317,7 +317,7 @@ private:
template <class T, typename = std::enable_if_t<is_static_shape_adapter<T>()>>
std::ostream& operator<<(std::ostream& out, const T& shape) {
- return out << '{' << ov::util::join(shape, ",") << '}';
+ return out << '{' << ov::util::join<std::ostream>(shape, ",") << '}';
}
template <class T, class U, typename = std::enable_if_t<is_static_shape_adapter<T>() && is_static_shape_adapter<U>()>>
diff --git a/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp b/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
index be93bf8a95b..313c610dbbf 100644
--- a/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
+++ b/src/plugins/intel_cpu/src/utils/debug_capabilities.cpp
@@ -500,7 +500,7 @@ public:
template <class Container>
std::string join(const Container& strs) {
std::stringstream ss;
- ss << "[" << ov::util::join(strs, ",") << "]";
+ ss << "[" << ov::util::join<std::ostream>(strs, ",") << "]";
return ss.str();
}
};
@@ -564,7 +564,7 @@ std::ostream& operator<<(std::ostream& os, const PrintableModel& model) {
if (constop->get_element_type().is_dynamic()) {
os << "...";
} else {
- os << ov::util::join(constop->get_value_strings(), ",");
+ os << ov::util::join<std::ostream>(constop->get_value_strings(), ",");
}
} else {
os << "...";
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/utils.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/utils.hpp
index 15a5dabc7e4..26519806711 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/utils.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/utils.hpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2017-2025 Intel Corporation
+// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
@@ -232,7 +232,7 @@ inline std::string to_string(const T& v) {
// Overload << operator for vectors
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
- os << "[" << ov::util::join(vec) << "]";
+ os << "[" << ov::util::join<std::ostream>(vec) << "]";
return os;
}
diff --git a/src/plugins/intel_gpu/src/graph/program_dump_graph.cpp b/src/plugins/intel_gpu/src/graph/program_dump_graph.cpp
index daf3f768f48..8716045ddc6 100644
--- a/src/plugins/intel_gpu/src/graph/program_dump_graph.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_dump_graph.cpp
@@ -219,7 +219,7 @@ void dump_graph_init(std::ofstream& graph,
std::ostringstream oss;
if (ptr->is_type<dynamic_quantize>()) {
auto dyn_quan = ptr->as<dynamic_quantize>().get_primitive();
- oss << "\n" << "group_sizes: " << ov::util::join(cldnn::convert_vector<int64_t>(dyn_quan->attrs.group_sizes));
+ oss << "\n" << "group_sizes: " << ov::util::join<std::ostream>(cldnn::convert_vector<int64_t>(dyn_quan->attrs.group_sizes));
if (dyn_quan->attrs.precomputed_reduction) {
oss << "\n" << "precomputed_reduction_dt: " << dyn_quan->attrs.precomputed_reduction_dt;
}