vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
0.018706292,
-0.0034992918,
-0.0074796234,
-0.019342856,
-0.007360268,
-0.040797945,
-0.017505502,
-0.021975914,
-0.045890454,
0.095426686,
0.021787839,
0.012065776,
0.026663339,
-0.035271417,
-0.008673181,
0.013150827,
0.0085429745,
-0.0033727025,
-0.06921183,
-0.008217459,
... |
The first sequence, the "context" used for the question, has all its tokens represented by a 0, whereas the second
sequence, corresponding to the "question", has all its tokens represented by a 1.
Some models, like [XLNetModel] use an additional token represented by a 2.
transfer learning
A technique that involves ta... |
[
0.034934647,
0.013823791,
-0.023524106,
-0.021766556,
-0.0062730946,
-0.025673721,
0.0011305766,
-0.022942763,
0.023794496,
0.0018555651,
-0.023835056,
-0.006661783,
0.0480757,
-0.041478135,
0.020049568,
0.016304638,
-0.010531769,
-0.050022524,
-0.044885073,
0.009896347,
0.02... | pip install transformers --upgrade
And then you can run the following code: |
[
0.0017470926,
0.010426084,
-0.023633398,
0.015840307,
-0.02688052,
-0.02335104,
-0.005851878,
0.006363652,
-0.05280102,
0.0503445,
-0.016390905,
-0.0030177054,
0.018692126,
-0.043934967,
-0.021911012,
0.04464086,
-0.010101371,
-0.010856681,
-0.05000567,
0.0050824513,
-0.00182... |
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModelForCausalLM
Will error if the minimal version of Transformers is not installed.
from transformers.utils import check_min_version
check_min_version("4.21.0")
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left"... |
[
0.0028294057,
0.01275999,
-0.0023486165,
0.013431568,
-0.0064296,
-0.034952603,
-0.009539466,
-0.016072093,
-0.013973409,
0.072164156,
0.0034342078,
0.0028542082,
0.021429457,
-0.04206523,
-0.02440577,
0.016438408,
0.01741525,
-0.00035272175,
-0.005883943,
0.04691891,
0.00808... |
As you can notice, enabling XLA on generate() is just a single line of code. The rest of the code remains unchanged. However, there are a couple of gotchas in the above code snippet that are specific to XLA. You need to be aware of those to realize the speed-ups that XLA can bring in. We discuss these in the followin... |
[
0.030596703,
0.02726338,
-0.030422032,
0.0031040662,
0.010407537,
-0.013675358,
0.0060516554,
-0.0045414707,
-0.041280806,
0.048762586,
-0.025400214,
0.02588056,
0.030946048,
-0.05263448,
-0.028136741,
0.05187757,
-0.00029202967,
-0.028777204,
-0.038660727,
0.03973787,
-0.013... |
In order to run the forward pass with an XLA-compiled function, we’d need to do:
py
xla_fn = tf.function(model, jit_compile=True)
_ = xla_fn(random_inputs)
The default call() function of the model is used for compiling the XLA graph. But if there’s any other model function you want to compile into XLA that’s also pos... |
[
0.0003032751,
0.018087193,
0.03611751,
0.016949633,
-0.006263686,
-0.06068879,
0.025097402,
0.0049092793,
-0.0041200975,
0.07479452,
0.028254129,
0.004582231,
0.03156727,
-0.033558,
-0.034809314,
0.0030980716,
0.030372834,
-0.027472056,
-0.0318801,
0.029832494,
-0.020476067,
... | This way, you can ensure that the inputs to xla_generate() will always receive inputs with the shape it was traced with and thus leading to speed-ups in the generation time. You can verify this with the code below: |
[
0.015815284,
0.022622045,
0.0039790045,
0.02229106,
-0.051950116,
-0.032954507,
-0.0033368235,
0.0009515793,
-0.048438806,
0.053590644,
-0.0036462217,
0.016765065,
0.0027629978,
-0.03778975,
-0.034796502,
0.033415005,
-0.011296632,
-0.031170068,
-0.04976274,
0.01875097,
0.004... |
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="")
model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2")
input_string = ["TensorFlow is"]
xla_generate = tf.functi... |
[
0.00001678659,
0.028259968,
-0.024502708,
0.014076309,
-0.01439836,
-0.023764674,
0.002824655,
0.011969559,
-0.026421595,
0.05582216,
-0.012459345,
0.019255962,
0.030836375,
-0.061833777,
-0.029172447,
0.03467415,
-0.016370922,
-0.030004412,
-0.04991789,
0.008319649,
0.013579... |
import time
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="")
model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2")
xla_generate = tf.function(model.generate, ji... |
[
0.022120992,
0.02217959,
0.014854758,
0.013660811,
0.0005823241,
-0.0522114,
0.015221,
-0.007529927,
-0.027922258,
0.043538798,
-0.005391076,
0.03164327,
0.016173229,
-0.081276335,
0.013624187,
0.034573205,
0.043626696,
0.000418431,
-0.006606998,
0.03665346,
-0.005270216,
-... |
On a Tesla T4 GPU, you can expect the outputs like so:
```bash
Execution time -- 30819.6 ms
Execution time -- 79.0 ms
Execution time -- 78.9 ms
``
The first call toxla_generate()` is time-consuming because of tracing, but the successive calls are orders of magnitude faster. Keep in mind that any change in the generat... |
[
0.035487033,
-0.00261663,
-0.030774584,
0.01255695,
0.014712033,
-0.025932832,
0.013426167,
-0.0055313795,
-0.029266026,
0.038820226,
-0.0021407157,
-0.031694088,
0.019654356,
-0.030113691,
-0.035257157,
0.050055392,
-0.00016129449,
-0.06856037,
-0.024783453,
0.028188486,
0.0... | The quantization method is available through a Python package that is pip-installable by anyone (it is also fine if you can only install the package from source). Ideally, pre-compiled kernels are included in the pip package.
The method can run on commonly-used hardware (CPU, GPU, ).
The method is wrapped in a nn.Modul... |
[
0.045770887,
0.0063236095,
-0.014972179,
0.0368352,
-0.016470797,
-0.039720394,
-0.002127128,
-0.022311207,
-0.026695017,
0.060953155,
-0.0155884335,
0.027577382,
0.021722965,
-0.03826379,
-0.02549052,
0.008368454,
0.000097712196,
-0.011071569,
-0.03820777,
0.0098950835,
0.00... |
This Colab Notebook provides an interactive demonstration if you want to fiddle with the XLA-compatible encoder-decoder (like T5) and decoder-only (like GPT2) text generation models.
This blog post provides an overview of the comparison benchmarks for XLA-compatible models along with a friendly introduction to XLA i... |
[
0.034019757,
0.012263756,
-0.03311708,
-0.008638931,
-0.012235547,
0.0022549373,
0.021424549,
0.00014203548,
0.0037834994,
0.034527514,
0.039351214,
-0.047503542,
0.027376596,
-0.057574067,
-0.0030359672,
0.057263773,
-0.007334276,
-0.0434979,
-0.012602261,
-0.018476734,
0.01... | For some quantization methods, they may require "pre-quantizing" the models through data calibration (e.g., AWQ). In this case, we prefer to only support inference in Transformers and let the third-party library maintained by the ML community deal with the model quantization itself.
Build a new HFQuantizer class |
[
0.030022362,
-0.013155788,
-0.0021652088,
-0.03321504,
0.029938346,
-0.0035795087,
-0.0023104898,
0.037387922,
-0.031366646,
0.047190003,
0.011013335,
-0.017489707,
0.055339728,
-0.044753484,
-0.013736911,
0.05209104,
-0.015403264,
-0.021690598,
-0.046153784,
0.0058252346,
-0... | Create a new quantization config class inside src/transformers/utils/quantization_config.py and make sure to expose the new quantization config inside Transformers main init by adding it to the _import_structure object of src/transformers/init.py. |
[
0.04060547,
0.030484496,
-0.042489856,
0.012316891,
-0.0003910765,
0.01787127,
0.01709624,
-0.0018767873,
0.013973326,
0.05543741,
0.024436226,
-0.015941294,
0.03914659,
-0.03586411,
-0.009155986,
0.043675195,
0.0013752976,
-0.05710904,
-0.016655536,
0.0025758336,
0.008920439... |
Contribute new quantization method
Transformers supports and integrates many quantization methods such as QLoRA, GPTQ, LLM.int8, and AWQ. However, there are other quantization approaches that are not yet integrated. To make adding and using these quantization methods with Transformers models easier, you should use the... |
[
0.017074455,
0.02747245,
-0.012879018,
-0.012586313,
0.005704262,
-0.00534535,
0.006523139,
0.019666985,
0.006307095,
0.025325947,
0.0037041116,
-0.012328453,
0.037549865,
-0.032113913,
-0.01027255,
0.028364504,
-0.0026900978,
-0.07666082,
-0.04150835,
0.0018363751,
0.0048261... | Create a new file inside src/transformers/quantizers/ named quantizer_your_method.py, and make it inherit from src/transformers/quantizers/base.py::HfQuantizer. Make sure to add the new quantizer and quantization config in the quantization auto-mapping in src/transformers/quantizers/auto.py.
Define the following class... |
[
0.036583453,
-0.00062784867,
-0.027883729,
0.000053559997,
0.012714981,
-0.014819273,
0.022872094,
-0.022381341,
-0.025236635,
0.038695183,
0.029817002,
-0.016730238,
0.03465018,
-0.03093235,
-0.030872865,
0.06787272,
0.016581524,
-0.06906242,
-0.055113122,
0.031794887,
0.025... | class Linear4bit(nn.Module):
def init(self, ):
def forward(self, x):
return my_4bit_kernel(x, self.weight, self.bias)
This way, Transformers models can be easily quantized by replacing some instances of nn.Linear with a target class.
The quantization method should be serializable. You can save the qu... |
[
0.023810789,
0.0030617586,
-0.017319638,
0.0030858961,
-0.009083155,
0.0069627617,
0.008058236,
-0.013576457,
-0.0005751241,
0.045096416,
-0.01028632,
0.014363714,
0.051245928,
-0.02339488,
-0.00566676,
0.01505442,
-0.02599431,
-0.0053511146,
-0.07165518,
0.013658154,
-0.0086... | is_trainable: A property method to determine whether you can fine-tune models on top of the quantization method (with or without PEFT approaches).
Write the validate_environment and update_torch_dtype methods. These methods are called before creating the quantized model to ensure users use the right configuration. Yo... |
[
0.008114173,
-0.024448289,
-0.0135236215,
-0.005949638,
-0.013954261,
0.00015995554,
0.0044575064,
0.0189784,
0.007993291,
0.041643687,
0.00010163965,
-0.013228972,
-0.009995392,
-0.052674126,
-0.00871858,
0.07222671,
0.0041590803,
-0.075732276,
-0.05358074,
0.024085645,
0.00... | Write the _process_model_after_weight_loading method. This method enables implementing additional features that require manipulating the model after loading the weights.
Document everything! Make sure your quantization method is documented in the docs/source/en/quantization.md file. |
[
0.035392422,
0.028500214,
-0.010997443,
-0.025290538,
-0.0028819032,
-0.023857646,
0.019401353,
0.020418705,
-0.00419479,
0.038401496,
-0.00037255182,
-0.008640337,
0.037455786,
-0.016120031,
-0.005362597,
0.040006336,
0.0058282865,
-0.051354837,
-0.05167007,
-0.0068277284,
0... | Document everything! Make sure your quantization method is documented in the docs/source/en/quantization.md file.
Add tests! You should add tests by first adding the package in our nightly Dockerfile inside docker/transformers-quantization-latest-gpu and then adding a new test file in tests/quantization/xxx. Feel free... |
[
0.007925523,
-0.0074233552,
0.011586256,
0.009715863,
0.00122358,
0.010654698,
0.023041513,
-0.012626982,
-0.025719743,
0.0558935,
0.0076053003,
-0.036068775,
0.009068138,
-0.02427874,
-0.005756739,
0.027801193,
-0.045850135,
-0.023769293,
-0.05670861,
-0.00024198683,
0.01622... | Define the following class attributes/property methods for your quantization method:
requires_calibration: Whether the quantization method requires a data calibration process. If set to True, you can only support inference (with quantized weights) and not inference and quantization. |
[
0.018488359,
0.040429596,
0.0014735931,
0.00020402821,
-0.024865877,
-0.03901237,
-0.022959063,
-0.00050971826,
-0.006822011,
0.046845764,
0.0065965434,
-0.021078017,
0.0039166976,
-0.026566548,
-0.014713384,
-0.03421957,
-0.0016716826,
-0.055761404,
-0.013412112,
-0.020601314,... | "Autoregressive generation iteratively selects the next token from a probability distribution to generate text" |
[
0.056376316,
-0.05329795,
-0.021322198,
0.018696535,
-0.013897909,
0.019088876,
0.021593818,
-0.008329691,
-0.016478302,
0.048076805,
0.015738891,
-0.03482777,
0.020658238,
-0.05797586,
-0.009408628,
0.06337809,
-0.023449892,
-0.033348944,
-0.032534085,
-0.013256583,
0.038479... |
required_packages: A list of strings of the required packages to use the quantized weights. You might need to define some new utility methods such as is_auto_awq_available in transformers/src/utils/import_utils.py.
requires_parameters_quantization: Only required if your quantization method requires extra attention to... |
[
0.017950019,
-0.018586334,
-0.016840165,
-0.0049721403,
-0.014228313,
0.0019736881,
0.008960212,
-0.005171914,
-0.0043099285,
0.04667301,
0.031756587,
-0.019962553,
0.019015478,
-0.05667648,
-0.012526538,
0.08310578,
-0.015715515,
-0.055729408,
-0.06966916,
0.020850435,
-0.01... |
Write the _process_model_before_weight_loading method. In Transformers, the quantized models are initialized first on the "meta" device before loading the weights. This means the _process_model_before_weight_loading method takes care of manipulating the model skeleton to replace some modules (e.g., nn.Linear) with th... |
[
0.038508326,
0.032769505,
-0.013446032,
0.008913195,
-0.030302089,
-0.0009105528,
0.009405292,
0.002956048,
0.011540023,
0.05453267,
0.008816161,
-0.01335593,
0.01335593,
-0.047684897,
0.012725214,
0.03016347,
0.005693771,
-0.07563046,
-0.040005412,
-0.017604599,
0.016329305,... | If you're interested in basic LLM usage, our high-level Pipeline interface is a great starting point. However, LLMs often require advanced features like quantization and fine control of the token selection step, which is best done through [~generation.GenerationMixin.generate]. Autoregressive generation with LLMs is al... |
[
0.026955815,
0.02897282,
-0.010798174,
-0.0011219584,
-0.021005655,
-0.0002885936,
0.0023069482,
-0.0063859783,
-0.027143108,
0.05212514,
0.039273944,
0.0041924864,
0.003954768,
-0.044316456,
0.020962432,
0.0598762,
0.009919337,
-0.034836538,
-0.046852116,
-0.0056512123,
0.01... | First, you need to load the model.
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-v0.1", device_map="auto", load_in_4bit=True
)
You'll notice two flags in the from_pretrained call:
device_map ensures the model is moved to your GPU(s)
load_in_4b... |
[
0.052053574,
0.00049654907,
0.0002828797,
-0.03273043,
-0.02250403,
0.006071926,
0.052677862,
-0.016008481,
-0.016588176,
0.049080785,
0.021344641,
-0.016008481,
0.015844978,
-0.035970774,
-0.014678157,
0.048961874,
-0.0061128023,
-0.046553914,
-0.054342624,
0.024897126,
0.00... | device_map ensures the model is moved to your GPU(s)
load_in_4bit applies 4-bit dynamic quantization to massively reduce the resource requirements
There are other ways to initialize a model, but this is a good baseline to begin with an LLM.
Next, you need to preprocess your text input with a tokenizer. |
[
-0.0059844595,
-0.004749284,
-0.01659902,
-0.013984204,
-0.021453042,
-0.03967007,
0.045535345,
0.002289769,
-0.008732905,
0.03397815,
-0.0023313027,
0.022002008,
0.015501086,
-0.02895077,
-0.031348888,
-0.014937673,
0.014923226,
-0.05218073,
-0.023923391,
-0.01186057,
-0.014... | The model_inputs variable holds the tokenized text input, as well as the attention mask. While [~generation.GenerationMixin.generate] does its best effort to infer the attention mask when it is not passed, we recommend passing it whenever possible for optimal results.
After tokenizing the inputs, you can call the [~gen... |
[
0.003926806,
0.012876113,
0.028147781,
0.022907503,
-0.011433335,
-0.060705565,
-0.014033058,
0.018415837,
0.014237224,
0.052756675,
0.03519834,
0.0047911117,
0.019572781,
-0.030107781,
-0.02605167,
0.012910141,
0.025139725,
-0.04938112,
-0.048401117,
0.0034827436,
-0.0001338... |
The process depicted above is repeated iteratively until some stopping condition is reached. Ideally, the stopping condition is dictated by the model, which should learn when to output an end-of-sequence (EOS) token. If this is not the case, generation stops when some predefined maximum length is reached.
Properly se... |
[
0.010886296,
-0.0008114826,
-0.014558606,
0.021380685,
-0.016997136,
-0.030481627,
0.013673187,
0.014609409,
-0.013629642,
0.08064568,
-0.009862984,
0.012345059,
0.021612925,
-0.04084538,
-0.034342635,
-0.023906305,
0.005668131,
-0.06851108,
-0.03991642,
-0.021162959,
-0.0082... | generated_ids = model.generate(**model_inputs)
tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
'A list of colors: red, blue, green, yellow, orange, purple, pink,'
Finally, you don't need to do it one sequence at a time! You can batch your inputs, which will greatly improve the throughput at a small ... |
[
0.013970766,
0.01790122,
0.010115466,
0.013850523,
-0.05212546,
-0.028903479,
0.019208865,
0.0018337101,
-0.0011216444,
0.06733624,
0.0034062667,
0.03405891,
-0.0032503263,
-0.04136369,
-0.04479062,
0.0096495235,
0.0018149221,
-0.039680284,
-0.011648568,
-0.00009264839,
0.001... | tokenizer.pad_token = tokenizer.eos_token # Most LLMs don't have a pad token by default
model_inputs = tokenizer(
["A list of colors: red, blue", "Portugal is"], return_tensors="pt", padding=True
).to("cuda")
generated_ids = model.generate(**model_inputs)
tokenizer.batch_decode(generated_ids, skip_special_tokens... |
[
-0.0060335835,
0.015784059,
0.00625907,
0.005531694,
-0.034448523,
-0.044544503,
0.03113169,
0.0052989335,
0.037328932,
0.024948994,
0.011208863,
-0.0069464403,
0.036572464,
-0.033168342,
-0.021195734,
-0.003369569,
0.011870775,
0.0017247902,
-0.0556879,
0.03558323,
0.0214284... | And that's it! In a few lines of code, you can harness the power of an LLM.
Common pitfalls
There are many generation strategies, and sometimes the default values may not be appropriate for your use case. If your outputs aren't aligned with what you're expecting, we've created a list of the most common pitfalls and how... |
[
0.02347264,
0.0072283056,
0.010755837,
-0.013004315,
-0.036034632,
0.014648285,
0.028470898,
-0.0007717626,
-0.0029746266,
0.049953084,
0.0070956084,
0.015599281,
0.0090160305,
-0.035326917,
0.0035680775,
0.052754465,
-0.0067306915,
-0.023649571,
-0.058268767,
-0.017162157,
0... | There are other ways to initialize a model, but this is a good baseline to begin with an LLM.
Next, you need to preprocess your text input with a tokenizer.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", padding_side="left")
model_inputs = tokenizer(["A li... |
[
0.011214523,
0.008881444,
0.005099137,
0.034466587,
-0.019466178,
-0.032920744,
0.005621575,
0.008301753,
-0.03005807,
0.068188876,
-0.003478148,
0.039991546,
0.0005215433,
-0.048064284,
-0.028898688,
0.01955206,
-0.017677007,
-0.06429564,
-0.05230104,
-0.00854508,
0.00844488... |
model_inputs = tokenizer(["A sequence of numbers: 1, 2"], return_tensors="pt").to("cuda")
By default, the output will contain up to 20 tokens
generated_ids = model.generate(**model_inputs)
tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
'A sequence of numbers: 1, 2, 3, 4, 5'
Setting max_new_tokens ... |
[
0.003601481,
-0.00036410027,
-0.0012737832,
0.017792989,
-0.03538246,
-0.047942217,
0.007682675,
0.02459619,
-0.008940104,
0.06186843,
0.030294597,
-0.00589102,
0.05433839,
-0.016731804,
-0.03794093,
-0.041051798,
0.0049352283,
-0.036865212,
-0.055385035,
-0.0041938536,
0.013... | Incorrect generation mode
By default, and unless specified in the [~generation.GenerationConfig] file, generate selects the most likely token at each iteration (greedy decoding). Depending on your task, this may be undesirable; creative tasks like chatbots or writing an essay benefit from sampling. On the other hand, i... |
[
0.018115353,
0.044000734,
0.010043645,
-0.0035244734,
-0.03676048,
0.013936019,
-0.0064823823,
-0.0023821467,
-0.01877757,
0.049063027,
0.021558888,
0.016231708,
-0.016584892,
-0.04488369,
-0.018748138,
0.050799508,
0.0010669074,
-0.03266944,
-0.038379237,
-0.004227161,
0.011... | from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1")
tokenizer.pad_token = tokenizer.eos_token # Most LLMs don't have a pad token by default
model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-v0.1", device_map="auto", ... |
[
0.05056199,
0.023569407,
0.025224878,
0.023162553,
-0.06240281,
-0.012437071,
0.03369864,
-0.008249292,
-0.029377582,
0.066050455,
-0.0036195875,
0.03010711,
-0.017831378,
-0.01924835,
-0.062963985,
0.009070013,
0.02110023,
-0.031117229,
-0.05931634,
0.007779307,
0.0436034,
... | Wrong padding side
LLMs are decoder-only architectures, meaning they continue to iterate on your input prompt. If your inputs do not have the same length, they need to be padded. Since LLMs are not trained to continue from pad tokens, your input needs to be left-padded. Make sure you also don't forget to pass the atten... |
[
0.017152945,
0.007230605,
0.0018145179,
0.051664837,
-0.05410937,
-0.0505387,
0.03306989,
0.047544833,
-0.01639761,
0.064436845,
0.006015204,
0.012744542,
-0.008652006,
-0.011989208,
-0.038398426,
-0.030460553,
0.023223082,
-0.03853576,
-0.037437093,
0.007834872,
-0.002810185... | Generated output is too short/long
If not specified in the [~generation.GenerationConfig] file, generate returns up to 20 tokens by default. We highly recommend manually setting max_new_tokens in your generate call to control the maximum number of new tokens it can return. Keep in mind LLMs (more precisely, decoder-onl... |
[
0.015854236,
0.025562402,
0.015182694,
0.033226743,
-0.04312469,
-0.008496469,
0.012496525,
-0.020321451,
-0.032350816,
0.0604388,
-0.03173767,
0.026832491,
-0.018160839,
-0.04233636,
-0.0593001,
0.026628109,
0.014022094,
-0.017299512,
-0.04709555,
-0.00395261,
0.03711001,
... |
The tokenizer initialized above has right-padding active by default: the 1st sequence,
which is shorter, has padding on the right side. Generation fails to capture the logic.
model_inputs = tokenizer(
["1, 2, 3", "A, B, C, D, E"], padding=True, return_tensors="pt"
).to("cuda")
generated_ids = model.generate(**m... |
[
0.014679932,
0.04748164,
0.0042250613,
-0.042538032,
-0.0015340996,
-0.03995126,
0.00004589163,
0.008924364,
-0.009851291,
0.028353898,
-0.004322065,
0.016612826,
0.026543157,
-0.031501137,
-0.0036394447,
-0.0067759063,
-0.0122153135,
-0.03966384,
-0.03724952,
0.00040665315,
... |
Set seed or reproducibility -- you don't need this unless you want full reproducibility
from transformers import set_seed
set_seed(42)
model_inputs = tokenizer(["I am a cat."], return_tensors="pt").to("cuda")
LLM + greedy decoding = repetitive, boring output
generated_ids = model.generate(**model_inputs)
tokenizer.ba... |
[
0.04576553,
0.05592583,
-0.05189116,
0.02202871,
-0.024104945,
-0.033043064,
-0.006044643,
0.026549308,
0.044499174,
0.050153602,
0.0074508875,
-0.019864125,
0.015343527,
-0.04791539,
0.01767009,
-0.007693851,
0.016933834,
-0.028419392,
-0.033013616,
-0.0033444325,
-0.0050691... | Further resources
While the autoregressive generation process is relatively straightforward, making the most out of your LLM can be a challenging endeavor because there are many moving parts. For your next steps to help you dive deeper into LLM usage and understanding:
Advanced generate usage |
[
-0.005444679,
-0.021778716,
-0.025336692,
0.012897669,
-0.030548574,
-0.007602398,
0.009040877,
0.027838396,
0.009624608,
0.09923422,
-0.012063769,
0.01559395,
-0.021806512,
-0.021973291,
-0.04172285,
0.0015461915,
-0.017247854,
-0.0345513,
-0.06243139,
0.0024217875,
0.036775... | Wrong prompt
Some models and tasks expect a certain input prompt format to work properly. When this format is not applied, you will get a silent performance degradation: the model kinda works, but not as well as if you were following the expected prompt. More information about prompting, including which models and task... |
[
0.041681424,
0.0022218318,
-0.019314747,
0.049113434,
-0.04315087,
0.0028258592,
0.023694828,
0.018509377,
-0.0018385747,
0.03752741,
-0.035464533,
0.009537275,
-0.0048251543,
-0.036990497,
-0.025658801,
0.04657016,
-0.011600152,
-0.034362447,
-0.068272755,
-0.0030589926,
0.0... | LLM leaderboards
Open LLM Leaderboard, which focuses on the quality of the open-source models;
Open LLM-Perf Leaderboard, which focuses on LLM throughput.
Latency, throughput and memory utilization
Guide on how to optimize LLMs for speed and memory;
Guide on quantization such as bitsandbytes and autogptq, which show... |
[
0.03452753,
-0.00027715816,
-0.016200053,
0.030128935,
-0.028073383,
-0.02818838,
-0.0046537416,
0.024810374,
-0.015121966,
0.039156117,
-0.0032378542,
0.013619831,
0.013986381,
-0.05042572,
-0.022136718,
0.022553578,
-0.013828261,
-0.055313043,
-0.061292835,
-0.017321263,
0.... |
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
model = AutoModelForCausalLM.from_pretrained(
"HuggingFaceH4/zephyr-7b-alpha", device_map="auto", load_in_4bit=True
)
set_seed(0)
prompt = """How many helicopters can a human eat in one sitting? Reply as a thug."""
model_inputs = tokeniz... |
[
0.041671723,
-0.025862245,
-0.025962485,
-0.007410693,
0.014148338,
0.008069421,
-0.025747683,
0.0056958515,
0.008234103,
0.06220686,
0.0207213,
-0.00717441,
0.008183982,
-0.049003653,
-0.027279941,
0.016998053,
-0.0028550858,
-0.01031053,
-0.04868861,
0.012859521,
-0.0007433... | Export to TorchScript
This is the very beginning of our experiments with TorchScript and we are still
exploring its capabilities with variable-input-size models. It is a focus of interest to
us and we will deepen our analysis in upcoming releases, with more code examples, a more
flexible implementation, and benchmarks... |
[
0.0498731,
-0.009345695,
-0.039557565,
-0.0087285265,
-0.0077513433,
-0.0006327815,
-0.025509633,
-0.007861552,
0.020792702,
0.04402469,
0.0102641005,
-0.009389779,
0.03309199,
-0.06389164,
-0.00019286518,
0.04202624,
0.009455904,
-0.012225815,
-0.07247322,
0.015105935,
0.024... | There are two PyTorch modules, JIT and
TRACE, that allow developers to export their
models to be reused in other programs like efficiency-oriented C++ programs.
We provide an interface that allows you to export 🤗 Transformers models to TorchScript
so they can be reused in a different environment than PyTorch-based Pyt... |
[
0.033990882,
0.011611838,
0.001264192,
0.03501913,
-0.029936654,
-0.05523151,
-0.0030627048,
0.03305077,
0.027307281,
0.0294666,
-0.024619153,
0.02790954,
0.039690305,
-0.041394256,
0.0010007039,
0.008549132,
0.017832728,
-0.022988647,
-0.057669923,
0.000113210364,
0.00793953... | Guide on how to control different generation methods, how to set up the generation configuration file, and how to stream the output;
Guide on the prompt template for chat LLMs;
Guide on to get the most of prompt design;
API reference on [~generation.GenerationConfig], [~generation.GenerationMixin.generate], and generat... |
[
0.05422032,
0.020921662,
-0.01284539,
0.03468965,
-0.020396492,
-0.011007292,
0.014222188,
0.0061494643,
-0.008140145,
0.01002792,
-0.0038252294,
-0.03278768,
0.0077994936,
-0.028259857,
-0.016337065,
0.023405578,
0.0053333207,
-0.016351258,
-0.04806021,
-0.002308267,
-0.0161... | Guide on how to optimize LLMs for speed and memory;
Guide on quantization such as bitsandbytes and autogptq, which shows you how to drastically reduce your memory requirements.
Related libraries
text-generation-inference, a production-ready server for LLMs;
optimum, an extension of 🤗 Transformers that optimizes for ... |
[
0.030151898,
-0.024776401,
0.008411151,
-0.020806175,
-0.023916867,
0.033153445,
0.0077835554,
0.017722767,
-0.011787891,
0.056647368,
0.050671563,
0.0060747205,
0.0073742536,
-0.051053576,
0.010403087,
0.03293515,
0.0034927086,
-0.018636875,
-0.038337935,
-0.032280266,
0.023... | model instantiation with the torchscript flag
a forward pass with dummy inputs |
[
0.04499143,
-0.026821814,
-0.033712674,
-0.0037370543,
-0.020734375,
0.029093016,
-0.0017777564,
0.019390194,
-0.02787244,
0.035504915,
0.038656786,
-0.007300292,
-0.00005365377,
-0.07100983,
-0.011572315,
0.051944092,
-0.0054385248,
-0.047030877,
-0.03405258,
-0.016701832,
0... |
These necessities imply several things developers should be careful about as detailed
below.
TorchScript flag and tied weights
The torchscript flag is necessary because most of the 🤗 Transformers language models
have tied weights between their Embedding layer and their Decoding layer.
TorchScript does not allow you ... |
[
0.023410395,
-0.033399757,
-0.01226788,
-0.02379941,
0.0048244866,
0.012851403,
-0.00672441,
-0.0066514695,
0.0052378154,
0.038790397,
0.024299573,
-0.016005207,
0.018200366,
-0.03484467,
0.028787144,
0.04295842,
-0.0021743185,
-0.056824043,
-0.048932586,
-0.004897427,
0.0000... | Loading a model
Now you can load the previously saved BertModel, traced_bert.pt, from disk and use
it on the previously initialised dummy_input:
thon
loaded_model = torch.jit.load("traced_bert.pt")
loaded_model.eval()
all_encoder_layers, pooled_output = loaded_model(*dummy_input) |
[
0.039694756,
-0.0008535031,
-0.048330415,
-0.014051241,
-0.010670161,
-0.00315787,
-0.009433359,
-0.023360187,
0.01378778,
0.02068167,
0.03419135,
-0.017622598,
0.03401571,
-0.07997498,
-0.0044129677,
0.029902797,
-0.010575023,
-0.013992694,
0.0002895324,
-0.0015514912,
0.006... | Easy-to-use API with one line of code change to trace and optimize a TorchScript
model for inference in the cloud.
Out of the box performance optimizations for improved
cost-performance.
Support for Hugging Face transformers models built with either
PyTorch
or
TensorFlow. |
[
0.03321122,
0.0034747827,
-0.06953233,
-0.015197361,
0.026654059,
0.0189967,
-0.016546933,
-0.024659038,
0.020346275,
0.048525937,
0.005299273,
-0.025671218,
0.061141506,
-0.058941115,
0.000966338,
-0.0011322841,
-0.012175492,
-0.017940514,
-0.027768923,
-0.024262968,
0.03121... |
Implications
Transformers models based on the BERT (Bidirectional Encoder Representations from
Transformers)
architecture, or its variants such as
distilBERT and
roBERTa run best on
Inf1 for non-generative tasks such as extractive question answering, sequence
classification, and token classification. However, text ge... |
[
0.026626173,
0.028153487,
-0.025480688,
0.023100624,
-0.05259051,
-0.04276479,
0.025366139,
0.024946127,
0.016189529,
0.05477966,
-0.003376,
0.046633985,
0.047754016,
-0.0056224247,
-0.028204398,
0.01211669,
0.015985886,
-0.03909924,
-0.045590322,
-0.012097599,
-0.029146241,
... | Training on TPU with TensorFlow
If you don't need long explanations and just want TPU code samples to get started with, check out our TPU example notebook! |
[
0.027570568,
0.01625703,
-0.05542362,
0.041158114,
-0.015268331,
-0.00526482,
0.019618606,
-0.0128601445,
0.008855915,
0.036016878,
-0.014886976,
0.018799398,
0.044802174,
-0.04468918,
-0.0068785176,
0.007245749,
0.021002784,
-0.022994306,
-0.066723034,
-0.01605929,
0.0238982... |
What is a TPU?
A TPU is a Tensor Processing Unit. They are hardware designed by Google, which are used to greatly speed up the tensor computations within neural networks, much like GPUs. They can be used for both network training and inference. They are generally accessed through Google’s cloud services, but small TP... |
[
0.022659233,
0.03822388,
-0.08374505,
-0.031969063,
-0.01775094,
0.030115785,
-0.016592639,
-0.014181929,
0.027538568,
0.03639956,
0.031505745,
-0.025091661,
0.038050137,
-0.041843567,
-0.01601349,
0.04549221,
-0.013030868,
-0.01775094,
-0.010497089,
-0.026134131,
0.056611884... |
Using a traced model for inference
Use the traced model for inference by using its __call__ dunder method:
python
traced_model(tokens_tensor, segments_tensors)
Deploy Hugging Face TorchScript models to AWS with the Neuron SDK
AWS introduced the Amazon EC2 Inf1
instance family for low cost, high performance machine le... |
[
0.005039278,
0.00024103458,
-0.014141904,
0.04310052,
0.0032488648,
-0.040633485,
0.038456693,
-0.0014865688,
-0.0009586963,
0.0382245,
-0.038863026,
-0.012233581,
0.04652534,
-0.011101649,
-0.038282547,
-0.011058113,
0.011936086,
-0.016325954,
-0.054855205,
-0.011370121,
0.0... |
🤗Specific Hugging Face Tip🤗: The methods Dataset.to_tf_dataset() and its higher-level wrapper model.prepare_tf_dataset() , which you will see throughout our TF code examples, will both fail on a TPU Node. The reason for this is that even though they create a tf.data.Dataset it is not a “pure” tf.data pipeline and u... |
[
0.024314128,
0.0070857303,
-0.016737167,
0.038761243,
-0.02295706,
-0.006343584,
0.009082457,
0.0004921137,
0.049787417,
0.04828899,
0.027551299,
-0.010913085,
0.0140301,
-0.06513924,
-0.022137165,
0.004078271,
0.028922502,
-0.006096202,
-0.03191936,
-0.017373292,
0.03064711,... | If you can fit all your data in memory as np.ndarray or tf.Tensor, then you can fit() on that data even when using Colab or a TPU Node, without needing to upload it to Google Cloud Storage. |
[
0.035185777,
0.020575244,
-0.034492906,
0.040487744,
-0.008540384,
-0.024280597,
0.018120073,
-0.006081446,
0.015152779,
0.042445857,
-0.015755275,
-0.008623227,
0.0432291,
-0.025109028,
-0.017592888,
0.028648693,
0.053652287,
-0.020876493,
-0.07085355,
0.017999573,
0.0316611... |
The second way to access a TPU is via a TPU VM. When using a TPU VM, you connect directly to the machine that the TPU is attached to, much like training on a GPU VM. TPU VMs are generally easier to work with, particularly when it comes to your data pipeline. All of the above warnings do not apply to TPU VMs!
This is ... |
[
0.003546468,
-0.010080878,
-0.004413382,
0.008819912,
-0.0244929,
-0.028536217,
0.03569083,
-0.00797013,
0.019257149,
0.049287338,
-0.015515368,
0.011389816,
0.02556198,
-0.03818535,
-0.008929561,
-0.0014280102,
0.020559235,
-0.029906832,
-0.06792771,
0.07149131,
0.014747824,... | XLA compiled code is usually faster - so even if you’re not planning to run on TPU, adding jit_compile=True can improve your performance. Be sure to note the caveats below about XLA compatibility, though! |
[
0.018322848,
-0.0068962,
-0.004195356,
0.027879681,
0.016111238,
-0.04069361,
0.023804959,
-0.0097243795,
-0.017411398,
0.10910606,
0.009235145,
0.048950285,
0.043508388,
-0.056134664,
-0.015548283,
0.03259778,
0.026405275,
0.0076199993,
-0.053990074,
0.035921898,
-0.00209265... | How do I make my model XLA compatible?
In many cases, your code is probably XLA-compatible already! However, there are a few things that work in normal TensorFlow that don’t work in XLA. We’ve distilled them into three core rules below: |
[
0.00027036862,
0.005755113,
0.009674154,
0.021031374,
-0.035274845,
-0.05210551,
0.029961362,
-0.009430735,
0.019723868,
0.051827315,
-0.011364175,
0.011635413,
0.034718458,
-0.046903305,
-0.0073025604,
0.017150585,
0.015606615,
-0.031352326,
-0.08996755,
0.065319665,
0.00179... | Tip born of painful experience: Although using jit_compile=True is a good way to get a speed boost and test if your CPU/GPU code is XLA-compatible, it can actually cause a lot of problems if you leave it in when actually training on TPU. XLA compilation will happen implicitly on TPU, so remember to remove that line bef... |
[
0.028819429,
0.0026640398,
0.035145644,
0.032896325,
-0.004793866,
-0.020314183,
0.06781703,
0.0059255557,
-0.006308643,
0.028453914,
0.0066600996,
0.005939614,
-0.017122958,
-0.044283513,
-0.06618628,
0.0083014015,
-0.0045127007,
-0.06686108,
-0.034527082,
0.01791022,
-0.003... | 🤗Specific HuggingFace Tip🤗: Our tokenizers and data collators have methods that can help you here. You can use padding="max_length" or padding="longest" when calling tokenizers to get them to output padded data. Our tokenizers and data collators also have a pad_to_multiple_of argument that you can use to reduce the n... |
[
0.031423155,
0.006503276,
-0.004730292,
-0.008108071,
-0.0051787943,
-0.037786275,
0.035123296,
-0.021920525,
-0.036945336,
0.07635743,
0.009222318,
0.013104661,
0.025522554,
-0.06363119,
-0.02550854,
0.027694985,
0.00017322505,
-0.011948368,
-0.06464032,
0.03728171,
0.016230... | 🤗Specific HuggingFace Tip🤗: We’ve put a lot of effort into rewriting our TensorFlow models and loss functions to be XLA-compatible. Our models and loss functions generally obey rule #1 and #2 by default, so you can skip over them if you’re using transformers models. Don’t forget about these rules when writing your ow... |
[
0.029319383,
-0.010056406,
-0.027364757,
-0.01014139,
-0.0023636096,
0.00089232903,
0.026543248,
-0.02271898,
0.03628805,
0.034730013,
-0.026741544,
0.019673731,
0.039432444,
-0.011904802,
-0.020764355,
0.036344703,
0.025310984,
-0.00798847,
-0.061528213,
0.0119543765,
0.0073... |
How do I actually train my model on TPU?
Once your training is XLA-compatible and (if you’re using TPU Node / Colab) your dataset has been prepared appropriately, running on TPU is surprisingly easy! All you really need to change in your code is to add a few lines to initialize your TPU, and to ensure that your model... |
[
0.021062814,
0.007637929,
-0.017758008,
0.018240254,
-0.01998485,
-0.014219172,
0.0140206,
0.0012916044,
0.017914029,
0.023360575,
-0.027998649,
0.0039891694,
0.03128927,
-0.02964396,
-0.009949874,
0.037615206,
0.011680287,
-0.020594751,
-0.0833435,
0.037501737,
0.005386265,
... |
Make sure your code follows the three rules of XLA
Compile your model with jit_compile=True on CPU/GPU and confirm that you can train it with XLA
Either load your dataset into memory or use a TPU-compatible dataset loading approach (see notebook)
Migrate your code either to Colab (with accelerator set to “TPU”) or a ... |
[
-0.014048894,
-0.0034819457,
-0.03191635,
0.0067786626,
-0.01496079,
-0.026558964,
0.027100403,
-0.007473271,
-0.044397924,
0.048387468,
0.01845164,
0.012061247,
0.033654653,
-0.058275837,
-0.03955348,
0.02650197,
-0.0066575515,
-0.013699809,
-0.061723944,
0.025946284,
0.0086... |
XLA Rule #1: Your code cannot have “data-dependent conditionals”
What that means is that any if statement cannot depend on values inside a tf.Tensor. For example, this code block cannot be compiled with XLA!
python
if tf.reduce_sum(tensor) > 10:
tensor = tensor / 2.0
This might seem very restrictive at first, but... |
[
0.018285258,
-0.0084801195,
-0.023290884,
0.026750654,
-0.0015440148,
-0.027192326,
-0.037129965,
-0.0023463871,
0.0052706297,
0.047259,
-0.00200041,
-0.0039492915,
0.030858211,
-0.01866804,
-0.018903598,
-0.017740527,
-0.023923947,
-0.014589927,
-0.06666316,
-0.029017908,
-0... |
| Task | Description | Modality | Pipeline identifier |
|------------------------------|--------------------------------------------------------------------------------------------... |
[
0.019358639,
0.010816348,
-0.015043762,
0.0044715833,
-0.0069679436,
-0.019358639,
0.028586062,
0.0031322949,
-0.005535725,
0.031312015,
-0.028965073,
-0.0426823,
0.009591856,
-0.025656028,
-0.019154556,
-0.022390716,
-0.0030557641,
-0.020349894,
-0.040728945,
0.000684677,
-0... | from transformers import pipeline
classifier = pipeline("sentiment-analysis")
The [pipeline] downloads and caches a default pretrained model and tokenizer for sentiment analysis. Now you can use the classifier on your target text:
classifier("We are very happy to show you the 🤗 Transformers library.")
[{'label': 'PO... |
[
0.045460165,
0.0045363093,
-0.034528222,
-0.02440273,
-0.013112361,
0.0035450417,
-0.020101637,
0.017234242,
-0.00017104499,
0.039874718,
0.015247973,
-0.019907491,
0.05274813,
-0.057019353,
-0.0034759704,
0.039247476,
0.0025575077,
-0.008094419,
-0.060245175,
-0.008094419,
0... |
Quick tour
[[open-in-colab]]
Get up and running with 🤗 Transformers! Whether you're a developer or an everyday user, this quick tour will help you get started and show you how to use the [pipeline] for inference, load a pretrained model and preprocessor with an AutoClass, and quickly train a model with PyTorch or Ten... |
[
0.03069622,
-0.009624368,
-0.06523653,
-0.029848259,
-0.037677772,
-0.0028018074,
-0.015461172,
-0.015093721,
0.011807869,
0.058170184,
0.012952618,
-0.053817313,
0.03253347,
-0.049153525,
0.018202916,
0.042115442,
-0.017623475,
-0.021326242,
-0.030611424,
-0.00882587,
0.0169... | !pip install transformers datasets
You'll also need to install your preferred machine learning framework:
pip install torch
pip install tensorflow
Pipeline
The [pipeline] is the easiest and fastest way to use a pretrained model for inference. You can use the [pipeline] out-of-the-box for many tasks across different... |
[
0.029745013,
0.010643977,
-0.03292731,
0.011806459,
-0.004944182,
-0.015155861,
-0.031299833,
-0.0174227,
0.0087549435,
0.047574583,
0.031067336,
-0.028800495,
0.04867894,
-0.0373738,
0.000235221,
0.02787051,
-0.04344777,
-0.024862587,
-0.06556399,
-0.04896956,
0.0030315355,
... | The [pipeline] can also iterate over an entire dataset for any task you like. For this example, let's choose automatic speech recognition as our task:
import torch
from transformers import pipeline
speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
Load an audio dataset ... |
[
0.012670394,
0.008333304,
-0.016115407,
-0.016376503,
-0.028967116,
-0.01969822,
0.011589748,
0.016463535,
-0.015375636,
0.060690243,
0.010987777,
-0.029039644,
0.006197396,
-0.020786118,
-0.03257894,
0.01263413,
-0.021453364,
-0.03031611,
-0.042819694,
-0.028517451,
0.001119... | If you have more than one input, pass your inputs as a list to the [pipeline] to return a list of dictionaries:
results = classifier(["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."])
for result in results:
print(f"label: {result['label']}, with score: {round(result['sco... |
[
0.0018536539,
0.05503574,
0.0020490533,
0.018462604,
-0.007358284,
-0.031348404,
-0.041009232,
0.012054912,
0.02409574,
0.04729018,
0.031348404,
-0.04312166,
0.070132546,
-0.03534793,
-0.021152427,
0.02974296,
-0.037460357,
-0.047374677,
-0.023011362,
-0.049656097,
-0.0076540... | You need to make sure the sampling rate of the dataset matches the sampling
rate facebook/wav2vec2-base-960h was trained on:
dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate))
The audio files are automatically loaded and resampled when calling the "audio" c... |
[
0.027646199,
-0.032813977,
-0.039393716,
0.019103834,
0.010879161,
-0.043093055,
-0.0038581837,
-0.00047962554,
0.044985082,
0.034988396,
0.020106327,
0.007702248,
0.025415301,
-0.013321853,
-0.018284896,
0.0041899947,
-0.031740885,
-0.033463478,
-0.010095522,
-0.026488392,
-... |
result = speech_recognizer(dataset[:4]["audio"])
print([d["text"] for d in result])
['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FONDERING HOW I'D SET UP A JOIN TO HELL T WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M... |
[
0.015796782,
-0.008536502,
-0.022021906,
0.023964599,
-0.023680994,
-0.012889834,
0.013981712,
0.0005375196,
0.013088358,
0.065058924,
-0.011358369,
-0.017654393,
0.008586133,
-0.05113393,
-0.019129138,
-0.01442839,
0.000075498676,
-0.038683683,
-0.065058924,
-0.02397878,
0.0... |
For larger datasets where the inputs are big (like in speech or vision), you'll want to pass a generator instead of a list to load all the inputs in memory. Take a look at the pipeline API reference for more information.
Use another model and tokenizer in the pipeline
The [pipeline] can accommodate any model from the... |
[
0.0030805243,
-0.026586866,
-0.015041839,
-0.008270237,
0.0076943724,
-0.0068132314,
0.02229911,
-0.0048046466,
-0.017220408,
0.052507672,
0.0028793188,
-0.019301843,
-0.0048254607,
-0.057003573,
-0.002509864,
0.013008971,
0.004787301,
-0.07104632,
-0.045042258,
0.009491345,
... | model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
Use [AutoModelForSequenceClassification] and [AutoTokenizer] to load the pretrained model and it's associated tokenizer (more on an AutoClass in the next section): |
[
-0.0032306204,
0.03451562,
0.014655221,
-0.004606722,
-0.015198007,
-0.035183664,
-0.007348487,
-0.0241331,
-0.0015161474,
0.042532153,
-0.0050312085,
-0.008705452,
0.0623508,
-0.02101556,
-0.0044083963,
0.025441352,
-0.042003285,
-0.02354856,
-0.065078646,
-0.052107453,
-0.0... | Load an audio dataset (see the 🤗 Datasets Quick Start for more details) you'd like to iterate over. For example, load the MInDS-14 dataset:
from datasets import load_dataset, Audio
dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT
You need to make sure the sampling rate... |
[
0.010700154,
-0.010665026,
-0.0068500657,
-0.0017388628,
0.009421475,
0.017170832,
-0.00073857757,
-0.012793815,
0.004412145,
0.04614485,
0.019166132,
-0.028327655,
0.012568993,
-0.046285365,
0.012133398,
0.03282411,
-0.0063020606,
-0.05319866,
-0.0654796,
-0.029507974,
-0.02... | from transformers import AutoTokenizer, AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
``
</pt>
<tf>
Use [TFAutoModelForSequenceClassification] and [AutoTokenizer] to load the pretrained model and it's assoc... |
[
-0.00088979205,
-0.013111961,
-0.014962293,
0.018372709,
0.00086167426,
0.021855688,
0.0028843412,
-0.022320084,
-0.0066902204,
0.035207104,
-0.041679636,
0.011101993,
-0.004977756,
-0.017806726,
-0.019577239,
0.015992675,
-0.019983586,
-0.0638546,
-0.0629258,
-0.035207104,
-... | from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
Specify the model and tokenizer in the [pipeline], and now you can apply the classifier on French text: |
[
0.0020090167,
-0.004670194,
-0.029434176,
0.0019057578,
0.006974494,
-0.011572225,
0.03028923,
-0.013021471,
-0.018985117,
0.033651482,
-0.05217284,
-0.008238961,
0.0048622186,
-0.01628952,
-0.03582535,
-0.007217243,
-0.024463264,
-0.04857871,
-0.05767997,
-0.0042173043,
-0.0... | Specify the model and tokenizer in the [pipeline], and now you can apply the classifier on French text:
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
classifier("Nous sommes très heureux de vous présenter la bibliothèque 🤗 Transformers.")
[{'label': '5 stars', 'score': 0.7273}] |
[
0.019086441,
-0.005015267,
-0.028264178,
0.0067377845,
-0.018030597,
-0.015472201,
0.0063249217,
0.005607488,
0.0007897692,
0.044914052,
0.05455203,
-0.011837655,
0.011350341,
-0.031512935,
0.005181089,
0.03822703,
0.012940878,
-0.029726118,
-0.06968582,
-0.013746299,
0.00835... | If you can't find a model for your use-case, you'll need to finetune a pretrained model on your data. Take a look at our finetuning tutorial to learn how. Finally, after you've finetuned your pretrained model, please consider sharing the model with the community on the Hub to democratize machine learning for everyone! ... |
[
0.03100191,
0.014657564,
-0.018147461,
-0.011509387,
-0.011683881,
-0.031030994,
0.039726652,
0.005009455,
-0.014039561,
0.034549974,
-0.01813292,
0.022815196,
0.0066744266,
-0.0077577485,
-0.020023279,
-0.0016122593,
-0.008884694,
-0.04254765,
-0.02556349,
0.0047767954,
-0.0... | Pass your text to the tokenizer:
encoding = tokenizer("We are very happy to show you the 🤗 Transformers library.")
print(encoding)
{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask':... |
[
0.035326608,
0.0029610794,
-0.0069710896,
0.004986719,
-0.01674162,
-0.040031318,
-0.0085634,
0.012023152,
-0.0148707405,
0.05150421,
-0.00015744707,
0.000077218916,
0.016191361,
-0.0421223,
-0.03461127,
0.050623797,
-0.008205732,
-0.07345953,
-0.03518904,
-0.015544808,
-0.01... | tf_batch = tokenizer(
["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."],
padding=True,
truncation=True,
max_length=512,
return_tensors="tf",
)
Check out the preprocess tutorial for more details about tokenization, and how to use an [AutoImageProcesso... |
[
0.0033488926,
-0.014274198,
0.0048707994,
-0.015410833,
0.013165454,
0.026707463,
-0.0007378547,
-0.046497483,
0.008723508,
0.03612132,
0.031574775,
-0.02648432,
0.012893498,
-0.04222987,
0.028785484,
0.06309377,
0.0020344392,
-0.0016622432,
-0.053917,
-0.031100595,
-0.010187... | AutoModel
🤗 Transformers provides a simple and unified way to load pretrained instances. This means you can load an [AutoModel] like you would load an [AutoTokenizer]. The only difference is selecting the correct [AutoModel] for the task. For text (or sequence) classification, you should load [AutoModelForSequenceCla... |
[
0.016809246,
-0.012772435,
0.0009912129,
0.018550614,
-0.0068935174,
0.020867642,
-0.016981943,
-0.0036086629,
-0.009383243,
0.06504948,
0.012297517,
-0.020277591,
-0.005364423,
-0.04110206,
-0.0021533247,
0.02260901,
-0.012995504,
-0.03986439,
-0.082319245,
-0.01948606,
0.00... |
Under the hood, the [AutoModelForSequenceClassification] and [AutoTokenizer] classes work together to power the [pipeline] you used above. An AutoClass is a shortcut that automatically retrieves the architecture of a pretrained model from its name or path. You only need to select the appropriate AutoClass for your ta... |
[
0.02032337,
-0.0109703075,
-0.024770793,
-0.005997281,
-0.0031822652,
-0.013699408,
0.032075346,
0.011132032,
-0.021792367,
0.045848876,
-0.029406892,
-0.017169744,
-0.0155659765,
-0.04277611,
-0.005400922,
0.005323429,
0.010464919,
-0.05525585,
-0.01250669,
0.027681833,
-0.0... | from transformers import AutoTokenizer
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
Pass your text to the tokenizer: |
[
0.033200543,
0.010901034,
0.00009732273,
0.018518968,
-0.011803532,
-0.045650747,
-0.0072413,
0.022725886,
-0.0035193854,
0.04329146,
0.0040150485,
0.015818581,
-0.0014905423,
-0.056736544,
-0.03712321,
0.033655345,
0.0060190195,
-0.038146514,
-0.01398516,
0.00005515695,
0.02... | Now pass your preprocessed batch of inputs directly to the model. You just have to unpack the dictionary by adding **:
pt_outputs = pt_model(**pt_batch)
The model outputs the final activations in the logits attribute. Apply the softmax function to the logits to retrieve the probabilities: |
[
0.011334148,
0.015542336,
-0.019485272,
-0.027571876,
-0.039515387,
-0.023714967,
-0.00782852,
0.0060004313,
-0.017592663,
0.041981515,
-0.006659977,
0.01124812,
-0.01699047,
-0.0011344901,
-0.03409564,
0.016044164,
-0.02613808,
-0.06423401,
-0.026095066,
-0.0069503207,
-0.01... | The tokenizer returns a dictionary containing:
input_ids: numerical representations of your tokens.
attention_mask: indicates which tokens should be attended to.
A tokenizer can also accept a list of inputs, and pad and truncate the text to return a batch with uniform length:
pt_batch = tokenizer(
["We are very... |
[
0.008129315,
-0.015355375,
-0.01431885,
0.015992098,
0.010032079,
0.015932867,
-0.015725562,
-0.03663375,
0.009832178,
0.035330687,
0.02988153,
-0.00564906,
0.040424466,
-0.03814411,
0.03426455,
0.033642635,
-0.011386965,
-0.014148563,
-0.038884487,
-0.02857847,
-0.010091309,... |
from torch import nn
pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1)
print(pt_predictions)
tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725],
[0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=)
``
</pt>
<tf>
🤗 Transformers provides a simple and unified way to load pretrained instances. This... |
[
0.013347349,
0.012813455,
-0.01734471,
0.004014472,
0.001558902,
-0.018029189,
-0.00032555553,
-0.0036106291,
-0.034744177,
0.04219131,
-0.010506759,
0.0018480945,
0.018672599,
-0.06828367,
-0.0239294,
0.009295231,
0.009425282,
-0.054402426,
-0.03351211,
-0.027036937,
-0.0275... | from transformers import TFAutoModelForSequenceClassification
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
See the task summary for tasks supported by an [AutoModel] class.
Now pass your preprocessed batch of inputs directl... |
[
0.029736195,
0.025796901,
-0.017713143,
0.021748183,
-0.026549198,
-0.03200676,
-0.004958312,
0.020558188,
-0.022021746,
0.055204824,
0.0063261227,
0.020270947,
0.004879663,
-0.05326253,
-0.04946002,
0.019737503,
0.0154289,
-0.030009758,
-0.01925877,
-0.0158666,
0.017973026,
... | See the task summary for tasks supported by an [AutoModel] class.
Now pass your preprocessed batch of inputs directly to the model. You can pass the tensors as-is:
tf_outputs = tf_model(tf_batch)
The model outputs the final activations in the logits attribute. Apply the softmax function to the logits to retrieve the... |
[
0.011261038,
-0.00413437,
-0.0076300125,
-0.00912269,
0.006401157,
0.0014874706,
-0.0016028928,
-0.0065781954,
-0.019231241,
0.04721027,
-0.0139339715,
-0.012989766,
0.0025410233,
-0.06870483,
-0.02304972,
0.016523594,
0.005189658,
-0.05254225,
-0.047515746,
-0.008435364,
-0.... | from transformers import AutoModelForSequenceClassification
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
pt_model = AutoModelForSequenceClassification.from_pretrained(model_name)
See the task summary for tasks supported by an [AutoModel] class.
Now pass your preprocessed batch of inputs directly to... |
[
0.0042180847,
-0.02153193,
-0.023257202,
0.036950693,
-0.020635333,
-0.0042961976,
-0.016559888,
-0.013129722,
0.010344836,
0.033228457,
0.02124665,
-0.0043403483,
0.014033113,
-0.030266965,
-0.009047486,
0.02373267,
0.018448178,
-0.037901632,
-0.074336104,
-0.038200498,
-0.0... | When you are ready to use the model again, reload it with [PreTrainedModel.from_pretrained]:
pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained")
``
</pt>
<tf>
Once your model is fine-tuned, you can save it with its tokenizer using [TFPreTrainedModel.save_pretrained`]:
tf_save_directo... |
[
-0.006135498,
-0.013838102,
-0.012508861,
0.032601394,
-0.01771389,
-0.03528786,
-0.0093256775,
-0.010514999,
-0.0028596178,
0.05328159,
0.017000297,
0.0023349174,
0.035735603,
-0.015279279,
0.004278058,
0.011893212,
0.026011154,
-0.012173052,
-0.056163944,
-0.042815562,
-0.0... | tf_save_directory = "./tf_save_pretrained"
tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT
tf_model.save_pretrained(tf_save_directory)
When you are ready to use the model again, reload it with [TFPreTrainedModel.from_pretrained]:
tf_model = TFAutoModelForSequenceClassification.from_pretrained(... |
[
-0.0008548026,
-0.004450285,
-0.021097645,
-0.008629523,
0.0063512702,
-0.032115307,
0.012841727,
-0.022694621,
-0.0031811295,
0.031968795,
0.0056370273,
0.00089051476,
0.021097645,
-0.050985977,
-0.025595546,
0.022328341,
-0.008409756,
-0.021126948,
-0.0652855,
0.011786845,
... | All 🤗 Transformers models (PyTorch or TensorFlow) output the tensors before the final activation
function (like softmax) because the final activation function is often fused with the loss. Model outputs are special dataclasses so their attributes are autocompleted in an IDE. The model outputs behave like a tuple or a ... |
[
0.00086317677,
0.003121091,
-0.042673826,
-0.09153309,
-0.004029045,
0.026401592,
-0.010789044,
0.008739055,
0.0014399579,
0.03447387,
0.052235715,
-0.011434543,
0.040914666,
-0.049228117,
0.033338927,
0.042106356,
-0.0069692545,
-0.03112579,
-0.062535316,
0.0034509336,
-0.02... |
Custom model builds
You can modify the model's configuration class to change how a model is built. The configuration specifies a model's attributes, such as the number of hidden layers or attention heads. You start from scratch when you initialize a model from a custom configuration class. The model attributes are ra... |
[
0.008649437,
-0.018697627,
-0.03145769,
-0.0054843994,
-0.02566285,
-0.01914009,
0.006997337,
0.0062301634,
0.017498694,
0.025748488,
0.0065441695,
0.0051311427,
0.0061730715,
-0.0054879677,
0.03328463,
0.036053594,
0.031029496,
-0.027175788,
-0.08775039,
-0.009327404,
-0.033... | Save a model
Once your model is fine-tuned, you can save it with its tokenizer using [PreTrainedModel.save_pretrained]:
pt_save_directory = "./pt_save_pretrained"
tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT
pt_model.save_pretrained(pt_save_directory)
When you are ready to use the model ag... |
[
-0.0034610713,
-0.017149517,
-0.009705496,
-0.04844775,
-0.037140373,
-0.0021110722,
-0.0035208699,
0.01654066,
0.0023720115,
0.068772025,
0.046447214,
-0.0063314047,
0.047606945,
-0.038358092,
0.028166959,
0.059146263,
-0.0019534214,
0.0000096054355,
-0.06662653,
-0.009582275,... | from transformers import AutoConfig
my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12)
Create a model from your custom configuration with [AutoModel.from_config]:
from transformers import AutoModel
my_model = AutoModel.from_config(my_config)
``
</pt>
<tf>
Create a model from your... |
[
0.015195399,
-0.03002532,
-0.03924658,
0.01997471,
-0.02177398,
-0.010289576,
-0.03227441,
-0.015701443,
0.016840044,
0.03345518,
0.034129906,
-0.013719435,
0.0396964,
-0.0506607,
0.0026057789,
0.053696968,
0.0050253053,
-0.03292102,
-0.04394155,
-0.041551895,
-0.026145644,
... | When you are ready to use the model again, reload it with [TFPreTrainedModel.from_pretrained]:
tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained")
One particularly cool 🤗 Transformers feature is the ability to save a model and reload it as either a PyTorch or TensorFlow model. The... |
[
0.0081413,
-0.0027143469,
-0.008593981,
-0.0026046587,
-0.014792234,
-0.00725683,
-0.0066892374,
-0.015168307,
-0.011498106,
0.05228818,
-0.00645245,
-0.006703166,
0.0321752,
-0.04841601,
0.014123658,
0.04072739,
-0.0012753427,
-0.022536566,
-0.06975471,
-0.043318123,
-0.0090... | from transformers import AutoModel
tokenizer = AutoTokenizer.from_pretrained(tf_save_directory)
pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True)
from transformers import TFAutoModel
tokenizer = AutoTokenizer.from_pretrained(pt_save_directory)
tf_model = TFAutoModelForSeque... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.