[Bug] 2.3.0: phi-4-mini (CUDA) returns garbage once the prompt exceeds ~4096 tokens (works on 1.2.4)
Describe the issue
With Foundry Local SDK/runtime 2.3.0, Phi-4-mini-instruct-cuda-gpu:5 returns garbage (runs of dots and digits) for any request whose prompt is longer than about 4096 tokens. At or below that, the same model answers correctly. ChatSession.ProcessRequestAsync and the obsolete OpenAIChatClient.CompleteChatAsync behave identically, so this looks like a runtime/model-execution problem rather than an API-surface one.
4096 is phi-4-mini's original_max_position_embeddings, the point where LongRoPE switches from the short to the long rope factors. So the long-factor path may be what is broken.
Output of the repro below (temperature 0, max 32 output tokens; the question is "What is the capital of France? Answer with one word."):
ChatSession promptTokens=3631: Paris
OpenAIChatClient promptTokens=3631: Paris
ChatSession promptTokens=3901: Paris
OpenAIChatClient promptTokens=3901: Paris
ChatSession promptTokens=4531: . . . . .. . . . . . . . . . . . . . . . . . . . .. . . ..
OpenAIChatClient promptTokens=4531: . . . . .. . . . . . . . . . . . . . . . . . . . .. . . ..
ChatSession promptTokens=5971: . . . . . . . . .. .1. 1.. .. . . . . . .. . . . . 0.
OpenAIChatClient promptTokens=5971: . . . . . . . . .. .1. 1.. .. . . . . . .. . . . . 0.
Regression from 1.2.4: with Microsoft.AI.Foundry.Local.WinML 1.2.4 and the same cached model files on the same machine, a real application prompt of 4,105, 6,144 and 8,500 tokens all produced correct output. On 2.3.0 the same prompts produce garbage. Raising model.context_length / search.max_length in the variant's genai_config.json to 131072 does not change the 2.3.0 result. Requests under 4096 tokens work on 2.3.0.
Possibly related: microsoft/onnxruntime-genai#552 (Phi-3 nonsense output for long prompts), #1067 (2.0.1 enforcing search.max_length).
To reproduce
dotnet new console, reference Microsoft.AI.Foundry.Local 2.3.0 and Microsoft.Extensions.Logging.Abstractions, net10.0, win-x64, then run:
using Microsoft.AI.Foundry.Local;
using Microsoft.Extensions.Logging.Abstractions;
// Minimal repro: phi-4-mini (CUDA) returns garbage once the prompt crosses 4096 tokens.
string variantId = args.Length > 0 ? args[0] : "Phi-4-mini-instruct-cuda-gpu:5";
await FoundryLocalManager.CreateAsync(new Configuration { AppName = "flrepro", ModelCacheDir = Environment.GetEnvironmentVariable("FL_MODEL_CACHE") }, NullLogger.Instance);
var manager = FoundryLocalManager.Instance;
await manager.DownloadAndRegisterEpsAsync();
var catalog = await manager.GetCatalogAsync();
var model = await catalog.GetModelVariantAsync(variantId) ?? throw new Exception($"{variantId} not in catalog");
await model.DownloadAsync();
await model.LoadAsync();
foreach (int factLines in new[] { 200, 215, 250, 330 })
{
var facts = string.Join("\n", Enumerable.Range(1, factLines).Select(i => $"Reference note {i}: warehouse shelf {i} stores spare parts for model line {i % 17}."));
string system = "You are a helpful assistant. The following reference notes are background only.\n" + facts;
using var session = new ChatSession(model);
session.SetOptions(new RequestOptions { Search = new SearchOptions { Temperature = 0f, MaxOutputTokens = 32 }, AdditionalOptions = new Dictionary<string, string>() });
using var request = new Request().AddItem(MessageItem.System(system)).AddItem(MessageItem.User("What is the capital of France? Answer with one word."));
using var response = await session.ProcessRequestAsync(request);
string text = string.Concat(response.Select(i => i is MessageItem m && m.IsSimpleText() ? m.GetSimpleText() : i is TextItem t ? t.Text : ""));
Console.WriteLine($"ChatSession promptTokens={response.GetUsage()?.PromptTokens}: {text.Trim().Replace("\n", " ")}");
var chat = await model.GetChatClientAsync();
chat.Settings.Temperature = 0f; chat.Settings.MaxTokens = 32;
var legacy = await chat.CompleteChatAsync(new[]
{
Betalgo.Ranul.OpenAI.ObjectModels.RequestModels.ChatMessage.FromSystem(system),
Betalgo.Ranul.OpenAI.ObjectModels.RequestModels.ChatMessage.FromUser("What is the capital of France? Answer with one word."),
});
Console.WriteLine($"OpenAIChatClient promptTokens={legacy.Usage?.PromptTokens}: {legacy.Choices?[0].Message.Content?.Trim().Replace("\n", " ")}");
}
await model.UnloadAsync();
Expected behavior
"Paris" for every prompt size that fits the model's context window, as with 1.2.4.
Platform and architecture
Windows x64
OS Version
Windows 11 Pro 26H2 (build 26300)
Installation type
Released package/binary (NuGet)
Foundry Local version
SDK/runtime 2.3.0 (foundry_local.dll runtime 2.3.0, ONNX Runtime 1.31.0, ONNX Runtime GenAI 0.18.0). Works correctly on 1.2.4 (Microsoft.AI.Foundry.Local.WinML).
API or surface area
C# SDK: ChatSession and OpenAIChatClient (both affected)
Hardware acceleration/backend
CUDA EP (CUDAExecutionProvider), NVIDIA GeForce RTX 5090 Laptop GPU (24 GB, compute capability 12.0), driver 616.56
Model
Phi-4-mini-instruct-cuda-gpu:5
Hi @andrewtheart, Thanks for creating this issue. This has been fixed upstream in onnxruntime-genai#2846 and should be fixed in Foundry Local 2.5.0 when the changes propagate downstream.