Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ Currently `flow-merge` supports most of the popular and proven merge methods.
| `qwen` | `QwenForCausalLM` |
| `mistral` | `MistralForCausalLM` |
| `llama` | `LlamaForCausalLM` |
| `gemma` | `GemmaForCausalLM` |
| `phi` | `PhiForCausalLM` |
| `phi3` | `Phi3ForCausalLM` |




> 📢 _We plan to support many models and architectures more, including encoder models such as BERT-Family models too._

Expand Down
72 changes: 72 additions & 0 deletions flow_merge/data/architectures/gemma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"model_type": "gemma",
"architectures": [
"GemmaForCausalLM"
],
"num_layers_config_key": "num_hidden_layers",
"weights": [
{
"name": "model.embed_tokens.weight",
"type": "embed_tokens",
"layer_type": "embedding"
},
{
"name": "model.layers.{layer_index}.input_layernorm.weight",
"type": "input_layernorm",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.q_proj.weight",
"type": "self_attn",
"projection": "q_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.k_proj.weight",
"type": "self_attn",
"projection": "k_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.v_proj.weight",
"type": "self_attn",
"projection": "v_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.o_proj.weight",
"type": "self_attn",
"projection": "o_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.post_attention_layernorm.weight",
"type": "post_attention_layernorm",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.gate_proj.weight",
"type": "mlp",
"projection": "gate_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.up_proj.weight",
"type": "mlp",
"projection": "up_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.down_proj.weight",
"type": "mlp",
"projection": "down_proj",
"layer_type": "decoder"
},
{
"name": "model.norm.weight",
"type": "norm",
"layer_type": "post_norm"
}
],
"residual": {}
}
117 changes: 117 additions & 0 deletions flow_merge/data/architectures/phi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"model_type": "phi",
"architectures": [
"PhiForCausalLM"
],
"num_layers_config_key": "num_hidden_layers",
"weights": [
{
"name": "model.embed_tokens.weight",
"type": "embed_tokens",
"layer_type": "embedding"
},
{
"name": "model.layers.{layer_index}.input_layernorm.weight",
"type": "input_layernorm",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.input_layernorm.bias",
"type": "input_layernorm",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.q_proj.weight",
"type": "self_attn",
"projection": "q_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.q_proj.bias",
"type": "self_attn",
"projection": "q_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.k_proj.weight",
"type": "self_attn",
"projection": "k_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.k_proj.bias",
"type": "self_attn",
"projection": "k_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.v_proj.weight",
"type": "self_attn",
"projection": "v_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.v_proj.bias",
"type": "self_attn",
"projection": "v_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.dense.weight",
"type": "self_attn",
"projection": "dense",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.dense.bias",
"type": "self_attn",
"projection": "dense",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.fc1.weight",
"type": "mlp",
"projection": "fc1",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.fc1.bias",
"type": "mlp",
"projection": "fc1",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.fc2.weight",
"type": "mlp",
"projection": "fc2",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.fc2.bias",
"type": "mlp",
"projection": "fc2",
"layer_type": "decoder"
},
{
"name": "model.final_layernorm.weight",
"type": "final_layernorm",
"layer_type": "layernorm"
},
{
"name": "model.final_layernorm.bias",
"type": "final_layernorm",
"layer_type": "layernorm"
},
{
"name": "lm_head.weight",
"type": "lm_head",
"layer_type": "head"
},
{
"name": "lm_head.bias",
"type": "lm_head",
"layer_type": "head"
}
],
"residual": {}
}
59 changes: 59 additions & 0 deletions flow_merge/data/architectures/phi3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"model_type": "phi3",
"architectures": [
"Phi3ForCausalLM"
],
"num_layers_config_key": "num_hidden_layers",
"weights": [
{
"name": "model.embed_tokens.weight",
"type": "embed_tokens",
"layer_type": "embedding"
},
{
"name": "model.layers.{layer_index}.input_layernorm.weight",
"type": "input_layernorm",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.o_proj.weight",
"type": "self_attn",
"projection": "o_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.self_attn.qkv_proj.weight",
"type": "self_attn",
"projection": "qkv_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.gate_up_proj.weight",
"type": "mlp",
"projection": "gate_up_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.mlp.down_proj.weight",
"type": "mlp",
"projection": "down_proj",
"layer_type": "decoder"
},
{
"name": "model.layers.{layer_index}.post_attention_layernorm.weight",
"type": "post_attention_layernorm",
"layer_type": "decoder"
},
{
"name": "model.norm.weight",
"type": "norm",
"layer_type": "post_norm"
},
{
"name": "lm_head.weight",
"type": "lm_head",
"layer_type": "head"
}
],
"residual": {}
}
11 changes: 6 additions & 5 deletions flow_merge/lib/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import flow_merge.data.architectures

