// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include "core/common/common.h" #include "core/framework/tensor_shape.h" #include "core/common/float16.h" #include "core/framework/framework_common.h" #include "core/framework/ort_value.h" #include "test/unittest_util/framework_test_utils.h" namespace onnxruntime { namespace test { /** * @brief Class represent a input data (dimensions, data type and value). */ struct TestInputData { template TestInputData(const std::string& name, const TensorShapeVector& dims, const std::vector& values) : name_(name), dims_(dims), values_(values) {} OrtValue ToOrtValue() { OrtValue ortvalue; std::vector dims; dims.reserve(dims_.size()); dims.insert(dims.end(), dims_.begin(), dims_.end()); std::visit([&ortvalue, &dims](auto&& arg) { using T = std::decay_t; if constexpr (std::is_same_v> || std::is_same_v> || std::is_same_v>) CreateMLValue( TestCPUExecutionProvider()->CreatePreferredAllocators()[0], dims, arg, &ortvalue); else static_assert("Unspported types!"); }, values_); return ortvalue; } std::string GetName() const { return name_; } private: std::string name_; TensorShapeVector dims_; std::variant, std::vector, std::vector> values_; }; /** * @brief A container for all input data. * */ struct InputContainer { InputContainer() = default; template TestInputData& AddInput(const std::string& name, const TensorShapeVector dims, const std::vector& values) { inputs_.emplace_back(TestInputData(name, dims, values)); return inputs_.back(); } template TestInputData& AddInput(const std::string& name, TensorShapeVector dims, std::function< void(const TensorShapeVector& shape, std::vector& data)> func = nullptr) { std::vector values(TensorShape(dims).Size()); if (func) { func(dims, values); } inputs_.emplace_back(TestInputData(name, dims, values)); return inputs_.back(); } void ToInputMap(NameMLValMap& feeds) const { for (auto input : inputs_) { feeds.insert({input.GetName(), input.ToOrtValue()}); } } private: std::vector inputs_; }; void RandomFillFloatVector(const TensorShapeVector& shape, std::vector& data); void RandomFillHalfVector(const TensorShapeVector& shape, std::vector& data); void RandomMasks(int64_t batch, int64_t sequence_length, std::vector& data); void RunModelWithData(const PathString& model_uri, const std::string session_log_id, const std::string& provider_type, const InputContainer& input_container, const std::vector& output_names, std::vector& run_results); } // namespace test } // namespace onnxruntime