-
Notifications
You must be signed in to change notification settings - Fork 65
UT refactor for preview #1160
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
Open
xin3he
wants to merge
25
commits into
main
Choose a base branch
from
ut_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,068
−2,764
Open
UT refactor for preview #1160
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ed41321
initial implementation
xin3he 25694c0
add readme
xin3he 0f20e5f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 28ccfae
add get_model_path func
n1ck-guo b9a177d
add more fixtures
xin3he 07b741e
fix bug
xin3he 0391794
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d35b148
fix few bugs
n1ck-guo c6064da
Merge branch 'main' into ut_refactor
n1ck-guo b15adc8
use get_model_path and remove self.assertTrue/False
xin3he 9d26d04
update cuda ut
n1ck-guo 390f997
update cuda ut
n1ck-guo b740edc
merge
n1ck-guo 65ac22d
replace model with tiny model and fix bug
xin3he 1caf92c
Merge branch 'main' into ut_refactor
xin3he 3764e88
support mllm and untied tiny model
sys-lpot-val 418022f
fix ut path
XuehaoSun 71ec4c2
fix UT failures
xin3he 1af54ed
Merge branch 'main' into ut_refactor
xin3he f1700bd
update cuda ut
n1ck-guo cb5acf6
add ./tmp as workspace and remove duplicate UTs
xin3he cd26af9
revert ark change and add some gguf tiny model back
xin3he b7a7403
Merge branch 'main' into ut_refactor
xin3he 95bd71e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f3369d6
add test_ark change and minor fix
xin3he 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
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,46 @@ | ||
| # Unit Test (UT) Guide | ||
|
|
||
| This project uses `pytest` for unit testing. All test cases are under the `test/` directory. Below is a simple guide for new users to write and run UTs: | ||
|
|
||
| ## 1. Environment Setup | ||
| - Recommended Python 3.8 or above. | ||
| - Install dependencies: | ||
| ```sh | ||
| pip install -r ../requirements.txt | ||
| pip install pytest | ||
| ``` | ||
|
|
||
| ## 2. Test Structure | ||
| - Place your test files in the `test/` directory, and name them starting with `test_`. | ||
| - You can refer to existing `test_*.py` files. | ||
| - Common fixtures (such as `tiny_opt_model`, `opt_model`, `opt_tokenizer`, `dataloader`) and helper functions (such as `model_infer`) are defined in `confest.py` and `helpers.py` and can be imported directly. | ||
| - Example: | ||
| ```python | ||
| # test_example.py | ||
| from ..helpers import model_infer | ||
|
|
||
| def test_model_infer(tiny_opt_model, opt_tokenizer): | ||
| result = model_infer(tiny_opt_model, opt_tokenizer, input_text="hello world") | ||
| assert result is not None | ||
| ``` | ||
|
|
||
| ## 3. Running Tests | ||
| - In the `test/` directory, run: | ||
| ```sh | ||
| pytest | ||
| ``` | ||
| - You can specify a single file or test case: | ||
| ```sh | ||
| pytest test_xxx.py | ||
| pytest -k "test_func_name" | ||
| ``` | ||
|
|
||
| ## 4. Debugging Tips | ||
| - `confest.py` adds the parent directory to `sys.path`, so you can debug without installing the local package. | ||
| - You can directly import project source code in your test cases. | ||
|
|
||
| ## 5. Reference | ||
| - Fixtures are defined in `confest.py` and `fixtures.py` | ||
| - Helper functions are in `helpers.py` | ||
|
|
||
| If you have any questions, feel free to open an issue. |
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,169 @@ | ||
| import os | ||
| import shutil | ||
|
|
||
| import pytest | ||
| import torch | ||
| import transformers | ||
|
|
||
| from .helpers import ( | ||
| DataLoader, | ||
| deepseek_v2_name_or_path, | ||
| gemma_name_or_path, | ||
| get_tiny_model, | ||
| gptj_name_or_path, | ||
| lamini_name_or_path, | ||
| opt_name_or_path, | ||
| phi2_name_or_path, | ||
| qwen_2_5_vl_name_or_path, | ||
| qwen_moe_name_or_path, | ||
| qwen_name_or_path, | ||
| qwen_vl_name_or_path, | ||
| save_tiny_model, | ||
| ) | ||
|
|
||
|
|
||
| # Create tiny model path fixtures for testing | ||
| @pytest.fixture(scope="session") | ||
| def tiny_opt_model_path(): | ||
| model_name_or_path = opt_name_or_path | ||
| tiny_model_path = "./tmp/tiny_opt_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_lamini_model_path(): | ||
| model_name_or_path = lamini_name_or_path | ||
| tiny_model_path = "./tmp/tiny_lamini_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_gptj_model_path(): | ||
| model_name_or_path = gptj_name_or_path | ||
| tiny_model_path = "./tmp/tiny_gptj_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_phi2_model_path(): | ||
| model_name_or_path = phi2_name_or_path | ||
| tiny_model_path = "./tmp/tiny_phi2_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_deepseek_v2_model_path(): | ||
| model_name_or_path = deepseek_v2_name_or_path | ||
| tiny_model_path = "./tmp/tiny_deepseek_v2_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, num_layers=2) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_gemma_model_path(): | ||
| model_name_or_path = gemma_name_or_path | ||
| tiny_model_path = "./tmp/tiny_gemma_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, num_layers=2) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_qwen_model_path(): | ||
| model_name_or_path = qwen_name_or_path | ||
| tiny_model_path = "./tmp/tiny_qwen_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_untied_qwen_model_path(): | ||
| model_name_or_path = qwen_name_or_path | ||
| tiny_model_path = "./tmp/tiny_untied_qwen_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, force_untie=True) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_qwen_moe_model_path(): | ||
| model_name_or_path = qwen_moe_name_or_path | ||
| tiny_model_path = "./tmp/tiny_qwen_moe_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, num_layers=2) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_qwen_vl_model_path(): | ||
| model_name_or_path = qwen_vl_name_or_path | ||
| tiny_model_path = "./tmp/tiny_qwen_vl_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, num_layers=2, is_mllm=True) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tiny_qwen_2_5_vl_model_path(): | ||
| model_name_or_path = qwen_2_5_vl_name_or_path | ||
| tiny_model_path = "./tmp/tiny_qwen_2_5_vl_model_path" | ||
| tiny_model_path = save_tiny_model(model_name_or_path, tiny_model_path, num_layers=2, is_mllm=True) | ||
| yield tiny_model_path | ||
| shutil.rmtree(tiny_model_path) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True, scope="session") | ||
| def clean_tmp_model_folder(): | ||
| yield | ||
| shutil.rmtree("./tmp", ignore_errors=True) # unittest default workspace | ||
| shutil.rmtree("./tmp_autoround", ignore_errors=True) # autoround default workspace | ||
|
|
||
|
|
||
| # Create objective fixtures for testing | ||
| @pytest.fixture(scope="function") | ||
| def tiny_opt_model(): | ||
| model_name_or_path = opt_name_or_path | ||
| return get_tiny_model(model_name_or_path, num_layers=2) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def opt_model(): | ||
| model_name_or_path = opt_name_or_path | ||
| model = transformers.AutoModelForCausalLM.from_pretrained(model_name_or_path, dtype="auto", trust_remote_code=True) | ||
| return model | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def opt_tokenizer(): | ||
| model_name_or_path = opt_name_or_path | ||
| tokenizer = transformers.AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True) | ||
| return tokenizer | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def model(): | ||
| model_name_or_path = opt_name_or_path | ||
| model = transformers.AutoModelForCausalLM.from_pretrained(model_name_or_path, dtype="auto", trust_remote_code=True) | ||
| return model | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tokenizer(): | ||
| model_name_or_path = opt_name_or_path | ||
| tokenizer = transformers.AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True) | ||
| return tokenizer | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def dataloader(): | ||
| return DataLoader() |
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.
It would be better to run most UTs on the latest version, while maintaining a specific environment for those that have known issues.