pytorch/caffe2/operators/loss_op.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

78 lines
1.8 KiB
C++

#include "caffe2/operators/loss_op.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(AveragedLoss, AveragedLoss<float, CPUContext>);
REGISTER_CPU_OPERATOR(AveragedLossGradient,
AveragedLossGradient<float, CPUContext>);
OPERATOR_SCHEMA(AveragedLoss)
.NumInputs(1)
.NumOutputs(1)
.ScalarType(TensorProto::FLOAT)
.SetDoc(R"DOC(
The *AveragedLoss* op takes a single 1-D input tensor *input* and returns a single output float value *output*. The output represents the average of the values in *input*. This op is commonly used for averaging losses, hence the name, however it does not exclusively operate on losses.
Github Links:
- https://github.com/caffe2/caffe2/blob/master/caffe2/operators/loss_op.h
- https://github.com/caffe2/caffe2/blob/master/caffe2/operators/loss_op.cc
<details>
<summary> <b>Example</b> </summary>
**Code**
```
workspace.ResetWorkspace()
op = core.CreateOperator(
"AveragedLoss",
["input"],
["output"],
)
workspace.FeedBlob("input", np.array([8, 10, 12]).astype(np.float32))
print("input:\n", workspace.FetchBlob("input"))
workspace.RunOperatorOnce(op)
print("output: \n", workspace.FetchBlob("output"))
```
**Result**
```
input:
[ 8. 10. 12.]
output:
10.0
```
</details>
)DOC")
.Input(0, "input", "The input data as Tensor")
.Output(0, "output", "The output tensor of size 1 containing the averaged value.");
OPERATOR_SCHEMA(AveragedLossGradient).NumInputs(2).NumOutputs(1);
class GetAveragedLossGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
vector<OperatorDef> GetGradientDefs() override {
return SingleGradientDef(
"AveragedLossGradient", "",
vector<string>{I(0), GO(0)},
vector<string>{GI(0)});
}
};
REGISTER_GRADIENT(AveragedLoss, GetAveragedLossGradient);
} // namespace caffe2