Skip to content

Arseni1919/GPUPathfindingProject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

GPU-Accelerated Neural Network Pathfinding ๐Ÿš€

Proof of Concept: This repository demonstrates how to use GPU acceleration to speed up single-agent pathfinding using PyTorch. This is a research prototype and I'm open to discussions about extensions, optimizations, and applications.

A novel approach to pathfinding using GPU-accelerated neural networks with convolutional activation propagation and gradient-based path tracing.

Key Idea ๐Ÿ’ก

This implements maximally parallelized dynamic programming. Instead of sequentially exploring individual states (like A* or Dijkstra), the entire state space is updated simultaneously on the GPU. Each iteration propagates activation to all neighbors in parallel, making the algorithm blazingly fast.

The Architecture ๐Ÿ—๏ธ

The core is surprisingly simple - a single-layer neural network with a custom propagation kernel:

class SimpleNN(nn.Module):
    def __init__(self, in_channels, out_channels, map_mask, start_xy, goal_xy, map_name):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
        self.relu = nn.ReLU()
        self.map_mask = map_mask
        self.map_mask_4d = map_mask.unsqueeze(0).unsqueeze(0)
        self.start_xy = start_xy
        self.goal_xy = goal_xy
        self.map_name = map_name

    def forward(self, x):
        saved_tensors = []
        goal_y, goal_x = self.goal_xy
        for i in range(int(1e6)):
            x = self.relu(self.conv(x) * self.map_mask_4d)
            x = clip_preserve_grad(x, min_val=0.0, max_val=1.0)
            x.retain_grad()
            saved_tensors.append(x)
            if x[0, 0, goal_y, goal_x] > 0:
                return x, True, saved_tensors
        return x, False, saved_tensors

How it works:

  • Conv layer: Propagates activation to neighbors (4-connected kernel: up/down/left/right)
  • ReLU: Only positive values propagate forward
  • Mask multiplication: Zeros out obstacles
  • Custom gradient clipping: Preserves gradient flow for path tracing while preventing numerical explosion
  • Iteration: Continues until goal receives positive activation

Results ๐Ÿ“Š

  • โฑ๏ธ Time Complexity: O(length of optimal path) - search time scales with solution length, not map size
  • ๐ŸŽฏ All Optimal Paths: Exposes all equally optimal paths simultaneously
  • ๐ŸŽจ Simple Implementation: ~50 lines of core logic, no complex data structures
  • ๐Ÿ”ง No Heuristics Required: Works out-of-the-box on unseen grids of any type
  • ๐Ÿ—บ๏ธ Universal: Works with any map representable as a tensor
  • โšก Blazingly Fast: Massive parallelization on GPU delivers exceptional performance

Example Results ๐Ÿ“ธ

Visual comparison of expanded nodes (exploration) vs. optimal paths (solution) on different map types:

Map Name Expanded Nodes Optimal Paths Avg Runtime (100 runs)
maze512-2-9
512ร—512 maze
maze512-2-9 expanded maze512-2-9 optimal 2.0121s ยฑ 1.0460s
maze512-8-2
512ร—512 maze
maze512-8-2 expanded maze512-8-2 optimal 1.6700s ยฑ 1.1873s
warehouse-20-40-10-2-2
Warehouse scenario
warehouse expanded warehouse optimal 0.0660s ยฑ 0.0353s
  • Expanded Nodes: Shows all cells explored during forward pass when goal is reached
  • Optimal Paths: Shows the traced path(s) extracted via gradient accumulation
  • Avg Runtime: Average execution time over 100 runs (forward + backward pass only, excludes initialization and I/O)

Benchmark Details: Measured on MPS (Apple Silicon) with 100 runs per map. Times include forward pass (activation propagation) and backward pass (gradient-based path extraction), excluding map loading and visualization. Note: These benchmarks were run on Mac with MPS backend. CUDA-enabled GPUs typically provide significantly better performance. See benchmarks.txt for detailed statistics.

Core Innovation (Implementation Details) ๐Ÿ’ก

This project implements pathfinding as a neural network forward-backward pass:

1. Forward Pass (Activation Propagation):

  • Place activation at start position
  • Iteratively apply convolution (spreads to neighbors) + ReLU
  • Multiply by map mask (blocks obstacles)
  • Custom gradient-preserving clipping prevents numerical explosion
  • Continue until activation reaches goal

