--- title: utils keywords: fastai sidebar: home_sidebar summary: "Various utility functions used by the blurr package." description: "Various utility functions used by the blurr package." nb_path: "nbs/00_utils.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
torch.cuda.set_device(1)
print(f'Using GPU #{torch.cuda.current_device()}: {torch.cuda.get_device_name()}')
Using GPU #1: GeForce GTX 1080 Ti
{% endraw %} {% raw %}

str_to_class[source]

str_to_class(classname)

converts string representation to class

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class Singleton[source]

Singleton()

{% endraw %} {% raw %}
{% endraw %}

Singleton functions as python decorator. Use this above any class to turn that class into a singleton (see here for more info on the singleton pattern).

{% raw %}
@Singleton
class TestSingleton: pass

a = TestSingleton()
b = TestSingleton()
test_eq(a,b)
{% endraw %}

ModelHelper

{% raw %}

Singleton object at 0x7f64c3907190>[source]

Singleton object at 0x7f64c3907190>(*args, **kwargs)

{% endraw %} {% raw %}
{% endraw %}

ModelHelper is a Singleton (there exists only one instance, and the same instance is returned upon subsequent instantiation requests). You can get at via the BLURR_MODEL_HELPER constant below.

{% raw %}
mh = ModelHelper()
mh2 = ModelHelper()
test_eq(mh, mh2)
{% endraw %} {% raw %}
display_df(mh._df.head(20))
class_name module module_part_0 module_part_1 module_part_2 module_part_3 functional_area arch model_task
3 AdaptiveEmbedding transformers.models.transfo_xl.modeling_transfo_xl transformers models transfo_xl modeling_transfo_xl modeling transfo_xl None
4 AlbertConfig transformers.models.albert.configuration_albert transformers models albert configuration_albert configuration albert NaN
5 AlbertForMaskedLM transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert MaskedLM
6 AlbertForMultipleChoice transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert MultipleChoice
7 AlbertForPreTraining transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert PreTraining
8 AlbertForQuestionAnswering transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert QuestionAnswering
9 AlbertForSequenceClassification transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert SequenceClassification
10 AlbertForTokenClassification transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert TokenClassification
11 AlbertModel transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert None
12 AlbertPreTrainedModel transformers.models.albert.modeling_albert transformers models albert modeling_albert modeling albert None
13 AlbertTokenizer transformers.models.albert.tokenization_albert transformers models albert tokenization_albert tokenization albert NaN
14 AlbertTokenizerFast transformers.models.albert.tokenization_albert_fast transformers models albert tokenization_albert_fast tokenization albert_fast NaN
15 AutoConfig transformers.models.auto.configuration_auto transformers models auto configuration_auto configuration auto NaN
16 AutoModel transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto None
17 AutoModelForCausalLM transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto CausalLM
18 AutoModelForMaskedLM transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto MaskedLM
19 AutoModelForMultipleChoice transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto MultipleChoice
20 AutoModelForNextSentencePrediction transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto NextSentencePrediction
21 AutoModelForPreTraining transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto PreTraining
22 AutoModelForQuestionAnswering transformers.models.auto.modeling_auto transformers models auto modeling_auto modeling auto QuestionAnswering
{% endraw %}

Provide global helper constant

Users of this library can simply use BLURR_MODEL_HELPER to access all the ModelHelper capabilities without having to fetch an instance themselves.

{% raw %}
{% endraw %}

Here's how you can get at the core huggingface objects you need to work with ...

... the task

{% raw %}

ModelHelper.get_tasks[source]

ModelHelper.get_tasks(arch=None)

Get the type of tasks for which there is a custom model for (optional: by architecture). There are a number of customized models built for specific tasks like token classification, question/answering, LM, etc....

{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_tasks())
print('')
print(BLURR_MODEL_HELPER.get_tasks('bart'))
['CausalLM', 'Classification', 'ConditionalGeneration', 'Generation', 'LMHead', 'LMHeadModel', 'MaskedLM', 'MultipleChoice', 'NextSentencePrediction', 'PreTraining', 'QuestionAnswering', 'QuestionAnsweringSimple', 'Seq2SeqLM', 'SequenceClassification', 'TokenClassification']

['ConditionalGeneration', 'QuestionAnswering', 'SequenceClassification']
{% endraw %}

... the architecture

{% raw %}

ModelHelper.get_architectures[source]

ModelHelper.get_architectures()

Used to get all the architectures supported by your Transformers install

