Browse Source

add eval() to pipeline call

master
雨泓 3 years ago
parent
commit
67a8440de5
7 changed files with 52 additions and 8 deletions
  1. +9
    -1
      modelscope/pipelines/nlp/fill_mask_pipeline.py
  2. +7
    -1
      modelscope/pipelines/nlp/nli_pipeline.py
  3. +7
    -1
      modelscope/pipelines/nlp/sentence_similarity_pipeline.py
  4. +7
    -1
      modelscope/pipelines/nlp/sentiment_classification_pipeline.py
  5. +8
    -2
      modelscope/pipelines/nlp/text_generation_pipeline.py
  6. +7
    -1
      modelscope/pipelines/nlp/word_segmentation_pipeline.py
  7. +7
    -1
      modelscope/pipelines/nlp/zero_shot_classification_pipeline.py

+ 9
- 1
modelscope/pipelines/nlp/fill_mask_pipeline.py View File

@@ -1,4 +1,6 @@
from typing import Dict, Optional, Union
from typing import Dict, Optional, Union, Any

import torch


from ...models import Model from ...models import Model
from ...models.nlp.masked_language_model import \ from ...models.nlp.masked_language_model import \
@@ -35,6 +37,7 @@ class FillMaskPipeline(Pipeline):
fill_mask_model.model_dir, fill_mask_model.model_dir,
first_sequence=first_sequence, first_sequence=first_sequence,
second_sequence=None) second_sequence=None)
fill_mask_model.eval()
super().__init__(model=fill_mask_model, preprocessor=preprocessor, **kwargs) super().__init__(model=fill_mask_model, preprocessor=preprocessor, **kwargs)
self.preprocessor = preprocessor self.preprocessor = preprocessor
self.tokenizer = preprocessor.tokenizer self.tokenizer = preprocessor.tokenizer
@@ -61,6 +64,11 @@ class FillMaskPipeline(Pipeline):
} }
} }


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Tensor]) -> Dict[str, Tensor]: def postprocess(self, inputs: Dict[str, Tensor]) -> Dict[str, Tensor]:
"""process the prediction results """process the prediction results




+ 7
- 1
modelscope/pipelines/nlp/nli_pipeline.py View File

@@ -1,6 +1,6 @@
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union


@@ -42,9 +42,15 @@ class NLIPipeline(Pipeline):
sc_model.model_dir, sc_model.model_dir,
first_sequence=first_sequence, first_sequence=first_sequence,
second_sequence=second_sequence) second_sequence=second_sequence)
sc_model.eval()
super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs) super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs)
assert len(sc_model.id2label) > 0 assert len(sc_model.id2label) > 0


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]: def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results




+ 7
- 1
modelscope/pipelines/nlp/sentence_similarity_pipeline.py View File

@@ -1,7 +1,7 @@
from typing import Any, Dict, Union from typing import Any, Dict, Union


import numpy as np import numpy as np
import torch
from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models.nlp import SbertForSentenceSimilarity from ...models.nlp import SbertForSentenceSimilarity
from ...preprocessors import SequenceClassificationPreprocessor from ...preprocessors import SequenceClassificationPreprocessor
@@ -39,11 +39,17 @@ class SentenceSimilarityPipeline(Pipeline):
sc_model.model_dir, sc_model.model_dir,
first_sequence=first_sequence, first_sequence=first_sequence,
second_sequence=second_sequence) second_sequence=second_sequence)
sc_model.eval()
super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs) super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs)


assert hasattr(self.model, 'id2label'), \ assert hasattr(self.model, 'id2label'), \
'id2label map should be initalizaed in init function.' 'id2label map should be initalizaed in init function.'


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]: def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results




+ 7
- 1
modelscope/pipelines/nlp/sentiment_classification_pipeline.py View File

@@ -1,7 +1,7 @@
import os import os
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import json import json
import numpy as np import numpy as np


