|
| 1 | +import torch |
| 2 | + |
| 3 | +try: |
| 4 | + import dgl.ops as dglops |
| 5 | + |
| 6 | + dgl_installed = True |
| 7 | +except ImportError: |
| 8 | + dgl_installed = False |
| 9 | + |
| 10 | + |
| 11 | +def segment_mm(a, b, seglen_a): |
| 12 | + """ |
| 13 | + Performs matrix multiplication according to segments. |
| 14 | + See https://docs.dgl.ai/generated/dgl.ops.segment_mm.html |
| 15 | +
|
| 16 | + Suppose ``seglen_a == [10, 5, 0, 3]``, the operator will perform |
| 17 | + four matrix multiplications:: |
| 18 | +
|
| 19 | + a[0:10] @ b[0], a[10:15] @ b[1], |
| 20 | + a[15:15] @ b[2], a[15:18] @ b[3] |
| 21 | +
|
| 22 | + Args: |
| 23 | + a (torch.Tensor): The left operand, 2-D tensor of shape ``(N, D1)`` |
| 24 | + b (torch.Tensor): The right operand, 3-D tensor of shape ``(R, D1, D2)`` |
| 25 | + seglen_a (torch.Tensor): An integer tensor of shape ``(R,)``. Each element is the length of segments of input ``a``. The summation of all elements must be equal to ``N``. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + torch.Tensor: The output dense matrix of shape ``(N, D2)`` |
| 29 | + """ |
| 30 | + if torch.__version__ < (2, 4): |
| 31 | + raise NotImplementedError("PyTorch version is too old for nested tesors") |
| 32 | + |
| 33 | + if dgl_installed: |
| 34 | + # DGL is probably more computationally efficient |
| 35 | + # See https://github.com/pytorch/pytorch/issues/136747 |
| 36 | + return dglops.segment_mm(a, b, seglen_a) |
| 37 | + |
| 38 | + if not a.dim() == 2 or not b.dim() == 3 or not seglen_a.dim() == 1: |
| 39 | + raise ValueError("Input tensors have unexpected dimensions") |
| 40 | + |
| 41 | + N, _ = a.shape |
| 42 | + R, D1, D2 = b.shape |
| 43 | + |
| 44 | + # Sanity check sizes |
| 45 | + if not a.shape[1] == D1 or not seglen_a.shape[0] == R: |
| 46 | + raise ValueError("Incompatible size for inputs") |
| 47 | + |
| 48 | + segidx_a = torch.cumsum(seglen_a[:-1], dim=0) |
| 49 | + |
| 50 | + # Ideally the conversions below to nested tensor would be handled natively |
| 51 | + nested_a = torch.nested.as_nested_tensor(torch.tensor_split(a, segidx_a, dim=0)) |
| 52 | + nested_b = torch.nested.as_nested_tensor(list(map(torch.squeeze, torch.split(b, 1, dim=0)))) |
| 53 | + |
| 54 | + # The actual gather matmul computation |
| 55 | + nested_ab = torch.matmul(nested_a, nested_b) |
| 56 | + |
| 57 | + # Convert back to tensors, again ideally this would be handled natively |
| 58 | + ab = torch.cat(nested_ab.unbind(), dim=0) |
| 59 | + return ab |
| 60 | + |
| 61 | + |
| 62 | +def gather_mm(a, b, idx_b): |
| 63 | + """ |
| 64 | + Gather data according to the given indices and perform matrix multiplication. |
| 65 | + See https://docs.dgl.ai/generated/dgl.ops.gather_mm.html |
| 66 | +
|
| 67 | + Let the result tensor be ``c``, the operator conducts the following computation: |
| 68 | +
|
| 69 | + c[i] = a[i] @ b[idx_b[i]] |
| 70 | + , where len(c) == len(idx_b) |
| 71 | +
|
| 72 | + Args: |
| 73 | + a (torch.Tensor): A 2-D tensor of shape ``(N, D1)`` |
| 74 | + b (torch.Tensor): A 3-D tensor of shape ``(R, D1, D2)`` |
| 75 | + idx_b (torch.Tensor): An 1-D integer tensor of shape ``(N,)``. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + torch.Tensor: The output dense matrix of shape ``(N, D2)`` |
| 79 | + """ |
| 80 | + if torch.__version__ < (2, 4): |
| 81 | + raise NotImplementedError("PyTorch version is too old for nested tesors") |
| 82 | + |
| 83 | + if dgl_installed: |
| 84 | + # DGL is more computationally efficient |
| 85 | + # See https://github.com/pytorch/pytorch/issues/136747 |
| 86 | + return dglops.gather_mm(a, b, idx_b) |
| 87 | + |
| 88 | + # Dependency free fallback |
| 89 | + if not isinstance(a, torch.Tensor) or not isinstance(b, torch.Tensor) or not isinstance(idx_b, torch.Tensor): |
| 90 | + raise ValueError("Inputs should be instances of torch.Tensor") |
| 91 | + |
| 92 | + if not a.dim() == 2 or not b.dim() == 3 or not idx_b.dim() == 1: |
| 93 | + raise ValueError("Input tensors have unexpected dimensions") |
| 94 | + |
| 95 | + N = idx_b.shape[0] |
| 96 | + R, D1, D2 = b.shape |
| 97 | + |
| 98 | + # Sanity check sizes |
| 99 | + if not a.shape[0] == N or not a.shape[1] == D1: |
| 100 | + raise ValueError("Incompatible size for inputs") |
| 101 | + |
| 102 | + torchdevice = a.device |
| 103 | + src_idx = torch.arange(N, device=torchdevice) |
| 104 | + |
| 105 | + # Ideally the conversions below to nested tensor would be handled without for looops and without copy |
| 106 | + nested_a = torch.nested.as_nested_tensor([a[idx_b == i, :] for i in range(R)]) |
| 107 | + src_idx_reshuffled = torch.cat([src_idx[idx_b == i] for i in range(R)]) |
| 108 | + nested_b = torch.nested.as_nested_tensor([b[i, :, :].squeeze() for i in range(R)]) |
| 109 | + |
| 110 | + # The actual gather matmul computation |
| 111 | + nested_ab = torch.matmul(nested_a, nested_b) |
| 112 | + |
| 113 | + # Convert back to tensors, again, ideally this would be handled natively with no copy |
| 114 | + ab_segmented = torch.cat(nested_ab.unbind(), dim=0) |
| 115 | + ab = torch.empty((N, D2), device=torchdevice) |
| 116 | + ab[src_idx_reshuffled] = ab_segmented |
| 117 | + return ab |
0 commit comments