mirror of
https://github.com/modelscope/modelscope.git
synced 2026-09-01 19:49:03 +02:00
fix chatglm2 evaluation error: hypothesis emtpy (#348)
* fix evaluation error: hypothesis emtpy * fix pipeline * fix bug
This commit is contained in:
@@ -16,6 +16,8 @@ class Seq2SeqTrainer(EpochBasedTrainer):
|
||||
if ignore_pad_token_for_loss:
|
||||
tokens = np.where(tokens != -100, tokens,
|
||||
self.tokenizer.pad_token_id)
|
||||
tokens = np.where(tokens < self.tokenizer.vocab_size, tokens,
|
||||
self.tokenizer.pad_token_id)
|
||||
return [
|
||||
t for t in self.tokenizer.batch_decode(
|
||||
tokens, skip_special_tokens=True) if t != '</s>'
|
||||
@@ -59,7 +61,9 @@ class Seq2SeqTrainer(EpochBasedTrainer):
|
||||
|
||||
gen_kwargs['input_ids'] = generation_inputs
|
||||
gen_kwargs['pad_token_id'] = self.tokenizer.pad_token_id
|
||||
generated_tokens = self.model.generate(**gen_kwargs)
|
||||
self.model.eval()
|
||||
with torch.no_grad():
|
||||
generated_tokens = self.model.generate(**gen_kwargs)
|
||||
generated_tokens = generated_tokens[:, generation_inputs.size()[-1]:]
|
||||
|
||||
# in case the batch is shorter than max length, the output should be padded
|
||||
|
||||
@@ -192,8 +192,15 @@ if config['model']['type'] == 'chatglm6b':
|
||||
model_config['model']['prefix_projection'] = args.prefix_projection
|
||||
|
||||
tokenizer = ChatGLMTokenizer.from_pretrained(model_dir, trust_remote_code=True)
|
||||
|
||||
device_map_kwargs = {}
|
||||
device_kwargs = {}
|
||||
if args.use_lora != 0:
|
||||
device_kwargs['device_map'] = 'auto'
|
||||
# No placement for model, leave the model to `device_map`
|
||||
device_kwargs['device'] = 'cpu'
|
||||
model = Model.from_pretrained(
|
||||
model_dir, cfg_dict=model_config, device_map='auto')
|
||||
model_dir, cfg_dict=model_config, **device_map_kwargs)
|
||||
|
||||
if args.ptuning_checkpoint is not None:
|
||||
# Evaluation
|
||||
@@ -378,8 +385,7 @@ trainer = Seq2SeqTrainer(
|
||||
seed=args.seed,
|
||||
data_collator=data_collator,
|
||||
remove_unused_data=True,
|
||||
# No placement for model, leave the model to `device_map`
|
||||
device='cpu',
|
||||
cfg_modify_fn=cfg_modify_fn)
|
||||
cfg_modify_fn=cfg_modify_fn,
|
||||
**device_kwargs)
|
||||
trainer.tokenizer = tokenizer
|
||||
trainer.train()
|
||||
|
||||
@@ -53,7 +53,7 @@ class TextGenerationMetric(Metric):
|
||||
}
|
||||
for pred, label in zip(preds, labels):
|
||||
hypothesis = list(jieba.cut(pred))
|
||||
if len(hypothesis) == 0:
|
||||
if len(hypothesis) == 0 or ''.join(hypothesis) == '.':
|
||||
hypothesis = ['</s>']
|
||||
reference = list(jieba.cut(label))
|
||||
rouge = Rouge()
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Any, Dict, Optional, Union
|
||||
|
||||
import torch
|
||||
|
||||
from modelscope import snapshot_download
|
||||
from modelscope.metainfo import Pipelines
|
||||
from modelscope.models.base import Model
|
||||
from modelscope.outputs import (ModelOutputBase, OutputKeys,
|
||||
@@ -192,9 +193,14 @@ class ChatGLM6bTextGenerationPipeline(Pipeline):
|
||||
quantization_bit=None,
|
||||
use_bf16=False,
|
||||
**kwargs):
|
||||
from modelscope.models.nlp.chatglm.text_generation import ChatGLMForConditionalGeneration
|
||||
model = ChatGLMForConditionalGeneration(model) if isinstance(
|
||||
model, str) else model
|
||||
from modelscope.models.nlp.chatglm.text_generation import ChatGLMForConditionalGeneration, ChatGLMConfig
|
||||
if isinstance(model, str):
|
||||
model_dir = snapshot_download(
|
||||
model) if not os.path.exists(model) else model
|
||||
config = ChatGLMConfig.from_pretrained(model_dir)
|
||||
model = ChatGLMForConditionalGeneration(config).half()
|
||||
if torch.cuda.is_available():
|
||||
model = model.cuda()
|
||||
if quantization_bit is not None:
|
||||
model = model.quantize(quantization_bit)
|
||||
if use_bf16:
|
||||
@@ -225,9 +231,14 @@ class ChatGLM6bV2TextGenerationPipeline(Pipeline):
|
||||
quantization_bit=None,
|
||||
use_bf16=False,
|
||||
**kwargs):
|
||||
from modelscope.models.nlp import ChatGLM2ForConditionalGeneration, ChatGLM2Tokenizer
|
||||
model = ChatGLM2ForConditionalGeneration(model) if isinstance(
|
||||
model, str) else model
|
||||
from modelscope.models.nlp import ChatGLM2ForConditionalGeneration, ChatGLM2Tokenizer, ChatGLM2Config
|
||||
if isinstance(model, str):
|
||||
model_dir = snapshot_download(
|
||||
model) if not os.path.exists(model) else model
|
||||
config = ChatGLM2Config.from_pretrained(model_dir)
|
||||
model = ChatGLM2ForConditionalGeneration(config)
|
||||
if torch.cuda.is_available():
|
||||
model = model.cuda()
|
||||
if quantization_bit is not None:
|
||||
model = model.quantize(quantization_bit)
|
||||
if use_bf16:
|
||||
|
||||
Reference in New Issue
Block a user