community[minor]: Add Ascend NPU optimized Embeddings (#20260)

- **Description:** Add NPU support for embeddings

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
yuncliu
2024-06-25 04:15:11 +08:00
committed by GitHub
parent 7b1066341b
commit 398b2b9c51
7 changed files with 335 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
# Ascend
>[Ascend](https://https://www.hiascend.com/) is Natural Process Unit provide by Huawei
This page covers how to use ascend NPU with LangChain.
### Installation
Install using torch-npu using:
```bash
pip install torch-npu
```
Please follow the installation instructions as specified below:
* Install CANN as shown [here](https://www.hiascend.com/document/detail/zh/canncommercial/700/quickstart/quickstart/quickstart_18_0002.html).
### Embedding Models
See a [usage example](/docs/integrations/text_embedding/ascend).
```python
from langchain_community.embeddings import AscendEmbeddings
```

View File

@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "a636f6f3-00d7-4248-8c36-3da51190e882",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-0.04053403 -0.05560051 -0.04385472 ... 0.09371872 0.02846981\n",
" -0.00576814]\n"
]
}
],
"source": [
"from langchain_community.embeddings import AscendEmbeddings\n",
"\n",
"model = AscendEmbeddings(\n",
" model_path=\"/root/.cache/modelscope/hub/yangjhchs/acge_text_embedding\",\n",
" device_id=0,\n",
" query_instruction=\"Represend this sentence for searching relevant passages: \",\n",
")\n",
"emb = model.embed_query(\"hellow\")\n",
"print(emb)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d29ddaa-eef3-4a4e-93d8-0f1c13525fb4",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.00348254 0.03098977 -0.00203087 ... 0.08492374 0.03970494\n",
" -0.03372753]\n",
" [-0.02198593 -0.01601127 0.00215684 ... 0.06065163 0.00126425\n",
" -0.03634358]]\n"
]
}
],
"source": [
"doc_embs = model.embed_documents(\n",
" [\"This is a content of the document\", \"This is another document\"]\n",
")\n",
"print(doc_embs)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "797a720d-c478-4254-be2c-975bc4529f57",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<coroutine object Embeddings.aembed_query at 0x7f9fac699cb0>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.aembed_query(\"hellow\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "57e62e53-4d2c-4532-9b77-a46bc3da1130",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.04053403, -0.05560051, -0.04385472, ..., 0.09371872,\n",
" 0.02846981, -0.00576814], dtype=float32)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await model.aembed_query(\"hellow\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7e260457-8b50-4ca3-8f76-8a76d8bba8c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<coroutine object Embeddings.aembed_documents at 0x7fa093ff1a80>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.aembed_documents(\n",
" [\"This is a content of the document\", \"This is another document\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ce954b94-aaac-4d2c-80be-b2988c16af6d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-0.00348254, 0.03098977, -0.00203087, ..., 0.08492374,\n",
" 0.03970494, -0.03372753],\n",
" [-0.02198593, -0.01601127, 0.00215684, ..., 0.06065163,\n",
" 0.00126425, -0.03634358]], dtype=float32)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await model.aembed_documents(\n",
" [\"This is a content of the document\", \"This is another document\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7823d69d-de79-4f95-90dd-38f4bdeb9bcc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}