Repository: pytorch
Issue: [Caffe2] Successive in-place operators cause RuntimeError of gradient operator versions
Labels: caffe2

## Issue description

Errors are reported when running a network with two successive in-place operators, e.g.
```
FC(x, y)-->ReLU(y, y)-->Dropout(y, y)
Conv(x, y)--> ReLU(y, y)-->UserDefinedInplaceOp(y, y)
```
The error (from the `FC-ReLU-Dropout` case) is as follows:
```
...
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/model_helper.py", line 335, in AddGradientOperators
    self.grad_map = self.net.AddGradientOperators(*args, **kwargs)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 1864, in AddGradientOperators
    self._net.op[skip:], ys)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 1126, in GetBackwardPass
    return ir.GetBackwardPass(ys)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 1001, in GetBackwardPass
    forward_op_idx, all_input_to_grad)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 955, in _GenerateGradientsForForwardOp
    forward_op_idx, gradient_ops, g_output, g_input)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 615, in BuildGradientGenerators
    s, g_output, fwd_op_idx, locally_generated_blobs)
  File "/home/<user_name>/repo/pytorch/build/caffe2/python/core.py", line 558, in CheckGradientOperatorInput
    ) + versionMismatchInfoOut(grad_op_input)
RuntimeError: Gradient operator needs output "gpu_0/fc6" at version 1, but currently we have version 2.
...
```
The problem can be solved if using a different output name for the last operator, e.g. changing from `y` to `z` as follows:
```
FC(x, y)-->ReLU(y, y)-->Dropout(y, z)
Conv(x, y)--> ReLU(y, y)-->UserDefinedInplaceOp(y, z)
```
But this seems a little weird. Note that in Caffe, it is OK to allow ReLU and Dropout to be both in-place operations.

I noticed that the gradient operator of Dropout does not allow in-place computation due to the trick used in generating a random mask. But for my `UserDefinedInplaceOp`, the gradient operator does support in-place gradient computation, which is also declared with `AllowInplace({{0, 0}})` in `OPERATOR_SCHEMA` for both the operator and the gradient operator. **So why does this problem happen? Does this mean one cannot use two consecutive in-place operators with Caffe2? If so, why? If not, what operators are allowed to use in such cases?**

The problem is also encountered in issue: https://github.com/caffe2/caffe2/issues/1017.

BTW, what is the parameter of `AllowInplace` exactly? I see explanations nowhere.

## Code example

One can try to add an in-place `Dropout` operator after the `FC-ReLU` in VGG-16 network and run it to reproduce the error.

## System Info

- PyTorch or Caffe2: **Caffe2**
- How you installed PyTorch (conda, pip, source): **source**
- Build command you used (if compiling from source):
- OS: **Ubuntu 16.04**
- PyTorch version: **(latest clone)**
- Python version: **Python 2.7.15 :: Anaconda, Inc.**
- CUDA/cuDNN version: **8.0/cudnn-8.0-linux-x64-v7**
- GPU models and configuration: **(Titan Xp)**
- GCC version (if compiling from source): **5.4.0**
- CMake version: **3.11.3**
- Versions of any other relevant libraries: