-
Notifications
You must be signed in to change notification settings - Fork 29
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
Implemented Roberta Model #65
Open
Mr-Niraj-Kulkarni
wants to merge
6
commits into
ivy-llc:main
Choose a base branch
from
Mr-Niraj-Kulkarni:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb11ef2
robert implementation
Mr-Niraj-Kulkarni 7c9b117
updated the roberta model
Mr-Niraj-Kulkarni 429176b
Merge branch 'unifyai:master' into master
Mr-Niraj-Kulkarni 60c4efb
added the init file for roberta
Mr-Niraj-Kulkarni 7c93334
Merge branch 'unifyai:master' into master
Mr-Niraj-Kulkarni 35bbd73
Merge branch 'unifyai:master' into master
Mr-Niraj-Kulkarni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import ivy | ||
from ivy_models.bert.layers import BertEmbedding | ||
|
||
|
||
class RobertaEmbeddings(BertEmbedding): | ||
"""Same as Bert Embedding with tiny change in the positional indexing.""" | ||
|
||
def __init__( | ||
self, | ||
vocab_size, | ||
hidden_size, | ||
max_position_embeddings, | ||
type_vocab_size=1, | ||
pad_token_id=None, | ||
embd_drop_rate=0.1, | ||
layer_norm_eps=1e-5, | ||
position_embedding_type="absolute", | ||
v=None, | ||
): | ||
super(RobertaEmbeddings, self).__init__( | ||
vocab_size, | ||
hidden_size, | ||
max_position_embeddings, | ||
type_vocab_size, | ||
pad_token_id, | ||
embd_drop_rate, | ||
layer_norm_eps, | ||
position_embedding_type, | ||
v, | ||
) | ||
self.padding_idx = 1 | ||
|
||
def _forward( | ||
self, | ||
input_ids, | ||
token_type_ids=None, | ||
position_ids=None, | ||
past_key_values_length: int = 0, | ||
): | ||
input_shape = input_ids.shape | ||
seq_length = input_shape[1] | ||
|
||
if position_ids is None: | ||
position_ids = ivy.expand_dims( | ||
ivy.arange(self.padding_idx + 1, seq_length + self.padding_idx), axis=0 | ||
) | ||
position_ids = position_ids[ | ||
:, past_key_values_length : seq_length + past_key_values_length | ||
] | ||
return super(RobertaEmbeddings, self)._forward( | ||
input_ids, | ||
token_type_ids=token_type_ids, | ||
position_ids=position_ids, | ||
past_key_values_length=past_key_values_length, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from ivy_models.helpers import load_transformers_weights | ||
from ivy_models.bert import BertConfig, BertModel | ||
from .layers import RobertaEmbeddings | ||
|
||
|
||
class RobertaModel(BertModel): | ||
def __init__(self, config: BertConfig, pooler_out=False): | ||
super(RobertaModel, self).__init__(config, pooler_out=pooler_out) | ||
|
||
@classmethod | ||
def get_spec_class(self): | ||
return BertConfig | ||
|
||
def _build(self, *args, **kwargs): | ||
self.embeddings = RobertaEmbeddings(**self.config.get_embd_attrs()) | ||
super(RobertaModel, self)._build(*args, **kwargs) | ||
|
||
def _forward( | ||
self, | ||
input_ids, | ||
attention_mask=None, | ||
token_type_ids=None, | ||
position_ids=None, | ||
encoder_hidden_states=None, | ||
encoder_attention_mask=None, | ||
past_key_values=None, | ||
use_cache=None, | ||
output_attentions=None, | ||
): | ||
if input_ids[:, 0].sum().item() != 0: | ||
print("NOT ALLOWED") | ||
return super(RobertaModel, self)._forward( | ||
input_ids, | ||
attention_mask=attention_mask, | ||
token_type_ids=token_type_ids, | ||
position_ids=position_ids, | ||
encoder_hidden_states=encoder_hidden_states, | ||
encoder_attention_mask=encoder_attention_mask, | ||
past_key_values=past_key_values, | ||
use_cache=use_cache, | ||
output_attentions=output_attentions, | ||
) | ||
|
||
|
||
def _roberta_weights_mapping(name): | ||
key_map = [(f"__v{i}__", f"__{j}__") for i, j in zip(range(12), range(12))] | ||
key_map = key_map + [ | ||
("attention__dense", "attention.output.dense"), | ||
("attention__LayerNorm", "attention.output.LayerNorm"), | ||
] | ||
key_map = key_map + [ | ||
("ffd__dense1", "intermediate.dense"), | ||
("ffd__dense2", "output.dense"), | ||
("ffd__LayerNorm", "output.LayerNorm"), | ||
] | ||
name = name.replace("__w", ".weight").replace("__b", ".bias") | ||
name = ( | ||
name.replace("biasias", "bias") | ||
.replace("weighteight", "weight") | ||
.replace(".weightord", ".word") | ||
) | ||
for ref, new in key_map: | ||
name = name.replace(ref, new) | ||
name = name.replace("__", ".") | ||
return name | ||
|
||
|
||
def roberta_base(pretrained=True): | ||
# instantiate the hyperparameters same as bert | ||
# set the dropout rate to 0.0 to avoid stochasticity in the output | ||
|
||
config = BertConfig( | ||
vocab_size=50265, | ||
hidden_size=768, | ||
num_hidden_layers=12, | ||
num_attention_heads=12, | ||
intermediate_size=3072, | ||
hidden_act="gelu", | ||
hidden_dropout=0.0, | ||
attn_drop_rate=0.0, | ||
max_position_embeddings=514, | ||
type_vocab_size=1, | ||
) | ||
model = RobertaModel(config, pooler_out=True) | ||
if pretrained: | ||
w_clean = load_transformers_weights( | ||
"roberta-base", model, _roberta_weights_mapping | ||
) | ||
model.v = w_clean | ||
return model |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import ivy | ||
import pytest | ||
import numpy as np | ||
from ivy_models.roberta import roberta_base | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please quickly ref |
||
|
||
@pytest.mark.parametrize("batch_shape", [[1]]) | ||
@pytest.mark.parametrize("load_weights", [False, True]) | ||
def test_roberta(device, fw, batch_shape, load_weights): | ||
"""Test RoBerta Base Sequence Classification""" | ||
|
||
num_dims = 768 | ||
inputs = np.load( | ||
"/models/ivy_models_tests/roberta/roberta_inputs.npy", allow_pickle=True | ||
Mr-Niraj-Kulkarni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
).tolist() | ||
|
||
model = roberta_base(load_weights) | ||
inputs = {k: ivy.asarray(v) for k, v in inputs.items()} | ||
logits = model(**inputs)["pooler_output"] | ||
ref_logits = np.load("/models/ivy_models_tests/roberta/roberta_pooled_output.npy") | ||
Mr-Niraj-Kulkarni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert logits.shape == tuple([ivy.to_scalar(batch_shape), num_dims]) | ||
assert np.allclose(ref_logits, logits, rtol=0.005, atol=0.005) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
to_scalar
instead ofitem