-
Notifications
You must be signed in to change notification settings - Fork 31
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
Showing
5 changed files
with
175 additions
and
35 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
81 changes: 81 additions & 0 deletions
81
OnnxStack.StableDiffusion/Diffusers/StableCascade/ImageDiffuser.cs
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,81 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.ML.OnnxRuntime.Tensors; | ||
using OnnxStack.Core; | ||
using OnnxStack.Core.Model; | ||
using OnnxStack.StableDiffusion.Common; | ||
using OnnxStack.StableDiffusion.Config; | ||
using OnnxStack.StableDiffusion.Enums; | ||
using OnnxStack.StableDiffusion.Models; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OnnxStack.StableDiffusion.Diffusers.StableCascade | ||
{ | ||
public sealed class ImageDiffuser : StableCascadeDiffuser | ||
{ | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ImageDiffuser"/> class. | ||
/// </summary> | ||
/// <param name="unet">The unet.</param> | ||
/// <param name="vaeDecoder">The vae decoder.</param> | ||
/// <param name="vaeEncoder">The vae encoder.</param> | ||
/// <param name="logger">The logger.</param> | ||
public ImageDiffuser(UNetConditionModel priorUnet, UNetConditionModel decoderUnet, AutoEncoderModel decoderVqgan, AutoEncoderModel imageEncoder, MemoryModeType memoryMode, ILogger logger = default) | ||
: base(priorUnet, decoderUnet, decoderVqgan, imageEncoder, memoryMode, logger) { } | ||
|
||
|
||
/// <summary> | ||
/// Gets the type of the diffuser. | ||
/// </summary> | ||
public override DiffuserType DiffuserType => DiffuserType.ImageToImage; | ||
|
||
|
||
/// <summary> | ||
/// Gets the timesteps. | ||
/// </summary> | ||
/// <param name="options">The options.</param> | ||
/// <param name="scheduler">The scheduler.</param> | ||
/// <returns></returns> | ||
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler) | ||
{ | ||
if (!options.Timesteps.IsNullOrEmpty()) | ||
return options.Timesteps; | ||
|
||
return scheduler.Timesteps; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Encodes the image. | ||
/// </summary> | ||
/// <param name="prompt">The prompt.</param> | ||
/// <param name="performGuidance">if set to <c>true</c> [perform guidance].</param> | ||
/// <returns></returns> | ||
protected override async Task<DenseTensor<float>> EncodeImageAsync(PromptOptions prompt, bool performGuidance) | ||
{ | ||
var metadata = await _vaeEncoder.GetMetadataAsync(); | ||
var imageTensor = prompt.InputImage.GetClipImageFeatureTensor(); | ||
using (var inferenceParameters = new OnnxInferenceParameters(metadata)) | ||
{ | ||
inferenceParameters.AddInputTensor(imageTensor); | ||
inferenceParameters.AddOutputBuffer(new[] { 1, ClipImageChannels }); | ||
|
||
var results = await _vaeEncoder.RunInferenceAsync(inferenceParameters); | ||
using (var result = results.First()) | ||
{ | ||
// Unload if required | ||
if (_memoryMode == MemoryModeType.Minimum) | ||
await _vaeEncoder.UnloadAsync(); | ||
|
||
var image_embeds = result.ToDenseTensor(new[] { 1, 1, ClipImageChannels }); | ||
if (performGuidance) | ||
return new DenseTensor<float>(image_embeds.Dimensions).Concatenate(image_embeds); | ||
|
||
return image_embeds; | ||
} | ||
} | ||
} | ||
} | ||
} |
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