Skip to content
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

[Zero Dim] hack process Tensor.numpy() from 0D to 1D to avoid much incompatible #51586

Merged
merged 1 commit into from
Mar 14, 2023
Merged
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
21 changes: 17 additions & 4 deletions paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,24 @@ static PyObject* tensor_method_numpy(TensorObject* self,
auto sizeof_dtype = phi::SizeOf(self->tensor.type());
Py_intptr_t py_dims[paddle::framework::DDim::kMaxRank];
Py_intptr_t py_strides[paddle::framework::DDim::kMaxRank];
size_t py_rank = tensor_dims.size();
size_t numel = 1;
for (int i = tensor_dims.size() - 1; i >= 0; --i) {
py_dims[i] = static_cast<size_t>(tensor_dims[i]);
py_strides[i] = sizeof_dtype * numel;
numel *= py_dims[i];
if (py_rank == 0) {
// 0D Tensor hack process to 1D numpy, will remove in future
VLOG(0) << "Warning:: 0D Tensor cannot be used as Tensor.numpy()[0], Now "
"0D will be changed to 1D numpy to avoid this problem, but it's "
"not correct and will be removed in future. Please change "
"'Tensor.numpy()[0]' to 'float(Tensor)' or "
"'Tensor.numpy().item()' as soon as possible.";
py_rank = 1;
py_dims[0] = 1;
py_strides[0] = sizeof_dtype * numel;
} else {
for (int i = tensor_dims.size() - 1; i >= 0; --i) {
py_dims[i] = static_cast<size_t>(tensor_dims[i]);
py_strides[i] = sizeof_dtype * numel;
numel *= py_dims[i];
}
}

PyObject* array = api.PyArray_NewFromDescr_(
Expand Down