Skip to content

Commit

Permalink
[Paddle.incubate.jit.inference] allow repeate call `my_layer = paddle…
Browse files Browse the repository at this point in the history
….incubate.jit.inference(my_layer)` (PaddlePaddle#66689)

* first commit
  • Loading branch information
zhoutianzi666 authored and Lans1ot committed Aug 5, 2024
1 parent 4d2aaf8 commit 2eba09d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/paddle/incubate/jit/inference_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def forward(self, args):
)
input_specs.append(None)

for i in range(len(input_specs)):
if input_specs[i] is not None:
if isinstance(input_specs[i], list):
for j in range(len(input_specs[i])):
input_specs[i][j].stop_gradient = True
else:
input_specs[i].stop_gradient = True

# update the input_spec's shape for doing d2s
d2s_shapes_id = 0
# initial the self.d2s_input_names!
Expand Down Expand Up @@ -547,6 +555,15 @@ def inference(
>>> decorator_result = mylayer(x)
"""
# if function has already been decorated by @paddle.incubate.jit.inference(), then we just return it.
if (
hasattr(function, "__name__")
and function.__name__ == "innermost_decorator"
):
return function
elif isinstance(function, Layer):
if function.forward.__name__ == "innermost_decorator":
return function

used_as_at_decorator = function is None

Expand Down
2 changes: 2 additions & 0 deletions test/dygraph_to_static/test_incubate_jit_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_dygraph_static_same_result(self):
result_x0 = my_layer(x).numpy()
result_y0 = my_layer(y).numpy()

my_layer.func = paddle.incubate.jit.inference(my_layer.func)
my_layer.func = paddle.incubate.jit.inference(my_layer.func)

result_x1 = my_layer(x).numpy()
Expand All @@ -119,6 +120,7 @@ def test_dygraph_static_same_result(self):
my_layer = TestLayer2(hidd)
result0 = my_layer([x, x]).numpy()
my_static_layer = paddle.incubate.jit.inference(my_layer)
my_static_layer = paddle.incubate.jit.inference(my_layer)

result1 = my_layer([x, x]).numpy()
np.testing.assert_allclose(result0, result1, rtol=0.001, atol=1e-05)
Expand Down

0 comments on commit 2eba09d

Please sign in to comment.