@@ -43,9 +43,15 @@ class SentimentClassificationPipeline(Pipeline):
sc_model.model_dir, sc_model.model_dir,
first_sequence=first_sequence, first_sequence=first_sequence,
second_sequence=second_sequence) second_sequence=second_sequence)
sc_model.eval()
super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs) super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs)
assert len(sc_model.id2label) > 0 assert len(sc_model.id2label) > 0


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]: def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results




+ 8
- 2
modelscope/pipelines/nlp/text_generation_pipeline.py View File

@@ -1,5 +1,5 @@
from typing import Dict, Optional, Union
from typing import Dict, Optional, Union, Any
import torch
from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp import PalmForTextGeneration from ...models.nlp import PalmForTextGeneration
@@ -33,9 +33,15 @@ class TextGenerationPipeline(Pipeline):
model.tokenizer, model.tokenizer,
first_sequence='sentence', first_sequence='sentence',
second_sequence=None) second_sequence=None)
model.eval()
super().__init__(model=model, preprocessor=preprocessor, **kwargs) super().__init__(model=model, preprocessor=preprocessor, **kwargs)
self.tokenizer = model.tokenizer self.tokenizer = model.tokenizer


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Tensor], **postprocess_params) -> Dict[str, str]: def postprocess(self, inputs: Dict[str, Tensor], **postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results




+ 7
- 1
modelscope/pipelines/nlp/word_segmentation_pipeline.py View File

@@ -1,5 +1,5 @@
from typing import Any, Dict, Optional, Union from typing import Any, Dict, Optional, Union
import torch
from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp import SbertForTokenClassification from ...models.nlp import SbertForTokenClassification
@@ -30,12 +30,18 @@ class WordSegmentationPipeline(Pipeline):
SbertForTokenClassification) else Model.from_pretrained(model) SbertForTokenClassification) else Model.from_pretrained(model)
if preprocessor is None: if preprocessor is None:
preprocessor = TokenClassifcationPreprocessor(model.model_dir) preprocessor = TokenClassifcationPreprocessor(model.model_dir)
model.eval()
super().__init__(model=model, preprocessor=preprocessor, **kwargs) super().__init__(model=model, preprocessor=preprocessor, **kwargs)
self.tokenizer = preprocessor.tokenizer self.tokenizer = preprocessor.tokenizer
self.config = model.config self.config = model.config
assert len(self.config.id2label) > 0 assert len(self.config.id2label) > 0
self.id2label = self.config.id2label self.id2label = self.config.id2label


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]: def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results




+ 7
- 1
modelscope/pipelines/nlp/zero_shot_classification_pipeline.py View File

@@ -1,7 +1,7 @@
import os import os
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import json import json
import numpy as np import numpy as np
from scipy.special import softmax from scipy.special import softmax
@@ -44,6 +44,7 @@ class ZeroShotClassificationPipeline(Pipeline):
if preprocessor is None: if preprocessor is None:
preprocessor = ZeroShotClassificationPreprocessor( preprocessor = ZeroShotClassificationPreprocessor(
sc_model.model_dir) sc_model.model_dir)
model.eval()
super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs) super().__init__(model=sc_model, preprocessor=preprocessor, **kwargs)


def _sanitize_parameters(self, **kwargs): def _sanitize_parameters(self, **kwargs):
@@ -62,6 +63,11 @@ class ZeroShotClassificationPipeline(Pipeline):
postprocess_params['multi_label'] = kwargs.pop('multi_label', False) postprocess_params['multi_label'] = kwargs.pop('multi_label', False)
return preprocess_params, {}, postprocess_params return preprocess_params, {}, postprocess_params


def forward(self, inputs: Dict[str, Any],
**forward_params) -> Dict[str, Any]:
with torch.no_grad():
return super().forward(inputs, **forward_params)

def postprocess(self, def postprocess(self,
inputs: Dict[str, Any], inputs: Dict[str, Any],
candidate_labels, candidate_labels,


Loading…
Cancel
Save