Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 132 additions & 22 deletions pages/advanced-algorithms/available-algorithms/embeddings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description: Calculate sentence embeddings on node strings using pytorch.

import { Cards } from 'nextra/components'
import GitHub from '/components/icons/GitHub'
import { Callout } from 'nextra/components'

The embeddings module provides tools for calculating sentence embeddings on node strings using pytorch.

Expand All @@ -27,38 +28,59 @@ The embeddings module provides tools for calculating sentence embeddings on node

## Procedures

### `compute()`
### `node_sentence()`

The procedure computes the sentence embeddings on the string properties of nodes. Embeddings are
created as a property of the nodes in the graph.

{<h4 className="custom-header"> Input: </h4>}

- `input_nodes: List[Vertex]` (**OPTIONAL**) ➡ The list of nodes to compute the embeddings for. If not provided, the embeddings are computed for all nodes in the graph.
- `embedding_property: string` ➡ The name of the property to store the embeddings in. This property is `embedding` by default.
- `excluded_properties: List[string]` ➡ The list of properties to exclude from the embeddings computation. This list is empty by default.
- `model_name: string` ➡ The name of the model to use for the embeddings computation, buy default this module uses the `all-MiniLM-L6-v2` model provided by the `sentence-transformers` library.
- `batch_size: int` ➡ The batch size to use for the embeddings computation. This is set to `2000` by default.
- `chunk_size: int` ➡ The number of batches per "chunk". This is used when computing embeddings across multiple GPUs, as this has to be done by spawning multiple processes. Each spawned process computes the embeddings for a single chunk. This is set to 48 by default.
- `device: string|int|List[string|int]` ➡ The device to use for the embeddings computation. This can be any of the following:
- `configuration`: (`mgp.Map`, **OPTIONAL**): User defined parameters from query module. Defaults to `{}`.

**Configuration options:**

| Name | Type | Default | Description |
|----------------------------|--------------|-------------------|----------------------------------------------------------------------------------------------------------|
| `embedding_property` | string | `"embedding"` | The name of the property to store the embeddings in. |
| `excluded_properties` | List[string] | `[]` | The list of properties to exclude from the embeddings computation. |
| `model_name` | string | `"all-MiniLM-L6-v2"` | The name of the model to use for the embeddings computation, provided by the `sentence-transformers` library. |
| `return_embeddings` | bool | `False` | Whether to return the embeddings as an additional output or not. |
| `batch_size` | int | `2000` | The batch size to use for the embeddings computation. |
| `chunk_size` | int | `48` | The number of batches per "chunk". This is used when computing embeddings across multiple GPUs, as this has to be done by spawning multiple processes. Each spawned process computes the embeddings for a single chunk. |
| `device` | NULL\|string\| int\|List[string\|int] | `NULL` | The device to use for the embeddings computation (see below). |

<Callout type="info">
The `device` parameter can be one of the following:
- `NULL` (default) - Use first GPU if available, otherwise use CPU.
- `"cpu"` - Use CPU for computation.
- `"cuda"` or `"all"` - Use all available CUDA devices for computation.
- `"cuda:id"` - Use a specific CUDA device for computation.
- `id` - Use a specific device for computation.
- `[id1, id2, ...]` - Use a list of device ids for computation.
- `["cuda:id1", "cuda:id2", ...]` - Use a list of CUDA devices for computation.
by default, the first device (`0`) is used.

**Note**: If you're running on a GPU device, make sure to start your container
with the `--gpus=all` flag.
For more details, see the [Install MAGE
documentation](/advanced-algorithms/install-mage).
</Callout>


{<h4 className="custom-header"> Output: </h4>}

- `success: bool` ➡ Whether the embeddings computation was successful.
- `embeddings: List[List[float]]|NULL` ➡ The list of embeddings. Only returned if the
`return_embeddings` parameter is set to `true` in the configuration, otherwise `NULL`.
- `dimension: int` ➡ The dimension of the embeddings.

{<h4 className="custom-header"> Usage: </h4>}

To compute the embeddings across the entire graph with the default parameters, use the following query:
To compute the embeddings across the entire graph with the default parameters,
use the following query:

```cypher
CALL embeddings.compute()
CALL embeddings.node_sentence()
YIELD success;
```

