Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mindspore backend #55

Merged
merged 136 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 116 commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
684a9d0
mindspore backend file created.
Oct 30, 2022
6c05a54
mindspore backend file created.
Nov 4, 2022
ab78ae8
function hungarian,test 1
Nov 6, 2022
7d0f02b
mindspore 11.11
Nov 11, 2022
0fc4300
mindspore 11.13
Nov 13, 2022
6c4fe54
mindspore 11.14
Nov 15, 2022
601b9a6
mindspore 11.14
Nov 15, 2022
56b318a
11.18
wlxlatiao666 Nov 18, 2022
d3d818a
mindspore 11.19
Nov 18, 2022
784435a
mindspore 11.21
Nov 21, 2022
516ae67
11.24
wlxlatiao666 Nov 24, 2022
0831543
11.24
wlxlatiao666 Nov 24, 2022
cb1f8ed
11.25
wlxlatiao666 Nov 25, 2022
a49db39
mindspore 11.27
Nov 27, 2022
9c3aafe
mindspore 11.28
Nov 28, 2022
fd7e3ca
mindspore 11.28
Nov 28, 2022
cdde8d6
mindspore 11.28
Nov 28, 2022
9df4755
mindspore 11.28
Nov 28, 2022
f4d7474
Merge branch 'main' into main
rogerwwww Nov 29, 2022
50bd466
mindspore 11.30
Nov 30, 2022
7830988
mindspore 12.6
Dec 6, 2022
d8aa15f
mindspore 12.6
Dec 6, 2022
b8358bc
mindspore 12.6
Dec 6, 2022
8eb433f
mindspore 12.6
Dec 6, 2022
dc0b975
mindspore 12.6
Dec 11, 2022
0a5faf9
mindspore 12.13
Dec 13, 2022
cb4293e
Merge branch 'main' into main
rogerwwww Dec 16, 2022
7e9c49e
mindspore 12.13
Dec 18, 2022
c52c0b2
Merge branch 'main' into main
rogerwwww Dec 18, 2022
aa8166a
mindspore 12.19
Dec 19, 2022
e4feb11
Merge remote-tracking branch 'origin/main'
Dec 19, 2022
fae7c66
mindspore 12.19
Dec 19, 2022
eaf40a1
mindspore 12.20
Dec 20, 2022
e1c8bb9
mindspore 12.20
Dec 20, 2022
0978205
mindspore 12.20
Dec 20, 2022
d684ccc
mindspore 12.22
Dec 22, 2022
1296c82
mindspore 1.26
Jan 26, 2023
04e17f1
mindspore 1.28
Jan 28, 2023
9ee3b09
mindspore 4.9
Apr 9, 2023
3fd66b5
mindspore 4.10
Apr 10, 2023
331b772
mindspore 4.10
Apr 10, 2023
7cbe3b1
mindspore 4.10
Apr 10, 2023
d7a737d
mindspore 4.10
Apr 10, 2023
436d9b9
mindspore 4.11
Apr 11, 2023
f0f4640
mindspore 4.11
Apr 11, 2023
7bd6fed
mindspore 4.11
Apr 11, 2023
049bd19
mindspore 4.11
Apr 11, 2023
9f58b5f
mindspore 4.11
Apr 11, 2023
9c273c9
mindspore 4.11
Apr 11, 2023
876a24b
mindspore 4.11
Apr 11, 2023
a585ad1
mindspore 4.11
Apr 11, 2023
c446df7
mindspore 4.11
Apr 11, 2023
a7e1821
mindspore 4.11
Apr 11, 2023
d1f8996
mindspore 4.11
Apr 11, 2023
a62d96e
mindspore 4.11
Apr 11, 2023
06a399f
mindspore 4.12
Apr 12, 2023
599fa63
mindspore 4.12
Apr 12, 2023
cb2c246
mindspore 4.12
Apr 12, 2023
b95accd
mindspore 4.12
Apr 12, 2023
5e55e2b
mindspore 4.12
Apr 12, 2023
48c62f3
mindspore 4.12
Apr 12, 2023
9386202
mindspore 4.12
Apr 12, 2023
08f5c53
mindspore 4.13
Apr 13, 2023
8ea8ee6
mindspore 4.13
Apr 13, 2023
27f166d
mindspore 4.13
Apr 13, 2023
63b54f6
mindspore 4.13
Apr 13, 2023
dc97e13
mindspore 4.13
Apr 13, 2023
3296954
mindspore 4.13
Apr 13, 2023
752acdc
mindspore 4.13
Apr 13, 2023
56d4202
mindspore 4.13
Apr 13, 2023
b9aee87
mindspore 4.13
Apr 13, 2023
90bc5c8
mindspore 4.13
Apr 13, 2023
aa2eabf
mindspore 4.13
Apr 14, 2023
d25bfcd
mindspore 4.14
Apr 14, 2023
3882d73
mindspore 4.14
Apr 14, 2023
d9ce180
mindspore 4.14
Apr 14, 2023
7eec85f
mindspore 4.14
Apr 14, 2023
f128fa1
mindspore 4.14
Apr 14, 2023
819e6f2
mindspore 4.14
Apr 14, 2023
11143f4
mindspore 4.14
Apr 14, 2023
5774943
mindspore 4.14
Apr 14, 2023
3941a15
mindspore 4.16
Apr 16, 2023
4c92c42
mindspore 4.17
Apr 17, 2023
2a00ded
mindspore 4.17
Apr 17, 2023
0ef6a1c
mindspore 4.17
Apr 17, 2023
70526e8
mindspore 4.17
Apr 17, 2023
d54784f
mindspore 4.18
Apr 18, 2023
cb00ed7
mindspore 4.18
Apr 18, 2023
71deffe
mindspore 4.18
Apr 18, 2023
713a19a
mindspore 4.18
Apr 18, 2023
39bd500
mindspore 4.18
Apr 18, 2023
66de506
mindspore 4.18
Apr 18, 2023
6f2b075
mindspore 4.18
Apr 18, 2023
c485e25
mindspore 4.18
Apr 18, 2023
e3382e6
mindspore 4.19
Apr 18, 2023
240894a
mindspore 4.19
Apr 19, 2023
28d1ff2
mindspore 4.19
Apr 19, 2023
3b66a19
mindspore 4.19
Apr 19, 2023
f6be231
mindspore 4.19
Apr 19, 2023
98fa0b4
mindspore 4.19
Apr 19, 2023
89fb815
mindspore 4.19
Apr 19, 2023
6052a05
mindspore 4.19
Apr 19, 2023
920e25a
mindspore 4.19
Apr 19, 2023
4a23691
mindspore 4.19
Apr 19, 2023
def0cb5
mindspore 4.20
Apr 20, 2023
3186868
mindspore 4.20
Apr 20, 2023
ba765b9
mindspore 4.20
Apr 20, 2023
a66e78f
mindspore 4.20
Apr 20, 2023
52df9ff
mindspore 4.20
Apr 20, 2023
9e09b27
Merge branch 'main' into main
rogerwwww Apr 21, 2023
4587243
fix spacing
rogerwwww Apr 21, 2023
6826271
fix spacing and blank line
rogerwwww Apr 21, 2023
66297d6
Update utils.py
rogerwwww Apr 21, 2023
565f968
Update utils.py
rogerwwww Apr 21, 2023
9b66930
add blank line back
rogerwwww Apr 21, 2023
62b25bf
drop 3.10 in testing because MS not supported
rogerwwww Apr 24, 2023
b3f5cf3
mindspore 4.24
Apr 24, 2023
e5ea8c4
Merge remote-tracking branch 'origin/main'
Apr 24, 2023
dab5e9c
mindspore 4.24
Apr 24, 2023
67c1ad5
mindspore 4.24
Apr 24, 2023
c599e13
mindspore 4.24
Apr 24, 2023
f1259e1
mindspore 4.25
Apr 25, 2023
4ef1588
mindspore 4.25
Apr 25, 2023
2d8c586
mindspore 4.25
Apr 25, 2023
5aec14f
mindspore 4.25
Apr 25, 2023
d1080fc
mindspore 4.25
Apr 25, 2023
730d3bd
mindspore 4.25
Apr 25, 2023
5fdeb3d
mindspore 4.25
Apr 25, 2023
5074e52
revert spacing
rogerwwww Apr 25, 2023
aa0e7bf
revert spacing
rogerwwww Apr 25, 2023
7068ce5
revert spacing
rogerwwww Apr 25, 2023
89cbd39
fix spacings
rogerwwww Apr 25, 2023
bf8442e
Delete test_mindspore_classic_solvers.py
rogerwwww Apr 25, 2023
8dc6016
add new line eof
rogerwwww Apr 25, 2023
1e5ef6e
fix spacing
rogerwwww Apr 25, 2023
b36b7ea
only upload codecov once
rogerwwww Apr 25, 2023
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
11 changes: 7 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9"]

