setup.py 2.75 KB
Newer Older
anton's avatar
anton committed
1
2
3
#!/usr/bin/env python
import os
from setuptools import setup, find_packages
anton's avatar
anton committed
4
from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension
5

anton's avatar
anton committed
6
7
8
9
10

with open('requirements.txt') as f:
    requirements = f.read().splitlines()


anton's avatar
anton committed
11
long_description = """
12
13
14
This package implements an efficient parallel algorithm for the computation of discounted cumulative sums
with differentiable bindings to PyTorch. The `cumsum` operation is frequently seen in data science
domains concerned with time series, including Reinforcement Learning (RL).
anton's avatar
anton committed
15

16
17
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
18

19
20
The proposed parallel algorithm requires a total of `O(N log N)` operations, but takes only `O(log N)` time, which is a
considerable trade-off in many applications involving large inputs.
anton's avatar
anton committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

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

Find more details and the most up-to-date information on the project webpage:
https://www.github.com/toshas/torch-discounted-cumsum
"""


38
39
40
def configure_extensions():
    out = [
        CppExtension(
41
            'torch_learned_nonlin_cpu',
42
            [
43
                os.path.join('torch_learned_nonlin', 'learned_nonlin_cpu.cpp'),
44
45
46
47
48
49
            ],
        )
    ]
    try:
        out.append(
            CUDAExtension(
50
                'torch_learned_nonlin_cuda',
51
                [
52
53
                    os.path.join('torch_learned_nonlin', 'learned_nonlin_cuda.cpp'),
                    os.path.join('torch_learned_nonlin', 'learned_nonlin_cuda_kernel.cu'),
54
55
56
57
58
59
60
61
                ],
            )
        )
    except Exception as e:
        print(f'Failed to build CUDA extension, this part of the package will not work. Reason: {str(e)}')
    return out


62
setup(
63
    name='torch_learned_nonlin',
anton's avatar
anton committed
64
    version='1.0.2',
anton's avatar
anton committed
65
    description='Fast discounted cumulative sums in PyTorch',
anton's avatar
anton committed
66
67
    long_description=long_description,
    long_description_content_type='text/markdown',
anton's avatar
anton committed
68
69
70
71
72
73
    install_requires=requirements,
    python_requires='>=3.6',
    packages=find_packages(),
    author='Anton Obukhov',
    license='BSD',
    url='https://www.github.com/toshas/torch-discounted-cumsum',
74
    ext_modules=configure_extensions(),
75
76
    cmdclass={
        'build_ext': BuildExtension
anton's avatar
anton committed
77
78
79
80
81
82
    },
    keywords=[
        'pytorch', 'discounted', 'cumsum', 'cumulative', 'sum', 'scan', 'differentiable',
        'reinforcement', 'learning', 'rewards', 'time', 'series'
    ],
)