From f2dce335f2278ddc7bcef1dd47e1e0c901826558 Mon Sep 17 00:00:00 2001 From: Zhenglong Li Date: Thu, 18 Feb 2021 09:54:31 +0800 Subject: [PATCH] Make the resource manager independent, new base class DeviceResource and new derived class AscendResource --- .../ccsrc/minddata/dataset/api/execute.cc | 142 ++++-------------- .../minddata/dataset/core/CMakeLists.txt | 7 + .../minddata/dataset/core/ascend_resource.cc | 98 ++++++++++++ .../minddata/dataset/core/ascend_resource.h | 59 ++++++++ .../minddata/dataset/core/device_resource.cc | 58 +++++++ .../minddata/dataset/core/device_resource.h | 51 +++++++ .../minddata/dataset/core/device_tensor.cc | 13 +- .../minddata/dataset/core/device_tensor.h | 18 +-- .../ccsrc/minddata/dataset/core/tensor.h | 4 - .../ccsrc/minddata/dataset/include/execute.h | 10 +- .../kernels/image/dvpp/dvpp_crop_jpeg_op.cc | 10 +- .../kernels/image/dvpp/dvpp_crop_jpeg_op.h | 4 +- .../kernels/image/dvpp/dvpp_decode_jpeg_op.cc | 10 +- .../kernels/image/dvpp/dvpp_decode_jpeg_op.h | 3 +- .../kernels/image/dvpp/dvpp_decode_png_op.cc | 10 +- .../kernels/image/dvpp/dvpp_decode_png_op.h | 4 +- .../dvpp/dvpp_decode_resize_crop_jpeg_op.cc | 10 +- .../dvpp/dvpp_decode_resize_crop_jpeg_op.h | 4 +- .../image/dvpp/dvpp_decode_resize_jpeg_op.cc | 10 +- .../image/dvpp/dvpp_decode_resize_jpeg_op.h | 4 +- .../kernels/image/dvpp/dvpp_resize_jpeg_op.cc | 10 +- .../kernels/image/dvpp/dvpp_resize_jpeg_op.h | 4 +- .../kernels/image/dvpp/utils/MDAclProcess.cc | 7 +- .../kernels/image/dvpp/utils/MDAclProcess.h | 4 +- .../minddata/dataset/kernels/tensor_op.cc | 5 +- .../minddata/dataset/kernels/tensor_op.h | 9 +- tests/st/cpp/dataset/test_de.cc | 34 +++++ 27 files changed, 425 insertions(+), 177 deletions(-) create mode 100644 mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc create mode 100644 mindspore/ccsrc/minddata/dataset/core/ascend_resource.h create mode 100644 mindspore/ccsrc/minddata/dataset/core/device_resource.cc create mode 100644 mindspore/ccsrc/minddata/dataset/core/device_resource.h diff --git a/mindspore/ccsrc/minddata/dataset/api/execute.cc b/mindspore/ccsrc/minddata/dataset/api/execute.cc index c69cf2d5dc..93ddb05fbb 100644 --- a/mindspore/ccsrc/minddata/dataset/api/execute.cc +++ b/mindspore/ccsrc/minddata/dataset/api/execute.cc @@ -27,108 +27,11 @@ #include "mindspore/lite/src/common/log_adapter.h" #endif #ifdef ENABLE_ACL -#include "acl/acl.h" -#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" -#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" -#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" -#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h" -#include "minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h" +#include "minddata/dataset/core/ascend_resource.h" #endif namespace mindspore { namespace dataset { -#ifdef ENABLE_ACL -class AscendResource { - public: - AscendResource(); - ~AscendResource() = default; - - Status InitChipResource(); - - Status FinalizeChipResource(); - - Status Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input); - - Status Pop(std::shared_ptr device_output, std::shared_ptr *host_output); - - Status DeviceDataRelease(); - - std::shared_ptr processor_; - std::shared_ptr ascend_resource_; -}; - -AscendResource::AscendResource() { InitChipResource(); } - -Status AscendResource::InitChipResource() { - ResourceInfo resource; - resource.aclConfigPath = ""; - resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID()); - ascend_resource_ = ResourceManager::GetInstance(); - APP_ERROR ret = ascend_resource_->InitResource(resource); - if (ret != APP_ERR_OK) { - ascend_resource_->Release(); - std::string err_msg = "Error in Init D-chip:" + std::to_string(ret); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_UNEXPECTED(err_msg); - } - int device_id = *(resource.deviceIds.begin()); - aclrtContext context = ascend_resource_->GetContext(device_id); - processor_ = std::make_shared(context, false); - ret = processor_->InitResource(); - if (ret != APP_ERR_OK) { - ascend_resource_->Release(); - std::string err_msg = "Error in Init resource:" + std::to_string(ret); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_UNEXPECTED(err_msg); - } - MS_LOG(INFO) << "Ascend resource all initialized!"; - return Status::OK(); -} - -Status AscendResource::FinalizeChipResource() { - processor_->Release(); - return Status::OK(); -} - -Status AscendResource::Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input) { - std::shared_ptr de_input; - Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(host_input.Shape()), - MSTypeToDEType(static_cast(host_input.DataType())), - (const uchar *)(host_input.Data().get()), &de_input); - RETURN_IF_NOT_OK(rc); - APP_ERROR ret = processor_->H2D_Sink(de_input, *device_input); - if (ret != APP_ERR_OK) { - ascend_resource_->Release(); - std::string err_msg = "Error in data sink process:" + std::to_string(ret); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_UNEXPECTED(err_msg); - } - MS_LOG(INFO) << "Process data sink successfully"; - return Status::OK(); -} - -Status AscendResource::Pop(std::shared_ptr device_output, std::shared_ptr *host_output) { - APP_ERROR ret = processor_->D2H_Pop(device_output, *host_output); - if (ret != APP_ERR_OK) { - ascend_resource_->Release(); - std::string err_msg = "Error in data pop processing:" + std::to_string(ret); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_UNEXPECTED(err_msg); - } - return Status::OK(); -} - -Status AscendResource::DeviceDataRelease() { - APP_ERROR ret = processor_->device_memory_release(); - if (ret != APP_ERR_OK) { - ascend_resource_->Release(); - std::string err_msg = "Error in device data release:" + std::to_string(ret); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_UNEXPECTED(err_msg); - } - return Status::OK(); -} -#endif Execute::Execute(std::shared_ptr op, std::string deviceType) { ops_.emplace_back(std::move(op)); @@ -136,7 +39,12 @@ Execute::Execute(std::shared_ptr op, std::string deviceType) { MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL if (device_type_ == "Ascend310") { - D_resource_ = std::make_shared(); + device_resource_ = std::make_shared(); + Status rc = device_resource_->InitResource(); + if (!rc.IsOk()) { + device_resource_ = nullptr; + MS_LOG(ERROR) << "Initialize Ascend310 resource fail"; + } } #endif } @@ -146,7 +54,12 @@ Execute::Execute(std::vector> ops, std::string MS_LOG(INFO) << "Running Device: " << device_type_; #ifdef ENABLE_ACL if (device_type_ == "Ascend310") { - D_resource_ = std::make_shared(); + device_resource_ = std::make_shared(); + Status rc = device_resource_->InitResource(); + if (!rc.IsOk()) { + device_resource_ = nullptr; + MS_LOG(ERROR) << "Initialize Ascend310 resource fail"; + } } #endif } @@ -154,7 +67,11 @@ Execute::Execute(std::vector> ops, std::string Execute::~Execute() { #ifdef ENABLE_ACL if (device_type_ == "Ascend310") { - D_resource_->FinalizeChipResource(); + if (device_resource_) { + device_resource_->FinalizeResource(); + } else { + MS_LOG(ERROR) << "Device resource is nullptr which is illegal under case Ascend310"; + } } #endif } @@ -200,11 +117,12 @@ Status Execute::operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output = mindspore::MSTensor(std::make_shared(de_tensor)); } else { // Ascend310 case, where we must set Ascend resource on each operators #ifdef ENABLE_ACL + CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310"); std::shared_ptr device_input; - RETURN_IF_NOT_OK(D_resource_->Sink(input, &device_input)); + RETURN_IF_NOT_OK(device_resource_->Sink(input, &device_input)); for (auto &t : transforms) { std::shared_ptr device_output; - RETURN_IF_NOT_OK(t->SetAscendResource(D_resource_->processor_)); + RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_)); RETURN_IF_NOT_OK(t->Compute(device_input, &device_output)); // For next transform @@ -262,12 +180,13 @@ Status Execute::operator()(const std::vector &input_tensor_list, std:: CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor is not valid"); } else { // Case Ascend310 #ifdef ENABLE_ACL + CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310"); for (auto &input_tensor : input_tensor_list) { std::shared_ptr device_input; - RETURN_IF_NOT_OK(D_resource_->Sink(input_tensor, &device_input)); + RETURN_IF_NOT_OK(device_resource_->Sink(input_tensor, &device_input)); for (auto &t : transforms) { std::shared_ptr device_output; - RETURN_IF_NOT_OK(t->SetAscendResource(D_resource_->processor_)); + RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_)); RETURN_IF_NOT_OK(t->Compute(device_input, &device_output)); // For next transform @@ -275,12 +194,12 @@ Status Execute::operator()(const std::vector &input_tensor_list, std:: } CHECK_FAIL_RETURN_UNEXPECTED(device_input->HasDeviceData(), "Apply transform failed, output tensor has no data"); // Due to the limitation of Ascend310 memory, we have to pop every data onto host memory - // So the speed of this method is slower than solo mode + // So the speed of this batch method is slower than solo mode std::shared_ptr host_output; - RETURN_IF_NOT_OK(D_resource_->Pop(device_input, &host_output)); + RETURN_IF_NOT_OK(device_resource_->Pop(device_input, &host_output)); auto ms_tensor = mindspore::MSTensor(std::make_shared(host_output)); output_tensor_list->emplace_back(ms_tensor); - RETURN_IF_NOT_OK(D_resource_->DeviceDataRelease()); + RETURN_IF_NOT_OK(device_resource_->DeviceDataRelease()); } CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor vector is empty"); #endif @@ -297,17 +216,16 @@ Status Execute::validate_device_() { return Status::OK(); } -#ifdef ENABLE_ACL Status Execute::DeviceMemoryRelease() { - Status rc = D_resource_->DeviceDataRelease(); + CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310"); + Status rc = device_resource_->DeviceDataRelease(); if (rc.IsError()) { - D_resource_->ascend_resource_->Release(); std::string err_msg = "Error in device data release"; MS_LOG(ERROR) << err_msg; RETURN_STATUS_UNEXPECTED(err_msg); } return Status::OK(); } -#endif + } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/core/CMakeLists.txt b/mindspore/ccsrc/minddata/dataset/core/CMakeLists.txt index c03dcf3263..dafb6a186d 100644 --- a/mindspore/ccsrc/minddata/dataset/core/CMakeLists.txt +++ b/mindspore/ccsrc/minddata/dataset/core/CMakeLists.txt @@ -5,6 +5,7 @@ set(DATASET_CORE_SRC_FILES config_manager.cc cv_tensor.cc data_type.cc + device_resource.cc device_tensor.cc de_tensor.cc global_context.cc @@ -14,6 +15,12 @@ set(DATASET_CORE_SRC_FILES tensor_shape.cc ) +if(ENABLE_ACL) + set(DATASET_CORE_SRC_FILES + ${DATASET_CORE_SRC_FILES} + ascend_resource.cc) +endif() + ms_protobuf_generate(EXAMPLE_SRCS EXAMPLE_HDRS example.proto) ms_protobuf_generate(FEATURE_SRCS FEATURE_HDRS feature.proto) add_library(core OBJECT ${DATASET_CORE_SRC_FILES} ${EXAMPLE_SRCS} ${FEATURE_SRCS}) diff --git a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc new file mode 100644 index 0000000000..faad212318 --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc @@ -0,0 +1,98 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/api/context.h" +#include "include/api/types.h" +#include "minddata/dataset/include/type_id.h" +#include "minddata/dataset/core/ascend_resource.h" + +namespace mindspore { +namespace dataset { + +Status AscendResource::InitResource() { + ResourceInfo resource; + resource.aclConfigPath = ""; + resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID()); + ascend_resource_ = ResourceManager::GetInstance(); + APP_ERROR ret = ascend_resource_->InitResource(resource); + if (ret != APP_ERR_OK) { + ascend_resource_->Release(); + std::string err_msg = "Error in Init D-chip:" + std::to_string(ret); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_UNEXPECTED(err_msg); + } + int device_id = *(resource.deviceIds.begin()); + aclrtContext context = ascend_resource_->GetContext(device_id); + processor_ = std::make_shared(context, false); + ret = processor_->InitResource(); + if (ret != APP_ERR_OK) { + ascend_resource_->Release(); + std::string err_msg = "Error in Init resource:" + std::to_string(ret); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_UNEXPECTED(err_msg); + } + MS_LOG(INFO) << "Ascend resource all initialized!"; + return Status::OK(); +} + +Status AscendResource::FinalizeResource() { + processor_->Release(); + return Status::OK(); +} + +Status AscendResource::Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input) { + std::shared_ptr de_input; + Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(host_input.Shape()), + MSTypeToDEType(static_cast(host_input.DataType())), + (const uchar *)(host_input.Data().get()), &de_input); + RETURN_IF_NOT_OK(rc); + APP_ERROR ret = processor_->H2D_Sink(de_input, *device_input); + if (ret != APP_ERR_OK) { + ascend_resource_->Release(); + std::string err_msg = "Error in data sink process:" + std::to_string(ret); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_UNEXPECTED(err_msg); + } + MS_LOG(INFO) << "Process data sink successfully"; + return Status::OK(); +} + +Status AscendResource::Pop(const std::shared_ptr &device_output, std::shared_ptr *host_output) { + APP_ERROR ret = processor_->D2H_Pop(device_output, *host_output); + if (ret != APP_ERR_OK) { + ascend_resource_->Release(); + std::string err_msg = "Error in data pop processing:" + std::to_string(ret); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_UNEXPECTED(err_msg); + } + return Status::OK(); +} + +Status AscendResource::DeviceDataRelease() { + APP_ERROR ret = processor_->device_memory_release(); + if (ret != APP_ERR_OK) { + ascend_resource_->Release(); + std::string err_msg = "Error in device data release:" + std::to_string(ret); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_UNEXPECTED(err_msg); + } + return Status::OK(); +} + +std::shared_ptr AscendResource::GetInstance() { return processor_; } + +} // namespace dataset +} // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h new file mode 100644 index 0000000000..48f3c1711a --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h @@ -0,0 +1,59 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_ +#define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_ + +#include +#include +#include "acl/acl.h" +#include "minddata/dataset/core/device_resource.h" +#include "minddata/dataset/core/device_tensor.h" +#include "minddata/dataset/core/tensor.h" +#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h" +#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" +#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" + +namespace mindspore { +namespace dataset { + +class AscendResource : public DeviceResource { + public: + AscendResource() = default; + ~AscendResource() = default; + + Status InitResource() override; + + Status FinalizeResource() override; + + Status Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input) override; + + Status Pop(const std::shared_ptr &device_output, std::shared_ptr *host_output) override; + + std::shared_ptr GetInstance() override; + + Status DeviceDataRelease() override; + + private: + std::shared_ptr processor_; + std::shared_ptr ascend_resource_; +}; + +} // namespace dataset +} // namespace mindspore + +#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_ diff --git a/mindspore/ccsrc/minddata/dataset/core/device_resource.cc b/mindspore/ccsrc/minddata/dataset/core/device_resource.cc new file mode 100644 index 0000000000..1bfe905070 --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/core/device_resource.cc @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "minddata/dataset/core/device_resource.h" + +namespace mindspore { +namespace dataset { + +Status DeviceResource::InitResource() { + return Status(StatusCode::kMDUnexpectedError, + "Is this a valid device? If yes, please implement this InitResource() in the derived class."); +} + +Status DeviceResource::FinalizeResource() { + return Status(StatusCode::kMDUnexpectedError, + "Is this a valid device? If yes, please implement this FinalizeResource() in the derived class."); +} + +Status DeviceResource::Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input) { + return Status(StatusCode::kMDUnexpectedError, + "Is this a valid device whose device memory is available? If yes, please implement this Sink() in the " + "derived class."); +} + +Status DeviceResource::Pop(const std::shared_ptr &device_output, std::shared_ptr *host_output) { + return Status(StatusCode::kMDUnexpectedError, + "Is this a valid device whose device memory is available? If yes, please implement this Pop() in the " + "derived class."); +} + +Status DeviceResource::DeviceDataRelease() { + return Status( + StatusCode::kMDUnexpectedError, + "Is this a valid device whose device memory is available? If yes, please implement this DeviceDataRelease() in the " + "derived class."); +} + +std::shared_ptr DeviceResource::GetInstance() { + MS_LOG(ERROR) << "Is this a device which contains a processor object? If yes, please implement this GetInstance() in " + "the derived class"; + return nullptr; +} + +} // namespace dataset +} // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/core/device_resource.h b/mindspore/ccsrc/minddata/dataset/core/device_resource.h new file mode 100644 index 0000000000..107d9c944c --- /dev/null +++ b/mindspore/ccsrc/minddata/dataset/core/device_resource.h @@ -0,0 +1,51 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_RESOURCE_H_ +#define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_RESOURCE_H_ + +#include +#include "include/api/context.h" +#include "include/api/status.h" +#include "include/api/types.h" +#include "minddata/dataset/core/device_tensor.h" +#include "minddata/dataset/core/tensor.h" + +namespace mindspore { +namespace dataset { + +class DeviceResource { + public: + DeviceResource() = default; + + virtual ~DeviceResource() = default; + + virtual Status InitResource(); + + virtual Status FinalizeResource(); + + virtual Status Sink(const mindspore::MSTensor &host_input, std::shared_ptr *device_input); + + virtual Status Pop(const std::shared_ptr &device_output, std::shared_ptr *host_output); + + virtual std::shared_ptr GetInstance(); + + virtual Status DeviceDataRelease(); +}; + +} // namespace dataset +} // namespace mindspore +#endif // MINDSPORE_DEVICE_RESOURCE_H diff --git a/mindspore/ccsrc/minddata/dataset/core/device_tensor.cc b/mindspore/ccsrc/minddata/dataset/core/device_tensor.cc index 618ff90e12..b8f504247c 100644 --- a/mindspore/ccsrc/minddata/dataset/core/device_tensor.cc +++ b/mindspore/ccsrc/minddata/dataset/core/device_tensor.cc @@ -15,8 +15,8 @@ */ #include "minddata/dataset/core/global_context.h" -#include "minddata/dataset/core/tensor.h" #include "minddata/dataset/core/device_tensor.h" +#include "minddata/dataset/util/status.h" namespace mindspore { namespace dataset { @@ -28,15 +28,14 @@ Status DeviceTensor::SetYuvStrideShape_(const uint32_t &width, const uint32_t &w std::vector DeviceTensor::GetYuvStrideShape() { return YUV_shape_; } -#ifdef ENABLE_ACL -Status DeviceTensor::SetAttributes(const std::shared_ptr &data_ptr) { - device_data_ = data_ptr->data; +Status DeviceTensor::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) { + device_data_ = data_ptr; CHECK_FAIL_RETURN_UNEXPECTED(device_data_ != nullptr, "Fail to get the device data."); - SetSize_(data_ptr->dataSize); - SetYuvStrideShape_(data_ptr->width, data_ptr->widthStride, data_ptr->height, data_ptr->heightStride); + SetSize_(dataSize); + SetYuvStrideShape_(width, widthStride, height, heightStride); return Status::OK(); } -#endif DeviceTensor::DeviceTensor(const TensorShape &shape, const DataType &type) : Tensor(shape, type) { // grab the mem pool from global context and create the allocator for char data area diff --git a/mindspore/ccsrc/minddata/dataset/core/device_tensor.h b/mindspore/ccsrc/minddata/dataset/core/device_tensor.h index 445dcb1e24..bcebe4dfb4 100644 --- a/mindspore/ccsrc/minddata/dataset/core/device_tensor.h +++ b/mindspore/ccsrc/minddata/dataset/core/device_tensor.h @@ -14,18 +14,15 @@ * limitations under the License. */ -#ifndef MINDSPORE_DEVICE_TENSOR_H -#define MINDSPORE_DEVICE_TENSOR_H +#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ +#define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ #include #include #include #include "include/api/status.h" -#include "minddata/dataset/core/tensor.h" -#ifdef ENABLE_ACL -#include "minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h" -#endif #include "minddata/dataset/core/constants.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/tensor.h" #include "minddata/dataset/util/status.h" namespace mindspore { @@ -36,9 +33,10 @@ class DeviceTensor : public Tensor { DeviceTensor(const TensorShape &shape, const DataType &type); ~DeviceTensor() {} -#ifdef ENABLE_ACL - Status SetAttributes(const std::shared_ptr &data); -#endif + + Status 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); + static Status CreateEmpty(const TensorShape &shape, const DataType &type, std::shared_ptr *out); uint8_t *GetDeviceBuffer(); @@ -62,4 +60,4 @@ class DeviceTensor : public Tensor { } // namespace dataset } // namespace mindspore -#endif // MINDSPORE_DEVICE_TENSOR_H +#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_TENSOR_H_ diff --git a/mindspore/ccsrc/minddata/dataset/core/tensor.h b/mindspore/ccsrc/minddata/dataset/core/tensor.h index 0b9b5ebfee..d176053ddc 100644 --- a/mindspore/ccsrc/minddata/dataset/core/tensor.h +++ b/mindspore/ccsrc/minddata/dataset/core/tensor.h @@ -423,7 +423,6 @@ class Tensor { static Status GetBufferInfo(Tensor *t, py::buffer_info *out); #endif -#ifdef ENABLE_ACL Status SetYuvShape(const uint32_t &width, const uint32_t &widthStride, const uint32_t &height, const uint32_t &heightStride) { std::vector tmp{width, widthStride, height, heightStride}; @@ -432,7 +431,6 @@ class Tensor { } std::vector GetYuvShape() { return yuv_shape_; } -#endif /// TensorIterator is a linear iterator that can be used to iterate over the elements of the Tensor /// The order elements is as the memory layout (i.e., row-major) [[1,2,3],[4,5,6] --> 1,2,3,4,5,6 @@ -697,10 +695,8 @@ class Tensor { /// pointer to the end of the physical data unsigned char *data_end_ = nullptr; -#ifdef ENABLE_ACL /// shape for interpretation of YUV image std::vector yuv_shape_; -#endif private: friend class DETensor; diff --git a/mindspore/ccsrc/minddata/dataset/include/execute.h b/mindspore/ccsrc/minddata/dataset/include/execute.h index 986c42e5c8..87afbf9034 100644 --- a/mindspore/ccsrc/minddata/dataset/include/execute.h +++ b/mindspore/ccsrc/minddata/dataset/include/execute.h @@ -22,12 +22,12 @@ #include #include "include/api/context.h" #include "include/api/types.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/include/constants.h" #include "minddata/dataset/include/transforms.h" namespace mindspore { namespace dataset { -class AscendResource; // Class to manage the resource of Ascend310 // class to run tensor operations in eager mode class Execute { @@ -51,9 +51,8 @@ class Execute { /// \param[out] out Result tensor after transform /// \return - Status Status operator()(const std::vector &input_tensor_list, std::vector *out); -#ifdef ENABLE_ACL + Status DeviceMemoryRelease(); -#endif private: Status validate_device_(); @@ -61,9 +60,8 @@ class Execute { std::vector> ops_; std::string device_type_; -#ifdef ENABLE_ACL - std::shared_ptr D_resource_; -#endif + + std::shared_ptr device_resource_; }; } // namespace dataset diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.cc index 48907d0f5f..5fbf7ef854 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.cc @@ -46,7 +46,8 @@ Status DvppCropJpegOp::Compute(const std::shared_ptr &input, std:: const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(CropOut); + (*output)->SetAttributes(CropOut->data, CropOut->dataSize, CropOut->width, CropOut->widthStride, CropOut->height, + CropOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from device memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -136,8 +137,11 @@ Status DvppCropJpegOp::OutputShape(const std::vector &inputs, std:: return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape"); } -Status DvppCropJpegOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppCropJpegOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } processor_->SetCropParas(crop_width_, crop_height_); return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h index 8fa94f8d5d..5080dbb22c 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h @@ -24,9 +24,11 @@ #include "acl/acl.h" #include "mindspore/core/utils/log_adapter.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/device_tensor.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" #include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" #include "minddata/dataset/kernels/tensor_op.h" #include "minddata/dataset/util/status.h" @@ -48,7 +50,7 @@ class DvppCropJpegOp : public TensorOp { std::string Name() const override { return kDvppCropJpegOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: uint32_t crop_height_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.cc index 2ddc4dddd5..ecc7a0aeda 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.cc @@ -43,7 +43,8 @@ Status DvppDecodeJpegOp::Compute(const std::shared_ptr &input, std const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(DecodeOut); + (*output)->SetAttributes(DecodeOut->data, DecodeOut->dataSize, DecodeOut->width, DecodeOut->widthStride, + DecodeOut->height, DecodeOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -121,8 +122,11 @@ Status DvppDecodeJpegOp::Compute(const std::shared_ptr &input, std::shar return Status::OK(); } -Status DvppDecodeJpegOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppDecodeJpegOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h index b616ea9d55..ac821dd88a 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h @@ -24,6 +24,7 @@ #include "acl/acl.h" #include "mindspore/core/utils/log_adapter.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" #include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" @@ -48,7 +49,7 @@ class DvppDecodeJpegOp : public TensorOp { std::string Name() const override { return kDvppDecodeJpegOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: std::shared_ptr processor_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.cc index 881fc78eb8..42f1cb6f60 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.cc @@ -42,7 +42,8 @@ Status DvppDecodePngOp::Compute(const std::shared_ptr &input, std: const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(DecodeOut); + (*output)->SetAttributes(DecodeOut->data, DecodeOut->dataSize, DecodeOut->width, DecodeOut->widthStride, + DecodeOut->height, DecodeOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -129,8 +130,11 @@ Status DvppDecodePngOp::OutputShape(const std::vector &inputs, std: return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape"); } -Status DvppDecodePngOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppDecodePngOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h index b53605fa91..5b2560d365 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h @@ -24,8 +24,10 @@ #include "acl/acl.h" #include "mindspore/core/utils/log_adapter.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" #include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" #include "minddata/dataset/kernels/tensor_op.h" #include "minddata/dataset/util/status.h" @@ -47,7 +49,7 @@ class DvppDecodePngOp : public TensorOp { std::string Name() const override { return kDvppDecodePngOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: std::shared_ptr processor_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.cc index 244e47a04a..48415066d8 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.cc @@ -43,7 +43,8 @@ Status DvppDecodeResizeCropJpegOp::Compute(const std::shared_ptr & const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(CropOut); + (*output)->SetAttributes(CropOut->data, CropOut->dataSize, CropOut->width, CropOut->widthStride, CropOut->height, + CropOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -127,8 +128,11 @@ Status DvppDecodeResizeCropJpegOp::OutputShape(const std::vector &i return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape"); } -Status DvppDecodeResizeCropJpegOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppDecodeResizeCropJpegOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } processor_->SetResizeParas(resized_width_, resized_height_); processor_->SetCropParas(crop_width_, crop_height_); return Status::OK(); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h index 2e22558699..21001150f6 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h @@ -24,9 +24,11 @@ #include "acl/acl.h" #include "mindspore/core/utils/log_adapter.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" #include "minddata/dataset/kernels/tensor_op.h" #include "minddata/dataset/util/status.h" @@ -51,7 +53,7 @@ class DvppDecodeResizeCropJpegOp : public TensorOp { std::string Name() const override { return kDvppDecodeResizeCropJpegOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: int32_t crop_height_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.cc index b7c5909ea6..2dc0af11e2 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.cc @@ -42,7 +42,8 @@ Status DvppDecodeResizeJpegOp::Compute(const std::shared_ptr &inpu const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(ResizeOut); + (*output)->SetAttributes(ResizeOut->data, ResizeOut->dataSize, ResizeOut->width, ResizeOut->widthStride, + ResizeOut->height, ResizeOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -125,8 +126,11 @@ Status DvppDecodeResizeJpegOp::OutputShape(const std::vector &input return Status(StatusCode::kMDUnexpectedError, "Input has a wrong shape"); } -Status DvppDecodeResizeJpegOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppDecodeResizeJpegOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } processor_->SetResizeParas(resized_width_, resized_height_); return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h index 947a4a4584..be51712ff7 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h @@ -24,8 +24,10 @@ #include "acl/acl.h" #include "mindspore/core/utils/log_adapter.h" #include "minddata/dataset/core/data_type.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" #include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" #include "minddata/dataset/kernels/tensor_op.h" #include "minddata/dataset/util/status.h" @@ -48,7 +50,7 @@ class DvppDecodeResizeJpegOp : public TensorOp { std::string Name() const override { return kDvppDecodeResizeJpegOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: int32_t resized_height_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.cc index a83fb72b78..3693bcad98 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.cc @@ -47,7 +47,8 @@ Status DvppResizeJpegOp::Compute(const std::shared_ptr &input, std const TensorShape dvpp_shape({1, 1, 1}); const DataType dvpp_data_type(DataType::DE_UINT8); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, output); - (*output)->SetAttributes(ResizeOut); // Set attributes for output DeviceTensor + (*output)->SetAttributes(ResizeOut->data, ResizeOut->dataSize, ResizeOut->width, ResizeOut->widthStride, + ResizeOut->height, ResizeOut->heightStride); if (!((*output)->HasDeviceData())) { std::string error = "[ERROR] Fail to get the Output result from device memory!"; RETURN_STATUS_UNEXPECTED(error); @@ -128,8 +129,11 @@ Status DvppResizeJpegOp::Compute(const std::shared_ptr &input, std::shar return Status::OK(); } -Status DvppResizeJpegOp::SetAscendResource(const std::shared_ptr &processor) { - processor_ = processor; +Status DvppResizeJpegOp::SetAscendResource(const std::shared_ptr &resource) { + processor_ = std::static_pointer_cast(resource->GetInstance()); + if (!processor_) { + RETURN_STATUS_UNEXPECTED("Resource initialize fail, please check your env"); + } processor_->SetResizeParas(resized_width_, resized_height_); return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h index c269abe5e7..c8c1241caf 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h @@ -24,8 +24,10 @@ #include "acl/acl.h" #include "minddata/dataset/core/data_type.h" #include "minddata/dataset/core/device_tensor.h" +#include "minddata/dataset/core/device_resource.h" #include "minddata/dataset/core/tensor.h" #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" +#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" #include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h" #include "minddata/dataset/kernels/tensor_op.h" #include "minddata/dataset/util/status.h" @@ -49,7 +51,7 @@ class DvppResizeJpegOp : public TensorOp { std::string Name() const override { return kDvppDecodeResizeJpegOp; } - Status SetAscendResource(const std::shared_ptr &processor) override; + Status SetAscendResource(const std::shared_ptr &resource) override; private: int32_t resized_height_; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc index 09d51fdb2c..82fd708c7c 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc @@ -157,7 +157,7 @@ std::shared_ptr MDAclProcess::GetDeviceModule() { return dvppCommon_ * Sink data from Tensor(On host) to DeviceTensor(On device) * Two cases are different, jpeg and png */ -APP_ERROR MDAclProcess::H2D_Sink(std::shared_ptr &input, +APP_ERROR MDAclProcess::H2D_Sink(const std::shared_ptr &input, std::shared_ptr &device_input) { RawData imageinfo; uint32_t filesize = input->SizeInBytes(); @@ -181,11 +181,12 @@ APP_ERROR MDAclProcess::H2D_Sink(std::shared_ptr &in const mindspore::dataset::DataType dvpp_data_type(mindspore::dataset::DataType::DE_UINT8); const mindspore::dataset::TensorShape dvpp_shape({1, 1, 1}); mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, &device_input); - device_input->SetAttributes(deviceInputData); + device_input->SetAttributes(deviceInputData->data, deviceInputData->dataSize, deviceInputData->width, + deviceInputData->widthStride, deviceInputData->height, deviceInputData->heightStride); return APP_ERR_OK; } -APP_ERROR MDAclProcess::D2H_Pop(std::shared_ptr &device_output, +APP_ERROR MDAclProcess::D2H_Pop(const std::shared_ptr &device_output, std::shared_ptr &output) { void *resHostBuf = nullptr; APP_ERROR ret = aclrtMallocHost(&resHostBuf, device_output->DeviceDataSize()); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h index 3e875a9601..63540b0e5f 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h @@ -84,10 +84,10 @@ class MDAclProcess { // API for access device memory of decode data std::shared_ptr Get_Decode_DeviceData(); - APP_ERROR H2D_Sink(std::shared_ptr &input, + APP_ERROR H2D_Sink(const std::shared_ptr &input, std::shared_ptr &device_input); - APP_ERROR D2H_Pop(std::shared_ptr &device_output, + APP_ERROR D2H_Pop(const std::shared_ptr &device_output, std::shared_ptr &output); // D-chip memory release diff --git a/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.cc index bec8049a67..3a2ed4f731 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.cc @@ -70,11 +70,10 @@ Status TensorOp::OutputType(const std::vector &inputs, std::vector &processor) { + +Status TensorOp::SetAscendResource(const std::shared_ptr &resource) { return Status(StatusCode::kMDUnexpectedError, "This is a CPU operator which doesn't have Ascend Resource. Please verify your context"); } -#endif } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h b/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h index 78765a8fb9..e2731d0319 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h @@ -25,9 +25,7 @@ #include "minddata/dataset/core/tensor_row.h" #include "minddata/dataset/util/status.h" #include "minddata/dataset/core/device_tensor.h" -#ifdef ENABLE_ACL -#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h" -#endif +#include "minddata/dataset/core/device_resource.h" #define IO_CHECK(input, output) \ do { \ @@ -216,9 +214,8 @@ class TensorOp { virtual std::string Name() const = 0; virtual Status to_json(nlohmann::json *out_json) { return Status::OK(); } -#ifdef ENABLE_ACL - virtual Status SetAscendResource(const std::shared_ptr &processor); -#endif + + virtual Status SetAscendResource(const std::shared_ptr &resource); protected: bool is_deterministic_{true}; diff --git a/tests/st/cpp/dataset/test_de.cc b/tests/st/cpp/dataset/test_de.cc index 02cb4e7818..2387deb850 100644 --- a/tests/st/cpp/dataset/test_de.cc +++ b/tests/st/cpp/dataset/test_de.cc @@ -126,3 +126,37 @@ TEST_F(TestDE, TestDvppSinkMode) { Transform.DeviceMemoryRelease(); #endif } + +TEST_F(TestDE, TestDvppDecodeResizeCrop) { +#ifdef ENABLE_ACL + std::shared_ptr de_tensor; + mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor); + auto image = MSTensor(std::make_shared(de_tensor)); + + // Define dvpp transform + std::vector crop_paras = {416}; + std::vector resize_paras = {512}; + mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_paras, resize_paras), "Ascend310"); + + // Apply transform on images + Status rc = Transform(image, &image); + + // Check image info + ASSERT_TRUE(rc.IsOk()); + ASSERT_EQ(image.Shape().size(), 2); + int32_t real_h = 0; + int32_t real_w = 0; + int32_t remainder = crop_paras[crop_paras.size() - 1] % 16; + if (crop_paras.size() == 1) { + real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1; + real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder; + } else { + real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1; + real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder; + } + ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte + ASSERT_EQ(image.Shape()[1], real_w); + ASSERT_EQ(image.DataSize(), 1.5 * real_w * real_h); + Transform.DeviceMemoryRelease(); +#endif +}