/
xfeed_manager_test.cc
140 lines (112 loc) · 4.82 KB
/
xfeed_manager_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
138
139
140
/* Copyright 2017 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/compiler/xla/service/cpu/xfeed_manager.h"
#include <memory>
#include "tensorflow/compiler/xla/service/cpu/cpu_runtime.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/test.h"
namespace xla {
namespace {
class InfeedManagerTest : public ::testing::Test {};
class TestInfeedBuffer : public cpu::runtime::XfeedBuffer {
public:
explicit TestInfeedBuffer(int32 length, bool expect_shape_match = true)
: shape_(ShapeUtil::MakeShape(U8, {length})),
done_called_(false),
length_(length),
expect_shape_match_(expect_shape_match) {}
~TestInfeedBuffer() override { EXPECT_TRUE(done_called_); }
int32 length() override { return length_; }
void* data() override { return nullptr; }
void Done(StatusOr<Shape> shape) override {
CHECK(!done_called_);
done_called_ = true;
TF_ASSERT_OK(shape.status());
EXPECT_EQ(expect_shape_match_, ShapeUtil::Equal(shape_, shape.ValueOrDie()))
<< "want " << ShapeUtil::HumanString(shape_) << " "
<< (expect_shape_match_ ? "==" : "!=") << " "
<< ShapeUtil::HumanString(shape.ValueOrDie());
}
const Shape& shape() const { return shape_; }
private:
Shape shape_;
bool done_called_;
int32 length_;
bool expect_shape_match_;
};
// Performs the acquire/release sequence on the infeed, as the generated CPU
// code would in the process of executing the infeed operation.
void ProcessNextBuffer(int32 length) {
auto shape = ShapeUtil::MakeShape(U8, {length});
string bytes = shape.SerializeAsString();
void* buffer = __xla_cpu_runtime_AcquireInfeedBufferForDequeue(
length, bytes.data(), bytes.size());
__xla_cpu_runtime_ReleaseInfeedBufferAfterDequeue(length, buffer,
bytes.data(), bytes.size());
}
// Performs the acquire/release sequence on the outfeed, as the generated CPU
// code would in the process of executing the outfeed operation.
void ProcessNextOutfeedBuffer(int32 length, const Shape& shape) {
string bytes = shape.SerializeAsString();
void* buffer = __xla_cpu_runtime_AcquireOutfeedBufferForPopulation(
length, bytes.data(), bytes.size());
__xla_cpu_runtime_ReleaseOutfeedBufferAfterPopulation(
length, buffer, bytes.data(), bytes.size());
}
TEST_F(InfeedManagerTest, SingleThreadedSequential) {
TestInfeedBuffer* a = new TestInfeedBuffer(64);
TestInfeedBuffer* b = new TestInfeedBuffer(32);
cpu::runtime::XfeedManager* xfeed = cpu::runtime::GetXfeedManager();
xfeed->infeed()->EnqueueBuffers({a});
xfeed->infeed()->EnqueueBuffers({b});
ProcessNextBuffer(a->length());
ProcessNextBuffer(b->length());
}
TEST_F(InfeedManagerTest, SingleThreadedInterleaved) {
TestInfeedBuffer* a = new TestInfeedBuffer(64);
TestInfeedBuffer* b = new TestInfeedBuffer(32);
cpu::runtime::XfeedManager* xfeed = cpu::runtime::GetXfeedManager();
xfeed->infeed()->EnqueueBuffers({a});
ProcessNextBuffer(a->length());
xfeed->infeed()->EnqueueBuffers({b});
ProcessNextBuffer(b->length());
}
TEST_F(InfeedManagerTest, MultiThreaded) {
tensorflow::thread::ThreadPool pool(tensorflow::Env::Default(), "test", 2);
cpu::runtime::XfeedManager* xfeed = cpu::runtime::GetXfeedManager();
const int32 length = 64;
pool.Schedule([xfeed]() {
// Spin for 100 milliseconds
int64 start_micros = tensorflow::Env::Default()->NowMicros();
while (true) {
int64 end_micros = tensorflow::Env::Default()->NowMicros();
if ((end_micros - start_micros) >= 100000) { // 100 ms
break;
}
}
TestInfeedBuffer* a = new TestInfeedBuffer(length);
xfeed->infeed()->EnqueueBuffers({a});
});
ProcessNextBuffer(length);
}
TEST_F(InfeedManagerTest, OutfeedWrongShape) {
TestInfeedBuffer* b = new TestInfeedBuffer(32, /*expect_shape_match=*/false);
cpu::runtime::XfeedManager* xfeed = cpu::runtime::GetXfeedManager();
xfeed->outfeed()->EnqueueBuffers({b});
ProcessNextOutfeedBuffer(32, ShapeUtil::MakeShape(U8, {33}));
}
} // namespace
} // namespace xla