Skip to content

Commit

Permalink
Support Phi-2 model (#1137)
Browse files Browse the repository at this point in the history
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
  • Loading branch information
lvliang-intel committed Jan 13, 2024
1 parent 8500e7f commit 04f5ef6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion intel_extension_for_transformers/neural_chat/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def build_chatbot(config: PipelineConfig=None):
"starcoder" in config.model_name_or_path.lower() or \
"codegen" in config.model_name_or_path.lower() or \
"magicoder" in config.model_name_or_path.lower() or \
"mixtral" in config.model_name_or_path.lower():
"mixtral" in config.model_name_or_path.lower() or \
"phi-2" in config.model_name_or_path.lower():
from .models.base_model import BaseModel
adapter = BaseModel()
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def predict_stream(self, query, origin_query="", config=None):
self.get_conv_template(self.model_name, config.task)
if (self.conv_template.roles[0] in query and self.conv_template.roles[1] in query) or \
"starcoder" in self.model_name.lower() or "codellama" in self.model_name.lower() or \
"codegen" in self.model_name.lower() or "magicoder" in self.model_name.lower():
"codegen" in self.model_name.lower() or "magicoder" in self.model_name.lower() or \
"phi-2" in self.model_name.lower():
query_include_prompt = True

# plugin pre actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def load_model(
or config.model_type == "llama"
or config.model_type == "mistral"
or config.model_type == "mixtral"
or config.model_type == "phi"
) and not ipex_int8) or config.model_type == "opt":
with smart_context_manager(use_deepspeed=use_deepspeed):
model = AutoModelForCausalLM.from_pretrained(
Expand All @@ -509,7 +510,7 @@ def load_model(
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
quantization_config=bitsandbytes_quant_config,
trust_remote_code=True if (config.model_type == "qwen" or \
trust_remote_code=True if (config.model_type == "qwen" or config.model_type == "phi" or \
re.search("codegen", model_name, re.IGNORECASE)) else False
)
elif (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from intel_extension_for_transformers.neural_chat import build_chatbot, PipelineConfig
import unittest

class TestPhi2Model(unittest.TestCase):
def setUp(self):
return super().setUp()

def tearDown(self) -> None:
return super().tearDown()

def test_code_gen(self):
config = PipelineConfig(
model_name_or_path="microsoft/phi-2")
chatbot = build_chatbot(config=config)
result = chatbot.predict("Calculate 99+22=")
print(result)
self.assertIn("The answer is 121", str(result))

if __name__ == "__main__":
unittest.main()

0 comments on commit 04f5ef6

Please sign in to comment.