Skip to content

Lazily import torchao Int4WeightOnlyConfig to avoid side effects#40123

Open
n0kovo wants to merge 2 commits intohuggingface:mainfrom
n0kovo:lazy-import-torchao-int4-config
Open

Lazily import torchao Int4WeightOnlyConfig to avoid side effects#40123
n0kovo wants to merge 2 commits intohuggingface:mainfrom
n0kovo:lazy-import-torchao-int4-config

Conversation

@n0kovo
Copy link
Copy Markdown

@n0kovo n0kovo commented Aug 13, 2025

Previously, modeling_utils imported torchao.quantization.Int4WeightOnlyConfig at module load time if torchao was available.

On CPU-only and Mac/Windows systems, this triggered a torch.distributed.elastic redirect warning and a Triton ImportError print on certain versions (pytorch/ao#1842, pytorch/ao#1808):

>>> from transformers import AutoProcessor
import error: No module named 'triton'
W0813 05:05:53.890000 17158 site-packages/torch/distributed/elastic/multiprocessing/redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.

This change replaces the eager import with a lazy import inside _load_shard_file(), preserving existing behavior while avoiding unnecessary side effects.

cc @MekkCyber

n0kovo added 2 commits August 13, 2025 04:27
Previously, modeling_utils imported torchao.quantization.Int4WeightOnlyConfig
at module load time if torchao was available. On CPU-only and Mac/Windows
systems, this triggered a Triton ImportError print and a torch.distributed.elastic
redirect warning, even if torchao quantization was not in use.

This change replaces the eager import with a lazy import inside
_load_shard_file(), preserving existing behavior while avoiding unnecessary
side effects.
Copy link
Copy Markdown
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix 🤗 ! left a small comment


if is_torchao_available():
from torchao.quantization import Int4WeightOnlyConfig
Int4WeightOnlyConfig = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this ?

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