Repository: microsoft/onnxruntime
File: onnxruntime/test/shared_lib/test_inference.cc (C++)

Code before:
    GTEST_SKIP() << "CUDA library was not found";
  }

  constexpr const char* cuda_ep_name = "ORT Cuda";
  ort_env->RegisterExecutionProviderLibrary(cuda_ep_name, cuda_lib);
  auto ep_devices = ort_env->GetEpDevices();

  Ort::ConstEpDevice cuda_device;
  for (const auto& device : ep_devices) {
    if (device.Device().Type() == OrtHardwareDeviceType_GPU &&
        device.Device().VendorId() == 0x10DE) {  // NVIDIA vendor ID
      cuda_device = device;
      break;
    }
  }

  if (!cuda_device) {
    GTEST_SKIP() << "No CUDA device found, skipping test.";
  }

  // Create a stream on CUDA Device
  const auto sync_stream = cuda_device.CreateSyncStream();
  TestCudaStreamOverrideUsed cuda_override_stream(sync_stream);

  // Create session with CUDA EP using C++ public API in Ort:: namespace
  Ort::SessionOptions session_options;
  Ort::CUDAProviderOptions cuda_options;
  session_options.AppendExecutionProvider_CUDA_V2(*cuda_options);

  Ort::Session session(*ort_env, MODEL_URI, session_options);

  constexpr const std::array<const char*, 1U> input_names = {"X"};
  constexpr const std::array<const char*, 1U> output_names = {"Y"};
  constexpr const std::array<int64_t, 2U> input_shape = {3LL, 2LL};
  float x_value[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
  auto input_value = Ort::Value::CreateTensor<float>(
      Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU),
      x_value, std::size(x_value), input_shape.data(), input_shape.size());
  Ort::Value ort_inputs[] = {std::move(input_value)};

  Ort::RunOptions run_options;
  run_options.SetSyncStream(reinterpret_cast<OrtSyncStream*>(&cuda_override_stream));

  auto output_values = session.Run(run_options,
                                   input_names.data(), ort_inputs, std::size(ort_inputs),
                                   output_names.data(), output_names.size());

  ASSERT_GT(cuda_override_stream.flush_count, 0U)
      << "Expected the custom CUDA stream override to be used during session run.";

  ort_env->UnregisterExecutionProviderLibrary(cuda_ep_name);

Review comment (suggestion):
This is a bit confusing as a test setup. You're using the old-style EP selection (i.e. not AppendExecutionProvider_V2 with an OrtEpDevice), which by-passes everything to do with OrtSyncStream (which is 'v2' along with using OrtEpFactory to construct an OrtSyncStream etc. etc.), with the exception of getting it from RunOptions. 

Can we instead use the 'v2' world and register the CUDA EP (RegisterExecutionProviderLibrary), check the results from GetEpDevices() for a CUDA device, call CreateSyncStreamForEpDevice if found to create the stream, use the V2 registration to enable the CUDA EP, and go from there? I think that will make it much easier for the next developer to understand how everything fits together.

You can get the cudaStream from the OrtSyncStream using SyncStream_GetHandle in order to do the assert at the end.

Code after:
    GTEST_SKIP() << "CUDA library was not found";
  }

  constexpr const char* cuda_ep_name = "ORT Cuda";
  ort_env->RegisterExecutionProviderLibrary(cuda_ep_name, cuda_lib);
  auto ep_devices = ort_env->GetEpDevices();

  Ort::ConstEpDevice cuda_device;
  for (const auto& device : ep_devices) {
    if (device.Device().Type() == OrtHardwareDeviceType_GPU &&
        device.Device().VendorId() == 0x10DE) {  // NVIDIA vendor ID
      cuda_device = device;
      break;
    }
  }

  if (!cuda_device) {
    GTEST_SKIP() << "No CUDA device found, skipping test.";
  }

  // Create session with CUDA EP using C++ public API in Ort:: namespace
  {
    // Create a stream on CUDA Device
    const auto sync_stream = cuda_device.CreateSyncStream();
    TestCudaStreamOverrideUsed cuda_override_stream(sync_stream);

    Ort::SessionOptions session_options;
    session_options.AppendExecutionProvider_V2(*ort_env, {cuda_device}, Ort::KeyValuePairs{});

    Ort::Session session(*ort_env, MODEL_URI, session_options);

    constexpr const std::array<const char*, 1U> input_names = {"X"};
    constexpr const std::array<const char*, 1U> output_names = {"Y"};
    constexpr const std::array<int64_t, 2U> input_shape = {3LL, 2LL};
    float x_value[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
    auto input_value = Ort::Value::CreateTensor<float>(
        Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU),
        x_value, std::size(x_value), input_shape.data(), input_shape.size());
    Ort::Value ort_inputs[] = {std::move(input_value)};

    Ort::RunOptions run_options;
    run_options.SetSyncStream(reinterpret_cast<OrtSyncStream*>(&cuda_override_stream));

    auto output_values = session.Run(run_options,
                                     input_names.data(), ort_inputs, std::size(ort_inputs),
                                     output_names.data(), output_names.size());

    ASSERT_GT(cuda_override_stream.flush_count, 0U)
        << "Expected the custom CUDA stream override to be used during session run.";
  }