2023-07-24 20:53:27 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
2025-01-22 23:06:20 +08:00
|
|
|
from modelscope.utils.hf_util.patcher import patch_context
|
2023-07-24 20:53:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class HFUtilTest(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_auto_tokenizer(self):
|
2025-01-22 23:06:20 +08:00
|
|
|
from modelscope import AutoTokenizer
|
2023-07-24 20:53:27 +08:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
2023-10-24 15:18:55 +08:00
|
|
|
'baichuan-inc/Baichuan2-7B-Chat',
|
2023-07-24 20:53:27 +08:00
|
|
|
trust_remote_code=True,
|
|
|
|
|
revision='v1.0.3')
|
2023-10-24 15:18:55 +08:00
|
|
|
self.assertEqual(tokenizer.vocab_size, 125696)
|
2023-07-24 20:53:27 +08:00
|
|
|
self.assertEqual(tokenizer.model_max_length, 4096)
|
|
|
|
|
self.assertFalse(tokenizer.is_fast)
|
|
|
|
|
|
2023-09-26 21:15:41 +08:00
|
|
|
def test_quantization_import(self):
|
|
|
|
|
from modelscope import GPTQConfig, BitsAndBytesConfig
|
|
|
|
|
self.assertTrue(BitsAndBytesConfig is not None)
|
|
|
|
|
|
2023-07-24 20:53:27 +08:00
|
|
|
def test_auto_model(self):
|
2025-01-22 23:06:20 +08:00
|
|
|
from modelscope import AutoModelForCausalLM
|
2023-07-24 20:53:27 +08:00
|
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
'baichuan-inc/baichuan-7B', trust_remote_code=True)
|
|
|
|
|
self.assertTrue(model is not None)
|
|
|
|
|
|
|
|
|
|
def test_auto_config(self):
|
2025-01-22 23:06:20 +08:00
|
|
|
from modelscope import AutoConfig, GenerationConfig
|
2023-07-24 20:53:27 +08:00
|
|
|
config = AutoConfig.from_pretrained(
|
|
|
|
|
'baichuan-inc/Baichuan-13B-Chat',
|
|
|
|
|
trust_remote_code=True,
|
|
|
|
|
revision='v1.0.3')
|
|
|
|
|
self.assertEqual(config.model_type, 'baichuan')
|
|
|
|
|
gen_config = GenerationConfig.from_pretrained(
|
|
|
|
|
'baichuan-inc/Baichuan-13B-Chat',
|
|
|
|
|
trust_remote_code=True,
|
|
|
|
|
revision='v1.0.3')
|
|
|
|
|
self.assertEqual(gen_config.assistant_token_id, 196)
|
|
|
|
|
|
|
|
|
|
def test_transformer_patch(self):
|
2025-01-22 23:06:20 +08:00
|
|
|
with patch_context():
|
|
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-base')
|
|
|
|
|
self.assertIsNotNone(tokenizer)
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-base')
|
|
|
|
|
self.assertIsNotNone(model)
|
|
|
|
|
|
|
|
|
|
def test_patch_model(self):
|
|
|
|
|
from modelscope.utils.hf_util.patcher import patch_context
|
|
|
|
|
with patch_context():
|
|
|
|
|
from transformers import AutoModel
|
|
|
|
|
model = AutoModel.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-tiny')
|
|
|
|
|
self.assertTrue(model is not None)
|
|
|
|
|
try:
|
|
|
|
|
model = AutoModel.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-tiny')
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
self.assertTrue(False)
|
|
|
|
|
|
|
|
|
|
def test_patch_config(self):
|
|
|
|
|
with patch_context():
|
|
|
|
|
from transformers import AutoConfig
|
|
|
|
|
config = AutoConfig.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-tiny')
|
|
|
|
|
self.assertTrue(config is not None)
|
|
|
|
|
try:
|
|
|
|
|
config = AutoConfig.from_pretrained(
|
|
|
|
|
'iic/nlp_structbert_sentiment-classification_chinese-tiny')
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
self.assertTrue(False)
|
|
|
|
|
|
|
|
|
|
def test_patch_diffusers(self):
|
|
|
|
|
with patch_context():
|
|
|
|
|
from diffusers import StableDiffusionPipeline
|
|
|
|
|
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
|
'AI-ModelScope/stable-diffusion-v1-5')
|
|
|
|
|
self.assertTrue(pipe is not None)
|
|
|
|
|
try:
|
|
|
|
|
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
|
'AI-ModelScope/stable-diffusion-v1-5')
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
self.assertTrue(False)
|
|
|
|
|
|
|
|
|
|
def test_patch_peft(self):
|
|
|
|
|
with patch_context():
|
|
|
|
|
from peft import PeftModel
|
|
|
|
|
self.assertTrue(hasattr(PeftModel, '_from_pretrained_origin'))
|
|
|
|
|
self.assertFalse(hasattr(PeftModel, '_from_pretrained_origin'))
|
2023-07-24 20:53:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|