1 /** 2 * Copyright 2020-2021 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_INCLUDE_DATASET_EXECUTE_H_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_EXECUTE_H_ 19 20 #include <string> 21 #include <vector> 22 #include <map> 23 #include <memory> 24 #include "include/api/context.h" 25 #include "include/api/types.h" 26 #include "include/dataset/constants.h" 27 #include "include/dataset/transforms.h" 28 29 namespace mindspore { 30 namespace dataset { 31 class DeviceResource; 32 // class to run tensor operations in eager mode 33 class Execute { 34 public: 35 /// \brief Constructor. 36 /// \param[in] op TensorOperation to be applied in Eager mode, it accepts operation in type of shared pointer. 37 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 38 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 39 explicit Execute(std::shared_ptr<TensorOperation> op, MapTargetDevice device_type = MapTargetDevice::kCpu, 40 uint32_t device_id = 0); 41 42 /// \brief Constructor. 43 /// \param[in] op TensorTransform to be applied in Eager mode, it accepts operation in type of shared pointer. 44 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 45 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 46 explicit Execute(std::shared_ptr<TensorTransform> op, MapTargetDevice device_type = MapTargetDevice::kCpu, 47 uint32_t device_id = 0); 48 49 /// \brief Constructor. 50 /// \param[in] op TensorTransform to be applied in Eager mode, it accepts operation in type of reference. 51 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 52 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 53 explicit Execute(std::reference_wrapper<TensorTransform> op, MapTargetDevice device_type = MapTargetDevice::kCpu, 54 uint32_t device_id = 0); 55 56 /// \brief Constructor. 57 /// \param[in] op TensorTransform to be applied in Eager mode, it accepts operation in type of raw pointer. 58 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 59 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 60 explicit Execute(TensorTransform *op, MapTargetDevice device_type = MapTargetDevice::kCpu, uint32_t device_id = 0); 61 62 /// \brief Constructor. 63 /// \param[in] ops A vector of TensorOperations to be applied in Eager mode, it accepts operation 64 /// in type of shared pointer. 65 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 66 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 67 explicit Execute(std::vector<std::shared_ptr<TensorOperation>> ops, 68 MapTargetDevice device_type = MapTargetDevice::kCpu, uint32_t device_id = 0); 69 70 /// \brief Constructor. 71 /// \param[in] ops A vector of TensorTransforms to be applied in Eager mode, it accepts operation 72 /// in type of shared pointer. 73 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 74 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 75 explicit Execute(std::vector<std::shared_ptr<TensorTransform>> ops, 76 MapTargetDevice device_type = MapTargetDevice::kCpu, uint32_t device_id = 0); 77 78 /// \brief Constructor. 79 /// \param[in] ops A vector of TensorTransforms to be applied in Eager mode, it accepts operation 80 /// in type of raw pointer. 81 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 82 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 83 explicit Execute(const std::vector<std::reference_wrapper<TensorTransform>> ops, 84 MapTargetDevice device_type = MapTargetDevice::kCpu, uint32_t device_id = 0); 85 86 /// \brief Constructor. 87 /// \param[in] ops A vector of TensorTransforms to be applied in Eager mode, it accepts operation 88 /// in type of raw pointer. 89 /// \param[in] device_type Target device environment to perform operation, can be kCPU/kGPU/kAscend310 (default=kCPU). 90 /// \param[in] device_id Target device ID to perform operation, only valid when device_type=kAscend310 (default=0). 91 explicit Execute(const std::vector<TensorTransform *> &ops, MapTargetDevice device_type = MapTargetDevice::kCpu, 92 uint32_t device_id = 0); 93 94 /// \brief Destructor. 95 ~Execute(); 96 97 /// \brief Callable function to execute the TensorTransform in eager mode. 98 /// \param[in] input Tensor to be transformed. 99 /// \param[out] output Transformed tensor. 100 /// \return Status error code, returns OK if no error encountered. 101 Status operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output); 102 103 /// \brief Callable function to execute the TensorTransform in eager mode. 104 /// \param[in] input_tensor_list List of Tensor to be transformed. 105 /// \param[out] out Result tensor after transform. 106 /// \return Status error code, returns OK if no error encountered. 107 Status operator()(const std::vector<mindspore::MSTensor> &input_tensor_list, std::vector<mindspore::MSTensor> *out); 108 109 /// \brief Given a set of Executes, run them 110 static Status Run(const std::vector<std::shared_ptr<dataset::Execute>> &data_graph, 111 const std::vector<mindspore::MSTensor> &inputs, std::vector<mindspore::MSTensor> *outputs); 112 113 /// \brief The function to release device memory on Ascend310. 114 Status DeviceMemoryRelease(); 115 116 /// \brief The function to generate AIPP configuration. 117 std::string AippCfgGenerator(); 118 119 private: 120 /// \brief The function to convert a TensorTransform object into a TensorOperation object. 121 Status ParseTransforms(); 122 123 /// \brief The function to validate target device setting is valid or not. 124 Status ValidateDevice(); 125 126 /// \brief Initialize 310 resource 127 Status InitResource(MapTargetDevice device_type, uint32_t device_id); 128 129 std::vector<std::shared_ptr<TensorTransform>> transforms_; 130 std::vector<std::shared_ptr<TensorOperation>> ops_; 131 MapTargetDevice device_type_; 132 std::shared_ptr<DeviceResource> device_resource_; 133 struct ExtraInfo; 134 std::shared_ptr<ExtraInfo> info_; 135 }; 136 137 } // namespace dataset 138 } // namespace mindspore 139 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASET_EXECUTE_H_ 140