You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_control.py 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import numpy as np
  16. import mindspore.context as context
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore.ops import operations as P
  20. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  21. class Net1(nn.Cell):
  22. def __init__(self):
  23. super(Net1, self).__init__()
  24. self.relu1 = P.ReLU()
  25. self.relu2 = P.ReLU()
  26. self.mul = P.Mul()
  27. self.control = P.ControlDepend()
  28. def construct(self, x, y):
  29. a = self.relu1(x)
  30. b = self.relu2(y)
  31. c = self.mul(a, b)
  32. e = self.control(a, b)
  33. return c, e
  34. class Net2(nn.Cell):
  35. def __init__(self):
  36. super(Net2, self).__init__()
  37. self.relu1 = P.ReLU()
  38. self.relu2 = P.ReLU().add_prim_attr("primitive_target", "CPU")
  39. self.mul = P.Mul()
  40. self.control = P.ControlDepend()
  41. def construct(self, x, y):
  42. a = self.relu1(x)
  43. b = self.relu2(y)
  44. c = self.mul(a, b)
  45. e = self.control(a, b)
  46. return c, e
  47. def test_net():
  48. x = np.random.randn(2, 3, 3, 4).astype(np.float32)
  49. y = np.random.randn(2, 3, 3, 4).astype(np.float32)
  50. net1 = Net1()
  51. output1 = net1(Tensor(x), Tensor(y))
  52. context.set_context(save_graphs=True)
  53. net2 = Net2()
  54. output2 = net2(Tensor(x), Tensor(y))
  55. assert np.allclose(output1[0].asnumpy(), output2[0].asnumpy())
  56. print("##success##")
  57. if __name__ == "__main__":
  58. test_net()