Skip to content

Commit

Permalink
[Typing][PEP585 Upgrade][BUAA][61-70] Use standard collections for ty…
Browse files Browse the repository at this point in the history
…pe hints for 10 files in `python/paddle/` (PaddlePaddle#67131)


---------

Co-authored-by: Nyakku Shigure <[email protected]>
  • Loading branch information
2 people authored and Jeff114514 committed Aug 14, 2024
1 parent 0eff652 commit 4e2ad65
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
4 changes: 3 additions & 1 deletion python/paddle/incubate/autograd/primapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

import logging
import typing
from typing import TYPE_CHECKING, Sequence, TypeVar
from typing import TYPE_CHECKING, TypeVar

import paddle
from paddle.base import backward, core, framework
from paddle.base.core import prim_config
from paddle.incubate.autograd import primx, utils

if TYPE_CHECKING:
from collections.abc import Sequence

from paddle import Tensor
from paddle.base.framework import Block

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/incubate/autograd/primx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import logging
import typing
from collections import OrderedDict
from typing import TYPE_CHECKING, Sequence, TypeVar
from typing import TYPE_CHECKING, TypeVar

import paddle
from paddle.base import framework
Expand All @@ -41,6 +41,8 @@
)

if TYPE_CHECKING:
from collections.abc import Sequence

from paddle import Tensor
from paddle.base.framework import Block

Expand Down
5 changes: 4 additions & 1 deletion python/paddle/incubate/framework/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Literal, Protocol, Sequence, overload
from typing import TYPE_CHECKING, overload

import paddle
from paddle import base
from paddle.base import core

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Literal, Protocol

from paddle._typing import PlaceLike

class _GeneratorState(Protocol):
Expand Down
13 changes: 9 additions & 4 deletions python/paddle/incubate/nn/attn_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List, Optional, Sequence
from typing import TYPE_CHECKING

import paddle

if TYPE_CHECKING:
from collections.abc import Sequence


class AttentionBias(ABC):
@abstractmethod
Expand Down Expand Up @@ -57,7 +62,7 @@ def materialize(self, shape, dtype=paddle.float32):
class SeqLenInfo:
seqstart: paddle.Tensor
max_seqlen: int
seqstart_py: List[int]
seqstart_py: list[int]

def intervals(self):
yield from zip(self.seqstart_py, self.seqstart_py[1:])
Expand Down Expand Up @@ -126,7 +131,7 @@ def split(self, x, batch_sizes=None):
class BlockDiagonalMask(AttentionBias):
q_seqinfo: SeqLenInfo
k_seqinfo: SeqLenInfo
_batch_sizes: Optional[Sequence[int]] = None
_batch_sizes: Sequence[int] | None = None

def _create_block_mask(self, shape, dtype=paddle.float32):
return paddle.zeros(shape=shape, dtype=dtype)
Expand Down Expand Up @@ -229,7 +234,7 @@ def _create_block_mask(self, shape, dtype=paddle.float32):
class BlockDiagonalCausalWithOffsetPaddedKeysMask(AttentionBias):
q_seqinfo: SeqLenInfo
k_seqinfo: PaddedSeqLenInfo
causal_diagonal: Optional[paddle.Tensor] = None
causal_diagonal: paddle.Tensor | None = None

def _create_block_mask(self, shape, offset=0, dtype=paddle.float32):
create_as = dtype if dtype is not paddle.bfloat16 else paddle.float32
Expand Down
6 changes: 2 additions & 4 deletions python/paddle/io/dataloader/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
TYPE_CHECKING,
Any,
Callable,
Generator,
Generic,
Iterable,
Iterator,
Sequence,
Tuple,
TypeVar,
)
Expand All @@ -37,6 +33,8 @@
from ... import framework

if TYPE_CHECKING:
from collections.abc import Generator, Iterable, Iterator, Sequence

from paddle import Tensor

_T = TypeVar('_T')
Expand Down
6 changes: 2 additions & 4 deletions python/paddle/io/dataloader/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
from typing import (
TYPE_CHECKING,
Any,
Generator,
Generic,
Iterator,
Sequence,
Sized,
TypeVar,
)

Expand All @@ -31,6 +27,8 @@
from ...tensor import randperm

if TYPE_CHECKING:
from collections.abc import Generator, Iterator, Sequence, Sized

import numpy.typing as npt

from paddle import Tensor
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
Any,
AnyStr,
Callable,
Mapping,
Protocol,
Sequence,
TypeVar,
overload,
)
Expand All @@ -49,6 +47,7 @@

if TYPE_CHECKING:
import numbers
from collections.abc import Mapping, Sequence

import numpy.typing as npt

Expand Down

0 comments on commit 4e2ad65

Please sign in to comment.