vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
0.022114363,
-0.00293718,
-0.035960007,
-0.016686156,
-0.03236103,
-0.014366156,
-0.01861949,
-0.009562566,
0.013436669,
0.041492313,
0.025103593,
-0.00017102566,
0.020002568,
-0.03432411,
0.021132823,
0.025118466,
-0.0075437194,
0.015117181,
-0.079058476,
-0.011503335,
-0.00... |
from_pretrained() lets you instantiate a model, configuration, and preprocessing class from a pretrained version either
provided by the library itself (the supported models can be found on the Model Hub) or
stored locally (or on a server) by the user.
save_pretrained() lets you save a model, configuration, and pr... |
[
0.042673428,
-0.011221759,
-0.052942105,
0.004561722,
0.010898941,
-0.03741611,
-0.009500064,
0.0046039955,
-0.002338507,
0.04310385,
-0.012290131,
-0.013788928,
0.039322272,
-0.014019513,
0.0064179236,
-0.0048153643,
-0.0017706936,
-0.011552262,
-0.018600449,
-0.0067138397,
... |
Transformers version v4.29.0, building on the concept of tools and agents. You can play with in
this colab.
In short, it provides a natural language API on top of transformers: we define a set of curated tools and design an
agent to interpret natural language and to use these tools. It is extensible by design; we cu... |
[
0.018203147,
-0.0011250355,
-0.03820056,
0.014990826,
-0.0025937313,
-0.045667034,
-0.001371024,
0.0053249267,
0.017349422,
0.05715614,
-0.018738532,
0.011952145,
0.02869383,
0.0020511097,
-0.020590682,
-0.042628355,
-0.037708584,
-0.03232578,
-0.037390247,
-0.014289035,
-0.0... | py
agent.run("Read the following text out loud", text=text)
| Input | Output |
|------------------------------------------------------------------------------------------------... |
[
-0.0006350126,
0.033531543,
0.0073683048,
-0.025069505,
-0.063724324,
-0.018895673,
-0.016290862,
-0.008814623,
0.06861734,
0.031085035,
0.004302975,
0.0048606344,
0.042799488,
-0.04222384,
-0.0027199406,
0.011527368,
-0.023054736,
-0.004223823,
-0.065048315,
-0.027559187,
0.... | All these classes can be instantiated from pretrained instances, saved locally, and shared on the Hub with three methods: |
[
0.0075932005,
0.016755477,
-0.06287506,
-0.045419108,
-0.01906706,
-0.017063687,
-0.01661538,
0.043541823,
0.017876243,
0.03771383,
-0.021798931,
-0.025105195,
0.031101301,
-0.019151118,
0.004889349,
0.043990128,
-0.008945127,
-0.026870403,
-0.03524814,
-0.00054024503,
0.0506... | pip install transformers[agents]
To use openAI models, you instantiate an [OpenAiAgent] after installing the openai dependency:
pip install openai
from transformers import OpenAiAgent
agent = OpenAiAgent(model="text-davinci-003", api_key="")
To use BigCode or OpenAssistant, start by logging in to have access to the ... |
[
0.0033523506,
0.013331441,
-0.026691232,
-0.0053332853,
-0.030617664,
-0.020085754,
0.015138733,
0.026648708,
0.03730819,
0.056189083,
0.050008852,
-0.020751972,
0.0018081786,
-0.04915836,
-0.035692256,
0.021262266,
0.0071299467,
-0.02405471,
0.013409402,
0.016612917,
0.02005... | This is using the inference API that Hugging Face provides for free at the moment. If you have your own inference
endpoint for this model (or another one) you can replace the URL above with your URL endpoint. |
[
0.038203876,
0.014357164,
-0.014894598,
0.008222739,
-0.009466515,
0.0004100333,
0.009474193,
0.023923488,
-0.006906026,
0.035808455,
-0.020683529,
0.016092308,
0.0031746991,
-0.018487727,
-0.013681533,
0.0130366115,
-0.009996272,
-0.028176894,
-0.02217299,
0.012568276,
0.004... |
py
agent.run(
"In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?",
document=document,
)
| Input | Output |
|-------------------------------------... |
[
0.018995726,
-0.009838562,
-0.035933353,
-0.023139743,
-0.025837526,
-0.045751054,
-0.0032818385,
0.020330712,
0.0059622386,
0.06997547,
-0.010902379,
-0.0026960443,
0.0016643858,
-0.06658239,
-0.019691031,
0.01934338,
0.0068383226,
-0.036684282,
-0.06541427,
-0.00076135877,
... | StarCoder and OpenAssistant are free to use and perform admirably well on simple tasks. However, the checkpoints
don't hold up when handling more complex prompts. If you're facing such an issue, we recommend trying out the OpenAI
model which, while sadly not open-source, performs better at this given time. |
[
0.014061025,
0.016355852,
-0.0702634,
-0.012517233,
-0.033268023,
-0.04561836,
0.005757927,
0.023017801,
0.027120672,
0.05980456,
-0.012941428,
0.03696756,
0.021279296,
0.017732747,
-0.026939867,
0.010424074,
-0.0039151125,
-0.0029571962,
-0.0067836447,
0.04016641,
0.02719021... | You're now good to go! Let's dive into the two APIs that you now have at your disposal.
Single execution (run)
The single execution method is when using the [~Agent.run] method of the agent:
py
agent.run("Draw me a picture of rivers and lakes.") |
[
0.020516515,
0.005905836,
-0.036909685,
-0.01937114,
-0.0055013755,
-0.022893166,
-0.021089202,
0.036451533,
0.022077087,
0.082581475,
0.008210901,
0.017023124,
0.012162442,
-0.0056266505,
-0.0073948223,
-0.011167399,
-0.027646469,
0.0007972878,
-0.025785236,
0.009256056,
0.0... | It automatically selects the tool (or tools) appropriate for the task you want to perform and runs them appropriately. It
can perform one or several tasks in the same instruction (though the more complex your instruction, the more likely
the agent is to fail).
py
agent.run("Draw me a picture of the sea then transform t... |
[
0.030088207,
0.01756838,
-0.015392279,
-0.008225657,
-0.026737012,
-0.040040236,
0.019933075,
0.029914118,
-0.0063941064,
0.035020698,
-0.01614666,
-0.013303223,
0.018003598,
-0.05327092,
0.0002636254,
0.007224651,
0.0077831834,
-0.04062053,
-0.023356805,
-0.025184728,
0.0362... | from huggingface_hub import login
login("")
Then, instantiate the agent
from transformers import HfAgent
Starcoder
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
StarcoderBase
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoderbase")
OpenAssistant
agent = H... |
[
0.031203948,
0.00008105713,
-0.0388631,
-0.028835284,
-0.004354369,
-0.047146328,
-0.02187113,
0.025261015,
0.018495431,
0.06984011,
-0.017956454,
-0.009446286,
-0.006191147,
-0.0049926317,
-0.0038614885,
-0.03494842,
-0.029984158,
-0.025502136,
-0.03679229,
-0.013446065,
0.0... |
Every [~Agent.run] operation is independent, so you can run it several times in a row with different tasks.
Note that your agent is just a large-language model, so small variations in your prompt might yield completely
different results. It's important to explain as clearly as possible the task you want to perform. W... |
[
0.03292969,
-0.0033989379,
-0.06153411,
0.0066248653,
-0.00063572393,
-0.069088906,
0.010337384,
0.03327571,
-0.008888421,
0.05660331,
-0.009739056,
0.0347463,
0.03757214,
0.009443496,
-0.019333936,
-0.033881247,
-0.021799339,
-0.003957618,
-0.006549173,
0.013026258,
0.019766... | Chat-based execution (chat)
The agent also has a chat-based approach, using the [~Agent.chat] method:
py
agent.chat("Generate a picture of rivers and lakes")
py
agent.chat("Transform the picture so that there is a rock in there") |
[
0.032846477,
-0.04887116,
-0.02693686,
-0.029795466,
-0.0025218949,
-0.057392005,
0.017234093,
0.02435312,
-0.004590261,
0.081690155,
-0.0028826564,
-0.014581637,
0.010575467,
0.010094451,
0.00292904,
-0.0014018163,
0.0043841116,
-0.025878627,
-0.035760056,
-0.009159907,
0.00... |
This can be helpful when the model is unable to understand your request and mixes tools. An example would be:
py
agent.run("Draw me the picture of a capybara swimming in the sea")
Here, the model could interpret in two ways:
- Have the text-to-image generate a capybara swimming in the sea
- Or, have the text-to-image... |
[
-0.0018086934,
-0.010758483,
-0.046608083,
-0.020248719,
-0.053064615,
-0.046031605,
-0.016559273,
0.016977217,
0.0026662014,
0.06364295,
0.0144839585,
0.0082796365,
0.0022464548,
-0.025854947,
-0.0033543697,
-0.014815433,
-0.0120555535,
-0.012221291,
-0.050124586,
0.012754531,... |
This is an interesting approach when you want to keep the state across instructions. It's better for experimentation,
but will tend to be much better at single instructions rather than complex instructions (which the [~Agent.run]
method is better at handling).
This method can also take arguments if you would like to... |
[
0.06730824,
-0.029546505,
-0.010786032,
0.00628536,
0.03419236,
-0.025283083,
0.0058816806,
-0.0044723437,
0.008165657,
0.0736538,
-0.027818473,
0.036232002,
0.027322726,
-0.020226466,
-0.013675528,
-0.0075495145,
-0.013101878,
-0.04104783,
-0.0208072,
0.00032776827,
-0.03376... |
Document question answering: given a document (such as a PDF) in image format, answer a question on this document (Donut)
Text question answering: given a long text and a question, answer the question in the text (Flan-T5)
Unconditional image captioning: Caption the image! (BLIP)
Image question answering: given an im... |
[
0.030079408,
0.01756385,
-0.05249623,
0.0047553107,
-0.031672023,
-0.025181362,
-0.005506545,
0.017068036,
0.0049055577,
0.05799526,
-0.011291046,
-0.0017090573,
0.007880444,
-0.012410385,
-0.010164195,
0.006930133,
0.009292764,
-0.020704007,
-0.025857473,
0.008158401,
0.0223... |
Agents
The "agent" here is a large language model, and we're prompting it so that it has access to a specific set of tools.
LLMs are pretty good at generating small samples of code, so this API takes advantage of that by prompting the
LLM gives a small sample of code performing a task with a set of tools. This promp... |
[
0.005119882,
0.0027372374,
-0.021507123,
-0.014075153,
-0.020653207,
-0.00085255987,
-0.035459254,
-0.03841178,
0.02940947,
0.0461115,
0.042290587,
-0.011867995,
0.03919333,
-0.05644534,
0.021796586,
0.03557504,
-0.015211296,
-0.02318601,
-0.04289846,
-0.0015558652,
-0.031580... | These tools have an integration in transformers, and can be used manually as well, for example:
from transformers import load_tool
tool = load_tool("text-to-speech")
audio = tool("This is a text to speech tool") |
[
0.024938991,
0.019754384,
-0.04978986,
-0.031166397,
0.0014852507,
-0.009781725,
0.014834147,
0.019857194,
-0.0053535113,
0.021737166,
0.008423151,
-0.0059630335,
0.0105014015,
-0.02828769,
0.03060828,
0.012381372,
0.024659934,
-0.017815664,
-0.04015501,
0.0142760305,
-0.0355... | Custom tools
While we identify a curated set of tools, we strongly believe that the main value provided by this implementation is
the ability to quickly create and share custom tools.
By pushing the code of a tool to a Hugging Face Space or a model repository, you're then able to leverage the tool
directly with the a... |
[
0.0049749725,
-0.012146135,
0.007774939,
0.005268033,
-0.011079818,
-0.027568903,
-0.0378225,
0.010465449,
0.043584853,
0.05109851,
-0.042229004,
-0.020803787,
0.056182936,
0.011517643,
0.019518556,
-0.010366585,
0.00972397,
-0.060674183,
-0.01820508,
0.0027205225,
-0.0110586... | Text downloader: to download a text from a web URL
Text to image: generate an image according to a prompt, leveraging stable diffusion
Image transformation: modify an image given an initial image and a prompt, leveraging instruct pix2pix stable diffusion
Text to video: generate a small video according to a prompt, leve... |
[
-0.02659006,
-0.02158313,
-0.04381805,
-0.041092385,
-0.0061808927,
-0.028619496,
-0.034307845,
0.009569459,
0.04583267,
0.008680655,
0.07092658,
0.022783015,
0.049950797,
-0.038011193,
0.0029237957,
0.015568888,
-0.044706855,
-0.033833817,
-0.074896574,
0.034041204,
0.006577... | that you can then modify and execute yourself. |
[
0.031855755,
0.0015134957,
-0.04361646,
-0.000668967,
0.0026700804,
-0.042783022,
0.008249468,
0.020681547,
0.006655908,
0.030482132,
-0.053247266,
-0.012053947,
0.02000245,
-0.018474486,
0.028398542,
-0.046240237,
0.021376077,
-0.0007147867,
-0.032936137,
0.015163896,
-0.022... |
The text-to-image tool we have been using since the beginning is a remote tool that lives in
huggingface-tools/text-to-image! We will
continue releasing such tools on this and other organizations, to further supercharge this implementation.
The agents have by default access to tools that reside on huggingface-tools.... |
[
0.011732456,
0.017487476,
-0.03947888,
-0.015874958,
-0.009424888,
0.01905829,
-0.0034891546,
0.035975825,
-0.019280707,
0.055826474,
0.010085186,
0.03069344,
0.0026464057,
-0.067058496,
-0.03714351,
0.0052962867,
-0.0024413657,
-0.051628366,
-0.046206973,
-0.026773354,
0.021... | def _forward(self, model_inputs):
# model_inputs == {"model_input": model_input}
outputs = self.model(**model_inputs)
# Maybe {"logits": Tensor()}
return outputs
def postprocess(self, model_outputs):
best_class = model_outputs["logits"].softmax(-1)
return best_class |
[
-0.015391855,
0.005832666,
0.011929034,
-0.005548146,
-0.040471278,
-0.056515448,
-0.0065647857,
0.0064537534,
0.017154492,
0.07133826,
0.0011701765,
-0.026023198,
0.05604356,
-0.030978015,
-0.023733158,
0.018903252,
-0.029701144,
-0.03208834,
-0.051518995,
-0.00505544,
-0.00... | pipe = pipeline("my-new-task")
pipe("This is a test")
[{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}, {"label": "3-star", "score": 0.05}
{"label": "4-star", "score": 0.025}, {"label": "5-star", "score": 0.025}]
pipe("This is a test", top_k=2)
[{"label": "1-star", "score": 0.8}, {"label": "2-star"... |
[
0.03919015,
-0.008101529,
0.009076917,
0.041897938,
-0.0188235,
-0.03735584,
-0.013619014,
0.047459096,
-0.02167687,
0.024646703,
0.014667191,
0.0021855205,
-0.0013084008,
0.0072062127,
-0.056980032,
0.009914001,
-0.042363796,
-0.05080744,
-0.026990542,
0.017047424,
0.0135243... |
In order to achieve that, we'll update our postprocess method with a default parameter to 5. and edit
_sanitize_parameters to allow this new parameter.
thon
def postprocess(self, model_outputs, top_k=5):
best_class = model_outputs["logits"].softmax(-1)
# Add logic to handle top_k
return best_class
def _sa... |
[
0.011649226,
0.0048862235,
0.00042566593,
-0.024442114,
-0.007888337,
0.0021480322,
-0.027917087,
0.02936866,
-0.016465804,
0.048180435,
-0.0062351585,
-0.008386856,
0.030351035,
-0.028708855,
-0.044162955,
0.034925684,
-0.030204412,
-0.035306904,
-0.06891298,
-0.01593796,
0.... |
How to create a custom pipeline?
In this guide, we will see how to create a custom pipeline and share it on the Hub or add it to the
🤗 Transformers library.
First and foremost, you need to decide the raw entries the pipeline will be able to take. It can be strings, raw bytes,
dictionaries or whatever seems to be the ... |
[
-0.0069261724,
0.02357515,
-0.0025438394,
0.004963934,
-0.032046366,
-0.0151322195,
-0.018597074,
0.000911292,
0.02582377,
0.08061089,
0.0017050264,
-0.020223433,
0.026785443,
-0.03351716,
-0.03295147,
0.0119855665,
-0.013618998,
-0.028694648,
-0.06279164,
-0.014410964,
0.005... |
Try to keep the inputs/outputs very simple and ideally JSON-serializable as it makes the pipeline usage very easy
without requiring users to understand new kinds of objects. It's also relatively common to support many different types
of arguments for ease of use (audio files, which can be filenames, URLs or pure byte... |
[
0.022048198,
-0.005109819,
-0.01124756,
0.009035292,
-0.012811791,
-0.0078286,
-0.019307071,
0.021184148,
-0.03584322,
0.05074065,
0.0032271561,
-0.0054003187,
0.006346306,
-0.027113326,
-0.046301216,
0.022942044,
-0.029005298,
-0.04883378,
-0.05121737,
-0.010867676,
0.040878... |
The structure of this breakdown is to support relatively seamless support for CPU/GPU, while supporting doing
pre/postprocessing on the CPU on different threads
preprocess will take the originally defined inputs, and turn them into something feedable to the model. It might
contain more information and is usually a Di... |
[
0.023647143,
0.029311556,
-0.005047783,
0.029168155,
-0.026257075,
-0.04353713,
-0.023159571,
-0.0023661482,
-0.01710797,
0.03719872,
0.00028501332,
-0.030258017,
0.024120372,
-0.023976969,
-0.03771497,
0.009909142,
-0.04551609,
-0.050248384,
-0.035793368,
-0.0062093465,
0.03... |
import numpy as np
from transformers import Pipeline
def softmax(outputs):
maxes = np.max(outputs, axis=-1, keepdims=True)
shifted_exp = np.exp(outputs - maxes)
return shifted_exp / shifted_exp.sum(axis=-1, keepdims=True)
class PairClassificationPipeline(Pipeline):
def _sanitize_parameters(self, **kwa... |
[
0.0015626573,
-0.0098050535,
-0.016692901,
-0.0042763585,
-0.004073775,
0.020494107,
-0.007889716,
-0.016000433,
0.018018903,
0.054572377,
-0.0052671772,
-0.040929284,
0.022630446,
-0.022011645,
-0.018284103,
0.038866613,
-0.033798333,
-0.030586462,
-0.10507835,
-0.011558323,
... |
You can specify a default model if you want, in which case it should come with a specific revision (which can be the name of a branch or a commit hash, here we took "abcdef") as well as the type:
python
PIPELINE_REGISTRY.register_pipeline(
"new-task",
pipeline_class=MyPipeline,
pt_model=AutoModelForSequen... |
[
0.0067150313,
-0.008700375,
-0.026733626,
-0.022938631,
-0.009691291,
0.00024904648,
0.01709153,
0.024400407,
-0.007667294,
0.04663626,
0.013247341,
0.024287963,
-0.0041885492,
-0.045343153,
-0.0299664,
0.01803325,
-0.012783508,
-0.050178256,
-0.04795748,
-0.044893377,
0.0148... | def _forward(self, model_inputs):
return self.model(**model_inputs)
def postprocess(self, model_outputs):
logits = model_outputs.logits[0].numpy()
probabilities = softmax(logits)
best_class = np.argmax(probabilities)
label = self.model.config.id2label[best_class]
score = probabilities[best_cla... |
[
0.032915477,
0.009600347,
-0.050908133,
0.0058803014,
-0.017765261,
-0.0062817964,
-0.026562618,
-0.014126936,
0.020437157,
0.058326904,
0.018930662,
-0.029220302,
0.038515087,
-0.037406534,
-0.019655485,
0.038202416,
-0.015747128,
-0.022284744,
-0.016230343,
-0.037548658,
0.... | The implementation is framework agnostic, and will work for PyTorch and TensorFlow models. If we have saved this in
a file named pair_classification.py, we can then import it and register it like this: |
[
0.04077805,
-0.014189459,
-0.027180433,
0.024176827,
-0.02530133,
-0.03592493,
-0.019782385,
-0.007165011,
0.023259468,
0.024857447,
0.048028138,
-0.030864663,
0.01741501,
-0.035510637,
-0.02112883,
-0.012369539,
-0.0063660215,
-0.038262714,
-0.031071808,
-0.016246118,
0.0119... | Once this is done, we can use it with a pretrained model. For instance sgugger/finetuned-bert-mrpc has been
fine-tuned on the MRPC dataset, which classifies pairs of sentences as paraphrases or not.
from transformers import pipeline
classifier = pipeline("pair-classification", model="sgugger/finetuned-bert-mrpc")
The... |
[
0.0024630711,
0.030421566,
-0.017566616,
-0.007040264,
-0.024634115,
0.002381366,
-0.02968622,
-0.013665194,
0.011002966,
0.0671072,
-0.009756962,
-0.02930493,
0.025151582,
-0.024566028,
-0.013066022,
0.037420977,
-0.011840444,
-0.031973965,
-0.055450592,
-0.032818254,
0.0106... | from pair_classification import PairClassificationPipeline
from transformers.pipelines import PIPELINE_REGISTRY
from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification
PIPELINE_REGISTRY.register_pipeline(
"pair-classification",
pipeline_class=PairClassificationPipeline,... |
[
0.018009529,
0.024656577,
-0.013975848,
0.04118898,
-0.03681443,
-0.025196295,
0.010893776,
-0.0029240628,
0.032723937,
0.009246216,
-0.00655473,
-0.038689237,
0.032439876,
-0.027298354,
-0.023207862,
-0.023491923,
-0.008060258,
-0.014586581,
-0.05672717,
-0.02191538,
-0.0108... | Then we can share it on the Hub by using the save_pretrained method in a Repository:
from huggingface_hub import Repository
repo = Repository("test-dynamic-pipeline", clone_from="{your_username}/test-dynamic-pipeline")
classifier.save_pretrained("test-dynamic-pipeline")
repo.push_to_hub() |
[
0.01672058,
0.024327492,
-0.011212882,
0.01604766,
-0.041574705,
-0.015535656,
0.0038765997,
-0.008060402,
0.021562671,
0.045670733,
0.020407006,
-0.04783578,
0.036600955,
-0.024327492,
-0.012134489,
0.014416562,
-0.018300477,
-0.034114078,
-0.035928033,
-0.034933284,
0.01993... | This will copy the file where you defined PairClassificationPipeline inside the folder "test-dynamic-pipeline",
along with saving the model and tokenizer of the pipeline, before pushing everything into the repository
{your_username}/test-dynamic-pipeline. After that, anyone can use it as long as they provide the option... |
[
0.036740538,
0.03362741,
-0.025942747,
-0.01932384,
0.0072078765,
0.011393773,
-0.0034900005,
0.016294848,
0.005535621,
0.0688815,
-0.035057765,
-0.032814067,
0.041957133,
-0.030093586,
-0.0006731091,
0.030121632,
-0.00925525,
-0.023951465,
-0.07488339,
-0.036039382,
-0.00355... |
Add the pipeline to 🤗 Transformers
If you want to contribute your pipeline to 🤗 Transformers, you will need to add a new module in the pipelines submodule
with the code of your pipeline, then add it to the list of tasks defined in pipelines/__init__.py.
Then you will need to add tests. Create a new file tests/test_... |
[
0.019927949,
0.0014469224,
0.008057027,
0.012637923,
-0.03000451,
-0.004651262,
0.0119342515,
0.00926734,
-0.016761431,
0.058826853,
0.0005875649,
0.0032667904,
0.010752086,
-0.06338664,
-0.019702774,
0.027217975,
-0.010301736,
-0.027246121,
-0.076221585,
-0.0170429,
-0.01209... |
test_small_model_pt : Define 1 small model for this pipeline (doesn't matter if the results don't make sense)
and test the pipeline outputs. The results should be the same as test_small_model_tf.
test_small_model_tf : Define 1 small model for this pipeline (doesn't matter if the results don't make sense)
and test... |
[
0.038105782,
0.0010890426,
-0.040905155,
-0.014982371,
0.0037241688,
-0.011618835,
-0.0058629774,
-0.016710555,
-0.0125257755,
0.06529971,
0.02069538,
-0.0074911853,
0.0021066726,
-0.041362196,
-0.033592504,
0.02679402,
-0.024680207,
-0.042133454,
-0.035991967,
0.0027101093,
... |
Multilingual models for inference
[[open-in-colab]]
There are several multilingual models in 🤗 Transformers, and their inference usage differs from monolingual models. Not all multilingual model usage is different though. Some models, like google-bert/bert-base-multilingual-uncased, can be used just like a monolingua... |
[
0.005505575,
-0.0063037532,
-0.016379364,
-0.018176192,
-0.008286206,
-0.0001556912,
-0.0075956886,
-0.023433033,
-0.031006448,
0.03860956,
0.011100248,
0.024828918,
0.009771189,
-0.037570074,
-0.020477917,
0.012132312,
-0.017003058,
-0.026699996,
-0.027115792,
-0.009333119,
... | print(tokenizer.lang2id)
{'en': 0, 'fr': 1}
Next, create an example input:
input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) # batch size of 1
Set the language id as "en" and use it to define the language embedding. The language embedding is a tensor filled with 0 since that is the language id f... |
[
-0.00555447,
0.015308077,
-0.04815012,
0.010296423,
0.005691027,
-0.02608245,
0.022067666,
-0.022368092,
-0.019473076,
0.02724319,
0.009914063,
0.07805617,
0.0155402245,
-0.03034304,
-0.051673297,
0.031080449,
-0.03684317,
-0.04689379,
0.001742813,
0.003632425,
0.01667365,
... |
FacebookAI/xlm-mlm-ende-1024 (Masked language modeling, English-German)
FacebookAI/xlm-mlm-enfr-1024 (Masked language modeling, English-French)
FacebookAI/xlm-mlm-enro-1024 (Masked language modeling, English-Romanian)
FacebookAI/xlm-mlm-xnli15-1024 (Masked language modeling, XNLI languages)
FacebookAI/xlm-mlm-tlm-xnl... |
[
0.010720911,
-0.012541443,
-0.0346768,
-0.023580225,
0.0017735739,
-0.0023840303,
-0.0075855507,
-0.0060973377,
-0.020213686,
0.05224638,
0.015084409,
0.03710418,
0.005476045,
-0.043346003,
-0.014954371,
0.029273,
-0.0027289921,
-0.04097642,
-0.030746765,
-0.013776804,
-0.026... | language_id = tokenizer.lang2id["en"] # 0
langs = torch.tensor([language_id] * input_ids.shape[1]) # torch.tensor([0, 0, 0, , 0])
We reshape it to be of size (batch_size, sequence_length)
langs = langs.view(1, -1) # is now of shape [1, sequence_length] (we have a batch size of 1)
Now you can pass the input_ids and ... |
[
-0.01532369,
0.023058228,
-0.026663803,
-0.018827494,
0.01594885,
-0.029164443,
0.0017155554,
-0.031345233,
-0.033089865,
0.045738455,
-0.017359095,
0.03137431,
0.008410583,
-0.041115176,
-0.015294613,
0.0075091897,
-0.023101844,
-0.027056346,
-0.030414764,
-0.021880602,
-0.0... | Language embeddings are represented as a tensor of the same shape as the input_ids passed to the model. The values in these tensors depend on the language used and are identified by the tokenizer's lang2id and id2lang attributes.
In this example, load the FacebookAI/xlm-clm-enfr-1024 checkpoint (Causal language modelin... |
[
0.00544387,
0.017937878,
-0.03389881,
-0.016019078,
-0.015248651,
-0.03782363,
0.017719833,
-0.0028618474,
-0.006468684,
0.020874225,
-0.01705116,
0.017298277,
0.004033842,
-0.027706318,
-0.042882286,
0.005367554,
-0.01668775,
-0.031165974,
-0.0019496904,
-0.0050259493,
-0.00... | Now you can pass the input_ids and language embedding to the model:
outputs = model(input_ids, langs=langs)
The run_generation.py script can generate text with language embeddings using the xlm-clm checkpoints.
XLM without language embeddings
The following XLM models do not require language embeddings during inferenc... |
[
0.03772818,
0.024292503,
-0.041717563,
0.0143689085,
0.004958235,
-0.01632798,
0.015216652,
-0.015401874,
0.0014336851,
0.04966784,
0.015316388,
0.013521164,
-0.009724125,
-0.032684457,
-0.039124463,
0.044709604,
-0.034251716,
-0.055851385,
0.02068781,
-0.009282443,
0.0165132... | FacebookAI/xlm-mlm-17-1280 (Masked language modeling, 17 languages)
FacebookAI/xlm-mlm-100-1280 (Masked language modeling, 100 languages)
These models are used for generic sentence representations, unlike the previous XLM checkpoints.
BERT
The following BERT models can be used for multilingual tasks: |
[
0.006092128,
-0.00042656867,
-0.018475281,
0.0021823372,
-0.00022375863,
-0.005819566,
0.0045414716,
-0.031086795,
0.004353625,
0.034858465,
-0.00066068646,
0.011786463,
0.022747872,
-0.045584146,
-0.02679947,
0.042136606,
-0.027963383,
-0.028449574,
-0.0424902,
-0.0016510256,
... | import torch
from transformers import XLMTokenizer, XLMWithLMHeadModel
tokenizer = XLMTokenizer.from_pretrained("FacebookAI/xlm-clm-enfr-1024")
model = XLMWithLMHeadModel.from_pretrained("FacebookAI/xlm-clm-enfr-1024")
The lang2id attribute of the tokenizer displays this model's languages and their ids:
print(tokeniz... |
[
0.045257613,
-0.0009847152,
-0.0503409,
-0.0005278005,
0.0060808086,
-0.0015014132,
0.009059722,
-0.009305687,
-0.00047655776,
0.064934835,
0.01883001,
-0.016534334,
-0.009223699,
-0.05542418,
-0.016739305,
0.011423722,
-0.021480968,
-0.064005636,
-0.0017473785,
-0.0004970549,
... | These models are used for generic sentence representations, unlike the previous XLM checkpoints.
BERT
The following BERT models can be used for multilingual tasks:
google-bert/bert-base-multilingual-uncased (Masked language modeling + Next sentence prediction, 102 languages)
google-bert/bert-base-multilingual-cased (M... |
[
0.010508029,
0.023784725,
-0.04817196,
-0.016855882,
0.008090824,
-0.009044794,
-0.004486534,
-0.0030017816,
-0.0015726179,
0.035060234,
0.015062704,
0.011935399,
-0.0128032975,
-0.044757746,
-0.02606565,
0.01582301,
-0.03818754,
-0.03451511,
-0.0010732176,
-0.03121566,
0.015... | These models do not require language embeddings during inference. They should identify the language from the
context and infer accordingly.
XLM-RoBERTa
The following XLM-RoBERTa models can be used for multilingual tasks:
FacebookAI/xlm-roberta-base (Masked language modeling, 100 languages)
FacebookAI/xlm-roberta-large... |
[
0.018465877,
0.024305392,
-0.03164367,
0.020190867,
0.011975955,
-0.007044887,
0.012958634,
-0.017956862,
0.0098338565,
0.01954046,
0.017730635,
0.026341446,
-0.0071721408,
-0.05576525,
-0.033821113,
0.015595606,
-0.02570518,
-0.033029318,
-0.012909146,
-0.0333121,
0.01467655... | FacebookAI/xlm-roberta-base (Masked language modeling, 100 languages)
FacebookAI/xlm-roberta-large (Masked language modeling, 100 languages)
XLM-RoBERTa was trained on 2.5TB of newly created and cleaned CommonCrawl data in 100 languages. It provides strong gains over previously released multilingual models like mBERT ... |
[
-0.0033420376,
-0.0138051985,
0.009802385,
0.0066632633,
0.01191132,
-0.010926225,
0.06032664,
-0.041984454,
0.018272812,
0.019452149,
0.014887416,
0.008768729,
0.036490124,
-0.008088876,
-0.024571866,
-0.0039403783,
-0.023364779,
-0.036878612,
-0.013590143,
-0.0130837215,
0.... | facebook/m2m100_418M (Translation)
facebook/m2m100_1.2B (Translation)
In this example, load the facebook/m2m100_418M checkpoint to translate from Chinese to English. You can set the source language in the tokenizer: |
[
0.008439217,
0.013165179,
-0.01036189,
0.0039444165,
-0.0044837925,
-0.02740177,
0.055067725,
-0.0032105716,
0.0039407476,
0.023835285,
0.02404076,
-0.002948222,
0.053893574,
-0.032729484,
-0.012218518,
-0.018199354,
-0.013708224,
-0.038394768,
-0.037514154,
-0.0076319873,
0.... | from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger."
chinese_text = "不要插手巫師的事務, 因為他們是微妙的, 很快就會發怒."
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="zh")
model = M2M100ForConditio... |
[
0.004305009,
-0.023386868,
-0.015880698,
0.016057314,
-0.009853686,
-0.019148089,
0.049334664,
-0.01878014,
-0.01652829,
0.02459374,
-0.016660752,
0.02510887,
0.0047539067,
-0.016204493,
-0.027713953,
-0.02424051,
0.006630449,
-0.033821914,
-0.030819446,
0.019398294,
-0.00332... | Tokenize the text:
encoded_zh = tokenizer(chinese_text, return_tensors="pt")
M2M100 forces the target language id as the first generated token to translate to the target language. Set the forced_bos_token_id to en in the generate method to translate to English: |
[
-0.0072911354,
-0.02526365,
-0.026630498,
-0.0057323147,
-0.007287296,
-0.027644116,
0.035814486,
-0.025217576,
-0.035814486,
0.027459823,
-0.002163536,
0.035200175,
0.0288881,
-0.02833522,
-0.035630193,
-0.012117337,
0.00061815314,
-0.018874789,
-0.0379953,
0.007287296,
-0.0... | M2M100 forces the target language id as the first generated token to translate to the target language. Set the forced_bos_token_id to en in the generate method to translate to English:
generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en"))
tokenizer.batch_decode(generated_toke... |
[
0.03670962,
-0.011420771,
-0.008572376,
0.006468371,
0.024038002,
0.015825925,
0.040897235,
-0.0069238422,
0.009918395,
0.029911542,
0.01552681,
0.006740294,
0.004449342,
-0.03777012,
-0.035050888,
0.028307196,
-0.03235885,
-0.07048247,
-0.0124540785,
-0.023929233,
-0.0003307... | MBart
The following MBart models can be used for multilingual translation:
facebook/mbart-large-50-one-to-many-mmt (One-to-many multilingual machine translation, 50 languages)
facebook/mbart-large-50-many-to-many-mmt (Many-to-many multilingual machine translation, 50 languages)
facebook/mbart-large-50-many-to-one-mmt ... |
[
0.0001644597,
-0.049323197,
-0.028529245,
0.022035595,
0.016669413,
-0.03459474,
0.042586926,
-0.019009981,
-0.035079982,
0.01752572,
-0.031483497,
0.016683685,
0.0037035213,
-0.034223676,
-0.028272353,
-0.033966783,
-0.0028418638,
-0.036621332,
-0.023990825,
0.00782806,
-0.0... | Tokenize the text:
encoded_en = tokenizer(en_text, return_tensors="pt")
MBart forces the target language id as the first generated token to translate to the target language. Set the forced_bos_token_id to en in the generate method to translate to English: |
[
-0.0113155255,
-0.046138924,
-0.013945988,
0.0017328552,
0.016281659,
-0.044989984,
0.037068363,
-0.009834001,
-0.04961597,
0.022631051,
-0.019063298,
0.021844937,
0.02238917,
-0.038066123,
-0.044264343,
-0.0237044,
-0.0002072765,
-0.017566655,
-0.029932853,
0.011776612,
-0.0... | MBart forces the target language id as the first generated token to translate to the target language. Set the forced_bos_token_id to en in the generate method to translate to English:
generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
tokenizer.batch_decode(generate... |
[
-0.018421285,
0.016479932,
0.0043536643,
0.037360262,
-0.017213332,
-0.032384645,
0.041444294,
-0.0091746915,
0.010289172,
0.014660811,
0.009117169,
-0.017457798,
0.022375893,
-0.0547893,
-0.074260354,
-0.0006727328,
-0.023497563,
-0.0126691265,
0.003440509,
0.000012070262,
-... | If you are using the facebook/mbart-large-50-many-to-one-mmt checkpoint, you don't need to force the target language id as the first generated token otherwise the usage is the same. |
[
0.012204834,
-0.024201866,
-0.037127078,
0.046353433,
0.010597841,
-0.034965947,
0.05048174,
-0.008914654,
0.006393338,
0.00069699856,
0.0016095904,
0.021625137,
0.034079332,
-0.040257942,
-0.04687986,
0.008693,
-0.03770892,
-0.06173069,
-0.0073977085,
-0.0047898083,
0.016000... | In this example, load the facebook/mbart-large-50-many-to-many-mmt checkpoint to translate Finnish to English. You can set the source language in the tokenizer: |
[
-0.018153535,
0.00966701,
-0.009213531,
0.010026913,
-0.050415292,
-0.031930644,
0.015360684,
0.0072952453,
0.036479823,
0.082979366,
-0.02779895,
0.018297497,
0.05332331,
-0.049234807,
0.0077451244,
0.060694136,
-0.0026470907,
-0.014453727,
-0.077854335,
-0.012798171,
0.0253... | Debugging
Training on multiple GPUs can be a tricky endeavor whether you're running into installation issues or communication problems between your GPUs. This debugging guide covers some issues you may run into and how to resolve them.
DeepSpeed CUDA installation
If you're using DeepSpeed, you've probably already insta... |
[
0.012983177,
-0.011206532,
0.0058190525,
0.02874281,
0.0070562307,
-0.025520394,
0.028282465,
-0.007782713,
0.012709847,
0.030210162,
0.0032943462,
-0.013565801,
0.021161499,
-0.042064056,
-0.025160749,
0.004614243,
-0.029893674,
-0.06277959,
-0.014817365,
-0.015479112,
-0.00... | from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger."
fi_text = "Älä sekaannu velhojen asioihin, sillä ne ovat hienovaraisia ja nopeasti vihaisia."
tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-many... |
[
-0.023930524,
-0.008933212,
-0.012340726,
-0.012680769,
-0.032389093,
0.046132494,
-0.01727135,
0.02176275,
-0.007820988,
0.024865642,
-0.04992964,
-0.0052317027,
0.034656044,
-0.060640994,
-0.0033118767,
0.03049052,
-0.01321917,
-0.01390634,
-0.06897205,
0.032304082,
0.06086... | pip install deepspeed
DeepSpeed compiles CUDA C++ code and it can be a potential source of errors when building PyTorch extensions that require CUDA. These errors depend on how CUDA is installed on your system, and this section focuses on PyTorch built with CUDA 10.2.
For any other installation issues, please open an ... |
[
-0.020188674,
0.01843371,
-0.023302745,
0.017602412,
-0.035890978,
-0.032275487,
0.01678431,
-0.012238559,
0.022616593,
0.07420987,
-0.0057366183,
0.01888235,
0.03620766,
-0.060539633,
0.0018011464,
0.0272877,
0.0023273055,
-0.018539272,
-0.07019853,
0.0019858794,
0.008820999... | which nvcc
Multiple CUDA toolkits
You may also have more than one CUDA toolkit installed system-wide. |
[
-0.00093685,
0.007110637,
-0.034850094,
-0.020773787,
-0.038909175,
0.026224552,
-0.020643318,
-0.000052239127,
-0.0049759964,
0.06616299,
-0.02918188,
-0.00789346,
0.044098996,
-0.054942537,
0.009604071,
0.049230833,
-0.012119251,
0.004381631,
-0.058624703,
0.0027126973,
0.0... |
Non-identical CUDA toolkits
PyTorch comes with its own CUDA toolkit, but to use DeepSpeed with PyTorch, you need to have an identical version of CUDA installed system-wide. For example, if you installed PyTorch with cudatoolkit==10.2 in your Python environment, then you'll also need to have CUDA 10.2 installed system... |
[
-0.001730789,
0.016835777,
-0.0074771475,
0.023051634,
-0.05622146,
-0.03386667,
0.030828426,
-0.020849604,
-0.010689603,
0.062270075,
-0.010515392,
0.012034514,
0.013720879,
-0.012362031,
0.004222881,
0.036681924,
-0.0058082035,
0.012543211,
-0.040249772,
0.038075615,
0.0548... | /usr/local/cuda-10.2
/usr/local/cuda-11.0
Typically, package installers set the paths to whatever the last version was installed. If the package build fails because it can't find the right CUDA version (despite it being installed system-wide already), then you need to configure the PATH and LD_LIBRARY_PATH environment ... |
[
0.019277185,
0.035832535,
0.0047370433,
0.01731806,
-0.04977073,
-0.027068818,
-0.0039780694,
-0.005982059,
0.0009767581,
0.06921242,
0.010311575,
0.03332007,
0.040019974,
-0.031974107,
-0.006262468,
0.021954156,
-0.0020357694,
-0.0026377141,
-0.03858428,
0.025438707,
0.02872... |
echo $PATH
echo $LD_LIBRARY_PATH
PATH lists the locations of the executables and LD_LIBRARY_PATH lists where to look for shared libraries. Earlier entries are prioritized over later ones, and : is used to separate multiple entries. To tell the build program where to find the specific CUDA toolkit you want, insert the... |
[
-0.023815265,
-0.0011857029,
0.018315138,
-0.0021846946,
-0.038696315,
0.0013968435,
0.009220388,
0.003126975,
-0.0140783675,
0.0527677,
-0.002736103,
-0.018357018,
0.030655522,
-0.032330688,
0.013359442,
0.054275353,
-0.011342264,
0.0059328764,
-0.058072392,
0.004463617,
0.0... |
In addition, you should also check the directories you assign actually exist. The lib64 sub-directory contains various CUDA .so objects (like libcudart.so) and while it is unlikely your system names them differently, you should check the actual names and change them accordingly.
Older CUDA versions
Sometimes, older C... |
[
-0.0038321093,
-0.009574926,
-0.028632095,
-0.018165981,
-0.0010533845,
0.0078852335,
-0.032510545,
0.0034578103,
0.032710172,
0.028346915,
-0.022957008,
-0.013681522,
0.048737302,
-0.057093084,
0.014216235,
0.056180507,
-0.005710734,
-0.034963097,
-0.067017354,
-0.00018792931,... | Prebuild
If you're still having issues with installing DeepSpeed or if you're building DeepSpeed at run time, you can try to prebuild the DeepSpeed modules before installing them. To make a local build for DeepSpeed:
git clone https://github.com/microsoft/DeepSpeed/
cd DeepSpeed
rm -rf build
TORCH_CUDA_ARCH_LIST="8.6"... |
[
0.020976273,
-0.007614963,
-0.010172017,
-0.006231063,
-0.018812612,
0.014836534,
-0.010495162,
-0.008036456,
0.018082025,
0.058671746,
0.007818685,
0.0005584775,
0.040912867,
-0.045436885,
-0.005367004,
0.05130968,
-0.02281679,
0.0042324867,
-0.05066339,
0.005254606,
-0.0106... | python -c "import torch; print(torch.cuda.get_arch_list())"
Find the architecture for a GPU with the following command:
CUDA_VISIBLE_DEVICES=0 python -c "import torch; print(torch.cuda.get_device_capability())"
To find the architecture for GPU 0: |
[
0.012301582,
0.014105452,
-0.03336482,
0.0032432368,
-0.04231636,
0.019842574,
0.020113833,
-0.011765846,
0.043645527,
0.05086101,
-0.005665916,
0.012186297,
0.042045098,
-0.028997555,
-0.008910849,
0.04690063,
-0.018025141,
-0.022419533,
-0.07600669,
0.0018428639,
0.01290513... | To use NVMe offload, add the DS_BUILD_AIO=1 parameter to the build command and make sure you install the libaio-dev package system-wide.
Next, you'll have to specify your GPU's architecture by editing the TORCH_CUDA_ARCH_LIST variable (find a complete list of NVIDIA GPUs and their corresponding architectures on this p... |
[
0.00968472,
-0.007938628,
-0.011342831,
-0.00833116,
-0.020574108,
0.0019965007,
-0.006459864,
0.0031385666,
0.0103208935,
0.06069362,
0.01312953,
0.008013073,
0.024431754,
-0.036112975,
-0.002864471,
0.038874235,
-0.020831285,
0.002654669,
-0.061993036,
-0.005843994,
0.03833... |
If you get 8, 6, then you can set TORCH_CUDA_ARCH_LIST="8.6". For multiple GPUs with different architectures, list them like TORCH_CUDA_ARCH_LIST="6.1;8.6".
It is also possible to not specify TORCH_CUDA_ARCH_LIST and the build program automatically queries the GPU architecture of the build. However, it may or may not... |
[
0.008824902,
-0.009889854,
-0.010216439,
0.0019666112,
-0.009307681,
0.0065814033,
0.0033386245,
0.000043873802,
0.004231409,
0.066453,
0.0071564773,
0.0027032031,
0.044869978,
-0.02105765,
-0.017876994,
0.06378353,
-0.01928273,
-0.0050052744,
-0.04364883,
0.0024067916,
-0.00... | CUDA_VISIBLE_DEVICES=0 python -c "import torch; print(torch.cuda.get_device_capability())"
To find the architecture for GPU 0:
CUDA_VISIBLE_DEVICES=0 python -c "import torch; \
print(torch.cuda.get_device_properties(torch.device('cuda')))
"_CudaDeviceProperties(name='GeForce RTX 3090', major=8, minor=6, total_memory=... |
[
0.013615156,
-0.023608401,
-0.04334561,
-0.008079645,
-0.011620905,
0.020661017,
-0.024121627,
-0.0062173675,
0.025382698,
0.06569294,
-0.0044247424,
-0.022640603,
0.027420938,
-0.0737286,
0.019546583,
0.04126338,
0.007086186,
-0.029121915,
-0.051498573,
-0.010301181,
0.07695... | git clone https://github.com/microsoft/DeepSpeed/
cd DeepSpeed
rm -rf build
TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 \
python setup.py build_ext -j8 bdist_wheel
This command generates a binary wheel that'll look something like dist/deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl. Now you can ... |
[
0.012657331,
-0.0061682616,
-0.017761094,
0.02992263,
-0.003089599,
-0.020269228,
-0.010061704,
-0.006237527,
0.03420979,
0.08101858,
-0.011118911,
0.0035689883,
0.05173757,
-0.02623334,
-0.029106028,
0.038117815,
-0.012343815,
-0.036163803,
-0.019831764,
-0.03619297,
0.01170... | python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
If both processes can talk to each and allocate GPU memory each will print an OK status.
For more GPUs or nodes adjust the arguments in the script.
You will find a lot more details inside the diagnostics script and even a recipe... |
[
0.008402497,
0.022108749,
-0.02222596,
0.029258562,
-0.013383925,
-0.0007453828,
0.012204498,
0.0004734188,
0.04354354,
0.06821626,
-0.024584811,
0.02341271,
0.06182831,
-0.065051585,
-0.011266817,
0.05450268,
-0.004582181,
-0.053594302,
-0.03539744,
-0.026635988,
0.044569127... | pip install deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl
Multi-GPU Network Issues Debug
When training or inferencing with DistributedDataParallel and multiple GPU, if you run into issue of inter-communication between processes and/or nodes, you can use the following script to diagnose network issues.
wget https... |
[
0.009934811,
-0.02958142,
-0.021265242,
0.016114393,
0.016617967,
-0.023811894,
-0.013438253,
0.002656357,
0.033235934,
0.0902406,
-0.007819797,
0.010884409,
0.03714943,
-0.039940674,
-0.028761312,
0.030962655,
0.0037876016,
-0.024819043,
-0.044343356,
-0.006100449,
0.0086255... | NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
This will dump a lot of NCCL-related debug information, which you can then search online if you find that some problems are reported. Or if you're not sure how to interpret the output you can share the log file i... |
[
0.04179087,
-0.009077294,
-0.044616487,
0.03628092,
-0.010059196,
-0.005637105,
-0.017730745,
0.0014746187,
0.013110861,
0.07939983,
-0.0012468033,
-0.014431837,
0.04119749,
-0.033370532,
0.0044856663,
0.036733016,
-0.004436218,
-0.039615147,
-0.054647427,
-0.0059973714,
0.01... | This feature is currently available for PyTorch-only.
For multi-GPU training it requires DDP (torch.distributed.launch).
This feature can be used with any nn.Module-based model. |
[
0.04276009,
0.002227088,
-0.004788239,
0.01964148,
-0.027227946,
-0.030317131,
0.002455185,
-0.0045583462,
0.01613561,
0.08638228,
0.011731724,
0.016983341,
0.016854027,
-0.06368034,
-0.026753793,
0.04689816,
-0.013254765,
-0.045317646,
-0.06954262,
0.0044649523,
0.026969315,... | For multi-GPU training it requires DDP (torch.distributed.launch).
This feature can be used with any nn.Module-based model.
If you start getting loss=NaN or the model inhibits some other abnormal behavior due to inf or nan in
activations or weights one needs to discover where the first underflow or overflow happens a... |
[
0.047272723,
0.02940272,
0.020053796,
-0.008349801,
-0.04224857,
-0.030887129,
0.025848702,
0.025634605,
0.024521297,
0.06657004,
0.009862757,
0.028489238,
0.021609573,
-0.042876586,
-0.01772727,
0.05506587,
-0.0021766578,
-0.021795124,
-0.054552037,
-0.007657553,
-0.01411616... | --debug underflow_overflow
to the normal command line arguments, or pass debug="underflow_overflow" when creating the
[TrainingArguments] object.
If you're using your own training loop or another Trainer you can accomplish the same with:
thon
from transformers.debug_utils import DebugUnderflowOverflow
debug_overflow = ... |
[
0.008851635,
-0.016257813,
-0.008336422,
0.01003233,
-0.010998353,
-0.021796346,
-0.0042361906,
0.006107413,
0.011921442,
0.069210194,
0.011034132,
0.008858791,
0.03678044,
-0.033173952,
-0.033173952,
0.015127208,
-0.017474286,
-0.031828675,
-0.052379925,
0.0059929215,
0.0177... |
You can see that we added 2 of these and now we track if inf or nan for forwarded_states was detected
somewhere in between.
Actually, the detector already reports these because each of the calls in the example above is a nn.Module, but
let's say if you had some local direct calculations this is how you'd do that.
Add... |
[
0.001960159,
-0.01806786,
0.00045669777,
0.030660609,
-0.004046667,
-0.010908101,
0.010564152,
0.025817243,
-0.022490062,
0.08816329,
-0.0034465108,
0.0077634235,
0.018755756,
-0.022770837,
-0.056856897,
0.020987917,
-0.014403747,
-0.0534876,
-0.06429743,
-0.008739116,
0.0068... |
Since the automatic detector only reports on inputs and outputs of full frames, once you know where to look, you may
want to analyse the intermediary stages of any specific forward function as well. In such a case you can use the
detect_overflow helper function to inject the detector where you want it, for example:
t... |
[
0.022857526,
0.027699849,
0.0066269166,
0.0097729545,
-0.023667034,
-0.031644356,
-0.010744362,
0.019163232,
0.014688868,
0.08872194,
-0.024932807,
-0.009103271,
0.038326465,
-0.012113165,
-0.06729209,
0.031791538,
-0.036560267,
-0.051337447,
-0.051072516,
0.008220173,
0.0083... |
Specific batch absolute min and max value tracing
The same debugging class can be used for per-batch tracing with the underflow/overflow detection feature turned off.
Let's say you want to watch the absolute min and max values for all the ingredients of each forward call of a given
batch, and only do that for batches... |
[
0.009033112,
-0.013269065,
0.003738171,
0.031550545,
-0.006561499,
0.004270548,
0.0052468926,
0.0075916583,
0.023309274,
0.060210496,
-0.0015538875,
0.012546416,
0.038930792,
-0.061102275,
-0.03920755,
0.0118775815,
-0.0013405522,
-0.026876392,
-0.054675315,
-0.013991714,
0.0... |
[~debug_utils.DebugUnderflowOverflow] inserts hooks into the model that immediately after each
forward call will test input and output variables and also the corresponding module's weights. As soon as inf or
nan is detected in at least one element of the activations or weights, the program will assert and print a rep... |
[
-0.018491283,
-0.054415926,
-0.03734017,
0.04777039,
0.0057738693,
-0.016226435,
0.026194742,
0.033019084,
0.008560227,
0.054564927,
0.030873438,
0.02370639,
0.042644683,
-0.031737655,
-0.06806461,
0.044343315,
0.00870178,
-0.042912886,
-0.048068393,
-0.009126439,
-0.00187557... |
Now it's easy to see the dropout call, and all the previous calls as well.
Since the detection is happening in a forward hook, these reports are printed immediately after each forward
returns.
Going back to the full report, to act on it and to fix the problem, we need to go a few frames up where the numbers
started t... |
[
0.022044012,
0.024412839,
0.024593983,
0.022545647,
-0.029986545,
-0.01560638,
0.015341629,
-0.009997837,
0.0029801917,
0.04422737,
-0.014770324,
-0.0019386051,
0.028648855,
-0.01903421,
-0.009050307,
0.020929271,
0.0020849148,
-0.03260619,
-0.045174897,
-0.026753794,
0.01737... | abs min abs max metadata
shared Embedding
1.01e-06 7.92e+02 weight
0.00e+00 2.78e+04 input[0]
5.36e-05 7.92e+02 output
[] |
[
0.023683703,
0.0002476558,
-0.014305687,
0.014684738,
-0.0076793036,
-0.03091376,
0.003534305,
0.020623213,
0.02358543,
0.083503656,
-0.013512486,
0.010788931,
0.028976386,
-0.039365206,
-0.023487158,
0.040684868,
0.0016539634,
-0.059412822,
-0.07435026,
-0.01160319,
0.016565... |
Here you will get a huge number of frames dumped - as many as there were forward calls in your model, so it may or may
not what you want, but sometimes it can be easier to use for debugging purposes than a normal debugger. For example, if
a problem starts happening at batch number 150. So you can dump traces for batc... |
[
0.030636534,
0.004469616,
-0.027175263,
-0.03893214,
-0.02628849,
0.005141846,
0.004473191,
0.007033387,
-0.019938061,
0.035528082,
0.037158597,
-0.03930401,
0.02498694,
-0.05226232,
0.0067616347,
0.036843937,
-0.0044410103,
0.007069144,
-0.09422665,
-0.01992376,
0.0078093125... |
Create a custom architecture
An AutoClass automatically infers the model architecture and downloads pretrained configuration and weights. Generally, we recommend using an AutoClass to produce checkpoint-agnostic code. But users who want more control over specific model parameters can create a custom 🤗 Transformers mo... |
[
0.032272104,
0.0049936906,
0.00066672335,
0.011468989,
-0.008748218,
-0.057163112,
0.010189419,
-0.0069702896,
-0.0058052074,
0.03396922,
-0.027046071,
0.02754443,
0.07806724,
-0.04296662,
-0.030817436,
0.0011937042,
-0.010000851,
-0.037686706,
-0.059803065,
-0.037201818,
0.0... |
*** Starting batch number=1 ***
abs min abs max metadata
shared Embedding
1.01e-06 7.92e+02 weight
0.00e+00 2.47e+04 input[0]
5.36e-05 7.92e+02 output
[]
decoder.dropout Dropout
1.60e-07 2.27e+01 input[0]
0.00e+00 2.52e+01 output
decoder T5Stac... |
[
0.031695407,
-0.014159824,
-0.050283264,
-0.06118739,
0.03855992,
0.022867586,
-0.019915562,
-0.0057875193,
0.011977587,
0.029124746,
0.0134324115,
-0.021808248,
0.03367284,
-0.07672435,
0.037740696,
0.054746613,
-0.030565446,
-0.07627237,
-0.07497291,
-0.022457976,
-0.011610... | Load and customize a model configuration.
Create a model architecture.
Create a slow and fast tokenizer for text.
Create an image processor for vision tasks.
Create a feature extractor for audio tasks.
Create a processor for multimodal tasks. |
[
0.024441844,
-0.020442016,
-0.0389464,
-0.043486,
0.007895851,
-0.008331818,
0.02840015,
-0.0031867125,
-0.018144539,
0.061948862,
0.041742127,
-0.012843732,
0.023915915,
-0.02613035,
-0.0084979,
0.049963225,
-0.04373512,
0.0019635824,
-0.06344361,
0.014947447,
0.0034012361,
... | Configuration
A configuration refers to a model's specific attributes. Each model configuration has different attributes; for instance, all NLP models have the hidden_size, num_attention_heads, num_hidden_layers and vocab_size attributes in common. These attributes specify the number of attention heads or hidden layers... |
[
-0.03053846,
-0.043778326,
-0.008285412,
-0.03400938,
0.00851634,
0.009069167,
0.01114052,
0.012533085,
-0.027823308,
0.06992216,
0.040419374,
-0.024086475,
0.031042304,
0.004401625,
0.001392565,
0.015955016,
-0.02919488,
-0.030790383,
-0.058893602,
0.011469417,
0.007795565,
... | [DistilBertConfig] displays all the default attributes used to build a base [DistilBertModel]. All attributes are customizable, creating space for experimentation. For example, you can customize a default model to:
Try a different activation function with the activation parameter.
Use a higher dropout ratio for the at... |
[
0.009327868,
-0.020590188,
-0.0097675165,
-0.007089326,
-0.0056934436,
-0.009965358,
0.016809214,
-0.0045466945,
-0.008558484,
0.06190246,
0.015446305,
-0.0016889817,
0.07345055,
-0.026203029,
0.0060341707,
0.02287636,
-0.018978145,
-0.011797225,
-0.074329846,
-0.026540093,
-... | from transformers import DistilBertConfig
config = DistilBertConfig()
print(config)
DistilBertConfig {
"activation": "gelu",
"attention_dropout": 0.1,
"dim": 768,
"dropout": 0.1,
"hidden_dim": 3072,
"initializer_range": 0.02,
"max_position_embeddings": 512,
"model_type": "distilbert",
"n_heads": 12,
... |
[
-0.02127944,
-0.022480113,
-0.0055838637,
-0.017851004,
-0.043253243,
-0.003931127,
0.011847627,
0.0017684646,
0.01711324,
0.061914343,
0.046291098,
-0.010487827,
0.03376357,
-0.023709722,
0.018849157,
0.018458575,
-0.013778835,
0.026877768,
-0.0677586,
0.011601706,
-0.028960... | my_config.save_pretrained(save_directory="./your_model_save_path")
To reuse the configuration file, load it with [~PretrainedConfig.from_pretrained]:
my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json")
You can also save your configuration file as a dictionary or even just the differenc... |
[
0.042562485,
-0.0015005308,
-0.053007267,
-0.04810402,
0.0033727943,
0.009189958,
-0.011866433,
-0.020497885,
-0.0042975927,
0.04453539,
0.022441775,
0.006941428,
0.020062685,
-0.048568238,
-0.016610105,
0.052078843,
-0.018974688,
-0.019511433,
-0.092494346,
-0.0022122627,
0.... |
Model
The next step is to create a model. The model - also loosely referred to as the architecture - defines what each layer is doing and what operations are happening. Attributes like num_hidden_layers from the configuration are used to define the architecture. Every model shares the base class [PreTrainedModel] and... |
[
0.0073063266,
-0.031434197,
-0.018902997,
-0.040156476,
0.009515216,
0.011504632,
0.028616445,
-0.012134732,
0.019894164,
0.06802247,
0.044687532,
-0.028588125,
0.034945764,
-0.048114143,
0.042478643,
0.04310166,
-0.013302894,
-0.008970073,
-0.066719785,
0.019865844,
-0.01118... | Load your custom configuration attributes into the model:
from transformers import DistilBertModel
my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json")
model = DistilBertModel(my_config) |
[
-0.0107426355,
-0.027454207,
-0.020934738,
-0.03401714,
-0.031148572,
0.0023524414,
0.022064779,
0.005012747,
0.010365955,
0.0454914,
0.03729136,
-0.0035929515,
0.03558181,
-0.011916139,
0.029989555,
0.020673959,
-0.0032235149,
-0.010076201,
-0.07238059,
-0.00035132692,
-0.02... | Pretrained model attributes can be modified in the [~PretrainedConfig.from_pretrained] function:
my_config = DistilBertConfig.from_pretrained("distilbert/distilbert-base-uncased", activation="relu", attention_dropout=0.4)
Once you are satisfied with your model configuration, you can save it with [~PretrainedConfig.sa... |
[
-0.0073310025,
-0.033425577,
-0.020011557,
-0.004532158,
-0.0045139124,
-0.010166338,
0.03149886,
-0.00082606054,
-0.012005474,
0.059144296,
0.015442908,
0.030214384,
0.05534925,
-0.019778015,
-0.0011731794,
0.011932492,
-0.018230805,
-0.0069697434,
-0.077418886,
-0.015121789,
... | my_config = DistilBertConfig(activation="relu", attention_dropout=0.4)
print(my_config)
DistilBertConfig {
"activation": "relu",
"attention_dropout": 0.4,
"dim": 768,
"dropout": 0.1,
"hidden_dim": 3072,
"initializer_range": 0.02,
"max_position_embeddings": 512,
"model_type": "distilbert",
"n_heads": 1... |
[
0.00025155157,
0.002191255,
0.0074384604,
-0.019210814,
-0.04742279,
-0.0019099689,
0.013918464,
0.01220991,
-0.010869459,
0.07839901,
0.060618937,
-0.016529912,
0.024447601,
-0.056840673,
0.01128618,
0.032309726,
-0.027434098,
-0.042616613,
-0.051201057,
0.003257365,
0.00478... | This creates a model with random values instead of pretrained weights. You won't be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the res... |
[
-0.00063417776,
-0.028692577,
-0.017407024,
-0.028460484,
-0.00013089266,
0.007318203,
0.010030798,
-0.004979134,
-0.024166752,
0.07276136,
0.022904743,
-0.004177686,
0.04256017,
-0.036351666,
0.027488591,
0.043952733,
-0.014128701,
-0.014513106,
-0.06283935,
0.002293738,
-0.... | model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config)
Load your custom configuration attributes into the model:
from transformers import TFDistilBertModel
my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json")
tf_model = TFDistilBertModel(my_con... |
[
0.00043302466,
0.0031774933,
-0.0004949459,
0.0061955187,
-0.049754154,
-0.004302255,
0.0059376545,
0.017263306,
-0.023099173,
0.07122471,
0.05211564,
-0.004193681,
0.042561106,
-0.059932988,
0.00045380645,
0.030590797,
-0.009439174,
-0.04378257,
-0.043809712,
-0.012479253,
-... | This creates a model with random values instead of pretrained weights. You won't be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the res... |
[
-0.024718458,
-0.03898921,
0.00075056416,
-0.049757935,
-0.023872135,
0.029329458,
0.023755401,
-0.00077883573,
-0.028468544,
0.062044207,
0.056586884,
-0.019231953,
0.015934212,
-0.04368776,
0.008098433,
0.065312766,
-0.037617583,
-0.009163632,
-0.06910662,
0.02514162,
-0.01... | model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased")
When you load pretrained weights, the default model configuration is automatically loaded if the model is provided by 🤗 Transformers. However, you can still replace - some or all of - the default model configuration attributes with your own... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.