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.

context_test.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright 2021 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 <memory>
  17. #include "common/common_test.h"
  18. #include "include/api/context.h"
  19. namespace mindspore {
  20. class TestCxxApiContext : public UT::Common {
  21. public:
  22. TestCxxApiContext() = default;
  23. };
  24. TEST_F(TestCxxApiContext, test_context_device_info_cast_SUCCESS) {
  25. std::shared_ptr<DeviceInfoContext> cpu = std::make_shared<CPUDeviceInfo>();
  26. std::shared_ptr<DeviceInfoContext> mali_gpu = std::make_shared<MaliGPUDeviceInfo>();
  27. std::shared_ptr<DeviceInfoContext> kirin_npu = std::make_shared<KirinNPUDeviceInfo>();
  28. std::shared_ptr<DeviceInfoContext> nvidia_gpu = std::make_shared<NvidiaGPUDeviceInfo>();
  29. std::shared_ptr<DeviceInfoContext> ascend310 = std::make_shared<Ascend310DeviceInfo>();
  30. std::shared_ptr<DeviceInfoContext> ascend910 = std::make_shared<Ascend910DeviceInfo>();
  31. ASSERT_TRUE(cpu->Cast<CPUDeviceInfo>() != nullptr);
  32. ASSERT_TRUE(mali_gpu->Cast<MaliGPUDeviceInfo>() != nullptr);
  33. ASSERT_TRUE(kirin_npu->Cast<KirinNPUDeviceInfo>() != nullptr);
  34. ASSERT_TRUE(nvidia_gpu->Cast<NvidiaGPUDeviceInfo>() != nullptr);
  35. ASSERT_TRUE(ascend310->Cast<Ascend310DeviceInfo>() != nullptr);
  36. ASSERT_TRUE(ascend910->Cast<Ascend910DeviceInfo>() != nullptr);
  37. }
  38. TEST_F(TestCxxApiContext, test_context_device_info_cast_FAILED) {
  39. std::shared_ptr<DeviceInfoContext> cpu = std::make_shared<CPUDeviceInfo>();
  40. std::shared_ptr<DeviceInfoContext> mali_gpu = std::make_shared<MaliGPUDeviceInfo>();
  41. std::shared_ptr<DeviceInfoContext> kirin_npu = std::make_shared<KirinNPUDeviceInfo>();
  42. std::shared_ptr<DeviceInfoContext> nvidia_gpu = std::make_shared<NvidiaGPUDeviceInfo>();
  43. std::shared_ptr<DeviceInfoContext> ascend310 = std::make_shared<Ascend310DeviceInfo>();
  44. std::shared_ptr<DeviceInfoContext> ascend910 = std::make_shared<Ascend910DeviceInfo>();
  45. ASSERT_TRUE(cpu->Cast<MaliGPUDeviceInfo>() == nullptr);
  46. ASSERT_TRUE(kirin_npu->Cast<MaliGPUDeviceInfo>() == nullptr);
  47. ASSERT_TRUE(nvidia_gpu->Cast<MaliGPUDeviceInfo>() == nullptr);
  48. ASSERT_TRUE(ascend310->Cast<MaliGPUDeviceInfo>() == nullptr);
  49. ASSERT_TRUE(ascend910->Cast<MaliGPUDeviceInfo>() == nullptr);
  50. ASSERT_TRUE(mali_gpu->Cast<CPUDeviceInfo>() == nullptr);
  51. ASSERT_TRUE(kirin_npu->Cast<CPUDeviceInfo>() == nullptr);
  52. ASSERT_TRUE(nvidia_gpu->Cast<CPUDeviceInfo>() == nullptr);
  53. ASSERT_TRUE(ascend310->Cast<CPUDeviceInfo>() == nullptr);
  54. ASSERT_TRUE(ascend910->Cast<CPUDeviceInfo>() == nullptr);
  55. }
  56. TEST_F(TestCxxApiContext, test_context_get_set_SUCCESS) {
  57. int32_t thread_num = 22;
  58. auto context = std::make_shared<Context>();
  59. context->SetThreadNum(thread_num);
  60. ASSERT_EQ(context->GetThreadNum(), thread_num);
  61. }
  62. TEST_F(TestCxxApiContext, test_context_cpu_context_SUCCESS) {
  63. auto context = std::make_shared<Context>();
  64. std::shared_ptr<CPUDeviceInfo> cpu = std::make_shared<CPUDeviceInfo>();
  65. cpu->SetEnableFP16(true);
  66. context->MutableDeviceInfo().push_back(cpu);
  67. ASSERT_EQ(context->MutableDeviceInfo().size(), 1);
  68. auto cpu_2 = context->MutableDeviceInfo()[0]->Cast<CPUDeviceInfo>();
  69. ASSERT_TRUE(cpu_2 != nullptr);
  70. ASSERT_TRUE(cpu_2->GetEnableFP16());
  71. }
  72. TEST_F(TestCxxApiContext, test_context_ascend_context_FAILED) {
  73. std::string option_1 = "aaa";
  74. std::string option_2 = "vvv";
  75. std::string option_3 = "www";
  76. std::string option_4 = "rrr";
  77. std::string option_5 = "ppp";
  78. std::string option_6 = "sss";
  79. uint32_t option_7 = 77;
  80. enum DataType option_8 = DataType::kNumberTypeInt16;
  81. std::vector<size_t> option_9 = {1, 2, 3, 4, 5};
  82. std::string option_9_ans = "1,2,3,4,5";
  83. auto context = std::make_shared<Context>();
  84. std::shared_ptr<Ascend310DeviceInfo> ascend310 = std::make_shared<Ascend310DeviceInfo>();
  85. ascend310->SetInputShape(option_1);
  86. ascend310->SetInsertOpConfigPath(option_2);
  87. ascend310->SetOpSelectImplMode(option_3);
  88. ascend310->SetPrecisionMode(option_4);
  89. ascend310->SetInputFormat(option_5);
  90. ascend310->SetFusionSwitchConfigPath(option_6);
  91. ascend310->SetDeviceID(option_7);
  92. ascend310->SetOutputType(option_8);
  93. ascend310->SetDynamicBatchSize(option_9);
  94. context->MutableDeviceInfo().push_back(ascend310);
  95. ASSERT_EQ(context->MutableDeviceInfo().size(), 1);
  96. auto ctx = context->MutableDeviceInfo()[0]->Cast<Ascend310DeviceInfo>();
  97. ASSERT_TRUE(ctx != nullptr);
  98. ASSERT_EQ(ascend310->GetInputShape(), option_1);
  99. ASSERT_EQ(ascend310->GetInsertOpConfigPath(), option_2);
  100. ASSERT_EQ(ascend310->GetOpSelectImplMode(), option_3);
  101. ASSERT_EQ(ascend310->GetPrecisionMode(), option_4);
  102. ASSERT_EQ(ascend310->GetInputFormat(), option_5);
  103. ASSERT_EQ(ascend310->GetFusionSwitchConfigPath(), option_6);
  104. ASSERT_EQ(ascend310->GetDeviceID(), option_7);
  105. ASSERT_EQ(ascend310->GetOutputType(), option_8);
  106. ASSERT_EQ(ascend310->GetDynamicBatchSize(), option_9_ans);
  107. }
  108. TEST_F(TestCxxApiContext, test_context_ascend310_context_default_value_SUCCESS) {
  109. auto ctx = std::make_shared<Ascend310DeviceInfo>();
  110. ASSERT_EQ(ctx->GetOpSelectImplMode(), "");
  111. }
  112. } // namespace mindspore