diff --git a/mindspore/ops/composite/multitype_ops/_constexpr_utils.py b/mindspore/ops/composite/multitype_ops/_constexpr_utils.py index dac8949fdc..dec427601d 100644 --- a/mindspore/ops/composite/multitype_ops/_constexpr_utils.py +++ b/mindspore/ops/composite/multitype_ops/_constexpr_utils.py @@ -131,6 +131,13 @@ def is_same_type(inst, type_): return inst == type_ +@constexpr +def check_valid_type(data_type, value_type, name): + if not data_type in value_type: + raise TypeError( + f"For {name}, valid type include {value_type}, {data_type} is invalid") + + def slice_expand(input_slices, shape): """ Converts slice to indices. diff --git a/mindspore/ops/composite/random_ops.py b/mindspore/ops/composite/random_ops.py index e760e9d328..aa97ec079f 100644 --- a/mindspore/ops/composite/random_ops.py +++ b/mindspore/ops/composite/random_ops.py @@ -92,6 +92,9 @@ def uniform(shape, minval, maxval, seed=0, dtype=mstype.float32): If dtype is int32, only one number is allowed. seed (int): Seed is used as entropy source for Random number engines generating pseudo-random numbers. Must be non-negative. Default: 0. + dtype (mindspore.dtype): type of the Uniform distribution. If it is int32, it generates numbers from discrete + uniform distribution; if it is float32, it generates numbers from continuous uniform distribution. It only + supports these two data types. Default: mstype.float32. Returns: Tensor. The shape should be the broadcasted shape of Input "shape" and shapes of minval and maxval. @@ -112,6 +115,7 @@ def uniform(shape, minval, maxval, seed=0, dtype=mstype.float32): """ minval_dtype = F.dtype(minval) maxval_dtype = F.dtype(maxval) + const_utils.check_valid_type(dtype, [mstype.int32, mstype.float32], 'uniform') const_utils.check_tensors_dtype_same(minval_dtype, dtype, "uniform") const_utils.check_tensors_dtype_same(maxval_dtype, dtype, "uniform") const_utils.check_non_negative("seed", seed, "uniform")