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

Problem on running with fdtd.set_backend("torch") #66

Open
MoeinMovafagh opened this issue Nov 3, 2023 · 10 comments
Open

Problem on running with fdtd.set_backend("torch") #66

MoeinMovafagh opened this issue Nov 3, 2023 · 10 comments

Comments

@MoeinMovafagh
Copy link

**Hello,

When I execute my code with fdtd.set_backend("numpy"), it functions properly. However, switching to fdtd.set_backend("torch") leads to an error:**

Exception has occurred: TypeError
'torch.dtype' object is not callable
File "C:\Users\yo0003\moein\FDTD.py", line 73, in
grid[row:row+1,col:col+1,0] = fdtd.AbsorbingObject(permittivity=45.38,conductivity=0.77, name=str(row) + "," + str(col))
~~~~^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'torch.dtype' object is not callable

this is my code:

import torch
import fdtd
import numpy as np
from line_profiler import LineProfiler
import nibabel as nib
import matplotlib.pyplot as plt

device = torch.device("cpu")

Load the MNC file

mnc_img = nib.load('C:\Users\yo0003\moein\MATLAB_DRIVE\Matlab\subject04.mnc')

Get the data as a numpy array

data = mnc_img.get_fdata()

pic=20*data[250,:,:]
pic = np.pad(pic, ((10, 10), (10, 10)), mode='constant', constant_values=0)
for i in range(len(pic[:,1])):
for j in range(len(pic[1,:])):
if pic[i,j]==160:
pic[i,j]=20

    if pic[i,j]==80 or pic[i,j]==180 or pic[i,j]==100:
       pic[i,j]=120 
    
    if pic[i,j]==140 or pic[i,j]==200 : 
       pic[i,j]=220 

pic=pic[::4,::4]
plt.imshow(pic)
plt.show()
pic = np.pad(pic, ((40, 40), (40, 40)), 'constant', constant_values=((0, 0), (0, 0)))

Set FDTD backend to CPU

fdtd.set_backend("torch")

Create a 2D grid with a single layer in the z-direction

Lx=len(pic[:,1])
Ly=len(pic[1,:])
freq=1e9
landa=3e8/freq
grid = fdtd.Grid(shape=(Lx, Ly, 1), grid_spacing=0.1*landa)
Brain=np.zeros((Lx, Ly,2))

# Add a light source

grid[20, 20, 0] = fdtd.PointSource(period=landa/3e8, name="source")

#*********************************************************

avg

#*********************************************************
loc=np.where(pic==120)

try:
# Your problematic code here, e.g.:
grid[40:50,50:60,0] = fdtd.Object(permittivity=45)

except Exception as e:
print("Error:", str(e))

# Importing the traceback module to print the full stack trace
import traceback
print("Full traceback:\n", traceback.format_exc())

for row, col in zip(*loc):
grid[row:row+1,col:col+1,0] = fdtd.AbsorbingObject(permittivity=45.38,conductivity=0.77, name=str(row) + "," + str(col))

@devbrones
Copy link
Contributor

same issue with the 05-lenses-and-analysing-lensing-actions.ipynb example :/ (using python 3.9, pytorch=2.0.1=py3.9_cuda11.7_cudnn8.5.0_0, pytorch-cuda=11.7=h778d358_5)

@devbrones
Copy link
Contributor

Appears to be caused by pr #63

@flaport
Copy link
Owner

flaport commented Nov 17, 2023

Hi everyone. My apologies for taking so long to fix this. The fix is now available in fdtd>=0.3.3.

@flaport flaport closed this as completed Nov 17, 2023
@devbrones
Copy link
Contributor

devbrones commented Dec 1, 2023

