added PhotoMakerIDEncoder model in SD

This commit is contained in:
bssrdf committed 2024-02-04 16:03:00 -05:00
1 parent 5dbbea0ca4
commit cbfa702fad
3 files changed
+240 -43

No files matched your search

+78 -39
View File
@@ -35,27 +35,30 @@ struct FuseBlock {
size_t calculate_mem_size(ggml_type wtype) {
size_t mem_size = 0;
mem_size += 2 * ggml_row_size(GGML_TYPE_F32, channels); // in_layer_0_w/b
mem_size += ggml_row_size(GGML_TYPE_F16, out_channels * channels * 3 * 3); // in_layer_2_w
mem_size += 5 * ggml_row_size(GGML_TYPE_F32, out_channels); // in_layer_2_b/emb_layer_1_b/out_layer_0_w/out_layer_0_b/out_layer_3_b
mem_size += ggml_row_size(wtype, out_channels * emb_channels); // emb_layer_1_w
mem_size += ggml_row_size(GGML_TYPE_F16, out_channels * out_channels * 3 * 3); // out_layer_3_w
if (out_channels != channels) {
mem_size += ggml_row_size(GGML_TYPE_F16, out_channels * channels * 1 * 1); // skip_w
mem_size += ggml_row_size(GGML_TYPE_F32, out_channels); // skip_b
}
mem_size += 2 * ggml_row_size(wtype, in_dim);
mem_size += ggml_row_size(wtype, in_dim*hidden_dim);
mem_size += 5 * ggml_row_size(wtype, in_dim);
mem_size += ggml_row_size(wtype, hidden_dim*out_dim);
mem_size += ggml_row_size(wtype, hidden_dim);
return mem_size;
}
void init_params(struct ggml_context* ctx, ggml_type wtype) {
ln_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, in_dim);
ln_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, in_dim);
void init_params(struct ggml_context* ctx, ggml_type wtype, ggml_allocr* alloc) {
ln_w = ggml_new_tensor_1d(ctx, wtype, in_dim);
ln_b = ggml_new_tensor_1d(ctx, wtype, in_dim);
fc1_b = ggml_new_tensor_1d(ctx, wtype, hidden_dim);
fc1_w = ggml_new_tensor_2d(ctx, wtype, in_dim, hidden_dim);
fc2_b = ggml_new_tensor_1d(ctx, wtype, out_dim);
fc2_w = ggml_new_tensor_2d(ctx, wtype, hidden_dim, out_dim);
// alloc all tensors linked to this context
for (struct ggml_tensor* t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
if (t->data == NULL) {
ggml_allocr_alloc(alloc, t);
}
}
fc1_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hidden_dim);
fc1_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, in_dim, hidden_dim);
fc2_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, out_dim);
fc2_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hidden_dim, out_dim);
}
void map_by_name(std::map<std::string, struct ggml_tensor*>& tensors, const std::string prefix) {
@@ -93,17 +96,27 @@ struct FuseModule{
struct ggml_tensor* ln_b; // [in_dim, ]
FuseModule(int imb_d)
: embed_dim(imb_d){
FuseModule(int imb_d):
embed_dim(imb_d),
mlp1(imb_d*2, imb_d, imb_d, false),
mlp2(imb_d*2, imb_d, imb_d, true) {
// mlp1 = FuseBlock(embed_dim*2, embed_dim, embed_dim, false);
// mlp2 = FuseBlock(embed_dim*2, embed_dim, embed_dim, true);
}
void init_params(struct ggml_context* ctx, ggml_type wtype) {
mlp1 = FuseBlock(embed_dim*2, embed_dim, embed_dim, false);
mlp2 = FuseBlock(embed_dim*2, embed_dim, embed_dim, true);
ln_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, embed_dim);
ln_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, embed_dim);
void init_params(struct ggml_context* ctx, ggml_type wtype, ggml_allocr* alloc) {
ln_w = ggml_new_tensor_1d(ctx, wtype, embed_dim);
ln_b = ggml_new_tensor_1d(ctx, wtype, embed_dim);
// alloc all tensors linked to this context
for (struct ggml_tensor* t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
if (t->data == NULL) {
ggml_allocr_alloc(alloc, t);
}
}
}
void map_by_name(std::map<std::string, struct ggml_tensor*>& tensors, const std::string prefix) {
@@ -115,6 +128,13 @@ struct FuseModule{
}
size_t calculate_mem_size(ggml_type wtype) {
size_t mem_size = mlp1.calculate_mem_size(wtype);
mem_size += mlp2.calculate_mem_size(wtype);
mem_size += 2 * ggml_row_size(wtype, embed_dim);
return mem_size;
}
struct ggml_tensor* fuse_fn(struct ggml_context* ctx,
struct ggml_tensor* prompt_embeds,
struct ggml_tensor* id_embeds) {
@@ -154,20 +174,23 @@ struct PhotoMakerIDEncoder : public GGMLModule {
struct ggml_tensor* visual_projection_2;
PhotoMakerIDEncoder(SDVersion version = VERSION_XL)
: version(version){
: version(version),
fuse_module(2048) {
vision_model = CLIPVisionModel();
// fuse_module = FuseModule(2048);
}
void init_params(struct ggml_context* ctx, ggml_type wtype) {
// void init_params(ggml_context* ctx, ggml_backend_t backend, ggml_type wtype, ggml_allocr* alloc) {
void init_params() {
vision_model = CLIPVisionModel();
fuse_module = FuseModule(2048);
visual_projection_2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1280, 1024); // python [1024, 1280]
ggml_allocr* alloc = ggml_allocr_new_from_buffer(params_buffer);
vision_model.init_params(params_ctx, backend, wtype, alloc);
fuse_module.init_params(params_ctx, wtype, alloc);
visual_projection_2 = ggml_new_tensor_2d(params_ctx, wtype, 1280, 1024); // python [1024, 1280]
ggml_allocr_alloc(alloc, visual_projection_2);
ggml_allocr_free(alloc);
}
void map_by_name(std::map<std::string, struct ggml_tensor*>& tensors, const std::string prefix) {
@@ -176,6 +199,22 @@ struct PhotoMakerIDEncoder : public GGMLModule {
tensors[prefix + "visual_projection_2.weight"] = visual_projection_2;
}
size_t calculate_mem_size() {
size_t mem_size = vision_model.calculate_mem_size(wtype);
mem_size += fuse_module.calculate_mem_size(wtype);
mem_size += ggml_row_size(wtype, 1280*1024);
return mem_size;
}
size_t get_num_tensors() {
size_t num_tensors = (3 + 2 + 37);
return num_tensors;
}
struct ggml_tensor* forward(struct ggml_context* ctx,
struct ggml_tensor* id_pixel_values,
@@ -185,18 +224,18 @@ struct PhotoMakerIDEncoder : public GGMLModule {
// in_layers
struct ggml_tensor *shared_id_embeds = vision_model.forward(ctx, id_pixel_values); // [1]
struct ggml_tensor *id_embeds = vision_model.visual_project(ctx, shared_id_embeds);
struct ggml_tensor *id_embeds_2 = ggml_mul_mat(ctx, visual_projection_2, shared_id_embeds);
struct ggml_tensor *shared_id_embeds = vision_model.forward(ctx, id_pixel_values); // [batch_size, seq_length, hidden_size]
struct ggml_tensor *id_embeds = vision_model.visual_project(ctx, shared_id_embeds); // [batch_size, seq_length, proj_dim(768)]
struct ggml_tensor *id_embeds_2 = ggml_mul_mat(ctx, visual_projection_2, shared_id_embeds); // [batch_size, seq_length, 1280]
// id_embeds = id_embeds.view(b, num_inputs, 1, -1)
// id_embeds_2 = id_embeds_2.view(b, num_inputs, 1, -1)
// id_embeds = torch.cat((id_embeds, id_embeds_2), dim=-1)
id_embeds = ggml_concat(ctx, id_embeds, id_embeds_2); // check whether concat at dim 2 is right
id_embeds = ggml_concat(ctx, id_embeds, id_embeds_2); // [batch_size, seq_length, 1, 2048] check whether concat at dim 2 is right
struct ggml_tensor * updated_prompt_embeds = fuse_module.forward(ctx, prompt_embeds, id_embeds, class_tokens_mask);
return updated_prompt_embeds
return updated_prompt_embeds;
}