|
| 1 | +# LitGPT High-level Python API |
| 2 | + |
| 3 | +This is a work-in-progress draft for a high-level LitGPT Pyhon API. |
| 4 | + |
| 5 | + |
| 6 | +## Model loading & saving |
| 7 | + |
| 8 | +The `LLM.load` command loads an `llm` object, which contains both the model object (a PyTorch module) and a preprocessor. |
| 9 | + |
| 10 | +```python |
| 11 | +from litgpt import LLM |
| 12 | + |
| 13 | +llm = LLM.load( |
| 14 | + source="url | local_path", |
| 15 | + # high-level user only needs to care about those: |
| 16 | + memory_reduction="none | medium | strong" |
| 17 | + # advanced options for technical users: |
| 18 | + hub="hf | local | other" |
| 19 | + quantize="bnb.nf4", |
| 20 | + precision="bf16-true", |
| 21 | + device=""auto | cuda | cpu", |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +Here, |
| 26 | + |
| 27 | +- `llm.model` contains the PyTorch Module |
| 28 | +- and `llm.preprocessor.tokenizer` contains the tokenizer |
| 29 | + |
| 30 | +The `llm.save` command saves the model weights, tokenizer, and configuration information. |
| 31 | + |
| 32 | + |
| 33 | +```python |
| 34 | +llm.save(checkpoint_dir, format="lightning | ollama | hf") |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## Inference / Chat |
| 40 | + |
| 41 | +``` |
| 42 | +response = llm.generate( |
| 43 | + prompt="What do Llamas eat?", |
| 44 | + temperature=0.1, |
| 45 | + top_p=0.8, |
| 46 | + ... |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Dataset |
| 53 | + |
| 54 | +The `llm.prepare_dataset` command prepares a dataset for training. |
| 55 | + |
| 56 | +``` |
| 57 | +llm.download_dataset( |
| 58 | + URL, |
| 59 | + ... |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | +``` |
| 64 | +dataset = llm.prepare_dataset( |
| 65 | + path, |
| 66 | + task="pretrain | instruction_finetune", |
| 67 | + test_portion=0.1, |
| 68 | + ... |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +## Training |
| 74 | + |
| 75 | + |
| 76 | +```python |
| 77 | +llm.instruction_finetune( |
| 78 | + config=None, |
| 79 | + dataset=dataset, |
| 80 | + max_iter=10, |
| 81 | + method="full | lora | adapter | adapter_v2" |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +```python |
| 86 | +llm.pretrain(config=None, dataset=dataset, max_iter=10, ...) |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +## Serving |
| 91 | + |
| 92 | + |
| 93 | +```python |
| 94 | +llm.serve(port=8000) |
| 95 | +``` |
| 96 | + |
| 97 | +Then in another Python session: |
| 98 | + |
| 99 | +```python |
| 100 | +import requests, json |
| 101 | + |
| 102 | +response = requests.post( |
| 103 | + "http://127.0.0.1:8000/predict", |
| 104 | + json={"prompt": "Fix typos in the following sentence: Exampel input"} |
| 105 | +) |
| 106 | + |
| 107 | +print(response.json()["output"]) |
| 108 | +``` |
0 commit comments