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

Environment:666ghj BettaFish PyTorch

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

Overview

PyTorch deep learning framework for sentiment analysis model training and inference, supporting CPU and CUDA GPU devices.

Description

This environment provides the PyTorch runtime used by BettaFish's sentiment analysis components. The framework powers the WeiboMultilingualSentimentAnalyzer (22-language sentiment classification), Qwen3 LoRA fine-tuning, BERT training, LSTM training, and the SentenceTransformer-based clustering model. PyTorch auto-detects CUDA availability and falls back to CPU (or Apple MPS) when no GPU is present. The main `requirements.txt` specifies the CPU version; GPU support requires a separate install command.

Usage

Use this environment for sentiment analysis model training, inference, and embedding-based clustering. It is required by WeiboMultilingualSentimentAnalyzer, Qwen3LoRAUniversal trainer, SentimentPredictor, and the Clustering_and_Sampling component.

System Requirements

Category Requirement Notes
OS Linux, macOS, Windows CUDA requires Linux/Windows
Hardware CUDA GPU optional CPU mode works but is slower for training
Disk 2-5GB For PyTorch + model weights

Dependencies

System Packages

  • CUDA Toolkit 11.8+ (optional, for GPU support)

Python Packages

Credentials

No credentials required.

Quick Install

# CPU version (default)
pip install torch>=2.0.0

# GPU version (CUDA 12.6)
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu126

Code Evidence

Device auto-detection from `InsightEngine/tools/sentiment_analyzer.py:142-156`:

def _select_device(self):
    if not TORCH_AVAILABLE:
        return None
    assert torch is not 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")

Qwen3 LoRA device and dtype 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
)

GPU install note from `requirements.txt:69`:

# NOTE:如果要安装GPU版本的torch,指令为pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu126

Common Errors

Error Message Cause Solution
`RuntimeError: CUDA out of memory` Insufficient VRAM Reduce batch size or use CPU mode
`UserWarning: Torch was not compiled with flash attention` No flash attention support Harmless warning; does not affect correctness
`KMP_DUPLICATE_LIB_OK` crash on macOS Duplicate MKL libraries Set `os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"`

Compatibility Notes

  • CPU mode: Default install. Works for all features but training is slow.
  • CUDA GPU: Requires separate install with `--index-url https://download.pytorch.org/whl/cu126`. Enables `float16` and `device_map="auto"`.
  • Apple MPS: Auto-detected via `torch.backends.mps.is_available()`. Supported for inference; training support is experimental.
  • KMP_DUPLICATE_LIB_OK: Set to `"TRUE"` in `bert_train.py` to resolve MKL conflicts on some systems.

Related Pages

Page Connections

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