1 /**
2 * Copyright 2020 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 #include "minddata/dataset/engine/consumers/python_tree_consumer.h"
18
19 namespace mindspore::dataset {
GetNextAsList(py::list * out)20 Status PythonIteratorConsumer::GetNextAsList(py::list *out) {
21 RETURN_UNEXPECTED_IF_NULL(out);
22 std::vector<TensorPtr> row;
23 {
24 py::gil_scoped_release gil_release;
25 RETURN_IF_NOT_OK(GetNextAsVector(&row));
26 }
27 for (auto el : row) {
28 (*out).append(el);
29 }
30 return Status::OK();
31 }
32
GetNextAsDict(py::dict * out)33 Status PythonIteratorConsumer::GetNextAsDict(py::dict *out) {
34 RETURN_UNEXPECTED_IF_NULL(out);
35 std::vector<std::pair<std::string, std::shared_ptr<Tensor>>> vec;
36 Status s;
37 {
38 py::gil_scoped_release gil_release;
39 s = GetNextAsOrderedPair(&vec);
40 }
41 RETURN_IF_NOT_OK(s);
42 // Generate Python dict, python dict maintains its insertion order
43 for (const auto &pair : vec) {
44 (*out)[common::SafeCStr(pair.first)] = pair.second;
45 }
46 return Status::OK();
47 }
48
Start()49 Status PythonBuildVocabConsumer::Start() {
50 py::gil_scoped_release gil_release;
51 return BuildVocabConsumer::Start();
52 }
53
Save()54 Status PythonSaveToDisk::Save() {
55 py::gil_scoped_release gil_release;
56 return SaveToDisk::Save();
57 }
58
PythonSaveToDisk(const std::string & datasetPath,int32_t numFiles,const std::string & datasetType)59 PythonSaveToDisk::PythonSaveToDisk(const std::string &datasetPath, int32_t numFiles, const std::string &datasetType)
60 : SaveToDisk(datasetPath, numFiles, datasetType) {}
61
GetRow(TensorRow * const r)62 Status PythonTreeGetters::GetRow(TensorRow *const r) {
63 py::gil_scoped_release gil_release;
64 return TreeGetters::GetRow(r);
65 }
GetRow(const std::shared_ptr<TreeAdapter> & tree_adapter,TensorRow * r)66 Status PythonDatasetSizeGetter::GetRow(const std::shared_ptr<TreeAdapter> &tree_adapter, TensorRow *r) {
67 RETURN_UNEXPECTED_IF_NULL(tree_adapter);
68 RETURN_UNEXPECTED_IF_NULL(r);
69 py::gil_scoped_release gil_release;
70 return DatasetSizeGetter::GetRow(tree_adapter, r);
71 }
72 } // namespace mindspore::dataset
73