Files
Xenith/wiki/03-python.md
2026-11-03 20:29:35 +07:00

93 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 03. Python
## Установка
```bash
pip install xenith # wheel с PyPI
# или из исходников:
pip install .
```
`pip` собирает C++-модуль через `pyproject.toml`/scikit-build-core
(CPU, без Vulkan). Для GPU-сборки соберите проект CMake с `-DXENITH_VULKAN=ON`
и используйте `PYTHONPATH=build`.
## Импорт
```python
import xenith
print(xenith.version(), xenith.backend_name())
xenith.init_vulkan() # {"ok": ..., "error": ..., "device": ...}; иначе CPU
```
## Модель, обучение, генерация
```python
text = open("corpus.txt", encoding="utf-8").read()
m = xenith.Model(vocab=2048, embd=128, layers=4, heads=4, ctx=256,
corpus_text=text)
st = m.train(corpus_text=text, steps=1000, batch=8, block=64, lr=3e-4)
print(st.step, st.loss)
print(m.generate_text("однажды", n=64, temperature=0.8))
r = m.chat("привет", n=64) # история хранится в модели
print(r.text)
m.save("model.xnh")
```
Фоновая тренировка и опрос статуса:
```python
state = m.train_async(corpus_text=text, steps=50000)
while state.is_running:
print(state.step, state.total, state.loss)
m.wait()
```
## Загрузка и mmap
```python
m = xenith.Model.load("model.xnh") # в RAM
m = xenith.Model.load("model.xnh", mmap=True) # веса из файла (COW)
m.detach() # скопировать в RAM и закрыть mmap
```
## numpy-веса (zero-copy)
```python
w = m.tensor("wte") # numpy-представление, правка in-place допустима
w[0, 0] += 0.1
m.touch_tensors() # сообщить движку, что host-данные изменились
m.set_tensor("wte", new_array)
```
## GGUF и конвертация
```python
xenith.convert_model("model.xnh", "model.q4_0.gguf", quant="q4_0")
print(xenith.gguf_dump("model.q4_0.gguf"))
print(xenith.probe("model.xnh"))
```
## Логи и MoE
```python
xenith.set_log_callback(lambda lvl, msg: print(lvl, msg))
brain = xenith.MoEBrain(...) # обучаемый MoE агента
```
## Консольная команда из пакета
```bash
xenith version
xenith train --corpus data.txt --steps 2000 --out model.xnh
xenith generate model.xnh "Однажды" --n 64
xenith chat model.xnh
xenith convert model.xnh model.q4_0.gguf --quant q4_0
xenith devices
```
Полное API — [docs/06-python-api.md](../docs/06-python-api.md).