17 #ifndef CAFFE2_OPERATORS_FUNHASH_OP_H_ 18 #define CAFFE2_OPERATORS_FUNHASH_OP_H_ 22 #include "caffe2/core/context.h" 23 #include "caffe2/core/operator.h" 24 #include "caffe2/utils/math.h" 26 #define SIGN_MAGIC 0x9e3779b97f4a7c15 27 #define INDEX_MAGIC 0xf39cc0605cedc834 33 template <
typename T,
class Context>
36 USE_OPERATOR_CONTEXT_FUNCTIONS;
40 OperatorBase::GetSingleArgument<int64_t>(
"num_outputs", -1)),
42 OperatorBase::GetSingleArgument<int64_t>(
"num_segments", -1)),
43 seed_(OperatorBase::GetSingleArgument<uint64_t>(
"seed", 0)) {
46 "Argument `num_outputs` is missing.");
48 adaptive_ = (InputSize() == 5);
51 bool RunOnDevice()
override {
52 const auto& val =
Input(0);
53 const auto& key =
Input(1);
54 const auto& seg =
Input(2);
55 const auto& weight =
Input(3);
57 int64_t num_alpha = 1;
59 const auto& alpha =
Input(4);
60 num_alpha = alpha.size(0);
63 const auto* seg_data = seg.template data<int>();
65 int64_t num_weight = weight.size(0);
66 int64_t num_nz_ent = seg.size(0);
68 int64_t n_segments = num_segments_;
69 if (num_segments_ == -1) {
70 for (int64_t i = 0; i < num_nz_ent; ++i) {
71 if (seg_data[i] > n_segments) {
72 n_segments = seg_data[i];
78 auto* output = Output(0, {n_segments, num_outputs_}, at::dtype<T>());
80 T* output_data = output->template mutable_data<T>();
82 memset(output_data, 0,
sizeof(
T) * n_segments * num_outputs_);
84 const auto* weight_data = weight.template data<T>();
85 const auto* alpha_data = adaptive_ ?
Input(4).template data<T>() : 0;
86 const auto* val_data = val.template data<T>();
87 const auto* key_data = key.template data<int64_t>();
89 for (int64_t j = 0; j < num_nz_ent; ++j) {
90 int64_t cur_seg = seg_data[j];
91 int64_t cur_key = key_data[j];
92 T cur_val = val_data[j];
93 int64_t output_stride = cur_seg * num_outputs_;
94 for (int64_t i = 0; i < num_outputs_; ++i) {
96 for (int64_t k = 0; k < num_alpha; ++k) {
104 hash_data[0] = cur_key;
108 hash_data[3] = INDEX_MAGIC;
109 hash = XXH64(hash_data.data(), hash_data.size(), seed_);
110 int64_t index = hash % num_weight;
112 T cur_weight = weight_data[index];
114 hash_data[3] = SIGN_MAGIC;
115 hash = XXH64(hash_data.data(), hash_data.size(), seed_);
117 cur_weight = -cur_weight;
122 sum += cur_weight * alpha_data[k];
127 output_data[output_stride + i] += sum * cur_val;
135 int64_t num_outputs_;
136 int64_t num_segments_;
138 std::array<uint64_t, 4> hash_data;
142 template <
typename T,
class Context>
145 USE_OPERATOR_CONTEXT_FUNCTIONS;
149 OperatorBase::GetSingleArgument<int64_t>(
"num_outputs", -1)),
150 seed_(OperatorBase::GetSingleArgument<uint64_t>(
"seed", 0)) {
151 adaptive_ = (InputSize() == 6);
154 bool RunOnDevice()
override {
155 const auto& grad_out =
Input(0);
156 const auto& val =
Input(1);
157 const auto& key =
Input(2);
158 const auto& seg =
Input(3);
159 const auto& weight =
Input(4);
161 int64_t num_alpha = 1;
162 T* grad_alpha_data = 0;
165 const auto& alpha =
Input(5);
166 num_alpha = alpha.size(0);
168 auto* grad_alpha = Output(1, alpha.sizes(), at::dtype<T>());
169 grad_alpha_data = grad_alpha->template mutable_data<T>();
170 memset(grad_alpha_data, 0,
sizeof(
T) * num_alpha);
173 const auto* seg_data = seg.template data<int>();
175 int64_t num_weight = weight.size(0);
176 int64_t num_nz_ent = seg.size(0);
178 auto* grad_weight = Output(0, weight.sizes(), at::dtype<T>());
179 T* grad_weight_data = grad_weight->template mutable_data<T>();
181 const auto* grad_out_data = grad_out.template data<T>();
182 const auto* weight_data = weight.template data<T>();
183 const auto* alpha_data = adaptive_ ?
Input(5).template data<T>() : 0;
184 const auto* val_data = val.template data<T>();
185 const auto* key_data = key.template data<int64_t>();
187 memset(grad_weight_data, 0,
sizeof(
T) * num_weight);
189 for (int64_t j = 0; j < num_nz_ent; ++j) {
190 int64_t cur_seg = seg_data[j];
191 int64_t cur_key = key_data[j];
192 T cur_val = val_data[j];
193 int64_t grad_out_stride = cur_seg * num_outputs_;
194 for (int64_t i = 0; i < num_outputs_; ++i) {
195 T grad_out_scale = grad_out_data[grad_out_stride + i] * cur_val;
196 for (int64_t k = 0; k < num_alpha; ++k) {
198 hash_data[0] = cur_key;
202 hash_data[3] = INDEX_MAGIC;
203 hash = XXH64(hash_data.data(), hash_data.size(), seed_);
204 int64_t index = hash % num_weight;
206 T cur_grad_out_scale = grad_out_scale;
208 hash_data[3] = SIGN_MAGIC;
209 hash = XXH64(hash_data.data(), hash_data.size(), seed_);
211 cur_grad_out_scale = -cur_grad_out_scale;
216 grad_alpha_data[k] += cur_grad_out_scale * weight_data[index];
217 grad_weight_data[index] += alpha_data[k] * cur_grad_out_scale;
219 grad_weight_data[index] += cur_grad_out_scale;
228 int64_t num_outputs_;
230 std::array<uint64_t, 4> hash_data;
236 #endif // CAFFE2_OPERATORS_FUNHASH_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position 'idx' for this operator. ...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.