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_ops_infer.py 2.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. """ test nn ops """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.common.dtype as mstype
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore import context
  22. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  23. def test_cast_op_attr():
  24. class CastNet(nn.Cell):
  25. def __init__(self):
  26. super(CastNet, self).__init__()
  27. self.cast = P.Cast()
  28. def construct(self, x, t):
  29. return self.cast(x, t)
  30. class CastTypeTest(nn.Cell):
  31. def __init__(self, net):
  32. super(CastTypeTest, self).__init__()
  33. self.net = net
  34. self.cast = P.Cast()
  35. def construct(self, x, y, z):
  36. cast_op = self.cast
  37. t1 = cast_op(x, mstype.float32)
  38. t2 = cast_op(y, mstype.int32)
  39. cast_net = self.net
  40. t3 = cast_net(x, mstype.float16)
  41. t4 = cast_net(y, mstype.int32)
  42. t5 = cast_net(z, mstype.float16)
  43. return (t1, t2, t3, t4, t5)
  44. net = CastTypeTest(CastNet())
  45. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.int32))
  46. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  47. t3 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.int32))
  48. out = net(t1, t2, t3)
  49. assert out[0].asnumpy().dtype == np.float32
  50. assert out[1].asnumpy().dtype == np.int32
  51. assert out[2].asnumpy().dtype == np.float16
  52. assert out[3].asnumpy().dtype == np.int32
  53. assert out[4].asnumpy().dtype == np.float16