# robbyant/lingbot-map architecture

> Architecture diagram of the GitHub repository robbyant/lingbot-map, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/robbyant/lingbot-map
- Repository: https://github.com/robbyant/lingbot-map

## Overview

1. **What this repo is**
   LingBot-Map is a PyTorch-based package for **streaming 3D reconstruction** from image sequences or video. The architecture is centered around a feed-forward geometric context transformer that predicts camera/scene structure incrementally, rather than doing iterative optimization.

2. **Primary entry point and orchestration**
   `demo.py` is the main end-to-end runner. It loads a checkpoint, reads frames from `--image_folder` or `--video_path`, runs streaming inference, and sends reconstructed outputs to a browser-based `viser` viewer. The demo also controls runtime modes such as keyframe caching, windowed inference, offloading to CPU, and optional sky masking.

3. **Model subsystem**
   The `lingbot_map/models` package contains three major model variants:
   - `gct_base.py`: base geometric context transformer model
   - `gct_stream.py`: streaming inference model
   - `gct_stream_window.py`: long-sequence/windowed variant for very long videos  
   These are the architectural core of the repo and likely define the forward pass, state handling, and how predictions are produced frame-by-frame.

4. **Transformer and sequence modeling internals**
   `lingbot_map/layers` holds the low-level building blocks that implement the transformer stack:
   - attention, RoPE, flashinfer cache, and drop-path utilities
   - patch embedding and vision transformer backbone pieces
   - MLP/SwiGLU feed-forward blocks, layer scale, and residual blocks  
   This is the main compute path for feature extraction and long-range temporal memory. The presence of `flashinfer_cache.py` indicates support for **paged KV-cache attention** for efficient streaming.

5. **Geometric prediction heads**
   `lingbot_map/heads` contains task-specific output heads:
   - `camera_head.py` for camera/pose estimation or pose refinement
   - `dpt_head.py` for dense depth prediction
   - `head_act.py` and utilities for activation/output shaping  
   These heads convert transformer features into the 3D reconstruction outputs consumed by downstream visualization or export.

6. **Streaming memory and aggregation**
   `lingbot_map/aggregator` appears to manage how per-frame features and state are accumulated across time:
   - `base.py` likely defines a common aggregation interface
   - `stream.py` likely implements the streaming memory / cache policy  
   This is an important boundary: the model does not just process independent frames; it maintains temporal context, keyframes, and/or windowed state across long sequences.

7. **Geometry and pose utilities**
   `lingbot_map/utils` contains mathematical helpers that support the reconstruction pipeline:
   - `geometry.py` for projection/backprojection and coordinate transforms
   - `rotation.py` for pose/rotation conversions
   - `pose_enc.py` for pose encoding / pose-reference features
   - `load_fn.py` for checkpoint or asset loading  
   These utilities form the bridge between neural predictions and explicit 3D geometry.

8. **Inference modes and sequence scaling**
   The README describes three operational regimes that map to the model structure:
   - normal streaming over ordinary sequences
   - **keyframe interval** mode to reduce KV-cache growth
   - **windowed inference** for very long sequences  
   This suggests the repo is designed around maintaining bounded memory while still supporting long-horizon reconstruction.

9. **Visualization and export boundary**
   `lingbot_map/vis` is a separate output/inspection layer, not part of the model:
   - `viser_wrapper.py` connects to the browser viewer
   - `point_cloud_viewer.py` handles point-cloud rendering
   - `glb_export.py` exports reconstructed scenes to GLB
   - `sky_segmentation.py` and `utils.py` support visualization cleanup and tooling  
   This keeps the reconstruction core separate from UI and export concerns.

10. **Optional sky masking pipeline**
   For outdoor scenes, the repo can run ONNX-based sky segmentation before visualization. That path depends on `onnxruntime` and a downloaded `skyseg.onnx` model. It is a preprocessing/postprocessing concern, not part of the main transformer.

11. **Runtime and acceleration dependencies**
   The repo is built on **PyTorch** and optionally **FlashInfer**. FlashInfer is used for efficient streaming attention; if it is unavailable, the code can fall back to PyTorch SDPA. This is a meaningful architectural choice because it directly affects streaming throughput and sequence length scalability.

12. **Examples and packaged assets**
   `example/` contains ready-to-run scene folders used by the demo, and `assets/` holds README imagery. The repository is packaged via `pyproject.toml`, making the codebase both a library (`lingbot_map`) and a runnable demo application.

13. **High-level data flow**
   The practical pipeline is:
   input frames/video -> optional sky mask generation -> streaming model inference with temporal cache -> camera/depth/3D reconstruction outputs -> point cloud / GLB / browser visualization.  
   The main architectural boundaries are therefore between input preprocessing, the transformer-based reconstruction core, temporal aggregation/state, and visualization/export.

