|
| 1 | +/* Copyright 2022 The DeepRec Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +======================================================================*/ |
| 15 | +#include "tensorflow/core/kernels/embedding_variable_test.h" |
| 16 | + |
| 17 | +namespace tensorflow { |
| 18 | +namespace embedding { |
| 19 | +float PerfMemory(Tensor& default_value, |
| 20 | + const std::vector<int64>& id_list, |
| 21 | + int value_size, int64 default_value_dim, |
| 22 | + int64 filter_freq = 0) { |
| 23 | + auto ev = CreateEmbeddingVar(value_size, default_value, |
| 24 | + default_value_dim, filter_freq); |
| 25 | + ValuePtr<float>* value_ptr = nullptr; |
| 26 | + bool is_filter = false; |
| 27 | + double start_mem, end_mem; |
| 28 | + start_mem = getResident() * getpagesize(); |
| 29 | + for (int i = 0; i < id_list.size(); i++) { |
| 30 | + ev->LookupOrCreateKey(id_list[i], &value_ptr, &is_filter, false); |
| 31 | + if (is_filter) |
| 32 | + ev->flat(value_ptr, id_list[i]); |
| 33 | + } |
| 34 | + end_mem = getResident() * getpagesize(); |
| 35 | + double used_mb = (end_mem - start_mem)/1000000; |
| 36 | + LOG(INFO)<<"[TestMemory]Use Memory: "<<used_mb; |
| 37 | + return used_mb; |
| 38 | +} |
| 39 | + |
| 40 | +TEST(EmbeddingVariabelMemoryTest, TestMemory) { |
| 41 | + int value_size = 32; |
| 42 | + int64 default_value_dim = 4096; |
| 43 | + int filter_freq = 2; |
| 44 | + Tensor default_value( |
| 45 | + DT_FLOAT, TensorShape({default_value_dim, value_size})); |
| 46 | + auto default_value_matrix = default_value.matrix<float>(); |
| 47 | + for (int i = 0; i < default_value_dim; i++) { |
| 48 | + for (int j = 0 ; j < value_size; j++) { |
| 49 | + default_value_matrix(i, j) = i * value_size + j; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + int num_of_ids = 1000000; |
| 54 | + std::vector<int64> id_list(num_of_ids); |
| 55 | + for (int i = 0; i < num_of_ids; i++) { |
| 56 | + id_list[i] = i; |
| 57 | + } |
| 58 | + float used_mb = PerfMemory(default_value, id_list, |
| 59 | + value_size, default_value_dim); |
| 60 | + float theoritical_mb = |
| 61 | + 50 + num_of_ids * (32 + 32 + value_size * sizeof(float))/ 1000000; |
| 62 | + EXPECT_TRUE((used_mb > theoritical_mb * 0.99) && |
| 63 | + (used_mb < theoritical_mb * 1.01)); |
| 64 | + |
| 65 | + for (int i = 0; i < num_of_ids / 2; i++) { |
| 66 | + id_list.emplace_back(i); |
| 67 | + } |
| 68 | + used_mb = PerfMemory(default_value, id_list, value_size, |
| 69 | + default_value_dim, filter_freq); |
| 70 | + theoritical_mb = |
| 71 | + 50 + num_of_ids * (32 + 32 + 16 + value_size * sizeof(float)/2)/ 1000000; |
| 72 | + EXPECT_TRUE((used_mb > theoritical_mb * 0.99) && |
| 73 | + (used_mb < theoritical_mb * 1.01)); |
| 74 | +} |
| 75 | +} //namespace embedding |
| 76 | +} //namespace tensorflow |
0 commit comments