Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit bac6d5a

Browse files
committed
Merge branch 'master' into LCM_Inpaint
2 parents d67ea08 + 9417b17 commit bac6d5a

File tree

60 files changed

+951
-406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+951
-406
lines changed

.dockerignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**/.dockerignore
2+
**/.env
3+
**/.git
4+
**/.gitignore
5+
**/.project
6+
**/.settings
7+
**/.toolstarget
8+
**/.vs
9+
**/.vscode
10+
**/.idea
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
docker-test-output/
25+
LICENSE
26+
README.md

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,4 @@ test/TensorFlowNET.Examples/mnist
344344
site/
345345

346346
/OnnxStack.WebUI/wwwroot/images/Results/*
347+
docker-test-output/*

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
2+
WORKDIR /app
3+
4+
# Install Git and Git LFS
5+
RUN apt-get update && apt-get install -y curl
6+
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash && apt-get install -y git-lfs
7+
8+
# Clone the Stable Diffusion 1.5 base model
9+
RUN git clone https://huggingface.co/runwayml/stable-diffusion-v1-5 -b onnx
10+
11+
# Clone the LCM Dreamshaper V7 model
12+
RUN git clone https://huggingface.co/TheyCallMeHex/LCM-Dreamshaper-V7-ONNX
13+
14+
COPY . .
15+
RUN dotnet build OnnxStackCore.sln
16+
17+
ENTRYPOINT ["dotnet", "test", "OnnxStackCore.sln"]

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public async Task RunAsync()
5252
foreach (var model in _stableDiffusionService.Models)
5353
{
5454
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
55-
await _stableDiffusionService.LoadModel(model);
55+
await _stableDiffusionService.LoadModelAsync(model);
5656

5757
foreach (var schedulerType in model.PipelineType.GetSchedulerTypes())
5858
{
@@ -62,7 +62,7 @@ public async Task RunAsync()
6262
}
6363

6464
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
65-
await _stableDiffusionService.UnloadModel(model);
65+
await _stableDiffusionService.UnloadModelAsync(model);
6666
}
6767
break;
6868
}

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task RunAsync()
5454
foreach (var model in _stableDiffusionService.Models)
5555
{
5656
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
57-
await _stableDiffusionService.LoadModel(model);
57+
await _stableDiffusionService.LoadModelAsync(model);
5858

5959
var batchIndex = 0;
6060
var callback = (int batch, int batchCount, int step, int steps) =>
@@ -72,7 +72,7 @@ public async Task RunAsync()
7272
}
7373

7474
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
75-
await _stableDiffusionService.UnloadModel(model);
75+
await _stableDiffusionService.UnloadModelAsync(model);
7676
}
7777
}
7878
}

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task RunAsync()
5050
foreach (var model in _stableDiffusionService.Models)
5151
{
5252
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
53-
await _stableDiffusionService.LoadModel(model);
53+
await _stableDiffusionService.LoadModelAsync(model);
5454

5555
foreach (var schedulerType in model.PipelineType.GetSchedulerTypes())
5656
{
@@ -60,7 +60,7 @@ public async Task RunAsync()
6060
}
6161

6262
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
63-
await _stableDiffusionService.UnloadModel(model);
63+
await _stableDiffusionService.UnloadModelAsync(model);
6464
}
6565
}
6666
}

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task RunAsync()
3434
foreach (var model in _stableDiffusionService.Models)
3535
{
3636
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
37-
await _stableDiffusionService.LoadModel(model);
37+
await _stableDiffusionService.LoadModelAsync(model);
3838

3939
foreach (var generationPrompt in _generationPrompts)
4040
{
@@ -56,7 +56,7 @@ public async Task RunAsync()
5656
}
5757

5858
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
59-
await _stableDiffusionService.UnloadModel(model);
59+
await _stableDiffusionService.UnloadModelAsync(model);
6060
}
6161
OutputHelpers.WriteConsole("Complete :)", ConsoleColor.DarkMagenta);
6262
OutputHelpers.ReadConsole(ConsoleColor.Gray);

OnnxStack.Console/OnnxStack.Console.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
13-
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
14-
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.16.1" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
14+
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.16.2" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

OnnxStack.Console/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"InterOpNumThreads": 0,
2727
"IntraOpNumThreads": 0,
2828
"ExecutionMode": "ORT_SEQUENTIAL",
29-
"ExecutionProvider": "Cuda",
29+
"ExecutionProvider": "DirectML",
3030
"ModelConfigurations": [
3131
{
3232
"Type": "Tokenizer",

OnnxStack.Core/Extensions.cs

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using Microsoft.ML.OnnxRuntime.Tensors;
33
using OnnxStack.Core.Config;
44
using System;
5+
using System.Buffers;
56
using System.Collections.Concurrent;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Numerics;
10+
using System.Runtime.InteropServices;
911

1012
namespace OnnxStack.Core
1113
{
@@ -205,26 +207,166 @@ public static T GetBufferLength<T>(this ReadOnlySpan<T> array) where T : INumber
205207
}
206208

207209

210+
/// <summary>
211+
/// Converts to long.
212+
/// </summary>
213+
/// <param name="array">The array.</param>
214+
/// <returns></returns>
208215
public static long[] ToLong(this ReadOnlySpan<int> array)
209216
{
210217
return Array.ConvertAll(array.ToArray(), Convert.ToInt64);
211218
}
212-
219+
220+
221+
/// <summary>
222+
/// Converts the string representation of a number to an integer.
223+
/// </summary>
224+
/// <param name="array">The array.</param>
225+
/// <returns></returns>
213226
public static int[] ToInt(this long[] array)
214227
{
215228
return Array.ConvertAll(array, Convert.ToInt32);
216229
}
217230

231+
232+
/// <summary>
233+
/// Converts to long.
234+
/// </summary>
235+
/// <param name="array">The array.</param>
236+
/// <returns></returns>
218237
public static long[] ToLong(this int[] array)
219238
{
220239
return Array.ConvertAll(array, Convert.ToInt64);
221240
}
222241

223242

224-
public static OrtValue ToOrtValue<T>(this DenseTensor<T> tensor) where T : unmanaged
243+
/// <summary>
244+
/// Creates and OrtValue form the DenseTensor and NodeMetaData provided
245+
/// </summary>
246+
/// <param name="tensor">The tensor.</param>
247+
/// <param name="nodeMetadata">The node metadata.</param>
248+
/// <returns></returns>
249+
public static OrtValue ToOrtValue(this DenseTensor<float> tensor, NodeMetadata nodeMetadata)
225250
{
226-
return OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, tensor.Dimensions.ToLong());
251+
var dimensions = tensor.Dimensions.ToLong();
252+
return nodeMetadata.ElementDataType switch
253+
{
254+
TensorElementType.Float16 => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer.ToFloat16(), dimensions),
255+
TensorElementType.BFloat16 => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer.ToBFloat16(), dimensions),
256+
_ => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, dimensions)
257+
};
227258
}
228259

260+
261+
/// <summary>
262+
/// Creates and allocates output tensors buffer.
263+
/// </summary>
264+
/// <param name="nodeMetadata">The node metadata.</param>
265+
/// <param name="dimensions">The dimensions.</param>
266+
/// <returns></returns>
267+
public static OrtValue CreateOutputBuffer(this NodeMetadata nodeMetadata, ReadOnlySpan<int> dimensions)
268+
{
269+
return OrtValue.CreateAllocatedTensorValue(OrtAllocator.DefaultInstance, nodeMetadata.ElementDataType, dimensions.ToLong());
270+
}
271+
272+
273+
/// <summary>
274+
/// Converts to DenseTensor<float>.
275+
/// </summary>
276+
/// <param name="ortValue">The ort value.</param>
277+
/// <returns></returns>
278+
public static DenseTensor<float> ToDenseTensor(this OrtValue ortValue)
279+
{
280+
var typeInfo = ortValue.GetTensorTypeAndShape();
281+
var dimensions = typeInfo.Shape.ToInt();
282+
return typeInfo.ElementDataType switch
283+
{
284+
TensorElementType.Float16 => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<Float16>().ToFloat(), dimensions),
285+
TensorElementType.BFloat16 => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<BFloat16>().ToFloat(), dimensions),
286+
_ => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<float>().ToArray(), dimensions)
287+
};
288+
}
289+
290+
291+
/// <summary>
292+
/// Converts to array.
293+
/// </summary>
294+
/// <param name="ortValue">The ort value.</param>
295+
/// <returns></returns>
296+
public static float[] ToArray(this OrtValue ortValue)
297+
{
298+
var typeInfo = ortValue.GetTensorTypeAndShape();
299+
var dimensions = typeInfo.Shape.ToInt();
300+
return typeInfo.ElementDataType switch
301+
{
302+
TensorElementType.Float16 => ortValue.GetTensorDataAsSpan<Float16>().ToFloat().ToArray(),
303+
TensorElementType.BFloat16 => ortValue.GetTensorDataAsSpan<BFloat16>().ToFloat().ToArray(),
304+
_ => ortValue.GetTensorDataAsSpan<float>().ToArray()
305+
};
306+
}
307+
308+
309+
/// <summary>
310+
/// Converts to float16.
311+
/// </summary>
312+
/// <param name="inputMemory">The input memory.</param>
313+
/// <returns></returns>
314+
internal static Memory<Float16> ToFloat16(this Memory<float> inputMemory)
315+
{
316+
var elementCount = inputMemory.Length;
317+
var floatArray = new Float16[inputMemory.Length];
318+
for (int i = 0; i < elementCount; i++)
319+
floatArray[i] = (Float16)inputMemory.Span[i];
320+
321+
return floatArray.AsMemory();
322+
}
323+
324+
325+
/// <summary>
326+
/// Converts to BFloat16.
327+
/// </summary>
328+
/// <param name="inputMemory">The input memory.</param>
329+
/// <returns></returns>
330+
internal static Memory<BFloat16> ToBFloat16(this Memory<float> inputMemory)
331+
{
332+
var elementCount = inputMemory.Length;
333+
var floatArray = new BFloat16[inputMemory.Length];
334+
for (int i = 0; i < elementCount; i++)
335+
floatArray[i] = (BFloat16)inputMemory.Span[i];
336+
337+
return floatArray.AsMemory();
338+
}
339+
340+
341+
/// <summary>
342+
/// Converts to float.
343+
/// </summary>
344+
/// <param name="inputMemory">The input memory.</param>
345+
/// <returns></returns>
346+
internal static Memory<float> ToFloat(this ReadOnlySpan<Float16> inputMemory)
347+
{
348+
var elementCount = inputMemory.Length;
349+
var floatArray = new float[elementCount];
350+
for (int i = 0; i < elementCount; i++)
351+
floatArray[i] = (float)inputMemory[i];
352+
353+
return floatArray.AsMemory();
354+
}
355+
356+
357+
/// <summary>
358+
/// Converts to float.
359+
/// </summary>
360+
/// <param name="inputMemory">The input memory.</param>
361+
/// <returns></returns>
362+
internal static Memory<float> ToFloat(this ReadOnlySpan<BFloat16> inputMemory)
363+
{
364+
var elementCount = inputMemory.Length;
365+
var floatArray = new float[elementCount];
366+
for (int i = 0; i < elementCount; i++)
367+
floatArray[i] = (float)inputMemory[i];
368+
369+
return floatArray.AsMemory();
370+
}
229371
}
230372
}

0 commit comments

Comments
 (0)