Skip to content

Commit

Permalink
chore: update sscma-micro
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr committed Nov 22, 2024
1 parent e4669de commit a249057
Show file tree
Hide file tree
Showing 26 changed files with 1,206 additions and 323 deletions.
4 changes: 1 addition & 3 deletions src/SSCMA_Micro_Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,7 @@ SSCMAMicroCore::Expected SSCMAMicroCore::invoke(const Frame& frame, const Invoke
}
auto results = algorithm->getResults();
if (_config.invoke_config && _config.invoke_config->top_k > 0) {
std::sort(results.begin(), results.end(), [](const ma_keypoint3f_t& a, const ma_keypoint3f_t& b) { return a.box.score > b.box.score; });
results.resize(std::min(results.size(), static_cast<size_t>(_config.invoke_config->top_k)));
results.shrink_to_fit();
results.sort([](const ma_keypoint3f_t& a, const ma_keypoint3f_t& b) { return a.box.score > b.box.score; });
}
std::vector<SSCMAMicroCore::Keypoints> keypoints;
for (const auto& result : results) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/sscma-micro/sscma/core/cv/ma_cv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ma_err_t convert(const ma_img_t* src, ma_img_t* dst);

#if MA_USE_LIB_JPEGENC
ma_err_t rgb_to_jpeg(const ma_img_t* src, ma_img_t* dst);
#endif
#endif

#ifdef __cplusplus
}
Expand Down
152 changes: 100 additions & 52 deletions src/components/sscma-micro/sscma/core/engine/ma_engine_halio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ ma_err_t EngineHalio::run() {
return MA_FAILED;
}

auto job = _configured_model->run_async(*_bindings, [](const AsyncInferCompletionInfo& info) {});
auto job = _configured_model->run_async(*_bindings, [&](const AsyncInferCompletionInfo& info) { sta = info.status; });

do {
this_thread::yield();
} while (job->wait(1000ms) != HAILO_SUCCESS);
} while (job->wait(50ms) != HAILO_SUCCESS);

return MA_OK;
switch (sta) {
case HAILO_SUCCESS:
return MA_OK;
case HAILO_TIMEOUT:
return MA_ETIMEOUT;
default:
return MA_FAILED;
}
}

