Skip to main content

GPU-accelerated t-SNE for CUDA, MPS, and CPU

Project description

g-tsne

GPU-accelerated t-SNE for Python. Runs on CUDA, Apple MPS, and CPU — no code changes needed across backends. Drop-in sklearn-compatible API.

from g_tsne import TSNE

embedding = TSNE(n_components=2, perplexity=30, device='cuda').fit_transform(X)

Install

pip install -e .

Dependencies: PyTorch ≥ 2.0, NumPy. FAISS is optional (used for faster KNN when available).


Quick Start

import numpy as np
from g_tsne import TSNE

X = np.random.randn(5000, 128)

# Auto-detects best device (CUDA > MPS > CPU)
embedding = TSNE(perplexity=30).fit_transform(X)

# Explicit device
embedding = TSNE(perplexity=30, device='cuda').fit_transform(X)

# Cosine distance (useful for text/image embeddings)
embedding = TSNE(perplexity=30, metric='cosine').fit_transform(X)

# Online: embed new points into an existing layout
tsne = TSNE(perplexity=30).fit(X)
X_new = np.random.randn(20, 128)
new_embedding = tsne.transform(X_new)

API

TSNE(...)

Parameter Default Description
n_components 2 Output dimensionality
perplexity 30.0 Effective neighbor count. Scalar or list for multiscale. Try 5–50.
learning_rate 'auto' 'auto' sets η = N / early_exaggeration (Belkina 2019). Or pass a float.
early_exaggeration 12.0 P scaling during early phase
early_exaggeration_iter 250 Iterations in early phase
n_iter 750 Total iterations (includes early exaggeration)
initialization 'pca' 'pca', 'random', or an (N, n_components) numpy array
metric 'euclidean' 'euclidean' or 'cosine'
negative_gradient_method 'auto' 'auto' (exact for N < 30K, fft for N ≥ 30K), 'exact', or 'fft'
exaggeration None Extra P scaling during refinement phase
dof 1.0 Student-t degrees of freedom. 1.0 = standard t-SNE.
max_step_norm None Per-point velocity clip. None = disabled.
knn_backend 'auto' 'auto', 'faiss', or 'exact'
knn_index None Precomputed (knn_indices, knn_dists) to skip KNN search
callbacks None List of fn(iter, kl, embedding) → bool. Return True to stop early.
callbacks_every_iters 50 How often callbacks and verbose logging fire
random_state None Integer seed for reproducibility
verbose False Print iteration, KL divergence, and timing
device None 'cuda', 'mps', 'cpu', or None (auto-detect)

Methods

Method Returns Description
fit_transform(X) np.ndarray Compute and return the embedding
fit(X) TSNE Compute embedding, store state for transform
transform(X_new) np.ndarray Embed new points into existing layout without re-running on all data

X can be a numpy array or a PyTorch tensor.


How It Works

Two gradient computation paths, selected automatically:

  • N < 30K — exact O(N²) GPU matrix multiply. A single cuBLAS call is faster than FFT at this scale.
  • N ≥ 30K — FFT-based O(N log N) repulsive gradient on a power-of-2 grid (256 or 512), using cuFFT / Metal FFT.

Per-point bandwidth (σ) is found via Newton's method rather than binary search — converges in 5–10 iterations instead of 50.

Online transform runs in O(m²) where m is the number of new points, instead of O((N+m)²) if the full embedding were re-run.


Running Tests

pytest tests/
# Benchmark
python tests/benchmark.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gpu_tsne-0.1.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gpu_tsne-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file gpu_tsne-0.1.0.tar.gz.

File metadata

  • Download URL: gpu_tsne-0.1.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for gpu_tsne-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5954855a246f3eaf6c0be59bbf21d33d503679f509981237c09601bf56d2602a
MD5 b882c57f3b97d798ec474d54a29d5c34
BLAKE2b-256 560aa9c2a3542dd2d137a529b5d9fcc87173ed2caa3ca98a8b4959d9f0da9371

See more details on using hashes here.

File details

Details for the file gpu_tsne-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gpu_tsne-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for gpu_tsne-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42634037c24527d9e11a4956f6ada001ec3115f0cec30b4bbc10dd3cd046565
MD5 afbfd178cb8d15b093ac318f00aa5d57
BLAKE2b-256 71d7514d26f1f840a0e50862d56a523d2f92bd1c8a42e7672b5ca574fe09b321

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page