MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / ViewShapeStridedKernel

Function ViewShapeStridedKernel

paddle/phi/kernels/stride/view_kernel.cc:25–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23
24template <typename Context>
25void ViewShapeStridedKernel(const Context& dev_ctx,
26 const DenseTensor& input,
27 const std::vector<int64_t>& dims,
28 DenseTensor* out) {
29 if (!FLAGS_use_stride_kernel) {
30 PADDLE_THROW(common::errors::Fatal(
31 "FLAGS_use_stride_kernel is closed. Strided kernel "
32 "be called, something wrong has happened!"));
33 }
34 // infer dims
35 auto infer_dim = -1;
36 int64_t new_size = 1;
37 int64_t numel = input.numel();
38 std::vector<int64_t> dims_copy = dims;
39 for (int dim = 0, ndim = dims_copy.size(); dim < ndim; ++dim) {
40 if (dims_copy[dim] == -1) {
41 if (infer_dim >= 0) {
42 PADDLE_THROW(
43 common::errors::Fatal("Only one dimension can be inferred"));
44 }
45 infer_dim = dim;
46 } else if (dims_copy[dim] >= 0) {
47 new_size *= dims_copy[dim];
48 } else {
49 PADDLE_THROW(common::errors::OutOfRange("Tensor idx is out of range"));
50 }
51 }
52 if (numel != 0) {
53 PADDLE_ENFORCE_NE(
54 new_size,
55 0,
56 common::errors::InvalidArgument(
57 "cannot reshape tensor with %d elements into shape %s because "
58 "the target shape has 0 elements",
59 numel,
60 common::make_ddim(dims_copy)));
61 }
62 if (infer_dim >= 0 && new_size > 0 && numel % new_size == 0) {
63 dims_copy[infer_dim] = numel / new_size;
64 }
65
66 DDim new_dims = DDim(dims_copy.data(), static_cast<int>(dims_copy.size()));
67 DDim stride;
68 if (ReshapeStride(input.dims(), input.strides(), new_dims, stride)) {
69 auto meta = input.meta();
70 meta.dims = new_dims;
71 meta.strides = stride;
72 meta.offset = input.offset();
73 out->set_meta(meta);
74 out->ResetHolder(input.Holder());
75 out->ShareInplaceVersionCounterWith(input);
76 } else {
77 PADDLE_THROW(common::errors::InvalidArgument(
78 "The Tensor can not be viewed, please call reshape."));
79 }
80}
81
82template <typename Context>

Callers

nothing calls this directly

Calls 13

make_ddimFunction · 0.85
ReshapeStrideFunction · 0.85
metaMethod · 0.80
ResetHolderMethod · 0.80
HolderMethod · 0.80
DDimClass · 0.50
numelMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45
dimsMethod · 0.45
stridesMethod · 0.45
offsetMethod · 0.45

Tested by

no test coverage detected