1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "minddata/dataset/text/kernels/data_utils.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include "minddata/dataset/core/pybind_support.h"
23 #include "minddata/dataset/kernels/data/slice_op.h"
24 #include "minddata/dataset/kernels/data/concatenate_op.h"
25
26 namespace mindspore {
27 namespace dataset {
SlidingWindowHelper(const std::shared_ptr<Tensor> & input,std::shared_ptr<Tensor> * output,TensorShape out_shape,uint32_t width,int32_t axis)28 Status SlidingWindowHelper(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, TensorShape out_shape,
29 uint32_t width, int32_t axis) {
30 // if the data row has fewer items than width, the corresponding result row will be empty
31 if (out_shape.Size() == 0) {
32 MS_LOG(WARNING) << "The data row has fewer items than width, the result will be empty.";
33 return Tensor::CreateEmpty(TensorShape({0}), input->type(), output);
34 }
35
36 axis = Tensor::HandleNeg(axis, input->shape().Size());
37 int32_t axis_end = input->shape()[axis];
38 std::shared_ptr<Tensor> tmp;
39 auto concatenate_op = std::make_unique<ConcatenateOp>(axis, nullptr, nullptr);
40
41 // Slice on specified axis and concatenate on new axis
42 for (int32_t i = 0; i + width <= axis_end; i++) {
43 auto slice_op = std::make_unique<SliceOp>(Slice(i, i + width, 1));
44 RETURN_IF_NOT_OK(slice_op->Compute(input, &tmp));
45 if (i == 0) {
46 *output = tmp;
47 } else {
48 TensorRow in({*output, tmp});
49 TensorRow out_row;
50 RETURN_IF_NOT_OK(concatenate_op->Compute(in, &out_row));
51 *output = out_row[0];
52 }
53 }
54 RETURN_IF_NOT_OK((*output)->Reshape(out_shape));
55 return Status::OK();
56 }
57
AppendOffsetsHelper(const std::vector<uint32_t> & offsets_start,const std::vector<uint32_t> & offsets_limit,TensorRow * output)58 Status AppendOffsetsHelper(const std::vector<uint32_t> &offsets_start, const std::vector<uint32_t> &offsets_limit,
59 TensorRow *output) {
60 std::shared_ptr<Tensor> offsets_start_tensor, offsets_limit_tensor;
61 RETURN_IF_NOT_OK(Tensor::CreateFromVector(offsets_start, &offsets_start_tensor));
62 RETURN_IF_NOT_OK(Tensor::CreateFromVector(offsets_limit, &offsets_limit_tensor));
63
64 output->push_back(offsets_start_tensor);
65 output->push_back(offsets_limit_tensor);
66 return Status::OK();
67 }
68 } // namespace dataset
69 } // namespace mindspore
70