openai-community/gpt2 throws error with SpaCy framework

#79
by csesaswati - opened

I want to use this model with a custom dataset. I used Spacy for the task. I created a config.cfg to train the model. The contain of config.cfg file is as follows

[paths]
train = null
dev = null
vectors = null
init_tok2vec = null

[system]
gpu_allocator = "pytorch"
seed = 0

[nlp]
lang = "en"
pipeline = ["transformer","ner"]
batch_size = 128
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"@tokenizers":"spacy.Tokenizer.v1"}
vectors = {"@vectors":"spacy.Vectors.v1"}

[components]

[components.ner]
factory = "ner"
incorrect_spans_key = null
moves = null
scorer = {"@scorers":"spacy.ner_scorer.v1"}
update_with_oracle_cut_size = 100

[components.ner.model]
@architectures = "spacy.TransitionBasedParser.v2"
state_type = "ner"
extra_state_tokens = false
hidden_width = 64
maxout_pieces = 2
use_upper = false
nO = null

[components.ner.model.tok2vec]
@architectures = "spacy-transformers.TransformerListener.v1"
grad_factor = 1.0
pooling = {"@layers":"reduce_mean.v1"}
upstream = "*"

[components.transformer]
factory = "transformer"
max_batch_items = 4096
set_extra_annotations = {"@annotation_setters":"spacy-transformers.null_annotation_setter.v1"}

[components.transformer.model]
@architectures = "spacy-transformers.TransformerModel.v3"
name = "openai-community/gpt2"
mixed_precision = false

[components.transformer.model.get_spans]
@span_getters = "spacy-transformers.strided_spans.v1"
window = 128
stride = 96

[components.transformer.model.grad_scaler_config]

[components.transformer.model.tokenizer_config]
use_fast = true

[components.transformer.model.transformer_config]

[corpora]

[corpora.dev]
@readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null

[corpora.train]
@readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null

[training]
accumulate_gradient = 3
dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
seed = ${system.seed}
gpu_allocator = ${system.gpu_allocator}
dropout = 0.1
patience = 1600
max_epochs = 0
max_steps = 20000
eval_frequency = 200
frozen_components = []
annotating_components = []
before_to_disk = null
before_update = null

[training.batcher]
@batchers = "spacy.batch_by_padded.v1"
discard_oversize = true
size = 2000
buffer = 256
get_length = null

[training.logger]
@loggers = "spacy.ConsoleLogger.v1"
progress_bar = false

[training.optimizer]
@optimizers = "Adam.v1"
beta1 = 0.9
beta2 = 0.999
L2_is_weight_decay = true
L2 = 0.01
grad_clip = 1.0
use_averages = false
eps = 0.00000001

[training.optimizer.learn_rate]
@schedules = "warmup_linear.v1"
warmup_steps = 250
total_steps = 20000
initial_rate = 0.00005

[training.score_weights]
ents_f = 1.0
ents_p = 0.0
ents_r = 0.0
ents_per_type = null

[pretraining]

[initialize]
vectors = ${paths.vectors}
init_tok2vec = ${paths.init_tok2vec}
vocab_data = null
lookups = null
before_init = null
after_init = null

[initialize.components]

[initialize.tokenizer]

When I used the following command to run the model

!python -m spacy train config.cfg --output ./ --paths.train ./train.spacy --paths.dev ./dev.spacy --gpu-id 0

I received an error as follows

