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

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