Skip to content

Commit

Permalink
【Hackathon 6th Fundable Projects 2 No.19】cppcoreguidelines-no-malloc-…
Browse files Browse the repository at this point in the history
…final (PaddlePaddle#63752)

* no-malloc fix

* minor changes

* minor changes

* minor changes

* minor changes

* interface value update

* minor changes

* linter

* capi fix

* restore

* minor changes

* format

* minor changes

* Update interface_value.h

* Update interface_value.h
  • Loading branch information
walkalone20 authored and co63oc committed May 23, 2024
1 parent 72e8e70 commit 9e344a8
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,13 @@ class CrossMultiheadMatMulOpConverter : public OpConverter {
auto creator = GetPluginRegistry()->getPluginCreator("fMHCA", "1");
assert(creator != nullptr);
std::vector<nvinfer1::PluginField> fields{};
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);

plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection);
free(plugin_collection);
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection.get());
plugin_collection.reset();
std::vector<nvinfer1::ITensor*> plugin_inputs;
plugin_inputs.emplace_back(reshape_after_fc_q_layer->getOutput(0));
plugin_inputs.emplace_back(reshape_after_fc_layer->getOutput(0));
Expand Down
22 changes: 9 additions & 13 deletions paddle/fluid/inference/tensorrt/convert/emb_eltwise_layernorm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ class EmbEltwiseLayerNormOpConverter : public OpConverter {
static_cast<int32_t>(emb_sizes[i]));
}

nvinfer1::PluginFieldCollection* plugin_ptr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_ptr) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_ptr(
new nvinfer1::PluginFieldCollection);
plugin_ptr->nbFields = static_cast<int>(fields.size());
plugin_ptr->fields = fields.data();

Expand All @@ -132,15 +130,15 @@ class EmbEltwiseLayerNormOpConverter : public OpConverter {
auto creator = GetPluginRegistry()->getPluginCreator(
"ManyEmbLayerNormVarlenPluginDynamic", "1");
auto plugin_obj = creator->createPlugin(
"ManyEmbLayerNormVarlenPluginDynamic", plugin_ptr);
"ManyEmbLayerNormVarlenPluginDynamic", plugin_ptr.get());

auto plugin_layer = engine_->network()->addPluginV2(
plugin_inputs.data(), plugin_inputs.size(), *plugin_obj);

plugin_layer->setName(("ManyEmbLayerNormVarlenPluginDynamicV1(Output: " +
op_desc.Output("Out")[0] + ")")
.c_str());
free(plugin_ptr);
plugin_ptr.reset();
if (enable_int8) {
float out_scale =
PADDLE_GET_CONST(float, op_desc.GetAttr("out_threshold"));
Expand Down Expand Up @@ -218,27 +216,25 @@ class EmbEltwiseLayerNormOpConverter : public OpConverter {
static_cast<int32_t>(emb_sizes[i]));
}

nvinfer1::PluginFieldCollection* plugin_ptr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_ptr) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_ptr(
new nvinfer1::PluginFieldCollection);
plugin_ptr->nbFields = static_cast<int>(fields.size());
plugin_ptr->fields = fields.data();

std::vector<nvinfer1::ITensor*> plugin_inputs = input_ids;

auto creator = GetPluginRegistry()->getPluginCreator(
"ManyEmbLayerNormPluginDynamic", "1");
auto plugin_obj =
creator->createPlugin("ManyEmbLayerNormPluginDynamic", plugin_ptr);
auto plugin_obj = creator->createPlugin("ManyEmbLayerNormPluginDynamic",
plugin_ptr.get());

auto plugin_layer = engine_->network()->addPluginV2(
plugin_inputs.data(), plugin_inputs.size(), *plugin_obj);

