fix "UserWarning: Creating a tensor from a list of numpy.ndarrays is …
Author: liucw2012
Pull request: yes

fix "UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor."

# What does this PR do?

reduce latency of codes below from 0.744675874710083s to 0.013312816619873047s.

Fixes #24764 


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker



Comments:
_The documentation is not available anymore as the PR was closed or merged._

@ArthurZucker   Do I need to make any changes to this PR? What's the next step?

@ydshieh  could review this pr?

Seems to have ~30x speed up.

```python
import numpy as np
import torch

import time

def measure(batch_size, seq_len):

    a = np.ones(shape=(batch_size, seq_len, 16))
    # A list of numpy arrary
    b = [x for x in a]

    # directly to torch.tensor
    st = time.time()
    c = torch.tensor(b)
    t1 = time.time() - st

    # np -> tensor
    st = time.time()
    d = np.array(b)
    e = torch.tensor(d)
    t2 = time.time() - st

    print(f"batch_size: {batch_size} | seq_len: {seq_len} | main: {t1} sec. | PR: {t2} sec.")


batch_size = 128
seq_len = 32
for idx in range(10):
    batch_size = batch_size * 2
    measure(batch_size, seq_len)


batch_size = 128
seq_len = 256
for idx in range(8):
    batch_size = batch_size * 2
    measure(batch_size, seq_len)
```


results:

```bash
batch_size: 256 | seq_len: 32 | main: 0.010269403457641602 sec. | PR: 0.002008676528930664 sec.
batch_size: 512 | seq_len: 32 | main: 0.015998125076293945 sec. | PR: 0.0010027885437011719 sec.
batch_size: 1024 | seq_len: 32 | main: 0.03223681449890137 sec. | PR: 0.0019538402557373047 sec.
batch_size: 2048 | seq_len: 32 | main: 0.0663607120513916 sec. | PR: 0.004067182540893555 sec.
batch_size: 4096 | seq_len: 32 | main: 0.13183259963989258 sec. | PR: 0.0060040950775146484 sec.
batch_size: 8192 | seq_len: 32 | main: 0.26061558723449707 sec. | PR: 0.011055707931518555 sec.
batch_size: 16384 | seq_len: 32 | main: 0.5237565040588379 sec. | PR: 0.02300405502319336 sec.
batch_size: 32768 | seq_len: 32 | main: 1.0568530559539795 sec. | PR: 0.041966915130615234 sec.
batch_size: 65536 | seq_len: 32 | main: 2.0813064575195312 sec. | PR: 0.0868995189666748 sec.
batch_size: 131072 | seq_len: 32 | main: 4.243735074996948 sec. | PR: 0.17353129386901855 sec.
```

```bash

batch_size: 256 | seq_len: 256 | main: 0.06456398963928223 sec. | PR: 0.0034742355346679688 sec.
batch_size: 512 | seq_len: 256 | main: 0.12811279296875 sec. | PR: 0.005001068115234375 sec.
batch_size: 1024 | seq_len: 256 | main: 0.26175403594970703 sec. | PR: 0.010001659393310547 sec.
batch_size: 2048 | seq_len: 256 | main: 0.5197086334228516 sec. | PR: 0.019011259078979492 sec.
batch_size: 4096 | seq_len: 256 | main: 1.040560245513916 sec. | PR: 0.03655409812927246 sec.
batch_size: 8192 | seq_len: 256 | main: 2.089771032333374 sec. | PR: 0.07351517677307129 sec.
batch_size: 16384 | seq_len: 256 | main: 4.197775602340698 sec. | PR: 0.1453232765197754 sec.
batch_size: 32768 | seq_len: 256 | main: 8.368194103240967 sec. | PR: 0.36582493782043457 sec.
```

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_24772). All of your documentation changes will be reflected on that endpoint.