clDNN
tensor.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
18 #pragma once
19 #include "cldnn_defs.h"
20 #include "compounds.h"
21 #include "meta_utils.hpp"
22 
23 #include <map>
24 #include <list>
25 #include <cstdint>
26 #include <iostream>
27 #include <numeric>
28 #include <algorithm>
29 #include <sstream>
30 
31 namespace cldnn
32 {
35 
38 
41 {
43  size_t batch_num;
45  size_t feature_num;
47  size_t spatial_num;
49  std::string order;
51  std::string internal_order;
53  static const char* batch_chars() { return "bn"; }
55  static const char* feature_chars() { return "fioc"; }
57  static const char* spatial_chars() { return "xyzhsw"; }
59  static bool is_batch_char(char c) { return std::string(batch_chars()).find_first_of(c) != std::string::npos; }
61  static bool is_feature_char(char c) { return std::string(feature_chars()).find_first_of(c) != std::string::npos; }
63  static bool is_spatial_char(char c) { return std::string(spatial_chars()).find_first_of(c) != std::string::npos; }
64 };
65 
75 struct format
76 {
77  enum type : int32_t
78  {
101  any = cldnn_format_any,
102  };
103 
105  static const format_traits& traits(type fmt)
106  {
107  static const std::map<type, format_traits> traits
108  {
109  { yxfb,{ 1, 1, 2, "yxfb", "bfxy" } },
110  { byxf,{ 1, 1, 2, "byxf", "bfxy" } },
111  { bfyx,{ 1, 1, 2, "bfyx", "bfxy" } },
112  { fyxb,{ 1, 1, 2, "fyxb", "bfxy" } },
113  { os_iyx_osv16, { 1, 1, 2, "bfyx", "bfxy" } },
114  { bs_xs_xsv8_bsv8, { 1, 1, 1, "bx", "b?x?" } },
115  { bs_xs_xsv8_bsv16,{ 1, 1, 1, "bx", "b?x?" } },
116  { bs_x_bsv16, { 1, 1, 1, "bx", "b?x?" } },
117  { bf8_xy16, { 1, 1, 2, "bfyx", "bfxy" }},
118  { image_2d_weights_c4_fyx_b, { 1, 1, 2, "bfyx", "bfxy" } },
119  { image_2d_weights_c1_b_fyx, { 1, 1, 2, "bfyx", "bfxy" } },
120  { winograd_2x3_s1_data, { 1, 1, 2, "bxyf", "bfxy" } },
121  { winograd_2x3_s1_weights, { 1, 1, 2, "bfyx", "bfxy" } },
122  { winograd_2x3_s1_fused_weights, { 1, 1, 2, "xyfb", "bfxy" } } };
123  return traits.at(fmt);
124  }
125 
127  static size_t batch_num(type fmt) { return traits(fmt).batch_num; }
129  static size_t feature_num(type fmt) { return traits(fmt).feature_num; }
131  static size_t spatial_num(type fmt) { return traits(fmt).spatial_num; }
133  static const std::string& order(type fmt) { return traits(fmt).order; }
135  static const std::string& internal_order(type fmt) { return traits(fmt).internal_order; }
137  static size_t dimension(type fmt) { return order(fmt).size(); }
139  static bool is_winograd(type fmt) { return (fmt == winograd_2x3_s1_data || fmt == winograd_2x3_s1_weights || fmt == winograd_2x3_s1_fused_weights); }
141  static bool is_image_2d(type fmt) { return (fmt == image_2d_weights_c4_fyx_b || fmt == image_2d_weights_c1_b_fyx); }
143  static bool is_image(type fmt) { return (is_image_2d(fmt)); }
144 
146  size_t batch_num() const { return traits(value).batch_num; }
148  size_t feature_num() const { return traits(value).feature_num; }
150  size_t spatial_num() const { return traits(value).spatial_num; }
152  const std::string& order() const { return traits(value).order; }
154  const std::string& internal_order() const { return traits(value).internal_order; }
156  size_t dimension() const { return order(value).size(); }
158  bool is_winograd() const { return is_winograd(value); }
160  bool is_image_2d() const { return is_image_2d(value); }
162  bool is_image() const { return is_image(value); }
163 
164  type value;
166  constexpr format(type t) :value(t) {}
168  constexpr operator type() const { return value; }
170  constexpr explicit format(cldnn_format_type t) : value(static_cast<type>(t)) {}
172  constexpr explicit operator cldnn_format_type() const { return static_cast<cldnn_format_type>(value); }
173 };
174 
175 struct tensor;
176 
178 namespace details
179 {
181 enum class dim_vec_kind
182 {
183  batch,
184  feature,
185  spatial
186 };
187 
189 template <dim_vec_kind Kind>
191 {
192  static_assert(meta::always_false_ty_val<dim_vec_kind, Kind>::value, "Limits are undefined for selected value of dim_vec_kind.");
193 };
194 
195 template <>
197 {
198  static constexpr int32_t max_dimentionality = CLDNN_TENSOR_BATCH_DIM_MAX;
199  static constexpr int32_t dim_offset = 0;
200 };
201 
202 template <>
203 struct dim_vec_limits<dim_vec_kind::feature>
204 {
205  static constexpr int32_t max_dimentionality = CLDNN_TENSOR_FEATURE_DIM_MAX;
206  static constexpr int32_t dim_offset = CLDNN_TENSOR_BATCH_DIM_MAX;
207 };
208 
209 template <>
210 struct dim_vec_limits<dim_vec_kind::spatial>
211 {
212  static constexpr int32_t max_dimentionality = CLDNN_TENSOR_SPATIAL_DIM_MAX;
213  static constexpr int32_t dim_offset = CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX;
214 };
215 
217 template <dim_vec_kind Kind>
219 {
220 public:
221  static constexpr auto _max_dimensionality = dim_vec_limits<Kind>::max_dimentionality;
222  static constexpr auto _dimOffset = dim_vec_limits<Kind>::dim_offset;
223 
224  template <typename ... DimTys>
225  explicit dim_vec_kind_init(DimTys&& ... values)
226  : _sizes{ int32_t(std::forward<DimTys>(values)) ... }, _dimSize(sizeof...(DimTys))
227  {
228  }
229 
230  void init_tensor_values(cldnn::tensor& t);
231 
232  int32_t _sizes[_max_dimensionality];
233  int32_t _dimSize;
234 };
235 }
236 
237 template <typename ... InitTys>
239 {
240  return details::dim_vec_kind_init<details::dim_vec_kind::batch>(std::forward<InitTys>(inits) ...);
241 }
242 
243 template <typename ... InitTys>
244 details::dim_vec_kind_init<details::dim_vec_kind::feature> feature(InitTys&& ... inits)
245 {
246  return details::dim_vec_kind_init<details::dim_vec_kind::feature>(std::forward<InitTys>(inits) ...);
247 }
248 
249 template <typename ... InitTys>
250 details::dim_vec_kind_init<details::dim_vec_kind::spatial> spatial(InitTys&& ... inits)
251 {
252  return details::dim_vec_kind_init<details::dim_vec_kind::spatial>(std::forward<InitTys>(inits) ...);
253 }
254 
256 struct tensor
257 {
258  friend class details::dim_vec_kind_init<details::dim_vec_kind::batch>;
259  friend class details::dim_vec_kind_init<details::dim_vec_kind::feature>;
260  friend class details::dim_vec_kind_init<details::dim_vec_kind::spatial>;
261 
262  typedef int32_t value_type;
263  //TODO find the way to prevent direct change of following fields.
264  mutable_array_ref<value_type> raw;
265  mutable_array_ref<value_type> batch;
266  mutable_array_ref<value_type> feature;
267  mutable_array_ref<value_type> spatial;
268 
269 private:
270  value_type _sizes[CLDNN_TENSOR_DIM_MAX];
271  value_type _dimOffset;
272  value_type _dimSize;
273 
274 public:
275  tensor(value_type default_size = 0)
276  : raw(_sizes, CLDNN_TENSOR_DIM_MAX)
277  , batch(_sizes, CLDNN_TENSOR_BATCH_DIM_MAX)
278  , feature(_sizes + CLDNN_TENSOR_BATCH_DIM_MAX, CLDNN_TENSOR_FEATURE_DIM_MAX)
279  , spatial(_sizes + CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX, CLDNN_TENSOR_SPATIAL_DIM_MAX)
280  {
281  std::fill_n(_sizes, CLDNN_TENSOR_DIM_MAX, default_size);
282  }
283 
287 
297  template <typename ... KindInitTys,
298  typename = typename std::enable_if<
299  meta::all<
300  meta::is_any_of<KindInitTys,
304  >::value...
305  >::value, void>::type>
306  tensor(KindInitTys&& ... kind_inits)
307  : tensor(1)
308  {
309  assign_inits(std::forward<KindInitTys>(kind_inits) ...);
310  }
311 
314 
324  tensor(value_type batch_num, value_type feature_num, value_type width, value_type height)
325  : tensor(1)
326  {
327  _sizes[0] = batch_num;
328  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX] = feature_num;
329  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX] = width;
330  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX + 1] = height;
331  }
332 
337 
347  tensor(const std::vector<value_type>& sizes, value_type default_size = 1)
348  : tensor(default_size)
349  {
350  _sizes[0] = sizes[0];
351  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX] = sizes[CLDNN_TENSOR_BATCH_DIM_MAX];
352  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX] = sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX];
353  _sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX + 1] = sizes[CLDNN_TENSOR_BATCH_DIM_MAX + CLDNN_TENSOR_FEATURE_DIM_MAX + 1];
354  }
355 
356  tensor(format fmt, const std::vector<value_type>& sizes, value_type default_size = 1)
357  : tensor(default_size)
358  {
359  auto in_order = fmt.order();
360  auto out_order = fmt.internal_order();
361  if (in_order.size() != sizes.size())
362  throw std::invalid_argument("The count of values passed to initialize tensor does not match passed format.");
363 
364  for (size_t out_idx = 0; out_idx < out_order.size(); ++out_idx)
365  {
366  auto channel = out_order[out_idx];
367  if (channel == '?')
368  continue;
369 
370  auto in_idx = in_order.find(channel);
371  if (in_idx == in_order.npos)
372  throw std::runtime_error("Internal order of a format contains channel which does not appear in external order.");
373 
374  _sizes[out_idx] = sizes[in_idx];
375  }
376  }
377 
379  tensor(const cldnn_tensor& other)
380  : tensor()
381  {
382  std::copy_n(other.sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
383  }
384 
386  operator cldnn_tensor() const
387  {
388  cldnn_tensor result;
389  result.batch_num = batch.size();
390  result.feature_num = feature.size();
391  result.spatial_num = spatial.size();
392  std::copy_n(_sizes, CLDNN_TENSOR_DIM_MAX, result.sizes);
393  return result;
394  }
395 
397  tensor(const tensor& other)
398  : tensor()
399  {
400  std::copy_n(other._sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
401  }
402 
404  tensor& operator=(const tensor& other)
405  {
406  if (this == &other)
407  return *this;
408  std::copy_n(other._sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
409  return *this;
410  }
411 
412  friend bool operator==(const tensor& lhs, const tensor& rhs)
413  {
414  return lhs.raw.size() == rhs.raw.size()
415  && std::equal(lhs.raw.begin(), lhs.raw.end(), rhs.raw.begin());
416  }
417 
418  friend bool operator!=(const tensor& lhs, const tensor& rhs)
419  {
420  return !(lhs == rhs);
421  }
422 
423  friend bool operator<(const tensor& lhs, const tensor& rhs)
424  {
425  if (lhs.raw.size() != rhs.raw.size())
426  return lhs.raw.size() < rhs.raw.size();
427  for (size_t i = 0; i < lhs.raw.size(); ++i)
428  if (lhs.raw[i] < rhs.raw[i])
429  return true;
430 
431  return false;
432  }
433 
434  friend std::ostream& operator<<(std::ostream& os, const tensor& tensor)
435  {
436  os << tensor.to_string();
437  return os;
438  }
439 
440  std::string to_string() const
441  {
442  std::stringstream out;
443  const char* delim = "";
444 
445  out << "[b:";
446  for (size_t i = 0; i < batch.size(); ++i)
447  {
448  out << delim << batch[i];
449  delim = ",";
450  }
451  delim = "";
452 
453  out << ", f:";
454  for (size_t i = 0; i < feature.size(); ++i)
455  {
456  out << delim << feature[i];
457  delim = ",";
458  }
459 
460  std::vector<std::string> spatial_dim_names = { ", x", ", y", ", z", ", w" };
461  for (size_t i = 0; i < spatial.size(); ++i)
462  {
463  out << spatial_dim_names[i] << ":" << spatial[i];
464  }
465  out << "]";
466 
467  return out.str();
468  }
469 
471  tensor negate() const
472  {
473  auto result = *this;
474  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
475  {
476  result._sizes[i] = -_sizes[i];
477  }
478  return result;
479  }
480 
482  tensor mul(value_type multiplier) const
483  {
484  auto result = *this;
485  for(size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++ )
486  {
487  result._sizes[i] *= multiplier;
488  }
489  return result;
490  }
491 
493  tensor div(value_type divider) const
494  {
495  auto result = *this;
496  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
497  {
498  result._sizes[i] /= divider;
499  }
500  return result;
501  }
502 
504  tensor add(const tensor& rhs) const
505  {
506  auto result = *this;
507  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
508  {
509  result._sizes[i] += rhs._sizes[i];
510  }
511  return result;
512  }
513 
515  tensor sub(const tensor& rhs) const
516  {
517  return add(rhs.negate());
518  }
519 
521  tensor& operator+=(const tensor& rhs)
522  {
523  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
524  _sizes[i] += rhs._sizes[i];
525  return *this;
526  }
527 
529  tensor& operator-=(const tensor& rhs)
530  {
531  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
532  _sizes[i] -= rhs._sizes[i];
533  return *this;
534  }
535 
537  std::vector<value_type> sizes(cldnn::format fmt) const {
538  auto output_order = fmt.order();
539  auto internal_order = fmt.internal_order();
540  std::vector<value_type> sizes(output_order.size(), 0);
541 
542  for (size_t i = 0; i < sizes.size(); ++i)
543  {
544  auto c = output_order[i];
545  auto pos = internal_order.find(c);
546  if (pos == internal_order.npos)
547  throw std::domain_error(std::string("Unknown coord type: ") + c);
548 
549  sizes[i] = _sizes[pos];
550  }
551 
552  return sizes;
553  }
554 
556  std::vector<value_type> sizes() const {
557  std::vector<value_type> sizes(sizeof(_sizes) / sizeof(_sizes[0]), 0);
558  for (size_t i = 0; i < sizes.size(); ++i)
559  sizes[i] = _sizes[i];
560  return sizes;
561  }
562 
564  size_t count() const {
565  return std::accumulate(
566  raw.begin(),
567  raw.end(),
568  static_cast<size_t>(1),
569  std::multiplies<size_t>()
570  );
571  }
572 
577 
593  tensor transform(cldnn::format new_fmt, value_type default_size) const
594  {
596  auto val_order = format.internal_order();
597  auto new_order = new_fmt.internal_order();
598  std::vector<value_type> old_sizes = sizes();
599  std::vector<value_type> new_sizes(old_sizes.size(), default_size);
600  auto tmp = 1;
601  for(size_t i = 0; i < format.order().size(); i++)
602  {
603  auto c = val_order[i];
604  //skip f or b and y for the formats that do not have it
605  if (((new_fmt == format::bs_xs_xsv8_bsv8) || (new_fmt == format::bs_xs_xsv8_bsv16) || (new_fmt == format::bs_x_bsv16)) && ((c == 'f') || (c == 'y')))
606  {
607  if (new_order[i] == '?')
608  new_sizes[i] = default_size;
609 
610  tmp *= old_sizes[i];
611  continue;
612  }
613 
614  auto new_pos = new_order.find(c);
615  if (new_pos == std::string::npos)
616  throw std::invalid_argument("cannot convert to new format");
617  new_sizes[new_pos] = old_sizes[i];
618  }
619 
620  //in case of formats with smaller number of dimensions than input, flatten is performed below
621  if (tmp != 1)
622  {
623  for (size_t i = 0; i < format.order().size(); i++)
624  {
625  auto c = val_order[i];
626  if (c == 'x')
627  {
628  auto new_pos = new_order.find(c);
629  new_sizes[new_pos] *= tmp;
630  }
631  }
632  }
633 
634  return { new_sizes };
635  }
636 
639  size_t get_linear_offset(const tensor& coord, cldnn::format fmt) const
640  {
641  auto my_sizes = this->sizes();
642  auto adjusted_coords = coord.sizes();
643  if (fmt == cldnn::format::os_iyx_osv16 && !is_aligned_to(my_sizes[0], 16))
644  {
645  my_sizes[0] = align_to(my_sizes[0], 16);
646  adjusted_coords[0] = align_to(adjusted_coords[0], 16);
647  }
648  else if (fmt == cldnn::format::bs_xs_xsv8_bsv8 && !(is_aligned_to(my_sizes[0], 8) && is_aligned_to(my_sizes[1], 8)))
649  {
650  my_sizes[0] = align_to(my_sizes[0], 8);
651  my_sizes[1] = align_to(my_sizes[1], 8);
652  adjusted_coords[0] = align_to(adjusted_coords[0], 8);
653  adjusted_coords[1] = align_to(adjusted_coords[1], 8);
654  }
655  else if (fmt == cldnn::format::bs_xs_xsv8_bsv16 && !(is_aligned_to(my_sizes[0], 16) && is_aligned_to(my_sizes[1], 8)))
656  {
657  my_sizes[0] = align_to(my_sizes[0], 16);
658  my_sizes[1] = align_to(my_sizes[1], 8);
659  adjusted_coords[0] = align_to(adjusted_coords[0], 16);
660  adjusted_coords[1] = align_to(adjusted_coords[1], 8);
661  }
662  else if (fmt == cldnn::format::bs_x_bsv16 && !is_aligned_to(my_sizes[0], 16))
663  {
664  my_sizes[0] = align_to(my_sizes[0], 16);
665  adjusted_coords[0] = align_to(adjusted_coords[0], 16);
666  }
667  else if (fmt == cldnn::format::bf8_xy16 && !(is_aligned_to(my_sizes[1], 8) && is_aligned_to(my_sizes[2] * my_sizes[3], 16)))
668  {
669  my_sizes[1] = align_to(my_sizes[1], 8);
670  my_sizes[3] = align_to(my_sizes[2] * my_sizes[3], 16);
671  my_sizes[2] = 1;
672  adjusted_coords[1] = align_to(adjusted_coords[1], 8);
673  adjusted_coords[3] = align_to(adjusted_coords[3], 16);
674  adjusted_coords[2] = 1;
675  }
676 
677  assert(my_sizes.size() == adjusted_coords.size());
678 
679  assert(adjusted_coords.size() > 0);
680  size_t offset = adjusted_coords[0];
681  for(size_t i = 1; i < adjusted_coords.size(); i++ )
682  {
683  offset = offset * my_sizes[i - 1] + adjusted_coords[i];
684  }
685  return offset;
686  }
687 
689  static tensor max(tensor const& lhs, tensor const& rhs)
690  {
691  auto ret = lhs;
692  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; ++i)
693  ret._sizes[i] = std::max(ret._sizes[i], rhs._sizes[i]);
694 
695  return ret;
696  }
697 
699  static tensor min(tensor const& lhs, tensor const& rhs)
700  {
701  auto ret = lhs;
702  for (size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; ++i)
703  ret._sizes[i] = std::min(ret._sizes[i], rhs._sizes[i]);
704 
705  return ret;
706  }
707 
708 private:
709 
711  template <typename KindInitT>
712  void assign_inits(KindInitT&& init)
713  {
714  init.init_tensor_values(*this);
715  }
716 
717  template <typename KindInitT, typename ... KindInitTys>
718  void assign_inits(KindInitT&& init, KindInitTys&& ... kind_inits)
719  {
720  init.init_tensor_values(*this);
721  assign_inits(std::forward<KindInitTys>(kind_inits) ...);
722  }
723 };
724 
725 
726 template<details::dim_vec_kind Kind>
727 inline void details::dim_vec_kind_init<Kind>::init_tensor_values(cldnn::tensor & t)
728 {
729  for (size_t i = _dimOffset; i < (size_t)(_dimOffset + _dimSize); i++)
730  t._sizes[i] = _sizes[i - _dimOffset];
731 }
732 
734 inline tensor operator+(const tensor& lhs, const tensor& rhs) { return lhs.add(rhs); }
736 inline tensor operator-(const tensor& lhs, const tensor& rhs) { return lhs.sub(rhs); }
738 inline tensor operator*(const tensor& lhs, tensor::value_type rhs) { return lhs.mul(rhs); }
740 inline tensor operator/(const tensor& lhs, tensor::value_type rhs) { return lhs.div(rhs); }
741 
744 }
static bool is_winograd(type fmt)
Checks if format is a winograd format.
Definition: tensor.hpp:139
tensor operator*(const tensor &lhs, tensor::value_type rhs)
Multiplies a tensor to a scalar.
Definition: tensor.hpp:738
static bool is_spatial_char(char c)
Checks if c represents spatial dimension.
Definition: tensor.hpp:63
tensor & operator=(const tensor &other)
Copy assignment.
Definition: tensor.hpp:404
template class with max_dimensionalities and dimension offset for dimension kinds ...
Definition: tensor.hpp:190
tensor(const std::vector< value_type > &sizes, value_type default_size=1)
Constructs tensor using vector of sizes.
Definition: tensor.hpp:347
mutable_array_ref< value_type > feature
Feature maps.
Definition: tensor.hpp:266
tensor mul(value_type multiplier) const
Returns a tensor with all elements multilied to multiplier.
Definition: tensor.hpp:482
mutable_array_ref< value_type > raw
Raw representation of all dimensions.
Definition: tensor.hpp:264
static size_t dimension(type fmt)
Returns number of dimensions contained within a format.
Definition: tensor.hpp:137
tensor(const cldnn_tensor &other)
Implicit conversion form C API :: cldnn_tensor.
Definition: tensor.hpp:379
tensor operator-(const tensor &lhs, const tensor &rhs)
Subtracts two tensors.
Definition: tensor.hpp:736
format used for weights for winograd fused convolution, F(2,3) – filter 3x3 with stride 1 ...
Definition: tensor.hpp:99
size_t batch_num() const
Returns number of batch dimensions.
Definition: tensor.hpp:146
size_t count() const
Returns tensor elements count calculated as multiplication of all elements.
Definition: tensor.hpp:564
std::vector< value_type > sizes() const
Returns a vector of tensors values, ordered batch, feature, spatial_x, spatial_y. ...
Definition: tensor.hpp:556
tensor(value_type batch_num, value_type feature_num, value_type width, value_type height)
Constructs tensor.
Definition: tensor.hpp:324
static tensor min(tensor const &lhs, tensor const &rhs)
Returns a tensor containing values minimum from lhs and rhs.
Definition: tensor.hpp:699
static const char * batch_chars()
Characters representing batch dimensions in an order.
Definition: tensor.hpp:53
tensor sub(const tensor &rhs) const
Returns a tensor with all elements subtracted by appropriate elements of rhs.
Definition: tensor.hpp:515
size_t spatial_num() const
Returns number of spatial dimensions.
Definition: tensor.hpp:150
static tensor max(tensor const &lhs, tensor const &rhs)
Returns a tensor containing values maximum from lhs and rhs.
Definition: tensor.hpp:689
mutable_array_ref< value_type > batch
Batch dimensions.
Definition: tensor.hpp:265
N-dimensional vector. Mostly used to represent memory size.
Definition: tensor.hpp:256
cldnn_format_type
Represents memory formats (orders). In CNN most of data is describe as 4 dimensional blocks...
Definition: cldnn.h:233
tensor transform(cldnn::format new_fmt, value_type default_size) const
Returns new tensor based on current but transformed to new format.
Definition: tensor.hpp:593
static const char * feature_chars()
Characters representing feature map/channel dimensions in an order.
Definition: tensor.hpp:55
static bool is_feature_char(char c)
Checks if c represents feature map/channel dimension.
Definition: tensor.hpp:61
tensor(KindInitTys &&... kind_inits)
Constructs tensor.
Definition: tensor.hpp:306
tensor negate() const
Returns a tensor with all negated elements.
Definition: tensor.hpp:471
static const char * spatial_chars()
Characters representing spatial dimensions in an order.
Definition: tensor.hpp:57
bool is_image_2d() const
Checks if format is of image 2d type.
Definition: tensor.hpp:160
number of format types
Definition: tensor.hpp:100
tensor & operator-=(const tensor &rhs)
Assign and subtract.
Definition: tensor.hpp:529
std::string internal_order
Dimensions order for internal storage.
Definition: tensor.hpp:51
size_t feature_num
Number of feature map/channel dimensions in a format.
Definition: tensor.hpp:45
Format information helper class.
Definition: tensor.hpp:40
mutable_array_ref< value_type > spatial
Spatial dimensions.
Definition: tensor.hpp:267
tensor(const tensor &other)
Copy construction.
Definition: tensor.hpp:397
size_t feature_num() const
Returns number of feature dimensions.
Definition: tensor.hpp:148
static const format_traits & traits(type fmt)
Get format traits for particular format::type.
Definition: tensor.hpp:105
format not used inside clDNN, but supported in reorder as extension for user provided formats...
Definition: cldnn.h:238
tensor operator+(const tensor &lhs, const tensor &rhs)
Adds two tensors.
Definition: tensor.hpp:734
format not used inside clDNN, but supported in reorder as extension for user provided formats...
Definition: tensor.hpp:82
batch first, feature and than spatials
Definition: tensor.hpp:79
format used for input for winograd convolution, F(2,3) – filter 3x3 with stride 1 ...
Definition: tensor.hpp:97
batch first, feature and than spatials
Definition: cldnn.h:235
Template class used in tensor constructor using dim_vec_kinds.
Definition: tensor.hpp:218
const std::string & order() const
Returns an order of dimensions in form of string.
Definition: tensor.hpp:152
tensor & operator+=(const tensor &rhs)
Assign and add.
Definition: tensor.hpp:521
size_t get_linear_offset(const tensor &coord, cldnn::format fmt) const
Calculates linear offset for given coord within current tensor.
Definition: tensor.hpp:639
size_t batch_num
Number of batch dimensions in a format.
Definition: tensor.hpp:43
static const std::string & order(type fmt)
Returns an order of dimensions for a @ format.
Definition: tensor.hpp:133
number of format types
Definition: cldnn.h:253
int32_t value_type
Values type stored in tensor.
Definition: tensor.hpp:262
tensor add(const tensor &rhs) const
Returns a tensor with all elements added by appropriate elements of rhs.
Definition: tensor.hpp:504
size_t spatial_num
Number of spatial (x,y) dimensions in a format.
Definition: tensor.hpp:47
format used for weights for winograd non-fused convolution, F(2,3) – filter 3x3 with stride 1 ...
Definition: tensor.hpp:98
static bool is_batch_char(char c)
Checks if c represents batch dimension.
Definition: tensor.hpp:59
constexpr format(type t)
Implicit conversion from format::type.
Definition: tensor.hpp:166
Represents memory formats (orders). In CNN most of data is described as 4 dimensional blocks...
Definition: tensor.hpp:75
the most common format for activations in clDNN.
Definition: tensor.hpp:81
static size_t feature_num(type fmt)
Returns number of feature dimensions for a format.
Definition: tensor.hpp:129
std::vector< value_type > sizes(cldnn::format fmt) const
Returns a vector of tensors values, ordered regarding to format.
Definition: tensor.hpp:537
constexpr format(cldnn_format_type t)
Conversion from C API cldnn_format_type.
Definition: tensor.hpp:170
used in bitmaps, input from user i.e b images of RGB format
Definition: tensor.hpp:80
tensor operator/(const tensor &lhs, tensor::value_type rhs)
Divides a tensor by a scalar.
Definition: tensor.hpp:740
static const std::string & internal_order(type fmt)
Returns an internal orders of dimensions for a format.
Definition: tensor.hpp:135
static size_t spatial_num(type fmt)
Returns number of spatial dimensions for a format.
Definition: tensor.hpp:131
the most common format for activations in clDNN.
Definition: cldnn.h:237
size_t dimension() const
Returns number of dimensions contained within this format.
Definition: tensor.hpp:156
static bool is_image(type fmt)
Checks if format is of image type.
Definition: tensor.hpp:143
tensor div(value_type divider) const
Returns a tensor with all elements divided by divider.
Definition: tensor.hpp:493
used in bitmaps, input from user i.e b images of RGB format
Definition: cldnn.h:236
static bool is_image_2d(type fmt)
Checks if format is of image2d type.
Definition: tensor.hpp:141
const std::string & internal_order() const
Returns an internal orders of dimensions form of string.
Definition: tensor.hpp:154
bool is_winograd() const
Checks if format is a winograd format.
Definition: tensor.hpp:158
std::string order
Dimensions changing order from rare to often.
Definition: tensor.hpp:49
N-dimensional vector. Mostly used to represent memory size.
Definition: cldnn.h:266
dim_vec_kind
enum class that represent dimension kinds
Definition: tensor.hpp:181
static size_t batch_num(type fmt)
Returns number of batch dimensions for a format.
Definition: tensor.hpp:127
bool is_image() const
Checks if format is of image type.
Definition: tensor.hpp:162