Skip to content

Commit 12d200d

Browse files
Merge pull request #217 from ooooo-create/fix_topk
[Accuracy diff No.70] Fix accuracy diff for topk API
2 parents c86f028 + ca21f30 commit 12d200d

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

tester/api_config/2_paddle_only_random/random_calculation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25604,3 +25604,4 @@ paddle.put_along_axis(Tensor([6, 8000],"float32"), Tensor([6, 799],"int64"), Ten
2560425604
paddle.put_along_axis(Tensor([7, 8000],"float32"), Tensor([7, 799],"int64"), Tensor([7, 799],"float32"), 1, )
2560525605
paddle.put_along_axis(Tensor([8, 8000],"float32"), Tensor([8, 799],"int64"), Tensor([8, 799],"float32"), 1, )
2560625606
paddle.put_along_axis(Tensor([9, 8000],"float32"), Tensor([9, 799],"int64"), Tensor([9, 799],"float32"), 1, )
25607+
paddle.topk(Tensor([128, 1000],"float16"), k=5, )

tester/api_config/5_accuracy/accuracy_gpu_error.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20095,7 +20095,6 @@ paddle.topk(Tensor([1, 93, 37044],"float32"), 13, axis=-1, largest=True, )
2009520095
paddle.topk(Tensor([1, 96, 52500],"float32"), 13, axis=-1, largest=True, )
2009620096
paddle.topk(Tensor([1, 99, 27216],"float32"), 13, axis=-1, largest=True, )
2009720097
paddle.topk(Tensor([1022, 14],"float32"), 10, axis=0, )
20098-
paddle.topk(Tensor([128, 1000],"float16"), k=5, )
2009920098
paddle.topk(Tensor([12906, 215],"float32"), 10, axis=0, )
2010020099
paddle.topk(Tensor([1302, 7],"float32"), 10, axis=0, )
2010120100
paddle.topk(Tensor([14, 400],"float16"), k=5, )

tester/api_config/config_analyzer.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,39 @@
3232
"paddle.mod",
3333
]
3434

35+
def generate_unique_array(num_items, float_dtype):
36+
def get_integer_dtype(float_dtype):
37+
float_dtype = numpy.dtype(float_dtype)
38+
if float_dtype == numpy.float16:
39+
return numpy.uint16, 16
40+
elif float_dtype == numpy.float32:
41+
return numpy.uint32, 32
42+
elif float_dtype == numpy.float64:
43+
return numpy.uint64, 64
44+
else:
45+
raise ValueError(f"Unsupported float dtype: {float_dtype}")
46+
integer_dtype, bits = get_integer_dtype(float_dtype)
47+
max_int = (1 << bits) - 1
48+
current_start_value = 1
49+
return_list = []
50+
attemp_count = 0
51+
while len(return_list) < num_items and attemp_count < 3:
52+
nums_to_generate = int(num_items * 1.5)
53+
if current_start_value >= max_int:
54+
raise ValueError(f"Cannot generate {num_items} unique items of type {float_dtype} within the range.")
55+
end_value = min(current_start_value + nums_to_generate, max_int)
56+
random_arr = numpy.arange(current_start_value, end_value, dtype=integer_dtype)
57+
float_arr = random_arr.view(float_dtype)
58+
if return_list is None:
59+
return_list = float_arr[numpy.isfinite(float_arr)]
60+
else:
61+
return_list = numpy.unique(numpy.concatenate([return_list, float_arr[numpy.isfinite(float_arr)]]))
62+
current_start_value = end_value
63+
attemp_count += 1
64+
if len(return_list) < num_items:
65+
raise ValueError(f"Could not generate {num_items} unique items of type {float_dtype}")
66+
return return_list[:num_items]
67+
3568
class TensorConfig:
3669
def __init__(self, shape, dtype, place=None):
3770
self.shape = shape
@@ -1638,8 +1671,21 @@ def get_padding_offset(bsz, max_seq_len, seq_lens_this_time):
16381671
if self.check_arg(api_config, 1, "repeat_times"):
16391672
self.numpy_tensor = numpy.random.randint(1, 128, size=self.shape).astype(self.dtype)
16401673

1641-
elif api_config.api_name == "paddle.topk":
1642-
if self.check_arg(api_config, 1, "k"):
1674+
elif api_config.api_name in {"paddle.topk", "paddle.Tensor.topk"}:
1675+
if self.check_arg(api_config, 0, "x"):
1676+
x_numel = self.numel()
1677+
if self.dtype in {"bfloat16", "float32", "float64"}:
1678+
dtype = "float32" if self.dtype == "bfloat16" else self.dtype
1679+
self.numpy_tensor = numpy.linspace(-x_numel, x_numel, x_numel, dtype=dtype).reshape(self.shape)
1680+
if numpy.unique(self.numpy_tensor).size < x_numel:
1681+
self.numpy_tensor = generate_unique_array(x_numel, dtype).reshape(self.shape)
1682+
elif self.dtype == "float16":
1683+
self.numpy_tensor = generate_unique_array(x_numel, self.dtype).reshape(self.shape)
1684+
elif self.dtype in {"int32", "int64"}:
1685+
self.numpy_tensor = numpy.random.choice(numpy.arange(-x_numel, x_numel), size=self.shape, replace=False).astype(self.dtype)
1686+
else:
1687+
raise ValueError(f"Unsupported dtype {self.dtype} for paddle.topk / paddle.Tensor.topk")
1688+
elif self.check_arg(api_config, 1, "k"):
16431689
x_config = self.get_arg(api_config, 0, "x")
16441690
max_k_value = 1
16451691
if isinstance(x_config, TensorConfig) and x_config.shape:
@@ -1762,7 +1808,7 @@ def get_padding_offset(bsz, max_seq_len, seq_lens_this_time):
17621808
else:
17631809
# self.check_arg(api_config, 1, "other"):
17641810
self.numpy_tensor = self.get_random_numpy_tensor(self.shape, self.dtype, min=-10, max=10)
1765-
1811+
17661812
if self.numpy_tensor is None:
17671813
if USE_CACHED_NUMPY and self.dtype not in ["int64", "float64"]:
17681814
dtype = "float32" if self.dtype == "bfloat16" else self.dtype

0 commit comments

Comments
 (0)