

# Trace lineage from data to AI

With Amazon SageMaker AI lineage tracking, Amazon SageMaker AI sends a lineage record to AWS Glue Data Catalog for each job, capturing its inputs and outputs. AWS Glue Data Catalog combines these records with lineage across AWS resources into a single graph, including column-level lineage from source tables to feature group features in OpenLineage format. You can trace a model back to the data that trained it, or trace forward from a table to the models that depend on it. You can view lineage on the detail page of each asset in the catalog in Amazon SageMaker AI Studio.

Use lineage to answer questions such as the following:
+ Which datasets, feature groups, and tables trained this model version?
+ Which models are affected if a column in a source table changes?
+ Which jobs read from a table, and which identity ran them?

For more information, see the following topics:
+ For the structure of the OpenLineage facets that Amazon SageMaker AI emits, see [Lineage tracking OpenLineage facets](lineage-tracking-ol-facets.md).
+ For how AWS Glue Data Catalog builds the lineage graph across AWS resources, see [Data lineage in AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/catalog-lineage-what-is.html).
+ For how ML lineage works in Amazon SageMaker AI, see [Amazon SageMaker AI ML lineage tracking](https://docs.aws.amazon.com/sagemaker/latest/dg/lineage-tracking.html).

## Examples of mapping your datasets to model development
<a name="catalog-assets-lineage-example"></a>

The following examples show how to record the data that produced a model or a feature group, so that it appears in the lineage graph.

### Managed MLflow
<a name="catalog-assets-lineage-mlflow"></a>

Amazon SageMaker AI managed MLflow integrates with Amazon SageMaker Model Registry. When automatic model registration is turned on for your MLflow app, each model that you register in MLflow is synced into Amazon SageMaker Model Registry as a model package version, along with lineage that links the model package to the MLflow run that produced it. For more information about automatic model registration, see [Automatically register Amazon SageMaker AI models with Model Registry](https://docs.aws.amazon.com/sagemaker/latest/dg/mlflow-track-experiments-model-registration.html).

Feature groups and hub datasets can also be synced as part of that lineage. You declare them as data inputs for a logged model with `log_lineage_inputs`, then register the model as usual. This call doesn't make any AWS calls. It saves your declaration as an artifact on the model, and Amazon SageMaker AI resolves it when you register the model.


**Lineage input types for a logged model**  

| Type | Required field | Optional field | 
| --- | --- | --- | 
| FeatureGroup | `Arn`: the Amazon SageMaker Feature Store feature group ARN | `SnapshotId`: the Iceberg snapshot ID of the data that you used | 
| HubDataSet | `Arn`: the versioned Amazon SageMaker AI hub dataset ARN | `QueryString`: the query used to select the data | 

The following Python example declares a feature group and a hub dataset as lineage inputs, then registers the model.

```python
import mlflow
from sagemaker_mlflow import log_lineage_inputs

mlflow.set_tracking_uri("arn:aws:sagemaker:us-west-2:<account-id>:mlflow-app/<app-id>")

with mlflow.start_run():
    info = mlflow.sklearn.log_model(model, name="model")

    log_lineage_inputs(
        info.model_id,
        lineage_inputs=[
            {
                "Type": "FeatureGroup",
                "Arn": "arn:aws:sagemaker:us-west-2:<account-id>:feature-group/<name>",
                "SnapshotId": "<iceberg-snapshot-id>",
            },
            {
                "Type": "HubDataSet",
                "Arn": "arn:aws:sagemaker:us-west-2:<account-id>:hub-content/<hub-name>/DataSet/<dataset-name>/<version>",
                "QueryString": "<athena-query-used-to-build-the-dataset>",
            },
        ],
    )

mlflow.register_model(info.model_uri, "<model-name>")
```

Note the following about MLflow lineage:
+ Lineage is recorded on the Amazon SageMaker AI model package, so automatic model registration must be turned on for your MLflow app. This feature works only with MLflow apps, not MLflow tracking servers. For information about how to turn it on, see [Automatically register Amazon SageMaker AI models with Model Registry](https://docs.aws.amazon.com/sagemaker/latest/dg/mlflow-track-experiments-model-registration.html).
+ Call `log_lineage_inputs` before `register_model`. Lineage is created only when the model is registered. Declaring inputs for a model that you never register has no effect.
+ Your MLflow app's execution role needs Amazon SageMaker AI lineage permissions. At a minimum, it needs `sagemaker:CreateAction` and `sagemaker:AddAssociation`. Without them, lineage is skipped silently.
+ Install the latest `sagemaker-mlflow` plugin on the client. Your MLflow app must run version 3.16 or later.

### Amazon SageMaker Feature Store offline table
<a name="catalog-assets-lineage-feature-store"></a>

Amazon SageMaker Feature Store can record which data produced a feature group. When you create a Feature Processor pipeline, Feature Store creates Amazon SageMaker AI lineage that links the data source pipelines to the output feature group. You can view the data sources of a feature group on the **Lineage** tab of its asset detail page in Amazon SageMaker AI Studio, from the **Catalogs** page.

You declare data sources in the `inputs` parameter of the `@feature_processor` decorator, then create the pipeline with `to_pipeline`. You don't call a lineage API. Feature Store creates lineage from the inputs and output that you declare.


**Supported input types**  

| Type | How you declare it | 
| --- | --- | 
| Feature group | `FeatureGroupDataSource`: the feature group name or ARN | 
| Amazon S3 CSV or Parquet | `CSVDataSource` or `ParquetDataSource`: the Amazon S3 URI | 
| Custom data source | A subclass of `PySparkDataSource` that sets `data_source_name` and `data_source_unique_id` and implements `read_data` | 

The following Python example declares a feature group and an Amazon S3 CSV source as inputs, then creates the pipeline.

```python
from sagemaker.mlops.feature_store.feature_processor import (
    feature_processor,
    FeatureGroupDataSource,
    CSVDataSource,
    TransformationCode,
    to_pipeline,
)

@feature_processor(
    inputs=[
        FeatureGroupDataSource("arn:aws:sagemaker:us-west-2:<account-id>:feature-group/<input-name>"),
        CSVDataSource("s3://<bucket>/<prefix>/"),
    ],
    output="arn:aws:sagemaker:us-west-2:<account-id>:feature-group/<output-name>",
)
def transform(fg_df, csv_df):
    return fg_df.join(csv_df, on="<record-id>")

to_pipeline(
    pipeline_name="<pipeline-name>",
    step=transform,
    transformation_code=TransformationCode(s3_uri="s3://<bucket>/<path-to-code>"),
)
```

Note the following about Feature Processor lineage:
+ Lineage is recorded when you create or update the pipeline with `to_pipeline`. To also track your transformation code and its versions, pass `transformation_code`.
+ A feature group input must have an offline store. If the feature group has only an online store, you get a validation error.
+ To read from AWS Glue Data Catalog tables, Amazon Athena, or other Amazon S3 formats, write a custom data source. Custom sources that share a `data_source_unique_id` appear as one data source in lineage.
+ Each pipeline uses at least two lineage contexts, which count toward your lineage quotas. For more information, see [Feature Processor restrictions, limits, and quotas](https://docs.aws.amazon.com/sagemaker/latest/dg/feature-store-feature-processor-quotas.html).

Feature Store also records lineage for a feature group's offline store beyond the Feature Processor pipeline that created it. It links the feature group to the S3 location and the AWS Glue table that back its offline store. If a job then reads from that S3 location or table — for example, an AWS Glue ETL job — and produces a new dataset, AWS Glue Data Catalog adds that job and its output to the same lineage graph. You can trace a feature group through to any table or file that a downstream job produced from it, on the **Lineage** tab of the feature group's asset detail page in Amazon SageMaker AI Studio.

Lineage also tracks individual features, not just the feature group as a whole. Every node in the lineage graph — the feature group, its S3 location, and its Glue table — lists the same features, matching the feature group's feature definitions. When a downstream job transforms a feature, such as normalizing or bucketing a value, AWS Glue Data Catalog records which output column came from which input feature. This gives you an unbroken column-level path from a feature in the feature group to the column it produced in a transformed dataset, even across hops where no transformation happens. On the **Lineage** tab of an asset's detail page, you can select a column to see its upstream and downstream columns.