Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:666ghj BettaFish Device Selection Priority

From Leeroopedia
Knowledge Sources
Domains Optimization, Machine_Learning, Deep_Learning
Last Updated 2026-10-08 21:30 GMT

Overview

Auto-select the best available compute device in priority order: CUDA GPU → Apple MPS → CPU, with matching dtype and placement strategy.

Description

BettaFish's ML components auto-detect the best available compute device at initialization. The priority order is: (1) NVIDIA CUDA GPU (fastest, uses `float16`), (2) Apple Silicon MPS (experimental, uses `float32`), (3) CPU fallback (always available, uses `float32`). The device selection affects model loading dtype, `device_map` strategy, and inference speed. This heuristic ensures the system works across diverse hardware without manual configuration.

Usage

Use this heuristic when deploying ML models across heterogeneous hardware. Apply when the deployment environment may or may not have GPU support.

The Insight (Rule of Thumb)

  • Action: Check `torch.cuda.is_available()` first, then MPS, then default to CPU. Set `torch_dtype` and `device_map` based on selection.
  • Value: CUDA → `float16` + `device_map="auto"`; CPU → `float32` + `device_map=None`.
  • Trade-off: GPU is 10-50x faster for training but not always available. MPS is ~2-5x faster than CPU but experimental.
  • Compatibility: Works with PyTorch >= 2.0. MPS requires Apple Silicon (M1+).

Reasoning

Hardcoding a device (e.g., always CUDA) causes failures on machines without GPUs. The auto-detection pattern ensures the system works everywhere. Using `float16` on GPU halves memory usage and doubles throughput; `float32` on CPU ensures numerical stability where FP16 is not hardware-accelerated. The `device_map="auto"` on GPU enables automatic model parallelism for models larger than single-GPU memory.

Code Evidence

Device priority selection from `InsightEngine/tools/sentiment_analyzer.py:142-156`:

def _select_device(self):
    if not TORCH_AVAILABLE:
        return None
    if torch.cuda.is_available():
        return torch.device("cuda")
    mps_backend = getattr(torch.backends, "mps", None)
    if (mps_backend
        and getattr(mps_backend, "is_available", lambda: False)()
        and getattr(mps_backend, "is_built", lambda: False)()):
        return torch.device("mps")
    return torch.device("cpu")

Dtype and device_map selection from `SentimentAnalysisModel/WeiboSentiment_SmallQwen/qwen3_lora_universal.py:43,57-58`:

self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# ...
self.base_model = AutoModelForCausalLM.from_pretrained(
    local_model_dir,
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    device_map="auto" if torch.cuda.is_available() else None
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment