24 #ifndef ARM_COMPUTE_TEST_TENSOR_LIBRARY_H
25 #define ARM_COMPUTE_TEST_TENSOR_LIBRARY_H
39 #include "utils/Utils.h"
46 #include <type_traits>
77 std::string
path()
const;
83 std::random_device::result_type
seed()
const;
194 template <
typename T,
typename D>
207 template <
typename T,
typename D>
210 template <
typename T,
typename D>
223 template <
typename T,
typename D>
224 void fill(std::vector<T> &vec, D &&
distribution, std::random_device::result_type seed_offset)
const;
236 template <
typename D>
249 template <
typename T>
277 template <
typename T>
307 template <
typename T>
332 template <
typename T>
340 template <
typename T>
352 template <
typename T,
typename D>
361 template <
typename T>
363 std::random_device::result_type seed_offset,
364 const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs)
const;
383 template <
typename T>
393 template <
typename T,
typename D>
406 template <
typename T,
typename DataType>
410 template <
typename ResultType>
417 template <
typename T,
typename ResultType>
432 const Extractor &get_extractor(
Format format,
Channel)
const;
433 const Loader &get_loader(
const std::string &extension)
const;
474 const std::string _library_path;
475 std::random_device::result_type _seed;
480 template <
typename T>
481 inline std::vector<std::pair<T, T>>
convert_range_pair(
const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs)
483 std::vector<std::pair<T, T>> converted;
484 std::transform(excluded_range_pairs.begin(),
485 excluded_range_pairs.end(),
486 std::back_inserter(converted),
489 return std::pair<T, T>(static_cast<T>(p.first), static_cast<T>(p.second));
505 template <
typename T,
typename D>
512 if(
tensor.shape().num_dimensions() > 1)
517 std::mt19937 gen(_seed + seed_offset);
524 if(
id.x() < 0 ||
id.x() >=
static_cast<int>(
shape.x()) ||
id.y() < 0 ||
id.y() >=
static_cast<int>(
shape.y()))
526 using ResultType = typename std::remove_reference<D>::type::result_type;
527 const ResultType value = distribution(gen);
528 void *const out_ptr = tensor(id);
529 store_value_with_data_type(out_ptr, value, tensor.data_type());
534 template <
typename T,
typename D>
538 using ResultType =
typename DistributionType::result_type;
540 std::mt19937 gen(_seed + seed_offset);
542 const uint32_t num_boxes =
tensor.num_elements() / 4;
544 DistributionType size_dist{ ResultType(0.f), ResultType(1.f) };
545 for(uint32_t element_idx = 0; element_idx < num_boxes * 4; element_idx += 4)
547 const ResultType delta = size_dist(gen);
548 const ResultType
epsilon = size_dist(gen);
551 const ResultType right = left + delta;
552 const ResultType bottom = top +
epsilon;
553 const std::tuple<ResultType, ResultType, ResultType, ResultType> box(left, top, right, bottom);
558 ResultType &target_value_x1 =
reinterpret_cast<ResultType *
>(
tensor(x1))[0];
559 ResultType &target_value_y1 =
reinterpret_cast<ResultType *
>(
tensor(y1))[0];
560 ResultType &target_value_x2 =
reinterpret_cast<ResultType *
>(
tensor(x2))[0];
561 ResultType &target_value_y2 =
reinterpret_cast<ResultType *
>(
tensor(y2))[0];
570 template <
typename T,
typename D>
575 using ResultType =
typename std::remove_reference<D>::type::result_type;
577 std::mt19937 gen(_seed + seed_offset);
578 for(
size_t i = 0; i < vec.size(); ++i)
586 template <
typename T,
typename ResultType>
599 const uint32_t num_elements =
tensor.num_elements();
600 for(uint32_t element_idx = 0; element_idx < num_elements; ++element_idx)
611 for(
int channel = 0; channel <
tensor.num_channels(); ++channel)
613 const ResultType value = generate_value();
614 ResultType &target_value =
reinterpret_cast<ResultType *
>(
tensor(
id))[channel];
621 template <
typename T,
typename D>
624 using ResultType =
typename std::remove_reference<D>::type::result_type;
625 std::mt19937 gen(_seed + seed_offset);
637 template <
typename T,
typename DataType>
640 auto it = values.begin();
646 if(it == values.end())
657 template <
typename D>
660 std::mt19937 gen(_seed + seed_offset);
664 using ResultType =
typename std::remove_reference<D>::type::result_type;
671 template <
typename T>
686 template <
typename T>
692 template <
typename T>
707 template <
typename T>
720 template <
typename T>
723 switch(
tensor.data_type())
795 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
802 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
809 fill(
tensor, distribution_sizet, seed_offset);
817 template <
typename T>
819 std::random_device::result_type seed_offset,
820 const std::vector<AssetsLibrary::RangePair> &excluded_range_pairs)
const
824 switch(
tensor.data_type())
829 const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs);
831 std::numeric_limits<uint8_t>::max(),
839 const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs);
841 std::numeric_limits<int8_t>::max(),
848 const auto converted_pairs = detail::convert_range_pair<uint16_t>(excluded_range_pairs);
850 std::numeric_limits<uint16_t>::max(),
858 const auto converted_pairs = detail::convert_range_pair<int16_t>(excluded_range_pairs);
860 std::numeric_limits<int16_t>::max(),
867 const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs);
869 std::numeric_limits<uint32_t>::max(),
876 const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs);
878 std::numeric_limits<int32_t>::max(),
886 const auto converted_pairs = detail::convert_range_pair<bfloat16>(excluded_range_pairs);
894 const auto converted_pairs = detail::convert_range_pair<half>(excluded_range_pairs);
902 const auto converted_pairs = detail::convert_range_pair<float>(excluded_range_pairs);
912 template <
typename T,
typename D>
915 switch(
tensor.data_type())
921 std::uniform_int_distribution<uint32_t> distribution_u8(low, high);
930 std::uniform_int_distribution<int32_t> distribution_s8(low, high);
937 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
945 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
952 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
959 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
966 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
973 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
992 std::uniform_real_distribution<float> distribution_f32(low, high);
999 std::uniform_real_distribution<double> distribution_f64(low, high);
1006 std::uniform_int_distribution<size_t> distribution_sizet(low, high);
1007 fill(
tensor, distribution_sizet, seed_offset);
1015 template <
typename T>
1019 const std::string path_separator(
"\\");
1021 const std::string path_separator(
"/");
1023 const std::string
path = _library_path + path_separator +
name;
1026 std::ifstream stream(
path, std::ios::in | std::ios::binary);
1035 if(
tensor.padding().empty())
1038 stream.read(
reinterpret_cast<char *
>(
tensor.data()),
tensor.size());
1048 stream.read(
reinterpret_cast<char *
>(
tensor(
id)),
tensor.element_size());
1053 template <
typename T,
typename D>