-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce97a3f
commit dd2e019
Showing
15 changed files
with
326 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.3.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34902.65 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloPhi3V", "HelloPhi3V.csproj", "{75C05439-20D3-44C3-883A-15E150E9F93E}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{75C05439-20D3-44C3-883A-15E150E9F93E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{75C05439-20D3-44C3-883A-15E150E9F93E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{75C05439-20D3-44C3-883A-15E150E9F93E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{75C05439-20D3-44C3-883A-15E150E9F93E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {58510186-ED6B-46C0-8D3D-DB5300239D3A} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.ML.OnnxRuntimeGenAI; | ||
|
||
class Program | ||
{ | ||
static void Run(string modelPath) | ||
{ | ||
using Model model = new Model(modelPath); | ||
using MultiModalProcessor processor = new MultiModalProcessor(model); | ||
using var tokenizerStream = processor.CreateStream(); | ||
|
||
while (true) | ||
{ | ||
Console.WriteLine("Image Path (leave empty if no image):"); | ||
string imagePath = Console.ReadLine(); | ||
|
||
Images images = null; | ||
if (imagePath == String.Empty) | ||
{ | ||
Console.WriteLine("No image provided"); | ||
} | ||
else | ||
{ | ||
if (!File.Exists(imagePath)) | ||
{ | ||
throw new Exception("Image file not found: " + imagePath); | ||
} | ||
images = Images.Load(imagePath); | ||
} | ||
|
||
Console.WriteLine("Prompt:"); | ||
string text = Console.ReadLine(); | ||
string prompt = "<|user|>\n"; | ||
if (images != null) | ||
{ | ||
prompt += "<|image_1|>\n"; | ||
} | ||
prompt += text + "<|end|>\n<|assistant|>\n"; | ||
|
||
Console.WriteLine("Processing image and prompt..."); | ||
var inputTensors = processor.ProcessImages(prompt, images); | ||
|
||
Console.WriteLine("Generating response..."); | ||
using GeneratorParams generatorParams = new GeneratorParams(model); | ||
generatorParams.SetSearchOption("max_length", 3072); | ||
generatorParams.SetInputs(inputTensors); | ||
|
||
using var generator = new Generator(model, generatorParams); | ||
while (!generator.IsDone()) | ||
{ | ||
generator.ComputeLogits(); | ||
generator.GenerateNextToken(); | ||
Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1])); | ||
} | ||
} | ||
|
||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("--------------------"); | ||
Console.WriteLine("Hello, Phi-3-Vision!"); | ||
Console.WriteLine("--------------------"); | ||
|
||
if (args.Length != 1) | ||
{ | ||
throw new Exception("Usage: .\\HelloPhi3V <model_path>"); | ||
} | ||
|
||
Run(args[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI | ||
{ | ||
public class Images : IDisposable | ||
{ | ||
private IntPtr _imagesHandle; | ||
private bool _disposed = false; | ||
|
||
private Images(IntPtr imagesHandle) | ||
{ | ||
_imagesHandle = imagesHandle; | ||
} | ||
|
||
internal IntPtr Handle { get { return _imagesHandle; } } | ||
|
||
public static Images Load(string imagePath) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaLoadImage(StringUtils.ToUtf8(imagePath), out IntPtr imagesHandle)); | ||
return new Images(imagesHandle); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
NativeMethods.OgaDestroyImages(_imagesHandle); | ||
_imagesHandle = IntPtr.Zero; | ||
_disposed = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI | ||
{ | ||
public class MultiModalProcessor : IDisposable | ||
{ | ||
private IntPtr _processorHandle; | ||
private bool _disposed = false; | ||
|
||
public MultiModalProcessor(Model model) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaCreateMultiModalProcessor(model.Handle, out _processorHandle)); | ||
} | ||
|
||
internal IntPtr Handle { get { return _processorHandle; } } | ||
|
||
public NamedTensors ProcessImages(string prompt, Images images) | ||
{ | ||
IntPtr imagesHandle = images == null ? IntPtr.Zero : images.Handle; | ||
Result.VerifySuccess(NativeMethods.OgaProcessorProcessImages(_processorHandle, StringUtils.ToUtf8(prompt), | ||
imagesHandle, out IntPtr namedTensorsHandle)); | ||
return new NamedTensors(namedTensorsHandle); | ||
} | ||
|
||
public string Decode(ReadOnlySpan<int> sequence) | ||
{ | ||
IntPtr outStr = IntPtr.Zero; | ||
unsafe | ||
{ | ||
fixed (int* sequencePtr = sequence) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaProcessorDecode(_processorHandle, sequencePtr, (UIntPtr)sequence.Length, out outStr)); | ||
} | ||
} | ||
try | ||
{ | ||
return StringUtils.FromUtf8(outStr); | ||
} | ||
finally | ||
{ | ||
NativeMethods.OgaDestroyString(outStr); | ||
} | ||
} | ||
|
||
public TokenizerStream CreateStream() | ||
{ | ||
IntPtr tokenizerStreamHandle = IntPtr.Zero; | ||
Result.VerifySuccess(NativeMethods.OgaCreateTokenizerStreamFromProcessor(_processorHandle, out tokenizerStreamHandle)); | ||
return new TokenizerStream(tokenizerStreamHandle); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
NativeMethods.OgaDestroyMultiModalProcessor(_processorHandle); | ||
_processorHandle = IntPtr.Zero; | ||
_disposed = true; | ||
} | ||
} | ||
} |
Oops, something went wrong.