Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions paddle/phi/core/compat/convert_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ Backend TransToPhiBackend(const phi::Place& place) {
}
case AllocationType::XPU:
return Backend::XPU;
case AllocationType::XPUPINNED: {
if (FLAGS_pinned_memory_as_cpu_backend) {
return Backend::CPU;
} else {
return Backend::XPU;
}
}
case AllocationType::IPU:
return Backend::IPU;
case AllocationType::UNDEFINED:
Expand Down
6 changes: 6 additions & 0 deletions python/paddle/base/dygraph/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import paddle
from paddle import _C_ops
from paddle.utils.decorator_utils import (
size_args_decorator_patch,
)

from .. import core
from ..framework import convert_np_dtype_to_dtype_
Expand Down Expand Up @@ -312,6 +315,7 @@ def _new_full_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_empty_(
var: Tensor,
size: ShapeLike,
Expand All @@ -334,6 +338,7 @@ def _new_empty_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_ones_(
var: Tensor,
size: ShapeLike,
Expand All @@ -357,6 +362,7 @@ def _new_ones_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_zeros_(
var: Tensor,
size: ShapeLike,
Expand Down
6 changes: 6 additions & 0 deletions python/paddle/pir/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from paddle import _C_ops
from paddle.base.libpaddle import DataType
from paddle.base.wrapped_decorator import wrap_decorator
from paddle.utils.decorator_utils import (
size_args_decorator_patch,
)

from . import Value

Expand Down Expand Up @@ -686,6 +689,7 @@ def _new_full_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_empty_(
self,
size: ShapeLike,
Expand Down Expand Up @@ -731,6 +735,7 @@ def _new_empty_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_ones_(
self,
size: ShapeLike,
Expand Down Expand Up @@ -777,6 +782,7 @@ def _new_ones_(
pin_memory=pin_memory,
)

@size_args_decorator_patch
def _new_zeros_(
self,
size: ShapeLike,
Expand Down
29 changes: 29 additions & 0 deletions python/paddle/utils/decorator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,35 @@ def wrapped_func(*args: Any, **kwargs: Any) -> Any:
return wrapped_func


def size_args_decorator_patch(method: Callable) -> Callable:
"""
A decorator that allow *size for patching method to Tensor.
e.g. Tensor.method(*size, *, ...).

Usage Example:

paddle.randn([]).new_ones(1, dtype=paddle.float32)
paddle.randn([]).new_ones(1, 2, 3, dtype=paddle.float32)
paddle.randn([]).new_ones([1, 2, 3], dtype=paddle.float32)
paddle.randn([]).new_ones(size=[1, 2, 3], dtype=paddle.float32)
paddle.randn([]).new_ones([1, 2, 3], paddle.float32)
"""

@functools.wraps(method)
def wrapped_func(*args: Any, **kwargs: Any) -> Any:
if len(args) >= 2 and isinstance(args[1], int):
# args[0]: Tensor
# args[1:]: *size
kwargs['size'] = list(args[1:])
args = (args[0],)

return method(*args, **kwargs)

wrapped_func.__signature__ = inspect.signature(method)

return wrapped_func


class VariableArgsDecorator(DecoratorBase):
def __init__(self, var: str) -> None:
super().__init__()
Expand Down
Loading
Loading