code: # Copyright 2021 Huawei Technologies Co., Ltd
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ============================================================================
  
  """test hccl allreduce performance with 8p"""
  
  import os
  from multiprocessing import Process, Queue
  import pytest
  import numpy as np
  import mindspore.nn as nn
  from mindspore import Tensor
  from mindspore import dtype as mstype
  from mindspore.ops import operations as P
  import mindspore.communication.management as D
  from mindspore import context
  from mindspore.context import ParallelMode
  
  MINDSPORE_HCCL_CONFIG_PATH = "/home/workspace/mindspore_config/hccl/rank_table_8p.json"
  
  np.random.seed(1)
  os.environ['GLOG_v'] = str(2)
  
  class AllReduceNet(nn.Cell):
      def __init__(self):
          super(AllReduceNet, self).__init__()
          self.mul = P.Mul()
          self.all_reduce = P.AllReduce()
          self.add = P.Add()
  
      def construct(self, x):
          x = self.mul(x, 2)
          y1 = Tensor(np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]])).astype(np.float32)
          z = self.add(x, y1)
          z = self.all_reduce(z)
          y2 = Tensor(np.array([[-16, -16, -16, -16], [-16, -16, -16, -16], [-16, -16, -16, -16]])).astype(np.float32)
          out = self.add(z, y2)
          out = self.all_reduce(out)
          out = self.mul(out, 2)
          return out
  
  def train_allreduce_8p(q, device_id, device_num):
      os.system("mkdir " + str(device_id))
      os.chdir(str(device_id))
      context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend", device_id=device_id)
      os.environ['MINDSPORE_HCCL_CONFIG_PATH'] = MINDSPORE_HCCL_CONFIG_PATH
      os.environ['RANK_ID'] = str(device_id)
      os.environ['RANK_SIZE'] = str(device_num)
      D.init()
      context.reset_auto_parallel_context()
      context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, gradients_mean=False,
                                        device_num=device_num)
  
      net = AllReduceNet()
      input_x = np.ones([3, 4]).astype(np.float32)
      output = net(Tensor(input_x, mstype.float32))
      q.put(output)
  
  @pytest.mark.level0
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_single
  def test_pynative_hccl_allreduce_8p():
      device_num = 8
      process = []
      q = Queue()
      for i in range(device_num):
          device_id = i
          process.append(Process(target=train_allreduce_8p, args=(q, device_id, device_num)))
  
      for i in range(device_num):
          process[i].start()
  
      print("Waiting for all subprocesses done...")
  
      for i in range(device_num):
          process[i].join()
  
      # check result
      for i in range(device_num):
          expect_output = [[256, 256, 256, 256], [256, 256, 256, 256], [256, 256, 256, 256]]
          assert not q.empty()
          output = Tensor(q.get())
          assert np.allclose(output.asnumpy(), expect_output)
  
      for i in range(device_num):
          os.system("rm -rf " + str(i))
  
      print("End training...")
   # Copyright 2021 Huawei Technologies Co., Ltd
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  from tqdm import tqdm
  import numpy as np
  import mindspore as ms
  import mindspore.nn as nn
  from mindspore.dataset import NumpySlicesDataset
  from mindspore import context, Tensor
  
  context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  
  class AutoEncoderTrainNetwork(nn.Cell):
      def __init__(self):
          super(AutoEncoderTrainNetwork, self).__init__()
          self.loss_fun = nn.MSELoss()
          self.net = nn.CellList([nn.Dense(2, 32), nn.Dense(32, 2)])
          self.relu = nn.ReLU()
  
      def reconstruct_sample(self, x: Tensor):
          for _, layer in enumerate(self.net):
              x = layer(x)
              x = self.relu(x)
          return x
  
      def construct(self, x: Tensor):
          recon_x = self.reconstruct_sample(x)
          return self.loss_fun(recon_x, x)
  
      def sample_2d_data(self, n_normals=2000, n_outliers=400):
          z = np.random.randn(n_normals, 2)
          outliers = np.random.uniform(low=-6, high=6, size=(n_outliers, 2))
          centers = np.array([(2., 0), (-2., 0)])
          sigma = 0.3
          normal_points = sigma * z + centers[np.random.randint(len(centers), size=(n_normals,))]
          return np.vstack((normal_points, outliers))
  
      def create_synthetic_dataset(self):
          transformed_dataset = self.sample_2d_data()
          for dim in range(transformed_dataset.shape[1]):
              min_val = transformed_dataset[:, dim].min()
              max_val = transformed_dataset[:, dim].max()
              if min_val != max_val:
                  transformed_dataset[:, dim] = (transformed_dataset[:, dim] - min_val) / (max_val - min_val)
              elif min_val != 1:
                  transformed_dataset[:, dim] = transformed_dataset[:, dim] / min_val
          transformed_dataset = transformed_dataset.astype(np.float32)
          return transformed_dataset
  
  
  def test_auto_monad_layer():
      ae_with_loss = AutoEncoderTrainNetwork()
      transformed_dataset = ae_with_loss.create_synthetic_dataset()
      dataloader = NumpySlicesDataset(data=(transformed_dataset,), shuffle=True)
      dataloader = dataloader.batch(batch_size=16)
      optim = nn.RMSProp(params=ae_with_loss.trainable_params(), learning_rate=0.002,)
      train_net = nn.TrainOneStepCell(ae_with_loss, optim)
      train_net.set_train()
      gen_samples = dict()
      num_epoch = 21
      for epoch in tqdm(range(num_epoch)):
          loss = []
          for _, (batch,) in enumerate(dataloader):
              batch = Tensor(batch, dtype=ms.float32)
              loss_ = train_net(batch)
              loss.append(loss_.asnumpy())
          avg_loss = np.array(loss).mean()
          if epoch % 10 == 0:
              gen_samples[epoch] = ae_with_loss.reconstruct_sample(Tensor(transformed_dataset)).asnumpy()
          print(f"epoch: {epoch}/{num_epoch}, avg loss: {avg_loss}")
   # Copyright 2021 Huawei Technologies Co., Ltd
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ============================================================================
  
  import numpy as np
  import pytest
  
  import mindspore.context as context
  from mindspore.common.tensor import Tensor
  from mindspore.ops import operations as P
  
  
  @pytest.mark.level0
  @pytest.mark.platform_x86_cpu
  @pytest.mark.env_onecard
  def test_broadcast():
      context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  
      shape = (4, 5, 2, 3, 4, 5, 6)
      x_np = np.random.rand(2, 3, 1, 5, 1).astype(np.float32)
      output = P.BroadcastTo(shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (3, 5, 7, 4, 5, 6)
      x_np = np.arange(20).reshape((4, 5, 1)).astype(np.int32)
      output = P.BroadcastTo(shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (8, 5, 7, 4, 5, 6)
      x_np = np.arange(24).reshape((1, 4, 1, 6)).astype(np.bool)
      output = P.BroadcastTo(shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (3, 4, 5, 2, 3, 4, 5, 7)
      x_np = np.random.rand(2, 3, 1, 5, 1).astype(np.float16)
      output = P.BroadcastTo(shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (3, 4, 5, 6)
      x_np = np.random.rand(3, 1, 5, 1).astype(np.float32)
      output = P.BroadcastTo(shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16)
      output = P.BroadcastTo(shape)(Tensor(x1_np))
      expect = np.broadcast_to(x1_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (2, 3, 4, 5)
      x1_np = np.random.rand(4, 5).astype(np.float32)
      output = P.BroadcastTo(shape)(Tensor(x1_np))
      expect = np.broadcast_to(x1_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
      shape = (4, 5)
      x1_np = np.ones((1,)).astype(np.bool_)
      output = P.BroadcastTo(shape)(Tensor(x1_np))
      expect = np.broadcast_to(x1_np, shape)
      assert np.allclose(output.asnumpy(), expect)
  
  
  @pytest.mark.level0
  @pytest.mark.platform_x86_cpu
  @pytest.mark.env_onecard
  def test_broadcast_dyn_init():
      """
      Test running the op with -1's in the init shape to support varied inputs.
      """
      context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  
      ms_shape = (-1, 4, 5, 6)
      np_shape = (3, 4, 5, 6)
      x_np = np.random.rand(3, 1, 5, 1).astype(np.float32)
      output = P.BroadcastTo(ms_shape)(Tensor(x_np))
      expect = np.broadcast_to(x_np, np_shape)
      assert np.allclose(output.asnumpy(), expect)
  
      x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16)
      output = P.BroadcastTo(ms_shape)(Tensor(x1_np))
      expect = np.broadcast_to(x1_np, np_shape)
      assert np.allclose(output.asnumpy(), expect)
  
      ms_shape = (2, 3, -1, 5)
      np_shape = (2, 3, 4, 5)
      x1_np = np.random.rand(4, 5).astype(np.float32)
      output = P.BroadcastTo(ms_shape)(Tensor(x1_np))
      expect = np.broadcast_to(x1_np, np_shape)
      assert np.allclose(output.asnumpy(), expect)
  
  
  @pytest.mark.level0
  @pytest.mark.platform_x86_cpu
  @pytest.mark.env_onecard
  def test_broadcast_dyn_invalid_init():
      """
      Test running the op with -1's in the init shape in incorrect positions.
      Expected to fail.
      """
      context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
      ms_shape = (2, -1, 4, 5)
      x_np = np.random.rand(4, 5).astype(np.float32)
      with pytest.raises(ValueError):
          P.BroadcastTo(ms_shape)(Tensor(x_np))
   # Copyright 2021 Huawei Technologies Co., Ltd
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ============================================================================
  """st for scipy.utils"""
  
  import pytest
  import numpy as onp
  from mindspore import context, Tensor
  from mindspore.scipy.utils import _safe_normalize
  
  
  @pytest.mark.level0
  @pytest.mark.platform_x86_gpu_training
  @pytest.mark.platform_x86_cpu
  @pytest.mark.env_onecard
  @pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
  @pytest.mark.parametrize('shape', [(10,), (10, 1)])
  @pytest.mark.parametrize('dtype', [onp.float32, onp.float64])
  def test_safe_normalize(mode, shape, dtype):
      """
      Feature: ALL TO ALL
      Description: test cases for _safe_normalize
      Expectation: the result match scipy
      """
      context.set_context(mode=mode)
      x = onp.random.random(shape).astype(dtype)
      normalized_x, x_norm = _safe_normalize(Tensor(x))
  
      normalized_x = normalized_x.asnumpy()
      x_norm = x_norm.asnumpy()
      assert onp.allclose(onp.sum(normalized_x ** 2), 1)
      assert onp.allclose(x / x_norm, normalized_x)
   # Copyright 2020 Huawei Technologies Co., Ltd
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  import pytest
  import numpy as np
  import mindspore.nn as nn
  import mindspore.ops.operations as P
  import mindspore.nn.probability.distribution as msd
  from mindspore import context, Tensor
  from mindspore.ops import composite as C
  from mindspore.common import dtype as mstype
  from mindspore import dtype
  
  context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  
  
  class Sampling(nn.Cell):
      """
      Test class: sample of Normal distribution.
      """
  
      def __init__(self, shape, seed=0):
          super(Sampling, self).__init__()
          self.n1 = msd.Normal(0, 1, seed=seed, dtype=dtype.float32)
          self.shape = shape
  
      def construct(self, mean=None, sd=None):
          s1 = self.n1.sample(self.shape, mean, sd)
          s2 = self.n1.sample(self.shape, mean, sd)
          s3 = self.n1.sample(self.shape, mean, sd)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_sample_graph():
      shape = (2, 3)
      seed = 0
      samp = Sampling(shape, seed=seed)
      sample1, sample2, sample3 = samp()
      assert ((sample1 != sample2).any() and (sample1 != sample3).any() and (sample2 != sample3).any()), \
          "The results should be different!"
  
  
  class CompositeNormalNet(nn.Cell):
      def __init__(self, shape=None, seed=0):
          super(CompositeNormalNet, self).__init__()
          self.shape = shape
          self.seed = seed
  
      def construct(self, mean, stddev):
          s1 = C.normal(self.shape, mean, stddev, self.seed)
          s2 = C.normal(self.shape, mean, stddev, self.seed)
          s3 = C.normal(self.shape, mean, stddev, self.seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_composite_normal():
      shape = (3, 2, 4)
      mean = Tensor(0.0, mstype.float32)
      stddev = Tensor(1.0, mstype.float32)
      net = CompositeNormalNet(shape)
      s1, s2, s3 = net(mean, stddev)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class CompositeLaplaceNet(nn.Cell):
      def __init__(self, shape=None, seed=0):
          super(CompositeLaplaceNet, self).__init__()
          self.shape = shape
          self.seed = seed
  
      def construct(self, mean, lambda_param):
          s1 = C.laplace(self.shape, mean, lambda_param, self.seed)
          s2 = C.laplace(self.shape, mean, lambda_param, self.seed)
          s3 = C.laplace(self.shape, mean, lambda_param, self.seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_composite_laplace():
      shape = (3, 2, 4)
      mean = Tensor(1.0, mstype.float32)
      lambda_param = Tensor(1.0, mstype.float32)
      net = CompositeLaplaceNet(shape)
      s1, s2, s3 = net(mean, lambda_param)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class CompositeGammaNet(nn.Cell):
      def __init__(self, shape=None, seed=0):
          super(CompositeGammaNet, self).__init__()
          self.shape = shape
          self.seed = seed
  
      def construct(self, alpha, beta):
          s1 = C.gamma(self.shape, alpha, beta, self.seed)
          s2 = C.gamma(self.shape, alpha, beta, self.seed)
          s3 = C.gamma(self.shape, alpha, beta, self.seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_composite_gamma():
      shape = (3, 2, 4)
      alpha = Tensor(1.0, mstype.float32)
      beta = Tensor(1.0, mstype.float32)
      net = CompositeGammaNet(shape)
      s1, s2, s3 = net(alpha, beta)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class CompositePoissonNet(nn.Cell):
      def __init__(self, shape=None, seed=0):
          super(CompositePoissonNet, self).__init__()
          self.shape = shape
          self.seed = seed
  
      def construct(self, mean):
          s1 = C.poisson(self.shape, mean, self.seed)
          s2 = C.poisson(self.shape, mean, self.seed)
          s3 = C.poisson(self.shape, mean, self.seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_composite_poisson():
      shape = (3, 2, 4)
      mean = Tensor(2.0, mstype.float32)
      net = CompositePoissonNet(shape)
      s1, s2, s3 = net(mean)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class CompositeUniformNet(nn.Cell):
      def __init__(self, shape=None, seed=0):
          super(CompositeUniformNet, self).__init__()
          self.shape = shape
          self.seed = seed
  
      def construct(self, a, b):
          s1 = C.uniform(self.shape, a, b, self.seed)
          s2 = C.uniform(self.shape, a, b, self.seed)
          s3 = C.uniform(self.shape, a, b, self.seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_composite_uniform():
      shape = (3, 2, 4)
      a = Tensor(0.0, mstype.float32)
      b = Tensor(1.0, mstype.float32)
      net = CompositeUniformNet(shape)
      s1, s2, s3 = net(a, b)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class StandardNormalNet(nn.Cell):
      def __init__(self, shape, seed=0, seed2=0):
          super(StandardNormalNet, self).__init__()
          self.shape = shape
          self.seed = seed
          self.seed2 = seed2
          self.standard_normal = P.StandardNormal(seed, seed2)
  
      def construct(self):
          s1 = self.standard_normal(self.shape)
          s2 = self.standard_normal(self.shape)
          s3 = self.standard_normal(self.shape)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_standard_normal():
      shape = (4, 16)
      net = StandardNormalNet(shape)
      s1, s2, s3 = net()
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class StandardLaplaceNet(nn.Cell):
      def __init__(self, shape, seed=0, seed2=0):
          super(StandardLaplaceNet, self).__init__()
          self.shape = shape
          self.seed = seed
          self.seed2 = seed2
          self.standard_laplace = P.StandardLaplace(seed, seed2)
  
      def construct(self):
          s1 = self.standard_laplace(self.shape)
          s2 = self.standard_laplace(self.shape)
          s3 = self.standard_laplace(self.shape)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_standard_laplace():
      shape = (4, 16)
      net = StandardLaplaceNet(shape)
      s1, s2, s3 = net()
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class GammaNet(nn.Cell):
      def __init__(self, shape, alpha, beta, seed=0, seed2=0):
          super(GammaNet, self).__init__()
          self.shape = shape
          self.alpha = alpha
          self.beta = beta
          self.seed = seed
          self.seed2 = seed2
          self.gamma = P.Gamma(seed, seed2)
  
      def construct(self):
          s1 = self.gamma(self.shape, self.alpha, self.beta)
          s2 = self.gamma(self.shape, self.alpha, self.beta)
          s3 = self.gamma(self.shape, self.alpha, self.beta)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_gamma():
      shape = (4, 16)
      alpha = Tensor(1.0, mstype.float32)
      beta = Tensor(1.0, mstype.float32)
      net = GammaNet(shape, alpha, beta)
      s1, s2, s3 = net()
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class PoissonNet(nn.Cell):
      def __init__(self, shape, seed=0, seed2=0):
          super(PoissonNet, self).__init__()
          self.shape = shape
          self.seed = seed
          self.seed2 = seed2
          self.poisson = P.Poisson(seed, seed2)
  
      def construct(self, mean):
          s1 = self.poisson(self.shape, mean)
          s2 = self.poisson(self.shape, mean)
          s3 = self.poisson(self.shape, mean)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_poisson():
      shape = (4, 16)
      mean = Tensor(5.0, mstype.float32)
      net = PoissonNet(shape=shape)
      s1, s2, s3 = net(mean)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class UniformIntNet(nn.Cell):
      def __init__(self, shape, seed=0, seed2=0):
          super(UniformIntNet, self).__init__()
          self.shape = shape
          self.seed = seed
          self.seed2 = seed2
          self.uniform_int = P.UniformInt(seed, seed2)
  
      def construct(self, minval, maxval):
          s1 = self.uniform_int(self.shape, minval, maxval)
          s2 = self.uniform_int(self.shape, minval, maxval)
          s3 = self.uniform_int(self.shape, minval, maxval)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_uniform_int():
      shape = (4, 16)
      minval = Tensor(1, mstype.int32)
      maxval = Tensor(5, mstype.int32)
      net = UniformIntNet(shape)
      s1, s2, s3 = net(minval, maxval)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class UniformRealNet(nn.Cell):
      def __init__(self, shape, seed=0, seed2=0):
          super(UniformRealNet, self).__init__()
          self.shape = shape
          self.seed = seed
          self.seed2 = seed2
          self.uniform_real = P.UniformReal(seed, seed2)
  
      def construct(self):
          s1 = self.uniform_real(self.shape)
          s2 = self.uniform_real(self.shape)
          s3 = self.uniform_real(self.shape)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_uniform_real():
      shape = (4, 16)
      net = UniformRealNet(shape)
      s1, s2, s3 = net()
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class DropoutGenMaskNet(nn.Cell):
      def __init__(self, shape):
          super(DropoutGenMaskNet, self).__init__()
          self.shape = shape
          self.dropout_gen_mask = P.DropoutGenMask(Seed0=0, Seed1=0)
  
      def construct(self, keep_prob):
          s1 = self.dropout_gen_mask(self.shape, keep_prob)
          s2 = self.dropout_gen_mask(self.shape, keep_prob)
          s3 = self.dropout_gen_mask(self.shape, keep_prob)
          return s1, s2, s3
  
  
  @pytest.mark.level0
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_dropout_gen_mask():
      shape = (2, 4, 5)
      keep_prob = Tensor(0.5, mstype.float32)
      net = DropoutGenMaskNet(shape)
      s1, s2, s3 = net(keep_prob)
      assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
          "The results should be different!"
  
  
  class RandomChoiceWithMaskNet(nn.Cell):
      def __init__(self):
          super(RandomChoiceWithMaskNet, self).__init__()
          self.rnd_choice_mask = P.RandomChoiceWithMask(count=4, seed=0)
  
      def construct(self, x):
          index1, _ = self.rnd_choice_mask(x)
          index2, _ = self.rnd_choice_mask(x)
          index3, _ = self.rnd_choice_mask(x)
          return index1, index2, index3
  
  
  @pytest.mark.level0
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_random_choice_with_mask():
      mode = context.get_context('mode')
      assert (mode == context.GRAPH_MODE), 'GRAPH_MODE required but got ' + str(mode)
      net = RandomChoiceWithMaskNet()
      x = Tensor(np.array([[1, 0, 1, 0], [0, 0, 0, 1], [1, 1, 1, 1], [0, 0, 0, 1]]).astype(np.bool))
      index1, index2, index3 = net(x)
      assert ((index1 != index2).any() and (index1 != index3).any() and (index2 != index3).any()), \
          "The results should be different!"
  
  
  class RandomCategoricalNet(nn.Cell):
      def __init__(self, num_sample):
          super(RandomCategoricalNet, self).__init__()
          self.random_categorical = P.RandomCategorical(mstype.int64)
          self.num_sample = num_sample
  
      def construct(self, logits, seed=0):
          s1 = self.random_categorical(logits, self.num_sample, seed)
          s2 = self.random_categorical(logits, self.num_sample, seed)
          s3 = self.random_categorical(logits, self.num_sample, seed)
          return s1, s2, s3
  
  
  @pytest.mark.level1
  @pytest.mark.platform_arm_ascend_training
  @pytest.mark.platform_x86_ascend_training
  @pytest.mark.env_onecard
  def test_random_categorical():
      num_sample = 8
      net = RandomCategoricalNet(num_sample)
      x = Tensor(np.random.random((10, 5)).astype(np.float32))
      # Outputs may be the same, only basic functions are verified here.
      net(x)
  
apis: numpy.array numpy.ones numpy.random.seed
apis: numpy.random.uniform numpy.random.randn numpy.array numpy.vstack
apis: numpy.broadcast_to numpy.ones numpy.arange numpy.random.rand
apis: numpy.random.random numpy.allclose numpy.sum
apis: numpy.array numpy.random.random