Skip to content

Commit

Permalink
Fix Op(aten::diagonal) | feat(torchlib) (#1209)
Browse files Browse the repository at this point in the history
dim1 and dim2 could go negative.
  • Loading branch information
titaiwangms authored Dec 9, 2023
1 parent 137afb7 commit 036b647
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,11 @@ def aten_diagonal(self: TReal, offset: int = 0, dim1: int = 0, dim2: int = 1) ->
# [0,1,2] -> [2,0,1] when dim1=0 and dim2=1
# [0,1,2] -> [1,0,2] when dim1=0 and dim2=2
# [0,1,2] -> [0,1,2] when dim1=1 and dim2=2
if dim1 < 0:
dim1 = dim1 + len(self.shape)
if dim2 < 0:
dim2 = dim2 + len(self.shape)

self_rank = len(self.shape)
perm = list(range(self_rank))
perm.remove(dim1)
Expand Down Expand Up @@ -2514,6 +2519,11 @@ def aten_diagonal_bool(self: BOOL, offset: int = 0, dim1: int = 0, dim2: int = 1
# [0,1,2] -> [2,0,1] when dim1=0 and dim2=1
# [0,1,2] -> [1,0,2] when dim1=0 and dim2=2
# [0,1,2] -> [0,1,2] when dim1=1 and dim2=2
if dim1 < 0:
dim1 = dim1 + len(self.shape)
if dim2 < 0:
dim2 = dim2 + len(self.shape)

self_rank = len(self.shape)
perm = list(range(self_rank))
perm.remove(dim1)
Expand Down

0 comments on commit 036b647

Please sign in to comment.