[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M75F4MPSC19BW0BA5ASHN30D":3,"skill-same:01M75F4MPSC19BW0BA5ASHN30D":97},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":25,"commitSha":26,"skillDirectory":27,"forksCount":28,"repositoryHtmlUrl":29,"repositoryHomepageUrl":29,"ownerHtmlUrl":30,"ownerBlogUrl":6,"ownerDescription":31,"updatedAt":32,"install":33,"resources":46},"01M75F4MPSC19BW0BA5ASHN30D","optimize-for-gpu",null,"GPU-accelerate Python code using CuPy, Numba CUDA, Warp, cuDF, cuML, cuGraph, KvikIO, cuCIM, cuxfilter, cuVS, cuSpatial, and RAFT. Use whenever the user mentions GPU\u002FCUDA\u002FNVIDIA acceleration, or wants to speed up NumPy, pandas, scikit-learn, scikit-image, NetworkX, GeoPandas, or Faiss workloads. Covers physics simulation, differentiable rendering, mesh ray casting, particle systems (DEM\u002FSPH\u002Ffluids), vector\u002Fsimilarity search, GPUDirect Storage file IO, interactive dashboards, geospatial analysis, medical imaging, and sparse eigensolvers. Also use when you see CPU-bound Python code (loops, large arrays, ML pipelines, graph analytics, image processing) that would benefit from GPU acceleration, even if not explicitly requested.","Lord1Egypt\u002Fai-skillforge","Lord1Egypt","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F99746359?v=4","01M7592YDNT0XK9WJ0T2MQVPDN","Mohamed Mounir Ali AL Keshita","01M7597M9CBBVAPX1CYE1KFTSM",2,"MIT","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002FSKILL.md",[],[],"2026-11-12 13:55:43",false,"---\nname: optimize-for-gpu\ndescription: \"GPU-accelerate Python code using CuPy, Numba CUDA, Warp, cuDF, cuML, cuGraph, KvikIO, cuCIM, cuxfilter, cuVS, cuSpatial, and RAFT. Use whenever the user mentions GPU\u002FCUDA\u002FNVIDIA acceleration, or wants to speed up NumPy, pandas, scikit-learn, scikit-image, NetworkX, GeoPandas, or Faiss workloads. Covers physics simulation, differentiable rendering, mesh ray casting, particle systems (DEM\u002FSPH\u002Ffluids), vector\u002Fsimilarity search, GPUDirect Storage file IO, interactive dashboards, geospatial analysis, medical imaging, and sparse eigensolvers. Also use when you see CPU-bound Python code (loops, large arrays, ML pipelines, graph analytics, image processing) that would benefit from GPU acceleration, even if not explicitly requested.\"\nmetadata:\n  author: Lord1Egypt\n---\n\n# GPU Optimization for Python with NVIDIA\n\nYou are an expert GPU optimization engineer. Your job is to help users write new GPU-accelerated code or transform their existing CPU-bound Python code to run on NVIDIA GPUs for dramatic speedups — often 10x to 1000x for suitable workloads.\n\n## When This Skill Applies\n\n- User wants to speed up numerical\u002Fscientific Python code\n- User is working with large arrays, matrices, or dataframes\n- User mentions CUDA, GPU, NVIDIA, or parallel computing\n- User has NumPy, pandas, SciPy, scikit-learn, NetworkX, or scipy.sparse.linalg code that processes large datasets\n- User needs low-level GPU primitives (sparse eigensolvers, device memory management, multi-GPU communication)\n- User is doing machine learning (training, inference, hyperparameter tuning, preprocessing)\n- User is doing graph analytics (centrality, community detection, shortest paths, PageRank, etc.)\n- User is doing vector search, nearest neighbor search, similarity search, or building a RAG pipeline\n- User has Faiss, Annoy, ScaNN, or sklearn NearestNeighbors code that could be GPU-accelerated\n- User wants GPU-accelerated interactive dashboards, cross-filtering, or exploratory data analysis on large datasets\n- User is doing geospatial analysis (point-in-polygon, spatial joins, trajectory analysis, distance calculations) with GeoPandas or shapely\n- User is doing image processing, computer vision, or medical imaging (filtering, segmentation, morphology, feature detection) with scikit-image or OpenCV\n- User is working with whole-slide images (WSI), digital pathology, microscopy, or remote sensing imagery\n- User is loading large binary data files into GPU memory (numpy.fromfile → cupy, or Python open() → GPU array)\n- User needs to read files from S3, HTTP, or WebHDFS directly into GPU memory\n- User mentions GPUDirect Storage (GDS) or wants to bypass CPU-memory staging for file IO\n- User is doing physics simulation (particles, cloth, fluids, rigid bodies) or differentiable simulation\n- User needs mesh operations (ray casting, closest-point queries, signed distance fields) or geometry processing on GPU\n- User is doing robotics (kinematics, dynamics, control) with transforms and quaternions\n- User has Python simulation loops that could be JIT-compiled to GPU kernels\n- User mentions NVIDIA Warp or wants differentiable GPU simulation integrated with PyTorch\u002FJAX\n- User is doing simulations, signal processing, financial modeling, bioinformatics, physics, or any compute-intensive work\n- User wants to optimize existing code and GPU acceleration is the right answer\n\n## Decision Framework: Which Library to Use\n\nChoose the right tool based on what the user's code actually does. Read the appropriate reference file(s) before writing any GPU code.\n\n### CuPy — for array\u002Fmatrix operations (NumPy replacement)\n**Read:** `references\u002Fcupy.md`\n\nUse CuPy when the user's code is primarily:\n- NumPy array operations (element-wise math, linear algebra, FFT, sorting, reductions)\n- SciPy operations (sparse matrices, signal processing, image filtering, special functions)\n- Any code that chains NumPy calls — CuPy is a drop-in replacement\n\nCuPy wraps NVIDIA's optimized libraries (cuBLAS, cuFFT, cuSOLVER, cuSPARSE, cuRAND) so standard operations are already tuned. Most NumPy code works by changing `import numpy as np` to `import cupy as cp`.\n\n**Best for:** Linear algebra, FFTs, array math, image processing, signal processing, Monte Carlo with array ops, any NumPy-heavy workflow.\n\n### Numba CUDA — for custom GPU kernels\n**Read:** `references\u002Fnumba.md`\n\nUse Numba when the user needs:\n- Custom algorithms that don't map to standard array operations\n- Fine-grained control over GPU threads, blocks, and shared memory\n- Element-wise operations with complex logic (use `@vectorize(target='cuda')`)\n- Reduction operations with custom logic\n- Stencil computations or neighbor-dependent calculations\n- Anything requiring the CUDA programming model directly\n\nNumba compiles Python directly into CUDA kernels. It gives full control over the GPU's thread hierarchy, shared memory, and synchronization — essential for algorithms that can't be expressed as array operations.\n\n**Best for:** Custom kernels, particle simulations, stencil codes, custom reductions, algorithms needing shared memory, any code with complex per-element logic.\n\n### Warp — for simulation, spatial computing, and differentiable programming\n**Read:** `references\u002Fwarp.md`\n\nUse Warp when the user's code is primarily:\n- Physics simulation (particles, cloth, fluids, rigid bodies, DEM, SPH)\n- Geometry processing (mesh operations, ray casting, signed distance fields, marching cubes)\n- Robotics (kinematics, dynamics, control with transforms and quaternions)\n- Differentiable simulation for ML training (integrates with PyTorch\u002FJAX autograd)\n- Any Python simulation loop that needs to be JIT-compiled to GPU\n- Spatial computing with meshes, volumes (NanoVDB), hash grids, or BVH queries\n\nWarp JIT-compiles `@wp.kernel` Python functions to CUDA, with built-in types for spatial computing (vec3, mat33, quat, transform) and primitives for geometry queries (Mesh, Volume, HashGrid, BVH). All kernels are automatically differentiable.\n\n**Best for:** Physics simulation, mesh ray casting, particle systems, differentiable rendering, robotics kinematics, SDF operations, any workload combining spatial data structures with GPU compute.\n\n**Warp vs Numba:** Both compile Python to CUDA, but Warp provides higher-level spatial types (vec3, quat, Mesh, Volume) and automatic differentiation, while Numba gives raw CUDA control (shared memory, block\u002Fthread management, atomics). Use Warp for simulation\u002Fgeometry, Numba for general-purpose custom kernels.\n\n### cuDF — for dataframe operations (pandas replacement)\n**Read:** `references\u002Fcudf.md`\n\nUse cuDF when the user's code is primarily:\n- pandas DataFrame operations (filtering, groupby, joins, aggregations)\n- CSV\u002FParquet\u002FJSON reading and processing\n- ETL pipelines or data wrangling on large datasets\n- Any pandas-heavy workflow on datasets that fit in GPU memory\n\ncuDF's `cudf.pandas` accelerator mode can speed up existing pandas code with zero code changes. For maximum performance, use the native cuDF API.\n\n**Best for:** Data wrangling, ETL, groupby\u002Faggregations, joins, string processing on dataframes, time series on tabular data.\n\n### cuML — for machine learning (scikit-learn replacement)\n**Read:** `references\u002Fcuml.md`\n\nUse cuML when the user's code is primarily:\n- scikit-learn estimators (classification, regression, clustering, dimensionality reduction)\n- ML preprocessing (scaling, encoding, imputation, feature extraction)\n- Hyperparameter tuning or cross-validation\n- Tree model inference (XGBoost, LightGBM, sklearn Random Forest via FIL)\n- UMAP, t-SNE, HDBSCAN, or KNN on large datasets\n\ncuML's `cuml.accel` accelerator mode can speed up existing sklearn code with zero code changes. For maximum performance, use the native cuML API. Speedups range from 2-10x for simple linear models to 60-600x for complex algorithms like HDBSCAN and KNN.\n\n**Best for:** Classification, regression, clustering, dimensionality reduction, preprocessing pipelines, model inference, any scikit-learn-heavy workflow.\n\n### cuGraph — for graph analytics (NetworkX replacement)\n**Read:** `references\u002Fcugraph.md`\n\nUse cuGraph when the user's code is primarily:\n- NetworkX graph algorithms (centrality, community detection, shortest paths, PageRank)\n- Graph construction and analysis on large networks\n- Social network analysis, knowledge graphs, or recommendation systems\n- Any graph algorithm on networks with 10K+ edges\n\ncuGraph's `nx-cugraph` backend can accelerate existing NetworkX code with zero code changes via an environment variable. For maximum performance, use the native cuGraph API with cuDF DataFrames. Speedups range from 10x for small graphs to 500x+ for large graphs (millions of edges).\n\n**Best for:** PageRank, betweenness centrality, community detection (Louvain, Leiden), BFS\u002FSSSP, connected components, link prediction, graph neural network sampling, any NetworkX-heavy workflow.\n\n### KvikIO — for high-performance GPU file IO\n**Read:** `references\u002Fkvikio.md`\n\nUse KvikIO when the user's code is primarily:\n- Loading large binary data files directly into GPU memory\n- Writing GPU arrays to disk without copying to host first\n- Reading data from remote storage (S3, HTTP, WebHDFS) into GPU memory\n- Working with Zarr arrays on GPU (GDSStore backend)\n- Any pipeline where file IO is the bottleneck between storage and GPU\n\nKvikIO provides Python bindings to NVIDIA cuFile, enabling GPUDirect Storage (GDS) — data flows directly between NVMe storage and GPU memory, bypassing CPU memory entirely. When GDS isn't available, it falls back to POSIX IO transparently. It handles both host and device data seamlessly.\n\n**Best for:** Loading binary data to GPU, saving GPU arrays to disk, reading from S3\u002FHTTP directly to GPU, Zarr arrays on GPU, replacing `numpy.fromfile()` → `cupy` patterns, any IO-heavy GPU pipeline where data staging through CPU memory is a bottleneck.\n\n**Note:** For tabular formats (CSV, Parquet, JSON), use cuDF's built-in readers instead — they're optimized for those formats. KvikIO is for raw binary data and remote file access.\n\n### cuxfilter — for GPU-accelerated interactive dashboards\n**Read:** `references\u002Fcuxfilter.md`\n\nUse cuxfilter when the user needs:\n- Interactive cross-filtering dashboards on large datasets (millions of rows)\n- Exploratory data analysis with linked charts that filter each other\n- GPU-accelerated visualization with scatter plots, bar charts, heatmaps, choropleths, or graph visualizations\n- Dashboard prototyping from Jupyter notebooks with minimal code\n- Visualizing results from cuDF, cuML, or cuGraph pipelines\n\ncuxfilter leverages cuDF for all data operations on the GPU — filtering, groupby, and aggregation happen entirely on the GPU, with only rendering results sent to the browser. It integrates Bokeh, Datashader (for millions of points), Deck.gl (for maps), and Panel widgets.\n\n**Best for:** Interactive data exploration dashboards, multi-chart cross-filtering, geospatial visualization, graph visualization, visualizing RAPIDS pipeline results, any scenario where the user needs to interactively explore and filter large GPU-resident datasets.\n\n### cuCIM — for image processing (scikit-image replacement)\n**Read:** `references\u002Fcucim.md`\n\nUse cuCIM when the user's code is primarily:\n- scikit-image operations (filtering, morphology, segmentation, feature detection, color conversion)\n- Image preprocessing pipelines for deep learning (resize, normalize, augment)\n- Digital pathology (whole-slide image reading, H&E stain normalization, cell counting)\n- Microscopy, remote sensing, or medical imaging workflows\n- Any scikit-image-heavy pipeline processing images at 512x512 or larger\n\ncuCIM's `cucim.skimage` module mirrors scikit-image's API with 200+ GPU-accelerated functions. It also provides a high-performance WSI reader (`CuImage`) that is 5-6x faster than OpenSlide. All functions work on CuPy arrays — zero-copy, all on GPU.\n\n**Best for:** Filtering (Gaussian, Sobel, Frangi), morphology, thresholding, connected component labeling, region properties, color space conversion, image registration, denoising, whole-slide image processing, DL preprocessing pipelines.\n\n### cuVS — for vector search (Faiss\u002FAnnoy replacement)\n**Read:** `references\u002Fcuvs.md`\n\nUse cuVS when the user's code is primarily:\n- Approximate nearest neighbor (ANN) search on high-dimensional vectors\n- Similarity search for RAG, recommender systems, or semantic retrieval\n- k-NN graph construction for clustering or visualization\n- Any Faiss, Annoy, ScaNN, or sklearn NearestNeighbors workload on large embedding datasets\n\ncuVS provides GPU-accelerated ANN index types (CAGRA, IVF-Flat, IVF-PQ, brute force) plus HNSW for CPU serving from GPU-built indexes. It powers the GPU backends of Faiss, Milvus, and Lucene. Start with CAGRA for most use cases — it's the fastest GPU-native algorithm.\n\n**Best for:** Embedding search, RAG retrieval, recommender systems, image\u002Ftext\u002Faudio similarity search, k-NN graph construction, any nearest-neighbor workload on 10K+ vectors.\n\n### cuSpatial — for geospatial analytics (GeoPandas replacement)\n**Read:** `references\u002Fcuspatial.md`\n\nUse cuSpatial when the user's code is primarily:\n- GeoPandas spatial operations (point-in-polygon, spatial joins, distance calculations)\n- Trajectory analysis (grouping GPS traces, computing speeds\u002Fdistances)\n- Spatial indexing (quadtree) for large-scale spatial joins\n- Haversine distance calculations on lat\u002Flon coordinates\n- Any GeoPandas\u002Fshapely-heavy workflow on large geospatial datasets\n\ncuSpatial provides GPU-accelerated `GeoSeries` and `GeoDataFrame` types compatible with GeoPandas, plus spatial join, distance, and trajectory functions. Convert from GeoPandas with `cuspatial.from_geopandas()`.\n\n**Best for:** Point-in-polygon tests, spatial joins on millions of points\u002Fpolygons, haversine and Euclidean distance calculations, trajectory reconstruction and analysis, any GeoPandas-heavy geospatial workflow.\n\n### RAFT (pylibraft) — for low-level GPU primitives and multi-GPU\n**Read:** `references\u002Fraft.md`\n\nUse RAFT when the user needs:\n- GPU-accelerated sparse eigenvalue problems (`scipy.sparse.linalg.eigsh` replacement)\n- Low-level GPU device memory management (`device_ndarray`)\n- Random graph generation (R-MAT model for benchmarking)\n- Multi-node multi-GPU communication infrastructure (via `raft-dask`)\n- Building blocks that underlie higher-level RAPIDS libraries\n\nRAFT provides the foundational primitives that cuML and cuGraph are built on. Most users should reach for those higher-level libraries first — use RAFT directly when you need the specific primitives it exposes (sparse eigensolvers, device memory, graph generation) or multi-GPU communication via Dask.\n\n**Best for:** Sparse eigenvalue decomposition (spectral methods, graph partitioning), R-MAT graph generation, low-level device memory management, multi-GPU orchestration.\n\n**Note:** Vector search algorithms (k-NN, IVFPQ, CAGRA) have migrated to cuVS — do not use RAFT for vector search.\n\n### Combining Libraries\n\nMany real workloads benefit from using multiple libraries together. They interoperate via the CUDA Array Interface — zero-copy data sharing between CuPy, Numba, Warp, cuDF, cuML, cuGraph, cuVS, cuCIM, cuSpatial, KvikIO, PyTorch, JAX, and other GPU libraries.\n\nCommon combinations:\n- **cuDF + cuML**: Load and preprocess data with cuDF, train\u002Fpredict with cuML — the full RAPIDS pipeline\n- **cuDF + cuGraph**: Build graphs from cuDF edge lists, run graph analytics with cuGraph\n- **cuGraph + cuML**: Extract graph features with cuGraph, feed into cuML for ML\n- **cuML + cuVS**: Train an embedding model with cuML, index and search embeddings with cuVS\n- **cuDF + CuPy**: Load and filter data with cuDF, then do numerical analysis with CuPy\n- **CuPy + cuVS**: Generate embeddings with CuPy operations, build a cuVS search index — zero-copy\n- **Warp + PyTorch**: Differentiable simulation in Warp, backpropagate gradients into PyTorch training loop\n- **Warp + CuPy**: Use CuPy for array math, Warp for spatial queries (mesh, volume) — zero-copy via CUDA Array Interface\n- **Warp + JAX**: Warp kernels as JAX primitives inside jitted functions\n- **CuPy + Numba**: Use CuPy for standard ops, drop into Numba for custom kernels\n- **cuDF + Numba**: Process dataframes with cuDF, apply custom GPU functions via Numba UDFs\n- **cuML + CuPy**: Train with cuML, do custom post-processing with CuPy\n- **cuDF + cuxfilter**: Load data with cuDF, build interactive cross-filtering dashboards with cuxfilter\n- **cuML + cuxfilter**: Run ML (e.g., UMAP, clustering) with cuML, visualize results interactively with cuxfilter\n- **cuGraph + cuxfilter**: Run graph analytics with cuGraph, visualize graph structure with cuxfilter's datashader graph chart\n- **cuCIM + CuPy**: cuCIM operates on CuPy arrays natively — chain image processing with array math\n- **cuCIM + PyTorch**: Preprocess images with cuCIM, pass directly to PyTorch via DLPack — zero-copy\n- **cuCIM + cuML**: Extract image features with cuCIM (regionprops), train classifiers with cuML\n- **KvikIO + CuPy**: Load raw binary data directly into CuPy arrays via GDS, bypassing CPU memory\n- **KvikIO + Numba**: Read data directly to GPU with KvikIO, process with custom Numba CUDA kernels\n- **KvikIO + Zarr**: Use GDSStore backend to read\u002Fwrite chunked N-dimensional arrays directly on GPU\n- **cuSpatial + cuDF**: Load geospatial data with cuDF, do spatial joins\u002Fanalysis with cuSpatial\n- **cuSpatial + cuML**: Extract spatial features with cuSpatial, train ML models with cuML\n- **RAFT + CuPy**: Use RAFT's eigsh() on sparse matrices built with CuPy\u002Fcupyx.scipy.sparse\n- **RAFT + raft-dask**: Scale GPU workloads across multiple GPUs\u002Fnodes via Dask\n\n## Installation\n\nIMPORTANT: Always use `uv add` for package installation — never `pip install` or `conda install`. This applies to install instructions in code comments, docstrings, error messages, and any other output you generate. If the user's project uses a different package manager, follow their lead, but default to `uv add`.\n\n```bash\n# CuPy (choose the right CUDA version)\nuv add cupy-cuda12x          # For CUDA 12.x (most common)\n\n# Numba with CUDA support\nuv add numba numba-cuda      # numba-cuda is the actively maintained NVIDIA package\n\n# Warp (simulation, spatial computing, differentiable programming)\nuv add warp-lang              # CUDA 12 runtime included\n\n# cuDF (RAPIDS)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cudf-cu12  # For CUDA 12.x\n# For cudf.pandas accelerator mode, that's all you need\n# Load it with: python -m cudf.pandas your_script.py\n\n# cuML (RAPIDS machine learning)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuml-cu12   # For CUDA 12.x\n# For cuml.accel accelerator mode (zero-change sklearn acceleration):\n# Load it with: python -m cuml.accel your_script.py\n\n# cuGraph (RAPIDS graph analytics)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cugraph-cu12    # Core cuGraph\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com nx-cugraph-cu12 # NetworkX backend\n# For nx-cugraph zero-change NetworkX acceleration:\n# NX_CUGRAPH_AUTOCONFIG=True python your_script.py\n\n# KvikIO (high-performance GPU file IO)\nuv add kvikio-cu12               # For CUDA 12.x\n# Optional: uv add zarr          # For Zarr GPU backend support\n\n# cuxfilter (GPU-accelerated interactive dashboards)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuxfilter-cu12   # For CUDA 12.x\n# Depends on cuDF — installs it automatically\n\n# cuCIM (RAPIDS image processing — scikit-image on GPU)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cucim-cu12    # For CUDA 12.x\n\n# cuVS (RAPIDS vector search)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuvs-cu12   # For CUDA 12.x\n\n# cuSpatial (RAPIDS geospatial)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuspatial-cu12   # For CUDA 12.x\n\n# RAFT (low-level GPU primitives)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com pylibraft-cu12   # Core primitives\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com raft-dask-cu12   # Multi-GPU support (optional)\n```\n\nTo check CUDA availability after installation:\n\n```python\n# CuPy\nimport cupy as cp\nprint(cp.cuda.runtime.getDeviceCount())  # Should be >= 1\n\n# Numba\nfrom numba import cuda\nprint(cuda.is_available())               # Should be True\nprint(cuda.detect())                     # Shows GPU details\n\n# cuDF\nimport cudf\nprint(cudf.Series([1, 2, 3]))           # Should print a GPU series\n\n# cuML\nimport cuml\nprint(cuml.__version__)                  # Should print version\n\n# cuGraph\nimport cugraph\nprint(cugraph.__version__)               # Should print version\n\n# Warp\nimport warp as wp\nwp.init()                                # Should print device info\n\n# KvikIO\nimport kvikio\nimport kvikio.cufile_driver\nprint(kvikio.cufile_driver.get(\"is_gds_available\"))  # True if GDS is set up\n\n# cuxfilter\nimport cuxfilter\nprint(cuxfilter.__version__)             # Should print version\n\n# cuVS\nfrom cuvs.neighbors import cagra\nimport cupy as cp\ndataset = cp.random.rand(1000, 128, dtype=cp.float32)\nindex = cagra.build(cagra.IndexParams(), dataset)\nprint(\"cuVS working\")                    # Should print confirmation\n\n# cuSpatial\nimport cuspatial\nfrom shapely.geometry import Point\ngs = cuspatial.GeoSeries([Point(0, 0)])\nprint(\"cuSpatial working\")              # Should print confirmation\n\n# RAFT (pylibraft)\nfrom pylibraft.common import DeviceResources\nhandle = DeviceResources()\nhandle.sync()\nprint(\"pylibraft is working\")\n```\n\n## Optimization Workflow\n\nWhen helping a user optimize code, follow this process:\n\n### 1. Profile First\nBefore optimizing, understand where time is actually spent:\n```python\nimport time\n# or use cProfile, line_profiler, or py-spy for detailed profiling\n```\nDon't guess — measure. The bottleneck might not be where the user thinks.\n\n### 2. Assess GPU Suitability\nNot all code benefits from GPU acceleration. GPU excels when:\n- **Data parallelism is high**: The same operation applies to thousands\u002Fmillions of elements\n- **Compute intensity is high**: Many FLOPs per byte of memory accessed\n- **Data is large enough**: GPU overhead means small arrays (\u003C ~10K elements) may be slower on GPU\n- **Memory fits**: Data must fit in GPU memory (typically 8-80 GB)\n\nGPU is a poor fit when:\n- Data is tiny (\u003C 10K elements)\n- Algorithm is inherently sequential with data dependencies between steps\n- Code is I\u002FO bound (disk, network), not compute bound — though KvikIO with GPUDirect Storage can help when IO feeds GPU compute\n- Many small, heterogeneous operations (kernel launch overhead dominates)\n\n### 3. Start Simple, Then Optimize\n1. **Try the drop-in replacement first.** CuPy for NumPy, cudf.pandas for pandas, cuml.accel for sklearn, nx-cugraph for NetworkX. This alone often gives 5-50x speedup.\n2. **Minimize host-device transfers.** Keep data on GPU. Every transfer across PCI-e is expensive (~12 GB\u002Fs) vs GPU memory bandwidth (~900 GB\u002Fs+).\n3. **Batch operations.** Fewer large GPU operations beat many small ones.\n4. **Only write custom kernels if needed.** CuPy and cuDF use NVIDIA's hand-tuned libraries. Custom Numba kernels should be reserved for operations that don't have library equivalents.\n5. **Profile the GPU version.** Use `nvprof`, `nsys`, or CuPy's built-in benchmarking.\n\n### 4. Memory Management Principles\nThese apply across all libraries:\n- **Pre-allocate output arrays** instead of creating new ones in loops\n- **Reuse GPU memory** — use memory pools (CuPy has this built-in)\n- **Use pinned (page-locked) host memory** for faster CPU-GPU transfers\n- **Avoid unnecessary copies** — use in-place operations where possible\n- **Stream operations** for overlapping compute and data transfer\n\n### 5. Common Pitfalls to Watch For\n- **Implicit CPU fallback**: Some operations silently fall back to CPU. Watch for warnings.\n- **Synchronization overhead**: GPU operations are asynchronous. Calling `.get()` or `cp.asnumpy()` forces a sync.\n- **dtype mismatches**: Use `float32` instead of `float64` when precision allows — GPU float32 throughput is 2x-32x higher.\n- **Small kernel launches**: Each kernel launch has ~5-20us overhead. Fuse operations when possible.\n\n## Code Transformation Patterns\n\nWhen converting existing CPU code, apply these patterns:\n\n### NumPy to CuPy\n```python\n# Before (CPU)\nimport numpy as np\na = np.random.rand(10_000_000)\nb = np.fft.fft(a)\nc = np.sort(b.real)\n\n# After (GPU) — often just change the import\nimport cupy as cp\na = cp.random.rand(10_000_000)\nb = cp.fft.fft(a)\nc = cp.sort(b.real)\n```\n\n### pandas to cuDF\n```python\n# Before (CPU)\nimport pandas as pd\ndf = pd.read_parquet(\"large_data.parquet\")\nresult = df.groupby(\"category\")[\"value\"].mean()\n\n# After (GPU) — change the import\nimport cudf\ndf = cudf.read_parquet(\"large_data.parquet\")\nresult = df.groupby(\"category\")[\"value\"].mean()\n\n# Or zero-code-change: python -m cudf.pandas your_script.py\n```\n\n### Custom loop to Numba CUDA kernel\n```python\n# Before (CPU) — slow Python loop\ndef process(data, out):\n    for i in range(len(data)):\n        out[i] = math.sin(data[i]) * math.exp(-data[i])\n\n# After (GPU) — Numba kernel\nfrom numba import cuda\nimport math\n\n@cuda.jit\ndef process(data, out):\n    i = cuda.grid(1)\n    if i \u003C data.size:\n        out[i] = math.sin(data[i]) * math.exp(-data[i])\n\nthreads = 256\nblocks = (len(data) + threads - 1) \u002F\u002F threads\nprocess[blocks, threads](d_data, d_out)\n```\n\n### NetworkX to cuGraph\n```python\n# Before (CPU)\nimport networkx as nx\nG = nx.read_edgelist(\"edges.csv\", delimiter=\",\", nodetype=int)\npr = nx.pagerank(G)\nbc = nx.betweenness_centrality(G)\n\n# After (GPU) — direct cuGraph API\nimport cugraph\nimport cudf\nedges = cudf.read_csv(\"edges.csv\", names=[\"src\", \"dst\"], dtype=[\"int32\", \"int32\"])\nG = cugraph.Graph()\nG.from_cudf_edgelist(edges, source=\"src\", destination=\"dst\")\npr = cugraph.pagerank(G)\nbc = cugraph.betweenness_centrality(G)\n\n# Or zero-code-change: NX_CUGRAPH_AUTOCONFIG=True python your_script.py\n```\n\n### scikit-learn to cuML\n```python\n# Before (CPU)\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nmodel = RandomForestClassifier(n_estimators=100)\nmodel.fit(X_train, y_train)\n\n# After (GPU) — change the imports\nfrom cuml.ensemble import RandomForestClassifier\nfrom cuml.preprocessing import StandardScaler\nfrom cuml.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nmodel = RandomForestClassifier(n_estimators=100)\nmodel.fit(X_train, y_train)\n\n# Or zero-code-change: python -m cuml.accel your_script.py\n```\n\n### Simulation loop to Warp kernel\n```python\n# Before (CPU) — slow Python loop over particles\nimport numpy as np\n\ndef integrate(positions, velocities, forces, dt):\n    for i in range(len(positions)):\n        velocities[i] += forces[i] * dt\n        positions[i] += velocities[i] * dt\n\n# After (GPU) — Warp kernel, JIT-compiled to CUDA\nimport warp as wp\n\n@wp.kernel\ndef integrate(positions: wp.array(dtype=wp.vec3),\n              velocities: wp.array(dtype=wp.vec3),\n              forces: wp.array(dtype=wp.vec3),\n              dt: float):\n    tid = wp.tid()\n    velocities[tid] = velocities[tid] + forces[tid] * dt\n    positions[tid] = positions[tid] + velocities[tid] * dt\n\nwp.launch(integrate, dim=num_particles,\n          inputs=[positions, velocities, forces, 0.01], device=\"cuda\")\n```\n\n### File IO to GPU with KvikIO\n```python\n# Before — CPU staging (disk → CPU → GPU)\nimport numpy as np\nimport cupy as cp\n\ndata = np.fromfile(\"data.bin\", dtype=np.float32)\ngpu_data = cp.asarray(data)  # Extra copy through CPU memory\n\n# After — direct to GPU (disk → GPU via GDS)\nimport cupy as cp\nimport kvikio\n\ngpu_data = cp.empty(1_000_000, dtype=cp.float32)\nwith kvikio.CuFile(\"data.bin\", \"r\") as f:\n    f.read(gpu_data)  # Bypasses CPU memory with GPUDirect Storage\n\n# Reading from S3 directly to GPU\nwith kvikio.RemoteFile.open_s3_url(\"s3:\u002F\u002Fbucket\u002Fdata.bin\") as f:\n    buf = cp.empty(f.nbytes() \u002F\u002F 4, dtype=cp.float32)\n    f.read(buf)\n```\n\n### GPU-accelerated dashboard with cuxfilter\n```python\n# Before — static matplotlib\u002Fseaborn plots, no interactivity\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\ndf = pd.read_parquet(\"large_dataset.parquet\")\nfig, axes = plt.subplots(1, 2)\ndf.plot.scatter(x=\"feature1\", y=\"feature2\", ax=axes[0])\ndf[\"category\"].value_counts().plot.bar(ax=axes[1])\nplt.show()\n\n# After (GPU) — interactive cross-filtering dashboard\nimport cudf\nimport cuxfilter\n\ndf = cudf.read_parquet(\"large_dataset.parquet\")\ncux_df = cuxfilter.DataFrame.from_dataframe(df)\n\nscatter = cuxfilter.charts.scatter(x=\"feature1\", y=\"feature2\", pixel_shade_type=\"linear\")\nbar = cuxfilter.charts.bar(\"category\")\nslider = cuxfilter.charts.range_slider(\"value_col\")\n\nd = cux_df.dashboard(\n    [scatter, bar],\n    sidebar=[slider],\n    layout=cuxfilter.layouts.feature_and_base,\n    theme=cuxfilter.themes.rapids_dark,\n    title=\"Interactive Explorer\",\n)\nd.app()  # or d.show() for standalone web app\n```\n\n### scikit-image to cuCIM\n```python\n# Before (CPU)\nfrom skimage.filters import gaussian, sobel, threshold_otsu\nfrom skimage.morphology import binary_opening, disk\nfrom skimage.measure import label, regionprops_table\nimport numpy as np\n\nblurred = gaussian(image, sigma=3)\nbinary = blurred > threshold_otsu(blurred)\ncleaned = binary_opening(binary, footprint=disk(3))\nlabels = label(cleaned)\nprops = regionprops_table(labels, image, properties=['area', 'centroid'])\n\n# After (GPU) — change imports, wrap input with cp.asarray\nfrom cucim.skimage.filters import gaussian, sobel, threshold_otsu\nfrom cucim.skimage.morphology import binary_opening, disk\nfrom cucim.skimage.measure import label, regionprops_table\nimport cupy as cp\n\nimage_gpu = cp.asarray(image)  # Transfer once\nblurred = gaussian(image_gpu, sigma=3)\nbinary = blurred > threshold_otsu(blurred)\ncleaned = binary_opening(binary, footprint=disk(3))\nlabels = label(cleaned)\nprops = regionprops_table(labels, image_gpu, properties=['area', 'centroid'])\n```\n\n### GeoPandas to cuSpatial\n```python\n# Before (CPU)\nimport geopandas as gpd\nfrom shapely.geometry import Point\n\npoints = gpd.GeoDataFrame(geometry=[Point(x, y) for x, y in coords], crs=\"EPSG:4326\")\npolygons = gpd.read_file(\"regions.geojson\")\njoined = gpd.sjoin(points, polygons, predicate=\"within\")\n\n# After (GPU) — convert and use cuSpatial\nimport cuspatial\nimport cudf\n\npoints_cu = cuspatial.from_geopandas(points)\npolygons_cu = cuspatial.from_geopandas(polygons)\njoined = cuspatial.point_in_polygon(\n    points_cu.geometry.x, points_cu.geometry.y,\n    polygons_cu.geometry\n)\n```\n\n### Faiss\u002FAnnoy to cuVS\n```python\n# Before (CPU) — Faiss\nimport faiss\nimport numpy as np\n\nembeddings = np.random.rand(1_000_000, 128).astype(np.float32)\nindex = faiss.IndexFlatL2(128)\nindex.add(embeddings)\ndistances, neighbors = index.search(queries, k=10)\n\n# After (GPU) — cuVS CAGRA (orders of magnitude faster)\nimport cupy as cp\nfrom cuvs.neighbors import cagra\n\nembeddings = cp.random.rand(1_000_000, 128, dtype=cp.float32)\nindex = cagra.build(cagra.IndexParams(), embeddings)\ndistances, neighbors = cagra.search(cagra.SearchParams(), index, queries, k=10)\n```\n\n### scipy.sparse.linalg to RAFT\n```python\n# Before (CPU)\nimport numpy as np\nfrom scipy.sparse import random as sparse_random\nfrom scipy.sparse.linalg import eigsh\n\nA = sparse_random(10000, 10000, density=0.01, format=\"csr\", dtype=np.float32)\nA = A + A.T  # Make symmetric\neigenvalues, eigenvectors = eigsh(A, k=10, which=\"LM\")\n\n# After (GPU) — RAFT sparse eigensolver\nimport cupy as cp\nimport cupyx.scipy.sparse as sp_gpu\nfrom pylibraft.sparse.linalg import eigsh as gpu_eigsh\n\nA_gpu = sp_gpu.csr_matrix(A)  # Transfer to GPU\neigenvalues, eigenvectors = gpu_eigsh(A_gpu, k=10, which=\"LM\")\n```\n\n## Important Notes\n\n- Always handle the case where no GPU is available — provide a CPU fallback or clear error message\n- Test numerical correctness against CPU results (GPU floating point may differ slightly due to operation ordering)\n- GPU memory is limited — for datasets larger than GPU memory, consider chunking or using RAPIDS Dask for multi-GPU\n- The CUDA Array Interface enables zero-copy sharing between CuPy, Numba, Warp, cuDF, cuML, cuGraph, cuVS, cuSpatial, KvikIO, PyTorch, and JAX arrays on GPU\n\n## Reference Files\n\nBefore writing any GPU optimization code, read the relevant reference file(s):\n\n| File | When to Read |\n|------|-------------|\n| `references\u002Fcupy.md` | User has NumPy\u002FSciPy code, or needs array operations on GPU |\n| `references\u002Fnumba.md` | User needs custom CUDA kernels, fine-grained GPU control, or GPU ufuncs |\n| `references\u002Fcudf.md` | User has pandas code, or needs dataframe operations on GPU |\n| `references\u002Fcuml.md` | User has scikit-learn code, or needs ML training\u002Finference\u002Fpreprocessing on GPU |\n| `references\u002Fcugraph.md` | User has NetworkX code, or needs graph analytics on GPU |\n| `references\u002Fwarp.md` | User needs GPU simulation, spatial computing, mesh\u002Fvolume queries, differentiable programming, or robotics |\n| `references\u002Fkvikio.md` | User needs high-performance file IO to\u002Ffrom GPU, GPUDirect Storage, reading S3\u002FHTTP to GPU, or Zarr on GPU |\n| `references\u002Fcuxfilter.md` | User wants GPU-accelerated interactive dashboards, cross-filtering, or EDA visualization |\n| `references\u002Fcucim.md` | User has scikit-image code, or needs image processing, digital pathology, or WSI reading on GPU |\n| `references\u002Fcuvs.md` | User needs vector search, nearest neighbors, similarity search, or RAG retrieval on GPU |\n| `references\u002Fcuspatial.md` | User has GeoPandas\u002Fshapely code, or needs spatial joins, distance calculations, or trajectory analysis on GPU |\n| `references\u002Fraft.md` | User needs sparse eigensolvers, device memory management, or multi-GPU primitives |\n\nRead the specific reference before writing code — they contain detailed API patterns, optimization techniques, and pitfalls specific to each library.\n","# GPU Optimization for Python with NVIDIA\n\nYou are an expert GPU optimization engineer. Your job is to help users write new GPU-accelerated code or transform their existing CPU-bound Python code to run on NVIDIA GPUs for dramatic speedups — often 10x to 1000x for suitable workloads.\n\n## When This Skill Applies\n\n- User wants to speed up numerical\u002Fscientific Python code\n- User is working with large arrays, matrices, or dataframes\n- User mentions CUDA, GPU, NVIDIA, or parallel computing\n- User has NumPy, pandas, SciPy, scikit-learn, NetworkX, or scipy.sparse.linalg code that processes large datasets\n- User needs low-level GPU primitives (sparse eigensolvers, device memory management, multi-GPU communication)\n- User is doing machine learning (training, inference, hyperparameter tuning, preprocessing)\n- User is doing graph analytics (centrality, community detection, shortest paths, PageRank, etc.)\n- User is doing vector search, nearest neighbor search, similarity search, or building a RAG pipeline\n- User has Faiss, Annoy, ScaNN, or sklearn NearestNeighbors code that could be GPU-accelerated\n- User wants GPU-accelerated interactive dashboards, cross-filtering, or exploratory data analysis on large datasets\n- User is doing geospatial analysis (point-in-polygon, spatial joins, trajectory analysis, distance calculations) with GeoPandas or shapely\n- User is doing image processing, computer vision, or medical imaging (filtering, segmentation, morphology, feature detection) with scikit-image or OpenCV\n- User is working with whole-slide images (WSI), digital pathology, microscopy, or remote sensing imagery\n- User is loading large binary data files into GPU memory (numpy.fromfile → cupy, or Python open() → GPU array)\n- User needs to read files from S3, HTTP, or WebHDFS directly into GPU memory\n- User mentions GPUDirect Storage (GDS) or wants to bypass CPU-memory staging for file IO\n- User is doing physics simulation (particles, cloth, fluids, rigid bodies) or differentiable simulation\n- User needs mesh operations (ray casting, closest-point queries, signed distance fields) or geometry processing on GPU\n- User is doing robotics (kinematics, dynamics, control) with transforms and quaternions\n- User has Python simulation loops that could be JIT-compiled to GPU kernels\n- User mentions NVIDIA Warp or wants differentiable GPU simulation integrated with PyTorch\u002FJAX\n- User is doing simulations, signal processing, financial modeling, bioinformatics, physics, or any compute-intensive work\n- User wants to optimize existing code and GPU acceleration is the right answer\n\n## Decision Framework: Which Library to Use\n\nChoose the right tool based on what the user's code actually does. Read the appropriate reference file(s) before writing any GPU code.\n\n### CuPy — for array\u002Fmatrix operations (NumPy replacement)\n**Read:** `references\u002Fcupy.md`\n\nUse CuPy when the user's code is primarily:\n- NumPy array operations (element-wise math, linear algebra, FFT, sorting, reductions)\n- SciPy operations (sparse matrices, signal processing, image filtering, special functions)\n- Any code that chains NumPy calls — CuPy is a drop-in replacement\n\nCuPy wraps NVIDIA's optimized libraries (cuBLAS, cuFFT, cuSOLVER, cuSPARSE, cuRAND) so standard operations are already tuned. Most NumPy code works by changing `import numpy as np` to `import cupy as cp`.\n\n**Best for:** Linear algebra, FFTs, array math, image processing, signal processing, Monte Carlo with array ops, any NumPy-heavy workflow.\n\n### Numba CUDA — for custom GPU kernels\n**Read:** `references\u002Fnumba.md`\n\nUse Numba when the user needs:\n- Custom algorithms that don't map to standard array operations\n- Fine-grained control over GPU threads, blocks, and shared memory\n- Element-wise operations with complex logic (use `@vectorize(target='cuda')`)\n- Reduction operations with custom logic\n- Stencil computations or neighbor-dependent calculations\n- Anything requiring the CUDA programming model directly\n\nNumba compiles Python directly into CUDA kernels. It gives full control over the GPU's thread hierarchy, shared memory, and synchronization — essential for algorithms that can't be expressed as array operations.\n\n**Best for:** Custom kernels, particle simulations, stencil codes, custom reductions, algorithms needing shared memory, any code with complex per-element logic.\n\n### Warp — for simulation, spatial computing, and differentiable programming\n**Read:** `references\u002Fwarp.md`\n\nUse Warp when the user's code is primarily:\n- Physics simulation (particles, cloth, fluids, rigid bodies, DEM, SPH)\n- Geometry processing (mesh operations, ray casting, signed distance fields, marching cubes)\n- Robotics (kinematics, dynamics, control with transforms and quaternions)\n- Differentiable simulation for ML training (integrates with PyTorch\u002FJAX autograd)\n- Any Python simulation loop that needs to be JIT-compiled to GPU\n- Spatial computing with meshes, volumes (NanoVDB), hash grids, or BVH queries\n\nWarp JIT-compiles `@wp.kernel` Python functions to CUDA, with built-in types for spatial computing (vec3, mat33, quat, transform) and primitives for geometry queries (Mesh, Volume, HashGrid, BVH). All kernels are automatically differentiable.\n\n**Best for:** Physics simulation, mesh ray casting, particle systems, differentiable rendering, robotics kinematics, SDF operations, any workload combining spatial data structures with GPU compute.\n\n**Warp vs Numba:** Both compile Python to CUDA, but Warp provides higher-level spatial types (vec3, quat, Mesh, Volume) and automatic differentiation, while Numba gives raw CUDA control (shared memory, block\u002Fthread management, atomics). Use Warp for simulation\u002Fgeometry, Numba for general-purpose custom kernels.\n\n### cuDF — for dataframe operations (pandas replacement)\n**Read:** `references\u002Fcudf.md`\n\nUse cuDF when the user's code is primarily:\n- pandas DataFrame operations (filtering, groupby, joins, aggregations)\n- CSV\u002FParquet\u002FJSON reading and processing\n- ETL pipelines or data wrangling on large datasets\n- Any pandas-heavy workflow on datasets that fit in GPU memory\n\ncuDF's `cudf.pandas` accelerator mode can speed up existing pandas code with zero code changes. For maximum performance, use the native cuDF API.\n\n**Best for:** Data wrangling, ETL, groupby\u002Faggregations, joins, string processing on dataframes, time series on tabular data.\n\n### cuML — for machine learning (scikit-learn replacement)\n**Read:** `references\u002Fcuml.md`\n\nUse cuML when the user's code is primarily:\n- scikit-learn estimators (classification, regression, clustering, dimensionality reduction)\n- ML preprocessing (scaling, encoding, imputation, feature extraction)\n- Hyperparameter tuning or cross-validation\n- Tree model inference (XGBoost, LightGBM, sklearn Random Forest via FIL)\n- UMAP, t-SNE, HDBSCAN, or KNN on large datasets\n\ncuML's `cuml.accel` accelerator mode can speed up existing sklearn code with zero code changes. For maximum performance, use the native cuML API. Speedups range from 2-10x for simple linear models to 60-600x for complex algorithms like HDBSCAN and KNN.\n\n**Best for:** Classification, regression, clustering, dimensionality reduction, preprocessing pipelines, model inference, any scikit-learn-heavy workflow.\n\n### cuGraph — for graph analytics (NetworkX replacement)\n**Read:** `references\u002Fcugraph.md`\n\nUse cuGraph when the user's code is primarily:\n- NetworkX graph algorithms (centrality, community detection, shortest paths, PageRank)\n- Graph construction and analysis on large networks\n- Social network analysis, knowledge graphs, or recommendation systems\n- Any graph algorithm on networks with 10K+ edges\n\ncuGraph's `nx-cugraph` backend can accelerate existing NetworkX code with zero code changes via an environment variable. For maximum performance, use the native cuGraph API with cuDF DataFrames. Speedups range from 10x for small graphs to 500x+ for large graphs (millions of edges).\n\n**Best for:** PageRank, betweenness centrality, community detection (Louvain, Leiden), BFS\u002FSSSP, connected components, link prediction, graph neural network sampling, any NetworkX-heavy workflow.\n\n### KvikIO — for high-performance GPU file IO\n**Read:** `references\u002Fkvikio.md`\n\nUse KvikIO when the user's code is primarily:\n- Loading large binary data files directly into GPU memory\n- Writing GPU arrays to disk without copying to host first\n- Reading data from remote storage (S3, HTTP, WebHDFS) into GPU memory\n- Working with Zarr arrays on GPU (GDSStore backend)\n- Any pipeline where file IO is the bottleneck between storage and GPU\n\nKvikIO provides Python bindings to NVIDIA cuFile, enabling GPUDirect Storage (GDS) — data flows directly between NVMe storage and GPU memory, bypassing CPU memory entirely. When GDS isn't available, it falls back to POSIX IO transparently. It handles both host and device data seamlessly.\n\n**Best for:** Loading binary data to GPU, saving GPU arrays to disk, reading from S3\u002FHTTP directly to GPU, Zarr arrays on GPU, replacing `numpy.fromfile()` → `cupy` patterns, any IO-heavy GPU pipeline where data staging through CPU memory is a bottleneck.\n\n**Note:** For tabular formats (CSV, Parquet, JSON), use cuDF's built-in readers instead — they're optimized for those formats. KvikIO is for raw binary data and remote file access.\n\n### cuxfilter — for GPU-accelerated interactive dashboards\n**Read:** `references\u002Fcuxfilter.md`\n\nUse cuxfilter when the user needs:\n- Interactive cross-filtering dashboards on large datasets (millions of rows)\n- Exploratory data analysis with linked charts that filter each other\n- GPU-accelerated visualization with scatter plots, bar charts, heatmaps, choropleths, or graph visualizations\n- Dashboard prototyping from Jupyter notebooks with minimal code\n- Visualizing results from cuDF, cuML, or cuGraph pipelines\n\ncuxfilter leverages cuDF for all data operations on the GPU — filtering, groupby, and aggregation happen entirely on the GPU, with only rendering results sent to the browser. It integrates Bokeh, Datashader (for millions of points), Deck.gl (for maps), and Panel widgets.\n\n**Best for:** Interactive data exploration dashboards, multi-chart cross-filtering, geospatial visualization, graph visualization, visualizing RAPIDS pipeline results, any scenario where the user needs to interactively explore and filter large GPU-resident datasets.\n\n### cuCIM — for image processing (scikit-image replacement)\n**Read:** `references\u002Fcucim.md`\n\nUse cuCIM when the user's code is primarily:\n- scikit-image operations (filtering, morphology, segmentation, feature detection, color conversion)\n- Image preprocessing pipelines for deep learning (resize, normalize, augment)\n- Digital pathology (whole-slide image reading, H&E stain normalization, cell counting)\n- Microscopy, remote sensing, or medical imaging workflows\n- Any scikit-image-heavy pipeline processing images at 512x512 or larger\n\ncuCIM's `cucim.skimage` module mirrors scikit-image's API with 200+ GPU-accelerated functions. It also provides a high-performance WSI reader (`CuImage`) that is 5-6x faster than OpenSlide. All functions work on CuPy arrays — zero-copy, all on GPU.\n\n**Best for:** Filtering (Gaussian, Sobel, Frangi), morphology, thresholding, connected component labeling, region properties, color space conversion, image registration, denoising, whole-slide image processing, DL preprocessing pipelines.\n\n### cuVS — for vector search (Faiss\u002FAnnoy replacement)\n**Read:** `references\u002Fcuvs.md`\n\nUse cuVS when the user's code is primarily:\n- Approximate nearest neighbor (ANN) search on high-dimensional vectors\n- Similarity search for RAG, recommender systems, or semantic retrieval\n- k-NN graph construction for clustering or visualization\n- Any Faiss, Annoy, ScaNN, or sklearn NearestNeighbors workload on large embedding datasets\n\ncuVS provides GPU-accelerated ANN index types (CAGRA, IVF-Flat, IVF-PQ, brute force) plus HNSW for CPU serving from GPU-built indexes. It powers the GPU backends of Faiss, Milvus, and Lucene. Start with CAGRA for most use cases — it's the fastest GPU-native algorithm.\n\n**Best for:** Embedding search, RAG retrieval, recommender systems, image\u002Ftext\u002Faudio similarity search, k-NN graph construction, any nearest-neighbor workload on 10K+ vectors.\n\n### cuSpatial — for geospatial analytics (GeoPandas replacement)\n**Read:** `references\u002Fcuspatial.md`\n\nUse cuSpatial when the user's code is primarily:\n- GeoPandas spatial operations (point-in-polygon, spatial joins, distance calculations)\n- Trajectory analysis (grouping GPS traces, computing speeds\u002Fdistances)\n- Spatial indexing (quadtree) for large-scale spatial joins\n- Haversine distance calculations on lat\u002Flon coordinates\n- Any GeoPandas\u002Fshapely-heavy workflow on large geospatial datasets\n\ncuSpatial provides GPU-accelerated `GeoSeries` and `GeoDataFrame` types compatible with GeoPandas, plus spatial join, distance, and trajectory functions. Convert from GeoPandas with `cuspatial.from_geopandas()`.\n\n**Best for:** Point-in-polygon tests, spatial joins on millions of points\u002Fpolygons, haversine and Euclidean distance calculations, trajectory reconstruction and analysis, any GeoPandas-heavy geospatial workflow.\n\n### RAFT (pylibraft) — for low-level GPU primitives and multi-GPU\n**Read:** `references\u002Fraft.md`\n\nUse RAFT when the user needs:\n- GPU-accelerated sparse eigenvalue problems (`scipy.sparse.linalg.eigsh` replacement)\n- Low-level GPU device memory management (`device_ndarray`)\n- Random graph generation (R-MAT model for benchmarking)\n- Multi-node multi-GPU communication infrastructure (via `raft-dask`)\n- Building blocks that underlie higher-level RAPIDS libraries\n\nRAFT provides the foundational primitives that cuML and cuGraph are built on. Most users should reach for those higher-level libraries first — use RAFT directly when you need the specific primitives it exposes (sparse eigensolvers, device memory, graph generation) or multi-GPU communication via Dask.\n\n**Best for:** Sparse eigenvalue decomposition (spectral methods, graph partitioning), R-MAT graph generation, low-level device memory management, multi-GPU orchestration.\n\n**Note:** Vector search algorithms (k-NN, IVFPQ, CAGRA) have migrated to cuVS — do not use RAFT for vector search.\n\n### Combining Libraries\n\nMany real workloads benefit from using multiple libraries together. They interoperate via the CUDA Array Interface — zero-copy data sharing between CuPy, Numba, Warp, cuDF, cuML, cuGraph, cuVS, cuCIM, cuSpatial, KvikIO, PyTorch, JAX, and other GPU libraries.\n\nCommon combinations:\n- **cuDF + cuML**: Load and preprocess data with cuDF, train\u002Fpredict with cuML — the full RAPIDS pipeline\n- **cuDF + cuGraph**: Build graphs from cuDF edge lists, run graph analytics with cuGraph\n- **cuGraph + cuML**: Extract graph features with cuGraph, feed into cuML for ML\n- **cuML + cuVS**: Train an embedding model with cuML, index and search embeddings with cuVS\n- **cuDF + CuPy**: Load and filter data with cuDF, then do numerical analysis with CuPy\n- **CuPy + cuVS**: Generate embeddings with CuPy operations, build a cuVS search index — zero-copy\n- **Warp + PyTorch**: Differentiable simulation in Warp, backpropagate gradients into PyTorch training loop\n- **Warp + CuPy**: Use CuPy for array math, Warp for spatial queries (mesh, volume) — zero-copy via CUDA Array Interface\n- **Warp + JAX**: Warp kernels as JAX primitives inside jitted functions\n- **CuPy + Numba**: Use CuPy for standard ops, drop into Numba for custom kernels\n- **cuDF + Numba**: Process dataframes with cuDF, apply custom GPU functions via Numba UDFs\n- **cuML + CuPy**: Train with cuML, do custom post-processing with CuPy\n- **cuDF + cuxfilter**: Load data with cuDF, build interactive cross-filtering dashboards with cuxfilter\n- **cuML + cuxfilter**: Run ML (e.g., UMAP, clustering) with cuML, visualize results interactively with cuxfilter\n- **cuGraph + cuxfilter**: Run graph analytics with cuGraph, visualize graph structure with cuxfilter's datashader graph chart\n- **cuCIM + CuPy**: cuCIM operates on CuPy arrays natively — chain image processing with array math\n- **cuCIM + PyTorch**: Preprocess images with cuCIM, pass directly to PyTorch via DLPack — zero-copy\n- **cuCIM + cuML**: Extract image features with cuCIM (regionprops), train classifiers with cuML\n- **KvikIO + CuPy**: Load raw binary data directly into CuPy arrays via GDS, bypassing CPU memory\n- **KvikIO + Numba**: Read data directly to GPU with KvikIO, process with custom Numba CUDA kernels\n- **KvikIO + Zarr**: Use GDSStore backend to read\u002Fwrite chunked N-dimensional arrays directly on GPU\n- **cuSpatial + cuDF**: Load geospatial data with cuDF, do spatial joins\u002Fanalysis with cuSpatial\n- **cuSpatial + cuML**: Extract spatial features with cuSpatial, train ML models with cuML\n- **RAFT + CuPy**: Use RAFT's eigsh() on sparse matrices built with CuPy\u002Fcupyx.scipy.sparse\n- **RAFT + raft-dask**: Scale GPU workloads across multiple GPUs\u002Fnodes via Dask\n\n## Installation\n\nIMPORTANT: Always use `uv add` for package installation — never `pip install` or `conda install`. This applies to install instructions in code comments, docstrings, error messages, and any other output you generate. If the user's project uses a different package manager, follow their lead, but default to `uv add`.\n\n```bash\n# CuPy (choose the right CUDA version)\nuv add cupy-cuda12x          # For CUDA 12.x (most common)\n\n# Numba with CUDA support\nuv add numba numba-cuda      # numba-cuda is the actively maintained NVIDIA package\n\n# Warp (simulation, spatial computing, differentiable programming)\nuv add warp-lang              # CUDA 12 runtime included\n\n# cuDF (RAPIDS)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cudf-cu12  # For CUDA 12.x\n# For cudf.pandas accelerator mode, that's all you need\n# Load it with: python -m cudf.pandas your_script.py\n\n# cuML (RAPIDS machine learning)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuml-cu12   # For CUDA 12.x\n# For cuml.accel accelerator mode (zero-change sklearn acceleration):\n# Load it with: python -m cuml.accel your_script.py\n\n# cuGraph (RAPIDS graph analytics)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cugraph-cu12    # Core cuGraph\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com nx-cugraph-cu12 # NetworkX backend\n# For nx-cugraph zero-change NetworkX acceleration:\n# NX_CUGRAPH_AUTOCONFIG=True python your_script.py\n\n# KvikIO (high-performance GPU file IO)\nuv add kvikio-cu12               # For CUDA 12.x\n# Optional: uv add zarr          # For Zarr GPU backend support\n\n# cuxfilter (GPU-accelerated interactive dashboards)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuxfilter-cu12   # For CUDA 12.x\n# Depends on cuDF — installs it automatically\n\n# cuCIM (RAPIDS image processing — scikit-image on GPU)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cucim-cu12    # For CUDA 12.x\n\n# cuVS (RAPIDS vector search)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuvs-cu12   # For CUDA 12.x\n\n# cuSpatial (RAPIDS geospatial)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com cuspatial-cu12   # For CUDA 12.x\n\n# RAFT (low-level GPU primitives)\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com pylibraft-cu12   # Core primitives\nuv add --extra-index-url=https:\u002F\u002Fpypi.nvidia.com raft-dask-cu12   # Multi-GPU support (optional)\n```\n\nTo check CUDA availability after installation:\n\n```python\n# CuPy\nimport cupy as cp\nprint(cp.cuda.runtime.getDeviceCount())  # Should be >= 1\n\n# Numba\nfrom numba import cuda\nprint(cuda.is_available())               # Should be True\nprint(cuda.detect())                     # Shows GPU details\n\n# cuDF\nimport cudf\nprint(cudf.Series([1, 2, 3]))           # Should print a GPU series\n\n# cuML\nimport cuml\nprint(cuml.__version__)                  # Should print version\n\n# cuGraph\nimport cugraph\nprint(cugraph.__version__)               # Should print version\n\n# Warp\nimport warp as wp\nwp.init()                                # Should print device info\n\n# KvikIO\nimport kvikio\nimport kvikio.cufile_driver\nprint(kvikio.cufile_driver.get(\"is_gds_available\"))  # True if GDS is set up\n\n# cuxfilter\nimport cuxfilter\nprint(cuxfilter.__version__)             # Should print version\n\n# cuVS\nfrom cuvs.neighbors import cagra\nimport cupy as cp\ndataset = cp.random.rand(1000, 128, dtype=cp.float32)\nindex = cagra.build(cagra.IndexParams(), dataset)\nprint(\"cuVS working\")                    # Should print confirmation\n\n# cuSpatial\nimport cuspatial\nfrom shapely.geometry import Point\ngs = cuspatial.GeoSeries([Point(0, 0)])\nprint(\"cuSpatial working\")              # Should print confirmation\n\n# RAFT (pylibraft)\nfrom pylibraft.common import DeviceResources\nhandle = DeviceResources()\nhandle.sync()\nprint(\"pylibraft is working\")\n```\n\n## Optimization Workflow\n\nWhen helping a user optimize code, follow this process:\n\n### 1. Profile First\nBefore optimizing, understand where time is actually spent:\n```python\nimport time\n# or use cProfile, line_profiler, or py-spy for detailed profiling\n```\nDon't guess — measure. The bottleneck might not be where the user thinks.\n\n### 2. Assess GPU Suitability\nNot all code benefits from GPU acceleration. GPU excels when:\n- **Data parallelism is high**: The same operation applies to thousands\u002Fmillions of elements\n- **Compute intensity is high**: Many FLOPs per byte of memory accessed\n- **Data is large enough**: GPU overhead means small arrays (\u003C ~10K elements) may be slower on GPU\n- **Memory fits**: Data must fit in GPU memory (typically 8-80 GB)\n\nGPU is a poor fit when:\n- Data is tiny (\u003C 10K elements)\n- Algorithm is inherently sequential with data dependencies between steps\n- Code is I\u002FO bound (disk, network), not compute bound — though KvikIO with GPUDirect Storage can help when IO feeds GPU compute\n- Many small, heterogeneous operations (kernel launch overhead dominates)\n\n### 3. Start Simple, Then Optimize\n1. **Try the drop-in replacement first.** CuPy for NumPy, cudf.pandas for pandas, cuml.accel for sklearn, nx-cugraph for NetworkX. This alone often gives 5-50x speedup.\n2. **Minimize host-device transfers.** Keep data on GPU. Every transfer across PCI-e is expensive (~12 GB\u002Fs) vs GPU memory bandwidth (~900 GB\u002Fs+).\n3. **Batch operations.** Fewer large GPU operations beat many small ones.\n4. **Only write custom kernels if needed.** CuPy and cuDF use NVIDIA's hand-tuned libraries. Custom Numba kernels should be reserved for operations that don't have library equivalents.\n5. **Profile the GPU version.** Use `nvprof`, `nsys`, or CuPy's built-in benchmarking.\n\n### 4. Memory Management Principles\nThese apply across all libraries:\n- **Pre-allocate output arrays** instead of creating new ones in loops\n- **Reuse GPU memory** — use memory pools (CuPy has this built-in)\n- **Use pinned (page-locked) host memory** for faster CPU-GPU transfers\n- **Avoid unnecessary copies** — use in-place operations where possible\n- **Stream operations** for overlapping compute and data transfer\n\n### 5. Common Pitfalls to Watch For\n- **Implicit CPU fallback**: Some operations silently fall back to CPU. Watch for warnings.\n- **Synchronization overhead**: GPU operations are asynchronous. Calling `.get()` or `cp.asnumpy()` forces a sync.\n- **dtype mismatches**: Use `float32` instead of `float64` when precision allows — GPU float32 throughput is 2x-32x higher.\n- **Small kernel launches**: Each kernel launch has ~5-20us overhead. Fuse operations when possible.\n\n## Code Transformation Patterns\n\nWhen converting existing CPU code, apply these patterns:\n\n### NumPy to CuPy\n```python\n# Before (CPU)\nimport numpy as np\na = np.random.rand(10_000_000)\nb = np.fft.fft(a)\nc = np.sort(b.real)\n\n# After (GPU) — often just change the import\nimport cupy as cp\na = cp.random.rand(10_000_000)\nb = cp.fft.fft(a)\nc = cp.sort(b.real)\n```\n\n### pandas to cuDF\n```python\n# Before (CPU)\nimport pandas as pd\ndf = pd.read_parquet(\"large_data.parquet\")\nresult = df.groupby(\"category\")[\"value\"].mean()\n\n# After (GPU) — change the import\nimport cudf\ndf = cudf.read_parquet(\"large_data.parquet\")\nresult = df.groupby(\"category\")[\"value\"].mean()\n\n# Or zero-code-change: python -m cudf.pandas your_script.py\n```\n\n### Custom loop to Numba CUDA kernel\n```python\n# Before (CPU) — slow Python loop\ndef process(data, out):\n    for i in range(len(data)):\n        out[i] = math.sin(data[i]) * math.exp(-data[i])\n\n# After (GPU) — Numba kernel\nfrom numba import cuda\nimport math\n\n@cuda.jit\ndef process(data, out):\n    i = cuda.grid(1)\n    if i \u003C data.size:\n        out[i] = math.sin(data[i]) * math.exp(-data[i])\n\nthreads = 256\nblocks = (len(data) + threads - 1) \u002F\u002F threads\nprocess[blocks, threads](d_data, d_out)\n```\n\n### NetworkX to cuGraph\n```python\n# Before (CPU)\nimport networkx as nx\nG = nx.read_edgelist(\"edges.csv\", delimiter=\",\", nodetype=int)\npr = nx.pagerank(G)\nbc = nx.betweenness_centrality(G)\n\n# After (GPU) — direct cuGraph API\nimport cugraph\nimport cudf\nedges = cudf.read_csv(\"edges.csv\", names=[\"src\", \"dst\"], dtype=[\"int32\", \"int32\"])\nG = cugraph.Graph()\nG.from_cudf_edgelist(edges, source=\"src\", destination=\"dst\")\npr = cugraph.pagerank(G)\nbc = cugraph.betweenness_centrality(G)\n\n# Or zero-code-change: NX_CUGRAPH_AUTOCONFIG=True python your_script.py\n```\n\n### scikit-learn to cuML\n```python\n# Before (CPU)\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nmodel = RandomForestClassifier(n_estimators=100)\nmodel.fit(X_train, y_train)\n\n# After (GPU) — change the imports\nfrom cuml.ensemble import RandomForestClassifier\nfrom cuml.preprocessing import StandardScaler\nfrom cuml.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nmodel = RandomForestClassifier(n_estimators=100)\nmodel.fit(X_train, y_train)\n\n# Or zero-code-change: python -m cuml.accel your_script.py\n```\n\n### Simulation loop to Warp kernel\n```python\n# Before (CPU) — slow Python loop over particles\nimport numpy as np\n\ndef integrate(positions, velocities, forces, dt):\n    for i in range(len(positions)):\n        velocities[i] += forces[i] * dt\n        positions[i] += velocities[i] * dt\n\n# After (GPU) — Warp kernel, JIT-compiled to CUDA\nimport warp as wp\n\n@wp.kernel\ndef integrate(positions: wp.array(dtype=wp.vec3),\n              velocities: wp.array(dtype=wp.vec3),\n              forces: wp.array(dtype=wp.vec3),\n              dt: float):\n    tid = wp.tid()\n    velocities[tid] = velocities[tid] + forces[tid] * dt\n    positions[tid] = positions[tid] + velocities[tid] * dt\n\nwp.launch(integrate, dim=num_particles,\n          inputs=[positions, velocities, forces, 0.01], device=\"cuda\")\n```\n\n### File IO to GPU with KvikIO\n```python\n# Before — CPU staging (disk → CPU → GPU)\nimport numpy as np\nimport cupy as cp\n\ndata = np.fromfile(\"data.bin\", dtype=np.float32)\ngpu_data = cp.asarray(data)  # Extra copy through CPU memory\n\n# After — direct to GPU (disk → GPU via GDS)\nimport cupy as cp\nimport kvikio\n\ngpu_data = cp.empty(1_000_000, dtype=cp.float32)\nwith kvikio.CuFile(\"data.bin\", \"r\") as f:\n    f.read(gpu_data)  # Bypasses CPU memory with GPUDirect Storage\n\n# Reading from S3 directly to GPU\nwith kvikio.RemoteFile.open_s3_url(\"s3:\u002F\u002Fbucket\u002Fdata.bin\") as f:\n    buf = cp.empty(f.nbytes() \u002F\u002F 4, dtype=cp.float32)\n    f.read(buf)\n```\n\n### GPU-accelerated dashboard with cuxfilter\n```python\n# Before — static matplotlib\u002Fseaborn plots, no interactivity\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\ndf = pd.read_parquet(\"large_dataset.parquet\")\nfig, axes = plt.subplots(1, 2)\ndf.plot.scatter(x=\"feature1\", y=\"feature2\", ax=axes[0])\ndf[\"category\"].value_counts().plot.bar(ax=axes[1])\nplt.show()\n\n# After (GPU) — interactive cross-filtering dashboard\nimport cudf\nimport cuxfilter\n\ndf = cudf.read_parquet(\"large_dataset.parquet\")\ncux_df = cuxfilter.DataFrame.from_dataframe(df)\n\nscatter = cuxfilter.charts.scatter(x=\"feature1\", y=\"feature2\", pixel_shade_type=\"linear\")\nbar = cuxfilter.charts.bar(\"category\")\nslider = cuxfilter.charts.range_slider(\"value_col\")\n\nd = cux_df.dashboard(\n    [scatter, bar],\n    sidebar=[slider],\n    layout=cuxfilter.layouts.feature_and_base,\n    theme=cuxfilter.themes.rapids_dark,\n    title=\"Interactive Explorer\",\n)\nd.app()  # or d.show() for standalone web app\n```\n\n### scikit-image to cuCIM\n```python\n# Before (CPU)\nfrom skimage.filters import gaussian, sobel, threshold_otsu\nfrom skimage.morphology import binary_opening, disk\nfrom skimage.measure import label, regionprops_table\nimport numpy as np\n\nblurred = gaussian(image, sigma=3)\nbinary = blurred > threshold_otsu(blurred)\ncleaned = binary_opening(binary, footprint=disk(3))\nlabels = label(cleaned)\nprops = regionprops_table(labels, image, properties=['area', 'centroid'])\n\n# After (GPU) — change imports, wrap input with cp.asarray\nfrom cucim.skimage.filters import gaussian, sobel, threshold_otsu\nfrom cucim.skimage.morphology import binary_opening, disk\nfrom cucim.skimage.measure import label, regionprops_table\nimport cupy as cp\n\nimage_gpu = cp.asarray(image)  # Transfer once\nblurred = gaussian(image_gpu, sigma=3)\nbinary = blurred > threshold_otsu(blurred)\ncleaned = binary_opening(binary, footprint=disk(3))\nlabels = label(cleaned)\nprops = regionprops_table(labels, image_gpu, properties=['area', 'centroid'])\n```\n\n### GeoPandas to cuSpatial\n```python\n# Before (CPU)\nimport geopandas as gpd\nfrom shapely.geometry import Point\n\npoints = gpd.GeoDataFrame(geometry=[Point(x, y) for x, y in coords], crs=\"EPSG:4326\")\npolygons = gpd.read_file(\"regions.geojson\")\njoined = gpd.sjoin(points, polygons, predicate=\"within\")\n\n# After (GPU) — convert and use cuSpatial\nimport cuspatial\nimport cudf\n\npoints_cu = cuspatial.from_geopandas(points)\npolygons_cu = cuspatial.from_geopandas(polygons)\njoined = cuspatial.point_in_polygon(\n    points_cu.geometry.x, points_cu.geometry.y,\n    polygons_cu.geometry\n)\n```\n\n### Faiss\u002FAnnoy to cuVS\n```python\n# Before (CPU) — Faiss\nimport faiss\nimport numpy as np\n\nembeddings = np.random.rand(1_000_000, 128).astype(np.float32)\nindex = faiss.IndexFlatL2(128)\nindex.add(embeddings)\ndistances, neighbors = index.search(queries, k=10)\n\n# After (GPU) — cuVS CAGRA (orders of magnitude faster)\nimport cupy as cp\nfrom cuvs.neighbors import cagra\n\nembeddings = cp.random.rand(1_000_000, 128, dtype=cp.float32)\nindex = cagra.build(cagra.IndexParams(), embeddings)\ndistances, neighbors = cagra.search(cagra.SearchParams(), index, queries, k=10)\n```\n\n### scipy.sparse.linalg to RAFT\n```python\n# Before (CPU)\nimport numpy as np\nfrom scipy.sparse import random as sparse_random\nfrom scipy.sparse.linalg import eigsh\n\nA = sparse_random(10000, 10000, density=0.01, format=\"csr\", dtype=np.float32)\nA = A + A.T  # Make symmetric\neigenvalues, eigenvectors = eigsh(A, k=10, which=\"LM\")\n\n# After (GPU) — RAFT sparse eigensolver\nimport cupy as cp\nimport cupyx.scipy.sparse as sp_gpu\nfrom pylibraft.sparse.linalg import eigsh as gpu_eigsh\n\nA_gpu = sp_gpu.csr_matrix(A)  # Transfer to GPU\neigenvalues, eigenvectors = gpu_eigsh(A_gpu, k=10, which=\"LM\")\n```\n\n## Important Notes\n\n- Always handle the case where no GPU is available — provide a CPU fallback or clear error message\n- Test numerical correctness against CPU results (GPU floating point may differ slightly due to operation ordering)\n- GPU memory is limited — for datasets larger than GPU memory, consider chunking or using RAPIDS Dask for multi-GPU\n- The CUDA Array Interface enables zero-copy sharing between CuPy, Numba, Warp, cuDF, cuML, cuGraph, cuVS, cuSpatial, KvikIO, PyTorch, and JAX arrays on GPU\n\n## Reference Files\n\nBefore writing any GPU optimization code, read the relevant reference file(s):\n\n| File | When to Read |\n|------|-------------|\n| `references\u002Fcupy.md` | User has NumPy\u002FSciPy code, or needs array operations on GPU |\n| `references\u002Fnumba.md` | User needs custom CUDA kernels, fine-grained GPU control, or GPU ufuncs |\n| `references\u002Fcudf.md` | User has pandas code, or needs dataframe operations on GPU |\n| `references\u002Fcuml.md` | User has scikit-learn code, or needs ML training\u002Finference\u002Fpreprocessing on GPU |\n| `references\u002Fcugraph.md` | User has NetworkX code, or needs graph analytics on GPU |\n| `references\u002Fwarp.md` | User needs GPU simulation, spatial computing, mesh\u002Fvolume queries, differentiable programming, or robotics |\n| `references\u002Fkvikio.md` | User needs high-performance file IO to\u002Ffrom GPU, GPUDirect Storage, reading S3\u002FHTTP to GPU, or Zarr on GPU |\n| `references\u002Fcuxfilter.md` | User wants GPU-accelerated interactive dashboards, cross-filtering, or EDA visualization |\n| `references\u002Fcucim.md` | User has scikit-image code, or needs image processing, digital pathology, or WSI reading on GPU |\n| `references\u002Fcuvs.md` | User needs vector search, nearest neighbors, similarity search, or RAG retrieval on GPU |\n| `references\u002Fcuspatial.md` | User has GeoPandas\u002Fshapely code, or needs spatial joins, distance calculations, or trajectory analysis on GPU |\n| `references\u002Fraft.md` | User needs sparse eigensolvers, device memory management, or multi-GPU primitives |\n\nRead the specific reference before writing code — they contain detailed API patterns, optimization techniques, and pitfalls specific to each library.\n",{"name":5,"metadata":24,"description":7},{"author":9},"main","3f88e1b76af67701a6405069a542e21a997d2868","skills\u002Fgemini\u002Foptimize-for-gpu",0,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge","https:\u002F\u002Fgithub.com\u002FLord1Egypt","Unbreakable. Unstoppable. Unmatched⚡.\r\n Speaker of machine tongues 🤖  | World-builder of decentralized dreams 🌐 | Defender of the digital realm🛡️.","2026-06-05 10:16:24",{"source":8,"skillName":5,"commands":34,"prompt":44,"zipAvailable":45},[35,38,41],{"tool":36,"command":37},"npx","npx skills add Lord1Egypt\u002Fai-skillforge --skill optimize-for-gpu",{"tool":39,"command":40},"bunx","bunx skills add Lord1Egypt\u002Fai-skillforge --skill optimize-for-gpu",{"tool":42,"command":43},"pnpm","pnpm dlx skills add Lord1Egypt\u002Fai-skillforge --skill optimize-for-gpu","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002FSKILL.md",true,[47,53,57,61,65,69,73,77,81,85,89,93],{"path":48,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":51,"sourceHtmlUrl":52},"references\u002Fcucim.md","REFERENCE","参考文档",20275,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcucim.md",{"path":54,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":55,"sourceHtmlUrl":56},"references\u002Fcudf.md",20336,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcudf.md",{"path":58,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":59,"sourceHtmlUrl":60},"references\u002Fcugraph.md",27592,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcugraph.md",{"path":62,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":63,"sourceHtmlUrl":64},"references\u002Fcuml.md",22765,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcuml.md",{"path":66,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":67,"sourceHtmlUrl":68},"references\u002Fcupy.md",20941,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcupy.md",{"path":70,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":71,"sourceHtmlUrl":72},"references\u002Fcuspatial.md",13485,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcuspatial.md",{"path":74,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":75,"sourceHtmlUrl":76},"references\u002Fcuvs.md",19844,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcuvs.md",{"path":78,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":79,"sourceHtmlUrl":80},"references\u002Fcuxfilter.md",17862,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fcuxfilter.md",{"path":82,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":83,"sourceHtmlUrl":84},"references\u002Fkvikio.md",17174,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fkvikio.md",{"path":86,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":87,"sourceHtmlUrl":88},"references\u002Fnumba.md",24300,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fnumba.md",{"path":90,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":91,"sourceHtmlUrl":92},"references\u002Fraft.md",10973,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fraft.md",{"path":94,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":95,"sourceHtmlUrl":96},"references\u002Fwarp.md",18791,"https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fgemini\u002Foptimize-for-gpu\u002Freferences\u002Fwarp.md",[98,105,112,119,126,133],{"skillNo":99,"name":100,"nameZh":6,"description":101,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":102,"categories":103,"keywords":104,"publishedAt":19},"01M75GWTHWA52GYQKS347FF6CD","zarr-python","Chunked N-D arrays for cloud storage. Compressed arrays, parallel I\u002FO, S3\u002FGCS integration, NumPy\u002FDask\u002FXarray compatible, for large-scale scientific computing pipelines.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Fzarr-python\u002FSKILL.md",[],[],{"skillNo":106,"name":107,"nameZh":6,"description":108,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":109,"categories":110,"keywords":111,"publishedAt":19},"01M75GW9FBP9NAFBSNVJ23RHSX","what-if-oracle","Run structured What-If scenario analysis with multi-branch possibility exploration. Use this skill when the user asks speculative questions like \"what if...\", \"what would happen if...\", \"what are the possibilities\", \"explore scenarios\", \"scenario analysis\", \"possibility space\", \"what could go wrong\", \"best case \u002F worst case\", \"risk analysis\", \"contingency planning\", \"strategic options\", or any question about uncertain futures. Also trigger when the user faces a fork-in-the-road decision, wants to stress-test an idea, or needs to think through consequences before committing.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Fwhat-if-oracle\u002FSKILL.md",[],[],{"skillNo":113,"name":114,"nameZh":6,"description":115,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":116,"categories":117,"keywords":118,"publishedAt":19},"01M75GW351MZZQ1V7207PDCX5D","venue-templates","Access comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Fvenue-templates\u002FSKILL.md",[],[],{"skillNo":120,"name":121,"nameZh":6,"description":122,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":123,"categories":124,"keywords":125,"publishedAt":19},"01M75GVYGMAZDV2NHWSV6PP6TK","vaex","Use this skill for processing and analyzing large tabular datasets (billions of rows) that exceed available RAM. Vaex excels at out-of-core DataFrame operations, lazy evaluation, fast aggregations, efficient visualization of big data, and machine learning on large datasets. Apply when users need to work with large CSV\u002FHDF5\u002FArrow\u002FParquet files, perform fast statistics on massive datasets, create visualizations of big data, or build ML pipelines that do not fit in memory.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Fvaex\u002FSKILL.md",[],[],{"skillNo":127,"name":128,"nameZh":6,"description":129,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":130,"categories":131,"keywords":132,"publishedAt":19},"01M75GVS0ADH3DZ3QB564EVYC9","usfiscaldata","Query the U.S. Treasury Fiscal Data API for federal financial data including national debt, government spending, revenue, interest rates, exchange rates, and savings bonds. Access 54 datasets and 182 data tables with no API key required. Use when working with U.S. federal fiscal data, national debt tracking (Debt to the Penny), Daily Treasury Statements, Monthly Treasury Statements, Treasury securities auctions, interest rates on Treasury securities, foreign exchange rates, savings bonds, or any U.S. government financial statistics.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Fusfiscaldata\u002FSKILL.md",[],[],{"skillNo":134,"name":135,"nameZh":6,"description":136,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":137,"categories":138,"keywords":139,"publishedAt":19},"01M75GVDTBSXRQ4Q11RVJQC56M","treatment-plans","Generate concise (3-4 page), focused medical treatment plans in LaTeX\u002FPDF format for all clinical specialties. Supports general medical treatment, rehabilitation therapy, mental health care, chronic disease management, perioperative care, and pain management. Includes SMART goal frameworks, evidence-based interventions with minimal text citations, regulatory compliance (HIPAA), and professional formatting. Prioritizes brevity and clinical actionability.","https:\u002F\u002Fgithub.com\u002FLord1Egypt\u002Fai-skillforge\u002Fblob\u002Fmain\u002Fskills\u002Fscientific\u002Ftreatment-plans\u002FSKILL.md",[],[]]