MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Split

Function Split

tensorflow/core/framework/tensor_util.cc:119–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117}
118
119Status Split(const Tensor& tensor, const gtl::ArraySlice<int64>& sizes,
120 std::vector<Tensor>* result) {
121 if (tensor.dims() == 0) {
122 return errors::InvalidArgument("Cannot split a zero-dimensional tensor");
123 }
124 int64 total_size = 0;
125 for (int64 size : sizes) {
126 total_size += size;
127 }
128 if (total_size != tensor.dim_size(0)) {
129 return errors::InvalidArgument(
130 "The values in 'sizes' do not sum to the zeroth-dimension size of "
131 "'tensor'");
132 }
133
134 StringPiece from_data = tensor.tensor_data();
135
136 if (DataTypeCanUseMemcpy(tensor.dtype())) {
137 int64 offset = 0;
138 for (int64 size : sizes) {
139 TensorShape shape = tensor.shape();
140 shape.set_dim(0, size);
141 result->emplace_back(tensor.dtype(), shape);
142 Tensor* split = &(*result)[result->size() - 1];
143
144 // We use StringPiece as a convenient map over the tensor buffer,
145 // but we cast the type to get to the underlying buffer to do the
146 // copy.
147 StringPiece to_data = split->tensor_data();
148 CHECK_LE(offset + to_data.size(), from_data.size());
149 memcpy(const_cast<char*>(to_data.data()), from_data.data() + offset,
150 to_data.size());
151
152 offset += to_data.size();
153 }
154 } else {
155 if (tensor.dtype() != DT_STRING) {
156 return errors::Internal("Unexpected data type");
157 }
158 auto from_strings = tensor.flat<tstring>();
159
160 int64 offset = 0;
161 for (int64 size : sizes) {
162 TensorShape shape = tensor.shape();
163 shape.set_dim(0, size);
164 result->emplace_back(tensor.dtype(), shape);
165 Tensor& split = (*result)[result->size() - 1];
166 tstring* to_strings = reinterpret_cast<tstring*>(
167 const_cast<char*>(split.tensor_data().data()));
168
169 CHECK_LE(offset + split.NumElements(), tensor.NumElements());
170 for (int i = 0; i < split.NumElements(); ++i) {
171 to_strings[i] = from_strings(offset + i);
172 }
173
174 offset += split.NumElements();
175 }
176 }

Callers 6

ParseMethod · 0.70
FinalizeDocFunction · 0.70
AddNodeMethod · 0.70
InferShapesMethod · 0.70
CreateNodeDefMethod · 0.70
TESTFunction · 0.70

Calls 13

InvalidArgumentFunction · 0.85
DataTypeCanUseMemcpyFunction · 0.85
InternalFunction · 0.85
tensor_dataMethod · 0.80
dimsMethod · 0.45
dim_sizeMethod · 0.45
dtypeMethod · 0.45
shapeMethod · 0.45
set_dimMethod · 0.45
emplace_backMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45

Tested by 3

InferShapesMethod · 0.56
CreateNodeDefMethod · 0.56
TESTFunction · 0.56