google / tangent

Source-to-Source Debuggable Derivatives in Pure Python
Apache License 2.0
2.31k stars 434 forks source link

Can't assign to literal #56

Open stefdoerr opened 6 years ago

stefdoerr commented 6 years ago

Hi, I was wondering if this project is still under development or abandoned. I have quite a few issues which I don't know if I should bother posting here. So, sorry for the issue spam if there is no intention on improving on the project.

Here is the first one which I cannot really understand.

def test(x):
    for f in range(np.shape(x)[2]):
        r = x[:, :, f]
    return r
In [39]: xxx = tangent.grad(test)
Traceback (most recent call last):

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-39-e1ef2738f728>", line 1, in <module>
    xxx = tangent.grad(test)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/grad_util.py", line 386, in grad
    verbose=verbose)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/grad_util.py", line 290, in autodiff
    check_dims, verbose)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/grad_util.py", line 144, in autodiff_tree
    check_dims, verbose)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/grad_util.py", line 104, in autodiff_ast
    node = reverse_ad.joint(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/reverse_ad.py", line 952, in joint
    node, _, _ = _fix(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/reverse_ad.py", line 994, in _fix
    fixes.FixStack().visit(node.body[0])

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/fixes.py", line 64, in visit
    return super(FixStack, self).visit(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/ast.py", line 253, in visit
    return visitor(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/transformers.py", line 199, in generic_visit
    new_values = copy(self.visit_statements(old_value))

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/transformers.py", line 177, in visit_statements
    node = self.visit(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/fixes.py", line 64, in visit
    return super(FixStack, self).visit(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/ast.py", line 253, in visit
    return visitor(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/transformers.py", line 199, in generic_visit
    new_values = copy(self.visit_statements(old_value))

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/transformers.py", line 177, in visit_statements
    node = self.visit(node)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/fixes.py", line 63, in visit
    self.insert_top(quoting.quote('{} = None'.format(varname)))

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/quoting.py", line 112, in quote
    node = parse_string(src_string)

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/tangent/quoting.py", line 95, in parse_string
    return gast.parse(textwrap.dedent(src))

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/site-packages/gast/gast.py", line 237, in parse
    return ast_to_gast(_ast.parse(*args, **kwargs))

  File "/shared/sdoerr/Software/miniconda3/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File "<unknown>", line 1
SyntaxError: can't assign to literal
stefdoerr commented 6 years ago

It seems to me like you cannot overwrite a variable because it tries to assign to the value of the variable in the second iteration.

alexbw commented 6 years ago

That's a really interesting error, I'll take a look into what specifically is causing the bug here. My guess is there's something wrong with our activity analysis, triggered by the fact that r is only ever defined inside of the loop, and not outside.