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_partial.py 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_partial """
  16. from mindspore.common.api import ms_function
  17. import mindspore.ops.functional as F
  18. import mindspore.ops.composite as C
  19. from mindspore import context
  20. def setup_module(module):
  21. context.set_context(mode=context.PYNATIVE_MODE)
  22. def myadd(x, y):
  23. return x + y
  24. def partial_simple_add(x):
  25. return F.partial(myadd, x)
  26. @ms_function
  27. def full_simple_add(x, y):
  28. p = partial_simple_add(x)
  29. return p(y)
  30. def test_full_simple_add():
  31. print(full_simple_add(2, 5))
  32. # partial with multitype
  33. MULTI_ADD = C.MultitypeFuncGraph('add')
  34. @MULTI_ADD.register("Int32", "Int32")
  35. def add_int(x, y):
  36. return F.scalar_add(x, y)
  37. @MULTI_ADD.register("Float32", "Float32")
  38. def add_float(x, y):
  39. return F.scalar_add(x, y)
  40. def partial_multi_add(x):
  41. return F.partial(MULTI_ADD, x)
  42. @ms_function
  43. def full_multi_add(x, y, m, n):
  44. p = partial_multi_add(x)(y)
  45. q = partial_multi_add(m)(n)
  46. return p, q
  47. def test_full_multi_add():
  48. print(full_multi_add(1, 2, 1.0, 2.0))