Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Phi-4 to Tiktoken encoding map #7337

Open
luisquintanilla opened this issue Dec 16, 2024 · 1 comment
Open

Add Phi-4 to Tiktoken encoding map #7337

luisquintanilla opened this issue Dec 16, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request Tokenizers
Milestone

Comments

@luisquintanilla
Copy link
Contributor

Phi-4 uses Tiktoken tokenizer (100k vocab).

https://arxiv.org/pdf/[2412.08905v1](https://arxiv.org/pdf/2412.08905v1)

we now use the tiktoken tokenizer (for better multilingual support) with a padded vocabulary size of 100,352 (including unused tokens)

Consider adding it as an option to the encoding map so it's easier to create.

private static readonly (string Prefix, ModelEncoding Encoding)[] _modelPrefixToEncoding =
[
// chat
( "o1-", ModelEncoding.O200kBase ), // e.g. o1-mini
( "gpt-4o-", ModelEncoding.O200kBase), // e.g., gpt-4o-2024-05-13
( "gpt-4-", ModelEncoding.Cl100kBase), // e.g., gpt-4-0314, etc., plus gpt-4-32k
( "gpt-3.5-", ModelEncoding.Cl100kBase), // e.g, gpt-3.5-turbo-0301, -0401, etc.
( "gpt-35-", ModelEncoding.Cl100kBase ) // Azure deployment name
];
private static readonly Dictionary<string, ModelEncoding> _modelToEncoding =

@luisquintanilla luisquintanilla added the enhancement New feature or request label Dec 16, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged label Dec 16, 2024
@tarekgh tarekgh added this to the ML.NET Future milestone Dec 16, 2024
@tarekgh tarekgh added Tokenizers and removed untriaged New issue has not been triaged labels Dec 16, 2024
@tarekgh tarekgh self-assigned this Dec 16, 2024
@MaxAkbar
Copy link

MaxAkbar commented Jan 12, 2025

I got the model to load using the following code, and generate text but I can't seem to workout out the stop sequence:

using Microsoft.Extensions.AI;
using Microsoft.ML.GenAI.Core;
using Microsoft.ML.GenAI.Phi;
using Microsoft.ML.Tokenizers;
using static TorchSharp.torch;
using TorchSharp;
using System.Text.Json;

var weightFolder = @"C:\Users\maxim\source\repos\models\microsoft\phi-4\";
var device = "cuda";
if (device == "cuda")
{
    InitializeDeviceType(DeviceType.CUDA);
}

var defaultType = ScalarType.Float16;
manual_seed(1);
set_default_dtype(defaultType);

var model = Phi3ForCasualLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt4: true);
var tokenizerPath = Path.Combine(weightFolder, "config.json");
var fileConfig = File.ReadAllText(tokenizerPath);
var config = JsonSerializer.Deserialize<Phi3Config>(fileConfig)!;
var tokenizer = TiktokenTokenizer.CreateForModel("gpt-4");
var pipeline = new CausalLMPipeline<Tokenizer, Phi3ForCasualLM>(tokenizer, model, device);
var client = new Phi3CausalLMChatClient(pipeline);

var task = """
            Can you tell me a funny joke?
            """;
var chatMessage = new ChatMessage(ChatRole.User, task);
var options = new ChatOptions 
{
    StopSequences = ["<|endoftext|>"],

};

await foreach (var response in client.CompleteStreamingAsync([chatMessage], options))
{
    Console.Write(response.Text);
}

Console.WriteLine();
Console.WriteLine("End!");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Tokenizers
Projects
None yet
Development

No branches or pull requests

3 participants