xref: /aosp_15_r20/external/pytorch/tools/shared/logging_utils.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerdef pluralize(count: int, singular_word: str, plural_word: str = "") -> str:
2*da0073e9SAndroid Build Coastguard Worker    if count == 1:
3*da0073e9SAndroid Build Coastguard Worker        return f"{count} {singular_word}"
4*da0073e9SAndroid Build Coastguard Worker
5*da0073e9SAndroid Build Coastguard Worker    if not plural_word:
6*da0073e9SAndroid Build Coastguard Worker        plural_word = f"{singular_word}s"
7*da0073e9SAndroid Build Coastguard Worker
8*da0073e9SAndroid Build Coastguard Worker    return f"{count} {plural_word}"
9*da0073e9SAndroid Build Coastguard Worker
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard Workerdef duration_to_str(seconds: float) -> str:
12*da0073e9SAndroid Build Coastguard Worker    if seconds < 0.00001:
13*da0073e9SAndroid Build Coastguard Worker        return "0s"
14*da0073e9SAndroid Build Coastguard Worker    elif seconds < 60:
15*da0073e9SAndroid Build Coastguard Worker        return f"{seconds:.1f}s"
16*da0073e9SAndroid Build Coastguard Worker    elif seconds < 3600:
17*da0073e9SAndroid Build Coastguard Worker        return f"{seconds / 60:.1f}m"
18*da0073e9SAndroid Build Coastguard Worker    else:
19*da0073e9SAndroid Build Coastguard Worker        return f"{seconds / 3600:.1f}h"
20