Files
fn_registry/python/functions/ml/torch_device_select.md
egutierrez e3c8979e8d chore: auto-commit (95 archivos)
- cmd/fn/doctor.go
- cmd/fn/main.go
- cpp/apps/primitives_gallery/playground/tables/CMakeLists.txt
- cpp/apps/primitives_gallery/playground/tables/data_table.cpp
- cpp/apps/primitives_gallery/playground/tables/data_table_logic.cpp
- cpp/apps/primitives_gallery/playground/tables/data_table_logic.h
- cpp/apps/primitives_gallery/playground/tables/self_test.cpp
- cpp/apps/primitives_gallery/playground/tables/tql.cpp
- cpp/apps/primitives_gallery/playground/tables/viz.cpp
- cpp/apps/primitives_gallery/playground/tables/viz.h
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 00:50:34 +02:00

2.6 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
torch_device_select function py ml 1.0.0 impure def torch_device_select(preference: str = 'auto') -> str Selecciona el torch device optimo segun preferencia y disponibilidad real del hardware. 'auto' elige CUDA > MPS > CPU. Para preferencias explicitas valida disponibilidad y hace fallback a CPU con warnings.warn.
torch
pytorch
cuda
mps
device
hardware
probe
ml
apple-silicon
cuda_available_py_ml
false error_go_core
name desc
preference 'auto' detecta el mejor device disponible (CUDA > MPS > CPU). 'cuda' fuerza cuda:0. 'cuda:N' fuerza GPU N. 'mps' fuerza Apple Silicon. 'cpu' siempre retorna cpu.
string de device listo para torch: 'cuda:0', 'cuda:N', 'mps' o 'cpu'. Nunca lanza excepcion — fallback a 'cpu' con warning si el device solicitado no esta disponible. true
preference=cpu siempre retorna cpu
preference=auto sin cuda ni mps retorna cpu
preference=cuda sin cuda disponible retorna cpu con warning
preference=cuda:5 con solo 1 GPU retorna cpu con warning
preference desconocida retorna cpu con warning
python/functions/ml/tests/test_torch_device_select.py python/functions/ml/torch_device_select.py

Ejemplo

from ml.torch_device_select import torch_device_select

# Deteccion automatica (recomendado)
device = torch_device_select()         # "cuda:0" o "mps" o "cpu"

# Forzar CPU para reproducibilidad
device = torch_device_select("cpu")    # siempre "cpu"

# Preferencia explicita con fallback automatico
device = torch_device_select("cuda")   # "cuda:0" o "cpu" + warning

# Uso tipico al cargar un modelo
import torch
device_str = torch_device_select("auto")
model = MyModel().to(torch.device(device_str))

Comparacion con gliner_load_model

gliner_load_model usa internamente _resolve_device con la misma logica CUDA/CPU. torch_device_select extiende ese patron con:

  • Soporte MPS (Apple Silicon M1/M2/M3).
  • Seleccion de GPU especifica (cuda:N).
  • Fallback con warnings.warn en vez de silencio.

Notas

  • No levanta excepcion si torch no esta instalado: todos los helpers internos capturan ImportError y tratan el device como no disponible.
  • warnings.warn en vez de logging para no imponer dependencia de logging al caller.
  • MPS requiere torch >= 1.12 y macOS 12.3+. En sistemas Linux/Windows torch.backends.mps puede no existir — el helper lo maneja con hasattr.
  • impure: depende del estado del hardware y de las librerias instaladas.