add qunatized linear, refactor model for it soon to be addition

This commit is contained in:
2024-03-23 21:38:27 +01:00
parent 38a7f7cfc4
commit 3fa1fc254f
5 changed files with 191 additions and 71 deletions

View File

@ -5,3 +5,22 @@ import torch
def replace_module(model, key: str, module: torch.nn.Module):
parent, target, target_name = _get_submodules(model, key)
setattr(parent, target_name, module)
def find_all_linear_module_names(model) -> list[str]:
module_names = set()
for name, module in model.named_modules():
if isinstance(module, torch.nn.Linear):
module_names.add(name)
if 'lm_head' in module_names: # needed for 16-bit
module_names.remove('lm_head')
return list(module_names)
def find_all_outher_module_names(model) -> list[str]:
module_names = set()
for name, module in model.named_modules():
if not isinstance(module, torch.nn.Linear):
module_names.add(name)
return list(module_names)