pycsamt.backends#
Runtime backend detection, selection, compatibility checks, and backend metadata helpers.
Backend abstraction and lazy discovery for deep-learning frameworks.
pycsamt’s AI/ML modules support both PyTorch and TensorFlow. Neither framework is a required dependency of the package — they are loaded lazily only when the AI module is actually used.
Configuration#
The active backend is resolved using this priority chain:
Explicit call to
set_backend()in the current Python session.PYCSAMT_AI_BACKENDenvironment variable (torch/tensorflow/auto).~/.pycsamt/config.jsonkey"ai_backend".Auto-detection: first available framework in
['torch', 'tensorflow'].
Quick start#
>>> import pycsamt
>>> pycsamt.list_backends()
{'torch': True, 'tensorflow': False}
>>> pycsamt.set_backend('torch') # or 'tensorflow' / 'auto'
>>> pycsamt.get_backend()
'torch'
Environment variable#
Set before importing pycsamt to override the default:
PYCSAMT_AI_BACKEND=tensorflow python my_script.py
- pycsamt.backends.get_backend()[source]#
Return the name of the currently active backend.
- Returns:
name –
'torch','tensorflow', or'none'when no DL framework is installed.- Return type:
Examples
>>> from pycsamt.backends import get_backend >>> get_backend() 'torch'
- pycsamt.backends.set_backend(name, *, persist=False)[source]#
Set the active AI backend.
- Parameters:
name ({'torch', 'tensorflow', 'auto'}) –
'auto'triggers detection and selects the first available framework.persist (bool, default False) – If
True, save the choice to~/.pycsamt/config.jsonso it survives across Python sessions.
- Raises:
ValueError – If name is not
'torch','tensorflow', or'auto'.ImportError – If the requested backend is not installed.
- Return type:
None
Examples
>>> from pycsamt.backends import set_backend >>> set_backend('torch') >>> set_backend('tensorflow', persist=True) # saves to config file
- pycsamt.backends.auto_detect()[source]#
Detect the best available backend and activate it.
- Returns:
name – The name of the detected and activated backend.
- Return type:
- Raises:
RuntimeError – If no compatible framework is installed.
Examples
>>> from pycsamt.backends import auto_detect >>> backend = auto_detect() >>> print(backend) torch
- pycsamt.backends.list_backends()[source]#
Return availability status for all known backends.
- Returns:
availability –
{'torch': bool, 'tensorflow': bool}- Return type:
Examples
>>> from pycsamt.backends import list_backends >>> list_backends() {'torch': True, 'tensorflow': False}
- pycsamt.backends.get_backend_instance()[source]#
Return the concrete
NeuralBackendinstance for the active backend, orNonewhen no DL framework is installed.- Returns:
backend –
TorchBackend,TensorFlowBackend, orNonewhen no framework is available.- Return type:
NeuralBackend or None
- pycsamt.backends.detect_available_backends()#
Return a list of available deep-learning backends.
- Returns:
backends – Ordered list of backend names that are currently importable. Possible values:
'torch','tensorflow'. Empty list if neither framework is installed.- Return type:
Examples
>>> from pycsamt.backends._detect import detect_available_backends >>> available = detect_available_backends() >>> print(available) # e.g. ['torch'] or ['torch', 'tensorflow']
- pycsamt.backends.probe_backend(name)#
Return
Trueif name is importable in the current environment.No import side-effects — uses
importlib.util.find_spec()only.
- pycsamt.backends.get_backend_versions()#
Return version strings for installed backends.
Only actually imports the framework if it was already detected; avoids triggering slow framework initialisation.
- Returns:
versions –
{'torch': '2.x.x', 'tensorflow': None}—Nonemeans not installed.- Return type: