Created
March 11, 2025 10:48
-
-
Save xiabingquan/3fcc00886243e1279cdc8df1a5eaa063 to your computer and use it in GitHub Desktop.
A print function used in distributed environment
This file contains hidden or 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
# References: https://github.com/huggingface/picotron_tutorial/blob/master/step1_modeling/utils.py | |
def print(*args, is_print_rank=True, **kwargs): | |
""" solves multi-process interleaved print problem """ | |
if not is_print_rank: return | |
with open(__file__, "r") as fh: | |
fcntl.flock(fh, fcntl.LOCK_EX) | |
try: | |
builtins.print(*args, **kwargs) | |
finally: | |
fcntl.flock(fh, fcntl.LOCK_UN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment