Skip to content

Instantly share code, notes, and snippets.

@xiabingquan
Created March 11, 2025 10:48
Show Gist options
  • Save xiabingquan/3fcc00886243e1279cdc8df1a5eaa063 to your computer and use it in GitHub Desktop.
Save xiabingquan/3fcc00886243e1279cdc8df1a5eaa063 to your computer and use it in GitHub Desktop.
A print function used in distributed environment
# 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