Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4636,7 +4636,7 @@ def adaptive_log_softmax_with_loss(
target_dim = label.dim()

if target_dim == 1:
if input.shape[0] != label.shape[0]:
if input.shape[0] != label.shape[0] and label.shape[0] != 0:
raise ValueError(
'Input and label should have the same size '
'in the batch dimension.'
Expand Down Expand Up @@ -4733,9 +4733,10 @@ def adaptive_log_softmax_with_loss(
x=input, weight=head_weight, bias=head_bias
)
head_logprob = paddle.nn.functional.log_softmax(head_output, axis=1)
output += paddle.take_along_axis(
head_logprob, gather_inds.unsqueeze(1), axis=1
).squeeze()
if gather_inds.size != 0:
output += paddle.take_along_axis(
head_logprob, gather_inds.unsqueeze(1), axis=1
).squeeze()
loss = (-output).mean()

if not is_batched:
Expand Down
24 changes: 24 additions & 0 deletions test/legacy_test/test_adaptive_log_softmax_with_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def test_forward(self):
assert not np.any(before != after)

def test_cluster(self):
paddle.disable_static()
model = SimpleModel(16, 20, [5, 10, 15], div_value=2.0)
x = paddle.randn((128, 16))
y = paddle.randint(low=0, high=20, shape=[128])
Expand Down Expand Up @@ -574,5 +575,28 @@ def test_grad(self):
np.allclose(analytic_grads, grad_numerical, rtol=1e-5, atol=1e-5)


class TestAdaptiveLogSoftmaxWithLossAPI_ZeroSize(unittest.TestCase):
def setUp(self):
self.places = get_places()

def test_dygraph_api(self):
for place in self.places:
paddle.disable_static(place)
input = paddle.to_tensor(np.random.random([4, 8]))
label = paddle.to_tensor(np.random.random([0]))
head_weight = paddle.to_tensor(np.random.random([8, 3]))
tail_weights = [
[
paddle.to_tensor(np.random.random([8, 4])),
paddle.to_tensor(np.random.random([4, 2])),
]
]
cutoffs = [2, 4]
out = paddle.nn.functional.adaptive_log_softmax_with_loss(
input, label, head_weight, tail_weights, cutoffs
)
np.testing.assert_allclose(np.array([]), out[0].numpy())


if __name__ == "__main__":
unittest.main()