Break the dependency between platform/types.h and bfloat16.h, and between
authorA. Unique TensorFlower <gardener@tensorflow.org>
Tue, 13 Mar 2018 20:24:30 +0000 (13:24 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Tue, 13 Mar 2018 20:28:54 +0000 (13:28 -0700)
hash.h and bfloat16.h. This change introduces a generic mechanism for adapting
types that are meant to be used in tensorflow's error objects.

PiperOrigin-RevId: 188920678

tensorflow/compiler/xla/service/cpu/runtime_conv2d.h
tensorflow/compiler/xla/service/cpu/runtime_matmul.h
tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv2d.h
tensorflow/compiler/xla/service/cpu/runtime_single_threaded_matmul.h
tensorflow/core/framework/numeric_types.h
tensorflow/core/lib/bfloat16/bfloat16.h
tensorflow/core/lib/core/errors.h
tensorflow/core/lib/hash/hash.h
tensorflow/core/lib/random/random_distributions.h
tensorflow/core/lib/strings/strcat.h
tensorflow/core/platform/types.h

index 39e20ed..7337c90 100644 (file)
@@ -16,6 +16,7 @@ limitations under the License.
 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_CONV2D_H_
 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_CONV2D_H_
 
+#include "third_party/eigen3/Eigen/Core"
 #include "tensorflow/core/platform/types.h"
 
 extern "C" {
index b515643..d96fe3d 100644 (file)
@@ -16,6 +16,7 @@ limitations under the License.
 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_MATMUL_H_
 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_MATMUL_H_
 
+#include "third_party/eigen3/Eigen/Core"
 #include "tensorflow/core/platform/types.h"
 
 extern "C" {
index f216bd0..44b2017 100644 (file)
@@ -16,6 +16,7 @@ limitations under the License.
 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_SINGLE_THREADED_CONV2D_H_
 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_SINGLE_THREADED_CONV2D_H_
 
+#include "third_party/eigen3/Eigen/Core"
 #include "tensorflow/core/platform/types.h"
 
 extern "C" {
index 9371a62..82a1fcc 100644 (file)
@@ -16,6 +16,7 @@ limitations under the License.
 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_SINGLE_THREADED_MATMUL_H_
 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_SINGLE_THREADED_MATMUL_H_
 
+#include "third_party/eigen3/Eigen/Core"
 #include "tensorflow/core/platform/types.h"
 
 extern "C" {
index 4c38fbb..dab53cb 100644 (file)
@@ -24,6 +24,7 @@ limitations under the License.
 #include "third_party/eigen3/unsupported/Eigen/CXX11/FixedPoint"
 // clang-format on
 
+#include "tensorflow/core/lib/bfloat16/bfloat16.h"
 #include "tensorflow/core/platform/types.h"
 
 namespace tensorflow {
index de8f92d..6a1cc09 100644 (file)
@@ -19,6 +19,9 @@ limitations under the License.
 #include <cmath>
 #include <complex>
 
+// We need types.h here in order to pick up __BYTE_ORDER__ from cpu_info.h
+#include "tensorflow/core/platform/types.h"
+
 #ifdef __CUDACC__
 // All functions callable from CUDA code must be qualified with __device__
 #define B16_DEVICE_FUNC __host__ __device__
index 1fd6275..1a0f4be 100644 (file)
@@ -16,6 +16,8 @@ limitations under the License.
 #ifndef TENSORFLOW_LIB_CORE_ERRORS_H_
 #define TENSORFLOW_LIB_CORE_ERRORS_H_
 
+#include <sstream>
+
 #include "tensorflow/core/lib/core/status.h"
 #include "tensorflow/core/lib/strings/strcat.h"
 #include "tensorflow/core/platform/logging.h"
@@ -26,6 +28,33 @@ namespace errors {
 
 typedef ::tensorflow::error::Code Code;
 
+namespace internal {
+
+// The DECLARE_ERROR macro below only supports types that can be converted
+// into StrCat's AlphaNum. For the other types we rely on a slower path
+// through std::stringstream. To add support of a new type, it is enough to
+// make sure there is an operator<<() for it:
+//
+//   std::ostream& operator<<(std::ostream& os, const MyType& foo) {
+//     os << foo.ToString();
+//     return os;
+//   }
+// Eventually absl::strings will have native support for this and we will be
+// able to completely remove PrepareForStrCat().
+template <typename T>
+typename std::enable_if<!std::is_convertible<T, strings::AlphaNum>::value,
+                        string>::type
+PrepareForStrCat(const T& t) {
+  std::stringstream ss;
+  ss << t;
+  return ss.str();
+}
+inline const strings::AlphaNum& PrepareForStrCat(const strings::AlphaNum& a) {
+  return a;
+}
+
+}  // namespace internal
+
 // Append some context to an error message.  Each time we append
 // context put it on a new line, since it is possible for there
 // to be several layers of additional context.
@@ -61,8 +90,10 @@ void AppendToMessage(::tensorflow::Status* status, Args... args) {
 #define DECLARE_ERROR(FUNC, CONST)                                       \
   template <typename... Args>                                            \
   ::tensorflow::Status FUNC(Args... args) {                              \
-    return ::tensorflow::Status(::tensorflow::error::CONST,              \
-                                ::tensorflow::strings::StrCat(args...)); \
+    return ::tensorflow::Status(                                         \
+        ::tensorflow::error::CONST,                                      \
+        ::tensorflow::strings::StrCat(                                   \
+            ::tensorflow::errors::internal::PrepareForStrCat(args)...)); \
   }                                                                      \
   inline bool Is##FUNC(const ::tensorflow::Status& status) {             \
     return status.code() == ::tensorflow::error::CONST;                  \
index b90c651..77b8031 100644 (file)
@@ -64,13 +64,6 @@ struct hash<T*> {
 };
 
 template <>
-struct hash<bfloat16> {
-  size_t operator()(const bfloat16& t) const {
-    return std::hash<float>()(static_cast<float>(t));
-  }
-};
-
-template <>
 struct hash<string> {
   size_t operator()(const string& s) const {
     return static_cast<size_t>(Hash64(s));
index 2ebe608..ad16dbf 100644 (file)
@@ -25,6 +25,7 @@ limitations under the License.
 #include <algorithm>
 
 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
+#include "tensorflow/core/lib/bfloat16/bfloat16.h"
 #include "tensorflow/core/lib/random/philox_random.h"
 
 namespace tensorflow {
index 2bc1494..b681f73 100644 (file)
@@ -119,9 +119,6 @@ class AlphaNum {
 
   AlphaNum(float f)  // NOLINT(runtime/explicit)
       : piece_(digits_, strlen(FloatToBuffer(f, digits_))) {}
-  AlphaNum(bfloat16 f)  // NOLINT(runtime/explicit)
-      : piece_(digits_, strlen(FloatToBuffer(static_cast<float>(f), digits_))) {
-  }
   AlphaNum(double f)  // NOLINT(runtime/explicit)
       : piece_(digits_, strlen(DoubleToBuffer(f, digits_))) {}
 
index e2dd5b0..38d75db 100644 (file)
@@ -35,8 +35,6 @@ limitations under the License.
 #include "tensorflow/core/platform/windows/cpu_info.h"
 #endif
 
-#include "tensorflow/core/lib/bfloat16/bfloat16.h"
-
 namespace tensorflow {
 
 // Define tensorflow::string to refer to appropriate platform specific type.