1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_CORE_UTIL_BATCH_UTIL_H_ 16 #define TENSORFLOW_CORE_UTIL_BATCH_UTIL_H_ 17 18 #include "tensorflow/core/framework/tensor.h" 19 #include "tensorflow/core/lib/core/status.h" 20 21 namespace tensorflow { 22 namespace batch_util { 23 24 // Copies element into the index^th slice of parent (in the 0th dimension). 25 // 26 // NOTE(mrry): The `element` argument is taken by value. Use `std::move()` 27 // to move the `element` argument into this function, and the implementation 28 // may be able to optimize the copy to a move. This is particularly important 29 // for DT_STRING tensors. 30 Status CopyElementToSlice(Tensor element, Tensor* parent, int64 index); 31 32 // Copies the index^th slice of parent (in the 0th dimension) into element. 33 Status CopySliceToElement(const Tensor& parent, Tensor* element, int64 index); 34 35 // Copies 'num_slices' contiguous slices from 'src' tensor starting from index 36 // 'src_offset' into target tensor 'dst', and places them into slices 37 // starting from 'dst_offset'. 38 // 39 // This function requires 'src' and 'dst' to have compatible shapes. That is it 40 // requires cum_prod(src.shape[1:] == cum_prod(dst->shape[1:]). For example if 41 // source is of shape [x, 2, 1] and dst is a tensor of shape [y, 1, 2], this 42 // function can still proceed successfully. 43 Status CopyContiguousSlices(const Tensor& src, int64 src_offset, 44 int64 dst_offset, int64 num_slices, Tensor* dst); 45 46 // Copies the index^th slice of parent (in the 0th dimension) into element. 47 // 48 // NOTE(mrry): The implementation may be able to optimize the copy to a move. 49 // This is particularly important for DT_STRING tensors. 50 Status MaybeMoveSliceToElement(Tensor* parent, Tensor* element, int64 index); 51 52 // Zero-initializes the tensor `element` using the scalar stored in `padding`. 53 // Both `element` and `padding` must have matching `dtype`. 54 Status SetElementZero(Tensor* element, const Tensor& padding); 55 56 // Copies `element` into a (0th dimension) slice of `parent`, assuming 57 // the shape of `element` is strictly not larger along any axis than a 58 // slice. 59 Status CopyElementToLargerSlice(const Tensor& element, Tensor* parent, 60 int index); 61 62 } // namespace batch_util 63 } // namespace tensorflow 64 65 #endif // TENSORFLOW_CORE_UTIL_BATCH_UTIL_H_ 66