Repository: pytorch/torchdynamo

Issue:
Multiple issues from OpenNMT-py
This AssertionError always happens when I try to torchdynamo.optimize transformer.


Discussion:
This seems like a bug.  Could you provide an example to reproduce the issue?
Thanks @jansel , I tried to enable torchdynamo for OpenNMT(https://github.com/OpenNMT/OpenNMT-py.git), and as the first step, I set my_compiler does nothing:
_def my_compiler(gm: torch.fx.GraphModule, example_inputs: List[torch.Tensor]):
    print("my_compiler() called with FX graph:")
    return gm.forward  # return a python callable_
and optimized the model:
        _with torch.no_grad():
            for batch in data_iter:
                with torchdynamo.optimize(my_compiler):
                    print("------------inside torch dynamo optimize---------------------")
                    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
                break_
and ran this part, got this error log:
(dynamo) [jiayisun@mlp-sdp-spr-7530 opennmt-py]$ bash run.sh
/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torchvision-0.13.0a0+aa21197-py3.8-linux-x86_64.egg/torchvision/io/image.py:13: UserWarning: Failed to load image Python extension:
  warn(f"Failed to load image Python extension: {e}")
[2022-04-11 19:24:36,281 INFO] Translating shard 0.
------------inside torch dynamo optimize---------------------
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
ERROR FROM offset=34 filename /home/jiayisun/opennmt-py/onmt/encoders/encoder.py 41 AssertionError
ERROR FROM offset=8 filename /home/jiayisun/opennmt-py/onmt/encoders/transformer.py 119 AssertionError
ERROR FROM offset=42 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 592 AssertionError
ERROR FROM offset=24 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 683 AssertionError
ERROR FROM offset=166 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 585 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/misc.py", line 262, in call_function
    return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/user_defined.py", line 170, in call_method
    return UserMethodVariable(method, self, **options).call_function(
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/misc.py", line 262, in call_function
    return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/user_defined.py", line 170, in call_method
    return UserMethodVariable(method, self, **options).call_function(
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/nn_module.py", line 183, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 553, in translate_batch
    def translate_batch(self, batch, src_vocabs, attn_debug):
========== End debug info ==========
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
ERROR FROM offset=34 filename /home/jiayisun/opennmt-py/onmt/encoders/encoder.py 41 AssertionError
ERROR FROM offset=8 filename /home/jiayisun/opennmt-py/onmt/encoders/transformer.py 119 AssertionError
ERROR FROM offset=42 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 592 AssertionError
ERROR FROM offset=24 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 683 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/misc.py", line 262, in call_function
    return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/user_defined.py", line 170, in call_method
    return UserMethodVariable(method, self, **options).call_function(
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/nn_module.py", line 183, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 661, in _translate_batch_with_strategy
    def _translate_batch_with_strategy(
========== End debug info ==========
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
ERROR FROM offset=34 filename /home/jiayisun/opennmt-py/onmt/encoders/encoder.py 41 AssertionError
ERROR FROM offset=8 filename /home/jiayisun/opennmt-py/onmt/encoders/transformer.py 119 AssertionError
ERROR FROM offset=42 filename /home/jiayisun/opennmt-py/onmt/translate/translator.py 592 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/nn_module.py", line 183, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 683, in _translate_batch_with_strategy
    src, enc_states, memory_bank, src_lengths = self._run_encoder(batch)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 588, in _run_encoder
    def _run_encoder(self, batch):
========== End debug info ==========
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
ERROR FROM offset=34 filename /home/jiayisun/opennmt-py/onmt/encoders/encoder.py 41 AssertionError
ERROR FROM offset=8 filename /home/jiayisun/opennmt-py/onmt/encoders/transformer.py 119 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 182, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 683, in _translate_batch_with_strategy
    src, enc_states, memory_bank, src_lengths = self._run_encoder(batch)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 592, in _run_encoder
    enc_states, memory_bank, src_lengths = self.model.encoder(
  File "/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/jiayisun/opennmt-py/onmt/encoders/transformer.py", line 117, in forward
    def forward(self, src, lengths=None):
========== End debug info ==========
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
ERROR FROM offset=34 filename /home/jiayisun/opennmt-py/onmt/encoders/encoder.py 41 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/functions.py", line 64, in call_function
    return tx.inline_user_function_return(
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 223, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1098, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 1140, in inline_call_
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 683, in _translate_batch_with_strategy
    src, enc_states, memory_bank, src_lengths = self._run_encoder(batch)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 592, in _run_encoder
    enc_states, memory_bank, src_lengths = self.model.encoder(
  File "/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/jiayisun/opennmt-py/onmt/encoders/transformer.py", line 119, in forward
    self._check_args(src, lengths)
  File "/home/jiayisun/opennmt-py/onmt/encoders/encoder.py", line 37, in _check_args
    def _check_args(self, src, lengths=None, hidden=None):
========== End debug info ==========
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
========== TorchDynamo Stack Trace ==========
Traceback (most recent call last):
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 158, in _convert_frame_assert
    code = transform_code_object(frame.f_code, transform)
  File "/home/jiayisun/torchdynamo/torchdynamo/bytecode_transformation.py", line 284, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jiayisun/torchdynamo/torchdynamo/convert_frame.py", line 134, in transform
    tracer.run()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 274, in run
    and self.step()
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 252, in step
    getattr(self, inst.opname)(inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 140, in wrapper
    return inner_fn(self, inst)
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 543, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jiayisun/torchdynamo/torchdynamo/symbolic_convert.py", line 191, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 213, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/builtin.py", line 354, in call_next
    val, next_iter = arg.next_variables()
  File "/home/jiayisun/torchdynamo/torchdynamo/variables/lists.py", line 309, in next_variables
    assert self.mutable_local
AssertionError
========== Exception (above) while processing ==========
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 683, in _translate_batch_with_strategy
    src, enc_states, memory_bank, src_lengths = self._run_encoder(batch)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 592, in _run_encoder
    enc_states, memory_bank, src_lengths = self.model.encoder(
  File "/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/jiayisun/opennmt-py/onmt/encoders/transformer.py", line 119, in forward
    self._check_args(src, lengths)
  File "/home/jiayisun/opennmt-py/onmt/encoders/encoder.py", line 41, in _check_args
    aeq(n_batch, n_batch_)
  File "/home/jiayisun/opennmt-py/onmt/utils/misc.py", line 34, in aeq
    def aeq(*args):
========== End debug info ==========
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
my_compiler() called with FX graph:
Traceback (most recent call last):
  File "onmt/bin/translate.py", line 52, in <module>
    main()
  File "onmt/bin/translate.py", line 48, in main
    translate(opt)
  File "onmt/bin/translate.py", line 25, in translate
    translator.translate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 387, in translate
    batch_data = self.translate_batch(batch, data.src_vocabs, attn_debug)
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 585, in translate_batch
    return self._translate_batch_with_strategy(batch, src_vocabs,
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 708, in _translate_batch_with_strategy
    log_probs, attn = self._decode_and_generate(
  File "/home/jiayisun/opennmt-py/onmt/translate/translator.py", line 603, in _decode_and_generate
    def _decode_and_generate(
  File "/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/jiayisun/opennmt-py/onmt/decoders/transformer.py", line 289, in forward
    def forward(self, tgt, memory_bank, step=None, **kwargs):
  File "/home/jiayisun/opennmt-py/onmt/decoders/transformer.py", line 289, in forward
    def forward(self, tgt, memory_bank, step=None, **kwargs):
  File "/home/jiayisun/anaconda3/envs/dynamo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/jiayisun/opennmt-py/onmt/decoders/transformer.py", line 75, in forward
    def forward(self, *args, **kwargs):
KeyError: 'with_align'

After modifying some model code, seems to solve the "KeyError: 'with_align'" , but after that, I ran into a core dumped error， and this error seems to be related to guards, could you please help me understand this error?
Traceback (most recent call last):
  File "<string>", line 3, in <lambda>
KeyError: 'layer_0'
ERROR RUNNING GUARDS _recursive_map /home/jiayisun/opennmt-py/onmt/decoders/transformer.py:274
  ___guarded_code.valid and
  ___check_tensors(struct['layer_0']['self_keys'], struct['layer_1']['self_keys'], struct['layer_2']['self_keys'], struct['layer_0']['memory_keys'], struct['layer_0']['self_values'], struct['layer_1']['memory_keys'], struct['layer_1']['self_values'], struct['layer_2']['memory_keys'], struct['layer_2']['self_values'], struct['layer_0']['memory_values'], struct['layer_1']['memory_values'], struct['layer_2']['memory_values'])
NULL ERROR: torchdynamo/_eval_frame.c:190
run.sh: line 13: 3027069 Aborted                 (core dumped) python onmt/bin/translate.py --model checkpoint_step_250000.pt --src valid_part.en --shard_size 1000000 --output valid.en.out --beam_size 4 --max_length 200 --length_penalty "wu" --alpha 0.6 --coverage_penalty "wu" --beta 0.6 --batch_size 1 --report_time --fp32

Hi @jansel, this error happens when we try to add `torchdynamo` support to [OpenNMT-py](https://github.com/OpenNMT/OpenNMT-py). 

- We haven't added any GraphModule optimization pass yet. The only thing added is the context of `with torchdynamo.optimize(my_compiler)`. Do we have any plan to support OpenNMT?
- From the error message, it looks like `InstructionTranslatorBase` has invoke `inline_call_` and trigger the `tracer.run()` again. Finally, the `assert self.mutable_local` happens in `variables/lists.py->next_variables`.

@jiayisunx the key error from the trace you included is:
```
ERROR FROM offset=18 filename /home/jiayisun/opennmt-py/onmt/utils/misc.py 39 AssertionError
```

Not sure what version of onmt you are using, but seems to be in this function:
https://github.com/OpenNMT/OpenNMT-py/blob/fc3409ce08785915728dac9e9c9fb49f73027917/onmt/utils/misc.py#L46

Based on what that function is doing and the error, my guess is the following patch will fix it:
```diff
diff --git a/torchdynamo/symbolic_convert.py b/torchdynamo/symbolic_convert.py
index d73bcbc..4d832ba 100644
--- a/torchdynamo/symbolic_convert.py
+++ b/torchdynamo/symbolic_convert.py
@@ -1148,6 +1148,7 @@ class InliningInstructionTranslator(InstructionTranslatorBase):
             assert tracer.symbolic_result.as_python_constant() is None
             return ListIteratorVariable(
                 tracer.generated_items,
+                mutable_local=MutableLocal(),
                 **VariableTracker.propagate(tracer.symbolic_result),
             )
         else:
```

This tells TorchDynamo that it is safe to mutate that `ListIteratorVariable`, because it was defined locally and doesn't escape the frame.

If this patch doesn't work:

1) This error is coming from validating inputs and asserting in `_check_args`.  You could try commenting out this line:
https://github.com/OpenNMT/OpenNMT-py/blob/fc3409ce08785915728dac9e9c9fb49f73027917/onmt/encoders/transformer.py#L130
which should work around the error

2) Once you have confirmed that the issue is in `_check_args`, it would be helpful to produce a minimal repro, so we can fix the underlying issue.  Perhaps pull `_check_args()` out of onmt and create a small test case that only does the arg validation.  Keep cutting it down to be as small as possible.  That way we can add it as a unit test to TorchDynamo.

3) You could turn this into a graph break by replacing the `assert` with a call to [unimplemented](https://github.com/facebookresearch/torchdynamo/blob/1bc7be4de849aeb3aacee1259bfb593fce79a137/torchdynamo/exc.py#L42) this will gracefully break the graph on `_check_args()`.
Thanks @jansel. After applying your patch, it fixes the first error we met. But after that, we meet another `core-dump` issue. 
It looks like a `guard error`. But why it triggers `core-dump` instead of `re-compile`?
```
Traceback (most recent call last):
  File "<string>", line 3, in <lambda>
KeyError: 'layer_0'
ERROR RUNNING GUARDS _recursive_map /home/lesliefang/pytorch_1_7_1/TorchDynamo/opennmt/opennmt-py/onmt/decoders/transformer.py:274
  ___guarded_code.valid and
  ___check_tensors(struct['layer_0']['self_keys'], struct['layer_1']['self_keys'], struct['layer_2']['self_keys'], struct['layer_0']['memory_keys'], struct['layer_0']['self_values'], struct['layer_1']['memory_keys'], struct['layer_1']['self_values'], struct['layer_2']['memory_keys'], struct['layer_2']['self_values'], struct['layer_0']['memory_values'], struct['layer_1']['memory_values'], struct['layer_2']['memory_values'])
NULL ERROR: torchdynamo/_eval_frame.c:190
run.sh: line 13: 13720 Aborted                 (core dumped) python onmt/bin/translate.py --model checkpoint_step_250000.pt --src valid_part.en --shard_size 1000000 --output valid.en.out --beam_size 4 --max_length 200 --length_penalty "wu" --alpha 0.6 --coverage_penalty "wu" --beta 0.6 --batch_size 1 --report_time --fp32
``` 
@leslie-fang-intel 

That looks like there is a missing [DICT_KEYS](https://github.com/facebookresearch/torchdynamo/blob/d6070992acb9677353eb8258843b9a05858dd244/torchdynamo/guards.py#L250) guard on the `struct` variable.  The guard is reading `struct['layer_0']` without first checking it exists.

Could you try to make a more minimal repro so we can fix the bug?  Perhaps take `_recursive_map` (transformer.py:274) then call it with two different dictionaries with different keys.
Hi @jansel, we have build a case to reproduce similar error.
```
from typing import List
import torch
import torchdynamo

def _recursive_map(struct, batch_dim=0):
    for k, v in struct.items():
        if v is not None:
            if isinstance(v, dict):
                _recursive_map(v)
            else:
                struct[k] = v

def toy_example(a, b, v):
    x = a / (torch.abs(a) + 1)
    if b.sum() < 0:
        b = b * -1
    if v is not None:
        _recursive_map(v)
    return x * b

def my_compiler(gm: torch.fx.GraphModule, example_inputs: List[torch.Tensor]):
    print("my_compiler() called with FX graph:")
    gm.graph.print_tabular()
    return gm.forward  # return a python callable

with torchdynamo.optimize(my_compiler):
    #for v in [None, {"layer0": torch.randn(10)}, {"layer1": {"memory_keys":torch.randn(10)}}]:
    toy_example(torch.randn(10), torch.randn(10), {"layer0": {"memory_keys":torch.randn(10)}})
```
The error message is:
```
Traceback (most recent call last):
  File "<string>", line 3, in <lambda>
KeyError: 'layer0'
ERROR RUNNING GUARDS _recursive_map test.py:5
  ___guarded_code.valid and
  ___check_tensors(struct['layer0']['memory_keys'])
NULL ERROR: torchdynamo/_eval_frame.c:190
Aborted (core dumped)


```

Resolving patch:
diff --git a/torchdynamo/symbolic_convert.py b/torchdynamo/symbolic_convert.py
--- a/torchdynamo/symbolic_convert.py
+++ b/torchdynamo/symbolic_convert.py
@@ -501,6 +501,7 @@ def COMPARE_OP(self, inst):
                     BaseListVariable,
                     UserDefinedVariable,
                     BaseUserFunctionVariable,
+                    ConstDictVariable,
                 ),
             )
             and isinstance(right, ConstantVariable)
@@ -1031,7 +1032,9 @@ def __init__(
 
         # TODO(jansel): figure out why the following is needed for detectron2_maskrcnn
         for val in self.symbolic_locals.values():
-            if isinstance(val, (ListIteratorVariable, BaseListVariable)):
+            if isinstance(
+                val, (ListIteratorVariable, BaseListVariable, ConstDictVariable)
+            ):
                 self.output.guards.update(val.guards)
 
         self._freevars_ids = dict()
@@ -1160,6 +1163,7 @@ def inline_call_(parent, func, args, kwargs):
             assert tracer.symbolic_result.as_python_constant() is None
             return ListIteratorVariable(
                 tracer.generated_items,
+                mutable_local=MutableLocal(),
                 **VariableTracker.propagate(tracer.symbolic_result),
             )
         else:
@@ -1188,8 +1192,6 @@ def __init__(
         )
         self.symbolic_result = None
         self.closure_cells = closure_cells
-        # self.funcvar = funcvar
-        # self.parent = parent
 
     def STORE_DEREF(self, inst):
         if inst.argval in self.closure_cells:


Test patch:
diff --git a/tests/test_repros.py b/tests/test_repros.py
--- a/tests/test_repros.py
+++ b/tests/test_repros.py
@@ -925,3 +925,29 @@ def test_reformer_sorting(self):
             )
         self.assertEqual(cnt.frame_count, 1)
         self.assertEqual(cnt.op_count, ifdyn(28, 14))
+
+    def test_recursive_map(self):
+        # https://github.com/facebookresearch/torchdynamo/issues/132
+        def _recursive_map(struct, batch_dim=0):
+            for k, v in struct.items():
+                if v is not None:
+                    if isinstance(v, dict):
+                        _recursive_map(v)
+                    else:
+                        struct[k] = v
+
+        def toy_example(a, b, v):
+            x = a / (torch.abs(a) + 1)
+            if v is not None:
+                _recursive_map(v)
+            return x * b
+
+        cnt = torchdynamo.testing.CompileCounter()
+        with torchdynamo.optimize(cnt):
+            toy_example(
+                torch.randn(10),
+                torch.randn(10),
+                {"layer0": {"memory_keys": torch.randn(10)}},
+            )
+        self.assertEqual(cnt.frame_count, 1)
+        self.assertEqual(cnt.op_count, 4)