Skip to content

Commit

Permalink
Add eval method for OVModels (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
echarlaix authored Mar 18, 2024
1 parent 428c778 commit e036ce6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions optimum/intel/openvino/modeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ def half(self):
self.request = None
return self

def eval(self):
return self

def forward(self, *args, **kwargs):
raise NotImplementedError

Expand Down
10 changes: 10 additions & 0 deletions tests/openvino/test_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForSequenceClassification.from_pretrained(model_id, export=True, compile=False)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
text = "This restaurant is awesome"
Expand Down Expand Up @@ -345,6 +346,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForQuestionAnswering.from_pretrained(model_id, export=True)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("question-answering", model=model, tokenizer=tokenizer)
question = "What's my name?"
Expand Down Expand Up @@ -411,6 +413,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForTokenClassification.from_pretrained(model_id, export=True)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("token-classification", model=model, tokenizer=tokenizer)
outputs = pipe("My Name is Arthur and I live in Lyon.")
Expand Down Expand Up @@ -460,6 +463,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForFeatureExtraction.from_pretrained(model_id, export=True)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
outputs = pipe("My Name is Arthur and I live in Lyon.")
Expand Down Expand Up @@ -568,6 +572,7 @@ def test_pipeline(self, model_arch):
model = OVModelForCausalLM.from_pretrained(
model_id, export=True, use_cache=False, compile=False, **model_kwargs
)
model.eval()
model.config.encoder_no_repeat_ngram_size = 0
model.to("cpu")
model.half()
Expand Down Expand Up @@ -758,6 +763,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForMaskedLM.from_pretrained(model_id, export=True)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("fill-mask", model=model, tokenizer=tokenizer)
outputs = pipe(f"This is a {tokenizer.mask_token}.")
Expand Down Expand Up @@ -815,6 +821,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForImageClassification.from_pretrained(model_id, export=True)
model.eval()
preprocessor = AutoFeatureExtractor.from_pretrained(model_id)
pipe = pipeline("image-classification", model=model, feature_extractor=preprocessor)
outputs = pipe("http://images.cocodataset.org/val2017/000000039769.jpg")
Expand Down Expand Up @@ -911,6 +918,7 @@ def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = OVModelForSeq2SeqLM.from_pretrained(model_id, export=True, compile=False)
model.eval()
model.half()
model.to("cpu")
model.compile()
Expand Down Expand Up @@ -1044,6 +1052,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForAudioClassification.from_pretrained(model_id, export=True)
model.eval()
preprocessor = AutoFeatureExtractor.from_pretrained(model_id)
pipe = pipeline("audio-classification", model=model, feature_extractor=preprocessor)
outputs = pipe([np.random.random(16000)])
Expand Down Expand Up @@ -1354,6 +1363,7 @@ def test_compare_to_transformers(self, model_arch):
def test_pipeline(self, model_arch):
model_id = MODEL_NAMES[model_arch]
model = OVModelForSpeechSeq2Seq.from_pretrained(model_id, export=True)
model.eval()
processor = get_preprocessor(model_id)
GenerationConfig.from_pretrained(model_id)
pipe = pipeline(
Expand Down

0 comments on commit e036ce6

Please sign in to comment.