• 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 
17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_COMPOSE_OP_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_COMPOSE_OP_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "minddata/dataset/core/tensor.h"
26 #include "minddata/dataset/kernels/tensor_op.h"
27 
28 namespace mindspore {
29 namespace dataset {
30 class ComposeOp : public TensorOp {
31  public:
32   /// constructor
33   /// \param[in] ops list of TensorOps to compose into 1 TensorOp
34   explicit ComposeOp(const std::vector<std::shared_ptr<TensorOp>> &ops);
35 
36   /// default destructor
37   ~ComposeOp() override = default;
38 
39   /// return the number of inputs the first tensorOp in compose takes
40   /// \return number of input tensors
NumInput()41   uint32_t NumInput() override { return ops_.front()->NumInput(); }
42 
43   /// return the number of outputs the last tensorOp in compose produces
44   /// \return number of output tensors
NumOutput()45   uint32_t NumOutput() override { return ops_.back()->NumOutput(); }
46 
47   /// \param[in] inputs
48   /// \param[out] outputs
49   /// \return  Status code
50   Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
51 
52   /// \param[in] inputs
53   /// \param[out] outputs
54   /// \return Status code
55   Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override;
56 
57   /// \param[in] input
58   /// \param[out] output
59   /// \return Status code
60   Status Compute(const TensorRow &input, TensorRow *output) override;
61 
Name()62   std::string Name() const override { return kComposeOp; }
63 
64   // Currently maybe 910b dvpp ops & cpu ops mixed used
IsMixedOps()65   bool IsMixedOps() {
66     bool have_dvpp = false;
67     bool have_cpu = false;
68     for (auto &item : ops_) {
69       if (item->IsDvppOp()) {
70         have_dvpp = true;
71       } else {
72         have_cpu = true;
73       }
74     }
75 
76     if (have_dvpp && have_cpu) {
77       MS_LOG(ERROR) << "Currently, it is not supported to mix DVPP transforms with CPU transforms in Compose.";
78       return true;
79     }
80     return false;
81   }
82 
83   // Check whether compose contains dvpp ops.
IsDvppOp()84   virtual bool IsDvppOp() {
85     bool have_dvpp = false;
86     for (auto &item : ops_) {
87       if (item->IsDvppOp()) {
88         have_dvpp = true;
89         break;
90       }
91     }
92 
93     if (have_dvpp) {
94       return true;
95     }
96     return false;
97   }
98 
GetOps()99   std::vector<std::shared_ptr<TensorOp>> GetOps() { return ops_; }
100 
101  private:
102   std::vector<std::shared_ptr<TensorOp>> ops_;
103 };
104 }  // namespace dataset
105 }  // namespace mindspore
106 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_COMPOSE_OP_
107