--- 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 %}

BlurrUtil

{% raw %}

Singleton object at 0x7f9cbdb69e90>[source]

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

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

BlurrUtil 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 constant below.

{% raw %}
mh = BlurrUtil()
mh2 = BlurrUtil()
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
18 BartConfig transformers.models.bart.configuration_bart transformers models bart configuration_bart configuration bart NaN
19 BartForCausalLM transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart CausalLM
20 BartForConditionalGeneration transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart ConditionalGeneration
21 BartForQuestionAnswering transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart QuestionAnswering
22 BartForSequenceClassification transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart SequenceClassification
23 BartModel transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart None
24 BartPretrainedModel transformers.models.bart.modeling_bart transformers models bart modeling_bart modeling bart None
25 BartTokenizer transformers.models.bart.tokenization_bart transformers models bart tokenization_bart tokenization bart NaN
{% endraw %}

Provide global helper constant

Users of this library can simply use BLURR to access all the BlurrUtil 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 %}

BlurrUtil.get_tasks[source]

BlurrUtil.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.get_tasks())
print('')
print(BLURR.get_tasks('bart'))
['CTC', 'CausalLM', 'Classification', 'ConditionalGeneration', 'Generation', 'ImageClassification', 'LMHead', 'LMHeadModel', 'MaskedLM', 'MultipleChoice', 'NextSentencePrediction', 'PreTraining', 'QuestionAnswering', 'QuestionAnsweringSimple', 'SequenceClassification', 'TokenClassification']

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

... the architecture

{% raw %}

BlurrUtil.get_architectures[source]

BlurrUtil.get_architectures()

Used to get all the architectures supported by your Transformers install

{% endraw %} {% raw %}
print(BLURR.get_architectures())
['albert', 'albert_fast', 'bart', 'bart_fast', 'barthez', 'barthez_fast', 'bert', 'bert_fast', 'bert_generation', 'bert_japanese', 'bertweet', 'big_bird', 'blenderbot', 'blenderbot_small', 'camembert', 'camembert_fast', 'convbert', 'convbert_fast', 'ctrl', 'deberta', 'deberta_v2', 'distilbert', 'distilbert_fast', 'dpr', 'dpr_fast', 'electra', 'electra_fast', 'encoder_decoder', 'flaubert', 'fsmt', 'funnel', 'funnel_fast', 'gpt2', 'gpt2_fast', 'gpt_neo', 'herbert', 'herbert_fast', 'ibert', 'layoutlm', 'layoutlm_fast', 'led', 'led_fast', 'longformer', 'longformer_fast', 'lxmert', 'lxmert_fast', 'm2m_100', 'marian', 'mbart', 'mbart50', 'mbart50_fast', 'mbart_fast', 'mmbt', 'mobilebert', 'mobilebert_fast', 'mpnet', 'mpnet_fast', 'mt5', 'openai', 'openai_fast', 'pegasus', 'pegasus_fast', 'phobert', 'prophetnet', 'rag', 'reformer', 'reformer_fast', 'retribert', 'retribert_fast', 'roberta', 'roberta_fast', 'speech_to_text', 'squeezebert', 'squeezebert_fast', 't5', 't5_fast', 'tapas', 'tf_albert', 'tf_auto', 'tf_bart', 'tf_bert', 'tf_blenderbot', 'tf_blenderbot_small', 'tf_camembert', 'tf_convbert', 'tf_ctrl', 'tf_distilbert', 'tf_dpr', 'tf_electra', 'tf_flaubert', 'tf_funnel', 'tf_gpt2', 'tf_layoutlm', 'tf_led', 'tf_longformer', 'tf_lxmert', 'tf_marian', 'tf_mbart', 'tf_mobilebert', 'tf_mpnet', 'tf_mt5', 'tf_openai', 'tf_pegasus', 'tf_rag', 'tf_roberta', 'tf_t5', 'tf_transfo_xl', 'tf_xlm', 'tf_xlm_roberta', 'tf_xlnet', 'transfo_xl', 'vit', 'wav2vec2', 'xlm', 'xlm_prophetnet', 'xlm_roberta', 'xlm_roberta_fast', 'xlnet', 'xlnet_fast']
{% endraw %} {% raw %}

BlurrUtil.get_model_architecture[source]

BlurrUtil.get_model_architecture(model_name_or_enum)

Get the architecture for a given model name / enum

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

... the config for that particular task and architecture

{% raw %}

BlurrUtil.get_config[source]

BlurrUtil.get_config(arch)

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

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

... the available tokenizers for that architecture

{% raw %}

BlurrUtil.get_tokenizers[source]

BlurrUtil.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.get_tokenizers('electra'))
[<class 'transformers.models.electra.tokenization_electra.ElectraTokenizer'>]
{% endraw %}

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

{% raw %}

BlurrUtil.get_models[source]

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

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

{% endraw %} {% raw %}
print(L(BLURR.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.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.get_models(task='TokenClassification')[:5])
[<class 'transformers.models.albert.modeling_albert.AlbertForTokenClassification'>, <class 'transformers.models.bert.modeling_bert.BertForTokenClassification'>, <class 'transformers.models.big_bird.modeling_big_bird.BigBirdForTokenClassification'>, <class 'transformers.models.camembert.modeling_camembert.CamembertForTokenClassification'>, <class 'transformers.models.convbert.modeling_convbert.ConvBertForTokenClassification'>]
{% endraw %} {% raw %}
print(BLURR.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.bart: 3>, <HF_ARCHITECTURES.bart_fast: 4>, <HF_ARCHITECTURES.barthez: 5>]
{% endraw %} {% raw %}
{% endraw %} {% raw %}
print('--- all tasks ---')
print(L(HF_TASKS))
--- all tasks ---
[<HF_TASKS_ALL.CTC: 1>, <HF_TASKS_ALL.CausalLM: 2>, <HF_TASKS_ALL.Classification: 3>, <HF_TASKS_ALL.ConditionalGeneration: 4>, <HF_TASKS_ALL.Generation: 5>, <HF_TASKS_ALL.ImageClassification: 6>, <HF_TASKS_ALL.LMHead: 7>, <HF_TASKS_ALL.LMHeadModel: 8>, <HF_TASKS_ALL.MaskedLM: 9>, <HF_TASKS_ALL.MultipleChoice: 10>, <HF_TASKS_ALL.NextSentencePrediction: 11>, <HF_TASKS_ALL.PreTraining: 12>, <HF_TASKS_ALL.QuestionAnswering: 13>, <HF_TASKS_ALL.QuestionAnsweringSimple: 14>, <HF_TASKS_ALL.SequenceClassification: 15>, <HF_TASKS_ALL.TokenClassification: 16>]
{% endraw %} {% raw %}
HF_TASKS.Classification
<HF_TASKS_ALL.Classification: 3>
{% endraw %}

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

{% raw %}

BlurrUtil.get_classes_for_model[source]

BlurrUtil.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.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.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 %}

BlurrUtil.get_hf_objects[source]

BlurrUtil.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.get_hf_objects("bert-base-cased-finetuned-mrpc",
                                                      model_cls=AutoModelForMaskedLM)

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.get_hf_objects("fmikaelian/flaubert-base-uncased-squad",
                                                      model_cls=AutoModelForQuestionAnswering)

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.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