@@ -252,6 +252,113 @@ TEST_F(QnnHTPBackendTests, QnnContextBinaryCpuNodeWithoutExternalWeights) {
252
252
EpCtxCpuNodeWithExternalIniFileTestBody (false );
253
253
}
254
254
255
+ // Set ep.context_file_path to folder path which is not a valid option, check the error message
256
+ TEST_F (QnnHTPBackendTests, QnnContextBinaryGenerationFolderPathNotExpected) {
257
+ ProviderOptions provider_options;
258
+ #if defined(_WIN32)
259
+ provider_options[" backend_path" ] = " QnnHtp.dll" ;
260
+ #else
261
+ provider_options[" backend_path" ] = " libQnnHtp.so" ;
262
+ #endif
263
+ provider_options[" offload_graph_io_quantization" ] = " 0" ;
264
+
265
+ const std::unordered_map<std::string, int > domain_to_version = {{" " , 13 }, {kMSDomain , 1 }};
266
+
267
+ auto & logging_manager = DefaultLoggingManager ();
268
+ logging_manager.SetDefaultLoggerSeverity (logging::Severity::kERROR );
269
+
270
+ onnxruntime::Model model (" QNN_EP_TestModel" , false , ModelMetaData (), PathString (),
271
+ IOnnxRuntimeOpSchemaRegistryList (), domain_to_version, {},
272
+ logging_manager.DefaultLogger ());
273
+ Graph& graph = model.MainGraph ();
274
+ ModelTestBuilder helper (graph);
275
+ bool single_ep_node = true ;
276
+ BuildGraphWithQAndNonQ (single_ep_node)(helper);
277
+ helper.SetGraphOutputs ();
278
+ ASSERT_STATUS_OK (model.MainGraph ().Resolve ());
279
+
280
+ // Serialize the model to a string.
281
+ std::string model_data;
282
+ model.ToProto ().SerializeToString (&model_data);
283
+
284
+ const auto model_data_span = AsByteSpan (model_data.data (), model_data.size ());
285
+
286
+ const std::string ep_context_onnx_file = " ./ep_context_folder_not_expected/" ;
287
+ std::remove (ep_context_onnx_file.c_str ());
288
+ Ort::SessionOptions so;
289
+ so.AddConfigEntry (kOrtSessionOptionEpContextEnable , " 1" );
290
+ so.AddConfigEntry (kOrtSessionOptionEpContextFilePath , ep_context_onnx_file.c_str ());
291
+ so.AppendExecutionProvider (" QNN" , provider_options);
292
+
293
+ try {
294
+ Ort::Session session (*ort_env, model_data_span.data (), model_data_span.size (), so);
295
+ FAIL (); // Should not get here!
296
+ } catch (const Ort::Exception& excpt) {
297
+ ASSERT_EQ (excpt.GetOrtErrorCode (), ORT_INVALID_ARGUMENT);
298
+ ASSERT_THAT (excpt.what (), testing::HasSubstr (" context_file_path should not point to a folder." ));
299
+ }
300
+ }
301
+
302
+ // Create session 1 to generate context binary file
303
+ // Create session 2 to do same thing, make sure session 2 failed because file exist already
304
+ // Make sure no new file over write from session 2
305
+ TEST_F (QnnHTPBackendTests, QnnContextBinaryGenerationNoOverWrite) {
306
+ ProviderOptions provider_options;
307
+ #if defined(_WIN32)
308
+ provider_options[" backend_path" ] = " QnnHtp.dll" ;
309
+ #else
310
+ provider_options[" backend_path" ] = " libQnnHtp.so" ;
311
+ #endif
312
+ provider_options[" offload_graph_io_quantization" ] = " 0" ;
313
+
314
+ const std::unordered_map<std::string, int > domain_to_version = {{" " , 13 }, {kMSDomain , 1 }};
315
+
316
+ auto & logging_manager = DefaultLoggingManager ();
317
+ logging_manager.SetDefaultLoggerSeverity (logging::Severity::kERROR );
318
+
319
+ onnxruntime::Model model (" QNN_EP_TestModel" , false , ModelMetaData (), PathString (),
320
+ IOnnxRuntimeOpSchemaRegistryList (), domain_to_version, {},
321
+ logging_manager.DefaultLogger ());
322
+ Graph& graph = model.MainGraph ();
323
+ ModelTestBuilder helper (graph);
324
+ bool single_ep_node = true ;
325
+ BuildGraphWithQAndNonQ (single_ep_node)(helper);
326
+ helper.SetGraphOutputs ();
327
+ ASSERT_STATUS_OK (model.MainGraph ().Resolve ());
328
+
329
+ // Serialize the model to a string.
330
+ std::string model_data;
331
+ model.ToProto ().SerializeToString (&model_data);
332
+
333
+ const auto model_data_span = AsByteSpan (model_data.data (), model_data.size ());
334
+
335
+ const std::string ep_context_onnx_file = " ./ep_context_no_over_write.onnx" ;
336
+ const std::string ep_context_binary_file = " ./ep_context_no_over_write.onnx_QNNExecutionProvider_QNN_10880527342279992768_1_0.bin" ;
337
+
338
+ std::remove (ep_context_onnx_file.c_str ());
339
+ Ort::SessionOptions so;
340
+ so.AddConfigEntry (kOrtSessionOptionEpContextEnable , " 1" );
341
+ so.AddConfigEntry (kOrtSessionOptionEpContextFilePath , ep_context_onnx_file.c_str ());
342
+ so.AppendExecutionProvider (" QNN" , provider_options);
343
+
344
+ Ort::Session session1 (*ort_env, model_data_span.data (), model_data_span.size (), so);
345
+
346
+ auto modify_time_1 = std::filesystem::last_write_time (ep_context_binary_file);
347
+
348
+ try {
349
+ Ort::Session session2 (*ort_env, model_data_span.data (), model_data_span.size (), so);
350
+ FAIL (); // Should not get here!
351
+ } catch (const Ort::Exception& excpt) {
352
+ ASSERT_EQ (excpt.GetOrtErrorCode (), ORT_FAIL);
353
+ ASSERT_THAT (excpt.what (), testing::HasSubstr (" exist already." ));
354
+ auto modify_time_2 = std::filesystem::last_write_time (ep_context_binary_file);
355
+ ASSERT_EQ (modify_time_1, modify_time_2);
356
+ }
357
+
358
+ ASSERT_EQ (std::remove (ep_context_onnx_file.c_str ()), 0 );
359
+ ASSERT_EQ (std::remove (ep_context_binary_file.c_str ()), 0 );
360
+ }
361
+
255
362
// Create a model with Case + Add (quantized)
256
363
// cast_input -> Cast -> Q -> DQ \
257
364
// Add -> Q -> DQ -> output
0 commit comments