Generates an epoch of data, that will be used for variable batch size verification. :param max_batch_size: Actual sizes of every batch in the epoch will be less or equal to max_batch_size :param n_iter: Number of iterations in the epoch :param sample_shap
(max_batch_size, n_iter, sample_shape, lo=0.0, hi=1.0, dtype=np.float32)
| 70 | |
| 71 | |
| 72 | def generate_data(max_batch_size, n_iter, sample_shape, lo=0.0, hi=1.0, dtype=np.float32): |
| 73 | """ |
| 74 | Generates an epoch of data, that will be used for variable batch size verification. |
| 75 | |
| 76 | :param max_batch_size: Actual sizes of every batch in the epoch will be less or equal |
| 77 | to max_batch_size |
| 78 | :param n_iter: Number of iterations in the epoch |
| 79 | :param sample_shape: If sample_shape is callable, shape of every sample will be determined by |
| 80 | calling sample_shape. In this case, every call to sample_shape has to |
| 81 | return a tuple of integers. If sample_shape is a tuple, this will be a |
| 82 | shape of every sample. |
| 83 | :param lo: Begin of the random range |
| 84 | :param hi: End of the random range |
| 85 | :param dtype: Numpy data type |
| 86 | :return: An epoch of data |
| 87 | """ |
| 88 | batch_sizes = np.array([max_batch_size // 2, max_batch_size // 4, max_batch_size]) |
| 89 | |
| 90 | if isinstance(sample_shape, tuple): |
| 91 | |
| 92 | def sample_shape_wrapper(): |
| 93 | return sample_shape |
| 94 | |
| 95 | size_fn = sample_shape_wrapper |
| 96 | elif inspect.isfunction(sample_shape): |
| 97 | size_fn = sample_shape |
| 98 | else: |
| 99 | raise RuntimeError( |
| 100 | "`sample_shape` shall be either a tuple or a callable. " |
| 101 | "Provide `(val,)` tuple for 1D shape" |
| 102 | ) |
| 103 | |
| 104 | if np.issubdtype(dtype, np.integer): |
| 105 | return [ |
| 106 | np.random.randint(lo, hi, size=(bs,) + size_fn(), dtype=dtype) for bs in batch_sizes |
| 107 | ] |
| 108 | elif np.issubdtype(dtype, np.float32): |
| 109 | ret = (np.random.random_sample(size=(bs,) + size_fn()) for bs in batch_sizes) |
| 110 | ret = map(lambda batch: (hi - lo) * batch + lo, ret) |
| 111 | ret = map(lambda batch: batch.astype(dtype), ret) |
| 112 | return list(ret) |
| 113 | elif np.issubdtype(dtype, bool): |
| 114 | assert isinstance(lo, bool) |
| 115 | assert isinstance(hi, bool) |
| 116 | return [np.random.choice(a=[lo, hi], size=(bs,) + size_fn()) for bs in batch_sizes] |
| 117 | else: |
| 118 | raise RuntimeError(f"Invalid type argument: {dtype}") |
| 119 | |
| 120 | |
| 121 | def single_op_pipeline( |
no test coverage detected