vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
0.04804328,
-0.0055972408,
-0.050598774,
0.027088232,
0.018058822,
-0.022900062,
0.00040950012,
-0.047503788,
-0.004617635,
0.053466607,
0.006388734,
-0.007964622,
0.019222992,
-0.07337106,
-0.01554592,
0.019492738,
0.016979836,
-0.02987088,
-0.046254434,
0.0070772977,
0.0262... | Using DeepSpeed and Megatron to Train Megatron-Turing NLG 530B, A Large-Scale Generative Language Model
🤗 Transformers status: not yet implemented, since we have no PP and TP.
FlexFlow
FlexFlow also solves the parallelization problem in a slightly different approach.
Paper: "Beyond Data and Model Parallelism for Deep... |
[
0.044338454,
0.005567112,
0.02076593,
0.001979142,
-0.015436245,
-0.021942431,
-0.0042276033,
-0.006786136,
-0.018923221,
0.06395623,
-0.021871557,
0.004103575,
0.030050358,
-0.043799814,
0.0064990986,
0.052049488,
0.007966179,
-0.012487908,
-0.054572582,
0.00021992928,
0.019... |
Here it's important to see how DP rank 0 doesn't see GPU2 and DP rank 1 doesn't see GPU3. To DP there is just GPUs 0
and 1 where it feeds data as if there were just 2 GPUs. GPU0 "secretly" offloads some of its load to GPU2 using PP.
And GPU1 does the same by enlisting GPU3 to its aid.
Since each dimension requires ... |
[
0.015846463,
-0.0172742,
-0.014107735,
-0.0038308578,
0.016864257,
-0.01812236,
0.028059974,
-0.013556431,
-0.0093509685,
0.08125377,
0.029261535,
-0.003940412,
-0.011181582,
-0.03550965,
-0.0068488955,
0.01781137,
-0.009153064,
-0.054678075,
-0.028625414,
0.0144611355,
0.007... | Sample = Data Parallelism (sample-wise parallel)
Operator = Parallelize a single operation into several sub-operations
Attribute = Data Parallelism (length-wise parallel)
Parameter = Model Parallelism (regardless of dimension - horizontal or vertical)
Examples:
* Sample
Let's take 10 batches of sequence length 512. If... |
[
0.02988425,
-0.00563709,
-0.010667727,
0.020619402,
-0.016951457,
-0.008994502,
-0.010156261,
0.0067769294,
-0.035013527,
0.06324647,
-0.0059805033,
-0.015665485,
0.030746436,
-0.03925139,
-0.0024166787,
0.03150633,
0.0026943318,
-0.02162772,
-0.05360168,
0.014299138,
0.01102... |
This diagram is from a blog post 3D parallelism: Scaling to trillion-parameter models, which is a good read as well.
Since each dimension requires at least 2 GPUs, here you'd need at least 8 GPUs.
Implementations:
- DeepSpeed - DeepSpeed also includes an even more efficient DP, which they call ZeRO-DP.
- Megatron-LM
... |
[
-0.01056088,
0.0033062715,
-0.00079105125,
0.0070553473,
0.020206632,
-0.02320294,
-0.0036033192,
-0.033180796,
-0.061815467,
0.0547306,
-0.0016134678,
0.020826558,
0.012988923,
-0.041977838,
-0.031173417,
-0.014996302,
-0.018110693,
-0.011623611,
-0.02208117,
0.020191872,
0.... | Operator
If we perform layer normalization, we compute std first and mean second, and then we can normalize data.
Operator parallelism allows computing std and mean in parallel. So if we parallelize them by operator dimension into 2
devices (cuda:0, cuda:1), first we copy input data into both devices, and cuda:0 com... |
[
0.031710077,
0.038998745,
-0.031071227,
0.02491506,
-0.030577572,
0.0029945984,
0.017466675,
-0.02706391,
-0.0022268924,
0.05575398,
0.028748145,
0.008000115,
0.041786447,
-0.044777416,
0.0193687,
0.021038415,
0.0061706877,
-0.034672007,
-0.053256664,
-0.0022631905,
0.0154630... |
The significance of this framework is that it takes resources like (1) GPU/TPU/CPU vs. (2) RAM/DRAM vs. (3)
fast-intra-connect/slow-inter-connect and it automatically optimizes all these algorithmically deciding which
parallelisation to use where.
One very important aspect is that FlexFlow is designed for optimizin... |
[
0.03474441,
0.006692915,
-0.01776385,
0.022267755,
0.023932243,
-0.019833969,
0.027680835,
-0.03460454,
0.01611335,
0.07301362,
0.0282683,
0.0009651226,
0.015330063,
-0.04000363,
-0.03418492,
0.035136055,
-0.0046822433,
-0.06506884,
-0.017777838,
-0.0024198,
0.0026785647,
0... | Attribute
We have 10 batches of 512 length. If we parallelize them by attribute dimension into 2 devices, 10 x 512 will be 10 x 2 x 256.
Parameter
It is similar with tensor model parallelism or naive layer-wise model parallelism. |
[
0.018727953,
0.03517095,
0.0040492867,
-0.0070898673,
-0.07410195,
-0.007086252,
-0.004555448,
0.016978081,
-0.006912711,
0.06779663,
0.009631518,
0.0057666185,
0.053913362,
0.005979929,
-0.008091343,
0.036559276,
-0.019942738,
-0.07016835,
-0.07710999,
-0.001151516,
0.002727... | accelerate launch --num_processes 2 trainer-program.py
Use --num_gpus to select how many GPUs to use.
deepspeed --num_gpus 2 trainer-program.py
Order of GPUs
Now, to select which GPUs to use and their order, you'll use the CUDA_VISIBLE_DEVICES environment variable. It is easiest to set the environment variable in ... |
[
0.016539939,
0.010188659,
0.00036872615,
-0.0024543812,
-0.031959474,
-0.0175483,
-0.011736214,
0.014124071,
-0.007905839,
0.045908485,
0.0065823635,
0.0061972253,
0.063862935,
-0.012863619,
-0.030895093,
0.030558974,
-0.02929852,
-0.0071985847,
-0.067112096,
0.021175604,
0.0... | CUDA_VISIBLE_DEVICES=0,2 torchrun trainer-program.py
Only the 2 physical GPUs (0 and 2) are "visible" to PyTorch and these are mapped to cuda:0 and cuda:1 respectively. You can also reverse the order of the GPUs to use 2 first. Now, the mapping is cuda:1 for GPU 0 and cuda:0 for GPU 2.
CUDA_VISIBLE_DEVICES=2,0 torchr... |
[
0.041181363,
0.01278907,
-0.02191417,
0.015603223,
-0.013513505,
0.004569515,
-0.0005650944,
0.005739757,
0.014655884,
0.05923652,
0.033797696,
-0.01313039,
0.055140674,
-0.04095846,
0.017442174,
0.04767342,
-0.0192254,
-0.053050958,
-0.035525195,
-0.025814975,
0.024630802,
... | Use the --nproc_per_node to select how many GPUs to use.
torchrun --nproc_per_node=2 trainer-program.py
Use --num_processes to select how many GPUs to use.
accelerate launch --num_processes 2 trainer-program.py
Use --num_gpus to select how many GPUs to use.
deepspeed --num_gpus 2 trainer-program.py |
[
-0.008549029,
0.013560289,
0.009195419,
0.023589758,
-0.051822405,
-0.028858185,
0.015985988,
0.007002558,
-0.019655816,
0.040646113,
0.0063909204,
0.0070234095,
0.06260947,
-0.036976285,
-0.03202758,
-0.0038609637,
-0.024535017,
-0.022255277,
-0.085962914,
0.012670633,
0.003... | CUDA_VISIBLE_DEVICES=2,0 torchrun trainer-program.py
You can also set the CUDA_VISIBLE_DEVICES environment variable to an empty value to create an environment without GPUs.
CUDA_VISIBLE_DEVICES= python trainer-program.py |
[
0.01395105,
0.022535747,
0.0005040119,
-0.007932232,
-0.0635165,
-0.043839872,
0.010666725,
0.013540509,
-0.014596186,
0.034544062,
0.0016998948,
0.022286491,
0.05756366,
-0.045833927,
-0.019456694,
-0.000013982035,
-0.0080202045,
-0.025512166,
-0.09272353,
0.016289666,
0.008... | CUDA_VISIBLE_DEVICES= python trainer-program.py
As with any environment variable, they can be exported instead of being added to the command line. However, this is not recommended because it can be confusing if you forget how the environment variable was setup and you end up using the wrong GPUs. Instead, it is commo... |
[
-0.011468309,
0.0040760064,
-0.013780468,
0.0055789095,
-0.021945693,
-0.023782209,
0.024786346,
-0.0020115785,
0.008270923,
0.077688545,
0.011686313,
0.00570773,
0.01746671,
-0.05950837,
-0.029172841,
0.017374225,
0.012716875,
-0.027534513,
-0.062996425,
0.009631794,
0.01597... | CUDA_DEVICE_ORDER is an alternative environment variable you can use to control how the GPUs are ordered. You can either order them by:
PCIe bus ID's that matches the order of nvidia-smi and rocm-smi for NVIDIA and AMD GPUs respectively
export CUDA_DEVICE_ORDER=PCI_BUS_ID
GPU compute ability |
[
-0.010005094,
0.019250995,
-0.010838852,
0.02627354,
-0.03080159,
-0.04842574,
0.02178616,
-0.01881717,
0.01579395,
0.071852304,
0.01437046,
0.023277434,
0.04894091,
-0.047503863,
-0.031072732,
0.018057974,
0.0048669768,
-0.025582131,
-0.08627699,
0.0043145274,
0.017068312,
... | GPU compute ability
export CUDA_DEVICE_ORDER=FASTEST_FIRST
The CUDA_DEVICE_ORDER is especially useful if your training setup consists of an older and newer GPU, where the older GPU appears first, but you cannot physically swap the cards to make the newer GPU appear first. In this case, set CUDA_DEVICE_ORDER=FASTEST_FI... |
[
0.010207984,
-0.013020755,
-0.02769608,
0.013812071,
-0.002154539,
-0.029552078,
-0.0065391525,
0.01986924,
0.002125764,
0.034012225,
-0.009064171,
-0.008654126,
0.035537306,
-0.026314873,
-0.026415586,
0.016747136,
-0.029523302,
-0.023178382,
-0.04103336,
-0.013366057,
-0.00... |
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
chat = [
{"role": "user", "content": "Hello, how are you?"},
{"role": "assistant", "content": "I'm doing great. How can I help you today?"},
{"role": "user", "content": "I'd like to show of... |
[
0.0046287556,
-0.024736429,
-0.017590018,
0.051143836,
-0.00051052455,
-0.037089724,
0.0031703806,
0.012800878,
-0.0041774428,
0.041476037,
0.013047049,
0.04084942,
0.011935551,
-0.027421927,
-0.041088134,
-0.011510347,
-0.015560974,
-0.030420735,
-0.050308347,
0.009690175,
0... | Notice how the entire chat is condensed into a single string. If we use tokenize=True, which is the default setting,
that string will also be tokenized for us. To see a more complex template in action, though, let's use the
mistralai/Mistral-7B-Instruct-v0.1 model.
thon |
[
0.015424889,
-0.011886934,
-0.035646,
0.0054586637,
0.0018522448,
-0.019347725,
-0.02387749,
0.00076143735,
-0.0074755936,
0.025017334,
-0.0017393709,
0.013552289,
0.01693481,
-0.026394026,
-0.01013276,
0.015942998,
-0.03443214,
-0.01777859,
-0.05998239,
-0.015306463,
-0.0237... |
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
chat = [
{"role": "user", "content": "Hello, how are you?"},
{"role": "assistant", "content": "I'm doing great. How can I help you today?"},
{"role": "user", "content": "I'd like to show off... |
[
0.025022525,
0.008861202,
-0.008989406,
0.0065233526,
-0.006749596,
-0.024042137,
-0.011809908,
0.022805339,
-0.0033125812,
0.0131522855,
-0.026379986,
0.019094948,
0.0076621114,
-0.026983302,
-0.02754137,
-0.006930591,
0.0230165,
-0.022051195,
-0.058974124,
-0.048838418,
0.0... |
Note that this time, the tokenizer has added the control tokens [INST] and [/INST] to indicate the start and end of
user messages (but not assistant messages!). Mistral-instruct was trained with these tokens, but BlenderBot was not.
How do I use chat templates?
As you can see in the example above, chat templates are... |
[
0.021525333,
-0.03138136,
-0.017781796,
0.0006096041,
-0.008452202,
-0.011245231,
-0.012276166,
0.01389934,
-0.016114753,
0.04229026,
0.012619811,
0.027944911,
0.015018014,
-0.034656957,
-0.06340614,
0.016465709,
-0.03509565,
-0.014476956,
-0.06603832,
-0.014352659,
0.0087519... |
Templates for Chat Models
Introduction
An increasingly common use case for LLMs is chat. In a chat context, rather than continuing a single string
of text (as is the case with a standard language model), the model instead continues a conversation that consists
of one or more messages, each of which includes a role, li... |
[
0.007976006,
-0.019710489,
-0.033309866,
0.026051126,
0.019409237,
-0.031186758,
-0.0056915116,
0.009553993,
-0.008076423,
0.042232666,
0.0014829489,
0.02678274,
0.0030286587,
-0.023598075,
0.000020873582,
0.0071870126,
0.019882634,
-0.04369589,
-0.07700576,
-0.010421885,
-0.... | From here, just continue training like you would with a standard language modelling task, using the formatted_chat column.
Advanced: How do chat templates work?
The chat template for a model is stored on the tokenizer.chat_template attribute. If no chat template is set, the
default template for that model class is used... |
[
0.05402868,
0.0031039205,
-0.02007126,
0.036140397,
-0.009072998,
-0.035473377,
-0.008322599,
0.038020182,
-0.0020958094,
0.022602906,
-0.038626567,
0.0029996983,
0.007720007,
-0.024967799,
-0.041810073,
-0.01384826,
0.010619273,
-0.04047603,
-0.03292657,
-0.035928164,
0.0175... |
Now that our input is formatted correctly for Zephyr, we can use the model to generate a response to the user's question:
python
outputs = model.generate(tokenized_chat, max_new_tokens=128)
print(tokenizer.decode(outputs[0]))
This will yield:
text
<|system|>
You are a friendly chatbot who always responds in the styl... |
[
0.0015370813,
-0.0031469765,
-0.017688423,
0.025644662,
-0.0005500993,
-0.038815077,
-0.003917737,
0.011046384,
0.003418696,
0.03600198,
-0.00081915513,
-0.019123387,
0.049641244,
-0.035206355,
-0.03318888,
0.025175812,
-0.032279596,
-0.030176876,
-0.044725426,
-0.014321229,
... | from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
tokenizer.default_chat_template
"{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_t... |
[
0.0052310177,
0.0105717005,
0.0010207978,
0.034478657,
0.0019063422,
-0.0590436,
0.012750377,
0.008773195,
0.00939463,
0.0034708953,
-0.00682847,
0.03980106,
0.012764999,
-0.032051407,
-0.026334208,
0.0046278606,
-0.008122517,
-0.040210478,
-0.07474762,
0.011690282,
0.0101695... |
If you're fine-tuning a model for chat, in addition to setting a chat template, you should probably add any new chat
control tokens as special tokens in the tokenizer. Special tokens are never split,
ensuring that your control tokens are always handled as single tokens rather than being tokenized in pieces. You
sho... |
[
0.039384395,
-0.032034274,
-0.019095,
0.034759946,
0.0007417114,
-0.021055033,
-0.011078776,
0.03684248,
-0.0028883677,
0.026077615,
-0.030962383,
-0.022080988,
0.026337933,
0.0067108134,
-0.046550762,
-0.005240789,
-0.0032941557,
-0.027425138,
-0.044161975,
-0.01958501,
-0.0... |
text
{'role': 'assistant', 'content': "Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and m... |
[
0.024347795,
-0.009052567,
0.013660246,
0.034256782,
-0.006858434,
-0.0169019,
0.017949421,
0.020808872,
0.01956317,
0.060812864,
-0.004597062,
0.03054799,
0.009392303,
-0.0010899885,
-0.03626689,
-0.00338675,
-0.013943359,
-0.04764807,
-0.025565185,
0.028990863,
-0.018105134... |
That's kind of intimidating. Let's add some newlines and indentation to make it more readable. Note that the first
newline after each block as well as any preceding whitespace before a block are ignored by default, using the
Jinja trim_blocks and lstrip_blocks flags. However, be cautious - although leading whitespac... |
[
-0.004949575,
-0.014791171,
0.0023650767,
0.023323433,
0.016100507,
-0.019136438,
-0.011215679,
-0.024042847,
-0.03248878,
0.037697345,
-0.013560972,
0.0055970484,
0.009143764,
-0.011302009,
-0.013690466,
0.008107807,
-0.02063282,
-0.036575057,
-0.086675115,
0.011539415,
-0.0... |
What are "default" templates?
Before the introduction of chat templates, chat handling was hardcoded at the model class level. For backwards
compatibility, we have retained this class-specific handling as default templates, also set at the class level. If a
model does not have a chat template set, but there is a def... |
[
0.05124072,
0.011489397,
-0.051325932,
-0.038032886,
0.042691134,
-0.0010527213,
0.03345985,
-0.00054899504,
0.013406663,
0.030761475,
0.011176953,
-0.055302486,
0.044935044,
-0.051155508,
0.018320546,
0.045645144,
0.016360674,
-0.027651243,
-0.041554976,
-0.0108503075,
0.010... | How to add a model to 🤗 Transformers?
The 🤗 Transformers library is often able to offer new models thanks to community contributors. But this can be a challenging project and requires an in-depth knowledge of the 🤗 Transformers library and the model to implement. At Hugging Face, we're trying to empower more of the ... |
[
0.032695774,
0.007121742,
-0.052661754,
-0.02416002,
0.027024962,
-0.032991126,
0.047197692,
0.0059477068,
0.029048141,
0.027157871,
0.033316016,
-0.015314145,
0.043121796,
-0.041822236,
0.009761475,
0.03668306,
0.015447054,
-0.033404622,
-0.057771392,
-0.012833165,
-0.019286... |
A Hugging Face team member will be available to help you along the way so you'll never be alone. 🤗 ❤️
To get started, open a New model addition issue for the model you want to see in 🤗 Transformers. If you're not especially picky about contributing a specific model, you can filter by the New model label to see if t... |
[
0.039962914,
0.004503556,
-0.05570077,
-0.0028559994,
-0.021794122,
-0.042548418,
0.018168794,
0.0055117626,
-0.0025767228,
0.0813872,
0.0091125,
-0.0022798814,
0.059522822,
-0.05823007,
0.016342077,
0.06643624,
0.018758964,
-0.04086222,
-0.05645956,
-0.014979067,
0.002546863... | If you're interested in implementing a TensorFlow model, take a look at the How to convert a 🤗 Transformers model to TensorFlow guide!
Along the way, you'll:
get insights into open-source best practices
understand the design principles behind one of the most popular deep learning libraries
learn how to efficiently t... |
[
0.008438965,
-0.02058522,
0.010174144,
-0.030773303,
-0.0076793884,
-0.012466812,
0.010118396,
-0.012877958,
0.01524728,
0.06868245,
0.0039442233,
0.010404108,
0.0087316455,
-0.043484014,
0.0039233174,
0.015233343,
-0.025714105,
-0.022006813,
-0.10904453,
-0.0004390213,
0.009... | Composition is generally favored over-abstraction
Duplicating code is not always bad if it strongly improves the readability or accessibility of a model
Model files are as self-contained as possible so that when you read the code of a specific model, you ideally only
have to look into the respective modeling_.py file... |
[
0.038321897,
-0.008743346,
-0.021092786,
-0.06416557,
0.021121405,
0.0028834438,
0.0116482545,
-0.0003034145,
-0.0033055858,
0.047337133,
0.030880753,
-0.024255272,
0.035545778,
-0.040411144,
0.028934607,
0.03669057,
-0.0014435106,
-0.009251347,
-0.065138645,
-0.0033055858,
0... |
In our opinion, the library's code is not just a means to provide a product, e.g. the ability to use BERT for
inference, but also as the very product that we want to improve. Hence, when adding a model, the user is not only the
person who will use your model, but also everybody who will read, try to understand, and p... |
[
0.026539195,
-0.027377123,
-0.02024029,
-0.0369844,
0.033054806,
0.022103956,
0.015328299,
-0.012301646,
-0.014223102,
0.051460322,
0.020355865,
-0.005851047,
0.045594826,
-0.045508146,
0.021396052,
0.032072406,
-0.004756685,
-0.00027155902,
-0.08494854,
0.0017200996,
0.03600... |
As you can see, we do make use of inheritance in 🤗 Transformers, but we keep the level of abstraction to an absolute
minimum. There are never more than two levels of abstraction for any model in the library. BrandNewBertModel
inherits from BrandNewBertPreTrainedModel which in turn inherits from [PreTrainedModel] and... |
[
0.031542435,
-0.001205455,
-0.035481595,
-0.018411912,
-0.020804586,
-0.020454438,
0.053601716,
0.0067731617,
0.021738311,
0.012722019,
-0.023241028,
-0.0062661776,
0.022409428,
-0.06145085,
-0.00030250355,
0.049312413,
0.0030072548,
-0.03994597,
-0.04610273,
0.01982709,
0.02... | Overview of tokenizers
Not quite ready yet :-( This section will be added soon!
Step-by-step recipe to add a model to 🤗 Transformers
Everyone has different preferences of how to port a model so it can be very helpful for you to take a look at summaries
of how other contributors ported models to Hugging Face. Here is a... |
[
0.044197835,
-0.009570749,
-0.023986317,
-0.016392136,
-0.0070331628,
0.017566187,
0.025635934,
-0.00040984983,
-0.0040088654,
0.071750894,
0.037331864,
0.01707576,
0.019052329,
-0.054363042,
-0.0341218,
0.03572683,
-0.007226361,
-0.03510265,
-0.07389093,
0.0058405343,
0.0275... |
The forward pass of your model should be fully written in the modeling file while being fully independent of other
models in the library. If you want to reuse a block from another model, copy the code and paste it with a
# Copied from comment on top (see here
for a good example and there for more documentati... |
[
0.02675066,
0.02121703,
-0.011569015,
-0.0077126767,
-0.010293126,
-0.014493524,
0.0168446,
-0.008329118,
0.03973892,
0.032341633,
-0.0128234,
0.006662577,
0.023525096,
-0.05077751,
-0.0043437565,
0.04748027,
-0.015855428,
-0.020944647,
-0.032542333,
-0.009561999,
0.026377928... | Porting GPT2 Model by Thomas
Porting WMT19 MT Model by Stas
From experience, we can tell you that the most important things to keep in mind when adding a model are: |
[
0.030388124,
-0.017009346,
-0.008811984,
-0.01935349,
0.010855964,
0.002051127,
0.0031213579,
-0.003273227,
0.011199011,
0.05240022,
0.014779549,
-0.0034393899,
0.034704782,
-0.04471028,
0.011184717,
0.022812534,
0.011999451,
-0.057803188,
-0.0799868,
-0.01155635,
0.01915338,... |
Don't reinvent the wheel! Most parts of the code you will add for the new 🤗 Transformers model already exist
somewhere in 🤗 Transformers. Take some time to find similar, already existing models and tokenizers you can copy
from. grep and rg are your
friends. Note that it might very well happen that your model'... |
[
0.058058564,
-0.01074886,
-0.03159777,
-0.0063342694,
0.010066606,
-0.021297058,
0.010956213,
-0.0021487689,
0.006675397,
0.04952369,
-0.0079328865,
-0.013752121,
0.030928895,
-0.041951995,
0.015317294,
-0.012079927,
-0.02402608,
-0.0619113,
-0.044895057,
0.0073375856,
-0.003... |
What type of model is brand_new_bert? BERT-like encoder-only model? GPT2-like decoder-only model? BART-like
encoder-decoder model? Look at the model_summary if you're not familiar with the differences between those.
What are the applications of brand_new_bert? Text classification? Text generation? Seq2Seq tasks, e.... |
[
0.03371714,
-0.005000554,
-0.015386912,
-0.04006607,
0.006125486,
-0.019940576,
0.019632375,
-0.006876725,
0.0008196208,
0.06983824,
0.012505237,
-0.030850874,
0.06540015,
-0.060099106,
0.030465623,
0.049712747,
-0.0043995627,
-0.031451866,
-0.06657132,
-0.019354993,
0.014932... |
In the following, we try to give you a general recipe that we found most useful when porting a model to 🤗 Transformers.
The following list is a summary of everything that has to be done to add a model and can be used by you as a To-Do
List:
☐ (Optional) Understood the model's theoretical aspects
☐ Prepared 🤗 Transf... |
[
0.029389014,
0.0068729986,
-0.045083907,
-0.031864036,
0.0011754495,
-0.023194054,
0.029878091,
0.01720658,
0.011308027,
0.01999283,
-0.023031028,
-0.0068137166,
0.036043413,
-0.030708037,
-0.0038644406,
0.028736914,
-0.0037903383,
-0.03758474,
-0.055665728,
-0.009344313,
0.0... | After you feel like you have gotten a good overview of the architecture of the model, you might want to write to the
Hugging Face team with any questions you might have. This might include questions regarding the model's architecture,
its attention layer, etc. We will be more than happy to help you.
2. Next prepare you... |
[
0.013528654,
0.048500832,
-0.031545486,
-0.066168584,
-0.0007889934,
-0.03396768,
0.024663612,
0.003921814,
0.01171201,
0.00829957,
-0.026629863,
0.0042993906,
0.06360391,
-0.03530701,
0.01573,
0.019064074,
0.0030028056,
-0.027869456,
-0.07574336,
-0.012296186,
0.0040892297,
... | Fork the repository by clicking on the ‘Fork' button on the
repository's page. This creates a copy of the code under your GitHub user account.
Clone your transformers fork to your local disk, and add the base repository as a remote:
git clone https://github.com/[your Github handle]/transformers.git
cd transformers... |
[
0.022196688,
0.0020468845,
-0.05368501,
-0.028591856,
-0.025322577,
-0.022454789,
-0.0026562896,
0.0068325074,
-0.014446488,
0.037568036,
0.00734154,
-0.0015602565,
0.061141264,
-0.034040656,
0.00051889056,
0.038026884,
-0.008330927,
-0.019386254,
-0.043934528,
0.014582708,
0... | Set up a development environment, for instance by running the following command:
python -m venv .env
source .env/bin/activate
pip install -e ".[dev]"
Depending on your OS, and since the number of optional dependencies of Transformers is growing, you might get a
failure with this command. If that's the case make sure t... |
[
0.027133612,
-0.012715333,
-0.027289715,
-0.005534576,
-0.01344618,
-0.0174552,
-0.0031256161,
-0.0059390254,
-0.009025616,
0.016078651,
-0.027601922,
-0.049953092,
0.05699194,
-0.03405893,
0.04932868,
0.049044855,
-0.0043708957,
-0.01548262,
-0.036982317,
-0.00080668216,
0.0... | pip install -e ".[quality]"
which should be enough for most use cases. You can then return to the parent directory
cd ..
We recommend adding the PyTorch version of brand_new_bert to Transformers. To install PyTorch, please follow the
instructions on https://pytorch.org/get-started/locally/.
Note: You don't need t... |
[
0.031522706,
-0.01061692,
-0.032059517,
-0.020443534,
-0.0016738051,
-0.05493361,
0.011191009,
-0.0015945883,
-0.027019463,
0.0029077237,
-0.021263663,
-0.032119162,
0.054038927,
-0.047000743,
0.03253668,
0.01689462,
-0.011675629,
-0.027272958,
-0.047537554,
0.02336617,
0.003... |
git clone https://github.com/org_that_created_brand_new_bert_org/brand_new_bert.git
cd brand_new_bert
pip install -e .
Now you have set up a development environment to port brand_new_bert to 🤗 Transformers.
3.-4. Run a pretrained checkpoint using the original repository
At first, you will work on the original brand_... |
[
-0.0039609782,
0.003199399,
-0.028779283,
-0.03429021,
-0.010577917,
-0.030386636,
0.01792582,
0.008465395,
0.007707643,
0.03943374,
-0.017864587,
-0.010891734,
0.058170892,
-0.045434527,
0.017772738,
0.043995563,
0.002543063,
-0.007562216,
-0.09742093,
0.013631889,
0.0369232... |
It is very important that before you start the porting process, you can efficiently debug code in the original
repository! Also, remember that you are working with an open-source library, so do not hesitate to open an issue, or
even a pull request in the original repository. The maintainers of this repository are mos... |
[
0.029214194,
-0.021693092,
-0.014816879,
-0.028219668,
0.012097473,
-0.006588733,
0.024661131,
0.00047856694,
-0.0025640116,
0.066073805,
-0.02144446,
-0.009820942,
0.024723288,
-0.033813875,
-0.016969096,
-0.003830478,
0.00812714,
-0.038475715,
-0.05920536,
0.021491079,
0.02... |
Jupyter notebooks have the advantage that they allow for cell-by-cell execution which can be helpful to better split
logical components from one another and to have faster debugging cycles as intermediate results can be stored. Also,
notebooks are often easier to share with other contributors, which might be very hel... |
[
0.016031528,
-0.047102496,
-0.03114286,
0.015255113,
0.018274503,
-0.008842502,
-0.010445654,
-0.00915163,
0.023163041,
0.057080865,
0.015542674,
0.035916373,
0.0029043665,
-0.09598787,
-0.03614642,
0.042702813,
-0.025362883,
-0.03378842,
-0.07039494,
-0.008547752,
0.00493526... | Decompose the original model into many small testable components and run a forward pass on each of those for
verification
Decompose the original model only into the original tokenizer and the original model, run a forward pass on
those, and use intermediate print statements or breakpoints for verification |
[
0.047451973,
-0.006058174,
-0.053294048,
-0.026050894,
0.011579831,
0.02301063,
-0.0032395956,
-0.0039940733,
-0.01962759,
0.050223976,
0.01280935,
0.006326433,
0.02223566,
-0.05013456,
0.0049888655,
0.055469926,
-0.014098481,
-0.022951016,
-0.069985695,
-0.012824253,
0.00984... |
Where to find the pretrained weights?
How to load the pretrained weights into the corresponding model?
How to run the tokenizer independently from the model?
Trace one forward pass so that you know which classes and functions are required for a simple forward pass. Usually,
you only have to reimplement those functi... |
[
0.014647037,
-0.0042371275,
-0.017971389,
-0.02256013,
-0.027063632,
-0.012068533,
0.0014774896,
0.0014037928,
0.004482192,
0.026026549,
0.020855334,
0.0044253655,
-0.0040844064,
-0.036028016,
-0.005352348,
0.005018492,
-0.018752754,
-0.013808846,
-0.056968592,
0.0071565905,
... | Again, it is up to you which strategy to choose. Often, one or the other is advantageous depending on the original code
base.
If the original code-base allows you to decompose the model into smaller sub-components, e.g. if the original
code-base can easily be run in eager mode, it is usually worth the effort to do so. ... |
[
0.013660662,
-0.0142490985,
-0.02449087,
-0.027529884,
0.0013900878,
-0.03199902,
0.023001159,
-0.006647841,
0.016640086,
0.030226262,
0.021779593,
-0.000043382053,
0.018993832,
-0.08068282,
-0.0106142005,
0.007761401,
-0.0054188287,
-0.0023090541,
-0.067752115,
0.014457658,
... |
at a later stage when comparing the original model to the Hugging Face implementation, you can verify automatically
for each component individually that the corresponding component of the 🤗 Transformers implementation matches instead
of relying on visual comparison via print statements
it can give you some rope ... |
[
0.04848849,
0.009299536,
-0.009899505,
-0.005798574,
-0.04287059,
-0.007015559,
0.015790122,
-0.010090405,
-0.001280902,
0.05127017,
-0.025689626,
0.026357776,
-0.024585137,
-0.009538161,
-0.022321614,
-0.006418998,
0.012565282,
-0.04930663,
-0.038861696,
-0.018176366,
0.0287... | Jupyter notebooks / google colab
Local python scripts. |
[
0.010980749,
-0.009750215,
-0.017744765,
-0.01882638,
0.015479642,
-0.0011413787,
-0.0032468059,
-0.0100167,
0.023246896,
0.058689397,
0.0023768109,
0.03370251,
0.015424778,
-0.09430433,
-0.031319823,
0.031586308,
-0.004659568,
-0.018591246,
-0.07380066,
-0.014202082,
0.02219... |
Lysandre's integration checks for ELECTRA
gives a nice example of how this can be done.
However, if the original code-base is very complex or only allows intermediate components to be run in a compiled mode,
it might be too time-consuming or even impossible to separate the model into smaller testable sub-components. ... |
[
0.025641374,
0.0043480066,
-0.023505755,
-0.023156798,
-0.006640655,
-0.031043231,
-0.00456436,
-0.021035139,
-0.01222746,
0.039557785,
-0.0014455551,
0.0027375692,
0.026450954,
-0.0562519,
-0.025110958,
-0.0011341108,
0.00085799844,
-0.01360933,
-0.048519008,
-0.031434063,
-... | Retrieve the input IDs passed to the model
Retrieve the word embeddings
Retrieve the input of the first Transformer layer
Retrieve the output of the first Transformer layer
Retrieve the output of the following n - 1 Transformer layers
Retrieve the output of the whole BrandNewBert Model |
[
0.031802554,
-0.013040171,
-0.019683277,
-0.016393358,
0.03188691,
-0.041812904,
0.02778154,
0.012997993,
-0.013026112,
0.027134804,
-0.002929645,
-0.043162614,
0.072884366,
-0.039647743,
0.024308847,
-0.004172152,
0.018600697,
-0.040575672,
-0.05123276,
-0.0047485908,
-0.010... | The following section gives you more specific details/tips on how you can do this for brand_new_bert.
5.-14. Port BrandNewBert to 🤗 Transformers
Next, you can finally start adding new code to 🤗 Transformers. Go into the clone of your 🤗 Transformers' fork: |
[
0.02040473,
0.004708784,
0.0046818545,
-0.037239403,
-0.026098358,
-0.023759354,
-0.00010855882,
-0.017419424,
-0.009163663,
0.062045153,
0.009756108,
-0.004724172,
0.02235903,
-0.06801577,
-0.01299532,
-0.011987395,
0.0065476717,
-0.01617298,
-0.0810034,
-0.028806679,
0.0171... |
Input IDs should thereby consists of an array of integers, e.g. input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19]
The outputs of the following layers often consist of multi-dimensional float arrays and can look like this:
[[
[-0.1465, -0.6501, 0.1993, , 0.1451, 0.3430, 0.6024],
[-0.4417, -0.5920, 0.3450, , -0.3062, ... |
[
0.0474011,
0.013969651,
-0.045274127,
-0.0151926605,
-0.01981123,
-0.032937687,
0.029777614,
0.00850789,
-0.004421064,
0.01838312,
-0.0072848806,
-0.02055567,
0.03533813,
-0.05366048,
-0.0030670185,
0.026708698,
0.0016018761,
-0.044787962,
-0.03676624,
-0.0022447156,
0.009062... |
In both cases, you will be prompted with a questionnaire to fill in the basic information of your model. The second command requires to install cookiecutter, you can find more information on it here.
Open a Pull Request on the main huggingface/transformers repo
Before starting to adapt the automatically generated cod... |
[
0.023383927,
0.013527639,
-0.043111734,
0.0014034164,
0.000007802873,
-0.0015728928,
-0.00021612993,
-0.006409633,
0.02710098,
0.056639373,
0.016483001,
0.00905651,
0.038389247,
-0.058284625,
-0.02678107,
0.007342705,
0.014175077,
-0.02678107,
-0.053348865,
-0.001642397,
0.00... |
Find the best way of debugging intermediate results. Is the original repository written in PyTorch? Then you should
probably take the time to write a longer script that decomposes the original model into smaller sub-components to
retrieve intermediate values. Is the original repository written in Tensorflow 1? Th... |
[
0.019548718,
0.017752118,
-0.01728158,
-0.03444909,
-0.0018268997,
-0.032509904,
-0.0032634668,
-0.02704881,
-0.0011193103,
0.045285724,
0.034392055,
-0.0016941153,
0.04231991,
-0.044202063,
-0.010494425,
0.041236248,
0.027890075,
-0.0074573155,
-0.08361319,
-0.010038146,
0.0... | cd transformers
In the special case that you are adding a model whose architecture exactly matches the model architecture of an
existing model you only have to add a conversion script as described in this section.
In this case, you can just re-use the whole model architecture of the already existing model.
Otherwise, l... |
[
0.04938309,
0.004906825,
-0.0212082,
-0.052518096,
0.01755069,
-0.0124127595,
-0.0023060401,
-0.016733445,
0.020752687,
0.06827352,
0.043782942,
-0.021596728,
0.013035742,
-0.06286094,
0.02036416,
0.019975632,
0.00461543,
-0.049195524,
-0.035985615,
-0.008259542,
-0.030787393... | transformers-cli add-new-model-like to add a new model like an existing one
transformers-cli add-new-model to add a new model from our template (will look like BERT or Bart depending on the type of model you select) |
[
0.005434881,
-0.010047068,
-0.012624359,
-0.029223861,
0.005795265,
-0.042809244,
0.027782327,
0.031684667,
0.007404252,
0.048575386,
0.021200769,
-0.030927494,
0.025175912,
-0.004572144,
-0.011059055,
-0.02181233,
-0.024549792,
-0.029718934,
-0.05658392,
-0.013206798,
0.0056... | Create a branch with a descriptive name from your main branch
git checkout -b add_brand_new_bert
Commit the automatically generated code:
git add .
git commit
Fetch and rebase to current main
git fetch upstream
git rebase upstream/main
Push the changes to your account using:
git push -u origin a-descriptive-name... |
[
-0.02044734,
0.011523467,
-0.051097386,
-0.02949001,
0.0062089753,
-0.037009265,
0.042012785,
0.022096545,
0.012669524,
0.042879317,
-0.01661783,
-0.004538806,
0.031726222,
-0.00870724,
-0.01798751,
-0.022082567,
0.0014290776,
-0.042907268,
-0.04494781,
-0.011041284,
-0.00618... | git add .
git commit
Fetch and rebase to current main
git fetch upstream
git rebase upstream/main
Push the changes to your account using:
git push -u origin a-descriptive-name-for-my-changes
Once you are satisfied, go to the webpage of your fork on GitHub. Click on “Pull request”. Make sure to add the
GitHub ha... |
[
0.015078345,
-0.014177143,
-0.024534257,
-0.023659956,
0.0040083383,
-0.040782824,
0.023996226,
-0.015697083,
0.007505546,
0.073226154,
0.021763394,
0.04296185,
0.044495244,
-0.007176001,
-0.010794266,
0.031367265,
0.020337608,
-0.039599154,
-0.041186348,
0.02660568,
-0.03903... | Change the PR into a draft by clicking on “Convert to draft” on the right of the GitHub pull request web page.
In the following, whenever you have made some progress, don't forget to commit your work and push it to your account so
that it shows in the pull request. Additionally, you should make sure to update your wor... |
[
0.030026423,
0.0037755996,
0.014151067,
-0.057971805,
0.00934238,
0.013690265,
0.003920529,
0.013036224,
-0.02106309,
0.045009904,
0.030412901,
-0.037131686,
0.04896388,
-0.055058353,
0.016737502,
0.059815012,
-0.05580158,
-0.03157234,
-0.055682663,
0.0047975387,
0.005994136,... |
The above command will create a model according to the default parameters as defined in BrandNewBertConfig() with
random weights, thus making sure that the init() methods of all components works.
Note that all random initialization should happen in the _init_weights method of your BrandnewBertPreTrainedModel
class. I... |
[
0.030632146,
-0.00068478304,
-0.05071527,
-0.023450814,
0.0009428381,
-0.026926149,
0.04231911,
0.015854288,
-0.0103491135,
0.034784093,
-0.032938786,
-0.007592682,
0.022097588,
-0.0281256,
0.009026642,
-0.014216576,
0.00184531,
-0.061633356,
-0.047855042,
0.0077541466,
-0.02... |
git fetch upstream
git merge upstream/main
In general, all questions you might have regarding the model or your implementation should be asked in your PR and
discussed/solved in the PR. This way, the Hugging Face team will always be notified when you are committing new code or
if you have a question. It is often very... |
[
0.059473258,
-0.031764776,
-0.029622367,
0.0024673403,
-0.017353509,
-0.03336444,
0.009562283,
-0.0045883246,
0.026951497,
0.067357324,
-0.0008649974,
-0.024266345,
0.049132567,
-0.049646746,
0.010897717,
0.033478703,
-0.0027976283,
-0.029122472,
-0.047332942,
0.009076671,
-0... | If you are porting a model from TensorFlow to PyTorch, a good starting point might be BERT's conversion script here
If you are porting a model from PyTorch to PyTorch, a good starting point might be BART's conversion script here |
[
0.042276688,
-0.011341119,
0.000056841993,
-0.017588835,
0.015078205,
0.010771177,
0.0033583276,
-0.024298275,
-0.004404423,
0.06700783,
0.047038227,
-0.0014284615,
0.0067094406,
-0.089055195,
-0.021369208,
0.06527636,
-0.044152446,
-0.043142423,
-0.043431003,
-0.01963774,
0.... | Now we can create an instance of this model definition which will fill all weights: dense, intermediate,
layer_norm with random weights. We can print the model to see its architecture
thon
model = SimpleModel()
print(model) |
[
0.053445667,
-0.031836282,
-0.00883298,
-0.031142935,
0.037671972,
0.03859644,
0.02020824,
-0.023400536,
-0.0045248256,
0.035129692,
0.04697441,
0.0076918425,
0.038249765,
-0.05191452,
-0.014350884,
0.047696646,
-0.026795058,
-0.024570562,
-0.042698756,
-0.0077496218,
0.02633... | In the following, we'll quickly explain how PyTorch models store layer weights and define layer names. In PyTorch, the
name of a layer is defined by the name of the class attribute you give the layer. Let's define a dummy model in
PyTorch, called SimpleModel as follows:
thon
from torch import nn
class SimpleModel(nn.Mo... |
[
0.03436819,
-0.007426825,
-0.0029856386,
-0.0044411863,
0.025407353,
0.03490176,
0.03267332,
-0.043250564,
0.0006409707,
0.03684772,
0.033897396,
0.015528459,
0.03352076,
-0.07777569,
-0.012381965,
0.029016798,
-0.009266857,
-0.009196238,
-0.04186956,
-0.023147527,
0.00588104... |
This will print out the following:
SimpleModel(
(dense): Linear(in_features=10, out_features=10, bias=True)
(intermediate): Linear(in_features=10, out_features=10, bias=True)
(layer_norm): LayerNorm((10,), eps=1e-05, elementwise_affine=True)
)
We can see that the layer names are defined by the name of the class... |
[
0.030688604,
-0.027487068,
-0.022064645,
0.016699908,
0.0014006722,
0.0026318938,
0.022280965,
-0.014147331,
0.020305242,
0.039572146,
0.018286254,
0.0072323,
0.057685345,
-0.03472658,
-0.027184218,
0.01564715,
-0.00283199,
0.0077009937,
-0.033515185,
-0.008400428,
0.00862395... | Some layers were not added, i.e. an activation layer was not added, or the residual connection was forgotten
The word embedding matrix was not tied
The wrong positional embeddings are used because the original implementation uses on offset
Dropout is applied during the forward pass. To fix this make sure model.training... |
[
0.034377195,
-0.0415492,
-0.0062376754,
0.007678885,
-0.010606696,
0.0027519157,
0.022696212,
-0.012059254,
0.009623194,
0.04642132,
0.036858648,
-0.028945236,
0.055469543,
-0.06379148,
-0.016628757,
0.054773524,
-0.020865384,
0.00048063957,
-0.04481745,
-0.021455485,
0.00738... |
While doing so, you must verify that each randomly initialized weight of your PyTorch model and its corresponding
pretrained checkpoint weight exactly match in both shape and name. To do so, it is necessary to add assert
statements for the shape and print out the names of the checkpoints weights. E.g. you should add ... |
[
0.052884843,
-0.01532894,
-0.005690185,
0.0074181124,
-0.008232462,
-0.022582814,
0.021556322,
0.0037227427,
-0.034490116,
0.023130275,
-0.020420339,
-0.030822119,
0.049545325,
-0.05559478,
-0.0091768345,
0.003558504,
-0.0028142976,
-0.014658299,
-0.028522778,
0.009717453,
-0... | pytest tests/models/brand_new_bert/test_modeling_brand_new_bert.py
Having fixed all common tests, it is now crucial to ensure that all the nice work you have done is well tested, so that
a) The community can easily understand your work by looking at specific tests of brand_new_bert
b) Future changes to your model will... |
[
0.026692461,
0.0042853854,
-0.017480541,
-0.0021463775,
-0.008231772,
-0.038233154,
0.007362167,
-0.011083783,
-0.0065257247,
0.053384922,
0.015240202,
0.0040606144,
0.06662061,
-0.041593663,
-0.025793377,
-0.01415688,
-0.0025959192,
-0.037820462,
-0.04486574,
-0.0043811896,
... |
The best way to fix the problem is usually to look at the forward pass of the original implementation and the 🤗
Transformers implementation side-by-side and check if there are any differences. Ideally, you should debug/print out
intermediate outputs of both implementations of the forward pass to find the exact posit... |
[
0.031569246,
0.0034754483,
-0.021956906,
-0.0063173883,
-0.012585228,
0.006763322,
0.00589269,
0.009753906,
-0.0046398295,
0.046462003,
-0.0059068464,
-0.010277701,
0.05136019,
-0.036467433,
-0.014963539,
0.024986422,
0.004147887,
-0.016846368,
-0.05821199,
0.005846681,
-0.01... | At first, integration tests should be added. Those integration tests essentially do the same as the debugging scripts
you used earlier to implement the model to 🤗 Transformers. A template of those model tests has already added by the
Cookiecutter, called BrandNewBertModelIntegrationTests and only has to be filled out ... |
[
0.04680081,
0.0063873203,
-0.023358544,
-0.00867922,
-0.005829171,
0.0056407955,
-0.00044150476,
0.0077931583,
-0.008881549,
0.043703083,
0.009055971,
-0.03320988,
0.048754334,
-0.054140475,
0.032400563,
0.012830455,
-0.00088562584,
-0.044093788,
-0.048000835,
-0.002717489,
-... | It helps to transfer the knowledge you have acquired during the model addition to the community by showing how the
special features of brand_new_bert should work.
Future contributors can quickly test changes to the model by running those special tests. |
[
0.038002577,
-0.004530531,
0.011124262,
-0.0046085212,
0.008323698,
0.020206593,
0.004789317,
-0.0061612385,
-0.0302319,
0.03247235,
-0.03632933,
-0.019922992,
0.02991994,
-0.031025983,
0.014243876,
-0.007962107,
0.009961496,
-0.048722707,
-0.037747335,
0.014775628,
0.0042823... | RUN_SLOW=1 pytest -sv tests/models/brand_new_bert/test_modeling_brand_new_bert.py::BrandNewBertModelIntegrationTests
In case you are using Windows, you should replace RUN_SLOW=1 with SET RUN_SLOW=1
Second, all features that are special to brand_new_bert should be tested additionally in a separate test under
BrandNewB... |
[
0.038966957,
0.0161556,
-0.012116701,
-0.0153639475,
0.004244144,
-0.017665608,
0.026359139,
0.009133339,
-0.0263738,
0.03377723,
-0.010306159,
-0.0065201484,
0.019952606,
-0.016829973,
-0.01148631,
0.0063552205,
-0.009140669,
-0.057995968,
-0.049287777,
-0.0038153315,
-0.007... |
9. Implement the tokenizer
Next, we should add the tokenizer of brand_new_bert. Usually, the tokenizer is equivalent to or very similar to an
already existing tokenizer of 🤗 Transformers.
It is very important to find/extract the original tokenizer file and to manage to load this file into the 🤗
Transformers' implem... |
[
0.02623461,
0.024346797,
0.0000112223515,
0.0029309797,
-0.019672211,
-0.039524212,
0.026279557,
0.0023298012,
-0.022488948,
0.051270608,
-0.034999456,
-0.00077535183,
0.031373657,
-0.044408556,
0.013649188,
0.013049883,
-0.010120776,
-0.036677513,
-0.041112375,
-0.015716793,
... |
When both input_ids yield the same values, as a final step a tokenizer test file should also be added.
Analogous to the modeling test files of brand_new_bert, the tokenization test files of brand_new_bert should
contain a couple of hard-coded integration tests.
10. Run End-to-end integration tests
Having added the to... |
[
0.008526336,
-0.013202282,
-0.007112985,
0.0017848507,
-0.010289722,
-0.029825665,
0.06562175,
-0.018928334,
0.032652367,
0.020685116,
-0.0067563453,
0.017462147,
0.055952843,
-0.04541215,
-0.004008897,
0.009404727,
-0.021002129,
0.009272638,
-0.07365275,
0.05233361,
0.006363... | make style
and verify that your coding style passes the quality check: |
[
0.042109743,
-0.0010987825,
-0.025796691,
-0.0064156223,
-0.01682004,
-0.04372017,
0.012465916,
0.008954285,
-0.00666166,
0.026915045,
0.0026113559,
0.00299346,
0.03334185,
-0.05147409,
0.010684005,
0.02666155,
0.022724947,
-0.039992325,
-0.057766695,
0.0027660613,
-0.0077464... |
It is worth spending some time to create fitting model cards for each checkpoint. The model cards should highlight the
specific characteristics of this particular checkpoint, e.g. On which dataset was the checkpoint
pretrained/fine-tuned on? On what down-stream task should the model be used? And also include some cod... |
[
0.032954894,
-0.030285204,
-0.015659316,
-0.010362994,
0.0059960396,
-0.026940914,
0.023754507,
-0.016506154,
0.008260254,
0.03998795,
-0.01344175,
0.00067415077,
0.04535604,
-0.057068232,
0.011073476,
0.008927676,
0.04001666,
0.0042377757,
-0.04753772,
0.013011155,
-0.004094... |
make quality
There are a couple of other very strict design tests in 🤗 Transformers that might still be failing, which shows up in
the tests of your pull request. This is often because of some missing information in the docstring or some incorrect
naming. The Hugging Face team will surely help you if you're stuck he... |
[
0.034917664,
-0.0016546751,
-0.04772174,
-0.018479243,
-0.041151963,
-0.0012755151,
-0.016829811,
0.015278225,
-0.00045385622,
0.07078585,
-0.020310394,
-0.037154183,
0.026209215,
-0.021232959,
-0.017458832,
0.037433747,
-0.01699755,
-0.047973346,
-0.057003293,
0.009568111,
0... | Using pipelines for a webserver
Creating an inference engine is a complex topic, and the "best" solution
will most likely depend on your problem space. Are you on CPU or GPU? Do
you want the lowest latency, the highest throughput, support for
many models, or just highly optimize 1 specific model?
There are many ways ... |
[
0.011713701,
0.012841483,
-0.025074102,
0.022984594,
-0.007430901,
-0.0021085355,
-0.033902623,
-0.0012021592,
-0.0021465893,
0.088506594,
-0.049954474,
-0.022320379,
0.026956044,
-0.020466113,
-0.008987654,
-0.0053413925,
-0.035701536,
-0.077381,
-0.09254724,
-0.012391754,
0... |
The key thing to understand is that we can use an iterator, just like you would on a
dataset, since a webserver is basically a system that waits for requests and
treats them as they come in.
Usually webservers are multiplexed (multithreaded, async, etc..) to handle various
requests concurrently. Pipelines on the othe... |
[
0.029693957,
0.014775046,
-0.022212923,
-0.019306827,
-0.022371175,
-0.024097567,
-0.017609207,
0.024227047,
0.0018558723,
0.06917081,
-0.037606593,
-0.002996011,
0.0153648965,
-0.059387915,
-0.0087398635,
0.0062581743,
-0.039793357,
-0.06560293,
-0.03893016,
-0.0053877844,
-... |
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from transformers import pipeline
import asyncio
async def homepage(request):
payload = await request.body()
string = payload.decode("utf-8")
response_q = asyncio.Queue()
await... |
[
0.025067776,
0.008728353,
-0.027153373,
-0.009134638,
-0.018011963,
0.0070151854,
-0.007109985,
0.011653604,
0.0016319109,
0.06430135,
0.013766286,
-0.02317178,
0.039247118,
-0.033884156,
-0.0274784,
0.042687,
-0.02800657,
-0.07226454,
-0.06478889,
-0.010746235,
0.038813747,
... | And there you go, now you have a good idea of how to create a webserver!
What is really important is that we load the model only once, so there are no copies
of the model on the webserver. This way, no unnecessary RAM is being used.
Then the queuing mechanism allows you to do fancy stuff like maybe accumulating a few
i... |
[
0.006891322,
0.018357493,
-0.012620777,
0.031138027,
-0.05027978,
0.014537857,
-0.004422355,
0.02169786,
0.0300633,
0.07151289,
0.02030362,
0.045080427,
0.010188119,
-0.025851533,
-0.050395966,
0.007864385,
0.0010002946,
-0.06146275,
-0.01616447,
-0.0104568,
-0.0155254435,
... | Now you can start it with:
uvicorn server:app
And you can query it:
```bash
curl -X POST -d "test [MASK]" http://localhost:8000/
[{"score":0.7742936015129089,"token":1012,"token_str":".","sequence":"test."},] |
[
-0.02341009,
-0.004637713,
0.018550852,
0.010818948,
-0.037015952,
-0.046277087,
0.019193986,
0.0077319033,
0.026640052,
0.051450744,
0.028240742,
-0.013534404,
0.016735783,
-0.047734857,
-0.034357663,
0.008367891,
0.005656009,
-0.028297909,
-0.071573704,
0.011119078,
-0.0101... | The code sample below is intentionally written like pseudo-code for readability.
Do not run this without checking if it makes sense for your system resources! |
[
-0.0029100054,
-0.009080972,
-0.031293526,
0.032170914,
-0.03307755,
-0.024932457,
0.016918976,
-0.015354299,
0.02703819,
0.0960448,
0.011903238,
-0.022095568,
0.016714253,
-0.037172027,
-0.053783916,
0.010528662,
-0.025093311,
-0.055948142,
-0.05910674,
0.0053118565,
0.03184... |
py
(string, rq) = await q.get()
strings = []
queues = []
while True:
try:
(string, rq) = await asyncio.wait_for(q.get(), timeout=0.001) # 1ms
except asyncio.exceptions.TimeoutError:
break
strings.append(string)
queues.append(rq)
strings
outs = pipe(strings, batch_size=len(strings))
fo... |
[
0.021304015,
-0.018264567,
-0.0102546485,
-0.009822434,
0.00091497117,
0.010533497,
-0.0137611665,
0.007201259,
-0.011586149,
0.045006126,
-0.023590572,
0.030645436,
-0.011544323,
-0.012603946,
-0.029306963,
0.009306564,
-0.011732545,
-0.048937887,
-0.038759924,
0.0069677234,
... | Summary of the tokenizers
[[open-in-colab]]
On this page, we will have a closer look at tokenization. |
[
0.03793997,
-0.010890987,
0.00647267,
-0.021767402,
-0.029926537,
-0.01596859,
0.03071331,
-0.0016236311,
0.010694293,
0.049974695,
0.0020324984,
0.018940846,
-0.013571844,
-0.018226922,
-0.012996335,
-0.022670735,
0.0001874734,
-0.02463767,
-0.04679846,
0.009754536,
-0.01365... |
A simple way of tokenizing this text is to split it by spaces, which would give:
["Don't", "you", "love", "🤗", "Transformers?", "We", "sure", "do."]
This is a sensible first step, but if we look at the tokens "Transformers?" and "do.", we notice that the
punctuation is attached to the words "Transformer" and "do", w... |
[
0.06536244,
0.006115176,
-0.018836893,
-0.033025537,
-0.005907152,
-0.02423116,
0.055549465,
-0.019181209,
-0.024604168,
0.037960716,
0.031246575,
0.002092789,
-0.03001278,
-0.046281658,
-0.015278973,
0.01207254,
-0.0073059313,
0.010975037,
-0.044990476,
0.0015790066,
0.00133... |
While character tokenization is very simple and would greatly reduce memory and time complexity it makes it much harder
for the model to learn meaningful input representations. E.g. learning a meaningful context-independent
representation for the letter "t" is much harder than learning a context-independent represent... |
[
0.04102701,
-0.027767556,
-0.0016555736,
-0.004656417,
-0.0053067547,
-0.027202692,
0.008911483,
-0.010234456,
0.0009104726,
0.04322701,
-0.018224318,
0.007818916,
0.0028354719,
-0.028406745,
-0.02425945,
0.008435807,
-0.024541881,
-0.041324306,
-0.049767546,
0.019606749,
-0.... |
As we saw in the preprocessing tutorial, tokenizing a text is splitting it into words or
subwords, which then are converted to ids through a look-up table. Converting words or subwords to ids is
straightforward, so in this summary, we will focus on splitting a text into words or subwords (i.e. tokenizing a text).
Mor... |
[
0.046248104,
-0.0010247047,
-0.018829385,
0.041715622,
-0.003836522,
-0.019668734,
0.012520282,
0.01563986,
-0.017710254,
0.035644334,
0.04630406,
-0.017990036,
-0.052291412,
-0.048094667,
-0.04675171,
0.025026575,
-0.041323926,
-0.055173174,
-0.023921432,
0.0035549907,
0.026... |
Subword tokenization algorithms rely on the principle that frequently used words should not be split into smaller
subwords, but rare words should be decomposed into meaningful subwords. For instance "annoyingly" might be
considered a rare word and could be decomposed into "annoying" and "ly". Both "annoying" and "ly"... |
[
0.0037025667,
-0.011693718,
-0.020177657,
-0.002993352,
-0.020497302,
-0.04480372,
0.025891328,
0.015409603,
-0.022388542,
0.04104788,
0.017753674,
-0.017007833,
0.016768098,
-0.04086142,
0.01421093,
0.015968984,
-0.03596018,
-0.07277275,
-0.026277568,
0.02108332,
-0.01786022... | from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
tokenizer.tokenize("I have a new GPU!")
["i", "have", "a", "new", "gp", "##u", "!"] |
[
0.014935021,
0.011443626,
-0.0007042502,
0.0028468566,
-0.0049771694,
-0.052209347,
0.034337904,
-0.005001757,
-0.0030207238,
0.03577099,
0.026413772,
0.014063928,
0.033438712,
-0.029336147,
-0.022634355,
0.01274324,
-0.014323851,
-0.052799445,
-0.04880928,
0.029139448,
-0.00... | from transformers import XLNetTokenizer
tokenizer = XLNetTokenizer.from_pretrained("xlnet/xlnet-base-cased")
tokenizer.tokenize("Don't you love 🤗 Transformers? We sure do.")
["▁Don", "'", "t", "▁you", "▁love", "▁", "🤗", "▁", "Transform", "ers", "?", "▁We", "▁sure", "▁do", "."] |
[
0.0017352782,
-0.030237338,
0.009832317,
0.014074725,
-0.025113871,
-0.049250435,
0.045666967,
-0.0008759684,
-0.025691371,
0.054877363,
0.032843493,
0.02253733,
-0.002769041,
-0.031540416,
-0.02311483,
0.025676563,
-0.009151163,
-0.06468006,
-0.051145818,
0.027616374,
0.0045... |
Because we are considering the uncased model, the sentence was lowercased first. We can see that the words ["i", "have", "a", "new"] are present in the tokenizer's vocabulary, but the word "gpu" is not. Consequently, the
tokenizer splits "gpu" into known subwords: ["gp" and "##u"]. "##" means that the rest of the tok... |
[
0.044493098,
0.001740785,
-0.013378992,
0.009466616,
-0.01076088,
-0.026698817,
0.034109402,
-0.006837412,
-0.012484101,
0.024568828,
0.05129723,
0.014821171,
0.0069816294,
-0.03073692,
-0.03697897,
0.01276514,
0.00058472995,
-0.016670119,
-0.048102245,
0.008135374,
0.0282667... | We'll get back to the meaning of those "▁" when we look at SentencePiece. As one can see,
the rare word "Transformers" has been split into the more frequent subwords "Transform" and "ers".
Let's now look at how the different subword tokenization algorithms work. Note that all of those tokenization
algorithms rely on so... |
[
0.028128233,
-0.0005022204,
0.026603583,
0.00015484725,
-0.024083244,
-0.026603583,
0.012274986,
-0.0006018866,
-0.014227471,
0.04284577,
0.00933459,
-0.011932719,
-0.015440969,
-0.021811826,
-0.03344895,
-0.00258257,
-0.028314924,
-0.04284577,
-0.013558493,
-0.010174704,
0.0... |
Byte-Pair Encoding (BPE)
Byte-Pair Encoding (BPE) was introduced in Neural Machine Translation of Rare Words with Subword Units (Sennrich et
al., 2015). BPE relies on a pre-tokenizer that splits the training data into
words. Pretokenization can be as simple as space tokenization, e.g. GPT-2, RoBERTa. More advanced pr... |
[
0.03333755,
-0.009766252,
-0.010015272,
0.009914108,
-0.009454977,
-0.031002985,
0.036761574,
-0.015882805,
-0.0025271636,
0.024341702,
0.013711662,
-0.010458839,
0.006676848,
-0.025197709,
-0.030333744,
-0.0015417838,
-0.04525938,
-0.02076204,
-0.016544264,
0.0014892562,
0.0... |
WordPiece
WordPiece is the subword tokenization algorithm used for BERT, DistilBERT, and Electra. The algorithm was outlined in Japanese and Korean
Voice Search (Schuster et al., 2012) and is very similar to
BPE. WordPiece first initializes the vocabulary to include every character present in the training data and
pr... |
[
0.042859323,
-0.028620083,
0.004779192,
-0.013751485,
-0.029170772,
-0.009117833,
0.04304813,
-0.03360775,
-0.040939778,
0.05031722,
0.008047923,
0.02865155,
-0.054502457,
-0.019431448,
-0.044495653,
0.01600931,
-0.022326497,
0.009039164,
-0.026134117,
-0.009479715,
0.0056012... |
Unigram
Unigram is a subword tokenization algorithm introduced in Subword Regularization: Improving Neural Network Translation
Models with Multiple Subword Candidates (Kudo, 2018). In contrast to BPE or
WordPiece, Unigram initializes its base vocabulary to a large number of symbols and progressively trims down each
s... |
[
0.021587929,
-0.005353522,
-0.0053100614,
0.013441093,
-0.014357711,
-0.01898031,
0.018917095,
0.002866406,
-0.010240833,
0.03413611,
0.020987386,
0.027624963,
-0.032555733,
-0.031338844,
-0.03530559,
-0.0035874522,
-0.024811896,
-0.0323977,
-0.06264608,
0.011694779,
0.016751... |
SentencePiece
All tokenization algorithms described so far have the same problem: It is assumed that the input text uses spaces to
separate words. However, not all languages use spaces to separate words. One possible solution is to use language
specific pre-tokenizers, e.g. XLM uses a specific Chinese, Japanese, and ... |
[
0.008040757,
-0.006451646,
-0.053692926,
0.0077624796,
-0.0012394337,
0.00095200224,
-0.011314181,
-0.006653031,
-0.011240949,
0.055157542,
0.0049247807,
-0.000009768893,
0.034682173,
-0.08125705,
0.014214125,
0.034945805,
-0.010223039,
0.003870255,
-0.06842699,
0.02120768,
0... | DeepSpeed
DeepSpeed is a PyTorch optimization library that makes distributed training memory-efficient and fast. At it's core is the Zero Redundancy Optimizer (ZeRO) which enables training large models at scale. ZeRO works in several stages:
ZeRO-1, optimizer state partioning across GPUs
ZeRO-2, gradient partitioning ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.