Skip to content

Commit

Permalink
[Sparse] change paddle.incubate.sparse to paddle.sparse (PaddlePaddle…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 authored Oct 20, 2022
1 parent f61f9e7 commit 1c8ef38
Show file tree
Hide file tree
Showing 35 changed files with 273 additions and 246 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 # noqa: F401

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 @@ -908,7 +908,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 @@ -933,7 +933,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 @@ -62,7 +62,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 @@ -17,7 +17,7 @@
import paddle
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 @@ -42,17 +42,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 @@ -62,11 +62,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 @@ -80,17 +83,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 @@ -100,11 +106,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 @@ -115,8 +123,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 @@ -126,10 +136,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 @@ -17,20 +17,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 @@ -15,12 +15,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 @@ -57,7 +57,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 @@ -106,7 +106,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 @@ -144,7 +144,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
12 changes: 5 additions & 7 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 All @@ -55,7 +54,6 @@ def test(self):
sparse_loss.backward(retain_graph=True)

assert np.allclose(dense_out.numpy(), sparse_out.to_dense().numpy())
assert np.allclose(loss.numpy(), loss.numpy())
assert np.allclose(x.grad.numpy(), sparse_x.grad.to_dense().numpy())

loss.backward()
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 @@ -59,7 +59,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 @@ -97,7 +97,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
14 changes: 7 additions & 7 deletions python/paddle/fluid/tests/unittests/test_sparse_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import unittest
import numpy as np
import paddle
from paddle.incubate.sparse import nn
import paddle.incubate.sparse as sparse
from paddle.sparse import nn
import paddle.sparse as sparse
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 Expand Up @@ -132,7 +132,7 @@ def test(self):
dense_shape = [1, 1, 3, 4, channels]
sp_x = sparse.sparse_coo_tensor(indices, values, dense_shape)

sparse_batch_norm = paddle.incubate.sparse.nn.BatchNorm(channels)
sparse_batch_norm = paddle.sparse.nn.BatchNorm(channels)
sp_y = sparse_batch_norm(sp_x)
out = sp_y.to_dense()

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 @@ -45,7 +45,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 @@ -106,8 +106,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
Loading

0 comments on commit 1c8ef38

Please sign in to comment.