Repository: pytorch
Issue: [dynamo] Graph breaks from copy.deepcopy
Labels: feature,triaged,oncall: pt2,module: dynamo,module: graph breaks

A few models in our benchmark suite have graph breaks calling `copy.deepcopy()`.
1) maml deepcopies a nn.Module in order to do online finetuning
2) hf_T5_generate deepcopies a config object (UserDefinedObjectVariable) so that it can make changes to the config locally
3) In theory one could also deepcopy python structures or tensors, but I haven't seen that yet -- this would be pretty easy to handle by rewriting to torch.clone()

To repro the hf_T5_generate ones, first rebase onto #115120 (if it hasn't been landed yet) then run:
```
$ ./benchmarks/dynamo/torchbench.py --performance --coverage --inference --dynamic-shapes --stats --only hf_T5_generate
loading model: 0it [00:04, ?it/s]
cuda eval  hf_T5_generate                     
[2023-12-04 17:17:34,652] torch._dynamo.convert_frame: [WARNING] torch._dynamo hit config.cache_size_limit (8)
[2023-12-04 17:17:34,652] torch._dynamo.convert_frame: [WARNING]    function: '__call__' (/home/jansel/conda/envs/pytorch/lib/python3.10/site-packages/transformers/generation/stopping_criteria.py:125)
[2023-12-04 17:17:34,652] torch._dynamo.convert_frame: [WARNING]    last reason: ___guarded_code.valid
[2023-12-04 17:17:34,652] torch._dynamo.convert_frame: [WARNING] To log all recompilation reasons, use TORCH_LOGS="recompiles".
[2023-12-04 17:17:34,652] torch._dynamo.convert_frame: [WARNING] To diagnose recompilation issues, see https://pytorch.org/docs/master/compile/troubleshooting.html.
STAGE:2023-12-04 17:17:43 570524:570524 ActivityProfilerController.cpp:314] Completed Stage: Warm Up
STAGE:2023-12-04 17:17:46 570524:570524 ActivityProfilerController.cpp:320] Completed Stage: Collection
STAGE:2023-12-04 17:17:47 570524:570524 ActivityProfilerController.cpp:324] Completed Stage: Post Processing
 35/ 37 +0 frames  15s 15 graphs 515 graph calls    0/-514 =   0% ops   0% time
STATS frames
  ('total', 37)
  ('ok', 35)
STATS graph_break
  ('copy.deepcopy UserDefinedObjectVariable(GenerationConfig)', 3)
  ('Tensor.item', 1)
  ('Dynamic control flow is not supported at the moment. Please use functorch.experimental.control_flow.cond to explicitly capture the control flow. For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#cond-operands', 1)
STATS inline_call
  ('copy.deepcopy UserDefinedObjectVariable(GenerationConfig)', 2)
  ('Tensor.item', 1)
  ('Dynamic control flow is not supported at the moment. Please use functorch.experimental.control_flow.cond to explicitly capture the control flow. For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#cond-operands', 1)
STATS stats
  ('calls_captured', 2377)
  ('unique_graphs', 15)
STATS unimplemented
  ('Tensor.item', 1)
  ('cache_size_limit reached', 1)
  ('generator', 1)
```

To debug further, one can do:
```
env BREAK='copy.deepcopy UserDefinedObjectVariable(GenerationConfig)' ./benchmarks/dynamo/torchbench.py --performance --coverage --inference --dynamic-shapes --stats --only hf_T5_generate
```
which will turn the graph break into an error and see the user code it is coming from:
```
...
from user code:
   File "/home/jansel/pytorch/./benchmarks/dynamo/torchbench.py", line 532, in forward_pass
    return mod(*inputs)
  File "/home/jansel/pytorch/torch/nn/modules/module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/jansel/torchbenchmark/torchbenchmark/util/framework/huggingface/model_factory.py", line 221, in forward
    return self.model.generate(inputs, self.generation_config)
  File "/home/jansel/pytorch/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/jansel/conda/envs/pytorch/lib/python3.10/site-packages/transformers/generation/utils.py", line 1426, in generate
    generation_config = copy.deepcopy(generation_config)
```



cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @zou3519 @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @aakhundov @kadeng