IndexError: list index out of range in encode function inside SentenceTransformer.py
Author: mali404
Pull request: no

### System Info

- `transformers` version: 4.32.1
- Platform: Windows-10-10.0.22621-SP0
- Python version: 3.11.4
- Huggingface_hub version: 0.16.4
- Safetensors version: 0.3.2
- Accelerate version: not installed
- Accelerate config: not found
- PyTorch version (GPU?): 2.0.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

 @ArthurZucker

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

Steps to reproduce:

1. 
```ruby
#####BGE_LARGE####
#https://github.com/hwchase17/chroma-langchain/blob/master/persistent-qa.ipynb
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceBgeEmbeddings

persist_directory = r'C:\Study\XXX\Code\Data\chroma\bge_large'

#defining embedding model
model_name = "BAAI/bge-large-en-v1.5"
model_kwargs = {'device': 'cuda'}
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
embedding_function = HuggingFaceBgeEmbeddings(
    model_name=model_name,
    cache_folder=r'C:\Study\XXX\Code\Data\chroma\models_cached',
    model_kwargs=model_kwargs,
    encode_kwargs=encode_kwargs 
)

#creating vector embeddings and storing them locally
vectordb = Chroma.from_texts(texts=chunks_redone, embedding=embedding_function, collection_name='iit_Bulletin', persist_directory=persist_directory)
```

2.  Default settings for batch_size = 32. Using that, following error occurred: **IndexError:**  list index out of range @ line 192 of SentenceTransformers.py () 
```ruby
        all_embeddings = [all_embeddings[idx] for idx in np.argsort(length_sorted_idx)]

```
3. This error happened here because all_embeddings list has multiple times fewer elements than length_sorted_idx list.
This error was removed by changing batch_size from 32 to 1. Because it was observed in two cases that IndexError was coming at a point when len(all_embeddings) was approximately equal to len(length_sorted_idx)/{batch_size}. Changing the batch size to 1 ensured that len(all_embeddings) = len(length_sorted_idx)/{batch_size}


### Expected behavior

1. I would expect the **_encode_** function to effectively handle parallel processes when batch_size>1. Especially so while storing the embeddings into all_embedding list.  
2. Disclaimer: I have only shared the _symptoms_ of the matter. I could not figure out its _root cause_.

Comments:
Hey! Not sure I understand if this is related to `transformers` or langchain. If it's indeed a bug in transformers could  you share a minimal reproducer? Isolating the bug

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.