• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2023 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 #include "minddata/dataset/kernels/data/compose_op.h"
17 
18 #include "minddata/dataset/core/tensor.h"
19 #include "minddata/dataset/kernels/tensor_op.h"
20 #include "minddata/dataset/util/status.h"
21 
22 namespace mindspore {
23 namespace dataset {
OutputShape(const std::vector<TensorShape> & inputs,std::vector<TensorShape> & outputs)24 Status ComposeOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
25   std::vector<TensorShape> in_shapes = inputs;
26   for (auto &op : ops_) {
27     RETURN_IF_NOT_OK(op->OutputShape(in_shapes, outputs));
28     in_shapes = std::move(outputs);  // outputs become empty after move
29   }
30   outputs = std::move(in_shapes);
31   return Status::OK();
32 }
33 
OutputType(const std::vector<DataType> & inputs,std::vector<DataType> & outputs)34 Status ComposeOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) {
35   std::vector<DataType> in_types = inputs;
36   for (auto &op : ops_) {
37     RETURN_IF_NOT_OK(op->OutputType(in_types, outputs));
38     in_types = std::move(outputs);  // outputs become empty after move
39   }
40   outputs = std::move(in_types);
41   return Status::OK();
42 }
43 
Compute(const TensorRow & inputs,TensorRow * outputs)44 Status ComposeOp::Compute(const TensorRow &inputs, TensorRow *outputs) {
45   IO_CHECK_VECTOR(inputs, outputs);
46   CHECK_FAIL_RETURN_UNEXPECTED(!ops_.empty(), "Compose: transform list should not be empty.");
47   TensorRow in_rows = inputs;
48   for (auto &op : ops_) {
49     RETURN_IF_NOT_OK(op->Compute(in_rows, outputs));
50     in_rows = std::move(*outputs);  // after move, *outputs become empty
51   }
52   (*outputs) = std::move(in_rows);
53   return Status::OK();
54 }
55 
ComposeOp(const std::vector<std::shared_ptr<TensorOp>> & ops)56 ComposeOp::ComposeOp(const std::vector<std::shared_ptr<TensorOp>> &ops) : ops_(ops) {}
57 }  // namespace dataset
58 }  // namespace mindspore
59