test_backend.cc 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
#include <iostream>

18 19
#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/common/backend.h"
20

21
namespace phi {
22 23
namespace tests {

24 25
TEST(Backend, OStream) {
  std::ostringstream oss;
26
  oss << phi::Backend::UNDEFINED;
27 28
  EXPECT_EQ(oss.str(), "Undefined");
  oss.str("");
29
  oss << phi::Backend::CPU;
30 31
  EXPECT_EQ(oss.str(), "CPU");
  oss.str("");
32
  oss << phi::Backend::GPU;
33
  EXPECT_EQ(oss.str(), "GPU");
34
  oss.str("");
35
  oss << phi::Backend::XPU;
36 37
  EXPECT_EQ(oss.str(), "XPU");
  oss.str("");
38
  oss << phi::Backend::NPU;
39 40
  EXPECT_EQ(oss.str(), "NPU");
  oss.str("");
41
  oss << phi::Backend::MKLDNN;
42 43
  EXPECT_EQ(oss.str(), "MKLDNN");
  oss.str("");
44 45
  oss << phi::Backend::GPUDNN;
  EXPECT_EQ(oss.str(), "GPUDNN");
46
  oss.str("");
47 48 49
  oss << phi::Backend::KPS;
  EXPECT_EQ(oss.str(), "KPS");
  oss.str("");
50
  try {
51
    oss << phi::Backend::NUM_BACKENDS;
52
  } catch (const std::exception& exception) {
53 54 55 56
    std::string ex_msg = exception.what();
    EXPECT_TRUE(ex_msg.find("Invalid enum backend type") != std::string::npos);
  }
}
57

58 59 60 61 62 63 64 65
TEST(Backend, StringToBackend) {
  namespace pexp = paddle::experimental;
  EXPECT_EQ(phi::Backend::UNDEFINED, pexp::StringToBackend("Undefined"));
  EXPECT_EQ(phi::Backend::CPU, pexp::StringToBackend("CPU"));
  EXPECT_EQ(phi::Backend::GPU, pexp::StringToBackend("GPU"));
  EXPECT_EQ(phi::Backend::XPU, pexp::StringToBackend("XPU"));
  EXPECT_EQ(phi::Backend::NPU, pexp::StringToBackend("NPU"));
  EXPECT_EQ(phi::Backend::MKLDNN, pexp::StringToBackend("MKLDNN"));
66
  EXPECT_EQ(phi::Backend::GPUDNN, pexp::StringToBackend("GPUDNN"));
67 68 69
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
  EXPECT_EQ(phi::Backend::GPU, pexp::StringToBackend("KPS"));
#else
70
  EXPECT_EQ(phi::Backend::KPS, pexp::StringToBackend("KPS"));
71
#endif
72 73 74 75 76
  EXPECT_EQ(static_cast<phi::Backend>(
                static_cast<size_t>(phi::Backend::NUM_BACKENDS) + 1),
            pexp::StringToBackend("CustomBackend"));
}

77
}  // namespace tests
78
}  // namespace phi