mdw771 / adorym

Adorym: Automatic Differentiation-based Object Reconstruction with DynaMical Scattering
https://adorym.readthedocs.io/
25 stars 10 forks source link

Mixed-type operation error #2

Closed mdw771 closed 3 years ago

mdw771 commented 3 years ago

With Pytorch < 1.3.0, running some of the demo scripts (e.g. 2d_multidist_holography_w_position_correction.py) fails with the following error message:

RuntimeError: expected device cuda:0 and dtype Double but got device cuda:0 and dtype Float
mdw771 commented 3 years ago

The problem is likely to be due to the lack of automatic type promotion in PyTorch 1.2.0 (this is implemented in 1.3.0): because the backend needs to be switchable between PyTorch and Autograd (which uses the Numpy API), a few tensors are created as numpy arrays whose data type defaults to float64 before they are converted to torch tensors (default to float32).

The best way to fix is probably to update PyTorch to 1.4.0 or above. Otherwise, a makeshift solution would be: in adorym/wrappers.py, change the default of argument dtype in create_variable (l. 81) from None to float32. This will set the default data type when creating new tensors to float32 and avoid mixed type.

I'm also planning an update to avoid mixed-type operations as much as possible.