FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tensorflow/tensorflow/core/framework/reader_base.cc at master · codeplaysoftware/tensorflow · GitHub
codeplaysoftware
tensorflow
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tensorflow
/
tensorflow
/
core
/
framework
/
reader_base.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
266 lines (237 loc) · 8.04 KB
Breadcrumbs
tensorflow
/
tensorflow
/
core
/
framework
/
reader_base.cc
Copy path
File metadata and controls
266 lines (237 loc) · 8.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
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/framework/reader_base.h
"
#
include
"
tensorflow/core/framework/reader_base.pb.h
"
#
include
"
tensorflow/core/framework/types.h
"
#
include
"
tensorflow/core/lib/core/coding.h
"
#
include
"
tensorflow/core/lib/core/errors.h
"
#
include
"
tensorflow/core/lib/core/notification.h
"
#
include
"
tensorflow/core/lib/core/stringpiece.h
"
#
include
"
tensorflow/core/lib/strings/str_util.h
"
#
include
"
tensorflow/core/lib/strings/strcat.h
"
namespace
tensorflow
{
//
ReaderBase ------------------------------------------------------
ReaderBase::ReaderBase
(
const
string& name) : name_(name) {}
int64
ReaderBase::NumRecordsProduced
() {
mutex_lock
lock
(mu_);
return
num_records_produced_;
}
int64
ReaderBase::NumWorkUnitsCompleted
() {
mutex_lock
lock
(mu_);
return
work_finished_;
}
Status
ReaderBase::Reset
() {
mutex_lock
lock
(mu_);
return
ResetLocked
();
}
Status
ReaderBase::ResetLocked
() {
work_started_ =
0
;
work_finished_ =
0
;
num_records_produced_ =
0
;
work_.
clear
();
return
Status::OK
();
}
Status
ReaderBase::SerializeState
(string* state) {
mutex_lock
lock
(mu_);
return
SerializeStateLocked
(state);
}
Status
ReaderBase::SerializeStateLocked
(string* state) {
return
errors::Unimplemented
(
"
Reader SerializeState
"
);
}
Status
ReaderBase::RestoreState
(
const
string& state) {
mutex_lock
lock
(mu_);
Status status =
RestoreStateLocked
(state);
if
(!status.
ok
()) {
ResetLocked
().
IgnoreError
();
}
return
status;
}
Status
ReaderBase::RestoreStateLocked
(
const
string& state) {
return
errors::Unimplemented
(
"
Reader RestoreState
"
);
}
int64
ReaderBase::ReadUpTo
(
const
int64 num_records, QueueInterface* queue,
std::vector<string>* keys,
std::vector<string>* values,
OpKernelContext* context) {
mutex_lock
lock
(mu_);
int64 records_produced_this_call =
0
;
while
(
true
) {
//
Records produced by this iteration of the ReadUpToLocked call.
int64 num_records_produced =
0
;
int64 remaining = num_records - records_produced_this_call;
if
(remaining ==
0
) {
return
records_produced_this_call;
}
if
(!
work_in_progress
()) {
work_ =
GetNextWorkLocked
(queue, context);
if
(!context->
status
().
ok
()) {
return
records_produced_this_call;
}
Status status =
OnWorkStartedLocked
();
if
(status.
ok
()) {
work_started_++;
}
else
{
context->
SetStatus
(status);
return
records_produced_this_call;
}
}
bool
at_end =
false
;
Status status =
ReadUpToLocked
(remaining, keys, values, &num_records_produced, &at_end);
//
This call so far.
records_produced_this_call += num_records_produced;
//
In total, over the lifetime of the ReaderBase.
num_records_produced_ += num_records_produced;
if
(!at_end && status.
ok
() && num_records_produced ==
0
) {
status =
errors::Internal
(
"
ReadManyLocked() for
"
,
name
(),
"
must set *at_end=true, *num_produced > 0 or return an error.
"
);
context->
SetStatus
(status);
return
records_produced_this_call;
}
if
(status.
ok
() && at_end) {
status =
OnWorkFinishedLocked
();
work_finished_ = work_started_;
if
(records_produced_this_call >
0
) {
return
records_produced_this_call;
}
}
if
(!status.
ok
()) {
context->
SetStatus
(status);
return
records_produced_this_call;
}
}
}
//
Default implementation just reads one record at a time.
Status
ReaderBase::ReadUpToLocked
(int64 num_records, std::vector<string>* keys,
std::vector<string>* values, int64* num_read,
bool
* at_end) {
bool
produced =
false
;
string key;
string value;
Status status =
ReadLocked
(&key, &value, &produced, at_end);
if
(produced) {
keys->
emplace_back
(key);
values->
emplace_back
(value);
*num_read =
1
;
}
else
{
*num_read =
0
;
}
return
status;
}
void
ReaderBase::Read
(QueueInterface* queue, string* key, string* value,
OpKernelContext* context) {
mutex_lock
lock
(mu_);
while
(
true
) {
if
(!
work_in_progress
()) {
work_ =
GetNextWorkLocked
(queue, context);
if
(!context->
status
().
ok
()) {
return
;
}
Status status =
OnWorkStartedLocked
();
if
(status.
ok
()) {
work_started_++;
}
else
{
context->
SetStatus
(status);
return
;
}
}
bool
produced =
false
;
bool
at_end =
false
;
Status status =
ReadLocked
(key, value, &produced, &at_end);
if
(!at_end && status.
ok
() && !produced) {
status =
errors::Internal
(
"
ReadLocked() for
"
,
name
(),
"
must set *at_end=true, *produced=true, or return an error.
"
);
}
if
(!status.
ok
() && produced) {
status =
errors::Internal
(
"
ReadLocked() for
"
,
name
(),
"
set *produced=true *and* returned an error:
"
, status.
ToString
());
}
if
(status.
ok
() && at_end) {
status =
OnWorkFinishedLocked
();
work_finished_ = work_started_;
}
if
(!status.
ok
()) {
context->
SetStatus
(status);
return
;
}
if
(produced) {
++num_records_produced_;
return
;
}
}
}
string
ReaderBase::GetNextWorkLocked
(QueueInterface* queue,
OpKernelContext* context)
const
{
string work;
Notification n;
queue->
TryDequeue
(
context, [
this
, context, &n, &work](
const
QueueInterface::Tuple& tuple) {
if
(context->
status
().
ok
()) {
if
(tuple.
size
() !=
1
) {
context->
SetStatus
(
errors::InvalidArgument
(
"
Expected single component queue
"
));
}
else
if
(tuple[
0
].
dtype
() !=
DT_STRING
) {
context->
SetStatus
(
errors::InvalidArgument
(
"
Expected queue with single string component
"
));
}
else
if
(tuple[
0
].
NumElements
() !=
1
) {
context->
SetStatus
(
errors::InvalidArgument
(
"
Expected to dequeue a one-element string tensor
"
));
}
else
{
work = tuple[
0
].
flat
<string>()(
0
);
}
}
n.
Notify
();
});
n.
WaitForNotification
();
return
work;
}
void
ReaderBase::SaveBaseState
(ReaderBaseState* state)
const
{
state->
Clear
();
state->
set_work_started
(work_started_);
state->
set_work_finished
(work_finished_);
state->
set_num_records_produced
(num_records_produced_);
state->
set_current_work
(work_);
}
string
ReaderBase::KeyName
(
const
string& key)
const
{
return
strings::StrCat
(
current_work
(),
"
:
"
, key);
}
Status
ReaderBase::RestoreBaseState
(
const
ReaderBaseState& state) {
work_started_ = state.
work_started
();
work_finished_ = state.
work_finished
();
num_records_produced_ = state.
num_records_produced
();
work_ = state.
current_work
();
if
(work_started_ <
0
|| work_finished_ <
0
|| num_records_produced_ <
0
) {
#
ifdef
__ANDROID__
const
string debug_string =
"
<debug state not available>
"
;
#
else
const
string debug_string = state.
DebugString
();
#
endif
return
errors::InvalidArgument
(
"
Unexpected negative value when restoring in
"
,
name
(),
"
:
"
,
debug_string);
}
if
(work_started_ > work_finished_) {
#
ifdef
__ANDROID__
const
string debug_string =
"
<debug state not available>
"
;
#
else
const
string debug_string = state.
DebugString
();
#
endif
return
errors::InvalidArgument
(
"
Inconsistent work started vs. finished when restoring in
"
,
name
(),
"
:
"
, debug_string);
}
return
Status::OK
();
}
}
//
namespace tensorflow
Back
|
FazBrowse Home
|
New Git URL