19 #include "cldnn_defs.h" 20 #include "compounds.h" 21 #include "meta_utils.hpp" 101 any = cldnn_format_any,
107 static const std::map<type, format_traits>
traits 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" } },
117 {
bf8_xy16, { 1, 1, 2,
"bfyx",
"bfxy" }},
168 constexpr
operator type()
const {
return value; }
189 template <dim_vec_kind Kind>
198 static constexpr int32_t max_dimentionality = CLDNN_TENSOR_BATCH_DIM_MAX;
199 static constexpr int32_t dim_offset = 0;
205 static constexpr int32_t max_dimentionality = CLDNN_TENSOR_FEATURE_DIM_MAX;
206 static constexpr int32_t dim_offset = CLDNN_TENSOR_BATCH_DIM_MAX;
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;
217 template <dim_vec_kind Kind>
224 template <
typename ... DimTys>
226 : _sizes{ int32_t(std::forward<DimTys>(values)) ... }, _dimSize(
sizeof...(DimTys))
232 int32_t _sizes[_max_dimensionality];
237 template <
typename ... InitTys>
243 template <
typename ... InitTys>
244 details::dim_vec_kind_init<details::dim_vec_kind::feature> feature(InitTys&& ... inits)
246 return details::dim_vec_kind_init<details::dim_vec_kind::feature>(std::forward<InitTys>(inits) ...);
249 template <
typename ... InitTys>
250 details::dim_vec_kind_init<details::dim_vec_kind::spatial> spatial(InitTys&& ... inits)
252 return details::dim_vec_kind_init<details::dim_vec_kind::spatial>(std::forward<InitTys>(inits) ...);
264 mutable_array_ref<value_type>
raw;
265 mutable_array_ref<value_type>
batch;
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)
281 std::fill_n(_sizes, CLDNN_TENSOR_DIM_MAX, default_size);
297 template <
typename ... KindInitTys,
298 typename =
typename std::enable_if<
305 >::value,
void>::type>
309 assign_inits(std::forward<KindInitTys>(kind_inits) ...);
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;
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];
359 auto in_order = fmt.
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.");
364 for (
size_t out_idx = 0; out_idx < out_order.size(); ++out_idx)
366 auto channel = out_order[out_idx];
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.");
374 _sizes[out_idx] =
sizes[in_idx];
382 std::copy_n(other.sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
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);
400 std::copy_n(other._sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
408 std::copy_n(other._sizes, CLDNN_TENSOR_DIM_MAX, _sizes);
412 friend bool operator==(
const tensor& lhs,
const tensor& rhs)
414 return lhs.
raw.size() == rhs.
raw.size()
415 && std::equal(lhs.
raw.begin(), lhs.
raw.end(), rhs.
raw.begin());
418 friend bool operator!=(
const tensor& lhs,
const tensor& rhs)
420 return !(lhs == rhs);
423 friend bool operator<(
const tensor& lhs,
const tensor& rhs)
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])
434 friend std::ostream& operator<<(std::ostream& os,
const tensor& tensor)
436 os << tensor.to_string();
440 std::string to_string()
const 442 std::stringstream out;
443 const char* delim =
"";
446 for (
size_t i = 0; i <
batch.size(); ++i)
448 out << delim <<
batch[i];
454 for (
size_t i = 0; i <
feature.size(); ++i)
460 std::vector<std::string> spatial_dim_names = {
", x",
", y",
", z",
", w" };
461 for (
size_t i = 0; i <
spatial.size(); ++i)
463 out << spatial_dim_names[i] <<
":" <<
spatial[i];
474 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
476 result._sizes[i] = -_sizes[i];
485 for(
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++ )
487 result._sizes[i] *= multiplier;
496 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
498 result._sizes[i] /= divider;
507 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
509 result._sizes[i] += rhs._sizes[i];
523 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
524 _sizes[i] += rhs._sizes[i];
531 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; i++)
532 _sizes[i] -= rhs._sizes[i];
538 auto output_order = fmt.
order();
540 std::vector<value_type>
sizes(output_order.size(), 0);
542 for (
size_t i = 0; i <
sizes.size(); ++i)
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);
549 sizes[i] = _sizes[pos];
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];
565 return std::accumulate(
568 static_cast<size_t>(1),
569 std::multiplies<size_t>()
598 std::vector<value_type> old_sizes =
sizes();
599 std::vector<value_type> new_sizes(old_sizes.size(), default_size);
603 auto c = val_order[i];
607 if (new_order[i] ==
'?')
608 new_sizes[i] = default_size;
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];
625 auto c = val_order[i];
628 auto new_pos = new_order.find(c);
629 new_sizes[new_pos] *= tmp;
634 return { new_sizes };
641 auto my_sizes = this->
sizes();
642 auto adjusted_coords = coord.
sizes();
645 my_sizes[0] = align_to(my_sizes[0], 16);
646 adjusted_coords[0] = align_to(adjusted_coords[0], 16);
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);
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);
664 my_sizes[0] = align_to(my_sizes[0], 16);
665 adjusted_coords[0] = align_to(adjusted_coords[0], 16);
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)))
669 my_sizes[1] = align_to(my_sizes[1], 8);
670 my_sizes[3] = align_to(my_sizes[2] * my_sizes[3], 16);
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;
677 assert(my_sizes.size() == adjusted_coords.size());
679 assert(adjusted_coords.size() > 0);
680 size_t offset = adjusted_coords[0];
681 for(
size_t i = 1; i < adjusted_coords.size(); i++ )
683 offset = offset * my_sizes[i - 1] + adjusted_coords[i];
692 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; ++i)
693 ret._sizes[i] =
std::max(ret._sizes[i], rhs._sizes[i]);
702 for (
size_t i = 0; i < CLDNN_TENSOR_DIM_MAX; ++i)
703 ret._sizes[i] = std::min(ret._sizes[i], rhs._sizes[i]);
711 template <
typename KindInitT>
712 void assign_inits(KindInitT&& init)
714 init.init_tensor_values(*
this);
717 template <
typename KindInitT,
typename ... KindInitTys>
718 void assign_inits(KindInitT&& init, KindInitTys&& ... kind_inits)
720 init.init_tensor_values(*
this);
721 assign_inits(std::forward<KindInitTys>(kind_inits) ...);
726 template<details::dim_vec_kind Kind>
727 inline void details::dim_vec_kind_init<Kind>::init_tensor_values(
cldnn::tensor & t)
729 for (
size_t i = _dimOffset; i < (size_t)(_dimOffset + _dimSize); i++)
730 t._sizes[i] = _sizes[i - _dimOffset];
tensor operator*(const tensor &lhs, tensor::value_type rhs)
Multiplies a tensor to a scalar.
tensor & operator=(const tensor &other)
Copy assignment.
template class with max_dimensionalities and dimension offset for dimension kinds ...
tensor(const std::vector< value_type > &sizes, value_type default_size=1)
Constructs tensor using vector of sizes.
mutable_array_ref< value_type > feature
Feature maps.
tensor mul(value_type multiplier) const
Returns a tensor with all elements multilied to multiplier.
mutable_array_ref< value_type > raw
Raw representation of all dimensions.
tensor(const cldnn_tensor &other)
Implicit conversion form C API :: cldnn_tensor.
tensor operator-(const tensor &lhs, const tensor &rhs)
Subtracts two tensors.
size_t count() const
Returns tensor elements count calculated as multiplication of all elements.
std::vector< value_type > sizes() const
Returns a vector of tensors values, ordered batch, feature, spatial_x, spatial_y. ...
tensor(value_type batch_num, value_type feature_num, value_type width, value_type height)
Constructs tensor.
static tensor min(tensor const &lhs, tensor const &rhs)
Returns a tensor containing values minimum from lhs and rhs.
tensor sub(const tensor &rhs) const
Returns a tensor with all elements subtracted by appropriate elements of rhs.
static tensor max(tensor const &lhs, tensor const &rhs)
Returns a tensor containing values maximum from lhs and rhs.
mutable_array_ref< value_type > batch
Batch dimensions.
N-dimensional vector. Mostly used to represent memory size.
cldnn_format_type
Represents memory formats (orders). In CNN most of data is describe as 4 dimensional blocks...
tensor transform(cldnn::format new_fmt, value_type default_size) const
Returns new tensor based on current but transformed to new format.
tensor(KindInitTys &&... kind_inits)
Constructs tensor.
tensor negate() const
Returns a tensor with all negated elements.
tensor & operator-=(const tensor &rhs)
Assign and subtract.
mutable_array_ref< value_type > spatial
Spatial dimensions.
tensor(const tensor &other)
Copy construction.
format not used inside clDNN, but supported in reorder as extension for user provided formats...
tensor operator+(const tensor &lhs, const tensor &rhs)
Adds two tensors.
batch first, feature and than spatials
Template class used in tensor constructor using dim_vec_kinds.
tensor & operator+=(const tensor &rhs)
Assign and add.
size_t get_linear_offset(const tensor &coord, cldnn::format fmt) const
Calculates linear offset for given coord within current tensor.
int32_t value_type
Values type stored in tensor.
tensor add(const tensor &rhs) const
Returns a tensor with all elements added by appropriate elements of rhs.
std::vector< value_type > sizes(cldnn::format fmt) const
Returns a vector of tensors values, ordered regarding to format.
tensor operator/(const tensor &lhs, tensor::value_type rhs)
Divides a tensor by a scalar.
the most common format for activations in clDNN.
tensor div(value_type divider) const
Returns a tensor with all elements divided by divider.
used in bitmaps, input from user i.e b images of RGB format
N-dimensional vector. Mostly used to represent memory size.
dim_vec_kind
enum class that represent dimension kinds