// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include #include #include #include "test_configuration.h" #include "test_session.h" #include "utils.h" #if defined(USE_CUDA) || defined(USE_TENSORRT) || defined(USE_NV) #include #endif class TestModelInfo; namespace onnxruntime { namespace perftest { class OnnxRuntimeTestSession : public TestSession { public: OnnxRuntimeTestSession(Ort::Env& env, std::random_device& rd, const PerformanceTestConfig& performance_test_config, const TestModelInfo& m); void PreLoadTestData(size_t test_data_id, size_t input_id, Ort::Value&& value) override; bool PopulateGeneratedInputTestData(int32_t seed); bool PopulateGeneratedMultiShapeInputTestData( int32_t seed, const std::map>>& data_shape_groups); std::vector GetLoadedInputShape(size_t test_data_id, size_t input_id) const; void SelectTestDataSets(const std::vector& selected_ids); void SetUseRoundRobin(bool v) { use_round_robin_ = v; } ~OnnxRuntimeTestSession(); RunTiming Run() override; ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(OnnxRuntimeTestSession); private: void CreateAndStoreGeneratedInput(size_t test_data_id, size_t input_idx, const std::vector& dims, ONNXTensorElementDataType element_type, int32_t seed); // Copies value into allocator_'s memory if a plugin EP allocator was selected. Strings and // non-tensor values can't live in device memory, so those are returned unchanged. Ort::Value StageInputForPluginEpAllocator(Ort::Value&& value); // Stores an already-staged value; used to avoid double-staging in CreateAndStoreGeneratedInput. void StoreTestData(size_t test_data_id, size_t input_id, Ort::Value&& value); // True if allocator_ is for device-only (i.e., non CPU or host-accessible) memory. bool IsAllocatorDeviceOnly() const; Ort::Session session_{nullptr}; std::mt19937 rand_engine_; std::uniform_int_distribution dist_; Ort::AllocatorWithDefaultOptions default_allocator_; // Note: custom_allocator_, if used, must outlive the `Ort::Value`s allocated with it in test_inputs_ and outputs_. // and must be declared before them to ensure it is destroyed after them. Ort::Allocator custom_allocator_{nullptr}; Ort::UnownedAllocator allocator_{default_allocator_}; std::vector> test_inputs_; std::vector outputs_; std::vector output_names_; // The same size with output_names_. // TODO: implement a customized allocator, then we can remove output_names_ to simplify this code std::vector output_names_raw_ptr; std::vector input_names_; std::vector input_names_str_; const int input_length_; std::string provider_name_; std::string device_memory_name_; // Device memory type name to use from the list in allocator.h const std::unordered_map& run_config_entries_; Ort::Env& env_; std::optional plugin_ep_allocator_selection_; bool has_dynamic_output_shapes_ = false; // Per-output dynamic-shape flag, aligned with outputs_. Lets Run() reset only the outputs that // actually have dynamic shapes instead of discarding every pre-allocated buffer. std::vector is_output_dynamic_; std::atomic round_robin_counter_{0}; bool use_round_robin_{false}; #if defined(USE_CUDA) || defined(USE_TENSORRT) || defined(USE_NV) cudaStream_t stream_; // Device stream if required by IO bindings #endif Ort::ArenaCfg cuda_mempool_arena_cfg_{nullptr}; }; } // namespace perftest } // namespace onnxruntime