• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 #include "minddata/dataset/core/global_context.h"
18 #include "minddata/dataset/core/device_tensor.h"
19 #include "minddata/dataset/kernels/image/dvpp/acl_adapter.h"
20 #include "minddata/dataset/util/status.h"
21 
22 namespace mindspore {
23 namespace dataset {
24 const int kYuvDefaultChannels = 4;
25 
DeviceTensor(const TensorShape & shape,const DataType & type)26 DeviceTensor::DeviceTensor(const TensorShape &shape, const DataType &type)
27     : Tensor(shape, type), device_data_(nullptr), size_(0) {
28   device_data_type_ = type;
29   host_data_tensor_ = nullptr;
30 }
31 
CreateEmpty(const TensorShape & shape,const DataType & type,std::shared_ptr<DeviceTensor> * out)32 Status DeviceTensor::CreateEmpty(const TensorShape &shape, const DataType &type, std::shared_ptr<DeviceTensor> *out) {
33   CHECK_FAIL_RETURN_UNEXPECTED(shape.known(), "Invalid shape.");
34   CHECK_FAIL_RETURN_UNEXPECTED(type != DataType::DE_UNKNOWN, "Invalid data type.");
35   CHECK_FAIL_RETURN_UNEXPECTED(out != nullptr, "Invalid nullptr pointer.");
36   *out = std::make_shared<DeviceTensor>(shape, type);
37   // if it's a string tensor and it has no elements, Just initialize the shape and type.
38   if (!type.IsNumeric() && shape.NumOfElements() == 0) {
39     return Status::OK();
40   }
41 
42   CHECK_FAIL_RETURN_UNEXPECTED(type.IsNumeric(), "Number of elements is not 0. The type should be numeric.");
43   CHECK_FAIL_RETURN_UNEXPECTED(out != nullptr, "Allocate memory faiiled.");
44 
45   int64_t bytes = (*out)->SizeInBytes();
46   // Don't allocate if we have a tensor with no elements.
47   if (bytes != 0) {
48     RETURN_IF_NOT_OK((*out)->AllocateBuffer(bytes));
49   }
50   return Status::OK();
51 }
52 
CreateFromDeviceMemory(const TensorShape & shape,const DataType & type,uint8_t * data_ptr,const uint32_t & dataSize,const std::vector<uint32_t> & attributes,std::shared_ptr<DeviceTensor> * out)53 Status DeviceTensor::CreateFromDeviceMemory(const TensorShape &shape, const DataType &type, uint8_t *data_ptr,
54                                             const uint32_t &dataSize, const std::vector<uint32_t> &attributes,
55                                             std::shared_ptr<DeviceTensor> *out) {
56   CHECK_FAIL_RETURN_UNEXPECTED(shape.known(), "Invalid shape.");
57   CHECK_FAIL_RETURN_UNEXPECTED(type != DataType::DE_UNKNOWN, "Invalid data type.");
58   CHECK_FAIL_RETURN_UNEXPECTED(data_ptr != nullptr, "Data pointer is NULL");
59   CHECK_FAIL_RETURN_UNEXPECTED(dataSize > 0, "Invalid data size");
60   CHECK_FAIL_RETURN_UNEXPECTED(out != nullptr, "Out pointer is NULL");
61 
62   *out = std::make_shared<DeviceTensor>(shape, type);
63   CHECK_FAIL_RETURN_UNEXPECTED(out != nullptr, "Allocate memory failed.");
64 
65   // if it's a string tensor and it has no elements, Just initialize the shape and type.
66   if (!type.IsNumeric() && shape.NumOfElements() == 0) {
67     return Status::OK();
68   }
69 
70   CHECK_FAIL_RETURN_UNEXPECTED(type.IsNumeric(), "Number of elements is not 0. The type should be numeric.");
71 
72   int64_t byte_size = (*out)->SizeInBytes();
73 
74   // Don't allocate if we have a tensor with no elements.
75   if (byte_size != 0) {
76     RETURN_IF_NOT_OK((*out)->AllocateBuffer(byte_size));
77   }
78 
79   CHECK_FAIL_RETURN_UNEXPECTED(attributes.size() >= kYuvDefaultChannels,
80                                "Invalid attributes size, should be greater than 4.");
81   CHECK_FAIL_RETURN_UNEXPECTED(
82     (*out)->SetAttributes(data_ptr, dataSize, attributes[0], attributes[1], attributes[2], attributes[3]),
83     "Fail to set attributes for DeviceTensor");
84 
85   return Status::OK();
86 }
87 
GetHostBuffer()88 const unsigned char *DeviceTensor::GetHostBuffer() {
89   if (AclAdapter::GetInstance().HasAclPlugin()) {
90     Status rc = DataPop_(&host_data_tensor_);
91     if (!rc.IsOk()) {
92       MS_LOG(ERROR) << "Pop device data onto host fail, a nullptr will be returned";
93       return nullptr;
94     }
95   }
96 
97   if (!host_data_tensor_) {
98     return nullptr;
99   }
100   return host_data_tensor_->GetBuffer();
101 }
102 
GetDeviceBuffer()103 const uint8_t *DeviceTensor::GetDeviceBuffer() { return device_data_; }
104 
GetDeviceMutableBuffer()105 uint8_t *DeviceTensor::GetDeviceMutableBuffer() { return device_data_; }
106 
DeviceDataType() const107 DataType DeviceTensor::DeviceDataType() const { return device_data_type_; }
108 
DeviceDataSize()109 uint32_t DeviceTensor::DeviceDataSize() { return size_; }
110 
SetYuvStrideShape_(const uint32_t & width,const uint32_t & widthStride,const uint32_t & height,const uint32_t & heightStride)111 Status DeviceTensor::SetYuvStrideShape_(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height,
112                                         const uint32_t &heightStride) {
113   YUV_shape_ = {width, widthStride, height, heightStride};
114   return Status::OK();
115 }
116 
GetYuvStrideShape()117 std::vector<uint32_t> DeviceTensor::GetYuvStrideShape() { return YUV_shape_; }
118 
SetAttributes(uint8_t * data_ptr,const uint32_t & dataSize,const uint32_t & width,const uint32_t & widthStride,const uint32_t & height,const uint32_t & heightStride)119 Status DeviceTensor::SetAttributes(uint8_t *data_ptr, const uint32_t &dataSize, const uint32_t &width,
120                                    const uint32_t &widthStride, const uint32_t &height, const uint32_t &heightStride) {
121   device_data_ = data_ptr;
122   CHECK_FAIL_RETURN_UNEXPECTED(device_data_ != nullptr, "Fail to get the device data.");
123   RETURN_IF_NOT_OK(SetSize_(dataSize));
124   RETURN_IF_NOT_OK(SetYuvStrideShape_(width, widthStride, height, heightStride));
125   return Status::OK();
126 }
127 
SetSize_(const uint32_t & new_size)128 Status DeviceTensor::SetSize_(const uint32_t &new_size) {
129   size_ = new_size;
130   return Status::OK();
131 }
132 
DataPop_(std::shared_ptr<Tensor> * host_tensor)133 Status DeviceTensor::DataPop_(std::shared_ptr<Tensor> *host_tensor) {
134   CHECK_FAIL_RETURN_UNEXPECTED(host_tensor != nullptr, "host tensor pointer is NULL.");
135   void *resHostBuf = nullptr;
136   APP_ERROR ret = AclAdapter::GetInstance().MallocHost(&resHostBuf, this->DeviceDataSize());
137   if (ret != APP_ERR_OK) {
138     MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
139     return Status(StatusCode::kMDNoSpace);
140   }
141 
142   std::shared_ptr<void> outBuf(resHostBuf, [](void *ptr) { AclAdapter::GetInstance().FreeHost(ptr); });
143   auto processedInfo_ = outBuf;
144   // Memcpy the output data from device to host
145   ret = AclAdapter::GetInstance().Memcpy(outBuf.get(), this->DeviceDataSize(), this->GetDeviceBuffer(),
146                                          this->DeviceDataSize(), 2);  // 2 means ACL_MEMCPY_DEVICE_TO_HOST
147   if (ret != APP_ERR_OK) {
148     MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
149     return Status(StatusCode::kMDOutOfMemory);
150   }
151 
152   auto data = std::static_pointer_cast<unsigned char>(processedInfo_);
153   unsigned char *ret_ptr = data.get();
154 
155   mindspore::dataset::dsize_t dvppDataSize = this->DeviceDataSize();
156   const mindspore::dataset::TensorShape dvpp_shape({dvppDataSize, 1, 1});
157 
158   CHECK_FAIL_RETURN_UNEXPECTED(this->GetYuvStrideShape().size() >= kYuvDefaultChannels,
159                                "Invalid YuvShape, should be greater than 4");
160 
161   uint32_t _output_width_ = this->GetYuvStrideShape()[0];
162   uint32_t _output_widthStride_ = this->GetYuvStrideShape()[1];
163   uint32_t _output_height_ = this->GetYuvStrideShape()[2];
164   uint32_t _output_heightStride_ = this->GetYuvStrideShape()[3];
165   const mindspore::dataset::DataType dvpp_data_type(mindspore::dataset::DataType::DE_UINT8);
166 
167   RETURN_IF_NOT_OK(mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, host_tensor));
168   CHECK_FAIL_RETURN_UNEXPECTED(host_tensor != nullptr, "Allocate memory failed.");
169 
170   (*host_tensor)->SetYuvShape(_output_width_, _output_widthStride_, _output_height_, _output_heightStride_);
171   if (!(*host_tensor)->HasData()) {
172     return Status(StatusCode::kMCDeviceError);
173   }
174 
175   MS_LOG(INFO) << "Successfully pop DeviceTensor data onto host";
176   return Status::OK();
177 }
178 }  // namespace dataset
179 }  // namespace mindspore
180