Skip to content

Fix geometry dtype to respect config.real(np) for NumPy 2.x compatibility#2069

Open
SimingShan wants to merge 1 commit intolululxvi:masterfrom
SimingShan:fix/geometry-dtype-precision
Open

Fix geometry dtype to respect config.real(np) for NumPy 2.x compatibility#2069
SimingShan wants to merge 1 commit intolululxvi:masterfrom
SimingShan:fix/geometry-dtype-precision

Conversation

@SimingShan
Copy link
Copy Markdown

Several geometry subclasses (Rectangle, Disk, StarShaped, Triangle, Polygon, Cuboid, GeometryXTime) created NumPy arrays via np.linspace, np.full, np.zeros, np.ones without specifying dtype, defaulting to float64. This caused a dtype mismatch with the neural network (float32 by default), resulting in "mat1 and mat2 must have the same dtype, but got Double and Float" errors in PyTorch with NumPy >= 2.0.

Added dtype=config.real(np) to all affected array-creation calls, consistent with the existing pattern used in geometry_1d.py, geometry_nd.py, and sampler.py.

…lity

Several geometry subclasses (Rectangle, Disk, StarShaped, Triangle, Polygon,
Cuboid, GeometryXTime) created NumPy arrays via np.linspace, np.full, np.zeros,
np.ones without specifying dtype, defaulting to float64. This caused a dtype
mismatch with the neural network (float32 by default), resulting in
"mat1 and mat2 must have the same dtype, but got Double and Float" errors
in PyTorch with NumPy >= 2.0.

Added dtype=config.real(np) to all affected array-creation calls, consistent
with the existing pattern used in geometry_1d.py, geometry_nd.py, and sampler.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@echen5503
Copy link
Copy Markdown
Contributor

Could you provide code to demonstrate the issue?

@SimingShan
Copy link
Copy Markdown
Author

See below
`(base) shansiming@wuzhui:~/project/deepxde$ python test_script.py
Using backend: pytorch
Other supported backends: tensorflow.compat.v1, tensorflow, jax, paddle.
paddle supports more examples now and is recommended.
/home/shansiming/project/deepxde/deepxde/utils/external.py:9: UserWarning: A NumPy version >=1.23.5 and <2.3.0 is required for this version of SciPy (detected version 2.4.3)
import scipy.spatial.distance
NumPy version: 2.4.3
Warning: 50 points required, but 54 points sampled.
uniform_boundary_points dtype: float64
random_boundary_points dtype: float64
Warning: Polygon.uniform_points not implemented. Use random_points instead.
Compiling model...
'compile' took 0.390143 s

Training model...

Traceback (most recent call last):
File "/home/shansiming/project/deepxde/test_script.py", line 23, in
model.train(iterations=100)
File "/home/shansiming/project/deepxde/deepxde/utils/internal.py", line 22, in wrapper
result = f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/shansiming/project/deepxde/deepxde/model.py", line 698, in train
self._test(verbose=verbose)
File "/home/shansiming/project/deepxde/deepxde/model.py", line 891, in _test
) = self._outputs_losses(
^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/project/deepxde/deepxde/model.py", line 604, in outputs_losses
outs = outputs_losses(inputs, targets, auxiliary_vars)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/project/deepxde/deepxde/model.py", line 354, in outputs_losses_train
return outputs_losses(
^^^^^^^^^^^^^^^
File "/home/shansiming/project/deepxde/deepxde/model.py", line 331, in outputs_losses
outputs
= self.net(inputs)
^^^^^^^^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/project/deepxde/deepxde/nn/pytorch/fnn.py", line 61, in forward
else self.activation(linear(x))
^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/nn/modules/linear.py", line 117, in forward
return F.linear(input, self.weight, self.bias)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/shansiming/.local/lib/python3.12/site-packages/torch/utils/_device.py", line 79, in torch_function
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float`

@SimingShan
Copy link
Copy Markdown
Author

`(base) shansiming@wuzhui:~/project/deepxde$ python test_script.py
Using backend: pytorch
Other supported backends: tensorflow.compat.v1, tensorflow, jax, paddle.
paddle supports more examples now and is recommended.
NumPy version: 1.26.4
Warning: 50 points required, but 54 points sampled.
uniform_boundary_points dtype: float64
random_boundary_points dtype: float32
Warning: Polygon.uniform_points not implemented. Use random_points instead.
Compiling model...
'compile' took 0.384630 s

Training model...

/home/shansiming/.local/lib/python3.12/site-packages/torch/autograd/graph.py:768: UserWarning: Attempting to run cuBLAS, but there was no current CUDA context! Attempting to set the primary context... (Triggered internally at ../aten/src/ATen/cuda/CublasHandlePool.cpp:135.)
return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
Step Train loss Test loss Test metric
0 [1.01e+00, 2.06e-02] [9.91e-01, 2.06e-02] []
100 [6.98e-02, 3.87e-03] [5.69e-02, 3.87e-03] []

Best model at step 100:
train loss: 7.37e-02
test loss: 6.08e-02
test metric: []

'train' took 0.478478 s

Training completed successfully`

@SimingShan
Copy link
Copy Markdown
Author

After the fix
`(base) shansiming@wuzhui:~/project/deepxde$ python test_script.py
Using backend: pytorch
Other supported backends: tensorflow.compat.v1, tensorflow, jax, paddle.
paddle supports more examples now and is recommended.
/home/shansiming/project/deepxde/deepxde/utils/external.py:9: UserWarning: A NumPy version >=1.23.5 and <2.3.0 is required for this version of SciPy (detected version 2.4.3)
import scipy.spatial.distance
NumPy version: 2.4.3
Warning: 50 points required, but 54 points sampled.
uniform_boundary_points dtype: float32
random_boundary_points dtype: float32
Warning: Polygon.uniform_points not implemented. Use random_points instead.
Compiling model...
'compile' took 0.380587 s

Training model...

/home/shansiming/.local/lib/python3.12/site-packages/torch/autograd/graph.py:768: UserWarning: Attempting to run cuBLAS, but there was no current CUDA context! Attempting to set the primary context... (Triggered internally at ../aten/src/ATen/cuda/CublasHandlePool.cpp:135.)
return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
Step Train loss Test loss Test metric
0 [9.77e-01, 4.38e-02] [9.86e-01, 4.38e-02] []
100 [7.47e-02, 3.49e-03] [6.68e-02, 3.49e-03] []

Best model at step 100:
train loss: 7.82e-02
test loss: 7.03e-02
test metric: []

'train' took 0.473906 s

Training completed successfully`

@SimingShan
Copy link
Copy Markdown
Author

Pay attention to the numpy version @echen5503

@echen5503
Copy link
Copy Markdown
Contributor

Logs are convincing. As a final check, can you provide the test script itself?

@echen5503
Copy link
Copy Markdown
Contributor

echen5503 commented Mar 23, 2026

Looks like some files like geometry/pointcloud.py aren't using correct dtype either, maybe good to fix that too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants