source stringclasses 470
values | url stringlengths 49 167 | file_type stringclasses 1
value | chunk stringlengths 1 512 | chunk_id stringlengths 5 9 |
|---|---|---|---|---|
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#quantized-cache | .md | >>> out = model.generate(**inputs, do_sample=False, max_new_tokens=20, cache_implementation="quantized", cache_config={"nbits": 4, "backend": "quanto"})
>>> print(tokenizer.batch_decode(out, skip_special_tokens=True)[0])
I like rock music because it's loud and energetic. It's a great way to express myself and rel | 6_5_6 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#quantized-cache | .md | >>> out = model.generate(**inputs, do_sample=False, max_new_tokens=20)
>>> print(tokenizer.batch_decode(out, skip_special_tokens=True)[0])
I like rock music because it's loud and energetic. I like to listen to it when I'm feeling
``` | 6_5_7 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | Similarly to KV cache quantization, [`~OffloadedCache`] strategy aims to reduce GPU VRAM usage.
It does so by moving the KV cache for most layers to the CPU.
As the model's `forward()` method iterates over the layers, this strategy maintains the current layer cache on the GPU.
At the same time it asynchronously prefetc... | 6_6_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | At the same time it asynchronously prefetches the next layer cache as well as sending the previous layer cache back to the CPU.
Unlike KV cache quantization, this strategy always produces the same result as the default KV cache implementation.
Thus, it can serve as a drop-in replacement or a fallback for it.
Dependin... | 6_6_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | you may notice a small degradation in generation throughput compared to the default KV cache implementation.
To enable KV cache offloading, pass `cache_implementation="offloaded"` in the `generation_config` or directly to the `generate()` call.
Use `cache_implementation="offloaded_static"` for an offloaded static cac... | 6_6_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | ```python
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> ckpt = "microsoft/Phi-3-mini-4k-instruct" | 6_6_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | >>> tokenizer = AutoTokenizer.from_pretrained(ckpt)
>>> model = AutoModelForCausalLM.from_pretrained(ckpt, torch_dtype=torch.float16).to("cuda:0")
>>> inputs = tokenizer("Fun fact: The shortest", return_tensors="pt").to(model.device)
>>> out = model.generate(**inputs, do_sample=False, max_new_tokens=23, cache_implemen... | 6_6_4 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | >>> out = model.generate(**inputs, do_sample=False, max_new_tokens=23)
>>> print(tokenizer.batch_decode(out, skip_special_tokens=True)[0])
Fun fact: The shortest war in history was between Britain and Zanzibar on August 27, 1896.
```
<Tip warning={true}>
Cache offloading requires a CUDA GPU and can be slower than d... | 6_6_5 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | </Tip>
The example below shows how KV cache offloading can be used as a fallback strategy.
```python
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
>>> def resilient_generate(model, *args, **kwargs):
... oom = False
... try:
... return model.generate(*args, **kwargs)... | 6_6_6 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | ... print(e)
... print("retrying with cache_implementation='offloaded'")
... oom = True
... if oom:
... torch.cuda.empty_cache()
... kwargs["cache_implementation"] = "offloaded"
... return model.generate(*args, **kwargs)
...
...
>>> ckpt = "microsoft/Phi-3-mini-4k-ins... | 6_6_7 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | >>> model = AutoModelForCausalLM.from_pretrained(ckpt, torch_dtype=torch.float16).to("cuda:0")
>>> prompt = ["okay "*1000 + "Fun fact: The most"]
>>> inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
>>> beams = { "num_beams": 40, "num_beam_groups": 40, "num_return_sequences": 40, "diversity_penalty": 1.... | 6_6_8 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-cache | .md | >>> responses = tokenizer.batch_decode(out[:,-28:], skip_special_tokens=True)
```
On a GPU with 50 GB of RAM, running this code will print
```
CUDA out of memory. Tried to allocate 4.83 GiB. GPU
retrying with cache_implementation='offloaded'
```
before successfully generating 40 beams. | 6_6_9 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#static-cache | .md | Since the "DynamicCache" dynamically grows with each generation step, it prevents you from taking advantage of JIT optimizations. The [`~StaticCache`] pre-allocates
a specific maximum size for the keys and values, allowing you to generate up to the maximum length without having to modify cache size. Check the below usa... | 6_7_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#static-cache | .md | ```python
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM | 6_7_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#static-cache | .md | >>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
>>> model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", torch_dtype=torch.float16, device_map="auto")
>>> inputs = tokenizer("Hello, my name is", return_tensors="pt").to(model.device) | 6_7_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#static-cache | .md | >>> # simply pass the cache implementation="static"
>>> out = model.generate(**inputs, do_sample=False, max_new_tokens=20, cache_implementation="static")
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"Hello, my name is [Your Name], and I am a [Your Profession] with [Number of Years] of"
``` | 6_7_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-static-cache | .md | Like [`~OffloadedCache`] exists for offloading a "DynamicCache", there is also an offloaded static cache. It fully supports
JIT optimizations. Just pass `cache_implementation="offloaded_static"` in the `generation_config` or directly to the `generate()` call.
This will use the [`~OffloadedStaticCache`] implementation i... | 6_8_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-static-cache | .md | >>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
>>> model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", torch_dtype=torch.float16, device_map="auto")
>>> inputs = tokenizer("Hello, my name is", return_tensors="pt").to(model.device) | 6_8_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#offloaded-static-cache | .md | >>> # simply pass the cache implementation="static"
>>> out = model.generate(**inputs, do_sample=False, max_new_tokens=20, cache_implementation="offloaded_static")
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"Hello, my name is [Your Name], and I am a [Your Profession] with [Number of Years] of"
```
Cac... | 6_8_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sliding-window-cache | .md | As the name suggests, this cache type implements a sliding window over previous keys and values, retaining only the last `sliding_window` tokens. It should be used with models like Mistral that support sliding window attention. Additionally, similar to Static Cache, this one is JIT-friendly and can be used with the sam... | 6_9_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sliding-window-cache | .md | Note that you can use this cache only for models that support sliding window, e.g. Mistral models.
```python
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, SinkCache | 6_9_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sliding-window-cache | .md | >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1")
>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", torch_dtype=torch.float16).to("cuda:0")
>>> inputs = tokenizer("Yesterday I was on a rock concert and.", return_tensors="pt").to(model.device) | 6_9_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sliding-window-cache | .md | >>> # can be used by passing in cache implementation
>>> out = model.generate(**inputs, do_sample=False, max_new_tokens=30, cache_implementation="sliding_window")
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"Yesterday I was on a rock concert and. I was so excited to see my favorite band. I was so excit... | 6_9_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | Sink Cache was introduced in ["Efficient Streaming Language Models with Attention Sinks"](https://arxiv.org/abs/2309.17453). It allows you to generate long sequences of text ("infinite length" according to the paper) without any fine-tuning. That is achieved by smart handling of previous keys and values, specifically i... | 6_10_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | This is based on the observation that these initial tokens attract a significant portion of attention scores during the generation process. Tokens that come after "sink tokens" are discarded on a sliding windowed basis, keeping only the latest `window_size` tokens. By keeping these initial tokens as "attention sinks," ... | 6_10_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | Unlike other cache classes, this one can't be used directly by indicating a `cache_implementation`. You have to initialize the Cache before calling on `generate()` as follows.
```python
>>> import torch
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, SinkCache | 6_10_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | >>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
>>> model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", torch_dtype=torch.float16).to("cuda:0")
>>> inputs = tokenizer("This is a long story about unicorns, fairies and magic.", return_tensors="pt").to(model.device) | 6_10_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | >>> # get our cache, specify number of sink tokens and window size
>>> # Note that window size already includes sink tokens, so has to be larger
>>> past_key_values = SinkCache(window_length=256, num_sink_tokens=4)
>>> out = model.generate(**inputs, do_sample=False, max_new_tokens=30, past_key_values=past_key_values)
>... | 6_10_4 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#sink-cache | .md | >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"This is a long story about unicorns, fairies and magic. It is a fantasy world where unicorns and fairies live together in harmony. The story follows a young girl named Lily"
``` | 6_10_5 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#encoder-decoder-cache | .md | The [`~EncoderDecoderCache`] is a wrapper designed to handle the caching needs of encoder-decoder models. This cache type is specifically built to manage both self-attention and cross-attention caches, ensuring storage and retrieval of past key/values required for these complex models. Cool thing about Encoder-Decoder ... | 6_11_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#encoder-decoder-cache | .md | for the decoder, depending on your use case. Currently this cache is only supported in [Whisper](./model_doc/whisper) models but we will be adding more models soon. | 6_11_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#encoder-decoder-cache | .md | In terms of usage, there is nothing special to be done and calling `generate()` or `forward()` will handle everything for you. | 6_11_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#model-specific-cache-classes | .md | Some models require storing previous keys, values, or states in a specific way, and the above cache classes cannot be used. For such cases, we have several specialized cache classes that are designed for specific models. These models only accept their own dedicated cache classes and do not support using any other cache... | 6_12_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | We have seen how to use each of the cache types when generating. What if you want to use cache in iterative generation setting, for example in applications like chatbots, where interactions involve multiple turns and continuous back-and-forth exchanges. Iterative generation with cache allows these systems to handle ong... | 6_13_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | The general format when doing iterative generation is as below. First you have to initialize an empty cache of the type you want, and you can start feeding in new prompts iteratively. Keeping track of dialogues history and formatting can be done with chat templates, read more on that in [chat_templating](./chat_templat... | 6_13_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | In case you are using Sink Cache, you have to crop your inputs to that maximum length because Sink Cache can generate text longer than its maximum window size, but it expects the first input to not exceed the maximum cache length.
```python
>>> import torch
>>> from transformers import AutoTokenizer,AutoModelForCausa... | 6_13_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | >>> DynamicCache,
>>> SinkCache,
>>> StaticCache,
>>> SlidingWindowCache,
>>> QuantoQuantizedCache,
>>> QuantizedCacheConfig,
>>> ) | 6_13_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | >>> model_id = "meta-llama/Llama-2-7b-chat-hf"
>>> model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map='auto')
>>> tokenizer = AutoTokenizer.from_pretrained(model_id)
>>> user_prompts = ["Hello, what's your name?", "Btw, yesterday I was on a rock concert."]
>>> past_key_value... | 6_13_4 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | >>> messages = []
>>> for prompt in user_prompts:
... messages.append({"role": "user", "content": prompt})
... inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
... if isinstance(past_key_values, SinkCache):
... input... | 6_13_5 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | ...
... input_length = inputs["input_ids"].shape[1]
...
... outputs = model.generate(**inputs, do_sample=False, max_new_tokens=256, past_key_values=past_key_values)
... completion = tokenizer.decode(outputs[0, input_length: ], skip_special_tokens=True)
... messages.append({"role": "assistant", "content"... | 6_13_6 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#iterative-generation-with-cache | .md | print(messages)
[{'role': 'user', 'content': "Hello, what's your name?"}, {'role': 'assistant', 'content': " Hello! My name is LLaMA, I'm a large language model trained by a team of researcher at Meta AI. 😊"}, {'role': 'user', 'content': 'Btw, yesterday I was on a rock concert.'}, {'role': 'assistant', 'content': ' Oh... | 6_13_7 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#re-use-cache-to-continue-generation | .md | Sometimes you would want to first fill-in cache object with key/values for certain prefix prompt and re-use it several times to generate different sequences from it. In that case you can construct a `Cache` object that will hold the instruction prompt, and re-use it several times with different text sequences.
```pyt... | 6_14_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#re-use-cache-to-continue-generation | .md | >>> model_id = "meta-llama/Llama-2-7b-chat-hf"
>>> model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="cuda")
>>> tokenizer = AutoTokenizer.from_pretrained(model_id)
>>> # Init StaticCache with big enough max-length (1024 tokens for the below example)
>>> # You can also init ... | 6_14_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#re-use-cache-to-continue-generation | .md | >>> INITIAL_PROMPT = "You are a helpful assistant. "
>>> inputs_initial_prompt = tokenizer(INITIAL_PROMPT, return_tensors="pt").to("cuda")
>>> # This is the common prompt cached, we need to run forward without grad to be abel to copy
>>> with torch.no_grad():
... prompt_cache = model(**inputs_initial_prompt, past_... | 6_14_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#re-use-cache-to-continue-generation | .md | >>> prompts = ["Help me to write a blogpost about travelling.", "What is the capital of France?"]
>>> responses = []
>>> for prompt in prompts:
... new_inputs = tokenizer(INITIAL_PROMPT + prompt, return_tensors="pt").to("cuda")
... past_key_values = copy.deepcopy(prompt_cache)
... outputs = model.generate(*... | 6_14_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#re-use-cache-to-continue-generation | .md | >>> print(responses)
['<s> You are a helpful assistant. Help me to write a blogpost about travelling.\n\nTitle: The Ultimate Guide to Travelling: Tips, Tricks, and', '<s> You are a helpful assistant. What is the capital of France?\n\nYes, the capital of France is Paris.</s>']
``` | 6_14_4 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#legacy-cache-format | .md | Prior to the introduction of the `Cache` object, the cache of LLMs used to be a tuple of tuples of tensors. The legacy
format has a dynamic size, growing as we generate text -- very similar to `DynamicCache`. If your project depend on
this legacy format, you can seamlessly convert it to a `DynamicCache` and back.
```... | 6_15_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#legacy-cache-format | .md | >>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
>>> model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", torch_dtype=torch.float16, device_map="auto")
>>> inputs = tokenizer("Hello, my name is", return_tensors="pt").to(model.device) | 6_15_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#legacy-cache-format | .md | >>> # `return_dict_in_generate=True` is required to return the cache. `return_legacy_cache` forces the returned cache
>>> # to be of the legacy type
>>> generation_outputs = model.generate(**inputs, return_dict_in_generate=True, return_legacy_cache=True, max_new_tokens=5) | 6_15_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/kv_cache.md | https://huggingface.co/docs/transformers/en/kv_cache/#legacy-cache-format | .md | >>> # We can convert a legacy cache to a DynamicCache -- and the other way around. This is helpful if you have custom
>>> # logic to manipulate a cache in a specific format.
>>> cache = DynamicCache.from_legacy_cache(generation_outputs.past_key_values)
>>> legacy_format_cache = cache.to_legacy_cache()
``` | 6_15_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/ | .md | <!--Copyright 2022 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agr... | 7_0_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/ | .md | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered ... | 7_0_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#distributed-training-with--accelerate | .md | As models get bigger, parallelism has emerged as a strategy for training larger models on limited hardware and accelerating training speed by several orders of magnitude. At Hugging Face, we created the [🤗 Accelerate](https://huggingface.co/docs/accelerate) library to help users easily train a 🤗 Transformers model on... | 7_1_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#distributed-training-with--accelerate | .md | machine or multiple GPU's across several machines. In this tutorial, learn how to customize your native PyTorch training loop to enable training in a distributed environment. | 7_1_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#setup | .md | Get started by installing 🤗 Accelerate:
```bash
pip install accelerate
```
Then import and create an [`~accelerate.Accelerator`] object. The [`~accelerate.Accelerator`] will automatically detect your type of distributed setup and initialize all the necessary components for training. You don't need to explicitly pl... | 7_2_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#prepare-to-accelerate | .md | The next step is to pass all the relevant training objects to the [`~accelerate.Accelerator.prepare`] method. This includes your training and evaluation DataLoaders, a model and an optimizer:
```py
>>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(
... train_dataloader, eval_dataloader... | 7_3_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#backward | .md | The last addition is to replace the typical `loss.backward()` in your training loop with 🤗 Accelerate's [`~accelerate.Accelerator.backward`] method:
```py
>>> for epoch in range(num_epochs):
... for batch in train_dataloader:
... outputs = model(**batch)
... loss = outputs.loss
... accele... | 7_4_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#backward | .md | ... optimizer.step()
... lr_scheduler.step()
... optimizer.zero_grad()
... progress_bar.update(1)
```
As you can see in the following code, you only need to add four additional lines of code to your training loop to enable distributed training!
```diff
+ from accelerate import Accele... | 7_4_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#backward | .md | + accelerator = Accelerator()
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)
- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
- model.to(device)
+ train_dataloader, eval_dataloader, model, optimi... | 7_4_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#backward | .md | num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
"linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps
)
progress_bar = tqdm(range(num_training_steps))
model.train()
for epoch in range(num_epochs):
for batch in train_dataloader:
- ... | 7_4_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#backward | .md | optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
progress_bar.update(1)
``` | 7_4_4 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#train | .md | Once you've added the relevant lines of code, launch your training in a script or a notebook like Colaboratory. | 7_5_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#train-with-a-script | .md | If you are running your training from a script, run the following command to create and save a configuration file:
```bash
accelerate config
```
Then launch your training with:
```bash
accelerate launch train.py
``` | 7_6_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/accelerate.md | https://huggingface.co/docs/transformers/en/accelerate/#train-with-a-notebook | .md | 🤗 Accelerate can also run in a notebook if you're planning on using Colaboratory's TPUs. Wrap all the code responsible for training in a function, and pass it to [`~accelerate.notebook_launcher`]:
```py
>>> from accelerate import notebook_launcher
>>> notebook_launcher(training_function)
```
For more information ... | 7_7_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/ | .md | <!--⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
--> | 8_0_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community | .md | This page regroups resources around 🤗 Transformers developed by the community. | 8_1_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-resources | .md | | Resource | Description | Author |
|:----------|:-------------|------:| | 8_2_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-resources | .md | | [Hugging Face Transformers Glossary Flashcards](https://www.darigovresearch.com/huggingface-transformers-glossary-flashcards) | A set of flashcards based on the [Transformers Docs Glossary](glossary) that has been put into a form which can be easily learned/revised using [Anki](https://apps.ankiweb.net/) an open sour... | 8_2_1 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-resources | .md | knowledge retention. See this [Introductory video on how to use the flashcards](https://www.youtube.com/watch?v=Dji_h7PILrw). | [Darigov Research](https://www.darigovresearch.com/) | | 8_2_2 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | Notebook | Description | Author | |
|:----------|:-------------|:-------------|------:| | 8_3_0 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |:----------|:-------------|:-------------|------:|
| [Fine-tune a pre-trained Transformer to generate lyrics](https://github.com/AlekseyKorshuk/huggingartists) | How to generate lyrics in the style of your favorite artist by fine-tuning a GPT-2 model | [Aleksey Korshuk](https://github.com/AlekseyKorshuk) | [ | How to train T5 for any task using Tensorflow 2. This notebook demonstrates a Question & Answer task implemented in Tensorflow 2 using SQUAD | [Muhammad Harris](https://github.com/HarrisDePerceptron) |[ | How to train T5 on SQUAD with Transformers and Nlp | [Suraj Patil](https://github.com/patil-suraj) |[](https://colab.research.google.com/github/patil-... | 8_3_3 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | [Fine-tune T5 for Classification and Multiple Choice](https://github.com/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | How to fine-tune T5 for classification and multiple choice tasks using a text-to-text format with PyTorch Lightning | [Suraj Patil](https://github.com/patil-suraj) | [ | How to fine-tune the DialoGPT model on a new dataset for open-dialog conversational chatbots | [Nathan Cooper](https://github.com/ncoop57) | [ | How to train on sequences as long as 500,000 tokens with Reformer | [Patrick von Platen](https://github.com/patrickvonplaten) | [ | How to fine-tune BART for summarization with fastai using blurr | [Wayde Gilliam](https://ohmeow.com/) | [ | How to generate tweets in the style of your favorite Twitter account by fine-tuning a GPT-2 model | [Boris Dayma](https://github.com/borisdayma) | [ | A complete tutorial showcasing W&B integration with Hugging Face | [Boris Dayma](https://github.com/borisdayma) |... | 8_3_9 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | A complete tutorial showcasing W&B integration with Hugging Face | [Boris Dayma](https://github.com/borisdayma) | [](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weight... | 8_3_10 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | [Pretrain Longformer](https://github.com/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | How to build a "long" version of existing pretrained models | [Iz Beltagy](https://beltagy.net) | [](https://colab.research.google.... | 8_3_11 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | [Fine-tune Longformer for QA](https://github.com/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | How to fine-tune longformer model for QA task | [Suraj Patil](https://github.com/patil-suraj) | [](https://colab.research.google.... | 8_3_12 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | | [Evaluate Model with 🤗nlp](https://github.com/patrickvonplaten/notebooks/blob/master/How_to_evaluate_Longformer_on_TriviaQA_using_NLP.ipynb) | How to evaluate longformer on TriviaQA with `nlp` | [Patrick von Platen](https://github.com/patrickvonplaten) | [ | How to fine-tune T5 for sentiment span extraction using a text-to-text format with PyTorch Lightning | [Lorenzo Ampil](https://github.com/enzoampil) | [ | How to fine-tune DistilBert for multiclass classification with PyTorch | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [ | [](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.... | 8_3_16 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[Fine-tune BERT for Multi-label Classification](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)|How to fine-tune BERT for multi-label classification using PyTorch|[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[|How to fine-tune T5 for summarization in PyTorch and track experiments with WandB|[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[|How to speed up fine-tuning by a factor of 2 using dynamic padding / bucketing|[Michael Benesty](https://github.com/pomm... | 8_3_19 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[Pretrain Reformer for Masked Language Modeling](https://github.com/patrickvonplaten/notebooks/blob/master/Reformer_For_Masked_LM.ipynb)| How to train a Reformer model with bi-directional self-attention layers | [Patrick von Platen](https://github.com/patrickvonplaten) | [| How to increase vocabulary of a pretrained SciBERT model from AllenAI on the CORD dataset and pipeline it. | [Tanmay Thakur](https://github.com/lordtt13) | [| How to fine-tune BlenderBotSmall for summarization on a custom dataset, using the Trainer API. | [Tanmay Thakur](https... | 8_3_22 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[Fine-tune Electra and interpret with Integrated Gradients](https://github.com/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb) | How to fine-tune Electra for sentiment analysis and interpret predictions with Captum Integrated Gradients | [Eliza Szczechla](https://elsanns.github.io) |... | 8_3_23 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | analysis and interpret predictions with Captum Integrated Gradients | [Eliza Szczechla](https://elsanns.github.io) | [](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb)| | 8_3_24 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[fine-tune a non-English GPT-2 Model with Trainer class](https://github.com/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb) | How to fine-tune a non-English GPT-2 Model with Trainer class | [Philipp Schmid](https://www.philschmid.de) | [ | How to fine-tune a DistilBERT Model for Multi Label Classification task | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Open In Colab]... | 8_3_26 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[Fine-tune ALBERT for sentence-pair classification](https://github.com/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb) | How to fine-tune an ALBERT model or another BERT-based model for the sentence-pair classification task | [Nadir El Manouzi](https://github.com/NadirEM) | [ | [](https://colab.research.google.com/github/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb)| | 8_3_28 |
/Users/nielsrogge/Documents/python_projecten/transformers/docs/source/en/community.md | https://huggingface.co/docs/transformers/en/community/#community-notebooks | .md | |[Fine-tune Roberta for sentiment analysis](https://github.com/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb) | How to fine-tune a Roberta model for sentiment analysis | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [ | How accurate are the answers to questions generated by your seq2seq transformer model? | [Pascal Zoleko](https://github.com/zolekode) | [](https://colab.research.g... | 8_3_30 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.