Compare commits

..

5 Commits

Author SHA1 Message Date
leejet
b247581782 fix: insufficient memory error on macOS 2023-09-04 03:50:42 +08:00
leejet
bb3f19cb40 fix: increase ctx_size 2023-09-04 03:45:43 +08:00
leejet
7620b920c8 use new graph api to avoid stack overflow on msvc 2023-09-03 22:56:33 +08:00
leejet
3ffffa6929 fix: do not check weights of open clip last layer 2023-09-03 21:10:08 +08:00
leejet
45842865ff fix: seed should be 64 bit 2023-09-03 20:08:22 +08:00
5 changed files with 42 additions and 39 deletions

View File

@@ -87,7 +87,7 @@ struct Option {
int sample_steps = 20;
float strength = 0.75f;
RNGType rng_type = STD_DEFAULT_RNG;
int seed = 42;
int64_t seed = 42;
bool verbose = false;
void print() {
@@ -106,7 +106,7 @@ struct Option {
printf(" sample_steps: %d\n", sample_steps);
printf(" strength: %.2f\n", strength);
printf(" rng: %s\n", rng_type_to_str[rng_type]);
printf(" seed: %d\n", seed);
printf(" seed: %ld\n", seed);
}
};
@@ -233,7 +233,7 @@ void parse_args(int argc, const char* argv[], Option* opt) {
invalid_arg = true;
break;
}
opt->seed = std::stoi(argv[i]);
opt->seed = std::stoll(argv[i]);
} else if (arg == "-h" || arg == "--help") {
print_usage(argc, argv);
exit(0);

4
rng.h
View File

@@ -6,7 +6,7 @@
class RNG {
public:
virtual void manual_seed(uint32_t seed) = 0;
virtual void manual_seed(uint64_t seed) = 0;
virtual std::vector<float> randn(uint32_t n) = 0;
};
@@ -15,7 +15,7 @@ class STDDefaultRNG : public RNG {
std::default_random_engine generator;
public:
void manual_seed(uint32_t seed) {
void manual_seed(uint64_t seed) {
generator.seed(seed);
}

View File

@@ -93,7 +93,7 @@ class PhiloxRNG : public RNG {
this->offset = 0;
}
void manual_seed(uint32_t seed) {
void manual_seed(uint64_t seed) {
this->seed = seed;
this->offset = 0;
}

View File

@@ -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;

View File

@@ -40,7 +40,7 @@ class StableDiffusion {
int height,
SampleMethod sample_method,
int sample_steps,
int seed);
int64_t seed);
std::vector<uint8_t> img2img(
const std::vector<uint8_t>& init_img,
const std::string& prompt,
@@ -51,7 +51,7 @@ class StableDiffusion {
SampleMethod sample_method,
int sample_steps,
float strength,
int seed);
int64_t seed);
};
void set_sd_log_level(SDLogLevel level);