Skip to content

Commit

Permalink
[Zero-Dim] support ReshapeTransform/nll_loss/matmul support 0D (Paddl…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 committed May 25, 2023
1 parent 8f455ca commit 334bffd
Show file tree
Hide file tree
Showing 22 changed files with 309 additions and 238 deletions.
4 changes: 0 additions & 4 deletions paddle/fluid/operators/matmul_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,6 @@ class MatMulOp : public framework::OperatorWithKernel {
dim_out.resize(dim_out.size() - 1);
}

if (dim_out.empty()) {
dim_out = {1};
}

phi::DDim ddim_out = phi::make_ddim(dim_out);

context->SetOutputDim("Out", ddim_out);
Expand Down
3 changes: 0 additions & 3 deletions paddle/fluid/operators/matmul_v2_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ void MatMulV2Op::InferShape(framework::InferShapeContext* ctx) const {
if (!y_broadcasted) {
new_dims.push_back(N);
}
if (x_broadcasted && y_broadcasted) {
new_dims.push_back(1);
}

ctx->SetOutputDim("Out", phi::make_ddim(new_dims));
ctx->ShareLoD("X", "Out");
Expand Down
5 changes: 3 additions & 2 deletions paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ void ExecuteMatMulV1(const ExecutionContext &ctx,
matmul_p->execute(astream, matmul_args);
astream.wait();

out->set_mem_desc(
dst_memory_p->get_desc().reshape(vectorize<int64_t>(out->dims())));
auto reshape_dims = out->dims().size() != 0 ? vectorize(out->dims())
: std::vector<int64_t>{1};
out->set_mem_desc(dst_memory_p->get_desc().reshape(reshape_dims));
}

template <typename T>
Expand Down
10 changes: 6 additions & 4 deletions paddle/phi/backends/onednn/matmul_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ inline void ExecuteMul(const OneDNNContext& dev_ctx,
// This kernel is flattening dims so then we need to unflattened version
// that should be set in out reshape require plain layout, but
// MatmulV2MKLDNNHanlder enforces one so it should work
out->set_mem_desc(
dst_memory_p->get_desc().reshape(vectorize<int64_t>(out->dims())));
auto reshape_dims = out->dims().size() != 0 ? vectorize(out->dims())
: std::vector<int64_t>{1};
out->set_mem_desc(dst_memory_p->get_desc().reshape(reshape_dims));
}

template <typename T, typename T_out>
Expand Down Expand Up @@ -177,8 +178,9 @@ inline void ExecuteMatmul(const OneDNNContext& dev_ctx,
matmul_p->execute(astream, matmul_args);
astream.wait();

out->set_mem_desc(
dst_memory_p->get_desc().reshape(vectorize<int64_t>(out->dims())));
auto reshape_dims = out->dims().size() != 0 ? vectorize(out->dims())
: std::vector<int64_t>{1};
out->set_mem_desc(dst_memory_p->get_desc().reshape(reshape_dims));
}

} // namespace funcs
Expand Down
20 changes: 10 additions & 10 deletions paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -822,22 +822,22 @@ void NllLossGradInferMeta(const MetaTensor& x,
if (check) {
auto batch_size = x_dims[0];
if (x_dims.size() == 2) {
PADDLE_ENFORCE_EQ(dout_dims.size(),
1,
phi::errors::InvalidArgument(
"The dimensions of Input(Out@Grad) must be 1"));
if (reduction == "none") {
PADDLE_ENFORCE_EQ(dout_dims.size(),
1,
phi::errors::InvalidArgument(
"The dimensions of Input(Out@Grad) must be 1"));
PADDLE_ENFORCE_EQ(
dout_dims[0],
batch_size,
phi::errors::InvalidArgument(
"The unreduced size ofInput(Out@Grad) must be the "
"same as batch_size."));
} else {
PADDLE_ENFORCE_EQ(dout_dims[0],
1,
PADDLE_ENFORCE_EQ(dout_dims.size(),
0,
phi::errors::InvalidArgument(
"The reduced size of Input(Out@Grad) must be 1"));
"The dimensions of Input(Out@Grad) must be 0"));
}
} else if (x_dims.size() == 4) {
if (reduction == "none") {
Expand All @@ -855,10 +855,10 @@ void NllLossGradInferMeta(const MetaTensor& x,
"The dimensions of Input(Out@Grad) must be match "
"to Input(Label) dimensions."));
} else {
PADDLE_ENFORCE_EQ(dout_dims[0],
1,
PADDLE_ENFORCE_EQ(dout_dims.size(),
0,
phi::errors::InvalidArgument(
"The reduced size of Input(Out@Grad) must be 1"));
"The dimensions of Input(Out@Grad) must be 0"));
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2056,9 +2056,6 @@ void MatmulInferMeta(const MetaTensor& x,
if (!y_broadcasted) {
new_dims.push_back(N);
}
if (x_broadcasted && y_broadcasted) {
new_dims.push_back(1);
}

auto ddim_out = phi::make_ddim(new_dims);

Expand Down
6 changes: 3 additions & 3 deletions paddle/phi/infermeta/ternary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ void NllLossRawInferMeta(const MetaTensor& input,
if (reduction == "none") {
out->set_dims({x_dims[0]});
} else {
out->set_dims({1});
out->set_dims(phi::make_ddim({}));
}
} else if (x_dims.size() == 4) {
PADDLE_ENFORCE_EQ(label_dims.size(),
Expand All @@ -841,10 +841,10 @@ void NllLossRawInferMeta(const MetaTensor& input,
if (reduction == "none") {
out->set_dims({x_dims[0], x_dims[2], x_dims[3]});
} else {
out->set_dims({1});
out->set_dims(phi::make_ddim({}));
}
}
total_weight->set_dims({1});
total_weight->set_dims(phi::make_ddim({}));
out->set_dtype(input.dtype());
total_weight->set_dtype(input.dtype());
}
Expand Down
4 changes: 2 additions & 2 deletions paddle/phi/kernels/impl/matmul_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void MatMulFunctionImplWithBlas(
M,
N));
VLOG(3) << "MatMul's case 1";
Out->Resize({1});
Out->Resize(phi::make_ddim({}));
dev_ctx.template Alloc<T>(Out);
blas.GEMM(CblasNoTrans,
CblasTrans,
Expand Down Expand Up @@ -516,7 +516,7 @@ void MatMulFunctionImplWithCublasLt(
N));

// MatMul's case 0 => vector * vector
Out->Resize({1});
Out->Resize(phi::make_ddim({}));
dev_ctx.template Alloc<T>(Out);
VLOG(3) << "MatMul with blaslt case 1";
blaslt::Run(dev_ctx,
Expand Down
3 changes: 1 addition & 2 deletions paddle/phi/kernels/nll_loss_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ void NllLossKernel(const Context& dev_ctx,
const std::string& reduction,
DenseTensor* out) {
DenseTensor total_weight;
total_weight.set_meta(
DenseTensorMeta(phi::CppTypeToDataType<T>::Type(), {1}));
total_weight.set_meta(DenseTensorMeta(phi::CppTypeToDataType<T>::Type(), {}));
dev_ctx.template Alloc<T>(total_weight);
NllLossRawKernel(dev_ctx,
input,
Expand Down
7 changes: 3 additions & 4 deletions python/paddle/distribution/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ class ReshapeTransform(Transform):
# [[[1., 1., 1.],
# [1., 1., 1.]]])
print(reshape_transform.forward_log_det_jacobian(x))
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [0.])
# Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# 0.)
"""
_type = Type.BIJECTION

Expand Down Expand Up @@ -945,8 +945,7 @@ def _inverse_shape(self, shape):
)

def _forward_log_det_jacobian(self, x):
# TODO(zhouwei): should not set shape to [1], which is []
shape = x.shape[: x.dim() - len(self._in_event_shape)] or [1]
shape = x.shape[: x.dim() - len(self._in_event_shape)]
return paddle.zeros(shape, dtype=x.dtype)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ def test_zerodim(self, input, expected):

self.assertEqual(out.shape, [1, 1])
self.assertEqual(reshape.inverse(out).shape, [])
# self.assertEqual(reshape.forward_log_det_jacobian(x).shape, [])
# self.assertEqual(reshape.inverse_log_det_jacobian(out).shape, [])
self.assertEqual(reshape.forward_log_det_jacobian(x).shape, [])
self.assertEqual(reshape.inverse_log_det_jacobian(out).shape, [])
self.assertEqual(reshape.forward_shape(x.shape), (1, 1))
self.assertEqual(reshape.inverse_shape(out.shape), ())

Expand Down
13 changes: 1 addition & 12 deletions python/paddle/fluid/tests/unittests/test_matmul_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def reference_matmul(X, Y, transpose_X=False, transpose_Y=False):
Y = np.transpose(Y, tuple(dim))

Out = np.matmul(X, Y)
if not Out.shape:
# We do not support 0-dimensional Tensors (scalars). So where
# np.matmul outputs a scalar, we must convert to a Tensor of
# shape (1, ) instead.
# Everywhere else, we are compatible with np.matmul.
Out = np.array([Out], dtype="float32")
return Out


Expand Down Expand Up @@ -167,19 +161,14 @@ def test_out(self):
with fluid.program_guard(fluid.Program()):
x = paddle.static.data(name="x", shape=[2], dtype="float64")
y = paddle.static.data(name='y', shape=[2], dtype='float64')
res = paddle.static.data(
name="output", shape=[1], dtype="float64"
)
result = paddle.mm(x, y)
exe = fluid.Executor(fluid.CPUPlace())
data1 = np.random.rand(2)
data2 = np.random.rand(2)
np_res = exe.run(
feed={'x': data1, 'y': data2}, fetch_list=[result]
)
expected_result = np.matmul(
data1.reshape(1, 2), data2.reshape(2, 1)
)
expected_result = np.matmul(data1, data2)

np.testing.assert_allclose(
np_res,
Expand Down
12 changes: 0 additions & 12 deletions python/paddle/fluid/tests/unittests/test_matmul_op_with_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ def reference_matmul_mul_head(
Y = transpose_mat(Y)

Out = matmul_head(X, Y, head_number)
if not Out.shape:
# We do not support 0-dimensional Tensors (scalars). So where
# np.matmul outputs a scalar, we must convert to a Tensor of
# shape (1, ) instead.
# Everywhere else, we are compatible with np.matmul.
Out = np.array([Out], dtype="float32")
return Out


Expand Down Expand Up @@ -196,12 +190,6 @@ def reference_matmul_mul_head2(
Y = transpose_mat(Y)

Out = matmul_head2(X, Y, head_number)
if not Out.shape:
# We do not support 0-dimensional Tensors (scalars). So where
# np.matmul outputs a scalar, we must convert to a Tensor of
# shape (1, ) instead.
# Everywhere else, we are compatible with np.matmul.
Out = np.array([Out], dtype="float32")
return Out


Expand Down
6 changes: 0 additions & 6 deletions python/paddle/fluid/tests/unittests/test_matmul_v2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ def reference_matmul(X, Y, transpose_X=False, transpose_Y=False):
Y = np.transpose(Y, tuple(dim))

Out = np.matmul(X, Y)
if not Out.shape:
# We do not support 0-dimensional Tensors (scalars). So where
# np.matmul outputs a scalar, we must convert to a Tensor of
# shape (1, ) instead.
# Everywhere else, we are compatible with np.matmul.
Out = np.array([Out], dtype="float64")
return Out


Expand Down
Loading

0 comments on commit 334bffd

Please sign in to comment.