MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / HostCompare

Function HostCompare

tensorflow/compiler/xla/service/gpu/buffer_comparator.cc:648–687  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

646// Returns true if no differences were seen, false otherwise.
647template <typename ElementType, typename ComparisonType>
648StatusOr<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
689template <typename ElementT, typename ComparisonT>
690static StatusOr<bool> CompareEqualParameterized(se::Stream* stream,

Callers

nothing calls this directly

Calls 10

isnanClass · 0.85
canonicalizeFunction · 0.85
isinfClass · 0.85
isfiniteClass · 0.85
maxFunction · 0.50
minFunction · 0.50
absClass · 0.50
sizeMethod · 0.45
dataMethod · 0.45
BlockHostUntilDoneMethod · 0.45

Tested by

no test coverage detected