-
Notifications
You must be signed in to change notification settings - Fork 453
[Tracing][Testing] Add tracing tests #1335
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
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ca96907
replace with patch_attr
kylesayrs 755a063
Merge branch 'main' into kylesayrs/rename-patch_attr
kylesayrs 96cf84e
Merge branch 'main' into kylesayrs/rename-patch_attr
kylesayrs 2f0136c
simplify
kylesayrs 803b73f
Merge remote-tracking branch 'origin' into kylesayrs/rename-patch_attr
kylesayrs 5dfaabf
remove dreg
kylesayrs 35a046e
add utils
kylesayrs 547e68f
add no init weights context
kylesayrs bb1912c
add tracing tests
kylesayrs 71c5575
add test
kylesayrs 0e074e4
Merge branch 'main' into kylesayrs/tracing-testing
kylesayrs ce1b91c
rename file to be picked up by pytest
kylesayrs daaf284
Merge remote-tracking branch 'origin' into kylesayrs/tracing-testing
kylesayrs b144eb1
add hf token
kylesayrs d17877b
Merge remote-tracking branch 'origin' into kylesayrs/tracing-testing
kylesayrs f212a3e
remove hf cache dir, remove whisper
kylesayrs 5875aa1
Merge remote-tracking branch 'origin' into kylesayrs/tracing-testing
kylesayrs 8c75c0d
cleanup, do not require ignore
kylesayrs e85ec84
add import skip
kylesayrs da5a171
Merge branch 'main' into kylesayrs/tracing-testing
kylesayrs 4111af6
Merge branch 'main' into kylesayrs/tracing-testing
kylesayrs 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import contextlib | ||
| import logging | ||
| import os | ||
| import tempfile | ||
| from typing import Type | ||
|
|
||
| import torch | ||
| from huggingface_hub import snapshot_download | ||
| from safetensors.torch import save_file | ||
| from transformers import AutoModelForCausalLM, PreTrainedModel | ||
| from transformers.modeling_utils import TORCH_INIT_FUNCTIONS | ||
| from transformers.utils import SAFE_WEIGHTS_INDEX_NAME, WEIGHTS_INDEX_NAME | ||
|
|
||
| from llmcompressor.utils.helpers import patch_attr | ||
|
|
||
| __all__ = ["skip_weights_download", "patch_transformers_logger_level"] | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def skip_weights_download(model_class: Type[PreTrainedModel] = AutoModelForCausalLM): | ||
| """ | ||
| Context manager under which models are initialized without having to download | ||
| the model weight files. This differs from `init_empty_weights` in that weights are | ||
| allocated on to assigned devices with random values, as opposed to being on the meta | ||
| device | ||
|
|
||
| :param model_class: class to patch, defaults to `AutoModelForCausalLM` | ||
| """ | ||
| original_fn = model_class.from_pretrained | ||
| weights_files = [ | ||
| "*.bin", | ||
| "*.safetensors", | ||
| "*.pth", | ||
| SAFE_WEIGHTS_INDEX_NAME, | ||
| WEIGHTS_INDEX_NAME, | ||
| "*.msgpack", | ||
| ] | ||
|
|
||
| @classmethod | ||
| def patched(cls, *args, **kwargs): | ||
| nonlocal tmp_dir | ||
|
|
||
| # intercept model stub | ||
| model_stub = args[0] if args else kwargs.pop("pretrained_model_name_or_path") | ||
|
|
||
| # download files into tmp dir | ||
| os.makedirs(tmp_dir, exist_ok=True) | ||
| snapshot_download( | ||
| repo_id=model_stub, local_dir=tmp_dir, ignore_patterns=weights_files | ||
| ) | ||
|
|
||
| # make an empty weights file to avoid errors | ||
| weights_file_path = os.path.join(tmp_dir, "model.safetensors") | ||
| save_file({}, weights_file_path, metadata={"format": "pt"}) | ||
|
|
||
| # load from tmp dir | ||
| model = original_fn(tmp_dir, **kwargs) | ||
|
|
||
| # replace model_path | ||
| model.name_or_path = model_stub | ||
| model.config._name_or_path = model_stub | ||
|
|
||
| return model | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp_dir, patch_attr( | ||
| model_class, "from_pretrained", patched | ||
| ), skip_weights_initialize(), patch_transformers_logger_level(): | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def skip_weights_initialize(use_zeros: bool = False): | ||
| def skip(tensor: torch.Tensor, *args, **kwargs) -> torch.Tensor: | ||
| if use_zeros: | ||
| return tensor.fill_(0) | ||
| return tensor | ||
|
|
||
| with contextlib.ExitStack() as stack: | ||
| for name in TORCH_INIT_FUNCTIONS.keys(): | ||
| stack.enter_context(patch_attr(torch.nn.init, name, skip)) | ||
| stack.enter_context(patch_attr(torch.Tensor, name, skip)) | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def patch_transformers_logger_level(level: int = logging.ERROR): | ||
| """ | ||
| Context under which the transformers logger's level is modified | ||
|
|
||
| This can be used with `skip_weights_download` to squelch warnings related to | ||
| missing parameters in the checkpoint | ||
|
|
||
| :param level: new logging level for transformers logger. Logs whose level is below | ||
| this level will not be logged | ||
| """ | ||
| transformers_logger = logging.getLogger("transformers.modeling_utils") | ||
| restore_log_level = transformers_logger.getEffectiveLevel() | ||
|
|
||
| transformers_logger.setLevel(level=level) | ||
| yield | ||
| transformers_logger.setLevel(level=restore_log_level) |
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import pytest | ||
| from transformers import AutoModelForCausalLM | ||
|
|
||
| from llmcompressor.transformers.tracing import ( | ||
| TraceableIdefics3ForConditionalGeneration, | ||
| TraceableLlavaForConditionalGeneration, | ||
| TraceableMllamaForConditionalGeneration, | ||
| TraceableQwen2_5_VLForConditionalGeneration, | ||
| TraceableQwen2VLForConditionalGeneration, | ||
| TraceableWhisperForConditionalGeneration, | ||
| ) | ||
| from llmcompressor.transformers.tracing.debug import trace | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model_id,model_class,targets", | ||
| [ | ||
| ("meta-llama/Meta-Llama-3-8B-Instruct", AutoModelForCausalLM, None), | ||
| ], | ||
| ) | ||
| def test_text_trace(model_id, model_class, targets): | ||
| trace( | ||
| model_id, | ||
| model_class, | ||
| targets, | ||
| ignore=[], | ||
| modality="text", | ||
| trust_remote_code=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model_id,model_class,targets,ignore", | ||
| [ | ||
| ( | ||
| "Qwen/Qwen2-VL-2B-Instruct", | ||
| TraceableQwen2VLForConditionalGeneration, | ||
| None, | ||
| ["lm_head", "re:visual.*"], | ||
| ), | ||
| ( | ||
| "Qwen/Qwen2.5-VL-7B-Instruct", | ||
| TraceableQwen2_5_VLForConditionalGeneration, | ||
| None, | ||
| ["lm_head", "re:visual.*"], | ||
| ), | ||
| ( | ||
| "mgoin/pixtral-12b", | ||
| TraceableLlavaForConditionalGeneration, | ||
| ["MistralDecoderLayer"], | ||
| ["re:.*lm_head", "re:vision_tower.*", "re:multi_modal_projector.*"], | ||
| ), | ||
| ( | ||
| "meta-llama/Llama-3.2-11B-Vision-Instruct", | ||
| TraceableMllamaForConditionalGeneration, | ||
| None, | ||
| ["re:.*lm_head", "re:multi_modal_projector.*", "re:vision_model.*"], | ||
| ), | ||
| ( | ||
| "llava-hf/llava-1.5-7b-hf", | ||
| TraceableLlavaForConditionalGeneration, | ||
| ["LlamaDecoderLayer"], | ||
| ["re:.*lm_head", "re:vision_tower.*", "re:multi_modal_projector.*"], | ||
| ), | ||
| ( | ||
| "HuggingFaceM4/Idefics3-8B-Llama3", | ||
| TraceableIdefics3ForConditionalGeneration, | ||
| ["Idefics3EncoderLayer", "LlamaDecoderLayer"], | ||
| ["re:.*lm_head", "re:model.vision_model.*", "re:model.connector.*"], | ||
| ), | ||
| ], | ||
| ) | ||
| def test_vision_trace(model_id, model_class, targets, ignore): | ||
| trace( | ||
| model_id, | ||
| model_class, | ||
| targets, | ||
| ignore=ignore, | ||
| modality="vision", | ||
| trust_remote_code=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model_id,model_class,targets,ignore", | ||
| [ | ||
| ("openai/whisper-large-v3", TraceableWhisperForConditionalGeneration, None, []), | ||
| ], | ||
| ) | ||
| def test_audio_trace(model_id, model_class, targets, ignore): | ||
| trace( | ||
| model_id, | ||
| model_class, | ||
| targets, | ||
| ignore=ignore, | ||
| modality="audio", | ||
| trust_remote_code=True, | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.