Caffe2 - C++ API
A deep learning, cross platform ML framework
key_split_ops.h
1 #pragma once
2 
3 #include <vector>
4 
5 #include "caffe2/core/context.h"
6 #include "caffe2/core/operator.h"
7 #include "caffe2/utils/math.h"
8 
9 namespace caffe2 {
10 template <typename T, class Context>
11 class KeySplitOp : public Operator<Context> {
12  public:
13  USE_OPERATOR_CONTEXT_FUNCTIONS;
14 
15  template <class... Args>
16  explicit KeySplitOp(Args&&... args)
17  : Operator<Context>(std::forward<Args>(args)...),
18  categorical_limit_(
19  this->template GetSingleArgument<int>("categorical_limit", 0)) {
20  CAFFE_ENFORCE_GT(categorical_limit_, 0);
21  }
22 
23  bool RunOnDevice() override {
24  auto& keys = Input(0);
25  int N = keys.numel();
26  const T* keys_data = keys.template data<T>();
27  std::vector<int> counts(categorical_limit_);
28  std::vector<int*> eids(categorical_limit_);
29  for (int k = 0; k < categorical_limit_; k++) {
30  counts[k] = 0;
31  }
32  for (int i = 0; i < N; i++) {
33  int k = keys_data[i];
34  CAFFE_ENFORCE_GT(categorical_limit_, k);
35  CAFFE_ENFORCE_GE(k, 0);
36  counts[k]++;
37  }
38  for (int k = 0; k < categorical_limit_; k++) {
39  auto* eid = Output(k, {counts[k]}, at::dtype<int>());
40  eids[k] = eid->template mutable_data<int>();
41  counts[k] = 0;
42  }
43  for (int i = 0; i < N; i++) {
44  int k = keys_data[i];
45  eids[k][counts[k]++] = i;
46  }
47  return true;
48  }
49 
50  private:
51  int categorical_limit_;
52 };
53 } // namespace caffe2
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