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