| 646 | // Returns true if no differences were seen, false otherwise. |
| 647 | template <typename ElementType, typename ComparisonType> |
| 648 | StatusOr<bool> HostCompare(se::Stream* stream, se::DeviceMemoryBase lhs, |
| 649 | se::DeviceMemoryBase rhs) { |
| 650 | int64 n = lhs.size() / sizeof(ElementType); |
| 651 | std::vector<ElementType> host_lhs(n), host_rhs(n); |
| 652 | stream->ThenMemcpy(host_lhs.data(), lhs, lhs.size()); |
| 653 | stream->ThenMemcpy(host_rhs.data(), rhs, rhs.size()); |
| 654 | TF_RETURN_IF_ERROR(stream->BlockHostUntilDone()); |
| 655 | |
| 656 | const auto canonicalize = [](ComparisonType a) -> ComparisonType { |
| 657 | if (std::is_same<ElementType, Eigen::half>::value && a) { |
| 658 | constexpr ComparisonType kMaxFp16Value = 65505.; |
| 659 | if (std::isnan(a)) { |
| 660 | return a; |
| 661 | } |
| 662 | return std::max(-kMaxFp16Value, std::min(a, kMaxFp16Value)); |
| 663 | } |
| 664 | return a; |
| 665 | }; |
| 666 | int differences_seen = 0; |
| 667 | for (int64 i = 0; i < n && differences_seen < 10; i++) { |
| 668 | auto original_lhs = static_cast<ComparisonType>(host_lhs[i]); |
| 669 | auto original_rhs = static_cast<ComparisonType>(host_rhs[i]); |
| 670 | ComparisonType lhs = canonicalize(original_lhs); |
| 671 | ComparisonType rhs = canonicalize(original_rhs); |
| 672 | if (std::isnan(lhs) && std::isnan(rhs)) { |
| 673 | continue; |
| 674 | } |
| 675 | if (std::isinf(lhs) && std::isinf(rhs) && lhs == rhs) { |
| 676 | continue; |
| 677 | } |
| 678 | if (std::isfinite(lhs) != std::isfinite(rhs) || |
| 679 | !(std::abs(lhs - rhs) / (std::max(std::abs(lhs), std::abs(rhs)) + 1) < |
| 680 | kTolerance)) { |
| 681 | differences_seen++; |
| 682 | LOG(ERROR) << "Difference at " << i << ": " << original_lhs << " vs " |
| 683 | << original_rhs; |
| 684 | } |
| 685 | } |
| 686 | return differences_seen == 0; |
| 687 | } |
| 688 | |
| 689 | template <typename ElementT, typename ComparisonT> |
| 690 | static StatusOr<bool> CompareEqualParameterized(se::Stream* stream, |
nothing calls this directly
no test coverage detected