steps:
- uses: actions/checkout@v2
Expand All @@ -37,7 +37,8 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest --cov=pygmtools --cov-report=xml
pytest --cov=pygmtools --cov-report=xml tests/test_mindspore_classic_solvers.py
pytest --cov=pygmtools --cov-report=xml --ignore=tests/test_mindspore_classic_solvers.py
- name: Upload to codecov
uses: codecov/codecov-action@v3

Expand Down Expand Up @@ -68,7 +69,8 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest --cov=pygmtools --cov-report=xml
pytest --cov=pygmtools --cov-report=xml tests/test_mindspore_classic_solvers.py
pytest --cov=pygmtools --cov-report=xml --ignore=tests/test_mindspore_classic_solvers.py

windows:

Expand Down Expand Up @@ -97,4 +99,5 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest --cov=pygmtools --cov-report=xml
pytest --cov=pygmtools --cov-report=xml tests/test_mindspore_classic_solvers.py
pytest --cov=pygmtools --cov-report=xml --ignore=tests/test_mindspore_classic_solvers.py
151 changes: 151 additions & 0 deletions pygmtools/classic_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,66 @@ def sm(K, n1=None, n2=None, n1max=None, n2max=None, x0=None,
>>> len(jt.nonzero(K.opt_grad(optim)))
2560

.. dropdown:: mindspore Example

::

>>> import mindspore
>>> import pygmtools as pygm
>>> pygm.BACKEND = 'mindspore'
>>> _ = mindspore.set_seed(1)
>>> mindspore.set_context(mode=mindspore.PYNATIVE_MODE)

# Generate a batch of isomorphic graphs
>>> batch_size = 10
>>> X_gt = mindspore.numpy.zeros((batch_size, 4, 4))
>>> X_gt[:, mindspore.numpy.arange(0, 4, dtype=mindspore.int64), mindspore.ops.Randperm(4)(mindspore.Tensor([4], dtype=mindspore.int32))] = 1
>>> A1 = mindspore.numpy.rand((batch_size, 4, 4))
>>> A2 = mindspore.ops.BatchMatMul()(mindspore.ops.BatchMatMul()(X_gt.swapaxes(1, 2), A1), X_gt)
>>> n1 = n2 = mindspore.Tensor([4] * batch_size)

# Build affinity matrix
>>> conn1, edge1, ne1 = pygm.utils.dense_to_sparse(A1)
>>> conn2, edge2, ne2 = pygm.utils.dense_to_sparse(A2)
>>> import functools
>>> gaussian_aff = functools.partial(pygm.utils.gaussian_aff_fn, sigma=1.) # set affinity function
>>> K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, n1, None, n2, None, edge_aff_fn=gaussian_aff)