plugin_layer->setName(("ManyEmbLayerNormPluginDynamicV1(Output: " +
op_desc.Output("Out")[0] + ")")
.c_str());
free(plugin_ptr);
plugin_ptr.reset();
if (enable_int8) {
float out_scale =
PADDLE_GET_CONST(float, op_desc.GetAttr("out_threshold"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,12 @@ class FlashMultiheadMatMulOpConverter : public OpConverter {
auto creator = GetPluginRegistry()->getPluginCreator("fMHA_V2", "1");
assert("fmha_v2 plugin creater must not be null" && creator != nullptr);
std::vector<nvinfer1::PluginField> fields{};
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free

std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);
plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection);
free(plugin_collection);
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection.get());
plugin_collection.reset();
std::vector<nvinfer1::ITensor*> plugin_inputs;
plugin_inputs.emplace_back(reshape_before_mha_layer->getOutput(0));
auto plugin_layer = engine_->network()->addPluginV2(
Expand Down
12 changes: 5 additions & 7 deletions paddle/fluid/inference/tensorrt/convert/multiclass_nms3_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ class MultiClassNMS3OpConverter : public OpConverter {
{"clipBoxes", &clip_boxes, nvinfer1::PluginFieldType::kINT32, 1},
};

nvinfer1::PluginFieldCollection* plugin_collections =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_collections) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collections(
new nvinfer1::PluginFieldCollection);
plugin_collections->nbFields = static_cast<int>(fields.size());
plugin_collections->fields = fields.data();
std::string nms_plugin_name = "BatchedNMS_TRT";
Expand All @@ -114,9 +112,9 @@ class MultiClassNMS3OpConverter : public OpConverter {
}
auto creator =
GetPluginRegistry()->getPluginCreator(nms_plugin_name.c_str(), "1");
auto batch_nms_plugin =
creator->createPlugin(nms_plugin_name.c_str(), plugin_collections);
free(plugin_collections);
auto batch_nms_plugin = creator->createPlugin(nms_plugin_name.c_str(),
plugin_collections.get());
plugin_collections.reset();

auto batch_nms_layer = engine_->network()->addPluginV2(
batch_nms_inputs.data(), batch_nms_inputs.size(), *batch_nms_plugin);
Expand Down
12 changes: 5 additions & 7 deletions paddle/fluid/inference/tensorrt/convert/multiclass_nms_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ class MultiClassNMSOpConverter : public OpConverter {
{"clipBoxes", &clip_boxes, nvinfer1::PluginFieldType::kINT32, 1},
};

nvinfer1::PluginFieldCollection* plugin_collections =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_collections) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collections(
new nvinfer1::PluginFieldCollection);
plugin_collections->nbFields = static_cast<int>(fields.size());
plugin_collections->fields = fields.data();

Expand All @@ -113,9 +111,9 @@ class MultiClassNMSOpConverter : public OpConverter {
}
auto creator =
GetPluginRegistry()->getPluginCreator(nms_plugin_name.c_str(), "1");
auto batch_nms_plugin =
creator->createPlugin(nms_plugin_name.c_str(), plugin_collections);
free(plugin_collections);
auto batch_nms_plugin = creator->createPlugin(nms_plugin_name.c_str(),
plugin_collections.get());
plugin_collections.reset();

auto batch_nms_layer = engine_->network()->addPluginV2(
batch_nms_inputs.data(), batch_nms_inputs.size(), *batch_nms_plugin);
Expand Down
33 changes: 12 additions & 21 deletions paddle/fluid/inference/tensorrt/convert/multihead_matmul_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,14 @@ class MultiheadMatMulOpConverter : public OpConverter {
nvinfer1::PluginFieldType::kFLOAT32,
1});
}
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(malloc(
sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);
plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();

auto plugin = creator->createPlugin("CustomQKVToContextPluginDynamic",
plugin_collection);
free(plugin_collection);
plugin_collection.get());
plugin_collection.reset();

