Skip to content

[CppExtension Unittest] Add unit test of vector<Tensor> #53040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions python/paddle/fluid/tests/cpp_extension/custom_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ paddle::Tensor custom_add(const paddle::Tensor& x, const paddle::Tensor& y) {
return x.exp() + y.exp();
}

std::vector<paddle::Tensor> custom_tensor(
const std::vector<paddle::Tensor>& inputs) {
std::vector<paddle::Tensor> out;
out.reserve(inputs.size());
for (const auto& input : inputs) {
out.push_back(input + 1.0);
}
return out;
}

paddle::Tensor nullable_tensor(bool return_none = false) {
paddle::Tensor t;
if (!return_none) {
Expand All @@ -43,6 +53,7 @@ paddle::optional<paddle::Tensor> optional_tensor(bool return_option = false) {
PYBIND11_MODULE(custom_cpp_extension, m) {
m.def("custom_add", &custom_add, "exp(x) + exp(y)");
m.def("custom_sub", &custom_sub, "exp(x) - exp(y)");
m.def("custom_tensor", &custom_tensor, "x + 1");
m.def("nullable_tensor", &nullable_tensor, "returned Tensor might be None");
m.def(
"optional_tensor", &optional_tensor, "returned Tensor might be optional");
Expand Down
15 changes: 15 additions & 0 deletions python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def tearDown(self):
def test_cpp_extension(self):
self._test_extension_function()
self._test_extension_class()
self._test_vector_tensor()
self._test_nullable_tensor()
self._test_optional_tensor()

Expand Down Expand Up @@ -106,6 +107,20 @@ def _test_extension_class(self):
atol=1e-5,
)

def _test_vector_tensor(self):
for dtype in self.dtypes:
np_inputs = [
np.random.uniform(-1, 1, [4, 8]).astype(dtype) for _ in range(3)
]
inputs = [paddle.to_tensor(np_x, dtype=dtype) for np_x in np_inputs]

out = custom_cpp_extension.custom_tensor(inputs)
target_out = [x + 1.0 for x in inputs]
for i in range(3):
np.testing.assert_allclose(
out[i].numpy(), target_out[i].numpy(), atol=1e-5
)

def _test_nullable_tensor(self):
x = custom_cpp_extension.nullable_tensor(True)
assert x is None, "Return None when input parameter return_none = True"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_cpp_extension(self):
# Extension
self._test_extension_function_plain()
self._test_extension_function_mixed()
self._test_vector_tensor()
self._test_extension_class()
self._test_nullable_tensor()
self._test_optional_tensor()
Expand Down Expand Up @@ -215,6 +216,22 @@ def _test_extension_class(self):
atol=1e-5,
)

def _test_vector_tensor(self):
import custom_cpp_extension

for dtype in self.dtypes:
np_inputs = [
np.random.uniform(-1, 1, [4, 8]).astype(dtype) for _ in range(3)
]
inputs = [paddle.to_tensor(np_x, dtype=dtype) for np_x in np_inputs]

out = custom_cpp_extension.custom_tensor(inputs)
target_out = [x + 1 for x in inputs]
for i in range(3):
np.testing.assert_allclose(
out[i].numpy(), target_out[i].numpy(), atol=1e-5
)

def _test_nullable_tensor(self):
import custom_cpp_extension

Expand Down