forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce pytest prints (pytorch#117069)
* custom pytest-shard so I can control the verbosity (also index by 1 since it's confusing) * normal runs (not keep-going) always rerun each failed test 9 times (3 per process, 3 processes). Previously it would only run the entire test file 3 times, so if a test before you segfaulted, you only got 2 tries Example of quieter log https://github.com/pytorch/pytorch/actions/runs/7481334046/job/20363147497 "items in shard" only gets printed once at the beginning, and the reruns just say how many got skipped. Pull Request resolved: pytorch#117069 Approved by: https://github.com/huydhn
- Loading branch information
1 parent
e432b2e
commit 2f89ef2
Showing
7 changed files
with
122 additions
and
46 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
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,67 @@ | ||
""" | ||
Custom pytest shard plugin | ||
https://github.com/AdamGleave/pytest-shard/blob/64610a08dac6b0511b6d51cf895d0e1040d162ad/pytest_shard/pytest_shard.py#L1 | ||
Modifications: | ||
* shards are now 1 indexed instead of 0 indexed | ||
* option for printing items in shard | ||
""" | ||
import hashlib | ||
|
||
from _pytest.config.argparsing import Parser | ||
|
||
|
||
def pytest_addoptions(parser: Parser): | ||
"""Add options to control sharding.""" | ||
group = parser.getgroup("shard") | ||
group.addoption( | ||
"--shard-id", dest="shard_id", type=int, default=1, help="Number of this shard." | ||
) | ||
group.addoption( | ||
"--num-shards", | ||
dest="num_shards", | ||
type=int, | ||
default=1, | ||
help="Total number of shards.", | ||
) | ||
group.addoption( | ||
"--print-items", | ||
dest="print_items", | ||
action="store_true", | ||
default=False, | ||
help="Print out the items being tested in this shard.", | ||
) | ||
|
||
|
||
class PytestShardPlugin: | ||
def __init__(self, config): | ||
self.config = config | ||
|
||
def pytest_report_collectionfinish(self, config, items) -> str: | ||
"""Log how many and which items are tested in this shard.""" | ||
msg = f"Running {len(items)} items in this shard" | ||
if config.getoption("print_items"): | ||
msg += ": " + ", ".join([item.nodeid for item in items]) | ||
return msg | ||
|
||
def sha256hash(self, x: str) -> int: | ||
return int.from_bytes(hashlib.sha256(x.encode()).digest(), "little") | ||
|
||
def filter_items_by_shard(self, items, shard_id: int, num_shards: int): | ||
"""Computes `items` that should be tested in `shard_id` out of `num_shards` total shards.""" | ||
new_items = [ | ||
item | ||
for item in items | ||
if self.sha256hash(item.nodeid) % num_shards == shard_id - 1 | ||
] | ||
return new_items | ||
|
||
def pytest_collection_modifyitems(self, config, items): | ||
"""Mutate the collection to consist of just items to be tested in this shard.""" | ||
shard_id = config.getoption("shard_id") | ||
shard_total = config.getoption("num_shards") | ||
if shard_id < 1 or shard_id > shard_total: | ||
raise ValueError( | ||
f"{shard_id} is not a valid shard ID out of {shard_total} total shards" | ||
) | ||
|
||
items[:] = self.filter_items_by_shard(items, shard_id, shard_total) |
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