• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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/de_tensor.h"
18 #include "minddata/dataset/core/device_tensor.h"
19 #include "minddata/dataset/include/dataset/constants.h"
20 #include "minddata/dataset/core/type_id.h"
21 #include "minddata/dataset/util/log_adapter.h"
22 #ifndef ENABLE_ANDROID
23 #define EXCEPTION_IF_NULL(ptr) MS_EXCEPTION_IF_NULL(ptr)
24 #else
25 #define EXCEPTION_IF_NULL(ptr) MS_ASSERT((ptr) != nullptr)
26 #endif
27 
28 namespace mindspore {
29 namespace dataset {
30 
DETensor(std::shared_ptr<dataset::Tensor> tensor_impl)31 DETensor::DETensor(std::shared_ptr<dataset::Tensor> tensor_impl)
32     : tensor_impl_(tensor_impl),
33       name_("MindDataTensor"),
34       type_(static_cast<mindspore::DataType>(DETypeToMSType(tensor_impl_->type()))),
35       shape_(tensor_impl_->shape().AsVector()),
36       is_device_(false) {}
37 
38 #ifndef ENABLE_ANDROID
DETensor(std::shared_ptr<dataset::DeviceTensor> device_tensor_impl,bool is_device)39 DETensor::DETensor(std::shared_ptr<dataset::DeviceTensor> device_tensor_impl, bool is_device)
40     : device_tensor_impl_(device_tensor_impl), name_("MindDataDeviceTensor"), is_device_(is_device) {
41   // The sequence of shape_ is (width, widthStride, height, heightStride) in Dvpp module
42   // We need to add [1]widthStride and [3]heightStride, which are actual YUV image shape, into shape_ attribute
43   if (device_tensor_impl && device_tensor_impl->GetYuvStrideShape().size() > 0) {
44     uint8_t flag = 0;
45     for (auto &i : device_tensor_impl->GetYuvStrideShape()) {
46       if (flag % 2 == 1) {
47         int64_t j = static_cast<int64_t>(i);
48         shape_.emplace_back(j);
49       }
50       ++flag;
51     }
52     std::reverse(shape_.begin(), shape_.end());
53   }
54   MS_LOG(INFO) << "This is a YUV420 format image, one pixel takes 1.5 bytes. Therefore, the shape of"
55                << " image is in (H, W) format. You can search for more information about YUV420 format";
56 }
57 #endif
58 
Name() const59 const std::string &DETensor::Name() const { return name_; }
60 
DataType() const61 enum mindspore::DataType DETensor::DataType() const {
62 #ifndef ENABLE_ANDROID
63   if (is_device_) {
64     EXCEPTION_IF_NULL(device_tensor_impl_);
65     return static_cast<mindspore::DataType>(DETypeToMSType(device_tensor_impl_->DeviceDataType()));
66   }
67 #endif
68   EXCEPTION_IF_NULL(tensor_impl_);
69   return static_cast<mindspore::DataType>(DETypeToMSType(tensor_impl_->type()));
70 }
71 
DataSize() const72 size_t DETensor::DataSize() const {
73 #ifndef ENABLE_ANDROID
74   if (is_device_) {
75     EXCEPTION_IF_NULL(device_tensor_impl_);
76     return device_tensor_impl_->DeviceDataSize();
77   }
78 #endif
79   EXCEPTION_IF_NULL(tensor_impl_);
80   return static_cast<size_t>(tensor_impl_->SizeInBytes());
81 }
82 
Shape() const83 const std::vector<int64_t> &DETensor::Shape() const { return shape_; }
84 
ElementNum() const85 int64_t DETensor::ElementNum() const {
86   if (shape_.empty()) {
87     // element number of scalar is 1
88     return 1;
89   }
90   return std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies<int64_t>());
91 }
92 
Data() const93 std::shared_ptr<const void> DETensor::Data() const {
94 #ifndef ENABLE_ANDROID
95   if (is_device_) {
96     EXCEPTION_IF_NULL(device_tensor_impl_);
97     return std::shared_ptr<const void>(device_tensor_impl_->GetHostBuffer(), [](const void *) {});
98   }
99 #endif
100   return std::shared_ptr<const void>(tensor_impl_->GetBuffer(), [](const void *) {});
101 }
102 
MutableData()103 void *DETensor::MutableData() {
104 #ifndef ENABLE_ANDROID
105   if (is_device_) {
106     EXCEPTION_IF_NULL(device_tensor_impl_);
107     return static_cast<void *>(device_tensor_impl_->GetDeviceMutableBuffer());
108   }
109 #endif
110   EXCEPTION_IF_NULL(tensor_impl_);
111   return static_cast<void *>(tensor_impl_->GetMutableBuffer());
112 }
113 
IsDevice() const114 bool DETensor::IsDevice() const { return is_device_; }
115 
Clone() const116 std::shared_ptr<mindspore::MSTensor::Impl> DETensor::Clone() const {
117 #ifndef ENABLE_ANDROID
118   if (is_device_) {
119     EXCEPTION_IF_NULL(device_tensor_impl_);
120     return std::make_shared<DETensor>(device_tensor_impl_, is_device_);
121   }
122 #endif
123   return std::make_shared<DETensor>(tensor_impl_);
124 }
125 }  // namespace dataset
126 }  // namespace mindspore
127