Skip to content

Commit d1c8cac

Browse files
author
betterpig
committed
not modify pow_op's original compute logic
1 parent 3a4ca51 commit d1c8cac

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

paddle/fluid/operators/elementwise/elementwise_pow_op.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ struct PowGradDX {
6363
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
6464
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
6565
if (std::is_integral<T>::value) {
66-
return std::llrint(dout * y * std::pow(static_cast<double>(x),
67-
static_cast<double>(y - 1)));
66+
return dout * y *
67+
std::pow(static_cast<double>(x), static_cast<double>(y - 1));
6868
}
6969
#endif
7070
return dout * y * std::pow(x, y - 1);
@@ -74,19 +74,16 @@ struct PowGradDX {
7474
template <typename T, typename Enable = void>
7575
struct PowGradDY {
7676
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
77+
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
78+
if (std::is_integral<T>::value) {
79+
return dout * std::log(static_cast<double>(x)) *
80+
std::pow(static_cast<double>(x), static_cast<double>(y));
81+
}
82+
#endif
7783
return dout * std::log(x) * std::pow(x, y);
7884
}
7985
};
8086

81-
template <typename T>
82-
struct PowGradDY<T, typename std::enable_if<std::is_integral<T>::value>::type> {
83-
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
84-
return std::llrint(
85-
dout * std::log(static_cast<double>(x)) *
86-
std::pow(static_cast<double>(x), static_cast<double>(y)));
87-
}
88-
};
89-
9087
template <typename DeviceContext, typename T>
9188
class ElementwisePowGradKernel : public ElemwiseGradKernel<T> {
9289
public:

paddle/scripts/paddle_build.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ if not defined PYTHON_ROOT set PYTHON_ROOT=C:\Python37
7878
if not defined BUILD_DIR set BUILD_DIR=build
7979
set task_name=%1
8080
set UPLOAD_TP_FILE=OFF
81-
set WITH_TPCACHE=OFF
8281

8382
rem ------initialize the python environment------
8483
set PYTHON_EXECUTABLE=%PYTHON_ROOT%\python.exe
@@ -262,6 +261,7 @@ set ON_INFER=ON
262261
set WITH_TESTING=ON
263262
set WITH_TENSORRT=ON
264263
set WITH_INFERENCE_API_TEST=ON
264+
set WITH_TPCACHE=OFF
265265

266266
call :cmake || goto cmake_error
267267
call :build || goto build_error
@@ -333,7 +333,7 @@ echo %task_name%|findstr wincheck_inference >nul && (
333333
)
334334
set DISTUTILS_USE_SDK=1
335335
rem Windows 10 Kit bin dir
336-
::set PATH=C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64;%PATH%
336+
set PATH=C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64;%PATH%
337337
rem Use 64-bit ToolSet to compile
338338
set PreferredToolArchitecture=x64
339339

python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ def setUp(self):
156156
# dout = 1
157157
self.grad_res = np.asarray([1, 1, 1])
158158
# dx = dout * y * pow(x, y-1)
159-
self.grad_x = (np.rint(self.grad_res * self.y * self.x
160-
**(self.y - 1))).astype("int")
159+
self.grad_x = self.grad_res * self.y * (self.x
160+
**(self.y - 1)).astype("int")
161161
# dy = dout * log(x) * pow(x, y)
162-
self.grad_y = (np.rint(self.grad_res * np.log(self.x) *
163-
(self.x**self.y))).astype("int")
162+
self.grad_y = (self.grad_res * np.log(self.x) *
163+
(self.x**self.y)).astype("int")
164164
print(self.grad_res, self.grad_x, self.grad_y)
165165

166166
def test_grad(self):
@@ -176,7 +176,6 @@ def test_grad(self):
176176
y.stop_gradient = False
177177
res = x**y
178178
res.backward()
179-
print(res.gradient(), x.gradient(), y.gradient())
180179
self.assertTrue(np.array_equal(res.gradient(), self.grad_res))
181180
self.assertTrue(np.array_equal(x.gradient(), self.grad_x))
182181
self.assertTrue(np.array_equal(y.gradient(), self.grad_y))

0 commit comments

Comments
 (0)