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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ 19 #include <memory> 20 #include <utility> 21 #include <vector> 22 #include "include/api/status.h" 23 #include "minddata/dataset/include/dataset/constants.h" 24 #include "minddata/dataset/core/data_type.h" 25 #include "minddata/dataset/core/tensor.h" 26 #include "minddata/dataset/util/status.h" 27 28 namespace mindspore { 29 namespace dataset { 30 class Tensor; 31 class DeviceTensor : public Tensor { 32 public: 33 DeviceTensor(const TensorShape &shape, const DataType &type); 34 ~DeviceTensor()35 ~DeviceTensor() {} 36 37 Status SetAttributes(uint8_t *data_ptr, const uint32_t &dataSize, const uint32_t &width, const uint32_t &widthStride, 38 const uint32_t &height, const uint32_t &heightStride); 39 40 static Status CreateEmpty(const TensorShape &shape, const DataType &type, std::shared_ptr<DeviceTensor> *out); 41 42 static Status CreateFromDeviceMemory(const TensorShape &shape, const DataType &type, uint8_t *data_ptr, 43 const uint32_t &dataSize, const std::vector<uint32_t> &attributes, 44 std::shared_ptr<DeviceTensor> *out); 45 46 const unsigned char *GetHostBuffer(); 47 48 const uint8_t *GetDeviceBuffer(); 49 50 uint8_t *GetDeviceMutableBuffer(); 51 52 std::vector<uint32_t> GetYuvStrideShape(); 53 54 uint32_t DeviceDataSize(); 55 56 DataType DeviceDataType() const; 57 HasDeviceData()58 bool HasDeviceData() { return device_data_ != nullptr; } 59 60 private: 61 Status SetSize_(const uint32_t &new_size); 62 63 Status SetYuvStrideShape_(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height, 64 const uint32_t &heightStride); 65 66 #ifdef ENABLE_ACL 67 Status DataPop_(std::shared_ptr<Tensor> *host_tensor); 68 #endif 69 70 std::vector<uint32_t> YUV_shape_; // YUV_shape_ = {width, widthStride, height, heightStride} 71 72 uint8_t *device_data_; 73 74 uint32_t size_; 75 76 DataType device_data_type_; 77 78 // We use this Tensor to store device_data when DeviceTensor pop onto host 79 std::shared_ptr<Tensor> host_data_tensor_; 80 }; 81 82 } // namespace dataset 83 } // namespace mindspore 84 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ 85