optimizer/sparse_tensor.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sparse_tensor.h" | ||
| 2 | |||
| 3 | 44 | uint64_t SparseTensor::concatKeyAndTag(uint64_t key, TAG_TYPE tag) { | |
| 4 | 44 | constexpr int tag_bits = sizeof(TAG_TYPE) * 8; | |
| 5 | 44 | constexpr int shift = (sizeof(uint64_t) * 8) - tag_bits; | |
| 6 | 44 | key &= (~0ULL >> tag_bits); | |
| 7 | 44 | return (static_cast<uint64_t>(tag) << shift) | key; | |
| 8 | } | ||
| 9 | |||
| 10 | 48 | void SparseTensor::init(std::string& name, | |
| 11 | TensorType type, | ||
| 12 | TAG_TYPE tag, | ||
| 13 | std::vector<uint64_t>& shape, | ||
| 14 | BaseKV* kv) { | ||
| 15 | 48 | this->name = name; | |
| 16 | 48 | this->type = type; | |
| 17 | 48 | this->tag = tag; | |
| 18 | 48 | this->shape = shape; | |
| 19 | 48 | this->kv = kv; | |
| 20 | 48 | } | |
| 21 | |||
| 22 | 4 | void SparseTensor::Get(const uint64_t key, std::string& value, unsigned tid) { | |
| 23 | 4 | auto _key = concatKeyAndTag(key, tag); | |
| 24 | 4 | kv->Get(_key, value, tid); | |
| 25 | 4 | } | |
| 26 | |||
| 27 | 20 | void SparseTensor::Put( | |
| 28 | const uint64_t key, const std::string_view& value, unsigned tid) { | ||
| 29 | 20 | auto _key = concatKeyAndTag(key, tag); | |
| 30 | 20 | kv->Put(_key, value, tid); | |
| 31 | 20 | } | |
| 32 | |||
| 33 | 20 | void SparseTensor::BatchGet(const std::vector<uint64_t>& keys, | |
| 34 | std::vector<base::ConstArray<float>>* values, | ||
| 35 | unsigned tid) { | ||
| 36 | 20 | std::vector<uint64_t> hashed_keys; | |
| 37 | 20 | hashed_keys.reserve(keys.size()); | |
| 38 | 40 | for (auto k : keys) { | |
| 39 | 20 | hashed_keys.push_back(concatKeyAndTag(k, tag)); | |
| 40 | } | ||
| 41 | 20 | kv->BatchGet(base::ConstArray<uint64_t>(hashed_keys), values, tid); | |
| 42 | 20 | } | |
| 43 | |||
| 44 | 12 | int64_t SparseTensor::EmbeddingDim() const { | |
| 45 | 12 | if (shape.size() < 2) { | |
| 46 | ✗ | return 0; | |
| 47 | } | ||
| 48 | 12 | return static_cast<int64_t>(shape[1]); | |
| 49 | } | ||
| 50 | |||
| 51 | 8 | bool SparseTensor::ApplySgdUpdateFlat( | |
| 52 | const base::ConstArray<uint64_t>& keys, | ||
| 53 | const float* grads, | ||
| 54 | int64_t num_rows, | ||
| 55 | int64_t embedding_dim, | ||
| 56 | float learning_rate, | ||
| 57 | unsigned tid, | ||
| 58 | BaseKV::UpdateFlatStats* stats) { | ||
| 59 | 16 | return kv != nullptr && | |
| 60 | 8 | kv->ApplySgdUpdateFlat( | |
| 61 | keys, | ||
| 62 | grads, | ||
| 63 | num_rows, | ||
| 64 | embedding_dim, | ||
| 65 | learning_rate, | ||
| 66 | 8 | tag, | |
| 67 | tid, | ||
| 68 | 8 | stats); | |
| 69 | } | ||
| 70 |