Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions optimum/exporters/openvino/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,11 @@ def main_export(
supported_quant_methods = ["gptq"]
if is_openvino_version(">=", "2024.6.0"):
supported_quant_methods.append("awq")
if is_openvino_version(">=", "2025.3.0"):
supported_quant_methods.append("bitnet")
do_quant_patching = quantization_config and quantization_config["quant_method"] in supported_quant_methods
do_gptq_patching = do_quant_patching and quantization_config["quant_method"] == "gptq"
do_bitnet_patching = do_quant_patching and quantization_config["quant_method"] == "bitnet"
model_type = config.model_type
if model_type not in TasksManager._SUPPORTED_MODEL_TYPE:
custom_architecture = True
Expand Down Expand Up @@ -356,6 +359,20 @@ class StoreAttr(object):
return model

GPTQQuantizer.post_init_model = post_init_model
if do_bitnet_patching:
from transformers.integrations.bitnet import AutoBitLinear

orig_load_hook = AutoBitLinear.load_hook

# rewrite load hook to save original weight
def bitnet_load_hook(self, state_dict, prefix, *args, **kwargs):
if (prefix + "weight") in state_dict and state_dict[prefix + "weight"].dtype != self.weight.dtype:
self.original_weight = state_dict[prefix + "weight"]
w_shape = self.original_weight.shape
state_dict[prefix + "weight"] = torch.empty((w_shape[0] * 4, w_shape[1]), dtype=self.weight.dtype, device="meta")
return state_dict

AutoBitLinear.load_hook = bitnet_load_hook
elif library_name == "diffusers" and is_openvino_version(">=", "2024.6"):
_loading_kwargs = {} if variant is None else {"variant": variant}
if dtype == "auto" or dtype is None:
Expand Down Expand Up @@ -531,6 +548,8 @@ class StoreAttr(object):
torch.cuda.is_available = orig_cuda_check
if do_gptq_patching:
GPTQQuantizer.post_init_model = orig_post_init_model
if do_bitnet_patching:
AutoBitLinear.load_hook = orig_load_hook


def maybe_convert_tokenizers(library_name: str, output: Path, model=None, preprocessors=None, task=None):
Expand Down
18 changes: 18 additions & 0 deletions optimum/exporters/openvino/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,24 @@ def patch_model_for_export(
return OVDecoderModelPatcher(self, model, model_kwargs=model_kwargs)


@register_in_tasks_manager(
"bitnet",
*[
"feature-extraction",
"feature-extraction-with-past",
"text-generation",
"text-generation-with-past",
"text-classification",
],
library_name="transformers",
)
class BitnetOpenVINOConfig(LlamaOnnxConfig):
def patch_model_for_export(
self, model: Union["PreTrainedModel", "TFPreTrainedModel"], model_kwargs: Optional[Dict[str, Any]] = None
) -> "ModelPatcher":
return LlamaModelPatcher(self, model, model_kwargs=model_kwargs)


@register_in_tasks_manager(
"exaone",
*[
Expand Down