NanoComp / meep

free finite-difference time-domain (FDTD) software for electromagnetic simulations
GNU General Public License v2.0
1.19k stars 610 forks source link

`test_adjoint_solver.py` failing due to autograd-related error #2539

Closed oskooi closed 1 year ago

oskooi commented 1 year ago

Several of the unit tests in test_adjoint_solve.py are failing due to an error related to autograd. See below for output from the test logs. I think this could be due to the recent changes made to the adjoint filters in #2465 and #2518.

Since there does not seem to have been any significant functional changes to autograd recently, it's not clear why these errors were not triggered in the Github CI.

____________________ TestAdjointSolver.test_periodic_design ____________________

>               solid_grad = grad(mpa.constraint_solid, 0)(
                    p,
                    c,
                    eta_e,
                    filter_f,
                    threshold_f,
                    self.design_region_resolution,
                    periodic_axes,
                )

tests/test_adjoint_solver.py:999:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../autograd/wrap_util.py:20: in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
../autograd/differential_operators.py:29: in grad
    return vjp(vspace(ans).ones())
../autograd/core.py:14: in vjp
    def vjp(g): return backward_pass(g, end_node)
../autograd/core.py:21: in backward_pass
    ingrads = node.vjp(outgrad[0])
../autograd/core.py:67: in <lambda>
    return lambda g: (vjp(g),)
../autograd/numpy/fft.py:17: in <lambda>
    return lambda g: match_complex(x, truncate_pad(fft_fun(g, *args, **kwargs), vs.shape))
../autograd/tracer.py:48: in f_wrapped
    return f_raw(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

x = array([[-1.28056837e-07+0.00000000e+00j,  1.13440917e-07-6.88609116e-09j,
        -7.57466336e-08+1.16364222e-08j, .......,
        -2.43666044e-10+9.31947518e-10j, -5.38423198e-09-5.29486095e-09j,
         8.57666150e-09+9.03418206e-09j]])
shape = (91, 273)

    @primitive
    def truncate_pad(x, shape):
        # truncate/pad x to have the appropriate shape
        slices = [slice(n) for n in shape]
        pads = list(zip(anp.zeros(len(shape), dtype=int),
                   anp.maximum(0, anp.array(shape) - anp.array(x.shape))))
>       return anp.pad(x, pads, 'constant')[slices]
E       IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

../autograd/numpy/fft.py:101: IndexError
smartalecH commented 1 year ago

(cc @mawc2019)

oskooi commented 1 year ago

Looks like the problem was the outdated version of Autograd 1.2 (Nov. 2017) I was using.

We should mention in the documentation that the adjoint solver requires the most recent version 1.5 (Sep. 2022).

mawc2019 commented 1 year ago

Looks like the problem was the outdated version of Autograd 1.2 (Nov. 2017) I was using.

I am using Autograd 1.3 and there is no error. So a version $\geq 1.3$ seems needed.