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.5 kB

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