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.

pynative_execute_test.cc 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "pipeline/jit/parse/python_adapter.h"
  20. #include "pipeline/jit/parse/data_converter.h"
  21. #include "frontend/operator/ops.h"
  22. #include "pipeline/pynative/pynative_execute.h"
  23. #include "utils/ms_context.h"
  24. #include "utils/utils.h"
  25. namespace py = pybind11;
  26. using pybind11::literals::operator"" _a;
  27. using Tensor = mindspore::tensor::Tensor;
  28. using TensorPtr = mindspore::tensor::TensorPtr;
  29. namespace mindspore {
  30. namespace pynative {
  31. class TestPynativeExecute : public UT::Common {
  32. public:
  33. TestPynativeExecute() {}
  34. };
  35. inline ValuePtr PyAttrValue(const py::object &obj) {
  36. ValuePtr converted_ret;
  37. bool converted = parse::ConvertData(obj, &converted_ret);
  38. if (!converted) {
  39. MS_LOG(EXCEPTION) << "attribute convert error with type:" << std::string(py::str(obj));
  40. }
  41. return converted_ret;
  42. }
  43. OpExecInfoPtr ConstructOpExecInfo() {
  44. py::str op_name = "Conv2D";
  45. py::object tensor_py_module = py::module::import("mindspore.common.tensor").attr("Tensor");
  46. py::object np_py_module = py::module::import("numpy");
  47. py::object np_ones = np_py_module.attr("ones");
  48. py::object np_float32 = np_py_module.attr("float32");
  49. py::tuple weight_dim = py::make_tuple(64, 3, 3, 3);
  50. py::object weight = tensor_py_module(np_float32(np_ones(weight_dim)));
  51. py::tuple op_params = py::make_tuple(weight);
  52. py::tuple inputs_dim = py::make_tuple(1, 3, 6, 6);
  53. py::object input = tensor_py_module(np_float32(np_ones(inputs_dim)));
  54. py::tuple op_inputs = py::make_tuple(input, weight);
  55. py::tuple kernel_size = py::make_tuple(3, 3);
  56. py::dict op_attrs = py::dict("out_channel"_a = 64, "kernel_size"_a = kernel_size, "mode"_a = 1, "pad_mode"_a = "same",
  57. "stride"_a = 1, "dilation"_a = 1, "group"_a = 1, "data_format"_a = kOpFormat_NCHW);
  58. auto conv_obj = prim::GetPythonOps("conv2d_prim", "gtest_input.pynative");
  59. py::none py_none;
  60. py::args args = py::make_tuple(conv_obj, op_name, op_inputs);
  61. py::list args_input = args[PY_INPUTS];
  62. return PynativeExecutor::GetInstance()->forward_executor()->GenerateOpExecInfo(args);
  63. }
  64. TEST_F(TestPynativeExecute, TestCreateContext) {
  65. auto ctx3 = MsContext::GetInstance();
  66. ASSERT_EQ(ctx3->backend_policy(), "vm");
  67. ASSERT_EQ(ctx3->get_param<std::string>(MS_CTX_DEVICE_TARGET), "CPU");
  68. ctx3->set_backend_policy("ge_only");
  69. ctx3->set_param<std::string>(MS_CTX_DEVICE_TARGET, "GPU");
  70. auto ctx4 = MsContext::GetInstance();
  71. ASSERT_EQ(ctx3.get(), ctx4.get());
  72. ASSERT_EQ(ctx4->backend_policy(), "ge_only");
  73. ASSERT_EQ(ctx4->get_param<std::string>(MS_CTX_DEVICE_TARGET), "GPU");
  74. }
  75. TEST_F(TestPynativeExecute, TestDefaultContext) {
  76. auto ctx = MsContext::GetInstance();
  77. ASSERT_EQ(std::string(ctx->backend_policy()), "ge_only");
  78. auto ctx2 = MsContext::GetInstance();
  79. ASSERT_EQ(ctx.get(), ctx2.get());
  80. }
  81. } // namespace pynative
  82. } // namespace mindspore