Skip to main content

💾 Load and save models

Load a pre-trained model

To load a pre-trained model for the first time, run the following line of code. This will load the model with the default weights.

from xturing.models.base import BaseModel

model = BaseModel.create("<model_key>")

For example,
model = BaseModel.create("llama_lora")

You can find all the supported model keys here.

Save a fine-tuned model

After fine-tuning your model, you can save it as simple as:"/path/to/a/directory")

Remember that the path that you specify should be a directory. If the directory doesn't exist, it will be created.

The model weights will be saved into 2 files. The whole model weights including based model parameters and LoRA parameters are stored in pytorch_model.bin file and only LoRA parameters are stored in adapter_model.bin file.

Load a model from local directory

To load a saved model, you only have to run the load method specifying the directory where the weights were saved.

model = BaseModel.load("/path/to/a/directory")
Sample code to load and save a model
from xturing.models.base import BaseModel

## Load the model
model = BaseModel.create("llama_lora")

# Save the model"/path/to/a/directory")

## Load the fine-tuned model
finetuned_model = BaseModel.load("/path/to/a/directory")