# Padding with sliding window

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpadding-with-sliding-window%2F18921%3E%5C
**Category:** 🤗Tokenizers\
**Created:** [June 9, 2022, 4:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpadding-with-sliding-window%2F18921 "2022-06-09T16:11:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davibarreira](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/davibarreira/32/5026_2.png) [@davibarreira](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fdavibarreira)%5C
**Post date:** [June 9, 2022, 4:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpadding-with-sliding-window%2F18921%2F1 "2022-06-09T16:11:12Z")

</div>

Hello, friends. I’m new to the forum, so if there is some issue with this question, please let me know.

So, I want to tokenize a long sequence, and I’m trying to use the sliding window option.

```auto
tokenizer = AutoTokenizer.from_pretrained('../../data/model/', do_lower_case=False)
tokenized_example = tokenizer(data[0], return_overflowing_tokens=True, max_length=512)

```

Everything worked fine, but I then wish to obtain the tensor for such encoding.

```auto
tokenized_example = tokenizer(data[0], return_overflowing_tokens=True, max_length=512, return_tensors="pt")

```

Now, this returns an error, because the last window is not padded, hence, instead of 512 tokens, it has 490. Ok, so I need padding, but

```auto
tokenized_example = tokenizer(data[0], padding='max_length', return_overflowing_tokens=True, max_length=512, return_tensors="pt")

```

The code above does not work, because it seems that the window sliding is not sliding when we have the padding activated. Hence, instead of, say, 12 tensors of 512, I have only one tensor of 6000.

How can one circumvent this?

---

<div class="post-metadata">

**Author:** ![lucasresck](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lucasresck/32/3260_2.png) [@lucasresck](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flucasresck)%5C
**Post date:** [September 3, 2022, 9:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpadding-with-sliding-window%2F18921%2F2 "2022-09-03T21:45:37Z")

</div>

Opa, e aí, Davi?

I also had an undesirable tensor shape with these arguments:

```auto
tokenized = tokenizer(
    "test "*1000,
    return_overflowing_tokens=True,
    max_length=512,
    return_tensors='pt',
    padding='max_length',
)
print(tokenized['input_ids'].shape)
>> torch.Size([1, 2002])

```

Try explicitly activating the `truncation`:

```auto
tokenized = tokenizer(
    "Test "*1000,
    return_overflowing_tokens=True,
    max_length=512,
    return_tensors='pt',
    padding='max_length',
    truncation=True,
)
print(tokenized['input_ids'].shape)
>> torch.Size([4, 512])

```

To fully benefit from a sliding window, also try the `stride` parameter. It controls the overlap between two consecutive “windows.”
