Download scripts/profile_plugin.py from trl-internal-testing/ci-profile: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/datasets/trl-internal-testing/ci-profile/resolve/main/scripts/profile_plugin.py
- Command line
-
hf download hf://datasets/trl-internal-testing/ci-profile/scripts/profile_plugin.py
-
curl -L -o profile_plugin.py https://huggingface.co/datasets/trl-internal-testing/ci-profile/resolve/main/scripts/profile_plugin.py
1.56 kB
| """Pytest plugin: per-test wall time (setup/call/teardown) and peak CUDA memory, written as JSONL. | |
| Load with `-p profile_plugin` (directory on PYTHONPATH). Output path: $PROFILE_OUT. Extra metadata copied into every | |
| row: $PROFILE_META (JSON object). Peak memory covers the PyTorch allocator of the pytest process only (not vLLM servers | |
| or `accelerate launch` subprocesses). | |
| """ | |
| import json | |
| import os | |
| import pytest | |
| import torch | |
| _rows = [] | |
| _meta = json.loads(os.environ.get("PROFILE_META", "{}")) | |
| def pytest_runtest_protocol(item, nextitem): | |
| row = {"nodeid": item.nodeid, "file": item.nodeid.split("::")[0], "outcome": "passed"} | |
| item._profile_row = row | |
| torch.cuda.reset_peak_memory_stats() | |
| yield | |
| row["peak_alloc_mib"] = torch.cuda.max_memory_allocated() / 2**20 | |
| row["peak_reserved_mib"] = torch.cuda.max_memory_reserved() / 2**20 | |
| row["total_s"] = sum(row.get(f"{when}_s", 0.0) for when in ("setup", "call", "teardown")) | |
| _rows.append({**_meta, **row}) | |
| def pytest_runtest_makereport(item, call): | |
| outcome = yield | |
| report = outcome.get_result() | |
| row = item._profile_row | |
| row[f"{report.when}_s"] = report.duration | |
| if report.outcome != "passed" and row["outcome"] == "passed": | |
| row["outcome"] = report.outcome if report.when == "call" or report.skipped else "error" | |
| def pytest_sessionfinish(session, exitstatus): | |
| with open(os.environ["PROFILE_OUT"], "w") as f: | |
| for row in _rows: | |
| f.write(json.dumps(row) + "\n") | |