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

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