Backend API
Backend
This is the base class for all backends.
torch_tweak.torch.backend.Backend
Bases: ABC
Backend interface for tuning a module.
Initialize the backend.
Source code in torch_tweak/torch/backend/backend.py
activate
Activates backend.
After activating, the backend should be ready to do inference.
Source code in torch_tweak/torch/backend/backend.py
build
Build the model with the given arguments.
Building a backend should be idempotent i.e. do not cause side effects. A model is not necessarily pure functional and can have an internal state (like kv cache for LLMs). That is why build can call a sample of inputs at most once so that subsequent calls have exact same state as the first call for the given sample.
After building, the backend should be activated.
Source code in torch_tweak/torch/backend/backend.py
deactivate
Deactivates backend.
After deactivating, the backend cannot be used to do inference.
Source code in torch_tweak/torch/backend/backend.py
deploy
Deploys the backend.
After deploying, the backend is ready to do inference. Backend cannot be deactivated anymore.
Parameters:
-
device(device | None) –The device to deploy the backend on.
Source code in torch_tweak/torch/backend/backend.py
describe
abstractmethod
from_dict
abstractmethod
classmethod
infer
Run inference with the given arguments.
Parameters:
-
args(Any, default:()) –Variable length argument list.
-
kwargs(Any, default:{}) –Arbitrary keyword arguments.
Returns:
-
Any(Any) –The result of the inference.
Source code in torch_tweak/torch/backend/backend.py
key
abstractmethod
to_dict
abstractmethod
Returns the state_dict of the backend.
Note: if there any binary artifacts (files) which should be stored by a backend, they must be passed as Python Path object. Such objects will be bundled with a checkpoint.
Source code in torch_tweak/torch/backend/backend.py
BackendConfig
torch_tweak.torch.backend.backend.BackendConfig
dataclass
Configuration for a backend.
describe
Describe the backend configuration. Display only changed fields.
key
Returns the keys of the backend configuration.
Source code in torch_tweak/torch/backend/backend.py
to_dict
to_json
Saves the backend configuration to a file.