|
|
|
|
@@ -14,9 +14,9 @@
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "ggml/ggml.h"
|
|
|
|
|
#include "stable-diffusion.h"
|
|
|
|
|
#include "rng.h"
|
|
|
|
|
#include "rng_philox.h"
|
|
|
|
|
#include "stable-diffusion.h"
|
|
|
|
|
|
|
|
|
|
static SDLogLevel log_level = SDLogLevel::INFO;
|
|
|
|
|
|
|
|
|
|
@@ -3029,6 +3029,9 @@ class StableDiffusionGGML {
|
|
|
|
|
}
|
|
|
|
|
bool some_tensor_not_init = false;
|
|
|
|
|
for (auto pair : tensors) {
|
|
|
|
|
if (pair.first.find("cond_stage_model.transformer.text_model.encoder.layers.23") != std::string::npos) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (tensor_names_in_file.find(pair.first) == tensor_names_in_file.end()) {
|
|
|
|
|
LOG_ERROR("tensor '%s' not in model file", pair.first.c_str());
|
|
|
|
|
some_tensor_not_init = true;
|
|
|
|
|
@@ -3096,7 +3099,7 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* c = ggml_new_tensor_4d(res_ctx, GGML_TYPE_F32, 1024, 2, 1, 1);
|
|
|
|
|
ggml_set_f32(c, 0.5);
|
|
|
|
|
|
|
|
|
|
size_t ctx_size = 1 * 1024 * 1024; // 1MB
|
|
|
|
|
size_t ctx_size = 10 * 1024 * 1024; // 10MB
|
|
|
|
|
// calculate the amount of memory required
|
|
|
|
|
{
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
@@ -3119,8 +3122,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* out = diffusion_model.forward(ctx, x_t, NULL, c, t_emb);
|
|
|
|
|
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ctx_size += cplan.work_size;
|
|
|
|
|
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
|
|
|
|
|
@@ -3152,8 +3155,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* out = diffusion_model.forward(ctx, x_t, NULL, c, t_emb);
|
|
|
|
|
ggml_hold_dynamic_tensor(out);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ggml_set_dynamic(ctx, false);
|
|
|
|
|
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
|
|
|
|
|
@@ -3162,7 +3165,7 @@ class StableDiffusionGGML {
|
|
|
|
|
cplan.work_data = (uint8_t*)buf->data;
|
|
|
|
|
|
|
|
|
|
int64_t t0 = ggml_time_ms();
|
|
|
|
|
ggml_graph_compute(&diffusion_graph, &cplan);
|
|
|
|
|
ggml_graph_compute(diffusion_graph, &cplan);
|
|
|
|
|
|
|
|
|
|
double result = 0.f;
|
|
|
|
|
|
|
|
|
|
@@ -3198,7 +3201,7 @@ class StableDiffusionGGML {
|
|
|
|
|
true);
|
|
|
|
|
std::vector<int>& tokens = tokens_and_weights.first;
|
|
|
|
|
std::vector<float>& weights = tokens_and_weights.second;
|
|
|
|
|
size_t ctx_size = 1 * 1024 * 1024; // 1MB
|
|
|
|
|
size_t ctx_size = 10 * 1024 * 1024; // 10MB
|
|
|
|
|
// calculate the amount of memory required
|
|
|
|
|
{
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
@@ -3248,14 +3251,14 @@ class StableDiffusionGGML {
|
|
|
|
|
ggml_set_dynamic(ctx, params.dynamic);
|
|
|
|
|
|
|
|
|
|
struct ggml_tensor* hidden_states = cond_stage_model.text_model.forward(ctx, input_ids);
|
|
|
|
|
struct ggml_cgraph cond_graph = ggml_build_forward(hidden_states);
|
|
|
|
|
struct ggml_cgraph* cond_graph = ggml_build_forward_ctx(ctx, hidden_states);
|
|
|
|
|
LOG_DEBUG("building condition graph completed: %d nodes, %d leafs",
|
|
|
|
|
cond_graph.n_nodes, cond_graph.n_leafs);
|
|
|
|
|
cond_graph->n_nodes, cond_graph->n_leafs);
|
|
|
|
|
|
|
|
|
|
memcpy(input_ids->data, tokens.data(), tokens.size() * ggml_element_size(input_ids));
|
|
|
|
|
|
|
|
|
|
int64_t t0 = ggml_time_ms();
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, &cond_graph, n_threads);
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, cond_graph, n_threads);
|
|
|
|
|
int64_t t1 = ggml_time_ms();
|
|
|
|
|
LOG_DEBUG("computing condition graph completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
|
|
|
|
|
|
|
|
|
|
@@ -3332,7 +3335,7 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* x = ggml_dup_tensor(res_ctx, x_t);
|
|
|
|
|
copy_ggml_tensor(x, x_t);
|
|
|
|
|
|
|
|
|
|
size_t ctx_size = 1 * 1024 * 1024; // 1MB
|
|
|
|
|
size_t ctx_size = 10 * 1024 * 1024; // 10MB
|
|
|
|
|
// calculate the amount of memory required
|
|
|
|
|
{
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
@@ -3357,8 +3360,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* out = diffusion_model.forward(ctx, noised_input, NULL, context, t_emb);
|
|
|
|
|
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ctx_size += cplan.work_size;
|
|
|
|
|
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
|
|
|
|
|
@@ -3390,8 +3393,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* out = diffusion_model.forward(ctx, noised_input, NULL, context, t_emb);
|
|
|
|
|
ggml_hold_dynamic_tensor(out);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ggml_set_dynamic(ctx, false);
|
|
|
|
|
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
|
|
|
|
|
@@ -3449,12 +3452,12 @@ class StableDiffusionGGML {
|
|
|
|
|
if (cfg_scale != 1.0 && uc != NULL) {
|
|
|
|
|
// uncond
|
|
|
|
|
copy_ggml_tensor(context, uc);
|
|
|
|
|
ggml_graph_compute(&diffusion_graph, &cplan);
|
|
|
|
|
ggml_graph_compute(diffusion_graph, &cplan);
|
|
|
|
|
copy_ggml_tensor(out_uncond, out);
|
|
|
|
|
|
|
|
|
|
// cond
|
|
|
|
|
copy_ggml_tensor(context, c);
|
|
|
|
|
ggml_graph_compute(&diffusion_graph, &cplan);
|
|
|
|
|
ggml_graph_compute(diffusion_graph, &cplan);
|
|
|
|
|
|
|
|
|
|
out_cond = out;
|
|
|
|
|
|
|
|
|
|
@@ -3471,7 +3474,7 @@ class StableDiffusionGGML {
|
|
|
|
|
} else {
|
|
|
|
|
// cond
|
|
|
|
|
copy_ggml_tensor(context, c);
|
|
|
|
|
ggml_graph_compute(&diffusion_graph, &cplan);
|
|
|
|
|
ggml_graph_compute(diffusion_graph, &cplan);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v = out, eps = out
|
|
|
|
|
@@ -3587,7 +3590,7 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* result = NULL;
|
|
|
|
|
|
|
|
|
|
// calculate the amount of memory required
|
|
|
|
|
size_t ctx_size = 1 * 1024 * 1024;
|
|
|
|
|
size_t ctx_size = 10 * 1024 * 1024; // 10MB
|
|
|
|
|
{
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
params.mem_size = ctx_size;
|
|
|
|
|
@@ -3604,8 +3607,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* moments = first_stage_model.encode(ctx, x);
|
|
|
|
|
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph vae_graph = ggml_build_forward(moments);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&vae_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, moments);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ctx_size += cplan.work_size;
|
|
|
|
|
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",
|
|
|
|
|
@@ -3629,10 +3632,10 @@ class StableDiffusionGGML {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ggml_tensor* moments = first_stage_model.encode(ctx, x);
|
|
|
|
|
struct ggml_cgraph vae_graph = ggml_build_forward(moments);
|
|
|
|
|
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, moments);
|
|
|
|
|
|
|
|
|
|
int64_t t0 = ggml_time_ms();
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, &vae_graph, n_threads);
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, vae_graph, n_threads);
|
|
|
|
|
int64_t t1 = ggml_time_ms();
|
|
|
|
|
|
|
|
|
|
#ifdef GGML_PERF
|
|
|
|
|
@@ -3716,7 +3719,7 @@ class StableDiffusionGGML {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calculate the amount of memory required
|
|
|
|
|
size_t ctx_size = 1 * 1024 * 1024;
|
|
|
|
|
size_t ctx_size = 10 * 1024 * 1024; // 10MB
|
|
|
|
|
{
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
params.mem_size = ctx_size;
|
|
|
|
|
@@ -3733,8 +3736,8 @@ class StableDiffusionGGML {
|
|
|
|
|
struct ggml_tensor* img = first_stage_model.decoder.forward(ctx, z);
|
|
|
|
|
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
|
|
|
|
|
|
|
|
|
|
struct ggml_cgraph vae_graph = ggml_build_forward(img);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(&vae_graph, n_threads);
|
|
|
|
|
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, img);
|
|
|
|
|
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
|
|
|
|
|
|
|
|
|
|
ctx_size += cplan.work_size;
|
|
|
|
|
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",
|
|
|
|
|
@@ -3758,10 +3761,10 @@ class StableDiffusionGGML {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ggml_tensor* img = first_stage_model.decode(ctx, z);
|
|
|
|
|
struct ggml_cgraph vae_graph = ggml_build_forward(img);
|
|
|
|
|
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, img);
|
|
|
|
|
|
|
|
|
|
int64_t t0 = ggml_time_ms();
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, &vae_graph, n_threads);
|
|
|
|
|
ggml_graph_compute_with_ctx(ctx, vae_graph, n_threads);
|
|
|
|
|
int64_t t1 = ggml_time_ms();
|
|
|
|
|
|
|
|
|
|
#ifdef GGML_PERF
|
|
|
|
|
@@ -3823,7 +3826,7 @@ std::vector<uint8_t> StableDiffusion::txt2img(const std::string& prompt,
|
|
|
|
|
int height,
|
|
|
|
|
SampleMethod sample_method,
|
|
|
|
|
int sample_steps,
|
|
|
|
|
int seed) {
|
|
|
|
|
int64_t seed) {
|
|
|
|
|
std::vector<uint8_t> result;
|
|
|
|
|
struct ggml_init_params params;
|
|
|
|
|
params.mem_size = static_cast<size_t>(10 * 1024) * 1024; // 10M
|
|
|
|
|
@@ -3911,7 +3914,7 @@ std::vector<uint8_t> StableDiffusion::img2img(const std::vector<uint8_t>& init_i
|
|
|
|
|
SampleMethod sample_method,
|
|
|
|
|
int sample_steps,
|
|
|
|
|
float strength,
|
|
|
|
|
int seed) {
|
|
|
|
|
int64_t seed) {
|
|
|
|
|
std::vector<uint8_t> result;
|
|
|
|
|
if (init_img_vec.size() != width * height * 3) {
|
|
|
|
|
return result;
|
|
|
|
|
|