Skip to content

fix diffuser models ut #2080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mindnlp/core/_C/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ def device(self):
default_generator = Generator()

class Tag: pass

def _log_api_usage_once(*args):
pass
1 change: 1 addition & 0 deletions mindnlp/core/_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __gt__(self, other):
bfloat16 : 2,
float32 : 4,
float64 : 8,
complex64: 8
}

np2dtype = {
Expand Down
16 changes: 16 additions & 0 deletions mindnlp/core/_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def detach(self):

origin_getitem = Tensor.__getitem__
def __getitem__(self, slices):
slices = self._convert_numpy_slices(slices)
# if 0 in self.shape:
# return self
if isinstance(slices, tuple):
Expand Down Expand Up @@ -308,6 +309,8 @@ def __setitem__(self, slices, value):
value = tensor(value, dtype=self.dtype)
else:
value = value.to(self.dtype)
if 1 in value.shape and self[slices].ndim != value.ndim:
value = value.squeeze()
return origin_setitem(self, slices, value)

Tensor.__setitem__ = __setitem__
Expand Down Expand Up @@ -588,6 +591,13 @@ def real(self):
Tensor.real = real
StubTensor.real = real

@property
def imag(self):
return ops.imag(self)

Tensor.imag = imag
StubTensor.imag = imag

def bfloat16(self):
return self.to(_dtype.bfloat16)

Expand Down Expand Up @@ -636,6 +646,12 @@ def __contains__(self, item):
Tensor.round_ = ops.inplace_round
StubTensor.round_ = ops.inplace_round

Tensor.split_with_sizes = ops.split_with_sizes
StubTensor.split_with_sizes = ops.split_with_sizes

Tensor.scatter_reduce_ = ops.inplace_scatter_reduce
StubTensor.scatter_reduce_ = ops.inplace_scatter_reduce

def _rebuild_from_type_v2(func, new_type, args, state):
ret = func(*args)
return ret
3 changes: 3 additions & 0 deletions mindnlp/core/linalg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ def cholesky_ex(A, *, upper=False, check_errors=False, out=None):

def norm(A, ord=None, dim=None, keepdim=False, *, out=None, dtype=None):
return mint.norm(A, 2 if ord is None else ord, dim, keepdim, dtype=dtype)

def vector_norm(x, ord=2, dim=None, keepdim=False, *, dtype=None, out=None):
return mint.linalg.vector_norm(x, ord, dim, keepdim, dtype=dtype)
14 changes: 13 additions & 1 deletion mindnlp/core/nn/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ def avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, coun

return ops.avg_pool2d(input, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override)

has_avg_pool3d = hasattr(mint.nn.functional, 'avg_pool3d')
def avg_pool3d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None):
if use_pyboost() and has_avg_pool3d:
return mint.nn.functional.avg_pool3d(input, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override)

if divisor_override is None:
divisor_override = 0
return ops.avg_pool3d(input, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override)


def adaptive_avg_pool1d(input, output_size):
if use_pyboost():
return mint.nn.functional.adaptive_avg_pool1d(input, output_size)
Expand Down Expand Up @@ -1020,7 +1030,9 @@ def scaled_dot_product_attention(query, key, value, attn_mask=None, dropout_p=0.
is_causal=False, scale=None, enable_gqa=False) -> core.Tensor:
L, S = query.size(-2), key.size(-2)
scale_factor = 1 / math.sqrt(query.size(-1)) if scale is None else scale
attn_bias = core.zeros(L, S, dtype=query.dtype, device=query.device)

attn_bias_shape = (L, S) if attn_mask is None else attn_mask.shape
attn_bias = core.zeros(attn_bias_shape, dtype=query.dtype, device=query.device)
if is_causal:
assert attn_mask is None
temp_mask = core.ones(L, S, dtype=core.bool).tril(diagonal=0)
Expand Down
5 changes: 4 additions & 1 deletion mindnlp/core/onnx/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .utils import register_custom_op_symbolic
from .utils import register_custom_op_symbolic

def is_in_onnx_export():
return False
11 changes: 10 additions & 1 deletion mindnlp/core/ops/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ def chunk(input, chunks, dim=0):
# gather
has_gather = hasattr(mindspore.mint, 'gather')
def gather(input, dim, index):
is_complex = input.dtype == mindspore.complex64
if is_complex:
real_part = mindspore.mint.gather(input.real, dim, index)
imag_part = mindspore.mint.gather(input.imag, dim, index)
_complex = _get_cache_prim(ops.Complex)()
return _complex(real_part, imag_part)

if use_pyboost() and has_gather:
return mindspore.mint.gather(input, dim, index)

index = ops.where(index < input.shape[dim], index, index - input.shape[dim])
return ops.gather_elements(input, dim, index)

