Skip to content

Commit

Permalink
corrected shape calculation for Nonconstant inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-esir committed Jul 31, 2020
1 parent 831e2db commit 55eadc5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 4 additions & 5 deletions model-optimizer/mo/ops/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,18 @@ def normalize_slice_indices(size: int, start: int, end: int):

def convert_negative_indices(size: int, val: int):
"""
Converts negative indices of a tensors: e.g. if val == -1 then convert it to size
If val is -1 and we expect it to the start then returned val = size should be clipped by `clip_indices`
Note: returned value is not always positive and it's expected behaviour
Converts negative indices of a tensors: e.g. if val == -1 then convert it to size - 1.
Note: returned value is not always positive and it's expected behaviour.
"""
if val < 0:
return val + size + 1
return val + size
else:
return val


def clip_indices(size: int, val: int):
# if slice starts and/or ends exceed indices bounds of a tensor this routine cuts them to size or 0
# Note: returned value is not always positive and it's expected behaviour
# Note: returned value is not always positive and it's expected behaviour.
if val >= size:
return size
elif val < 0:
Expand Down
4 changes: 4 additions & 0 deletions model-optimizer/mo/ops/slice_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class TestSliceOp(unittest.TestCase):
(None, [np.iinfo(np.int32).min, 2], [3, 3], [0, 1], [1, 1], [3, 1, 6], [4, 5, 6]),
# 1D input
([1, 3, 224, 224], [1], [2], [0], [1], [3]),
# 1D input with negative starts
(None, [-1], [1], [0], [-1], [2], [4]),
# 1D input with negative ends
(None, [1], [-1], [0], [1], [2], [4]),
# case when output shape has zero elements
(None, [1], [1], [0], [1], [0], [4])
])
Expand Down

0 comments on commit 55eadc5

Please sign in to comment.