std::vector<nvinfer1::ITensor*> plugin_inputs;
plugin_inputs.emplace_back(fc_layer->getOutput(0));
Expand Down Expand Up @@ -429,17 +426,14 @@ class MultiheadMatMulOpConverter : public OpConverter {
nvinfer1::PluginFieldType::kFLOAT32,
1});
}
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(malloc(
sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);
plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();

auto plugin = creator->createPlugin("CustomQKVToContextPluginDynamic",
plugin_collection);
free(plugin_collection);
plugin_collection.get());
plugin_collection.reset();

std::vector<nvinfer1::ITensor*> plugin_inputs;
plugin_inputs.emplace_back(
Expand Down Expand Up @@ -661,16 +655,13 @@ class MultiheadMatMulOpConverter : public OpConverter {
&var_seqlen,
nvinfer1::PluginFieldType::kINT32,
1}};
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(malloc(
sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);
plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();
auto plugin = creator->createPlugin("CustomQKVToContextPluginDynamic",
plugin_collection);
free(plugin_collection);
plugin_collection.get());
plugin_collection.reset();
// set inputs
std::vector<nvinfer1::ITensor*> plugin_inputs;
// input_0 for plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ class PrelnEmbEltwiseLayerNormOpConverter : public OpConverter {
static_cast<int32_t>(emb_sizes[i]));
}

nvinfer1::PluginFieldCollection* plugin_ptr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_ptr) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_ptr(
new nvinfer1::PluginFieldCollection);
plugin_ptr->nbFields = static_cast<int>(fields.size());
plugin_ptr->fields = fields.data();

Expand All @@ -188,15 +186,15 @@ class PrelnEmbEltwiseLayerNormOpConverter : public OpConverter {
"ManyEmbLayerNormVarlenPluginDynamic", "2");

auto plugin_obj = creator->createPlugin(
"ManyEmbLayerNormVarlenPluginDynamic", plugin_ptr);
"ManyEmbLayerNormVarlenPluginDynamic", plugin_ptr.get());

auto plugin_layer = engine_->network()->addPluginV2(
plugin_inputs.data(), plugin_inputs.size(), *plugin_obj);

plugin_layer->setName(("ManyEmbLayerNormPluginDynamic_V3(Output: " +
op_desc.Output("Out")[0] + ")")
.c_str());
free(plugin_ptr);
plugin_ptr.reset();
float out_0_scale =
PADDLE_GET_CONST(float, op_desc.GetAttr("out_0_threshold"));
float out_1_scale =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ class PromptTuningEmbEltwiseLayerNormOpConverter : public OpConverter {
static_cast<int32_t>(emb_sizes[i]));
}

nvinfer1::PluginFieldCollection* plugin_ptr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_ptr) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_ptr(
new nvinfer1::PluginFieldCollection);
plugin_ptr->nbFields = static_cast<int>(fields.size());
plugin_ptr->fields = fields.data();

Expand All @@ -134,7 +132,7 @@ class PromptTuningEmbEltwiseLayerNormOpConverter : public OpConverter {
auto creator = GetPluginRegistry()->getPluginCreator(
"PromptTuningEmbLayerNormVarlenPluginDynamic", "1");
auto plugin_obj = creator->createPlugin(
"PromptTuningEmbLayerNormVarlenPluginDynamic", plugin_ptr);
"PromptTuningEmbLayerNormVarlenPluginDynamic", plugin_ptr.get());