ArchitectureType = Enum(
"ArchitectureType", ["MistralForCausalLM", "LlamaForCausalLM", "Qwen2ForCausalLM"]
"ArchitectureType", ["MistralForCausalLM", "LlamaForCausalLM", "Qwen2ForCausalLM", "PhiForCausalLM", "Phi3ForCausalLM"]
)
ModelType = Enum("ModelType", ["mistral", "llama", "qwen-1.5"])
ModelType = Enum("ModelType", ["mistral", "llama", "qwen-1.5", "phi", "phi3"])
ModelWeightType = Enum(
"ModelWeightType",
[
Expand All @@ -22,15 +22,16 @@
"embed_tokens",
"norm",
"lm_head",
"post_attention_layernorm",
"post_attention_layernorm",
"final_layernorm",
],
)
ProjectionType = Enum(
"ProjectionType",
["v_proj", "q_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
["v_proj", "q_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj", "dense", "fc1", "fc2", "qkv_proj", "gate_up_proj"],
)
ModelWeightLayerType = Enum(
"ModelWeightLayerType", ["decoder", "embedding", "head", "post_norm"]
"ModelWeightLayerType", ["decoder", "embedding", "head", "post_norm", "layernorm"]
)


Expand Down
14 changes: 7 additions & 7 deletions flow_merge/lib/merge_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def __init__(
self.directory_settings: DirectorySettings = data.directory_settings
self.hf_hub_settings: HfHubSettings = data.hf_hub_settings
self.device = self.select_device()
self.models: List[Model] = self.create_models()
self.base_model: Model = self.create_base_model()
self.models: List[Model] = self.create_models(trust_remote_code=self.hf_hub_settings.trust_remote_code)
self.base_model: Model = self.create_base_model(trust_remote_code=self.hf_hub_settings.trust_remote_code)
self.method_config = self._extract_and_set_weights()

@staticmethod
Expand Down Expand Up @@ -171,18 +171,18 @@ def select_device(self) -> str:
return "cpu"
return device

def create_models(self) -> List[Model]:
def create_models(self, trust_remote_code: bool) -> List[Model]:
# observe that models list doesn't contain the base_model by design
# access base_model: Model by accessing base_model variable
models_data = [
m for m in self.data.models if m.path_or_id != self.data.base_model
]
return [Model.from_path(model_data.path_or_id) for model_data in models_data]
return [Model.from_path(model_data.path_or_id, trust_remote_code=trust_remote_code) for model_data in models_data] # TODO - this should take trust_remote_code

def create_base_model(self) -> Model:
def create_base_model(self, trust_remote_code: bool) -> Model:
if not self.data.base_model:
first_model_from_list = self.data.models[0]
return Model.from_path(first_model_from_list.path_or_id)
return Model.from_path(first_model_from_list.path_or_id, trust_remote_code=trust_remote_code) # TODO - this should take trust_remote_code
else:
base_model = next(
(
Expand All @@ -197,7 +197,7 @@ def create_base_model(self) -> Model:
f"Base model '{self.data.base_model}' not found in the list of "
+ f"models {[model.path_or_id for model in self.data.models]}."
)
return Model.from_path(base_model.path_or_id)
return Model.from_path(base_model.path_or_id, trust_remote_code=trust_remote_code)

def _extract_and_set_weights(
self,
Expand Down