-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UT] Add Skeleton code for adding Unit Tests (#31)
* add-infer-ut-tests * add new case * fix workflow tests * change workflow name * add test to inference workflow * add pip install pytest * add pip install pytest2 * put tests in inference * new set tests workflow * new set tests workflow * new set tests workflow * new set tests workflow * new set pip * new set testut * add reqs * fix review * fix review name * fix pyhonpath in pytest.ini * fix format review * fix format review * fix review * fix lint error * fix review * delete space * delete space * Update tests/pytest.ini Signed-off-by: Xiaochang Wu <[email protected]> --------- Signed-off-by: yutianchen <[email protected]> Signed-off-by: Xiaochang Wu <[email protected]> Co-authored-by: Xiaochang Wu <[email protected]>
- Loading branch information
1 parent
e6018a4
commit 0d07f56
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 @@ | ||
name: tests | ||
|
||
on: | ||
workflow_call | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
defaults: | ||
run: | ||
shell: bash | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.9' | ||
architecture: 'x64' | ||
|
||
- name: Display Python version | ||
run: python -c "import sys; print(sys.version)" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r ./tests/requirements.txt | ||
- name: Start tests | ||
run: | | ||
bash -c "./tests/run-tests.sh" |
This file contains 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,76 @@ | ||
import pytest | ||
import torch | ||
|
||
from utils import ( | ||
get_deployment_actor_options, | ||
StoppingCriteriaSub, | ||
max_input_len, | ||
get_torch_dtype, | ||
is_cpu_without_ipex, | ||
) | ||
from inference_config import InferenceConfig, DEVICE_CPU | ||
|
||
|
||
# Mock the InferenceConfig for testing | ||
@pytest.fixture | ||
def mock_infer_conf(): | ||
infer_conf = InferenceConfig() | ||
infer_conf.ipex.enabled = False | ||
infer_conf.deepspeed = False | ||
infer_conf.device = DEVICE_CPU | ||
infer_conf.cpus_per_worker = 1 | ||
infer_conf.gpus_per_worker = 0 | ||
infer_conf.hpus_per_worker = 0 | ||
return infer_conf | ||
|
||
|
||
def test_get_deployment_actor_options_cpu(mock_infer_conf): | ||
actor_options = get_deployment_actor_options(mock_infer_conf) | ||
assert actor_options["num_cpus"] == mock_infer_conf.cpus_per_worker | ||
assert "num_gpus" not in actor_options | ||
assert "resources" not in actor_options | ||
|
||
|
||
# Add more tests for different configurations of InferenceConfig | ||
|
||
|
||
def test_stopping_criteria_sub(): | ||
stopping_criteria = StoppingCriteriaSub(stops=[torch.tensor([1, 2, 3])]) | ||
input_ids = torch.tensor([[1, 2, 3]]) | ||
scores = torch.tensor([0.0]) | ||
assert stopping_criteria(input_ids, scores) is True | ||
|
||
input_ids = torch.tensor([[1, 2, 4]]) | ||
assert stopping_criteria(input_ids, scores) is False | ||
|
||
|
||
# Add more tests for different stopping criteria | ||
|
||
|
||
def test_max_input_len(): | ||
assert max_input_len(100) == 128 | ||
assert max_input_len(200) == 512 | ||
assert max_input_len(1000) == 2048 | ||
assert max_input_len(5000) == 4096 | ||
|
||
|
||
# Add more tests for edge cases | ||
|
||
|
||
def test_get_torch_dtype_cpu_without_ipex(mock_infer_conf): | ||
hf_config = None | ||
dtype = get_torch_dtype(mock_infer_conf, hf_config) | ||
assert dtype == torch.get_default_dtype() | ||
|
||
|
||
# Add more tests for different configurations and hf_config values | ||
|
||
|
||
def test_is_cpu_without_ipex(mock_infer_conf): | ||
assert is_cpu_without_ipex(mock_infer_conf) is True | ||
|
||
mock_infer_conf.ipex.enabled = True | ||
assert is_cpu_without_ipex(mock_infer_conf) is False | ||
|
||
|
||
# Add more tests for different configurations |
This file contains 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,2 @@ | ||
[pytest] | ||
pythonpath = ./ ../ ../inference |
This file contains 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,7 @@ | ||
pytest==7.4.4 | ||
torch==2.1.0 | ||
transformers==4.35.2 | ||
starlette==0.34.0 | ||
pydantic==1.10.13 | ||
pydantic-yaml==1.2.0 | ||
pydantic_core==2.14.5 |
This file contains 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,7 @@ | ||
#!/bin/bash | ||
cd $(dirname $0) | ||
|
||
# Run pytest with the test file | ||
pytest -vs ./inference | ||
|
||
echo "Pytest finished running tests." |