narrow.py 2.14 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
    start = src.size(dim) + start if start < 0 else start
rusty1s's avatar
rusty1s committed
7

rusty1s's avatar
rusty1s committed
8
9
    if dim == 0:
        (row, col), value = src.coo()
rusty1s's avatar
rusty1s committed
10
11
12
13
14
15
        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
16

rusty1s's avatar
rusty1s committed
17
18
19
20
        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
21

rusty1s's avatar
rusty1s committed
22
        row = row.narrow(0, row_start, row_length) - start
rusty1s's avatar
rusty1s committed
23
        col = col.narrow(0, row_start, row_length)
rusty1s's avatar
rusty1s committed
24
25
26
27
        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
28

rusty1s's avatar
rusty1s committed
29
30
31
32
33
34
35
        storage = src.storage.__class__(
            index,
            value,
            sparse_size,
            rowcount=rowcount,
            rowptr=rowptr,
            is_sorted=True)
rusty1s's avatar
rusty1s committed
36

rusty1s's avatar
rusty1s committed
37
    elif dim == 1:
rusty1s's avatar
rusty1s committed
38
        # This is faster than accessing `csc()` contrary to the `dim=0` case.
rusty1s's avatar
rusty1s committed
39
40
        (row, col), value = src.coo()
        mask = (col >= start) & (col < start + length)
rusty1s's avatar
rusty1s committed
41

rusty1s's avatar
rusty1s committed
42
43
44
45
46
47
48
        # 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
49
50
51
52
        if colptr is not None:
            colptr = colptr.narrow(0, start=start, length=length + 1)
            colptr = colptr - colptr[0]

rusty1s's avatar
rusty1s committed
53
54
55
56
        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
57

rusty1s's avatar
rusty1s committed
58
59
60
61
62
63
64
        storage = src.storage.__class__(
            index,
            value,
            sparse_size,
            colcount=colcount,
            colptr=colptr,
            is_sorted=True)
rusty1s's avatar
rusty1s committed
65
66

    else:
rusty1s's avatar
rusty1s committed
67
68
        storage = src.storage.apply_value(lambda x: x.narrow(
            dim - 1, start, length))
rusty1s's avatar
rusty1s committed
69

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