> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-1761.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Forking a W&B run

# Fork a run

<Warning>
  The ability to fork a run is in private preview. Contact W\&B Support at [support@wandb.com](mailto:support@wandb.com) to request access to this feature.
</Warning>

Use `fork_from` when you initialize a run with [`wandb.init()`](/models/ref/python/functions/init) to "fork" from an existing W\&B run. When you fork from a run, W\&B creates a new run using the `run ID` and `step` of the source run.

Forking a run enables you to explore different parameters or models from a specific point in an experiment without impacting the original run.

<Note>
  * Forking a run requires [`wandb`](https://pypi.org/project/wandb/) SDK version >= 0.16.5
  * Forking a run requires monotonically increasing steps. You can not use non-monotonic steps defined with [`define_metric()`](/models/ref/python/experiments/run#define_metric) to set a fork point because it would disrupt the essential chronological order of run history and system metrics.
</Note>

## Start a forked run

To fork a run, use the `fork_from` argument in [`wandb.init()`](/models/ref/python/functions/init) and specify the source `run ID` and the `step` from the source run to fork from:

```python theme={null}
import wandb

# Initialize a run to be forked later
with wandb.init(
    project="your_project_name",
    entity="your_entity_name"
) as original_run:
    # ... perform training or logging ...
    pass

# Fork the run from a specific step
with wandb.init(
    project="your_project_name",
    entity="your_entity_name",
    fork_from=f"{original_run.id}?_step=200",
) as forked_run:
    pass
```

## Continue from a forked run

After initializing a forked run, you can continue logging to the new run. You can log the same metrics for continuity and introduce new metrics.

For example, the following code example shows how to first fork a run and then how to log metrics to the forked run starting from a training step of 200:

```python theme={null}
import wandb
import math

# Initialize the first run and log some metrics
with wandb.init(
    project="your_project_name",
    entity="your_entity_name"
) as run1:
    for i in range(300):
        run1.log({"metric": i})

# Fork from the first run at a specific step and log the
# metric starting from step 200
with wandb.init(
    project="your_project_name", 
    entity="your_entity_name", 
    fork_from=f"{run1.id}?_step=200"
) as run2:
    # Continue logging in the new run
    # For the first few steps, log the metric as is from run1
    # After step 250, start logging the spikey pattern
    for i in range(200, 300):
        if i < 250:
            # Continue logging from run1 without spikes
            metric_value = i
        else:
            # Introduce the spikey behavior starting from step 250
            metric_value = i + (2 * math.sin(i / 3.0))  # Apply a subtle spikey pattern

        # Log both metrics in a single call to ensure they're
        # logged at the same step
        run2.log({
            "metric": metric_value,
            "additional_metric": i * 1.1
        })
```

<Note>
  **Rewind and forking compatibility**

  Forking compliments a [`rewind`](/models/runs/rewind/) by providing more flexibility in managing and experimenting with your runs.

  When you fork from a run, W\&B creates a new branch off a run at a specific point to try different parameters or models.

  When you  rewind a run, W\&B let's you correct or modify the run history itself.
</Note>
