Skip to content

Commit

Permalink
[bf16] change bf16 print behavior (#39370)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiu authored Feb 8, 2022
1 parent f57b21e commit 96964ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion paddle/fluid/platform/bfloat16_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ TEST(bfloat16, floating) {

TEST(bfloat16, print) {
bfloat16 a = bfloat16(1.0f);
std::cout << a << std::endl;
std::cout << "a:" << a << std::endl;
std::stringstream ss1, ss2;
ss1 << a;
ss2 << 1.0f;
EXPECT_EQ(ss1.str(), ss2.str());
}

// CPU test
Expand Down
6 changes: 3 additions & 3 deletions paddle/pten/common/bfloat16.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct PADDLE_ALIGN(2) bfloat16 {

#if defined(PADDLE_CUDA_BF16)
HOSTDEVICE inline explicit bfloat16(const __nv_bfloat16& val) {
x = *reinterpret_cast<const unsigned short*>(&val);
x = *reinterpret_cast<const unsigned short*>(&val); // NOLINT
}
#endif

Expand All @@ -93,7 +93,7 @@ struct PADDLE_ALIGN(2) bfloat16 {
// Assignment operators
#if defined(PADDLE_CUDA_BF16)
HOSTDEVICE inline bfloat16& operator=(const __nv_bfloat16& val) {
x = *reinterpret_cast<const unsigned short*>(&val);
x = *reinterpret_cast<const unsigned short*>(&val); // NOLINT
return *this;
}
#endif
Expand Down Expand Up @@ -311,7 +311,7 @@ HOSTDEVICE inline bool(isfinite)(const bfloat16& a) {
}

inline std::ostream& operator<<(std::ostream& os, const bfloat16& a) {
os << a.x;
os << static_cast<float>(a);
return os;
}

Expand Down

0 comments on commit 96964ff

Please sign in to comment.