Fix SoftmaxOps (#16049)
authorJerry Zhang <jerryzh@fb.com>
Fri, 18 Jan 2019 20:14:34 +0000 (12:14 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 18 Jan 2019 20:30:59 +0000 (12:30 -0800)
commiteea50e91fa6fbcfef59b6a252c5ec5f9400e1ac2
treed07a0ffcefe859bc004484edbbcd39203167eb23
parent3f4bb3d49321cc9b28967f44b5de081459f0e07e
Fix SoftmaxOps (#16049)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/16049

We might see the pattern
```
if (scale_.numel() != N) {
   scale_->Resize(N);
   // set initial value for scale_
}

// In class:
Tensor scale_{CPU};
```
before in the code, where `scale_` is a member variable of Type `caffe2::Tensor`
This pattern actually serves two purposes, if `scale_` is partially initialized with device type but not size, this call will
initialize Tensor with the correct size, or if `scale_` is already initialized with size, it will check whether the size
matches a runtime value `N` and if not it will Resize. To rewrite this we'll do the following:
```
if (!scale_.defined() || scale_.numel() != N) {
  ReinitializeTensor(&scale_, {N}, at::dtype<float>().device(CPU));
  // set initial value for scale_
}

```
There are some variants, if `scale_` is resized to a constant size, we can call `ReinitializeTensor` instead
```
if (scale_.numel() != 1) {
  scale_->Resize(1);
}
```
-->
```
ReinitializeTensor(&scale_, {1}, at::dtype<float>().device(CPU));
```

Normal Resize will be refactored directly into ReinitializeTensor:
```
scale_->Resize(N);
```
-->
```
ReinitializeTensor(&scale_, {N}, at::dtype<float>().device(CPU));
```

Reviewed By: dzhulgakov

Differential Revision: D13667883

fbshipit-source-id: 2c7cb61544b72765b594011b99150eb5a1b50836
caffe2/operators/softmax_op.cc
caffe2/operators/softmax_ops.cu
caffe2/operators/softmax_with_loss_op.cc
caffe2/operators/softmax_with_loss_op.h
caffe2/operators/spatial_softmax_with_loss_op.cc
caffe2/operators/spatial_softmax_with_loss_op.h