Skip to content

Commit 11659c3

Browse files
Merge pull request #44894 from Intel-tensorflow:vishakh1/new_bf16_sparse_xent
PiperOrigin-RevId: 345710893 Change-Id: Iff85c524d71c2a72f07a62250180e9356f4ee81a
2 parents 95bc751 + c409bed commit 11659c3

3 files changed

Lines changed: 49 additions & 28 deletions

File tree

‎tensorflow/core/kernels/sparse_xent_op.cc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#define EIGEN_USE_THREADS
1919

2020
#include "tensorflow/core/kernels/sparse_xent_op.h"
21+
2122
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
2223
#include "tensorflow/core/framework/op_kernel.h"
2324
#include "tensorflow/core/framework/tensor.h"
@@ -122,6 +123,8 @@ REGISTER(CPU, float, int32)
122123
REGISTER(CPU, float, int64)
123124
REGISTER(CPU, double, int32)
124125
REGISTER(CPU, double, int64)
126+
REGISTER(CPU, bfloat16, int32)
127+
REGISTER(CPU, bfloat16, int64)
125128
REGISTER(CPU, Eigen::half, int32)
126129
REGISTER(CPU, Eigen::half, int64)
127130

‎tensorflow/core/kernels/sparse_xent_op_test.cc‎

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ limitations under the License.
2323

2424
namespace tensorflow {
2525

26-
static Graph* SparseXent(int batch_size, int num_classes) {
26+
static Graph* SparseXent(int batch_size, int num_classes, DataType value_type) {
2727
Graph* g = new Graph(OpRegistry::Global());
28-
Tensor logits(DT_FLOAT, TensorShape({batch_size, num_classes}));
28+
Tensor logits(value_type, TensorShape({batch_size, num_classes}));
2929
logits.flat<float>().setRandom();
3030
Tensor labels(DT_INT64, TensorShape({batch_size}));
3131
std::random_device rd;
@@ -41,44 +41,45 @@ static Graph* SparseXent(int batch_size, int num_classes) {
4141
return g;
4242
}
4343

44-
#define BM_SparseXentDev(BATCH, CLASS, DEVICE) \
45-
static void BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE( \
44+
#define BM_SparseXentDev(BATCH, CLASS, DEVICE, DTYPE) \
45+
static void BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE##_##DTYPE( \
4646
::testing::benchmark::State& state) { \
47-
test::Benchmark(#DEVICE, SparseXent(BATCH, CLASS), \
47+
test::Benchmark(#DEVICE, SparseXent(BATCH, CLASS, DTYPE), \
4848
/*old_benchmark_api*/ false) \
4949
.Run(state); \
5050
state.SetItemsProcessed(static_cast<int64>(state.iterations()) * BATCH * \
5151
CLASS); \
5252
} \
53-
BENCHMARK(BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE);
53+
BENCHMARK(BM_SparseXent##_##BATCH##_##CLASS##_##DEVICE##_##DTYPE);
54+
55+
#define BM_SPARSE_XENT_DEV_CPU(DTYPE) \
56+
BM_SparseXentDev(8, 1000000, cpu, DTYPE); \
57+
BM_SparseXentDev(16, 10000, cpu, DTYPE); \
58+
BM_SparseXentDev(16, 100000, cpu, DTYPE); \
59+
BM_SparseXentDev(32, 10000, cpu, DTYPE); \
60+
BM_SparseXentDev(32, 100000, cpu, DTYPE); \
61+
BM_SparseXentDev(64, 10000, cpu, DTYPE); \
62+
BM_SparseXentDev(64, 100000, cpu, DTYPE);
63+
64+
// CPU
65+
BM_SPARSE_XENT_DEV_CPU(DT_FLOAT);
66+
BM_SPARSE_XENT_DEV_CPU(DT_BFLOAT16);
5467

5568
/// The representative tests for ptb_word on GPU
5669
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
57-
BM_SparseXentDev(8, 1000000, gpu);
70+
BM_SparseXentDev(8, 1000000, gpu, DT_FLOAT);
5871

59-
BM_SparseXentDev(16, 10000, gpu);
60-
BM_SparseXentDev(16, 30000, gpu);
61-
BM_SparseXentDev(16, 100000, gpu);
72+
BM_SparseXentDev(16, 10000, gpu, DT_FLOAT);
73+
BM_SparseXentDev(16, 30000, gpu, DT_FLOAT);
74+
BM_SparseXentDev(16, 100000, gpu, DT_FLOAT);
6275

63-
BM_SparseXentDev(32, 10000, gpu);
64-
BM_SparseXentDev(32, 30000, gpu);
65-
BM_SparseXentDev(32, 100000, gpu);
76+
BM_SparseXentDev(32, 10000, gpu, DT_FLOAT);
77+
BM_SparseXentDev(32, 30000, gpu, DT_FLOAT);
78+
BM_SparseXentDev(32, 100000, gpu, DT_FLOAT);
6679

67-
BM_SparseXentDev(64, 10000, gpu);
68-
BM_SparseXentDev(64, 30000, gpu);
69-
BM_SparseXentDev(64, 100000, gpu);
80+
BM_SparseXentDev(64, 10000, gpu, DT_FLOAT);
81+
BM_SparseXentDev(64, 30000, gpu, DT_FLOAT);
82+
BM_SparseXentDev(64, 100000, gpu, DT_FLOAT);
7083
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
7184

72-
// CPU
73-
BM_SparseXentDev(8, 1000000, cpu);
74-
75-
BM_SparseXentDev(16, 10000, cpu);
76-
BM_SparseXentDev(16, 100000, cpu);
77-
78-
BM_SparseXentDev(32, 10000, cpu);
79-
BM_SparseXentDev(32, 100000, cpu);
80-
81-
BM_SparseXentDev(64, 10000, cpu);
82-
BM_SparseXentDev(64, 100000, cpu);
83-
8485
} // end namespace tensorflow

‎tensorflow/python/kernel_tests/sparse_xent_op_test.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,23 @@ def testDouble(self):
182182
np.array([[1., 1., 1., 1.], [1., 2., 3., 4.]]).astype(np.float64),
183183
np.array([0, 3]).astype(label_dtype))
184184

185+
def testBfloat16(self):
186+
for label_dtype in np.int32, np.int64:
187+
np_features = np.array([[1., 1., 1., 1.], [1., 2., 3.,
188+
4.]]).astype(np.float32)
189+
np_labels = np.array([0, 3]).astype(label_dtype)
190+
np_loss, np_backprop = self._npXent(np_features, np_labels)
191+
192+
np_features_bf16 = math_ops.cast(np_features, dtypes.bfloat16)
193+
np_loss_bf16 = math_ops.cast(np_loss, dtypes.bfloat16)
194+
np_backprop_bf16 = math_ops.cast(np_backprop, dtypes.bfloat16)
195+
with self.cached_session(use_gpu=False):
196+
loss, backprop = gen_nn_ops.sparse_softmax_cross_entropy_with_logits(
197+
np_features_bf16, np_labels)
198+
tf_loss, tf_backprop = self.evaluate([loss, backprop])
199+
self.assertAllCloseAccordingToType(np_loss_bf16, tf_loss)
200+
self.assertAllCloseAccordingToType(np_backprop_bf16, tf_backprop)
201+
185202
def testHalf(self):
186203
for label_dtype in np.int32, np.int64:
187204
self._testXent(

0 commit comments

Comments
 (0)