Skip to content

Commit

Permalink
Merge branch 'my-test' into change-tree-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
bili2002 committed Jan 12, 2024
2 parents 8263476 + 19a772c commit c93dd0a
Show file tree
Hide file tree
Showing 8 changed files with 7,124,344 additions and 2 deletions.
93 changes: 91 additions & 2 deletions onnxruntime/test/framework/inference_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,26 @@ TEST(InferenceSessionTests, InterThreadPoolWithDenormalAsZero) {
VerifyThreadPoolWithDenormalAsZero(session2.GetInterOpThreadPoolToUse(), false);
}

TEST(InferenceSessionTests, Bench) {

float calculateSD(std::vector<float>::iterator beg, std::vector<float>::iterator end) {
float sum = 0.0, mean, standardDeviation = 0.0;
int n = 0;

for (auto curr = beg; curr != end; curr++) {
sum += *curr;
n++;
}

mean = sum / n;

for (auto curr = beg; curr != end; curr++) {
standardDeviation += (*curr - mean) * (*curr - mean);
}

return sqrt(standardDeviation / n);
}

TEST(InferenceSessionTests, BenchSameMode) {
// Initialize logging manager
auto logging_manager = std::make_unique<logging::LoggingManager>(
std::unique_ptr<ISink>(new CLogSink()), logging::Severity::kVERBOSE, false,
Expand Down Expand Up @@ -3021,7 +3040,77 @@ TEST(InferenceSessionTests, Bench) {
}

const auto average = std::accumulate(times.begin(), times.end(), 0.0) / times.size();
std::cout << "Average " << average << std::endl;
const auto SD = calculateSD(times.begin(), times.end());
std::cout << "Same Mode - Average " << average << "; Standard Deviation " << SD << std::endl;
}

TEST(InferenceSessionTests, BenchNotSameMode) {
// Initialize logging manager
auto logging_manager = std::make_unique<logging::LoggingManager>(
std::unique_ptr<ISink>(new CLogSink()), logging::Severity::kVERBOSE, false,
LoggingManager::InstanceType::Temporal);

// Create environment
std::unique_ptr<Environment> env;
ASSERT_TRUE(Environment::Create(std::move(logging_manager), env).IsOK());

// Configure session options
SessionOptions so;
so.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
so.graph_optimization_level = TransformerLevel::Level2;
so.intra_op_param.thread_pool_size = 1;

// Initialize and load the InferenceSession
InferenceSession session{so, *env};

ASSERT_STATUS_OK(session.Load("model3.onnx"));
ASSERT_STATUS_OK(session.Initialize());

// Input numpy array
std::fstream input_file("input3.txt");
std::vector<float> values = {};
for(float number; input_file >> number; ) { values.push_back(number); }

std::vector<int64_t> dims = {static_cast<long long>(values.size()) / 15, 15};

std::cout << "Loaded: " << values.size() << std::endl;

OrtValue ml_value;
CreateMLValue<float>(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], dims, values, &ml_value);
NameMLValMap feeds;
feeds.insert(std::make_pair("float_input", ml_value));

// Configure output
std::vector<std::string> output_names;
output_names.push_back("variable");
std::vector<OrtValue> fetches;

// Configure RunOptions
RunOptions run_options;

const int MAX_ITER = 10;
std::vector<float> times = {};

for(size_t ITER = 0; ITER < MAX_ITER; ITER ++) {
const auto begin = clock();

{
common::Status st = session.Run(run_options, feeds, output_names, &fetches);
if (!st.IsOK()) {
std::cout << "Run returned status: " << st.ErrorMessage() << std::endl;
}
ASSERT_TRUE(st.IsOK());
}

const auto end = clock();
const auto time_in_seconds = (double)(end - begin) / CLOCKS_PER_SEC;
std::cout << "Total time in predict " << time_in_seconds << std::endl;
times.push_back(time_in_seconds);
}

const auto average = std::accumulate(times.begin(), times.end(), 0.0) / times.size();
const auto SD = calculateSD(times.begin(), times.end());
std::cout << "Not SM - Average " << average << "; Standard Deviation " << SD << std::endl;
}

} // namespace test
Expand Down
Loading

0 comments on commit c93dd0a

Please sign in to comment.