19 #include <unordered_map>
22 #include "ngraph/axis_vector.hpp"
23 #include "ngraph/graph_util.hpp"
24 #include "ngraph/node.hpp"
25 #include "ngraph/runtime/host_tensor.hpp"
26 #include "ngraph/runtime/tensor.hpp"
27 #include "ngraph/shape.hpp"
43 std::string join(
const T& v,
const std::string& sep =
", ")
45 std::ostringstream ss;
47 for (
const auto& x : v)
59 std::string vector_to_string(
const T& v)
61 std::ostringstream os;
62 os <<
"[ " << ngraph::join(v) <<
" ]";
67 size_t hash_combine(
const std::vector<size_t>& list);
69 void dump(std::ostream& out,
const void*,
size_t);
71 std::string to_lower(
const std::string& s);
73 std::string to_upper(
const std::string& s);
75 std::string trim(
const std::string& s);
77 std::vector<std::string> split(
const std::string& s,
char delimiter,
bool trim =
false);
80 std::string locale_string(T x)
83 ss.imbue(std::locale(
""));
93 if (m_active ==
false)
97 m_start_time = m_clock.now();
103 if (m_active ==
true)
105 auto end_time = m_clock.now();
106 m_last_time = end_time - m_start_time;
107 m_total_time += m_last_time;
112 size_t get_call_count()
const;
113 size_t get_seconds()
const;
114 size_t get_milliseconds()
const;
115 size_t get_microseconds()
const;
116 std::chrono::nanoseconds get_timer_value()
const;
117 size_t get_nanoseconds()
const;
119 size_t get_total_seconds()
const;
120 size_t get_total_milliseconds()
const;
121 size_t get_total_microseconds()
const;
122 size_t get_total_nanoseconds()
const;
125 std::chrono::high_resolution_clock m_clock;
126 std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time;
127 bool m_active =
false;
128 std::chrono::nanoseconds m_total_time =
129 std::chrono::high_resolution_clock::duration::zero();
130 std::chrono::nanoseconds m_last_time = std::chrono::high_resolution_clock::duration::zero();
131 size_t m_total_count = 0;
135 template <
typename T>
139 std::stringstream ss;
145 if (ss.fail() || ss.rdbuf()->in_avail() != 0)
147 throw std::runtime_error(
"Could not parse literal '" + s +
"'");
158 NGRAPH_API
double parse_string<double>(
const std::string& s);
166 NGRAPH_API uint8_t parse_string<uint8_t>(
const std::string& s);
169 template <
typename T>
172 std::vector<T> result(ss.size());
173 std::transform(ss.begin(), ss.end(), result.begin(), [](
const std::string& s) {
174 return parse_string<T>(s);
179 template <
typename T>
180 T ceil_div(
const T& x,
const T& y)
182 return (x == 0 ? 0 : (1 + (x - 1) / y));
185 template <
typename T>
186 T subtract_or_zero(T x, T y)
188 return y > x ? 0 : x - y;
192 void* ngraph_malloc(
size_t size);
194 void ngraph_free(
void*);
197 size_t round_up(
size_t size,
size_t alignment);
199 template <
typename T>
202 extern template NGRAPH_API AxisVector apply_permutation<AxisVector>(AxisVector input,
205 extern template NGRAPH_API Coordinate apply_permutation<Coordinate>(Coordinate input,
208 extern template NGRAPH_API Strides apply_permutation<Strides>(Strides input, AxisVector order);
210 extern template NGRAPH_API Shape apply_permutation<Shape>(Shape input, AxisVector order);
213 NGRAPH_API PartialShape apply_permutation(PartialShape input, AxisVector order);
216 AxisVector get_default_order(
size_t rank);
219 AxisVector get_default_order(
const Rank& rank);
222 AxisVector get_default_order(
const Shape& shape);
225 AxisVector get_default_order(
const PartialShape& shape);
236 template <
typename T>
241 static_assert(std::is_enum<T>::value,
"EnumMask template type must be an enum");
246 static_assert(std::is_unsigned<value_type>::value,
"EnumMask enum must use unsigned type.");
252 constexpr
EnumMask(
const T& enum_value)
253 : m_value{static_cast<value_type>(enum_value)}
256 EnumMask(
const EnumMask& other)
257 : m_value{other.m_value}
260 EnumMask(std::initializer_list<T> enum_values)
263 for (
auto& v : enum_values)
265 m_value |=
static_cast<value_type
>(v);
268 value_type value()
const {
return m_value; }
272 bool is_set(
const EnumMask& p)
const {
return (m_value & p.m_value) == p.m_value; }
277 void set(
const EnumMask& p) { m_value |= p.m_value; }
278 void clear(
const EnumMask& p) { m_value &= ~p.m_value; }
279 void clear_all() { m_value = 0; }
280 bool operator[](
const EnumMask& p)
const {
return is_set(p); }
281 bool operator==(
const EnumMask& other)
const {
return m_value == other.m_value; }
282 bool operator!=(
const EnumMask& other)
const {
return m_value != other.m_value; }
283 EnumMask& operator=(
const EnumMask& other)
285 m_value = other.m_value;
288 EnumMask& operator&=(
const EnumMask& other)
290 m_value &= other.m_value;
294 EnumMask& operator|=(
const EnumMask& other)
296 m_value |= other.m_value;
300 EnumMask operator&(
const EnumMask& other)
const
302 return EnumMask(m_value & other.m_value);
305 EnumMask operator|(
const EnumMask& other)
const
307 return EnumMask(m_value | other.m_value);
310 friend std::ostream& operator<<(std::ostream& os,
const EnumMask& m)
318 explicit EnumMask(
const value_type& value)
339 std::string version,
size_t& major,
size_t& minor,
size_t& patch, std::string& extra);
341 template <
typename T>
342 T double_to_int(
double x,
double float_to_int_converter(
double))
344 if (!std::is_integral<T>())
346 throw std::runtime_error(
347 "Function double_to_int template parameter must be an integral type.");
350 x = float_to_int_converter(x);
352 double min_t =
static_cast<double>(std::numeric_limits<T>::min());
355 return std::numeric_limits<T>::min();
358 double max_t =
static_cast<double>(std::numeric_limits<T>::max());
361 return std::numeric_limits<T>::max();
364 return static_cast<T
>(x);
368 template <
typename T>
369 std::vector<T> read_vector(std::shared_ptr<ngraph::runtime::Tensor> tv)
371 if (ngraph::element::from<T>() != tv->get_element_type())
373 throw std::invalid_argument(
"read_vector type must match Tensor type");
376 size_t size = element_count *
sizeof(T);
377 std::vector<T> rc(element_count);
378 tv->read(rc.data(), size);
382 template <
typename T>
383 std::vector<T> host_tensor_2_vector(ngraph::HostTensorPtr tensor)
385 NGRAPH_CHECK(tensor !=
nullptr,
386 "Invalid Tensor received, can't read the data from a null pointer.");
388 switch (tensor->get_element_type())
390 case ngraph::element::Type_t::boolean:
392 auto p = tensor->get_data_ptr<ngraph::element::Type_t::boolean>();
393 return std::vector<T>(p, p + tensor->get_element_count());
395 case ngraph::element::Type_t::bf16:
397 auto p = tensor->get_data_ptr<ngraph::element::Type_t::bf16>();
398 return std::vector<T>(p, p + tensor->get_element_count());
400 case ngraph::element::Type_t::f16:
402 auto p = tensor->get_data_ptr<ngraph::element::Type_t::f16>();
403 return std::vector<T>(p, p + tensor->get_element_count());
405 case ngraph::element::Type_t::f32:
407 auto p = tensor->get_data_ptr<ngraph::element::Type_t::f32>();
408 return std::vector<T>(p, p + tensor->get_element_count());
410 case ngraph::element::Type_t::f64:
412 auto p = tensor->get_data_ptr<ngraph::element::Type_t::f64>();
413 return std::vector<T>(p, p + tensor->get_element_count());
415 case ngraph::element::Type_t::i8:
417 auto p = tensor->get_data_ptr<ngraph::element::Type_t::i8>();
418 return std::vector<T>(p, p + tensor->get_element_count());
420 case ngraph::element::Type_t::i16:
422 auto p = tensor->get_data_ptr<ngraph::element::Type_t::i16>();
423 return std::vector<T>(p, p + tensor->get_element_count());
425 case ngraph::element::Type_t::i32:
427 auto p = tensor->get_data_ptr<ngraph::element::Type_t::i32>();
428 return std::vector<T>(p, p + tensor->get_element_count());
430 case ngraph::element::Type_t::i64:
432 auto p = tensor->get_data_ptr<ngraph::element::Type_t::i64>();
433 return std::vector<T>(p, p + tensor->get_element_count());
435 case ngraph::element::Type_t::u1: NGRAPH_CHECK(
false,
"u1 element type is unsupported");
break;
436 case ngraph::element::Type_t::u8:
438 auto p = tensor->get_data_ptr<ngraph::element::Type_t::u8>();
439 return std::vector<T>(p, p + tensor->get_element_count());
441 case ngraph::element::Type_t::u16:
443 auto p = tensor->get_data_ptr<ngraph::element::Type_t::u16>();
444 return std::vector<T>(p, p + tensor->get_element_count());
446 case ngraph::element::Type_t::u32:
448 auto p = tensor->get_data_ptr<ngraph::element::Type_t::u32>();
449 return std::vector<T>(p, p + tensor->get_element_count());
451 case ngraph::element::Type_t::u64:
453 auto p = tensor->get_data_ptr<ngraph::element::Type_t::u64>();
454 return std::vector<T>(p, p + tensor->get_element_count());
456 default: NGRAPH_UNREACHABLE(
"unsupported element type");
460 std::vector<float> NGRAPH_API read_float_vector(std::shared_ptr<ngraph::runtime::Tensor> tv);
462 std::vector<int64_t> NGRAPH_API read_index_vector(std::shared_ptr<ngraph::runtime::Tensor> tv);
465 std::ostream& operator<<(std::ostream& os,
const ngraph::NodeVector& nv);
A vector of axes.
Definition: axis_vector.hpp:18
Class representing a dimension, which may be dynamic (undetermined until runtime),...
Definition: dimension.hpp:23
static Dimension dynamic()
Create a dynamic dimension.
Definition: dimension.hpp:118
bool is_clear(const EnumMask &p) const
Check if all of the input parameter enum bit mask do not match.
Definition: util.hpp:276
bool is_set(const EnumMask &p) const
Check if all of the input parameter enum bit mask match.
Definition: util.hpp:272
constexpr EnumMask()
Definition: util.hpp:248
bool is_any_clear(const EnumMask &p) const
Check if any of the input parameter enum bit mask does not match.
Definition: util.hpp:274
std::underlying_type< T >::type value_type
Make sure the template type is an enum.
Definition: util.hpp:241
bool is_any_set(const EnumMask &p) const
Check if any of the input parameter enum bit mask match.
Definition: util.hpp:270
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Dimension Rank
Alias for Dimension, used when the value represents the number of axes in a shape,...
Definition: rank.hpp:15
NGRAPH_API void parse_version_string(std::string version, size_t &major, size_t &minor, size_t &patch, std::string &extra)
Function to query parsed version information of the version of ngraph which contains this function....
size_t shape_size(const SHAPE_TYPE &shape)
Number of elements in spanned by a shape.
Definition: shape.hpp:59
T parse_string(const std::string &s)
Parses a string containing a literal of the underlying type.
Definition: util.hpp:136
NGRAPH_API float parse_string< float >(const std::string &s)
NGRAPH_API int8_t parse_string< int8_t >(const std::string &s)