{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_architectures())
['albert', 'albert_fast', 'auto', 'bart', 'bart_fast', 'bert', 'bert_fast', 'bert_generation', 'bert_japanese', 'bertweet', 'blenderbot', 'camembert', 'camembert_fast', 'ctrl', 'deberta', 'distilbert', 'distilbert_fast', 'dpr', 'dpr_fast', 'electra', 'electra_fast', 'encoder_decoder', 'flaubert', 'fsmt', 'funnel', 'funnel_fast', 'gpt2', 'gpt2_fast', 'herbert', 'herbert_fast', 'layoutlm', 'layoutlm_fast', 'longformer', 'longformer_fast', 'lxmert', 'lxmert_fast', 'marian', 'mbart', 'mbart_fast', 'mmbt', 'mobilebert', 'mobilebert_fast', 'mt5', 'openai', 'openai_fast', 'pegasus', 'pegasus_fast', 'phobert', 'prophetnet', 'rag', 'reformer', 'reformer_fast', 'retribert', 'retribert_fast', 'roberta', 'roberta_fast', 'squeezebert', 'squeezebert_fast', 't5', 't5_fast', 'transfo_xl', 'xlm', 'xlm_prophetnet', 'xlm_roberta', 'xlm_roberta_fast', 'xlnet', 'xlnet_fast']
{% endraw %} {% raw %}

ModelHelper.get_model_architecture[source]

ModelHelper.get_model_architecture(model_name_or_enum)

Get the architecture for a given model name / enum

{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_model_architecture('RobertaForSequenceClassification'))
roberta
{% endraw %}

... the config for that particular task and architecture

{% raw %}

ModelHelper.get_config[source]

ModelHelper.get_config(arch)

Used the locate the name of the configuration class for a given architecture

{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_config('bert'))
<class 'transformers.models.bert.configuration_bert.BertConfig'>
{% endraw %}

... the available tokenizers for that architecture

{% raw %}

ModelHelper.get_tokenizers[source]

ModelHelper.get_tokenizers(arch)

Used to get the available huggingface tokenizers for a given architecture. Note: There may be multiple tokenizers and so this returns a list.

{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_tokenizers('electra'))
[<class 'transformers.models.electra.tokenization_electra.ElectraTokenizer'>]
{% endraw %}

... and lastly the models (optionally for a given task and/or architecture)

{% raw %}

ModelHelper.get_models[source]

ModelHelper.get_models(arch=None, task=None)

The transformer models available for use (optional: by architecture | task)

{% endraw %} {% raw %}
print(L(BLURR_MODEL_HELPER.get_models())[:5])
[<class 'transformers.models.transfo_xl.modeling_transfo_xl.AdaptiveEmbedding'>, <class 'transformers.models.albert.modeling_albert.AlbertForMaskedLM'>, <class 'transformers.models.albert.modeling_albert.AlbertForMultipleChoice'>, <class 'transformers.models.albert.modeling_albert.AlbertForPreTraining'>, <class 'transformers.models.albert.modeling_albert.AlbertForQuestionAnswering'>]
{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_models(arch='bert')[:5])
[<class 'transformers.models.bert.modeling_bert.BertForMaskedLM'>, <class 'transformers.models.bert.modeling_bert.BertForMultipleChoice'>, <class 'transformers.models.bert.modeling_bert.BertForNextSentencePrediction'>, <class 'transformers.models.bert.modeling_bert.BertForPreTraining'>, <class 'transformers.models.bert.modeling_bert.BertForQuestionAnswering'>]
{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_models(task='TokenClassification')[:5])
[<class 'transformers.models.albert.modeling_albert.AlbertForTokenClassification'>, <class 'transformers.models.auto.modeling_auto.AutoModelForTokenClassification'>, <class 'transformers.models.bert.modeling_bert.BertForTokenClassification'>, <class 'transformers.models.camembert.modeling_camembert.CamembertForTokenClassification'>, <class 'transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification'>]
{% endraw %} {% raw %}
print(BLURR_MODEL_HELPER.get_models(arch='bert', task='TokenClassification'))
[<class 'transformers.models.bert.modeling_bert.BertForTokenClassification'>]
{% endraw %}

Here we define some helpful enums to make it easier to get at the architecture and task you're looking for.

{% raw %}
{% endraw %} {% raw %}
print(L(HF_ARCHITECTURES)[:5])
[<HF_ARCHITECTURES.albert: 1>, <HF_ARCHITECTURES.albert_fast: 2>, <HF_ARCHITECTURES.auto: 3>, <HF_ARCHITECTURES.bart: 4>, <HF_ARCHITECTURES.bart_fast: 5>]
{% endraw %} {% raw %}
{% endraw %} {% raw %}
print('--- all tasks ---')
print(L(HF_TASKS_ALL))
print('\n--- auto only ---')
print(L(HF_TASKS_AUTO))
--- all tasks ---
[<HF_TASKS_ALL.CausalLM: 1>, <HF_TASKS_ALL.Classification: 2>, <HF_TASKS_ALL.ConditionalGeneration: 3>, <HF_TASKS_ALL.Generation: 4>, <HF_TASKS_ALL.LMHead: 5>, <HF_TASKS_ALL.LMHeadModel: 6>, <HF_TASKS_ALL.MaskedLM: 7>, <HF_TASKS_ALL.MultipleChoice: 8>, <HF_TASKS_ALL.NextSentencePrediction: 9>, <HF_TASKS_ALL.PreTraining: 10>, <HF_TASKS_ALL.QuestionAnswering: 11>, <HF_TASKS_ALL.QuestionAnsweringSimple: 12>, <HF_TASKS_ALL.Seq2SeqLM: 13>, <HF_TASKS_ALL.SequenceClassification: 14>, <HF_TASKS_ALL.TokenClassification: 15>]

