# Difference between pipeline and model.generate?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-pipeline-and-model-generate%2F35015%3E%5C
**Category:** 🤗Transformers\
**Created:** [March 29, 2023, 2:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-pipeline-and-model-generate%2F35015 "2023-03-29T14:03:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skrishna](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/skrishna/32/15038_2.png) [@skrishna](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fskrishna)%5C
**Post date:** [March 29, 2023, 2:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-pipeline-and-model-generate%2F35015%2F1 "2023-03-29T14:03:19Z")

</div>

I tried the following two things and find a significant difference between pipeline and model.generate to complete sequences.

```auto
model_pr = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer.decode(model_pr.generate(**input_tok)[0])
'My name is Merve and my favorite+ and my CR+ and my CR+ and my CR'

```

(2) Using pipeline to do that same thing

```auto
generator = pipeline('text-generation', model='gpt2') 
generator(input) 
[{'generated_text': 'My name is Merve and my favorite brand is Baskin-Robbins.\n\n"They bring a whole lot of stuff to the table and we have to come up with a new way of making a big deal," explains Jeff.'}]

```

I get a lot more sensible output for pipeline for some reason. My understanding was that both should have given similar responses.

---

<div class="post-metadata">

**Author:** ![AKstudent](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@AKstudent](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAKstudent)%5C
**Post date:** [February 23, 2024, 6:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-pipeline-and-model-generate%2F35015%2F2 "2024-02-23T18:16:29Z")

</div>

Has anyone figured out the solution to this? Experiencing the same problem.

---

<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:** [February 26, 2024, 8:47am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-pipeline-and-model-generate%2F35015%2F3 "2024-02-26T08:47:11Z")

</div>

Hi,

After digging a bit into the code base, I found that GPT-2 by default uses `do_sample=True` and `max_length=50` when generating text as seen here: [config.json · openai-community/gpt2 at main](https://huggingface.co/openai-community/gpt2/blob/main/config.json#L25).

Hence to get the equivalent behaviour, one can do the following:

```python
from transformers import pipeline, set_seed, AutoModelForCausalLM, AutoTokenizer

set_seed(42)

pipe = pipeline(model="gpt2")

prompt = "hello world, my name is"

result = pipe(prompt, do_sample=False)["generated_text"]
print(result)

# equivalent to:
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")

inputs = tokenizer(prompt, return_tensors="pt")

generated_ids = model.generate(**inputs, do_sample=False, max_length=50)
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
print(generated_text)

```

The pipeline uses the generate() method behind the scenes, but uses some default generation keyword arguments which are documented here. I didn’t get equivalent results when using sampling (despite using `set\_seed).
