https://github.com/aliutkus/torchsearchsorted
Pytorch Custom CUDA kernel for searchsorted
Science Score: 23.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
1 of 5 committers (20.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.2%) to scientific vocabulary
Repository
Pytorch Custom CUDA kernel for searchsorted
Basic Info
Statistics
- Stars: 137
- Watchers: 4
- Forks: 19
- Open Issues: 6
- Releases: 1
Metadata Files
README.md
Pytorch Custom CUDA kernel for searchsorted
This repository is an implementation of the searchsorted function to work for pytorch CUDA Tensors. Initially derived from the great C extension tutorial, but totally changed since then because building C extensions is not available anymore on pytorch 1.0.
Warnings: * only works with pytorch > v1.4 and CUDA >= v10.1 * NOTE When using
searchsorted()for practical applications, tensors need to be contiguous in memory. This can be easily achieved by callingtensor.contiguous()on the input tensors. Failing to do so will lead to inconsistent results across applications.
Description
Implements a function searchsorted(a, v, out, side) that works just like the numpy version except that a and v are matrices.
* a is of shape either (1, ncols_a) or (nrows, ncols_a), and is contiguous in memory (do a.contiguous() to ensure this).
* v is of shape either (1, ncols_v) or (nrows, ncols_v), and is contiguous in memory (do v.contiguous() to ensure this).
* out is either None or of shape (nrows, ncols_v). If provided and of the right shape, the result is put there. This is to avoid costly memory allocations if the user already did it. If provided, out should be contiguous in memory too (do out.contiguous() to ensure this).
* side is either "left" or "right". See the numpy doc. Please not that the current implementation does not correctly handle this parameter. Help welcome to improve the speed of this PR
the output is of size as (nrows, ncols_v). If all input tensors are on GPU, a cuda version will be called. Otherwise, it will be on CPU.
Disclaimers
- This function has not been heavily tested. Use at your own risks
- When
ais not sorted, the results vary from numpy's version. But I decided not to care about this because the function should not be called in this case. - In some cases, the results vary from numpy's version. However, as far as I could see, this only happens when values are equal, which means we actually don't care about the order in which this value is added. I decided not to care about this also.
- vectors have to be contiguous for torchsearchsorted to give consistant results. use
.contiguous()on all tensor arguments before calling
Installation
Just pip install ., in the root folder of this repo. This will compile
and install the torchsearchsorted module.
be careful that sometimes, nvcc needs versions of gcc and g++ that are older than those found by default on the system. If so, just create symbolic links to the right versions in your cuda/bin folder (where nvcc is)
For instance, on my machine, I had gcc and g++ v9 installed, but nvcc required v8.
So I had to do:
sudo apt-get install g++-8 gcc-8
sudo ln -s /usr/bin/gcc-8 /usr/local/cuda-10.1/bin/gcc
sudo ln -s /usr/bin/g++-8 /usr/local/cuda-10.1/bin/g++
be careful that you need pytorch to be installed on your system. The code was tested on pytorch v1.5
Usage
Just import the torchsearchsorted package after installation. I typically do:
from torchsearchsorted import searchsorted
Testing
Under the examples subfolder, you may:
- try
python test.pywithtorchavailable.
``` Looking for 50000x1000 values in 50000x300 entries NUMPY: searchsorted in 4851.592ms CPU: searchsorted in 4805.432ms difference between CPU and NUMPY: 0.000 GPU: searchsorted in 1.055ms difference between GPU and NUMPY: 0.000
Looking for 50000x1000 values in 50000x300 entries NUMPY: searchsorted in 4333.964ms CPU: searchsorted in 4753.958ms difference between CPU and NUMPY: 0.000 GPU: searchsorted in 0.391ms difference between GPU and NUMPY: 0.000 ``` The first run comprises the time of allocation, while the second one does not.
- You may also use the nice
benchmark.pycode written by @baldassarreFe, that testssearchsortedon many runs:
``` Benchmark searchsorted: - a [5000 x 300] - v [5000 x 100] - reporting fastest time of 20 runs - each run executes searchsorted 100 times
Numpy: 4.6302046799100935 CPU: 5.041533078998327 CUDA: 0.0007955809123814106 ```
Owner
- Name: Antoine Liutkus
- Login: aliutkus
- Kind: user
- Location: France
- Company: @INRIA
- Repositories: 7
- Profile: https://github.com/aliutkus
Researcher at Inria
GitHub Events
Total
- Watch event: 2
Last Year
- Watch event: 2
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Antoine Liutkus | a****s@i****r | 51 |
| Chris Choy | c****8@g****m | 14 |
| Marian Ivanov | m****v@g****m | 4 |
| Krishna Murthy | k****4@g****m | 1 |
| Federico Baldassarre | b****e@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 32
- Total pull requests: 8
- Average time to close issues: 17 days
- Average time to close pull requests: about 2 months
- Total issue authors: 17
- Total pull request authors: 5
- Average comments per issue: 5.22
- Average comments per pull request: 0.63
- Merged pull requests: 5
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- krrish94 (2)
- stone100010 (2)
- baldassarreFe (2)
- qsobad (1)
- WangDeyao (1)
- KovenYu (1)
- soumith (1)
- rxqy (1)
- AndreaPi (1)
- Veereshdammur (1)
- qway (1)
- grey-area (1)
- HannesStark (1)
- FarmerLiuAng (1)
- redyxg (1)
Pull Request Authors
- ScheiklP (2)
- pl0xz0rz (2)
- baldassarreFe (2)
- aliutkus (1)
- krrish94 (1)