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.1 kB

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