Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions test/legacy_test/test_multi_label_soft_margin_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np

import paddle
from paddle.pir_utils import test_with_pir_api


def call_MultiLabelSoftMarginLoss_layer(
Expand Down Expand Up @@ -139,6 +140,7 @@ def LogSigmoid(x):


class TestMultiLabelMarginLoss(unittest.TestCase):
@test_with_pir_api
def test_MultiLabelSoftMarginLoss(self):
input = np.random.uniform(0.1, 0.8, size=(5, 5)).astype(np.float64)
label = np.random.randint(0, 2, size=(5, 5)).astype(np.float64)
Expand Down Expand Up @@ -209,6 +211,7 @@ def test_MultiLabelSoftMarginLoss_error(self):
)
paddle.enable_static()

@test_with_pir_api
def test_MultiLabelSoftMarginLoss_weights(self):
input = np.random.uniform(0.1, 0.8, size=(5, 5)).astype(np.float64)
label = np.random.randint(0, 2, size=(5, 5)).astype(np.float64)
Expand Down
5 changes: 5 additions & 0 deletions test/legacy_test/test_multimarginloss.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np

import paddle
from paddle.pir_utils import test_with_pir_api


def call_MultiMarginLoss_layer(
Expand Down Expand Up @@ -238,6 +239,7 @@ def calc_multi_margin_loss(


class TestMultiMarginLoss(unittest.TestCase):
@test_with_pir_api
def test_MultiMarginLoss(self):
batch_size = 5
num_classes = 2
Expand Down Expand Up @@ -330,6 +332,7 @@ def test_MultiMarginLoss_dimension(self):
)
paddle.enable_static()

@test_with_pir_api
def test_MultiMarginLoss_p(self):
p = 2
batch_size = 5
Expand Down Expand Up @@ -383,6 +386,7 @@ def test_MultiMarginLoss_p(self):
np.testing.assert_allclose(static_functional, dy_functional)
np.testing.assert_allclose(dy_functional, expected)

@test_with_pir_api
def test_MultiMarginLoss_weight(self):
batch_size = 5
num_classes = 2
Expand Down Expand Up @@ -436,6 +440,7 @@ def test_MultiMarginLoss_weight(self):
np.testing.assert_allclose(static_functional, dy_functional)
np.testing.assert_allclose(dy_functional, expected)

@test_with_pir_api
def test_MultiMarginLoss_static_data_shape(self):
batch_size = 5
num_classes = 2
Expand Down
43 changes: 26 additions & 17 deletions test/legacy_test/test_square_error_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,43 @@
from paddle import base
from paddle.base import core
from paddle.base.executor import Executor
from paddle.pir_utils import test_with_pir_api


class TestSquareErrorCost(unittest.TestCase):
@test_with_pir_api
def test_square_error_cost(self):
input_val = np.random.uniform(0.1, 0.5, (2, 3)).astype("float32")
label_val = np.random.uniform(0.1, 0.5, (2, 3)).astype("float32")
paddle.enable_static()
shape = [2, 3]
input_val = np.random.uniform(0.1, 0.5, shape).astype("float32")
label_val = np.random.uniform(0.1, 0.5, shape).astype("float32")

sub = input_val - label_val
np_result = sub * sub

input_var = paddle.tensor.create_tensor(dtype="float32", name="input")
label_var = paddle.tensor.create_tensor(dtype="float32", name="label")
output = paddle.nn.functional.square_error_cost(
input=input_var, label=label_var
)

for use_cuda in (
[False, True] if core.is_compiled_with_cuda() else [False]
):
place = base.CUDAPlace(0) if use_cuda else base.CPUPlace()
exe = Executor(place)
(result,) = exe.run(
base.default_main_program(),
feed={"input": input_val, "label": label_val},
fetch_list=[output],
)

np.testing.assert_allclose(np_result, result, rtol=1e-05)
with paddle.static.program_guard(paddle.static.Program()):
input_var = paddle.static.data(
name="input", shape=shape, dtype="float32"
)
label_var = paddle.static.data(
name="label", shape=shape, dtype="float32"
)
output = paddle.nn.functional.square_error_cost(
input=input_var, label=label_var
)

place = base.CUDAPlace(0) if use_cuda else base.CPUPlace()
exe = Executor(place)
(result,) = exe.run(
paddle.static.default_main_program(),
feed={"input": input_val, "label": label_val},
fetch_list=[output],
)

np.testing.assert_allclose(np_result, result, rtol=1e-05)


class TestSquareErrorInvalidInput(unittest.TestCase):
Expand Down