1 /* Copyright 2018 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 16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_BUFFER_COMPARATOR_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_BUFFER_COMPARATOR_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_module_config.h" 20 #include "tensorflow/compiler/xla/shape.h" 21 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 22 23 namespace xla { 24 namespace gpu { 25 26 // A device-side comparator that compares buffers. 27 class BufferComparator { 28 public: 29 BufferComparator(const BufferComparator&) = delete; 30 BufferComparator(BufferComparator&&) = default; 31 32 BufferComparator(const Shape& shape, const HloModuleConfig& config); 33 34 // Returns true if the two buffers compare equal. The definition of "equal" 35 // is: 36 // * All NaNs equal. 37 // * All fp16 infs are treated as 65505 or -65505. Otherwise, 38 // infs and negative infs compare equal. 39 // * With NaNs and infs taken care of, a and b compare equal iff: 40 // abs(a - b) / (max(abs(a), abs(b)) + 1) < tolerance 41 // 42 // See the implementation for the tolerance value. 43 StatusOr<bool> CompareEqual(se::Stream* stream, se::DeviceMemoryBase lhs, 44 se::DeviceMemoryBase rhs) const; 45 46 private: 47 Shape shape_; 48 HloModuleConfig config_; 49 }; 50 51 } // namespace gpu 52 } // namespace xla 53 54 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_BUFFER_COMPARATOR_H_ 55