Skip to content

Commit

Permalink
Comp index select (#51215)
Browse files Browse the repository at this point in the history
  • Loading branch information
sljlp authored Mar 16, 2023
1 parent f824bc0 commit d1e2c61
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ set(TEST_CINN_OPS
test_stack_op
test_activation_op
test_full_like_op
test_index_select_op
test_fill_any_like_op
test_concat_op
test_top_k_v2_op
Expand Down
7 changes: 5 additions & 2 deletions python/paddle/fluid/tests/unittests/test_index_select_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class TestIndexSelectOp(OpTest):
def setUp(self):
self.python_api = paddle.index_select
self.op_type = "index_select"
self.prim_op_type = "comp"
self.init_dtype_type()

index_np = np.random.randint(
low=0, high=self.x_shape[self.dim], size=self.index_size
)
Expand Down Expand Up @@ -57,10 +59,10 @@ def init_dtype_type(self):
self.index_size = 100

def test_check_output(self):
self.check_output(check_eager=True)
self.check_output(check_eager=True, check_prim=True)

def test_check_grad_normal(self):
self.check_grad(['X'], 'Out', check_eager=True)
self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)


class TestIndexSelectOpCase2(TestIndexSelectOp):
Expand Down Expand Up @@ -92,6 +94,7 @@ def init_dtype_type(self):
self.index_size = 100


# no scatter op (the backward op of index_select/gather) for bf16
class TestIndexSelectBF16Op(OpTest):
def setUp(self):
self.python_api = paddle.index_select
Expand Down
9 changes: 9 additions & 0 deletions python/paddle/incubate/autograd/composite_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ def hard_swish_composite(x):
return res


@REGISTER_COMPOSITE('index_select')
def index_select_composite(x, index, axis):
"""define composite rule of op index_select."""
if axis < 0:
axis = len(x.shape) + axis
res = gather(x, index, axis=axis)
return res


@REGISTER_COMPOSITE('sigmoid')
def sigmoid_composite(x):
"""
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/incubate/autograd/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from paddle.tensor import exp # noqa: F401
from paddle.tensor import expm1 # noqa: F401
from paddle.tensor import full # noqa: F401
from paddle.tensor import gather # noqa: F401
from paddle.tensor import greater_equal # noqa: F401
from paddle.tensor import lgamma # noqa: F401
from paddle.tensor import log # noqa: F401
Expand Down Expand Up @@ -124,6 +125,7 @@
'cast',
'fill_constant',
'reshape',
'gather'
'full',
'tile',
'concat',
Expand Down

0 comments on commit d1e2c61

Please sign in to comment.