Expand Down Expand Up @@ -150,7 +158,8 @@ def permute(input, dims):

# reshape
has_reshape = hasattr(mindspore.mint, 'reshape')
def reshape(input, *shape):
def reshape(input, *shape, **kwargs):
shape = kwargs.pop('shape', shape)
if isinstance(shape[0], (tuple, list)):
shape = shape[0]
new_shape = ()
Expand Down
4 changes: 3 additions & 1 deletion mindnlp/core/ops/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def from_numpy(ndarray):
def zeros(*size, dtype=None, device=None, requires_grad=False, **kwargs):
if dtype is None:
dtype = get_default_dtype()
if not isinstance(dtype, Type):
dtype = py2dtype[dtype]
if len(size) == 0:
size = kwargs.get('size', None)
if size == () or size == []:
Expand Down Expand Up @@ -88,7 +90,7 @@ def ones(*size, dtype=None, device=None, **kwargs):

# ones_like
has_ones_like = hasattr(mindspore.mint, 'ones_like')
def ones_like(input, *, dtype=None, device=None):
def ones_like(input, *, dtype=None, device=None, **kwargs):
if dtype is None:
dtype = input.dtype
if use_pyboost() and has_ones_like:
Expand Down
10 changes: 7 additions & 3 deletions mindnlp/core/ops/inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, inplace_scatter_value_op
from mindspore.ops.auto_generate.gen_ops_prim import inplace_normal_op, inplace_scatter_value_op, inplace_scatter_src_reduce_op

from mindnlp import core
from ..configs import use_pyboost
Expand Down Expand Up @@ -132,7 +132,10 @@ def inplace_round(input, decimals=0):
input.assign_value(out)
return input


def inplace_scatter_reduce(input, dim, index, src, reduce, *, include_self=True):
if reduce == 'sum':
reduce = "add"
return inplace_scatter_src_reduce_op(input, dim, index, src, reduce)

__all__ = [
'inplace_copy',
Expand All @@ -148,5 +151,6 @@ def inplace_round(input, decimals=0):
'inplace_unsqueeze',
'inplace_fill_diagonal',
'inplace_triu',
'inplace_round'
'inplace_round',
'inplace_scatter_reduce'
]
15 changes: 14 additions & 1 deletion mindnlp/core/ops/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,16 +853,27 @@ def triu(input, diagonal=0, *, out=None):

# unflatten
def unflatten(x, dim, sizes):
new_shape = x.shape[:dim] + sizes + x.shape[dim+1:]
if dim < 0:
dim = x.ndim + dim
front_part = x.shape[:dim] if dim != 0 else ()
new_shape = front_part + sizes + x.shape[dim+1:]
return ops.reshape(x, new_shape)


# vander


# view_as_real
def view_as_real(input):
real_part = input.real.expand_dims(-1)
imag_part = input.imag.expand_dims(-1)
return core.concat((real_part, imag_part), -1)

# view_as_complex
def view_as_complex(input):
_complex = _get_cache_prim(ops.Complex)()
real_part, imag_part = input.tensor_split(2, -1)
return _complex(real_part.squeeze(-1), imag_part.squeeze(-1))


# resolve_conj
Expand Down Expand Up @@ -1051,4 +1062,6 @@ def unfold(input, dimension, size, step):
"unflatten",
"unfold",
"histc",
"view_as_complex",
"view_as_real"
]
4 changes: 3 additions & 1 deletion mindnlp/core/ops/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def normal(mean=0.0, std=1.0, size=None, *, generator=None, out=None):

# rand
has_rand = hasattr(mindspore.mint, 'rand')
def rand(*size, generator=None, out=None, dtype=None, device=None, pin_memory=False):
def rand(*size, generator=None, out=None, dtype=None, device=None, pin_memory=False, **kwargs):
size = kwargs.pop('size', size)
if size[0] == []:
size = ()
elif isinstance(size[0], (tuple, list)):
Expand Down Expand Up @@ -110,6 +111,7 @@ def randint_like(*args, **kwargs):
# randn
has_randn = hasattr(mindspore.mint, 'randn')
def randn(*size, generator=None, dtype=None, **kwargs):
size = kwargs.pop('size', size)
if dtype is None:
dtype = get_default_dtype()
if use_pyboost() and has_randn:
Expand Down
4 changes: 3 additions & 1 deletion tests/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def run_tests():
"and not compile " \
"and not compilation " \
"and not torchscript " \
"and not torch_fx"
"and not torch_fx " \
"and not test_wrong_device_map " \
"and not test_layerwise_casting"

pytest_args.extend(["--ignore-glob=test_modeling_flax_*.py"])
pytest_args.extend(['-k', skip_ut])
Expand Down
Loading