Skip to content

Commit 14c25d8

Browse files
authored
[Embedding] Add memory and performance tests of EmbeddingVariable. (#913)
Signed-off-by: lixy9474 <[email protected]>
1 parent 616e9e4 commit 14c25d8

File tree

5 files changed

+402
-119
lines changed

5 files changed

+402
-119
lines changed

tensorflow/core/framework/embedding/embedding_var.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class EmbeddingVar : public ResourceBase {
141141
memcpy(default_value_, &default_tensor_flat(0),
142142
default_tensor.TotalBytes());
143143

144-
default_value_no_permission_ = TypedAllocator::Allocate<V>(alloc_,
145-
value_len_, AllocationAttributes());
144+
default_value_no_permission_ = TypedAllocator::Allocate<V>(
145+
default_value_alloc_, value_len_, AllocationAttributes());
146146
for (int i = 0; i < value_len_; ++i) {
147147
default_value_no_permission_[i] = static_cast<V>(
148148
emb_config_.default_value_no_permission);
@@ -755,7 +755,8 @@ class EmbeddingVar : public ResourceBase {
755755
TypedAllocator::Deallocate(default_value_alloc_, default_value_,
756756
value_len_ * emb_config_.default_value_dim);
757757
if (default_value_no_permission_) {
758-
TypedAllocator::Deallocate(alloc_, default_value_no_permission_,
758+
TypedAllocator::Deallocate(default_value_alloc_,
759+
default_value_no_permission_,
759760
value_len_);
760761
}
761762
if (filter_) {

tensorflow/core/kernels/BUILD

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,33 @@ tf_cuda_cc_test(
461461
)
462462

463463
tf_cuda_cc_test(
464-
name = "embedding_variable_memory_performance_test",
465-
srcs = ["embedding_variable_memory_performance_test.cc"],
464+
name = "embedding_variable_performance_test",
465+
srcs = ["embedding_variable_performance_test.cc",
466+
"embedding_variable_test.h"],
467+
extra_copts = ["-fexceptions", "-g"],
468+
deps = [
469+
":io",
470+
":ops_testutil",
471+
":ops_util",
472+
"//tensorflow/core:all_kernels",
473+
"//tensorflow/core:core_cpu",
474+
"//tensorflow/core:core_cpu_internal",
475+
"//tensorflow/core:direct_session_internal",
476+
"//tensorflow/core/util/tensor_bundle",
477+
"//tensorflow/core:framework",
478+
"//tensorflow/core:lib",
479+
"//tensorflow/core:test",
480+
"//tensorflow/core:protos_all_cc",
481+
"//tensorflow/core:testlib",
482+
"//third_party/eigen3",
483+
"//tensorflow/core:test_main",
484+
],
485+
)
486+
487+
tf_cuda_cc_test(
488+
name = "embedding_variable_memory_test",
489+
srcs = ["embedding_variable_memory_test.cc",
490+
"embedding_variable_test.h"],
466491
extra_copts = ["-fexceptions", "-g"],
467492
deps = [
468493
":io",
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)