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>
208 void fill(T &&tensor, D &&
distribution, std::random_device::result_type seed_offset)
const;
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>
250 void fill(T &&tensor,
const std::string &name,
Format format)
const;
277 template <
typename T>
278 void fill(T &&tensor,
const std::string &name,
Channel channel)
const;
307 template <
typename T>
308 void fill(T &&tensor,
const std::string &name,
Format format,
Channel channel)
const;
332 template <
typename T>
340 template <
typename T>
352 template <
typename T,
typename D>
353 void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset, D low, D high)
const;
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>
417 template <
typename ResultType>
418 using GeneratorFunctionType = std::function<ResultType(void)>;
424 const Extractor &get_extractor(
Format format,
Channel)
const;
425 const Loader &get_loader(
const std::string &extension)
const;
434 RawTensor load_image(
const std::string &name)
const;
447 const RawTensor &find_or_create_raw_tensor(
const std::string &name,
Format format)
const;
461 const RawTensor &find_or_create_raw_tensor(
const std::string &name,
Format format,
Channel channel)
const;
468 template <
typename T,
typename ResultType>
469 void fill_with_generator(T &&tensor,
const GeneratorFunctionType<ResultType> &generate_value)
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;
528 void *
const out_ptr = tensor(
id);
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>
587 void AssetsLibrary::fill_with_generator(T &&tensor,
const GeneratorFunctionType<ResultType> &generate_value)
const 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);
627 GeneratorFunctionType<ResultType> number_generator = [&]()
633 fill_with_generator(tensor, number_generator);
637 template <
typename T,
typename DataType>
640 auto it = values.begin();
641 GeneratorFunctionType<DataType> get_next_value = [&]()
646 if(it == values.end())
654 fill_with_generator(tensor, get_next_value);
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())
729 fill(tensor, distribution_u8, seed_offset);
738 fill(tensor, distribution_s8, seed_offset);
744 fill(tensor, distribution_u16, seed_offset);
751 fill(tensor, distribution_s16, seed_offset);
757 fill(tensor, distribution_u32, seed_offset);
763 fill(tensor, distribution_s32, seed_offset);
769 fill(tensor, distribution_u64, seed_offset);
775 fill(tensor, distribution_s64, seed_offset);
782 fill(tensor, distribution_bf16, seed_offset);
789 fill(tensor, distribution_f16, seed_offset);
795 std::uniform_real_distribution<float> distribution_f32(-1000.f, 1000.f);
796 fill(tensor, distribution_f32, seed_offset);
802 std::uniform_real_distribution<double> distribution_f64(-1000.f, 1000.f);
803 fill(tensor, distribution_f64, seed_offset);
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<uint8_t>(excluded_range_pairs);
831 std::numeric_limits<uint8_t>::max(),
833 fill(tensor, distribution_u8, seed_offset);
839 const auto converted_pairs = detail::convert_range_pair<int8_t>(excluded_range_pairs);
841 std::numeric_limits<int8_t>::max(),
843 fill(tensor, distribution_s8, seed_offset);
848 const auto converted_pairs = detail::convert_range_pair<uint16_t>(excluded_range_pairs);
850 std::numeric_limits<uint16_t>::max(),
852 fill(tensor, distribution_u16, seed_offset);
858 const auto converted_pairs = detail::convert_range_pair<int16_t>(excluded_range_pairs);
860 std::numeric_limits<int16_t>::max(),
862 fill(tensor, distribution_s16, seed_offset);
867 const auto converted_pairs = detail::convert_range_pair<uint32_t>(excluded_range_pairs);
869 std::numeric_limits<uint32_t>::max(),
871 fill(tensor, distribution_u32, seed_offset);
876 const auto converted_pairs = detail::convert_range_pair<int32_t>(excluded_range_pairs);
878 std::numeric_limits<int32_t>::max(),
880 fill(tensor, distribution_s32, seed_offset);
886 const auto converted_pairs = detail::convert_range_pair<bfloat16>(excluded_range_pairs);
888 fill(tensor, distribution_bf16, seed_offset);
894 const auto converted_pairs = detail::convert_range_pair<half>(excluded_range_pairs);
896 fill(tensor, distribution_f16, seed_offset);
902 const auto converted_pairs = detail::convert_range_pair<float>(excluded_range_pairs);
904 fill(tensor, distribution_f32, seed_offset);
912 template <
typename T,
typename D>
915 switch(tensor.data_type())
921 std::uniform_int_distribution<uint8_t> distribution_u8(low, high);
922 fill(tensor, distribution_u8, seed_offset);
930 std::uniform_int_distribution<int8_t> distribution_s8(low, high);
931 fill(tensor, distribution_s8, seed_offset);
937 std::uniform_int_distribution<uint16_t> distribution_u16(low, high);
938 fill(tensor, distribution_u16, seed_offset);
945 std::uniform_int_distribution<int16_t> distribution_s16(low, high);
946 fill(tensor, distribution_s16, seed_offset);
952 std::uniform_int_distribution<uint32_t> distribution_u32(low, high);
953 fill(tensor, distribution_u32, seed_offset);
959 std::uniform_int_distribution<int32_t> distribution_s32(low, high);
960 fill(tensor, distribution_s32, seed_offset);
966 std::uniform_int_distribution<uint64_t> distribution_u64(low, high);
967 fill(tensor, distribution_u64, seed_offset);
973 std::uniform_int_distribution<int64_t> distribution_s64(low, high);
974 fill(tensor, distribution_s64, seed_offset);
980 fill(tensor, distribution_bf16, seed_offset);
986 fill(tensor, distribution_f16, seed_offset);
992 std::uniform_real_distribution<float> distribution_f32(low, high);
993 fill(tensor, distribution_f32, seed_offset);
999 std::uniform_real_distribution<double> distribution_f64(low, high);
1000 fill(tensor, distribution_f64, seed_offset);
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>
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
unsigned int top
top of the border
size_t element_size() const override
Size of each element in the tensor in bytes.
Subclass of SimpleTensor using uint8_t as value type.
Stores RawTensor categorised by the image they are created from including name, format and channel...
size_t size() const override
Total size of the tensor in bytes.
quantized, symmetric fixed-point 16-bit number
Container for 2D border size.
Factory class to create and fill tensors.
std::string path() const
Path to assets directory used to initialise library.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Format get_format_for_channel(Channel channel)
Look up the format corresponding to a channel.
1 channel, 1 U8 per channel
half_float::half half
16-bit floating point type
std::vector< std::pair< T, T > > convert_range_pair(const std::vector< AssetsLibrary::RangePair > &excluded_range_pairs)
1 channel, 1 F32 per channel
DataType data_type() const override
Data type of the tensor.
Strides PermutationVector
Permutation vector.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
void validate_npy_header(std::ifstream &stream, const std::string &expect_typestr, const TensorShape &expect_shape)
Describe one of the image's dimensions with a start, end and step.
1 channel, 1 U16 per channel
unsigned int bottom
bottom of the border
std::mutex Mutex
Wrapper of Mutex data-object.
TensorShape shape() const override
Shape of the tensor.
decltype(strategy::transforms) typedef type
TensorShape get_image_shape(const std::string &name)
Provides a tensor shape for the specified image.
std::pair< float, float > RangePair
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
SimpleTensor< float > src
void fill_boxes(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
void fill_tensor_value(T &&tensor, D value) const
Fill a tensor with a constant value.
arm_compute::bfloat16 bfloat16
void permute(Dimensions< T > &dimensions, const PermutationVector &perm)
Permutes given Dimensions according to a permutation vector.
1 channel, 1 S32 per channel
16-bit brain floating-point number
T x() const
Alias to access the size of the first dimension.
void fill_tensor_uniform_ranged(T &&tensor, std::random_device::result_type seed_offset, const std::vector< AssetsLibrary::RangePair > &excluded_range_pairs) const
Fill a tensor with uniform distribution across the specified range.
void store_value_with_data_type(void *ptr, T value, DataType data_type)
Write the value after casting the pointer according to data_type.
1 channel, 1 U32 per channel
Channel
Available channels.
Format
Image colour formats.
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
uint8_t value_type
Tensor value type.
void fill_layer_data(T &&tensor, std::string name) const
Fills the specified tensor with data loaded from .npy (numpy binary) in specified path...
void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Fills the specified tensor with random values drawn from distribution.
Error class for when some external assets are missing.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
std::uniform_real_distribution< float > distribution(-5.f, 5.f)
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
unsigned int left
left of the border
unsigned int right
right of the border
1 channel, 1 S16 per channel
quantized, symmetric fixed-point 8-bit number
quantized, symmetric per channel fixed-point 8-bit number
void fill_static_values(T &&tensor, const std::vector< DataType > &values) const
Fill a tensor with a given vector with static values.
void fill_tensor_uniform(T &&tensor, std::random_device::result_type seed_offset) const
Fill a tensor with uniform distribution.
Num samples, height, width, channels.
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
T y() const
Alias to access the size of the second dimension.
quantized, asymmetric fixed-point 8-bit number signed
64-bit floating-point number
DataType
Available data types.
void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Puts garbage values all around the tensor for testing purposes.
AssetsLibrary(std::string path, std::random_device::result_type seed)
Initialises the library with a path to the assets directory.
Describe a multidimensional execution window.
std::random_device::result_type seed() const
Seed that is used to fill tensors with random values.
const T * data() const
Constant pointer to the underlying buffer.