auto plugin_layer = engine_->network()->addPluginV2(
plugin_inputs.data(), plugin_inputs.size(), *plugin_obj);
Expand All @@ -143,7 +141,7 @@ class PromptTuningEmbEltwiseLayerNormOpConverter : public OpConverter {
("PromptTuningEmbLayerNormVarlenPluginDynamicV1(Output: " +
op_desc.Output("Out")[0] + ")")
.c_str());
free(plugin_ptr);
plugin_ptr.reset();
if (enable_int8) {
float out_scale =
PADDLE_GET_CONST(float, op_desc.GetAttr("out_threshold"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,12 @@ class QkMultiheadMatMulOpConverter : public OpConverter {
auto creator = GetPluginRegistry()->getPluginCreator("fMHA_V2", "1");
assert(creator != nullptr);
std::vector<nvinfer1::PluginField> fields{};
nvinfer1::PluginFieldCollection* plugin_collection =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(*plugin_collection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free

std::unique_ptr<nvinfer1::PluginFieldCollection> plugin_collection(
new nvinfer1::PluginFieldCollection);
plugin_collection->nbFields = static_cast<int>(fields.size());
plugin_collection->fields = fields.data();
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection);
free(plugin_collection);
auto plugin = creator->createPlugin("fMHA_V2", plugin_collection.get());
plugin_collection.reset();
std::vector<nvinfer1::ITensor*> plugin_inputs;
plugin_inputs.emplace_back(mha_input_tensor);
auto plugin_layer = engine_->network()->addPluginV2(
Expand Down
34 changes: 13 additions & 21 deletions paddle/fluid/inference/tensorrt/convert/skip_layernorm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,15 @@ class SkipLayerNormOpConverter : public OpConverter {
scale_weight.values,
GetPluginFieldType(scale_weight.type),
static_cast<int32_t>(scale_weight.count)}};
nvinfer1::PluginFieldCollection* pluginPtr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(nvinfer1::PluginFieldCollection) +
fields.size() * sizeof(nvinfer1::PluginField)));
std::unique_ptr<nvinfer1::PluginFieldCollection> pluginPtr(
new nvinfer1::PluginFieldCollection);
pluginPtr->nbFields = static_cast<int32_t>(fields.size());
pluginPtr->fields = fields.data();

auto pluginObj =
creator->createPlugin("CustomSkipLayerNormPluginDynamic", pluginPtr);
auto pluginObj = creator->createPlugin("CustomSkipLayerNormPluginDynamic",
pluginPtr.get());

free(pluginPtr);
pluginPtr.reset();

auto plugin_layer = engine_->network()->addPluginV2(
inputs.data(), inputs.size(), *pluginObj);
Expand Down Expand Up @@ -213,18 +211,15 @@ class SkipLayerNormOpConverter : public OpConverter {
smooth_scale.data(),
nvinfer1::PluginFieldType::kFLOAT32,
static_cast<int32_t>(smooth_scale.size())});
nvinfer1::PluginFieldCollection* pluginPtr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(nvinfer1::PluginFieldCollection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> pluginPtr(
new nvinfer1::PluginFieldCollection);
pluginPtr->nbFields = static_cast<int32_t>(fields.size());
pluginPtr->fields = fields.data();

auto pluginObj = creator->createPlugin(
"CustomSkipLayerNormPluginDynamicWithSmooth", pluginPtr);
"CustomSkipLayerNormPluginDynamicWithSmooth", pluginPtr.get());

free(pluginPtr);
pluginPtr.reset();

auto plugin_layer = engine_->network()->addPluginV2(
inputs.data(), inputs.size(), *pluginObj);
Expand All @@ -237,18 +232,15 @@ class SkipLayerNormOpConverter : public OpConverter {
"layer"));
layer = plugin_layer;
} else {
nvinfer1::PluginFieldCollection* pluginPtr =
static_cast<nvinfer1::PluginFieldCollection*>(
malloc(sizeof(nvinfer1::PluginFieldCollection) +
fields.size() *
sizeof(nvinfer1::PluginField))); // remember to free
std::unique_ptr<nvinfer1::PluginFieldCollection> pluginPtr(
new nvinfer1::PluginFieldCollection);
pluginPtr->nbFields = static_cast<int32_t>(fields.size());
pluginPtr->fields = fields.data();

auto pluginObj = creator->createPlugin(
"CustomSkipLayerNormPluginDynamic", pluginPtr);
"CustomSkipLayerNormPluginDynamic", pluginPtr.get());

free(pluginPtr);
pluginPtr.reset();

auto plugin_layer = engine_->network()->addPluginV2(
inputs.data(), inputs.size(), *pluginObj);
Expand Down
Loading

0 comments on commit 9e344a8

Please sign in to comment.