forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#8 from ckl117/ADFM-PNC
trt支持p_norm和scatter_nd_add
- Loading branch information
Showing
5 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright (c) 2024 PaddlePaddle Authors. 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 "paddle/fluid/inference/tensorrt/convert/op_converter.h" | ||
|
||
namespace paddle::inference::tensorrt { | ||
|
||
/* | ||
* p_norm Op | ||
*/ | ||
class PNormOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, | ||
bool test_mode) override { | ||
VLOG(3) << "convert a p_norm op to tensorrt layer"; | ||
|
||
framework::OpDesc op_desc(op, nullptr); | ||
std::string input_name = op_desc.Input("X").front(); | ||
std::string output_name = op_desc.Output("Out").front(); | ||
auto* input_tensor = engine_->GetITensor(input_name); | ||
int rank = input_tensor->getDimensions().nbDims; | ||
int axis = PADDLE_GET_CONST(int, op_desc.GetAttr("axis")); | ||
bool keepdim = PADDLE_GET_CONST(bool, op_desc.GetAttr("keepdim")); | ||
if (axis < 0) { | ||
axis += rank; | ||
} | ||
uint32_t axisMask = 1 << axis; | ||
auto* prod_tensor = Prod(input_tensor, input_tensor); | ||
auto* prod_layer = TRT_ENGINE_ADD_LAYER(engine_, | ||
Reduce, | ||
*prod_tensor, | ||
nvinfer1::ReduceOperation::kSUM, | ||
axisMask, | ||
keepdim); | ||
auto* reduce_tensor = prod_layer->getOutput(0); | ||
auto* sqrt_layer = TRT_ENGINE_ADD_LAYER( | ||
engine_, Unary, *reduce_tensor, nvinfer1::UnaryOperation::kSQRT); | ||
ReplenishLayerAndOutput(sqrt_layer, "p_norm", {output_name}, test_mode); | ||
} | ||
}; | ||
|
||
} // namespace paddle::inference::tensorrt | ||
|
||
REGISTER_TRT_OP_CONVERTER(p_norm, PNormOpConverter); |
61 changes: 61 additions & 0 deletions
61
paddle/fluid/inference/tensorrt/convert/scatter_nd_add_op.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Copyright (c) 2024 PaddlePaddle Authors. 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 "paddle/fluid/inference/tensorrt/convert/op_converter.h" | ||
|
||
namespace paddle::inference::tensorrt { | ||
|
||
/* | ||
* scatter_nd_add Op | ||
*/ | ||
class ScatterNdAddOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, | ||
bool test_mode) override { | ||
VLOG(3) << "convert a scatter_nd_add op to tensorrt layer"; | ||
|
||
framework::OpDesc op_desc(op, nullptr); | ||
std::string input_name = op_desc.Input("X").front(); | ||
std::string index_name = op_desc.Input("Index").front(); | ||
std::string update_name = op_desc.Input("Updates").front(); | ||
std::string output_name = op_desc.Output("Out").front(); | ||
|
||
auto* input_tensor = engine_->GetITensor(input_name); | ||
auto* index_tensor = engine_->GetITensor(index_name); | ||
auto* update_tensor = engine_->GetITensor(update_name); | ||
auto* input_shape_tensor = Shape(input_tensor); | ||
nvinfer1::Dims input_dims = input_tensor->getDimensions(); | ||
auto rank = input_dims.nbDims; | ||
auto* zero_tensor = FillConstantLayer(input_shape_tensor, rank, 0.f); | ||
auto* value_layer = TRT_ENGINE_ADD_LAYER(engine_, | ||
Scatter, | ||
*zero_tensor, | ||
*index_tensor, | ||
*update_tensor, | ||
nvinfer1::ScatterMode::kND); | ||
value_layer->setAxis(0); | ||
auto* value_tensor = value_layer->getOutput(0); | ||
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, | ||
ElementWise, | ||
*input_tensor, | ||
*value_tensor, | ||
nvinfer1::ElementWiseOperation::kSUM); | ||
ReplenishLayerAndOutput(layer, "scatter_nd_add", {output_name}, test_mode); | ||
} | ||
}; | ||
|
||
} // namespace paddle::inference::tensorrt | ||
|
||
REGISTER_TRT_OP_CONVERTER(scatter_nd_add, ScatterNdAddOpConverter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters