Skip to content

Commit

Permalink
Merge pull request #251 from msalemor/oga-errors-fix
Browse files Browse the repository at this point in the history
Updated Lab1 and Lab4 to dispose objects when closing the app
  • Loading branch information
leestott authored Feb 14, 2025
2 parents 04ebb9d + bb016cb commit 7c223fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
11 changes: 10 additions & 1 deletion md/07.Labs/Csharp/src/LabsPhi301/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

var model = new Model(modelPath);
var tokenizer = new Tokenizer(model);
using OgaHandle ogaHandle = new();

var systemPrompt = "You are an AI assistant that helps people find information. Answer questions using a direct style. Do not share more information that the requested by the users.";

Expand All @@ -44,7 +45,7 @@
// Get user question
Console.WriteLine();
Console.Write(@"Q: ");
var userQ = Console.ReadLine();
var userQ = Console.ReadLine();
if (string.IsNullOrEmpty(userQ))
{
break;
Expand All @@ -70,5 +71,13 @@
var output = tokenizer.Decode(newToken);
Console.Write(output);
}

// cleanup
generator.Dispose();
generatorParams.Dispose();
tokenizer.Dispose();
model.Dispose();
ogaHandle.Dispose();

Console.WriteLine();
}
36 changes: 29 additions & 7 deletions md/07.Labs/Csharp/src/LabsPhi304/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
SpectreConsoleOutput.DisplayTitle($".NET - Phi3v");

// load model and create processor
using Model model = new Model(modelPath);
using MultiModalProcessor processor = new MultiModalProcessor(model);
using Model model = new(modelPath);
using MultiModalProcessor processor = new(model);
using var tokenizerStream = processor.CreateStream();
var tokenizer = new Tokenizer(model);
using var tokenizer = new Tokenizer(model);
using OgaHandle ogaHandle = new();

// define prompts
var systemPrompt = "You are an AI assistant that helps people find information. Answer questions using a direct style. Do not share more information that the requested by the users.";
Expand Down Expand Up @@ -69,8 +70,23 @@
AnswerQuestion();
break;
}

DisposeAll();
SpectreConsoleOutput.DisplayTitleH3("Done !");

///
/// Dispose all resources
/// Fixes errors when exiting the application and the resources are not disposed
///
void DisposeAll()
{
model?.Dispose();
tokenizer?.Dispose();
tokenizerStream?.Dispose();
processor?.Dispose();
ogaHandle?.Dispose();
}

void AnswerQuestion()
{
var question = SpectreConsoleOutput.AskForString("Type a question");
Expand All @@ -80,12 +96,12 @@ void AnswerQuestion()
var fullPrompt = $"<|system|>{systemPrompt}<|end|><|user|>{question}<|end|><|assistant|>";
var tokens = tokenizer.Encode(fullPrompt);

var generatorParams = new GeneratorParams(model);
using var generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 2048);
generatorParams.SetSearchOption("past_present_share_buffer", false);
generatorParams.SetInputSequences(tokens);

var generator = new Generator(model, generatorParams);
using var generator = new Generator(model, generatorParams);
while (!generator.IsDone())
{
generator.ComputeLogits();
Expand All @@ -95,6 +111,8 @@ void AnswerQuestion()
var output = tokenizer.Decode(newToken);
Console.Write(output);
}
generator.Dispose();
generatorParams.Dispose();
Console.WriteLine();
}

Expand All @@ -113,7 +131,7 @@ void AnalizeImage(string imagePath)
AnsiConsole.Status()
.Start("Analyzing image ...", ctx =>
{
var img = Images.Load([imagePath]);
var img = Images.Load([imagePath]);
string userPrompt = "Describe the image, and return the string 'STOP' at the end.";
var fullPrompt = $"<|system|>{systemPrompt}<|end|><|user|><|image_1|>{userPrompt}<|end|><|assistant|>";

Expand All @@ -126,7 +144,7 @@ void AnalizeImage(string imagePath)
ctx.SpinnerStyle(Style.Parse("green"));


var inputTensors = processor.ProcessImages(fullPrompt, img);
using var inputTensors = processor.ProcessImages(fullPrompt, img);
using GeneratorParams generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 3072);
generatorParams.SetInputs(inputTensors);
Expand All @@ -153,8 +171,12 @@ void AnalizeImage(string imagePath)
AnsiConsole.Markup($"[bold][blue]>> Token:[/][/] {tokenString}");
phiResponse.Append(tokenString);
}
generator.Dispose();
generatorParams.Dispose();
inputTensors.Dispose();
});


// display the response
SpectreConsoleOutput.DisplaySubtitle("Phi-3 Response", phiResponse.ToString());
}

0 comments on commit 7c223fb

Please sign in to comment.