> This page is for Structured Data Models.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/sdgm/llms.txt.

# Run Kumo Relational

> Describe related tables with the SDM library, run Kumo Relational, sample real data, and tune accuracy.

Use `sdm.models.KumoRelational` to predict a target for task rows from the related tables they link to.
For what the model is and how it works, see [Kumo Relational](/sdgm/structured-data-models/models/kumo-relational).

| Property     | Value                                                                                      |
| ------------ | ------------------------------------------------------------------------------------------ |
| Class        | `sdm.models.KumoRelational`                                                                |
| Inputs       | Task rows (`x_context`, `y_context`, `x_query`) plus `RelatedTables` for context and query |
| Available in | SDM 0.2.1 and later                                                                        |

## Describe Your Tables

A `RelatedTables` object has three parts:

* `tables`: a name for each related table and its `TableTensor`.
* `relationships`: key joins between tables. Single or composite keys.
* `task_links`: exactly one entry point from task rows into a related table.

Give key columns the `id` semantic type. They define relationships and are not used as features.

## Example

This toy example predicts churn for two users from two labeled users and their orders.

```python
import torch
import sdm

task_table = sdm.TableTensor.from_columns(
    {"user_id": [0, 1, 2, 3], "churn": [True, False, True, False]},
    stypes={"user_id": "id", "churn": "categorical"},
)
related = sdm.RelatedTables(
    tables={
        "users": sdm.TableTensor.from_columns(
            {"user_id": [0, 1, 2, 3], "age": [42, 23, 31, 26]},
            stypes={"user_id": "id", "age": "numerical"},
        ),
        "orders": sdm.TableTensor.from_columns(
            {"user_id": [0, 0, 1, 3, 3, 3],
             "amount": [9.99, 4.99, 12.99, 7.99, 3.99, 5.99]},
            stypes={"user_id": "id", "amount": "numerical"},
        ),
    },
    relationships=[{"left_table": "orders", "left_columns": "user_id",
                    "right_table": "users", "right_columns": "user_id"}],
    task_links=[{"task_columns": "user_id",
                 "table": "users", "table_columns": "user_id"}],
)

# Context rows (users 0, 1) carry labels; query rows (users 2, 3) do not.
related_context = related.replace_tables({
    "users": related.tables["users"][:2], "orders": related.tables["orders"][:3]})
related_query = related.replace_tables({
    "users": related.tables["users"][2:], "orders": related.tables["orders"][3:]})

model = sdm.models.KumoRelational(task="classification")
probs = model(
    x_context=task_table[:2].drop_columns("churn"),
    y_context=task_table[:2, "churn"],
    x_query=task_table[2:].drop_columns("churn"),
    related_context_tables=related_context,
    related_query_tables=related_query,
    num_hops=1,
    generator=torch.Generator().manual_seed(0),
)
print(probs.to_pandas())
```

**`Expected output`**

```text title="Expected output"
      False      True
0  0.321723  0.678277
1  0.203182  0.796818
```

With two labeled users, the probabilities only show that the pipeline runs.
Real tasks need many more context rows.

## Work with Real Data

For real datasets, use SDM's subgraph samplers to build `RelatedTables` for each batch of task rows, with CPU (`pyg-lib`) or CUDA (`cugraph`) backends.
When two task rows refer to the same entity at different times, add a task ID to composite keys so each row only sees records available at its prediction time.

The [RelBench example](https://github.com/NVIDIA/structured-data-models/tree/main/examples/relational/relbench) in the SDM repository shows this end to end.
From that example's directory, run:

```bash
python main.py --dataset=rel-f1 --task=driver-dnf
```

## Improve Accuracy

The model weights stay fixed, so accuracy depends on the choices you make at prediction time: which labeled rows go into the context, whether you average across several contexts, how the sampler selects related records, and how you turn the output into a decision.
These notebooks tune each choice on a validation window and measure its lift on a later test window, using synthetic banking data across five related tables:

* [Imbalanced classification](https://github.com/NVIDIA/kumo-engine/blob/main/examples/kumo-example-notebooks/structured-data-models/relational/cross-industry/notebooks/local_structured_data_models_imbalanced_classification_tuning.ipynb), with fraud detection as the example. Covers rebalancing the context, ensembling contexts, sampling settings, and the decision threshold.
* [Regression](https://github.com/NVIDIA/kumo-engine/blob/main/examples/kumo-example-notebooks/structured-data-models/relational/cross-industry/notebooks/local_structured_data_models_regression_tuning.ipynb), with 30 day spend forecasting as the example. Covers context composition, ensembling contexts, sampling settings, and which output quantile to report.

## Inputs and Limits

* Pass `related_context_tables` and `related_query_tables` together; the model requires related tables.
* Exactly one task link to an entity table.
* Tables must be two-dimensional.
* One target column per call.

For GPU memory, system RAM, and disk planning, see the [GPU Sizing Guide](/sdgm/structured-data-models/gpu-sizing#kumo-relational-choose-a-gpu).

## Learn More

* [KumoRelational API reference](https://nvidia.github.io/structured-data-models/api/generated/sdm.models.KumoRelational.html)
* [Relational context guide](https://nvidia.github.io/structured-data-models/icl.html#relational-context)