-
Notifications
You must be signed in to change notification settings - Fork 14k
model: support Ministral3 #17644
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
+342
−10
Merged
model: support Ministral3 #17644
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e41c14
conversion script
ngxson 2b2f411
support ministral 3
ngxson 4cebf7b
maybe this is better?
ngxson 84be00f
add TODO for rope_yarn_log_mul
ngxson 786b3f8
better ppl (tested on 14B-Instruct)
ngxson 55a196f
Merge remote-tracking branch 'mistral/xsn/ministral3' into xsn/minist…
ngxson a4f540b
Add Ministral3 support to Mistral format
juliendenize bf08fcc
improve arch handling
ngxson 34234a5
add sizes
ngxson b185b7f
Apply suggestions from code review
ngxson 5600361
nits
ngxson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1581,10 +1581,27 @@ def __init__(self, *args, **kwargs): | |
|
|
||
| # load preprocessor config | ||
| self.preprocessor_config = {} | ||
| if not self.is_mistral_format: | ||
| with open(self.dir_model / "preprocessor_config.json", "r", encoding="utf-8") as f: | ||
|
|
||
| # prefer preprocessor_config.json if possible | ||
| preprocessor_config_path = self.dir_model / "preprocessor_config.json" | ||
| if preprocessor_config_path.is_file(): | ||
| with open(preprocessor_config_path, "r", encoding="utf-8") as f: | ||
| self.preprocessor_config = json.load(f) | ||
|
|
||
| # prefer processor_config.json if possible | ||
| processor_config_path = self.dir_model / "processor_config.json" | ||
| if processor_config_path.is_file(): | ||
| with open(processor_config_path, "r", encoding="utf-8") as f: | ||
| cfg = json.load(f) | ||
| # move image_processor to root level for compat | ||
| if "image_processor" in cfg: | ||
| cfg = { | ||
| **cfg, | ||
| **cfg["image_processor"], | ||
| } | ||
| # merge configs | ||
| self.preprocessor_config = {**self.preprocessor_config, **cfg} | ||
|
|
||
| def get_vision_config(self) -> dict[str, Any] | None: | ||
| config_name = "vision_config" if not self.is_mistral_format else "vision_encoder" | ||
| return self.global_config.get(config_name) | ||
|
|
@@ -2797,7 +2814,32 @@ def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iter | |
|
|
||
| @ModelBase.register("Mistral3ForConditionalGeneration") | ||
| class Mistral3Model(LlamaModel): | ||
| model_arch = gguf.MODEL_ARCH.LLAMA | ||
| model_arch = gguf.MODEL_ARCH.MISTRAL3 | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| # for compatibility, we use LLAMA arch for older models | ||
| # TODO: remove this once everyone has migrated to newer version of llama.cpp | ||
| if self.hparams.get("model_type") != "ministral3": | ||
| self.model_arch = gguf.MODEL_ARCH.LLAMA | ||
| self.gguf_writer.arch = str(self.model_arch) | ||
ngxson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.gguf_writer.add_architecture() | ||
| self.tensor_map = gguf.get_tensor_name_map(self.model_arch, self.block_count) | ||
|
Comment on lines
2821
to
2827
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a time frame of ~1 week to remove this could be a reasonable timeline to remove this. This is for the case where users using new version of script (i.e. via gguf-my-repo) to convert old models, while their local llama.cpp version probably not yet up-to-date |
||
|
|
||
| def set_gguf_parameters(self): | ||
| super().set_gguf_parameters() | ||
| rope_params = self.hparams.get("rope_parameters") | ||
| if self.hparams.get("model_type") == "ministral3": | ||
| assert rope_params is not None, "ministral3 must have 'rope_parameters' config" | ||
| assert rope_params["rope_type"] == "yarn", "ministral3 rope_type must be 'yarn'" | ||
| self.gguf_writer.add_rope_scaling_type(gguf.RopeScalingType.YARN) | ||
| self.gguf_writer.add_rope_scaling_factor(rope_params["factor"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_beta_fast(rope_params["beta_fast"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_beta_slow(rope_params["beta_slow"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_log_mul(rope_params["mscale_all_dim"]) | ||
| self.gguf_writer.add_rope_scaling_orig_ctx_len(rope_params["original_max_position_embeddings"]) | ||
| self.gguf_writer.add_rope_freq_base(rope_params["rope_theta"]) | ||
| self.gguf_writer.add_attn_temperature_scale(rope_params["llama_4_scaling_beta"]) | ||
|
|
||
| def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None): | ||
| name = name.replace("language_model.", "") | ||
|
|
@@ -9809,12 +9851,22 @@ def modify_tensors(self, data_torch, name, bid): | |
|
|
||
|
|
||
| class MistralModel(LlamaModel): | ||
| model_arch = gguf.MODEL_ARCH.LLAMA | ||
| model_arch = gguf.MODEL_ARCH.MISTRAL3 | ||
| model_name = "Mistral" | ||
| hf_arch = "" | ||
| is_mistral_format = True | ||
| undo_permute = False | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| # for compatibility, we use LLAMA arch for older models | ||
| # TODO: remove this once everyone migrates to newer version of llama.cpp | ||
| if "llama_4_scaling" not in self.hparams: | ||
| self.model_arch = gguf.MODEL_ARCH.LLAMA | ||
| self.gguf_writer.arch = str(self.model_arch) | ||
ngxson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.gguf_writer.add_architecture() | ||
| self.tensor_map = gguf.get_tensor_name_map(self.model_arch, self.block_count) | ||
|
|
||
| @staticmethod | ||
| def get_community_chat_template(vocab: MistralVocab, templates_dir: Path, is_mistral_format: bool): | ||
| assert TokenizerVersion is not None and Tekkenizer is not None and SentencePieceTokenizer is not None, _mistral_import_error_msg | ||
|
|
@@ -9854,6 +9906,20 @@ def get_community_chat_template(vocab: MistralVocab, templates_dir: Path, is_mis | |
|
|
||
| return template | ||
|
|
||
| def set_gguf_parameters(self): | ||
| super().set_gguf_parameters() | ||
| if "yarn" in self.hparams: | ||
| yarn_params = self.hparams["yarn"] | ||
| self.gguf_writer.add_rope_scaling_type(gguf.RopeScalingType.YARN) | ||
| self.gguf_writer.add_rope_scaling_factor(yarn_params["factor"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_beta_fast(yarn_params["beta"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_beta_slow(yarn_params["alpha"]) | ||
| self.gguf_writer.add_rope_scaling_yarn_log_mul(0.1) | ||
| self.gguf_writer.add_rope_scaling_orig_ctx_len(yarn_params["original_max_position_embeddings"]) | ||
|
|
||
| if "llama_4_scaling" in self.hparams: | ||
| self.gguf_writer.add_attn_temperature_scale(self.hparams["llama_4_scaling"]["beta"]) | ||
|
|
||
|
|
||
| class PixtralModel(LlavaVisionModel): | ||
| model_name = "Pixtral" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for maintainers: while the ministral3 and the old mistral models have almost the same cgraph, the hparams handling in
llama_model::load_hparamsis quite more complicated. Therefore, it's better to separate the 2 archs to make it more readable.This also make the code to be more future-proof, in case future mistral models become significantly more complicated than the traditional llama arch.