|
| 1 | +#include <algorithm> |
| 2 | +#include <cmath> |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "caffe/layers/embedding_lookup_layer.hpp" |
| 6 | +#include "caffe/util/math_functions.hpp" |
| 7 | + |
| 8 | +namespace caffe { |
| 9 | + |
| 10 | +template <typename Dtype> |
| 11 | +void EmbeddingLookupLayer<Dtype>::LayerSetUp( |
| 12 | + const vector<Blob<Dtype> *> &bottom, const vector<Blob<Dtype> *> &top) { |
| 13 | + const EmbeddingLookupParameter &embedding_lookup_param = |
| 14 | + this->layer_param_.embedding_lookup_param(); |
| 15 | + ids.clear(); |
| 16 | + std::copy(embedding_lookup_param.ids().begin(), |
| 17 | + embedding_lookup_param.ids().end(), std::back_inserter(ids)); |
| 18 | + ids_shape.clear(); |
| 19 | + std::copy(embedding_lookup_param.ids_shape().begin(), |
| 20 | + embedding_lookup_param.ids_shape().end(), |
| 21 | + std::back_inserter(ids_shape)); |
| 22 | + p_strategy = embedding_lookup_param.partition_strategy(); |
| 23 | + |
| 24 | + // if max_norm = None, define None -> none_value = 9999999999 |
| 25 | + max_norm = embedding_lookup_param.max_norm(); |
| 26 | + // CHECK PART |
| 27 | + const int num_axes = bottom[0]->num_axes(); |
| 28 | + vector<int> bottom_shape = bottom[0]->shape(); |
| 29 | + for (int i = 1; i < bottom.size(); ++i) { |
| 30 | + CHECK_EQ(num_axes, bottom[i]->num_axes()) |
| 31 | + << "All inputs must have the same #axes."; |
| 32 | + for (int j = 1; j < num_axes; ++j) { |
| 33 | + CHECK_EQ(bottom_shape[j], bottom[i]->shape(j)) |
| 34 | + << "Dimension " << j - 1 << " in both shapes must be equal, but are " |
| 35 | + << bottom_shape[j] << " and " << bottom[i]->shape(j); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +template <typename Dtype> |
| 41 | +void EmbeddingLookupLayer<Dtype>::Reshape(const vector<Blob<Dtype> *> &bottom, |
| 42 | + const vector<Blob<Dtype> *> &top) { |
| 43 | + vector<int> top_shape = bottom[0]->shape(); |
| 44 | + top_shape.erase(top_shape.begin()); |
| 45 | + top_shape.insert(top_shape.begin(), ids_shape.begin(), ids_shape.end()); |
| 46 | + top[0]->Reshape(top_shape); |
| 47 | +} |
| 48 | + |
| 49 | +template <typename Dtype> |
| 50 | +void EmbeddingLookupLayer<Dtype>::Forward_cpu( |
| 51 | + const vector<Blob<Dtype> *> &bottom, const vector<Blob<Dtype> *> &top) { |
| 52 | + Dtype *top_data = top[0]->mutable_cpu_data(); |
| 53 | + const int copy_num = bottom[0]->count(1); |
| 54 | + // define none_value |
| 55 | + const float none_value = 9999999999; |
| 56 | + // for one params |
| 57 | + if (bottom.size() == 1) { |
| 58 | + const Dtype *bottom_data = bottom[0]->cpu_data(); |
| 59 | + for (int i = 0; i < ids.size(); ++i) { |
| 60 | + CHECK_GE(ids[i], 0) << "ids[" << i << "] = " << ids[i] << " is not in " |
| 61 | + << "[0, " << bottom[0]->shape(0) << ")."; |
| 62 | + CHECK_LT(ids[i], bottom[0]->shape(0)) |
| 63 | + << "ids[" << i << "] = " << ids[i] << " is not in " |
| 64 | + << "[0, " << bottom[0]->shape(0) << ")."; |
| 65 | + const int t_offset = i * copy_num; |
| 66 | + const int b_offset = ids[i] * copy_num; |
| 67 | + caffe_copy(copy_num, bottom_data + b_offset, top_data + t_offset); |
| 68 | + const auto normt = std::sqrt(caffe_cpu_dot( |
| 69 | + copy_num, bottom_data + b_offset, bottom_data + b_offset)); |
| 70 | + if (max_norm != none_value && (normt > max_norm)) { |
| 71 | + const auto alpha = max_norm / normt; |
| 72 | + caffe_scal(copy_num, alpha, top_data + t_offset); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + // for multiple params |
| 77 | + else { |
| 78 | + for (int i = 0; i < ids.size(); ++i) { |
| 79 | + // strategy = mod |
| 80 | + if (p_strategy == "mod") { |
| 81 | + const int bottom_num = ids[i] % bottom.size(); |
| 82 | + const int row_num = ids[i] / bottom.size(); |
| 83 | + CHECK_GE(row_num, 0) << "ids[" << i << "] is not in " |
| 84 | + << "[0, " << bottom[bottom_num]->shape(0) |
| 85 | + << ") for params[" << bottom_num << "]"; |
| 86 | + CHECK_LT(row_num, bottom[bottom_num]->shape(0)) |
| 87 | + << "ids[" << i << "] is not in " |
| 88 | + << "[0, " << bottom[bottom_num]->shape(0) << ") for params[" |
| 89 | + << bottom_num << "]"; |
| 90 | + const Dtype *bottom_data = bottom[bottom_num]->cpu_data(); |
| 91 | + const int t_offset = i * copy_num; |
| 92 | + const int b_offset = row_num * copy_num; |
| 93 | + caffe_copy(copy_num, bottom_data + b_offset, top_data + t_offset); |
| 94 | + // max_norm part |
| 95 | + const auto normt = std::sqrt(caffe_cpu_dot( |
| 96 | + copy_num, bottom_data + b_offset, bottom_data + b_offset)); |
| 97 | + if (max_norm != none_value && (normt > max_norm)) { |
| 98 | + const auto alpha = max_norm / normt; |
| 99 | + caffe_scal(copy_num, alpha, top_data + t_offset); |
| 100 | + } |
| 101 | + } |
| 102 | + // strategy = div |
| 103 | + if (p_strategy == "div") { |
| 104 | + int all_idx = 0; |
| 105 | + for (int i = 0; i < bottom.size(); ++i) { |
| 106 | + all_idx += bottom[i]->shape(0); |
| 107 | + } |
| 108 | + const int a = all_idx / bottom.size(); |
| 109 | + const int b = all_idx % bottom.size(); |
| 110 | + const int bottom_num = (ids[i] < b * (a + 1)) |
| 111 | + ? (ids[i] / (a + 1)) |
| 112 | + : (b + (ids[i] - b * (a + 1)) / a); |
| 113 | + const int row_num = (ids[i] < b * (a + 1)) |
| 114 | + ? (ids[i] % (a + 1)) |
| 115 | + : ((ids[i] - b * (a + 1)) % a); |
| 116 | + CHECK_GE(row_num, 0) << "ids[" << i << "] is not in " |
| 117 | + << "[0, " << bottom[bottom_num]->shape(0) |
| 118 | + << ") for params[" << bottom_num << "]"; |
| 119 | + CHECK_LT(row_num, bottom[bottom_num]->shape(0)) |
| 120 | + << "ids[" << i << "] is not in " |
| 121 | + << "[0, " << bottom[bottom_num]->shape(0) << ") for params[" |
| 122 | + << bottom_num << "]"; |
| 123 | + const Dtype *bottom_data = bottom[bottom_num]->cpu_data(); |
| 124 | + const int t_offset = i * copy_num; |
| 125 | + const int b_offset = row_num * copy_num; |
| 126 | + caffe_copy(copy_num, bottom_data + b_offset, top_data + t_offset); |
| 127 | + // max_norm part |
| 128 | + const auto normt = std::sqrt(caffe_cpu_dot( |
| 129 | + copy_num, bottom_data + b_offset, bottom_data + b_offset)); |
| 130 | + if (max_norm != none_value && (normt > max_norm)) { |
| 131 | + const auto alpha = max_norm / normt; |
| 132 | + caffe_scal(copy_num, alpha, top_data + t_offset); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +INSTANTIATE_CLASS(EmbeddingLookupLayer); |
| 140 | +REGISTER_LAYER_CLASS(EmbeddingLookup); |
| 141 | + |
| 142 | +} // namespace caffe |
0 commit comments