From 56f223eadf0d029c0ae9e65e331c07789089b387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 05:32:36 +0000 Subject: [PATCH 1/6] =?UTF-8?q?add=20mindscience/models/neural=5Foperator/?= =?UTF-8?q?test=5Fpdenet.py.=20=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95pdenet?= =?UTF-8?q?=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 詹玉梁 --- .../models/neural_operator/test_pdenet.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 mindscience/models/neural_operator/test_pdenet.py diff --git a/mindscience/models/neural_operator/test_pdenet.py b/mindscience/models/neural_operator/test_pdenet.py new file mode 100644 index 000000000..f6c7c9396 --- /dev/null +++ b/mindscience/models/neural_operator/test_pdenet.py @@ -0,0 +1,72 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# Licensed under the Apache License, Version 2.0 (the "License"); + +import numpy as np +import pytest +from mindspore import Tensor +import mindspore.common.dtype as mstype + +from mindscience.models.neural_operator.pdenet import PDENet + +@pytest.mark.level0 +@pytest.mark.platform_arm_ascend_training +@pytest.mark.env_onecard +def test_pdenet_forward(): + """Test PDENet forward computation.""" + height, width, channels = 8, 8, 2 + kernel_size, max_order = 3, 2 + + # Construct network + net = PDENet( + height=height, + width=width, + channels=channels, + kernel_size=kernel_size, + max_order=max_order, + dx=0.01, + dy=0.01, + dt=0.01, + periodic=True, + enable_moment=True, + if_fronzen=False + ) + + # Random input + np_input = np.random.rand(1, channels, height, width).astype(np.float32) + x = Tensor(np_input, mstype.float32) + + # Forward pass + out = net(x) + + # Shape check + assert out.shape == (1, channels, height, width) + + # Type check + assert out.dtype == mstype.float32 + + # Ensure numerical values are finite + assert np.isfinite(out.asnumpy()).all() + +def test_pdenet_no_moment(): + """Test PDENet without moment constraint.""" + net = PDENet( + height=4, + width=4, + channels=1, + kernel_size=3, + max_order=1, + enable_moment=False + ) + + x = Tensor(np.random.rand(1, 1, 4, 4).astype(np.float32)) + y = net(x) + + assert y.shape == (1, 1, 4, 4) + assert np.isfinite(y.asnumpy()).all() + +def test_coefficient_parameter(): + """Test that coefficient parameter exists and has correct shape.""" + net = PDENet(4, 4, 1, 3, 2) + coe = net.coe + assert coe.shape[0] == net.num_filter - 1 + assert coe.ndim == 3 -- Gitee From 2c9c171641aa2cfec403a1d75dc0e556a334b0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 09:31:19 +0000 Subject: [PATCH 2/6] update mindscience/models/neural_operator/test_pdenet.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 詹玉梁 --- mindscience/models/neural_operator/test_pdenet.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mindscience/models/neural_operator/test_pdenet.py b/mindscience/models/neural_operator/test_pdenet.py index f6c7c9396..604c7eafc 100644 --- a/mindscience/models/neural_operator/test_pdenet.py +++ b/mindscience/models/neural_operator/test_pdenet.py @@ -1,12 +1,16 @@ -# Copyright 2022 Huawei Technologies Co., Ltd -# Licensed under the Apache License, Version 2.0 (the "License"); +""" +Unit tests for the PDENet module in mindscience.models.neural_operator.pdenet. +These tests verify the forward computation, moment constraint branch, +and coefficient parameter initialization of PDENet. +""" import numpy as np import pytest from mindspore import Tensor import mindspore.common.dtype as mstype - from mindscience.models.neural_operator.pdenet import PDENet +... + @pytest.mark.level0 @pytest.mark.platform_arm_ascend_training -- Gitee From 6c05b73c99a3ddf56968a072bc36f68401e0c06b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 09:34:02 +0000 Subject: [PATCH 3/6] update mindscience/models/neural_operator/test_pdenet.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 詹玉梁 --- .../models/neural_operator/test_pdenet.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mindscience/models/neural_operator/test_pdenet.py b/mindscience/models/neural_operator/test_pdenet.py index 604c7eafc..a7990dca4 100644 --- a/mindscience/models/neural_operator/test_pdenet.py +++ b/mindscience/models/neural_operator/test_pdenet.py @@ -1,9 +1,19 @@ -""" -Unit tests for the PDENet module in mindscience.models.neural_operator.pdenet. +# Copyright 2024 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""Unit tests for PDENet in mindscience.models.neural_operator.pdenet.""" -These tests verify the forward computation, moment constraint branch, -and coefficient parameter initialization of PDENet. -""" import numpy as np import pytest from mindspore import Tensor -- Gitee From bd1a6283eb936d4156e9b807d1846e122aecc0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 09:42:47 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20mind?= =?UTF-8?q?science/models/neural=5Foperator/test=5Fpdenet.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/neural_operator/test_pdenet.py | 86 ------------------- 1 file changed, 86 deletions(-) delete mode 100644 mindscience/models/neural_operator/test_pdenet.py diff --git a/mindscience/models/neural_operator/test_pdenet.py b/mindscience/models/neural_operator/test_pdenet.py deleted file mode 100644 index a7990dca4..000000000 --- a/mindscience/models/neural_operator/test_pdenet.py +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright 2024 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Unit tests for PDENet in mindscience.models.neural_operator.pdenet.""" - -import numpy as np -import pytest -from mindspore import Tensor -import mindspore.common.dtype as mstype -from mindscience.models.neural_operator.pdenet import PDENet -... - - -@pytest.mark.level0 -@pytest.mark.platform_arm_ascend_training -@pytest.mark.env_onecard -def test_pdenet_forward(): - """Test PDENet forward computation.""" - height, width, channels = 8, 8, 2 - kernel_size, max_order = 3, 2 - - # Construct network - net = PDENet( - height=height, - width=width, - channels=channels, - kernel_size=kernel_size, - max_order=max_order, - dx=0.01, - dy=0.01, - dt=0.01, - periodic=True, - enable_moment=True, - if_fronzen=False - ) - - # Random input - np_input = np.random.rand(1, channels, height, width).astype(np.float32) - x = Tensor(np_input, mstype.float32) - - # Forward pass - out = net(x) - - # Shape check - assert out.shape == (1, channels, height, width) - - # Type check - assert out.dtype == mstype.float32 - - # Ensure numerical values are finite - assert np.isfinite(out.asnumpy()).all() - -def test_pdenet_no_moment(): - """Test PDENet without moment constraint.""" - net = PDENet( - height=4, - width=4, - channels=1, - kernel_size=3, - max_order=1, - enable_moment=False - ) - - x = Tensor(np.random.rand(1, 1, 4, 4).astype(np.float32)) - y = net(x) - - assert y.shape == (1, 1, 4, 4) - assert np.isfinite(y.asnumpy()).all() - -def test_coefficient_parameter(): - """Test that coefficient parameter exists and has correct shape.""" - net = PDENet(4, 4, 1, 3, 2) - coe = net.coe - assert coe.shape[0] == net.num_filter - 1 - assert coe.ndim == 3 -- Gitee From ef7875f8cb7f7e4b5e65f9a675b3da3a4e95b33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 09:46:11 +0000 Subject: [PATCH 5/6] update mindscience/models/neural_operator/pdenet.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 詹玉梁 --- mindscience/models/neural_operator/pdenet.py | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/mindscience/models/neural_operator/pdenet.py b/mindscience/models/neural_operator/pdenet.py index 3c0550af1..2343ac284 100644 --- a/mindscience/models/neural_operator/pdenet.py +++ b/mindscience/models/neural_operator/pdenet.py @@ -75,6 +75,70 @@ class PDENet(nn.Cell): ``Ascend`` ``GPU`` Examples: + import numpy as np + import pytest + from mindspore import Tensor + import mindspore.common.dtype as mstype + from mindscience.models.neural_operator.pdenet import PDENet + @pytest.mark.level0 + @pytest.mark.platform_arm_ascend_training + @pytest.mark.env_onecard + def test_pdenet_forward(): + height, width, channels = 8, 8, 2 + kernel_size, max_order = 3, 2 + + # Construct network + net = PDENet( + height=height, + width=width, + channels=channels, + kernel_size=kernel_size, + max_order=max_order, + dx=0.01, + dy=0.01, + dt=0.01, + periodic=True, + enable_moment=True, + if_fronzen=False + ) + + # Random input + np_input = np.random.rand(1, channels, height, width).astype(np.float32) + x = Tensor(np_input, mstype.float32) + + # Forward pass + out = net(x) + + # Shape check + assert out.shape == (1, channels, height, width) + + # Type check + assert out.dtype == mstype.float32 + + # Ensure numerical values are finite + assert np.isfinite(out.asnumpy()).all() + + def test_pdenet_no_moment(): + net = PDENet( + height=4, + width=4, + channels=1, + kernel_size=3, + max_order=1, + enable_moment=False + ) + + x = Tensor(np.random.rand(1, 1, 4, 4).astype(np.float32)) + y = net(x) + + assert y.shape == (1, 1, 4, 4) + assert np.isfinite(y.asnumpy()).all() + + def test_coefficient_parameter(): + net = PDENet(4, 4, 1, 3, 2) + coe = net.coe + assert coe.shape[0] == net.num_filter - 1 + assert coe.ndim == 3 >>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.common.dtype as mstype -- Gitee From 9611ad49d767909576a4efada84728f1fe56da69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E7=8E=89=E6=A2=81?= Date: Fri, 7 Nov 2025 10:09:46 +0000 Subject: [PATCH 6/6] update mindscience/models/neural_operator/pdenet.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 詹玉梁 --- mindscience/models/neural_operator/pdenet.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mindscience/models/neural_operator/pdenet.py b/mindscience/models/neural_operator/pdenet.py index 2343ac284..b5041879a 100644 --- a/mindscience/models/neural_operator/pdenet.py +++ b/mindscience/models/neural_operator/pdenet.py @@ -207,6 +207,7 @@ class PDENet(nn.Cell): return self.coe_param def _one_step_forward(self, x): + """Perform one forward step for PDE evolution.""" if self.periodic: x = self._periodicpad(x) @@ -244,6 +245,7 @@ class PDENet(nn.Cell): return out def _init_moment(self): + """Initialize the moment parameters for PDE-Net.""" raw_moment = ms_np.zeros((self.num_filter, self.kernel_size, self.kernel_size)) mask = ms_np.ones((self.num_filter, self.kernel_size, self.kernel_size)) scale = ms_np.ones((self.num_filter,)) @@ -270,6 +272,7 @@ class PDENet(nn.Cell): self.moment = Parameter(raw_moment) def _periodicpad(self, x): + """Apply periodic padding to the input tensor.""" cast = ops.Cast() x = cast(x, self.dtype) x_dim = len(x.shape) -- Gitee