Skip to content

Correctly set vocab size in Gemma3 merges #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
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
1 change: 1 addition & 0 deletions mergekit/_data/architectures/gemma3vl.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"preprocessor_config.json",
"processor_config.json"
],
"vocab_size_config_key": "text_config.vocab_size",
"modules": {
"text_decoder": {
"weight_prefix": "language_model.",
Expand Down
1 change: 1 addition & 0 deletions mergekit/architecture/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class ModelArchitecture(BaseModel, frozen=True):
architectures: List[str]
expected_model_type: str = Field(alias="model_type")
tagalong_files: Optional[List[str]] = None
vocab_size_config_key: Optional[str] = None

def all_weights(self, config: PretrainedConfig) -> List[WeightInfo]:
res = []
Expand Down
2 changes: 2 additions & 0 deletions mergekit/architecture/json_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class JsonModularArchitectureDefinition(BaseModel, frozen=True):
architectures: List[str]
expected_model_type: str = Field(alias="model_type")
tagalong_files: Optional[List[str]] = None
vocab_size_config_key: Optional[str] = None


class TemplateWithArithmetic(string.Template):
Expand Down Expand Up @@ -154,6 +155,7 @@ def _load_architecture_json(name: str) -> ModelArchitecture:
architectures=parsed.architectures,
model_type=parsed.expected_model_type,
tagalong_files=parsed.tagalong_files,
vocab_size_config_key=parsed.vocab_size_config_key,
)
elif data.get("kind", "module") == "module":
module = JsonModuleArchitecture(
Expand Down
7 changes: 5 additions & 2 deletions mergekit/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def run_merge(
pad_to_multiple_of = None
if merge_config.tokenizer and merge_config.tokenizer.pad_to_multiple_of:
pad_to_multiple_of = merge_config.tokenizer.pad_to_multiple_of
_update_config_vocab(cfg_out, tokenizer, pad_to_multiple_of=pad_to_multiple_of)
_update_config_vocab(
cfg_out, arch_info, tokenizer, pad_to_multiple_of=pad_to_multiple_of
)

logger.info("Saving config")
cfg_out.save_pretrained(out_path)
Expand Down Expand Up @@ -308,14 +310,15 @@ def _model_out_config(

def _update_config_vocab(
config: transformers.PretrainedConfig,
arch_info: ModelArchitecture,
tokenizer: transformers.PreTrainedTokenizerBase,
pad_to_multiple_of: Optional[int] = None,
):
vocab_size = len(tokenizer.get_vocab())
if pad_to_multiple_of and vocab_size % pad_to_multiple_of:
vocab_size = vocab_size + pad_to_multiple_of - (vocab_size % pad_to_multiple_of)
try:
config.vocab_size = vocab_size
setattr(config, arch_info.vocab_size_config_key or "vocab_size", vocab_size)
except Exception as e:
logger.warning(
"Unable to set vocabulary size in output config - you may need to manually correct it.",
Expand Down
6 changes: 5 additions & 1 deletion mergekit/scripts/tokensurgeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ def main(
tokenizer.save_pretrained(out_path)
cfg_out = arch_info.config
try:
cfg_out.vocab_size = new_embed.shape[0]
setattr(
cfg_out,
arch_info.info.vocab_size_config_key or "vocab_size",
new_embed.shape[0],
)
except AttributeError:
LOG.error(
"Could not set vocab size in config.json - you may need to update it manually."
Expand Down
6 changes: 4 additions & 2 deletions mergekit/tokenizer/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from pydantic import BaseModel
from typing_extensions import Literal

from mergekit.common import ModelPath, ModelReference
from mergekit.architecture import arch_info_for_config
from mergekit.common import ModelPath, ModelReference, get_config_value
from mergekit.graph import Task

logger = logging.getLogger(__name__)
Expand All @@ -26,7 +27,8 @@ def get_vocab_size(model_path: ModelPath, trust_remote_code: bool) -> Optional[i
revision=model_path.revision,
trust_remote_code=trust_remote_code,
)
return cfg.vocab_size
arch_info = arch_info_for_config(cfg)
return get_config_value(cfg, arch_info.vocab_size_config_key or "vocab_size")
except Exception as e:
logger.warning(f"Unable to get vocab size for {model_path}", exc_info=e)

Expand Down
Loading