-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTransformerModel.cs
More file actions
173 lines (143 loc) · 5.65 KB
/
TransformerModel.cs
File metadata and controls
173 lines (143 loc) · 5.65 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.Threading;
using System.Threading.Tasks;
using TensorStack.Common;
using TensorStack.StableDiffusion.Config;
using TensorStack.StableDiffusion.Enums;
namespace TensorStack.StableDiffusion.Models
{
/// <summary>
/// TransformerModel: Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
/// </summary>
public abstract class TransformerModel : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="TransformerModel"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public TransformerModel(TransformerModelConfig configuration)
{
Configuration = configuration;
Transformer = new ModelSession<TransformerModelConfig>(configuration);
if (!string.IsNullOrEmpty(configuration.ControlNetPath))
TransformerControlNet = new ModelSession<TransformerModelConfig>(configuration with { Path = configuration.ControlNetPath });
}
/// <summary>
/// Gets the configuration.
/// </summary>
public TransformerModelConfig Configuration { get; }//temp
/// <summary>
/// Gets the Transformer.
/// </summary>
protected ModelSession<TransformerModelConfig> Transformer { get; }
/// <summary>
/// Gets the Transformer ControlNet.
/// </summary>
protected ModelSession<TransformerModelConfig> TransformerControlNet { get; }
/// <summary>
/// Gets the type of the model.
/// </summary>
public ModelType ModelType => Configuration.ModelType;
/// <summary>
/// Gets the in channels.
/// </summary>
public int InChannels => Configuration.InChannels;
/// <summary>
/// Gets the out channels.
/// </summary>
public int OutChannels => Configuration.OutChannels;
/// <summary>
/// Gets the joint attention.
/// </summary>
public int JointAttention => Configuration.JointAttention;
/// <summary>
/// Gets the pooled projection.
/// </summary>
public int PooledProjection => Configuration.PooledProjection;
/// <summary>
/// Gets the caption projection.
/// </summary>
public int CaptionProjection => Configuration.CaptionProjection;
/// <summary>
/// Gets a value indicating whether this instance has TransformerControlNet.
/// </summary>
public bool HasControlNet => TransformerControlNet != null;
/// <summary>
/// Determines whether Transformer is loaded.
/// </summary>
public bool IsLoaded()
{
return Transformer.IsLoaded();
}
/// <summary>
/// Determines whether TransformerControlNet is loaded.
/// </summary>
public bool IsControlNetLoaded()
{
return TransformerControlNet.IsLoaded();
}
/// <summary>
/// Load Transformer model
/// </summary>
/// <param name="onnxOptimizations">The onnx optimizations.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<ModelMetadata> LoadAsync(ModelOptimization optimizations = null, CancellationToken cancellationToken = default)
{
await UnloadControlNetAsync();
return await Transformer.LoadAsync(optimizations, cancellationToken);
}
/// <summary>
/// Load TransformerControlNet model
/// </summary>
/// <param name="onnxOptimizations">The onnx optimizations.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<ModelMetadata> LoadControlNetAsync(ModelOptimization optimizations = null, CancellationToken cancellationToken = default)
{
await UnloadAsync();
return await TransformerControlNet.LoadAsync(optimizations, cancellationToken);
}
/// <summary>
/// Unload Transformer model
/// </summary>
public async Task UnloadAsync()
{
if (Transformer.IsLoaded())
await Transformer.UnloadAsync();
}
/// <summary>
/// Unload TransformerControlNet model
/// </summary>
public async Task UnloadControlNetAsync()
{
if (TransformerControlNet.IsLoaded())
await TransformerControlNet.UnloadAsync();
}
/// <summary>
/// Determines whether Transformer optimizations have changed
/// </summary>
/// <param name="optimizations">The optimizations.</param>
public bool HasOptimizationsChanged(ModelOptimization optimizations)
{
return Transformer.HasOptimizationsChanged(optimizations);
}
/// <summary>
/// Determines whether TransformerControlNet optimizations have changed
/// </summary>
/// <param name="optimizations">The optimizations.</param>
public bool HasControlNetOptimizationsChanged(ModelOptimization optimizations)
{
return TransformerControlNet.HasOptimizationsChanged(optimizations);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Transformer?.Dispose();
TransformerControlNet?.Dispose();
GC.SuppressFinalize(this);
}
}
}