#if MA_USE_FILESYSTEM
Expand Down Expand Up @@ -121,11 +128,16 @@ ma_err_t EngineHalio::load(const string& model_path) {

{

auto create_internal_bindings = [&](const string& name, const InferModel::InferStream& tsr, shared_ptr<ma_tensor_t>& tensor) {
auto create_internal_bindings =
[&](const string& name, const InferModel::InferStream& tsr, shared_ptr<ma_tensor_t>& tensor, hailort::ConfiguredInferModel::Bindings::InferStream* cis, bool is_input) -> ma_err_t {
auto shape = tsr.shape();
auto size = tsr.get_frame_size();
auto format = tsr.format();

if (!cis) {
return MA_FAILED;
}

void* buffer = aligned_alloc(4096, size);
if (!buffer) {
return MA_ENOMEM;
Expand All @@ -145,27 +157,30 @@ ma_err_t EngineHalio::load(const string& model_path) {
return MA_ENOMEM;
}

cis->set_buffer(MemoryView(buffer, size));

tensor->data.data = buffer;
tensor->size = size;

tensor->shape.size = 3;
tensor->shape.size = 4;
tensor->shape.dims[0] = 1;
switch (format.order) {
case HAILO_FORMAT_ORDER_NCHW:
tensor->shape.dims[0] = shape.features;
tensor->shape.dims[1] = shape.height;
tensor->shape.dims[2] = shape.width;
tensor->shape.dims[1] = shape.features;
tensor->shape.dims[2] = shape.height;
tensor->shape.dims[3] = shape.width;
break;
case HAILO_FORMAT_ORDER_NHWC:
case HAILO_FORMAT_ORDER_FCR:
case HAILO_FORMAT_ORDER_HAILO_NMS:
tensor->shape.dims[0] = shape.height;
tensor->shape.dims[1] = shape.width;
tensor->shape.dims[2] = shape.features;
tensor->shape.dims[1] = shape.height;
tensor->shape.dims[2] = shape.width;
tensor->shape.dims[3] = shape.features;
break;
case HAILO_FORMAT_ORDER_NHCW:
tensor->shape.dims[0] = shape.height;
tensor->shape.dims[1] = shape.features;
tensor->shape.dims[2] = shape.width;
tensor->shape.dims[1] = shape.height;
tensor->shape.dims[2] = shape.features;
tensor->shape.dims[3] = shape.width;
break;
default:
break;
Expand All @@ -192,44 +207,76 @@ ma_err_t EngineHalio::load(const string& model_path) {
break;
case HAILO_FORMAT_TYPE_FLOAT32:
tensor->type = MA_TENSOR_TYPE_F32;
if (format.order == HAILO_FORMAT_ORDER_HAILO_NMS) {
break;
default:
tensor->type = MA_TENSOR_TYPE_NONE;
break;
}

if (format.order == HAILO_FORMAT_ORDER_HAILO_NMS) {
switch (format.type) {
case HAILO_FORMAT_TYPE_UINT16:
tensor->type = MA_TENSOR_TYPE_NMS_BBOX_U16;
break;
case HAILO_FORMAT_TYPE_FLOAT32:
tensor->type = MA_TENSOR_TYPE_NMS_BBOX_F32;
break;
default:
tensor->type = MA_TENSOR_TYPE_NONE;
break;
}

function<ma_err_t(int, void*, size_t)> f = [this_ptr = this, name](int flag, void* data, size_t size) -> ma_err_t {
if (!data || sizeof(float) != size) {
auto fp = make_shared<ExternalHandler>([this_ptr = this, name, is_input](int flag, void* data, size_t size) -> ma_err_t {
if (!data) {
return MA_EINVAL;
}
auto tsr = is_input ? this_ptr->_model->input(name) : this_ptr->_model->output(name);
if (!tsr) {
return MA_FAILED;
}
switch (flag) {
case 0: // get score threshold
return MA_ENOTSUP;
case 1: // set score threshold
{
if (sizeof(float) != size) {
return MA_EINVAL;
}
float threshold = *static_cast<float*>(data);
tsr->set_nms_score_threshold(threshold);
return MA_OK;
}
case 2: // get iou threshold
return MA_ENOTSUP;
case 3: // set iou threshold
{
if (sizeof(float) != size) {
return MA_EINVAL;
}
float threshold = *static_cast<float*>(data);
auto tsr = this_ptr->_model->input(name);
if (!tsr) {
tsr->set_nms_iou_threshold(threshold);
return MA_OK;
}
case 4: // get nms shape
{
auto nms_shape = tsr->get_nms_shape();
if (!nms_shape) {
return MA_FAILED;
}
switch (flag) {
case 0: // get score threshold
return MA_ENOTSUP;
case 1: // set score threshold
tsr->set_nms_score_threshold(threshold);
return MA_OK;
case 2: // get iou threshold
return MA_ENOTSUP;
case 3: // set iou threshold
tsr->set_nms_iou_threshold(threshold);
return MA_OK;
default:
return MA_ENOTSUP;
auto shape = nms_shape.value();
if (sizeof(hailo_nms_shape_t) != size) {
return MA_EINVAL;
}
};

_external_handlers[name] = f;
if (!_external_handlers[name]) {
break;
*static_cast<hailo_nms_shape_t*>(data) = shape;
return MA_OK;
}
tensor->external_handler = reinterpret_cast<void*>(&_external_handlers[name]);
default:
return MA_ENOTSUP;
}
break;
default:
tensor->type = MA_TENSOR_TYPE_NONE;
break;
});

_external_handlers[name] = fp;
tensor->external_handler = reinterpret_cast<void*>(fp.get());
}

_io_buffers[name] = tensor;
Expand All @@ -243,14 +290,15 @@ ma_err_t EngineHalio::load(const string& model_path) {
if (_io_buffers.find(name) != _io_buffers.end()) {
continue;
}

shared_ptr<ma_tensor_t> tensor = nullptr;

auto ret = create_internal_bindings(name, tsr, tensor);
auto bindings_input = _bindings->input(name);
if (!bindings_input) {
return MA_FAILED;
}
auto ret = create_internal_bindings(name, tsr, tensor, &bindings_input.value(), true);
if (ret != MA_OK) {
return ret;
}

_input_tensors.push_back(tensor);
}

Expand All @@ -260,19 +308,19 @@ ma_err_t EngineHalio::load(const string& model_path) {
if (_io_buffers.find(name) != _io_buffers.end()) {
continue;
}

shared_ptr<ma_tensor_t> tensor = nullptr;

auto ret = create_internal_bindings(name, tsr, tensor);
auto bindings_output = _bindings->output(name);
if (!bindings_output) {
return MA_FAILED;
}
auto ret = create_internal_bindings(name, tsr, tensor, &bindings_output.value(), false);
if (ret != MA_OK) {
return ret;
}

_output_tensors.push_back(tensor);
}
}


return MA_OK;
}

Expand Down Expand Up @@ -348,7 +396,7 @@ ma_quant_param_t EngineHalio::getOutputQuantParam(int32_t index) {


ma_err_t EngineHalio::setInput(int32_t index, const ma_tensor_t& tensor) {
return MA_ENOTSUP;
return MA_ENOTSUP;
}

} // namespace ma::engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ using namespace hailort;

class EngineHalio final : public Engine {
public:
using ExternalHandler = function<ma_err_t(int, void*, size_t)>;

EngineHalio();
~EngineHalio() override;

Expand Down Expand Up @@ -56,7 +58,7 @@ class EngineHalio final : public Engine {
shared_ptr<ConfiguredInferModel::Bindings> _bindings;

unordered_map<string, shared_ptr<ma_tensor_t>> _io_buffers;
unordered_map<string, function<ma_err_t(int, void*, size_t)>> _external_handlers;
unordered_map<string, shared_ptr<ExternalHandler>> _external_handlers;

vector<shared_ptr<ma_tensor_t>> _input_tensors;
vector<shared_ptr<ma_tensor_t>> _output_tensors;
Expand All @@ -66,4 +68,4 @@ class EngineHalio final : public Engine {

#endif

#endif
#endif
38 changes: 36 additions & 2 deletions src/components/sscma-micro/sscma/core/ma_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ typedef enum {
MA_TENSOR_TYPE_STR = 12,
MA_TENSOR_TYPE_BOOL = 13,
MA_TENSOR_TYPE_BF16 = 14,
MA_TENSOR_TYPE_NMS_BBOX_F32 = 15,
MA_TENSOR_TYPE_NMS_BBOX_U16 = 15,
MA_TENSOR_TYPE_NMS_BBOX_F32 = 16,
} ma_tensor_type_t;

typedef struct {
Expand Down Expand Up @@ -212,6 +213,17 @@ struct ma_keypoint4f_t {
ma_bbox_t box;
std::vector<ma_pt4f_t> pts;
};

struct ma_segm2f_t {
ma_bbox_t box;
struct {
uint16_t width;
uint16_t height;
std::vector<uint8_t> data;
} mask;
};


#endif

typedef enum {
Expand Down Expand Up @@ -239,6 +251,26 @@ typedef enum {

typedef enum { MA_MSG_TYPE_RESP = 0, MA_MSG_TYPE_EVT = 1, MA_MSG_TYPE_LOG = 2, MA_MSG_TYPE_REQ = 3, MA_MSG_TYPE_HB = 4 } ma_msg_type_t;

#define MA_INPUT_TYPE_MASK 0xF000
#define MA_OUTPUT_TYPE_MASK 0x0F00
#define MA_MODEL_TYPE_MASK 0x00FF

typedef enum {
MA_INPUT_TYPE_TENSOR = 0x0000,
MA_INPUT_TYPE_IMAGE = 0x1000,
MA_INPUT_TYPE_AUDIO = 0x2000,
} ma_input_type_t;

typedef enum {
MA_OUTPUT_TYPE_TENSOR = 0x0000,
MA_OUTPUT_TYPE_CLASS = 0x0100,
MA_OUTPUT_TYPE_POINT = 0x0200,
MA_OUTPUT_TYPE_BBOX = 0x0300,
MA_OUTPUT_TYPE_KEYPOINT = 0x0400,
MA_OUTPUT_TYPE_SEGMENTATION = 0x0500,
} ma_output_type_t;


typedef enum {
MA_MODEL_TYPE_UNDEFINED = 0u,
MA_MODEL_TYPE_FOMO = 1u,
Expand All @@ -249,7 +281,9 @@ typedef enum {
MA_MODEL_TYPE_YOLOV8 = 6u,
MA_MODEL_TYPE_NVIDIA_DET = 7u,
MA_MODEL_TYPE_YOLO_WORLD = 8u,
MA_MODEL_TYPE_YOLO11 = 9u,
MA_MODEL_TYPE_YOLO11 = 9u,
MA_MODEL_TYPE_YOLO11_POSE = 10u,
MA_MODEL_TYPE_YOLO11_SEG = 11u,
} ma_model_type_t;

typedef struct {
Expand Down
Loading

0 comments on commit a249057

Please sign in to comment.