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 2.0 kB

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