!9844 New Dvpp operation for Ascend 310 chip
From: @lizhenglong1992 Reviewed-by: Signed-off-by:pull/9844/MERGE
commit
280db3d651
@ -0,0 +1,6 @@
|
|||||||
|
file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
|
||||||
|
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
|
||||||
|
add_subdirectory(utils)
|
||||||
|
add_library(kernels-dvpp-image OBJECT
|
||||||
|
dvpp_decode_resize_crop_jpeg_op.cc)
|
||||||
|
add_dependencies(kernels-dvpp-image dvpp-utils)
|
@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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 <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/AclProcess.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||||
|
#include "minddata/dataset/core/data_type.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
Status DvppDecodeResizeCropJpegOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
if (!IsNonEmptyJPEG(input)) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("SoftDvppDecodeReiszeJpegOp only support process jpeg image.");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(input->GetBuffer() != nullptr, "The input image buffer is empty.");
|
||||||
|
unsigned char *buffer = const_cast<unsigned char *>(input->GetBuffer());
|
||||||
|
RawData imageInfo;
|
||||||
|
uint32_t filesize = input->SizeInBytes();
|
||||||
|
imageInfo.lenOfByte = filesize;
|
||||||
|
imageInfo.data = std::make_shared<uint8_t>();
|
||||||
|
imageInfo.data.reset(new uint8_t[filesize], std::default_delete<uint8_t[]>());
|
||||||
|
memcpy_s(imageInfo.data.get(), filesize, buffer, filesize);
|
||||||
|
// First part end, whose function is to transform data from a Tensor to imageinfo data structure which can be
|
||||||
|
// applied on device
|
||||||
|
ResourceInfo resource;
|
||||||
|
resource.aclConfigPath = "";
|
||||||
|
resource.deviceIds.insert(0); // 0 is device id which should be refined later!
|
||||||
|
std::shared_ptr<ResourceManager> instance = ResourceManager::GetInstance();
|
||||||
|
APP_ERROR ret = instance->InitResource(resource);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
instance->Release();
|
||||||
|
std::string error = "Error in Init D-chip:" + std::to_string(ret);
|
||||||
|
RETURN_STATUS_UNEXPECTED(error);
|
||||||
|
}
|
||||||
|
int deviceId = *(resource.deviceIds.begin());
|
||||||
|
aclrtContext context = instance->GetContext(deviceId);
|
||||||
|
// Second part end where we initialize the resource of D chip and set up all configures
|
||||||
|
AclProcess process(resized_width_, resized_height_, crop_width_, crop_height_, context);
|
||||||
|
process.set_mode(true);
|
||||||
|
ret = process.InitResource();
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
instance->Release();
|
||||||
|
std::string error = "Error in Init resource:" + std::to_string(ret);
|
||||||
|
RETURN_STATUS_UNEXPECTED(error);
|
||||||
|
}
|
||||||
|
ret = process.Process(imageInfo);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
instance->Release();
|
||||||
|
std::string error = "Error in dvpp processing:" + std::to_string(ret);
|
||||||
|
RETURN_STATUS_UNEXPECTED(error);
|
||||||
|
}
|
||||||
|
// Third part end where we execute the core function of dvpp
|
||||||
|
auto data = std::static_pointer_cast<unsigned char>(process.Get_Memory_Data());
|
||||||
|
unsigned char *ret_ptr = data.get();
|
||||||
|
std::shared_ptr(DvppDataInfo) CropOut = process.Get_Device_Memory_Data();
|
||||||
|
dsize_t dvpp_length = CropOut->dataSize;
|
||||||
|
const TensorShape dvpp_shape({dvpp_length, 1, 1});
|
||||||
|
const DataType dvpp_data_type(DataType::DE_UINT8);
|
||||||
|
mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, output);
|
||||||
|
if (!((*output)->HasData())) {
|
||||||
|
std::string error = "[ERROR] Fail to get the Output result from memory!";
|
||||||
|
RETURN_STATUS_UNEXPECTED(error);
|
||||||
|
}
|
||||||
|
process.device_memory_release();
|
||||||
|
// Last part end where we transform the processed data into a tensor which can be applied in later units.
|
||||||
|
} catch (const cv::Exception &e) {
|
||||||
|
std::string error = "[ERROR] Fail in DvppDecodeResizeCropJpegOp:" + std::string(e.what());
|
||||||
|
RETURN_STATUS_UNEXPECTED(error);
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status DvppDecodeResizeCropJpegOp::OutputShape(const std::vector<TensorShape> &inputs,
|
||||||
|
std::vector<TensorShape> &outputs) {
|
||||||
|
RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs));
|
||||||
|
outputs.clear();
|
||||||
|
TensorShape out({-1, 1, 1}); // we don't know what is output image size, but we know it should be 3 channels
|
||||||
|
if (inputs[0].Rank() == 1) outputs.emplace_back(out);
|
||||||
|
if (!outputs.empty()) return Status::OK();
|
||||||
|
return Status(StatusCode::kUnexpectedError, "Input has a wrong shape");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H
|
||||||
|
#define MINDSPORE_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
#include "minddata/dataset/core/data_type.h"
|
||||||
|
#include "mindspore/core/utils/log_adapter.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||||
|
#include "acl/acl.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
class DvppDecodeResizeCropJpegOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
DvppDecodeResizeCropJpegOp(int32_t crop_height, int32_t crop_width, int32_t resized_height, int32_t resized_width)
|
||||||
|
: crop_height_(crop_height),
|
||||||
|
crop_width_(crop_width),
|
||||||
|
resized_height_(resized_height),
|
||||||
|
resized_width_(resized_width) {}
|
||||||
|
|
||||||
|
/// \brief Destructor
|
||||||
|
~DvppDecodeResizeCropJpegOp() = default;
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kDvppDecodeResizeCropJpegOp; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int32_t crop_height_;
|
||||||
|
int32_t crop_width_;
|
||||||
|
int32_t resized_height_;
|
||||||
|
int32_t resized_width_;
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_DVPP_DECODE_RESIZE_CROP_JPEG_OP_H
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,86 @@
|
|||||||
|
#include <climits>
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 ACLMANAGER_H
|
||||||
|
#define ACLMANAGER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <string.h>
|
||||||
|
#include <map>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include "acl/acl.h"
|
||||||
|
#include "CommonDataType.h"
|
||||||
|
#include "mindspore/core/utils/log_adapter.h"
|
||||||
|
#include "ErrorCode.h"
|
||||||
|
#include "DvppCommon.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
mode_t SetFileDefaultUmask();
|
||||||
|
|
||||||
|
class AclProcess {
|
||||||
|
public:
|
||||||
|
AclProcess(uint32_t resizeWidth, uint32_t resizeHeight, uint32_t cropWidth, uint32_t cropHeight, aclrtContext context,
|
||||||
|
aclrtStream stream = nullptr, std::shared_ptr<DvppCommon> dvppCommon = nullptr);
|
||||||
|
|
||||||
|
~AclProcess(){};
|
||||||
|
|
||||||
|
// Release all the resource
|
||||||
|
APP_ERROR Release();
|
||||||
|
// Create resource for this sample
|
||||||
|
APP_ERROR InitResource();
|
||||||
|
// Process the result
|
||||||
|
APP_ERROR Process(RawData &ImageInfo);
|
||||||
|
// API for access memory
|
||||||
|
std::shared_ptr<void> Get_Memory_Data();
|
||||||
|
// API for access device memory
|
||||||
|
std::shared_ptr<DvppDataInfo> Get_Device_Memory_Data();
|
||||||
|
// change output method
|
||||||
|
void set_mode(bool flag);
|
||||||
|
// Get the mode of Acl process
|
||||||
|
bool get_mode();
|
||||||
|
// Save the result
|
||||||
|
APP_ERROR WriteResult(uint32_t fileSize, std::shared_ptr<void> outBuf, std::string filename);
|
||||||
|
// Color space reform
|
||||||
|
void YUV420TOYUV444(unsigned char *inputBuffer, unsigned char *outputBuffer, int w, int h);
|
||||||
|
// Crop definition
|
||||||
|
void CropConfigFilter(CropRoiConfig &cfg, DvppCropInputInfo &cropinfo);
|
||||||
|
// D-chip memory release
|
||||||
|
void device_memory_release();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Initialize the modules used by this sample
|
||||||
|
APP_ERROR InitModule();
|
||||||
|
// Preprocess the input image
|
||||||
|
APP_ERROR Preprocess(RawData &ImageInfo);
|
||||||
|
// Filename process
|
||||||
|
APP_ERROR RenameFile(std::string &filename);
|
||||||
|
|
||||||
|
aclrtContext context_;
|
||||||
|
aclrtStream stream_;
|
||||||
|
std::shared_ptr<DvppCommon> dvppCommon_; // dvpp object
|
||||||
|
std::shared_ptr<void> processedInfo_; // processed data
|
||||||
|
uint32_t resizeWidth_; // dvpp resize width
|
||||||
|
uint32_t resizeHeight_; // dvpp resize height
|
||||||
|
uint32_t cropWidth_; // dvpp crop width
|
||||||
|
uint32_t cropHeight_; // dvpp crop height
|
||||||
|
bool repeat_; // Repeatly process image or not
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,11 @@
|
|||||||
|
file(GLOB_RECURSE _CURRENT_SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
|
||||||
|
set_property(SOURCE ${_CURRENT_SRC_FILES} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_MD)
|
||||||
|
|
||||||
|
add_definitions(-DENABLE_DVPP_INTERFACE)
|
||||||
|
|
||||||
|
add_library(dvpp-utils OBJECT
|
||||||
|
AclProcess.cc
|
||||||
|
DvppCommon.cc
|
||||||
|
ErrorCode.cpp
|
||||||
|
ResourceManager.cc
|
||||||
|
)
|
@ -0,0 +1,187 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 COMMONDATATYPE_H
|
||||||
|
#define COMMONDATATYPE_H
|
||||||
|
#ifndef ENABLE_DVPP_INTERFACE
|
||||||
|
#define ENABLE_DVPP_INTERFACE
|
||||||
|
#endif
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include "acl/acl.h"
|
||||||
|
#include "acl/ops/acl_dvpp.h"
|
||||||
|
|
||||||
|
#define DVPP_ALIGN_UP(x, align) ((((x) + ((align)-1)) / (align)) * (align))
|
||||||
|
|
||||||
|
const uint32_t VIDEO_H264 = 0;
|
||||||
|
const uint32_t VIDEO_H265 = 1;
|
||||||
|
|
||||||
|
const float SEC2MS = 1000.0;
|
||||||
|
const uint32_t VIDEO_PROCESS_THREAD = 16;
|
||||||
|
const int YUV_BGR_SIZE_CONVERT_3 = 3;
|
||||||
|
const int YUV_BGR_SIZE_CONVERT_2 = 2;
|
||||||
|
const int DVPP_JPEG_OFFSET = 8;
|
||||||
|
const int VPC_WIDTH_ALIGN = 16;
|
||||||
|
const int VPC_HEIGHT_ALIGN = 2;
|
||||||
|
const int JPEG_WIDTH_ALIGN = 128;
|
||||||
|
const int JPEG_HEIGHT_ALIGN = 16;
|
||||||
|
const int VPC_OFFSET_ALIGN = 2;
|
||||||
|
|
||||||
|
// Tensor Descriptor
|
||||||
|
struct Tensor {
|
||||||
|
aclDataType dataType; // Tensor data type
|
||||||
|
int numDim; // Number of dimensions of Tensor
|
||||||
|
std::vector<int64_t> dims; // Dimension vector
|
||||||
|
aclFormat format; // Format of tensor, e.g. ND, NCHW, NC1HWC0
|
||||||
|
std::string name; // Name of tensor
|
||||||
|
};
|
||||||
|
|
||||||
|
// Data type of tensor
|
||||||
|
enum OpAttrType {
|
||||||
|
BOOL = 0,
|
||||||
|
INT = 1,
|
||||||
|
FLOAT = 2,
|
||||||
|
STRING = 3,
|
||||||
|
LIST_BOOL = 4,
|
||||||
|
LIST_INT = 6,
|
||||||
|
LIST_FLOAT = 7,
|
||||||
|
LIST_STRING = 8,
|
||||||
|
LIST_LIST_INT = 9,
|
||||||
|
};
|
||||||
|
|
||||||
|
// operator attribution describe
|
||||||
|
// type decide whether the other attribute needed to set a value
|
||||||
|
struct OpAttr {
|
||||||
|
std::string name;
|
||||||
|
OpAttrType type;
|
||||||
|
int num; // LIST_BOOL/INT/FLOAT/STRING/LIST_LIST_INT need
|
||||||
|
uint8_t numBool; // BOOL need
|
||||||
|
int64_t numInt; // INT need
|
||||||
|
float numFloat; // FLOAT need
|
||||||
|
std::string numString; // STRING need
|
||||||
|
std::vector<uint8_t> valuesBool; // LIST_BOOL need
|
||||||
|
std::vector<int64_t> valuesInt; // LIST_INT need
|
||||||
|
std::vector<float> valuesFloat; // LIST_FLOAT need
|
||||||
|
std::vector<std::string> valuesString; // LIST_STRING need
|
||||||
|
std::vector<int> numLists; // LIST_LIST_INT need
|
||||||
|
std::vector<std::vector<int64_t>> valuesListList; // LIST_LIST_INT need
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of image data
|
||||||
|
struct ImageInfo {
|
||||||
|
uint32_t width; // Image width
|
||||||
|
uint32_t height; // Image height
|
||||||
|
uint32_t lenOfByte; // Size of image data, bytes
|
||||||
|
std::shared_ptr<uint8_t> data; // Smart pointer of image data
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of data in device
|
||||||
|
struct RawData {
|
||||||
|
size_t lenOfByte; // Size of memory, bytes
|
||||||
|
std::shared_ptr<void> data; // Smart pointer of data
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of data in device
|
||||||
|
struct StreamData {
|
||||||
|
size_t size; // Size of memory, bytes
|
||||||
|
std::shared_ptr<void> data; // Smart pointer of data
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of stream data
|
||||||
|
struct StreamInfo {
|
||||||
|
std::string format;
|
||||||
|
uint32_t height;
|
||||||
|
uint32_t width;
|
||||||
|
uint32_t channelId;
|
||||||
|
std::string streamPath;
|
||||||
|
};
|
||||||
|
|
||||||
|
// define the structure of an rectangle
|
||||||
|
struct Rectangle {
|
||||||
|
uint32_t leftTopX;
|
||||||
|
uint32_t leftTopY;
|
||||||
|
uint32_t rightBottomX;
|
||||||
|
uint32_t rightBottomY;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ObjectDetectInfo {
|
||||||
|
int32_t classId;
|
||||||
|
float confidence;
|
||||||
|
Rectangle location;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum VpcProcessType {
|
||||||
|
VPC_PT_DEFAULT = 0,
|
||||||
|
VPC_PT_PADDING, // Resize with locked ratio and paste on upper left corner
|
||||||
|
VPC_PT_FIT, // Resize with locked ratio and paste on middle location
|
||||||
|
VPC_PT_FILL, // Resize with locked ratio and paste on whole locatin, the input image may be cropped
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DvppDataInfo {
|
||||||
|
uint32_t width = 0; // Width of image
|
||||||
|
uint32_t height = 0; // Height of image
|
||||||
|
uint32_t widthStride = 0; // Width after align up
|
||||||
|
uint32_t heightStride = 0; // Height after align up
|
||||||
|
acldvppPixelFormat format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // Format of image
|
||||||
|
uint32_t frameId = 0; // Needed by video
|
||||||
|
uint32_t dataSize = 0; // Size of data in byte
|
||||||
|
uint8_t *data = nullptr; // Image data
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CropRoiConfig {
|
||||||
|
uint32_t left;
|
||||||
|
uint32_t right;
|
||||||
|
uint32_t down;
|
||||||
|
uint32_t up;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DvppCropInputInfo {
|
||||||
|
DvppDataInfo dataInfo;
|
||||||
|
CropRoiConfig roi;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of matrix info
|
||||||
|
struct MatrixInfo {
|
||||||
|
uint32_t row = 0; // row of matrix
|
||||||
|
uint32_t col = 0; // col of matrix
|
||||||
|
uint32_t dataSize = 0; // size of memory, bytes
|
||||||
|
std::shared_ptr<void> data = nullptr; // data of matrix
|
||||||
|
aclDataType dataType = ACL_FLOAT16; // data Type of matrix
|
||||||
|
};
|
||||||
|
|
||||||
|
// Description of coefficient info
|
||||||
|
struct CoefficientInfo {
|
||||||
|
std::shared_ptr<void> data = nullptr; // data of coefficient
|
||||||
|
aclDataType dataType = ACL_FLOAT16; // dataType
|
||||||
|
};
|
||||||
|
|
||||||
|
// define the input of BLAS operator such as producing:
|
||||||
|
// C = alpha * A * B + beta * C
|
||||||
|
struct BlasInput {
|
||||||
|
MatrixInfo A;
|
||||||
|
MatrixInfo B;
|
||||||
|
MatrixInfo C;
|
||||||
|
CoefficientInfo alpha;
|
||||||
|
CoefficientInfo beta;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern bool g_vdecNotified[VIDEO_PROCESS_THREAD];
|
||||||
|
extern bool g_vpcNotified[VIDEO_PROCESS_THREAD];
|
||||||
|
extern bool g_inferNotified[VIDEO_PROCESS_THREAD];
|
||||||
|
extern bool g_postNotified[VIDEO_PROCESS_THREAD];
|
||||||
|
|
||||||
|
#endif
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,220 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 DVPP_COMMON_H
|
||||||
|
#define DVPP_COMMON_H
|
||||||
|
|
||||||
|
#include "CommonDataType.h"
|
||||||
|
#include "ErrorCode.h"
|
||||||
|
|
||||||
|
#include "acl/ops/acl_dvpp.h"
|
||||||
|
|
||||||
|
const int MODULUS_NUM_2 = 2;
|
||||||
|
const uint32_t ODD_NUM_1 = 1;
|
||||||
|
|
||||||
|
struct Rect {
|
||||||
|
/* left location of the rectangle */
|
||||||
|
uint32_t x;
|
||||||
|
/* top location of the rectangle */
|
||||||
|
uint32_t y;
|
||||||
|
/* with of the rectangle */
|
||||||
|
uint32_t width;
|
||||||
|
/* height of the rectangle */
|
||||||
|
uint32_t height;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DvppBaseData {
|
||||||
|
uint32_t dataSize; // Size of data in byte
|
||||||
|
uint8_t *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VdecConfig {
|
||||||
|
int inputWidth = 0;
|
||||||
|
int inputHeight = 0;
|
||||||
|
acldvppStreamFormat inFormat = H264_MAIN_LEVEL; // stream format renference acldvppStreamFormat
|
||||||
|
acldvppPixelFormat outFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // output format renference acldvppPixelFormat
|
||||||
|
uint32_t channelId = 0; // user define channelId: 0-15
|
||||||
|
uint32_t deviceId = 0;
|
||||||
|
pthread_t threadId = 0; // thread for callback
|
||||||
|
aclvdecCallback callback = {0}; // user define how to process vdec out data
|
||||||
|
bool runflag = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DeviceStreamData {
|
||||||
|
std::vector<ObjectDetectInfo> detectResult;
|
||||||
|
uint32_t framId;
|
||||||
|
uint32_t channelId;
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint32_t JPEGD_STRIDE_WIDTH = 128; // Jpegd module output width need to align up to 128
|
||||||
|
const uint32_t JPEGD_STRIDE_HEIGHT = 16; // Jpegd module output height need to align up to 16
|
||||||
|
const uint32_t JPEGE_STRIDE_WIDTH = 16; // Jpege module input width need to align up to 16
|
||||||
|
const uint32_t JPEGE_STRIDE_HEIGHT = 1; // Jpege module input height remains unchanged
|
||||||
|
const uint32_t VPC_STRIDE_WIDTH = 16; // Vpc module output width need to align up to 16
|
||||||
|
const uint32_t VPC_STRIDE_HEIGHT = 2; // Vpc module output height need to align up to 2
|
||||||
|
const uint32_t VDEC_STRIDE_WIDTH = 16; // Vdec module output width need to align up to 16
|
||||||
|
const uint32_t VDEC_STRIDE_HEIGHT = 2; // Vdec module output width need to align up to 2
|
||||||
|
const uint32_t YUV_BYTES_NU = 3; // Numerator of yuv image, H x W x 3 / 2
|
||||||
|
const uint32_t YUV_BYTES_DE = 2; // Denominator of yuv image, H x W x 3 / 2
|
||||||
|
const uint32_t YUV422_WIDTH_NU = 2; // Width of YUV422, WidthStride = Width * 2
|
||||||
|
const uint32_t YUV444_RGB_WIDTH_NU = 3; // Width of YUV444 and RGB888, WidthStride = Width * 3
|
||||||
|
const uint32_t XRGB_WIDTH_NU = 4; // Width of XRGB8888, WidthStride = Width * 4
|
||||||
|
const uint32_t JPEG_OFFSET = 8; // Offset of input file for jpegd module
|
||||||
|
const uint32_t MAX_JPEGD_WIDTH = 8192; // Max width of jpegd module
|
||||||
|
const uint32_t MAX_JPEGD_HEIGHT = 8192; // Max height of jpegd module
|
||||||
|
const uint32_t MIN_JPEGD_WIDTH = 32; // Min width of jpegd module
|
||||||
|
const uint32_t MIN_JPEGD_HEIGHT = 32; // Min height of jpegd module
|
||||||
|
const uint32_t MAX_JPEGE_WIDTH = 8192; // Max width of jpege module
|
||||||
|
const uint32_t MAX_JPEGE_HEIGHT = 8192; // Max height of jpege module
|
||||||
|
const uint32_t MIN_JPEGE_WIDTH = 32; // Min width of jpege module
|
||||||
|
const uint32_t MIN_JPEGE_HEIGHT = 32; // Min height of jpege module
|
||||||
|
const uint32_t MAX_RESIZE_WIDTH = 4096; // Max width stride of resize module
|
||||||
|
const uint32_t MAX_RESIZE_HEIGHT = 4096; // Max height stride of resize module
|
||||||
|
const uint32_t MIN_RESIZE_WIDTH = 32; // Min width stride of resize module
|
||||||
|
const uint32_t MIN_RESIZE_HEIGHT = 6; // Min height stride of resize module
|
||||||
|
const float MIN_RESIZE_SCALE = 0.03125; // Min resize scale of resize module
|
||||||
|
const float MAX_RESIZE_SCALE = 16.0; // Min resize scale of resize module
|
||||||
|
const uint32_t MAX_VPC_WIDTH = 4096; // Max width of picture to VPC(resize/crop)
|
||||||
|
const uint32_t MAX_VPC_HEIGHT = 4096; // Max height of picture to VPC(resize/crop)
|
||||||
|
const uint32_t MIN_VPC_WIDTH = 32; // Min width of picture to VPC(resize/crop)
|
||||||
|
const uint32_t MIN_VPC_HEIGHT = 6; // Min height of picture to VPC(resize/crop)
|
||||||
|
const uint32_t MIN_CROP_WIDTH = 10; // Min width of crop area
|
||||||
|
const uint32_t MIN_CROP_HEIGHT = 6; // Min height of crop area
|
||||||
|
const uint8_t YUV_GREYER_VALUE = 128; // Filling value of the resized YUV image
|
||||||
|
|
||||||
|
#define CONVERT_TO_ODD(NUM) (((NUM) % MODULUS_NUM_2 != 0) ? (NUM) : ((NUM)-1)) // Convert the input to odd num
|
||||||
|
#define CONVERT_TO_EVEN(NUM) (((NUM) % MODULUS_NUM_2 == 0) ? (NUM) : ((NUM)-1)) // Convert the input to even num
|
||||||
|
#define CHECK_ODD(num) ((num) % MODULUS_NUM_2 != 0)
|
||||||
|
#define CHECK_EVEN(num) ((num) % MODULUS_NUM_2 == 0)
|
||||||
|
#define RELEASE_DVPP_DATA(dvppDataPtr) \
|
||||||
|
do { \
|
||||||
|
APP_ERROR retMacro; \
|
||||||
|
if (dvppDataPtr != nullptr) { \
|
||||||
|
retMacro = acldvppFree(dvppDataPtr); \
|
||||||
|
if (retMacro != APP_ERR_OK) { \
|
||||||
|
MS_LOG(ERROR) << "Failed to free memory on dvpp, ret = " << retMacro << "."; \
|
||||||
|
} \
|
||||||
|
dvppDataPtr = nullptr; \
|
||||||
|
} \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
class DvppCommon {
|
||||||
|
public:
|
||||||
|
explicit DvppCommon(aclrtStream dvppStream);
|
||||||
|
explicit DvppCommon(const VdecConfig &vdecConfig); // Need by vdec
|
||||||
|
~DvppCommon();
|
||||||
|
APP_ERROR Init(void);
|
||||||
|
APP_ERROR InitVdec(); // Needed by vdec
|
||||||
|
APP_ERROR DeInit(void);
|
||||||
|
|
||||||
|
static APP_ERROR GetVpcDataSize(uint32_t widthVpc, uint32_t heightVpc, acldvppPixelFormat format, uint32_t &vpcSize);
|
||||||
|
static APP_ERROR GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||||
|
uint32_t &widthStride, uint32_t &heightStride);
|
||||||
|
static APP_ERROR GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||||
|
uint32_t &widthStride, uint32_t &heightStride);
|
||||||
|
static void GetJpegDecodeStrideSize(uint32_t width, uint32_t height, uint32_t &widthStride, uint32_t &heightStride);
|
||||||
|
static APP_ERROR GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t &width, uint32_t &height,
|
||||||
|
int32_t &components);
|
||||||
|
static APP_ERROR GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format,
|
||||||
|
uint32_t &decSize);
|
||||||
|
static APP_ERROR GetJpegEncodeStrideSize(std::shared_ptr<DvppDataInfo> &input);
|
||||||
|
static APP_ERROR SetEncodeLevel(uint32_t level, acldvppJpegeConfig &jpegeConfig);
|
||||||
|
static APP_ERROR GetVideoDecodeStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||||
|
uint32_t &widthStride, uint32_t &heightStride);
|
||||||
|
static APP_ERROR GetVideoDecodeDataSize(uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||||
|
uint32_t &vdecSize);
|
||||||
|
|
||||||
|
// The following interfaces can be called only when the DvppCommon object is initialized with Init
|
||||||
|
APP_ERROR VpcResize(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize,
|
||||||
|
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||||
|
APP_ERROR VpcCrop(const DvppCropInputInfo &input, const DvppDataInfo &output, bool withSynchronize);
|
||||||
|
APP_ERROR JpegDecode(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize);
|
||||||
|
|
||||||
|
APP_ERROR JpegEncode(DvppDataInfo &input, DvppDataInfo &output, acldvppJpegeConfig *jpegeConfig,
|
||||||
|
bool withSynchronize);
|
||||||
|
|
||||||
|
APP_ERROR GetJpegEncodeDataSize(DvppDataInfo &input, acldvppJpegeConfig *jpegeConfig, uint32_t &encSize);
|
||||||
|
|
||||||
|
// These functions started with "Combine" encapsulate the DVPP process together, malloc DVPP memory,
|
||||||
|
// transfer pictures from host to device, and then execute the DVPP operation.
|
||||||
|
// The caller needs to pay attention to the release of the memory alloced in these functions.
|
||||||
|
// You can call the ReleaseDvppBuffer function to release memory after use completely.
|
||||||
|
APP_ERROR CombineResizeProcess(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize,
|
||||||
|
VpcProcessType processType = VPC_PT_DEFAULT);
|
||||||
|
APP_ERROR CombineCropProcess(DvppCropInputInfo &input, DvppDataInfo &output, bool withSynchronize);
|
||||||
|
APP_ERROR CombineJpegdProcess(const RawData &imageInfo, acldvppPixelFormat format, bool withSynchronize);
|
||||||
|
APP_ERROR CombineJpegeProcess(const RawData &imageInfo, uint32_t width, uint32_t height, acldvppPixelFormat format,
|
||||||
|
bool withSynchronize);
|
||||||
|
// The following interface can be called only when the DvppCommon object is initialized with InitVdec
|
||||||
|
APP_ERROR CombineVdecProcess(std::shared_ptr<DvppDataInfo> data, void *userData);
|
||||||
|
|
||||||
|
// Get the private member variables which are assigned in the interfaces which are started with "Combine"
|
||||||
|
std::shared_ptr<DvppDataInfo> GetInputImage();
|
||||||
|
std::shared_ptr<DvppDataInfo> GetDecodedImage();
|
||||||
|
std::shared_ptr<DvppDataInfo> GetResizedImage();
|
||||||
|
std::shared_ptr<DvppDataInfo> GetEncodedImage();
|
||||||
|
std::shared_ptr<DvppDataInfo> GetCropedImage();
|
||||||
|
|
||||||
|
// Release the memory that is allocated in the interfaces which are started with "Combine"
|
||||||
|
void ReleaseDvppBuffer();
|
||||||
|
APP_ERROR VdecSendEosFrame() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
APP_ERROR SetDvppPicDescData(const DvppDataInfo &dataInfo, acldvppPicDesc &picDesc);
|
||||||
|
APP_ERROR ResizeProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, bool withSynchronize);
|
||||||
|
APP_ERROR ResizeWithPadding(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, CropRoiConfig &cropRoi,
|
||||||
|
CropRoiConfig &pasteRoi, bool withSynchronize);
|
||||||
|
void GetCropRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType,
|
||||||
|
CropRoiConfig &cropRoi);
|
||||||
|
void GetPasteRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType,
|
||||||
|
CropRoiConfig &pasteRoi);
|
||||||
|
APP_ERROR CropProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, const CropRoiConfig &cropArea,
|
||||||
|
bool withSynchronize);
|
||||||
|
APP_ERROR CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output);
|
||||||
|
APP_ERROR CheckCropParams(const DvppCropInputInfo &input);
|
||||||
|
APP_ERROR TransferImageH2D(const RawData &imageInfo, const std::shared_ptr<DvppDataInfo> &jpegInput);
|
||||||
|
APP_ERROR CreateStreamDesc(std::shared_ptr<DvppDataInfo> data);
|
||||||
|
APP_ERROR DestroyResource();
|
||||||
|
|
||||||
|
std::shared_ptr<acldvppRoiConfig> cropAreaConfig_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppRoiConfig> pasteAreaConfig_ = nullptr;
|
||||||
|
|
||||||
|
std::shared_ptr<acldvppPicDesc> cropInputDesc_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppPicDesc> cropOutputDesc_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppRoiConfig> cropRoiConfig_ = nullptr;
|
||||||
|
|
||||||
|
std::shared_ptr<acldvppPicDesc> encodeInputDesc_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppJpegeConfig> jpegeConfig_ = nullptr;
|
||||||
|
|
||||||
|
std::shared_ptr<acldvppPicDesc> resizeInputDesc_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppPicDesc> resizeOutputDesc_ = nullptr;
|
||||||
|
std::shared_ptr<acldvppResizeConfig> resizeConfig_ = nullptr;
|
||||||
|
|
||||||
|
std::shared_ptr<acldvppPicDesc> decodeOutputDesc_ = nullptr;
|
||||||
|
|
||||||
|
acldvppChannelDesc *dvppChannelDesc_ = nullptr;
|
||||||
|
aclrtStream dvppStream_ = nullptr;
|
||||||
|
std::shared_ptr<DvppDataInfo> inputImage_ = nullptr;
|
||||||
|
std::shared_ptr<DvppDataInfo> decodedImage_ = nullptr;
|
||||||
|
std::shared_ptr<DvppDataInfo> encodedImage_ = nullptr;
|
||||||
|
std::shared_ptr<DvppDataInfo> resizedImage_ = nullptr;
|
||||||
|
std::shared_ptr<DvppDataInfo> cropImage_ = nullptr;
|
||||||
|
bool isVdec_ = false;
|
||||||
|
aclvdecChannelDesc *vdecChannelDesc_ = nullptr;
|
||||||
|
acldvppStreamDesc *streamInputDesc_ = nullptr;
|
||||||
|
acldvppPicDesc *picOutputDesc_ = nullptr;
|
||||||
|
VdecConfig vdecConfig_;
|
||||||
|
};
|
||||||
|
#endif
|
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 "mindspore/core/utils/log_adapter.h"
|
||||||
|
#include "ErrorCode.h"
|
||||||
|
|
||||||
|
std::string GetAppErrCodeInfo(const APP_ERROR err) {
|
||||||
|
if ((err < APP_ERR_ACL_END) && (err >= APP_ERR_ACL_FAILURE)) {
|
||||||
|
return APP_ERR_ACL_LOG_STRING[((err < 0) ? (err + APP_ERR_ACL_END + 1) : err)];
|
||||||
|
} else if ((err < APP_ERR_COMM_END) && (err > APP_ERR_COMM_BASE)) {
|
||||||
|
return (err - APP_ERR_COMM_BASE) <
|
||||||
|
(int)sizeof(APP_ERR_COMMON_LOG_STRING) / (int)sizeof(APP_ERR_COMMON_LOG_STRING[0])
|
||||||
|
? APP_ERR_COMMON_LOG_STRING[err - APP_ERR_COMM_BASE]
|
||||||
|
: "Undefine the error code information";
|
||||||
|
} else if ((err < APP_ERR_DVPP_END) && (err > APP_ERR_DVPP_BASE)) {
|
||||||
|
return (err - APP_ERR_DVPP_BASE) < (int)sizeof(APP_ERR_DVPP_LOG_STRING) / (int)sizeof(APP_ERR_DVPP_LOG_STRING[0])
|
||||||
|
? APP_ERR_DVPP_LOG_STRING[err - APP_ERR_DVPP_BASE]
|
||||||
|
: "Undefine the error code information";
|
||||||
|
} else if ((err < APP_ERR_QUEUE_END) && (err > APP_ERR_QUEUE_BASE)) {
|
||||||
|
return (err - APP_ERR_QUEUE_BASE) < (int)sizeof(APP_ERR_QUEUE_LOG_STRING) / (int)sizeof(APP_ERR_QUEUE_LOG_STRING[0])
|
||||||
|
? APP_ERR_QUEUE_LOG_STRING[err - APP_ERR_QUEUE_BASE]
|
||||||
|
: "Undefine the error code information";
|
||||||
|
} else {
|
||||||
|
return "Error code unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssertErrorCode(int code, std::string file, std::string function, int line) {
|
||||||
|
if (code != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed at " << file << "->" << function << "->" << line << ": error code=" << code;
|
||||||
|
exit(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckErrorCode(int code, std::string file, std::string function, int line) {
|
||||||
|
if (code != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed at " << file << "->" << function << "->" << line << ": error code=" << code;
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 "ResourceManager.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
bool ResourceManager::initFlag_ = true;
|
||||||
|
std::shared_ptr<ResourceManager> ResourceManager::ptr_ = nullptr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the file exists.
|
||||||
|
*
|
||||||
|
* @param filePath the file path we want to check
|
||||||
|
* @return APP_ERR_OK if file exists, error code otherwise
|
||||||
|
*/
|
||||||
|
APP_ERROR ExistFile(const std::string &filePath) {
|
||||||
|
struct stat fileSat = {0};
|
||||||
|
char c[PATH_MAX + 1] = {0x00};
|
||||||
|
size_t count = filePath.copy(c, PATH_MAX + 1);
|
||||||
|
if (count != filePath.length()) {
|
||||||
|
MS_LOG(ERROR) << "Failed to strcpy" << c;
|
||||||
|
return APP_ERR_COMM_FAILURE;
|
||||||
|
}
|
||||||
|
// Get the absolute path of input directory
|
||||||
|
char path[PATH_MAX + 1] = {0x00};
|
||||||
|
if ((strlen(c) > PATH_MAX) || (realpath(c, path) == nullptr)) {
|
||||||
|
MS_LOG(ERROR) << "Failed to get canonicalize path";
|
||||||
|
return APP_ERR_COMM_EXIST;
|
||||||
|
}
|
||||||
|
if (stat(c, &fileSat) == 0 && S_ISREG(fileSat.st_mode)) {
|
||||||
|
return APP_ERR_OK;
|
||||||
|
}
|
||||||
|
return APP_ERR_COMM_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourceManager::Release() {
|
||||||
|
APP_ERROR ret;
|
||||||
|
for (size_t i = 0; i < deviceIds_.size(); i++) {
|
||||||
|
if (contexts_[i] != nullptr) {
|
||||||
|
ret = aclrtDestroyContext(contexts_[i]); // Destroy context
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to destroy context, ret = " << ret << ".";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
contexts_[i] = nullptr;
|
||||||
|
}
|
||||||
|
ret = aclrtResetDevice(deviceIds_[i]); // Reset device
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to reset device, ret = " << ret << ".";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = aclFinalize();
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to finalize acl, ret = " << ret << ".";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Finalized acl successfully.";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ResourceManager> ResourceManager::GetInstance() {
|
||||||
|
if (ptr_ == nullptr) {
|
||||||
|
ResourceManager *temp = new ResourceManager();
|
||||||
|
ptr_.reset(temp);
|
||||||
|
}
|
||||||
|
return ptr_;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_ERROR ResourceManager::InitResource(ResourceInfo &resourceInfo) {
|
||||||
|
if (!GetInitStatus()) {
|
||||||
|
return APP_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string &aclConfigPath = resourceInfo.aclConfigPath;
|
||||||
|
APP_ERROR ret;
|
||||||
|
if (aclConfigPath.length() == 0) {
|
||||||
|
// Init acl without aclconfig
|
||||||
|
ret = aclInit(nullptr);
|
||||||
|
} else {
|
||||||
|
ret = ExistFile(aclConfigPath);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Acl config file not exist, ret = " << ret << ".";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
ret = aclInit(aclConfigPath.c_str()); // Initialize ACL
|
||||||
|
}
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to init acl, ret = " << ret;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
std::copy(resourceInfo.deviceIds.begin(), resourceInfo.deviceIds.end(), std::back_inserter(deviceIds_));
|
||||||
|
MS_LOG(INFO) << "Initialized acl successfully.";
|
||||||
|
// Open device and create context for each chip, note: it create one context for each chip
|
||||||
|
for (size_t i = 0; i < deviceIds_.size(); i++) {
|
||||||
|
deviceIdMap_[deviceIds_[i]] = i;
|
||||||
|
ret = aclrtSetDevice(deviceIds_[i]);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to open acl device: " << deviceIds_[i];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Open device " << deviceIds_[i] << " successfully.";
|
||||||
|
aclrtContext context;
|
||||||
|
ret = aclrtCreateContext(&context, deviceIds_[i]);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to create acl context, ret = " << ret << ".";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Created context for device " << deviceIds_[i] << " successfully";
|
||||||
|
contexts_.push_back(context);
|
||||||
|
}
|
||||||
|
std::string singleOpPath = resourceInfo.singleOpFolderPath;
|
||||||
|
if (!singleOpPath.empty()) {
|
||||||
|
ret = aclopSetModelDir(singleOpPath.c_str()); // Set operator model directory for application
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
MS_LOG(ERROR) << "Failed to aclopSetModelDir, ret = " << ret << ".";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Init resource successfully.";
|
||||||
|
ResourceManager::initFlag_ = false;
|
||||||
|
return APP_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
aclrtContext ResourceManager::GetContext(int deviceId) { return contexts_[deviceIdMap_[deviceId]]; }
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
|
||||||
|
* 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 RESOURCEMANAGER_H
|
||||||
|
#define RESOURCEMANAGER_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
#include <cstring>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <mutex>
|
||||||
|
#include "CommonDataType.h"
|
||||||
|
#include "ErrorCode.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include "mindspore/core/utils/log_adapter.h"
|
||||||
|
|
||||||
|
#define PATH_MAX 4096
|
||||||
|
|
||||||
|
enum ModelLoadMethod {
|
||||||
|
LOAD_FROM_FILE = 0, // Loading from file, memory of model and weights are managed by ACL
|
||||||
|
LOAD_FROM_MEM, // Loading from memory, memory of model and weights are managed by ACL
|
||||||
|
LOAD_FROM_FILE_WITH_MEM, // Loading from file, memory of model and weight are managed by user
|
||||||
|
LOAD_FROM_MEM_WITH_MEM // Loading from memory, memory of model and weight are managed by user
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ModelInfo {
|
||||||
|
std::string modelName;
|
||||||
|
std::string modelPath; // Path of om model file
|
||||||
|
size_t modelFileSize; // Size of om model file
|
||||||
|
std::shared_ptr<void> modelFilePtr; // Smart pointer of model file data
|
||||||
|
uint32_t modelWidth; // Input width of model
|
||||||
|
uint32_t modelHeight; // Input height of model
|
||||||
|
ModelLoadMethod method; // Loading method of model
|
||||||
|
};
|
||||||
|
|
||||||
|
// Device resource info, such as model infos, etc
|
||||||
|
struct DeviceResInfo {
|
||||||
|
std::vector<ModelInfo> modelInfos;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ResourceInfo {
|
||||||
|
std::set<int> deviceIds;
|
||||||
|
std::string aclConfigPath;
|
||||||
|
std::string singleOpFolderPath;
|
||||||
|
std::unordered_map<int, DeviceResInfo> deviceResInfos; // map <deviceId, deviceResourceInfo>
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_ERROR ExistFile(const std::string &filePath);
|
||||||
|
|
||||||
|
class ResourceManager {
|
||||||
|
public:
|
||||||
|
ResourceManager(){};
|
||||||
|
|
||||||
|
~ResourceManager(){};
|
||||||
|
|
||||||
|
// Get the Instance of resource manager
|
||||||
|
static std::shared_ptr<ResourceManager> GetInstance();
|
||||||
|
|
||||||
|
// Init the resource of resource manager
|
||||||
|
APP_ERROR InitResource(ResourceInfo &resourceInfo);
|
||||||
|
|
||||||
|
aclrtContext GetContext(int deviceId);
|
||||||
|
|
||||||
|
void Release();
|
||||||
|
|
||||||
|
static bool GetInitStatus() { return initFlag_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::shared_ptr<ResourceManager> ptr_;
|
||||||
|
static bool initFlag_;
|
||||||
|
std::vector<int> deviceIds_;
|
||||||
|
std::vector<aclrtContext> contexts_;
|
||||||
|
std::unordered_map<int, int> deviceIdMap_; // Map of device to index
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in new issue