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  KeySplitOp(const OperatorDef& operator_def, Workspace* ws)
16  : Operator<Context>(operator_def, ws),
17  categorical_limit_(
18  OperatorBase::GetSingleArgument<int>("categorical_limit", 0)) {
19  CAFFE_ENFORCE_GT(categorical_limit_, 0);
20  }
21 
22  bool RunOnDevice() override {
23  auto& keys = Input(0);
24  int N = keys.size();
25  const T* keys_data = keys.template data<T>();
26  std::vector<int> counts(categorical_limit_);
27  std::vector<int*> eids(categorical_limit_);
28  for (int k = 0; k < categorical_limit_; k++) {
29  counts[k] = 0;
30  }
31  for (int i = 0; i < N; i++) {
32  int k = keys_data[i];
33  CAFFE_ENFORCE_GT(categorical_limit_, k);
34  CAFFE_ENFORCE_GE(k, 0);
35  counts[k]++;
36  }
37  for (int k = 0; k < categorical_limit_; k++) {
38  auto* eid = Output(k);
39  eid->Resize(counts[k]);
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
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...