diff --git a/onnxruntime/core/framework/allocator.cc b/onnxruntime/core/framework/allocator.cc index a656abb098911..56bff8aa30f68 100644 --- a/onnxruntime/core/framework/allocator.cc +++ b/onnxruntime/core/framework/allocator.cc @@ -237,9 +237,22 @@ ORT_API_STATUS_IMPL(OrtApis::CreateMemoryInfo, _In_ const char* name1, enum OrtA OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, OrtDevice::VendorIds::AMD, device_id), mem_type1); } else if (strcmp(name1, onnxruntime::WEBGPU_BUFFER) == 0 || - strcmp(name1, onnxruntime::WEBNN_TENSOR) == 0) { + strcmp(name1, onnxruntime::WEBNN_TENSOR) == 0 || + // Accept pre-1.25 names "WebGPU_Buffer"/"WebNN_Tensor" for backward compatibility + // with released onnxruntime-genai that still uses the old names. + // Normalize to the current (short) constant so downstream name comparisons work. + // See: https://github.com/microsoft/onnxruntime/pull/27207 + strcmp(name1, "WebGPU_Buffer") == 0 || + strcmp(name1, "WebNN_Tensor") == 0) { + // Map old long names to current short constants to keep downstream name comparisons consistent. + const char* normalized_name = name1; + if (strcmp(name1, "WebGPU_Buffer") == 0) { + normalized_name = onnxruntime::WEBGPU_BUFFER; + } else if (strcmp(name1, "WebNN_Tensor") == 0) { + normalized_name = onnxruntime::WEBNN_TENSOR; + } *out = new OrtMemoryInfo( - name1, type, + normalized_name, type, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, OrtDevice::VendorIds::NONE, device_id), mem_type1); diff --git a/onnxruntime/test/shared_lib/test_allocator.cc b/onnxruntime/test/shared_lib/test_allocator.cc index bf9e54e8b3c7b..c80712e272ad2 100644 --- a/onnxruntime/test/shared_lib/test_allocator.cc +++ b/onnxruntime/test/shared_lib/test_allocator.cc @@ -22,6 +22,27 @@ TEST(CApiTest, allocation_info) { ASSERT_EQ(OrtMemTypeDefault, cpu_mem_info_1.GetMemoryType()); } +// Verify that legacy (pre-1.25) memory info names "WebGPU_Buffer" and "WebNN_Tensor" are accepted +// and normalized to the current short names "WebGPU_Buf" and "WebNN_Ten". +// This ensures backward compatibility with released onnxruntime-genai that uses the old names. +TEST(CApiTest, LegacyWebGpuWebNNMemoryInfoNames) { + // Old (pre-1.25) names must be accepted + Ort::MemoryInfo legacy_webgpu("WebGPU_Buffer", OrtDeviceAllocator, 0, OrtMemTypeDefault); + Ort::MemoryInfo legacy_webnn("WebNN_Tensor", OrtDeviceAllocator, 0, OrtMemTypeDefault); + + // Current (short) names + Ort::MemoryInfo current_webgpu("WebGPU_Buf", OrtDeviceAllocator, 0, OrtMemTypeDefault); + Ort::MemoryInfo current_webnn("WebNN_Ten", OrtDeviceAllocator, 0, OrtMemTypeDefault); + + // Legacy names should be normalized to the current names + ASSERT_EQ(std::string("WebGPU_Buf"), legacy_webgpu.GetAllocatorName()); + ASSERT_EQ(std::string("WebNN_Ten"), legacy_webnn.GetAllocatorName()); + + // Memory infos created with legacy and current names should be equal + ASSERT_EQ(legacy_webgpu, current_webgpu); + ASSERT_EQ(legacy_webnn, current_webnn); +} + TEST(CApiTest, DefaultAllocator) { Ort::AllocatorWithDefaultOptions default_allocator; auto cpu_info = default_allocator.GetInfo();