vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
0.020441942,
-0.0060866456,
-0.010113306,
-0.012754681,
-0.034079473,
-0.01297001,
-0.006485005,
0.0020958732,
-0.013659065,
0.050358377,
0.013903105,
0.012675727,
0.01394617,
-0.041486807,
-0.018719306,
0.012711614,
-0.0300887,
-0.058339924,
-0.0300887,
-0.02173392,
-0.02674... | from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
Then tokenize your input as shown below:
sequence = "In a hole in the ground there lived a hobbit."
print(tokenizer(sequence))
{'input_ids': [101, 1999, 1037, 4920, 1999, 1996, 2598, 2045, 2973, 1037, 757... |
[
-0.0000100364205,
-0.0070798355,
-0.017587801,
-0.039746445,
-0.020426834,
0.012804033,
-0.012094275,
0.00030075992,
0.0016280073,
0.07188428,
0.0074808486,
-0.020781713,
-0.018780194,
-0.03208106,
-0.008630657,
0.059790008,
-0.014429378,
-0.012349788,
-0.089259155,
0.022187034... | out_indices is the index of the layer you'd like to get the feature map from
out_features is the name of the layer you'd like to get the feature map from
These parameters can be used interchangeably, but if you use both, make sure they're aligned with each other! If you don't pass any of these parameters, the backbone... |
[
-0.016228378,
-0.024342567,
-0.0028080523,
-0.030901415,
-0.009933646,
0.0038810181,
-0.019749906,
-0.0147390645,
0.016272398,
0.050768707,
-0.0047247177,
-0.007989468,
0.019133639,
-0.025780525,
0.012229975,
0.058105223,
-0.030754684,
-0.024093125,
-0.064502664,
0.0055243983,
... | A feature map from the first stage of the backbone. The patch partition refers to the model stem.
For example, in the above diagram, to return the feature map from the first stage of the Swin backbone, you can set out_indices=(1,): |
[
-0.018296175,
0.039149266,
-0.037501473,
-0.008743241,
-0.026236827,
0.024035037,
-0.047217764,
0.006601822,
0.026677186,
0.03002959,
0.018296175,
-0.014460796,
0.027430056,
-0.052388422,
0.018296175,
0.042842593,
-0.020469556,
-0.039206088,
-0.05468965,
-0.026762417,
0.01180... | Now you can access the feature_maps object from the first stage of the backbone:
list(feature_maps[0].shape)
[1, 96, 56, 56]
AutoFeatureExtractor
For audio tasks, a feature extractor processes the audio signal the correct input format.
Load a feature extractor with [AutoFeatureExtractor.from_pretrained]:
from transf... |
[
0.0082299765,
0.007571289,
-0.022192707,
0.0074482374,
-0.03005353,
0.009576305,
-0.036654882,
0.007321567,
0.012876982,
0.028519005,
-0.01989092,
-0.037581388,
0.0147879,
-0.03882638,
0.0056314175,
0.057066962,
0.0013309472,
-0.060628217,
-0.045977846,
-0.05347675,
-0.007947... | from transformers import AutoFeatureExtractor
feature_extractor = AutoFeatureExtractor.from_pretrained(
"ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
)
AutoProcessor
Multimodal tasks require a processor that combines two types of preprocessing tools. For example, the LayoutLMV2 model requires an im... |
[
-0.01662591,
-0.007903415,
-0.017143223,
-0.015232035,
-0.051300343,
0.036700584,
-0.02392579,
0.0070448164,
-0.00030356296,
0.06943509,
0.013989043,
-0.016108595,
-0.029012715,
-0.04434534,
0.0020584802,
0.062422603,
-0.042678438,
-0.009419433,
-0.104152635,
-0.0013004709,
0... | AutoBackbone
A Swin backbone with multiple stages for outputting a feature map.
The [AutoBackbone] lets you use pretrained models as backbones to get feature maps from different stages of the backbone. You should specify one of the following parameters in [~PretrainedConfig.from_pretrained]:
out_indices is the index... |
[
-0.0075238175,
-0.013664283,
0.01076513,
0.007880693,
-0.028093824,
0.010978519,
-0.03549623,
-0.03481927,
-0.018130746,
0.07729113,
0.009963079,
-0.030639783,
0.043031085,
-0.043413717,
-0.0003030682,
0.03317102,
-0.023708304,
-0.016820975,
-0.062162556,
-0.021854023,
0.0032... | from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased")
Easily reuse the same checkpoint to load an architecture for a different task:
from transformers import AutoModelForTokenClassification
model = AutoModelForToken... |
[
0.024113879,
-0.013698122,
-0.00941521,
-0.0248193,
-0.023206908,
-0.002879269,
-0.02348044,
-0.005999677,
0.019291103,
0.05689436,
0.022918982,
-0.005261864,
0.01556245,
-0.047018465,
0.0022422308,
0.045694,
0.010545323,
-0.029944398,
-0.05136616,
-0.026014196,
0.014396345,
... |
from transformers import AutoImageProcessor, AutoBackbone
import torch
from PIL import Image
import requests
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
processor = AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224")
mo... |
[
0.013731624,
-0.040078234,
-0.013867431,
0.026784211,
-0.022000778,
-0.028942037,
-0.0046891235,
-0.005187083,
-0.0063565345,
0.048498284,
0.029213652,
-0.022649635,
0.048588824,
-0.074180946,
-0.019858042,
0.041617382,
0.004323198,
0.012848876,
-0.043156534,
-0.0020333366,
0... |
For PyTorch models, the from_pretrained() method uses torch.load() which internally uses pickle and is known to be insecure. In general, never load a model that could have come from an untrusted source, or that could have been tampered with. This security risk is partially mitigated for public models hosted on the Hu... |
[
0.016667223,
-0.01287543,
-0.010812861,
-0.017695036,
0.002812594,
0.015972756,
0.0011901439,
-0.0044897334,
0.0040174956,
0.02469527,
0.017070014,
-0.010319789,
0.012347635,
-0.022181297,
0.019681213,
0.06327989,
-0.015681079,
-0.04894608,
-0.07061347,
-0.01501439,
-0.016319... | Generally, we recommend using the AutoTokenizer class and the AutoModelFor class to load pretrained instances of models. This will ensure you load the correct architecture every time. In the next tutorial, learn how to use your newly loaded tokenizer, image processor, feature extractor and processor to preprocess a dat... |
[
-0.000036974514,
-0.024726713,
0.0020207984,
-0.010230292,
-0.016924188,
0.019688752,
0.0026715938,
-0.03438164,
-0.010293442,
0.054280892,
0.009297076,
-0.017962653,
0.010644275,
-0.021990215,
0.012075674,
0.073253945,
0.006550053,
-0.035616573,
-0.07516247,
-0.049060494,
-0... | from transformers import AutoProcessor
processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased")
AutoModel
The AutoModelFor classes let you load a pretrained model for a given task (see here for a complete list of available tasks). For example, load a model for sequence classification with [AutoM... |
[
0.0044274726,
-0.017353985,
-0.020229707,
-0.0060254913,
-0.011403234,
0.0072355727,
-0.01323971,
-0.026166221,
0.0013408764,
0.05321509,
0.032458644,
-0.021752985,
0.05147827,
-0.03456561,
0.021995,
0.04717892,
-0.012613315,
-0.01350308,
-0.05321509,
-0.034195464,
0.00850615... | Finally, the TFAutoModelFor classes let you load a pretrained model for a given task (see here for a complete list of available tasks). For example, load a model for sequence classification with [TFAutoModelForSequenceClassification.from_pretrained]:
from transformers import TFAutoModelForSequenceClassification
model ... |
[
-0.003724826,
-0.015494709,
-0.015622296,
0.014410221,
-0.04094119,
-0.01480007,
-0.027218519,
-0.022852216,
-0.022540336,
0.054210216,
0.014700836,
-0.019534959,
0.053643163,
-0.04170671,
0.00888146,
0.03878639,
-0.02475184,
-0.004235173,
-0.05077955,
-0.015282065,
0.0026137... | Easily reuse the same checkpoint to load an architecture for a different task:
from transformers import TFAutoModelForTokenClassification
model = TFAutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") |
[
0.04880609,
0.02055429,
-0.013456851,
-0.0027900033,
0.0060179923,
-0.02625155,
0.02204413,
0.012884366,
-0.00866315,
0.0362528,
0.027768983,
-0.022168282,
0.04179832,
-0.048226707,
0.013829311,
0.041246526,
0.020292187,
-0.01761599,
-0.07620261,
-0.0023520177,
0.019892137,
... | BetterTransformer is not supported for all models. Check this list to see if a model supports BetterTransformer.
Before you start, make sure you have 🤗 Optimum installed.
Enable BetterTransformer with the [PreTrainedModel.to_bettertransformer] method:
from transformers import AutoModelForCausalLM
model = AutoModelFo... |
[
0.012894462,
0.019967034,
-0.026827501,
0.0051014675,
-0.0019052792,
-0.041133553,
0.005792634,
-0.02280484,
-0.031011071,
0.030308934,
0.026330154,
0.02615462,
0.050700177,
-0.045902237,
-0.010590573,
0.02102024,
0.022395259,
-0.02444316,
-0.028670613,
0.005448879,
0.0245748... | fusion, which combines multiple sequential operations into a single "kernel" to reduce the number of computation steps
skipping the inherent sparsity of padding tokens to avoid unnecessary computation with nested tensors
BetterTransformer also converts all attention operations to use the more memory-efficient scaled d... |
[
0.030366836,
-0.02068099,
-0.046450395,
-0.004426327,
0.01272848,
-0.006520463,
-0.015228053,
-0.006014597,
-0.0022075842,
0.06082294,
0.02358228,
0.010771969,
0.01620259,
-0.06778604,
-0.02724237,
0.0159199,
-0.024400592,
-0.03192907,
-0.0554072,
0.0085922815,
0.023344226,
... |
TorchScript
TorchScript is an intermediate PyTorch model representation that can be run in production environments where performance is important. You can train a model in PyTorch and then export it to TorchScript to free the model from Python performance constraints. PyTorch traces a model to return a [ScriptFunctio... |
[
-0.0104308305,
-0.020977398,
-0.007870141,
-0.019241339,
-0.019299207,
0.0015362328,
-0.013845083,
-0.004553542,
-0.017621014,
0.03165417,
0.021107603,
0.00012240584,
0.057058524,
-0.01662278,
0.019863427,
0.062093098,
-0.011964351,
-0.026880004,
-0.04722085,
-0.01749081,
-0.... | from transformers import TFAutoModelForTokenClassification
model = TFAutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased")
Generally, we recommend using the AutoTokenizer class and the TFAutoModelFor class to load pretrained instances of models. This will ensure you load the correct arc... |
[
0.04313359,
-0.009326182,
-0.071595505,
-0.0075846314,
0.021310894,
0.017642975,
-0.012404107,
-0.008899679,
0.018026827,
0.038982302,
0.0444984,
-0.017515024,
0.0382146,
-0.060392715,
0.0008898791,
0.036707625,
-0.0024523877,
-0.031248396,
-0.036224257,
0.011572427,
0.016491... |
CPU inference
With some optimizations, it is possible to efficiently run large model inference on a CPU. One of these optimization techniques involves compiling the PyTorch code into an intermediate format for high-performance environments like C++. The other technique fuses multiple operations into one kernel to redu... |
[
0.047728952,
-0.0018902376,
-0.031171702,
0.022632105,
0.0016211098,
-0.011780004,
-0.007473959,
-0.02380648,
0.0038674658,
0.04610512,
-0.03250556,
0.01928296,
0.03221559,
-0.056022074,
0.028895443,
0.029750852,
-0.022646604,
-0.0557611,
-0.028619971,
0.030968724,
0.01078685... | pip install intel_extension_for_pytorch
Set the --use_ipex and --jit_mode_eval flags in the [Trainer] class to enable JIT mode with the graph optimizations:
python run_qa.py \
--model_name_or_path csarron/bert-base-uncased-squad-v1 \
--dataset_name squad \
--do_eval \
--max_seq_length 384 \
--doc_stride 128 \
--output... |
[
0.046318747,
-0.015880307,
0.0059604463,
-0.004961707,
-0.035257977,
-0.06221327,
-0.0145296985,
0.0005300248,
0.03122037,
0.048508152,
0.045351997,
-0.037390515,
0.0065966537,
-0.018737908,
-0.017657423,
0.013342585,
0.00029477908,
-0.013989456,
-0.058090363,
0.019107549,
-0... | Learn more details about using ORT with 🤗 Optimum in the Optimum Inference with ONNX Runtime guide. This section only provides a brief and simple example. |
[
0.050727025,
-0.022072969,
-0.033668846,
0.016623829,
0.0026587595,
-0.012444837,
0.005340553,
-0.0062158373,
-0.021019993,
0.034116358,
-0.012490905,
-0.00574858,
0.033721495,
-0.04483037,
-0.011352376,
0.010773241,
0.0041000177,
-0.051701024,
-0.0425928,
-0.0020121676,
0.01... | python run_qa.py \
--model_name_or_path csarron/bert-base-uncased-squad-v1 \
--dataset_name squad \
--do_eval \
--max_seq_length 384 \
--doc_stride 128 \
--output_dir /tmp/ \
--no_cuda \
--jit_mode_eval |
[
0.031677656,
-0.024505906,
-0.048721455,
0.018190699,
-0.013690206,
0.014314468,
-0.004144808,
-0.0020415538,
0.0008093627,
0.094655514,
-0.00006306133,
0.0010207769,
0.02809178,
-0.047792323,
-0.024317175,
-0.014024114,
-0.020179626,
-0.01715994,
-0.04189813,
0.028062746,
0.... |
For PyTorch >= 1.14.0, JIT-mode could benefit any model for prediction and evaluation since the dict input is supported in jit.trace.
For PyTorch < 1.14.0, JIT-mode could benefit a model if its forward parameter order matches the tuple input order in jit.trace, such as a question-answering model. If the forward param... |
[
0.026325613,
0.0023770183,
-0.028891856,
-0.0050928383,
0.032178953,
-0.013962955,
-0.012968176,
-0.020991288,
0.032582633,
0.03725377,
-0.0072554024,
0.025835432,
0.025590342,
-0.06914438,
-0.0058893827,
0.009724329,
-0.023802621,
-0.027522232,
-0.0011416536,
0.010474019,
0.... | IPEX graph optimization
Intel® Extension for PyTorch (IPEX) provides further optimizations in JIT mode for Intel CPUs, and we recommend combining it with TorchScript for even faster performance. The IPEX graph optimization fuses operations like Multi-head attention, Concat Linear, Linear + Add, Linear + Gelu, Add + Lay... |
[
0.046095893,
-0.008614455,
-0.028025504,
0.02598599,
-0.007765846,
-0.03345946,
-0.025287135,
-0.00079156004,
-0.022605814,
0.008015437,
0.021778598,
-0.015845463,
0.020708922,
-0.03146273,
-0.014504802,
0.013627669,
-0.032546666,
-0.036454547,
-0.06286841,
-0.020081379,
-0.0... | from transformers import AutoTokenizer, pipeline
from optimum.onnxruntime import ORTModelForQuestionAnswering
model = ORTModelForQuestionAnswering.from_pretrained("optimum/roberta-base-squad2")
tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
onnx_qa = pipeline("question-answering", model=model,... |
[
0.027348962,
-0.0013387303,
-0.031806238,
-0.034915324,
0.027005037,
-0.00799283,
-0.015793063,
0.0028408254,
0.0037419102,
0.026509784,
0.023868436,
0.0075663626,
0.034832783,
-0.02236892,
0.02379965,
-0.018406898,
0.02379965,
-0.052386746,
-0.03543809,
0.009299748,
-0.02492... | BERTology
There is a growing field of study concerned with investigating the inner working of large-scale transformers like BERT
(that some call "BERTology"). Some good examples of this field are: |
[
0.047139086,
-0.023314374,
-0.040800154,
-0.025946641,
-0.007977384,
-0.02222655,
-0.0077625047,
0.0008070554,
-0.018036408,
0.043513004,
0.011361728,
-0.00983743,
0.0108581055,
-0.023247223,
-0.0063892934,
0.0030049488,
-0.015229551,
-0.020762686,
-0.07510022,
0.0077289296,
... |
BERT Rediscovers the Classical NLP Pipeline by Ian Tenney, Dipanjan Das, Ellie Pavlick:
https://arxiv.org/abs/1905.05950
Are Sixteen Heads Really Better than One? by Paul Michel, Omer Levy, Graham Neubig: https://arxiv.org/abs/1905.10650
What Does BERT Look At? An Analysis of BERT's Attention by Kevin Clark, Urvash... |
[
0.017941944,
-0.013755017,
-0.02937247,
-0.010996905,
0.014885275,
-0.008139274,
-0.004698742,
-0.026301583,
0.0003176627,
0.030822612,
0.0049013356,
-0.021666817,
0.029201865,
-0.05462199,
-0.010797867,
0.02802185,
-0.00548779,
-0.029941153,
-0.058005653,
0.0113807665,
0.003... |
ONNX Runtime (ORT) is a model accelerator that runs inference on CPUs by default. ORT is supported by 🤗 Optimum which can be used in 🤗 Transformers, without making too many changes to your code. You only need to replace the 🤗 Transformers AutoClass with its equivalent [~optimum.onnxruntime.ORTModel] for the task y... |
[
0.051401515,
-0.004018478,
-0.019880893,
-0.011194853,
-0.004277382,
-0.021820847,
0.0006226453,
0.0045435787,
0.0073514082,
0.01045096,
0.02768447,
-0.0327021,
0.031010108,
-0.040928673,
-0.008292214,
0.053181022,
-0.0016236183,
-0.048921872,
-0.015257089,
-0.00084417214,
0.... | accessing all the hidden-states of BERT/GPT/GPT-2,
accessing all the attention weights for each head of BERT/GPT/GPT-2,
retrieving heads output values and gradients to be able to compute head importance score and prune head as explained
in https://arxiv.org/abs/1905.10650.
To help you understand and use these featur... |
[
0.02113345,
-0.0113889,
0.002400391,
-0.0017489074,
0.02027553,
-0.0034263213,
0.011524737,
-0.0034281088,
0.0041466174,
0.023721512,
0.027939623,
-0.026252378,
0.030113023,
-0.04272446,
0.025380159,
0.052933715,
0.0034996022,
-0.07624057,
-0.035489324,
-0.0149850175,
-0.0045... | If you have an Intel CPU, take a look at 🤗 Optimum Intel which supports a variety of compression techniques (quantization, pruning, knowledge distillation) and tools for converting models to the OpenVINO format for higher performance inference. |
[
0.029045789,
0.027807726,
-0.036112484,
-0.042121958,
-0.033274677,
-0.037642673,
0.016539963,
-0.0025613294,
0.046295203,
0.05631099,
0.013625646,
-0.0006725011,
0.06504698,
-0.027348667,
-0.015900064,
0.037503567,
-0.021200087,
0.031076767,
-0.05731257,
-0.010586132,
-0.024... | Testing
Let's take a look at how 🤗 Transformers models are tested and how you can write new tests and improve the existing ones.
There are 2 test suites in the repository:
tests -- tests for the general API
examples -- tests primarily for various applications that aren't part of the API
How transformers are tested |
[
0.035560723,
-0.013518459,
-0.014056805,
-0.006422763,
-0.0056414143,
-0.0033254062,
0.02471904,
0.006699413,
-0.005974142,
0.023328314,
-0.018707514,
0.025048029,
0.030147359,
-0.036517784,
-0.01766073,
0.029025806,
-0.0032039047,
-0.037235577,
-0.060145177,
-0.001979542,
-0... |
🤗 Transformers Notebooks
You can find here a list of the official notebooks provided by Hugging Face.
Also, we would like to list here interesting content created by the community.
If you wrote some notebook(s) leveraging 🤗 Transformers and would like to be listed here, please open a
Pull Request so it can be includ... |
[
0.041306492,
0.04371898,
-0.015592907,
-0.051015284,
-0.02694925,
-0.00837751,
-0.004659484,
-0.01784358,
0.037717182,
0.07496364,
0.013342232,
0.034392655,
0.017858291,
-0.050603393,
-0.03962952,
0.021609414,
-0.006012831,
0.033627722,
-0.037452396,
-0.028802747,
-0.01800539... | tests -- tests for the general API
examples -- tests primarily for various applications that aren't part of the API
How transformers are tested
Once a PR is submitted it gets tested with 9 CircleCi jobs. Every new commit to that PR gets retested. These jobs
are defined in this config file, so that if needed you ca... |
[
0.004585179,
0.007963158,
-0.025613008,
0.00039679435,
-0.014290051,
0.025133036,
-0.0117520215,
-0.009373982,
0.024333084,
0.051691443,
-0.015402711,
0.053756773,
0.033917964,
-0.06667236,
-0.02533666,
-0.014435497,
0.007904979,
0.008683115,
-0.04968429,
0.008435857,
0.01666... | These CI jobs don't run @slow tests.
There are 3 jobs run by github actions:
torch hub integration: checks whether torch hub
integration works.
self-hosted (push): runs fast tests on GPU only on commits on
main. It only runs if a commit on main has updated the code in one of the following folders: src,
... |
[
0.025921443,
-0.008476747,
-0.021949722,
-0.036826134,
0.023774188,
-0.033289477,
0.015227268,
-0.021248005,
0.015325509,
0.041906565,
0.017851692,
-0.009241619,
0.039352316,
-0.037864674,
-0.007031209,
0.013571216,
-0.011346771,
-0.023858393,
-0.034103468,
-0.016728943,
0.00... | In order to help this new field develop, we have included a few additional features in the BERT/GPT/GPT-2 models to
help people access the inner representations, mainly adapted from the great work of Paul Michel
(https://arxiv.org/abs/1905.10650): |
[
0.021837668,
0.015271288,
-0.005264071,
0.023523815,
-0.044689767,
0.025443008,
0.002236202,
-0.00016214605,
0.029884567,
0.06349785,
-0.03514864,
-0.030515159,
0.032955274,
-0.027444452,
-0.0748485,
-0.015147911,
-0.029171724,
0.008334778,
-0.055683997,
-0.00003196869,
0.004... | make test
Note that the latter is defined as:
python -m pytest -n auto --dist=loadfile -s -v ./tests/
which tells pytest to:
run as many test processes as they are CPU cores (which could be too many if you don't have a ton of RAM!)
ensure that all tests from the same file will be run by the same test process
do not c... |
[
0.053723436,
0.030551895,
-0.029994888,
-0.005577044,
-0.037765153,
0.006638841,
-0.00754746,
0.032000117,
0.03506366,
0.044616353,
-0.02531602,
0.002499574,
0.04940662,
-0.021054907,
-0.07252246,
-0.03305843,
-0.008564,
-0.010798995,
-0.049601573,
0.006412556,
-0.026193308,
... | Getting the list of all tests
All tests of the test suite:
pytest --collect-only -q
All tests of a given test file:
pytest tests/test_optimization.py --collect-only -q
Run a specific test module
To run an individual test module:
pytest tests/utils/test_logging.py
Run specific tests
Since unittest is used inside most... |
[
0.055371534,
0.019148616,
0.0077257683,
0.0011941427,
-0.011662735,
0.037478797,
-0.016552193,
0.04267164,
-0.003081488,
0.049727138,
-0.0068226648,
0.004173326,
0.036491025,
-0.054073323,
-0.054665983,
-0.017709294,
0.0030444465,
-0.008445429,
-0.056980185,
-0.0149576515,
-0... | If the file contains multiple classes, you can choose to run only tests of a given class. For example:
pytest tests/test_optimization.py::OptimizationTest
will run all the tests inside that class.
As mentioned earlier you can see what tests are contained inside the OptimizationTest class by running:
pytest tests/test... |
[
0.04230723,
-0.008474189,
-0.012190939,
-0.020389026,
-0.048197392,
0.0081343725,
-0.016806789,
0.018604986,
0.00022112449,
0.043723136,
-0.017514741,
0.017401468,
0.03072513,
-0.028134026,
-0.04287359,
-0.004141521,
-0.029733997,
-0.04587531,
-0.054172512,
0.015574951,
0.011... | self-hosted runner: runs normal and slow tests on GPU in
tests and examples:
RUN_SLOW=1 pytest tests/
RUN_SLOW=1 pytest examples/
The results can be observed here.
Running tests
Choosing which tests to run
This document goes into many details of how tests can be run. If after reading everything, you need even mor... |
[
0.06594626,
0.012686818,
-0.01086042,
0.0024456938,
-0.0043210755,
-0.009390905,
-0.0069242176,
0.030174052,
-0.01992943,
0.038459323,
0.0014695156,
-0.010930397,
0.038319368,
-0.06365102,
-0.058052864,
-0.014065363,
0.00090357714,
0.0046114796,
-0.059508383,
-0.011756125,
0.... | pytest tests/test_optimization.py::OptimizationTest --collect-only -q
You can run tests by keyword expressions.
To run only tests whose name contains adam:
pytest -k adam tests/test_optimization.py
Logical and and or can be used to indicate whether all keywords should match or either. not can be used to
negate.
To run... |
[
0.030461906,
-0.012311067,
-0.02532053,
-0.0033340927,
-0.0052565364,
0.03952618,
-0.020238593,
0.00008184315,
0.012474522,
0.04535109,
0.0029756082,
0.012155044,
0.02115988,
-0.03486031,
-0.029659495,
0.053612955,
-0.0015082358,
-0.024904465,
-0.061102126,
-0.020818112,
-0.0... |
RUN_SLOW=1 pytest -m accelerate_tests tests/models/opt/test_modeling_opt.py
Run documentation tests
In order to test whether the documentation examples are correct, you should check that the doctests are passing.
As an example, let's use WhisperModel.forward's docstring:
thon
r"""
Returns:
Example:
thon
>>... |
[
0.022106698,
0.0017654162,
0.018802747,
0.0021784103,
-0.031678233,
0.00042739557,
0.00071653567,
0.0035042448,
0.0038640636,
0.072942175,
-0.037406974,
-0.012946386,
0.038059253,
-0.015612235,
-0.041405745,
-0.008855442,
-0.009961485,
-0.01671828,
-0.06613576,
0.011918332,
-... |
Just run the following line to automatically test every docstring example in the desired file:
pytest --doctest-modules <path_to_file_or_dir>
If the file has a markdown extention, you should add the --doctest-glob="*.md" argument.
Run only modified tests
You can run the tests related to the unstaged files or the c... |
[
0.05557124,
0.022641655,
0.0044086245,
0.019631889,
-0.042984944,
0.025364127,
-0.013181411,
0.030590177,
-0.008057967,
0.030562814,
0.004921653,
-0.016389549,
0.038306125,
-0.037020132,
-0.033216882,
-0.0023736116,
-0.0056775147,
0.019741334,
-0.034475513,
-0.0005848524,
0.0... | pytest tests/test_optimization.py::OptimizationTest::test_adam_w
Here:
tests/test_optimization.py - the file with tests
OptimizationTest - the name of the class
test_adam_w - the name of the specific test function
If the file contains multiple classes, you can choose to run only tests of a given class. For example: |
[
0.06130396,
0.009546787,
-0.007891741,
0.014182259,
-0.060604267,
-0.022834245,
-0.004938225,
-0.0064788973,
-0.023211002,
0.03678776,
-0.004198164,
-0.03869846,
0.05554494,
-0.061250135,
-0.039236687,
0.018474612,
-0.007387154,
0.0055370014,
-0.053768795,
0.01622752,
0.04281... | pytest -k "not adam" tests/test_optimization.py
And you can combine the two patterns in one:
pytest -k "ada and not adam" tests/test_optimization.py
For example to run both test_adafactor and test_adam_w you can use:
pytest -k "test_adam_w or test_adam_w" tests/test_optimization.py
Note that we use or here, since we ... |
[
0.036240052,
0.017690249,
-0.03072225,
0.015777038,
-0.048079766,
0.034576394,
-0.002696515,
0.032302726,
-0.0009886645,
0.026826512,
-0.032912735,
-0.02844858,
0.022889184,
-0.050880257,
-0.022972368,
0.01975596,
-0.011936759,
0.0028542161,
-0.06443909,
0.032164086,
0.018979... | pytest -k "test and ada" tests/test_optimization.py
Run accelerate tests
Sometimes you need to run accelerate tests on your models. For that you can just add -m accelerate_tests to your command, if let's say you want to run these tests on OPT run: |
[
0.0200785,
0.0049601286,
-0.0028464375,
-0.0132019585,
-0.080163695,
0.0021543994,
0.00103884,
0.031213738,
-0.01583233,
0.014116325,
-0.02733081,
-0.058619704,
0.048724495,
-0.00091749855,
-0.02951026,
0.011041297,
-0.007922428,
-0.017147515,
-0.030236743,
0.010496434,
0.025... | pip install pytest-picked |
[
0.017319202,
-0.028209517,
-0.0027502892,
-0.01607222,
-0.028458912,
0.01878787,
-0.031728778,
-0.017125227,
0.0036335685,
0.04868774,
-0.030842034,
-0.025798682,
0.049214244,
-0.021434244,
-0.045168478,
-0.029456498,
-0.0025476546,
0.0012002207,
-0.06451056,
0.016113786,
-0.... | pytest --picked
All tests will be run from files and folders which are modified, but not yet committed.
Automatically rerun failed tests on source modification
pytest-xdist provides a very useful feature of detecting all failed
tests, and then waiting for you to modify files and continuously re-rerun those failing test... |
[
0.032733712,
-0.0250309,
-0.03070734,
0.014301515,
-0.032733712,
0.0058323164,
-0.005306239,
0.013587089,
0.026576659,
0.0584011,
0.0015124729,
-0.036214918,
0.034448337,
-0.026381815,
-0.03366896,
-0.024108639,
0.0087419795,
-0.034188546,
-0.045229677,
0.015340681,
-0.009573... | pytest-flakefinder:
pip install pytest-flakefinder
And then run every test multiple times (50 by default):
pytest --flake-finder --flake-runs=5 tests/test_failing_test.py
This plugin doesn't work with -n flag from pytest-xdist.
There is another plugin pytest-repeat, but it doesn't work with unittest.
Run tests in ... |
[
0.01790669,
0.009118384,
-0.032292556,
-0.02965194,
-0.04032444,
-0.0031322956,
-0.0311923,
0.011862151,
0.003474407,
0.047833696,
-0.007935607,
-0.028001554,
0.032292556,
-0.049016472,
-0.06403498,
-0.04032444,
-0.0064949575,
-0.02670875,
-0.0023775876,
0.018236768,
-0.01801... | There is another plugin pytest-repeat, but it doesn't work with unittest.
Run tests in a random order
pip install pytest-random-order
Important: the presence of pytest-random-order will automatically randomize tests, no configuration change or
command line options is required.
As explained earlier this allows detecti... |
[
0.01077405,
0.013265326,
-0.0047821035,
-0.02296555,
-0.023695752,
0.018484117,
-0.023881882,
0.005000448,
-0.0049682334,
0.06838121,
-0.006299777,
-0.038514547,
0.07113021,
-0.024612082,
-0.053175844,
0.0055767344,
-0.0030389265,
0.003812081,
-0.060420588,
0.026129756,
-0.00... |
pip install pytest-xdist
To enter the mode: pytest -f or pytest --looponfail
File changes are detected by looking at looponfailroots root directories and all of their contents (recursively).
If the default for this value does not work for you, you can change it in your project by setting a configuration
option in set... |
[
0.0065515577,
0.02584833,
0.019565199,
-0.0029543317,
-0.037937395,
-0.017643142,
-0.023356287,
0.0114991935,
0.03616115,
0.07831381,
-0.0019353109,
-0.031707287,
0.02215003,
-0.032184485,
-0.052253395,
-0.037619263,
-0.00031896177,
-0.0011971423,
-0.008768549,
-0.0023296636,
... | pytest tests
[]
Using --random-order-bucket=module
Using --random-order-seed=573663
So that if the given particular sequence fails, you can reproduce it by adding that exact seed, e.g.: |
[
-0.007628742,
0.014878504,
0.018163003,
0.005491712,
-0.057043623,
-0.010857097,
-0.012155457,
0.009797354,
0.032367766,
0.025363639,
0.005277658,
0.0141065065,
0.020984305,
-0.05544348,
-0.06984475,
-0.0019142037,
-0.044130202,
-0.0074181976,
-0.044439003,
0.014288979,
0.016... | pytest *ls -1 tests/*py | grep -v test_modeling*
Clearing state
CI builds and when isolation is important (against speed), cache should be cleared: |
[
-0.008770278,
-0.016156496,
0.0142517015,
-0.0037993118,
-0.030202646,
0.002269652,
-0.035190742,
-0.017595371,
0.030695973,
0.059309006,
-0.019952383,
-0.009770638,
0.05528016,
-0.028174518,
-0.06517413,
-0.031956702,
-0.030230053,
-0.020856818,
-0.051881675,
0.024611592,
0.... |
pytest --cache-clear tests
Running tests in parallel
As mentioned earlier make test runs tests in parallel via pytest-xdist plugin (-n X argument, e.g. -n 2
to run 2 parallel jobs).
pytest-xdist's --dist= option allows one to control how the tests are grouped. --dist=loadfile puts the
tests located in one file onto t... |
[
0.01733395,
-0.019093217,
-0.016894134,
-0.0093849115,
-0.05099286,
-0.032365333,
-0.0044887173,
0.025211843,
0.022262484,
0.010400371,
-0.022779915,
-0.033451937,
0.026350193,
-0.056710478,
-0.04305029,
-0.018355878,
0.017799638,
-0.014630372,
-0.046568822,
0.016441382,
0.00... | pip install pytest-sugar
To run tests without it, run:
pytest -p no:sugar
or uninstall it.
Report each sub-test name and its progress
For a single or a group of tests via pytest (after pip install pytest-pspec):
pytest --pspec tests/test_optimization.py
Instantly shows failed tests
pytest-instafail shows failures and... |
[
0.017661072,
0.0068764025,
-0.002252962,
0.012087426,
-0.049961362,
-0.0049659177,
0.0087835295,
0.010824962,
0.023006402,
0.042386577,
-0.034435734,
-0.051384993,
0.058288258,
-0.013544633,
-0.025853662,
0.005006209,
-0.018735511,
-0.024510615,
-0.040022813,
-0.011765095,
0.... | pip install pytest-instafail
pytest --instafail
To GPU or not to GPU
On a GPU-enabled setup, to test in CPU-only mode add CUDA_VISIBLE_DEVICES="":
CUDA_VISIBLE_DEVICES="" pytest tests/utils/test_logging.py
or if you have multiple gpus, you can specify which one is to be used by pytest. For example, to use only the
se... |
[
0.02311835,
0.012202857,
0.009377432,
-0.020502968,
-0.056644022,
-0.015272205,
0.00970266,
0.039433997,
0.008483052,
0.06889431,
0.005830405,
-0.03775365,
0.024798699,
-0.008313662,
-0.057673916,
-0.048919838,
0.01375447,
-0.00085838395,
-0.0032793907,
0.0074938145,
0.006701... | pytest --random-order-seed=573663
[]
Using --random-order-bucket=module
Using --random-order-seed=573663
It will only reproduce the exact order if you use the exact same list of tests (or no list at all). Once you start to
manually narrowing down the list you can no longer rely on the seed, but have to list them manual... |
[
0.02761157,
0.020843776,
-0.008446876,
0.0075076194,
-0.06093588,
-0.026865313,
-0.0069736587,
0.022027498,
0.0038181425,
0.064898774,
-0.013921584,
-0.02861516,
0.03221779,
-0.05764205,
-0.059958026,
-0.02735424,
-0.032114856,
-0.014487712,
-0.0375188,
-0.01022889,
-0.006513... | pytest --random-order-bucket=none tests/test_a.py tests/test_c.py tests/test_b.py
To disable the shuffling for all tests: |
[
0.04185263,
0.0030779124,
-0.029380549,
-0.027329769,
-0.060658414,
-0.02353513,
-0.011523425,
0.009095972,
-0.029966485,
0.052511103,
-0.004216653,
-0.048074722,
0.026409011,
-0.03512831,
-0.07416286,
-0.008035705,
-0.018819734,
-0.02784595,
-0.019670738,
-0.0016924158,
0.01... |
pytest --random-order-bucket=none
By default --random-order-bucket=module is implied, which will shuffle the files on the module levels. It can also
shuffle on class, package, global and none levels. For the complete details please see its
documentation.
Another randomization alternative is: pytest-randomly. This
mod... |
[
0.044941906,
-0.016385071,
-0.014650181,
0.010113306,
-0.05039442,
0.0018312726,
0.027689392,
0.01787212,
0.0074834335,
0.057774585,
0.0015068413,
-0.028584374,
0.019428013,
-0.050587185,
-0.01650899,
0.047778316,
-0.024963137,
-0.035634086,
-0.066311345,
-0.009720891,
0.0123... |
Let's depict the GPU requirements in the following table:
| n gpus | decorator |
|--------+--------------------------------|
| >= 0 | @require_torch |
| >= 1 | @require_torch_gpu |
| >= 2 | @require_torch_multi_gpu |
| < 2 | @require_torch_non_multi_gpu |
| < 3 | @re... |
[
0.04243333,
0.009526269,
-0.031475723,
0.018271808,
-0.03254409,
-0.0013371704,
0.023805398,
0.009190692,
0.032982394,
0.04287163,
0.021367332,
-0.005858895,
0.036269676,
-0.013073794,
-0.0051500746,
0.051144626,
-0.014368161,
-0.0027702197,
-0.031064812,
-0.021983696,
-0.030... | How many GPUs are available:
thon
from transformers.testing_utils import get_gpu_count
n_gpu = get_gpu_count() # works with torch and tf
Testing with a specific PyTorch backend or device
To run the test suite on a specific torch device add TRANSFORMERS_TEST_DEVICE="$device" where $device is the target backend. For e... |
[
0.010562678,
-0.006973587,
-0.0089952685,
0.013336855,
-0.07002022,
0.01404427,
0.018878274,
-0.0065921377,
0.008114467,
0.033345606,
-0.02715919,
-0.016256675,
0.054540314,
-0.030737879,
-0.04252813,
0.015313456,
-0.034760434,
-0.042278454,
-0.07112989,
-0.014647653,
0.00036... | CUDA_VISIBLE_DEVICES="1" pytest tests/utils/test_logging.py
This is handy when you want to run different tasks on different GPUs.
Some tests must be run on CPU-only, others on either CPU or GPU or TPU, yet others on multiple-GPUs. The following skip
decorators are used to set the requirements of tests CPU/GPU/TPU-wise: |
[
0.013433297,
-0.011271534,
-0.0051296684,
-0.012891049,
-0.014966052,
0.041384388,
-0.008256634,
-0.0070347674,
0.030423742,
0.01237772,
0.023714324,
-0.007721615,
0.041528985,
-0.019867975,
-0.047602165,
0.017380863,
-0.04375582,
-0.004030712,
-0.036207724,
0.009153151,
0.00... | TRANSFORMERS_TEST_BACKEND="torch_npu" pytest tests/utils/test_logging.py
Alternative backends may also require the replacement of device-specific functions. For example torch.cuda.manual_seed may need to be replaced with a device-specific seed setter like torch.npu.manual_seed to correctly set a random seed on the devi... |
[
0.042452224,
-0.007296078,
-0.024141997,
0.0030989232,
-0.021582639,
-0.015903667,
0.021187913,
0.023352543,
0.032673188,
0.069268174,
0.017393442,
-0.024129264,
0.028369393,
-0.05358097,
-0.027809136,
0.034048364,
-0.012484824,
-0.0055070748,
-0.033055183,
-0.021824569,
-0.0... | require_torch - this test will run only under torch
require_torch_gpu - as require_torch plus requires at least 1 GPU
require_torch_multi_gpu - as require_torch plus requires at least 2 GPUs
require_torch_non_multi_gpu - as require_torch plus requires 0 or 1 GPUs
require_torch_up_to_2_gpus - as require_torch plus requi... |
[
0.015537213,
-0.00278085,
-0.00038301488,
0.024735123,
-0.046211727,
0.032229718,
-0.006639233,
-0.010123627,
0.00027702042,
0.009671878,
-0.0002807233,
-0.02855648,
0.054535765,
-0.03839128,
-0.0381543,
0.032940667,
-0.014759611,
-0.029267428,
-0.06611832,
-0.020528672,
-0.0... | TRANSFORMERS_TEST_DEVICE="cpu" pytest tests/utils/test_logging.py
This variable is useful for testing custom or less common PyTorch backends such as mps. It can also be used to achieve the same effect as CUDA_VISIBLE_DEVICES by targeting specific GPUs or testing in CPU-only mode.
Certain devices will require an additio... |
[
-0.0011412805,
-0.019782197,
0.010234492,
0.016186656,
-0.038648672,
-0.011810065,
0.013183642,
0.01253052,
0.027148336,
0.051307123,
-0.03525513,
-0.013143242,
0.03816388,
0.005581838,
-0.055158526,
-0.033962354,
0.016886912,
0.0015503236,
-0.023014141,
0.014691883,
-0.02499... | pytest -s tests/utils/test_logging.py
To send test results to JUnit format output:
py.test tests --junitxml=result.xml
Color control
To have no color (e.g., yellow on white background is not readable):
pytest --color=no tests/utils/test_logging.py
Sending test report to online pastebin service
Creating a URL for each... |
[
0.036378995,
0.013513458,
-0.013410526,
0.002604539,
-0.031732365,
0.047172114,
-0.0065030763,
0.0033783645,
0.010896053,
0.044995844,
0.008888885,
-0.00391508,
0.048377886,
-0.006436906,
-0.050907064,
0.010418156,
-0.012917925,
-0.017013134,
-0.059523918,
-0.0042128465,
-0.0... |
import torch
import torch_npu
!! Further additional imports can be added here !!
Specify the device name (eg. 'cuda', 'cpu', 'npu')
DEVICE_NAME = 'npu'
Specify device-specific backends to dispatch to.
If not specified, will fallback to 'default' in 'testing_utils.py`
MANUAL_SEED_FN = torch.npu.manual_seed
EMPTY_CACHE... |
[
0.011266406,
0.016221981,
-0.057440873,
0.011375921,
-0.042519387,
-0.016098777,
0.005865923,
0.032170173,
0.034387864,
0.07233498,
-0.01797423,
0.03646866,
0.05221151,
-0.012861225,
0.042355116,
0.040876657,
-0.0035216075,
-0.03378553,
-0.03085599,
-0.0030955237,
0.015893435... | test_trainer_distributed.py
test_deepspeed.py
To jump right into the execution point, search for the execute_subprocess_async call in those tests.
You will need at least 2 GPUs to see these tests in action: |
[
-0.00958694,
-0.0071692416,
0.011781035,
0.01185091,
-0.05053407,
-0.035860185,
-0.04829805,
0.005506201,
-0.010998427,
0.04189744,
-0.039437816,
0.0057752226,
0.042372596,
-0.003326081,
-0.03848751,
-0.024959583,
0.019131953,
-0.003933999,
-0.057130333,
0.012493766,
-0.00412... | CUDA_VISIBLE_DEVICES=0,1 RUN_SLOW=1 pytest -sv tests/test_trainer_distributed.py
Output capture
During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails, its
according captured output will usually be shown along with the failure traceback.
To disable output capturing and... |
[
0.084825635,
0.009794151,
-0.014207768,
-0.020298691,
-0.051016413,
0.0069723306,
0.0020144116,
0.04870107,
-0.01679937,
0.054568354,
0.018470097,
-0.032677867,
-0.000606379,
-0.050779615,
-0.059777867,
0.0005924014,
-0.009300826,
-0.0002575569,
-0.012484418,
0.0025110256,
0.... | Now, by default this test will be run 3 times, each time with the last 3 arguments of test_floor being assigned the
corresponding arguments in the parameter list.
and you could run just the negative and integer sets of params with:
pytest -k "negative and integer" tests/test_mytest.py
or all but negative sub-tests, wi... |
[
0.062006067,
-0.013584441,
-0.03364159,
-0.023870686,
-0.04644958,
-0.025025055,
0.011158893,
0.033614106,
-0.006469272,
0.071515866,
-0.02377449,
-0.029106572,
0.018167557,
-0.018854681,
-0.07596843,
-0.0017513073,
-0.024008112,
-0.001996095,
-0.03031591,
0.004583117,
-0.012... | pytest -k "negative and integer" tests/test_mytest.py
or all but negative sub-tests, with:
pytest -k "not negative" tests/test_mytest.py
Besides using the -k filter that was just mentioned, you can find out the exact name of each sub-test and run any
or all of them using their exact names.
pytest test_this1.py --coll... |
[
0.061800145,
0.03239743,
-0.029239362,
-0.007180523,
-0.032016285,
0.0037399977,
0.016362064,
0.035637174,
0.039040267,
0.052026466,
-0.023726355,
-0.0015781837,
0.02433891,
-0.020731634,
-0.057335287,
0.005604892,
-0.025645697,
-0.0073983213,
-0.044675786,
-0.004053082,
0.00... | pytest test_this1.py --collect-only -q
and it will list:
test_this1.py::TestMathUnitTest::test_floor_0_negative
test_this1.py::TestMathUnitTest::test_floor_1_integer
test_this1.py::TestMathUnitTest::test_floor_2_large_fraction
So now you can run just 2 specific sub-tests: |
[
0.07294163,
0.0028430787,
-0.014874761,
-0.00754373,
-0.049346205,
0.02290061,
0.008827015,
0.019681761,
-0.008323627,
0.034202024,
0.0017334981,
-0.017554771,
0.020560918,
-0.0076926197,
-0.020716898,
0.0072282264,
0.0030469154,
-0.011202156,
-0.029324124,
0.0056967926,
-0.0... |
pytest test_this1.py::TestMathUnitTest::test_floor_0_negative test_this1.py::TestMathUnitTest::test_floor_1_integer
The module parameterized which is already in the developer dependencies
of transformers works for both: unittests and pytest tests.
If, however, the test is not a unittest, you may use pytest.mark.para... |
[
0.011365089,
-0.026619833,
-0.00554411,
0.008184484,
-0.029253455,
-0.026417246,
0.013174859,
0.009548565,
0.034682766,
0.031792536,
-0.013897416,
0.0047945413,
0.031306326,
-0.013526008,
-0.03930173,
-0.034655754,
0.025566384,
0.006351079,
-0.035952307,
-0.010399427,
-0.0010... | pytest --color=no tests/utils/test_logging.py
Sending test report to online pastebin service
Creating a URL for each test failure:
pytest --pastebin=failed tests/utils/test_logging.py
This will submit test run information to a remote Paste service and provide a URL for each failure. You may select
tests as usual or ad... |
[
0.060428467,
-0.013229356,
-0.003270138,
0.007046917,
-0.06020172,
0.0050947545,
-0.004871549,
0.024162883,
0.006366672,
0.03114957,
-0.013767883,
-0.0122019015,
0.033246994,
-0.0021222238,
-0.059011288,
0.0076811044,
-0.022022944,
-0.0138174845,
-0.037980366,
0.014469387,
0.... | Same as with parameterized, with pytest.mark.parametrize you can have a fine control over which sub-tests are
run, if the -k filter doesn't do the job. Except, this parametrization function creates a slightly different set of
names for the sub-tests. Here is what they look like:
pytest test_this2.py --collect-only -q
... |
[
0.05759632,
-0.0023490945,
-0.0133405365,
-0.01713969,
-0.048315942,
0.028638652,
-0.017183192,
0.018285235,
-0.00030519196,
0.03459839,
0.007362671,
-0.02389696,
0.019343778,
-0.04651787,
-0.033844363,
0.017255694,
0.008736601,
-0.0037629013,
-0.04051463,
-0.00571323,
-0.000... |
pytest --pastebin=all tests/utils/test_logging.py
Writing tests
🤗 transformers tests are based on unittest, but run by pytest, so most of the time features from both systems
can be used.
You can read here which features are supported, but the important
thing to remember is that most pytest fixtures don't work. Neith... |
[
0.070741504,
0.036591437,
0.017874291,
-0.03676582,
-0.032260917,
0.0064412844,
0.022742493,
0.019981423,
-0.012163238,
0.071729675,
-0.016043266,
-0.01973438,
0.026346415,
-0.0016784396,
-0.040340677,
-0.0025412738,
0.010862629,
0.034644157,
-0.008973476,
-0.010571989,
-0.01... |
pytest test_this2.py::test_floor[negative--1.5--2.0] test_this2.py::test_floor[integer-1-1.0]
as in the previous example.
Files and directories
In tests often we need to know where things are relative to the current test file, and it's not trivial since the test
could be invoked from more than one directory or could ... |
[
0.03423288,
0.022207046,
0.009416187,
-0.02390868,
-0.029914448,
-0.025581716,
0.009902369,
-0.009752224,
0.017817114,
0.07618747,
0.004386356,
-0.016144078,
0.050820243,
-0.026811467,
-0.014642636,
0.017888611,
0.01323414,
0.034690466,
-0.017016346,
0.016544463,
0.009430487,... | pathlib objects (all fully resolved):
test_file_path - the current test file path, i.e. __file__
test_file_dir - the directory containing the current test file
tests_dir - the directory of the tests test suite
examples_dir - the directory of the examples test suite
repo_root_dir - the directory of the repository
src... |
[
0.02427004,
0.028284315,
0.025163678,
-0.029050289,
-0.06354751,
-0.015390412,
0.010546333,
-0.002390124,
0.014851393,
0.062980115,
0.011893881,
-0.0065320595,
0.052369956,
-0.0014415213,
0.012205944,
0.018794743,
0.0063121966,
0.0018147565,
-0.014014495,
-0.011234291,
-0.007... | test_file_path_str
test_file_dir_str
tests_dir_str
examples_dir_str
repo_root_dir_str
src_dir_str
To start using those all you need is to make sure that the test resides in a subclass of
transformers.test_utils.TestCasePlus. For example:
thon
from transformers.testing_utils import TestCasePlus
class PathExampleTest(T... |
[
0.019241832,
0.0039593237,
-0.0025585708,
-0.011846959,
0.013183963,
-0.038897168,
0.004048917,
-0.016760793,
-0.0019124671,
0.05246018,
0.01294275,
-0.0279806,
0.038290694,
-0.009579565,
-0.014996499,
0.015837295,
0.016857278,
0.056292005,
-0.018635355,
0.016057832,
0.046836... | src_dir - the directory of src (i.e. where the transformers sub-dir resides)
stringified paths---same as above but these return paths as strings, rather than pathlib objects:
test_file_path_str
test_file_dir_str
tests_dir_str
examples_dir_str
repo_root_dir_str
src_dir_str |
[
0.013369431,
-0.008994636,
0.008886527,
-0.029751487,
-0.03825603,
-0.034219977,
0.01303069,
-0.0042090286,
0.016201876,
0.069189504,
0.0338452,
-0.024360472,
0.033470426,
-0.0075099445,
-0.00540543,
0.019646937,
0.010183831,
-0.010436084,
-0.01627395,
0.025138855,
0.00267388... | If you don't need to manipulate paths via pathlib or you just need a path as a string, you can always invoked
str() on the pathlib object or use the accessors ending with _str. For example:
thon
from transformers.testing_utils import TestCasePlus
class PathExampleTest(TestCasePlus):
def test_something_involving_str... |
[
0.050016344,
0.036722597,
-0.014419869,
-0.0044838926,
-0.03718953,
0.0019260824,
0.016644647,
0.03595354,
0.029059468,
0.04927475,
-0.028620007,
-0.012215689,
0.022096733,
-0.011872359,
-0.058613334,
-0.010773702,
-0.020915676,
-0.006399675,
-0.050400876,
-0.0041542957,
0.00... | pytest test_this2.py --collect-only -q
and it will list:
test_this2.py::test_floor[integer-1-1.0]
test_this2.py::test_floor[negative--1.5--2.0]
test_this2.py::test_floor[large fraction-1.6-1]
So now you can run just the specific test: |
[
-0.033538256,
-0.0028441558,
0.020822007,
-0.0026758525,
-0.06256123,
-0.0022253436,
-0.0008563918,
0.0357143,
-0.0035054686,
0.057556327,
0.017993152,
-0.019461982,
0.027363736,
-0.022399638,
-0.037917543,
0.014429882,
0.0049232966,
0.0022729444,
-0.035823103,
0.0040902803,
... | Create a temporary dir of my choice, ensure it's empty before the test starts and don't empty it after the test.
python
def test_whatever(self):
tmp_dir = self.get_auto_remove_tmp_dir("./xxx")
This is useful for debug when you want to monitor a specific directory and want to make sure the previous tests didn't
lea... |
[
0.012046015,
0.008963984,
-0.0054058963,
0.012321071,
-0.05755005,
-0.003163137,
-0.0023661815,
0.006199325,
-0.015642893,
0.07238893,
0.017490702,
-0.013068657,
0.0488047,
-0.020946525,
-0.06488486,
0.03204748,
0.030241987,
0.018717872,
-0.053656958,
0.01675722,
-0.024106136... | In order to run the equivalent of rm -r safely, only subdirs of the project repository checkout are allowed if
an explicit tmp_dir is used, so that by mistake no /tmp or similar important part of the filesystem will
get nuked. i.e. please always pass paths that start with ./.
Each test can register multiple temporary ... |
[
0.025567502,
0.019906852,
0.010884741,
0.0016325293,
-0.047176912,
-0.011692366,
-0.012645508,
-0.0031140838,
0.012929268,
0.06053546,
0.0040490367,
-0.0061917882,
0.052386455,
-0.030966217,
-0.013293063,
0.008112625,
0.034633268,
0.0017262065,
-0.017243875,
-0.017942362,
-0.... |
Temporary files and directories
Using unique temporary files and directories are essential for parallel test running, so that the tests won't overwrite
each other's data. Also we want to get the temporary files and directories removed at the end of each test that created
them. Therefore, using packages like tempfile,... |
[
-0.011363961,
-0.0026751803,
-0.01034517,
-0.00047136165,
-0.04471739,
-0.006112744,
-0.015958775,
0.02047154,
-0.0030187657,
0.06777351,
0.009360567,
-0.018365582,
0.027664613,
-0.03484401,
-0.030003045,
0.0076785367,
0.0075144363,
0.0060238563,
-0.02250912,
-0.0032324384,
-... | This code creates a unique temporary directory, and sets tmp_dir to its location.
Create a unique temporary dir:
python
def test_whatever(self):
tmp_dir = self.get_auto_remove_tmp_dir()
tmp_dir will contain the path to the created temporary dir. It will be automatically removed at the end of the
test.
Create a t... |
[
-0.011793911,
-0.0055921767,
-0.034628347,
0.00091776083,
-0.054161586,
-0.03323116,
0.029423138,
0.013204797,
-0.0017370678,
0.048901584,
0.004951799,
0.0031042916,
0.066571906,
-0.059120234,
-0.033970848,
-0.027368449,
0.010924093,
-0.021998864,
-0.084105246,
0.00092717813,
... | Skipping tests
This is useful when a bug is found and a new test is written, yet the bug is not fixed yet. In order to be able to
commit it to the main repository we need make sure it's skipped during make test.
Methods: |
[
0.025563706,
-0.010351594,
-0.022266435,
0.004413877,
-0.04631944,
-0.009885246,
0.019415805,
0.018036468,
-0.0030838028,
0.06683871,
-0.021688426,
-0.01241403,
0.026102304,
-0.046634715,
-0.06389613,
-0.006955798,
-0.03709759,
-0.010955874,
-0.08113127,
0.000026298736,
-0.00... | A skip means that you expect your test to pass only if some conditions are met, otherwise pytest should skip
running the test altogether. Common examples are skipping windows-only tests on non-windows platforms, or skipping
tests that depend on an external resource which is not available at the moment (for example ... |
[
-0.039397813,
-0.00028048398,
0.04292142,
0.0017652716,
-0.034126274,
-0.018200679,
-0.02118326,
0.026413182,
-0.016494365,
0.06952882,
0.04078506,
-0.002306298,
0.023167023,
-0.026898718,
-0.05629448,
0.030880116,
-0.020295423,
0.005979034,
-0.036651064,
0.007213684,
-0.0104... | You can override the default behavior by directly overriding the before and after args, leading to one of the
following behaviors:
before=True: the temporary dir will always be cleared at the beginning of the test.
before=False: if the temporary dir already existed, any existing files will remain there.
after=True:... |
[
0.009996219,
0.023258809,
0.001169452,
0.022920908,
-0.074619666,
0.027750067,
0.0071944618,
0.009383774,
0.0017299794,
0.07428176,
0.009707596,
-0.0002114077,
0.022963146,
-0.041195687,
-0.020062834,
0.045532074,
0.018880183,
0.02873561,
-0.025398843,
-0.0061561223,
-0.00107... | Each test can register multiple temporary directories and they all will get auto-removed, unless requested
otherwise.
Temporary sys.path override
If you need to temporary override sys.path to import from another test for example, you can use the
ExtendSysPath context manager. Example:
thon
import os
from transformers.... |
[
0.007619015,
0.029585866,
-0.005403348,
0.008502664,
-0.03034515,
-0.03911618,
0.023917424,
-0.0029225862,
-0.019505726,
0.07409558,
0.062418323,
0.005364075,
0.055296768,
-0.099911205,
-0.037833255,
-0.03301573,
-0.022870136,
-0.012495447,
-0.06346561,
0.00054778036,
-0.0193... | Here is how to skip whole test unconditionally: |
[
0.010856814,
-0.02643458,
-0.02294218,
-0.019173698,
-0.050881393,
-0.009600653,
0.046961065,
0.0060564866,
-0.0034492642,
0.05383544,
0.004976327,
0.0041032266,
0.06371907,
-0.019325541,
-0.029512864,
0.0019170247,
-0.0135278795,
-0.03630441,
-0.05593364,
0.0090484945,
-0.04... |
python no-style
@unittest.skip("this bug needs to be fixed")
def test_feature_x():
or via pytest:
python no-style
@pytest.mark.skip(reason="this bug needs to be fixed")
or the xfail way:
python no-style
@pytest.mark.xfail
def test_feature_x():
Here's how to skip a test based on internal checks within the test:
python... |
[
0.0129396925,
-0.011939131,
-0.005929002,
0.0066591413,
-0.043483857,
-0.0036709786,
0.0127503965,
0.009147024,
0.009938008,
0.06268381,
0.0030202756,
-0.010303077,
0.02939487,
-0.04946018,
-0.046296243,
0.006743648,
0.0054929466,
-0.023418544,
-0.054841578,
0.015387011,
-0.0... | or the xfail way:
python
def test_feature_x():
pytest.xfail("expected to fail until bug XYZ is fixed")
Here is how to skip all tests in a module if some import is missing:
python
docutils = pytest.importorskip("docutils", minversion="0.3")
Skip a test based on a condition: |
[
0.050980896,
0.013685137,
-0.018362146,
-0.01929454,
-0.04941688,
0.01592589,
-0.0040077902,
-0.0023535427,
0.0100833895,
0.05723696,
-0.020121664,
0.004451429,
0.01494838,
-0.044273674,
-0.055522557,
-0.012632434,
-0.0071997354,
-0.031129926,
-0.09179569,
0.024016663,
0.0035... |
RUN_SLOW=1 pytest tests
Some decorators like @parameterized rewrite test names, therefore @slow and the rest of the skip decorators
@require_* have to be listed last for them to work correctly. Here is an example of the correct usage:
python no-style
@parameterized.expand()
@slow
def test_integration_foo():
As explai... |
[
0.009270866,
-0.034322776,
-0.005232697,
0.011049386,
-0.023027848,
-0.031163912,
0.007684798,
-0.0088395085,
0.0022878533,
0.07400766,
-0.01148738,
0.013537986,
0.044250634,
-0.0200548,
-0.031800997,
0.008414787,
0.014108705,
-0.012940722,
-0.057762075,
0.035888936,
-0.00491... | A xfail means that you expect a test to fail for some reason. A common example is a test for a feature not yet
implemented, or a bug not yet fixed. When a test passes despite being expected to fail (marked with
pytest.mark.xfail), it’s an xpass and will be reported in the test summary.
One of the important differe... |
[
0.019215504,
0.024213258,
0.019359117,
0.018037872,
-0.050092436,
-0.0061035794,
0.028593475,
-0.025678117,
0.0044376613,
0.057215672,
0.0138802575,
0.03621936,
0.01833946,
-0.058019906,
-0.005134187,
0.0022062645,
0.008071086,
-0.017563947,
-0.07594289,
-0.021987248,
0.00003... |
All tests that need to download a heavy set of weights or a dataset that is larger than ~50MB (e.g., model or
tokenizer integration tests, pipeline integration tests) should be set to slow. If you're adding a new model, you
should create and upload to the hub a tiny version of it (with random weights) for integra... |
[
0.04144292,
0.017938863,
0.00096854323,
-0.0067566764,
-0.02596103,
0.02326724,
0.016547566,
-0.009169246,
-0.016073933,
0.06542061,
0.038541917,
0.011056379,
0.014001787,
-0.067196734,
-0.024569731,
0.018501304,
-0.0057317037,
-0.017820455,
-0.03490086,
0.0017585489,
-0.0173... | Collectively, all the non-slow tests need to cover entirely the different internals, while remaining fast. For example,
a significant coverage can be achieved by testing with specially created tiny models with random weights. Such models
have the very minimal number of layers (e.g., 2), vocab size (e.g., 1000), etc. Th... |
[
0.018175822,
0.018854586,
0.026396422,
-0.01565685,
-0.013530051,
-0.017451806,
-0.026833847,
-0.0141786495,
-0.0022625504,
0.061692208,
-0.010015557,
0.017210467,
0.011297668,
-0.044255488,
-0.0066933786,
-0.013846809,
0.030182423,
-0.0056714597,
-0.052068826,
0.0058562346,
... |
grep tiny tests examples
Here is a an example of a script that created the tiny model
stas/tiny-wmt19-en-de. You can easily adjust it to your specific
model's architecture.
It's easy to measure the run-time incorrectly if for example there is an overheard of downloading a huge model, but if
you test it locally the do... |
[
-0.033765182,
-0.010059713,
0.005093653,
0.0075548585,
-0.027130336,
-0.02393379,
0.00059515494,
-0.022792168,
-0.006147976,
0.095681444,
0.006651633,
0.005674538,
0.03266385,
-0.009562772,
-0.027439246,
-0.017245224,
-0.011342361,
-0.025599217,
-0.033174224,
-0.0012918807,
0... | And, of course, most of the time, stderr will come as a part of an exception, so try/except has to be used in such
a case:
thon
def raise_exception(msg):
raise ValueError(msg)
def test_something_exception():
msg = "Not a good value"
error = ""
try:
raise_exception(msg)
except Exception as e:... |
[
0.029113786,
0.0011674499,
-0.007743637,
-0.015544528,
-0.045774773,
0.0060689505,
0.037215263,
-0.006562768,
-0.013061126,
0.052072737,
-0.036728602,
-0.0029861669,
0.05018335,
-0.022314843,
-0.03077416,
-0.01638903,
0.007256976,
-0.032062378,
-0.077121474,
0.018908216,
-0.0... |
python no-style
@pytest.mark.skipif(sys.version_info < (3,6), reason="requires python3.6 or higher")
def test_feature_x():
or:
python no-style
@unittest.skipIf(torch_device == "cpu", "Can't do half precision")
def test_feature_x():
or skip the whole module:
python no-style
@pytest.mark.skipif(sys.platform == 'win32',... |
[
-0.00008451179,
-0.007583977,
0.0039643515,
0.008563,
-0.029122472,
-0.010169424,
-0.00972128,
0.016340023,
-0.007515032,
0.056231745,
-0.016505493,
-0.027771145,
0.02494439,
-0.019235725,
-0.057307288,
-0.016160766,
0.023289705,
-0.010327998,
-0.048206516,
-0.000008712417,
-... |
An important potential issue with capturing stdout is that it may contain \r characters that in normal print
reset everything that has been printed so far. There is no problem with pytest, but with pytest -s these
characters get included in the buffer, so to be able to have the test run with and without -s, you have ... |
[
-0.009678121,
-0.020460006,
-0.022263717,
0.03941243,
-0.013837425,
-0.03074385,
-0.036289588,
0.039708562,
0.039520115,
0.044258222,
-0.031820692,
0.02770177,
0.0203254,
0.003149764,
0.008816647,
-0.026113428,
0.012780773,
-0.048430987,
-0.025615389,
-0.013756662,
-0.0057409... | Another approach to capturing stdout is via contextlib.redirect_stdout:
thon
from io import StringIO
from contextlib import redirect_stdout
def print_to_stdout(s):
print(s)
def test_result_and_stdout():
msg = "Hello"
buffer = StringIO()
with redirect_stdout(buffer):
print_to_stdout(msg)
out ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.