Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 85 additions & 77 deletions engineV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,85 @@ def _print_argument(prefix, message):
print(f"{prefix} {message}", flush=True)


def _mode_uses_torch(options):
return any(
getattr(options, opt, False)
for opt in (
"accuracy",
"paddle_cinn",
"paddle_gpu_performance",
"torch_gpu_performance",
"paddle_torch_gpu_performance",
"accuracy_stable",
"paddle_custom_device",
"custom_device_vs_gpu",
)
)


def _load_test_classes(options):
from tester import APIConfig, APITestPaddleOnly

test_classes = {
"APIConfig": APIConfig,
"APITestPaddleOnly": APITestPaddleOnly,
}
if _mode_uses_torch(options):
from tester import (
APITestAccuracy,
APITestAccuracyStable,
APITestCINNVSDygraph,
APITestCustomDeviceVSCPU,
APITestPaddleDeviceVSGPU,
APITestPaddleGPUPerformance,
APITestPaddleTorchGPUPerformance,
APITestTorchGPUPerformance,
)

test_classes.update(
{
"APITestAccuracy": APITestAccuracy,
"APITestCINNVSDygraph": APITestCINNVSDygraph,
"APITestPaddleGPUPerformance": APITestPaddleGPUPerformance,
"APITestTorchGPUPerformance": APITestTorchGPUPerformance,
"APITestPaddleTorchGPUPerformance": APITestPaddleTorchGPUPerformance,
"APITestAccuracyStable": APITestAccuracyStable,
"APITestCustomDeviceVSCPU": APITestCustomDeviceVSCPU,
"APITestPaddleDeviceVSGPU": APITestPaddleDeviceVSGPU,
}
)
return test_classes


def _select_test_class(options):
test_classes = _load_test_classes(options)
option_to_class_name = {
"paddle_only": "APITestPaddleOnly",
"paddle_cinn": "APITestCINNVSDygraph",
"accuracy": "APITestAccuracy",
"paddle_gpu_performance": "APITestPaddleGPUPerformance",
"torch_gpu_performance": "APITestTorchGPUPerformance",
"paddle_torch_gpu_performance": "APITestPaddleTorchGPUPerformance",
"accuracy_stable": "APITestAccuracyStable",
"paddle_custom_device": "APITestCustomDeviceVSCPU",
"custom_device_vs_gpu": "APITestPaddleDeviceVSGPU",
}
for opt, class_name in option_to_class_name.items():
if getattr(options, opt, False):
return test_classes[class_name]
return test_classes["APITestAccuracy"]


def _clear_device_cache(options):
import paddle

if _mode_uses_torch(options):
import torch

torch.cuda.empty_cache()
paddle.device.cuda.empty_cache()


def _parse_gpu_ids(gpu_ids_arg, device_count):
gpu_ids = []
for raw_part in gpu_ids_arg.split(","):
Expand Down Expand Up @@ -441,7 +520,6 @@ def pid_exists(pid):
os.environ["CUDA_VISIBLE_DEVICES"] = str(assigned_gpu)

import paddle
import torch

# Load custom ops from paddlefleet to register _run_custom_op operators
try:
Expand All @@ -453,39 +531,11 @@ def pid_exists(pid):
except ImportError:
pass

globals()["torch"] = torch
globals()["paddle"] = paddle

from tester import (
APIConfig,
APITestAccuracy,
APITestAccuracyStable,
APITestCINNVSDygraph,
APITestCustomDeviceVSCPU,
APITestPaddleDeviceVSGPU,
APITestPaddleGPUPerformance,
APITestPaddleOnly,
APITestPaddleTorchGPUPerformance,
APITestTorchGPUPerformance,
)

test_classes = {
"APIConfig": APIConfig,
"APITestAccuracy": APITestAccuracy,
"APITestCINNVSDygraph": APITestCINNVSDygraph,
"APITestPaddleOnly": APITestPaddleOnly,
"APITestPaddleGPUPerformance": APITestPaddleGPUPerformance,
"APITestTorchGPUPerformance": APITestTorchGPUPerformance,
"APITestPaddleTorchGPUPerformance": APITestPaddleTorchGPUPerformance,
"APITestAccuracyStable": APITestAccuracyStable,
"APITestCustomDeviceVSCPU": APITestCustomDeviceVSCPU,
"APITestPaddleDeviceVSGPU": APITestPaddleDeviceVSGPU,
}
globals().update(test_classes)
globals().update(_load_test_classes(options))

def signal_handler(*args):
torch.cuda.empty_cache()
paddle.device.cuda.empty_cache()
_clear_device_cache(options)
restore_stdio()
close_process_files()
sys.exit(0)
Expand Down Expand Up @@ -540,22 +590,7 @@ def run_test_case(api_config_str, options):
print(f"[config parse error] {api_config_str} {err!s}", flush=True)
return

option_to_class = {
"paddle_only": APITestPaddleOnly,
"paddle_cinn": APITestCINNVSDygraph,
"accuracy": APITestAccuracy,
"paddle_gpu_performance": APITestPaddleGPUPerformance,
"torch_gpu_performance": APITestTorchGPUPerformance,
"paddle_torch_gpu_performance": APITestPaddleTorchGPUPerformance,
"accuracy_stable": APITestAccuracyStable,
"paddle_custom_device": APITestCustomDeviceVSCPU,
"custom_device_vs_gpu": APITestPaddleDeviceVSGPU,
}

test_class = next(
(cls for opt, cls in option_to_class.items() if getattr(options, opt, False)),
APITestAccuracy, # default fallback
)
test_class = _select_test_class(options)
kwargs = {k: v for k, v in vars(options).items() if k in VALID_TEST_ARGS}
case = test_class(api_config, **kwargs)
try:
Expand All @@ -582,8 +617,7 @@ def run_test_case(api_config_str, options):
"paddle_torch_gpu_performance",
)
) and not getattr(options, "use_gpu_cache_mode", False):
torch.cuda.empty_cache()
paddle.device.cuda.empty_cache()
_clear_device_cache(options)


def main():
Expand Down Expand Up @@ -903,18 +937,7 @@ def main():
except ImportError:
pass

from tester import (
APIConfig,
APITestAccuracy,
APITestAccuracyStable,
APITestCINNVSDygraph,
APITestCustomDeviceVSCPU,
APITestPaddleDeviceVSGPU,
APITestPaddleGPUPerformance,
APITestPaddleOnly,
APITestPaddleTorchGPUPerformance,
APITestTorchGPUPerformance,
)
globals().update(_load_test_classes(options))

# set log_writer
set_engineV2()
Expand All @@ -930,22 +953,7 @@ def main():
print(f"[config parse error] {options.api_config} {err!s}", flush=True)
return

option_to_class = {
"paddle_only": APITestPaddleOnly,
"paddle_cinn": APITestCINNVSDygraph,
"accuracy": APITestAccuracy,
"paddle_gpu_performance": APITestPaddleGPUPerformance,
"torch_gpu_performance": APITestTorchGPUPerformance,
"paddle_torch_gpu_performance": APITestPaddleTorchGPUPerformance,
"accuracy_stable": APITestAccuracyStable,
"paddle_custom_device": APITestCustomDeviceVSCPU,
"custom_device_vs_gpu": APITestPaddleDeviceVSGPU,
}

test_class = next(
(cls for opt, cls in option_to_class.items() if getattr(options, opt, False)),
APITestAccuracy, # default fallback
)
test_class = _select_test_class(options)

if options.test_cpu:
import paddle
Expand Down
Loading
Loading