From d03cd37975d68d23dfda93599024005059d8208c Mon Sep 17 00:00:00 2001 From: lvyufeng Date: Mon, 21 Jul 2025 13:37:16 +0800 Subject: [PATCH] fix g class ut --- mindnlp/core/__init__.py | 1 + mindnlp/core/_tensor.py | 12 ++++++++++++ mindnlp/core/ops/creation.py | 5 ++++- mindnlp/core/ops/inplace.py | 6 ++---- mindnlp/core/ops/other.py | 5 +++++ mindnlp/core/special/__init__.py | 4 ++++ 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 mindnlp/core/special/__init__.py diff --git a/mindnlp/core/__init__.py b/mindnlp/core/__init__.py index 46284f102..51c8b9488 100644 --- a/mindnlp/core/__init__.py +++ b/mindnlp/core/__init__.py @@ -29,6 +29,7 @@ strided = None contiguous_format = None preserve_format = None +legacy_contiguous_format = None inf = float("inf") nan = float("nan") diff --git a/mindnlp/core/_tensor.py b/mindnlp/core/_tensor.py index a654c3d3e..66388b39c 100644 --- a/mindnlp/core/_tensor.py +++ b/mindnlp/core/_tensor.py @@ -558,6 +558,18 @@ def bfloat16(self): Tensor.bfloat16 = bfloat16 StubTensor.bfloat16 = bfloat16 + def sort(self, dim=-1, descending=False): + return ops.sort(self, dim=dim, descending=descending) + + Tensor.sort = sort + StubTensor.sort = sort + + Tensor.cumsum = ops.cumsum + StubTensor.cumsum = ops.cumsum + + Tensor.scatter_ = ops.inplace_scatter + StubTensor.scatter_ = ops.inplace_scatter + def _rebuild_from_type_v2(func, new_type, args, state): ret = func(*args) return ret \ No newline at end of file diff --git a/mindnlp/core/ops/creation.py b/mindnlp/core/ops/creation.py index 17ec23bab..533119499 100644 --- a/mindnlp/core/ops/creation.py +++ b/mindnlp/core/ops/creation.py @@ -129,6 +129,9 @@ def range(start=0, end=None, step=1, dtype=None): def linspace(start, end, steps, *, dtype=None, **kwargs): if dtype is None: dtype = mindspore.float32 + start = start.item() if isinstance(start, mindspore.Tensor) else start + end = end.item() if isinstance(end, mindspore.Tensor) else end + steps = steps.item() if isinstance(steps, mindspore.Tensor) else steps if use_pyboost() and has_linspace: return mindspore.mint.linspace(start, end, steps, dtype=dtype) return ops.linspace(start, end, steps).to(dtype) @@ -139,7 +142,7 @@ def logspace(start, end, steps, base=10.0, *, dtype=None): # eye has_eye = hasattr(mindspore.mint, 'eye') -def eye(n, m=None, *, dtype=None): +def eye(n, m=None, *, dtype=None, **kwargs): if use_pyboost() and has_eye: return mindspore.mint.eye(n, m, dtype) return ops.eye(n, m, dtype) diff --git a/mindnlp/core/ops/inplace.py b/mindnlp/core/ops/inplace.py index 544d83b79..396f414a7 100644 --- a/mindnlp/core/ops/inplace.py +++ b/mindnlp/core/ops/inplace.py @@ -2,7 +2,7 @@ from mindspore import ops from mindspore.ops._primitive_cache import _get_cache_prim from mindspore.common.generator import default_generator -from mindspore.ops.auto_generate.gen_ops_prim import inplace_normal_op +from mindspore.ops.auto_generate.gen_ops_prim import inplace_normal_op, inplace_scatter_value_op from mindnlp import core from ..configs import use_pyboost @@ -85,9 +85,7 @@ def inplace_add(input, other, alpha): return input def inplace_scatter(input, dim, index, src): - if not isinstance(src, core.Tensor): - return execute('inplace_scatter_value', input, dim, index, src) - return execute('inplace_scatter', input, dim, index, src) + return inplace_scatter_value_op(input, dim, index, src) def inplace_index_copy(input, dim, index, tensor): selected = input.index_select(dim, index) diff --git a/mindnlp/core/ops/other.py b/mindnlp/core/ops/other.py index aee4c6eb3..2fa4cfcbf 100644 --- a/mindnlp/core/ops/other.py +++ b/mindnlp/core/ops/other.py @@ -151,6 +151,9 @@ def clone(input): def cumsum(input, dim, dtype=None, out=None): + input_dtype = input.dtype + if input_dtype == mindspore.int64: + input = input.to(mindspore.int32) if ( use_pyboost() and has_cumsum and not ON_ORANGE_PI ): # since cann8.0 community remove aclnn cumsum @@ -161,6 +164,8 @@ def cumsum(input, dim, dtype=None, out=None): output = ops.cumsum(input, dim, dtype) if out is not None: out.assign_value(output) + return out + output = output.to(input_dtype) return output diff --git a/mindnlp/core/special/__init__.py b/mindnlp/core/special/__init__.py new file mode 100644 index 000000000..fc5983c91 --- /dev/null +++ b/mindnlp/core/special/__init__.py @@ -0,0 +1,4 @@ +from mindspore import ops + +def logit(input, eps=None, *, out=None): + return ops.logit(input, eps)