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.

inner_ops.py 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. """inner_ops"""
  16. from ...common.dtype import tensor, dtype_to_pytype
  17. from ..primitive import prim_attr_register, PrimitiveWithInfer
  18. class ScalarCast(PrimitiveWithInfer):
  19. """
  20. Cast the input scalar to another type.
  21. Inputs:
  22. - **input_x** (scalar) - The input scalar. Only constant value is allowed.
  23. - **input_y** (mindspore.dtype) - The type should cast to be. Only constant value is allowed.
  24. Outputs:
  25. Scalar. The type is same as the python type corresponding to `input_y`.
  26. Examples:
  27. >>> scalar_cast = P.ScalarCast()
  28. >>> output = scalar_cast(255.0, mindspore.int32)
  29. """
  30. @prim_attr_register
  31. def __init__(self):
  32. pass
  33. def __infer__(self, x, t):
  34. value, to = x['value'], t['value']
  35. if value is not None:
  36. if isinstance(to, type(tensor)):
  37. to = to.element_type()
  38. np_type = dtype_to_pytype(to)
  39. value = np_type(value)
  40. out = {'shape': x['shape'],
  41. 'dtype': t['value'],
  42. 'value': value}
  43. return out