Expand All @@ -70,25 +92,79 @@ MATCH (n)
WITH n ORDER BY id(n)
LIMIT 5
WITH collect(n) AS subset
CALL embeddings.compute(subset)
CALL embeddings.node_sentence(subset)
YIELD success;
```

To run the computation on specific device(s), use the following query:

```cypher
CALL embeddings.compute(
NULL,
"embedding",
NULL,
"all-MiniLM-L6-v2",
2000,
48,
"cuda:1"
)
WITH {device: "cuda:1"} AS configuration
CALL embeddings.node_sentence(NULL, configuration)
YIELD success;
```

To return the embeddings as an additional output, use the following query:

```cypher
WITH {return_embeddings: True} AS configuration
CALL embeddings.node_sentence(NULL, configuration)
YIELD success, embeddings;
```


### `text()`

This procedure can be used to return a list of embeddings when given a list of strings.

{<h4 className="custom-header"> Input: </h4>}

- `strings: List[string]` ➡ The list of strings to compute the embeddings for.
- `configuration: mgp.Map` (**OPTIONAL**) ➡ User defined parameters from query module. Defaults to `{}`.

**Configuration options:**

| Name | Type | Default | Description |
|----------------------------|--------------|-------------------|----------------------------------------------------------------------------------------------------------|
| `model_name` | string | `"all-MiniLM-L6-v2"` | The name of the model to use for the embeddings computation, provided by the `sentence-transformers` library. |
| `batch_size` | int | `2000` | The batch size to use for the embeddings computation. |
| `chunk_size` | int | `48` | The number of batches per "chunk". This is used when computing embeddings across multiple GPUs, as this has to be done by spawning multiple processes. Each spawned process computes the embeddings for a single chunk. |
| `device` | NULL\|string\| int\|List[string\|int] | `NULL` | The device to use for the embeddings computation. |


{<h4 className="custom-header"> Output: </h4>}

- `success: bool` ➡ Whether the embeddings computation was successful.
- `embeddings: List[List[float]]` ➡ The list of embeddings.
- `dimension: int` ➡ The dimension of the embeddings.

{<h4 className="custom-header"> Usage: </h4>}

To compute the embeddings for a list of strings, use the following query:

```cypher
CALL embeddings.text(["Hello", "World"])
YIELD success, embeddings;
```

### `model_info()`

The procedure returns the information about the model used for the embeddings computation.

{<h4 className="custom-header"> Input: </h4>}

- `configuration: mgp.Map` (**OPTIONAL**) ➡ User defined parameters from query module. Defaults to `{}`.
The key `model_name` is used to specify the name of the model to use for the embeddings computation.

{<h4 className="custom-header"> Output: </h4>}

- `model_info: mgp.Map` ➡ The information about the model used for the embeddings computation.

| Name | Type | Default | Description |
|----------------------------|--------------|-------------------|----------------------------------------------------------------------------------------------------------|
| `model_name` | string | `"all-MiniLM-L6-v2"` | The name of the model to use for the embeddings computation, provided by the `sentence-transformers` library. |
| `dimension` | int | `384` | The dimension of the embeddings. |
| `max_seq_length` | int | `256` | The maximum sequence length. |

## Example

Expand All @@ -106,7 +182,7 @@ CREATE (a:Node {id: 1, Title: "Stilton", Description: "A stinky cheese from the
Run the following query to compute the embeddings:

```cypher
CALL embeddings.compute()
CALL embeddings.node_sentence()
YIELD success;

MATCH (n)
Expand All @@ -132,4 +208,38 @@ Results:
| "Parmesan" | [-0.0755439, 0.00906182, -0.010977, 0.0208911, -0.0527448, 0.0085... |
| "Red Leicester" | [-0.0244318, -0.0280038, -0.0373183, 0.0284436, -0.0277753, 0.066... |
+----------------------------------------------------------------------+----------------------------------------------------------------------+
```
```

To compute the embeddings for a list of strings, use the following query:

```cypher
CALL embeddings.text(["Hello", "World"])
YIELD success, embeddings;
```

Results:

```plaintext
+----------------------------------------------------------+----------------------------------------------------------------------------------+
| success | embeddings |
+----------------------------------------------------------+----------------------------------------------------------------------------------+
| true | [[-0.0627718, 0.0549588, 0.0521648, 0.08579, -0.0827489, -0.074573, 0.0685547... |
+----------------------------------------------------------+----------------------------------------------------------------------------------+
```

To get the information about the model used for the embeddings computation, use the following query:

```cypher
CALL embeddings.model_info()
YIELD info;
```

Results:

```plaintext
+----------------------------------------------------------------------------+
| info |
+----------------------------------------------------------------------------+
| {dimension: 384, max_sequence_length: 256, model_name: "all-MiniLM-L6-v2"} |
+----------------------------------------------------------------------------+
```
3 changes: 3 additions & 0 deletions pages/advanced-algorithms/install-mage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ The following tags are available on Docker Hub:
- `x.y-relwithdebinfo-cuda` - Memgraph built with CUDA support* - available since version `3.6.1`.

*To run GPU-accelerated algorithms, you need to launch the container with the `--gpus all` flag.
This requires the installation of NVIDIA Container Toolkit. See the
[NVIDIA Container Toolkit documentation](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
for more details.

For versions prior to `3.2`, MAGE image tags included both MAGE and Memgraph versions, e.g.

Expand Down