Skip to content

Add device_name classmethod in Accelerator.#21112

Open
GdoongMathew wants to merge 40 commits into
Lightning-AI:masterfrom
GdoongMathew:feat/device_name
Open

Add device_name classmethod in Accelerator.#21112
GdoongMathew wants to merge 40 commits into
Lightning-AI:masterfrom
GdoongMathew:feat/device_name

Conversation

@GdoongMathew

@GdoongMathew GdoongMathew commented Aug 24, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #17355

  • 1 GPU
GPU available: NVIDIA GeForce RTX 3050 4GB Laptop GPU, using: 1 devices.
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
  • 2 GPUs (Kaggle)
>>> t = Trainer(devices=2)
INFO: GPU available: Tesla T4, using: 2 devices.
INFO: TPU available: False, using: 0 TPU cores
INFO: HPU available: False, using: 0 HPUs
  • No cuda GPU available
GPU available: False, using: 0 devices.
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
  • MPS
GPU available: Apple M1 Pro, using: 1 devices.
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
  • XLA (Kaggle)
GPU available: False, using: 0 devices.
TPU available: v3-8, using: 8 TPU cores
HPU available: False, using: 0 HPUs
Before submitting
  • Was this discussed/agreed 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 minor internal changes/refactors)

PR review

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

Reviewer checklist
  • 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

📚 Documentation preview 📚: https://pytorch-lightning--21112.org.readthedocs.build/en/21112/

@github-actions github-actions Bot added the pl Generic label for PyTorch Lightning package label Aug 24, 2025
@GdoongMathew GdoongMathew changed the title [WIP] Add device_name classmethod in Accelerator. Add device_name classmethod in Accelerator. Aug 26, 2025
Comment thread src/lightning/pytorch/accelerators/cuda.py Outdated
@GdoongMathew

This comment was marked as off-topic.

@GdoongMathew

GdoongMathew commented Aug 27, 2025

Copy link
Copy Markdown
Contributor Author

@justusschock justusschock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How would you handle heterogeneous gpu types? E.g. say I have one 3090 and one 4090 in my workstation. This doesn't handle it at all.

Comment thread src/lightning/pytorch/accelerators/accelerator.py
Comment thread src/lightning/pytorch/accelerators/mps.py
Comment thread src/lightning/pytorch/accelerators/xla.py
Comment thread src/lightning/pytorch/accelerators/cuda.py
@GdoongMathew

Copy link
Copy Markdown
Contributor Author

How would you handle heterogeneous gpu types? E.g. say I have one 3090 and one 4090 in my workstation. This doesn't handle it at all.

Hi @justusschock , based on the change, the output should look something like

GPU available: RTX 3090, RTX 4090, using: 2 devices.

that being said, if the setup is more complex - for example 2 x 3090 and 1 x 4090 - the current output might not fully reflect that.

@GdoongMathew

This comment has been minimized.

@GdoongMathew
GdoongMathew marked this pull request as draft August 28, 2025 12:47
@GdoongMathew

Copy link
Copy Markdown
Contributor Author

Hi @Borda , a friendly ping for a review ~

@Borda

Borda commented Oct 22, 2025

Copy link
Copy Markdown
Collaborator

Hi @Borda , a friendly ping for a review ~

unfortunatly, I am not in a position to approve a feature type PR, lest ping @ethanwharris

# Conflicts:
#	src/lightning/pytorch/accelerators/xla.py
@codecov

codecov Bot commented Nov 4, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.00000% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 79%. Comparing base (7d2de87) to head (ea2767d).
⚠️ Report is 46 commits behind head on master.
✅ All tests successful. No failed tests found.

❗ There is a different number of reports uploaded between BASE (7d2de87) and HEAD (ea2767d). Click for more details.

HEAD has 1312 uploads less than BASE
Flag BASE (7d2de87) HEAD (ea2767d)
cpu 328 33
python 30 3
lightning_fabric 88 0
pytest 163 0
python3.10 30 3
lightning 150 15
python3.13 29 3
python3.12 89 9
python3.11 60 6
python3.12.7 90 9
pytorch2.1 30 6
pytest-full 165 33
pytorch2.3 15 3
pytorch2.5.1 15 3
pytorch2.2.2 15 3
pytorch2.8 30 6
pytorch_lightning 90 18
pytorch2.9 15 3
pytorch2.7 15 3
pytorch2.6 15 3
pytorch2.4.1 15 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #21112     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         270      267      -3     
  Lines       24071    24055     -16     
=========================================
- Hits        20867    18986   -1881     
- Misses       3204     5069   +1865     

# Conflicts:
#	src/lightning/pytorch/CHANGELOG.md
@codecov-commenter