========================== Initializing pipeline ===========================
config.json: 100% 665/665 [00:00<00:00, 3.75MB/s]
vocab.json: 100% 1.04M/1.04M [00:00<00:00, 3.18MB/s]
merges.txt: 100% 456k/456k [00:00<00:00, 1.85MB/s]
tokenizer.json: 100% 1.36M/1.36M [00:00<00:00, 3.95MB/s]
model.safetensors: 100% 548M/548M [00:01<00:00, 296MB/s]
Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/dist-packages/spacy/__main__.py", line 4, in
setup_cli()
File "/usr/local/lib/python3.10/dist-packages/spacy/cli/_util.py", line 87, in setup_cli
command(prog_name=COMMAND)
File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1157, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/typer/core.py", line 778, in main
return _main(
File "/usr/local/lib/python3.10/dist-packages/typer/core.py", line 216, in _main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/typer/main.py", line 683, in wrapper
return callback(**use_params) # type: ignore
File "/usr/local/lib/python3.10/dist-packages/spacy/cli/train.py", line 54, in train_cli
train(config_path, output_path, use_gpu=use_gpu, overrides=overrides)
File "/usr/local/lib/python3.10/dist-packages/spacy/cli/train.py", line 81, in train
nlp = init_nlp(config, use_gpu=use_gpu)
File "/usr/local/lib/python3.10/dist-packages/spacy/training/initialize.py", line 95, in init_nlp
nlp.initialize(lambda: train_corpus(nlp), sgd=optimizer)
File "/usr/local/lib/python3.10/dist-packages/spacy/language.py", line 1349, in initialize
proc.initialize(get_examples, nlp=self, **p_settings)
File "/usr/local/lib/python3.10/dist-packages/spacy_transformers/pipeline_component.py", line 351, in initialize
self.model.initialize(X=docs)
File "/usr/local/lib/python3.10/dist-packages/thinc/model.py", line 318, in initialize
self.init(self, X=X, Y=Y)
File "/usr/local/lib/python3.10/dist-packages/spacy_transformers/layers/transformer_model.py", line 144, in init
token_data = huggingface_tokenize(tokenizer, [span.text for span in flat_spans])
File "/usr/local/lib/python3.10/dist-packages/spacy_transformers/layers/transformer_model.py", line 286, in huggingface_tokenize
token_data = tokenizer(
File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 2798, in call
encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs)
File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 2884, in _call_one
return self.batch_encode_plus(
File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 3066, in batch_encode_plus
padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
File "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py", line 2703, in _get_padding_truncation_strategies
raise ValueError(
ValueError: Asking to pad but the tokenizer does not have a padding token. Please select a token to use as pad_token (tokenizer.pad_token = tokenizer.eos_token e.g.) or add a new pad token via tokenizer.add_special_tokens({'pad_token': '[PAD]'}).

I used Google Colab and T4 GPU of Google Colab for the whole process.

Hi @csesaswati
Thanks for the issue !
Have you tried to call tokenizer.pad_token = tokenizer.eos_token before launching the training - perhaps the training protocol needs to create instances of batched input thus the error. Can you try that out?

Thank you for the reply. I changed the config.cfg file as follows

[nlp]
lang = "en"
pipeline = ["transformer","ner"]
batch_size = 128
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"@tokenizers":"spacy.Tokenizer.v1"}
vectors = {"@vectors":"spacy.Vectors.v1"}
tokenizer.pad_token = tokenizer.eos_token

I got an error

=========================== Initializing pipeline ===========================
✘ Config validation error
nlp -> tokenizer.pad_token extra fields not permitted
{'lang': 'en', 'pipeline': ['transformer', 'ner'], 'batch_size': 128, 'disabled': [], 'before_creation': None, 'after_creation': None, 'after_pipeline_creation': None, 'tokenizer': {'@tokenizers': 'spacy.Tokenizer.v1'}, 'vectors': {'@vectors': 'spacy.Vectors.v1'}, 'tokenizer.pad_token': 'tokenizer.eos_token'}

Can you please let me know how can I include tokenizer.pad_token = tokenizer.eos_token?

Thanks for getting back @csesaswati !
I understand better now, you are using the spacy CLI - I would recommend to open an issue at: https://github.com/explosion/spaCy and provide your cfg there :) ! you can also tag me on GitHub on that issue if I can be at any help for the spacy devs, my GH handle is @younesbelkada

Sign up or log in to comment