Skip to content

Commit 915fa43

Browse files
authored
[New Features]add ranks testing for test_predictor (PaddlePaddle#7800)
* add ranks testing * update gpus testing
1 parent 48c1313 commit 915fa43

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

llm/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ def dybatch_preprocess(
470470
max_length=src_length,
471471
return_attention_mask=False,
472472
return_token_type_ids=False,
473+
add_special_tokens=tokenizer.chat_template is None or isinstance(tokenizer, ChatGLMv2Tokenizer),
473474
)
474475
input_ids.append(tokens["input_ids"][0])
475476

tests/fixtures/llm/pretrain.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ inference-predict:
3333
batch_size: 2
3434
decode_strategy: greedy_search
3535
dtype: float16
36+
chat_template: none
3637

3738
inference-to-static:
3839
default:
@@ -45,3 +46,4 @@ inference-infer:
4546
batch_size: 2
4647
decode_strategy: greedy_search
4748
max_length: 20
49+
chat_template: none

tests/llm/test_predictor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
get_path_from_url_with_filelock,
3434
url_file_exists,
3535
)
36+
from tests.testing_utils import GPUsTesting, require_gpu
3637

3738
from .testing_utils import LLMTest, argv_context_guard, load_test_config
3839

@@ -286,3 +287,41 @@ def test_cachekv_int8(self):
286287
full_match += int(inference_item[:min_length] == no_inference_item[:min_length])
287288

288289
self.assertGreaterEqual(count / len(result_0), 0.2)
290+
291+
292+
@parameterized_class(
293+
["model_name_or_path", "model_class"],
294+
[
295+
["__internal_testing__/tiny-random-llama", LlamaForCausalLM],
296+
],
297+
)
298+
class GPUsPredictorTest(LLMTest, GPUsTesting, unittest.TestCase):
299+
config_path: str = "./tests/fixtures/llm/predictor.yaml"
300+
model_name_or_path: str = None
301+
model_class = None
302+
303+
def setUp(self) -> None:
304+
super().setUp()
305+
self.model_class.from_pretrained(self.model_name_or_path, dtype="float16").save_pretrained(self.output_dir)
306+
AutoTokenizer.from_pretrained(self.model_name_or_path).save_pretrained(self.output_dir)
307+
308+
@require_gpu(2)
309+
def test_predictor(self):
310+
self.init_dist_env()
311+
312+
self.run_predictor({"inference_model": True})
313+
result_0 = self._read_result(os.path.join(self.output_dir, "predict.json"))
314+
self.run_predictor({"inference_model": False})
315+
result_1 = self._read_result(os.path.join(self.output_dir, "predict.json"))
316+
317+
# compare the generation result of inference & dygraph model
318+
assert len(result_0) == len(result_1)
319+
320+
count, full_match = 0, 0
321+
for inference_item, no_inference_item in zip(result_0, result_1):
322+
min_length = min(len(inference_item), len(no_inference_item))
323+
count += int(inference_item[: min_length // 2] == no_inference_item[: min_length // 2])
324+
full_match += int(inference_item[:min_length] == no_inference_item[:min_length])
325+
326+
self.assertGreaterEqual(full_match / len(result_0), 0.25)
327+
self.assertGreaterEqual(count / len(result_0), 0.4)

tests/testing_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import numpy as np
2828
import paddle
29+
import paddle.distributed.fleet as fleet
2930
import yaml
3031

3132
from paddlenlp.trainer.argparser import strtobool
@@ -470,3 +471,36 @@ def require_paddle_up_to_2_gpus(test_case):
470471
import paddle
471472

472473
return unittest.skipUnless(paddle.device.cuda.device_count() < 3, "test requires 0 or 1 or 2 GPUs")(test_case)
474+
475+
476+
def require_gpu(min_gpus: int = 1):
477+
def actual_decorator(func):
478+
gpu_count = paddle.device.cuda.device_count()
479+
480+
if gpu_count < min_gpus:
481+
return unittest.skip(f"test requires {min_gpus} GPUs")(func)
482+
483+
def wrapper(*args, **kwargs):
484+
result = func(*args, **kwargs)
485+
return result
486+
487+
return wrapper
488+
489+
return actual_decorator
490+
491+
492+
class GPUsTesting(unittest.TestCase):
493+
def init_dist_env(self, config: dict = {}):
494+
world_size = paddle.distributed.get_world_size()
495+
strategy = fleet.DistributedStrategy()
496+
hybrid_configs = {
497+
"dp_degree": 1,
498+
"mp_degree": world_size,
499+
"pp_degree": 1,
500+
"sharding_degree": 1,
501+
}
502+
hybrid_configs.update(config)
503+
strategy.hybrid_configs = hybrid_configs
504+
505+
fleet.init(is_collective=True, strategy=strategy)
506+
fleet.get_hybrid_communicate_group()

0 commit comments

Comments
 (0)