# Solve by SM. Note that X is normalized with a squared sum of 1
>>> X = pygm.sm(K, n1, n2)
>>> (X ** 2).sum(axis=(1, 2))
[1.0000002 0.9999998 1.0000002 0.99999964 1. 1.0000001
1. 1. 1. 0.99999994]

# Accuracy
>>> (pygm.hungarian(X) * X_gt).sum() / X_gt.sum()
1.0

# This solver supports gradient back-propogation
>>> def fn(K, n1, n2):
>>> res = pygm.sm(K, n1, n2).sum()
>>> return res

>>> g = mindspore.ops.grad(fn)(K, n1, n2)
>>> mindspore.ops.count_nonzero(g)

# This solver supports gradient back-propogation
>>> from jittor import nn
>>> class Model(nn.Module):
... def __init__(self, K):
... self.K = K
... def execute(self, K, n1, n2):
... X = pygm.sm(K, n1, n2)
... return X

>>> model = Model(K)
>>> optim = nn.SGD(model.parameters(), lr=0.1)
>>> X = model(K, n1, n2)
>>> loss = X.sum()
>>> optim.step(loss)
>>> len(jt.nonzero(K.opt_grad(optim)))
2560

.. dropdown:: Tensorflow Example

::
Expand Down Expand Up @@ -266,6 +326,13 @@ def sm(K, n1=None, n2=None, n1max=None, n2max=None, x0=None,

# Solve by SM. Note that X is normalized with a squared sum of 1
>>> X = pygm.sm(K, n1, n2)
>>> (X ** 2).sum(axis=(1, 2))
[1. 0.9999998 0.99999976 1. 0.99999976 1.
1. 1.0000001 1.0000001 1. ]

# Accuracy
>>> (pygm.hungarian(X) * X_gt).sum() / X_gt.sum()
1.0
>>> tf.reduce_sum((X ** 2), axis=[1, 2])
<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([1. , 1.0000001 , 1. , 0.9999999 , 1. ,
Expand Down Expand Up @@ -546,6 +613,53 @@ def rrwm(K, n1=None, n2=None, n1max=None, n2max=None, x0=None,
>>> len(jt.nonzero(K.opt_grad(optim)))
1536

.. dropdown:: mindspore Example

::

>>> import mindspore
>>> import pygmtools as pygm
>>> pygm.BACKEND = 'mindspore'
>>> _ = mindspore.set_seed(1)
>>> mindspore.set_context(mode=mindspore.PYNATIVE_MODE)

# Generate a batch of isomorphic graphs
>>> batch_size = 10
>>> X_gt = mindspore.numpy.zeros((batch_size, 4, 4))
>>> X_gt[:, mindspore.numpy.arange(0, 4, dtype=mindspore.int64), mindspore.ops.Randperm(4)(mindspore.Tensor([4], dtype=mindspore.int32))] = 1
>>> A1 = mindspore.numpy.rand((batch_size, 4, 4))
>>> A2 = mindspore.ops.BatchMatMul()(mindspore.ops.BatchMatMul()(X_gt.swapaxes(1, 2), A1), X_gt)
>>> n1 = n2 = mindspore.Tensor([4] * batch_size)

# Build affinity matrix
>>> conn1, edge1, ne1 = pygm.utils.dense_to_sparse(A1)
>>> conn2, edge2, ne2 = pygm.utils.dense_to_sparse(A2)
>>> import functools
>>> gaussian_aff = functools.partial(pygm.utils.gaussian_aff_fn, sigma=1.) # set affinity function
>>> K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, n1, None, n2, None, edge_aff_fn=gaussian_aff)

# Solve by RRWM. Note that X is normalized with a sum of 1
>>> X = pygm.rrwm(K, n1, n2, beta=100)
>>> X.sum(axis=(1, 2))
[1. 0.99999994 0.99999994 1. 1.0000002 1.
1. 1. 1.0000001 1.0000001 ]

# Accuracy
>>> (pygm.hungarian(X) * X_gt).sum() / X_gt.sum()
1.0

# This solver supports gradient back-propogation
>>> def fn(K, n1, n2, beta):
>>> X = pygm.rrwm(K, n1, n2, beta=beta)
>>> X_gt = mindspore.numpy.zeros((batch_size, 4, 4))
>>> X_gt[:, mindspore.numpy.arange(0, 4, dtype=mindspore.int64),mindspore.ops.Randperm(4)(mindspore.Tensor([4], dtype=mindspore.int32))] = 1
>>> res = pygm.utils.permutation_loss(X, X_gt)
>>> return res

>>> g = mindspore.ops.grad(fn)(K, n1, n2, beta=100)
>>> mindspore.ops.count_nonzero(g)
2560

.. dropdown:: Tensorflow Example

::
Expand Down Expand Up @@ -823,6 +937,43 @@ def ipfp(K, n1=None, n2=None, n1max=None, n2max=None, x0=None,
>>> (pygm.hungarian(X) * X_gt).sum() / X_gt.sum()
jt.Var([1.], dtype=float32)

.. dropdown:: mindspore Example

::

>>> import mindspore
>>> import pygmtools as pygm
>>> pygm.BACKEND = 'mindspore'
>>> _ = mindspore.set_seed(1)

# Generate a batch of isomorphic graphs
>>> batch_size = 10
>>> X_gt = mindspore.numpy.zeros((batch_size, 4, 4))
>>> X_gt[:, mindspore.numpy.arange(0, 4, dtype=mindspore.int64), mindspore.ops.Randperm(4)(mindspore.Tensor([4], dtype=mindspore.int32))] = 1
>>> A1 = mindspore.numpy.rand((batch_size, 4, 4))
>>> A2 = mindspore.ops.BatchMatMul()(mindspore.ops.BatchMatMul()(X_gt.swapaxes(1, 2), A1), X_gt)
>>> n1 = mindspore.Tensor([4] * batch_size)
>>> n2 = mindspore.Tensor([4] * batch_size)

# Build affinity matrix
>>> conn1, edge1, ne1 = pygm.utils.dense_to_sparse(A1)
>>> conn2, edge2, ne2 = pygm.utils.dense_to_sparse(A2)
>>> import functools
>>> gaussian_aff = functools.partial(pygm.utils.gaussian_aff_fn, sigma=1.) # set affinity function
>>> K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, n1, None, n2, None, edge_aff_fn=gaussian_aff)

# Solve by IPFP
>>> X = pygm.ipfp(K, n1, n2)
>>> X[0]
[[1. 0. 0. 0.]
[0. 0. 0. 1.]
[0. 0. 1. 0.]
[0. 1. 0. 0.]]

# Accuracy
>>> (pygm.hungarian(X) * X_gt).sum() / X_gt.sum()
1.0

.. dropdown:: Tensorflow Example

::
Expand Down
Loading