vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
0.018480953,
-0.0018012468,
-0.023837127,
0.03380277,
-0.00907534,
0.009843585,
-0.003277605,
0.01213396,
0.012234477,
0.04282067,
0.031993445,
-0.0030586193,
0.033371978,
-0.05155138,
-0.016039802,
0.06398689,
-0.024813589,
-0.02321966,
-0.06054056,
-0.021338537,
0.016958823... | Let's check out 3 important ones:
Device
If you use device=n, the pipeline automatically puts the model on the specified device.
This will work regardless of whether you are using PyTorch or Tensorflow.
py
transcriber = pipeline(model="openai/whisper-large-v2", device=0)
If the model is too large for a single GPU and y... |
[
0.0043136957,
-0.0046701394,
-0.03503332,
0.027395241,
-0.023598025,
-0.05060045,
-0.049291063,
0.038030356,
0.012504625,
0.024412753,
-0.020310013,
0.05505236,
-0.0008592838,
-0.039979883,
-0.037710283,
-0.0035698921,
-0.039077863,
-0.022899685,
-0.040299956,
-0.0147087565,
... | transcriber = pipeline(model="openai/whisper-large-v2", chunk_length_s=30, return_timestamps=True)
transcriber("https://huggingface.co/datasets/sanchit-gandhi/librispeech_long/resolve/main/audio.wav")
{'text': " Chapter 16. I might have told you of the beginning of this liaison in a few lines, but I wanted you to see e... |
[
0.008059736,
0.022355048,
-0.032093592,
0.016235754,
-0.023241691,
0.0044659222,
-0.020203847,
0.0023801294,
0.021105025,
0.066570945,
0.018590447,
-0.026642915,
0.021395728,
-0.050785784,
-0.030988922,
0.046018258,
-0.035494816,
-0.054942835,
-0.05110556,
-0.011853408,
0.016... |
pip install --upgrade accelerate
The following code automatically loads and stores model weights across devices:
py
transcriber = pipeline(model="openai/whisper-large-v2", device_map="auto")
Note that if device_map="auto" is passed, there is no need to add the argument device=device when instantiating your pipeline ... |
[
0.008007085,
-0.009350069,
-0.0115423985,
0.033973854,
0.012369967,
-0.021676479,
-0.0146348905,
0.018032275,
0.018961474,
0.03275428,
0.017335376,
0.008391832,
0.017378932,
-0.028674513,
-0.031534705,
0.047273017,
-0.029022962,
-0.024130147,
-0.016551364,
-0.0071432204,
-0.0... | transcriber = pipeline(model="openai/whisper-large-v2", return_timestamps=True)
transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.', 'chunks': [{'timestamp': (0.0, 11.88), 'text... |
[
0.034114473,
0.01199131,
-0.03941466,
0.01573951,
-0.020951848,
-0.011178712,
0.00013909333,
-0.005402678,
0.011954706,
0.046149705,
0.037277013,
0.0039605,
0.008513977,
-0.070513,
-0.02764297,
0.026764486,
-0.010988373,
-0.038565457,
-0.036779206,
-0.019121673,
0.03513937,
... | As you can see, the model inferred the text and also outputted when the various sentences were pronounced.
There are many parameters available for each task, so check out each task's API reference to see what you can tinker with!
For instance, the [~transformers.AutomaticSpeechRecognitionPipeline] has a chunk_length_s ... |
[
-0.014246681,
0.027861847,
0.0061459956,
0.035575353,
-0.023877287,
-0.036868457,
-0.037981126,
-0.010156868,
-0.005461854,
0.051694024,
-0.014427113,
-0.008089408,
0.014036176,
-0.034613043,
-0.013186637,
0.012201775,
0.0010666215,
-0.018840201,
-0.044957865,
-0.02462909,
0.... | KeyDataset is a util that will just output the item we're interested in.
from transformers.pipelines.pt_utils import KeyDataset
from datasets import load_dataset
pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0)
dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split... |
[
0.022564529,
0.0007215905,
-0.01383099,
0.009682385,
-0.0060116667,
-0.0059285564,
-0.010250308,
0.004321751,
0.0149460565,
0.083387636,
-0.011330745,
-0.022924675,
0.019018477,
-0.0004952007,
-0.03260706,
0.009419202,
-0.0131938085,
-0.03440779,
-0.04194315,
0.0060843886,
-0... | If you can't find a parameter that would really help you out, feel free to request it!
Using pipelines on a dataset
The pipeline can also run inference on a large dataset. The easiest way we recommend doing this is by using an iterator:
def data():
for i in range(1000):
yield f"My example {i}"
pipe = pipel... |
[
0.014081967,
0.01816233,
0.021206297,
-0.0015989881,
-0.04548555,
-0.01995972,
0.002779431,
0.016524388,
-0.013683352,
0.0429924,
0.007834589,
-0.02632306,
0.0285553,
-0.02569977,
-0.0062510017,
0.019843759,
-0.020409068,
-0.052385207,
-0.055516142,
-0.0238444,
-0.013879036,
... |
from transformers import pipeline
vision_classifier = pipeline(model="google/vit-base-patch16-224")
preds = vision_classifier(
images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for ... |
[
-0.011921249,
0.013283878,
-0.0030484437,
0.029796135,
-0.0009092922,
-0.014730359,
-0.011697639,
-0.0013556402,
0.008196732,
0.07591586,
-0.024569234,
-0.027014976,
0.033318006,
-0.032004293,
-0.023758644,
-0.01417832,
-0.010873074,
-0.044554446,
-0.055092104,
0.0034572321,
... |
The iterator data() yields each result, and the pipeline automatically
recognizes the input is iterable and will start fetching the data while
it continues to process it on the GPU (this uses DataLoader under the hood).
This is important because you don't have to allocate memory for the whole dataset
and you can feed... |
[
0.007958346,
0.024035178,
-0.008759054,
0.014301349,
-0.004184572,
-0.018938495,
-0.019606913,
-0.024411164,
0.008438771,
0.056035668,
-0.016390154,
-0.016961094,
-0.015930617,
-0.062107123,
-0.038406156,
-0.020233555,
-0.03205619,
-0.012275209,
-0.04815391,
-0.0013794814,
0.... | Text pipeline
Using a [pipeline] for NLP tasks is practically identical. |
[
0.035668306,
0.025196346,
-0.012489693,
-0.014008417,
-0.04472279,
0.001300859,
-0.0271056,
0.0127717415,
-0.012496925,
0.08273427,
-0.005518029,
-0.037375063,
0.026367933,
0.0068776477,
-0.006906576,
0.03309371,
-0.020842671,
-0.032341577,
-0.050363764,
-0.0071343845,
0.0244... | Using pipelines for a webserver
Creating an inference engine is a complex topic which deserves it's own
page.
Link
Vision pipeline
Using a [pipeline] for vision tasks is practically identical.
Specify your task and pass your image to the classifier. The image can be a link, a local path or a base64-encoded image. For... |
[
0.04685465,
0.0054352595,
-0.025670687,
-0.009522997,
0.0070236805,
-0.025791135,
-0.009071313,
-0.009786478,
0.0003608764,
0.04489735,
-0.014536684,
-0.0060751447,
0.028109778,
0.000752806,
-0.006843007,
0.06799344,
0.014047361,
-0.031497404,
-0.025098553,
-0.019121274,
-0.0... | from transformers import pipeline
vqa = pipeline(model="impira/layoutlm-document-qa")
vqa(
image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png",
question="What is the invoice number?",
)
[{'score': 0.42515, 'answer': 'us-001', 'start': 16, 'end': ... |
[
0.016206108,
-0.012493696,
-0.016406007,
-0.028185777,
-0.020860903,
0.010501844,
0.009473791,
-0.00225065,
0.018847633,
0.046462268,
-0.009916425,
-0.04492019,
0.062082957,
-0.04149335,
0.021489156,
0.05160253,
-0.033097584,
-0.056542896,
-0.040922206,
-0.0041157803,
0.02491... | pip install accelerate
import torch
from transformers import pipeline
pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto")
output = pipe("This is a cool example!", do_sample=True, top_p=0.95)
You can also pass 8-bit loaded models if you install bitsandbytes and add the argument loa... |
[
0.0037564794,
-0.021433366,
0.009851828,
-0.00030293432,
0.012438873,
-0.017537758,
0.029901426,
0.0016263037,
0.017838577,
0.056343433,
0.0012126773,
-0.0090621775,
0.040490262,
-0.017026365,
0.0090095345,
0.007245982,
-0.024591967,
-0.031435605,
-0.04912377,
-0.044430993,
0... |
from transformers import pipeline
This model is a zero-shot-classification model.
It will classify text, except you are free to choose any label you might imagine
classifier = pipeline(model="facebook/bart-large-mnli")
classifier(
"I have a problem with my iphone that needs to be resolved asap!!",
candidate... |
[
0.028477926,
0.010763716,
-0.033973664,
0.04258464,
-0.013585058,
-0.0145402,
-0.017971365,
0.014165491,
0.0021196804,
0.08034948,
-0.006303937,
0.0005602275,
0.023628743,
-0.011968664,
0.0045663132,
0.022967491,
-0.019587759,
-0.022394406,
-0.054193288,
0.000051861225,
-0.00... | Multimodal pipeline
The [pipeline] supports more than one modality. For example, a visual question answering (VQA) task combines text and image. Feel free to use any image link you like and a question you want to ask about the image. The image can be a URL or a local path to the image.
For example, if you use this invo... |
[
0.036000628,
-0.01472753,
-0.005009657,
-0.01922043,
0.0030269667,
0.010012137,
0.01108871,
0.0060969964,
0.036029335,
0.040651426,
-0.052077465,
-0.050555907,
0.044929013,
-0.001784421,
-0.010880573,
0.06539827,
-0.01669407,
-0.04963723,
-0.05891012,
-0.022765948,
0.01632085... | To run the example above you need to have pytesseract installed in addition to 🤗 Transformers:
sudo apt install -y tesseract-ocr
pip install pytesseract
Using pipeline on large models with 🤗 accelerate:
You can easily run pipeline on large models using 🤗 accelerate! First make sure you have installed accelerate wi... |
[
0.013274603,
0.033385057,
-0.03488838,
0.012459122,
-0.0077009713,
0.017004538,
-0.047397137,
0.011026713,
-0.002832908,
0.052360933,
-0.0263932,
0.005236802,
0.056190144,
-0.02664848,
0.05185037,
0.051368173,
-0.03948343,
-0.021939969,
-0.06620283,
0.024563687,
-0.003822121,... | pip install accelerate
FSDP configuration
To start, run the accelerate config command to create a configuration file for your training environment. Accelerate uses this configuration file to automatically setup the correct training environment based on your selected training options in accelerate config. |
[
-0.014612045,
-0.009007994,
-0.005621348,
0.009824387,
-0.03561686,
-0.074499294,
0.0045697233,
0.0031496845,
-0.034343842,
0.055846795,
-0.017725408,
0.02487922,
0.052470528,
-0.0029352084,
-0.0108621735,
0.0009374679,
-0.025225151,
0.010315606,
-0.09863131,
0.012391181,
0.0... |
FULL_SHARD - shards model parameters, gradients and optimizer states across workers; select 1 for this option
SHARD_GRAD_OP- shard gradients and optimizer states across workers; select 2 for this option
NO_SHARD - don't shard anything (this is equivalent to DDP); select 3 for this option
HYBRID_SHARD - shard model pa... |
[
0.04281636,
-0.02631859,
-0.0070549687,
-0.0025562565,
-0.008458477,
-0.0120514585,
0.038983848,
-0.02039017,
0.010367248,
0.04018151,
-0.020704554,
-0.051259868,
0.05982314,
-0.048804663,
0.018698474,
0.043684665,
-0.008683038,
-0.049822673,
-0.0273366,
-0.0110259615,
0.0243... | You can also pass 8-bit loaded models if you install bitsandbytes and add the argument load_in_8bit=True
pip install accelerate bitsandbytes
import torch
from transformers import pipeline
pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True})
output = pipe("This is a cool ex... |
[
0.009807715,
0.010185758,
-0.016648151,
0.062255807,
-0.0032222802,
-0.0069795265,
-0.05335397,
-0.016419899,
-0.022796696,
0.041427787,
-0.030557273,
-0.022725368,
0.058832023,
-0.004447353,
-0.0011778175,
0.027376007,
-0.049216896,
-0.024009285,
-0.06191343,
-0.011769258,
0... |
Fully Sharded Data Parallel
Fully Sharded Data Parallel (FSDP) is a data parallel method that shards a model's parameters, gradients and optimizer states across the number of available GPUs (also called workers or rank). Unlike DistributedDataParallel (DDP), FSDP reduces memory-usage because a model is replicated on e... |
[
-0.016144361,
-0.023396535,
0.0035433336,
0.04911015,
-0.03812657,
-0.0538346,
-0.037554823,
-0.015219032,
-0.025457837,
0.034966912,
-0.025773803,
-0.006251614,
0.04820739,
0.00290764,
-0.0029132823,
0.059040517,
-0.026480965,
0.004716922,
-0.08443817,
0.019018147,
0.0073349... | directory containing checkpoints
accelerator.load_state("ckpt")
However, when training ends, you want to save the full state dict because sharded state dict is only compatible with FSDP.
if trainer.is_fsdp_enabled:
trainer.accelerator.state.fsdp_plugin.set_state_dict_type("FULL_STATE_DICT")
trainer.save_model(scr... |
[
0.015781347,
0.008488986,
-0.030098591,
0.04026285,
-0.02483345,
-0.00060799014,
-0.05439708,
0.01690758,
-0.034124877,
0.042233758,
-0.02069454,
0.010847037,
0.053693183,
-0.00016266597,
0.0032432007,
0.046597913,
-0.027099995,
0.009002829,
-0.065884665,
0.01407792,
0.006028... | accelerate config
When you run accelerate config, you'll be prompted with a series of options to configure your training environment. This section covers some of the most important FSDP options. To learn more about the other available FSDP options, take a look at the fsdp_config parameters.
Sharding strategy
FSDP offer... |
[
0.027005088,
0.028038707,
-0.01863424,
0.07238237,
-0.03601649,
-0.025869563,
-0.039102785,
0.016858162,
-0.007897714,
0.039102785,
-0.020847054,
0.0019835273,
0.06126006,
0.012796481,
0.0045166193,
0.054505147,
0.006562018,
-0.021735093,
-0.058756083,
-0.0057722465,
0.000112... | accelerate launch my-trainer-script.py
accelerate launch --fsdp="full shard" --fsdp_config="path/to/fsdp_config/ my-trainer-script.py
Next steps
FSDP can be a powerful tool for training really large models and you have access to more than one GPU or TPU. By sharding the model parameters, optimizer and gradient states,... |
[
0.016923474,
-0.0012279837,
-0.03929548,
0.051265743,
0.007195916,
0.0075467685,
-0.038580015,
0.005128638,
0.034452338,
0.06263061,
-0.027201388,
-0.027187629,
0.037782,
-0.014460626,
0.006683396,
0.034865107,
-0.0036805114,
-0.025096273,
-0.018725893,
-0.011261677,
-0.00440... | Follow along with the more in-depth Accelerate guide for FSDP.
Read the Introducing PyTorch Fully Sharded Data Parallel (FSDP) API blog post.
Read the Scaling PyTorch models on Cloud TPUs with FSDP blog post. |
[
-0.0009830787,
-0.002250241,
-0.0024446142,
0.059328616,
-0.009143006,
-0.012230546,
-0.010466238,
-0.008328135,
-0.009030868,
0.04515434,
0.0075356914,
-0.006186294,
0.053975884,
-0.029425079,
-0.010840032,
0.05436463,
-0.019377492,
-0.031936977,
-0.07637363,
0.02248746,
0.0... |
This is enabled by the fsdp_sharding_strategy flag.
CPU offload
You could also offload parameters and gradients when they are not in use to the CPU to save even more GPU memory and help you fit large models where even FSDP may not be sufficient. This is enabled by setting fsdp_offload_params: true when running accele... |
[
0.020616181,
-0.0029508169,
-0.04376266,
-0.014369507,
0.012558044,
0.012708999,
-0.007134435,
-0.009941485,
0.01409635,
0.042181224,
0.017740842,
0.009021376,
-0.0018285359,
-0.022154488,
0.0132121835,
0.008971058,
-0.007957501,
-0.03401526,
-0.055436537,
0.0105812475,
0.019... |
The Transformer model family
Since its introduction in 2017, the original Transformer model has inspired many new and exciting models that extend beyond natural language processing (NLP) tasks. There are models for predicting the folded structure of proteins, training a cheetah to run, and time series forecasting. Wit... |
[
0.016618842,
-0.024991479,
-0.029894248,
-0.030119017,
0.025047671,
0.0067009195,
-0.0216059,
-0.013731968,
-0.008161916,
0.04118888,
0.006297038,
-0.015424757,
0.059788495,
-0.05032011,
-0.0026375211,
-0.012762653,
-0.007782619,
-0.027548224,
-0.061305683,
-0.015930487,
-0.0... |
Convolutional network
For a long time, convolutional networks (CNNs) were the dominant paradigm for computer vision tasks until the Vision Transformer demonstrated its scalability and efficiency. Even then, some of a CNN's best qualities, like translation invariance, are so powerful (especially for certain tasks) tha... |
[
0.0525896,
0.007873371,
0.009367679,
-0.04721511,
0.024637247,
0.01746708,
-0.032297146,
-0.0022712855,
0.021799317,
0.0423178,
0.02075707,
-0.008865391,
0.024273088,
-0.02048081,
0.0031126186,
0.0132353,
-0.006994366,
-0.03980636,
-0.03272409,
-0.010485271,
-0.0037326308,
... | Computer vision |
[
0.024155118,
-0.00008638458,
-0.0117808655,
0.060206883,
-0.017266061,
0.00806859,
-0.03936604,
-0.01730948,
-0.01046384,
0.06674859,
-0.023272276,
-0.008249501,
0.07942677,
0.027179934,
-0.017439734,
0.05256524,
-0.02179605,
-0.007786371,
-0.048744418,
0.030392898,
-0.019002... |
TPU
PyTorch XLA supports FSDP training for TPUs and it can be enabled by modifying the FSDP configuration file generated by accelerate config. In addition to the sharding strategies and wrapping options specified above, you can add the parameters shown below to the file.
yaml
xla: True # must be set to True to enable... |
[
0.013879723,
0.041587517,
-0.04681178,
0.005416117,
0.02043957,
-0.032113,
-0.037101142,
-0.007924946,
-0.0013817003,
0.02793654,
0.0051172716,
-0.0062462445,
0.024409423,
-0.045513093,
-0.0105297,
-0.0044716173,
-0.02489643,
-0.02415854,
-0.07437938,
-0.030725766,
0.00623148... |
Encoder[[audio-encoder]]
Wav2Vec2 uses a Transformer encoder to learn speech representations directly from raw audio waveforms. It is pretrained with a contrastive task to determine the true speech representation from a set of false ones. HuBERT is similar to Wav2Vec2 but has a different training process. Target labe... |
[
0.011564079,
-0.016126469,
-0.027961582,
-0.012851486,
-0.009011851,
-0.009651789,
-0.007332951,
-0.036950845,
-0.017075084,
0.044208508,
0.031349495,
0.009621675,
0.046075627,
-0.021652533,
-0.018580824,
-0.014605672,
-0.036017288,
-0.037402567,
-0.06793896,
0.011134943,
0.0... |
Encoder[[nlp-encoder]]
BERT is an encoder-only Transformer that randomly masks certain tokens in the input to avoid seeing other tokens, which would allow it to "cheat". The pretraining objective is to predict the masked token based on the context. This allows BERT to fully use the left and right contexts to help it ... |
[
0.0068779592,
-0.0018490531,
-0.010676014,
-0.0016565591,
0.0026875127,
-0.012341828,
-0.028681608,
-0.0009772773,
-0.00080514327,
0.045991264,
-0.009891232,
0.02434309,
-0.0025560984,
-0.0019193875,
-0.0088621285,
-0.00025311112,
-0.027526645,
-0.025660934,
-0.042289454,
0.015... |
Decoder[[rl-decoder]]
The Decision and Trajectory Transformer casts the state, action, and reward as a sequence modeling problem. The Decision Transformer generates a series of actions that lead to a future desired return based on returns-to-go, past states, and actions. For the last K timesteps, each of the three mo... |
[
-0.0028153092,
-0.06327674,
-0.005439199,
-0.040046375,
0.016093673,
-0.027102092,
0.0068622036,
-0.005829261,
-0.017928408,
0.053539634,
-0.031985093,
-0.008451345,
0.033343084,
-0.014027788,
0.018766321,
-0.035741244,
-0.015327996,
-0.04464044,
-0.07489192,
0.0050021852,
-0... |
Encoder[[mm-encoder]]
VisualBERT is a multimodal model for vision-language tasks released shortly after BERT. It combines BERT and a pretrained object detection system to extract image features into visual embeddings, passed alongside text embeddings to BERT. VisualBERT predicts the masked text based on the unmasked ... |
[
0.039604023,
0.006372562,
-0.03933697,
-0.04764233,
-0.03319474,
-0.007617699,
0.014794759,
-0.0044531156,
-0.0016907819,
0.055653933,
0.014581117,
0.019241199,
0.013452816,
-0.047615625,
0.025637127,
0.05789718,
0.016824365,
-0.053063516,
-0.060407482,
0.01584962,
-0.0203895... | Custom hardware for training
The hardware you use to run model training and inference can have a big effect on performance. For a deep dive into GPUs make sure to check out Tim Dettmer's excellent blog post.
Let's have a look at some practical advice for GPU setups.
GPU
When you train bigger models you have essentially... |
[
0.0066309515,
0.05338389,
0.019574093,
0.009345684,
-0.045117095,
-0.053439938,
0.013836375,
-0.030264882,
0.020709027,
0.063836485,
0.018817471,
0.003005471,
0.017724572,
-0.054813065,
0.02267064,
0.05024531,
0.01207793,
-0.052234948,
-0.07050597,
0.018943574,
0.025514977,
... |
Let's start at the case where you have a single GPU.
Power and Cooling
If you bought an expensive high end GPU make sure you give it the correct power and sufficient cooling.
Power:
Some high end consumer GPU cards have 2 and sometimes 3 PCI-E 8-Pin power sockets. Make sure you have as many independent 12V PCI-E 8-Pi... |
[
0.02034477,
0.04063005,
0.0128641995,
0.019675534,
0.017177053,
-0.016820127,
0.0068633854,
-0.02284325,
-0.012098297,
0.06377074,
-0.0069674887,
0.014901651,
0.021400675,
-0.044585977,
0.008915708,
0.03899414,
0.012432914,
-0.062937915,
-0.07138515,
0.005268373,
0.0015243705... |
nvidia-smi topo -m
and it will tell you how the GPUs are inter-connected. On a machine with dual-GPU and which are connected with NVLink, you will most likely see something like:
GPU0 GPU1 CPU Affinity NUMA Affinity
GPU0 X NV2 0-23 N/A
GPU1 NV2 X 0-23 N/A
on a ... |
[
0.005401941,
0.07468474,
0.014819485,
0.02963897,
-0.020897523,
-0.013501438,
-0.0080858385,
-0.015461435,
-0.01595314,
0.070259385,
-0.019285819,
0.005798038,
0.015160947,
-0.042177483,
0.00596194,
0.04294236,
-0.028327752,
-0.054169655,
-0.03155116,
0.01716875,
0.0056546237... | Third-Generation NVLink®
GA102 GPUs utilize NVIDIA’s third-generation NVLink interface, which includes four x4 links,
with each link providing 14.0625 GB/sec bandwidth in each direction between two GPUs. Four
links provide 56.25 GB/sec bandwidth in each direction, and 112.5 GB/sec total bandwidth
between two GPUs. Two ... |
[
0.013766845,
0.008074915,
-0.025886025,
0.01856005,
-0.018914092,
-0.024347298,
0.0005408525,
0.0097021535,
0.02188261,
0.03957117,
0.0021991548,
-0.005896186,
0.04169543,
-0.017198343,
0.0025038365,
0.07761723,
-0.016136212,
-0.040497128,
-0.035186477,
0.0068391673,
-0.00699... | Hardware: 2x TITAN RTX 24GB each + NVlink with 2 NVLinks (NV2 in nvidia-smi topo -m)
Software: pytorch-1.8-to-be + cuda-11.0 / transformers==4.3.0.dev0 |
[
0.022511253,
0.016425451,
-0.0061634257,
-0.011993064,
-0.011845577,
0.0064428756,
0.032323055,
-0.034186054,
-0.056076307,
0.047630705,
-0.001983319,
0.052226108,
0.0069396757,
-0.020260127,
0.000673397,
-0.0006166337,
-0.004816632,
-0.048531156,
-0.038005203,
0.0047351257,
... |
Attention mechanisms
Most transformer models use full attention in the sense that the attention matrix is square. It can be a big
computational bottleneck when you have long texts. Longformer and reformer are models that try to be more efficient and
use a sparse version of the attention matrix to speed up training.
LS... |
[
0.039944977,
-0.01910948,
0.024641983,
-0.026614575,
0.015025599,
0.018770441,
0.0011038036,
0.0051510837,
-0.03114537,
0.057790767,
0.04154771,
0.008414336,
-0.0025986582,
-0.02370192,
-0.003945183,
-0.012197704,
0.032147076,
-0.03254776,
-0.058098983,
0.0022962198,
0.058961... |
Using those attention matrices with less parameters then allows the model to have inputs having a bigger sequence
length.
Other tricks
Axial positional encodings
Reformer uses axial positional encodings: in traditional transformer models, the positional encoding
E is a matrix of size \(l\) by \(d\), \(l\) being the s... |
[
0.007625229,
0.00088057405,
-0.0370768,
0.0058168606,
-0.01617388,
0.00063004333,
0.014075194,
-0.0006575886,
0.0012181129,
0.019965509,
0.009556022,
-0.007317422,
0.026065692,
-0.0632964,
0.005844843,
0.01161973,
-0.011773634,
-0.03231978,
-0.07286641,
0.007415361,
-0.004330... | Use tokenizers from 🤗 Tokenizers
The [PreTrainedTokenizerFast] depends on the 🤗 Tokenizers library. The tokenizers obtained from the 🤗 Tokenizers library can be
loaded very simply into 🤗 Transformers.
Before getting in the specifics, let's first start by creating a dummy tokenizer in a few lines:
thon |
[
-0.0015152316,
0.017379433,
0.010990378,
0.02201093,
0.036478702,
-0.010484987,
0.0057818294,
-0.038077854,
-0.0041713654,
0.071086705,
-0.028694166,
0.02364025,
0.04884948,
-0.06251768,
0.006294764,
0.033129543,
-0.014988251,
-0.05207795,
-0.06058663,
0.030776078,
0.01781693... |
So the higher X you get in the report of NVX in the output of nvidia-smi topo -m the better. The generation will depend on your GPU architecture.
Let's compare the execution of a openai-community/gpt2 language model training over a small sample of wikitext.
The results are:
| NVlink | Time |
| ----- | ---: |
| Y ... |
[
0.018632118,
0.0026069279,
-0.00037936002,
-0.021332819,
-0.029953243,
-0.023283327,
0.0041908626,
0.02141466,
-0.032653946,
0.06847235,
0.0032480035,
-0.00035442456,
0.015672257,
-0.035927523,
-0.0052684154,
0.0012489043,
-0.020582624,
-0.05311381,
-0.067817636,
-0.020514425,
... | from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from tokenizers.pre_tokenizers import Whitespace
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"])
tokenizer.pre_tokenizer = White... |
[
0.017655248,
0.0021603694,
-0.023440098,
-0.011405033,
-0.02611774,
-0.010216561,
0.0045749,
0.005086802,
0.025917275,
0.021965249,
0.0010989783,
-0.0043529565,
0.0067835962,
-0.03244671,
0.015450132,
0.012242691,
-0.013574352,
-0.02759259,
-0.05856445,
-0.003257558,
-0.02564... | We now have a tokenizer trained on the files we defined. We can either continue using it in that runtime, or save it to
a JSON file for future re-use.
Loading directly from the tokenizer object
Let's see how to leverage this tokenizer object in the 🤗 Transformers library. The
[PreTrainedTokenizerFast] class allows for... |
[
0.016950583,
-0.0028351445,
-0.035631683,
-0.011539162,
-0.024284014,
-0.0033245126,
0.015092401,
0.0024273375,
0.025461335,
0.04411407,
-0.0078157075,
-0.0044504143,
0.013071097,
-0.045929696,
0.015007294,
0.019986084,
-0.03131957,
-0.035518207,
-0.050724085,
0.0059007886,
-... | from transformers import PreTrainedTokenizerFast
fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer)
This object can now be used with all the methods shared by the 🤗 Transformers tokenizers! Head to the tokenizer
page for more information.
Loading from a JSON file
In order to load a tokenizer from a ... |
[
0.04555247,
0.011480128,
-0.034114808,
-0.011819861,
-0.053507872,
-0.009172779,
0.009038301,
0.0065681622,
0.012952303,
0.040145062,
-0.008557013,
0.002057859,
0.01937891,
-0.03632307,
0.022903634,
0.022705458,
-0.008188969,
-0.016222227,
-0.054923426,
0.005824997,
-0.030943... | tokenizer.save("tokenizer.json")
The path to which we saved this file can be passed to the [PreTrainedTokenizerFast] initialization
method using the tokenizer_file parameter:
thon
from transformers import PreTrainedTokenizerFast
fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json")
This object ca... |
[
0.049676374,
-0.0034568433,
-0.017589688,
-0.02003347,
-0.018901322,
0.0074763084,
0.0042386455,
-0.0067480067,
0.012163671,
0.033053152,
0.056828227,
-0.024078822,
0.023885528,
-0.04274543,
0.0053155655,
0.056137893,
-0.036145844,
-0.036863793,
-0.035317443,
-0.0040142876,
0... | Instantiating a big model
When you want to use a very big pretrained model, one challenge is to minimize the use of the RAM. The usual workflow
from PyTorch is:
Create your model with random weights.
Load your pretrained weights.
Put those pretrained weights in your random model. |
[
0.02566241,
-0.011810787,
-0.017991187,
-0.023983425,
-0.008308078,
0.013757541,
0.011477886,
-0.007924518,
0.013214766,
0.046577346,
0.052685376,
-0.005974145,
0.015110861,
-0.076306954,
0.01344635,
0.051266924,
-0.024374222,
-0.038558748,
-0.040758796,
-0.029367754,
0.00961... | Create your model with random weights.
Load your pretrained weights.
Put those pretrained weights in your random model.
Step 1 and 2 both require a full version of the model in memory, which is not a problem in most cases, but if your model starts weighing several GigaBytes, those two copies can make you get out of RA... |
[
0.03776464,
-0.015164862,
0.000040596293,
0.03395867,
-0.03862024,
-0.029547878,
-0.0039092298,
-0.013357765,
-0.029960928,
0.05204439,
0.027217092,
-0.020593528,
0.042780254,
-0.030742774,
-0.021434382,
0.036259953,
-0.0039424216,
-0.009433784,
-0.048680976,
-0.0158877,
0.00... |
In this guide, we explore the solutions Transformers offer to deal with this issue. Note that this is an area of active development, so the APIs explained here may change slightly in the future.
Sharded checkpoints
Since version 4.18.0, model checkpoints that end up taking more than 10GB of space are automatically sh... |
[
0.014178297,
0.018182643,
0.0003621781,
-0.028925668,
-0.01604514,
-0.01497639,
0.007960111,
0.013657802,
-0.031784926,
0.07017667,
0.060294196,
-0.0021062717,
0.0184186,
-0.09194032,
-0.010035153,
0.028925668,
-0.014212997,
-0.04030717,
-0.024470227,
-0.0059648785,
0.0233598... | Note that the randomly created model is initialized with "empty" tensors, which take the space in memory without filling it (thus the random values are whatever was in this chunk of memory at a given time). The random initialization following the appropriate distribution for the kind of model/parameters instantiated (l... |
[
0.045694824,
-0.01793536,
-0.0038999144,
0.0015650972,
-0.042552244,
0.009548066,
-0.0010563743,
0.014325638,
-0.011105201,
0.031482432,
0.03989096,
-0.030689707,
0.028226605,
-0.062455256,
0.029528935,
0.06494667,
-0.0017677017,
-0.0074459347,
-0.042778734,
-0.016321601,
-0.... | from transformers import AutoModel
model = AutoModel.from_pretrained("google-bert/bert-base-cased")
If you save it using [~PreTrainedModel.save_pretrained], you will get a new folder with two files: the config of the model and its weights:
import os
import tempfile
with tempfile.TemporaryDirectory() as tmp_dir:
... |
[
0.023751564,
-0.021646082,
0.0019883101,
0.044763118,
-0.07435523,
-0.02148745,
-0.008703138,
0.002860787,
-0.011904624,
0.05872275,
0.016036272,
-0.012697785,
0.054771364,
-0.03637003,
-0.013375577,
0.044878487,
-0.0033979735,
0.007866714,
-0.049291342,
-0.00938814,
-0.00474... | Now let's use a maximum shard size of 200MB:
with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir, max_shard_size="200MB")
print(sorted(os.listdir(tmp_dir)))
['config.json', 'pytorch_model-00001-of-00003.bin', 'pytorch_model-00002-of-00003.bin', 'pytorch_model-00003-of-00003.bin', 'py... |
[
0.015026037,
-0.012641776,
0.0059792185,
0.04040615,
-0.06488752,
-0.01833132,
-0.017142903,
0.004489985,
0.006384023,
0.065362886,
0.020723006,
-0.0028299158,
0.024763621,
-0.069462925,
-0.016236736,
0.0522606,
-0.022030264,
0.019029513,
-0.052943937,
-0.0144244,
-0.00516218... | On top of the configuration of the model, we see three different weights files, and an index.json file which is our index. A checkpoint like this can be fully reloaded using the [~PreTrainedModel.from_pretrained] method:
with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir, max_shard_size=... |
[
0.029987846,
-0.016922476,
0.0014225933,
0.030353736,
-0.051499207,
-0.007981005,
0.011464596,
-0.0029195081,
0.002384011,
0.06945837,
0.02498733,
-0.032716785,
0.011632296,
-0.070434086,
-0.028387072,
0.039424792,
-0.021602836,
-0.009116793,
-0.038205154,
0.00010886221,
0.02... | The main advantage of doing this for big models is that during step 2 of the workflow shown above, each shard of the checkpoint is loaded after the previous one, capping the memory usage in RAM to the model size plus the size of the biggest shard.
Behind the scenes, the index file is used to determine which keys are in... |
[
0.010825648,
-0.03081146,
-0.008236577,
0.012884793,
-0.01618548,
-0.011877932,
-0.012165606,
-0.022362912,
-0.027919574,
0.052023675,
0.0359139,
-0.018290047,
0.054960985,
-0.058715895,
-0.023135092,
0.05668703,
-0.02490656,
0.008728652,
-0.039335713,
-0.007964043,
0.0182294... | index["weight_map"]
{'embeddings.LayerNorm.bias': 'pytorch_model-00001-of-00003.bin',
'embeddings.LayerNorm.weight': 'pytorch_model-00001-of-00003.bin',
If you want to directly load such a sharded checkpoint inside a model without using [~PreTrainedModel.from_pretrained] (like you would do model.load_state_dict() f... |
[
0.010949765,
-0.0051052547,
0.014309352,
0.034137502,
-0.056037035,
-0.027593995,
-0.0162929,
-0.029218893,
-0.021021208,
0.046785362,
0.0029753207,
-0.02166531,
0.05770585,
-0.05103059,
0.0062873317,
0.04174964,
-0.004142759,
0.0009743901,
-0.05310929,
-0.01341639,
-0.010327... | from transformers.modeling_utils import load_sharded_checkpoint
with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir, max_shard_size="200MB")
load_sharded_checkpoint(model, tmp_dir) |
[
0.040849853,
-0.034001846,
-0.0060515543,
0.00074388337,
-0.044869334,
0.009110827,
-0.0035449602,
-0.010770724,
0.0072611207,
0.07634039,
0.017387984,
-0.032036763,
0.028582986,
-0.03480574,
-0.0017752714,
0.054367222,
-0.010927037,
0.00988495,
-0.051211186,
0.0021213936,
-0... | import json
with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir, max_shard_size="200MB")
with open(os.path.join(tmp_dir, "pytorch_model.bin.index.json"), "r") as f:
index = json.load(f)
print(index.keys())
dict_keys(['metadata', 'weight_map'])
The metadata just consists of t... |
[
0.033728264,
-0.023447866,
-0.05514449,
0.026579339,
-0.002921436,
0.007019084,
-0.023234008,
-0.018559711,
0.0031009228,
0.046773527,
0.011387872,
0.002759134,
0.026976502,
-0.07393333,
0.020820482,
0.032597877,
-0.020606626,
-0.04344347,
-0.03705832,
-0.034614243,
0.0088979... |
Efficient Training on CPU
This guide focuses on training large models efficiently on CPU.
Mixed precision with IPEX
Mixed precision uses single (fp32) and half-precision (bf16/fp16) data types in a model to accelerate training or inference while still preserving much of the single-precision accuracy. Modern CPUs such ... |
[
0.042339277,
0.005307162,
-0.023397245,
0.032219157,
-0.051957816,
-0.0019473117,
0.0055800807,
0.0110716475,
0.003630925,
0.04915487,
-0.008954683,
0.020373011,
0.0337239,
-0.06868699,
0.019945193,
0.06154685,
-0.017260263,
-0.0518398,
-0.040746022,
-0.0046580583,
0.01993044... | pip install intel_extension_for_pytorch==<version_name> -f https://developer.intel.com/ipex-whl-stable-cpu
You can check the latest versions in ipex-whl-stable-cpu if needed.
Check more approaches for IPEX installation.
Usage in Trainer
To enable auto mixed precision with IPEX in Trainer, users should add use_ipex, bf1... |
[
0.05490518,
-0.036487434,
0.0015236333,
-0.005670345,
0.00018025732,
0.007765907,
0.0018000806,
0.007178569,
-0.0023275968,
0.05214977,
0.041244145,
-0.013958329,
0.014799454,
-0.024943717,
0.002514312,
0.0530489,
0.008781058,
0.010731598,
-0.050960593,
0.0070045437,
0.002258... | The metadata just consists of the total size of the model for now. We plan to add other information in the future:
index["metadata"]
{'total_size': 433245184}
The weights map is the main part of this index, which maps each parameter name (as usually found in a PyTorch model state_dict) to the file it's stored in:
in... |
[
0.07345323,
-0.010553974,
-0.01634653,
0.034088124,
-0.019212479,
0.004719719,
-0.0028242494,
-0.003648779,
-0.009515257,
0.039698713,
-0.0018594556,
0.018666584,
0.04036592,
-0.055893604,
0.016240383,
0.045764215,
-0.014837735,
-0.038455285,
-0.026066497,
-0.011865639,
-0.01... |
python run_qa.py \
--model_name_or_path google-bert/bert-base-uncased \
--dataset_name squad \
--do_train \
--do_eval \
--per_device_train_batch_size 12 \
--learning_rate 3e-5 \
--num_train_epochs 2 \
--max_seq_length 384 \
--doc_stride 128 \
--output_dir /tmp/debug_squad/ \
--use_ipex \
--bf16 \
--use_cpu
If you wa... |
[
0.026020056,
0.004660902,
-0.0046175113,
0.047035698,
-0.050940875,
-0.008374437,
-0.0059120054,
-0.025195628,
0.0021297683,
0.056176707,
-0.012474873,
-0.021145815,
0.0008605855,
-0.040555995,
-0.0076295603,
0.049986277,
-0.028247453,
-0.050911948,
-0.06525986,
0.0018088566,
... | Low memory loading
Sharded checkpoints reduce the memory usage during step 2 of the workflow mentioned above, but in order to use that model in a low memory setting, we recommend leveraging our tools based on the Accelerate library.
Please read the following guide for more information: Large model loading using Acceler... |
[
0.053479675,
0.018988352,
-0.033961546,
-0.02262709,
-0.0074099186,
-0.016018808,
0.00742386,
0.00015063379,
-0.014987135,
0.052057642,
0.049548168,
0.0015117837,
0.008532211,
-0.053674858,
0.04405521,
0.038534366,
-0.0026349477,
-0.03142419,
-0.06474443,
-0.00093233923,
-0.0... | Model training anatomy
To understand performance optimization techniques that one can apply to improve efficiency of model training
speed and memory utilization, it's helpful to get familiar with how GPU is utilized during training, and how compute
intensity varies depending on an operation performed.
Let's start by ... |
[
0.035603583,
0.013117501,
-0.0076091895,
-0.004910711,
0.0068185763,
-0.009242381,
0.029545926,
-0.032782614,
-0.003889966,
0.034653362,
-0.0014169796,
-0.02868479,
0.033228032,
-0.03566297,
0.009598714,
0.04442282,
-0.008381244,
-0.046887454,
-0.014119687,
-0.032426283,
0.00... |
pip install transformers datasets accelerate nvidia-ml-py3
The nvidia-ml-py3 library allows us to monitor the memory usage of the models from within Python. You might be familiar
with the nvidia-smi command in the terminal - this library allows to access the same information in Python directly.
Then, we create some ... |
[
0.022668205,
0.00861939,
0.03488081,
0.009937142,
-0.032950986,
-0.032864578,
0.009677912,
0.0073736473,
-0.010477204,
0.06999204,
-0.0030657523,
-0.000006539814,
0.012234205,
-0.0023456695,
0.0036184157,
0.022408975,
-0.015611394,
-0.042168047,
-0.06313685,
-0.01636028,
-0.0... | import numpy as np
from datasets import Dataset
seq_len, dataset_size = 512, 512
dummy_data = {
"input_ids": np.random.randint(100, 30000, (dataset_size, seq_len)),
"labels": np.random.randint(0, 1, (dataset_size)),
}
ds = Dataset.from_dict(dummy_data)
ds.set_format("pt")
To print summary statistics for the... |
[
0.01955558,
0.002001494,
0.013249234,
0.053311925,
-0.033388857,
-0.062262867,
0.038953666,
0.019936193,
0.007841919,
0.04556844,
0.0481146,
0.0023263267,
0.02719407,
-0.016405689,
-0.015368849,
0.045962177,
-0.01387265,
-0.04464972,
-0.04467597,
-0.010066531,
-0.014240137,
... | Let's verify that we start with a free GPU memory:
print_gpu_utilization()
GPU memory occupied: 0 MB. |
[
0.014788279,
-0.007413928,
0.015579802,
0.035222754,
-0.017598184,
-0.03757094,
0.027887978,
0.02674027,
-0.022835426,
0.055775955,
0.057675608,
0.007783305,
0.020632355,
-0.03028893,
-0.011582613,
0.051237892,
0.015856834,
-0.050208915,
-0.062108137,
-0.020777468,
0.00316279... | print_gpu_utilization()
GPU memory occupied: 0 MB.
That looks good: the GPU memory is not occupied as we would expect before we load any models. If that's not the case on
your machine make sure to stop all processes that are using GPU memory. However, not all free GPU memory can be used by
the user. When a model is ... |
[
0.05025762,
-0.021968044,
0.010540656,
0.0012094224,
-0.018235192,
0.021767814,
0.0046267333,
0.014302111,
-0.0035558625,
0.039388016,
0.053890355,
0.00049789227,
0.017548691,
-0.04287773,
0.033209503,
0.06784922,
0.0034628988,
-0.027131105,
-0.018664256,
-0.011155647,
0.0088... | import torch
torch.ones((1, 1)).to("cuda")
print_gpu_utilization()
GPU memory occupied: 1343 MB.
We see that the kernels alone take up 1.3GB of GPU memory. Now let's see how much space the model uses.
Load Model
First, we load the google-bert/bert-large-uncased model. We load the model weights directly to the GPU so t... |
[
0.021144921,
-0.00648266,
0.025382133,
0.032087624,
-0.015975248,
0.015605007,
0.007775078,
-0.016564893,
0.005882731,
0.033650868,
0.028330356,
-0.03376057,
0.013486401,
-0.037243586,
0.0365031,
0.030963186,
-0.025272433,
-0.04851539,
-0.026438009,
-0.03732586,
-0.0030493527... | from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-large-uncased").to("cuda")
print_gpu_utilization()
GPU memory occupied: 2631 MB. |
[
0.030423809,
-0.00741217,
0.030063927,
0.016526856,
-0.025537727,
-0.011246289,
0.035046898,
0.020430183,
-0.017301984,
0.053677674,
0.04717213,
0.011260131,
0.0071145757,
-0.07070283,
0.020485548,
0.061788846,
0.020790065,
-0.056030747,
-0.067602314,
-0.027973847,
0.02805689... | We can see that the model weights alone take up 1.3 GB of GPU memory. The exact number depends on the specific
GPU you are using. Note that on newer GPUs a model can sometimes take up more space since the weights are loaded in an
optimized fashion that speeds up the usage of the model. Now we can also quickly check i... |
[
0.021768607,
0.016198326,
-0.007458513,
0.03644286,
-0.026664522,
-0.036415886,
0.030913042,
-0.0021023431,
0.022915034,
0.04806897,
0.031101864,
-0.0068178633,
0.045857042,
-0.032531526,
-0.0107696615,
0.038223192,
-0.005890607,
-0.061610285,
-0.054354083,
0.005843401,
-0.01... | from pynvml import *
def print_gpu_utilization():
nvmlInit()
handle = nvmlDeviceGetHandleByIndex(0)
info = nvmlDeviceGetMemoryInfo(handle)
print(f"GPU memory occupied: {info.used//1024**2} MB.")
def print_summary(result):
print(f"Time: {result.metrics['train_runtime']:.2f}")
print(f"Sample... |
[
0.037117526,
0.026800742,
-0.011504429,
0.027158467,
-0.052199204,
-0.02638578,
0.022479426,
0.023266422,
-0.018716162,
0.044901617,
0.035972804,
-0.0062673385,
0.023896016,
-0.019288521,
-0.018945105,
0.02571326,
0.0048257075,
-0.045130562,
-0.057264585,
0.0041782255,
0.0033... | We get the same number as before and you can also see that we are using a V100 GPU with 16GB of memory. So now we can
start training the model and see how the GPU memory consumption changes. First, we set up a few standard training
arguments:
py
default_args = {
"output_dir": "tmp",
"evaluation_strategy": "st... |
[
0.044960603,
0.019999716,
0.0015600554,
0.02534143,
-0.071316816,
-0.026962267,
0.031599272,
0.012614339,
-0.023086354,
0.044594154,
0.04073233,
0.010394497,
0.02460853,
-0.028780423,
-0.00058535114,
0.040083997,
-0.012050569,
-0.026356217,
-0.060548823,
0.0011830346,
-0.0231... | Memory utilization at vanilla training
Let's use the [Trainer] and train the model without using any GPU performance optimization techniques and a batch size of 4:
from transformers import TrainingArguments, Trainer, logging
logging.set_verbosity_error()
training_args = TrainingArguments(per_device_train_batch_size=4,... |
[
0.034503687,
-0.0025576034,
-0.010223314,
-0.011579324,
-0.021383766,
-0.0076319883,
0.010244613,
0.018827938,
-0.0057328655,
0.048077974,
0.045294963,
-0.0019576936,
0.032601014,
-0.029335232,
-0.00024204938,
0.062532604,
-0.0012069191,
-0.03890539,
-0.031095915,
0.0036953022,... |
Time: 57.82
Samples/second: 8.86
GPU memory occupied: 14949 MB.
We see that already a relatively small batch size almost fills up our GPU's entire memory. However, a larger batch size
can often result in faster model convergence or better end performance. So ideally we want to tune the batch size to our
model's need... |
[
0.019197997,
0.026093254,
-0.035506282,
-0.029698243,
0.020099245,
-0.012810586,
-0.0044847783,
-0.031186016,
-0.030928517,
0.014176763,
0.01803925,
0.03882516,
0.011473021,
-0.08760696,
-0.017581474,
-0.0056542535,
0.0010836426,
-0.016251061,
-0.009963789,
0.00035227323,
0.0... | Tensor Contractions
Linear layers and components of Multi-Head Attention all do batched matrix-matrix multiplications. These operations are the most compute-intensive part of training a transformer.
Statistical Normalizations
Softmax and layer normalization are less compute-intensive than tensor contractions, and invo... |
[
0.0013274738,
0.030280462,
-0.010482808,
0.025248136,
-0.014181349,
-0.04132562,
0.031578194,
-0.016178418,
0.009783473,
0.050525114,
0.030741878,
0.016264934,
0.045305338,
-0.05274568,
0.0006348984,
0.036278877,
0.024930913,
-0.044180635,
-0.05369735,
-0.020013943,
-0.007014... |
nvidia-smi
```bash
Tue Jan 11 08:58:05 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.91.03 Driver Version: 460.91.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| ... |
[
0.037258554,
0.023769308,
-0.0043110996,
0.0120577635,
-0.06583499,
-0.030920073,
0.028443271,
0.029588459,
-0.020094052,
0.056300633,
0.03917608,
-0.0060122367,
0.028762858,
-0.025021024,
-0.0040114867,
0.038883124,
-0.0006063004,
-0.06279891,
-0.060934648,
-0.005393036,
-0.... | If you plan to run multiple experiments, in order to properly clear the memory between experiments, restart the Python
kernel between experiments.
Memory utilization at vanilla training
Let's use the [Trainer] and train the model without using any GPU performance optimization techniques and a batch size of 4: |
[
0.049572907,
-0.015891595,
-0.04198041,
-0.011686626,
-0.04261774,
0.00657416,
0.0047695567,
0.025714735,
-0.014284424,
0.041564763,
0.05807983,
-0.015988579,
-0.007062546,
-0.032614484,
-0.0055419686,
0.042977966,
-0.01268418,
-0.02460634,
-0.034471042,
-0.019355325,
0.04727... | model weights
optimizer states
gradients
forward activations saved for gradient computation
temporary buffers
functionality-specific memory
A typical model trained in mixed precision with AdamW requires 18 bytes per model parameter plus activation memory. For
inference there are no optimizer states and gradients, so ... |
[
0.04670457,
0.016800001,
-0.01715533,
-0.0070390864,
-0.015535026,
-0.04514112,
0.0483533,
-0.006129442,
-0.00041951143,
0.042469036,
0.04212792,
-0.016615229,
0.019528935,
-0.05057056,
0.02933604,
0.010183757,
0.0117329955,
-0.010894417,
-0.068450764,
0.003491117,
0.01903147... | This knowledge can be helpful to know when analyzing performance bottlenecks.
This summary is derived from Data Movement Is All You Need: A Case Study on Optimizing Transformers 2020
Anatomy of Model's Memory
We've seen that training the model uses much more memory than just putting the model on the GPU. This is becaus... |
[
-0.003301776,
0.0016919889,
-0.037566263,
-0.012926248,
0.031099716,
-0.017906312,
0.032880757,
-0.036607243,
-0.01475524,
0.044005413,
0.026551211,
0.048115507,
-0.017673407,
-0.067953564,
-0.054198448,
-0.023331637,
-0.021852003,
-0.0014556585,
-0.041840762,
-0.034881003,
0... | Element-wise Operators
These are the remaining operators: biases, dropout, activations, and residual connections. These are the least compute-intensive operations. |
[
0.047549132,
-0.025176303,
0.0029116974,
0.0027330446,
-0.061291657,
-0.024571633,
0.005929899,
0.020806182,
-0.03251481,
0.039496016,
0.052523926,
0.002449605,
0.020380164,
-0.029024212,
0.007860724,
0.03174523,
0.01803019,
-0.016133724,
-0.072340645,
0.00015202667,
0.008011... | 4 bytes * number of parameters for fp32 training
6 bytes * number of parameters for mixed precision training (maintains a model in fp32 and one in fp16 in memory)
Optimizer States:
8 bytes * number of parameters for normal AdamW (maintains 2 states)
2 bytes * number of parameters for 8-bit AdamW optimizers like bitsa... |
[
0.05526922,
-0.0005942927,
0.025356488,
0.026488474,
0.0115604075,
0.0042661726,
-0.02269632,
0.019682407,
-0.0020216564,
0.084898956,
0.066390984,
0.0029378575,
-0.024196202,
-0.04963759,
-0.027210115,
0.06259883,
-0.0031854794,
-0.047713213,
0.001376955,
-0.006240073,
0.033... |
When working with approximate models, however, we typically have a constraint on the number of tokens the model can
process. The largest version of GPT-2, for example, has a fixed length of 1024 tokens, so we
cannot calculate \(p_\theta(x_t|x_{<t})\) directly when \(t\) is greater than 1024.
Instead, the sequence is ... |
[
0.03842341,
0.013078289,
-0.014014019,
0.0074419775,
-0.007409081,
-0.012968633,
-0.024489809,
0.017252522,
-0.021097787,
0.076028064,
0.07129093,
-0.013750845,
-0.016404517,
-0.06807436,
-0.022910764,
0.048599478,
-0.016565345,
-0.04356993,
-0.026712168,
-0.009569301,
0.0136... | This is quick to compute since the perplexity of each segment can be computed in one forward pass, but serves as a poor
approximation of the fully-factorized perplexity and will typically yield a higher (worse) PPL because the model will
have less context at most of the prediction steps.
Instead, the PPL of fixed-lengt... |
[
0.03850911,
-0.025302015,
0.008000719,
0.021006234,
-0.02552445,
0.0016769536,
0.013471237,
0.013450384,
-0.058889743,
0.054413233,
0.040177375,
0.034755513,
0.013304411,
-0.01878188,
-0.051660597,
0.046600193,
0.017447269,
-0.038147654,
-0.053690318,
-0.0058910595,
-0.002067... | Gradients
4 bytes * number of parameters for either fp32 or mixed precision training (gradients are always kept in fp32)
Forward Activations
size depends on many factors, the key ones being sequence length, hidden size and batch size. |
[
0.028039083,
-0.0035496287,
-0.0029455952,
0.020835426,
-0.01530218,
-0.02714422,
-0.013788369,
0.009493019,
-0.011126147,
0.05598868,
0.03484005,
-0.0212232,
-0.0049105682,
-0.05900139,
-0.038658142,
0.051335387,
-0.02559312,
-0.035317313,
-0.025265004,
0.00019621766,
0.0298... |
This is a closer approximation to the true decomposition of the sequence probability and will typically yield a more
favorable score. The downside is that it requires a separate forward pass for each token in the corpus. A good
practical compromise is to employ a strided sliding window, moving the context by larger s... |
[
0.04508842,
-0.011873872,
0.003058366,
0.011903226,
0.0023703713,
-0.009290681,
-0.016585259,
-0.006901963,
-0.06598879,
0.06968744,
0.032172468,
-0.025391592,
-0.023674358,
-0.043591347,
-0.02904622,
0.04256394,
-0.03871851,
-0.040391713,
-0.03302375,
0.00031395492,
0.023380... |
Perplexity of fixed-length models
[[open-in-colab]]
Perplexity (PPL) is one of the most common metrics for evaluating language models. Before diving in, we should note
that the metric applies specifically to classical language models (sometimes called autoregressive or causal language
models) and is not well defined f... |
[
0.015981946,
-0.013783491,
0.005961341,
0.030463241,
-0.00694802,
-0.007075575,
0.010804696,
-0.01069965,
-0.007371954,
0.061766848,
0.008478685,
-0.008020986,
0.018232925,
-0.0453197,
-0.028722484,
0.0063702688,
-0.026696604,
-0.0075820456,
-0.022284687,
0.0012464792,
0.0448... | We'll load in the WikiText-2 dataset and evaluate the perplexity using a few different sliding-window strategies. Since
this dataset is small and we're just doing one forward pass over the set, we can just load and encode the entire
dataset in memory.
thon
from datasets import load_dataset
test = load_dataset("wikitext... |
[
0.021105722,
0.0064522573,
-0.01828078,
0.011957937,
0.002538381,
-0.025039935,
0.020307047,
0.01318553,
-0.04043661,
0.040821157,
0.016772171,
0.03386973,
0.013451755,
-0.08412708,
-0.023324262,
-0.0029525086,
-0.0043002726,
-0.03499379,
-0.06667456,
0.004063628,
0.018561795... |
There are the input and output that are being passed and returned by the forward and the backward functions and the
forward activations saved for gradient computation.
Temporary Memory
Additionally, there are all kinds of temporary variables which get released once the calculation is done, but in the
moment these c... |
[
0.026029687,
-0.0074869944,
-0.01632864,
-0.0046028993,
0.024776999,
-0.019518623,
0.015877089,
-0.0042387457,
0.010050635,
0.077724904,
0.036502738,
-0.0092203645,
0.0050471663,
-0.03370604,
-0.000453371,
0.02074218,
0.004435389,
-0.0067914613,
-0.017348269,
-0.00467573,
0.0... | # loss is calculated using CrossEntropyLoss which averages over valid labels
# N.B. the model only calculates loss over trg_len - 1 labels, because it internally shifts the labels
# to the left by 1.
neg_log_likelihood = outputs.loss
nlls.append(neg_log_likelihood)
prev_end_loc = end_loc
if end_loc == seq... |
[
0.04025126,
0.04882198,
0.017551586,
0.021610655,
-0.01866889,
-0.0047662244,
-0.02883778,
0.016278706,
0.0066613997,
0.05498837,
0.0342263,
-0.013711735,
-0.025358576,
-0.051961746,
-0.026504168,
0.017678874,
-0.0149209695,
-0.03482031,
-0.043164738,
-0.0067250435,
0.0400249... |
Running this with the stride length equal to the max input length is equivalent to the suboptimal, non-sliding-window
strategy we discussed above. The smaller the stride, the more context the model will have in making each prediction,
and the better the reported perplexity will typically be.
When we run the above wit... |
[
0.038100816,
-0.008425546,
-0.036521025,
0.017594524,
-0.0067334673,
0.004441223,
-0.012808689,
0.019669933,
-0.017083414,
0.054301407,
0.02668606,
-0.011546406,
-0.013474679,
-0.03896815,
-0.013722489,
0.039154008,
0.0012032345,
-0.007596931,
-0.037264455,
-0.0040501477,
0.0... |
With 🤗 Transformers, we can simply pass the input_ids as the labels to our model, and the average negative
log-likelihood for each token is returned as the loss. With our sliding window approach, however, there is overlap in
the tokens we pass to the model at each iteration. We don't want the log-likelihood for the ... |
[
0.011577936,
0.024494868,
-0.02209341,
-0.036036417,
0.002354156,
-0.0047992766,
-0.0145979505,
-0.016591888,
-0.0043226234,
0.008849007,
0.020376002,
-0.032456063,
0.029865399,
-0.034289904,
-0.008674356,
0.025775645,
-0.053297803,
-0.0017647074,
-0.066775076,
-0.011214079,
... | The library was designed with two strong goals in mind:
Be as easy and fast to use as possible:
We strongly limited the number of user-facing abstractions to learn, in fact, there are almost no abstractions,
just three standard classes required to use each model: configuration,
models, and a preprocessing cla... |
[
0.01693003,
0.0065046586,
-0.03463417,
-0.059634995,
0.01305949,
-0.015353144,
0.026993437,
-0.019782763,
-0.002675332,
0.0044332026,
0.008049291,
-0.03985223,
0.05172189,
-0.039536856,
0.029817497,
0.004146496,
-0.01840657,
0.0008507126,
-0.04954292,
-0.015338809,
0.03391740... | Philosophy
🤗 Transformers is an opinionated library built for:
machine learning researchers and educators seeking to use, study or extend large-scale Transformers models.
hands-on practitioners who want to fine-tune those models or serve them in production, or both.
engineers who just want to download a pretrained mo... |
[
0.025586987,
0.013450353,
-0.017689392,
0.000022003735,
-0.044483166,
0.0074507776,
0.0018369165,
-0.009677228,
-0.009432814,
0.02323451,
0.004792786,
-0.016009051,
0.034462236,
-0.07564583,
-0.0064158416,
0.045552474,
-0.018208768,
-0.012808769,
-0.06684696,
-0.021355586,
0.... |
All of these classes can be initialized in a simple and unified way from pretrained instances by using a common
from_pretrained() method which downloads (if needed), caches and
loads the related class instance and associated data (configurations' hyperparameters, tokenizers' vocabulary,
and models' weight... |
[
0.039866354,
-0.014720969,
-0.035245802,
-0.0049692076,
0.012650172,
-0.03318909,
-0.022468848,
-0.0005577592,
-0.0035851547,
0.04733249,
0.0069343513,
-0.0008174892,
0.04600831,
-0.045670222,
-0.018369514,
0.025920175,
-0.0053354707,
-0.009346759,
-0.042542897,
0.021229187,
... | Provide state-of-the-art models with performances as close as possible to the original models:
We provide at least one example for each architecture which reproduces a result provided by the official authors
of said architecture.
The code is usually as close to the original code base as possible which means some ... |
[
0.02675329,
-0.019422002,
-0.034350634,
-0.056817483,
-0.008018597,
-0.031364907,
-0.01599285,
-0.0040905927,
-0.025866441,
0.015165125,
0.04522932,
0.0073904116,
0.030566743,
-0.043603428,
-0.007419973,
0.06071962,
-0.034971427,
-0.029044317,
-0.054718606,
-0.009178891,
0.00... | A few other goals:
Expose the models' internals as consistently as possible:
We give access, using a single API, to the full hidden-states and attention weights.
The preprocessing classes and base model APIs are standardized to easily switch between models.
Incorporate a subjective selection of promising tools for ... |
[
0.05037012,
0.0033495165,
-0.03737138,
-0.03175328,
0.005621542,
0.02062724,
-0.002831426,
-0.013370527,
0.004141283,
0.057613064,
0.04108924,
-0.033020105,
0.051141232,
-0.0671418,
0.017556561,
0.020269223,
0.01969089,
-0.031643122,
-0.056731794,
0.0068298467,
0.04772631,
... | As a consequence, this library is NOT a modular toolbox of building blocks for neural nets. If you want to
extend or build upon the library, just use regular Python, PyTorch, TensorFlow, Keras modules and inherit from the base
classes of the library to reuse functionalities like model loading and saving. If you... |
[
0.02270715,
-0.014872431,
-0.028180925,
-0.045654904,
-0.004894819,
0.023985367,
0.0042594704,
0.012744576,
0.014661901,
0.032180995,
0.044722557,
-0.0167672,
0.03654197,
-0.06634698,
0.0037481834,
0.06580562,
-0.02353423,
-0.04288794,
-0.05491822,
-0.016391253,
0.022225939,
... |
Model classes can be PyTorch models (torch.nn.Module), Keras models (tf.keras.Model) or JAX/Flax models (flax.linen.Module) that work with the pretrained weights provided in the library.
Configuration classes store the hyperparameters required to build a model (such as the number of layers and hidden size). You don't... |
[
0.04405839,
-0.0054592164,
-0.033672564,
-0.04559703,
0.0045419494,
-0.01279735,
-0.011510218,
-0.010119523,
-0.0029792674,
0.013855165,
0.041987143,
0.009638698,
0.052639272,
-0.052047487,
0.002135974,
0.053379003,
-0.019928358,
-0.032341048,
-0.026186485,
0.0047379783,
0.00... | A simple and consistent way to add new tokens to the vocabulary and embeddings for fine-tuning.
Simple ways to mask and prune Transformer heads.
Easily switch between PyTorch, TensorFlow 2.0 and Flax, allowing training with one framework and inference with another.
Main concepts
The library is built around three typ... |
[
0.004185555,
0.017065221,
-0.07030656,
-0.026849462,
0.01717289,
-0.05582535,
-0.015382925,
-0.005938509,
-0.0030079472,
0.03283844,
0.008505695,
-0.009239175,
0.019797273,
-0.031384934,
0.013330522,
0.01582705,
-0.020914318,
0.012462457,
0.014481214,
-0.028720176,
-0.0214795... | Transformers Agents
Transformers Agents is an experimental API which is subject to change at any time. Results returned by the agents
can vary as the APIs or underlying models are prone to change. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.