1 /** 2 * Copyright 2019-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_KERNELS_PY_FUNC_OP_H_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_PY_FUNC_OP_H_ 19 20 #include <memory> 21 #include <vector> 22 #include <utility> 23 #include <string> 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 PyFuncOp : public TensorOp { 31 public: PyFuncOp(py::function func)32 explicit PyFuncOp(py::function func) : py_func_ptr_(std::move(func)) { output_type_ = DataType::DE_UNKNOWN; } PyFuncOp(py::function func,DataType::Type output_type)33 explicit PyFuncOp(py::function func, DataType::Type output_type) 34 : py_func_ptr_(std::move(func)), output_type_(output_type) {} 35 36 ~PyFuncOp() override = default; 37 NumInput()38 uint32_t NumInput() override { return 0; } NumOutput()39 uint32_t NumOutput() override { return 0; } 40 41 // Compute function for n-n mapping. 42 Status Compute(const TensorRow &input, TensorRow *output) override; 43 44 /// \brief Function to convert a primitive type py::object to a TensorRow 45 /// \notes Changes the py::object to a tensor with corresponding C++ DataType based on output_type_ and adds it to a 46 /// TensorRow. This function is used inside Compute. 47 /// \param[in] ret_py_obj The python object we want to cast 48 /// \param[output] The TensorRow output 49 /// \return Status 50 Status CastOutput(const py::object &ret_py_obj, TensorRow *output); Name()51 std::string Name() const override { return kPyFuncOp; } 52 Status to_json(nlohmann::json *out_json) override; 53 54 static Status from_json(nlohmann::json op_params, std::vector<std::shared_ptr<TensorOperation>> *result); 55 56 /// \brief Check whether this pyfunc op is deterministic 57 /// \return True if this pyfunc op is random 58 bool IsRandom(); 59 60 private: 61 py::function py_func_ptr_; 62 DataType::Type output_type_; 63 }; 64 } // namespace dataset 65 } // namespace mindspore 66 67 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_PY_FUNC_OP_H_ 68