(tensors, dim=-1)
| 166 | |
| 167 | # Broadcast and concatenate tensors along specified dimension |
| 168 | def broadcast(tensors, dim=-1): |
| 169 | num_tensors = len(tensors) |
| 170 | shape_lens = {len(t.shape) for t in tensors} |
| 171 | assert len(shape_lens) == 1, "tensors must all have the same number of dimensions" |
| 172 | shape_len = list(shape_lens)[0] |
| 173 | dim = (dim + shape_len) if dim < 0 else dim |
| 174 | dims = list(zip(*(list(t.shape) for t in tensors))) |
| 175 | expandable_dims = [(i, val) for i, val in enumerate(dims) if i != dim] |
| 176 | assert all( |
| 177 | [*(len(set(t[1])) <= 2 for t in expandable_dims)] |
| 178 | ), "invalid dimensions for broadcastable concatenation" |
| 179 | max_dims = [(t[0], max(t[1])) for t in expandable_dims] |
| 180 | expanded_dims = [(t[0], (t[1],) * num_tensors) for t in max_dims] |
| 181 | expanded_dims.insert(dim, (dim, dims[dim])) |
| 182 | expandable_shapes = list(zip(*(t[1] for t in expanded_dims))) |
| 183 | tensors = [t[0].expand(*t[1]) for t in zip(tensors, expandable_shapes)] |
| 184 | return torch.cat(tensors, dim=dim) |
| 185 | |
| 186 | freqs = broadcast((freqs_t[:, None, None, :], freqs_h[None, :, None, :], freqs_w[None, None, :, :]), dim=-1) |
| 187 |
nothing calls this directly
no outgoing calls
no test coverage detected