1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2019-2020 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_ 20 #define MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_ 21 22 #include <cmath> 23 24 namespace mindspore { 25 const int64_t kNullTag = 0; 26 const int64_t kInitStep = -5; 27 const int64_t kEmptyRate = 4; 28 const double kGoldenRatio = 0.6180339; 29 template <typename T> 30 struct HashmapEntry { 31 T key_; 32 T value_; 33 T step_; 34 T tag_; 35 IsEmptyHashmapEntry36 bool IsEmpty() const { return tag_ == kNullTag; } 37 IsUsingHashmapEntry38 bool IsUsing(const T train_step) const { return step_ >= (train_step - 1); } 39 IsKeyHashmapEntry40 bool IsKey(const T emb_idx) const { return key_ == emb_idx; } 41 SetEmptyHashmapEntry42 void SetEmpty() { tag_ = kNullTag; } 43 }; 44 45 template <typename T> HashFunc(const T key,const size_t m)46T HashFunc(const T key, const size_t m) { 47 return static_cast<T>(((kGoldenRatio * key) - floor(kGoldenRatio * key)) * m); 48 } 49 } // namespace mindspore 50 51 #endif // MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_ 52