# What is the difference between forward() and generate()?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235%3E%5C
**Category:** Beginners\
**Created:** [September 23, 2021, 4:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235 "2021-09-23T04:04:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon2764274](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@anon2764274](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fanon2764274)%5C
**Post date:** [September 23, 2021, 4:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235%2F1 "2021-09-23T04:04:00Z")

</div>

Hi!

It seems like some models implement both functions and semantically they behave similarly, but might be implemented differently? What is the difference? In both cases, for an input sequence, the model produces a prediction (inference)?

Thank you,

wilornel

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [September 23, 2021, 7:36am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235%2F2 "2021-09-23T07:36:17Z")

</div>

Hi,

- `forward()` can be used both for training and inference. Forward refers to a single forward pass through the network. During training, we apply a forward pass to get the model’s predictions, and then do a backward pass to compute the gradients of the parameters with respect to the loss, which we then update. We then do another forward pass, followed by another backward pass etc. This is typically done on batches of data.
- `generate()` can only be used at inference time, and uses `forward()` behind the scenes, in a sequence of time steps (see [this post](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fgenerate-without-using-the-generate-method%2F11379) for a simple showcase of that). The first forward is used to predict the first token, next we append the predicted token to the input of the next time step, which again uses forward() to predict the next token, and so on. This is called autoregressive generation. There are decoding strategies to decide which next token to take as prediction such as beam search, top k sampling, and so on (a detailed blog post can be found [here](https://huggingface.co/blog/how-to-generate)).

---

<div class="post-metadata">

**Author:** ![alecstash](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/alecstash/32/14195_2.png) [@alecstash](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Falecstash)%5C
**Post date:** [February 17, 2023, 7:08am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235%2F3 "2023-02-17T07:08:05Z")

</div>

@nielsr Can you provide any insight as to why one would prefer to use one over the other.

For example, I am realizing that using `generate()` we are not able to obtain the model loss at inference time. If I can also generate the text sequence using `forward()`, i’d rather just use that. I feel there is something else at play though.

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [December 25, 2023, 9:08pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-the-difference-between-forward-and-generate%2F10235%2F4 "2023-12-25T21:08:28Z")

</div>

The generate method is more feature complete with various fancier decoding methods besides greedy decoding, such as beam search and top-k sampling.
