This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Torchchat CLI pipeline for Multimodal Models #1140
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -15,15 +15,26 @@ | |
| import torch._dynamo.config | ||
| import torch._inductor.config | ||
| import torch.nn as nn | ||
|
|
||
| try: | ||
| from _torchchat_test_script import flamingo_meta_to_tune | ||
| except ImportError: | ||
| pass | ||
|
|
||
| from distributed import ( | ||
| init_distributed, | ||
| launch_distributed, | ||
| ParallelDims, | ||
| parallelize_llama, | ||
| ) | ||
|
|
||
| from torch.distributed.device_mesh import DeviceMesh | ||
|
|
||
| from torchchat.model import Model | ||
| from torchtune.models.convert_weights import meta_to_tune | ||
|
|
||
| from torchtune.training import set_default_dtype | ||
|
|
||
| from torchchat.model import Model, ModelType | ||
|
|
||
| from torchchat.model_config.model_config import resolve_model_config | ||
| from torchchat.utils.build_utils import ( | ||
|
|
@@ -35,10 +46,6 @@ | |
| from torchchat.utils.measure_time import measure_time | ||
| from torchchat.utils.quantize import quantize_model | ||
|
|
||
| from torchtune.models.convert_weights import meta_to_tune | ||
|
|
||
|
|
||
|
|
||
|
|
||
| @dataclass | ||
| class BuilderArgs: | ||
|
|
@@ -143,7 +150,6 @@ def from_args(cls, args): # -> BuilderArgs: | |
| if "chat" in path_basename or "instruct" in path_basename: | ||
| is_chat_model = True | ||
|
|
||
|
|
||
| output_pte_path = getattr(args, "output_pte_path", None) | ||
| output_dso_path = getattr(args, "output_dso_path", None) | ||
| if output_pte_path and args.dtype.startswith("fast"): | ||
|
|
@@ -234,7 +240,12 @@ def validate_model( | |
|
|
||
| is_tiktoken = self.is_tiktoken | ||
| is_sentencepiece = self.is_sentencepiece | ||
| use_tiktoken = model.config.transformer_args["text"].use_tiktoken | ||
| text_args = model.config.transformer_args.get("text") | ||
| if text_args is None: | ||
| # TODO: Will be refactored: Currently, the only model that doesn't have text in transfomer_args is Flamingo | ||
| use_tiktoken = model.config.model_type == ModelType.Flamingo | ||
|
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. Just calling out that this might cause issues with other use cases that manually pass in checkpoints/params Not something that needs to be addressed in this diff, but may need to revisit sooner rather than later 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. The reason why we need this is on my side I didn't provide a unify config system: the way we get model config from model between tune-support and chat-support is different. |
||
| else: | ||
| use_tiktoken = text_args.use_tiktoken | ||
|
|
||
| if not (is_tiktoken == use_tiktoken) or not (is_sentencepiece != use_tiktoken): | ||
| raise RuntimeError( | ||
|
|
@@ -266,7 +277,9 @@ def from_args(cls, args): # -> TokenizerArgs: | |
| raise RuntimeError("cannot find tokenizer model") | ||
|
|
||
| if not tokenizer_path.is_file(): | ||
| raise RuntimeError(f"did not find tokenizer at {tokenizer_path}") | ||
| raise RuntimeError( | ||
| f"did not find tokenizer at {os.path.abspath(tokenizer_path)}" | ||
| ) | ||
|
|
||
| return cls( | ||
| tokenizer_path=tokenizer_path, | ||
|
|
@@ -335,7 +348,9 @@ def _load_model_default(builder_args, only_config=False): | |
|
|
||
| if builder_args.params_table and builder_args.params_table.endswith("Tune"): | ||
| print("Loading Tune checkpoint") | ||
| meta_checkpoint = torch.load(str(builder_args.checkpoint_path), mmap=True, weights_only=True) | ||
| meta_checkpoint = torch.load( | ||
| str(builder_args.checkpoint_path), mmap=True, weights_only=True | ||
| ) | ||
| checkpoint = meta_to_tune(meta_checkpoint) | ||
| elif builder_args.checkpoint_dir is not None: | ||
| # Load multiple checkpoint; ignore the single path. | ||
|
|
@@ -372,8 +387,17 @@ def _load_model_default(builder_args, only_config=False): | |
| if "model" in checkpoint and "stories" in str(builder_args.checkpoint_path): | ||
| checkpoint = checkpoint["model"] | ||
|
|
||
| checkpoint = {"model." + k: v for k, v in checkpoint.items()} | ||
| model.load_state_dict(checkpoint, assign=True, strict=True) | ||
| if model.config.model_type == ModelType.Flamingo: | ||
| # TODO: Refactor this. For now, overwrite the model with model loaded from params_path | ||
| with set_default_dtype(builder_args.precision), torch.device( | ||
| builder_args.device | ||
| ): | ||
| model = Model.from_params(builder_args.params_path) | ||
Jack-Khuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| state_dict = flamingo_meta_to_tune(checkpoint) | ||
| model.model.load_state_dict(state_dict) | ||
| else: | ||
| checkpoint = {"model." + k: v for k, v in checkpoint.items()} | ||
| model.load_state_dict(checkpoint, assign=True, strict=True) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
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.
plz add a comment to describe why we need to pass the import error