-
Notifications
You must be signed in to change notification settings - Fork 682
fix lite module for transformers>=5.0 #4488
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
2dbcb9f
a5c6941
67bc719
0dd1268
618d630
18d1c6e
9922e63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,23 +251,25 @@ def calibrate(model: str, | |
| model = load_hf_from_pretrained(model, dtype=dtype, trust_remote_code=True) | ||
| vl_model = None | ||
| elif model_type == 'vlm': | ||
| from transformers import AutoConfig | ||
| original_torch_dtype = AutoConfig.from_pretrained(model, trust_remote_code=True).torch_dtype | ||
| vl_model = load_vl_model(model, backend=None, with_llm=True).vl_model | ||
| model = vl_model | ||
| if hasattr(vl_model, 'language_model'): # deepseek-vl, ... | ||
| model = vl_model.language_model | ||
| if hasattr(vl_model, 'llm'): # MiniCPMV, ... | ||
| model = vl_model.llm | ||
| model.config.use_cache = False | ||
| if dtype == 'float16': | ||
| if hasattr(model.config, 'text_config'): | ||
| model.config.text_config.use_cache = False | ||
| elif hasattr(model.config, 'llm_config'): | ||
| model.config.llm_config.use_cache = False | ||
| if dtype == 'float16' or (dtype == 'auto' and original_torch_dtype == torch.float16): | ||
| model.half() | ||
| elif dtype == 'bfloat16': | ||
| elif dtype == 'bfloat16' or (dtype == 'auto' and original_torch_dtype == torch.bfloat16): | ||
| assert torch.cuda.is_bf16_supported( | ||
|
||
| ), 'your device does not support bfloat16 please set --dtype float16' # noqa | ||
| model.to(torch.bfloat16) | ||
| elif dtype == 'auto' and model.config.torch_dtype == torch.bfloat16: | ||
| print('Warning: we cast model to float16 to prevent OOM. You' | ||
| ' may enforce it bfloat16 by `--dtype bfloat16`') | ||
| model.half() | ||
| model.eval() | ||
|
|
||
| model_type = type(model).__name__ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -236,7 +236,10 @@ def smooth_fc_fcs(pre_fc: torch.nn.Module, | |||||||
| 'clamping w_scales.pow(1 - alpha) to 1e-4') | ||||||||
| w_scales_pow = w_scales_pow.clamp(min=1e-4) | ||||||||
| scales = (act_scales.pow(alpha) / w_scales_pow).clamp(min=1e-4).to(device).to(dtype) | ||||||||
| scales = scales / (scales.max() * scales.min()).sqrt() | ||||||||
| # prevent scales.max() * scales.min() == inf | ||||||||
| denom = (scales.max().float() * scales.min().float()).sqrt() | ||||||||
| denom = denom.to(dtype=dtype) | ||||||||
| scales = scales / denom | ||||||||
|
||||||||
| denom = denom.to(dtype=dtype) | |
| scales = scales / denom | |
| scales = (scales.float() / denom).to(device=device, dtype=dtype) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -66,10 +66,6 @@ def load_hf_from_pretrained(pretrained_model_name_or_path, dtype: Literal['float | |||||||||||||||||||||||||
| torch_dtype = torch.bfloat16 | ||||||||||||||||||||||||||
| elif dtype == 'float16': | ||||||||||||||||||||||||||
| torch_dtype = torch.float16 | ||||||||||||||||||||||||||
| elif dtype == 'auto' and torch_dtype == torch.bfloat16: | ||||||||||||||||||||||||||
| print('Warning: we cast model to float16 to prevent OOM. ' | ||||||||||||||||||||||||||
| 'You may enforce it bfloat16 by `--dtype bfloat16`') | ||||||||||||||||||||||||||
| torch_dtype = torch.float16 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| if torch_dtype == torch.bfloat16 and not torch.cuda.is_bf16_supported(): | |
| if dtype == 'auto': | |
| torch_dtype = torch.float16 | |
| if hasattr(hf_config, 'bf16'): | |
| hf_config.bf16 = False | |
| if hasattr(hf_config, 'fp16'): | |
| hf_config.fp16 = True | |
| else: | |
| raise RuntimeError('Your device does not supports bf16(bfloat16), ' | |
| 'please change to fp16(float16)') |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,7 +48,7 @@ def from_float(cls, mod: nn.Module, initialization: bool = True, quant_dtype=tor | |||||||||
| `initialization = True` for real init. `initialization = False` for dummy init. | ||||||||||
| """ | ||||||||||
| hidden_size = mod.weight.shape[0] | ||||||||||
| eps = mod.variance_epsilon | ||||||||||
| eps = getattr(mod, 'variance_epsilon', None) or getattr(mod, 'eps', 1e-6) | ||||||||||
|
||||||||||
| eps = getattr(mod, 'variance_epsilon', None) or getattr(mod, 'eps', 1e-6) | |
| eps = getattr(mod, 'variance_epsilon', None) | |
| if eps is None: | |
| eps = getattr(mod, 'eps', 1e-6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds an extra
AutoConfig.from_pretrained()call for VLM calibration, butload_vl_model()already loads the HF config viaget_model_arch()(which callsAutoConfig.from_pretrained). Consider reusing that existing config (or reading torch_dtype from the loaded model/config) to avoid duplicate network/cache IO.