Partitioner that partitions shards to have max_shard_bytes total size. Args: shape: A `TensorShape`. dtype: A `DType`. Returns: A tuple representing how much to slice each axis in shape. Raises: ValueError: If shape is not a fully defined `TensorShape` or dtype
(shape, dtype)
| 109 | "max_shards must be positive.") |
| 110 | |
| 111 | def _partitioner(shape, dtype): |
| 112 | """Partitioner that partitions shards to have max_shard_bytes total size. |
| 113 | |
| 114 | Args: |
| 115 | shape: A `TensorShape`. |
| 116 | dtype: A `DType`. |
| 117 | |
| 118 | Returns: |
| 119 | A tuple representing how much to slice each axis in shape. |
| 120 | |
| 121 | Raises: |
| 122 | ValueError: If shape is not a fully defined `TensorShape` or dtype is not |
| 123 | a `DType`. |
| 124 | """ |
| 125 | if not isinstance(shape, tensor_shape.TensorShape): |
| 126 | raise ValueError("shape is not a TensorShape: %s" % shape) |
| 127 | if not shape.is_fully_defined(): |
| 128 | raise ValueError("shape is not fully defined: %s" % shape) |
| 129 | if not isinstance(dtype, dtypes.DType): |
| 130 | raise ValueError("dtype is not a DType: %s" % dtype) |
| 131 | |
| 132 | if dtype.base_dtype == dtypes.string: |
| 133 | element_size = bytes_per_string_element |
| 134 | else: |
| 135 | element_size = dtype.size |
| 136 | |
| 137 | partitions = [1] * shape.ndims |
| 138 | bytes_per_slice = 1.0 * ( |
| 139 | shape.num_elements() / shape.dims[axis].value) * element_size |
| 140 | # How many slices can we fit on one shard of size at most max_shard_bytes? |
| 141 | # At least one slice is required. |
| 142 | slices_per_shard = max(1, math.floor(max_shard_bytes / bytes_per_slice)) |
| 143 | # How many shards do we need for axis given that each shard fits |
| 144 | # slices_per_shard slices from a total of shape[axis] slices? |
| 145 | axis_shards = int(math.ceil( |
| 146 | 1.0 * shape.dims[axis].value / slices_per_shard)) |
| 147 | if max_shards: |
| 148 | axis_shards = min(max_shards, axis_shards) |
| 149 | |
| 150 | partitions[axis] = axis_shards |
| 151 | |
| 152 | return partitions |
| 153 | |
| 154 | return _partitioner |
| 155 |
nothing calls this directly
no test coverage detected