Skip to content

Commit 6d9ea1c

Browse files
committed
add on_process and on_local_process
1 parent 1872522 commit 6d9ea1c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/accelerate/accelerator.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,36 @@ def wrapper(self, *args, **kwargs):
381381

382382
return wrapper
383383

384+
def on_process(process_idx):
385+
"""
386+
Run func on certain process only
387+
"""
388+
389+
def decorator(func):
390+
@wraps(func)
391+
def wrapper(self, *args, **kwargs):
392+
if self.process_idx == process_idx or not self.use_distributed:
393+
return func(self, *args, **kwargs)
394+
395+
return wrapper
396+
397+
return decorator
398+
399+
def on_process(local_process_idx):
400+
"""
401+
Run func on certain local process only
402+
"""
403+
404+
def decorator(func):
405+
@wraps(func)
406+
def wrapper(self, *args, **kwargs):
407+
if self.local_process_idx == local_process_idx or not self.use_distributed:
408+
return func(self, *args, **kwargs)
409+
410+
return wrapper
411+
412+
return decorator
413+
384414
def _goes_first(self, is_main):
385415
if not is_main:
386416
self.wait_for_everyone()

0 commit comments

Comments
 (0)