Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AcceleratorRegistry #12180

Merged
merged 21 commits into from
Mar 24, 2022
Merged

Add AcceleratorRegistry #12180

merged 21 commits into from
Mar 24, 2022

Conversation

kaushikb11
Copy link
Contributor

@kaushikb11 kaushikb11 commented Mar 2, 2022

What does this PR do?

Follow up to #12030

accelerator_name = "custom_accelerator"

class TestAccelerator(Accelerator):
    @staticmethod
    def parse_devices(devices):
        return devices

    @staticmethod
    def get_parallel_devices(devices):
        return ["foo"] * devices

    @staticmethod
    def auto_device_count():
        return 3

    @staticmethod
    def is_available():
        return True

AcceleratorRegistry.register(
    accelerator_name, CustomAccelerator, description=accelerator_description, param1="abc", param2=123
)

assert accelerator_name in AcceleratorRegistry
assert isinstance(AcceleratorRegistry.get(accelerator_name), TestAccelerator)

trainer = Trainer(accelerator="custom_accelerator", devices="auto")
assert isinstance(trainer.accelerator, TestAccelerator)
assert trainer._accelerator_connector.parallel_devices == ["foo"] * 3

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda @akihironitta @rohitgr7 @justusschock

@kaushikb11 kaushikb11 self-assigned this Mar 2, 2022
@kaushikb11 kaushikb11 added this to the 1.6 milestone Mar 2, 2022
@kaushikb11 kaushikb11 marked this pull request as ready for review March 2, 2022 12:29
@kaushikb11 kaushikb11 changed the title Add AcceleratorRegistry Add AcceleratorRegistry Mar 2, 2022
@rohitgr7 rohitgr7 mentioned this pull request Mar 2, 2022
12 tasks
Copy link
Contributor

@four4fish four4fish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! Provides more flexibilities for customized accelerator and it's consistent with Strategy behavior.

Do you think we can remove AcceleratorType after this PR?

pytorch_lightning/accelerators/registry.py Show resolved Hide resolved
@mergify mergify bot added the has conflicts label Mar 9, 2022
@mergify mergify bot removed the has conflicts label Mar 21, 2022
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
pytorch_lightning/accelerators/registry.py Outdated Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Mar 23, 2022
@kaushikb11 kaushikb11 enabled auto-merge (squash) March 24, 2022 15:22
@kaushikb11 kaushikb11 marked this pull request as draft March 24, 2022 17:14
auto-merge was automatically disabled March 24, 2022 17:14

Pull request was converted to draft

@kaushikb11 kaushikb11 marked this pull request as ready for review March 24, 2022 17:14
@kaushikb11 kaushikb11 enabled auto-merge (squash) March 24, 2022 17:31
@codecov
Copy link

codecov bot commented Mar 24, 2022

Codecov Report

Merging #12180 (096afb9) into master (c099c8b) will decrease coverage by 0%.
The diff coverage is 89%.

@@           Coverage Diff           @@
##           master   #12180   +/-   ##
=======================================
- Coverage      88%      88%   -0%     
=======================================
  Files         207      209    +2     
  Lines       17704    17747   +43     
=======================================
+ Hits        15524    15561   +37     
- Misses       2180     2186    +6     

@kaushikb11 kaushikb11 merged commit dcc973e into master Mar 24, 2022
@kaushikb11 kaushikb11 deleted the add/acc_registry branch March 24, 2022 18:29
accelerator_registry.register(
"gpu",
cls,
description=f"{cls.__class__.__name__}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__class__.__name__ is already a string so the fstring is redundant.

Comment on lines +37 to +40
@AcceleratorRegistry.register("sota", description="Custom sota accelerator", a=1, b=True)
class SOTAAccelerator(Accelerator):
def __init__(self, a, b):
...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work:

Traceback (most recent call last):
  File "/Users/carmocca/git/pytorch-lightning/thing.py", line 5, in <module>
    class SOTAAccelerator:
TypeError: do_register() missing 1 required positional argument: 'accelerator'

@@ -75,7 +75,6 @@ def auto_device_count() -> int:
def is_available() -> bool:
"""Detect if the hardware is available."""

@staticmethod
@abstractmethod
def name() -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the reasoning for removing the name.

If we kept it, the registry could use it to automatically define the name for the class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related discussion #12180 (comment)

from typing import Any


def _is_register_method_overridden(mod: type, base_cls: Any, method: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use is_overridden?

def register(
self,
name: str,
accelerator: Optional[Callable] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should be Optional[Type], not Callable

from pytorch_lightning.accelerators.tpu import TPUAccelerator # noqa: F401

ACCELERATORS_BASE_MODULE = "pytorch_lightning.accelerators"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why expose this string?

If it was to avoid registering these by default, it would not work because to change the path you'd need to import the variable

And at import time, they would get registered anyways.

"""Name of the Accelerator."""
return "cpu"
@classmethod
def register_accelerators(cls, accelerator_registry: Dict) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument should be typed as _AcceleratorRegistry, not Dict

def name() -> str:
"""Name of the Accelerator."""
@classmethod
def register_accelerators(cls, accelerator_registry: Dict) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing is wrong. should be the actual accelerator registry

Comment on lines +27 to +29
an accelerator, e.g., "gpu". It also returns Optional description and
parameters to initialize the Accelerator, which were defined during the
registration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... an optional description ...

registration.

The motivation for having a AcceleratorRegistry is to make it convenient
for the Users to try different accelerators by passing mapped aliases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

users capitalized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accelerator ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants