diff --git a/docs/gen-ai/CausalLMPipeline.md b/docs/gen-ai/CausalLMPipeline.md index aeab451ae4..a1c61c2bcd 100644 --- a/docs/gen-ai/CausalLMPipeline.md +++ b/docs/gen-ai/CausalLMPipeline.md @@ -20,11 +20,11 @@ public abstract class CausalLMPipeline bool echo = false); // echo the input token ids in the output token ids } -public CasualLMPipeline : CausalLMPipeline +public CausalLMPipeline : CausalLMPipeline where TTokenizer : ITokenizer where TCausalLM : nn.Module { - public CausalLMPipeline Create(LLama2Tokenizer tokenizer, Phi3ForCasualLM model); + public CausalLMPipeline Create(LLama2Tokenizer tokenizer, Phi3ForCausalLM model); } ``` @@ -105,7 +105,7 @@ The extension `Generate` method provides a even-easier way to generate text with ```C# public static string Generate( - this CasualLMPipeline pipeline, + this CausalLMPipeline pipeline, string prompt, int maxLen = 128, float temperature = 0.7f, diff --git a/docs/gen-ai/Usage.md b/docs/gen-ai/Usage.md index 611d5fb22e..40b86a3282 100644 --- a/docs/gen-ai/Usage.md +++ b/docs/gen-ai/Usage.md @@ -7,9 +7,9 @@ This document shows how to use the causal language model API for text generation ```C# var pathToPhi3 = "path/to/phi3"; var tokenizer = LLama2Tokenizer.FromPretrained(pathToPhi3); -var phi3CausalModel = Phi3ForCasualLM.FromPretrained(pathToPhi3); +var phi3CausalModel = Phi3ForCausalLM.FromPretrained(pathToPhi3); -CausalLMPipeline pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); +CausalLMPipeline pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); var prompt = "<|user|>Once upon a time<|end|>"; var output = pipeline.Generate( @@ -24,8 +24,8 @@ In most cases, developers would like to consume the model in a uniformed way. In ```C# var pathToPhi3 = "path/to/phi3"; var tokenizer = LLama2Tokenizer.FromPretrained(pathToPhi3); -var phi3CausalModel = Phi3ForCasualLM.FromPretrained(pathToPhi3); -CausalLMPipeline pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); +var phi3CausalModel = Phi3ForCausalLM.FromPretrained(pathToPhi3); +CausalLMPipeline pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); var kernel = Kernel.CreateBuilder() // the type of the tokenizer and the model are explicitly specified // here for clarity, but the compiler can infer them @@ -33,7 +33,7 @@ var kernel = Kernel.CreateBuilder() // The reason why we don't want to allow developers to pass an arbitrary CausalLMPipeline is because // - the model and the tokenizer must be compatible // - the chat template must be compatible with the model. e.g. In `AddPhi3AsChatCompletionService`, the chat template is fixed to "<|user|>{prompt}<|end|>" - .AddPhi3AsChatCompletionService(pipeline) + .AddPhi3AsChatCompletionService(pipeline) .Build(); ``` @@ -42,7 +42,7 @@ Similarly, developers would also like to consume the language model like agent. ```C# var pathToPhi3 = "path/to/phi3"; var tokenizer = LLama2Tokenizer.FromPretrained(pathToPhi3); -var phi3CausalModel = Phi3ForCasualLM.FromPretrained(pathToPhi3); +var phi3CausalModel = Phi3ForCausalLM.FromPretrained(pathToPhi3); var pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); var agent = new Phi3MiniAgent(pipeline, name: "assistant"); @@ -59,7 +59,7 @@ If the model is deployed as a service, developers can consume the model similar // server.cs var pathToPhi3 = "path/to/phi3"; var tokenizer = LLama2Tokenizer.FromPretrained(pathToPhi3); -var phi3CausalModel = Phi3ForCasualLM.FromPretrained(pathToPhi3); +var phi3CausalModel = Phi3ForCausalLM.FromPretrained(pathToPhi3); var pipeline = new CausalLMPipeline(tokenizer, phi3CausalModel); var agent = new Phi3MiniAgent(pipeline, name: "assistant"); diff --git a/docs/samples/Microsoft.ML.GenAI.Samples/Llama/SFT_Llama_3_2_1B.cs b/docs/samples/Microsoft.ML.GenAI.Samples/Llama/SFT_Llama_3_2_1B.cs index 98f4ae71ef..33443496a4 100644 --- a/docs/samples/Microsoft.ML.GenAI.Samples/Llama/SFT_Llama_3_2_1B.cs +++ b/docs/samples/Microsoft.ML.GenAI.Samples/Llama/SFT_Llama_3_2_1B.cs @@ -25,7 +25,7 @@ public static async Task Train(string weightFolder, string checkPointName = "mod using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); // create logger - var logger = loggerFactory.CreateLogger(); + var logger = loggerFactory.CreateLogger(); var device = "cuda"; @@ -46,10 +46,10 @@ public static async Task Train(string weightFolder, string checkPointName = "mod var input = CreateDataset(dataset, pipeline.TypedTokenizer, Llama3_1ChatTemplateBuilder.Instance); // create trainer - var sftTrainer = new CasualLMSupervisedFineTuningTrainer(pipeline, logger: logger); + var sftTrainer = new CausalLMSupervisedFineTuningTrainer(pipeline, logger: logger); // Train the model - var option = new CasualLMSupervisedFineTuningTrainer.Option + var option = new CausalLMSupervisedFineTuningTrainer.Option { BatchSize = 1, Device = device, diff --git a/docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Phi3.cs b/docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Phi3.cs index a10c01dfb3..e1db7c6d95 100644 --- a/docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Phi3.cs +++ b/docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Phi3.cs @@ -27,8 +27,8 @@ public static async Task RunAsync(string weightFolder) torch.set_default_dtype(defaultType); var tokenizerPath = Path.Combine(weightFolder, "tokenizer.model"); var tokenizer = Phi3TokenizerHelper.FromPretrained(tokenizerPath); - var model = Phi3ForCasualLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); - var pipeline = new CausalLMPipeline(tokenizer, model, device); + var model = Phi3ForCausalLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); + var pipeline = new CausalLMPipeline(tokenizer, model, device); var client = new Phi3CausalLMChatClient(pipeline); var task = """ diff --git a/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/AutoGenSample.cs b/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/AutoGenSample.cs index 20f2dd4418..76b641a532 100644 --- a/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/AutoGenSample.cs +++ b/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/AutoGenSample.cs @@ -29,8 +29,8 @@ public static async Task RunAsync() var weightFolder = @"C:\Users\xiaoyuz\source\repos\Phi-3-mini-4k-instruct"; var tokenizerPath = Path.Combine(weightFolder, "tokenizer.model"); var tokenizer = Phi3TokenizerHelper.FromPretrained(tokenizerPath); - var model = Phi3ForCasualLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); - var pipeline = new CausalLMPipeline(tokenizer, model, device); + var model = Phi3ForCausalLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); + var pipeline = new CausalLMPipeline(tokenizer, model, device); var question = @"write a C# program to calculate the factorial of a number"; // agent diff --git a/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/SemanticKernelSample.cs b/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/SemanticKernelSample.cs index 8ba882618b..6684627b58 100644 --- a/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/SemanticKernelSample.cs +++ b/docs/samples/Microsoft.ML.GenAI.Samples/Phi3Mini/SemanticKernelSample.cs @@ -25,8 +25,8 @@ public static async Task RunChatCompletionSample() var weightFolder = @"C:\Users\xiaoyuz\source\repos\Phi-3-mini-4k-instruct"; var tokenizerPath = Path.Combine(weightFolder, "tokenizer.model"); var tokenizer = Phi3TokenizerHelper.FromPretrained(tokenizerPath); - var model = Phi3ForCasualLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); - var pipeline = new CausalLMPipeline(tokenizer, model, device); + var model = Phi3ForCausalLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); + var pipeline = new CausalLMPipeline(tokenizer, model, device); var kernel = Kernel.CreateBuilder() .AddGenAIChatCompletion(pipeline) @@ -56,8 +56,8 @@ public static async Task RunTextGenerationSample() var weightFolder = @"C:\Users\xiaoyuz\source\repos\Phi-3-mini-4k-instruct"; var tokenizerPath = Path.Combine(weightFolder, "tokenizer.model"); var tokenizer = Phi3TokenizerHelper.FromPretrained(tokenizerPath); - var model = Phi3ForCasualLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); - var pipeline = new CausalLMPipeline(tokenizer, model, device); + var model = Phi3ForCausalLM.FromPretrained(weightFolder, "config.json", layersOnTargetDevice: -1, quantizeToInt8: true); + var pipeline = new CausalLMPipeline(tokenizer, model, device); var kernel = Kernel.CreateBuilder() .AddGenAITextGeneration(pipeline) diff --git a/src/Microsoft.ML.GenAI.Core/Trainer/CasualLMSupervisedFineTuningTrainer.cs b/src/Microsoft.ML.GenAI.Core/Trainer/CausalLMSupervisedFineTuningTrainer.cs similarity index 93% rename from src/Microsoft.ML.GenAI.Core/Trainer/CasualLMSupervisedFineTuningTrainer.cs rename to src/Microsoft.ML.GenAI.Core/Trainer/CausalLMSupervisedFineTuningTrainer.cs index f5ee202cd5..ae447b60c4 100644 --- a/src/Microsoft.ML.GenAI.Core/Trainer/CasualLMSupervisedFineTuningTrainer.cs +++ b/src/Microsoft.ML.GenAI.Core/Trainer/CausalLMSupervisedFineTuningTrainer.cs @@ -13,12 +13,12 @@ namespace Microsoft.ML.GenAI.Core.Trainer; -public class CasualLMSupervisedFineTuningTrainer +public class CausalLMSupervisedFineTuningTrainer { - private readonly ILogger? _logger; + private readonly ILogger? _logger; private readonly ICausalLMPipeline _pipeline; - public CasualLMSupervisedFineTuningTrainer(ICausalLMPipeline pipeline, ILogger? logger = null) + public CausalLMSupervisedFineTuningTrainer(ICausalLMPipeline pipeline, ILogger? logger = null) { _logger = logger; _pipeline = pipeline; diff --git a/src/Microsoft.ML.GenAI.Core/Utility/AttentionMaskConverter.cs b/src/Microsoft.ML.GenAI.Core/Utility/AttentionMaskConverter.cs index b292c3d731..a2a3f92aea 100644 --- a/src/Microsoft.ML.GenAI.Core/Utility/AttentionMaskConverter.cs +++ b/src/Microsoft.ML.GenAI.Core/Utility/AttentionMaskConverter.cs @@ -13,12 +13,12 @@ namespace Microsoft.ML.GenAI.Core; public class AttentionMaskConverter { - private readonly bool _isCasual; + private readonly bool _isCausal; private readonly int? _slidingWindow; public AttentionMaskConverter(bool isCausal, int? slidingWindow) { - this._isCasual = isCausal; + this._isCausal = isCausal; this._slidingWindow = slidingWindow; } @@ -42,8 +42,8 @@ public Tensor To4D( // create causal mask // [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] - Tensor? casual4dMask = null; - if ((inputShape[^1] > 1 || this._slidingWindow is not null) && this._isCasual) + Tensor? causal4dMask = null; + if ((inputShape[^1] > 1 || this._slidingWindow is not null) && this._isCausal) { if (keyValueLength is null) { @@ -51,7 +51,7 @@ public Tensor To4D( } var pastKeyValuesLength = keyValueLength.Value - queryLength; - casual4dMask = MakeCasualMask(inputShape, dType, attentionMask2d.device, pastKeyValuesLength, this._slidingWindow); + causal4dMask = MakeCausalMask(inputShape, dType, attentionMask2d.device, pastKeyValuesLength, this._slidingWindow); } else if (this._slidingWindow is not null) { @@ -59,25 +59,25 @@ public Tensor To4D( } var expandedAttnMask = ExpandMask(attentionMask2d, dType, queryLength).to(attentionMask2d.device); - if (casual4dMask is not null) + if (causal4dMask is not null) { var min = torch.finfo(dType).min; - expandedAttnMask = casual4dMask.masked_fill(expandedAttnMask.to(ScalarType.Bool), min); + expandedAttnMask = causal4dMask.masked_fill(expandedAttnMask.to(ScalarType.Bool), min); } return expandedAttnMask; } - public Tensor? ToCasual4D( + public Tensor? ToCausal4D( int batchSize, int queryLength, int keyValueLength, ScalarType dType, Device device) { - if (!_isCasual) + if (!_isCausal) { - throw new ArgumentException("This is not a casual mask"); + throw new ArgumentException("This is not a causal mask"); } long[] inputShape = [batchSize, queryLength]; @@ -88,13 +88,13 @@ public Tensor To4D( Tensor? causal4DMask = null; if (queryLength > 1 || this._slidingWindow is int) { - causal4DMask = MakeCasualMask(inputShape, dType, device, pastKeyValueLength, this._slidingWindow); + causal4DMask = MakeCausalMask(inputShape, dType, device, pastKeyValueLength, this._slidingWindow); } return causal4DMask; } - public static Tensor MakeCasualMask( + public static Tensor MakeCausalMask( long[] inputIdsShape, ScalarType dType, Device device, @@ -158,7 +158,7 @@ public static Tensor MakeCasualMask( return converter.To4D(attentionMask, (int)inputShape[1], dType, keyValueLength); } - return converter.ToCasual4D(batchSize, queryLength, keyValueLength, dType, device); + return converter.ToCausal4D(batchSize, queryLength, keyValueLength, dType, device); } public static Tensor ExpandMask( diff --git a/src/Microsoft.ML.GenAI.Phi/Extension/SemanticKernelExtension.cs b/src/Microsoft.ML.GenAI.Phi/Extension/SemanticKernelExtension.cs index ace7a7b425..82b4b759c5 100644 --- a/src/Microsoft.ML.GenAI.Phi/Extension/SemanticKernelExtension.cs +++ b/src/Microsoft.ML.GenAI.Phi/Extension/SemanticKernelExtension.cs @@ -15,7 +15,7 @@ public static class SemanticKernelExtension { public static IKernelBuilder AddGenAIChatCompletion( this IKernelBuilder builder, - ICausalLMPipeline pipeline) + ICausalLMPipeline pipeline) { builder.Services.AddSingleton(new Phi3CausalLMChatCompletionService(pipeline)); @@ -24,7 +24,7 @@ public static IKernelBuilder AddGenAIChatCompletion( public static IKernelBuilder AddGenAITextGeneration( this IKernelBuilder builder, - ICausalLMPipeline pipeline) + ICausalLMPipeline pipeline) { builder.Services.AddSingleton(new Phi3CausalLMTextGenerationService(pipeline)); diff --git a/src/Microsoft.ML.GenAI.Phi/Module/Phi2Model.cs b/src/Microsoft.ML.GenAI.Phi/Module/Phi2Model.cs index b96e0409f9..05edab0985 100644 --- a/src/Microsoft.ML.GenAI.Phi/Module/Phi2Model.cs +++ b/src/Microsoft.ML.GenAI.Phi/Module/Phi2Model.cs @@ -80,7 +80,7 @@ public override (Tensor, Tensor?, Tensor?) forward( // use 4d attention mask if (attentionMask is not null) { - attentionMask = this.Prepare4DCasualAttentionMask(attentionMask, seqLen, pastKeyValueLength, inputEmbeddings.dtype); + attentionMask = this.Prepare4DCausalAttentionMask(attentionMask, seqLen, pastKeyValueLength, inputEmbeddings.dtype); } var hiddenStates = inputEmbeddings; @@ -100,7 +100,7 @@ public override (Tensor, Tensor?, Tensor?) forward( return (hiddenStates, null, null); } - private Tensor Prepare4DCasualAttentionMask( + private Tensor Prepare4DCausalAttentionMask( Tensor attentionMask, int queryLength, int pastKeyValueLength, @@ -110,11 +110,11 @@ private Tensor Prepare4DCasualAttentionMask( var seqLen = attentionMask.shape[1]; Contract.Assert(seqLen == queryLength, "seqLen must be equal to queryLength"); var targetLength = queryLength + pastKeyValueLength; - var casual4DMask = this.MakeCasualAttentionMask(batchSize, queryLength, pastKeyValueLength, attentionMask.device, dtype); + var causal4DMask = this.MakeCausalAttentionMask(batchSize, queryLength, pastKeyValueLength, attentionMask.device, dtype); var expandedMask = this.ExpandMask(attentionMask, dtype, queryLength).to(attentionMask.device); - casual4DMask.masked_fill_(expandedMask.to_type(ScalarType.Bool), torch.finfo(dtype).min); - return casual4DMask; + causal4DMask.masked_fill_(expandedMask.to_type(ScalarType.Bool), torch.finfo(dtype).min); + return causal4DMask; } private Tensor ExpandMask( @@ -132,7 +132,7 @@ private Tensor ExpandMask( return invertedMask.masked_fill(invertedMask.to_type(ScalarType.Bool), torch.finfo(dtype).min); } - private Tensor MakeCasualAttentionMask( + private Tensor MakeCausalAttentionMask( int batchSize, int targetLen, int pastKeyValueLength, diff --git a/src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCasualLM.cs b/src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCausalLM.cs similarity index 91% rename from src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCasualLM.cs rename to src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCausalLM.cs index 1d49375565..3c8c5c0a5b 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCasualLM.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi2/Phi2ForCausalLM.cs @@ -14,15 +14,15 @@ namespace Microsoft.ML.GenAI.Phi; -public class Phi2ForCasualLM : nn.Module +public class Phi2ForCausalLM : nn.Module { #pragma warning disable MSML_PrivateFieldName // Private field name not in: _camelCase format private readonly Phi2Model model; private readonly GenAILinear lm_head; #pragma warning restore MSML_PrivateFieldName // Private field name not in: _camelCase format - public Phi2ForCasualLM(Phi2Config config) - : base(nameof(Phi2ForCasualLM)) + public Phi2ForCausalLM(Phi2Config config) + : base(nameof(Phi2ForCausalLM)) { this.model = new Phi2Model(config); this.lm_head = new GenAILinear(config.HiddenSize, config.VocabSize, dtype: config.Dtype); @@ -47,7 +47,7 @@ public override CausalLMModelOutput forward(CausalLMModelInput input) // use_cac return new CausalLMModelOutput(lastHiddenState: hiddenState, logits: lmLogits); } - public static Phi2ForCasualLM FromPretrained( + public static Phi2ForCausalLM FromPretrained( string modelFolder, string configName = "config.json", string checkPointName = "model.safetensors.index.json", @@ -58,7 +58,7 @@ public static Phi2ForCasualLM FromPretrained( var config = Path.Join(modelFolder, configName); var modelConfig = JsonSerializer.Deserialize(File.ReadAllText(config)) ?? throw new ArgumentNullException(nameof(config)); modelConfig.Dtype = torchDtype; - var wrapper = new Phi2ForCasualLM(modelConfig); + var wrapper = new Phi2ForCausalLM(modelConfig); var loadedParameters = new Dictionary(); wrapper.load_checkpoint(path: modelFolder, checkpointName: checkPointName, strict: true, loadedParameters: loadedParameters, useTqdm: useTqdm); wrapper = wrapper.to(device); diff --git a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMAgent.cs b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMAgent.cs index 6971ac5991..e1e02d338b 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMAgent.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMAgent.cs @@ -17,12 +17,12 @@ namespace Microsoft.ML.GenAI.Phi; public class Phi3Agent : IStreamingAgent { private const char Newline = '\n'; - private readonly ICausalLMPipeline _pipeline; + private readonly ICausalLMPipeline _pipeline; private readonly string? _systemMessage; private readonly IAutoGenChatTemplateBuilder _templateBuilder; public Phi3Agent( - ICausalLMPipeline pipeline, + ICausalLMPipeline pipeline, string name, string? systemMessage = "you are a helpful assistant", IAutoGenChatTemplateBuilder? templateBuilder = null) diff --git a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatClient.cs b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatClient.cs index e297e40c5a..ebd74e4848 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatClient.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatClient.cs @@ -14,12 +14,12 @@ namespace Microsoft.ML.GenAI.Phi; -public class Phi3CausalLMChatClient : CausalLMPipelineChatClient +public class Phi3CausalLMChatClient : CausalLMPipelineChatClient { private readonly string _eotToken = "<|end|>"; public Phi3CausalLMChatClient( - ICausalLMPipeline pipeline, + ICausalLMPipeline pipeline, IMEAIChatTemplateBuilder? templateBuilder = null, ChatClientMetadata? metadata = null) : base( diff --git a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatCompletionService.cs b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatCompletionService.cs index 1d95882655..896926c43b 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatCompletionService.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatCompletionService.cs @@ -14,12 +14,12 @@ namespace Microsoft.ML.GenAI.Phi; public class Phi3CausalLMChatCompletionService : IChatCompletionService { - private readonly ICausalLMPipeline _pipeline; + private readonly ICausalLMPipeline _pipeline; private readonly Phi3CausalLMTextGenerationService _textGenerationService; private readonly ISemanticKernelChatTemplateBuilder _templateBuilder; public Phi3CausalLMChatCompletionService( - ICausalLMPipeline pipeline, + ICausalLMPipeline pipeline, ISemanticKernelChatTemplateBuilder? templateBuilder = null) { _pipeline = pipeline; diff --git a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMTextGenerationService.cs b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMTextGenerationService.cs index d4c8c34e85..01a574c8f6 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMTextGenerationService.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMTextGenerationService.cs @@ -12,10 +12,10 @@ namespace Microsoft.ML.GenAI.Phi; public class Phi3CausalLMTextGenerationService : ITextGenerationService { - private readonly ICausalLMPipeline _pipeline; + private readonly ICausalLMPipeline _pipeline; public Phi3CausalLMTextGenerationService( - ICausalLMPipeline pipeline) + ICausalLMPipeline pipeline) { _pipeline = pipeline; } diff --git a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCasualLM.cs b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCausalLM.cs similarity index 90% rename from src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCasualLM.cs rename to src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCausalLM.cs index a5840b242a..c46cbaf57e 100644 --- a/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCasualLM.cs +++ b/src/Microsoft.ML.GenAI.Phi/Phi3/Phi3ForCausalLM.cs @@ -18,7 +18,7 @@ namespace Microsoft.ML.GenAI.Phi; -public class Phi3ForCasualLM : nn.Module +public class Phi3ForCausalLM : nn.Module { private readonly Phi3Config _config; @@ -27,8 +27,8 @@ public class Phi3ForCasualLM : nn.Module(File.ReadAllText(config)) ?? throw new ArgumentNullException(nameof(config)); modelConfig.DType = torchDtype; - var phi = new Phi3ForCasualLM(modelConfig); + var phi = new Phi3ForCausalLM(modelConfig); phi.LoadSafeTensors(modelFolder, checkPointName); phi = phi.to(device); phi.eval(); @@ -67,7 +67,7 @@ public static Phi3ForCasualLM FromPretrained( return phi; } - public static Phi3ForCasualLM FromPretrained( + public static Phi3ForCausalLM FromPretrained( string modelFolder, string configName = "config.json", string checkPointName = "model.safetensors.index.json", @@ -87,7 +87,7 @@ public static Phi3ForCasualLM FromPretrained( var config = Path.Join(modelFolder, configName); var modelConfig = JsonSerializer.Deserialize(File.ReadAllText(config)) ?? throw new ArgumentNullException(nameof(config)); modelConfig.DType = torchDtype; - var model = new Phi3ForCasualLM(modelConfig); + var model = new Phi3ForCausalLM(modelConfig); if (quantizeToInt8) { @@ -105,7 +105,7 @@ public static Phi3ForCasualLM FromPretrained( ]); torch.set_default_device("cpu"); - model = new Phi3ForCasualLM(modelConfig); + model = new Phi3ForCausalLM(modelConfig); model.LoadSafeTensors(modelFolder, checkPointName); diff --git a/src/Microsoft.ML.GenAI.Phi/README.md b/src/Microsoft.ML.GenAI.Phi/README.md index 2daf51039e..0e0bbb4dda 100644 --- a/src/Microsoft.ML.GenAI.Phi/README.md +++ b/src/Microsoft.ML.GenAI.Phi/README.md @@ -25,7 +25,7 @@ git clone https://huggingface.co/microsoft/Phi-3-mini-4k-instruct var weightFolder = "/path/to/Phi-3-mini-4k-instruct"; var configName = "config.json"; var config = JsonSerializier.Deserialize(File.ReadAllText(Path.Combine(weightFolder, configName))); -var model = new Phi3ForCasualLM(config); +var model = new Phi3ForCausalLM(config); // load tokenizer var tokenizerModelName = "tokenizer.model"; diff --git a/test/Microsoft.ML.GenAI.Core.Tests/CasualLMDatasetTest.cs b/test/Microsoft.ML.GenAI.Core.Tests/CausalLMDatasetTest.cs similarity index 99% rename from test/Microsoft.ML.GenAI.Core.Tests/CasualLMDatasetTest.cs rename to test/Microsoft.ML.GenAI.Core.Tests/CausalLMDatasetTest.cs index f451dcb718..25b8874d7d 100644 --- a/test/Microsoft.ML.GenAI.Core.Tests/CasualLMDatasetTest.cs +++ b/test/Microsoft.ML.GenAI.Core.Tests/CausalLMDatasetTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.ML.GenAI.Core.Tests; -public class CasualLMDatasetTest +public class CausalLMDatasetTest { private static Tokenizer CreateLlamaTokenizer() { diff --git a/test/Microsoft.ML.GenAI.Core.Tests/Microsoft.ML.GenAI.Core.Tests.csproj b/test/Microsoft.ML.GenAI.Core.Tests/Microsoft.ML.GenAI.Core.Tests.csproj index f9ff796999..c565c05c64 100644 --- a/test/Microsoft.ML.GenAI.Core.Tests/Microsoft.ML.GenAI.Core.Tests.csproj +++ b/test/Microsoft.ML.GenAI.Core.Tests/Microsoft.ML.GenAI.Core.Tests.csproj @@ -26,7 +26,7 @@ - + diff --git a/test/Microsoft.ML.GenAI.Phi.Tests/AutoGenTests.cs b/test/Microsoft.ML.GenAI.Phi.Tests/AutoGenTests.cs index 33ab565fe7..6f0928f3c0 100644 --- a/test/Microsoft.ML.GenAI.Phi.Tests/AutoGenTests.cs +++ b/test/Microsoft.ML.GenAI.Phi.Tests/AutoGenTests.cs @@ -16,7 +16,7 @@ public class AutoGenTests [Fact] public async Task ItGenerateTextReply() { - var pipeline = Mock.Of>(); + var pipeline = Mock.Of>(); // mock generate api Mock.Get(pipeline).Setup(p => p.Generate( It.IsAny(), // prompt diff --git a/test/Microsoft.ML.GenAI.Phi.Tests/Phi2Tests.cs b/test/Microsoft.ML.GenAI.Phi.Tests/Phi2Tests.cs index 33402e73a0..a196d8ab81 100644 --- a/test/Microsoft.ML.GenAI.Phi.Tests/Phi2Tests.cs +++ b/test/Microsoft.ML.GenAI.Phi.Tests/Phi2Tests.cs @@ -30,7 +30,7 @@ public Phi2Tests() [UseApprovalSubdirectory("Approvals")] public void LoadSafeTensorShapeTest() { - var model = new Phi2ForCasualLM(Phi2Config.Phi2); + var model = new Phi2ForCausalLM(Phi2Config.Phi2); var stateDictStr = model.PeekShape(); Approvals.Verify(stateDictStr); } diff --git a/test/Microsoft.ML.GenAI.Phi.Tests/Phi3Tests.cs b/test/Microsoft.ML.GenAI.Phi.Tests/Phi3Tests.cs index 1200d79f9d..aec4016fd7 100644 --- a/test/Microsoft.ML.GenAI.Phi.Tests/Phi3Tests.cs +++ b/test/Microsoft.ML.GenAI.Phi.Tests/Phi3Tests.cs @@ -32,7 +32,7 @@ public Phi3Tests() [UseApprovalSubdirectory("Approvals")] public void Phi3Mini4KShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Mini4kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Mini4kInstruct); var stateDictStr = model.PeekShape(); Approvals.Verify(stateDictStr); } @@ -42,7 +42,7 @@ public void Phi3Mini4KShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Mini4KInt8QuantizeShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Mini4kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Mini4kInstruct); model.ToInt8QuantizeModule(); var size = model.GetSizeInBytes(); var stateDictStr = model.PeekShape(); @@ -56,7 +56,7 @@ public void Phi3Mini4KInt8QuantizeShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Mini4KInt4QuantizeShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Mini4kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Mini4kInstruct); model.ToInt4QuantizeModule(); var size = model.GetSizeInBytes(); var stateDictStr = model.PeekShape(); @@ -70,7 +70,7 @@ public void Phi3Mini4KInt4QuantizeShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Medium4KShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Medium4kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Medium4kInstruct); var stateDictStr = model.PeekShape(); Approvals.Verify(stateDictStr); } @@ -81,7 +81,7 @@ public void Phi3Medium4KShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Medium128KShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Medium128kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Medium128kInstruct); var stateDictStr = model.PeekShape(); Approvals.Verify(stateDictStr); } @@ -91,7 +91,7 @@ public void Phi3Medium128KShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Mini128KShapeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Mini128kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Mini128kInstruct); var stateDictStr = model.PeekShape(); Approvals.Verify(stateDictStr); } @@ -101,7 +101,7 @@ public void Phi3Mini128KShapeTest() [UseApprovalSubdirectory("Approvals")] public void Phi3Mini128KLayerSizeTest() { - var model = new Phi3ForCasualLM(Phi3Config.Phi3Mini128kInstruct); + var model = new Phi3ForCausalLM(Phi3Config.Phi3Mini128kInstruct); var size = model.GetSizeForEachDynamicLayerInBytes(); // convert size to MB var sizeInMB = size.ToDictionary(x => x.Key, x => x.Value / 1024 / 1024); diff --git a/test/Microsoft.ML.GenAI.Phi.Tests/SemanticKernelTests.cs b/test/Microsoft.ML.GenAI.Phi.Tests/SemanticKernelTests.cs index 98359a8722..63777bc3bf 100644 --- a/test/Microsoft.ML.GenAI.Phi.Tests/SemanticKernelTests.cs +++ b/test/Microsoft.ML.GenAI.Phi.Tests/SemanticKernelTests.cs @@ -19,7 +19,7 @@ public class SemanticKernelTests [Fact] public async Task ItAddPhi3CausalLMChatCompletionServiceTestAsync() { - var pipeline = Mock.Of>(); + var pipeline = Mock.Of>(); // mock generate api Mock.Get(pipeline).Setup(p => p.Generate( It.IsAny(), // prompt @@ -55,7 +55,7 @@ public async Task ItAddPhi3CausalLMChatCompletionServiceTestAsync() [Fact] public async Task ItAddPhi3CausalLMTextGenerationServiceTestAsync() { - var pipeline = Mock.Of>(); + var pipeline = Mock.Of>(); // mock generate api Mock.Get(pipeline).Setup(p => p.Generate( It.IsAny(), // prompt