mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-12-29 07:59:27 +00:00
* feat: Framework-agnostic device management * Add release note * Linting * Fix test * Add `first_device` property, expand release notes, validate `ComponentDevice` state
32 lines
1.4 KiB
YAML
32 lines
1.4 KiB
YAML
---
|
|
upgrade:
|
|
- |
|
|
Implement framework-agnostic device representations. The main impetus behind this change is to move away from stringified representations
|
|
of devices that are not portable between different frameworks. It also enables support for multi-device inference in a generic manner.
|
|
|
|
Going forward, components can expose a single, optional device parameter in their constructor (`Optional[ComponentDevice]`):
|
|
```python
|
|
import haystack.utils import ComponentDevice, Device, DeviceMap
|
|
class MyComponent(Component):
|
|
def __init__(self, device: Optional[ComponentDevice] = None):
|
|
# If device is None, automatically select a device.
|
|
self.device = ComponentDevice.resolve_device(device)
|
|
|
|
def warm_up(self):
|
|
# Call the framework-specific conversion method.
|
|
self.model = AutoModel.from_pretrained("deepset/bert-base-cased-squad2", device=self.device.to_hf())
|
|
|
|
# Automatically selects a device.
|
|
c = MyComponent(device=None)
|
|
# Uses the first GPU available.
|
|
c = MyComponent(device=ComponentDevice.from_str("cuda:0"))
|
|
# Uses the CPU.
|
|
c = MyComponent(device=ComponentDevice.from_single(Device.cpu()))
|
|
# Allow the component to use multiple devices using a device map.
|
|
c = MyComponent(device=ComponentDevice.from_multiple(DeviceMap({
|
|
"layer1": Device.cpu(),
|
|
"layer2": Device.gpu(1),
|
|
"layer3": Device.disk()
|
|
})))
|
|
```
|