-
Notifications
You must be signed in to change notification settings - Fork 74k
/
constant_op_test.cc
137 lines (116 loc) · 4.72 KB
/
constant_op_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/common_runtime/device_factory.h"
#include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/kernels/ops_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test_benchmark.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session_options.h"
#include "tensorflow/core/public/version.h"
namespace tensorflow {
class ConstantOpTest : public OpsTestBase {
protected:
void PersistentMemoryTrackingTest(bool on_gpu);
};
void ConstantOpTest::PersistentMemoryTrackingTest(bool on_gpu) {
DataType data_type = DT_INT32;
std::initializer_list<int64_t> dims = {2, 3, 4, 5};
Tensor tensor(data_type, TensorShape(dims));
for (int i = 0; i < 2 * 3 * 4 * 5; ++i) {
tensor.flat<int32>()(i) = i;
}
NodeDef const_node;
TF_ASSERT_OK(NodeDefBuilder("some_node", "Const")
.Attr("dtype", data_type)
.Attr("value", tensor)
.Finalize(&const_node));
string device_string = "CPU";
DeviceType device_type = DEVICE_CPU;
if (on_gpu) {
device_string = "GPU";
DeviceType device_type = DEVICE_GPU;
}
std::unique_ptr<Device> device(DeviceFactory::NewDevice(
device_string, {}, "/job:worker/replica:0/task:0"));
Status status;
std::unique_ptr<OpKernel> op(CreateOpKernel(device_type, device.get(),
cpu_allocator(), const_node,
TF_GRAPH_DEF_VERSION, &status));
TF_ASSERT_OK(status);
OpKernelContext::Params params;
params.device = device.get();
params.frame_iter = FrameAndIter(0, 0);
params.op_kernel = op.get();
params.track_allocations = true;
OpKernelContext ctx(¶ms);
op->Compute(&ctx);
TF_EXPECT_OK(ctx.status());
if (on_gpu) {
EXPECT_EQ(ctx.persistent_memory_allocated(), 512);
} else {
EXPECT_EQ(ctx.persistent_memory_allocated(), 480);
}
// Remove memory leak errors.
for (auto allocator_pair : ctx.ConsumeWrappedAllocators()) {
allocator_pair.second->GetRecordsAndUnRef();
}
}
TEST_F(ConstantOpTest, PersistentMemoryTracking) {
PersistentMemoryTrackingTest(false);
#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
(defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
PersistentMemoryTrackingTest(true);
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
}
// Returns graph containing "num" const nodes. If 'sequential' is
// true, make sure all constants are executed sequentially in the
// graph by adding control dependencies.
static Graph* ManyConsts(int num, bool sequential) {
Graph* g = new Graph(OpRegistry::Global());
Node* prev = nullptr;
for (int i = 0; i < num; ++i) {
Tensor c(DT_FLOAT, TensorShape({}));
c.scalar<float>()() = i;
Node* curr = test::graph::Constant(g, c);
if (sequential && prev != nullptr) {
g->AddControlEdge(prev, curr);
}
prev = curr;
}
return g;
}
static void BM_ManyConsts_Parallel(::testing::benchmark::State& state) {
const int num = state.range(0);
test::Benchmark("cpu", ManyConsts(num, false /* !sequential */),
/*old_benchmark_api*/ false)
.Run(state);
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * num);
}
BENCHMARK(BM_ManyConsts_Parallel)->Range(1, 1 << 10);
static void BM_ManyConsts_Sequential(::testing::benchmark::State& state) {
const int num = state.range(0);
test::Benchmark("cpu", ManyConsts(num, true /* sequential */),
/*old_benchmark_api*/ false)
.Run(state);
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * num);
}
BENCHMARK(BM_ManyConsts_Sequential)->Range(1, 1 << 10);
} // end namespace tensorflow