huggingface / accelerate

🚀 A simple way to launch, train, and use PyTorch models on almost any device and distributed configuration, automatic mixed precision (including fp8), and easy-to-configure FSDP and DeepSpeed support
https://huggingface.co/docs/accelerate
Apache License 2.0
7.32k stars 871 forks source link

[Feature Request] Allows registering custom trackers to internal tracker type registry #2734

Open luowyang opened 2 months ago

luowyang commented 2 months ago

Currently accelerate maintains an internal known tracker registry LOGGER_TYPE_TO_CLASS, which is used for tracker initializing, filtering, among other things. However, this makes working with custom trackers annoying in user code. One such example is when initializing custom trackers, instead of something like

accelerator = Accelerator(log_with=args.tracker_names)

one has to write:

trackers = [name if name != 'custom_tracker' else CustomTracker() for name in args.tracker_names]
accelerator = Accelerator(log_with=trackers)

This is annoying in that users have to write many boilerplate code to manage the init args of both custom trackers and built-in trackers in one place. If the users can register their custom trackers into LOGGER_TYPE_TO_CLASS, it will be possible to initialize the custom trackers in the same (and uniform) way as the built-in trackers by:

init_kwargs = dict(
    wandb=dict(
        # init args for wandb
    ),
    custom_tracker=dict(
        # init args for custom tracker named `custom_tracker`
    ),
)
self.accelerator.init_trackers(self.args.project, config, init_kwargs)

The API for registering custom trackers could be something like:

accelerator.register_custom_tracker(name, CustomTracker)

I have examined the source code, and there seems no obstacle in doing so, as long as the GeneralTracker protocol is obeyed by the custom trackers. To make sure of it, it may be beneficial to mark GeneralTracker as an abstract base class, though.

SunMarc commented 1 month ago

Hi @luowyang, thanks for the detailed request ! What do you think about having an API that register custom tracker @muellerzr ?

muellerzr commented 1 month ago

Sure. I'd be open to this. However we can make name a class attribute (they already are) so the API is just:

accelerator.register_tracker_class(CustomTracker)

How does that sound :)