rpeft 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +104 -3
- data/lib/rpeft/backend/python.rb +41 -4
- data/lib/rpeft/backend.rb +2 -1
- data/lib/rpeft/cli/doctor.rb +18 -1
- data/lib/rpeft/cli/export_gguf.rb +51 -0
- data/lib/rpeft/cli.rb +25 -0
- data/lib/rpeft/diagnosis.rb +2 -1
- data/lib/rpeft/errors.rb +3 -0
- data/lib/rpeft/gguf.rb +353 -0
- data/lib/rpeft/lora/progress.rb +21 -0
- data/lib/rpeft/lora.rb +13 -5
- data/lib/rpeft/model.rb +9 -0
- data/lib/rpeft/version.rb +1 -1
- data/lib/rpeft.rb +2 -0
- data/python/rpeft_backend/__init__.py +6 -4
- data/python/rpeft_backend/_chat.py +1 -2
- data/python/rpeft_backend/_training.py +57 -3
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bc9adf5a2aa075428b4fcf4824f73e642db324bed5cb9bf7d9e64e27b05b6ba4
|
|
4
|
+
data.tar.gz: 737d95c6221fc21aea6eb47785d1a2cd2825904880b8dfd620740c61a451d6cf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a798171b54141af44ce5d1bbc88ae0d32282323a1924385f343424eec0e8b2cb49adf49c0073244e30e432cbbbc7d0c30ca559dc94999232b8f1d6a5e5aba92d
|
|
7
|
+
data.tar.gz: 65beaa5212f4df284b5add8e93b98c8693c1aed4fe5fbdf2f75c751016d60795e8ff84a9b173201f9d7439a3b6aaf20dd20b4ef7d33fc6b5e822b3f09f84f937
|
data/README.md
CHANGED
|
@@ -12,8 +12,10 @@ It is an independent project, not affiliated with Hugging Face.
|
|
|
12
12
|
- LoRA fine-tuning from JSONL files or Ruby Enumerables, in plain text, instruction, and chat formats
|
|
13
13
|
- Merging an adapter into its base model to produce a standalone model
|
|
14
14
|
- Generation and chat with a merged model, a Hugging Face model id, or an adapter on top of its base model
|
|
15
|
+
- GGUF export of a merged model for llama.cpp, Ollama, and other local runtimes
|
|
15
16
|
- Apple Silicon (MPS) as a first-class target, with CUDA and CPU auto-detected
|
|
16
17
|
- Validation loss, per-epoch checkpoints, and resuming
|
|
18
|
+
- Training progress through a block, and its history in the adapter metadata
|
|
17
19
|
- A CLI for training, merging, and generation, and `rpeft doctor` to check the Python environment
|
|
18
20
|
|
|
19
21
|
## The model behind the API
|
|
@@ -118,6 +120,28 @@ adapter.metadata.dig('training', 'eval_loss') # => 0.91
|
|
|
118
120
|
`adapter.merge` returns a `RPEFT::Model` with `path` and `base_model`. `save` moves the artifact to the given path; a destination produced by a previous `save` is replaced,
|
|
119
121
|
so scripts can be re-run, while any other existing path is refused.
|
|
120
122
|
|
|
123
|
+
### Progress
|
|
124
|
+
|
|
125
|
+
A block given to `RPEFT::LoRA.train` is called as the run goes, with a `RPEFT::LoRA::Progress` for every logged training step (every 5 steps)
|
|
126
|
+
and for the evaluation at the end of each epoch:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
adapter = RPEFT::LoRA.train(model: 'Qwen/Qwen2.5-Coder-0.5B', dataset: 'ruby_answers.jsonl') do |progress|
|
|
130
|
+
puts "step #{progress.step}/#{progress.total_steps} loss #{progress.loss}"
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
A training step carries `loss` and `learning_rate`, an evaluation carries `eval_loss`, and both carry `step`, `total_steps`, and `epoch`; what a step lacks is `nil`.
|
|
135
|
+
An exception raised in the block ends the run, saves no adapter, and is raised from `train`. Ctrl-C ends a run the same way, at the next logged step.
|
|
136
|
+
`break` and `throw` cannot leave the block, which is called from Python: they end the run with `RPEFT::TrainingError`, so raise an exception to stop early.
|
|
137
|
+
|
|
138
|
+
The same steps are kept with or without a block, so runs can be compared afterwards:
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
adapter.metadata.dig('training', 'history')
|
|
142
|
+
# => [{"step" => 5, "epoch" => 0.5, "loss" => 1.81, "learning_rate" => 0.00019}, ...]
|
|
143
|
+
```
|
|
144
|
+
|
|
121
145
|
### Generation
|
|
122
146
|
|
|
123
147
|
`RPEFT::Model#generate` produces a completion for a prompt. It uses greedy decoding, so the output is deterministic for a given model and prompt.
|
|
@@ -211,6 +235,13 @@ Lines with `instruction`/`output` keys (and an optional `input`) are also accept
|
|
|
211
235
|
{"instruction":"Explain what Array#map does","output":"It returns a new array with the block applied to each element."}
|
|
212
236
|
```
|
|
213
237
|
|
|
238
|
+
The prompt is the instruction under a `### Instruction:` heading, the input, if any, under `### Input:`, and a `### Response:` heading that the output follows.
|
|
239
|
+
A model trained this way expects the same prompt at inference, up to and including the line break after `### Response:`:
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
adapter.generate("### Instruction:\nExplain what Array#map does\n\n### Response:\n")
|
|
243
|
+
```
|
|
244
|
+
|
|
214
245
|
Chat transcripts are accepted as a `messages` array. These are rendered with the tokenizer's own chat template,
|
|
215
246
|
so the conversation is formatted the way the model expects. Roles are limited to `system`, `user`, and `assistant`,
|
|
216
247
|
the ones every chat template understands:
|
|
@@ -228,7 +259,9 @@ Examples whose answer does not fit within `max_length:` are skipped with a warni
|
|
|
228
259
|
```ruby
|
|
229
260
|
RPEFT::LoRA.train(
|
|
230
261
|
model: 'Qwen/Qwen2.5-Coder-0.5B',
|
|
231
|
-
dataset:
|
|
262
|
+
dataset: CodingExample.where(language: 'Ruby').map { |example|
|
|
263
|
+
{instruction: example.prompt, output: example.completion}
|
|
264
|
+
}
|
|
232
265
|
)
|
|
233
266
|
```
|
|
234
267
|
|
|
@@ -248,6 +281,59 @@ Apple Silicon Macs (M1 and later) are a first-class target: small models such as
|
|
|
248
281
|
Training uses float32 on MPS (half precision there is numerically fragile), bfloat16 on capable CUDA GPUs, and `PYTORCH_ENABLE_MPS_FALLBACK=1` is set automatically for
|
|
249
282
|
operators MPS does not implement. Merging always runs on CPU, which is the memory-safest place for it.
|
|
250
283
|
|
|
284
|
+
### Exporting to GGUF
|
|
285
|
+
|
|
286
|
+
A merged model can be written to a single GGUF file for llama.cpp, Ollama, and other local runtimes.
|
|
287
|
+
The conversion is done by llama.cpp's own `convert_hf_to_gguf.py`, which is not published as a package, so RPEFT needs a llama.cpp checkout:
|
|
288
|
+
|
|
289
|
+
```console
|
|
290
|
+
$ git clone https://github.com/ggml-org/llama.cpp ~/src/llama.cpp
|
|
291
|
+
$ .venv/bin/pip install sentencepiece # or, with uv: uv pip install sentencepiece
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
model = adapter.merge(output_dir: './ruby-model')
|
|
296
|
+
model.export_gguf('./ruby-model.gguf', llama_cpp: '~/src/llama.cpp') # => "/path/to/ruby-model.gguf"
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
`llama_cpp:` defaults to the `LLAMA_CPP_DIR` environment variable. The converter runs in its own process with the Python RPEFT uses,
|
|
300
|
+
so the packages it needs beyond RPEFT's own go into the same environment. `sentencepiece` is one of them even for models without
|
|
301
|
+
a SentencePiece tokenizer, such as Qwen2.5, because the converter imports it before looking for one.
|
|
302
|
+
|
|
303
|
+
`quantize:` picks the type of the weights. `:q8_0` (the default), `:f16`, and `:bf16` are written by the converter directly.
|
|
304
|
+
`:q6_k`, `:q5_k_m`, and `:q4_k_m` are smaller and also need `llama-quantize`, found on `PATH` (`brew install llama.cpp`), in the checkout's `build/bin`,
|
|
305
|
+
or given as `llama_quantize:`. Small models lose more to quantization, which is why the default stays at 8 bits.
|
|
306
|
+
|
|
307
|
+
Export a model you merged yourself, from a directory no one else writes to while the export runs; an adapter is merged first.
|
|
308
|
+
An existing output file is never overwritten, and a failed export leaves nothing behind.
|
|
309
|
+
|
|
310
|
+
A GGUF file carries the model's chat template, and Ollama (checked with 0.33.1) wraps every prompt in it, even when a Modelfile gives a `TEMPLATE` of its own.
|
|
311
|
+
A model fine-tuned on plain text, like the example above, expects the prompt as it was in training, so export it without the chat template:
|
|
312
|
+
|
|
313
|
+
```ruby
|
|
314
|
+
model.export_gguf('./ruby-model.gguf', chat_template: false, llama_cpp: '~/src/llama.cpp')
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
This removes the default chat template, the one Ollama used in that check, and the list of named ones; templates stored under their own names, such as one for tool use, stay.
|
|
318
|
+
Leaving it out uses `gguf_new_metadata.py` from the same checkout and briefly needs room for one more copy of the file.
|
|
319
|
+
Keep the chat template for a model fine-tuned on chat-format data: Ollama then needs a Modelfile with the `FROM` line alone, and its chat answers as `#chat` does.
|
|
320
|
+
|
|
321
|
+
To run a file exported without its chat template in Ollama, point a Modelfile at it and give a template that passes the prompt through:
|
|
322
|
+
|
|
323
|
+
```text
|
|
324
|
+
FROM ./ruby-model.gguf
|
|
325
|
+
TEMPLATE {{ .Prompt }}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
```console
|
|
329
|
+
$ ollama create ruby-model -f Modelfile
|
|
330
|
+
$ ollama run ruby-model $'User: How do I find the minimum value in an array?\nAssistant:'
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
The converter loads some tokenizers with `trust_remote_code=True`, so it runs offline with empty Hugging Face caches of its own, and a model directory is refused when it holds Python code,
|
|
334
|
+
`auto_map` in a JSON file, a link leading out of it or to a directory, a hard link, or a special file. Any of these could run code or carry another file, such as a credential, into the GGUF file.
|
|
335
|
+
The checks run before the converter starts, which is why the directory must not change during the export.
|
|
336
|
+
|
|
251
337
|
### Environment check
|
|
252
338
|
|
|
253
339
|
```ruby
|
|
@@ -256,7 +342,7 @@ RPEFT.check # => a Hash describing Python, packages, and devices
|
|
|
256
342
|
|
|
257
343
|
```console
|
|
258
344
|
$ rpeft doctor
|
|
259
|
-
Ruby: 4.0.
|
|
345
|
+
Ruby: 4.0.9 (rpeft 0.4.0)
|
|
260
346
|
Python: 3.12.13 (/path/to/.venv/bin/python)
|
|
261
347
|
torch: OK 2.13.0
|
|
262
348
|
transformers: OK 5.15.0
|
|
@@ -264,18 +350,23 @@ peft: OK 0.20.0
|
|
|
264
350
|
datasets: OK 5.0.1
|
|
265
351
|
accelerate: OK 1.14.0
|
|
266
352
|
Device: mps (CUDA: no, MPS: yes)
|
|
353
|
+
llama.cpp: OK /Users/you/src/llama.cpp
|
|
354
|
+
Quantizer: OK /opt/homebrew/bin/llama-quantize
|
|
267
355
|
Status: OK
|
|
268
356
|
```
|
|
269
357
|
|
|
358
|
+
The llama.cpp lines are optional, for GGUF export only, and never affect the status.
|
|
359
|
+
|
|
270
360
|
### Errors
|
|
271
361
|
|
|
272
362
|
Python exceptions never surface raw. They are translated into `RPEFT::DependencyError`, `RPEFT::ModelLoadError`, `RPEFT::DatasetError`, `RPEFT::TrainingError`,
|
|
273
363
|
`RPEFT::MergeError`, or `RPEFT::GenerationError`, and the original Python error (with its traceback) stays reachable via `error.cause`.
|
|
364
|
+
A failing llama.cpp tool during GGUF export raises `RPEFT::ExportError` with the end of the tool's output.
|
|
274
365
|
Invalid arguments are rejected in Ruby with `RPEFT::ConfigurationError` before the backend is called.
|
|
275
366
|
|
|
276
367
|
## CLI
|
|
277
368
|
|
|
278
|
-
The CLI covers the core of the lifecycle: training, merging,
|
|
369
|
+
The CLI covers the core of the lifecycle: training, merging, generation from a prompt, and GGUF export.
|
|
279
370
|
Validation datasets, checkpoints, and chat are available from the Ruby API only.
|
|
280
371
|
|
|
281
372
|
```console
|
|
@@ -311,6 +402,16 @@ $ rpeft generate \
|
|
|
311
402
|
--prompt $'User: How do I find the minimum value in an array?\nAssistant:'
|
|
312
403
|
```
|
|
313
404
|
|
|
405
|
+
A merged model is exported to GGUF with `rpeft export gguf`, which takes the same choices as `export_gguf`:
|
|
406
|
+
|
|
407
|
+
```console
|
|
408
|
+
$ rpeft export gguf \
|
|
409
|
+
--model ruby-model \
|
|
410
|
+
--output ruby-model.gguf \
|
|
411
|
+
--no-chat-template \
|
|
412
|
+
--llama-cpp ~/src/llama.cpp
|
|
413
|
+
```
|
|
414
|
+
|
|
314
415
|
`rpeft doctor` and `rpeft version` are also available.
|
|
315
416
|
|
|
316
417
|
## Examples
|
data/lib/rpeft/backend/python.rb
CHANGED
|
@@ -54,8 +54,45 @@ module RPEFT
|
|
|
54
54
|
@mutex = Mutex.new
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
# Yields each progress entry as a Hash. Whatever is raised while the block runs ends the run and is raised
|
|
58
|
+
# from here once Python is back: PyCall drops an exception raised while Python is calling into Ruby,
|
|
59
|
+
# and the training would carry on as if nothing had happened. That includes Interrupt, which Ruby raises
|
|
60
|
+
# wherever its code happens to be running, so the rescue covers everything Python calls, not the block alone.
|
|
61
|
+
def train_lora(config, &on_progress)
|
|
62
|
+
return call(:train_lora, config, default_error: TrainingError) unless on_progress
|
|
63
|
+
|
|
64
|
+
failure = nil
|
|
65
|
+
callback = lambda do |json|
|
|
66
|
+
next 'stop' if failure
|
|
67
|
+
|
|
68
|
+
left_early = true
|
|
69
|
+
on_progress.call(JSON.parse(json.to_s))
|
|
70
|
+
left_early = false
|
|
71
|
+
nil
|
|
72
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
73
|
+
left_early = false
|
|
74
|
+
failure = e
|
|
75
|
+
'stop'
|
|
76
|
+
ensure
|
|
77
|
+
# break and throw leave the block without raising, and PyCall drops them as it drops exceptions,
|
|
78
|
+
# so the run would go on to its end. They cannot be resumed once Python is back, so the run ends in an error;
|
|
79
|
+
# next cancels the jump and hands Python the answer that stops the run.
|
|
80
|
+
if left_early
|
|
81
|
+
failure ||= TrainingError.new(
|
|
82
|
+
'the progress block was left with break or throw, which cannot pass through Python; ' \
|
|
83
|
+
'raise an exception to end a run early'
|
|
84
|
+
)
|
|
85
|
+
next 'stop'
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
begin
|
|
89
|
+
result = call(:train_lora, config, callback, default_error: TrainingError)
|
|
90
|
+
rescue TrainingError
|
|
91
|
+
raise unless failure
|
|
92
|
+
end
|
|
93
|
+
raise failure if failure
|
|
94
|
+
|
|
95
|
+
result
|
|
59
96
|
end
|
|
60
97
|
|
|
61
98
|
def merge_lora(config)
|
|
@@ -76,10 +113,10 @@ module RPEFT
|
|
|
76
113
|
|
|
77
114
|
private
|
|
78
115
|
|
|
79
|
-
def call(name, config, default_error:)
|
|
116
|
+
def call(name, config, *extra, default_error:)
|
|
80
117
|
facade = backend_module
|
|
81
118
|
result = if config
|
|
82
|
-
facade.public_send(name, JSON.generate(config))
|
|
119
|
+
facade.public_send(name, JSON.generate(config), *extra)
|
|
83
120
|
else
|
|
84
121
|
facade.public_send(name)
|
|
85
122
|
end
|
data/lib/rpeft/backend.rb
CHANGED
|
@@ -7,7 +7,8 @@ module RPEFT
|
|
|
7
7
|
#
|
|
8
8
|
# A backend implements four methods, each taking and returning plain Hashes with String keys:
|
|
9
9
|
#
|
|
10
|
-
# train_lora(config) -> Hash
|
|
10
|
+
# train_lora(config) -> Hash, yielding a progress Hash per logged step when given a block;
|
|
11
|
+
# what the block raises ends the run and is raised to the caller
|
|
11
12
|
# merge_lora(config) -> Hash
|
|
12
13
|
# generate(config) -> Hash
|
|
13
14
|
# detect_device(config) -> Hash
|
data/lib/rpeft/cli/doctor.rb
CHANGED
|
@@ -28,6 +28,7 @@ module RPEFT
|
|
|
28
28
|
else
|
|
29
29
|
line 'Backend', "NG #{backend['error']}"
|
|
30
30
|
end
|
|
31
|
+
render_gguf(report['gguf'])
|
|
31
32
|
|
|
32
33
|
# Uppercase for consistency with the OK/MISSING library markers;
|
|
33
34
|
# the check API itself keeps the machine-readable lowercase value.
|
|
@@ -35,9 +36,11 @@ module RPEFT
|
|
|
35
36
|
render_hint(report)
|
|
36
37
|
end
|
|
37
38
|
|
|
39
|
+
# Values come from the environment, such as LLAMA_CPP_DIR and the directories on PATH, which may hold
|
|
40
|
+
# escape sequences a terminal would act on.
|
|
38
41
|
def line(label, value)
|
|
39
42
|
padded = "#{label}:".ljust(14)
|
|
40
|
-
@out.puts "#{padded}#{value}"
|
|
43
|
+
@out.puts "#{padded}#{printable(value)}"
|
|
41
44
|
end
|
|
42
45
|
|
|
43
46
|
def render_python(python)
|
|
@@ -65,6 +68,20 @@ module RPEFT
|
|
|
65
68
|
line 'Device', "#{device['recommended']} (CUDA: #{cuda}, MPS: #{mps})"
|
|
66
69
|
end
|
|
67
70
|
|
|
71
|
+
def render_gguf(gguf)
|
|
72
|
+
gguf ||= {}
|
|
73
|
+
|
|
74
|
+
if gguf['converter']
|
|
75
|
+
line 'llama.cpp', "OK #{gguf['llama_cpp_dir']}"
|
|
76
|
+
elsif gguf['llama_cpp_dir']
|
|
77
|
+
line 'llama.cpp', "NG no #{GGUF::CONVERTER} in #{gguf['llama_cpp_dir']} (LLAMA_CPP_DIR)"
|
|
78
|
+
else
|
|
79
|
+
line 'llama.cpp', 'not configured (optional, set LLAMA_CPP_DIR for GGUF export)'
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
line 'Quantizer', gguf['quantizer'] ? "OK #{gguf['quantizer']}" : "no #{GGUF::QUANTIZER} (optional, for q6_k, q5_k_m, and q4_k_m)"
|
|
83
|
+
end
|
|
84
|
+
|
|
68
85
|
def render_hint(report)
|
|
69
86
|
return unless report['status'] == 'degraded'
|
|
70
87
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPEFT
|
|
4
|
+
module CLI
|
|
5
|
+
# @api private
|
|
6
|
+
class ExportGGUF < Command
|
|
7
|
+
REQUIRED_OPTIONS = %i(model output).freeze
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def option_parser(options)
|
|
12
|
+
OptionParser.new do |o|
|
|
13
|
+
o.banner = 'Usage: rpeft export gguf --model DIR --output FILE [options]'
|
|
14
|
+
o.on('--model DIR', 'Model directory to export, such as a merged model') do |v|
|
|
15
|
+
options[:model] = v
|
|
16
|
+
end
|
|
17
|
+
o.on('--output FILE', 'GGUF file to write') do |v|
|
|
18
|
+
options[:output] = v
|
|
19
|
+
end
|
|
20
|
+
o.on('--quantize TYPE', "#{GGUF::TYPES.join(', ')}, in any case (default: q8_0)") do |v|
|
|
21
|
+
type = v.downcase.to_sym
|
|
22
|
+
raise OptionParser::InvalidArgument, v unless GGUF::TYPES.include?(type)
|
|
23
|
+
|
|
24
|
+
options[:quantize] = type
|
|
25
|
+
end
|
|
26
|
+
o.on('--no-chat-template', 'Leave out the chat template, for a model fine-tuned on plain text') do
|
|
27
|
+
options[:chat_template] = false
|
|
28
|
+
end
|
|
29
|
+
o.on('--llama-cpp DIR', 'llama.cpp checkout holding convert_hf_to_gguf.py (default: $LLAMA_CPP_DIR)') do |v|
|
|
30
|
+
options[:llama_cpp] = v
|
|
31
|
+
end
|
|
32
|
+
o.on('--llama-quantize PATH', 'llama-quantize executable for q6_k, q5_k_m, and q4_k_m (default: from PATH)') do |v|
|
|
33
|
+
options[:llama_quantize] = v
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def required_options
|
|
39
|
+
REQUIRED_OPTIONS
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def execute(options)
|
|
43
|
+
export_options = options.slice(:quantize, :chat_template, :llama_cpp, :llama_quantize)
|
|
44
|
+
path = Model.new(path: options[:model]).export_gguf(options[:output], **export_options)
|
|
45
|
+
|
|
46
|
+
@out.puts "GGUF saved to #{printable(path)}"
|
|
47
|
+
0
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/rpeft/cli.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative 'cli/lora_train'
|
|
|
6
6
|
require_relative 'cli/lora_merge'
|
|
7
7
|
require_relative 'cli/generate'
|
|
8
8
|
require_relative 'cli/doctor'
|
|
9
|
+
require_relative 'cli/export_gguf'
|
|
9
10
|
|
|
10
11
|
module RPEFT
|
|
11
12
|
# Command line interface. Commands go through the public Ruby API only,
|
|
@@ -18,6 +19,7 @@ module RPEFT
|
|
|
18
19
|
lora train Fine-tune a base model with LoRA
|
|
19
20
|
lora merge Merge a LoRA adapter into its base model
|
|
20
21
|
generate Generate text from a base or fine-tuned model
|
|
22
|
+
export gguf Export a model to a GGUF file for llama.cpp and Ollama
|
|
21
23
|
doctor Diagnose the Python environment
|
|
22
24
|
version Print the RPEFT version
|
|
23
25
|
help Show this message
|
|
@@ -33,11 +35,19 @@ module RPEFT
|
|
|
33
35
|
merge Merge a LoRA adapter into its base model
|
|
34
36
|
USAGE
|
|
35
37
|
|
|
38
|
+
EXPORT_USAGE = <<~USAGE
|
|
39
|
+
Usage: rpeft export <format> [options]
|
|
40
|
+
|
|
41
|
+
Formats:
|
|
42
|
+
gguf Export a model to a GGUF file for llama.cpp and Ollama
|
|
43
|
+
USAGE
|
|
44
|
+
|
|
36
45
|
class << self
|
|
37
46
|
def run(argv, out: $stdout, err: $stderr)
|
|
38
47
|
command, *rest = argv
|
|
39
48
|
case command
|
|
40
49
|
when 'lora' then run_lora(rest, out: out, err: err)
|
|
50
|
+
when 'export' then run_export(rest, out: out, err: err)
|
|
41
51
|
when 'generate' then Generate.run(rest, out: out, err: err)
|
|
42
52
|
when 'doctor' then Doctor.run(rest, out: out, err: err)
|
|
43
53
|
when 'version', '--version', '-v'
|
|
@@ -72,6 +82,21 @@ module RPEFT
|
|
|
72
82
|
2
|
|
73
83
|
end
|
|
74
84
|
end
|
|
85
|
+
|
|
86
|
+
def run_export(argv, out:, err:)
|
|
87
|
+
format, *rest = argv
|
|
88
|
+
|
|
89
|
+
case format
|
|
90
|
+
when 'gguf' then ExportGGUF.run(rest, out: out, err: err)
|
|
91
|
+
when nil, 'help', '--help', '-h'
|
|
92
|
+
out.puts EXPORT_USAGE
|
|
93
|
+
0
|
|
94
|
+
else
|
|
95
|
+
err.puts "rpeft: unknown export format #{format.inspect}"
|
|
96
|
+
err.puts EXPORT_USAGE
|
|
97
|
+
2
|
|
98
|
+
end
|
|
99
|
+
end
|
|
75
100
|
end
|
|
76
101
|
end
|
|
77
102
|
end
|
data/lib/rpeft/diagnosis.rb
CHANGED
|
@@ -12,7 +12,8 @@ module RPEFT
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def report
|
|
15
|
-
|
|
15
|
+
# The llama.cpp tools are optional, for GGUF export only, so they never affect the status.
|
|
16
|
+
base = {'ruby' => {'version' => RUBY_VERSION, 'rpeft' => VERSION}, 'gguf' => GGUF.tools}
|
|
16
17
|
begin
|
|
17
18
|
diagnostics = @backend.diagnostics
|
|
18
19
|
rescue Error => e
|
data/lib/rpeft/errors.rb
CHANGED
data/lib/rpeft/gguf.rb
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'find'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'open3'
|
|
6
|
+
require 'tmpdir'
|
|
7
|
+
|
|
8
|
+
module RPEFT
|
|
9
|
+
# Exports a merged model directory to a single GGUF file for llama.cpp, Ollama, and other local runtimes.
|
|
10
|
+
#
|
|
11
|
+
# The conversion is left to llama.cpp's own tools, which know every architecture and tokenizer GGUF supports:
|
|
12
|
+
# its `convert_hf_to_gguf.py` writes 16-bit or Q8_0 files, and `llama-quantize` turns a 16-bit file into
|
|
13
|
+
# the K-quant types. RPEFT checks the input, runs the tools, and cleans up after them.
|
|
14
|
+
module GGUF
|
|
15
|
+
CONVERTER = 'convert_hf_to_gguf.py'
|
|
16
|
+
QUANTIZER = 'llama-quantize'
|
|
17
|
+
METADATA_EDITOR = File.join('gguf-py', 'gguf', 'scripts', 'gguf_new_metadata.py')
|
|
18
|
+
CHAT_TEMPLATE_KEY = 'tokenizer.chat_template'
|
|
19
|
+
# The list of a model's named templates, such as one for tool use. The templates themselves sit under keys
|
|
20
|
+
# holding their names, which only a reader of the file would know, so they stay; the default one is what Ollama was seen to use.
|
|
21
|
+
CHAT_TEMPLATE_NAMES_KEY = 'tokenizer.chat_templates'
|
|
22
|
+
MAGIC = 'GGUF'
|
|
23
|
+
|
|
24
|
+
# Types the converter writes directly, by its --outtype name.
|
|
25
|
+
CONVERTED = {f16: 'f16', bf16: 'bf16', q8_0: 'q8_0'}.freeze
|
|
26
|
+
# Types that need a 16-bit file run through llama-quantize, by its type name.
|
|
27
|
+
QUANTIZED = {q6_k: 'Q6_K', q5_k_m: 'Q5_K_M', q4_k_m: 'Q4_K_M'}.freeze
|
|
28
|
+
TYPES = [*CONVERTED.keys, *QUANTIZED.keys].freeze
|
|
29
|
+
MAX_JSON_BYTES = 256 * 1024 * 1024
|
|
30
|
+
|
|
31
|
+
# Runs a command given as an argument list, never through a shell, with extra environment variables,
|
|
32
|
+
# and returns its combined output and whether it succeeded. Replaced in tests so that no external tool runs.
|
|
33
|
+
DEFAULT_RUNNER = lambda do |argv, env|
|
|
34
|
+
output, status = Open3.capture2e(env, *argv)
|
|
35
|
+
|
|
36
|
+
[output, status.success?]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
attr_writer :runner
|
|
41
|
+
|
|
42
|
+
def runner
|
|
43
|
+
@runner || DEFAULT_RUNNER
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def export(model_dir, output, quantize: :q8_0, chat_template: true, llama_cpp: nil, llama_quantize: nil)
|
|
47
|
+
type = quantize_type(quantize)
|
|
48
|
+
raise ConfigurationError, "chat_template must be true or false, got #{chat_template.inspect}" unless [true, false].include?(chat_template)
|
|
49
|
+
|
|
50
|
+
model_dir = model_directory(model_dir)
|
|
51
|
+
output = output_path(output)
|
|
52
|
+
llama_cpp_dir = llama_cpp_directory(llama_cpp)
|
|
53
|
+
converter = converter_path(llama_cpp_dir)
|
|
54
|
+
quantizer = quantizer_path(llama_quantize, llama_cpp_dir) if QUANTIZED.key?(type)
|
|
55
|
+
editor = metadata_editor_path(llama_cpp_dir) unless chat_template
|
|
56
|
+
|
|
57
|
+
# The tools write into a directory of their own next to the output, under fixed names: the converter
|
|
58
|
+
# expands placeholders such as {ftype} in the name it is given, so it never sees the caller's name.
|
|
59
|
+
# Only a verified file reaches the output, and everything else goes with the directory, including the bf16
|
|
60
|
+
# intermediate of the K-quant types, which is as large as the model and so belongs on the output's disk.
|
|
61
|
+
Dir.mktmpdir('.rpeft-gguf-', File.dirname(output)) do |dir|
|
|
62
|
+
staged = File.join(dir, 'model.gguf')
|
|
63
|
+
env = converter_env(dir)
|
|
64
|
+
|
|
65
|
+
if quantizer
|
|
66
|
+
intermediate = File.join(dir, 'model-bf16.gguf')
|
|
67
|
+
convert(model_dir, intermediate, 'bf16', converter, env)
|
|
68
|
+
run([quantizer, intermediate, staged, QUANTIZED.fetch(type)], QUANTIZER)
|
|
69
|
+
else
|
|
70
|
+
convert(model_dir, staged, CONVERTED.fetch(type), converter, env)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
verify(staged, quantizer ? QUANTIZER : CONVERTER)
|
|
74
|
+
staged = remove_chat_template(staged, editor, env) if editor
|
|
75
|
+
|
|
76
|
+
publish(staged, output)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
output
|
|
80
|
+
rescue SystemCallError => e
|
|
81
|
+
raise ExportError, "GGUF export to #{output} failed: #{e.message}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The llama.cpp tools as RPEFT would find them now, for `rpeft doctor`. Never raises.
|
|
85
|
+
def tools(env: ENV)
|
|
86
|
+
configured = env.fetch('LLAMA_CPP_DIR', '').to_s
|
|
87
|
+
dir = configured.empty? ? nil : expand(configured) || configured
|
|
88
|
+
converter = dir && File.join(dir, CONVERTER)
|
|
89
|
+
|
|
90
|
+
{
|
|
91
|
+
'llama_cpp_dir' => dir,
|
|
92
|
+
'converter' => converter && File.file?(converter) ? converter : nil,
|
|
93
|
+
'quantizer' => find_quantizer(dir, env)
|
|
94
|
+
}
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def quantize_type(value)
|
|
100
|
+
type = value.to_s.downcase.to_sym if value.is_a?(String) || value.is_a?(Symbol)
|
|
101
|
+
return type if TYPES.include?(type)
|
|
102
|
+
|
|
103
|
+
raise ConfigurationError, "quantize must be one of #{TYPES.join(', ')}, got #{value.inspect}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def model_directory(value)
|
|
107
|
+
dir = expanded_path(value, 'model')
|
|
108
|
+
|
|
109
|
+
unless File.directory?(dir)
|
|
110
|
+
raise ConfigurationError, "GGUF export needs a local model directory, got #{dir.inspect}; save a Hub model or merge an adapter into a directory first"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
if File.file?(File.join(dir, 'adapter_config.json'))
|
|
114
|
+
raise ConfigurationError, "#{dir} is a LoRA adapter; merge it into its base model before exporting to GGUF"
|
|
115
|
+
end
|
|
116
|
+
raise ConfigurationError, "#{dir} has no config.json, so it is not a Transformers model directory" unless File.file?(File.join(dir, 'config.json'))
|
|
117
|
+
|
|
118
|
+
refuse_custom_code(dir)
|
|
119
|
+
|
|
120
|
+
dir
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# A model RPEFT merged ships no custom code, so a directory that does is refused rather than trusted:
|
|
124
|
+
# Python files, and auto_map in any JSON file, since a config can name further config files
|
|
125
|
+
# (configuration_files) that Transformers reads instead. Extensions are compared in any case, as a file
|
|
126
|
+
# system such as macOS's default one finds configuration_x.PY when Transformers asks for
|
|
127
|
+
# configuration_x.py. Find covers hidden directories too and does not follow links.
|
|
128
|
+
#
|
|
129
|
+
# The check runs once, and the converter reads the directory again later, so a directory someone else can
|
|
130
|
+
# write to in between is not covered. Checking a private copy instead would cost a full copy of the model,
|
|
131
|
+
# for a directory that is normally a model the user merged; the README asks for such a directory instead.
|
|
132
|
+
def refuse_custom_code(dir)
|
|
133
|
+
# The model directory itself may be reached through a link, which Find would not enter.
|
|
134
|
+
root = File.realpath(dir)
|
|
135
|
+
|
|
136
|
+
Find.find(root) do |path|
|
|
137
|
+
next if path == root
|
|
138
|
+
|
|
139
|
+
name = path.delete_prefix("#{root}/")
|
|
140
|
+
refuse_entry(dir, root, name, path)
|
|
141
|
+
extension = File.extname(path).downcase
|
|
142
|
+
raise ConfigurationError, "#{dir} contains Python code (#{name}), which the GGUF converter could run" if extension == '.py'
|
|
143
|
+
|
|
144
|
+
refuse_auto_map(dir, name, path) if extension == '.json' && File.file?(path)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The converter reads whatever a name in the directory leads to and writes some of it, such as the chat
|
|
149
|
+
# template, into the GGUF file. A link leading out of the directory, or a hard link, could thus carry
|
|
150
|
+
# another file, such as a credential, into a GGUF file that is then published, and a special file such
|
|
151
|
+
# as a FIFO could stall the converter. A link to a directory is refused because Find does not enter it.
|
|
152
|
+
def refuse_entry(dir, root, name, path)
|
|
153
|
+
stat = File.lstat(path)
|
|
154
|
+
if stat.symlink?
|
|
155
|
+
refuse_link(dir, root, name, path)
|
|
156
|
+
elsif stat.file?
|
|
157
|
+
raise ConfigurationError, "#{dir} holds #{name}, a hard link that may share another file's contents" if stat.nlink > 1
|
|
158
|
+
elsif !stat.directory?
|
|
159
|
+
raise ConfigurationError, "#{dir} holds #{name}, which is not a regular file"
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def refuse_link(dir, root, name, path)
|
|
164
|
+
target = File.realpath(path)
|
|
165
|
+
|
|
166
|
+
raise ConfigurationError, "#{dir} holds #{name}, a link leading outside the directory" unless target.start_with?("#{root}/")
|
|
167
|
+
raise ConfigurationError, "#{dir} holds #{name}, a link to a directory, which RPEFT cannot check" if File.directory?(target)
|
|
168
|
+
raise ConfigurationError, "#{dir} holds #{name}, a link to something other than a regular file" unless File.file?(target)
|
|
169
|
+
rescue Errno::ENOENT, Errno::ELOOP
|
|
170
|
+
raise ConfigurationError, "#{dir} holds #{name}, a link that leads nowhere"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Every JSON file is parsed rather than searched for the text auto_map, which a key can spell with
|
|
174
|
+
# a Unicode escape for any of its letters; a multi-megabyte tokenizer.json takes tens of milliseconds.
|
|
175
|
+
# NaN is allowed, as Python's json, which reads the configs, allows it.
|
|
176
|
+
def refuse_auto_map(dir, name, path)
|
|
177
|
+
# Configs and tokenizers run to tens of megabytes at most; a larger file would only be read into memory.
|
|
178
|
+
if File.size(path) > MAX_JSON_BYTES
|
|
179
|
+
raise ConfigurationError, "#{dir} holds #{name}, a JSON file larger than #{MAX_JSON_BYTES / 1024 / 1024} MB"
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
config = JSON.parse(File.read(path), allow_nan: true)
|
|
183
|
+
return unless config.is_a?(Hash) && config.key?('auto_map')
|
|
184
|
+
|
|
185
|
+
raise ConfigurationError, "#{dir} maps classes to custom code (auto_map in #{name}), which the GGUF converter could run"
|
|
186
|
+
rescue JSON::ParserError => e
|
|
187
|
+
raise ConfigurationError, "#{dir} holds #{name}, which is not valid JSON: #{e.message}"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def output_path(value)
|
|
191
|
+
path = expanded_path(value, 'output')
|
|
192
|
+
|
|
193
|
+
# File.exist? is false for a link whose target is missing, which the converter would write through.
|
|
194
|
+
if File.exist?(path) || File.symlink?(path)
|
|
195
|
+
raise ConfigurationError, "output #{path} already exists; remove it or choose another path"
|
|
196
|
+
end
|
|
197
|
+
raise ConfigurationError, "output directory #{File.dirname(path)} does not exist" unless File.directory?(File.dirname(path))
|
|
198
|
+
|
|
199
|
+
path
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def llama_cpp_directory(llama_cpp)
|
|
203
|
+
dir = llama_cpp ? Arguments.path(llama_cpp, 'llama_cpp') : ENV.fetch('LLAMA_CPP_DIR', '')
|
|
204
|
+
|
|
205
|
+
if dir.empty?
|
|
206
|
+
raise DependencyError, 'GGUF export needs a llama.cpp checkout for its converter: clone ' \
|
|
207
|
+
'https://github.com/ggml-org/llama.cpp and pass llama_cpp: (--llama-cpp) or set LLAMA_CPP_DIR'
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
expand(dir) || raise(ConfigurationError, "llama_cpp #{dir.inspect} cannot be expanded to a path")
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def converter_path(llama_cpp_dir)
|
|
214
|
+
converter = File.join(llama_cpp_dir, CONVERTER)
|
|
215
|
+
raise DependencyError, "#{CONVERTER} not found in #{llama_cpp_dir}; is it a llama.cpp checkout?" unless File.file?(converter)
|
|
216
|
+
|
|
217
|
+
converter
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def metadata_editor_path(llama_cpp_dir)
|
|
221
|
+
editor = File.join(llama_cpp_dir, METADATA_EDITOR)
|
|
222
|
+
raise DependencyError, "#{METADATA_EDITOR} not found in #{llama_cpp_dir}, needed for chat_template: false" unless File.file?(editor)
|
|
223
|
+
|
|
224
|
+
editor
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def quantizer_path(llama_quantize, llama_cpp_dir)
|
|
228
|
+
if llama_quantize
|
|
229
|
+
path = expanded_path(llama_quantize, 'llama_quantize')
|
|
230
|
+
return path if File.file?(path) && File.executable?(path)
|
|
231
|
+
|
|
232
|
+
raise DependencyError, "#{path} is not an executable #{QUANTIZER}"
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
found = find_quantizer(llama_cpp_dir, ENV)
|
|
236
|
+
return found if found
|
|
237
|
+
|
|
238
|
+
raise DependencyError, "#{QUANTIZER} not found for the K-quant types; install llama.cpp (e.g. `brew install llama.cpp`), " \
|
|
239
|
+
'build it in the llama.cpp checkout, or pass llama_quantize: (--llama-quantize)'
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def find_quantizer(llama_cpp_dir, env)
|
|
243
|
+
candidates = env.fetch('PATH', '').split(File::PATH_SEPARATOR).reject(&:empty?).map { |dir| File.join(dir, QUANTIZER) }
|
|
244
|
+
candidates << File.join(llama_cpp_dir, 'build', 'bin', QUANTIZER) if llama_cpp_dir
|
|
245
|
+
|
|
246
|
+
candidates.find { |path| File.file?(path) && File.executable?(path) }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def expanded_path(value, name)
|
|
250
|
+
path = Arguments.path(value, name)
|
|
251
|
+
|
|
252
|
+
expand(path) || raise(ConfigurationError, "#{name} #{path.inspect} cannot be expanded to a path")
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# File.expand_path raises for a ~user that does not exist; nil stands for that here.
|
|
256
|
+
def expand(path)
|
|
257
|
+
File.expand_path(path)
|
|
258
|
+
rescue ArgumentError
|
|
259
|
+
nil
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def convert(model_dir, output, outtype, converter, env)
|
|
263
|
+
run([python, converter, model_dir, '--outfile', output, '--outtype', outtype], CONVERTER, env)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Ollama (checked with 0.33.1) wraps every prompt in the chat template a GGUF file carries, even when
|
|
267
|
+
# a Modelfile gives a TEMPLATE of its own. A model fine-tuned on plain text expects its prompt as it was in
|
|
268
|
+
# training, so for such a model the template is left out. The editor writes a whole new file, skips keys
|
|
269
|
+
# the file does not have, and with --force skips its confirmation prompt.
|
|
270
|
+
def remove_chat_template(staged, editor, env)
|
|
271
|
+
edited = File.join(File.dirname(staged), 'model-no-chat-template.gguf')
|
|
272
|
+
argv = [
|
|
273
|
+
python, editor, '--remove-metadata', CHAT_TEMPLATE_KEY, '--remove-metadata', CHAT_TEMPLATE_NAMES_KEY, '--force', staged, edited
|
|
274
|
+
]
|
|
275
|
+
|
|
276
|
+
run(argv, File.basename(editor), env)
|
|
277
|
+
verify(edited, File.basename(editor))
|
|
278
|
+
|
|
279
|
+
edited
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# The converter loads some tokenizers with trust_remote_code=True. Besides the model directory, checked
|
|
283
|
+
# by refuse_custom_code, that code could come from the Hugging Face Hub, which offline mode keeps out,
|
|
284
|
+
# or from modules earlier runs left in the Hugging Face caches, which offline mode still loads. The caches
|
|
285
|
+
# are therefore pointed at empty directories of this export; a local model needs nothing from them.
|
|
286
|
+
def converter_env(dir)
|
|
287
|
+
{
|
|
288
|
+
'HF_HUB_OFFLINE' => '1',
|
|
289
|
+
'TRANSFORMERS_OFFLINE' => '1',
|
|
290
|
+
'HF_HOME' => File.join(dir, 'hf-home'),
|
|
291
|
+
'HF_HUB_CACHE' => File.join(dir, 'hf-hub'),
|
|
292
|
+
'HF_MODULES_CACHE' => File.join(dir, 'hf-modules'),
|
|
293
|
+
'TRANSFORMERS_CACHE' => nil,
|
|
294
|
+
'HUGGINGFACE_HUB_CACHE' => nil
|
|
295
|
+
}
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def run(argv, name, env = {})
|
|
299
|
+
begin
|
|
300
|
+
output, success = runner.call(argv, env)
|
|
301
|
+
rescue SystemCallError => e
|
|
302
|
+
raise DependencyError, "could not run #{name} (#{argv.first}): #{e.message}"
|
|
303
|
+
end
|
|
304
|
+
return if success
|
|
305
|
+
|
|
306
|
+
tail = output.to_s.lines.last(20).join
|
|
307
|
+
|
|
308
|
+
raise ExportError, "#{name} failed:\n#{tail}"
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# The converter runs in its own process with the interpreter RPEFT uses, so it sees the same packages
|
|
312
|
+
# without loading torch a second time into this process through PyCall.
|
|
313
|
+
def python
|
|
314
|
+
require_relative 'backend/python'
|
|
315
|
+
|
|
316
|
+
Backend::Python.default_python || 'python3'
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def verify(path, name)
|
|
320
|
+
header = File.file?(path) ? File.binread(path, MAGIC.bytesize) : nil
|
|
321
|
+
|
|
322
|
+
raise ExportError, "#{name} did not write a GGUF file" unless header == MAGIC
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# A hard link puts the file at the output in one step and fails if anything is there by now, so a file
|
|
326
|
+
# another process created since output_path checked is neither replaced nor removed.
|
|
327
|
+
def publish(staged, output)
|
|
328
|
+
File.link(staged, output)
|
|
329
|
+
rescue Errno::EEXIST
|
|
330
|
+
raise ConfigurationError, "output #{output} already exists; remove it or choose another path"
|
|
331
|
+
rescue Errno::EPERM, Errno::ENOTSUP, Errno::EOPNOTSUPP
|
|
332
|
+
copy_exclusively(staged, output)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# Some file systems, such as exFAT, have no hard links. Creating the output exclusively keeps the same
|
|
336
|
+
# guarantee at the cost of a copy, and a file this creates is the only one a failed copy removes.
|
|
337
|
+
def copy_exclusively(staged, output)
|
|
338
|
+
created = false
|
|
339
|
+
|
|
340
|
+
File.open(output, File::WRONLY | File::CREAT | File::EXCL | File::BINARY) do |file|
|
|
341
|
+
created = true
|
|
342
|
+
|
|
343
|
+
IO.copy_stream(staged, file)
|
|
344
|
+
end
|
|
345
|
+
rescue Errno::EEXIST
|
|
346
|
+
raise ConfigurationError, "output #{output} already exists; remove it or choose another path"
|
|
347
|
+
rescue StandardError
|
|
348
|
+
File.delete(output) if created
|
|
349
|
+
raise
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPEFT
|
|
4
|
+
module LoRA
|
|
5
|
+
# One step of a training run, as the block given to LoRA.train sees it. A training step carries loss and learning_rate,
|
|
6
|
+
# an evaluation at the end of an epoch carries eval_loss, and whatever a step lacks is nil.
|
|
7
|
+
class Progress
|
|
8
|
+
attr_reader :step, :total_steps, :epoch, :loss, :eval_loss, :learning_rate
|
|
9
|
+
|
|
10
|
+
def initialize(entry)
|
|
11
|
+
@step = entry['step']
|
|
12
|
+
@total_steps = entry['total_steps']
|
|
13
|
+
@epoch = entry['epoch']
|
|
14
|
+
@loss = entry['loss']
|
|
15
|
+
@eval_loss = entry['eval_loss']
|
|
16
|
+
@learning_rate = entry['learning_rate']
|
|
17
|
+
freeze
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/rpeft/lora.rb
CHANGED
|
@@ -18,7 +18,8 @@ module RPEFT
|
|
|
18
18
|
POSITIVE_NUMBER_OPTIONS = %i(alpha learning_rate).freeze
|
|
19
19
|
|
|
20
20
|
class << self
|
|
21
|
-
|
|
21
|
+
# A block, if given, is called with a Progress for every logged training step and every evaluation.
|
|
22
|
+
def train(model:, dataset:, eval_dataset: nil, rank: 8, alpha: 16, dropout: 0.05, target_modules: nil, epochs: 3, batch_size: 4, learning_rate: 2e-4, max_length: 512, train_on_prompt: false, output_dir: nil, device: :auto, checkpoints: false, resume: false, &on_progress)
|
|
22
23
|
options = {
|
|
23
24
|
rank: rank,
|
|
24
25
|
alpha: alpha,
|
|
@@ -42,9 +43,7 @@ module RPEFT
|
|
|
42
43
|
resolved_device = Device.resolve(device)
|
|
43
44
|
|
|
44
45
|
options[:eval_dataset] = evaluation_dataset
|
|
45
|
-
result =
|
|
46
|
-
request_payload(model, training_dataset, adapter_dir, resolved_device, options)
|
|
47
|
-
)
|
|
46
|
+
result = run_backend(request_payload(model, training_dataset, adapter_dir, resolved_device, options), on_progress)
|
|
48
47
|
|
|
49
48
|
build_adapter(
|
|
50
49
|
adapter_dir: adapter_dir,
|
|
@@ -67,6 +66,14 @@ module RPEFT
|
|
|
67
66
|
|
|
68
67
|
private
|
|
69
68
|
|
|
69
|
+
# The backend is given a block even when the caller gave none: an exception raised while the block runs ends the run,
|
|
70
|
+
# and Interrupt is such an exception, so Ctrl-C then ends a run at the next logged step rather than after the last one.
|
|
71
|
+
def run_backend(payload, on_progress)
|
|
72
|
+
RPEFT.backend.train_lora(payload) do |entry|
|
|
73
|
+
on_progress&.call(Progress.new(entry))
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
70
77
|
def validate_options(options)
|
|
71
78
|
POSITIVE_INTEGER_OPTIONS.each do |key|
|
|
72
79
|
value = options[key]
|
|
@@ -212,7 +219,8 @@ module RPEFT
|
|
|
212
219
|
'device' => result['device'] || device.to_s,
|
|
213
220
|
'train_loss' => result['final_loss'],
|
|
214
221
|
'eval_loss' => result['eval_loss'],
|
|
215
|
-
'duration_seconds' => result['train_runtime_seconds']
|
|
222
|
+
'duration_seconds' => result['train_runtime_seconds'],
|
|
223
|
+
'history' => result['history']
|
|
216
224
|
}
|
|
217
225
|
}
|
|
218
226
|
end
|
data/lib/rpeft/model.rb
CHANGED
|
@@ -38,6 +38,15 @@ module RPEFT
|
|
|
38
38
|
self
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# Writes the model to a single GGUF file for llama.cpp, Ollama, and other local runtimes, and returns its path.
|
|
42
|
+
# chat_template: false leaves out the chat template, for a model fine-tuned on plain text.
|
|
43
|
+
# See GGUF for the quantize: types and how the llama.cpp tools are found.
|
|
44
|
+
def export_gguf(output, quantize: :q8_0, chat_template: true, llama_cpp: nil, llama_quantize: nil)
|
|
45
|
+
GGUF.export(
|
|
46
|
+
path, output, quantize: quantize, chat_template: chat_template, llama_cpp: llama_cpp, llama_quantize: llama_quantize
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
41
50
|
# Generates a completion for the prompt. Decoding is greedy (and thus deterministic for a given model and prompt)
|
|
42
51
|
# unless temperature or top_p turns sampling on.
|
|
43
52
|
def generate(prompt, max_new_tokens: 64, device: :auto, temperature: nil, top_p: nil, seed: nil)
|
data/lib/rpeft/version.rb
CHANGED
data/lib/rpeft.rb
CHANGED
|
@@ -9,8 +9,10 @@ require_relative 'rpeft/dataset'
|
|
|
9
9
|
require_relative 'rpeft/messages'
|
|
10
10
|
require_relative 'rpeft/generation'
|
|
11
11
|
require_relative 'rpeft/model'
|
|
12
|
+
require_relative 'rpeft/gguf'
|
|
12
13
|
require_relative 'rpeft/lora'
|
|
13
14
|
require_relative 'rpeft/lora/adapter'
|
|
15
|
+
require_relative 'rpeft/lora/progress'
|
|
14
16
|
require_relative 'rpeft/backend'
|
|
15
17
|
require_relative 'rpeft/diagnosis'
|
|
16
18
|
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Imported through PyCall; not a public API. Every facade function takes
|
|
4
4
|
a JSON string and returns a JSON string, so only plain strings cross
|
|
5
|
-
the PyCall boundary.
|
|
5
|
+
the PyCall boundary. The one callable that crosses, train_lora's
|
|
6
|
+
on_progress, is itself handed JSON strings only.
|
|
6
7
|
|
|
7
8
|
This module must stay importable without torch installed: all heavy
|
|
8
9
|
imports live inside function bodies, and diagnostics() reports what is
|
|
@@ -14,7 +15,7 @@ import math
|
|
|
14
15
|
import os
|
|
15
16
|
import sys
|
|
16
17
|
|
|
17
|
-
__version__ = "0.
|
|
18
|
+
__version__ = "0.4.0"
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
def _dumps(value):
|
|
@@ -53,12 +54,13 @@ def _repair_sys_executable():
|
|
|
53
54
|
_repair_sys_executable()
|
|
54
55
|
|
|
55
56
|
|
|
56
|
-
def train_lora(config_json):
|
|
57
|
+
def train_lora(config_json, on_progress=None):
|
|
58
|
+
"""on_progress, if given, is called with a JSON string per logged step and may return "stop"."""
|
|
57
59
|
from . import _training, _versions
|
|
58
60
|
|
|
59
61
|
_versions.require_minimums()
|
|
60
62
|
|
|
61
|
-
return _dumps(_training.train_lora(json.loads(config_json)))
|
|
63
|
+
return _dumps(_training.train_lora(json.loads(config_json), on_progress))
|
|
62
64
|
|
|
63
65
|
|
|
64
66
|
def merge_lora(config_json):
|
|
@@ -11,8 +11,7 @@ def render(tokenizer, messages, add_generation_prompt=False):
|
|
|
11
11
|
messages, tokenize=False, add_generation_prompt=add_generation_prompt
|
|
12
12
|
)
|
|
13
13
|
|
|
14
|
-
# Tokenizers without a chat template (
|
|
15
|
-
# a deterministic rendering.
|
|
14
|
+
# Tokenizers without a chat template (some base models) still need a deterministic rendering.
|
|
16
15
|
rendered = "\n".join(
|
|
17
16
|
"%s: %s" % (message["role"], message["content"]) for message in messages
|
|
18
17
|
)
|
|
@@ -3,10 +3,14 @@ import os
|
|
|
3
3
|
import shutil
|
|
4
4
|
|
|
5
5
|
from . import _data, _device, _models
|
|
6
|
-
from ._errors import stage
|
|
6
|
+
from ._errors import BackendError, stage
|
|
7
7
|
|
|
8
|
+
# What on_progress returns to end the run early.
|
|
9
|
+
STOP = "stop"
|
|
10
|
+
HISTORY_KEYS = ("loss", "eval_loss", "learning_rate")
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
|
|
13
|
+
def train_lora(config, on_progress=None):
|
|
10
14
|
lora = config.get("lora", {})
|
|
11
15
|
resolved = _device.resolve_device(config.get("device"))
|
|
12
16
|
device = resolved["device"]
|
|
@@ -30,9 +34,19 @@ def train_lora(config):
|
|
|
30
34
|
with stage("training"):
|
|
31
35
|
model, target_modules, param_counts = _apply_lora(model, lora)
|
|
32
36
|
trainer = _build_trainer(model, tokenizer, dataset, eval_dataset, config, device, dtype)
|
|
37
|
+
progress = _progress_callback(on_progress) if on_progress is not None else None
|
|
38
|
+
if progress is not None:
|
|
39
|
+
trainer.add_callback(progress)
|
|
33
40
|
resume = _resume_point(config)
|
|
34
41
|
metrics = trainer.train(resume_from_checkpoint=resume).metrics
|
|
42
|
+
if progress is not None and progress.stopped:
|
|
43
|
+
# Nothing is saved: an adapter cut short is not the one that was asked for.
|
|
44
|
+
raise BackendError("training", "training was stopped by the progress callback")
|
|
35
45
|
steps = trainer.state.global_step
|
|
46
|
+
# Taken before the evaluation below, which would log the last epoch's eval_loss a second time.
|
|
47
|
+
history = _history(trainer.state.log_history)
|
|
48
|
+
if progress is not None:
|
|
49
|
+
trainer.remove_callback(progress)
|
|
36
50
|
if eval_dataset is not None:
|
|
37
51
|
metrics = dict(metrics, **trainer.evaluate())
|
|
38
52
|
|
|
@@ -42,15 +56,55 @@ def train_lora(config):
|
|
|
42
56
|
# just travel with it wherever the adapter directory is saved or shared.
|
|
43
57
|
shutil.rmtree(_trainer_dir(output_dir), ignore_errors=True)
|
|
44
58
|
|
|
45
|
-
|
|
59
|
+
result = _result(
|
|
46
60
|
config, device, dtype, target_modules, param_counts, metrics, steps, len(dataset), skipped, eval_skipped
|
|
47
61
|
)
|
|
62
|
+
result["history"] = history
|
|
63
|
+
return result
|
|
48
64
|
finally:
|
|
49
65
|
model = None
|
|
50
66
|
gc.collect()
|
|
51
67
|
_device.empty_cache(device)
|
|
52
68
|
|
|
53
69
|
|
|
70
|
+
def _progress_callback(on_progress):
|
|
71
|
+
import json
|
|
72
|
+
|
|
73
|
+
from transformers import TrainerCallback
|
|
74
|
+
|
|
75
|
+
from . import _finite
|
|
76
|
+
|
|
77
|
+
class Progress(TrainerCallback):
|
|
78
|
+
stopped = False
|
|
79
|
+
|
|
80
|
+
def on_log(self, args, state, control, logs=None, **kwargs):
|
|
81
|
+
entry = _history_entry(dict(logs or {}, step=state.global_step))
|
|
82
|
+
if entry is None:
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
entry["total_steps"] = state.max_steps
|
|
86
|
+
# Only a JSON string crosses to the caller, as everywhere else at this boundary.
|
|
87
|
+
if on_progress(json.dumps(_finite(entry), allow_nan=False)) == STOP:
|
|
88
|
+
self.stopped = True
|
|
89
|
+
control.should_training_stop = True
|
|
90
|
+
|
|
91
|
+
return Progress()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _history(log_history):
|
|
95
|
+
entries = (_history_entry(log) for log in log_history)
|
|
96
|
+
return [entry for entry in entries if entry is not None]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _history_entry(log):
|
|
100
|
+
# The Trainer also logs a summary of the whole run, which holds neither a loss of a step nor an eval_loss.
|
|
101
|
+
values = {key: log[key] for key in HISTORY_KEYS if key in log}
|
|
102
|
+
if "loss" not in values and "eval_loss" not in values:
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
return dict({"step": log.get("step"), "epoch": log.get("epoch")}, **values)
|
|
106
|
+
|
|
107
|
+
|
|
54
108
|
def _load_dataset(config, tokenizer, name):
|
|
55
109
|
return _data.load_tokenized_dataset(
|
|
56
110
|
config[name + "_path"],
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rpeft
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Koichi ITO
|
|
@@ -60,6 +60,7 @@ files:
|
|
|
60
60
|
- lib/rpeft/cli.rb
|
|
61
61
|
- lib/rpeft/cli/command.rb
|
|
62
62
|
- lib/rpeft/cli/doctor.rb
|
|
63
|
+
- lib/rpeft/cli/export_gguf.rb
|
|
63
64
|
- lib/rpeft/cli/generate.rb
|
|
64
65
|
- lib/rpeft/cli/lora_merge.rb
|
|
65
66
|
- lib/rpeft/cli/lora_train.rb
|
|
@@ -69,8 +70,10 @@ files:
|
|
|
69
70
|
- lib/rpeft/directory.rb
|
|
70
71
|
- lib/rpeft/errors.rb
|
|
71
72
|
- lib/rpeft/generation.rb
|
|
73
|
+
- lib/rpeft/gguf.rb
|
|
72
74
|
- lib/rpeft/lora.rb
|
|
73
75
|
- lib/rpeft/lora/adapter.rb
|
|
76
|
+
- lib/rpeft/lora/progress.rb
|
|
74
77
|
- lib/rpeft/messages.rb
|
|
75
78
|
- lib/rpeft/model.rb
|
|
76
79
|
- lib/rpeft/version.rb
|
|
@@ -107,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
107
110
|
- !ruby/object:Gem::Version
|
|
108
111
|
version: '0'
|
|
109
112
|
requirements: []
|
|
110
|
-
rubygems_version: 4.0.
|
|
113
|
+
rubygems_version: 4.0.20
|
|
111
114
|
specification_version: 4
|
|
112
115
|
summary: LoRA fine-tuning for LLMs from Ruby.
|
|
113
116
|
test_files: []
|