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