|
18 | 18 |
|
19 | 19 | #include "test/shared_lib/test_fixture.h" |
20 | 20 | #include "test/shared_lib/utils.h" |
| 21 | +#include "test/util/include/scoped_env_vars.h" |
21 | 22 | #include "test/util/include/test_allocator.h" |
22 | 23 |
|
23 | 24 | #include "onnxruntime_config.h" // generated file in build output dir |
@@ -1014,3 +1015,79 @@ TEST(ModelEditorCompileAPITest, CompileFromOrtModelToFile) { |
1014 | 1015 | // Cleanup |
1015 | 1016 | std::filesystem::remove(output_path); |
1016 | 1017 | } |
| 1018 | + |
| 1019 | +// Test: ORT_LOAD_CONFIG_FROM_MODEL=1 with OrtModel input fails fast with a clear, |
| 1020 | +// actionable error message. |
| 1021 | +TEST(ModelEditorCompileAPITest, LoadConfigFromModelEnvVarFailsForOrtModel) { |
| 1022 | + // RAII helper saves the current env var value and restores it when the scope exits. |
| 1023 | + onnxruntime::test::ScopedEnvironmentVariables scoped_env( |
| 1024 | + {{"ORT_LOAD_CONFIG_FROM_MODEL", "1"}}); |
| 1025 | + |
| 1026 | + std::vector<std::unique_ptr<std::vector<float>>> weights; |
| 1027 | + auto model = CreateSimpleGemmModel(weights); |
| 1028 | + |
| 1029 | + Ort::SessionOptions session_options; |
| 1030 | + Ort::ModelCompilationOptions compile_options(*ort_env, session_options); |
| 1031 | + compile_options.SetInputModel(static_cast<const OrtModel*>(model)); |
| 1032 | + compile_options.SetEpContextEmbedMode(true); |
| 1033 | + |
| 1034 | + std::unique_ptr<MockedOrtAllocator> allocator = std::make_unique<MockedOrtAllocator>(); |
| 1035 | + void* output_buffer = nullptr; |
| 1036 | + size_t output_size = 0; |
| 1037 | + compile_options.SetOutputModelBuffer(allocator.get(), &output_buffer, &output_size); |
| 1038 | + |
| 1039 | + // Should fail with a clear error about the unsupported env-var/input combination. |
| 1040 | + Ort::Status status = Ort::CompileModel(*ort_env, compile_options); |
| 1041 | + EXPECT_FALSE(status.IsOK()); |
| 1042 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::HasSubstr("ORT_LOAD_CONFIG_FROM_MODEL=1")); |
| 1043 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::HasSubstr("in-memory OrtModel input")); |
| 1044 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::HasSubstr("Unset ORT_LOAD_CONFIG_FROM_MODEL")); |
| 1045 | + |
| 1046 | + if (output_buffer != nullptr) { |
| 1047 | + allocator->Free(output_buffer); |
| 1048 | + } |
| 1049 | +} |
| 1050 | + |
| 1051 | +// Test: Validation error for OrtModel with no model_path, no output location, and no embed mode. |
| 1052 | +// Verifies the error message contains the expected remediation guidance. |
| 1053 | +TEST(ModelEditorCompileAPITest, NoOutputLocationNoModelPathFails) { |
| 1054 | + std::vector<std::unique_ptr<std::vector<float>>> weights; |
| 1055 | + auto model = CreateSimpleGemmModel(weights); |
| 1056 | + |
| 1057 | + Ort::SessionOptions session_options; |
| 1058 | + Ort::ModelCompilationOptions compile_options(*ort_env, session_options); |
| 1059 | + compile_options.SetInputModel(static_cast<const OrtModel*>(model)); |
| 1060 | + // Intentionally do NOT call SetEpContextEmbedMode, SetOutputModelPath, or SetOutputModelBuffer |
| 1061 | + |
| 1062 | + Ort::Status status = Ort::CompileModel(*ort_env, compile_options); |
| 1063 | + EXPECT_FALSE(status.IsOK()); |
| 1064 | + // Should suggest setting output location or model_path, including embed mode as an option |
| 1065 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::HasSubstr("SetOutputModelPath")); |
| 1066 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::HasSubstr("SetEpContextEmbedMode(true)")); |
| 1067 | + // Should NOT suggest SetEpContextBinaryInformation (that alone is not sufficient) |
| 1068 | + EXPECT_THAT(status.GetErrorMessage(), ::testing::Not(::testing::HasSubstr("SetEpContextBinaryInformation"))); |
| 1069 | +} |
| 1070 | + |
| 1071 | +// Test: Setting embed mode with buffer output satisfies the output location requirement |
| 1072 | +// for OrtModel with no model_path. |
| 1073 | +TEST(ModelEditorCompileAPITest, EmbedModeWithBufferOutputSatisfiesValidation) { |
| 1074 | + std::vector<std::unique_ptr<std::vector<float>>> weights; |
| 1075 | + auto model = CreateSimpleGemmModel(weights); |
| 1076 | + |
| 1077 | + Ort::SessionOptions session_options; |
| 1078 | + Ort::ModelCompilationOptions compile_options(*ort_env, session_options); |
| 1079 | + compile_options.SetInputModel(static_cast<const OrtModel*>(model)); |
| 1080 | + compile_options.SetEpContextEmbedMode(true); |
| 1081 | + |
| 1082 | + std::unique_ptr<MockedOrtAllocator> allocator = std::make_unique<MockedOrtAllocator>(); |
| 1083 | + void* output_buffer = nullptr; |
| 1084 | + size_t output_size = 0; |
| 1085 | + compile_options.SetOutputModelBuffer(allocator.get(), &output_buffer, &output_size); |
| 1086 | + |
| 1087 | + Ort::Status status = Ort::CompileModel(*ort_env, compile_options); |
| 1088 | + EXPECT_TRUE(status.IsOK()) << "CompileModel failed: " << status.GetErrorMessage(); |
| 1089 | + |
| 1090 | + if (output_buffer != nullptr) { |
| 1091 | + allocator->Free(output_buffer); |
| 1092 | + } |
| 1093 | +} |
0 commit comments