Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more model outputs for BERT/ERNIE/RoBERTa #2583

Merged
merged 9 commits into from
Jun 26, 2022

Conversation

guoshengCS
Copy link
Contributor

@guoshengCS guoshengCS commented Jun 20, 2022

PR types

New features

PR changes

APIs

Description

Support more model outputs for BERT. The default behavior is the same as before for compatibility.

Import more output structures refer to HF.

    BaseModelOutputWithPastAndCrossAttentions,
    BaseModelOutputWithPoolingAndCrossAttentions,
    SequenceClassifierOutput,
    TokenClassifierOutput,
    QuestionAnsweringModelOutput,
    MultipleChoiceModelOutput,
    MaskedLMOutput,
    ModelOutput,

Usage:

import paddle
import paddlenlp
from paddlenlp.transformers import AutoModel, AutoTokenizer, AutoModelForTokenClassification, AutoModelForPretraining

paddle.seed(123)
model_name = "bert-base-uncased"
model_name = "ernie-1.0-base-zh"
model_name = "hfl/roberta-wwm-ext"
#model = AutoModel.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)
#model = AutoModelForPretraining.from_pretrained(model_name)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(model_name)

inputs = tokenizer("欢迎使用百度飞桨!")
inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
print(inputs)
outputs = model(**inputs, return_dict=True, output_hidden_states=True, output_attentions=False)
print(outputs)

@guoshengCS guoshengCS marked this pull request as ready for review June 22, 2022 06:48
@guoshengCS guoshengCS changed the title Support more model outputs for BERT Support more model outputs for BERT/ERNIE/RoBERTa Jun 22, 2022
Comment on lines 116 to 124

import inspect
import dataclasses
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack
from dataclasses import fields, dataclass
from enum import Enum
from typing import Any, ContextManager, List, Tuple, Optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 import 是否应该挪到文件开头位置

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 72 to 86
all_hidden_states = [] if output_hidden_states else None
for i, mod in enumerate(self.layers):
layer_outputs = mod(output,
src_mask=src_mask,
cache=None if cache is None else cache[i],
output_attentions=output_attentions)
if isinstance(layer_outputs, tuple):
output = layer_outputs[0]
outputs = layer_outputs[1:]
else:
output = layer_outputs
outputs = None

if output_hidden_states:
all_hidden_states.append(output)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HF 中的 all_hidden_states 包含了 embedding_output,这里的实现相比会少这个 Tensor。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks

Copy link
Contributor

@yingyibiao yingyibiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants