Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device parameter for TransformerModel in models.py #49

Merged

Conversation

ScientistIzaak
Copy link
Contributor

@ScientistIzaak ScientistIzaak commented Jan 3, 2025

Added "device" parameter to TransformerModel in model.py for Improved GPU/CPU Compatibility

Description

This PR introduces the device parameter to the TransformersModel class, allowing users to explicitly specify the device ("cpu", "cuda", etc.) for model loading and inference. This change improves flexibility and control over model deployment environments, especially when running on systems with mixed hardware setups.

Changes Made

  1. Device Detection:

    • Added logic to automatically detect and set the device (cuda if available, otherwise cpu).
    • Modified the model instantiation and data tensor operations to respect the detected device.
    • Added a to(device) call to move the model and tensors to the appropriate device.
  2. Backward Compatibility:

    • No breaking changes introduced. If the device is not specified, the script defaults to the best available option.
  3. Enhance Logging:

    • Added logging to indicate when the default device is used.

Example Usage

Before

model = TransformersModel(model_id="my-model")

After

model = TransformersModel(model_id="my-model", device="cuda")

This ensures the model explicitly runs on the GPU.

Motivation

Explicit hardware selection is crucial in scenarios involving mixed hardware environments. Many users work in systems where both CPU and GPU resources are available, and having the ability to specify the device simplifies configuration and improves usability.

By adding the device parameter, users no longer need to modify internal logic or rely on automatic device detection, making the library more adaptable to diverse deployment environments.

Testing

  • Device Compatibility: Tested both GPU ("cuda") and CPU ("cpu").
  • Fallback Behavior: Verified correct fallback to default device when no device parameter is provided or when the specified device is unavailable.
  • Backward Compatibility: Ensured that existing code without a device parameter continues to work as expected.

Checklist

  • Added device parameter to TransformersModel.
  • Updated docstrings to include the new parameter description.
  • Verified backward compatibility with existing implementations.
  • Enhanced logging to clarify default behavior and errors.

Impact

This update is fully backward-compatible. Users who do not specify the device parameter will experience no change in behavior, as the default device ("cuda" if available, otherwise "cpu") is automatically selected.

@aymeric-roucher
Copy link
Collaborator

This is a really nice addition, thank you @ScientistIzaak ! Let's merge this.

@aymeric-roucher aymeric-roucher merged commit 19143af into huggingface:main Jan 6, 2025
1 check failed
@ScientistIzaak ScientistIzaak deleted the add-device-parameter branch January 6, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants