Skip to content

Commit

Permalink
not modify pow_op's original compute logic
Browse files Browse the repository at this point in the history
  • Loading branch information
betterpig committed Jan 10, 2022
1 parent 3a4ca51 commit d1c8cac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
19 changes: 8 additions & 11 deletions paddle/fluid/operators/elementwise/elementwise_pow_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ struct PowGradDX {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
if (std::is_integral<T>::value) {
return std::llrint(dout * y * std::pow(static_cast<double>(x),
static_cast<double>(y - 1)));
return dout * y *
std::pow(static_cast<double>(x), static_cast<double>(y - 1));
}
#endif
return dout * y * std::pow(x, y - 1);
Expand All @@ -74,19 +74,16 @@ struct PowGradDX {
template <typename T, typename Enable = void>
struct PowGradDY {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
if (std::is_integral<T>::value) {
return dout * std::log(static_cast<double>(x)) *
std::pow(static_cast<double>(x), static_cast<double>(y));
}
#endif
return dout * std::log(x) * std::pow(x, y);
}
};

template <typename T>
struct PowGradDY<T, typename std::enable_if<std::is_integral<T>::value>::type> {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
return std::llrint(
dout * std::log(static_cast<double>(x)) *
std::pow(static_cast<double>(x), static_cast<double>(y)));
}
};

template <typename DeviceContext, typename T>
class ElementwisePowGradKernel : public ElemwiseGradKernel<T> {
public:
Expand Down
4 changes: 2 additions & 2 deletions paddle/scripts/paddle_build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ if not defined PYTHON_ROOT set PYTHON_ROOT=C:\Python37
if not defined BUILD_DIR set BUILD_DIR=build
set task_name=%1
set UPLOAD_TP_FILE=OFF
set WITH_TPCACHE=OFF

rem ------initialize the python environment------
set PYTHON_EXECUTABLE=%PYTHON_ROOT%\python.exe
Expand Down Expand Up @@ -262,6 +261,7 @@ set ON_INFER=ON
set WITH_TESTING=ON
set WITH_TENSORRT=ON
set WITH_INFERENCE_API_TEST=ON
set WITH_TPCACHE=OFF

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ def setUp(self):
# dout = 1
self.grad_res = np.asarray([1, 1, 1])
# dx = dout * y * pow(x, y-1)
self.grad_x = (np.rint(self.grad_res * self.y * self.x
**(self.y - 1))).astype("int")
self.grad_x = self.grad_res * self.y * (self.x
**(self.y - 1)).astype("int")
# dy = dout * log(x) * pow(x, y)
self.grad_y = (np.rint(self.grad_res * np.log(self.x) *
(self.x**self.y))).astype("int")
self.grad_y = (self.grad_res * np.log(self.x) *
(self.x**self.y)).astype("int")
print(self.grad_res, self.grad_x, self.grad_y)

def test_grad(self):
Expand All @@ -176,7 +176,6 @@ def test_grad(self):
y.stop_gradient = False
res = x**y
res.backward()
print(res.gradient(), x.gradient(), y.gradient())
self.assertTrue(np.array_equal(res.gradient(), self.grad_res))
self.assertTrue(np.array_equal(x.gradient(), self.grad_x))
self.assertTrue(np.array_equal(y.gradient(), self.grad_y))
Expand Down

1 comment on commit d1c8cac

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.