-
Notifications
You must be signed in to change notification settings - Fork 12.4k
finetune.cpp command-line arg #13873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
perhaps no need to review until i have an actual SGD impl in a follow-on, @JohannesGaessler - but a few general questions about contributing:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should better keep that change as it time to get more feedbacks/approval.
Any changes made to the ggml source in this repository will eventually be synced to the ggml repository and vice versa; it is completely fine. I think the issue of a git submodule was previously brought up and rejected.
My opinion is that people serious about training should be writing a program rather than use a command line tool. Still, I think it's good to make things such as the learning rate configurable in the provided example program.
I don't remember whether those args were put in by me when I copypasted code or by Georgi when he later refactored it but I myself definitely did not make an intentional choice to use these exact arguments.
I don't know, sorry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the previous perplexity-specific arguments are needed.
For adding an SDG optimizer, add a new ggml op like |
yes, will do. should the actual SGD impl be a subsequent pull req (or several, e.g. starting first w/ just CPU impl) or do you want it all in one pull req? |
Either way would be fine with me as long as there are at no point broken or unfinished features on master. |
e752031
to
e689af8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking forward to the next PR(s).
you should see frivolous clang-format changes (using the project's .clang-format) only on lines changed in the PR (using git-clang-format). if there's something undesireable we could figure out what in the format config does it |
Don't autoformat code en masse unless it's done in a dedicated PR, it makes it unnecessarily difficult to track what was actually changed in a PR. |
Sorry, I didn't read the
part. |
7534bbf
to
48a16bf
Compare
Hi @WilliamTambellini @JohannesGaessler I think this is usable now, inviting code nitpicks etc :) |
Second (actual usable SGD) commit is 48a16bf (also shows above here) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mix up different projects: change of CLI/renaming and SGD. Need to split in 2 PRs.
@slaren ?
ggml/src/ggml-opt.cpp
Outdated
@@ -770,7 +814,7 @@ void ggml_opt_eval(ggml_opt_context_t opt_ctx, ggml_opt_result_t result) { | |||
// beta1, beta2 after applying warmup | |||
const float beta1h = 1.0f/(1.0f - powf(opt_pars.adamw.beta1, opt_ctx->iter)); | |||
const float beta2h = 1.0f/(1.0f - powf(opt_pars.adamw.beta2, opt_ctx->iter)); | |||
|
|||
const float keep = 1.0f - opt_pars.adamw.alpha * opt_pars.adamw.wd; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimizer steps are going to be I/O bound and optimizing compute is not going to make a meaningful difference for the runtime of the steps, for the runtime of the total probram it's completely negligible. So please revert this change, I think the other variant is easier to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it's not likely to matter, but it's 1. per parameter per epoch (ok, does seem unimportant now that I think further) and 2. i'm not confident cuda CC optimizes this and was hoping to learn more - would seem possible that w/o this we're loading repeatedly two floats instead of one - and mostly 3. this is exactly following precedent established for beta1h and beta2h, which are stored in the tensor just as i stored this quantity.
Anyway, totally willing, just curious what you think about the existing practice of saving beta1h and beta2h in light of this opinion that we're not compute bound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i checked it out - doesn't seem to change runtime noticeably as you predicted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My biggest concern with the code is the amount of effort needed to maintain it, particularly when it comes to debugging and asserting that the code on master works correctly. It is quite likely that I will at some point be in a situation where a user reports bad training results and I will not know whether that is the due to a bug in ggml or due to bad hyperparamters or something similar. So it is very important to me that the data layout is consistent across multiple levels.
The correct way to implement the micro-optimization of pre-computing a parameter derived from the human-interpretable parameters is as follows:
- Pass the human-interpretable parameters to
ggml_opt_step_adamw
/ggml_opt_step_sdg
. - In the CUDA host code, pre-compute some derived parameters from the human-interpretable parameters.
- Change the CUDA device code to accept the derived parameters instead.
The way CUDA works is that the CPU schedules the GPU kernels in a CUDA stream and then waits for said stream to finish all kernels. Scheduling the kernels is of course much faster and it doesn't matter how fast you are as long as you are fast enough to keep the GPU busy. So adding a bit of overhead to the scheduling has essentially no impact on the runtime of a CUDA program even if you do it once per CUDA kernel launch instead of once per epoch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining all that, the bottom line for me is that you were right and the micro-optimization has no visible benefit in this case.
fixed: another unreachable break, windows strcasecmp, skip SGD test-opt case (keep ADAMW) for vulkan. should (nearly) pass CI now |
@graehl need fix windows builds)) |
D:\a\llama.cpp\llama.cpp\common\common.cpp(1571,39): error: dllimport cannot be applied to non-inline function definition |
fixed windows dll GGML_API build |
@graehl fail on build on some windows cases |
I'll see if I can figure it out |
seems test-opt can't link on windows (test-opt.obj : error LNK2019: unresolved external symbol ggml_backend_is_cpu referenced in function main [D:\a\llama.cpp\llama.cpp\build\tests\test-opt.vcxproj] |
do we care about the vulkan test-backend-ops timing out? seems nothing is actually wrong ... |
The Vulkan issue should be fixed with #14574 . |
I believe the windows link issue is fixed by virtue of removing the code in test-opt on windows that attempts to set the # of threads for the cpu backend. Can we see QA? |
common/common.h
Outdated
struct lr_opt lr; | ||
enum ggml_opt_optimizer_type optimizer = GGML_OPT_OPTIMIZER_TYPE_ADAMW; | ||
float val_split = 0.05f; // fraction of the data used for the validation set | ||
std::string opt_save_model_to = "finetuned-model.gguf"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::string opt_save_model_to = "finetuned-model.gguf"; |
I think you forgot to remove this?
ggml/src/ggml-opt.cpp
Outdated
if (0) | ||
GGML_LOG_DEBUG("%s static=%d accumulate=%d opt_period=%d optimizer=%d\n", __func__, (int32_t)opt_ctx->static_graphs, (int32_t)accumulate, (int32_t)opt_ctx->opt_period, (int32_t)optimizer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (0) | |
GGML_LOG_DEBUG("%s static=%d accumulate=%d opt_period=%d optimizer=%d\n", __func__, (int32_t)opt_ctx->static_graphs, (int32_t)accumulate, (int32_t)opt_ctx->opt_period, (int32_t)optimizer); |
Forgot to remove this?
ggml/src/ggml-opt.cpp
Outdated
GGML_ASSERT(result->opt_period >= 1); | ||
|
||
result->static_graphs = result->ctx_compute; | ||
GGML_LOG_DEBUG("%s opt_period=%d\n", __func__, (int32_t)result->opt_period); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GGML_LOG_DEBUG("%s opt_period=%d\n", __func__, (int32_t)result->opt_period); |
Forgot to remove this?
GGML_ASSERT(opt_pars.sgd.wd >= 0.0f); | ||
GGML_ASSERT(opt_pars.sgd.wd <= 1.0f); | ||
float * sgd = ggml_get_data_f32(opt_ctx->adamw_params); | ||
sgd[1] = 1. - (sgd[0] = opt_pars.sgd.alpha) * opt_pars.sgd.wd; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inconsistent with the docstring in ggml.h
. As I outlined before for AdamW, the interface in ggml.h
should be using the human-readable parameters. Please simply pass alpha
and wd
here. A derived parameter keep
should be calculated in the backend-specific implementations for OPT_STEP_SGD
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine by me but i'm holding off for now
ggml/src/ggml-vulkan/ggml-vulkan.cpp
Outdated
case GGML_OP_OPT_STEP_ADAMW: | ||
case GGML_OP_OPT_STEP_SGD: | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case GGML_OP_OPT_STEP_ADAMW: | |
case GGML_OP_OPT_STEP_SGD: | |
return true; | |
case GGML_OP_OPT_STEP_ADAMW: | |
return true. | |
case GGML_OP_OPT_STEP_SGD: | |
return false; |
There is no working Vulkan implementation for OPT_STEP_SGD
so this function should return false, the CPU backend will then be used as a fallback. It is not necessary to make any further changes for the Vulkan backend.
tests/test-backend-ops.cpp
Outdated
char const* name = ggml_backend_name(backend); | ||
bool const vulkan = strstr(name, "ulkan"); | ||
bool const sgd = !vulkan; | ||
|
||
if (mode == MODE_TEST) { | ||
auto test_cases = make_test_cases_eval(); | ||
auto test_cases = make_test_cases_eval(sgd); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this logic if the Vulkan backend simply returns that OPT_STEP_SGD
is unsupported, the corresponding test will then simply be skipped.
tests/test-opt.cpp
Outdated
helper_after_test_forward_backward(__func__, high_level, shuffle, "weights_after_forward_backward", subtest_ok, ntest, npass); | ||
const bool subtest_ok = weights == -ndata * .5; | ||
TEST_LOG("%s: ndata=%d weights=%f\n", __func__, (int) ndata, (double) weights); | ||
assert(subtest_ok); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(subtest_ok); |
Forgot to remove this?
tests/test-opt.cpp
Outdated
@@ -417,75 +450,76 @@ static std::pair<int, int> test_forward_backward( | |||
double loss_unc; | |||
ggml_opt_result_loss(cd.result, &loss, &loss_unc); | |||
subtest_ok = subtest_ok && loss == 18.0 && (shuffle || loss_unc == 0.0); | |||
assert(subtest_ok); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(subtest_ok); |
tests/test-opt.cpp
Outdated
|
||
double accuracy; | ||
double accuracy_unc; | ||
ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc); | ||
subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc); | ||
assert(subtest_ok); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(subtest_ok); |
tests/test-opt.cpp
Outdated
if (optim == GGML_OPT_OPTIMIZER_TYPE_SGD && !strcmp(devname, "Vulkan0")) | ||
// TODO | ||
continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (optim == GGML_OPT_OPTIMIZER_TYPE_SGD && !strcmp(devname, "Vulkan0")) | |
// TODO | |
continue; |
The mac CI test seems unrelated (timeout - just increase it above 900s on this platform?) https://github.com/ggml-org/llama.cpp/actions/runs/16152514813/job/45670340308?pr=13873 27 - test-thread-safety (Timeout) main |
I think we should skip the SGD part of test-opt on vulkan as I had it (I do not know how to fix this; I don't have a local vulkan backend machine): |
56ca1c4
to
61cc635
Compare
I took some guesses about disabling allocation in vulkan/SGD. perhaps they will work if you run CI |
add unit tested GGML_OPT_OPTIMIZER_SGD to ggml - avoids allocating m, v tensors. support finetune.cpp arg -opt SGD (or sgd). (default adamw as before) llama 3.2-1b-F32 result: observed 11gb gpu ram (41 sec/epoch) when using SGD instead of 19gb (55 sec/epoch) using adamw. (wikipedia 100 lines finetune) ( using the same GPU memory, adamw can only do before OOM 512 batch/context, reaching: train: [███████▉] data=0000140/0000140 loss=0.02575±0.00099 acc=99.52±0.03% t=00:00:47 ETA=00:00:00 val: [███████▉] data=0000008/0000008 loss=4.76565±0.28810 acc=41.46±0.77% t=00:00:00 ETA=00:00:00 SGD is superior, though it converges slower, with max before OOM 1728 batch/context (esp see the better validation perf): train: [███████▉] data=0000039/0000039 loss=0.00371±0.00010 acc=99.96±0.01% t=00:00:41 ETA=00:00:00 val: [███████▉] data=0000003/0000003 loss=5.11406±0.76034 acc=48.01±0.69% t=00:00:01 ETA=00:00:00 ) note: when finetuning long enough (or w/ enough -lr), validation accuracy *eventually* drops ('catastrophic forgetting') -lr-half (halflife) option useful for SGD to avoid oscillation or super slow underdamped learning (makes setting -lr more forgiving). terminal -lr for now is set by lr-halvings i.e. if you want at most 1/8 the inital -lr you set -lr-halvings 3. note: objective loss not directly comparable between adamw, sgd? - check perplexity or accuracy or consider relative improvements for convergence new finetune args -wd 1e-9 to enable weight decay in sgd or adamw, and max -epochs N (default 2 as before) cache (1 - wd*alpha) in 'adamw' opt struct - no noticeable perf benefit, disabled (still done for new SGD though) since opt. memory is pre-allocated, the ggml_opt_get_optimizer_params would probably be able to change between SGD and AdamW with each epoch but would need to use adamw for the first (unconfirmed - no cmdline arg to set such a policy yet) test-opt checks adamw as before and now sgd (except for a few disabled tests for sgd only; probably just needs logging values and adding alternate reference values); tolerance on the 'regression' test is broader for sgd (so we don't need many more epochs)
add to ggml-opt learning rate (adamw alpha) cmdline arg, and an optimizer enum defaulting to adamw,
preparatory to work to support SGD
these are in common args a set of optimizer options active only for the new FINETUNE example (which includes all the previous finetune.cpp PERPLEXITY options as a precaution)
perhaps breaking with precedent, the ggml_opt_optimizer_params struct is included directly as args - if desired, we can instead just add learning rate and optimizer type to a struct independent of ggml-opt.h
as proposed in
#13835