diff --git a/tensorflow/core/framework/BUILD b/tensorflow/core/framework/BUILD index 83f254b7f32..1b71908d541 100644 --- a/tensorflow/core/framework/BUILD +++ b/tensorflow/core/framework/BUILD @@ -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", diff --git a/tensorflow/security/fuzzing/BUILD b/tensorflow/security/fuzzing/BUILD index 71df37b7253..068bc418797 100644 --- a/tensorflow/security/fuzzing/BUILD +++ b/tensorflow/security/fuzzing/BUILD @@ -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"], diff --git a/tensorflow/security/fuzzing/bfloat16_fuzz.cc b/tensorflow/security/fuzzing/bfloat16_fuzz.cc new file mode 100644 index 00000000000..043092f7506 --- /dev/null +++ b/tensorflow/security/fuzzing/bfloat16_fuzz.cc @@ -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