float16_test.cc 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
K
Kexin Zhao 已提交
2 3 4 5 6 7 8 9 10
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. */
11
#include "paddle/fluid/platform/float16.h"
12 13 14

#include <vector>

P
peizhilin 已提交
15
#define GLOG_NO_ABBREVIATED_SEVERITIES  // msvc conflict logging with windows.h
16
#include "gtest/gtest.h"
K
kexinzhao 已提交
17
#include "paddle/fluid/framework/lod_tensor.h"
18
#include "paddle/fluid/platform/enforce.h"
19
#include "paddle/fluid/platform/init.h"
K
Kexin Zhao 已提交
20 21

namespace paddle {
K
kexinzhao 已提交
22
namespace platform {
K
Kexin Zhao 已提交
23 24

TEST(float16, conversion_cpu) {
K
Kexin Zhao 已提交
25 26 27 28 29 30 31 32
  // Explicit conversion from Eigen::half
  EXPECT_EQ(float16(Eigen::half(1.0f)).x, 0x3c00);
  EXPECT_EQ(float16(Eigen::half(0.5f)).x, 0x3800);
  EXPECT_EQ(float16(Eigen::half(0.33333f)).x, 0x3555);
  EXPECT_EQ(float16(Eigen::half(0.0f)).x, 0x0000);
  EXPECT_EQ(float16(Eigen::half(-0.0f)).x, 0x8000);
  EXPECT_EQ(float16(Eigen::half(65504.0f)).x, 0x7bff);
  EXPECT_EQ(float16(Eigen::half(65536.0f)).x, 0x7c00);
K
Kexin Zhao 已提交
33 34 35 36 37 38 39 40 41 42 43

  // Conversion from float
  EXPECT_EQ(float16(1.0f).x, 0x3c00);
  EXPECT_EQ(float16(0.5f).x, 0x3800);
  EXPECT_EQ(float16(0.33333f).x, 0x3555);
  EXPECT_EQ(float16(0.0f).x, 0x0000);
  EXPECT_EQ(float16(-0.0f).x, 0x8000);
  EXPECT_EQ(float16(65504.0f).x, 0x7bff);
  EXPECT_EQ(float16(65536.0f).x, 0x7c00);

  // Conversion from double
K
Kexin Zhao 已提交
44 45 46 47 48 49 50
  EXPECT_EQ(float16(1.0).x, 0x3c00);
  EXPECT_EQ(float16(0.5).x, 0x3800);
  EXPECT_EQ(float16(0.33333).x, 0x3555);
  EXPECT_EQ(float16(0.0).x, 0x0000);
  EXPECT_EQ(float16(-0.0).x, 0x8000);
  EXPECT_EQ(float16(65504.0).x, 0x7bff);
  EXPECT_EQ(float16(65536.0).x, 0x7c00);
K
Kexin Zhao 已提交
51 52

  // Conversion from int
K
Kexin Zhao 已提交
53 54 55 56 57
  EXPECT_EQ(float16(-1).x, 0xbc00);
  EXPECT_EQ(float16(0).x, 0x0000);
  EXPECT_EQ(float16(1).x, 0x3c00);
  EXPECT_EQ(float16(2).x, 0x4000);
  EXPECT_EQ(float16(3).x, 0x4200);
K
Kexin Zhao 已提交
58 59

  // Conversion from bool
K
Kexin Zhao 已提交
60 61 62 63 64
  EXPECT_EQ(float16(true).x, 0x3c00);
  EXPECT_EQ(float16(false).x, 0x0000);

  // Assignment operator
  float16 v_assign;
K
kexinzhao 已提交
65
  v_assign = float16(0);
K
Kexin Zhao 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
  EXPECT_EQ(v_assign.x, 0x0000);
  v_assign = Eigen::half(1.0f);
  EXPECT_EQ(v_assign.x, 0x3c00);
  v_assign = 0.5f;
  EXPECT_EQ(v_assign.x, 0x3800);
  v_assign = 0.33333;
  EXPECT_EQ(v_assign.x, 0x3555);
  v_assign = -1;
  EXPECT_EQ(v_assign.x, 0xbc00);
  v_assign = true;
  EXPECT_EQ(v_assign.x, 0x3c00);

  // Conversion operator
  EXPECT_EQ(Eigen::half(float16(1.0f)).x, 0x3c00);
80 81 82 83
  EXPECT_EQ(static_cast<float>(float16(0.5f)), 0.5f);
  EXPECT_NEAR(static_cast<double>(float16(0.33333)), 0.33333, 0.0001);
  EXPECT_EQ(static_cast<int>(float16(-1)), -1);
  EXPECT_EQ(static_cast<bool>(float16(true)), true);
K
Kexin Zhao 已提交
84 85
}

K
Kexin Zhao 已提交
86
TEST(float16, arithmetic_cpu) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100
  EXPECT_EQ(static_cast<float>(float16(1) + float16(1)), 2);
  EXPECT_EQ(static_cast<float>(float16(5) + float16(-5)), 0);
  EXPECT_NEAR(static_cast<float>(float16(0.33333f) + float16(0.66667f)), 1.0f,
              0.001);
  EXPECT_EQ(static_cast<float>(float16(3) - float16(5)), -2);
  EXPECT_NEAR(static_cast<float>(float16(0.66667f) - float16(0.33333f)),
              0.33334f, 0.001);
  EXPECT_NEAR(static_cast<float>(float16(3.3f) * float16(2.0f)), 6.6f, 0.01);
  EXPECT_NEAR(static_cast<float>(float16(-2.1f) * float16(-3.0f)), 6.3f, 0.01);
  EXPECT_NEAR(static_cast<float>(float16(2.0f) / float16(3.0f)), 0.66667f,
              0.001);
  EXPECT_EQ(static_cast<float>(float16(1.0f) / float16(2.0f)), 0.5f);
  EXPECT_EQ(static_cast<float>(-float16(512.0f)), -512.0f);
  EXPECT_EQ(static_cast<float>(-float16(-512.0f)), 512.0f);
K
Kexin Zhao 已提交
101
}
K
Kexin Zhao 已提交
102

K
Kexin Zhao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
TEST(float16, comparison_cpu) {
  EXPECT_TRUE(float16(1.0f) == float16(1.0f));
  EXPECT_FALSE(float16(-1.0f) == float16(-0.5f));
  EXPECT_TRUE(float16(1.0f) != float16(0.5f));
  EXPECT_FALSE(float16(-1.0f) != float16(-1.0f));
  EXPECT_TRUE(float16(1.0f) < float16(2.0f));
  EXPECT_FALSE(float16(-1.0f) < float16(-1.0f));
  EXPECT_TRUE(float16(1.0f) <= float16(1.0f));
  EXPECT_TRUE(float16(2.0f) > float16(1.0f));
  EXPECT_FALSE(float16(-2.0f) > float16(-2.0f));
  EXPECT_TRUE(float16(2.0f) >= float16(2.0f));

  EXPECT_TRUE(float16(0.0f) == float16(-0.0f));
  EXPECT_TRUE(float16(0.0f) <= float16(-0.0f));
  EXPECT_TRUE(float16(0.0f) >= float16(-0.0f));
  EXPECT_FALSE(float16(0.0f) < float16(-0.0f));
  EXPECT_FALSE(float16(-0.0f) < float16(0.0f));
  EXPECT_FALSE(float16(0.0f) > float16(-0.0f));
  EXPECT_FALSE(float16(-0.0f) > float16(0.0f));
}
K
Kexin Zhao 已提交
123

K
kexinzhao 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
TEST(float16, lod_tensor_cpu) {
  framework::LoDTensor lod_tensor;

  std::vector<float16> input_data = {float16(1.0f), float16(0.5f),
                                     float16(0.33333f), float16(0.0f)};
  EXPECT_EQ(input_data[0].x, 0x3c00);
  EXPECT_EQ(input_data[1].x, 0x3800);
  EXPECT_EQ(input_data[2].x, 0x3555);
  EXPECT_EQ(input_data[3].x, 0x0000);

  lod_tensor.Resize({4, 1});
  lod_tensor.set_lod(framework::LoD({{0, 2, 4}}));
  float16* data_ptr = lod_tensor.mutable_data<float16>(CPUPlace());

  EXPECT_NE(data_ptr, nullptr);
  EXPECT_EQ(input_data.size(), static_cast<size_t>(lod_tensor.numel()));
  for (size_t i = 0; i < input_data.size(); ++i) {
    data_ptr[i] = input_data[i];
    EXPECT_EQ(data_ptr[i].x, input_data[i].x);
  }
}

146 147
TEST(float16, floating) {
  // compile time assert.
148
  PADDLE_ENFORCE_EQ(std::is_floating_point<float16>::value, true);
149 150
}

151 152 153 154 155
TEST(float16, print) {
  float16 a = float16(1.0f);
  std::cout << a << std::endl;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
// CPU test
TEST(float16, isinf) {
  float16 a;
  a.x = 0x7c00;
  float16 b = float16(INFINITY);
  float16 c = static_cast<float16>(INFINITY);
  EXPECT_EQ(std::isinf(a), true);
  EXPECT_EQ(std::isinf(b), true);
  EXPECT_EQ(std::isinf(c), true);
}

TEST(float16, isnan) {
  float16 a;
  a.x = 0x7fff;
  float16 b = float16(NAN);
  float16 c = static_cast<float16>(NAN);
  EXPECT_EQ(std::isnan(a), true);
  EXPECT_EQ(std::isnan(b), true);
  EXPECT_EQ(std::isnan(c), true);
}

K
kexinzhao 已提交
177
}  // namespace platform
K
Kexin Zhao 已提交
178
}  // namespace paddle