Skip to content

Commit

Permalink
Merge pull request #13 from JiabinYang/add_properties
Browse files Browse the repository at this point in the history
add some properties
  • Loading branch information
wanghuancoder authored Oct 26, 2021
2 parents 92946fb + bb9b180 commit 28690c7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
4 changes: 4 additions & 0 deletions paddle/fluid/eager/autograd_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class AutogradMeta : public AbstractAutogradMeta {
}
}

bool Persistable() const { return persistable_; }

void SetPersistable(bool persistable) { persistable_ = persistable; }

private:
// TODO(jiabin) :Should we use pointer instead of object?
egr::EagerTensor grad_;
Expand Down
71 changes: 56 additions & 15 deletions paddle/fluid/pybind/eager_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,15 @@ static const int numpy_initialized_m = init_numpy_p();

extern PyTypeObject* pEagerTensorType;

PyObject* eager_tensor_properties_get_shape(EagerTensorObject* self,
void* closure) {
auto ddim = self->eagertensor.shape();
std::vector<int64_t> value;
size_t rank = static_cast<size_t>(ddim.size());
value.resize(rank);
for (size_t i = 0; i < rank; i++) {
value[i] = ddim[i];
}
PyObject* eager_tensor_properties_get_name(EagerTensorObject* self,
void* closure) {
return ToPyObject(self->eagertensor.name());
}

return ToPyObject(value);
int eager_tensor_properties_set_name(EagerTensorObject* self, PyObject* value,
void* closure) {
self->eagertensor.set_name(CastPyArg2AttrString(value, 0));
return 0;
}

PyObject* eager_tensor_properties_get_stop_gradient(EagerTensorObject* self,
Expand All @@ -73,9 +71,38 @@ int eager_tensor_properties_set_stop_gradient(EagerTensorObject* self,
return 0;
}

PyObject* eager_tensor_properties_get_dtype(EagerTensorObject* self,
PyObject* eager_tensor_properties_get_persistable(EagerTensorObject* self,
void* closure) {
auto meta = egr::EagerUtils::unsafe_autograd_meta(self->eagertensor);
return ToPyObject(meta->Persistable());
}

int eager_tensor_properties_set_persistable(EagerTensorObject* self,
PyObject* value, void* closure) {
auto meta = egr::EagerUtils::unsafe_autograd_meta(self->eagertensor);
meta->SetPersistable(CastPyArg2AttrBoolean(value, 0));
return 0;
}

PyObject* eager_tensor_properties_get_shape(EagerTensorObject* self,
void* closure) {
return ToPyObject(pten::DataType2String(self->eagertensor.type()));
auto ddim = self->eagertensor.shape();
std::vector<int64_t> value;
size_t rank = static_cast<size_t>(ddim.size());
value.resize(rank);
for (size_t i = 0; i < rank; i++) {
value[i] = ddim[i];
}

return ToPyObject(value);
}

PyObject* eager_tensor_properties_get_place(EagerTensorObject* self,
void* closure) {
auto place = self->eagertensor.place();
auto obj = ::pybind11::cast(place);
obj.inc_ref();
return obj.ptr();
}

PyObject* eager_tensor_properties_get_place_str(EagerTensorObject* self,
Expand All @@ -85,15 +112,29 @@ PyObject* eager_tensor_properties_get_place_str(EagerTensorObject* self,
return ToPyObject(ostr.str());
}

PyObject* eager_tensor_properties_get_dtype(EagerTensorObject* self,
void* closure) {
return ToPyObject(pten::DataType2String(self->eagertensor.type()));
}

struct PyGetSetDef variable_properties[] = {
{"shape", (getter)eager_tensor_properties_get_shape, nullptr, nullptr,
nullptr},
{"name", (getter)eager_tensor_properties_get_name,
(setter)eager_tensor_properties_set_name, nullptr, nullptr},
{"stop_gradient", (getter)eager_tensor_properties_get_stop_gradient,
(setter)eager_tensor_properties_set_stop_gradient, nullptr, nullptr},
{"dtype", (getter)eager_tensor_properties_get_dtype, nullptr, nullptr,
{"persistable", (getter)eager_tensor_properties_get_persistable,
(setter)eager_tensor_properties_set_persistable, nullptr, nullptr},
{"shape", (getter)eager_tensor_properties_get_shape, nullptr, nullptr,
nullptr},
// {"is_leaf", (getter)eager_tensor_properties_get_is_leaf, nullptr,
// nullptr,
// nullptr},
{"place", (getter)eager_tensor_properties_get_place, nullptr, nullptr,
nullptr},
{"_place_str", (getter)eager_tensor_properties_get_place_str, nullptr,
nullptr, nullptr},
{"dtype", (getter)eager_tensor_properties_get_dtype, nullptr, nullptr,
nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr}};

} // namespace pybind
Expand Down
48 changes: 19 additions & 29 deletions python/paddle/fluid/tests/unittests/test_egr_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,23 @@ def test_scale_base(self):
with eager_guard():
paddle.set_device("cpu")
arr = np.ones([4, 16, 16, 32]).astype('float32')
a = paddle.to_tensor(arr, 'float32', core.CPUPlace())
print(arr)
print("=============")
print(a)
a = core.eager.scale(a, 2.0, 0.9, True, False)
tensor = paddle.to_tensor(arr, 'float32', core.CPUPlace())
print(tensor)
tensor = core.eager.scale(tensor, 2.0, 0.9, True, False)
for i in range(0, 100):
a = core.eager.scale(a, 2.0, 0.9, True, False)
print(a.shape)
print(a.stop_gradient)
a.stop_gradient = False
print(a.stop_gradient)
a.stop_gradient = True
print(a.stop_gradient)
print(a)


with eager_guard():
paddle.set_device("cpu")
arr = np.ones([4, 16, 16, 32]).astype('float32')
a = paddle.to_tensor(arr, 'float32', core.CPUPlace())
a = core.eager.scale(a, 2.0, 0.9, True, False)
for i in range(0, 100):
a = core.eager.scale(a, 2.0, 0.9, True, False)
print(a.shape)
print(a.stop_gradient)
a.stop_gradient = False
print(a.stop_gradient)
a.stop_gradient = True
print(a.stop_gradient)
print(a)
tensor = core.eager.scale(tensor, 2.0, 0.9, True, False)
print(tensor)
self.assertEqual(tensor.shape, [4, 16, 16, 32])
self.assertEqual(tensor.stop_gradient, True)
tensor.stop_gradient = False
self.assertEqual(tensor.stop_gradient, False)
tensor.stop_gradient = True
self.assertEqual(tensor.stop_gradient, False)
tensor.name = 'tensor_name_test'
self.assertEqual(tensor.name, 'tensor_name_test')
self.assertEqual(tensor.persistable, False)
tensor.persistable = True
self.assertEqual(tensor.persistable, True)
tensor.persistable = False
self.assertEqual(tensor.persistable, False)
self.assertTrue(tensor.place.is_cpu_place())

1 comment on commit 28690c7

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 28690c7 Oct 26, 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: #13 Commit ID: 28690c7 contains failed CI.

🔹 Failed: PR-CI-iScan-Python

Unknown Failed
Unknown Failed

🔹 Failed: PR-CI-musl

Unknown Failed
2021-10-26 15:05:49 + git config --global user.name PaddleCI
2021-10-26 15:05:49 + git config --global user.email paddle_ci@example.com
2021-10-26 15:05:49 + git merge --no-edit develop
2021-10-26 15:05:49 Removing python/paddle/tensor/fft.py
2021-10-26 15:05:49 Auto-merging python/paddle/signal.py
2021-10-26 15:05:49 Removing python/paddle/nn/functional/fused_transformer.py
2021-10-26 15:05:49 Auto-merging paddle/fluid/pybind/op_function_generator.cc
2021-10-26 15:05:49 Auto-merging paddle/fluid/platform/flags.cc
2021-10-26 15:05:49 Auto-merging paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:05:49 CONFLICT (content): Merge conflict in paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_runner_test.cc
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.h
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.cc
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object_test.cc
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.h
2021-10-26 15:05:49 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.cc
2021-10-26 15:05:49 Auto-merging paddle/fluid/framework/details/CMakeLists.txt
2021-10-26 15:05:49 Auto-merging paddle/fluid/framework/CMakeLists.txt
2021-10-26 15:05:49 Automatic merge failed; fix conflicts and then commit the result.

🔹 Failed: PR-CI-iScan-C

Unknown Failed
Unknown Failed

🔹 Failed: PR-CI-Py3

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-Static-Check

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-Coverage

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-OP-benchmark

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-GpuPS

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-Model-benchmark

failed in build-docker-image job
failed in build-docker-image job

🔹 Failed: PR-CI-Mac-Python3

Unknown Failed
2021-10-26 15:13:22 + git pull upstream develop --no-edit
2021-10-26 15:13:34 From https://github.com/PaddlePaddle/Paddle
2021-10-26 15:13:34 * branch develop -> FETCH_HEAD
2021-10-26 15:13:34 * [new branch] develop -> upstream/develop
2021-10-26 15:13:35 Removing python/paddle/tensor/fft.py
2021-10-26 15:13:35 Removing python/paddle/nn/functional/fused_transformer.py
2021-10-26 15:13:35 Auto-merging paddle/fluid/pybind/op_function_generator.cc
2021-10-26 15:13:35 Auto-merging paddle/fluid/platform/flags.cc
2021-10-26 15:13:35 Auto-merging paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:13:35 CONFLICT (content): Merge conflict in paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_runner_test.cc
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.h
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.cc
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object_test.cc
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.h
2021-10-26 15:13:35 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.cc
2021-10-26 15:13:35 Auto-merging paddle/fluid/framework/details/CMakeLists.txt
2021-10-26 15:13:35 Auto-merging paddle/fluid/framework/CMakeLists.txt
2021-10-26 15:13:35 Automatic merge failed; fix conflicts and then commit the result.

🔹 Failed: PR-CI-Windows

Unknown Failed
2021-10-26 15:15:36 Switched to a new branch 'test_pr'
2021-10-26 15:15:36 C:\Users\Administrator\Downloads\workspace\4c50a85d-680c-4d41-aa19-ffca799eee4c\Paddle>if 0 NEQ 0 exit /b 1
2021-10-26 15:15:36 C:\Users\Administrator\Downloads\workspace\4c50a85d-680c-4d41-aa19-ffca799eee4c\Paddle>git merge --no-edit develop
2021-10-26 15:15:37 Removing python/paddle/tensor/fft.py
2021-10-26 15:15:37 Removing python/paddle/nn/functional/fused_transformer.py
2021-10-26 15:15:37 Auto-merging paddle/fluid/pybind/op_function_generator.cc
2021-10-26 15:15:37 Auto-merging paddle/fluid/platform/flags.cc
2021-10-26 15:15:37 Auto-merging paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:15:37 CONFLICT (content): Merge conflict in paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_runner_test.cc
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.h
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.cc
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object_test.cc
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.h
2021-10-26 15:15:37 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.cc
2021-10-26 15:15:37 Auto-merging paddle/fluid/framework/details/CMakeLists.txt
2021-10-26 15:15:37 Auto-merging paddle/fluid/framework/CMakeLists.txt
2021-10-26 15:15:37 Automatic merge failed; fix conflicts and then commit the result.
2021-10-26 15:15:37 C:\Users\Administrator\Downloads\workspace\4c50a85d-680c-4d41-aa19-ffca799eee4c\Paddle>if 1 NEQ 0 exit /b 1

🔹 Failed: PR-CI-Kunlun

Unknown Failed
2021-10-26 15:30:29 来自 https://github.com/PaddlePaddle/Paddle
2021-10-26 15:30:29 * branch develop -> FETCH_HEAD
2021-10-26 15:30:29 * [新分支] develop -> upstream/develop
2021-10-26 15:30:30 删除 python/paddle/tensor/fft.py
2021-10-26 15:30:30 自动合并 python/paddle/signal.py
2021-10-26 15:30:30 删除 python/paddle/nn/functional/fused_transformer.py
2021-10-26 15:30:30 自动合并 paddle/fluid/pybind/op_function_generator.cc
2021-10-26 15:30:30 自动合并 paddle/fluid/platform/flags.cc
2021-10-26 15:30:30 自动合并 paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:30:30 冲突(内容):合并冲突于 paddle/fluid/imperative/prepared_operator.cc
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_runner_test.cc
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_runner.h
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_runner.cc
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_compiled_object_test.cc
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_compiled_object.h
2021-10-26 15:30:30 删除 paddle/fluid/framework/paddle2cinn/cinn_compiled_object.cc
2021-10-26 15:30:30 自动合并 paddle/fluid/framework/details/CMakeLists.txt
2021-10-26 15:30:30 自动合并 paddle/fluid/framework/CMakeLists.txt
2021-10-26 15:30:30 自动合并失败,修正冲突然后提交修正的结果。

🔹 Failed: PR-CI-NPU

Unknown Failed
2021-10-26 16:16:30 + git pull upstream develop
2021-10-26 16:16:37 From https://github.com/PaddlePaddle/Paddle
2021-10-26 16:16:37 * branch develop -> FETCH_HEAD
2021-10-26 16:16:38 Removing python/paddle/tensor/fft.py
2021-10-26 16:16:38 Auto-merging python/paddle/signal.py
2021-10-26 16:16:38 Removing python/paddle/nn/functional/fused_transformer.py
2021-10-26 16:16:38 Auto-merging paddle/fluid/pybind/op_function_generator.cc
2021-10-26 16:16:38 Auto-merging paddle/fluid/platform/flags.cc
2021-10-26 16:16:38 Auto-merging paddle/fluid/imperative/prepared_operator.cc
2021-10-26 16:16:38 CONFLICT (content): Merge conflict in paddle/fluid/imperative/prepared_operator.cc
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_runner_test.cc
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.h
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_runner.cc
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object_test.cc
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.h
2021-10-26 16:16:38 Removing paddle/fluid/framework/paddle2cinn/cinn_compiled_object.cc
2021-10-26 16:16:38 Auto-merging paddle/fluid/framework/details/CMakeLists.txt
2021-10-26 16:16:38 Auto-merging paddle/fluid/framework/CMakeLists.txt
2021-10-26 16:16:38 Automatic merge failed; fix conflicts and then commit the result.

Please sign in to comment.