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/gnn/graph_feature_parser.h"
18
19 #include <memory>
20 #include <utility>
21
22 #include "mindspore/ccsrc/minddata/mindrecord/include/shard_error.h"
23
24 namespace mindspore {
25 namespace dataset {
26 namespace gnn {
27
28 using mindrecord::MSRStatus;
29
GraphFeatureParser(const ShardColumn & shard_column)30 GraphFeatureParser::GraphFeatureParser(const ShardColumn &shard_column) {
31 shard_column_ = std::make_unique<ShardColumn>(shard_column);
32 }
33
LoadFeatureTensor(const std::string & key,const std::vector<uint8_t> & col_blob,std::shared_ptr<Tensor> * tensor)34 Status GraphFeatureParser::LoadFeatureTensor(const std::string &key, const std::vector<uint8_t> &col_blob,
35 std::shared_ptr<Tensor> *tensor) {
36 const unsigned char *data = nullptr;
37 std::unique_ptr<unsigned char[]> data_ptr;
38 uint64_t n_bytes = 0, col_type_size = 1;
39 mindrecord::ColumnDataType col_type = mindrecord::ColumnNoDataType;
40 std::vector<int64_t> column_shape;
41 RETURN_IF_NOT_OK(shard_column_->GetColumnValueByName(key, col_blob, {}, &data, &data_ptr, &n_bytes, &col_type,
42 &col_type_size, &column_shape));
43 if (data == nullptr) {
44 data = reinterpret_cast<const unsigned char *>(&data_ptr[0]);
45 }
46 RETURN_IF_NOT_OK(Tensor::CreateFromMemory(std::move(TensorShape({static_cast<dsize_t>(n_bytes / col_type_size)})),
47 std::move(DataType(mindrecord::ColumnDataTypeNameNormalized[col_type])),
48 data, tensor));
49 return Status::OK();
50 }
51
52 #if !defined(_WIN32) && !defined(_WIN64)
LoadFeatureToSharedMemory(const std::string & key,const std::vector<uint8_t> & col_blob,GraphSharedMemory * shared_memory,std::shared_ptr<Tensor> * out_tensor)53 Status GraphFeatureParser::LoadFeatureToSharedMemory(const std::string &key, const std::vector<uint8_t> &col_blob,
54 GraphSharedMemory *shared_memory,
55 std::shared_ptr<Tensor> *out_tensor) {
56 const unsigned char *data = nullptr;
57 std::unique_ptr<unsigned char[]> data_ptr;
58 uint64_t n_bytes = 0, col_type_size = 1;
59 mindrecord::ColumnDataType col_type = mindrecord::ColumnNoDataType;
60 std::vector<int64_t> column_shape;
61 RETURN_IF_NOT_OK(shard_column_->GetColumnValueByName(key, col_blob, {}, &data, &data_ptr, &n_bytes, &col_type,
62 &col_type_size, &column_shape));
63 if (data == nullptr) {
64 data = reinterpret_cast<const unsigned char *>(&data_ptr[0]);
65 }
66 std::shared_ptr<Tensor> tensor;
67 RETURN_IF_NOT_OK(Tensor::CreateEmpty(std::move(TensorShape({2})), std::move(DataType(DataType::DE_INT64)), &tensor));
68 auto fea_itr = tensor->begin<int64_t>();
69 int64_t offset = 0;
70 RETURN_IF_NOT_OK(shared_memory->InsertData(data, n_bytes, &offset));
71 *fea_itr = offset;
72 ++fea_itr;
73 *fea_itr = n_bytes;
74 *out_tensor = std::move(tensor);
75 return Status::OK();
76 }
77 #endif
78
LoadFeatureIndex(const std::string & key,const std::vector<uint8_t> & col_blob,std::vector<int32_t> * indices)79 Status GraphFeatureParser::LoadFeatureIndex(const std::string &key, const std::vector<uint8_t> &col_blob,
80 std::vector<int32_t> *indices) {
81 const unsigned char *data = nullptr;
82 std::unique_ptr<unsigned char[]> data_ptr;
83 uint64_t n_bytes = 0, col_type_size = 1;
84 mindrecord::ColumnDataType col_type = mindrecord::ColumnNoDataType;
85 std::vector<int64_t> column_shape;
86 RETURN_IF_NOT_OK(shard_column_->GetColumnValueByName(key, col_blob, {}, &data, &data_ptr, &n_bytes, &col_type,
87 &col_type_size, &column_shape));
88
89 if (data == nullptr) {
90 data = reinterpret_cast<const unsigned char *>(&data_ptr[0]);
91 }
92
93 for (int i = 0; i < n_bytes; i += col_type_size) {
94 int32_t feature_ind = -1;
95 if (col_type == mindrecord::ColumnInt32) {
96 feature_ind = *(reinterpret_cast<const int32_t *>(data + i));
97 } else if (col_type == mindrecord::ColumnInt64) {
98 feature_ind = *(reinterpret_cast<const int64_t *>(data + i));
99 } else {
100 RETURN_STATUS_UNEXPECTED("Feature Index needs to be int32/int64 type!");
101 }
102 if (feature_ind >= 0) {
103 indices->push_back(feature_ind);
104 }
105 }
106 return Status::OK();
107 }
108
109 } // namespace gnn
110 } // namespace dataset
111 } // namespace mindspore
112