2020-05-02 07:33:17 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
#include "testPch.h"
|
|
|
|
|
#include "RawApiTests.h"
|
|
|
|
|
#include "RawApiHelpers.h"
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
#include <roapi.h>
|
|
|
|
|
|
|
|
|
|
namespace ml = Microsoft::AI::MachineLearning;
|
|
|
|
|
|
2023-07-26 04:56:50 +00:00
|
|
|
auto CreateModelAsBuffer(const wchar_t* model_path) {
|
|
|
|
|
std::ifstream input_stream(model_path, std::ios::binary | std::ios::ate);
|
|
|
|
|
std::streamsize size = input_stream.tellg();
|
|
|
|
|
input_stream.seekg(0, std::ios::beg);
|
2020-05-02 07:33:17 +00:00
|
|
|
|
2023-07-26 04:56:50 +00:00
|
|
|
std::vector<char> buffer(static_cast<std::vector<char>::size_type>(size));
|
|
|
|
|
input_stream.read(buffer.data(), size);
|
2020-05-02 07:33:17 +00:00
|
|
|
|
2023-07-26 04:56:50 +00:00
|
|
|
return std::make_pair(buffer, size);
|
2020-05-02 07:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void RawApiTestsApiTestsClassSetup() {
|
2021-06-08 01:06:31 +00:00
|
|
|
WINML_EXPECT_HRESULT_SUCCEEDED(RoInitialize(RO_INIT_TYPE::RO_INIT_MULTITHREADED));
|
2020-05-02 07:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CreateModelFromFilePath() {
|
|
|
|
|
std::wstring model_path = L"model.onnx";
|
|
|
|
|
std::unique_ptr<ml::learning_model> model = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(model = std::make_unique<ml::learning_model>(model_path.c_str(), model_path.size()));
|
|
|
|
|
WINML_EXPECT_NO_THROW(model.reset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CreateCpuDevice() {
|
|
|
|
|
std::unique_ptr<ml::learning_model_device> device = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(device = std::make_unique<ml::learning_model_device>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Evaluate() {
|
|
|
|
|
std::wstring model_path = L"model.onnx";
|
|
|
|
|
std::unique_ptr<ml::learning_model> model = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(model = std::make_unique<ml::learning_model>(model_path.c_str(), model_path.size()));
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ml::learning_model_device> device = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(device = std::make_unique<ml::learning_model_device>());
|
|
|
|
|
|
2020-11-16 18:06:22 +00:00
|
|
|
RunOnDevice(*model.get(), *device.get(), InputStrategy::CopyInputs);
|
2020-05-02 07:33:17 +00:00
|
|
|
|
|
|
|
|
WINML_EXPECT_NO_THROW(model.reset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void EvaluateNoInputCopy() {
|
|
|
|
|
std::wstring model_path = L"model.onnx";
|
|
|
|
|
std::unique_ptr<ml::learning_model> model = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(model = std::make_unique<ml::learning_model>(model_path.c_str(), model_path.size()));
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ml::learning_model_device> device = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(device = std::make_unique<ml::learning_model_device>());
|
|
|
|
|
|
2020-11-16 18:06:22 +00:00
|
|
|
RunOnDevice(*model.get(), *device.get(), InputStrategy::BindAsReference);
|
|
|
|
|
|
|
|
|
|
WINML_EXPECT_NO_THROW(model.reset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void EvaluateManyBuffers() {
|
|
|
|
|
std::wstring model_path = L"model.onnx";
|
|
|
|
|
std::unique_ptr<ml::learning_model> model = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(model = std::make_unique<ml::learning_model>(model_path.c_str(), model_path.size()));
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ml::learning_model_device> device = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(device = std::make_unique<ml::learning_model_device>());
|
|
|
|
|
|
|
|
|
|
RunOnDevice(*model.get(), *device.get(), InputStrategy::BindWithMultipleReferences);
|
2020-05-02 07:33:17 +00:00
|
|
|
|
|
|
|
|
WINML_EXPECT_NO_THROW(model.reset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void EvaluateFromModelFromBuffer() {
|
|
|
|
|
std::wstring model_path = L"model.onnx";
|
|
|
|
|
|
2020-09-19 19:41:42 +00:00
|
|
|
auto [buffer, size] = CreateModelAsBuffer(model_path.c_str());
|
2020-05-02 07:33:17 +00:00
|
|
|
|
|
|
|
|
std::unique_ptr<ml::learning_model> model = nullptr;
|
2020-09-19 19:41:42 +00:00
|
|
|
WINML_EXPECT_NO_THROW(model = std::make_unique<ml::learning_model>(buffer.data(), static_cast<size_t>(size)));
|
2020-05-02 07:33:17 +00:00
|
|
|
|
|
|
|
|
std::unique_ptr<ml::learning_model_device> device = nullptr;
|
|
|
|
|
WINML_EXPECT_NO_THROW(device = std::make_unique<ml::learning_model_device>());
|
|
|
|
|
|
2020-11-16 18:06:22 +00:00
|
|
|
RunOnDevice(*model.get(), *device.get(), InputStrategy::CopyInputs);
|
2020-05-02 07:33:17 +00:00
|
|
|
|
|
|
|
|
WINML_EXPECT_NO_THROW(model.reset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const RawApiTestsApi& getapi() {
|
|
|
|
|
static constexpr RawApiTestsApi api = {
|
2023-07-26 04:56:50 +00:00
|
|
|
RawApiTestsApiTestsClassSetup,
|
|
|
|
|
CreateModelFromFilePath,
|
|
|
|
|
CreateCpuDevice,
|
|
|
|
|
Evaluate,
|
|
|
|
|
EvaluateNoInputCopy,
|
|
|
|
|
EvaluateManyBuffers,
|
|
|
|
|
EvaluateFromModelFromBuffer,
|
2020-05-02 07:33:17 +00:00
|
|
|
};
|
|
|
|
|
return api;
|
|
|
|
|
}
|