FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tensorflow/tensorflow/core/util/batch_util.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
/
util
/
batch_util.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
287 lines (252 loc) · 10.3 KB
Breadcrumbs
tensorflow
/
tensorflow
/
core
/
util
/
batch_util.cc
Copy path
File metadata and controls
287 lines (252 loc) · 10.3 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
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/core/util/batch_util.h
"
#
include
"
tensorflow/core/framework/register_types.h
"
#
include
"
tensorflow/core/framework/types.h
"
#
include
"
tensorflow/core/lib/core/errors.h
"
#
define
TF_CALL_DATASET_TYPES
(
m
)
TF_CALL_ALL_TYPES
(m)
TF_CALL_QUANTIZED_TYPES
(m)
namespace
tensorflow
{
namespace
batch_util
{
namespace
{
Status
ValidateInput
(
const
Tensor& parent,
const
Tensor& element, int64 index) {
DCHECK_NE
(parent.
dim_size
(
0
),
0
);
DCHECK_GE
(index,
0
);
if
(element.
NumElements
() != (parent.
NumElements
() / parent.
dim_size
(
0
))) {
TensorShape chip_shape = parent.
shape
();
chip_shape.
RemoveDim
(
0
);
return
errors::Internal
(
"
ValidateInput Cannot perform copy: number of elements does not match.
"
"
Shapes are: [element]:
"
,
element.
shape
().
DebugString
(),
"
, [parent slice]:
"
, chip_shape.
DebugString
());
}
return
Status::OK
();
}
template
<
typename
T>
Status
HandleElementToSlice
(Tensor element, Tensor* parent, int64 index,
bool
/*
can_move
*/
) {
parent->
flat_outer_dims
<T>().
chip
(index,
0
) = element.
flat
<T>();
return
Status::OK
();
}
template
<>
Status HandleElementToSlice<string>(Tensor element, Tensor* parent, int64 index,
bool
can_move) {
auto
parent_as_matrix = parent->
flat_outer_dims
<string>();
auto
element_flat = element.
flat
<string>();
if
(can_move) {
for
(int64 i =
0
; i < element.
NumElements
(); ++i) {
parent_as_matrix
(index, i) =
std::move
(
element_flat
(i));
}
}
else
{
parent_as_matrix.
chip
(index,
0
) = element_flat;
}
return
Status::OK
();
}
template
<>
Status HandleElementToSlice<Variant>(Tensor element, Tensor* parent,
int64 index,
bool
can_move) {
auto
parent_as_matrix = parent->
flat_outer_dims
<Variant>();
auto
element_flat = element.
flat
<Variant>();
if
(can_move) {
for
(int64 i =
0
; i < element.
NumElements
(); ++i) {
parent_as_matrix
(index, i) =
std::move
(
element_flat
(i));
}
}
else
{
parent_as_matrix.
chip
(index,
0
) = element_flat;
}
return
Status::OK
();
}
//
TODO(b/78245576): Consider removing this overload.
template
<
typename
T>
void
HandleSliceToElement
(
const
Tensor& parent, Tensor* element, int64 index) {
element->
flat
<T>() = parent.
flat_outer_dims
<T>().
chip
(index,
0
);
}
template
<
typename
T>
void
HandleSliceToElement
(Tensor* parent, Tensor* element, int64 index,
bool
can_move) {
element->
flat
<T>() = parent->
flat_outer_dims
<T>().
chip
(index,
0
);
}
template
<>
void
HandleSliceToElement<string>(Tensor* parent, Tensor* element, int64 index,
bool
can_move) {
auto
parent_as_matrix = parent->
flat_outer_dims
<string>();
auto
element_flat = element->
flat
<string>();
if
(can_move) {
for
(int64 i =
0
; i < element->
NumElements
(); ++i) {
element_flat
(i) =
std::move
(
parent_as_matrix
(index, i));
}
}
else
{
element_flat = parent_as_matrix.
chip
(index,
0
);
}
}
template
<>
void
HandleSliceToElement<Variant>(Tensor* parent, Tensor* element, int64 index,
bool
can_move) {
auto
parent_as_matrix = parent->
flat_outer_dims
<Variant>();
auto
element_flat = element->
flat
<Variant>();
if
(can_move) {
for
(int64 i =
0
; i < element->
NumElements
(); ++i) {
element_flat
(i) =
std::move
(
parent_as_matrix
(index, i));
}
}
else
{
element_flat = parent_as_matrix.
chip
(index,
0
);
}
}
}
//
namespace
//
Copies element into the index^th slice of parent (in the 0th dimension).
Status
CopyElementToSlice
(Tensor element, Tensor* parent, int64 index) {
TF_RETURN_IF_ERROR
(
ValidateInput
(*parent, element, index));
bool
can_move = element.
RefCountIsOne
();
#
define
HANDLE_TYPE
(
T
) \
case
DataTypeToEnum<T>::value: { \
return
HandleElementToSlice<T>(
std::move
(element), parent, index, \
can_move); \
}
switch
(element.
dtype
()) {
TF_CALL_ALL_TYPES
(
HANDLE_TYPE
);
TF_CALL_QUANTIZED_TYPES
(
HANDLE_TYPE
);
TF_CALL_uint32
(
HANDLE_TYPE
);
TF_CALL_uint64
(
HANDLE_TYPE
);
#
undef
HANDLE_TYPE
default
:
return
errors::Unimplemented
(
"
CopyElementToSlice Unhandled data type:
"
,
element.
dtype
());
}
}
//
Copies the index^th slice of parent (in the 0th dimension) into element.
Status
CopySliceToElement
(
const
Tensor& parent, Tensor* element, int64 index) {
TF_RETURN_IF_ERROR
(
ValidateInput
(parent, *element, index));
#
define
HANDLE_TYPE
(
T
) \
case
DataTypeToEnum<T>::value: { \
HandleSliceToElement<T>(parent, element, index); \
return
Status::OK
(); \
}
switch
(parent.
dtype
()) {
TF_CALL_ALL_TYPES
(
HANDLE_TYPE
);
TF_CALL_QUANTIZED_TYPES
(
HANDLE_TYPE
);
#
undef
HANDLE_TYPE
default
:
return
errors::Unimplemented
(
"
CopySliceToElement Unhandled data type:
"
,
element->
dtype
());
}
}
//
Copies the index^th slice of parent (in the 0th dimension) into element.
//
//
NOTE(mrry): The implementation may be able to optimize the copy to a move.
//
This is particularly important for DT_STRING tensors.
Status
MaybeMoveSliceToElement
(Tensor* parent, Tensor* element, int64 index) {
TF_RETURN_IF_ERROR
(
ValidateInput
(*parent, *element, index));
bool
can_move = parent->
RefCountIsOne
();
#
define
HANDLE_TYPE
(
T
) \
case
DataTypeToEnum<T>::value: { \
HandleSliceToElement<T>(parent, element, index, can_move); \
return
Status::OK
(); \
}
switch
(parent->
dtype
()) {
TF_CALL_ALL_TYPES
(
HANDLE_TYPE
);
TF_CALL_QUANTIZED_TYPES
(
HANDLE_TYPE
);
#
undef
HANDLE_TYPE
default
:
return
errors::Unimplemented
(
"
MaybeMoveSliceToElement Unhandled data type:
"
, element->
dtype
());
}
}
//
The following five functions are copied from padding_fifo_queue.cc.
//
TODO(mrry): Reconcile these functions with the similar methods in the
//
queue implementation.
Status
ValidateElementToLargerSlice
(
const
Tensor& element, Tensor* parent) {
DCHECK_NE
(parent->
dim_size
(
0
),
0
);
if
(element.
NumElements
() > (parent->
NumElements
() / parent->
dim_size
(
0
))) {
TensorShape chip_shape = parent->
shape
();
chip_shape.
RemoveDim
(
0
);
return
errors::Internal
(
"
HandleElementToLargerSlice Cannot copy slice: number of entries in
"
"
element is greater than number of elements in parent slice.
"
,
"
Shapes are: [element]:
"
, element.
shape
().
DebugString
(),
"
, [parent slice]:
"
, chip_shape.
DebugString
());
}
return
Status::OK
();
}
template
<
typename
T,
int
NDIMS
>
Status
HandleElementToLargerSlice
(
const
Tensor& element, Tensor* parent,
int
index) {
TF_RETURN_IF_ERROR
(
ValidateElementToLargerSlice
(element, parent));
if
(element.
NumElements
() ==
0
) {
return
Status::OK
();
}
auto
element_t
= element.
tensor
<T,
NDIMS
>();
auto
parent_t
= parent->
tensor
<T,
NDIMS
+
1
>();
Eigen::DSizes<Eigen::DenseIndex,
NDIMS
+
1
> slice_indices;
slice_indices[
0
] = index;
Eigen::DSizes<Eigen::DenseIndex,
NDIMS
+
1
> slice_size;
slice_size[
0
] =
1
;
for
(
size_t
i =
1
; i < slice_size.
size
(); ++i) {
slice_size[i] =
element_t
.
dimension
(i -
1
);
}
parent_t
.
slice
(slice_indices, slice_size) =
element_t
.
reshape
(slice_size);
return
Status::OK
();
}
template
<
int
NDIMS
>
Status
HandleElementToLargerSliceWithRank
(
const
Tensor& element, Tensor* parent,
int
index) {
#
define
HANDLE_TYPE
(
T
) \
case
DataTypeToEnum<T>::value: { \
return
HandleElementToLargerSlice<T,
NDIMS
>(element, parent, index); \
}
switch
(element.
dtype
()) {
TF_CALL_DATASET_TYPES
(
HANDLE_TYPE
);
#
undef
HANDLE_TYPE
default
:
return
errors::Unimplemented
(
"
HandleElementToLargerSliceWithRank Unhandled data type:
"
,
element.
dtype
());
}
}
Status
CopyElementToLargerSlice
(
const
Tensor& element, Tensor* parent,
int
index) {
if
(parent->
dims
() != element.
dims
() +
1
) {
return
errors::Internal
(
"
Mismatched ranks. Element's rank is:
"
, element.
dims
(),
"
but element is meant to be a slice in output Tensor having rank:
"
,
parent->
dims
(),
"
(should be:
"
, element.
dims
() +
1
,
"
)
"
);
}
#
define
HANDLE_DIMS
(
NDIMS
) \
case
NDIMS
: { \
TF_RETURN_IF_ERROR
( \
HandleElementToLargerSliceWithRank<
NDIMS
>(element, parent, index)); \
return
Status::OK
(); \
}
switch
(element.
dims
()) {
HANDLE_DIMS
(
0
);
HANDLE_DIMS
(
1
);
HANDLE_DIMS
(
2
);
HANDLE_DIMS
(
3
);
HANDLE_DIMS
(
4
);
#
undef
HANDLE_DIMS
default
:
return
errors::Unimplemented
(
"
CopyElementToLargerSlice Unhandled rank:
"
,
element.
dims
());
}
}
Status
SetElementZero
(Tensor* element,
const
Tensor& padding) {
#
define
HANDLE_TYPE
(
T
) \
if
(element->
dtype
() == DataTypeToEnum<T>::value) { \
element->
flat
<T>().
setConstant
(padding.
scalar
<T>()()); \
return
Status::OK
(); \
}
TF_CALL_DATASET_TYPES
(
HANDLE_TYPE
);
#
undef
HANDLE_TYPE
return
errors::Unimplemented
(
"
SetElementZero Unhandled data type:
"
,
element->
dtype
());
}
}
//
namespace batch_util
}
//
namespace tensorflow
Back
|
FazBrowse Home
|
New Git URL