2. Backward Pass (Path Extraction):

  • Compute gradients from goal back to start
  • Gradient flow reveals the path taken by activation
  • Accumulate positive gradients to extract final path

3. GPU Acceleration:

  • All operations run on GPU (CUDA/MPS/CPU fallback)
  • Handles large 3D maps efficiently (tested on 896ร—390ร—255 voxels)

Features โœจ

  • ๐Ÿ—บ๏ธ 2D pathfinding on grid maps (.map format)
  • ๐ŸงŠ 3D pathfinding on voxel maps (.3dmap format)
  • ๐Ÿ“Š Interactive 3D visualizations using Plotly
  • โšก Automatic device selection (CUDA > MPS > CPU)
  • ๐ŸŽฒ Random map selection for testing
  • โš™๏ธ Configurable memory/quality tradeoffs

Installation ๐Ÿ“ฆ

Prerequisites

  • Python 3.12+
  • uv package manager
  • GPU with CUDA or Apple Silicon (MPS) for best performance

Setup with uv (Recommended)

# Clone the repository
git clone <repository-url>
cd GPUPathfindingProject

# Install dependencies using uv
uv sync

# Activate the environment (optional, uv run handles this automatically)
source .venv/bin/activate

Alternative: Setup with pip

pip install -r requirements.txt

Dependencies

  • torch >= 2.11.0 (with CUDA/MPS support)
  • matplotlib >= 3.10.8
  • plotly >= 6.6.0
  • psutil >= 7.2.2

Usage ๐ŸŽฎ

2D Pathfinding

Run on a specific map:

uv run search2D_nn.py --map maze-128-128-1.map

Random map selection:

uv run search2D_nn.py

With custom seed and device:

uv run search2D_nn.py --map warehouse-20-40-10-2-2.map --seed 8535 --device cuda

3D Pathfinding

Run on a specific map:

uv run search3D_nn.py --map A1.3dmap

With custom settings:

uv run search3D_nn.py --map A1.3dmap --seed 8535 --save-freq 50 --device cuda

Note: If you've activated the virtual environment with source .venv/bin/activate, you can use python instead of uv run.

Common Parameters:

  • --map: Map file name (from maps/2d/ or maps/3d/)
  • --seed: Random seed for reproducibility
  • --device: Force device (auto/cuda/mps/cpu) - available for both 2D and 3D

3D-Specific Parameters:

  • --save-freq: Save every Nth iteration (default: 100, lower = more memory, better path visualization)

Project Structure ๐Ÿ“

GPUPathfindingProject/
โ”œโ”€โ”€ maps/
โ”‚   โ”œโ”€โ”€ 2d/          # 2D grid maps (.map format)
โ”‚   โ””โ”€โ”€ 3d/          # 3D voxel maps (.3dmap format)
โ”œโ”€โ”€ outputs/         # Generated visualizations (HTML/PNG)
โ”œโ”€โ”€ search2D_nn.py   # 2D pathfinding script
โ”œโ”€โ”€ search3D_nn.py   # 3D pathfinding script
โ”œโ”€โ”€ utils.py         # 2D utilities (map loading, plotting)
โ”œโ”€โ”€ utils3D.py       # 3D utilities (map loading, 3D visualization)
โ”œโ”€โ”€ pyproject.toml   # uv project configuration
โ”œโ”€โ”€ uv.lock          # uv lock file
โ”œโ”€โ”€ requirements.txt # Pip-compatible requirements
โ””โ”€โ”€ README.md        # This file

Map Formats ๐Ÿ“‹

2D Maps (.map format)

type octile
height 32
width 32
map
@@@@@@@@...
...
  • . = walkable
  • @, T, etc. = obstacles

3D Maps (.3dmap format)

voxel <width> <height> <depth>
<x> <y> <z>
...
  • Lists occupied (obstacle) voxels
  • All other voxels are walkable

How It Works ๐Ÿ”ฌ

Custom Gradient Clipping

Standard torch.clamp() kills gradients at boundaries. We use a custom autograd function that clips forward values while passing gradients unchanged:

class ClipWithFullGradients(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input, min_val, max_val):
        return torch.clamp(input, min=min_val, max=max_val)

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output, None, None  # Full gradient passthrough

Connectivity Kernels

  • 2D: 4-connected (up, down, left, right)
  • 3D: 18-connected (6 faces + 12 edges, excludes 8 corners)

Memory Optimization (3D)

