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

Commit 049bb31

Browse files
author
James Tayler
committed
add integration tests for loading model and text to image
1 parent 4297565 commit 049bb31

File tree

9 files changed

+232
-1
lines changed

9 files changed

+232
-1
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
LICENSE
25+
README.md

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
COPY . .
12+
RUN ls -ltr
13+
RUN dotnet build OnnxStackCore.sln
14+
RUN dotnet test OnnxStackCore.sln

OnnxStack.Core/OnnxStack.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
4040
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
4141
<PackageReference Include="Microsoft.ML" Version="2.0.1" />
42+
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.2" />
4243
<PackageReference Include="Microsoft.ML.OnnxRuntime.Extensions" Version="0.9.0" />
43-
<PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.16.1" />
44+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.16.2" />
4445
</ItemGroup>
4546

4647
<ItemGroup>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
17+
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.2" />
18+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Extensions" Version="0.9.0" />
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
20+
<PackageReference Include="xunit" Version="2.4.2"/>
21+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
<PrivateAssets>all</PrivateAssets>
24+
</PackageReference>
25+
<PackageReference Include="coverlet.collector" Version="3.1.2">
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
<PrivateAssets>all</PrivateAssets>
28+
</PackageReference>
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<ProjectReference Include="..\OnnxStack.Core\OnnxStack.Core.csproj" />
33+
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<None Update="appsettings.json">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</None>
40+
</ItemGroup>
41+
42+
</Project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using OnnxStack.Core;
4+
using OnnxStack.StableDiffusion.Common;
5+
using OnnxStack.StableDiffusion.Config;
6+
using OnnxStack.StableDiffusion.Enums;
7+
using Xunit.Abstractions;
8+
9+
namespace OnnxStack.IntegrationTests;
10+
11+
public class StableDiffusionTests
12+
{
13+
private readonly ITestOutputHelper _testOutputHelper;
14+
private readonly IStableDiffusionService _stableDiffusion;
15+
16+
public StableDiffusionTests(ITestOutputHelper testOutputHelper)
17+
{
18+
_testOutputHelper = testOutputHelper;
19+
20+
var services = new ServiceCollection();
21+
services.AddLogging();
22+
services.AddOnnxStack();
23+
services.AddOnnxStackStableDiffusion();
24+
var provider = services.BuildServiceProvider();
25+
_stableDiffusion = provider.GetRequiredService<IStableDiffusionService>();
26+
}
27+
28+
[Fact]
29+
public async Task GivenStableDiffusion15_WhenLoadModel_ThenModelIsLoaded()
30+
{
31+
//arrange
32+
var model = _stableDiffusion.Models.Single(m => m.Name == "StableDiffusion 1.5");
33+
34+
//act
35+
var isModelLoaded = await _stableDiffusion.LoadModel(model);
36+
37+
//assert
38+
isModelLoaded.Should().BeTrue();
39+
}
40+
41+
[Fact]
42+
public async Task GivenTextToImage_WhenInference_ThenImageGenerated()
43+
{
44+
//arrange
45+
var model = _stableDiffusion.Models.Single(m => m.Name == "StableDiffusion 1.5");
46+
await _stableDiffusion.LoadModel(model);
47+
48+
var prompt = new PromptOptions
49+
{
50+
Prompt = "an astronaut riding a horse in space",
51+
NegativePrompt = "blurry,ugly,cartoon",
52+
BatchCount = 1,
53+
SchedulerType = SchedulerType.EulerAncestral,
54+
DiffuserType = DiffuserType.TextToImage
55+
};
56+
57+
var scheduler = new SchedulerOptions
58+
{
59+
Width = 512,
60+
Height = 512,
61+
InferenceSteps = 10,
62+
GuidanceScale = 7.0f,
63+
Seed = -1
64+
};
65+
66+
var steps = 0;
67+
68+
//act
69+
var image = await _stableDiffusion.GenerateAsImageAsync(model, prompt, scheduler, (currentStep, totalSteps) =>
70+
{
71+
_testOutputHelper.WriteLine($"Step {currentStep}/{totalSteps}");
72+
steps++;
73+
});
74+
75+
//assert
76+
steps.Should().Be(10);
77+
image.Should().NotBeNull();
78+
image.Size.IsEmpty.Should().BeFalse();
79+
image.Width.Should().Be(512);
80+
image.Height.Should().Be(512);
81+
}
82+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
"OnnxStackConfig": {
8+
"OnnxModelSets": [
9+
{
10+
"Name": "StableDiffusion 1.5",
11+
"IsEnabled": true,
12+
"PadTokenId": 49407,
13+
"BlankTokenId": 49407,
14+
"TokenizerLimit": 77,
15+
"EmbeddingsLength": 768,
16+
"ScaleFactor": 0.18215,
17+
"PipelineType": "StableDiffusion",
18+
"Diffusers": [
19+
"TextToImage",
20+
"ImageToImage",
21+
"ImageInpaintLegacy"
22+
],
23+
"DeviceId": 0,
24+
"InterOpNumThreads": 0,
25+
"IntraOpNumThreads": 0,
26+
"ExecutionMode": "ORT_SEQUENTIAL",
27+
"ExecutionProvider": "Cpu",
28+
"ModelConfigurations": [
29+
{
30+
"Type": "Tokenizer",
31+
"OnnxModelPath": "/app/OnnxStack.StableDiffusion/cliptokenizer.onnx"
32+
},
33+
{
34+
"Type": "Unet",
35+
"OnnxModelPath": "/app/stable-diffusion-v1-5/unet/model.onnx"
36+
},
37+
{
38+
"Type": "TextEncoder",
39+
"OnnxModelPath": "/app/stable-diffusion-v1-5/text_encoder/model.onnx"
40+
},
41+
{
42+
"Type": "VaeEncoder",
43+
"OnnxModelPath": "/app/stable-diffusion-v1-5/vae_encoder/model.onnx"
44+
},
45+
{
46+
"Type": "VaeDecoder",
47+
"OnnxModelPath": "/app/stable-diffusion-v1-5/vae_decoder/model.onnx"
48+
}
49+
]
50+
}
51+
]
52+
}
53+
}

OnnxStackCore.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxStack.Core", "OnnxStack
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxStack.StableDiffusion", "OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj", "{93F03618-9D5D-40E3-A565-729D72B164BB}"
66
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxStack.IntegrationTests", "OnnxStack.IntegrationTests\OnnxStack.IntegrationTests.csproj", "{9336EF88-5290-451F-972C-CF9550EF545D}"
8+
EndProject
79
Global
810
GlobalSection(SolutionConfigurationPlatforms) = preSolution
911
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
1820
{93F03618-9D5D-40E3-A565-729D72B164BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
1921
{93F03618-9D5D-40E3-A565-729D72B164BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
2022
{93F03618-9D5D-40E3-A565-729D72B164BB}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{9336EF88-5290-451F-972C-CF9550EF545D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{9336EF88-5290-451F-972C-CF9550EF545D}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{9336EF88-5290-451F-972C-CF9550EF545D}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{9336EF88-5290-451F-972C-CF9550EF545D}.Release|Any CPU.Build.0 = Release|Any CPU
2127
EndGlobalSection
2228
EndGlobal

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3.7'
2+
3+
services:
4+
app:
5+
build: .
6+
volumes:
7+
- ./output:/app/images

0 commit comments

Comments
 (0)