Skip to content

Commit

Permalink
Merge pull request #17 from JiabinYang/run_backward
Browse files Browse the repository at this point in the history
2 python api
  • Loading branch information
JiabinYang committed Nov 10, 2021
2 parents 348e091 + a47b8fb commit 2119c06
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
25 changes: 25 additions & 0 deletions paddle/fluid/pybind/eager_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License. */

#include "paddle/fluid/eager/api/api.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/backward.h"
#include "paddle/fluid/eager/function_api.h"
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memcpy.h"
Expand Down Expand Up @@ -232,6 +233,25 @@ static PyObject* eager_api_to_tensor(PyObject* self, PyObject* args,
}
}

static PyObject* eager_api_retain_grad_for_tensor(PyObject* self,
PyObject* args,
PyObject* kwargs) {
RetainGradForTensor(CastPyArg2EagerTensor(PyTuple_GET_ITEM(args, 0), 0));
Py_INCREF(Py_None);
return Py_None;
}

static PyObject* eager_api_run_backward(PyObject* self, PyObject* args,
PyObject* kwargs) {
auto tensors = CastPyArg2VectorOfEagerTensor(PyTuple_GET_ITEM(args, 0), 0);
auto grad_tensors =
CastPyArg2VectorOfEagerTensor(PyTuple_GET_ITEM(args, 1), 1);
RunBackward(tensors, grad_tensors,
CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 2), 2));
Py_INCREF(Py_None);
return Py_None;
}

PyMethodDef variable_functions[] = {
{"to_tensor", (PyCFunction)(void (*)(void))eager_api_to_tensor,
METH_VARARGS | METH_KEYWORDS, NULL},
Expand All @@ -240,6 +260,11 @@ PyMethodDef variable_functions[] = {
{"_set_expected_place",
(PyCFunction)(void (*)(void))eager_api_set_expected_place,
METH_VARARGS | METH_KEYWORDS, NULL},
{"retain_grad_for_tensor",
(PyCFunction)(void (*)(void))eager_api_retain_grad_for_tensor,
METH_VARARGS | METH_KEYWORDS, NULL},
{"run_backward", (PyCFunction)(void (*)(void))eager_api_run_backward,
METH_VARARGS | METH_KEYWORDS, NULL},
{NULL, NULL, 0, NULL}};

void BindFunctions(PyObject* module) {
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/pybind/eager_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License. */
#include "paddle/pten/core/dense_tensor.h"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wconversion-null"
#pragma GCC diagnostic ignored "-Wwrite-strings"

namespace paddle {
namespace pybind {
Expand Down
57 changes: 57 additions & 0 deletions paddle/fluid/pybind/eager_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,63 @@ std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos) {
}
}

egr::EagerTensor CastPyArg2EagerTensor(PyObject* obj, ssize_t arg_pos) {
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(pEagerTensorType))) {
return reinterpret_cast<EagerTensorObject*>(obj)->eagertensor;
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"argument (position %d) must be "
"EagerTensor, but got %s",
arg_pos + 1, reinterpret_cast<PyTypeObject*>(obj->ob_type)->tp_name));
}
}

std::vector<egr::EagerTensor> CastPyArg2VectorOfEagerTensor(PyObject* obj,
ssize_t arg_pos) {
std::vector<egr::EagerTensor> result;
if (PyList_Check(obj)) {
Py_ssize_t len = PyList_Size(obj);
PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) {
item = PyList_GetItem(obj, i);
if (PyObject_IsInstance(item,
reinterpret_cast<PyObject*>(pEagerTensorType))) {
result.emplace_back(
reinterpret_cast<EagerTensorObject*>(item)->eagertensor);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"argument (position %d) must be "
"list of bool, but got %s at pos %d",
arg_pos + 1,
reinterpret_cast<PyTypeObject*>(item->ob_type)->tp_name, i));
}
}
} else if (PyTuple_Check(obj)) {
Py_ssize_t len = PyTuple_Size(obj);
PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) {
item = PyTuple_GetItem(obj, i);
if (PyObject_IsInstance(item,
reinterpret_cast<PyObject*>(pEagerTensorType))) {
result.emplace_back(
reinterpret_cast<EagerTensorObject*>(item)->eagertensor);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"argument (position %d) must be "
"list of bool, but got %s at pos %d",
arg_pos + 1,
reinterpret_cast<PyTypeObject*>(item->ob_type)->tp_name, i));
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"argument (position %d) must be "
"list or tuple, but got %s",
arg_pos + 1, reinterpret_cast<PyTypeObject*>(obj->ob_type)->tp_name));
}
return result;
}

