Caffe2 - C++ API
A deep learning, cross platform ML framework
spatial_softmax_with_loss_op.h
1 #ifndef SPATIAL_SOFTMAX_WITH_LOSS_OP_H_
2 #define SPATIAL_SOFTMAX_WITH_LOSS_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/core/operator.h"
7 #include "caffe2/utils/math.h"
8 
9 namespace caffe2 {
10 
11 template <typename T, class Context>
12 class SpatialSoftmaxWithLossOp final : public Operator<Context> {
13  public:
14  template <class... Args>
15  explicit SpatialSoftmaxWithLossOp(Args&&... args)
16  : Operator<Context>(std::forward<Args>(args)...),
17  scale_(this->template GetSingleArgument<float>("scale", 1.)),
18  order_(StringToStorageOrder(
19  this->template GetSingleArgument<string>("order", "NCHW"))) {
20  CAFFE_ENFORCE(scale_ >= 0);
21  CAFFE_ENFORCE_EQ(
22  order_, StorageOrder::NCHW, "Only NCHW order is supported right now.");
23  }
24  USE_OPERATOR_CONTEXT_FUNCTIONS;
25 
26  bool RunOnDevice() override;
27 
28  protected:
29  float scale_;
30  StorageOrder order_;
31 
32  Tensor losses_; // Per example loss
33  Tensor rowmax_; // per example row max
34  Tensor weights_; // unignored weights
35  Tensor sum_multiplier_; // Vector of ones for summing via dot prod
36  Tensor total_weight_ptr_;
37  Tensor scratch_{Context::GetDeviceType()};
38 };
39 
40 template <typename T, class Context>
41 class SpatialSoftmaxWithLossGradientOp final : public Operator<Context> {
42  public:
43  template <class... Args>
44  explicit SpatialSoftmaxWithLossGradientOp(Args&&... args)
45  : Operator<Context>(std::forward<Args>(args)...),
46  scale_(this->template GetSingleArgument<float>("scale", 1.)),
47  order_(StringToStorageOrder(
48  this->template GetSingleArgument<string>("order", "NCHW"))),
49  only_loss_(this->template GetSingleArgument<bool>("only_loss", false)) {
50  CAFFE_ENFORCE(scale_ >= 0);
51  CAFFE_ENFORCE_EQ(
52  order_, StorageOrder::NCHW, "Only NCHW order is supported right now.");
53  }
54  USE_OPERATOR_CONTEXT_FUNCTIONS;
55 
56  bool RunOnDevice() override;
57 
58  protected:
59  float scale_;
60  Tensor sum_multiplier_;
61  Tensor weights_; // unignored weights
62  Tensor total_weight_ptr_;
63  StorageOrder order_;
64  bool only_loss_;
65  Tensor scratch_{Context::GetDeviceType()};
66 };
67 
68 } // namespace caffe2
69 
70 #endif // SOFTMAX_WITH_LOSS_OP_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13