Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PIR API adaptor No.248、249】 Migrate CTCLoss/RNNTLossinto pir #58661

Merged
merged 15 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ def warpctc(
input_length=None,
label_length=None,
):
if in_dynamic_mode():
if in_dynamic_or_pir_mode():
if input_length is None or label_length is None:
raise ValueError(
"input_length and label_length must not be None in dygraph mode!"
Expand Down Expand Up @@ -2017,7 +2017,7 @@ def rnnt_loss(
def warprnnt(
input, label, input_length, label_length, blank=0, fastemit_lambda=0.001
):
if in_dynamic_mode():
if in_dynamic_or_pir_mode():
loss_out = _C_ops.warprnnt(
input,
label,
Expand Down
22 changes: 15 additions & 7 deletions test/legacy_test/test_warpctc_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

import paddle
import paddle.nn.functional as F
from paddle.base import Program, core, program_guard
from paddle.base import core
from paddle.pir_utils import test_with_pir_api

CUDA_BLOCK_SIZE = 32

Expand Down Expand Up @@ -394,7 +395,7 @@ def setUp(self):
}

def test_check_output(self):
self.check_output()
self.check_output(check_pir=True)

def test_check_grad(self):
self.outputs['WarpCTCGrad'] = self.gradient
Expand All @@ -404,13 +405,15 @@ def test_check_grad(self):
"Loss",
max_relative_error=0.009,
check_dygraph=False,
check_pir=True,
)
else:
self.check_grad(
["Logits"],
"Loss",
max_relative_error=0.007,
check_dygraph=False,
check_pir=True,
)


Expand Down Expand Up @@ -516,17 +519,21 @@ def setUp(self):
}

def test_check_output(self):
self.check_output()
self.check_output(check_pir=True)

def test_check_grad(self):
self.outputs['WarpCTCGrad'] = self.gradient
self.check_grad(["Logits"], "Loss")
self.check_grad(["Logits"], "Loss", check_pir=True)


class TestWarpCTCOpError(unittest.TestCase):
def test_errors(self):
paddle.enable_static()
with program_guard(Program(), Program()):
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(
main_program=main_program, startup_program=startup_program
):
logits = paddle.static.data(
name='logits', shape=[5, 16, 6], dtype='float32'
)
Expand Down Expand Up @@ -610,6 +617,7 @@ def test_dygraph_with_lod():


class TestCTCLossAPICase(unittest.TestCase):
@test_with_pir_api
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved
def test_class_api(self):
self.batch_size = 3
self.num_classes = 15
Expand Down Expand Up @@ -660,7 +668,7 @@ def test_class_api(self):
np.testing.assert_allclose(loss_pd, loss_np, rtol=1e-05, atol=1)

def test_eager_ctcloss(self):
def test_functinal_api():
def test_functional_api():
self.batch_size = 4
self.num_classes = CUDA_BLOCK_SIZE + 2
self.logits_length = np.array([4, 1, 3, 3], dtype=np.int64)
Expand Down Expand Up @@ -730,7 +738,7 @@ def test_functinal_api():
loss_pd_sum, loss_np_sum, rtol=1e-05, atol=1
)

test_functinal_api()
test_functional_api()


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion test/legacy_test/test_warprnnt_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import paddle
from paddle import _C_ops
from paddle.base import Program, core, program_guard
from paddle.pir_utils import test_with_pir_api

paddle.enable_static()

Expand Down Expand Up @@ -246,7 +247,7 @@ def test_check_grad(self):
class TestWarpRNNTFP64Op(TestWarpRNNTOp):
def test_check_output(self):
self.acts.astype(np.float64)
self.check_output()
self.check_output(check_pir=True)

def test_check_grad(self):
self.acts.astype(np.float64)
Expand Down Expand Up @@ -448,6 +449,7 @@ def config(self):
dtype=np.float64,
)

@test_with_pir_api
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved
def test_functinal_api(self):
self.config()

Expand Down Expand Up @@ -490,6 +492,7 @@ def test_functinal_api(self):
)
np.testing.assert_allclose(loss_pd_sum, loss_np_sum, rtol=1e-05, atol=1)

@test_with_pir_api
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved
def test_class_api(self):
self.config()

Expand Down