--- auto only ---
[<HF_TASKS_AUTO.CausalLM: 1>, <HF_TASKS_AUTO.LMHead: 2>, <HF_TASKS_AUTO.MaskedLM: 3>, <HF_TASKS_AUTO.MultipleChoice: 4>, <HF_TASKS_AUTO.NextSentencePrediction: 5>, <HF_TASKS_AUTO.PreTraining: 6>, <HF_TASKS_AUTO.QuestionAnswering: 7>, <HF_TASKS_AUTO.Seq2SeqLM: 8>, <HF_TASKS_AUTO.SequenceClassification: 9>, <HF_TASKS_AUTO.TokenClassification: 10>]
{% endraw %} {% raw %}
HF_TASKS_ALL.Classification
<HF_TASKS_ALL.Classification: 2>
{% endraw %}

BLURR_MODEL_HELPER.get_classes_for_model can be used to get the config, tokenizer, and model classes you want

{% raw %}

ModelHelper.get_classes_for_model[source]

ModelHelper.get_classes_for_model(model_name_or_cls)

Get tokenizers, config, and model for a given model name / class

{% endraw %} {% raw %}
config, tokenizers, model = BLURR_MODEL_HELPER.get_classes_for_model('RobertaForSequenceClassification')

print(config)
print(tokenizers[0])
print(model)
<class 'transformers.models.roberta.configuration_roberta.RobertaConfig'>
<class 'transformers.models.roberta.tokenization_roberta.RobertaTokenizer'>
<class 'transformers.models.roberta.modeling_roberta.RobertaForSequenceClassification'>
{% endraw %} {% raw %}
config, tokenizers, model = BLURR_MODEL_HELPER.get_classes_for_model(DistilBertModel)

print(config)
print(tokenizers[0])
print(model)
<class 'transformers.models.distilbert.configuration_distilbert.DistilBertConfig'>
<class 'transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer'>
<class 'transformers.models.distilbert.modeling_distilbert.DistilBertModel'>
{% endraw %}

Methods for loading pre-trained (configs, tokenizer, model) hugginface objects

{% raw %}

ModelHelper.get_hf_objects[source]

ModelHelper.get_hf_objects(pretrained_model_name_or_path, task=None, config=None, tokenizer_cls=None, model_cls=None, config_kwargs={}, tokenizer_kwargs={}, model_kwargs={}, cache_dir=None)

Returns the architecture (str), config (obj), tokenizer (obj), and model (obj) given at minimum a pre-trained model name or path. Specify a task to ensure the right "AutoModelFor" is used to create the model.

Optionally, you can pass a config (obj), tokenizer (class), and/or model (class) (along with any related kwargs for each) to get as specific as you want w/r/t what huggingface objects are returned.

{% endraw %} {% raw %}
arch, config, tokenizer, model = BLURR_MODEL_HELPER.get_hf_objects("bert-base-cased-finetuned-mrpc",
                                                                   task=HF_TASKS_AUTO.MaskedLM)

print(arch)
print(type(config))
print(type(tokenizer))
print(type(model))
bert
<class 'transformers.models.bert.configuration_bert.BertConfig'>
<class 'transformers.models.bert.tokenization_bert_fast.BertTokenizerFast'>
<class 'transformers.models.bert.modeling_bert.BertForMaskedLM'>
{% endraw %} {% raw %}
arch, tokenizer, config, model = BLURR_MODEL_HELPER.get_hf_objects("fmikaelian/flaubert-base-uncased-squad",
                                                                   task=HF_TASKS_AUTO.QuestionAnswering)

print(arch)
print(type(config))
print(type(tokenizer))
print(type(model))
flaubert
<class 'transformers.models.flaubert.tokenization_flaubert.FlaubertTokenizer'>
<class 'transformers.models.flaubert.configuration_flaubert.FlaubertConfig'>
<class 'transformers.models.flaubert.modeling_flaubert.FlaubertForQuestionAnsweringSimple'>
{% endraw %} {% raw %}
arch, tokenizer, config, model = BLURR_MODEL_HELPER.get_hf_objects("bert-base-cased-finetuned-mrpc",
                                                                   config=None,
                                                                   tokenizer_cls=BertTokenizer, 
                                                                   model_cls=BertForNextSentencePrediction)
print(arch)
print(type(config))
print(type(tokenizer))
print(type(model))
bert
<class 'transformers.models.bert.tokenization_bert.BertTokenizer'>
<class 'transformers.models.bert.configuration_bert.BertConfig'>
<class 'transformers.models.bert.modeling_bert.BertForNextSentencePrediction'>
{% endraw %}

Cleanup