Skip to content

Commit

Permalink
[UT] Add Skeleton code for adding Unit Tests (#31)
Browse files Browse the repository at this point in the history
* 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
yutianchen666 and xwu99 authored Jan 16, 2024
1 parent e6018a4 commit 0d07f56
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/workflow_orders_on_merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ on:
- 'rlhf/**'
- 'tools/**'
- 'pyproject.toml'
- 'tests/**'

jobs:
call-lint:
uses: ./.github/workflows/workflow_lint.yml

call-tests:
uses: ./.github/workflows/workflow_tests.yml

call-inference:
uses: ./.github/workflows/workflow_inference.yml

Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/workflow_orders_on_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ on:
- 'rlhf/**'
- 'tools/**'
- 'pyproject.toml'
- 'tests/**'

jobs:

call-lint:
uses: ./.github/workflows/workflow_lint.yml

call-tests:
needs: call-lint
uses: ./.github/workflows/workflow_tests.yml

call-inference:
needs: call-lint
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/workflow_tests.yml
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"
76 changes: 76 additions & 0 deletions tests/inference/test_utils.py
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
2 changes: 2 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = ./ ../ ../inference
7 changes: 7 additions & 0 deletions tests/requirements.txt
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
7 changes: 7 additions & 0 deletions tests/run-tests.sh
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."

0 comments on commit 0d07f56

Please sign in to comment.