codecov-commenter commented Jul 19, 2026

Copy link
Copy Markdown

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
3339 1 3338 531
View the full list of 1 ❄️ flaky test(s)
tests/tests_pytorch/callbacks/test_stochastic_weight_avg.py::test_swa_callback_ddp_cpu

Flake rate in main: 8.70% (Passed 21 times, Failed 2 times)

Stack Traces | 6.16s run time
tmp_path = PosixPath('.../pytest-of-runner/pytest-0/test_swa_callback_ddp_cpu0')

    @RunIf(skip_windows=True)
    def test_swa_callback_ddp_cpu(tmp_path):
>       train_with_swa(tmp_path, strategy="ddp_spawn", accelerator="cpu", devices=2)

callbacks/test_stochastic_weight_avg.py:197: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
callbacks/test_stochastic_weight_avg.py:179: in train_with_swa
    trainer.fit(model, weights_only=weights_only)
../../.venv/lib/python3.12.../pytorch_lightning/trainer/trainer.py:590: in fit
    call._call_and_handle_interrupt(
../../.venv/lib/python3.12.../pytorch_lightning/trainer/call.py:48: in _call_and_handle_interrupt
    return trainer.strategy.launcher.launch(trainer_fn, *args, trainer=trainer, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../.venv/lib/python3.12.../strategies/launchers/multiprocessing.py:144: in launch
    while not process_context.join():
              ^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <torch.multiprocessing.spawn.ProcessContext object at 0x7ff7a2323b90>
timeout = None, grace_period = None

    def join(self, timeout: float | None = None, grace_period: float | None = None):
        r"""Join one or more processes within spawn context.
    
        Attempt to join one or more processes in this spawn context.
        If one of them exited with a non-zero exit status, this function
        kills the remaining processes (optionally with a grace period)
        and raises an exception with the cause of the first process exiting.
    
        Returns ``True`` if all processes have been joined successfully,
        ``False`` if there are more processes that need to be joined.
    
        Args:
            timeout (float): Wait this long (in seconds) before giving up on waiting.
            grace_period (float): When any processes fail, wait this long (in seconds)
                for others to shutdown gracefully before terminating them. If they
                still don't exit, wait another grace period before killing them.
        """
        # Ensure this function can be called even when we're done.
        if len(self.sentinels) == 0:
            return True
    
        # Wait for any process to fail or all of them to succeed.
        ready = multiprocessing.connection.wait(
            self.sentinels.keys(),
            timeout=timeout,
        )
    
        error_index = None
        for sentinel in ready:
            index = self.sentinels.pop(sentinel)
            process = self.processes[index]
            process.join()
            if process.exitcode != 0:
                error_index = index
                break
    
        # Return if there was no error.
        if error_index is None:
            # Return whether or not all processes have been joined.
            return len(self.sentinels) == 0
        # An error occurred. Clean-up all processes before returning.
        # First, allow a grace period for processes to shutdown themselves.
        if grace_period is not None:
            self._join_procs_with_timeout(grace_period)
        # Then, terminate processes that are still alive. Try SIGTERM first.
        for process in self.processes:
            if process.is_alive():
                log.warning("Terminating process %s via signal SIGTERM", process.pid)
                process.terminate()
    
        # Try SIGKILL if the process isn't going down after another grace_period.
        # The reason is related to python signal handling is limited
        # to main thread and if that is in c/c++ land and stuck it won't
        # to handle it. We have seen processes getting stuck not handling
        # SIGTERM for the above reason.
        self._join_procs_with_timeout(30 if grace_period is None else grace_period)
        for process in self.processes:
            if process.is_alive():
                log.warning(
                    "Unable to shutdown process %s via SIGTERM , forcefully exiting via SIGKILL",
                    process.pid,
                )
                process.kill()
            process.join()
    
        # The file will only be created if the process crashed.
        failed_process = self.processes[error_index]
        if not os.access(self.error_files[error_index], os.R_OK):
            exitcode = self.processes[error_index].exitcode
            if exitcode < 0:
                try:
                    name = signal.Signals(-exitcode).name
                except ValueError:
                    name = f"<Unknown signal {-exitcode}>"
>               raise ProcessExitedException(
                    f"process {error_index:d} terminated with signal {name}",
                    error_index=error_index,
                    error_pid=failed_process.pid,
                    exit_code=exitcode,
                    signal_name=name,
                )
E               torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGABRT

../../.venv/lib/python3.12.../torch/multiprocessing/spawn.py:192: ProcessExitedException

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accelerator has conflicts pl Generic label for PyTorch Lightning package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add device information to the accelerator config message

5 participants