Skip to content

Commit 66c78b8

Browse files
authored
Fix int64->int32 casting in static slices (#317)
* Use int64 type for slice bounds check * Fixing odd-strided slices
1 parent 11ccb57 commit 66c78b8

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

builtin_op_importers.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ DEFINE_BUILTIN_OP_IMPORTER(Concat)
311311
{
312312
tensors.push_back(&convertToTensor(input, ctx));
313313
}
314+
314315
OnnxAttrs attrs(node);
315316
int axis = attrs.get<int>("axis");
316317
int nbDims = inputs.at(0).shape().nbDims;
@@ -1580,6 +1581,10 @@ NodeImportResult staticInputSliceHelper(IImporterContext* ctx, nvinfer1::ITensor
15801581
{
15811582
static const auto handleNegativeIndex = [&ctx, &axis, &shape](int64_t index) -> int64_t
15821583
{
1584+
if (std::abs(index) > static_cast<int64_t>(std::numeric_limits<int32_t>::max()))
1585+
{
1586+
return static_cast<int64_t>(std::numeric_limits<int32_t>::max());
1587+
}
15831588
return (index < 0) ? (shape.d[axis] + index) : index;
15841589
};
15851590

@@ -1588,9 +1593,13 @@ NodeImportResult staticInputSliceHelper(IImporterContext* ctx, nvinfer1::ITensor
15881593
int64_t start, end, stride;
15891594
std::tie(start, end, stride) = sliceBounds.at(axis);
15901595

1596+
end = std::min(handleNegativeIndex(end), static_cast<int64_t>(shape.d[axis]));
1597+
15911598
starts.d[axis] = handleNegativeIndex(start);
15921599
strides.d[axis] = handleNegativeIndex(stride);
1593-
sizes.d[axis] = (std::min(handleNegativeIndex(end), static_cast<int64_t>(shape.d[axis])) - starts.d[axis]) / strides.d[axis];
1600+
int64_t sliceSize = (end - starts.d[axis]) / strides.d[axis];
1601+
// Add 1 to slice size since int division floors the result.
1602+
sizes.d[axis] = (end - starts.d[axis]) % strides.d[axis] == 0 ? sliceSize : sliceSize + 1;
15941603
}
15951604
else
15961605
{
@@ -1616,7 +1625,7 @@ NodeImportResult dynamicInputSliceHelper(IImporterContext* ctx, nvinfer1::ITenso
16161625
{
16171626
static const auto handleNegativeIndex = [&ctx, &indexShape, &tensor, &i](int64_t index) -> nvinfer1::ITensor*
16181627
{
1619-
if (index > static_cast<int64_t>(std::numeric_limits<int32_t>::max()))
1628+
if (std::abs(index) > static_cast<int64_t>(std::numeric_limits<int32_t>::max()))
16201629
{
16211630
std::cout << "WARNING: Slice index is out of bounds of INT32, clamping to INT32_MAX" << std::endl;
16221631
index = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
@@ -1639,7 +1648,7 @@ NodeImportResult dynamicInputSliceHelper(IImporterContext* ctx, nvinfer1::ITenso
16391648

16401649
startIndices.emplace_back(handleNegativeIndex(start));
16411650
endIndex = handleNegativeIndex(end);
1642-
strideIndices.emplace_back(handleNegativeIndex(1)); // This should never be negative anyway.
1651+
strideIndices.emplace_back(handleNegativeIndex(stride)); // This should never be negative anyway.
16431652
}
16441653
else
16451654
{
@@ -1741,9 +1750,9 @@ DEFINE_BUILTIN_OP_IMPORTER(Slice)
17411750

17421751
if (dims.d[axis] != -1)
17431752
{
1744-
int startsVal = static_cast<int>(handleNegativeIndex(starts.at(i)));
1745-
int endsVal = static_cast<int>(handleNegativeIndex(ends.at(i)));
1746-
ASSERT((std::min(dims.d[axis], endsVal) - startsVal) / steps.at(i) != 0
1753+
int64_t startsVal = handleNegativeIndex(starts.at(i));
1754+
int64_t endsVal = handleNegativeIndex(ends.at(i));
1755+
ASSERT((std::min(static_cast<int64_t>(dims.d[axis]), endsVal) - startsVal) / steps.at(i) != 0
17471756
&& "TensorRT does not support size 0 slices!", ErrorCode::kUNSUPPORTED_NODE);
17481757
}
17491758

@@ -1885,7 +1894,6 @@ DEFINE_BUILTIN_OP_IMPORTER(Split)
18851894
sliceStart.d[axis] = start_index[i];
18861895
sliceSize.d[axis] = output_lengths[i];
18871896
auto const layer = ctx->network()->addSlice(*tensor_ptr, sliceStart, sliceSize, sliceStride);
1888-
// std::vector<int> size_vecto(dims.d, dims.d+dims.nbDims);
18891897
if (std::any_of(sliceSize.d, sliceSize.d + sliceSize.nbDims, [](int i){return i == -1;})){
18901898
layer->setInput(1, dimension_to_tensor(ctx, sliceStart));
18911899
layer->setInput(2, dimension_to_tensor(ctx, sliceSize));

0 commit comments

Comments
 (0)