Caffe2 - C++ API
A deep learning, cross platform ML framework
enforce_finite_op.h
1 #ifndef CAFFE_OPERATORS_ENFORCE_FINITE_OP_H_
2 #define CAFFE_OPERATORS_ENFORCE_FINITE_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 <class Context>
12 class EnforceFiniteOp final : public Operator<Context> {
13  public:
14  USE_OPERATOR_CONTEXT_FUNCTIONS;
15  template <class... Args>
16  explicit EnforceFiniteOp(Args&&... args)
17  : Operator<Context>(std::forward<Args>(args)...) {}
18 
19  bool RunOnDevice() override {
20  return DispatchHelper<TensorTypes<float, double>>::call(this, Input(0));
21  }
22 
23  template <typename T>
24  bool DoRunWithType();
25 
26  private:
27  Tensor buffer_{CPU};
28 
29  template <typename T>
30  void EnforceOnCPU(const Tensor& input) {
31  const T* input_data = input.template data<T>();
32  auto size = input.numel();
33 
34  for (auto i = 0; i < size; i++) {
35  CAFFE_ENFORCE(
36  std::isfinite(input_data[i]),
37  "Index ",
38  i,
39  " is not finite (e.g., NaN, Inf): ",
40  input_data[i]);
41  }
42  }
43 };
44 
45 } // namespace caffe2
46 
47 #endif // CAFFE_OPERATORS_ENFORCE_FINITE_OP_H_
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position &#39;idx&#39; for this operator. ...
Definition: operator.h:702
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13