zlq
into master
1 year ago
@@ -111,7 +111,7 @@ from mindspore import context | |||||
import numpy as np | import numpy as np | ||||
import mindspore as ms | import mindspore as ms | ||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
def test_linear_model(): | def test_linear_model(): | ||||
@@ -0,0 +1,48 @@ | |||||
#!/usr/bin/env python | |||||
# -*- coding: utf-8 -*- | |||||
from mindspore.ops import constexpr | |||||
from ms_adapter.pytorch.tensor import cast_to_adapter_tensor | |||||
from ms_adapter.utils import pynative_mode_condition, graph_mode_condition | |||||
def _out_limit_pynative(out, op_name): | |||||
if out is not None and graph_mode_condition(): # TODO: ms_function | |||||
raise ValueError('In MindSpore static graph mode, `out` in `{}` shoud be None, ' | |||||
'please set out=None and use return value instead of `out`.'.format(op_name)) | |||||
def _out_inplace_assign(out, output, op_name): | |||||
if out is None: | |||||
return cast_to_adapter_tensor(output) | |||||
if pynative_mode_condition(): # TODO: ms_function | |||||
out.assign_value(output) | |||||
return out | |||||
raise ValueError('In MindSpore static graph mode, `out` in `{}` shoud be None, ' | |||||
'please set out=None and use return value instead of `out`.'.format(op_name)) | |||||
def _inplace_assign_pynative(input, inplace, output, op_name): | |||||
if inplace is True: | |||||
if pynative_mode_condition(): # TODO: ms_function | |||||
input.assign_value(output) | |||||
return input | |||||
raise ValueError('In MindSpore static graph mode, `inplace` in `{}` shoud not be Ture, ' | |||||
'please set inplace=False and use return value instead of `input`.'.format(op_name)) | |||||
return cast_to_adapter_tensor(output) | |||||
@constexpr | |||||
def _inplace_limit_pynative(inplace, op_name): | |||||
if inplace is True and graph_mode_condition(): # TODO: ms_function | |||||
raise ValueError('In MindSpore static graph mode, `inplace` in `{}` shoud not be Ture, ' | |||||
'please set inplace=False and use return value instead of `input`.'.format(op_name)) | |||||
def _inplace_assign(input, inplace, output): | |||||
if inplace is True: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) |
@@ -4,7 +4,7 @@ | |||||
import mindspore as ms | import mindspore as ms | ||||
from mindspore.common import dtype as mstype | from mindspore.common import dtype as mstype | ||||
from ms_adapter.utils import unsupported_attr | from ms_adapter.utils import unsupported_attr | ||||
from ms_adapter.pytorch.tensor import cast_to_adapter_tensor | |||||
from ms_adapter.pytorch.common._inner import _out_inplace_assign | |||||
def range(start, end, step=1, out=None, dtype=None, layout=None, device=None, requires_grad=False): | def range(start, end, step=1, out=None, dtype=None, layout=None, device=None, requires_grad=False): | ||||
@@ -20,10 +20,7 @@ def range(start, end, step=1, out=None, dtype=None, layout=None, device=None, re | |||||
# which produces values in [start, end). | # which produces values in [start, end). | ||||
step = ms.Tensor(step, dtype=dtype) | step = ms.Tensor(step, dtype=dtype) | ||||
output = ms.ops.range(start, end, step) | output = ms.ops.range(start, end, step) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "range") | |||||
def arange(start, end, step=1, *, out=None, dtype=None, | def arange(start, end, step=1, *, out=None, dtype=None, | ||||
@@ -46,7 +43,4 @@ def arange(start, end, step=1, *, out=None, dtype=None, | |||||
step = ms.Tensor(step, dtype) | step = ms.Tensor(step, dtype) | ||||
output = ms.ops.range(start=start, limit=end, delta=step) | output = ms.ops.range(start=start, limit=end, delta=step) | ||||
if out is not None: | |||||
ms.ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arange") |
@@ -11,10 +11,12 @@ from mindspore import ops | |||||
from mindspore.common import dtype as mstype | from mindspore.common import dtype as mstype | ||||
from mindspore.ops import constexpr | from mindspore.ops import constexpr | ||||
from ms_adapter.pytorch.tensor import tensor, cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | from ms_adapter.pytorch.tensor import tensor, cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | ||||
from ms_adapter.utils import unsupported_attr, get_backend | |||||
from ms_adapter.utils import unsupported_attr, get_backend, pynative_mode_condition | |||||
from ms_adapter.pytorch.tensor import Tensor as adapter_tensor | from ms_adapter.pytorch.tensor import Tensor as adapter_tensor | ||||
from ms_adapter.pytorch.common._inner import _out_inplace_assign, _out_limit_pynative | |||||
from ms_adapter.pytorch.common.dtype import _TypeDict | from ms_adapter.pytorch.common.dtype import _TypeDict | ||||
def empty(*size, out=None, dtype=None, layout=None, \ | def empty(*size, out=None, dtype=None, layout=None, \ | ||||
device=None, requires_grad=False, pin_memory=False, \ | device=None, requires_grad=False, pin_memory=False, \ | ||||
memory_format=None): | memory_format=None): | ||||
@@ -30,10 +32,7 @@ def empty(*size, out=None, dtype=None, layout=None, \ | |||||
if isinstance(size[0], (tuple, list)): | if isinstance(size[0], (tuple, list)): | ||||
_size = size[0] | _size = size[0] | ||||
output = ms.numpy.empty(_size, dtype) | output = ms.numpy.empty(_size, dtype) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "empty") | |||||
def eye(n, m=None, *, out=None, dtype=None, layout=None, \ | def eye(n, m=None, *, out=None, dtype=None, layout=None, \ | ||||
@@ -48,10 +47,7 @@ def eye(n, m=None, *, out=None, dtype=None, layout=None, \ | |||||
dtype = ms.float32 | dtype = ms.float32 | ||||
output = ms.ops.eye(n, m, dtype) | output = ms.ops.eye(n, m, dtype) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "eye") | |||||
def cat(tensors, dim=0, *, out=None): | def cat(tensors, dim=0, *, out=None): | ||||
@@ -63,13 +59,18 @@ def cat(tensors, dim=0, *, out=None): | |||||
inputs = cast_to_ms_tensor(tensors) | inputs = cast_to_ms_tensor(tensors) | ||||
output = ops.concat(inputs, dim) | output = ops.concat(inputs, dim) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "cat") | |||||
def concat(tensors, dim=0, *, out=None): | def concat(tensors, dim=0, *, out=None): | ||||
return cat(tensors, dim, out=out) | |||||
if tensors is None: | |||||
raise ValueError('`tensors` in `{}` should not be None'.format(concat.__name__)) | |||||
if not isinstance(tensors, (tuple, list)): | |||||
raise TypeError('`tensors` in `{}` should be tuple or list'.format(concat.__name__)) | |||||
inputs = cast_to_ms_tensor(tensors) | |||||
output = ops.concat(inputs, dim) | |||||
return _out_inplace_assign(out, output, "concat") | |||||
def ones(*size, out=None, dtype=None, layout=None, | def ones(*size, out=None, dtype=None, layout=None, | ||||
device=None, requires_grad=False): | device=None, requires_grad=False): | ||||
@@ -84,21 +85,13 @@ def ones(*size, out=None, dtype=None, layout=None, | |||||
output = ms.ops.ones(*size, type=dtype) | output = ms.ops.ones(*size, type=dtype) | ||||
else: | else: | ||||
output = ms.ops.ones(size, type=dtype) | output = ms.ops.ones(size, type=dtype) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "ones") | |||||
def stack(tensors, dim = 0, *, out=None): | def stack(tensors, dim = 0, *, out=None): | ||||
tensors = cast_to_ms_tensor(tensors) | tensors = cast_to_ms_tensor(tensors) | ||||
output = ops.stack(tensors, dim) | output = ops.stack(tensors, dim) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "stack") | |||||
def meshgrid(*tensors, indexing='ij'): | def meshgrid(*tensors, indexing='ij'): | ||||
@@ -116,10 +109,7 @@ def meshgrid(*tensors, indexing='ij'): | |||||
def log(input, *, out=None): | def log(input, *, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ops.log(input) | output = ops.log(input) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "log") | |||||
def chunk(input, chunks, dim=0): | def chunk(input, chunks, dim=0): | ||||
@@ -141,10 +131,7 @@ def diag(input, diagonal=0, *, out=None): | |||||
# May be use mindspore.ops.diag instead. Nowadays, this operator do not support CPU. | # May be use mindspore.ops.diag instead. Nowadays, this operator do not support CPU. | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.numpy.diag(input, diagonal) | output = ms.numpy.diag(input, diagonal) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "diag") | |||||
def sqrt(input, *, out=None): | def sqrt(input, *, out=None): | ||||
@@ -153,10 +140,7 @@ def sqrt(input, *, out=None): | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ops.sqrt(input) | output = ops.sqrt(input) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "sqrt") | |||||
def mm(input, mat2, *, out=None): | def mm(input, mat2, *, out=None): | ||||
@@ -168,10 +152,7 @@ def mm(input, mat2, *, out=None): | |||||
input2 = cast_to_ms_tensor(mat2) | input2 = cast_to_ms_tensor(mat2) | ||||
output = ops.matmul(input1, input2) | output = ops.matmul(input1, input2) | ||||
output = ops.cast(output, output_type) | output = ops.cast(output, output_type) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "mm") | |||||
def zeros(*size, out=None, dtype=None, device=None, requires_grad=False): | def zeros(*size, out=None, dtype=None, device=None, requires_grad=False): | ||||
@@ -189,11 +170,7 @@ def zeros(*size, out=None, dtype=None, device=None, requires_grad=False): | |||||
dtype = mstype.float32 | dtype = mstype.float32 | ||||
output = ms.ops.zeros(size, dtype) | output = ms.ops.zeros(size, dtype) | ||||
if out is not None: | |||||
out.assign_value(output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "zeros") | |||||
def div(input, other, *, rounding_mode=None, out=None): | def div(input, other, *, rounding_mode=None, out=None): | ||||
@@ -217,10 +194,7 @@ def div(input, other, *, rounding_mode=None, out=None): | |||||
output = ms.ops.floor_div(input, other) | output = ms.ops.floor_div(input, other) | ||||
output = ms.ops.cast(output, input_dtype) | output = ms.ops.cast(output, input_dtype) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "div") | |||||
def flatten(input, start_dim=0, end_dim=-1): | def flatten(input, start_dim=0, end_dim=-1): | ||||
@@ -332,10 +306,7 @@ def multinomial(input, num_samples, replacement=False, *, generator=None, out=No | |||||
warnings.warn("torch.multinomal don't support generator now.") | warnings.warn("torch.multinomal don't support generator now.") | ||||
input_tensor = cast_to_ms_tensor(input).astype(mstype.float32) | input_tensor = cast_to_ms_tensor(input).astype(mstype.float32) | ||||
output = ms.ops.multinomial(input_tensor, num_samples, replacement) | output = ms.ops.multinomial(input_tensor, num_samples, replacement) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "multinomial") | |||||
def randperm(n, *, generator=None, out=None, dtype=mstype.int64, layout=None, device=None, | def randperm(n, *, generator=None, out=None, dtype=mstype.int64, layout=None, device=None, | ||||
@@ -353,9 +324,7 @@ def randperm(n, *, generator=None, out=None, dtype=mstype.int64, layout=None, de | |||||
output = np.random.permutation(n) | output = np.random.permutation(n) | ||||
output = tensor(output, dtype=dtype) | output = tensor(output, dtype=dtype) | ||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "randperm") | |||||
def randint(low, high, size, *, generator=None, out=None, dtype=None, layout=None, | def randint(low, high, size, *, generator=None, out=None, dtype=None, layout=None, | ||||
@@ -372,9 +341,7 @@ def randint(low, high, size, *, generator=None, out=None, dtype=None, layout=Non | |||||
output = np.random.randint(low, high, size) | output = np.random.randint(low, high, size) | ||||
output = tensor(output, dtype=dtype) | output = tensor(output, dtype=dtype) | ||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "randint") | |||||
def as_tensor(data, dtype=None, device=None): | def as_tensor(data, dtype=None, device=None): | ||||
@@ -408,7 +375,6 @@ def ones_like(input, dtype=None, layout=None, device=None, requires_grad=False, | |||||
def empty_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=None): | def empty_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=None): | ||||
unsupported_attr(layout) | unsupported_attr(layout) | ||||
unsupported_attr(device) | unsupported_attr(device) | ||||
@@ -425,10 +391,7 @@ def full(size, fill_value, out=None, dtype=None, layout=None, device=None, requi | |||||
unsupported_attr(device) | unsupported_attr(device) | ||||
unsupported_attr(requires_grad) | unsupported_attr(requires_grad) | ||||
output = ms.numpy.full(size, fill_value, dtype) | output = ms.numpy.full(size, fill_value, dtype) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "full") | |||||
def full_like(input, fill_value, dtype=None, layout=None, device=None, requires_grad=False, memory_format=None): | def full_like(input, fill_value, dtype=None, layout=None, device=None, requires_grad=False, memory_format=None): | ||||
@@ -466,10 +429,7 @@ def rand(*size, out=None, dtype=None, layout=None, device=None, requires_grad=Fa | |||||
if dtype is None: | if dtype is None: | ||||
dtype = ms.float32 | dtype = ms.float32 | ||||
output = ms.numpy.rand(*size, dtype=dtype) | output = ms.numpy.rand(*size, dtype=dtype) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "rand") | |||||
def linspace(start, end, steps, out=None, dtype=None, device=None, requires_grad=False): | def linspace(start, end, steps, out=None, dtype=None, device=None, requires_grad=False): | ||||
@@ -480,10 +440,7 @@ def linspace(start, end, steps, out=None, dtype=None, device=None, requires_grad | |||||
start = ms.Tensor(start, dtype) | start = ms.Tensor(start, dtype) | ||||
end = ms.Tensor(end, dtype) | end = ms.Tensor(end, dtype) | ||||
output = ms.ops.linspace(start, end, steps) | output = ms.ops.linspace(start, end, steps) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "linspace") | |||||
def take(input, index): | def take(input, index): | ||||
@@ -497,40 +454,34 @@ def take(input, index): | |||||
def abs(input, out=None): | def abs(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.numpy.abs(input) | output = ms.numpy.abs(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "abs") | |||||
def atan2(input, other, out=None): | def atan2(input, other, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.atan2(input, other) | output = ms.ops.atan2(input, other) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "atan2") | |||||
def clamp(input, min=None, max=None, out=None): | def clamp(input, min=None, max=None, out=None): | ||||
input = cast_to_ms_tensor(input) | |||||
min = ms.Tensor(min, ms.float32) | |||||
max = ms.Tensor(max, ms.float32) | |||||
output = ms.ops.clip_by_value(input, min, max) | |||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
input_ms = cast_to_ms_tensor(input) | |||||
type = input_ms.dtype | |||||
if min is not None and max is not None and min > max: | |||||
output = ms.ops.ones_like(input_ms).astype(type)*max | |||||
else: | |||||
if min is not None: | |||||
min = ms.Tensor(min, type) | |||||
if max is not None: | |||||
max = ms.Tensor(max, type) | |||||
output = ms.ops.clip_by_value(input_ms, min, max) | |||||
return _out_inplace_assign(out, output, "clamp") | |||||
def cos(input, out=None): | def cos(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.cos(input) | output = ms.ops.cos(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "cos") | |||||
class Device(): | class Device(): | ||||
@@ -575,10 +526,7 @@ def fmod(input, other, out=None): | |||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
other = ms.numpy.array(other) | other = ms.numpy.array(other) | ||||
output = ms.numpy.fmod(input, other) | output = ms.numpy.fmod(input, other) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "fmod") | |||||
def frac(input, out=None): | def frac(input, out=None): | ||||
@@ -586,49 +534,34 @@ def frac(input, out=None): | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
output = input - ms.numpy.floor(ms.numpy.abs(input)) * ms.numpy.sign(input) | output = input - ms.numpy.floor(ms.numpy.abs(input)) * ms.numpy.sign(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "frac") | |||||
def log10(input, out=None): | def log10(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
output = ms.numpy.log10(input) | output = ms.numpy.log10(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "log10") | |||||
def log1p(input, out=None): | def log1p(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
output = ms.numpy.log1p(input) | output = ms.numpy.log1p(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "log1p") | |||||
def log2(input, out=None): | def log2(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
output = ms.numpy.log2(input) | output = ms.numpy.log2(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "log2") | |||||
def sin(input, out=None): | def sin(input, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.sin(input) | output = ms.ops.sin(input) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "sin") | |||||
def max(input, dim=None, keepdim=False, *, out=None): | def max(input, dim=None, keepdim=False, *, out=None): | ||||
@@ -648,8 +581,15 @@ def max(input, dim=None, keepdim=False, *, out=None): | |||||
point = collections.namedtuple('max', 'values,indices') | point = collections.namedtuple('max', 'values,indices') | ||||
rlt = point(cast_to_adapter_tensor(value), cast_to_adapter_tensor(indice)) | rlt = point(cast_to_adapter_tensor(value), cast_to_adapter_tensor(indice)) | ||||
if out is not None: | if out is not None: | ||||
ops.assign(out, rlt) | |||||
return out | |||||
if pynative_mode_condition(): | |||||
if len(out) != 2 or not isinstance(out[0], adapter_tensor) or not isinstance(out[1], adapter_tensor): | |||||
raise TypeError("In ms_adapter.torch.max(), `out` should be tuple of Tensors.") | |||||
out[0].assign_value(value) | |||||
out[1].assign_value(indice) | |||||
return out | |||||
else: | |||||
raise ValueError('In MindSpore static graph mode, `out` in `max` shoud be None, ' | |||||
'please set out=None and use return value instead of `out`.') | |||||
return rlt | return rlt | ||||
@@ -661,12 +601,15 @@ def min(input, dim=None, keepdim=False, *, out=None): | |||||
indices, result = ms.ops.min(input, axis=dim, keep_dims=keepdim) | indices, result = ms.ops.min(input, axis=dim, keep_dims=keepdim) | ||||
if out is not None: | if out is not None: | ||||
if len(out) != 2 or not isinstance(out[0], adapter_tensor) or not isinstance(out[1], adapter_tensor): | |||||
raise TypeError("In ms_adapter.torch.min(), `out` should be tuple of Tensors.") | |||||
out[0].assign_value(result) | |||||
out[1].assign_value(indices) | |||||
return out | |||||
if pynative_mode_condition(): | |||||
if len(out) != 2 or not isinstance(out[0], adapter_tensor) or not isinstance(out[1], adapter_tensor): | |||||
raise TypeError("In ms_adapter.torch.min(), `out` should be tuple of Tensors.") | |||||
out[0].assign_value(result) | |||||
out[1].assign_value(indices) | |||||
return out | |||||
else: | |||||
raise ValueError('In MindSpore static graph mode, `out` in `min` shoud be None, ' | |||||
'please set out=None and use return value instead of `out`.') | |||||
return cast_to_adapter_tensor(result), cast_to_adapter_tensor(indices) | return cast_to_adapter_tensor(result), cast_to_adapter_tensor(indices) | ||||
@@ -679,10 +622,7 @@ def mean(input, dim=None, keepdim=False, *, dtype=None, out=None): | |||||
output = ms.ops.mean(input, axis=dim, keep_dims=keepdim) | output = ms.ops.mean(input, axis=dim, keep_dims=keepdim) | ||||
else: | else: | ||||
output = ms.ops.mean(input, keep_dims=keepdim) | output = ms.ops.mean(input, keep_dims=keepdim) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "mean") | |||||
def round(input, *, decimals=0, out=None): | def round(input, *, decimals=0, out=None): | ||||
@@ -693,37 +633,25 @@ def round(input, *, decimals=0, out=None): | |||||
p = 10**decimals | p = 10**decimals | ||||
input = input*p | input = input*p | ||||
output = ms.ops.round(input)/p | output = ms.ops.round(input)/p | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "round") | |||||
def floor(input, *, out=None): | def floor(input, *, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.floor(input) | output = ms.ops.floor(input) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "floor") | |||||
def ceil(input, *, out=None): | def ceil(input, *, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.ceil(input) | output = ms.ops.ceil(input) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "ceil") | |||||
def sign(input, *, out=None): | def sign(input, *, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.Sign()(input) | output = ms.ops.Sign()(input) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "sign") | |||||
def pow(input, exponent, *, out=None): | def pow(input, exponent, *, out=None): | ||||
@@ -733,10 +661,7 @@ def pow(input, exponent, *, out=None): | |||||
exponent = cast_to_ms_tensor(exponent) | exponent = cast_to_ms_tensor(exponent) | ||||
output = ms.ops.pow(input, exponent) | output = ms.ops.pow(input, exponent) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "pow") | |||||
def exp(input, *, out=None): | def exp(input, *, out=None): | ||||
@@ -749,10 +674,7 @@ def exp(input, *, out=None): | |||||
output = ms.ops.exp(input) | output = ms.ops.exp(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "exp") | |||||
def ge(input, other, *, out=None): | def ge(input, other, *, out=None): | ||||
@@ -760,10 +682,7 @@ def ge(input, other, *, out=None): | |||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.ge(input, other) | output = ms.ops.ge(input, other) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "ge") | |||||
def gt(input, other, *, out=None): | def gt(input, other, *, out=None): | ||||
@@ -771,10 +690,7 @@ def gt(input, other, *, out=None): | |||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.gt(input, other) | output = ms.ops.gt(input, other) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "gt") | |||||
def le(input, other, *, out=None): | def le(input, other, *, out=None): | ||||
@@ -782,10 +698,7 @@ def le(input, other, *, out=None): | |||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.le(input, other) | output = ms.ops.le(input, other) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "le") | |||||
def lt(input, other, *, out=None): | def lt(input, other, *, out=None): | ||||
@@ -794,10 +707,7 @@ def lt(input, other, *, out=None): | |||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = 1 - ms.ops.ge(input, other) | output = 1 - ms.ops.ge(input, other) | ||||
output = output.astype(ms.bool_) | output = output.astype(ms.bool_) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "lt") | |||||
def sum(input, dim=None, keepdim=False, *, dtype=None, out=None): | def sum(input, dim=None, keepdim=False, *, dtype=None, out=None): | ||||
@@ -805,19 +715,13 @@ def sum(input, dim=None, keepdim=False, *, dtype=None, out=None): | |||||
if dtype is not None: | if dtype is not None: | ||||
input = input.astype(dtype) | input = input.astype(dtype) | ||||
output = input.sum(axis=dim, keepdims=keepdim) | output = input.sum(axis=dim, keepdims=keepdim) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "sum") | |||||
def median(input, dim=None, keepdim=False, *, out=None): | def median(input, dim=None, keepdim=False, *, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if dim is None: | if dim is None: | ||||
output, _ = ms.ops.median(input, global_median=True, keep_dims=keepdim) | output, _ = ms.ops.median(input, global_median=True, keep_dims=keepdim) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
else: | else: | ||||
output = list(ms.ops.median(input, axis=dim, keep_dims=keepdim)) | output = list(ms.ops.median(input, axis=dim, keep_dims=keepdim)) | ||||
@@ -826,8 +730,15 @@ def median(input, dim=None, keepdim=False, *, out=None): | |||||
point = collections.namedtuple('median', 'values,indices') | point = collections.namedtuple('median', 'values,indices') | ||||
rlt = point(cast_to_adapter_tensor(value), cast_to_adapter_tensor(indice)) | rlt = point(cast_to_adapter_tensor(value), cast_to_adapter_tensor(indice)) | ||||
if out is not None: | if out is not None: | ||||
ops.assign(out, rlt) | |||||
return out | |||||
if pynative_mode_condition(): | |||||
if len(out) != 2 or not isinstance(out[0], adapter_tensor) or not isinstance(out[1], adapter_tensor): | |||||
raise TypeError("In ms_adapter.torch.median(), `out` should be tuple of Tensors.") | |||||
out[0].assign_value(value) | |||||
out[1].assign_value(indice) | |||||
return out | |||||
else: | |||||
raise ValueError('In MindSpore static graph mode, `out` in `median` shoud be None, ' | |||||
'please set out=None and use return value instead of `out`.') | |||||
return rlt | return rlt | ||||
@@ -835,10 +746,7 @@ def matmul(input, other, *, out=None): | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.matmul(input, other) | output = ms.ops.matmul(input, other) | ||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "matmul") | |||||
def norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None): | def norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None): | ||||
@@ -847,10 +755,7 @@ def norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None): | |||||
dtype = ms.float32 | dtype = ms.float32 | ||||
input = ms.numpy.array(input, dtype=dtype) | input = ms.numpy.array(input, dtype=dtype) | ||||
output = ms.numpy.norm(input, ord=p, axis=dim, keepdims=keepdim) | output = ms.numpy.norm(input, ord=p, axis=dim, keepdims=keepdim) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "norm") | |||||
def stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, | def stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, | ||||
@@ -914,11 +819,7 @@ def hann_window(window_length, periodic=False, dtype=None, layout=None, device=N | |||||
def cumsum(input, dim, dtype=None, out=None): | def cumsum(input, dim, dtype=None, out=None): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.cumsum(input, axis=dim, dtype=dtype) | output = ms.ops.cumsum(input, axis=dim, dtype=dtype) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
ops.assign(out, output) | |||||
return out | |||||
return output | |||||
return _out_inplace_assign(out, output, "cumsum") | |||||
def einsum(equation, *operands): | def einsum(equation, *operands): | ||||
@@ -932,10 +833,7 @@ def histc(input, bins=100, min=0, max=0, out=None): | |||||
hist = ms.ops.HistogramFixedWidth(nbins) | hist = ms.ops.HistogramFixedWidth(nbins) | ||||
rang_op = ms.Tensor([min, max], ms.float32) | rang_op = ms.Tensor([min, max], ms.float32) | ||||
output = hist(input, rang_op) | output = hist(input, rang_op) | ||||
output = cast_to_adapter_tensor(output) | |||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "histc") | |||||
def triu(input, diagonal=0, out=None): | def triu(input, diagonal=0, out=None): | ||||
@@ -943,9 +841,7 @@ def triu(input, diagonal=0, out=None): | |||||
input = ms.numpy.array(input) | input = ms.numpy.array(input) | ||||
output = ms.numpy.triu(input, diagonal) | output = ms.numpy.triu(input, diagonal) | ||||
output = cast_to_adapter_tensor(output) | output = cast_to_adapter_tensor(output) | ||||
if out is not None: | |||||
return out.assign_value(output) | |||||
return output | |||||
return _out_inplace_assign(out, output, "triu") | |||||
def unbind(input, dim=0): | def unbind(input, dim=0): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
@@ -1007,10 +903,7 @@ def mul(input, other, *, out=None): | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.mul(input, other) | output = ms.ops.mul(input, other) | ||||
if out is not None: | |||||
out.assign_value(output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "mul") | |||||
def index_select(input, dim, index, *, out=None): | def index_select(input, dim, index, *, out=None): | ||||
@@ -1019,27 +912,21 @@ def index_select(input, dim, index, *, out=None): | |||||
_input_indices = cast_to_ms_tensor(index) | _input_indices = cast_to_ms_tensor(index) | ||||
output = ms.ops.gather(_input_params, _input_indices, _axis) | output = ms.ops.gather(_input_params, _input_indices, _axis) | ||||
if out is not None: | |||||
out.assign_value(output) | |||||
return out | |||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "index_select") | |||||
def sort(input, dim=-1, descending=False, stable=False, *, out=None): | def sort(input, dim=-1, descending=False, stable=False, *, out=None): | ||||
unsupported_attr(stable) | unsupported_attr(stable) | ||||
unsupported_attr(out) | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
# TODO: ops.sort() should be replaced. | # TODO: ops.sort() should be replaced. | ||||
output = ms.ops.Sort(dim, descending)(input) | output = ms.ops.Sort(dim, descending)(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "sort") | |||||
def msort(input, *, out=None): | def msort(input, *, out=None): | ||||
unsupported_attr(out) | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
# TODO: ops.sort() should be replaced. | # TODO: ops.sort() should be replaced. | ||||
output, _ = ms.ops.Sort(axis=0)(input) | output, _ = ms.ops.Sort(axis=0)(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "msort") | |||||
def argsort(input, dim=-1, descending=False, stable=False): | def argsort(input, dim=-1, descending=False, stable=False): | ||||
@@ -1074,32 +961,24 @@ def from_numpy(np_data): | |||||
def absolute(input, *, out=None): | def absolute(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.absolute(input) | output = ms.ops.absolute(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "absolute") | |||||
def acos(input, *, out=None): | def acos(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.acos(input) | output = ms.ops.acos(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "acos") | |||||
def arccos(input, *, out=None): | def arccos(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.acos(input) | output = ms.ops.acos(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arccos") | |||||
def acosh(input, *, out=None): | def acosh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
@@ -1107,12 +986,9 @@ def acosh(input, *, out=None): | |||||
output = ms.ops.acosh(input) | output = ms.ops.acosh(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "acosh") | |||||
def arccosh(input, *, out=None): | def arccosh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
@@ -1120,46 +996,37 @@ def arccosh(input, *, out=None): | |||||
output = ms.ops.acosh(input) | output = ms.ops.acosh(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arccosh") | |||||
def add(input, other, *, alpha=1, out=None): | def add(input, other, *, alpha=1, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.add(input, other*alpha) | output = ms.ops.add(input, other*alpha) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "add") | |||||
def addcdiv(input, tensor1, tensor2, *, value=1, out=None): | def addcdiv(input, tensor1, tensor2, *, value=1, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
tensor1 = cast_to_ms_tensor(tensor1) | tensor1 = cast_to_ms_tensor(tensor1) | ||||
tensor2 = cast_to_ms_tensor(tensor2) | tensor2 = cast_to_ms_tensor(tensor2) | ||||
value = ms.Tensor(value) | value = ms.Tensor(value) | ||||
output = ms.ops.addcdiv(input, tensor1, tensor2, value) | output = ms.ops.addcdiv(input, tensor1, tensor2, value) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "addcdiv") | |||||
def addcmul(input, tensor1, tensor2, *, value=1, out=None): | def addcmul(input, tensor1, tensor2, *, value=1, out=None): | ||||
#Todo: use ms.ops.addcmul after it has been fixed | #Todo: use ms.ops.addcmul after it has been fixed | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
tensor1 = cast_to_ms_tensor(tensor1) | tensor1 = cast_to_ms_tensor(tensor1) | ||||
tensor2 = cast_to_ms_tensor(tensor2) | tensor2 = cast_to_ms_tensor(tensor2) | ||||
value = ms.Tensor(value) | value = ms.Tensor(value) | ||||
mul = ms.ops.mul(tensor1, tensor2) * value | mul = ms.ops.mul(tensor1, tensor2) * value | ||||
output = ms.ops.add(input, mul) | output = ms.ops.add(input, mul) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "addcmul") | |||||
def angle(input, *, out=None): | def angle(input, *, out=None): | ||||
#TODO: use ms.ops.angle | |||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
shape = input.shape | shape = input.shape | ||||
if len(shape)>7: | if len(shape)>7: | ||||
@@ -1182,28 +1049,22 @@ def angle(input, *, out=None): | |||||
if len(shape)>7: | if len(shape)>7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "angle") | |||||
def asin(input, *, out=None): | def asin(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.asin(input) | output = ms.ops.asin(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "asin") | |||||
def arcsin(input, *, out=None): | def arcsin(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.asin(input) | output = ms.ops.asin(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arcsin") | |||||
def asinh(input, *, out=None): | def asinh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
@@ -1211,12 +1072,10 @@ def asinh(input, *, out=None): | |||||
output = ms.ops.asinh(input) | output = ms.ops.asinh(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "asinh") | |||||
def arcsinh(input, *, out=None): | def arcsinh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
@@ -1224,12 +1083,10 @@ def arcsinh(input, *, out=None): | |||||
output = ms.ops.asinh(input) | output = ms.ops.asinh(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arcsinh") | |||||
def atan(input, *, out=None): | def atan(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
input = input.flatten() | input = input.flatten() | ||||
@@ -1237,12 +1094,10 @@ def atan(input, *, out=None): | |||||
output = ms.ops.atan(input) | output = ms.ops.atan(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "atan") | |||||
def arctan(input, *, out=None): | def arctan(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
shape = input.shape | shape = input.shape | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
input = input.flatten() | input = input.flatten() | ||||
@@ -1250,37 +1105,29 @@ def arctan(input, *, out=None): | |||||
output = ms.ops.atan(input) | output = ms.ops.atan(input) | ||||
if len(shape) > 7: | if len(shape) > 7: | ||||
output = output.reshape(shape) | output = output.reshape(shape) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arctan") | |||||
def atanh(input, *, out=None): | def atanh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.atanh(input) | output = ms.ops.atanh(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "atanh") | |||||
def arctanh(input, *, out=None): | def arctanh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.atanh(input) | output = ms.ops.atanh(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arctanh") | |||||
def arctan2(input, other, *, out=None): | def arctan2(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.atan2(input, other) | output = ms.ops.atan2(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "arctan2") | |||||
def bitwise_not(input, *, out=None): | def bitwise_not(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
type = input.dtype | type = input.dtype | ||||
if str(type) != 'Bool': | if str(type) != 'Bool': | ||||
@@ -1288,12 +1135,10 @@ def bitwise_not(input, *, out=None): | |||||
else: | else: | ||||
output = 1 - input | output = 1 - input | ||||
output = output.astype(ms.bool_) | output = output.astype(ms.bool_) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_not") | |||||
def bitwise_and(input, other, *, out=None): | def bitwise_and(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input_is_bool = str(input.dtype) == 'Bool' | input_is_bool = str(input.dtype) == 'Bool' | ||||
@@ -1312,12 +1157,10 @@ def bitwise_and(input, other, *, out=None): | |||||
output = ms.ops.bitwise_and(input, other) | output = ms.ops.bitwise_and(input, other) | ||||
if input_is_bool and other_is_bool: | if input_is_bool and other_is_bool: | ||||
output = output.astype(ms.bool_) | output = output.astype(ms.bool_) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_and") | |||||
def bitwise_or(input, other, *, out=None): | def bitwise_or(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input_is_bool = str(input.dtype) == 'Bool' | input_is_bool = str(input.dtype) == 'Bool' | ||||
@@ -1336,12 +1179,10 @@ def bitwise_or(input, other, *, out=None): | |||||
output = ms.ops.bitwise_or(input, other) | output = ms.ops.bitwise_or(input, other) | ||||
if input_is_bool and other_is_bool: | if input_is_bool and other_is_bool: | ||||
output = output.astype(ms.bool_) | output = output.astype(ms.bool_) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_or") | |||||
def bitwise_xor(input, other, *, out=None): | def bitwise_xor(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input_is_bool = str(input.dtype) == 'Bool' | input_is_bool = str(input.dtype) == 'Bool' | ||||
@@ -1360,29 +1201,25 @@ def bitwise_xor(input, other, *, out=None): | |||||
output = ms.ops.bitwise_xor(input, other) | output = ms.ops.bitwise_xor(input, other) | ||||
if input_is_bool and other_is_bool: | if input_is_bool and other_is_bool: | ||||
output = output.astype(ms.bool_) | output = output.astype(ms.bool_) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_xor") | |||||
def bitwise_left_shift(input, other, *, out=None): | def bitwise_left_shift(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).asnumpy() | input = cast_to_ms_tensor(input).asnumpy() | ||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other).asnumpy() | other = cast_to_ms_tensor(other).asnumpy() | ||||
output = ms.Tensor(np.left_shift(input, other)) | output = ms.Tensor(np.left_shift(input, other)) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_left_shift") | |||||
def bitwise_right_shift(input, other, *, out=None): | def bitwise_right_shift(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).asnumpy() | input = cast_to_ms_tensor(input).asnumpy() | ||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other).asnumpy() | other = cast_to_ms_tensor(other).asnumpy() | ||||
output = ms.Tensor(np.right_shift(input, other)) | output = ms.Tensor(np.right_shift(input, other)) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "bitwise_right_shift") | |||||
def split(tensor, split_size_or_sections, dim=0): | def split(tensor, split_size_or_sections, dim=0): | ||||
@@ -1440,8 +1277,6 @@ def split(tensor, split_size_or_sections, dim=0): | |||||
return res | return res | ||||
def nonzero(input, *, out=None, as_tuple=False): | def nonzero(input, *, out=None, as_tuple=False): | ||||
if out is not None: | |||||
warnings.warn("Do not support parameter 'out'.") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if as_tuple: | if as_tuple: | ||||
if input.ndim == 1: | if input.ndim == 1: | ||||
@@ -1451,34 +1286,28 @@ def nonzero(input, *, out=None, as_tuple=False): | |||||
output = [] | output = [] | ||||
res = ms.ops.nonzero(input) | res = ms.ops.nonzero(input) | ||||
res = res.transpose(1,0) | res = res.transpose(1,0) | ||||
res = ms.ops.split(res, axis=0, output_num= input.ndim) | |||||
res = ms.ops.split(res, axis=0, output_num=input.ndim) | |||||
for cur in res: | for cur in res: | ||||
output.append(cast_to_adapter_tensor(cur)) | output.append(cast_to_adapter_tensor(cur)) | ||||
output = tuple(output) | output = tuple(output) | ||||
elif input.ndim == 0: | elif input.ndim == 0: | ||||
raise ValueError("Do not support input ndim == 0.") | raise ValueError("Do not support input ndim == 0.") | ||||
return output | |||||
return cast_to_adapter_tensor(ms.ops.nonzero(input)) | |||||
return output # TODO: out is not assigned | |||||
output = ms.ops.nonzero(input) | |||||
return _out_inplace_assign(out, output, "nonzero") | |||||
def clip(input, min=None, max=None, *, out=None): | def clip(input, min=None, max=None, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = input.clip(min, max) | output = input.clip(min, max) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "clip") | |||||
def conj_physical(input, *, out=None): | def conj_physical(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.conj(input) | output = ms.ops.conj(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "conj_physical") | |||||
def copysign(input, other, *, out=None): | def copysign(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
input_type = input.dtype | input_type = input.dtype | ||||
input = input.asnumpy() | input = input.asnumpy() | ||||
@@ -1497,28 +1326,25 @@ def copysign(input, other, *, out=None): | |||||
output = output.astype(other_type) | output = output.astype(other_type) | ||||
elif is_num or 'Int' in str(other_type): | elif is_num or 'Int' in str(other_type): | ||||
output = output.astype(input_type) | output = output.astype(input_type) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "copysign") | |||||
def cosh(input, *, out=None): | def cosh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.cosh(input) | output = ms.ops.cosh(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "cosh") | |||||
def deg2rad(input, *, out=None): | def deg2rad(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if input.dtype not in (ms.float16, ms.float32, ms.float64): | if input.dtype not in (ms.float16, ms.float32, ms.float64): | ||||
input = input.astype(ms.float32) | input = input.astype(ms.float32) | ||||
output = ms.ops.deg2rad(input) | output = ms.ops.deg2rad(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "cosh") | |||||
def devide(input, other, *, rounding_mode=None, out=None): | def devide(input, other, *, rounding_mode=None, out=None): | ||||
_out_limit_pynative(out, "devide") | |||||
return div(input, other, rounding_mode=rounding_mode, out=out) | return div(input, other, rounding_mode=rounding_mode, out=out) | ||||
#Todo: not found class Digamma | #Todo: not found class Digamma | ||||
@@ -1526,43 +1352,33 @@ def devide(input, other, *, rounding_mode=None, out=None): | |||||
def erf(input, *, out=None): | def erf(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.erf(input) | output = ms.ops.erf(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "erf") | |||||
def erfc(input, *, out=None): | def erfc(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.erfc(input) | output = ms.ops.erfc(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "erfc") | |||||
def erfinv(input, *, out=None): | def erfinv(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.erfinv(input) | output = ms.ops.erfinv(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "erfinv") | |||||
def exp2(input, *, out=None): | def exp2(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.exp2(input) | output = ms.ops.exp2(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "exp2") | |||||
def expm1(input, *, out=None): | def expm1(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.expm1(input) | output = ms.ops.expm1(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "expm1") | |||||
def fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max): | def fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max): | ||||
@@ -1596,39 +1412,38 @@ def fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_m | |||||
def fix(input, *, out=None): | def fix(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.trunc(input) | output = ms.ops.trunc(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "fix") | |||||
def float_power(input, exponent, *, out=None): | def float_power(input, exponent, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).asnumpy() | input = cast_to_ms_tensor(input).asnumpy() | ||||
if isinstance(exponent, adapter_tensor): | if isinstance(exponent, adapter_tensor): | ||||
exponent = cast_to_ms_tensor(exponent).asnumpy() | exponent = cast_to_ms_tensor(exponent).asnumpy() | ||||
output = ms.Tensor(np.float_power(input, exponent)) | output = ms.Tensor(np.float_power(input, exponent)) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "float_power") | |||||
def floor_divide(input, other, *, out=None): | def floor_divide(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.floor_div(input, other) | output = ms.ops.floor_div(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "floor_divide") | |||||
def frexp(input, *, out=None): | def frexp(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
_out_limit_pynative(out, "frexp") | |||||
input = cast_to_ms_tensor(input).asnumpy() | input = cast_to_ms_tensor(input).asnumpy() | ||||
mantissa, exponent = np.frexp(input) | mantissa, exponent = np.frexp(input) | ||||
return cast_to_adapter_tensor(ms.Tensor(mantissa)), cast_to_adapter_tensor(ms.Tensor(exponent)) | |||||
out1 = ms.Tensor(mantissa) | |||||
out2 = ms.Tensor(exponent) | |||||
if out is not None and len(out) != 2: | |||||
out[0].assign_value(out1) | |||||
out[1].assign_value(out2) | |||||
return out | |||||
return cast_to_adapter_tensor(out1), cast_to_adapter_tensor(out2) | |||||
def gradient(input, *, spacing=1, dim=None, edge_order=1): | def gradient(input, *, spacing=1, dim=None, edge_order=1): | ||||
@@ -1652,17 +1467,13 @@ def imag(input): | |||||
def ldexp(input, other, *, out=None): | def ldexp(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.ldexp(input, other) | output = ms.ops.ldexp(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "ldexp") | |||||
def lerp(input, end, weight, *, out=None): | def lerp(input, end, weight, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
end = cast_to_ms_tensor(end) | end = cast_to_ms_tensor(end) | ||||
if isinstance(weight, adapter_tensor): | if isinstance(weight, adapter_tensor): | ||||
@@ -1670,7 +1481,7 @@ def lerp(input, end, weight, *, out=None): | |||||
elif not isinstance(weight, float): | elif not isinstance(weight, float): | ||||
weight = float(weight) | weight = float(weight) | ||||
output = ms.ops.lerp(input, end, weight) | output = ms.ops.lerp(input, end, weight) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "lerp") | |||||
#Todo | #Todo | ||||
@@ -1678,74 +1489,60 @@ def lerp(input, end, weight, *, out=None): | |||||
def logaddexp(input, other, *, out=None): | def logaddexp(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.logaddexp(input, other) | output = ms.ops.logaddexp(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logaddexp") | |||||
def logaddexp2(input, other, *, out=None): | def logaddexp2(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.logaddexp2(input, other) | output = ms.ops.logaddexp2(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logaddexp2") | |||||
def logical_and(input, other, *, out=None): | def logical_and(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).astype(ms.bool_) | input = cast_to_ms_tensor(input).astype(ms.bool_) | ||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other).astype(ms.bool_) | other = cast_to_ms_tensor(other).astype(ms.bool_) | ||||
output = ms.ops.logical_and(input, other) | output = ms.ops.logical_and(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logical_and") | |||||
def logical_not(input, *, out=None): | def logical_not(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).astype(ms.bool_) | input = cast_to_ms_tensor(input).astype(ms.bool_) | ||||
output = ms.ops.logical_not(input) | output = ms.ops.logical_not(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logical_not") | |||||
def logical_or(input, other, *, out=None): | def logical_or(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).astype(ms.bool_) | input = cast_to_ms_tensor(input).astype(ms.bool_) | ||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other).astype(ms.bool_) | other = cast_to_ms_tensor(other).astype(ms.bool_) | ||||
output = ms.ops.logical_or(input, other) | output = ms.ops.logical_or(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logical_or") | |||||
def logical_xor(input, other, *, out=None): | def logical_xor(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
if isinstance(input, adapter_tensor): | if isinstance(input, adapter_tensor): | ||||
input = cast_to_ms_tensor(input).astype(ms.bool_) | input = cast_to_ms_tensor(input).astype(ms.bool_) | ||||
if isinstance(other, adapter_tensor): | if isinstance(other, adapter_tensor): | ||||
other = cast_to_ms_tensor(other).astype(ms.bool_) | other = cast_to_ms_tensor(other).astype(ms.bool_) | ||||
output = ms.ops.logical_xor(input, other) | output = ms.ops.logical_xor(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logical_xor") | |||||
def logit(input, eps=None, *, out=None): | def logit(input, eps=None, *, out=None): | ||||
#TODO: ops.logit not support cpu | #TODO: ops.logit not support cpu | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if eps is not None: | if eps is not None: | ||||
input = ms.ops.clip_by_value(input, eps, 1.0-eps) | input = ms.ops.clip_by_value(input, eps, 1.0-eps) | ||||
output = ms.ops.log(input/(1.0-input)) | output = ms.ops.log(input/(1.0-input)) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "logit") | |||||
def frombuffer(buffer, *, dtype = None, count=- 1, offset=0, requires_grad=False): | def frombuffer(buffer, *, dtype = None, count=- 1, offset=0, requires_grad=False): | ||||
unsupported_attr(requires_grad) | unsupported_attr(requires_grad) | ||||
@@ -1770,62 +1567,50 @@ def as_strided(input, size, stride, storage_offset=None): | |||||
return cast_to_adapter_tensor(out) | return cast_to_adapter_tensor(out) | ||||
def ne(input, other, *, out=None): | def ne(input, other, *, out=None): | ||||
unsupported_attr(out) | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.ne(input, other) | output = ms.ops.ne(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "ne") | |||||
def tanh(input, *, out=None): | def tanh(input, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = ms.ops.tanh(input) | output = ms.ops.tanh(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "tanh") | |||||
def maximum(input, other, *, out=None): | def maximum(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.maximum(input, other) | output = ms.ops.maximum(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "maximum") | |||||
def minimum(input, other, *, out=None): | def minimum(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.minimum(input, other) | output = ms.ops.minimum(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "minimum") | |||||
def sigmoid(input, *, out=None): | def sigmoid(input, *, out=None): | ||||
#TODO: ms.ops.sigmoid() not support float64 | #TODO: ms.ops.sigmoid() not support float64 | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
output = 1 / (ms.ops.exp(0 - input) + 1) | output = 1 / (ms.ops.exp(0 - input) + 1) | ||||
#output = ms.ops.sigmoid(input) | #output = ms.ops.sigmoid(input) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "sigmoid") | |||||
def softmax(input, dim, dtype=None, *, out=None): | def softmax(input, dim, dtype=None, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if dtype is not None: | if dtype is not None: | ||||
input = input.astype(dtype) | input = input.astype(dtype) | ||||
output = ms.ops.softmax(input, dim) | output = ms.ops.softmax(input, dim) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "softmax") | |||||
def prod(input, dim=None, keepdim=False, *, dtype=None, out=None): | def prod(input, dim=None, keepdim=False, *, dtype=None, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
if dtype is not None: | if dtype is not None: | ||||
input = input.astype(dtype) | input = input.astype(dtype) | ||||
@@ -1833,13 +1618,11 @@ def prod(input, dim=None, keepdim=False, *, dtype=None, out=None): | |||||
output = ms.ops.prod(input) | output = ms.ops.prod(input) | ||||
else: | else: | ||||
output = ms.ops.prod(input, axis=dim, keep_dims=keepdim) | output = ms.ops.prod(input, axis=dim, keep_dims=keepdim) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "prod") | |||||
def eq(input, other, *, out=None): | def eq(input, other, *, out=None): | ||||
if out is not None: | |||||
warnings.warn("do not support parameter out") | |||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = ms.ops.equal(input, other) | output = ms.ops.equal(input, other) | ||||
return cast_to_adapter_tensor(output) | |||||
return _out_inplace_assign(out, output, "eq") |
@@ -11,6 +11,7 @@ from mindspore.ops.operations.nn_ops import TripletMarginLoss as TripletMarginLo | |||||
from ms_adapter.utils import unsupported_attr, get_backend | from ms_adapter.utils import unsupported_attr, get_backend | ||||
from ms_adapter.pytorch.tensor import cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | from ms_adapter.pytorch.tensor import cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | ||||
from ms_adapter.pytorch.common._inner import _inplace_assign_pynative | |||||
all = [ | all = [ | ||||
@@ -165,57 +166,30 @@ def logsigmoid(input_x): | |||||
def elu(input, alpha=1.0, inplace=False): | def elu(input, alpha=1.0, inplace=False): | ||||
# TODO | # TODO | ||||
# ms.ops.ELU should be replaced by ms.ops.elu | # ms.ops.ELU should be replaced by ms.ops.elu | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(elu.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.Elu(alpha=alpha)(input_ms) | out = ms.ops.Elu(alpha=alpha)(input_ms) | ||||
if inplace is True: | |||||
ms.ops.assign(input, out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "elu") | |||||
def rrelu(input, lower=1.0/8, upper=1.0/3, training=False, inplace=False): | def rrelu(input, lower=1.0/8, upper=1.0/3, training=False, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(rrelu.__name__)) | |||||
if training: | if training: | ||||
raise ValueError("training '{}' is not currently supported.".format(training)) | raise ValueError("training '{}' is not currently supported.".format(training)) | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = nn.RReLU(lower=lower, upper=upper)(input_ms) | out = nn.RReLU(lower=lower, upper=upper)(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "rrelu") | |||||
def selu(input, inplace=False): | def selu(input, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(selu.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.selu(input_ms) | out = ms.ops.selu(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "selu") | |||||
def celu(input, alpha=1.0, inplace=False): | def celu(input, alpha=1.0, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(celu.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.celu(input_ms, alpha) | out = ms.ops.celu(input_ms, alpha) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "celu") | |||||
def gelu(input): | def gelu(input): | ||||
@@ -229,15 +203,9 @@ def gelu(input): | |||||
def mish(input, inplace=False): | def mish(input, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(mish.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.mish(input_ms) | out = ms.ops.mish(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "mish") | |||||
def softshrink(input, lambd=0.5): | def softshrink(input, lambd=0.5): | ||||
@@ -250,68 +218,38 @@ def softshrink(input, lambd=0.5): | |||||
def relu(input, inplace=False): | def relu(input, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(relu.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.ReLU()(input_ms) | out = ms.ops.ReLU()(input_ms) | ||||
if inplace is True: | |||||
ms.ops.assign(input, out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "relu") | |||||
def hardtanh(input, min_val=-1.0, max_val=1.0, inplace=False): | def hardtanh(input, min_val=-1.0, max_val=1.0, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(hardtanh.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = nn.Hardtanh(min_val, max_val)(input_ms) | out = nn.Hardtanh(min_val, max_val)(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "hardtanh") | |||||
def hardswish(input, inplace=False): | def hardswish(input, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(hardswish.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.hardswish(input_ms) | out = ms.ops.hardswish(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "hardswish") | |||||
def relu6(input, inplace=False): | def relu6(input, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(relu6.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = nn.Hardtanh(0., 6.)(input_ms) | out = nn.Hardtanh(0., 6.)(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "relu6") | |||||
def leaky_relu(input, negative_slope=0.01, inplace=False): | def leaky_relu(input, negative_slope=0.01, inplace=False): | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(leaky_relu.__name__)) | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = nn.LeakyReLU(alpha=negative_slope)(input_ms) | out = nn.LeakyReLU(alpha=negative_slope)(input_ms) | ||||
if inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "leaky_relu") | |||||
def upsample(input, size=None, scale_factor=None, mode='nearest', | def upsample(input, size=None, scale_factor=None, mode='nearest', | ||||
@@ -473,25 +411,13 @@ def sigmoid(input): | |||||
def hardsigmoid(input, inplace=False): | def hardsigmoid(input, inplace=False): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.HSigmoid()(input_ms) | out = ms.ops.HSigmoid()(input_ms) | ||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(hardsigmoid.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "hardsigmoid") | |||||
def silu(input, inplace=False): | def silu(input, inplace=False): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = ms.ops.Sigmoid()(input_ms) * input_ms | out = ms.ops.Sigmoid()(input_ms) * input_ms | ||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(silu.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "silu") | |||||
def gumbel_softmax(logits, tau=1.0, hard=False, eps=1e-10, dim=-1): | def gumbel_softmax(logits, tau=1.0, hard=False, eps=1e-10, dim=-1): | ||||
@@ -507,13 +433,7 @@ def threshold(input, threshold, value, inplace=False): | |||||
cond = ms.ops.ge(input_ms, threshold) | cond = ms.ops.ge(input_ms, threshold) | ||||
value = ms.ops.fill(input_ms.dtype, input_ms.shape, value) | value = ms.ops.fill(input_ms.dtype, input_ms.shape, value) | ||||
out = ms.ops.select(cond, input_ms, value) | out = ms.ops.select(cond, input_ms, value) | ||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(threshold.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign_pynative(input, inplace, out, "threshold") | |||||
rrelu_ = rrelu | rrelu_ = rrelu | ||||
@@ -834,18 +754,9 @@ def dropout1d(input, p = 0.5, training = True, inplace = False): | |||||
if not training: | if not training: | ||||
return input | return input | ||||
@cast_tensor | |||||
def _call_ms_api(input): | |||||
out = ms.ops.dropout1d(input, p) | |||||
return out | |||||
out = _call_ms_api(input) | |||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return out | |||||
input_ms = cast_to_ms_tensor(input) | |||||
out = ms.ops.dropout1d(input_ms, p) | |||||
return _inplace_assign_pynative(input, inplace, out, "dropout1d") | |||||
def dropout2d(input, p=0.5, training=True, inplace=False): | def dropout2d(input, p=0.5, training=True, inplace=False): | ||||
@@ -868,18 +779,10 @@ def dropout2d(input, p=0.5, training=True, inplace=False): | |||||
"input as one without a batch dimension, i.e. shape (C, H, W). To maintain the 1D " | "input as one without a batch dimension, i.e. shape (C, H, W). To maintain the 1D " | ||||
"channel-wise dropout behavior, please switch to using dropout1d instead.") | "channel-wise dropout behavior, please switch to using dropout1d instead.") | ||||
return dropout1d(input, p, training, inplace) | return dropout1d(input, p, training, inplace) | ||||
@cast_tensor | |||||
def _call_ms_api(input): | |||||
out, _ = ms.ops.dropout2d(input, p) | |||||
return out | |||||
out = _call_ms_api(input) | |||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return out | |||||
input_ms = cast_to_ms_tensor(input) | |||||
out, _ = ms.ops.dropout2d(input_ms, p) | |||||
return _inplace_assign_pynative(input, inplace, out, "dropout2d") | |||||
def dropout3d(input, p=0.5, training=True, inplace=False): | def dropout3d(input, p=0.5, training=True, inplace=False): | ||||
@@ -898,47 +801,31 @@ def dropout3d(input, p=0.5, training=True, inplace=False): | |||||
is_batched = inp_dim == 5 | is_batched = inp_dim == 5 | ||||
@cast_tensor | |||||
def _call_ms_api(input): | |||||
if not is_batched: | |||||
input = ms.ops.expand_dims(input, 0) | |||||
out, _ = ms.ops.dropout3d(input, p) | |||||
if not is_batched: | |||||
out = ms.ops.squeeze(out, 0) | |||||
return out | |||||
out = _call_ms_api(input) | |||||
input_ms = cast_to_ms_tensor(input) | |||||
if not is_batched: | |||||
input_ms = ms.ops.expand_dims(input_ms, 0) | |||||
out, _ = ms.ops.dropout3d(input_ms, p) | |||||
if not is_batched: | |||||
out = ms.ops.squeeze(out, 0) | |||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return out | |||||
return _inplace_assign_pynative(input, inplace, out, "dropout3d") | |||||
def dropout(input, p=0.5, training=True, inplace=False): | def dropout(input, p=0.5, training=True, inplace=False): | ||||
if p < 0.0 or p > 1.0: | if p < 0.0 or p > 1.0: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p)) | raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p)) | ||||
@cast_tensor | |||||
def _call_ms_api(input): | |||||
shape = input.shape | |||||
random_array_np = np.random.rand(input.size).reshape(shape) | |||||
random_array = ms.Tensor(random_array_np, ms.float32) | |||||
mask = (random_array > ms.Tensor(p, ms.float32)) | |||||
out = mask * 1.0 / (1.0-p) * input | |||||
if not training: | |||||
return input | |||||
return out | |||||
out = _call_ms_api(input) | |||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
if not training: | |||||
return input | return input | ||||
return out | |||||
input_ms = cast_to_ms_tensor(input) | |||||
shape = input_ms.shape | |||||
random_array_np = np.random.rand(input_ms.size).reshape(shape) | |||||
random_array = ms.Tensor(random_array_np, ms.float32) | |||||
mask = (random_array > ms.Tensor(p, ms.float32)) | |||||
out = mask * 1.0 / (1.0-p) * input_ms | |||||
return _inplace_assign_pynative(input, inplace, out, "dropout") | |||||
def alpha_dropout(input, p=0.5, training=False, inplace=False): | def alpha_dropout(input, p=0.5, training=False, inplace=False): | ||||
@@ -971,13 +858,7 @@ def alpha_dropout(input, p=0.5, training=False, inplace=False): | |||||
out = out * a + b | out = out * a + b | ||||
return out | return out | ||||
out = _call_ms_api(input) | out = _call_ms_api(input) | ||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return out | |||||
return _inplace_assign_pynative(input, inplace, out, "alpha_dropout") | |||||
def feature_alpha_dropout(input, p=0.5, training=False, inplace=False): | def feature_alpha_dropout(input, p=0.5, training=False, inplace=False): | ||||
@@ -1014,13 +895,8 @@ def feature_alpha_dropout(input, p=0.5, training=False, inplace=False): | |||||
out = out * a + b | out = out * a + b | ||||
return out | return out | ||||
out = _call_ms_api(input) | out = _call_ms_api(input) | ||||
if inplace: | |||||
if ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(dropout1d.__name__)) | |||||
input.assign_value(out) | |||||
return input | |||||
return out | |||||
return _inplace_assign_pynative(input, inplace, out, "feature_alpha_dropout") | |||||
def hardshrink(input, lambd=0.5): | def hardshrink(input, lambd=0.5): | ||||
input = cast_to_ms_tensor(input) | input = cast_to_ms_tensor(input) | ||||
@@ -65,7 +65,7 @@ def _no_grad_trunc_normal_(tensor, mean, std, a, b): | |||||
# Uniformly fill tensor with values from [l, u], then translate to | # Uniformly fill tensor with values from [l, u], then translate to | ||||
# [2l-1, 2u-1]. | # [2l-1, 2u-1]. | ||||
tensor = tensor.uniform(2 * l - 1, 2 * u - 1) | |||||
tensor = tensor.uniform_(2 * l - 1, 2 * u - 1) | |||||
# Use inverse cdf transform for normal distribution to get truncated | # Use inverse cdf transform for normal distribution to get truncated | ||||
# standard normal | # standard normal | ||||
@@ -97,7 +97,7 @@ def kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu'): | |||||
gain = calculate_gain(nonlinearity, a) | gain = calculate_gain(nonlinearity, a) | ||||
std = gain / math.sqrt(fan) | std = gain / math.sqrt(fan) | ||||
bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation | bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation | ||||
return tensor.uniform(-bound, bound) | |||||
return tensor.uniform_(-bound, bound) | |||||
def kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu'): | def kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu'): | ||||
@@ -122,7 +122,7 @@ def xavier_uniform_(tensor, gain = 1.): | |||||
std = gain * math.sqrt(2.0 / float(fan_in + fan_out)) | std = gain * math.sqrt(2.0 / float(fan_in + fan_out)) | ||||
a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation | a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation | ||||
return tensor.uniform(-a, a) | |||||
return tensor.uniform_(-a, a) | |||||
def zeros_(tensor): | def zeros_(tensor): | ||||
return tensor.zero_() | return tensor.zero_() | ||||
@@ -140,4 +140,4 @@ def normal_(tensor, mean = 0., std = 1.): | |||||
return tensor.normal_(mean, std) | return tensor.normal_(mean, std) | ||||
def uniform_(tensor, a = 0., b = 1.): | def uniform_(tensor, a = 0., b = 1.): | ||||
return tensor.assign_value(tensor.uniform(a, b)) | |||||
return tensor.uniform_(a, b) |
@@ -11,6 +11,7 @@ from mindspore import nn | |||||
import ms_adapter.pytorch.nn.functional as ms_torch_nn_func | import ms_adapter.pytorch.nn.functional as ms_torch_nn_func | ||||
from ms_adapter.pytorch.tensor import cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | from ms_adapter.pytorch.tensor import cast_tensor, cast_to_ms_tensor, cast_to_adapter_tensor | ||||
from ms_adapter.utils import unsupported_attr | from ms_adapter.utils import unsupported_attr | ||||
from ms_adapter.pytorch.common._inner import _inplace_assign, _inplace_limit_pynative | |||||
from .module import Module | from .module import Module | ||||
__all__ = ['ReLU', 'Hardtanh', 'ReLU6', 'SiLU', 'Hardswish', 'LeakyReLU', 'Sigmoid', 'LogSigmoid', 'ELU', 'RReLU', | __all__ = ['ReLU', 'Hardtanh', 'ReLU6', 'SiLU', 'Hardswish', 'LeakyReLU', 'Sigmoid', 'LogSigmoid', 'ELU', 'RReLU', | ||||
@@ -45,17 +46,12 @@ class ReLU(Module): | |||||
super(ReLU, self).__init__() | super(ReLU, self).__init__() | ||||
self.relu = P.ReLU() | self.relu = P.ReLU() | ||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
_inplace_limit_pynative(inplace, "ReLU") | |||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output = self.relu(input_ms) | output = self.relu(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = 'inplace=True' if self.inplace else '' | inplace_str = 'inplace=True' if self.inplace else '' | ||||
@@ -72,6 +68,7 @@ class Hardtanh(Module): | |||||
max_value=None | max_value=None | ||||
): | ): | ||||
super(Hardtanh, self).__init__() | super(Hardtanh, self).__init__() | ||||
_inplace_limit_pynative(inplace, "Hardtanh") | |||||
if min_value is not None: | if min_value is not None: | ||||
warnings.warn("keyword argument min_value is deprecated and rename to min_val") | warnings.warn("keyword argument min_value is deprecated and rename to min_val") | ||||
min_val = min_value | min_val = min_value | ||||
@@ -82,9 +79,6 @@ class Hardtanh(Module): | |||||
self.min_val = min_val | self.min_val = min_val | ||||
self.max_val = max_val | self.max_val = max_val | ||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
if self.max_val <= self.min_val: | if self.max_val <= self.min_val: | ||||
raise ValueError('`max_val` must be larger than `min_val` in `{}`, but get `max_val`:{} and ' | raise ValueError('`max_val` must be larger than `min_val` in `{}`, but get `max_val`:{} and ' | ||||
'`min_val`:{}'.format(self.__class__.__name__, self.max_val, self.min_val)) | '`min_val`:{}'.format(self.__class__.__name__, self.max_val, self.min_val)) | ||||
@@ -94,10 +88,7 @@ class Hardtanh(Module): | |||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output = self.hardtanh(input_ms) | output = self.hardtanh(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = ', inplace=True' if self.inplace else '' | inplace_str = ', inplace=True' if self.inplace else '' | ||||
@@ -108,6 +99,7 @@ class Hardtanh(Module): | |||||
class ReLU6(Hardtanh): | class ReLU6(Hardtanh): | ||||
def __init__(self, inplace=False): | def __init__(self, inplace=False): | ||||
_inplace_limit_pynative(inplace, "ReLU6") | |||||
super(ReLU6, self).__init__(0., 6., inplace) | super(ReLU6, self).__init__(0., 6., inplace) | ||||
def extra_repr(self): | def extra_repr(self): | ||||
@@ -118,19 +110,14 @@ class ReLU6(Hardtanh): | |||||
class SiLU(Module): | class SiLU(Module): | ||||
def __init__(self, inplace=False): | def __init__(self, inplace=False): | ||||
super(SiLU, self).__init__() | super(SiLU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "SiLU") | |||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.sigmoid = P.Sigmoid() | self.sigmoid = P.Sigmoid() | ||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output = self.sigmoid(input_ms) * input_ms | output = self.sigmoid(input_ms) * input_ms | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
def extra_repr(self) -> str: | def extra_repr(self) -> str: | ||||
inplace_str = 'inplace=True' if self.inplace else '' | inplace_str = 'inplace=True' if self.inplace else '' | ||||
@@ -140,29 +127,22 @@ class SiLU(Module): | |||||
class Hardswish(Module): | class Hardswish(Module): | ||||
def __init__(self, inplace=False): | def __init__(self, inplace=False): | ||||
super(Hardswish, self).__init__() | super(Hardswish, self).__init__() | ||||
_inplace_limit_pynative(inplace, "Hardswish") | |||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.hardswish = P.HSwish() | self.hardswish = P.HSwish() | ||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output = self.hardswish(input_ms) | output = self.hardswish(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
class LeakyReLU(Module): | class LeakyReLU(Module): | ||||
def __init__(self, negative_slope=1e-2, inplace=False): | def __init__(self, negative_slope=1e-2, inplace=False): | ||||
super(LeakyReLU, self).__init__() | super(LeakyReLU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "LeakyReLU") | |||||
self.negative_slope = negative_slope | self.negative_slope = negative_slope | ||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.greater_equal = P.GreaterEqual() | self.greater_equal = P.GreaterEqual() | ||||
self.mul = P.Mul() | self.mul = P.Mul() | ||||
self.select_op = P.Maximum() | self.select_op = P.Maximum() | ||||
@@ -174,10 +154,7 @@ class LeakyReLU(Module): | |||||
alpha_array = P.Cast()(F.scalar_to_tensor(self.negative_slope), P.DType()(input_ms)) | alpha_array = P.Cast()(F.scalar_to_tensor(self.negative_slope), P.DType()(input_ms)) | ||||
output = self.select_op(alpha_array * input_ms, input_ms) | output = self.select_op(alpha_array * input_ms, input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
def extra_repr(self) -> str: | def extra_repr(self) -> str: | ||||
inplace_str = ', inplace=True' if self.inplace else '' | inplace_str = ', inplace=True' if self.inplace else '' | ||||
@@ -208,6 +185,7 @@ class LogSigmoid(Module): | |||||
class ELU(Module): | class ELU(Module): | ||||
def __init__(self, alpha: float=1., inplace: bool=False): | def __init__(self, alpha: float=1., inplace: bool=False): | ||||
super(ELU, self).__init__() | super(ELU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "ELU") | |||||
self.elu = ms_torch_nn_func.elu | self.elu = ms_torch_nn_func.elu | ||||
self.alpha = alpha | self.alpha = alpha | ||||
self.inplace = inplace | self.inplace = inplace | ||||
@@ -224,22 +202,16 @@ class RReLU(Module): | |||||
inplace=False | inplace=False | ||||
): | ): | ||||
super(RReLU, self).__init__() | super(RReLU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "RReLU") | |||||
self.lower = lower | self.lower = lower | ||||
self.upper = upper | self.upper = upper | ||||
self.inplace = inplace | self.inplace = inplace | ||||
self.rrelu = ms.nn.RReLU(lower=self.lower, upper=self.upper) | self.rrelu = ms.nn.RReLU(lower=self.lower, upper=self.upper) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = self.rrelu(input_ms) | out = self.rrelu(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign(input, self.inplace, out) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = ', inplace=True' if self.inplace else '' | inplace_str = ', inplace=True' if self.inplace else '' | ||||
@@ -249,19 +221,14 @@ class RReLU(Module): | |||||
class SELU(Module): | class SELU(Module): | ||||
def __init__(self, inplace=False): | def __init__(self, inplace=False): | ||||
super(SELU, self).__init__() | super(SELU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "SELU") | |||||
self.inplace = inplace | self.inplace = inplace | ||||
self.selu = P.SeLU() | self.selu = P.SeLU() | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = self.selu(input_ms) | out = self.selu(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign(input, self.inplace, out) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = 'inplace=True' if self.inplace else '' | inplace_str = 'inplace=True' if self.inplace else '' | ||||
@@ -271,20 +238,15 @@ class SELU(Module): | |||||
class CELU(Module): | class CELU(Module): | ||||
def __init__(self, alpha=1., inplace=False): | def __init__(self, alpha=1., inplace=False): | ||||
super(CELU, self).__init__() | super(CELU, self).__init__() | ||||
_inplace_limit_pynative(inplace, "CELU") | |||||
self.alpha = alpha | self.alpha = alpha | ||||
self.inplace = inplace | self.inplace = inplace | ||||
self.celu = P.CeLU(alpha=self.alpha) | self.celu = P.CeLU(alpha=self.alpha) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = self.celu(input_ms) | out = self.celu(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign(input, self.inplace, out) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = ', inplace=True' if self.inplace else '' | inplace_str = ', inplace=True' if self.inplace else '' | ||||
@@ -307,19 +269,14 @@ class GELU(Module): | |||||
class Mish(Module): | class Mish(Module): | ||||
def __init__(self, inplace=False): | def __init__(self, inplace=False): | ||||
super(Mish, self).__init__() | super(Mish, self).__init__() | ||||
_inplace_limit_pynative(inplace, "Mish") | |||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.mish = P.Mish() | self.mish = P.Mish() | ||||
def forward(self, input): | def forward(self, input): | ||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
out = self.mish(input_ms) | out = self.mish(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(out) | |||||
return input | |||||
return cast_to_adapter_tensor(out) | |||||
return _inplace_assign(input, self.inplace, out) | |||||
def extra_repr(self): | def extra_repr(self): | ||||
inplace_str = 'inplace=True' if self.inplace else '' | inplace_str = 'inplace=True' if self.inplace else '' | ||||
@@ -357,12 +314,11 @@ class Tanhshrink(Module): | |||||
class Threshold(Module): | class Threshold(Module): | ||||
def __init__(self, threshold, value, inplace=False): | def __init__(self, threshold, value, inplace=False): | ||||
super(Threshold, self).__init__() | super(Threshold, self).__init__() | ||||
_inplace_limit_pynative(inplace, "Threshold") | |||||
self.threshold = threshold | self.threshold = threshold | ||||
self.value = value | self.value = value | ||||
self.inplace = inplace | self.inplace = inplace | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise ValueError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
return ms_torch_nn_func.threshold(input, self.threshold, self.value, self.inplace) | return ms_torch_nn_func.threshold(input, self.threshold, self.value, self.inplace) | ||||
@@ -444,6 +400,7 @@ class Hardshrink(Module): | |||||
class Hardsigmoid(Module): | class Hardsigmoid(Module): | ||||
def __init__(self, inplace: bool=False): | def __init__(self, inplace: bool=False): | ||||
super(Hardsigmoid, self).__init__() | super(Hardsigmoid, self).__init__() | ||||
_inplace_limit_pynative(inplace, "Hardsigmoid") | |||||
self.inplace = inplace | self.inplace = inplace | ||||
def forward(self, input): | def forward(self, input): | ||||
@@ -1,11 +1,10 @@ | |||||
#!/usr/bin/env python | #!/usr/bin/env python | ||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
import mindspore as ms | |||||
from mindspore.common.seed import _get_graph_seed | from mindspore.common.seed import _get_graph_seed | ||||
from mindspore.ops import operations as P | from mindspore.ops import operations as P | ||||
from ms_adapter.pytorch.tensor import cast_to_ms_tensor, cast_to_adapter_tensor | |||||
from ms_adapter.pytorch.tensor import cast_to_ms_tensor | |||||
import ms_adapter.pytorch.nn.functional as ms_torch_nn_func | import ms_adapter.pytorch.nn.functional as ms_torch_nn_func | ||||
from ms_adapter.pytorch.common._inner import _inplace_assign, _inplace_limit_pynative | |||||
from .module import Module | from .module import Module | ||||
__all__ = ['Dropout', 'Dropout2d', 'Dropout3d', 'AlphaDropout', 'FeatureAlphaDropout'] | __all__ = ['Dropout', 'Dropout2d', 'Dropout3d', 'AlphaDropout', 'FeatureAlphaDropout'] | ||||
@@ -55,15 +54,12 @@ class Dropout(_DropoutNd): | |||||
""" | """ | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "Dropout") | |||||
super(Dropout, self).__init__(p, inplace) | super(Dropout, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.keep_prob = 1.0 - self.p | self.keep_prob = 1.0 - self.p | ||||
seed0, seed1 = _get_graph_seed(0, "dropout") | seed0, seed1 = _get_graph_seed(0, "dropout") | ||||
self.seed0 = seed0 | self.seed0 = seed0 | ||||
@@ -79,10 +75,7 @@ class Dropout(_DropoutNd): | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output, _ = self.dropout(input_ms) | output, _ = self.dropout(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
class Dropout2d(_DropoutNd): | class Dropout2d(_DropoutNd): | ||||
@@ -123,13 +116,12 @@ class Dropout2d(_DropoutNd): | |||||
https://arxiv.org/abs/1411.4280 | https://arxiv.org/abs/1411.4280 | ||||
""" | """ | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "Dropout2d") | |||||
super(Dropout2d, self).__init__(p, inplace) | super(Dropout2d, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.keep_prob = 1.0 - self.p | self.keep_prob = 1.0 - self.p | ||||
self.dropout2d = P.Dropout2D(self.keep_prob) | self.dropout2d = P.Dropout2D(self.keep_prob) | ||||
@@ -142,10 +134,7 @@ class Dropout2d(_DropoutNd): | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output, _ = self.dropout2d(input_ms) | output, _ = self.dropout2d(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
class Dropout3d(_DropoutNd): | class Dropout3d(_DropoutNd): | ||||
@@ -187,13 +176,12 @@ class Dropout3d(_DropoutNd): | |||||
""" | """ | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "Dropout3d") | |||||
super(Dropout3d, self).__init__(p, inplace) | super(Dropout3d, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
self.keep_prob = 1.0 - self.p | self.keep_prob = 1.0 - self.p | ||||
self.dropout3d = P.Dropout3D(self.keep_prob) | self.dropout3d = P.Dropout3D(self.keep_prob) | ||||
@@ -206,61 +194,39 @@ class Dropout3d(_DropoutNd): | |||||
input_ms = cast_to_ms_tensor(input) | input_ms = cast_to_ms_tensor(input) | ||||
output, _ = self.dropout3d(input_ms) | output, _ = self.dropout3d(input_ms) | ||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return cast_to_adapter_tensor(output) | |||||
return _inplace_assign(input, self.inplace, output) | |||||
class AlphaDropout(_DropoutNd): | class AlphaDropout(_DropoutNd): | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "AlphaDropout") | |||||
super(AlphaDropout, self).__init__(p, inplace) | super(AlphaDropout, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
output = ms_torch_nn_func.alpha_dropout(input, self.p, self.training, self.inplace) | |||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return output | |||||
return ms_torch_nn_func.alpha_dropout(input, self.p, self.training, self.inplace) | |||||
class FeatureAlphaDropout(_DropoutNd): | class FeatureAlphaDropout(_DropoutNd): | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "FeatureAlphaDropout") | |||||
super(FeatureAlphaDropout, self).__init__(p, inplace) | super(FeatureAlphaDropout, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
output = ms_torch_nn_func.feature_alpha_dropout(input, self.p, self.training, self.inplace) | |||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return output | |||||
return ms_torch_nn_func.feature_alpha_dropout(input, self.p, self.training, self.inplace) | |||||
class Dropout1d(_DropoutNd): | class Dropout1d(_DropoutNd): | ||||
def __init__(self, p=0.5, inplace=False): | def __init__(self, p=0.5, inplace=False): | ||||
_inplace_limit_pynative(inplace, "Dropout1d") | |||||
super(Dropout1d, self).__init__(p, inplace) | super(Dropout1d, self).__init__(p, inplace) | ||||
if p < 0 or p > 1: | if p < 0 or p > 1: | ||||
raise ValueError("dropout probability has to be between 0 and 1, " | raise ValueError("dropout probability has to be between 0 and 1, " | ||||
"but got {}".format(p)) | "but got {}".format(p)) | ||||
if inplace and ms.get_context('mode') != ms.PYNATIVE_MODE: | |||||
raise NotImplementedError('keyword argument `inplace` in `{}` is only valid in ' | |||||
'PYNATIVE_MODE.'.format(self.__class__.__name__)) | |||||
def forward(self, input): | def forward(self, input): | ||||
output = ms_torch_nn_func.dropout1d(input, self.p, self.training, self.inplace) | |||||
if self.inplace: | |||||
input.assign_value(output) | |||||
return input | |||||
return output | |||||
return ms_torch_nn_func.dropout1d(input, self.p, self.training, self.inplace) |
@@ -130,7 +130,7 @@ class Bilinear(Module): | |||||
init.uniform_(self.bias, -bound, bound) | init.uniform_(self.bias, -bound, bound) | ||||
def forward(self, input1, input2): | def forward(self, input1, input2): | ||||
x = self.matmul(input1, self.weight.transpose(1, 0, 2).reshape(self.weight.shape[1], -1)) | |||||
x = self.matmul(input1, self.weight.permute(1, 0, 2).reshape(self.weight.shape[1], -1)) | |||||
x = self.mul(x, self.tile(input2, (1, self.out_features))) | x = self.mul(x, self.tile(input2, (1, self.out_features))) | ||||
x = x.reshape(input1.shape[0], self.out_features, -1) | x = x.reshape(input1.shape[0], self.out_features, -1) | ||||
x = self.reducesum(x, 2) | x = self.reducesum(x, 2) | ||||
@@ -9,7 +9,7 @@ from mindspore.common import dtype as mstype | |||||
from mindspore.common._register_for_tensor import tensor_operator_registry | from mindspore.common._register_for_tensor import tensor_operator_registry | ||||
import mindspore.ops as P | import mindspore.ops as P | ||||
from mindspore.ops import constexpr | from mindspore.ops import constexpr | ||||
from mindspore.common.initializer import _assignment, _init_random_normal, _init_random_uniform | |||||
from mindspore.common.initializer import _init_random_normal, _init_random_uniform | |||||
from mindspore._checkparam import Validator as validator | from mindspore._checkparam import Validator as validator | ||||
from mindspore.common.initializer import Zero | from mindspore.common.initializer import Zero | ||||
from ms_adapter.utils import unsupported_attr, pynative_mode_condition, is_under_gpu_context, get_backend | from ms_adapter.utils import unsupported_attr, pynative_mode_condition, is_under_gpu_context, get_backend | ||||
@@ -290,19 +290,26 @@ class Tensor(ms.Tensor): | |||||
out = tensor_ms.__getitem__(index) | out = tensor_ms.__getitem__(index) | ||||
return cast_to_adapter_tensor(out) | return cast_to_adapter_tensor(out) | ||||
def fill_(self, val): | |||||
def fill_adapter(self, val): | |||||
if not isinstance(val, (int, float, bool)): | if not isinstance(val, (int, float, bool)): | ||||
raise TypeError("For 'Tensor.fill', the type of the argument 'value' must be int, float or bool, " | raise TypeError("For 'Tensor.fill', the type of the argument 'value' must be int, float or bool, " | ||||
"but got {}.".format(type(val))) | "but got {}.".format(type(val))) | ||||
output = tensor_operator_registry.get("fill")(self.dtype, self.shape, val) | output = tensor_operator_registry.get("fill")(self.dtype, self.shape, val) | ||||
_assignment(self, output) | |||||
return cast_to_adapter_tensor(output) | |||||
def normal_(self, mean=0, std=1, *, generator=None): | |||||
def fill_(self, val): | |||||
output = self.fill_adapter(val) | |||||
return _tensor_inplace_assign(self, output, "fill_", "fill_adapter") | |||||
def normal_adapter(self, mean=0, std=1, *, generator=None): | |||||
if generator is not None: | if generator is not None: | ||||
raise ValueError("`generator` can not be supportted.") | raise ValueError("`generator` can not be supportted.") | ||||
output = ms.Tensor(_init_random_normal(mean, std, self.shape), ms.float32) | output = ms.Tensor(_init_random_normal(mean, std, self.shape), ms.float32) | ||||
_assignment(self, output) | |||||
return self | |||||
return cast_to_adapter_tensor(output) | |||||
def normal_(self, mean=0, std=1, *, generator=None): | |||||
output = self.normal_adapter(mean, std, generator=generator) | |||||
return _tensor_inplace_assign(self, output, "normal_", "normal_adapter") | |||||
def size(self, dim=None): | def size(self, dim=None): | ||||
""" | """ | ||||
@@ -313,16 +320,16 @@ class Tensor(ms.Tensor): | |||||
return self.shape | return self.shape | ||||
return self.shape[dim] | return self.shape[dim] | ||||
def uniform_(self, from_alias=0, to=1): #TODO: from_alias->from | |||||
raise NotImplementedError("Tensor.uniform_ is not supported, please call Tensor.uniform to replace, " | |||||
"which is a other than in-place version.") | |||||
def uniform(self, from_alias=0, to=1): #TODO: from_alias->from | |||||
def uniform_adpater(self, from_alias=0, to=1): #TODO: from_alias->from | |||||
self_dtype = self.dtype | self_dtype = self.dtype | ||||
output = ms.Tensor(_init_random_uniform(from_alias, to, self.shape), self_dtype) | output = ms.Tensor(_init_random_uniform(from_alias, to, self.shape), self_dtype) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def random(self, from_alias=0, to=None, *, generator=None): #TODO: from_alias->from | |||||
def uniform_(self, from_alias=0, to=1): | |||||
output = self.uniform_adpater(from_alias, to) | |||||
return _tensor_inplace_assign(self, output, "uniform_", "uniform_adpater") | |||||
def random_adapter(self, from_alias=0, to=None, *, generator=None): #TODO: from_alias->from | |||||
unsupported_attr(generator) | unsupported_attr(generator) | ||||
if generator: | if generator: | ||||
raise NotImplementedError("generator is not supported.") | raise NotImplementedError("generator is not supported.") | ||||
@@ -331,33 +338,40 @@ class Tensor(ms.Tensor): | |||||
if not to: | if not to: | ||||
if self_dtype == ms.float64: | if self_dtype == ms.float64: | ||||
return self.uniform(from_alias, kMantissaFloat64) | |||||
return self.uniform_adpater(from_alias, kMantissaFloat64) | |||||
elif self_dtype == ms.float32: | elif self_dtype == ms.float32: | ||||
return self.uniform(from_alias, kMantissaFloat32) | |||||
return self.uniform_adpater(from_alias, kMantissaFloat32) | |||||
elif self_dtype == ms.float16: | elif self_dtype == ms.float16: | ||||
return self.uniform(from_alias, kMantissaFloat16) | |||||
return self.uniform_adpater(from_alias, kMantissaFloat16) | |||||
elif self_dtype == ms.uint64: | elif self_dtype == ms.uint64: | ||||
return self.uniform(from_alias, kMaxUint64) | |||||
return self.uniform_adpater(from_alias, kMaxUint64) | |||||
elif self_dtype == ms.uint32: | elif self_dtype == ms.uint32: | ||||
return self.uniform(from_alias, kMaxUint32) | |||||
return self.uniform_adpater(from_alias, kMaxUint32) | |||||
elif self_dtype == ms.uint16: | elif self_dtype == ms.uint16: | ||||
return self.uniform(from_alias, kMaxUint16) | |||||
return self.uniform_adpater(from_alias, kMaxUint16) | |||||
elif self_dtype == ms.uint8: | elif self_dtype == ms.uint8: | ||||
return self.uniform(from_alias, kMaxUint8) | |||||
return self.uniform_adpater(from_alias, kMaxUint8) | |||||
elif self_dtype == ms.int64: | elif self_dtype == ms.int64: | ||||
return self.uniform(from_alias, kMaxInt64) | |||||
return self.uniform_adpater(from_alias, kMaxInt64) | |||||
elif self_dtype == ms.int32: | elif self_dtype == ms.int32: | ||||
return self.uniform(from_alias, kMaxInt32) | |||||
return self.uniform_adpater(from_alias, kMaxInt32) | |||||
elif self_dtype == ms.int16: | elif self_dtype == ms.int16: | ||||
return self.uniform(from_alias, kMaxInt16) | |||||
return self.uniform_adpater(from_alias, kMaxInt16) | |||||
elif self_dtype == ms.int8: | elif self_dtype == ms.int8: | ||||
return self.uniform(from_alias, kMaxInt8) | |||||
return self.uniform(from_alias, to) | |||||
return self.uniform_adpater(from_alias, kMaxInt8) | |||||
return self.uniform_adpater(from_alias, to) | |||||
def zero_(self): | |||||
def random_(self, from_alias=0, to=None, *, generator=None): | |||||
output = self.random_adapter(from_alias, to, generator=generator) | |||||
return _tensor_inplace_assign(self, output, "random_", "random_adapter") | |||||
def zero_adapter(self): | |||||
output = tensor_operator_registry.get("fill")(self.dtype, self.shape, 0.0) | output = tensor_operator_registry.get("fill")(self.dtype, self.shape, 0.0) | ||||
_assignment(self, output) | |||||
return self | |||||
return cast_to_adapter_tensor(output) | |||||
def zero_(self): | |||||
output = self.zero_adapter() | |||||
return _tensor_inplace_assign(self, output, "zero_", "zero_adapter") | |||||
def new_zeros(self, size, *, dtype=None, device=None, requires_grad=False, layout=None, pin_memory=False): | def new_zeros(self, size, *, dtype=None, device=None, requires_grad=False, layout=None, pin_memory=False): | ||||
unsupported_attr(device) | unsupported_attr(device) | ||||
@@ -372,40 +386,24 @@ class Tensor(ms.Tensor): | |||||
output = tensor_operator_registry.get("fill")(dtype, size, 0.0) | output = tensor_operator_registry.get("fill")(dtype, size, 0.0) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def add_(self, other): | |||||
def add(self, other, *, alpha=1): | |||||
input = cast_to_ms_tensor(self) | input = cast_to_ms_tensor(self) | ||||
other = cast_to_ms_tensor(other) | other = cast_to_ms_tensor(other) | ||||
output = tensor_operator_registry.get('__add__')(input, other) | |||||
_assignment(self, output) | |||||
return self | |||||
output = ms.ops.add(input, other*alpha) | |||||
return cast_to_adapter_tensor(output) | |||||
def mul_(self, other): | |||||
input = cast_to_ms_tensor(self) | |||||
other = cast_to_ms_tensor(other) | |||||
output = tensor_operator_registry.get('__mul__')(input, other) | |||||
_assignment(self, output) | |||||
return self | |||||
def add_(self, other, *, alpha=1): | |||||
output = self.add(other, alpha=alpha) | |||||
return _tensor_inplace_assign(self, output, "add_", "add") | |||||
def erfinv_(self): | |||||
def erfinv(self): | |||||
input = cast_to_ms_tensor(self) | input = cast_to_ms_tensor(self) | ||||
output = P.Erfinv()(input) | |||||
_assignment(self, output) | |||||
return self | |||||
output = ms.ops.erfinv(input) | |||||
return cast_to_adapter_tensor(output) | |||||
def clamp_(self, min = None, max = None): | |||||
if min is None and max is None: | |||||
raise ValueError("min and max should not both be None.") | |||||
if min is not None: | |||||
min = ms.Tensor(min) | |||||
if max is not None: | |||||
max = ms.Tensor(max) | |||||
input = cast_to_ms_tensor(self) | |||||
if min is not None and max is not None and min > max: | |||||
output = ms.ops.ones_like(input).astype(input.dtype) * max | |||||
else: | |||||
output = P.clip_by_value(input, clip_value_min=min, clip_value_max=max) | |||||
_assignment(self, output) | |||||
return self | |||||
def erfinv_(self): | |||||
output = self.erfinv() | |||||
return _tensor_inplace_assign(self, output, "erfinv_", "erfinv") | |||||
def permute(self, *dims): | def permute(self, *dims): | ||||
ms_input = cast_to_ms_tensor(self) | ms_input = cast_to_ms_tensor(self) | ||||
@@ -417,10 +415,23 @@ class Tensor(ms.Tensor): | |||||
unsupported_attr(memory_format) | unsupported_attr(memory_format) | ||||
return self | return self | ||||
def new_tensor(self, data, *, dtype=None, device=None, requires_grad=False, layout=None, pin_memory=False): | |||||
unsupported_attr(device) | |||||
unsupported_attr(requires_grad) | |||||
unsupported_attr(layout) | |||||
unsupported_attr(pin_memory) | |||||
if isinstance(data, Tensor): | |||||
raise ValueError("To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() " | |||||
"or sourceTensor.clone().detach().requires_grad_(True), " | |||||
"rather than tensor.new_tensor(sourceTensor).") | |||||
return tensor(data, dtype) | |||||
def copy_(self, src, non_blocking=False): | def copy_(self, src, non_blocking=False): | ||||
unsupported_attr(non_blocking) | unsupported_attr(non_blocking) | ||||
self.assign_value(src) | |||||
return self | |||||
input_ms = cast_to_ms_tensor(src) | |||||
output = ms.ops.broadcast_to(input_ms, self.shape) | |||||
output = output.astype(self.dtype) | |||||
return _tensor_inplace_assign(self, output, "copy_", "new_tensor") | |||||
def expand(self, *size): | def expand(self, *size): | ||||
@constexpr | @constexpr | ||||
@@ -446,9 +457,8 @@ class Tensor(ms.Tensor): | |||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def sigmoid_(self): | def sigmoid_(self): | ||||
input = cast_to_ms_tensor(self) | |||||
self.assign_value(P.Sigmoid()(input)) | |||||
return self | |||||
output = self.sigmoid() | |||||
return _tensor_inplace_assign(self, output, "sigmoid_", "sigmoid") | |||||
def float(self, memory_format=None): | def float(self, memory_format=None): | ||||
unsupported_attr(memory_format) | unsupported_attr(memory_format) | ||||
@@ -473,6 +483,10 @@ class Tensor(ms.Tensor): | |||||
output = P.mul(input, ms_value) | output = P.mul(input, ms_value) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def mul_(self, value): | |||||
output = self.mul(value) | |||||
return _tensor_inplace_assign(self, output, "mul_", "mul") | |||||
def device(self): | def device(self): | ||||
#TODO | #TODO | ||||
pass | pass | ||||
@@ -493,6 +507,10 @@ class Tensor(ms.Tensor): | |||||
result = P.div(input, value) | result = P.div(input, value) | ||||
return cast_to_adapter_tensor(result) | return cast_to_adapter_tensor(result) | ||||
def div_(self, value, rounding_mode=None): | |||||
output = self.div(value, rounding_mode) | |||||
return _tensor_inplace_assign(self, output, "div_", "div") | |||||
def cpu(self): | def cpu(self): | ||||
#TODO | #TODO | ||||
return self | return self | ||||
@@ -719,14 +737,17 @@ class Tensor(ms.Tensor): | |||||
return cast_to_adapter_tensor(input_ms.negative()) | return cast_to_adapter_tensor(input_ms.negative()) | ||||
def negative_(self): | def negative_(self): | ||||
input_ms = cast_to_ms_tensor(self) | |||||
output = input_ms.negative() | |||||
_assignment(self, output) | |||||
output = self.negative() | |||||
return _tensor_inplace_assign(self, output, "negative_", "negative") | |||||
def abs(self): | def abs(self): | ||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
return cast_to_adapter_tensor(input_ms.abs()) | return cast_to_adapter_tensor(input_ms.abs()) | ||||
def abs_(self): | |||||
output = self.abs() | |||||
return _tensor_inplace_assign(self, output, "abs_", "abs") | |||||
@property | @property | ||||
def ndim(self): | def ndim(self): | ||||
return len(self.shape) | return len(self.shape) | ||||
@@ -776,6 +797,10 @@ class Tensor(ms.Tensor): | |||||
output = ms.ops.clip_by_value(input_ms, min, max) | output = ms.ops.clip_by_value(input_ms, min, max) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def clamp_(self, min=None, max=None): | |||||
output = self.clamp(min, max) | |||||
return _tensor_inplace_assign(self, output, "clamp_", "clamp") | |||||
def dim(self): | def dim(self): | ||||
return len(self.shape) | return len(self.shape) | ||||
@@ -813,6 +838,10 @@ class Tensor(ms.Tensor): | |||||
output = ms.ops.squeeze(input_ms) | output = ms.ops.squeeze(input_ms) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def squeeze_(self, dim=None): | |||||
output = self.squeeze(dim) | |||||
return _tensor_inplace_assign(self, output, "squeeze_", "squeeze") | |||||
def stride(self, dim=None): | def stride(self, dim=None): | ||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
bytelen = input_ms.nbytes//input_ms.size | bytelen = input_ms.nbytes//input_ms.size | ||||
@@ -830,6 +859,10 @@ class Tensor(ms.Tensor): | |||||
output = ms.ops.sub(input_ms, input_other) | output = ms.ops.sub(input_ms, input_other) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def sub_(self, other, *, alpha=1): | |||||
output = self.sub(other, alpha=alpha) | |||||
return _tensor_inplace_assign(self, output, "sub_", "sub") | |||||
# TODO: delete it, apply ms.Tensor.is_floating_point | # TODO: delete it, apply ms.Tensor.is_floating_point | ||||
def is_floating_point(self): | def is_floating_point(self): | ||||
return self._dtype in (mstype.float16, mstype.float32, mstype.float64) | return self._dtype in (mstype.float16, mstype.float32, mstype.float64) | ||||
@@ -842,6 +875,10 @@ class Tensor(ms.Tensor): | |||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
return cast_to_adapter_tensor(input_ms.unsqueeze(dim)) | return cast_to_adapter_tensor(input_ms.unsqueeze(dim)) | ||||
def unsqueeze_(self, dim): | |||||
output = self.unsqueeze(dim) | |||||
return _tensor_inplace_assign(self, output, "unsqueeze_", "unsqueeze") | |||||
def is_signed(self): | def is_signed(self): | ||||
# input_ms = cast_to_ms_tensor(self) | # input_ms = cast_to_ms_tensor(self) | ||||
# return input_ms.is_signed() #TODO mindspore 11/17 2.0nightly supported | # return input_ms.is_signed() #TODO mindspore 11/17 2.0nightly supported | ||||
@@ -854,11 +891,19 @@ class Tensor(ms.Tensor): | |||||
output = input_ms.transpose(dims) | output = input_ms.transpose(dims) | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def transpose_(self, dim0, dim1): | |||||
output = self.transpose(dim0, dim1) | |||||
return _tensor_inplace_assign(self, output, "transpose_", "transpose") | |||||
def floor(self): | def floor(self): | ||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
output = input_ms.floor() | output = input_ms.floor() | ||||
return cast_to_adapter_tensor(output) | return cast_to_adapter_tensor(output) | ||||
def floor_(self): | |||||
output = self.floor() | |||||
return _tensor_inplace_assign(self, output, "floor_", "floor") | |||||
def isfinite(self): | def isfinite(self): | ||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
output = ms.ops.isfinite(input_ms) | output = ms.ops.isfinite(input_ms) | ||||
@@ -927,6 +972,10 @@ class Tensor(ms.Tensor): | |||||
input_ms = cast_to_ms_tensor(self) | input_ms = cast_to_ms_tensor(self) | ||||
return cast_to_adapter_tensor(ms.ops.sqrt(input_ms)) | return cast_to_adapter_tensor(ms.ops.sqrt(input_ms)) | ||||
def sqrt_(self): | |||||
output = self.sqrt() | |||||
return _tensor_inplace_assign(self, output, "sqrt_", "sqrt") | |||||
def resize(self, *size, memory_format=None): | def resize(self, *size, memory_format=None): | ||||
unsupported_attr(memory_format) | unsupported_attr(memory_format) | ||||
input = cast_to_ms_tensor(self) | input = cast_to_ms_tensor(self) | ||||
@@ -934,9 +983,8 @@ class Tensor(ms.Tensor): | |||||
return cast_to_adapter_tensor(out) | return cast_to_adapter_tensor(out) | ||||
def resize_(self, *size, memory_format=None): | def resize_(self, *size, memory_format=None): | ||||
# TODO: warning for not support inplace resize | |||||
unsupported_attr(memory_format) | |||||
return self.resize(*size) | |||||
output = self.resize(*size, memory_format=memory_format) | |||||
return _tensor_inplace_assign(self, output, "resize_", "resize") | |||||
def resize_as(self, tensor, memory_format=None): | def resize_as(self, tensor, memory_format=None): | ||||
unsupported_attr(memory_format) | unsupported_attr(memory_format) | ||||
@@ -946,13 +994,8 @@ class Tensor(ms.Tensor): | |||||
return cast_to_adapter_tensor(out) | return cast_to_adapter_tensor(out) | ||||
def resize_as_(self, tensor, memory_format=None): | def resize_as_(self, tensor, memory_format=None): | ||||
# TODO: warning for not support inplace resize_as | |||||
unsupported_attr(memory_format) | |||||
return self.resize_as(tensor) | |||||
def squeeze_(self, dim=None): | |||||
# TODO: warning for not support inplace squeeze_ | |||||
return self.squeeze(dim) | |||||
output = self.resize_as(tensor, memory_format) | |||||
return _tensor_inplace_assign(self, output, "resize_as_", "resize_as") | |||||
def index_fill(self, dim, index, value): | def index_fill(self, dim, index, value): | ||||
input = cast_to_ms_tensor(self) | input = cast_to_ms_tensor(self) | ||||
@@ -962,8 +1005,8 @@ class Tensor(ms.Tensor): | |||||
return cast_to_adapter_tensor(out) | return cast_to_adapter_tensor(out) | ||||
def index_fill_(self, dim, index, value): | def index_fill_(self, dim, index, value): | ||||
# TODO: warning for not support inplace index_fill_ | |||||
return self.index_fill(dim, index, value) | |||||
output = self.index_fill(dim, index, value) | |||||
return _tensor_inplace_assign(self, output, "index_fill_", "index_fill") | |||||
def index_select(self, dim, index): | def index_select(self, dim, index): | ||||
_input_params = cast_to_ms_tensor(self) | _input_params = cast_to_ms_tensor(self) | ||||
@@ -1276,3 +1319,13 @@ def cast_tensor(func): | |||||
return result | return result | ||||
return cast_function | return cast_function | ||||
def _tensor_inplace_assign(input, output, op_name, replace_op): | |||||
if pynative_mode_condition(): # TODO: ms_function | |||||
input.assign_value(output) | |||||
return input | |||||
raise RuntimeError('`Tensor.{a}` is an in-place operation and "x.{a}()" is not encouraged to use ' | |||||
'in MindSpore static graph mode. Please use "x = x.{b}()" or other API ' | |||||
'instead.'.format(a=op_name, b=replace_op)) |
@@ -14,6 +14,10 @@ def unsupported_attr(attr): | |||||
def pynative_mode_condition(): | def pynative_mode_condition(): | ||||
return context.get_context("mode") == context.PYNATIVE_MODE | return context.get_context("mode") == context.PYNATIVE_MODE | ||||
@constexpr | |||||
def graph_mode_condition(): | |||||
return context.get_context("mode") == context.GRAPH_MODE | |||||
@constexpr | @constexpr | ||||
def get_backend(): | def get_backend(): | ||||
return context.get_context("device_target") | return context.get_context("device_target") | ||||
@@ -0,0 +1,78 @@ | |||||
import numpy as np | |||||
import mindspore as ms | |||||
from mindspore import context | |||||
import ms_adapter.pytorch as ms_torch | |||||
import ms_adapter.pytorch.nn as nn | |||||
def test_tensor_inplace(): | |||||
x = ms_torch.tensor([1.0, 2.0]) | |||||
y = -1.0 | |||||
try: | |||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
out = x.add_(y) | |||||
assert (id(out) != id(x)) | |||||
except RuntimeError as e: | |||||
assert "is not encouraged to use in MindSpore static graph mode" in str(e) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
out = x.add_(y) | |||||
assert (id(out) == id(x)) | |||||
assert np.allclose(out.numpy(), [0., 1.0]) | |||||
def test_nn_inplace(): | |||||
input = ms_torch.randn(2) | |||||
try: | |||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
nn_relu = nn.ReLU(inplace=True) | |||||
output = nn_relu(input) | |||||
assert (id(output) != id(input)) | |||||
except ValueError as e: | |||||
assert "please set inplace=False and use return value instead" in str(e) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
nn_relu = nn.ReLU(inplace=True) | |||||
output = nn_relu(input) | |||||
assert (id(output) == id(input)) | |||||
assert np.allclose(output.numpy(), input.numpy()) | |||||
def test_out_param(): | |||||
x = ms_torch.tensor([10.0, 2.0]) | |||||
out = ms_torch.tensor([1.0, 1.0]) | |||||
try: | |||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
output = ms_torch.log(x, out=out) | |||||
assert (id(output) != id(out)) | |||||
except ValueError as e: | |||||
assert "please set out=None and use return value instead" in str(e) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
output = ms_torch.log(x, out=out) | |||||
assert (id(out) == id(output)) | |||||
assert np.allclose(output.numpy(), out.numpy()) | |||||
def test_inplace_param(): | |||||
x = ms_torch.tensor([-1, -2, 0, 2, 1], dtype=ms_torch.float16) | |||||
try: | |||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
output = nn.functional.relu6(x, inplace=True) | |||||
assert (id(output) != id(x)) | |||||
except ValueError as e: | |||||
assert "please set inplace=False and use return value instead" in str(e) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
output = nn.functional.relu6(x, inplace=True) | |||||
assert (id(x) == id(output)) | |||||
assert np.allclose(output.numpy(), x.numpy()) | |||||
if __name__ == '__main__': | |||||
test_tensor_inplace() | |||||
test_nn_inplace() | |||||
test_out_param() | |||||
test_inplace_param() | |||||
@@ -7,10 +7,11 @@ import numpy as np | |||||
import mindspore as ms | import mindspore as ms | ||||
import torch | import torch | ||||
context.set_context(mode=ms.GRAPH_MODE) | |||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
def test_linear_model(): | def test_linear_model(): | ||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
class LinearModel(Module): | class LinearModel(Module): | ||||
def __init__(self): | def __init__(self): | ||||
super(LinearModel, self).__init__() | super(LinearModel, self).__init__() | ||||
@@ -59,6 +60,7 @@ def test_identity(): | |||||
def test_bilinear_model(): | def test_bilinear_model(): | ||||
context.set_context(mode=ms.PYNATIVE_MODE) | |||||
class BilinearModel(Module): | class BilinearModel(Module): | ||||
def __init__(self): | def __init__(self): | ||||
super(BilinearModel, self).__init__() | super(BilinearModel, self).__init__() | ||||
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》