Create a round trip fuzzer for FloatToBFloat16 and BFloat16ToFloat.

PiperOrigin-RevId: 350163764
Change-Id: I1228ab9eef664d1c2aae0d981307644e8fcdb3ef
This commit is contained in:
Amit Patankar 2021-01-05 10:04:39 -08:00 committed by TensorFlower Gardener
parent 242f42a186
commit 4788116863
3 changed files with 68 additions and 1 deletions

View File

@ -623,7 +623,10 @@ cc_library(
name = "bfloat16",
srcs = ["bfloat16.cc"],
hdrs = ["bfloat16.h"],
visibility = ["//tensorflow/core:__subpackages__"],
visibility = [
"//tensorflow/core:__subpackages__",
"//tensorflow/security/fuzzing:__subpackages__",
],
deps = [
":numeric_types",
"//tensorflow/core/platform:byte_order",

View File

@ -27,6 +27,16 @@ tf_fuzz_target(
],
)
tf_fuzz_target(
name = "bfloat16_fuzz",
srcs = ["bfloat16_fuzz.cc"],
deps = [
"//tensorflow/core:test",
"//tensorflow/core/framework:bfloat16",
"@com_google_absl//absl/strings",
],
)
tf_fuzz_target(
name = "AreAttrValuesEqual_fuzz",
srcs = ["AreAttrValuesEqual_fuzz.cc"],

View File

@ -0,0 +1,54 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
#include <fuzzer/FuzzedDataProvider.h>
#include <cstdint>
#include <cstdlib>
#include "tensorflow/core/framework/bfloat16.h"
#include "tensorflow/core/platform/test.h"
// This is a fuzzer for tensorflow::FloatToBFloat16 and
// tensorflow::BFloat16ToFloat.
namespace {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fuzzed_data(data, size);
const int array_size = 100;
float float_originals[array_size];
for (int i = 0; i < array_size; ++i) {
float_originals[i] = fuzzed_data.ConsumeFloatingPointInRange(1.0f, 1000.0f);
}
tensorflow::bfloat16 bfloats[array_size];
float floats_converted[array_size];
tensorflow::FloatToBFloat16(float_originals, bfloats, array_size);
tensorflow::BFloat16ToFloat(bfloats, floats_converted, array_size);
for (int i = 0; i < array_size; ++i) {
// The relative error should be less than 1/(2^7) since bfloat16
// has 7 bits mantissa.
// Copied this logic from bfloat16_test.cc
assert(fabs(floats_converted[i] - float_originals[i]) / float_originals[i] <
1.0 / 128);
}
return 0;
}
} // namespace