Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
21 changes: 21 additions & 0 deletions optimum/exporters/openvino/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,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 = quant_method in supported_quant_methods
do_gptq_patching = quant_method == "gptq"
do_bitnet_patching = quant_method == "bitnet"

model_type = config.model_type
if model_type not in TasksManager._SUPPORTED_MODEL_TYPE:
Expand Down Expand Up @@ -364,6 +367,22 @@ 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 @@ -539,6 +558,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
15 changes: 15 additions & 0 deletions optimum/exporters/openvino/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,21 @@ class GptOssOpenVINOConfig(LlamaOpenVINOConfig):
MIN_TRANSFORMERS_VERSION = "4.55.1"


@register_in_tasks_manager(
"bitnet",
*[
"text-generation",
"text-generation-with-past",
],
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 OVDecoderModelPatcher(self, model, model_kwargs=model_kwargs)


@register_in_tasks_manager(
"exaone",
*[
Expand Down
6 changes: 5 additions & 1 deletion tests/openvino/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
from optimum.intel.pipelines import pipeline as optimum_pipeline
from optimum.intel.utils.import_utils import is_openvino_version, is_transformers_version


if is_transformers_version(">=", "4.55"):
from transformers import Mxfp4Config

torch.compile = lambda func: func # Mock torch.compile to avoid compilation errors in tests

SEED = 42
F32_CONFIG = {"INFERENCE_PRECISION_HINT": "f32"}
TENSOR_ALIAS_TO_TYPE = {"pt": torch.Tensor, "np": np.ndarray}
Expand Down Expand Up @@ -120,6 +121,8 @@ class OVModelForCausalLMIntegrationTest(unittest.TestCase):

if is_transformers_version(">=", "4.53.0"):
SUPPORTED_ARCHITECTURES += ("arcee",)
if is_openvino_version(">=", "2025.3.0"):
SUPPORTED_ARCHITECTURES += ("bitnet",)

if is_transformers_version(">=", "4.54.0"):
# remote code models differs after transformers v4.54
Expand Down Expand Up @@ -216,6 +219,7 @@ class OVModelForCausalLMIntegrationTest(unittest.TestCase):
"mamba": 0,
"falcon-mamba": 0,
"arcee": 2,
"bitnet": 6,
}

# TODO: remove gptq/awq from here
Expand Down
1 change: 1 addition & 0 deletions tests/openvino/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"baichuan2-13b": "katuni4ka/tiny-random-baichuan2-13b",
"bigbird_pegasus": "hf-internal-testing/tiny-random-bigbird_pegasus",
"biogpt": "hf-tiny-model-private/tiny-random-BioGptForCausalLM",
"bitnet": "mvafin/tiny-bitnet",
"blenderbot-small": "hf-internal-testing/tiny-random-BlenderbotModel",
"blenderbot": "hf-internal-testing/tiny-random-BlenderbotModel",
"bloom": "hf-internal-testing/tiny-random-BloomModel",
Expand Down
Loading