You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache_embedding_hashmap_struct.h 1.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #ifndef MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_
  19. #define MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_
  20. #include <math.h>
  21. namespace mindspore {
  22. const int64_t kNullTag = 0;
  23. const int64_t kInitStep = -5;
  24. const int64_t kEmptyRate = 4;
  25. const double kGoldenRatio = 0.6180339;
  26. template <typename T>
  27. struct HashmapEntry {
  28. T key_;
  29. T value_;
  30. T step_;
  31. T tag_;
  32. bool IsEmpty() { return tag_ == kNullTag; }
  33. bool IsUsing(const T train_step) { return step_ >= (train_step - 1); }
  34. bool IsKey(const T emb_idx) { return key_ == emb_idx; }
  35. void SetEmpty() { tag_ = kNullTag; }
  36. };
  37. template <typename T>
  38. T HashFunc(const T key, const size_t m) {
  39. return (T)(((kGoldenRatio * key) - floor(kGoldenRatio * key)) * m);
  40. }
  41. } // namespace mindspore
  42. #endif // MINDSPORE_CCSRC_UTIL_CACHE_EMBBEDDING_HASHMAP_STRUCT_H_