Sorry, but i still cant get the examples to work properly, using 05-lenses-and-analysing-lensing-actions.ipynb. Setting the backend using fdtd.set_backend("torch.cuda")
i get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb) Cell 7 line 7
      [5](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#W6sZmlsZQ%3D%3D?line=4) for i, val in enumerate(np.flip(col)):
      [6](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#W6sZmlsZQ%3D%3D?line=5)     if val:
----> [7](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#W6sZmlsZQ%3D%3D?line=6)         grid[30 + i : 50 - i, j - 100 : j - 99, 0] = fdtd.Object(permittivity=1.5 ** 2, name=str(i) + "," + str(j))
      [8](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#W6sZmlsZQ%3D%3D?line=7)         break

File [~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:370](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:370), in Grid.__setitem__(self, key, attr)
    [367](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:367) else:
    [368](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:368)     raise KeyError("maximum number of indices for the grid is 3")
--> [370](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:370) attr._register_grid(
    [371](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:371)     grid=self,
    [372](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:372)     x=self._handle_single_key(x),
    [373](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:373)     y=self._handle_single_key(y),
    [374](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:374)     z=self._handle_single_key(z),
    [375](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:375) )

File [~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:73](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:73), in Object._register_grid(self, grid, x, y, z)
     [70](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:70) if bd.is_array(self.permittivity) and len(self.permittivity.shape) == 3:
     [71](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:71)     self.permittivity = self.permittivity[:, :, :, None]
     [72](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:72) self.inverse_permittivity = (
---> [73](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:73)     bd.ones((self.Nx, self.Ny, self.Nz, 3), dtype=self.permittivity.dtype)
     [74](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:74)     [/](https://file+.vscode-resource.vscode-cdn.net/) self.permittivity
     [75](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:75) )
     [77](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:77) # set the permittivity values of the object at its border to be equal
     [78](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:78) # to the grid permittivity. This way, the object is made symmetric.
     [79](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/objects.py:79) if self.Nx > 1:

TypeError: ones() got an unexpected keyword argument 'dtype'

flaport added a commit that referenced this issue Dec 3, 2023
@flaport
Copy link
Owner

flaport commented Dec 3, 2023

version 0.3.4 should fix the issue

@devbrones
Copy link
Contributor

please tell me if im doing something wrong here, but it still does not work?
are you developing on a system that has cuda support?

TypeError                                 Traceback (most recent call last)
[/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb) Cell 15 line 1
      [7](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#X20sZmlsZQ%3D%3D?line=6)         plt.title(f"{i:3.0f}")
      [8](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#X20sZmlsZQ%3D%3D?line=7)         clear_output(wait=True) # only necessary in jupyter notebooks
---> [10](vscode-notebook-cell:/home/tb/repos/magisim/src/tests/05-lenses-and-analysing-lensing-actions.ipynb#X20sZmlsZQ%3D%3D?line=9) grid.save_data()  # saving detector readings

File [~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:505](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:505), in Grid.save_data(self)
    [503](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:503)     dic[detector.name + " (E)"] = [x for x in detector.detector_values()["E"]]
    [504](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:504)     dic[detector.name + " (H)"] = [x for x in detector.detector_values()["H"]]
--> [505](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/fdtd/grid.py:505) savez(path.join(self.folder, "detector_readings"), **dic)

File <__array_function__ internals>:180, in savez(*args, **kwargs)

File [~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:612](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:612), in savez(file, *args, **kwds)
    [528](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:528) @array_function_dispatch(_savez_dispatcher)
    [529](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:529) def savez(file, *args, **kwds):
    [530](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:530)     """Save several arrays into a single file in uncompressed ``.npz`` format.
    [531](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:531) 
    [532](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:532)     Provide arrays as keyword arguments to store them under the
   (...)
    [610](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:610) 
    [611](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:611)     """
--> [612](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/numpy/lib/npyio.py:612)     _savez(file, args, kwds, False)
...
--> [970](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/torch/_tensor.py:970)     return self.numpy()
    [971](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/torch/_tensor.py:971) else:
    [972](https://file+.vscode-resource.vscode-cdn.net/home/tb/repos/magisim/src/tests/~/anaconda3/envs/msimdev/lib/python3.9/site-packages/torch/_tensor.py:972)     return self.numpy().astype(dtype, copy=False)

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.```

flaport added a commit that referenced this issue Dec 7, 2023
@flaport
Copy link
Owner

flaport commented Dec 7, 2023

Hi, my apologies... I don't have my computer with cuda GPU anymore so before I thought I could just debug by using the torch.cpu backend, but it seems that this was a wrong assumption...

I spun up a VM with a GPU to debug this further. I can now confirm that all example notebooks now succesfully run with torch.cuda backend for fdtd>=0.3.5.

My apologies again for making you loose your time 🙂

@devbrones
Copy link
Contributor

still appears broken in 0.3.5, when using fdtd.set_backend("torch.cuda") i get

python3.9/site-packages/fdtd/objects.py", line 80, in _register_grid
    self.inverse_permittivity[-1, :, :, 0] = self.grid.inverse_permittivity[
TypeError: can't assign a numpy.ndarray to a torch.cuda.DoubleTensor

@flaport
Copy link
Owner

flaport commented Dec 17, 2023

which notebook is this in? Or is this in a custom notebook?

@flaport flaport reopened this Dec 17, 2023
@devbrones
Copy link
Contributor

this is in the lenses example 05-lenses-and-analysing-lensing-actions.ipynb all the path prefixes are just because i copied them to my project

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

No branches or pull requests

3 participants