Main entry points: [`lingbot_map/models/gct_stream.py`](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_stream.py) (Streaming GCT), [`demo.py`](https://github.com/robbyant/lingbot-map/blob/main/demo.py) (Demo), [`lingbot_map/layers/vision_transformer.py`](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/vision_transformer.py) (Transformer), [`lingbot_map/heads/camera_head.py`](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/heads/camera_head.py) (Camera Head)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_demo["Demo<br/>runner<br/>[demo.py]"]
  node_load_fn["Load Helpers<br/>runtime<br/>[load_fn.py]"]
end

subgraph group_model["Model Core"]
  node_gct_stream["Streaming GCT<br/>model<br/>[gct_stream.py]"]
  node_gct_base["Base GCT<br/>model<br/>[gct_base.py]"]
  node_gct_window["Window GCT<br/>model"]
  node_aggregator_base["Aggregator API<br/>state<br/>[base.py]"]
  node_aggregator_stream["Stream Cache<br/>state<br/>[stream.py]"]
  node_transformer["Transformer<br/>backbone"]
  node_patch_embed["Patch Embed<br/>[patch_embed.py]"]
  node_attention["Attention<br/>[attention.py]"]
  node_cache["KV Cache<br/>stream-cache"]
  node_camera_head["Camera Head<br/>[camera_head.py]"]
  node_depth_head["Depth Head<br/>[dpt_head.py]"]
end

subgraph group_math["Geometry"]
  node_geometry["Geometry Utils<br/>math<br/>[geometry.py]"]
  node_rotation["Rotation Utils<br/>math<br/>[rotation.py]"]
  node_pose_enc["Pose Encoding<br/>math<br/>[pose_enc.py]"]
end

subgraph group_output["Output"]
  node_viser["Viser Viewer<br/>[viser_wrapper.py]"]
  node_pointcloud["Point Cloud<br/>viewer"]
  node_glb_export["GLB Export<br/>[glb_export.py]"]
  node_sky_mask["Sky Mask<br/>preprocess"]
end

node_demo -->|"loads"| node_load_fn
node_demo -.->|"optional"| node_sky_mask
node_demo -->|"runs"| node_gct_stream
node_demo -->|"visualizes"| node_viser
node_gct_stream -->|"extends"| node_gct_base
node_gct_window -->|"variant"| node_gct_stream
node_gct_stream -->|"uses"| node_aggregator_stream
node_aggregator_stream -->|"implements"| node_aggregator_base
node_gct_base -->|"contains"| node_transformer
node_transformer -->|"starts with"| node_patch_embed
node_transformer -->|"mixes"| node_attention
node_attention -->|"accelerates"| node_cache
node_gct_stream -->|"predicts"| node_camera_head
node_gct_stream -->|"predicts"| node_depth_head
node_gct_stream -->|"conditions on"| node_pose_enc
node_camera_head -->|"uses"| node_geometry
node_camera_head -->|"uses"| node_rotation
node_demo -->|"renders"| node_pointcloud
node_demo -->|"exports"| node_glb_export

click node_demo "https://github.com/robbyant/lingbot-map/blob/main/demo.py"
click node_gct_stream "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_stream.py"
click node_gct_base "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_base.py"
click node_gct_window "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_stream_window.py"
click node_aggregator_base "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/aggregator/base.py"
click node_aggregator_stream "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/aggregator/stream.py"
click node_transformer "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/vision_transformer.py"
click node_patch_embed "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/patch_embed.py"
click node_attention "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/attention.py"
click node_cache "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/flashinfer_cache.py"
click node_camera_head "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/heads/camera_head.py"
click node_depth_head "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/heads/dpt_head.py"
click node_geometry "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/geometry.py"
click node_rotation "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/rotation.py"
click node_pose_enc "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/pose_enc.py"
click node_load_fn "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/load_fn.py"
click node_viser "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/viser_wrapper.py"
click node_pointcloud "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/point_cloud_viewer.py"
click node_glb_export "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/glb_export.py"
click node_sky_mask "https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/sky_segmentation.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_demo,node_load_fn toneBlue
class node_gct_stream,node_gct_base,node_gct_window,node_aggregator_base,node_aggregator_stream,node_transformer,node_patch_embed,node_attention,node_cache,node_camera_head,node_depth_head toneAmber
class node_geometry,node_rotation,node_pose_enc toneMint
class node_viser,node_pointcloud,node_glb_export,node_sky_mask toneRose
```

## Components

### Runtime

End-to-end demo and inference orchestration

- [Demo](https://github.com/robbyant/lingbot-map/blob/main/demo.py): runner · Main end-to-end entry point for streaming reconstruction · `demo.py`
- [Load Helpers](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/load_fn.py): runtime · Checkpoint and asset loading helpers · `lingbot_map/utils/load_fn.py`

### Model Core

Streaming reconstruction model and temporal state

- [Streaming GCT](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_stream.py): model · Primary streaming reconstruction model · `lingbot_map/models/gct_stream.py`
- [Base GCT](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_base.py): model · Shared geometric context transformer core · `lingbot_map/models/gct_base.py`
- [Window GCT](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/models/gct_stream_window.py): model · Long-sequence windowed streaming variant · `lingbot_map/models/gct_stream_window.py`
- [Aggregator API](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/aggregator/base.py): state · Common interface for temporal aggregation · `lingbot_map/aggregator/base.py`
- [Stream Cache](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/aggregator/stream.py): state · Streaming memory and keyframe accumulation policy · `lingbot_map/aggregator/stream.py`
- [Transformer](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/vision_transformer.py): backbone · Vision transformer backbone and block stack · `lingbot_map/layers/vision_transformer.py`
- [Patch Embed](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/patch_embed.py): embed · Image tokenization and feature projection · `lingbot_map/layers/patch_embed.py`
- [Attention](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/attention.py): attention · Attention and rotary-positioned sequence mixing · `lingbot_map/layers/attention.py`
- [KV Cache](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/layers/flashinfer_cache.py): stream-cache · Paged cache support for efficient streaming attention · `lingbot_map/layers/flashinfer_cache.py`
- [Camera Head](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/heads/camera_head.py): head · Camera and pose output head · `lingbot_map/heads/camera_head.py`
- [Depth Head](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/heads/dpt_head.py): head · Dense depth prediction head · `lingbot_map/heads/dpt_head.py`

### Geometry

Geometric helpers and pose representations

- [Geometry Utils](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/geometry.py): math · Projection, backprojection, and coordinate transforms · `lingbot_map/utils/geometry.py`
- [Rotation Utils](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/rotation.py): math · Pose and rotation conversions · `lingbot_map/utils/rotation.py`
- [Pose Encoding](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/utils/pose_enc.py): math · Pose feature encoding for model inputs · `lingbot_map/utils/pose_enc.py`

### Output

Visualization, export, and optional masking

- [Viser Viewer](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/viser_wrapper.py): viewer · Browser-based live visualization bridge · `lingbot_map/vis/viser_wrapper.py`
- [Point Cloud](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/point_cloud_viewer.py): viewer · Point-cloud rendering and inspection · `lingbot_map/vis/point_cloud_viewer.py`
- [GLB Export](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/glb_export.py): export · Scene export to GLB · `lingbot_map/vis/glb_export.py`
- [Sky Mask](https://github.com/robbyant/lingbot-map/blob/main/lingbot_map/vis/sky_segmentation.py): preprocess · Optional sky segmentation for outdoor scenes · `lingbot_map/vis/sky_segmentation.py`

## Connections

- Demo → Load Helpers: loads · Loads checkpoints and runtime assets
- Demo → Sky Mask: optional · Applies sky masking when enabled
- Demo → Streaming GCT: runs · Executes streaming reconstruction inference
- Demo → Viser Viewer: visualizes · Streams reconstructed outputs to the browser viewer
- Streaming GCT → Base GCT: extends · Builds on the shared geometric transformer core
- Window GCT → Streaming GCT: variant · Adds long-sequence windowed streaming behavior
- Streaming GCT → Stream Cache: uses · Maintains temporal memory and keyframe state
- Stream Cache → Aggregator API: implements · Concrete streaming policy behind the aggregator API
- Base GCT → Transformer: contains · Main transformer backbone path
- Transformer → Patch Embed: starts with · Converts frames into patch tokens
- Transformer → Attention: mixes · Performs token and temporal attention
- Attention → KV Cache: accelerates · Supports streaming KV-cache attention
- Streaming GCT → Camera Head: predicts · Produces camera and pose estimates
- Streaming GCT → Depth Head: predicts · Produces dense depth outputs
- Streaming GCT → Pose Encoding: conditions on · Uses pose encoding as geometric context
- Camera Head → Geometry Utils: uses · Converts model outputs into 3D geometry
- Camera Head → Rotation Utils: uses · Handles pose and rotation parameterization
- Demo → Point Cloud: renders · Displays reconstructed point clouds
- Demo → GLB Export: exports · Writes scene exports for downstream use
