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

[Typing][C-91, C-92] Add annotations for python/paddle/incubate/operators/{softmax_mask_fuse, softmax_mask_fuse_upper_triangle}.py #66867

Merged
merged 8 commits into from
Aug 2, 2024
Merged
12 changes: 10 additions & 2 deletions python/paddle/incubate/operators/softmax_mask_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import TYPE_CHECKING

from paddle import _C_ops
from paddle.base.layer_helper import LayerHelper
from paddle.framework import in_dynamic_or_pir_mode

if TYPE_CHECKING:
from paddle import Tensor


def softmax_mask_fuse(x, mask, name=None):
def softmax_mask_fuse(
x: Tensor, mask: Tensor, name: str | None = None
) -> Tensor:
"""
Do a masked softmax on x.

Expand Down Expand Up @@ -52,7 +60,7 @@ def softmax_mask_fuse(x, mask, name=None):
>>> x = paddle.rand([2, 8, 8, 32])
>>> mask = paddle.rand([2, 1, 8, 32])

>>> rst = incubate.softmax_mask_fuse(x, mask)
>>> rst = incubate.softmax_mask_fuse(x, mask) # type: ignore[operator]
>>> rst.shape
[2, 8, 8, 32]
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import TYPE_CHECKING

from paddle import _C_ops
from paddle.base.layer_helper import LayerHelper
from paddle.framework import in_dynamic_or_pir_mode

if TYPE_CHECKING:
from paddle import Tensor


def softmax_mask_fuse_upper_triangle(x):
def softmax_mask_fuse_upper_triangle(x: Tensor) -> Tensor:
"""
Do a masked softmax on x, which will always mask upper triangle part of x.

Expand Down Expand Up @@ -51,7 +57,7 @@ def softmax_mask_fuse_upper_triangle(x):
>>> paddle.set_device("gpu")
>>> x = paddle.rand((1, 1, 32, 32))

>>> rst = incubate.softmax_mask_fuse_upper_triangle(x)
>>> rst = incubate.softmax_mask_fuse_upper_triangle(x) # type: ignore[operator]
>>> print(rst)
Tensor(shape=[1, 1, 32, 32], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[[[1. , 0. , 0. , ..., 0. ,
Expand Down