|
32 | 32 | "paddle.mod", |
33 | 33 | ] |
34 | 34 |
|
| 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 | + |
35 | 68 | class TensorConfig: |
36 | 69 | def __init__(self, shape, dtype, place=None): |
37 | 70 | self.shape = shape |
@@ -1638,8 +1671,21 @@ def get_padding_offset(bsz, max_seq_len, seq_lens_this_time): |
1638 | 1671 | if self.check_arg(api_config, 1, "repeat_times"): |
1639 | 1672 | self.numpy_tensor = numpy.random.randint(1, 128, size=self.shape).astype(self.dtype) |
1640 | 1673 |
|
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"): |
1643 | 1689 | x_config = self.get_arg(api_config, 0, "x") |
1644 | 1690 | max_k_value = 1 |
1645 | 1691 | 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): |
1762 | 1808 | else: |
1763 | 1809 | # self.check_arg(api_config, 1, "other"): |
1764 | 1810 | self.numpy_tensor = self.get_random_numpy_tensor(self.shape, self.dtype, min=-10, max=10) |
1765 | | - |
| 1811 | + |
1766 | 1812 | if self.numpy_tensor is None: |
1767 | 1813 | if USE_CACHED_NUMPY and self.dtype not in ["int64", "float64"]: |
1768 | 1814 | dtype = "float32" if self.dtype == "bfloat16" else self.dtype |
|
0 commit comments