Skip to content

Commit 4788116

Browse files
Amit Patankartensorflower-gardener
authored andcommitted
Create a round trip fuzzer for FloatToBFloat16 and BFloat16ToFloat.
PiperOrigin-RevId: 350163764 Change-Id: I1228ab9eef664d1c2aae0d981307644e8fcdb3ef
1 parent 242f42a commit 4788116

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎tensorflow/core/framework/BUILD‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,10 @@ cc_library(
623623
name = "bfloat16",
624624
srcs = ["bfloat16.cc"],
625625
hdrs = ["bfloat16.h"],
626-
visibility = ["//tensorflow/core:__subpackages__"],
626+
visibility = [
627+
"//tensorflow/core:__subpackages__",
628+
"//tensorflow/security/fuzzing:__subpackages__",
629+
],
627630
deps = [
628631
":numeric_types",
629632
"//tensorflow/core/platform:byte_order",

‎tensorflow/security/fuzzing/BUILD‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ tf_fuzz_target(
2727
],
2828
)
2929

30+
tf_fuzz_target(
31+
name = "bfloat16_fuzz",
32+
srcs = ["bfloat16_fuzz.cc"],
33+
deps = [
34+
"//tensorflow/core:test",
35+
"//tensorflow/core/framework:bfloat16",
36+
"@com_google_absl//absl/strings",
37+
],
38+
)
39+
3040
tf_fuzz_target(
3141
name = "AreAttrValuesEqual_fuzz",
3242
srcs = ["AreAttrValuesEqual_fuzz.cc"],
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#include <fuzzer/FuzzedDataProvider.h>
16+
17+
#include <cstdint>
18+
#include <cstdlib>
19+
20+
#include "tensorflow/core/framework/bfloat16.h"
21+
#include "tensorflow/core/platform/test.h"
22+
23+
// This is a fuzzer for tensorflow::FloatToBFloat16 and
24+
// tensorflow::BFloat16ToFloat.
25+
26+
namespace {
27+
28+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
29+
FuzzedDataProvider fuzzed_data(data, size);
30+
31+
const int array_size = 100;
32+
33+
float float_originals[array_size];
34+
for (int i = 0; i < array_size; ++i) {
35+
float_originals[i] = fuzzed_data.ConsumeFloatingPointInRange(1.0f, 1000.0f);
36+
}
37+
tensorflow::bfloat16 bfloats[array_size];
38+
float floats_converted[array_size];
39+
40+
tensorflow::FloatToBFloat16(float_originals, bfloats, array_size);
41+
tensorflow::BFloat16ToFloat(bfloats, floats_converted, array_size);
42+
43+
for (int i = 0; i < array_size; ++i) {
44+
// The relative error should be less than 1/(2^7) since bfloat16
45+
// has 7 bits mantissa.
46+
// Copied this logic from bfloat16_test.cc
47+
assert(fabs(floats_converted[i] - float_originals[i]) / float_originals[i] <
48+
1.0 / 128);
49+
}
50+
51+
return 0;
52+
}
53+
54+
} // namespace

0 commit comments

Comments
 (0)