Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/runtime/contrib/tensorrt/tensorrt_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,16 @@ class BiasAddOpConverter : public TensorRTOpConverter {
void Convert(TensorRTOpConverterParams* params) const {
auto input_tensor = params->inputs.at(0).tensor;
auto input_dims = TrtDimsToVector(input_tensor->getDimensions());
const size_t required_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
size_t required_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
const size_t input_nbDims = input_tensor->getDimensions().nbDims;
int axis = std::stoi(params->node.GetAttr<std::vector<std::string>>("axis")[0]);
if (axis == -1) {
// Make sure there are 2 dimensions after channel dimension,
if (input_nbDims + 2 > required_rank) required_rank = input_nbDims + 2;
axis = input_nbDims - 1;
} else if (TRT_HAS_IMPLICIT_BATCH(params)) {
axis -= 1;
}
ICHECK(input_dims.size() > 0 && input_dims.size() <= required_rank);
const bool need_reshape_on_input = input_dims.size() != required_rank;
if (need_reshape_on_input) {
Expand All @@ -968,10 +977,11 @@ class BiasAddOpConverter : public TensorRTOpConverter {

const nvinfer1::DataType weight_type = params->inputs.at(1).weight.type;

nvinfer1::Weights shift{weight_type, nullptr, 0};
nvinfer1::Weights scale{weight_type, nullptr, 0};
nvinfer1::Weights power{weight_type, nullptr, 0};
nvinfer1::IScaleLayer* scale_layer = params->network->addScale(
*input_tensor, nvinfer1::ScaleMode::kCHANNEL, params->inputs.at(1).weight, shift, power);
nvinfer1::IScaleLayer* scale_layer =
params->network->addScaleNd(*input_tensor, nvinfer1::ScaleMode::kCHANNEL,
params->inputs.at(1).weight, scale, power, axis);
ICHECK(scale_layer != nullptr);
auto output_tensor = scale_layer->getOutput(0);
if (need_reshape_on_input) {
Expand Down
5 changes: 3 additions & 2 deletions tests/python/contrib/test_tensorrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,16 @@ def get_graph(x_shape=(12, 128, 64), y_shape=(12, 128, 64), transa=False, transb


def test_bias_add(run_module):
def get_graph(x_shape=(1, 16), channels=16):
def get_graph(x_shape=(1, 16), channels=16, axis=1):
x = relay.var("x", shape=(x_shape), dtype="float32")
bias = relay.var("bias", shape=(channels,), dtype="float32")
out = relay.nn.bias_add(x, bias)
out = relay.nn.bias_add(x, bias, axis)
f = relay.Function([x, bias], out)
return f, {"x": x_shape, "bias": (channels,)}, ["bias"]

run_and_verify_func(get_graph(), run_module=run_module)
run_and_verify_func(get_graph((1, 6, 3, 4), 6), run_module=run_module)
run_and_verify_func(get_graph((1, 6, 3, 4), 4, -1), run_module=run_module)


def test_pool2d(run_module):
Expand Down