Skip to content

Commit

Permalink
[cherry-pick 2.4] remove incubate of all paddle sparse api (#47183)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 authored Oct 20, 2022
1 parent 396427a commit 50d4fa5
Show file tree
Hide file tree
Showing 35 changed files with 312 additions and 256 deletions.
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import paddle.vision # noqa: F401
import paddle.audio # noqa: F401
import paddle.geometric # noqa: F401
import paddle.sparse

from .tensor.attribute import is_complex # noqa: F401
from .tensor.attribute import is_integer # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/dygraph/varbase_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def values(self):
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
values = [1, 2, 3, 4, 5]
dense_shape = [3, 4]
sparse_x = paddle.incubate.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int32'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
sparse_x = paddle.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int32'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
print(sparse_x.values())
#[1, 2, 3, 4, 5]
"""
Expand All @@ -944,7 +944,7 @@ def to_dense(self):
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
values = [1, 2, 3, 4, 5]
dense_shape = [3, 4]
sparse_x = paddle.incubate.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int64'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
sparse_x = paddle.sparse.sparse_coo_tensor(paddle.to_tensor(indices, dtype='int64'), paddle.to_tensor(values, dtype='float32'), shape=dense_shape)
dense_x = sparse_x.to_dense()
#[[0., 1., 0., 2.],
# [0., 0., 3., 0.],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def check_result(self, input_shape, x_shape, y_shape, format):
sp_x.stop_gradient = False
sp_y = origin_y.detach()
sp_y.stop_gradient = False
sp_out = paddle.incubate.sparse.addmm(sp_input, sp_x, sp_y, 3.0, 2.0)
sp_out = paddle.sparse.addmm(sp_input, sp_x, sp_y, 3.0, 2.0)

np.testing.assert_allclose(sp_out.numpy(),
dense_out.numpy(),
Expand Down
65 changes: 36 additions & 29 deletions python/paddle/fluid/tests/unittests/test_sparse_conv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from paddle import _C_ops, _legacy_C_ops
from paddle.fluid import core
from paddle.fluid.framework import _test_eager_guard
import paddle.incubate.sparse as sparse
import paddle.sparse as sparse


class TestSparseConv(unittest.TestCase):
Expand All @@ -44,17 +44,17 @@ def test_conv3d(self):
correct_out_values = [[5], [11]]
sparse_input = core.eager.sparse_coo_tensor(indices, values,
dense_shape, False)
out = paddle.incubate.sparse.nn.functional.conv3d(
sparse_input,
dense_kernel,
bias=paddle.to_tensor(bias, dtype='float32'),
stride=strides,
padding=paddings,
dilation=dilations,
groups=1,
data_format="NDHWC")
out = paddle.sparse.nn.functional.conv3d(sparse_input,
dense_kernel,
bias=paddle.to_tensor(
bias, dtype='float32'),
stride=strides,
padding=paddings,
dilation=dilations,
groups=1,
data_format="NDHWC")
out.backward(out)
out = paddle.incubate.sparse.coalesce(out)
out = paddle.sparse.coalesce(out)
assert np.array_equal(correct_out_values, out.values().numpy())

def test_subm_conv3d(self):
Expand All @@ -64,11 +64,14 @@ def test_subm_conv3d(self):
indices = paddle.to_tensor(indices, dtype='int32')
values = paddle.to_tensor(values, dtype='float32')
dense_shape = [1, 1, 3, 4, 1]
sparse_x = paddle.incubate.sparse.sparse_coo_tensor(
indices, values, dense_shape, stop_gradient=True)
sparse_x = paddle.sparse.sparse_coo_tensor(indices,
values,
dense_shape,
stop_gradient=True)
weight = paddle.randn((1, 3, 3, 1, 1), dtype='float32')
y = paddle.incubate.sparse.nn.functional.subm_conv3d(
sparse_x, weight, key='subm_conv')
y = paddle.sparse.nn.functional.subm_conv3d(sparse_x,
weight,
key='subm_conv')
assert np.array_equal(sparse_x.indices().numpy(),
y.indices().numpy())

Expand All @@ -82,17 +85,20 @@ def test_Conv3D(self):
values = paddle.to_tensor(values, dtype='float32')
dense_shape = [1, 1, 3, 4, 1]
correct_out_values = [[4], [10]]
sparse_input = paddle.incubate.sparse.sparse_coo_tensor(
sparse_input = paddle.sparse.sparse_coo_tensor(
indices, values, dense_shape, False)

sparse_conv3d = paddle.incubate.sparse.nn.Conv3D(
1, 1, (1, 3, 3), data_format='NDHWC')
sparse_conv3d = paddle.sparse.nn.Conv3D(1,
1, (1, 3, 3),
data_format='NDHWC')
sparse_out = sparse_conv3d(sparse_input)
#test errors
with self.assertRaises(ValueError):
#Currently, only support data_format='NDHWC'
conv3d = paddle.incubate.sparse.nn.SubmConv3D(
1, 1, (1, 3, 3), data_format='NCDHW', key='subm_conv')
conv3d = paddle.sparse.nn.SubmConv3D(1,
1, (1, 3, 3),
data_format='NCDHW',
key='subm_conv')

def test_SubmConv3D(self):
with _test_eager_guard():
Expand All @@ -102,11 +108,13 @@ def test_SubmConv3D(self):
values = paddle.to_tensor(values, dtype='float32')
dense_shape = [1, 1, 3, 4, 1]
correct_out_values = [[4], [10]]
sparse_input = paddle.incubate.sparse.sparse_coo_tensor(
sparse_input = paddle.sparse.sparse_coo_tensor(
indices, values, dense_shape, False)

subm_conv3d = paddle.incubate.sparse.nn.SubmConv3D(
1, 1, (1, 3, 3), data_format='NDHWC', key='subm_conv')
subm_conv3d = paddle.sparse.nn.SubmConv3D(1,
1, (1, 3, 3),
data_format='NDHWC',
key='subm_conv')
# test extra_repr
print(subm_conv3d.extra_repr())

Expand All @@ -117,8 +125,10 @@ def test_SubmConv3D(self):
#test errors
with self.assertRaises(ValueError):
#Currently, only support data_format='NDHWC'
conv3d = paddle.incubate.sparse.nn.SubmConv3D(
1, 1, (1, 3, 3), data_format='NCDHW', key='subm_conv')
conv3d = paddle.sparse.nn.SubmConv3D(1,
1, (1, 3, 3),
data_format='NCDHW',
key='subm_conv')

def test_Conv3D_bias(self):
with _test_eager_guard():
Expand All @@ -128,10 +138,7 @@ def test_Conv3D_bias(self):
sp_x = x.to_sparse_coo(4)
conv3d = paddle.nn.Conv3D(3, 2, 3, data_format='NDHWC')

sp_conv3d = paddle.incubate.sparse.nn.Conv3D(3,
2,
3,
data_format='NDHWC')
sp_conv3d = paddle.sparse.nn.Conv3D(3, 2, 3, data_format='NDHWC')
sp_conv3d.weight.set_value(
paddle.to_tensor(conv3d.weight.numpy().transpose(2, 3, 4, 1,
0)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

import numpy as np
import paddle
import paddle.incubate.sparse as sparse
import paddle.sparse as sparse

op_list = [__add__, __sub__, __mul__, __truediv__]


def get_actual_res(x, y, op):
if op == __add__:
res = paddle.incubate.sparse.add(x, y)
res = paddle.sparse.add(x, y)
elif op == __sub__:
res = paddle.incubate.sparse.subtract(x, y)
res = paddle.sparse.subtract(x, y)
elif op == __mul__:
res = paddle.incubate.sparse.multiply(x, y)
res = paddle.sparse.multiply(x, y)
elif op == __truediv__:
res = paddle.incubate.sparse.divide(x, y)
res = paddle.sparse.divide(x, y)
else:
raise ValueError("unsupported op")
return res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_dygraph(self):
output = paddle.matmul(softmax, value)
output.backward()

output_sp = paddle.incubate.sparse.nn.functional.attention(
output_sp = paddle.sparse.nn.functional.attention(
query_sp, key_sp, value_sp, sp_mask, kp_mask, attn_mask)
output_sp.backward()
else:
Expand All @@ -103,7 +103,7 @@ def test_dygraph(self):
output = paddle.matmul(softmax, value)
output.backward()

output_sp = paddle.incubate.sparse.nn.functional.attention(
output_sp = paddle.sparse.nn.functional.attention(
query_sp, key_sp, value_sp, sp_mask)
output_sp.backward()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import unittest

import paddle
from paddle.incubate.sparse.binary import is_same_shape
from paddle.sparse.binary import is_same_shape


class TestSparseIsSameShapeAPI(unittest.TestCase):
"""
test paddle.incubate.sparse.is_same_shape
test paddle.sparse.is_same_shape
"""

def setUp(self):
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/fluid/tests/unittests/test_sparse_matmul_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def check_result(self, x_shape, y_shape, format):
sp_x.stop_gradient = False
sp_y = origin_y.detach()
sp_y.stop_gradient = False
sp_out = paddle.incubate.sparse.matmul(sp_x, sp_y)
sp_out = paddle.sparse.matmul(sp_x, sp_y)

np.testing.assert_allclose(sp_out.numpy(),
dense_out.numpy(),
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_masked_matmul_2d(self):
x = paddle.to_tensor(np_x, stop_gradient=False)
y = paddle.to_tensor(np_y, stop_gradient=False)
mask = paddle.to_tensor(np.ones([10, 6]) * np_mask).to_sparse_csr()
out = paddle.incubate.sparse.masked_matmul(x, y, mask)
out = paddle.sparse.masked_matmul(x, y, mask)

np.testing.assert_allclose(np_out.indptr,
out.crows().numpy(),
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_masked_matmul_3d(self):
sp_x.stop_gradient = False
sp_y = origin_y.detach()
sp_y.stop_gradient = False
sp_out = paddle.incubate.sparse.matmul(sp_x, sp_y)
sp_out = paddle.sparse.matmul(sp_x, sp_y)
sp_out.backward()

np.testing.assert_allclose(sp_out.numpy(),
Expand Down
11 changes: 5 additions & 6 deletions python/paddle/fluid/tests/unittests/test_sparse_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import unittest
import numpy as np
import paddle
from paddle.incubate import sparse
from paddle.incubate.sparse import nn
from paddle.sparse import nn
from paddle.fluid.framework import _test_eager_guard


Expand All @@ -26,10 +25,10 @@ def sparse(self, sp_x):
indentity = sp_x
out = nn.functional.relu(sp_x)
values = out.values() + indentity.values()
out = sparse.sparse_coo_tensor(out.indices(),
values,
shape=out.shape,
stop_gradient=out.stop_gradient)
out = paddle.sparse.sparse_coo_tensor(out.indices(),
values,
shape=out.shape,
stop_gradient=out.stop_gradient)
return out

def dense(self, x):
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_sparse_mv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_mv(self):
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.incubate.sparse.mv(sp_x, sp_vec)
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(sp_out.numpy(),
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_mv(self):
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.incubate.sparse.mv(sp_x, sp_vec)
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(sp_out.numpy(),
Expand Down
10 changes: 5 additions & 5 deletions python/paddle/fluid/tests/unittests/test_sparse_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import unittest
import numpy as np
import paddle
from paddle.incubate.sparse import nn
from paddle.sparse import nn
import paddle.fluid as fluid
import copy

Expand All @@ -40,7 +40,7 @@ def test(self):
dense_x2 = copy.deepcopy(dense_x)
dense_x2.stop_gradient = False
sparse_x = dense_x2.to_sparse_coo(sparse_dim)
sparse_batch_norm = paddle.incubate.sparse.nn.BatchNorm(channels)
sparse_batch_norm = paddle.sparse.nn.BatchNorm(channels)
# set same params
sparse_batch_norm._mean.set_value(batch_norm._mean)
sparse_batch_norm._variance.set_value(batch_norm._variance)
Expand All @@ -66,8 +66,8 @@ def test_error_layout(self):
shape = [2, 3, 6, 6, 3]
x = paddle.randn(shape)
sparse_x = x.to_sparse_coo(4)
sparse_batch_norm = paddle.incubate.sparse.nn.BatchNorm(
3, data_format='NCDHW')
sparse_batch_norm = paddle.sparse.nn.BatchNorm(3,
data_format='NCDHW')
sparse_batch_norm(sparse_x)

def test2(self):
Expand All @@ -76,7 +76,7 @@ def test2(self):
x_data = paddle.randn((1, 6, 6, 6, channels)).astype('float32')
dense_x = paddle.to_tensor(x_data)
sparse_x = dense_x.to_sparse_coo(4)
batch_norm = paddle.incubate.sparse.nn.BatchNorm(channels)
batch_norm = paddle.sparse.nn.BatchNorm(channels)
batch_norm_out = batch_norm(sparse_x)
dense_bn = paddle.nn.BatchNorm1D(channels)
dense_x = dense_x.reshape((-1, dense_x.shape[-1]))
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/fluid/tests/unittests/test_sparse_pooling_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test(self):
self.setUp()
self.dense_x.stop_gradient = False
sparse_x = self.dense_x.to_sparse_coo(4)
sparse_out = paddle.incubate.sparse.nn.functional.max_pool3d(
sparse_out = paddle.sparse.nn.functional.max_pool3d(
sparse_x,
self.kernel_sizes,
stride=self.strides,
Expand Down Expand Up @@ -109,8 +109,8 @@ def test(self):
with _test_eager_guard():
dense_x = paddle.randn((2, 3, 6, 6, 3))
sparse_x = dense_x.to_sparse_coo(4)
max_pool3d = paddle.incubate.sparse.nn.MaxPool3D(
kernel_size=3, data_format='NDHWC')
max_pool3d = paddle.sparse.nn.MaxPool3D(kernel_size=3,
data_format='NDHWC')
out = max_pool3d(sparse_x)
out = out.to_dense()

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/test_sparse_reshape_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class TestReshape(unittest.TestCase):
"""
Test the API paddle.incubate.sparse.reshape on some sparse tensors.
Test the API paddle.sparse.reshape on some sparse tensors.
x: sparse, out: sparse
"""

Expand All @@ -31,7 +31,7 @@ def check_result(self, x_shape, new_shape, format):
Transform a sparse tensor with shape "x_shape" to
a sparse tensor with shape "new_shape".
Compare the output of paddle.reshape and the output of
paddle.incubate.sparse.reshape.
paddle.sparse.reshape.
"""
mask = np.random.randint(0, 2, x_shape)
np_x = np.random.randint(-100, 100, x_shape) * mask
Expand All @@ -49,7 +49,7 @@ def check_result(self, x_shape, new_shape, format):
sp_x = paddle.to_tensor(np_x,
place=paddle.CPUPlace()).to_sparse_csr()
sp_x.stop_gradient = False
sp_out = paddle.incubate.sparse.reshape(sp_x, new_shape)
sp_out = paddle.sparse.reshape(sp_x, new_shape)

np.testing.assert_allclose(sp_out.to_dense().numpy(),
dense_out.numpy(),
Expand All @@ -75,7 +75,7 @@ def check_result(self, x_shape, new_shape, format):
sp_x = paddle.to_tensor(
np_x, place=paddle.CUDAPlace(0)).to_sparse_csr()
sp_x.stop_gradient = False
sp_out = paddle.incubate.sparse.reshape(sp_x, new_shape)
sp_out = paddle.sparse.reshape(sp_x, new_shape)

np.testing.assert_allclose(sp_out.to_dense().numpy(),
dense_out.numpy(),
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_sparse_softmax_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_softmax2d(self):
np_out = np.concatenate([np_out, x_exp / x_exp_sum])

csr = paddle.to_tensor(np_x, stop_gradient=False).to_sparse_csr()
m = paddle.incubate.sparse.nn.Softmax()
m = paddle.sparse.nn.Softmax()
out = m(csr)
np.testing.assert_allclose(out.crows().numpy(),
np_csr.indptr,
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_softmax3d(self):
np_out = np.concatenate([np_out, x_exp / x_exp_sum])

csr = paddle.to_tensor(np_x, stop_gradient=False).to_sparse_csr()
m = paddle.incubate.sparse.nn.Softmax()
m = paddle.sparse.nn.Softmax()
out = m(csr)
np.testing.assert_allclose(out.values().numpy(), np_out, rtol=1e-05)

Expand Down
Loading

0 comments on commit 50d4fa5

Please sign in to comment.