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 tie_word_embeddings #263

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/bumblebee/text/albert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,14 @@ defmodule Bumblebee.Text.Albert do
end

defimpl Bumblebee.HuggingFace.Transformers.Model do
def params_mapping(_spec) do
def params_mapping(spec) do
language_modeling_head_output =
if Map.get(spec, :tie_word_embeddings, true) do
"albert.embeddings.word_embeddings"
else
"predictions.decoder"
end

%{
"embedder.token_embedding" => "albert.embeddings.word_embeddings",
"embedder.position_embedding" => "albert.embeddings.position_embeddings",
Expand All @@ -522,7 +529,7 @@ defmodule Bumblebee.Text.Albert do
"pooler.output" => "albert.pooler",
"language_modeling_head.dense" => "predictions.dense",
"language_modeling_head.norm" => "predictions.LayerNorm",
"language_modeling_head.output" => "predictions.decoder",
"language_modeling_head.output" => language_modeling_head_output,
"sequence_classification_head.output" => "classifier",
"token_classification_head.output" => "classifier",
"multiple_choice_head.output" => "classifier",
Expand Down
10 changes: 7 additions & 3 deletions lib/bumblebee/text/bart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,12 @@ defmodule Bumblebee.Text.Bart do
end

defimpl Bumblebee.HuggingFace.Transformers.Model do
def params_mapping(_spec) do
def params_mapping(spec) do
tie_word_embeddings = Map.get(spec, :tie_word_embeddings, true)

%{
"encoder_embedder.token_embedding" => "model.encoder.embed_tokens",
"encoder_embedder.token_embedding" =>
if(tie_word_embeddings, do: "model.shared", else: "model.encoder.embed_tokens"),
"encoder_embedder.position_embedding" => "model.encoder.embed_positions",
"encoder_embedder.norm" => "model.encoder.layernorm_embedding",
"encoder.blocks.{n}.self_attention.query" => "model.encoder.layers.{n}.self_attn.q_proj",
Expand All @@ -667,7 +670,8 @@ defmodule Bumblebee.Text.Bart do
"encoder.blocks.{n}.ffn.intermediate" => "model.encoder.layers.{n}.fc1",
"encoder.blocks.{n}.ffn.output" => "model.encoder.layers.{n}.fc2",
"encoder.blocks.{n}.output_norm" => "model.encoder.layers.{n}.final_layer_norm",
"decoder_embedder.token_embedding" => "model.decoder.embed_tokens",
"decoder_embedder.token_embedding" =>
if(tie_word_embeddings, do: "model.shared", else: "model.decoder.embed_tokens"),
"decoder_embedder.position_embedding" => "model.decoder.embed_positions",
"decoder_embedder.norm" => "model.decoder.layernorm_embedding",
"decoder.blocks.{n}.self_attention.query" => "model.decoder.layers.{n}.self_attn.q_proj",
Expand Down
11 changes: 9 additions & 2 deletions lib/bumblebee/text/bert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,14 @@ defmodule Bumblebee.Text.Bert do
end

defimpl Bumblebee.HuggingFace.Transformers.Model do
def params_mapping(_spec) do
def params_mapping(spec) do
language_modeling_head_output =
if Map.get(spec, :tie_word_embeddings, true) do
"bert.embeddings.word_embeddings"
else
"cls.predictions.decoder"
end

%{
"embedder.token_embedding" => "bert.embeddings.word_embeddings",
"embedder.position_embedding" => "bert.embeddings.position_embeddings",
Expand Down Expand Up @@ -655,7 +662,7 @@ defmodule Bumblebee.Text.Bert do
"pooler.output" => "bert.pooler.dense",
"language_modeling_head.dense" => "cls.predictions.transform.dense",
"language_modeling_head.norm" => "cls.predictions.transform.LayerNorm",
"language_modeling_head.output" => "cls.predictions.decoder",
"language_modeling_head.output" => language_modeling_head_output,
"language_modeling_head.bias" => "cls.predictions",
"next_sentence_prediction_head.output" => "cls.seq_relationship",
"sequence_classification_head.output" => "classifier",
Expand Down
Loading