-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStableDiffustionInferer.h
More file actions
81 lines (66 loc) · 2.42 KB
/
Copy pathStableDiffustionInferer.h
File metadata and controls
81 lines (66 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include "TextEncoder.h"
#include "OnnxEnvironment.h"
#include "Schedulers/StableDiffusionScheduler.h"
#include "Threading/AsyncOperation.h"
namespace Axodox::MachineLearning
{
typedef std::vector<std::shared_ptr<EncodedText>> ScheduledTensor;
struct TextEmbedding
{
std::variant<EncodedText, ScheduledTensor> Tensor;
std::vector<float> Weights;
};
struct AXODOX_MACHINELEARNING_API StableDiffusionOptions
{
size_t StepCount = 15;
size_t BatchSize = 1;
size_t Width = 512;
size_t Height = 512;
float GuidanceScale = 7.f;
uint32_t Seed = 0;
TextEmbedding TextEmbeddings;
Tensor LatentInput;
Tensor MaskInput;
float DenoisingStrength = 1.f;
StableDiffusionSchedulerKind Scheduler = StableDiffusionSchedulerKind::EulerAncestral;
StableDiffusionSchedulerPredictionType PredictionType = StableDiffusionSchedulerPredictionType::Epsilon;
void Validate() const;
};
struct StableDiffusionContext
{
const StableDiffusionOptions* Options;
std::unique_ptr<StableDiffusionScheduler> Scheduler;
std::vector<std::minstd_rand> Randoms;
};
enum class ImageDiffusionInfererKind
{
StableDiffusion,
ControlNet
};
class AXODOX_MACHINELEARNING_API ImageDiffusionInferer
{
public:
virtual ~ImageDiffusionInferer() = default;
virtual ImageDiffusionInfererKind Type() const = 0;
};
class AXODOX_MACHINELEARNING_API StableDiffusionInferer : public ImageDiffusionInferer
{
static inline const Infrastructure::logger _logger{ "StableDiffusionInferer" };
public:
StableDiffusionInferer(OnnxEnvironment& environment, std::optional<ModelSource> source = {});
Tensor RunInference(const StableDiffusionOptions& options, Threading::async_operation_source* async = nullptr);
static Tensor GenerateLatentSample(StableDiffusionContext& context);
static Tensor PrepareLatentSample(StableDiffusionContext& context, const Tensor& latents, float initialSigma, float vaeScalingFactor = 0.18215f);
static Tensor BlendLatentSamples(const Tensor& a, const Tensor& b, const Tensor& weights);
virtual ImageDiffusionInfererKind Type() const override;
private:
OnnxEnvironment& _environment;
Ort::Session _session;
bool _hasTextEmbeds;
bool _hasTimeIds;
bool _isUsingFloat16;
float _vaeScalingFactor;
Tensor GetTimeIds() const;
};
}