"git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "d4c038daede43544d107f81cb5b6337c7a13803a"
README.md 7.63 KB
Newer Older
Daniel Povey's avatar
Daniel Povey committed
1
2
# TODO: update this README!

anton's avatar
anton committed
3
# Fast Discounted Cumulative Sums in PyTorch
Anton Obukhov's avatar
Anton Obukhov committed
4

anton's avatar
anton committed
5
6
7
[![PyPiVersion](https://badge.fury.io/py/torch-discounted-cumsum.svg)](https://pypi.org/project/torch-discounted-cumsum/)
![PythonVersion](https://img.shields.io/badge/python-%3E%3D3.6-yellowgreen)
[![PyPiDownloads](https://pepy.tech/badge/torch-discounted-cumsum)](https://pepy.tech/project/torch-discounted-cumsum)
anton's avatar
anton committed
8
9
[![License](https://img.shields.io/badge/License(code)-BSD%203--Clause-blue.svg)](LICENSE_code)
[![License: CC BY 4.0](https://img.shields.io/badge/License(doc)-CC%20BY%204.0-lightgrey.svg)](LICENSE_doc)
Anton Obukhov's avatar
Anton Obukhov committed
10
[![Twitter Follow](https://img.shields.io/twitter/follow/AntonObukhov1?style=social&label=Subscribe!)](https://twitter.com/antonobukhov1)
anton's avatar
anton committed
11

Anton Obukhov's avatar
Anton Obukhov committed
12
<img src="doc/img/logo_small.png" align="left" width="33%">
anton's avatar
anton committed
13

Daniel Povey's avatar
Daniel Povey committed
14
15
16
This repository implements an efficient parallel algorithm for the computation of discounted cumulative sums
and a Python package with differentiable bindings to PyTorch. The discounted `cumsum` operation is frequently seen in
data science domains concerned with time series, including Reinforcement Learning (RL).
anton's avatar
anton committed
17

Daniel Povey's avatar
Daniel Povey committed
18
19
The traditional sequential algorithm performs the computation of the output elements in a loop. For an input of size
`N`, it requires `O(N)` operations and takes `O(N)` time steps to complete.
anton's avatar
anton committed
20

Daniel Povey's avatar
Daniel Povey committed
21
22
The proposed parallel algorithm requires a total of `O(N log N)` operations, but takes only `O(log N)` time steps, which is a
considerable trade-off in many applications involving large inputs.
anton's avatar
anton committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

Features of the parallel algorithm:
- Speed logarithmic in the input size
- Better numerical precision than sequential algorithms

Features of the package:
- CPU: sequential algorithm in C++
- GPU: parallel algorithm in CUDA
- Gradients computation wrt input
- Both left and right directions of summation supported
- PyTorch bindings

## Usage

#### Installation

```shell script
pip install torch-discounted-cumsum
```

#### API

- `discounted_cumsum_right`: Computes discounted cumulative sums to the right of each position (a standard setting in RL)
- `discounted_cumsum_left`: Computes discounted cumulative sums to the left of each position

#### Example

```python
import torch
from torch_discounted_cumsum import discounted_cumsum_right

N = 8
gamma = 0.99
x = torch.ones(1, N).cuda()
y = discounted_cumsum_right(x, gamma)

print(y)
```

Output:
```
tensor([[7.7255, 6.7935, 5.8520, 4.9010, 3.9404, 2.9701, 1.9900, 1.0000]],
       device='cuda:0')
```

#### Up to `K` elements

```python
import torch
from torch_discounted_cumsum import discounted_cumsum_right

N = 8
K = 2
gamma = 0.99
x = torch.ones(1, N).cuda()
Anton Obukhov's avatar
Anton Obukhov committed
78
y_N = discounted_cumsum_right(x, gamma)
Daniel Povey's avatar
Daniel Povey committed
79
y_K = y_N - (gamma ** K) * torch.cat((y_N[:, K:], torch.zeros(1, K).cuda()), dim=1)
anton's avatar
anton committed
80
81
82
83
84
85
86
87
88
89
90
91
92

print(y_K)
```

Output:
```
tensor([[1.9900, 1.9900, 1.9900, 1.9900, 1.9900, 1.9900, 1.9900, 1.0000]],
       device='cuda:0')
```


## Parallel Algorithm

Daniel Povey's avatar
Daniel Povey committed
93
94
95
96
97
98
For the sake of simplicity, the algorithm is explained for `N=16`.
The processing is performed in-place in the input vector in `log2 N` stages. Each stage updates `N / 2` positions in parallel
(that is, in a single time step, provided unrestricted parallelism). A stage is characterized by the size of the group of
sequential elements being updated, which is computed as `2 ^ (stage - 1)`.
The group stride is always twice larger than the group size. The elements updated during the stage are highlighted with
the respective stage color in the figure below. Here input elements are denoted with their position id in hex, and the
Anton Obukhov's avatar
Anton Obukhov committed
99
elements tagged with two symbols indicate the range over which the discounted partial sum is computed upon stage completion.
anton's avatar
anton committed
100

Daniel Povey's avatar
Daniel Povey committed
101
102
103
104
Each element update includes an in-place addition of a discounted element, which follows the last
updated element in the group. The discount factor is computed as gamma raised to the power of the distance between the
updated and the discounted elements. In the figure below, this operation is denoted with tilted arrows with a greek
gamma tag. After the last stage completes, the output is written in place of the input.
anton's avatar
anton committed
105

anton's avatar
anton committed
106
107
108
<p align="center">
<img src="doc/img/algorithm.png">
</p>
anton's avatar
anton committed
109

Daniel Povey's avatar
Daniel Povey committed
110
111
In the CUDA implementation, `N / 2` CUDA threads are allocated during each stage to update the respective elements. The
strict separation of updates into stages via separate kernel invocations guarantees stage-level synchronization and
anton's avatar
anton committed
112
113
global consistency of updates.

Daniel Povey's avatar
Daniel Povey committed
114
The gradients wrt input can be obtained from the gradients wrt output by simply taking `cumsum` operation with the
anton's avatar
anton committed
115
116
117
118
reversed direction of summation.

## Numerical Precision

Daniel Povey's avatar
Daniel Povey committed
119
The parallel algorithm produces a more numerically-stable output than the sequential algorithm using the same scalar
anton's avatar
anton committed
120
121
data type.

Daniel Povey's avatar
Daniel Povey committed
122
123
124
The comparison is performed between 3 runs with identical inputs ([code](tests/test.py#L116)). The first run casts inputs to
double precision and obtains the output reference using the sequential algorithm. Next, we run both sequential and
parallel algorithms with the same inputs cast to single precision and compare the results to the reference. The
anton's avatar
anton committed
125
126
comparison is performed using the `L_inf` norm, which is just the maximum of per-element discrepancies.

Daniel Povey's avatar
Daniel Povey committed
127
128
129
With 10000-element non-zero-centered input (such as all elements are 1.0), the errors of the algorithms are 2.8e-4
(sequential) and 9.9e-5 (parallel). With zero-centered inputs (such as standard gaussian noise), the errors are
1.8e-5 (sequential) and 1.5e-5 (parallel).
anton's avatar
anton committed
130
131
132

## Speed-up

Daniel Povey's avatar
Daniel Povey committed
133
134
We tested 3 implementations of the algorithm with the same 100000-element input ([code](tests/test.py#L154)):
1. Sequential in PyTorch on CPU (as in
Anton Obukhov's avatar
Anton Obukhov committed
135
136
[REINFORCE](https://github.com/pytorch/examples/blob/87d9a1e/reinforcement_learning/reinforce.py#L66-L68)) (Intel Xeon CPU, DGX-1)
2. Sequential in C++ on CPU (Intel Xeon CPU, DGX-1)
anton's avatar
anton committed
137
138
3. Parallel in CUDA (NVIDIA P-100, DGX-1)

Daniel Povey's avatar
Daniel Povey committed
139
The observed speed-ups are as follows:
anton's avatar
anton committed
140
141
142
143
- PyTorch to C++: 387 times
- PyTorch to CUDA: 36573 times
- C++ to CUDA: 94 times

anton's avatar
anton committed
144
## Ops-Space-Time Complexity
Daniel Povey's avatar
Daniel Povey committed
145

anton's avatar
anton committed
146
Assumptions:
Daniel Povey's avatar
Daniel Povey committed
147
- A fused operation of raising `gamma` to a power, multiplying the result by `x`, and adding `y` is counted as a
anton's avatar
anton committed
148
single fused operation;
Daniel Povey's avatar
Daniel Povey committed
149
150
- `N` is a power of two. When it isn't, the parallel algorithm's complexity is the same as with N equal to the next
power of two.
anton's avatar
anton committed
151

Daniel Povey's avatar
Daniel Povey committed
152
Under these assumptions, the sequential algorithm takes `N` operations and `N` time steps to complete.
anton's avatar
anton committed
153
The parallel algorithm takes `0.5 * N * log2 N` operations and can be completed in `log2 N` time steps
Daniel Povey's avatar
Daniel Povey committed
154
if the parallelism is unrestricted.
anton's avatar
anton committed
155
156
157
158
159
160
161

Both algorithms can be performed in-place; hence their space complexity is `O(1)`.

## In Other Frameworks

#### PyTorch

Daniel Povey's avatar
Daniel Povey committed
162
163
164
As of the time of writing, PyTorch does not provide discounted `cumsum` functionality via the API. PyTorch RL code
samples (e.g., [REINFORCE](https://github.com/pytorch/examples/blob/87d9a1e/reinforcement_learning/reinforce.py#L66-L68))
suggest computing returns in a loop over reward items. Since most RL algorithms do not require differentiating through
anton's avatar
anton committed
165
166
167
168
returns, many code samples resort to using SciPy function listed below.

#### TensorFlow

Daniel Povey's avatar
Daniel Povey committed
169
TensorFlow API provides `tf.scan` API, which can be supplied with an appropriate lambda function implementing the
anton's avatar
anton committed
170
formula above. Under the hood, however, `tf.scan` implement the traditional sequential algorithm.
Daniel Povey's avatar
Daniel Povey committed
171

anton's avatar
anton committed
172
173
#### SciPy

Daniel Povey's avatar
Daniel Povey committed
174
175
SciPy provides a `scipy.signal.lfilter` function for computing IIR filter response using the sequential algorithm, which
can be used for the task at hand, as suggested in this [StackOverflow](https://stackoverflow.com/a/47971187/411907)
anton's avatar
anton committed
176
177
178
179
180
181
182
183
response.

## Citation

To cite this repository, use the following BibTeX:

```
@misc{obukhov2021torchdiscountedcumsum,
Anton Obukhov's avatar
Anton Obukhov committed
184
185
186
187
  author={Anton Obukhov},
  year=2021,
  title={Fast discounted cumulative sums in PyTorch},
  url={https://github.com/toshas/torch-discounted-cumsum}
anton's avatar
anton committed
188
189
}
```