narrow.py 2.1 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
import torch


def narrow(src, dim, start, length):
rusty1s's avatar
rusty1s committed
5
6
    dim = src.dim() - dim if dim < 0 else dim

rusty1s's avatar
rusty1s committed
7
8
    if dim == 0:
        (row, col), value = src.coo()
rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
        rowptr = src.storage.rowptr

        # Maintain `rowcount`...
        rowcount = src.storage._rowcount
        if rowcount is not None:
            rowcount = rowcount.narrow(0, start=start, length=length)
rusty1s's avatar
rusty1s committed
15

rusty1s's avatar
rusty1s committed
16
17
18
19
        rowptr = rowptr.narrow(0, start=start, length=length + 1)
        row_start = rowptr[0]
        rowptr = rowptr - row_start
        row_length = rowptr[-1]
rusty1s's avatar
rusty1s committed
20

rusty1s's avatar
rusty1s committed
21
        row = row.narrow(0, row_start, row_length) - start
rusty1s's avatar
rusty1s committed
22
        col = col.narrow(0, row_start, row_length)
rusty1s's avatar
rusty1s committed
23
24
25
26
        index = torch.stack([row, col], dim=0)
        if src.has_value():
            value = value.narrow(0, row_start, row_length)
        sparse_size = torch.Size([length, src.sparse_size(1)])
rusty1s's avatar
rusty1s committed
27

rusty1s's avatar
rusty1s committed
28
29
30
        storage = src.storage.__class__(index, value, sparse_size,
                                        rowcount=rowcount, rowptr=rowptr,
                                        is_sorted=True)
rusty1s's avatar
rusty1s committed
31

rusty1s's avatar
rusty1s committed
32
    elif dim == 1:
rusty1s's avatar
rusty1s committed
33
        # This is faster than accessing `csc()` contrary to the `dim=0` case.
rusty1s's avatar
rusty1s committed
34
35
        (row, col), value = src.coo()
        mask = (col >= start) & (col < start + length)
rusty1s's avatar
rusty1s committed
36

rusty1s's avatar
rusty1s committed
37
38
39
40
41
42
43
        # Maintain `colcount`...
        colcount = src.storage._colcount
        if colcount is not None:
            colcount = colcount.narrow(0, start=start, length=length)

        # Maintain `colptr`...
        colptr = src.storage._colptr
rusty1s's avatar
rusty1s committed
44
45
46
47
        if colptr is not None:
            colptr = colptr.narrow(0, start=start, length=length + 1)
            colptr = colptr - colptr[0]

rusty1s's avatar
rusty1s committed
48
49
50
51
        index = torch.stack([row, col - start], dim=0)[:, mask]
        if src.has_value():
            value = value[mask]
        sparse_size = torch.Size([src.sparse_size(0), length])
rusty1s's avatar
rusty1s committed
52

rusty1s's avatar
rusty1s committed
53
54
55
        storage = src.storage.__class__(index, value, sparse_size,
                                        colcount=colcount, colptr=colptr,
                                        is_sorted=True)
rusty1s's avatar
rusty1s committed
56
57

    else:
rusty1s's avatar
rusty1s committed
58
59
        storage = src.storage.apply_value(
            lambda x: x.narrow(dim - 1, start, length))
rusty1s's avatar
rusty1s committed
60

rusty1s's avatar
rusty1s committed
61
    return src.from_storage(storage)