For large 3D maps, saving every iteration is memory-prohibitive. We use checkpoint-based gradient accumulation:

  • Save every Nth iteration (configurable via --save-freq)
  • Detach intermediate tensors to break gradient chain
  • Trade path visualization quality for memory efficiency

Output Formats ๐ŸŽจ

  • 2D: PNG images with matplotlib visualization
    • {map_name}_expanded_nodes.png - All explored cells
    • {map_name}_optimal_paths.png - Traced optimal path(s)
  • 3D: Interactive HTML with Plotly (rotate, zoom, inspect voxels)
    • {map_name}_expanded_nodes.html - 3D exploration visualization
    • {map_name}_optimal_paths.html - 3D path visualization
    • {map_name}_start_goal_preview.html - Start/goal preview before pathfinding
  • All outputs saved to outputs/ directory

Performance Tips ๐ŸŽ๏ธ

  1. Use GPU: CUDA or MPS dramatically speeds up computation
  2. Adjust save frequency: Higher --save-freq = less memory (100-1000 for 3D)
  3. Map size: Larger maps take longer but are fully supported
  4. Device selection: Let auto choose, or force with --device

Technical Details ๐Ÿ”ง

Algorithm Overview

  1. Initialize activation tensor with 1.0 at start position
  2. Apply Conv2d/Conv3d with neighbor connectivity kernel
  3. Apply ReLU activation
  4. Multiply by map mask to zero out obstacles
  5. Apply custom gradient-preserving clip to [0, 1]
  6. Check if goal position has positive activation
  7. If goal reached: backward pass from goal
  8. Accumulate positive gradients across saved checkpoints
  9. Visualize accumulated gradients as the discovered path

Why This Approach?

  • Differentiable: The entire pathfinding process is differentiable
  • Parallel: GPU acceleration for massive parallelism
  • Flexible: Easy to modify connectivity patterns or cost functions
  • Visualizable: Gradient flow shows exactly how activation propagated

Limitations & Future Work ๐Ÿ”ฎ

Multi-Agent Pathfinding (MAPF)

  • โœ… Trivially extends to Prioritized Planning (PrP): Each agent plans sequentially in a predefined order. The algorithm naturally handles this by planning one agent at a time.
  • โŒ Non-trivial for other MAPF algorithms: Adapting to sophisticated algorithms like CBS (Conflict-Based Search) or ECBS requires significant modifications.

3D Case Challenges

The repository includes a 3D implementation (search3D_nn.py), but faces memory constraints:

  • Gradient explosion issue: The width ร— height ร— depth ร— time dimensions exceed GPU memory during backward pass
  • This is a scale/optimization problem, not a theoretical limitation
  • โœ… Forward pass works: Can find the optimal solution length
  • โŒ Backward pass (path extraction): Requires memory optimization techniques (checkpointing, gradient accumulation)

Discussion Welcome

This is a proof-of-concept repository. I'm open to discussions about:

  • Performance optimizations
  • Memory-efficient 3D path extraction
  • Extensions to MAPF algorithms
  • Applications in robotics, game AI, or other domains

Feel free to open issues or reach out!

Contributing ๐Ÿค

Contributions welcome! Areas for improvement:

  • More connectivity patterns (8-connected 2D, 26-connected 3D)
  • Memory-efficient backward pass for 3D
  • Optimal subpath guarantees
  • Benchmarking against A*/Dijkstra

Acknowledgments ๐Ÿ™

The 2D and 3D map files used in this repository are taken from the Moving AI Lab Benchmarks, a comprehensive collection of pathfinding benchmark scenarios used widely in MAPF (Multi-Agent Pathfinding) research.

License ๐Ÿ“„

This project is licensed under the MIT License - see the LICENSE file for details.

Citation ๐Ÿ“š

If you use this code in research, please cite:

@software{pertzovsky2026gpu,
  author = {Pertzovsky, Arseniy},
  title = {GPU-Accelerated Neural Network Pathfinding},
  year = {2026},
  publisher = {GitHub},
  url = {https://github.com/Arseni1919/GPUPathfindingProject}
}

Or in plain text:

Pertzovsky, A. (2026). GPU-Accelerated Neural Network Pathfinding. GitHub. https://github.com/Arseni1919/GPUPathfindingProject


Built with PyTorch ๐Ÿ”ฅ | Visualized with Plotly ๐Ÿ“Š | Accelerated by GPUs ๐Ÿš€

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors