Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/model/diffusion/flux3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Flux3 {
int depth = 5;
int single_depth = 28;
bool flow_map = false;
bool fused_qkv = false;

static Flux3Config detect_from_weights(const String2TensorStorage& tensors, const std::string& prefix) {
Flux3Config config;
Expand All @@ -33,6 +34,8 @@ namespace Flux3 {
config.channels = tensor.logical_ne0();
} else if (key == "single_blocks.0.mlp_out.weight") {
config.mlp_dim = tensor.logical_ne0();
} else if (key == "single_blocks.0.qkv.weight") {
config.fused_qkv = true;
} else if (key == "ref_time_in.in_layer.weight") {
config.flow_map = true;
}
Expand Down Expand Up @@ -62,15 +65,21 @@ namespace Flux3 {

struct Block : public GGMLBlock {
int heads;
bool fused_qkv;

Block(const Flux3Config& config)
: heads(config.heads) {
for (const auto* name : {"q_proj", "k_proj", "v_proj", "attn_out"}) {
blocks[name] = std::make_shared<Linear>(config.hidden_size, config.hidden_size, false);
: heads(config.heads), fused_qkv(config.fused_qkv) {
if (fused_qkv) {
blocks["qkv"] = std::make_shared<Linear>(config.hidden_size, 3 * config.hidden_size, false);
} else {
for (const auto* name : {"q_proj", "k_proj", "v_proj"}) {
blocks[name] = std::make_shared<Linear>(config.hidden_size, config.hidden_size, false);
}
}
blocks["mlp_in"] = std::make_shared<Linear>(config.hidden_size, 2 * config.mlp_dim, false);
blocks["mlp_out"] = std::make_shared<Linear>(config.mlp_dim, config.hidden_size, false);
blocks["norm"] = std::make_shared<Flux::QKNorm>(config.hidden_size / heads);
blocks["attn_out"] = std::make_shared<Linear>(config.hidden_size, config.hidden_size, false);
blocks["mlp_in"] = std::make_shared<Linear>(config.hidden_size, 2 * config.mlp_dim, false);
blocks["mlp_out"] = std::make_shared<Linear>(config.mlp_dim, config.hidden_size, false);
blocks["norm"] = std::make_shared<Flux::QKNorm>(config.hidden_size / heads);
}

ggml_tensor* linear(GGMLRunnerContext* ctx, const std::string& name, ggml_tensor* x) {
Expand All @@ -87,13 +96,28 @@ namespace Flux3 {
x_mod = x_mod ? ggml_concat(gc, x_mod, part, 1) : part;
offset += lengths[i];
}
auto split = [&](ggml_tensor* v) {
return ggml_reshape_4d(gc, v, x->ne[0] / heads, heads, x->ne[1], x->ne[2]);
};
const int64_t head_dim = x->ne[0] / heads;
ggml_tensor *q, *k, *v;
if (fused_qkv) {
auto qkv = linear(ctx, "qkv", x_mod);
auto select = [&](int i) {
return ggml_view_4d(gc, qkv, head_dim, heads, qkv->ne[1], qkv->ne[2],
head_dim * qkv->nb[0], qkv->nb[1], qkv->nb[2], i * x->ne[0] * qkv->nb[0]);
};
q = select(0);
k = select(1);
v = select(2);
} else {
auto reshape = [&](ggml_tensor* projected) {
return ggml_reshape_4d(gc, projected, head_dim, heads, projected->ne[1], projected->ne[2]);
};
q = reshape(linear(ctx, "q_proj", x_mod));
k = reshape(linear(ctx, "k_proj", x_mod));
v = reshape(linear(ctx, "v_proj", x_mod));
}
auto norm = std::dynamic_pointer_cast<Flux::QKNorm>(blocks["norm"]);
auto q = norm->query_norm(ctx, split(linear(ctx, "q_proj", x_mod)));
auto k = norm->key_norm(ctx, split(linear(ctx, "k_proj", x_mod)));
auto v = split(linear(ctx, "v_proj", x_mod));
q = norm->query_norm(ctx, q);
k = norm->key_norm(ctx, k);
auto attn = Rope::attention(ctx, q, k, v, pe, nullptr);
auto mlp = ggml_ext_silu_act(ctx, linear(ctx, "mlp_in", x_mod));
auto update = ggml_add(gc, linear(ctx, "attn_out", attn), linear(ctx, "mlp_out", mlp));
Expand Down
6 changes: 4 additions & 2 deletions src/model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ SDVersion ModelLoader::get_sd_version() const {
return VERSION_FIBO2;
}

if (tensor_storage_map.find("model.diffusion_model.emb_in.image.weight") != tensor_storage_map.end() &&
tensor_storage_map.find("model.diffusion_model.txt_mode_blocks.0.q_proj.weight") != tensor_storage_map.end()) {
if (tensor_storage_map.count("model.diffusion_model.emb_in.image.weight") &&
(tensor_storage_map.count("model.diffusion_model.txt_mode_blocks.0.q_proj.weight") ||
tensor_storage_map.count("model.diffusion_model.single_blocks.0.qkv.weight") ||
tensor_storage_map.count("model.diffusion_model.single_transformer_blocks.0.qkv.weight"))) {
return VERSION_FLUX3;
}

Expand Down
2 changes: 2 additions & 0 deletions src/name_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ static std::string convert_diffusers_dit_to_original_flux3(std::string name) {
{".attn.to_k.", ".k_proj."},
{".attn.to_v.", ".v_proj."},
{".attn.to_out.0.", ".attn_out."},
{".norm.query_norm.weight", ".norm.query_norm.scale"},
{".norm.key_norm.weight", ".norm.key_norm.scale"},
{".ff.linear_in.", ".mlp_in."},
{".ff.linear_out.", ".mlp_out."},
};
Expand Down
Loading