Skip to content
Open
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
14 changes: 13 additions & 1 deletion paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,8 @@ static PyObject* tensor_method_set_underline_tensor(TensorObject* self,
static_cast<phi::DenseTensor*>(self->tensor.impl().get());
if (self->tensor.has_allocation() && self->tensor.initialized() &&
(!dst_tensor->meta().is_contiguous() ||
!src_tensor->meta().is_contiguous())) {
!src_tensor->meta().is_contiguous()) &&
dst_tensor->place().GetType() == src_tensor->place().GetType()) {
VLOG(8) << "set_tensor() method , src or dst tensor is not contiguous ";
if (!FLAGS_use_stride_kernel) {
PADDLE_THROW(common::errors::Fatal(
Expand All @@ -1451,6 +1452,17 @@ static PyObject* tensor_method_set_underline_tensor(TensorObject* self,
dst_tensor);
}));
} else {
if (!dst_tensor->meta().is_contiguous()) {
PADDLE_THROW(common::errors::Fatal(
"dst_tensor is not contiguous and src_tesnor has different place "
"with dst_tensor, so Strided kernel "
"can't be called, please change src_tensor'place as same as "
"dst_tensor'place or change dst_tensor to be contiguous"));
} else if (!src_tensor->meta().is_contiguous()) {
VLOG(6) << "src_tensor is not contiguous, so dst_tensor will be not "
"contiguous after set_value ";
}

if (dst_tensor->place().GetType() != phi::AllocationType::UNDEFINED) {
framework::TensorCopy(*src_tensor, dst_tensor->place(), dst_tensor);
} else if (src_tensor->place().GetType() !=
Expand Down
5 changes: 3 additions & 2 deletions paddle/fluid/pybind/slice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ static paddle::Tensor getValueForBoolTensor(const paddle::Tensor& tensor,
}

auto bool_2_idx = nonzero_ad_func(bool_index);
if (FLAGS_use_stride_kernel) {
if (FLAGS_use_stride_kernel && self_tensor.is_contiguous()) {
std::vector<paddle::Tensor> indices =
PrepareIndices(tensor, bool_2_idx, bool_index);
for (int i = 0; i < pos_of_new_dim; ++i) {
Expand Down Expand Up @@ -1302,7 +1302,8 @@ static void ApplyGetitem(const int index_size,
}
}

if (FLAGS_use_stride_kernel && !has_empty_index) {
if (FLAGS_use_stride_kernel && !has_empty_index &&
self_tensor->is_contiguous()) {
const phi::distributed::ProcessMesh* mesh = nullptr;
if (InputsContainDistTensor(
&mesh, *self_tensor, *transed_tensor, *transed_index)) {
Expand Down
10 changes: 5 additions & 5 deletions paddle/phi/kernels/funcs/stride_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ static inline void CopyStride(

coalesce_dimensions<N>(ndim, strides_array, &stride_size, desired_shape);

int num = 1;
int64_t num = 1;
for (size_t i = 0; i < desired_shape->size(); i++) {
num *= (*desired_shape)[i];
}
Expand Down Expand Up @@ -385,7 +385,7 @@ static inline void IndexPutStride(

coalesce_dimensions<N>(ndim, strides_array, &stride_size, desired_shape);

int num = 1;
int64_t num = 1;
for (size_t i = 0; i < desired_shape->size(); i++) {
num *= (*desired_shape)[i];
}
Expand Down Expand Up @@ -444,7 +444,7 @@ static inline void IndexGetStride(

coalesce_dimensions<N>(ndim, strides_array, &stride_size, desired_shape);

int num = 1;
int64_t num = 1;
for (size_t i = 0; i < desired_shape->size(); i++) {
num *= (*desired_shape)[i];
}
Expand Down Expand Up @@ -539,8 +539,8 @@ static inline void ScatterAddStride(

coalesce_dimensions<N>(ndim, strides_array, &stride_size, desired_shape);

int num = 1;
for (int i = 0; i < desired_shape->size(); i++) {
int64_t num = 1;
for (size_t i = 0; i < desired_shape->size(); i++) {
num *= (*desired_shape)[i];
}
*numel = num;
Expand Down
18 changes: 18 additions & 0 deletions test/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,24 @@ def test_indexing_is_boolean_false(self):

np.testing.assert_allclose(y.numpy(), np_res)

def test_input_strided_tensor(self):
base = paddle.to_tensor(
[5.0, 5.0, 6.0, 5.0, 5.0, 6.0], dtype=paddle.float64
)
foo_strided = paddle.as_strided(base, shape=(2, 1), stride=(2, 1))

base2 = paddle.to_tensor(
[0, 0, 1, 0, 1, 0, 0, 5, 5, 5, 5], dtype=paddle.int64
)
atype = paddle.as_strided(base2, shape=(2, 3), stride=(4, 1))

result = foo_strided[atype]
expected_result = paddle.to_tensor(
[[[5.0], [5.0], [6.0]], [[6.0], [5.0], [5.0]]], dtype=paddle.float64
)

np.testing.assert_allclose(result.numpy(), expected_result.numpy())


class TestMultipleIndexing(TestGetitemInDygraph):
def test_indexing_with_all_possible_start_end_step_dygraph(self):
Expand Down
6 changes: 5 additions & 1 deletion test/legacy_test/test_set_value_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
import unittest

import numpy as np
from op_test import OpTest, convert_float_to_uint16, get_devices
from op_test import (
OpTest,
convert_float_to_uint16,
get_devices,
)

import paddle
from paddle.base import core
Expand Down
Loading