• 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_PY_FUNC_OP_H_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_PY_FUNC_OP_H_
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 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) {}
33 
PyFuncOp(py::function func,DataType::Type output_type)34   explicit PyFuncOp(py::function func, DataType::Type output_type)
35       : py_func_ptr_(std::move(func)), output_type_(output_type) {}
36 
37   ~PyFuncOp() override = default;
38 
NumInput()39   uint32_t NumInput() override { return 0; }
40 
NumOutput()41   uint32_t NumOutput() override { return 0; }
42 
43   // Compute function for n-n mapping.
44   Status Compute(const TensorRow &input, TensorRow *output) override;
45 
46   /// \brief Function to convert a primitive type py::object to a TensorRow
47   /// \notes Changes the py::object to a tensor with corresponding C++ DataType based on output_type_ and adds it to a
48   ///    TensorRow. This function is used inside Compute.
49   /// \param[in] ret_py_obj The python object we want to cast
50   /// \param[output] The TensorRow output
51   /// \return Status
52   Status CastOutput(const py::object &ret_py_obj, TensorRow *output);
53 
Name()54   std::string Name() const override { return kPyFuncOp; }
55 
56   Status to_json(nlohmann::json *out_json) override;
57 
58   static Status from_json(nlohmann::json op_params, std::vector<std::shared_ptr<TensorOperation>> *result);
59 
60   /// \brief Check whether this pyfunc op is deterministic
61   /// \return True if this pyfunc op is random
62   bool IsRandom();
63 
ReleaseResource()64   Status ReleaseResource() {
65     {
66       py::gil_scoped_acquire gil_acquire;
67       if (py::hasattr(py_func_ptr_, "release_resource")) {
68         // release the executor which is used in the PyFunc
69         // the PyFunc maybe contains vision/nlp/audio transform
70         (void)py_func_ptr_.attr("release_resource")();
71       }
72     }
73     return Status::OK();
74   }
75 
76  private:
77   py::function py_func_ptr_;
78   DataType::Type output_type_;
79 };
80 }  // namespace dataset
81 }  // namespace mindspore
82 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_PY_FUNC_OP_H_
83