Fix c10::div_floor_floating compile error (#115647)

Introduced by #113276. I've added a test to catch future regressions.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/115647
Approved by: https://github.com/desertfire, https://github.com/vfdev-5
This commit is contained in:
Jez Ng
2023-12-18 15:00:12 -08:00
committed by PyTorch MergeBot
parent 68c7aac809
commit 110339a310
4 changed files with 32 additions and 16 deletions

View File

@ -4,16 +4,6 @@
#include <ATen/native/DispatchStub.h>
#include <c10/core/Scalar.h>
#include <c10/util/TypeSafeSignMath.h>
#if defined(__CUDA_ARCH__)
#include <c10/cuda/CUDAMathCompat.h>
#define compat_copysign c10::cuda::compat::copysign
#elif defined(__HIPCC__)
#include <c10/hip/HIPMathCompat.h>
#define compat_copysign c10::hip::compat::copysign
#else
#include <c10/util/copysign.h>
#define compat_copysign c10::copysign
#endif
namespace at {

View File

@ -0,0 +1,17 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
#include <c10/util/generic_math.h>
#include <gtest/gtest.h>
#include <cmath>
using namespace ::testing;
TEST(GenericMathTest, div_floor_test) {
EXPECT_EQ(c10::div_floor_floating(5., 0.), INFINITY);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., 2.), 2.);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., -2.), -3.);
EXPECT_EQ(c10::div_floor_integer(5, 2), 2);
EXPECT_EQ(c10::div_floor_integer(5, -2), -3);
}

View File

@ -4,6 +4,17 @@
#include <c10/util/TypeSafeSignMath.h>
#include <cmath>
#if defined(__CUDA_ARCH__)
#include <c10/cuda/CUDAMathCompat.h>
#define C10_COMPAT_COPYSIGN c10::cuda::compat::copysign
#elif defined(__HIPCC__)
#include <c10/hip/HIPMathCompat.h>
#define C10_COMPAT_COPYSIGN c10::hip::compat::copysign
#else
#include <c10/util/copysign.h>
#define C10_COMPAT_COPYSIGN c10::copysign
#endif
// The functions in this file should be header-only as it is used under
// ABI-compatibility mode.
@ -40,7 +51,7 @@ inline C10_HOST_DEVICE scalar_t div_floor_floating(scalar_t a, scalar_t b)
floordiv += scalar_t(1.0);
}
} else {
floordiv = compat_copysign(scalar_t(0), a / b);
floordiv = C10_COMPAT_COPYSIGN(scalar_t(0), a / b);
}
return floordiv;
}

View File

@ -1746,15 +1746,13 @@ class CommonTemplate:
)
def test_floordiv(self):
if self.device == "cpu":
raise unittest.SkipTest("Fails on CPU")
def fn_floor_input(a, i):
n = (i * 1.234) // 8.234
return a + n
self.common(
fn_floor_input, (make_tensor(10, device="cpu", dtype=torch.float32), 33)
fn_floor_input,
(make_tensor(10, device=self.device, dtype=torch.float32), 33),
)
def fn_int_input(a, i):
@ -1762,7 +1760,7 @@ class CommonTemplate:
return a + n
self.common(
fn_int_input, (make_tensor(10, device="cpu", dtype=torch.float32), 33)
fn_int_input, (make_tensor(10, device=self.device, dtype=torch.float32), 33)
)
def test_both_scalars(self):