Skip to content

Commit 55ef7be

Browse files
committed
latent proj bias
1 parent d465a70 commit 55ef7be

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

latent-preview.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/latent_formats.py#L152-L169
32
const float flux_latent_rgb_proj[16][3] = {
43
{-0.0346f, 0.0244f, 0.0681f},
@@ -17,6 +16,7 @@ const float flux_latent_rgb_proj[16][3] = {
1716
{-0.1264f, -0.0522f, -0.1103f},
1817
{-0.0280f, -0.0881f, -0.0499f},
1918
{-0.1262f, -0.0982f, -0.0778f}};
19+
float flux_latent_rgb_bias[3] = {-0.0329, -0.0718, -0.0851};
2020

2121
// https://github.com/Stability-AI/sd3.5/blob/main/sd3_impls.py#L228-L246
2222
const float sd3_latent_rgb_proj[16][3] = {
@@ -37,22 +37,25 @@ const float sd3_latent_rgb_proj[16][3] = {
3737
{-0.0749f, -0.0634f, -0.0456f},
3838
{-0.1418f, -0.1457f, -0.1259f},
3939
};
40+
float sd3_latent_rgb_bias[3] = {0, 0, 0};
4041

4142
// https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/latent_formats.py#L32-L38
4243
const float sdxl_latent_rgb_proj[4][3] = {
4344
{0.3651f, 0.4232f, 0.4341f},
4445
{-0.2533f, -0.0042f, 0.1068f},
4546
{0.1076f, 0.1111f, -0.0362f},
4647
{-0.3165f, -0.2492f, -0.2188f}};
48+
float sdxl_latent_rgb_bias[3] = {0.1084, -0.0175, -0.0011};
4749

4850
// https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/latent_formats.py#L32-L38
4951
const float sd_latent_rgb_proj[4][3]{
5052
{0.3512f, 0.2297f, 0.3227f},
5153
{0.3250f, 0.4974f, 0.2350f},
5254
{-0.2829f, 0.1762f, 0.2721f},
5355
{-0.2120f, -0.2616f, -0.7177f}};
56+
float sd_latent_rgb_bias[3] = {0,0,0};
5457

55-
void preview_latent_image(uint8_t* buffer, struct ggml_tensor* latents, const float (*latent_rgb_proj)[3], int width, int height, int dim) {
58+
void preview_latent_image(uint8_t* buffer, struct ggml_tensor* latents, const float (*latent_rgb_proj)[3], const float latent_rgb_bias[3], int width, int height, int dim) {
5659
size_t buffer_head = 0;
5760
for (int j = 0; j < height; j++) {
5861
for (int i = 0; i < width; i++) {
@@ -64,6 +67,10 @@ void preview_latent_image(uint8_t* buffer, struct ggml_tensor* latents, const fl
6467
g += value * latent_rgb_proj[d][1];
6568
b += value * latent_rgb_proj[d][2];
6669
}
70+
// bias
71+
r += latent_rgb_bias[0];
72+
g += latent_rgb_bias[1];
73+
b += latent_rgb_bias[2];
6774

6875
// change range
6976
r = r * .5f + .5f;

stable-diffusion.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,14 +883,17 @@ class StableDiffusionGGML {
883883
uint32_t dim = latents->ne[2];
884884
if (preview_mode == PREVIEW_PROJ) {
885885
const float (*latent_rgb_proj)[channel];
886+
float *latent_rgb_bias;
886887

887888
if (dim == 16) {
888889
// 16 channels VAE -> Flux or SD3
889890

890891
if (sd_version_is_sd3(version)) {
891892
latent_rgb_proj = sd3_latent_rgb_proj;
893+
latent_rgb_bias = sd3_latent_rgb_bias;
892894
} else if (sd_version_is_flux(version)) {
893895
latent_rgb_proj = flux_latent_rgb_proj;
896+
latent_rgb_bias = flux_latent_rgb_bias;
894897
} else {
895898
LOG_WARN("No latent to RGB projection known for this model");
896899
// unknown model
@@ -901,8 +904,10 @@ class StableDiffusionGGML {
901904
// 4 channels VAE
902905
if (sd_version_is_sdxl(version)) {
903906
latent_rgb_proj = sdxl_latent_rgb_proj;
907+
latent_rgb_bias = sdxl_latent_rgb_bias;
904908
} else if (sd_version_is_sd1(version) || sd_version_is_sd2(version)) {
905909
latent_rgb_proj = sd_latent_rgb_proj;
910+
latent_rgb_bias = sd_latent_rgb_bias;
906911
} else {
907912
// unknown model
908913
LOG_WARN("No latent to RGB projection known for this model");
@@ -915,7 +920,7 @@ class StableDiffusionGGML {
915920
}
916921
uint8_t* data = (uint8_t*)malloc(width * height * channel * sizeof(uint8_t));
917922

918-
preview_latent_image(data, latents, latent_rgb_proj, width, height, dim);
923+
preview_latent_image(data, latents, latent_rgb_proj,latent_rgb_bias, width, height, dim);
919924
sd_image_t image = {
920925
width,
921926
height,

0 commit comments

Comments
 (0)