Repository: pytorch
Issue: RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 24 and 195 in dimension 0 at /opt/conda/conda-bld/pytorch_1565272271120/work/aten/src/TH/generic/THTensor.cpp:689
Labels: oncall: jit,module: nn,triaged

I am trying to use Tracing mechanism to trace my python model. Below is my nn.Module:

```
class GeneralizedRCNN(nn.Module):
    """
    Main class for Generalized R-CNN. Currently supports boxes and masks.
    It consists of three main parts:
    - backbone
    - rpn
    - heads: takes the features + the proposals from the RPN and computes
        detections / masks from it.
    """

    def __init__(self, cfg):
        super(GeneralizedRCNN, self).__init__()

        self.backbone = build_backbone(cfg)
        self.rpn = build_rpn(cfg, self.backbone.out_channels)
        self.roi_heads = build_roi_heads(cfg, self.backbone.out_channels)

    def forward(self, images, targets=None):
        """
        Arguments:
            images (list[Tensor] or ImageList): images to be processed
            targets (list[BoxList]): ground-truth boxes present in the image (optional)

        Returns:
            result (list[BoxList] or dict[Tensor]): the output from the model.
                During training, it returns a dict[Tensor] which contains the losses.
                During testing, it returns list[BoxList] contains additional fields
                like `scores`, `labels` and `mask` (for Mask R-CNN models).

        """
        if self.training and targets is None:
            raise ValueError("In training mode, targets should be passed")
        images = to_image_list(images)
        features = self.backbone(images.tensors)
        proposals, proposal_losses = self.rpn(images, features, targets)
        if self.roi_heads:
            print(type(features))
            print(type(proposals))
            print(type(targets))
            x, result, detector_losses = self.roi_heads(features, proposals, targets)
        else:
            # RPN-only models don't have roi_heads
            x = features
            result = proposals
            detector_losses = {}

        if self.training:
            losses = {}
            losses.update(detector_losses)
            losses.update(proposal_losses)
            return losses
        print("result.....")
        print(result[0].bbox.size())
        return result[0].bbox
```

When I execute the file, it does print the last two lines as shown below:

**result…
torch.Size([24, 4])**


Here is my starting code snippet from trace.py file:

**`traced_script_module = torch.jit.trace(torch_model, y)`**


Following is the error that I receive when I execute the trace.py. **Please be noted that error is thrown after it does print everything until the second last line of nn.Module. The bbox is populated and it has valid values.** 

```
Traceback (most recent call last):
File “/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/jit/init.py”, line 595, in run_mod_and_filter_tensor_outputs
outs = wrap_retval(mod(*_clone_inputs(inputs)))
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 24 and 195 in dimension 0 at /opt/conda/conda-bld/pytorch_1565272271120/work/aten/src/TH/generic/THTensor.cpp:689
The above operation failed in interpreter, with the following stack trace:
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/poolers.py(96): convert_to_roi_format
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/poolers.py(110): forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(531): _slow_forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(545): call
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/roi_mask_feature_extractors.py(60): forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(531): _slow_forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(545): call
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/mask_head.py(70): forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(531): _slow_forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(545): call
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/roi_heads.py(39): forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(531): _slow_forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(545): call
/mnt/d/work/MASKRCNN/maskrcnn-benchmark/maskrcnn_benchmark/modeling/detector/generalized_rcnn.py(56): forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(531): _slow_forward
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/nn/modules/module.py(545): call
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/jit/init.py(904): trace_module
/home/fariha/anaconda3/envs/maskrcnn_benchmark/lib/python3.7/site-packages/torch/jit/init.py(772): trace
trace.py(117):
```

cc @suo