Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bb01a4373 | ||
|
|
100bbcb786 |
@@ -56,7 +56,6 @@ from ..utils_squad import (
|
|||||||
write_predictions,
|
write_predictions,
|
||||||
write_predictions_extended,
|
write_predictions_extended,
|
||||||
)
|
)
|
||||||
|
|
||||||
# The follwing import is the official SQuAD evaluation script (2.0).
|
# The follwing import is the official SQuAD evaluation script (2.0).
|
||||||
# You can remove it from the dependencies if you are using this script outside of the library
|
# You can remove it from the dependencies if you are using this script outside of the library
|
||||||
# We've added it here for automated tests (see examples/test_examples.py file)
|
# We've added it here for automated tests (see examples/test_examples.py file)
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class TextDataset(Dataset):
|
|||||||
assert os.path.isfile(file_path)
|
assert os.path.isfile(file_path)
|
||||||
directory, filename = os.path.split(file_path)
|
directory, filename = os.path.split(file_path)
|
||||||
cached_features_file = os.path.join(
|
cached_features_file = os.path.join(
|
||||||
directory, args.model_name_or_path + "_cached_lm_" + str(block_size) + "_" + filename
|
directory, args.model_name_or_path + "_cached_lm_" + str(block_size) + "_" + filename + ".bin"
|
||||||
)
|
)
|
||||||
|
|
||||||
if os.path.exists(cached_features_file) and not args.overwrite_cache:
|
if os.path.exists(cached_features_file) and not args.overwrite_cache:
|
||||||
@@ -625,6 +625,12 @@ def main():
|
|||||||
args.config_name if args.config_name else args.model_name_or_path,
|
args.config_name if args.config_name else args.model_name_or_path,
|
||||||
cache_dir=args.cache_dir if args.cache_dir else None,
|
cache_dir=args.cache_dir if args.cache_dir else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Desactivate past output for now (reduce memory)
|
||||||
|
# we use it only for GPT/GPT2 generation or specific XLNet/Transformer-XL trainings (not implemented currently)
|
||||||
|
if hasattr(config, "output_past"):
|
||||||
|
config.output_past = False
|
||||||
|
|
||||||
tokenizer = tokenizer_class.from_pretrained(
|
tokenizer = tokenizer_class.from_pretrained(
|
||||||
args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,
|
args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,
|
||||||
do_lower_case=args.do_lower_case,
|
do_lower_case=args.do_lower_case,
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, Open
|
|||||||
from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig
|
from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig
|
||||||
from .configuration_t5 import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, T5Config
|
from .configuration_t5 import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, T5Config
|
||||||
from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
|
from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
|
||||||
|
|
||||||
# Configurations
|
# Configurations
|
||||||
from .configuration_utils import PretrainedConfig
|
from .configuration_utils import PretrainedConfig
|
||||||
from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig
|
from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig
|
||||||
@@ -56,7 +55,6 @@ from .data import (
|
|||||||
xnli_processors,
|
xnli_processors,
|
||||||
xnli_tasks_num_labels,
|
xnli_tasks_num_labels,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Files and general utilities
|
# Files and general utilities
|
||||||
from .file_utils import (
|
from .file_utils import (
|
||||||
CONFIG_NAME,
|
CONFIG_NAME,
|
||||||
@@ -73,10 +71,8 @@ from .file_utils import (
|
|||||||
is_tf_available,
|
is_tf_available,
|
||||||
is_torch_available,
|
is_torch_available,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Model Cards
|
# Model Cards
|
||||||
from .modelcard import ModelCard
|
from .modelcard import ModelCard
|
||||||
|
|
||||||
# TF 2.0 <=> PyTorch conversion utilities
|
# TF 2.0 <=> PyTorch conversion utilities
|
||||||
from .modeling_tf_pytorch_utils import (
|
from .modeling_tf_pytorch_utils import (
|
||||||
convert_tf_weight_name_to_pt_weight_name,
|
convert_tf_weight_name_to_pt_weight_name,
|
||||||
@@ -87,7 +83,6 @@ from .modeling_tf_pytorch_utils import (
|
|||||||
load_tf2_model_in_pytorch_model,
|
load_tf2_model_in_pytorch_model,
|
||||||
load_tf2_weights_in_pytorch_model,
|
load_tf2_weights_in_pytorch_model,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Pipelines
|
# Pipelines
|
||||||
from .pipelines import (
|
from .pipelines import (
|
||||||
CsvPipelineDataFormat,
|
CsvPipelineDataFormat,
|
||||||
@@ -113,7 +108,6 @@ from .tokenization_openai import OpenAIGPTTokenizer
|
|||||||
from .tokenization_roberta import RobertaTokenizer
|
from .tokenization_roberta import RobertaTokenizer
|
||||||
from .tokenization_t5 import T5Tokenizer
|
from .tokenization_t5 import T5Tokenizer
|
||||||
from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer
|
from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer
|
||||||
|
|
||||||
# Tokenizers
|
# Tokenizers
|
||||||
from .tokenization_utils import PreTrainedTokenizer
|
from .tokenization_utils import PreTrainedTokenizer
|
||||||
from .tokenization_xlm import XLMTokenizer
|
from .tokenization_xlm import XLMTokenizer
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ class Attention(nn.Module):
|
|||||||
def __init__(self, nx, n_ctx, config, scale=False):
|
def __init__(self, nx, n_ctx, config, scale=False):
|
||||||
super(Attention, self).__init__()
|
super(Attention, self).__init__()
|
||||||
self.output_attentions = config.output_attentions
|
self.output_attentions = config.output_attentions
|
||||||
|
self.output_past = config.output_past
|
||||||
|
|
||||||
n_state = nx # in Attention: n_state=768 (nx=n_embd)
|
n_state = nx # in Attention: n_state=768 (nx=n_embd)
|
||||||
# [switch nx => n_state from Block to Attention to keep identical to TF implem]
|
# [switch nx => n_state from Block to Attention to keep identical to TF implem]
|
||||||
@@ -159,10 +160,11 @@ class Attention(nn.Module):
|
|||||||
if head_mask is not None:
|
if head_mask is not None:
|
||||||
w = w * head_mask
|
w = w * head_mask
|
||||||
|
|
||||||
outputs = [torch.matmul(w, v)]
|
x = torch.matmul(w, v)
|
||||||
if self.output_attentions:
|
if self.output_attentions:
|
||||||
outputs.append(w)
|
return (x, w)
|
||||||
return outputs
|
else:
|
||||||
|
return (x,)
|
||||||
|
|
||||||
def merge_heads(self, x):
|
def merge_heads(self, x):
|
||||||
x = x.permute(0, 2, 1, 3).contiguous()
|
x = x.permute(0, 2, 1, 3).contiguous()
|
||||||
@@ -187,7 +189,8 @@ class Attention(nn.Module):
|
|||||||
past_key, past_value = layer_past[0].transpose(-2, -1), layer_past[1] # transpose back cf below
|
past_key, past_value = layer_past[0].transpose(-2, -1), layer_past[1] # transpose back cf below
|
||||||
key = torch.cat((past_key, key), dim=-1)
|
key = torch.cat((past_key, key), dim=-1)
|
||||||
value = torch.cat((past_value, value), dim=-2)
|
value = torch.cat((past_value, value), dim=-2)
|
||||||
present = torch.stack((key.transpose(-2, -1), value)) # transpose to have same shapes for stacking
|
if self.output_past:
|
||||||
|
present = torch.stack((key.transpose(-2, -1), value)) # transpose to have same shapes for stacking
|
||||||
|
|
||||||
attn_outputs = self._attn(query, key, value, attention_mask, head_mask)
|
attn_outputs = self._attn(query, key, value, attention_mask, head_mask)
|
||||||
a = attn_outputs[0]
|
a = attn_outputs[0]
|
||||||
@@ -196,8 +199,11 @@ class Attention(nn.Module):
|
|||||||
a = self.c_proj(a)
|
a = self.c_proj(a)
|
||||||
a = self.resid_dropout(a)
|
a = self.resid_dropout(a)
|
||||||
|
|
||||||
outputs = [a, present] + attn_outputs[1:]
|
if self.output_past:
|
||||||
return outputs # a, present, (attentions)
|
outputs = (a, present) + attn_outputs[1:]
|
||||||
|
else:
|
||||||
|
outputs = (a,) + attn_outputs[1:]
|
||||||
|
return outputs # a, (present), (attentions)
|
||||||
|
|
||||||
|
|
||||||
class MLP(nn.Module):
|
class MLP(nn.Module):
|
||||||
@@ -228,14 +234,14 @@ class Block(nn.Module):
|
|||||||
output_attn = self.attn(
|
output_attn = self.attn(
|
||||||
self.ln_1(x), layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask
|
self.ln_1(x), layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask
|
||||||
)
|
)
|
||||||
a = output_attn[0] # output_attn: a, present, (attentions)
|
a = output_attn[0] # output_attn: a, (present), (attentions)
|
||||||
|
|
||||||
x = x + a
|
x = x + a
|
||||||
m = self.mlp(self.ln_2(x))
|
m = self.mlp(self.ln_2(x))
|
||||||
x = x + m
|
x = x + m
|
||||||
|
|
||||||
outputs = [x] + output_attn[1:]
|
outputs = (x,) + output_attn[1:]
|
||||||
return outputs # x, present, (attentions)
|
return outputs # x, (present), (attentions)
|
||||||
|
|
||||||
|
|
||||||
class GPT2PreTrainedModel(PreTrainedModel):
|
class GPT2PreTrainedModel(PreTrainedModel):
|
||||||
@@ -465,7 +471,7 @@ class GPT2Model(GPT2PreTrainedModel):
|
|||||||
output_shape = input_shape + (hidden_states.size(-1),)
|
output_shape = input_shape + (hidden_states.size(-1),)
|
||||||
|
|
||||||
presents = ()
|
presents = ()
|
||||||
all_attentions = []
|
all_attentions = ()
|
||||||
all_hidden_states = ()
|
all_hidden_states = ()
|
||||||
for i, (block, layer_past) in enumerate(zip(self.h, past)):
|
for i, (block, layer_past) in enumerate(zip(self.h, past)):
|
||||||
if self.output_hidden_states:
|
if self.output_hidden_states:
|
||||||
@@ -475,12 +481,12 @@ class GPT2Model(GPT2PreTrainedModel):
|
|||||||
hidden_states, layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask[i]
|
hidden_states, layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask[i]
|
||||||
)
|
)
|
||||||
|
|
||||||
hidden_states, present = outputs[:2]
|
hidden_states = outputs[0]
|
||||||
if self.output_past:
|
if self.output_past:
|
||||||
presents = presents + (present,)
|
presents = presents + (outputs[1],)
|
||||||
|
|
||||||
if self.output_attentions:
|
if self.output_attentions:
|
||||||
all_attentions.append(outputs[2])
|
all_attentions = all_attentions + (outputs[2 if self.output_past else 1],)
|
||||||
|
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ from utils_squad import (
|
|||||||
write_predictions,
|
write_predictions,
|
||||||
write_predictions_extended,
|
write_predictions_extended,
|
||||||
)
|
)
|
||||||
|
|
||||||
# The follwing import is the official SQuAD evaluation script (2.0).
|
# The follwing import is the official SQuAD evaluation script (2.0).
|
||||||
# You can remove it from the dependencies if you are using this script outside of the library
|
# You can remove it from the dependencies if you are using this script outside of the library
|
||||||
# We've added it here for automated tests (see examples/test_examples.py file)
|
# We've added it here for automated tests (see examples/test_examples.py file)
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import logging
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
from transformers.tokenization_bert import BasicTokenizer, whitespace_tokenize
|
from transformers.tokenization_bert import BasicTokenizer, whitespace_tokenize
|
||||||
|
|
||||||
# Required by XLNet evaluation method to compute optimal threshold (see write_predictions_extended() method)
|
# Required by XLNet evaluation method to compute optimal threshold (see write_predictions_extended() method)
|
||||||
from utils_squad_evaluate import find_all_best_thresh_v2, get_raw_scores, make_qid_to_has_ans
|
from utils_squad_evaluate import find_all_best_thresh_v2, get_raw_scores, make_qid_to_has_ans
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user