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

bump python 3.9+ #20413

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
11 changes: 6 additions & 5 deletions .actions/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import shutil
import tempfile
import urllib.request
from collections.abc import Iterable, Iterator, Sequence
from itertools import chain
from os.path import dirname, isfile
from pathlib import Path
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple
from typing import Any, Optional
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the amount of changes and expecting a fair number of merge conflicts, I'd recommend merging all other patches immediately ready first and then push this.


from packaging.requirements import Requirement
from packaging.version import Version
Expand Down Expand Up @@ -127,7 +128,7 @@ def _parse_requirements(lines: Iterable[str]) -> Iterator[_RequirementWithCommen
pip_argument = None


def load_requirements(path_dir: str, file_name: str = "base.txt", unfreeze: str = "all") -> List[str]:
def load_requirements(path_dir: str, file_name: str = "base.txt", unfreeze: str = "all") -> list[str]:
"""Loading requirements from a file.

>>> path_req = os.path.join(_PROJECT_ROOT, "requirements")
Expand Down Expand Up @@ -222,7 +223,7 @@ def _load_aggregate_requirements(req_dir: str = "requirements", freeze_requireme
fp.writelines([ln + os.linesep for ln in requires] + [os.linesep])


def _retrieve_files(directory: str, *ext: str) -> List[str]:
def _retrieve_files(directory: str, *ext: str) -> list[str]:
all_files = []
for root, _, files in os.walk(directory):
for fname in files:
Expand All @@ -232,7 +233,7 @@ def _retrieve_files(directory: str, *ext: str) -> List[str]:
return all_files


def _replace_imports(lines: List[str], mapping: List[Tuple[str, str]], lightning_by: str = "") -> List[str]:
def _replace_imports(lines: list[str], mapping: list[tuple[str, str]], lightning_by: str = "") -> list[str]:
"""Replace imports of standalone package to lightning.

>>> lns = [
Expand Down Expand Up @@ -320,7 +321,7 @@ def copy_replace_imports(
fo.writelines(lines)


def create_mirror_package(source_dir: str, package_mapping: Dict[str, str]) -> None:
def create_mirror_package(source_dir: str, package_mapping: dict[str, str]) -> None:
"""Create a mirror package with adjusted imports."""
# replace imports and copy the code
mapping = package_mapping.copy()
Expand Down
12 changes: 6 additions & 6 deletions .github/checkgroup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ subprojects:
- "pl-cpu (macOS-14, pytorch, 3.9, 2.1)"
- "pl-cpu (ubuntu-20.04, pytorch, 3.9, 2.1)"
- "pl-cpu (windows-2022, pytorch, 3.9, 2.1)"
- "pl-cpu (macOS-13, pytorch, 3.10, 2.1)"
- "pl-cpu (ubuntu-22.04, pytorch, 3.10, 2.1)"
- "pl-cpu (windows-2022, pytorch, 3.10, 2.1)"
- "pl-cpu (macOS-14, pytorch, 3.12, 2.5.1)"
- "pl-cpu (ubuntu-22.04, pytorch, 3.12, 2.5.1)"
- "pl-cpu (windows-2022, pytorch, 3.12, 2.5.1)"

- id: "pytorch_lightning: Azure GPU"
paths:
Expand Down Expand Up @@ -192,9 +192,9 @@ subprojects:
- "fabric-cpu (macOS-14, fabric, 3.9, 2.1)"
- "fabric-cpu (ubuntu-20.04, fabric, 3.9, 2.1)"
- "fabric-cpu (windows-2022, fabric, 3.9, 2.1)"
- "fabric-cpu (macOS-13, fabric, 3.10, 2.1)"
- "fabric-cpu (ubuntu-22.04, fabric, 3.10, 2.1)"
- "fabric-cpu (windows-2022, fabric, 3.10, 2.1)"
- "fabric-cpu (macOS-14, fabric, 3.12, 2.5.1)"
- "fabric-cpu (ubuntu-22.04, fabric, 3.12, 2.5.1)"
- "fabric-cpu (windows-2022, fabric, 3.12, 2.5.1)"

- id: "lightning_fabric: Azure GPU"
paths:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_legacy-checkpoints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
- uses: actions/setup-python@v5
with:
# Python version here needs to be supported by all PL versions listed in back-compatible-versions.txt.
python-version: 3.8
python-version: "3.9"

- name: Install PL from source
env:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci-tests-fabric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ jobs:
- { os: "ubuntu-22.04", pkg-name: "lightning", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "windows-2022", pkg-name: "lightning", python-version: "3.12", pytorch-version: "2.5.1" }
# only run PyTorch latest with Python latest, use Fabric scope to limit dependency issues
- { os: "macOS-13", pkg-name: "fabric", python-version: "3.10", pytorch-version: "2.1" }
- { os: "ubuntu-22.04", pkg-name: "fabric", python-version: "3.10", pytorch-version: "2.1" }
- { os: "windows-2022", pkg-name: "fabric", python-version: "3.10", pytorch-version: "2.1" }
- { os: "macOS-14", pkg-name: "fabric", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "ubuntu-22.04", pkg-name: "fabric", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "windows-2022", pkg-name: "fabric", python-version: "3.12", pytorch-version: "2.5.1" }
# "oldest" versions tests, only on minimum Python
- { os: "macOS-13", pkg-name: "lightning", python-version: "3.9", pytorch-version: "2.1", requires: "oldest" }
- {
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci-tests-pytorch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ jobs:
- { os: "ubuntu-22.04", pkg-name: "lightning", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "windows-2022", pkg-name: "lightning", python-version: "3.12", pytorch-version: "2.5.1" }
# only run PyTorch latest with Python latest, use PyTorch scope to limit dependency issues
- { os: "macOS-13", pkg-name: "pytorch", python-version: "3.10", pytorch-version: "2.1" }
- { os: "ubuntu-22.04", pkg-name: "pytorch", python-version: "3.10", pytorch-version: "2.1" }
- { os: "windows-2022", pkg-name: "pytorch", python-version: "3.10", pytorch-version: "2.1" }
- { os: "macOS-14", pkg-name: "pytorch", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "ubuntu-22.04", pkg-name: "pytorch", python-version: "3.12", pytorch-version: "2.5.1" }
- { os: "windows-2022", pkg-name: "pytorch", python-version: "3.12", pytorch-version: "2.5.1" }
# "oldest" versions tests, only on minimum Python
- { os: "macOS-13", pkg-name: "lightning", python-version: "3.9", pytorch-version: "2.1", requires: "oldest" }
- {
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ repos:
hooks:
# try to fix what is possible
- id: ruff
args: ["--fix"]
args: ["--fix", "--unsafe-fixes"]
# perform formatting updates
- id: ruff-format
# validate if all is fine with preview mode
Expand Down
4 changes: 2 additions & 2 deletions docs/source-pytorch/accelerators/tpu_faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Unsupported datatype transfer to TPUs?

.. code-block::

File "/usr/local/lib/python3.8/dist-packages/torch_xla/utils/utils.py", line 205, in _for_each_instance_rewrite
File "/usr/local/lib/python3.9/dist-packages/torch_xla/utils/utils.py", line 205, in _for_each_instance_rewrite
v = _for_each_instance_rewrite(result.__dict__[k], select_fn, fn, rwmap)
File "/usr/local/lib/python3.8/dist-packages/torch_xla/utils/utils.py", line 206, in _for_each_instance_rewrite
File "/usr/local/lib/python3.9/dist-packages/torch_xla/utils/utils.py", line 206, in _for_each_instance_rewrite
result.__dict__[k] = v
TypeError: 'mappingproxy' object does not support item assignment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Installation
Prerequisites
=============

Python version: 3.8, 3.9, 3.10
Python version: 3.9, 3.10

Install Intel® Neural Compressor
================================
Expand Down
12 changes: 6 additions & 6 deletions examples/fabric/build_your_own_trainer/trainer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from functools import partial
from typing import Any, Iterable, List, Literal, Optional, Tuple, Union, cast
from typing import Any, Literal, Optional, Union, cast

import lightning as L
import torch
Expand All @@ -19,11 +19,11 @@ def __init__(
self,
accelerator: Union[str, Accelerator] = "auto",
strategy: Union[str, Strategy] = "auto",
devices: Union[List[int], str, int] = "auto",
devices: Union[list[int], str, int] = "auto",
precision: Union[str, int] = "32-true",
plugins: Optional[Union[str, Any]] = None,
callbacks: Optional[Union[List[Any], Any]] = None,
loggers: Optional[Union[Logger, List[Logger]]] = None,
callbacks: Optional[Union[list[Any], Any]] = None,
loggers: Optional[Union[Logger, list[Logger]]] = None,
max_epochs: Optional[int] = 1000,
max_steps: Optional[int] = None,
grad_accum_steps: int = 1,
Expand Down Expand Up @@ -465,7 +465,7 @@ def get_latest_checkpoint(checkpoint_dir: str) -> Optional[str]:

def _parse_optimizers_schedulers(
self, configure_optim_output
) -> Tuple[
) -> tuple[
Optional[L.fabric.utilities.types.Optimizable],
Optional[Mapping[str, Union[L.fabric.utilities.types.LRScheduler, bool, str, int]]],
]:
Expand Down
19 changes: 9 additions & 10 deletions examples/fabric/reinforcement_learning/rl/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math
from typing import Dict, Tuple

import gymnasium as gym
import torch
Expand Down Expand Up @@ -43,7 +42,7 @@ def __init__(self, envs: gym.vector.SyncVectorEnv, act_fun: str = "relu", ortho_
layer_init(torch.nn.Linear(64, envs.single_action_space.n), std=0.01, ortho_init=ortho_init),
)

def get_action(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor]:
def get_action(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor]:
logits = self.actor(x)
distribution = Categorical(logits=logits)
if action is None:
Expand All @@ -58,12 +57,12 @@ def get_greedy_action(self, x: Tensor) -> Tensor:
def get_value(self, x: Tensor) -> Tensor:
return self.critic(x)

def get_action_and_value(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
def get_action_and_value(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
action, log_prob, entropy = self.get_action(x, action)
value = self.get_value(x)
return action, log_prob, entropy, value

def forward(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
def forward(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
return self.get_action_and_value(x, action)

@torch.no_grad()
Expand All @@ -77,7 +76,7 @@ def estimate_returns_and_advantages(
num_steps: int,
gamma: float,
gae_lambda: float,
) -> Tuple[Tensor, Tensor]:
) -> tuple[Tensor, Tensor]:
next_value = self.get_value(next_obs).reshape(1, -1)
advantages = torch.zeros_like(rewards)
lastgaelam = 0
Expand Down Expand Up @@ -143,7 +142,7 @@ def __init__(
self.avg_value_loss = MeanMetric(**torchmetrics_kwargs)
self.avg_ent_loss = MeanMetric(**torchmetrics_kwargs)

def get_action(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor]:
def get_action(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor]:
logits = self.actor(x)
distribution = Categorical(logits=logits)
if action is None:
Expand All @@ -158,12 +157,12 @@ def get_greedy_action(self, x: Tensor) -> Tensor:
def get_value(self, x: Tensor) -> Tensor:
return self.critic(x)

def get_action_and_value(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
def get_action_and_value(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
action, log_prob, entropy = self.get_action(x, action)
value = self.get_value(x)
return action, log_prob, entropy, value

def forward(self, x: Tensor, action: Tensor = None) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
def forward(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
return self.get_action_and_value(x, action)

@torch.no_grad()
Expand All @@ -177,7 +176,7 @@ def estimate_returns_and_advantages(
num_steps: int,
gamma: float,
gae_lambda: float,
) -> Tuple[Tensor, Tensor]:
) -> tuple[Tensor, Tensor]:
next_value = self.get_value(next_obs).reshape(1, -1)
advantages = torch.zeros_like(rewards)
lastgaelam = 0
Expand All @@ -193,7 +192,7 @@ def estimate_returns_and_advantages(
returns = advantages + values
return returns, advantages

def training_step(self, batch: Dict[str, Tensor]):
def training_step(self, batch: dict[str, Tensor]):
# Get actions and values given the current observations
_, newlogprob, entropy, newvalue = self(batch["obs"], batch["actions"].long())
logratio = newlogprob - batch["logprobs"]
Expand Down
3 changes: 1 addition & 2 deletions examples/fabric/reinforcement_learning/train_fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import os
import time
from datetime import datetime
from typing import Dict

import gymnasium as gym
import torch
Expand All @@ -38,7 +37,7 @@ def train(
fabric: Fabric,
agent: PPOLightningAgent,
optimizer: torch.optim.Optimizer,
data: Dict[str, Tensor],
data: dict[str, Tensor],
global_step: int,
args: argparse.Namespace,
):
Expand Down
3 changes: 1 addition & 2 deletions examples/fabric/reinforcement_learning/train_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import random
import time
from datetime import datetime
from typing import Dict

import gymnasium as gym
import torch
Expand All @@ -41,7 +40,7 @@
def train(
agent: PPOAgent,
optimizer: torch.optim.Optimizer,
data: Dict[str, Tensor],
data: dict[str, Tensor],
logger: SummaryWriter,
global_step: int,
args: argparse.Namespace,
Expand Down
4 changes: 2 additions & 2 deletions examples/fabric/tensor_parallel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


from dataclasses import dataclass
from typing import Optional, Tuple
from typing import Optional

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -87,7 +87,7 @@ def apply_rotary_emb(
xq: torch.Tensor,
xk: torch.Tensor,
freqs_cis: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
) -> tuple[torch.Tensor, torch.Tensor]:
"""Apply rotary embeddings to input tensors using the given frequency tensor.

This function applies rotary embeddings to the given query 'xq' and key 'xk' tensors using the provided
Expand Down
4 changes: 2 additions & 2 deletions examples/pytorch/basics/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

from os import path
from typing import Optional, Tuple
from typing import Optional

import torch
import torch.nn.functional as F
Expand All @@ -45,7 +45,7 @@ def __init__(
nrow: int = 8,
padding: int = 2,
normalize: bool = True,
value_range: Optional[Tuple[int, int]] = None,
value_range: Optional[tuple[int, int]] = None,
scale_each: bool = False,
pad_value: int = 0,
) -> None:
Expand Down
12 changes: 6 additions & 6 deletions examples/pytorch/domain_templates/reinforce_learn_Qnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import argparse
import random
from collections import OrderedDict, deque, namedtuple
from typing import Iterator, List, Tuple
from collections.abc import Iterator

import gym
import torch
Expand Down Expand Up @@ -102,7 +102,7 @@ def append(self, experience: Experience) -> None:
"""
self.buffer.append(experience)

def sample(self, batch_size: int) -> Tuple:
def sample(self, batch_size: int) -> tuple:
indices = random.sample(range(len(self.buffer)), batch_size)
states, actions, rewards, dones, next_states = zip(*(self.buffer[idx] for idx in indices))

Expand Down Expand Up @@ -190,7 +190,7 @@ def get_action(self, net: nn.Module, epsilon: float, device: str) -> int:
return action

@torch.no_grad()
def play_step(self, net: nn.Module, epsilon: float = 0.0, device: str = "cpu") -> Tuple[float, bool]:
def play_step(self, net: nn.Module, epsilon: float = 0.0, device: str = "cpu") -> tuple[float, bool]:
"""Carries out a single interaction step between the agent and the environment.

Args:
Expand Down Expand Up @@ -295,7 +295,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
return self.net(x)

def dqn_mse_loss(self, batch: Tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor:
def dqn_mse_loss(self, batch: tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor:
"""Calculates the mse loss using a mini batch from the replay buffer.

Args:
Expand All @@ -318,7 +318,7 @@ def dqn_mse_loss(self, batch: Tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor

return nn.MSELoss()(state_action_values, expected_state_action_values)

def training_step(self, batch: Tuple[torch.Tensor, torch.Tensor], nb_batch) -> OrderedDict:
def training_step(self, batch: tuple[torch.Tensor, torch.Tensor], nb_batch) -> OrderedDict:
"""Carries out a single step through the environment to update the replay buffer. Then calculates loss based on
the minibatch received.

Expand Down Expand Up @@ -356,7 +356,7 @@ def training_step(self, batch: Tuple[torch.Tensor, torch.Tensor], nb_batch) -> O

return OrderedDict({"loss": loss, "log": log, "progress_bar": log})

def configure_optimizers(self) -> List[Optimizer]:
def configure_optimizers(self) -> list[Optimizer]:
"""Initialize Adam optimizer."""
optimizer = optim.Adam(self.net.parameters(), lr=self.lr)
return [optimizer]
Expand Down
Loading
Loading