Skip to content

[feat] Add automatic type promotion to element-wise ops #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions core/conversion/converters/converter_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ nvinfer1::ITensor* addUnpadding(
}
}

nvinfer1::DataType promote_types(nvinfer1::DataType type_a, nvinfer1::DataType type_b){
auto torch_type_a = util::TRTDataTypeToScalarType(type_a);
auto torch_type_b = util::TRTDataTypeToScalarType(type_b);
auto promo_type = at::promote_types(torch_type_a, torch_type_b);
auto trt_promo_type = util::ScalarTypeToTRTDataType(promo_type);
return trt_promo_type;
}

nvinfer1::ILayer* add_elementwise(
ConversionCtx* ctx,
nvinfer1::ElementWiseOperation op,
Expand All @@ -78,6 +86,20 @@ nvinfer1::ILayer* add_elementwise(
std::swap(self, other);
swapSelfOther = true;
}

if(self->getType() != other->getType()){
LOG_DEBUG("Type mismatch for inputs in element-wise operation " << name << ": " << self->getType() << ", " << other->getType());
auto promo_type = promote_types(self->getType(), other->getType());
if(self->getType() != promo_type){
LOG_DEBUG("Element-wise op type promotion adding cast from " << self->getType() << " to " << promo_type << " for layer " << name);
self = castITensor(ctx, self, promo_type);
}
if(other->getType() != promo_type){
LOG_DEBUG("Element-wise op type promotion adding cast from " << other->getType() << " to " << promo_type << " for layer " << name);
other = castITensor(ctx, other, promo_type);
}
}

auto selfDim = util::toVec(self->getDimensions());
auto otherDim = util::toVec(other->getDimensions());
if (selfDim.size() != otherDim.size()) {
Expand Down
Loading