Skip to content

Commit

Permalink
Merge pull request #10522 from sandipmgiri/master
Browse files Browse the repository at this point in the history
np.float64("np.inf").astype(np.int32) is negative on x86 but positive on ppc64le
  • Loading branch information
drpngx authored Jun 30, 2017
2 parents 6fff4cb + c8d78a1 commit d0d2308
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
13 changes: 10 additions & 3 deletions tensorflow/core/platform/profile_utils/cpu_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ namespace profile_utils {

static ICpuUtilsHelper* cpu_utils_helper_instance_ = nullptr;

/* static */ int64 CpuUtils::GetCycleCounterFrequency() {
static const int64 cpu_frequency = GetCycleCounterFrequencyImpl();
return cpu_frequency;
#if defined(__powerpc__) || defined(__ppc__) && ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
/* static */ uint64 CpuUtils::GetCycleCounterFrequency() {
static const uint64 cpu_frequency = GetCycleCounterFrequencyImpl();
return cpu_frequency;
}
#else
/* static */ int64 CpuUtils::GetCycleCounterFrequency() {
static const int64 cpu_frequency = GetCycleCounterFrequencyImpl();
return cpu_frequency;
}
#endif

/* static */ double CpuUtils::GetMicroSecPerClock() {
static const double micro_sec_per_clock =
Expand Down
6 changes: 5 additions & 1 deletion tensorflow/core/platform/profile_utils/cpu_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ class CpuUtils {
// Return cycle counter frequency.
// As this method caches the cpu frequency internally,
// the first call will incur overhead, but not subsequent calls.
static int64 GetCycleCounterFrequency();
#if defined(__powerpc__) || defined(__ppc__) && ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
static uint64 GetCycleCounterFrequency();
#else
static int64 GetCycleCounterFrequency();
#endif

// Return micro secound per each clock
// As this method caches the cpu frequency internally,
Expand Down
12 changes: 9 additions & 3 deletions tensorflow/core/platform/profile_utils/cpu_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ TEST_F(CpuUtilsTest, CheckGetCurrentClockCycle) {
}

TEST_F(CpuUtilsTest, CheckCycleCounterFrequency) {
const int64 cpu_frequency = CpuUtils::GetCycleCounterFrequency();
CHECK_GT(cpu_frequency, 0);
CHECK_NE(cpu_frequency, CpuUtils::INVALID_FREQUENCY);
#if defined(__powerpc__) || defined(__ppc__) && ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
const uint64 cpu_frequency = CpuUtils::GetCycleCounterFrequency();
CHECK_GT(cpu_frequency, 0);
CHECK_NE(cpu_frequency, unsigned(CpuUtils::INVALID_FREQUENCY));
#else
const int64 cpu_frequency = CpuUtils::GetCycleCounterFrequency();
CHECK_GT(cpu_frequency, 0);
CHECK_NE(cpu_frequency, CpuUtils::INVALID_FREQUENCY);
#endif
if (DBG) {
LOG(INFO) << "Cpu frequency = " << cpu_frequency;
}
Expand Down
14 changes: 11 additions & 3 deletions tensorflow/python/kernel_tests/cast_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import numpy as np
import sys
import platform

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
Expand Down Expand Up @@ -146,9 +147,16 @@ def testInfNan(self):
if sys.byteorder == "big":
self._compare(np.inf, np.int32, i4.max, False)
self._compare(np.inf, np.int64, i8.max, False)
else:
self._compare(np.inf, np.int32, i4.min, False)
self._compare(np.inf, np.int64, i8.min, False)
else:
# np.float64("np.inf").astype(np.int32) is negative on x86 but positive on ppc64le
# Numpy link to relevant discussion - https://github.com/numpy/numpy/issues/9040
# Tensorflow link to relevant discussion - https://github.com/tensorflow/tensorflow/issues/9360
if platform.machine() == "ppc64le":
self._compare(-np.inf, np.int32, i4.min, False)
self._compare(-np.inf, np.int64, i8.min, False)
else:
self._compare(np.inf, np.int32, i4.min, False)
self._compare(np.inf, np.int64, i8.min, False)
self._compare(-np.inf, np.float32, -np.inf, False)
self._compare(-np.inf, np.float64, -np.inf, False)
self._compare(-np.inf, np.int32, i4.min, False)
Expand Down

0 comments on commit d0d2308

Please sign in to comment.