PyObject* ToPyObject(bool value) {
if (value) {
Py_INCREF(Py_True);
Expand Down
3 changes: 3 additions & 0 deletions paddle/fluid/pybind/eager_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ int CastPyArg2AttrInt(PyObject* obj, ssize_t arg_pos);
int64_t CastPyArg2AttrLong(PyObject* obj, ssize_t arg_pos);
float CastPyArg2AttrFloat(PyObject* obj, ssize_t arg_pos);
std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos);
egr::EagerTensor CastPyArg2EagerTensor(PyObject* obj, ssize_t arg_pos);
std::vector<egr::EagerTensor> CastPyArg2VectorOfEagerTensor(PyObject* obj,
ssize_t arg_pos);

PyObject* ToPyObject(int value);
PyObject* ToPyObject(bool value);
Expand Down

1 comment on commit 2119c06

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 2119c06 Nov 10, 2021

Choose a reason for hiding this comment

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

🕵️ CI failures summary

🔍 PR: #17 Commit ID: 2119c06 contains failed CI.

🔹 Failed: PR-CI-musl

Unknown Failed
2021-11-10 09:34:58 [ 18%] Built target paddle_infer_contrib
2021-11-10 09:34:58 [ 18%] Building CXX object paddle/fluid/framework/fleet/CMakeFiles/ps_gpu_wrapper.dir/ps_gpu_wrapper.cc.o
2021-11-10 09:34:58 [ 18%] Linking CXX static library libps_gpu_wrapper.a
2021-11-10 09:34:58 [ 18%] Built target ps_gpu_wrapper
2021-11-10 09:34:58 In file included from /paddle/paddle/fluid/eager/legacy/type_def.h:16,
2021-11-10 09:34:58 from /paddle/paddle/fluid/framework/details/nan_inf_utils.h:20,
2021-11-10 09:34:58 from /paddle/paddle/fluid/framework/details/nan_inf_utils_detail.cc:15:
2021-11-10 09:34:58 /paddle/paddle/fluid/eager/eager_tensor.h:23:10: fatal error: paddle/pten/hapi/all.h: No such file or directory
2021-11-10 09:34:58 23 | #include "paddle/pten/hapi/all.h"
2021-11-10 09:34:58 | ^~~~~~~~~~~~~~~~~~~~~~~~
2021-11-10 09:34:58 compilation terminated.
2021-11-10 09:34:58 make[2]: *** [paddle/fluid/framework/details/CMakeFiles/nan_inf_utils.dir/build.make:63: paddle/fluid/framework/details/CMakeFiles/nan_inf_utils.dir/nan_inf_utils_detail.cc.o] Error 1
2021-11-10 09:34:58 make[1]: *** [CMakeFiles/Makefile2:24315: paddle/fluid/framework/details/CMakeFiles/nan_inf_utils.dir/all] Error 2
2021-11-10 09:34:58 make[1]: *** Waiting for unfinished jobs....
2021-11-10 09:35:03 [ 18%] Linking CXX static library libpten.a
2021-11-10 09:35:03 [ 18%] Built target pten
2021-11-10 09:35:06 [ 18%] Linking CXX static library libdata_transform.a
2021-11-10 09:35:06 [ 18%] Built target data_transform
2021-11-10 09:35:06 make: *** [Makefile:130: all] Error 2

🔹 Failed: PR-CI-Mac-Python3

build_failed
2021-11-10 09:37:21 + collect_ccache_hits
2021-11-10 09:37:21 ++ ccache -s
2021-11-10 09:37:21 ++ grep 'cache hit rate'
2021-11-10 09:37:21 ++ awk '{print $4}'
2021-11-10 09:37:21 + rate=95.19
2021-11-10 09:37:21 + echo 'ccache hit rate: 95.19%'
2021-11-10 09:37:21 ccache hit rate: 95.19%
2021-11-10 09:37:21 + echo 'ipipe_log_param_Ccache_Hit_Rate: 95.19%'
2021-11-10 09:37:21 + '[' 2 '!=' 0 ']'
2021-11-10 09:37:21 + exit 7
2021-11-10 09:37:21 + EXCODE=7
2021-11-10 09:37:21 + echo 'EXCODE: 7'
2021-11-10 09:37:21 EXCODE: 7
2021-11-10 09:37:21 + echo 'ipipe_log_param_EXCODE: 7'
2021-11-10 09:37:21 ipipe_log_param_EXCODE: 7
2021-11-10 09:37:21 + '[' 7 -eq 0 ']'
2021-11-10 09:37:21 + set +x
2021-11-10 09:37:21 Sorry, build failed.
2021-11-10 09:37:21 + exit 7

🔹 Failed: PR-CI-Windows

build_failed
2021-11-10 09:37:46 注锟�: 锟�:   ..\paddle/fluid/operators/recurrent_op.h
2021-11-10 09:37:46 注锟�: 锟�: ..\paddle/fluid/operators/controlflow/while_op_helper.h
2021-11-10 09:37:46 注锟�: 锟�: ..\paddle/fluid/platform/profiler.h
2021-11-10 09:37:46 注锟�: 锟�: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\forward_list
2021-11-10 09:37:46 注锟�: 锟�: ..\paddle/fluid/platform/event.h
2021-11-10 09:37:46 注锟�: 锟�: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include\cuda_runtime.h
2021-11-10 09:37:46 注锟�: 锟�: .\paddle/fluid/platform/profiler.pb.h
2021-11-10 09:37:46 注锟�: 锟�: ..\paddle/fluid/platform/mkldnn_helper.h
2021-11-10 09:37:46 ninja: build stopped: subcommand failed.
2021-11-10 09:37:46 7
2021-11-10 09:37:46 Build Paddle failed, will exit
2021-11-10 09:37:47 EXCODE: 7

🔹 Failed: PR-CI-ROCM-Compile

Unknown Failed
2021-11-10 09:39:03 ccache hit rate: 96.68%
2021-11-10 09:39:03 + '[' 2 '!=' 0 ']'
2021-11-10 09:39:03 + exit 7
2021-11-10 09:39:03 + EXCODE=7
2021-11-10 09:39:03 + export current_dir=/paddle
2021-11-10 09:39:03 + current_dir=/paddle
2021-11-10 09:39:03 + set +x
2021-11-10 09:39:03 No such file or directory: /paddle/build/coverage-diff
2021-11-10 09:39:03 + SOURCE=/paddle/build/coverage-diff
2021-11-10 09:39:03 + [[ -d /paddle/build/coverage-diff ]]
2021-11-10 09:39:03 + [[ -f /paddle/build/coverage-diff ]]
2021-11-10 09:39:03 + echo 'No such file or directory: /paddle/build/coverage-diff'
2021-11-10 09:39:03 + exit 0
2021-11-10 09:39:03 report uploaded
2021-11-10 09:39:03 ===================================================================
2021-11-10 09:39:03 c++-coverage
2021-11-10 09:39:03 https://xly.bce.baidu.com/ipipe/ipipe-report/report/9076848/c++-coverage/
2021-11-10 09:39:03 ===================================================================
2021-11-10 09:39:03 Sorry, build failed.

🔹 Failed: PR-CI-Kunlun

Unknown Failed
2021-11-10 09:38:33 [ 14%] Linking CXX static library libimperative_gloo_context.a
2021-11-10 09:38:33 [ 14%] Built target imperative_gloo_context
2021-11-10 09:38:34 [ 14%] Linking CXX static library libdevice_worker.a
2021-11-10 09:38:34 [ 14%] Built target device_worker
2021-11-10 09:39:34 [ 14%] Linking CXX static library libmath_function.a
2021-11-10 09:39:35 [ 14%] Built target math_function
2021-11-10 09:39:35 Makefile:140: recipe for target 'all' failed
2021-11-10 09:39:35 make: *** [all] Error 2
2021-11-10 09:39:35 + build_error=2
2021-11-10 09:39:35 + collect_ccache_hits
2021-11-10 09:39:35 ++ ccache -s
2021-11-10 09:39:35 ++ grep 'cache hit rate'
2021-11-10 09:39:35 ++ awk '{print $4}'
2021-11-10 09:39:35 ccache hit rate: 0.00%
2021-11-10 09:39:35 + rate=0.00
2021-11-10 09:39:35 + echo 'ccache hit rate: 0.00%'
2021-11-10 09:39:35 + echo 'ipipe_log_param_Ccache_Hit_Rate: 0.00%'
2021-11-10 09:39:35 + '[' 2 '!=' 0 ']'
2021-11-10 09:39:35 + exit 7

🔹 Failed: PR-CI-Model-benchmark

Unknown Failed
2021-11-10 09:39:39 + exit 7
2021-11-10 09:39:39 + '[' 7 -ne 0 ']'
2021-11-10 09:39:39 + echo 'build paddle failed.'
2021-11-10 09:39:39 build paddle failed.
2021-11-10 09:39:39 + exit 1
2021-11-10 09:39:39 + EXCODE=1
2021-11-10 09:39:39 + '[' OFF == ON ']'
2021-11-10 09:39:39 + '[' 1 -eq 0 ']'
2021-11-10 09:39:39 + [[ 1 -eq 0 ]]
2021-11-10 09:39:39 + [[ 1 -eq 4 ]]
2021-11-10 09:39:39 + [[ 1 -eq 5 ]]
2021-11-10 09:39:39 + [[ 1 -eq 6 ]]
2021-11-10 09:39:39 + [[ 1 -eq 7 ]]
2021-11-10 09:39:39 + [[ 1 -eq 8 ]]
2021-11-10 09:39:39 + [[ 1 -eq 9 ]]
2021-11-10 09:39:39 + set -x
2021-11-10 09:39:39 + exit 1
2021-11-10 09:39:39 {build code state=1}
2021-11-10 09:39:49 kill agent BUILD_CODE_FAIL

🔹 Failed: PR-CI-Windows-OPENBLAS

build_failed
2021-11-10 09:42:14        "C:\home\workspace\Paddle\build\ALL_BUILD.vcxproj" (default target) (1) ->
2021-11-10 09:42:14 "C:\home\workspace\Paddle\build\paddle\pten\tests\api\test_matmul_api.vcxproj" (default target) (605) ->
2021-11-10 09:42:14 LINK : fatal error LNK1104: cannot open file 'python37.lib' [C:\home\workspace\Paddle\build\paddle\pten\tests\api\test_matmul_api.vcxproj]
2021-11-10 09:42:14 "C:\home\workspace\Paddle\build\ALL_BUILD.vcxproj" (default target) (1) ->
2021-11-10 09:42:14 "C:\home\workspace\Paddle\build\paddle\pten\tests\api\test_mean_api.vcxproj" (default target) (606) ->
2021-11-10 09:42:14 LINK : fatal error LNK1104: cannot open file 'python37.lib' [C:\home\workspace\Paddle\build\paddle\pten\tests\api\test_mean_api.vcxproj]
2021-11-10 09:42:14 308 Warning(s)
2021-11-10 09:42:14 11 Error(s)
2021-11-10 09:42:14 Time Elapsed 00:07:05.42
2021-11-10 09:42:14 7
2021-11-10 09:42:15 Build Paddle failed, will exit
2021-11-10 09:42:15 EXCODE: 7

🔹 Failed: PR-CI-GpuPS

Unknown Failed
2021-11-10 09:43:48 ++ awk '{print $4}'
2021-11-10 09:43:48 + rate=9.54
2021-11-10 09:43:48 + echo 'ccache hit rate: 9.54%'
2021-11-10 09:43:48 ccache hit rate: 9.54%
2021-11-10 09:43:48 + echo 'ipipe_log_param_Ccache_Hit_Rate: 9.54%'
2021-11-10 09:43:48 + '[' 2 '!=' 0 ']'
2021-11-10 09:43:48 + exit 7
2021-11-10 09:43:48 + EXCODE=7
2021-11-10 09:43:48 + '[' 7 -eq 0 ']'
2021-11-10 09:43:48 + [[ 7 -eq 0 ]]
2021-11-10 09:43:48 + [[ 7 -eq 4 ]]
2021-11-10 09:43:48 + [[ 7 -eq 5 ]]
2021-11-10 09:43:48 + [[ 7 -eq 6 ]]
2021-11-10 09:43:48 + [[ 7 -eq 7 ]]
2021-11-10 09:43:48 + echo 'Sorry, build failed.'
2021-11-10 09:43:48 Sorry, build failed.
2021-11-10 09:43:48 + set -x
2021-11-10 09:43:48 + exit 7
2021-11-10 09:43:48 {build code state=7}

🔹 Failed: PR-CI-Static-Check

build_failed
2021-11-10 09:47:39 [ 16%] Linking CXX static library libmanipulation_cuda.a
2021-11-10 09:47:39 [ 16%] Built target manipulation_cuda
2021-11-10 09:47:48 [ 16%] Linking CXX static library libdepthwise_conv.a
2021-11-10 09:47:48 [ 16%] Built target depthwise_conv
2021-11-10 09:50:55 [ 16%] Linking CXX static library libmath_function.a
2021-11-10 09:50:55 [ 16%] Built target math_function
2021-11-10 09:50:55 Makefile:129: recipe for target 'all' failed
2021-11-10 09:50:55 make: *** [all] Error 2
2021-11-10 09:50:55 ccache hit rate: 9.47%
2021-11-10 09:50:55 + EXCODE=7
2021-11-10 09:50:55 + echo 'EXCODE: 7'
2021-11-10 09:50:55 EXCODE: 7
2021-11-10 09:50:55 + echo 'ipipe_log_param_EXCODE: 7'
2021-11-10 09:50:55 ipipe_log_param_EXCODE: 7
2021-11-10 09:50:55 + '[' 7 -eq 0 ']'
2021-11-10 09:50:55 + set +x
2021-11-10 09:50:55 Sorry, build failed.
2021-11-10 09:50:55 + exit 7
2021-11-10 09:50:55 {build code state=7}

🔹 Failed: PR-CI-OP-benchmark

build_failed
2021-11-10 09:53:03 ccache hit rate: 94.69%
2021-11-10 09:53:03 + echo 'ipipe_log_param_Ccache_Hit_Rate: 94.69%'
2021-11-10 09:53:03 + '[' 2 '!=' 0 ']'
2021-11-10 09:53:03 + exit 7
2021-11-10 09:53:03 + '[' 7 -ne 0 ']'
2021-11-10 09:53:03 + LOG '[FATAL] compile fail.'
2021-11-10 09:53:03 + echo '[tools/test_ci_op_benchmark.sh:175] [FATAL] compile fail.'
2021-11-10 09:53:03 [tools/test_ci_op_benchmark.sh:175] [FATAL] compile fail.
2021-11-10 09:53:03 + exit 7
2021-11-10 09:53:03 + EXCODE=7
2021-11-10 09:53:03 + echo 'EXCODE: 7'
2021-11-10 09:53:03 EXCODE: 7
2021-11-10 09:53:03 + echo 'ipipe_log_param_EXCODE: 7'
2021-11-10 09:53:03 ipipe_log_param_EXCODE: 7
2021-11-10 09:53:03 + '[' 7 -eq 0 ']'
2021-11-10 09:53:03 + set +x
2021-11-10 09:53:03 Sorry, build failed.
2021-11-10 09:53:03 + exit 7
2021-11-10 09:53:03 {build code state=7}

Please sign in to comment.