Skip to content

Commit 676b186

Browse files
committed
update stable-diffusion.cpp to master-0ebe6fe
1 parent 93ab734 commit 676b186

File tree

13 files changed

+521
-823
lines changed

13 files changed

+521
-823
lines changed

otherarch/sdcpp/ggml_extend.hpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,10 @@ __STATIC_INLINE__ float sd_image_get_f32(sd_image_t image, int iw, int ih, int i
194194
return value;
195195
}
196196

197-
#if 0 // kcpp
198-
static struct ggml_tensor* get_tensor_from_graph(struct ggml_cgraph* gf, const char* name) {
199-
struct ggml_tensor* res = NULL;
200-
for (int i = 0; i < ggml_graph_n_nodes(gf); i++) {
201-
struct ggml_tensor* node = ggml_graph_node(gf, i);
202-
// printf("%d, %s \n", i, ggml_get_name(node));
203-
if (strcmp(ggml_get_name(node), name) == 0) {
204-
res = node;
205-
break;
206-
}
207-
}
208-
return res;
197+
__STATIC_INLINE__ float sd_image_get_f32(sd_image_f32_t image, int iw, int ih, int ic) {
198+
float value = *(image.data + ih * image.width * image.channel + iw * image.channel + ic);
199+
return value;
209200
}
210-
#endif
211201

212202
__STATIC_INLINE__ void print_ggml_tensor(struct ggml_tensor* tensor, bool shape_only = false, const char* mark = "") {
213203
printf("%s (%s): shape(%zu, %zu, %zu, %zu)\n", mark, ggml_type_name(tensor->type), tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
@@ -462,28 +452,6 @@ __STATIC_INLINE__ void sd_apply_mask(struct ggml_tensor* image_data,
462452
}
463453
}
464454

465-
__STATIC_INLINE__ void sd_mul_images_to_tensor(const uint8_t* image_data,
466-
struct ggml_tensor* output,
467-
int idx,
468-
float* mean = NULL,
469-
float* std = NULL) {
470-
int64_t width = output->ne[0];
471-
int64_t height = output->ne[1];
472-
int64_t channels = output->ne[2];
473-
GGML_ASSERT(channels == 3 && output->type == GGML_TYPE_F32);
474-
for (int iy = 0; iy < height; iy++) {
475-
for (int ix = 0; ix < width; ix++) {
476-
for (int k = 0; k < channels; k++) {
477-
int value = *(image_data + iy * width * channels + ix * channels + k);
478-
float pixel_val = value / 255.0f;
479-
if (mean != NULL && std != NULL)
480-
pixel_val = (pixel_val - mean[k]) / std[k];
481-
ggml_tensor_set_f32(output, pixel_val, ix, iy, k, idx);
482-
}
483-
}
484-
}
485-
}
486-
487455
__STATIC_INLINE__ void sd_image_f32_to_tensor(const float* image_data,
488456
struct ggml_tensor* output,
489457
bool scale = true) {
@@ -786,9 +754,11 @@ __STATIC_INLINE__ std::vector<struct ggml_tensor*> ggml_chunk(struct ggml_contex
786754

787755
typedef std::function<void(ggml_tensor*, ggml_tensor*, bool)> on_tile_process;
788756

789-
__STATIC_INLINE__ void
790-
sd_tiling_calc_tiles(int &num_tiles_dim, float& tile_overlap_factor_dim, int small_dim, int tile_size, const float tile_overlap_factor) {
791-
757+
__STATIC_INLINE__ void sd_tiling_calc_tiles(int& num_tiles_dim,
758+
float& tile_overlap_factor_dim,
759+
int small_dim,
760+
int tile_size,
761+
const float tile_overlap_factor) {
792762
int tile_overlap = (tile_size * tile_overlap_factor);
793763
int non_tile_overlap = tile_size - tile_overlap;
794764

otherarch/sdcpp/lora.hpp

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __LORA_HPP__
22
#define __LORA_HPP__
33

4+
#include <mutex>
45
#include "ggml_extend.hpp"
56

67
#define LORA_GRAPH_BASE_SIZE 10240
@@ -115,49 +116,61 @@ struct LoraModel : public GGMLRunner {
115116
return "lora";
116117
}
117118

118-
bool load_from_file(bool filter_tensor = false) {
119+
bool load_from_file(bool filter_tensor = false, int n_threads = 0) {
119120
LOG_INFO("loading LoRA from '%s'", file_path.c_str());
120121

121122
if (load_failed) {
122123
LOG_ERROR("init lora model loader from file failed: '%s'", file_path.c_str());
123124
return false;
124125
}
125126

127+
std::unordered_map<std::string, TensorStorage> tensors_to_create;
128+
std::mutex lora_mutex;
126129
bool dry_run = true;
127130
auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool {
128-
const std::string& name = tensor_storage.name;
131+
if (dry_run) {
132+
const std::string& name = tensor_storage.name;
129133

130-
if (filter_tensor && !contains(name, "lora")) {
131-
// LOG_INFO("skipping LoRA tesnor '%s'", name.c_str());
132-
return true;
133-
}
134-
// LOG_INFO("lora_tensor %s", name.c_str());
135-
for (int i = 0; i < LORA_TYPE_COUNT; i++) {
136-
if (name.find(type_fingerprints[i]) != std::string::npos) {
137-
type = (lora_t)i;
138-
break;
134+
if (filter_tensor && !contains(name, "lora")) {
135+
return true;
139136
}
140-
}
141137

142-
if (dry_run) {
143-
struct ggml_tensor* real = ggml_new_tensor(params_ctx,
144-
tensor_storage.type,
145-
tensor_storage.n_dims,
146-
tensor_storage.ne);
147-
lora_tensors[name] = real;
138+
{
139+
std::lock_guard<std::mutex> lock(lora_mutex);
140+
for (int i = 0; i < LORA_TYPE_COUNT; i++) {
141+
if (name.find(type_fingerprints[i]) != std::string::npos) {
142+
type = (lora_t)i;
143+
break;
144+
}
145+
}
146+
tensors_to_create[name] = tensor_storage;
147+
}
148148
} else {
149-
auto real = lora_tensors[name];
150-
*dst_tensor = real;
149+
const std::string& name = tensor_storage.name;
150+
auto iter = lora_tensors.find(name);
151+
if (iter != lora_tensors.end()) {
152+
*dst_tensor = iter->second;
153+
}
151154
}
152-
153155
return true;
154156
};
155157

156-
model_loader.load_tensors(on_new_tensor_cb);
158+
model_loader.load_tensors(on_new_tensor_cb, n_threads);
159+
160+
for (const auto& pair : tensors_to_create) {
161+
const auto& name = pair.first;
162+
const auto& ts = pair.second;
163+
struct ggml_tensor* real = ggml_new_tensor(params_ctx,
164+
ts.type,
165+
ts.n_dims,
166+
ts.ne);
167+
lora_tensors[name] = real;
168+
}
169+
157170
alloc_params_buffer();
158-
// exit(0);
171+
159172
dry_run = false;
160-
model_loader.load_tensors(on_new_tensor_cb);
173+
model_loader.load_tensors(on_new_tensor_cb, n_threads);
161174

162175
LOG_DEBUG("lora type: \"%s\"/\"%s\"", lora_downs[type].c_str(), lora_ups[type].c_str());
163176

0 commit comments

Comments
 (0)