33#include < algorithm>
44#include < cassert>
55#include < cstring>
6+ #include < stdexcept>
67#include < thread>
8+ #include < vector>
79
810#include < fildesh/fildesh.h>
911#include < fildesh/ostream.hh>
@@ -122,6 +124,20 @@ rendezllama::make_llama_context(rendezllama::ChatOptions& opt)
122124 if (opt.context_token_limit == 0 ) {
123125 opt.context_token_limit = opt.model_token_limit ;
124126 }
127+ float rope_freq_scale = llama_model_rope_freq_scale_train (model);
128+ if (rope_freq_scale <= 0.0 ) {
129+ rope_freq_scale = 1 .0f ;
130+ }
131+ while (
132+ (unsigned )(opt.model_token_limit / rope_freq_scale)
133+ <
134+ opt.context_token_limit )
135+ {
136+ rope_freq_scale /= 2 ;
137+ }
138+ llama_model_free (model);
139+ model = nullptr ;
140+
125141
126142 model_params = llama_model_default_params ();
127143 model_params.use_mlock = opt.mlock_on ;
@@ -130,14 +146,33 @@ rendezllama::make_llama_context(rendezllama::ChatOptions& opt)
130146 llama_context_params ctx_params = llama_context_default_params ();
131147 ctx_params.n_ctx = opt.context_token_limit ;
132148 ctx_params.n_batch = opt.batch_count ;
133- ctx_params.rope_freq_scale = llama_model_rope_freq_scale_train (model);
134- assert (ctx_params.rope_freq_scale > 0.0 );
135- while (
136- (unsigned )(opt.model_token_limit / ctx_params.rope_freq_scale )
137- <
138- opt.context_token_limit )
139- {
140- ctx_params.rope_freq_scale /= 2 ;
149+ ctx_params.rope_freq_scale = rope_freq_scale;
150+
151+ std::vector<float > tensor_split (llama_max_devices ());
152+ std::vector<llama_model_tensor_buft_override> tensor_buft_overrides (llama_max_tensor_buft_overrides ());
153+ std::vector<size_t > margins (llama_max_devices (), 0 );
154+
155+ // Auto-tune parameters if possible (and not manually overridden by user yet).
156+ // This helps avoid OOM crashes on Vulkan/GPU by fitting layers to available memory.
157+ auto status = llama_params_fit (
158+ opt.model_filename .c_str (),
159+ &model_params,
160+ &ctx_params,
161+ tensor_split.data (),
162+ tensor_buft_overrides.data (),
163+ margins.data (),
164+ /* n_ctx_min=*/ 0 ,
165+ GGML_LOG_LEVEL_ERROR );
166+
167+ if (status != 0 ) {
168+ fildesh_log_warning (" llama_params_fit failed" );
169+ }
170+
171+ model = llama_model_load_from_file (
172+ opt.model_filename .c_str (), model_params);
173+ if (!model) {
174+ fildesh_log_error (" Failed to open model." );
175+ return std::make_tuple (nullptr , nullptr );
141176 }
142177
143178 struct llama_context * ctx = llama_init_from_model (model, ctx_params);
@@ -367,14 +402,6 @@ Inference::commit_to_context(
367402 opt.batch_count ,
368403 chat_traj.token_count () - chat_traj.context_token_count_ );
369404
370- #if LLAMA_OPENBLAS_ON
371- if (n < 32 ) {
372- llama_set_n_threads (ctx, thread_count_, batch_thread_count_);
373- }
374- else {
375- llama_set_n_threads (ctx, thread_count_, 1 );
376- }
377- #endif
378405 chat_disp.show_new (chat_traj.context_token_count_ + n, chat_traj, vocabulary_);
379406
380407 if (!batch_.token || (unsigned )batch_.n_tokens < n) {
@@ -445,4 +472,3 @@ Inference::sample_to_trajectory(
445472 llama_sampler_accept (smpl_, chat_traj.token ());
446473 token_count_ += 1 ;
447474}
448-
0 commit comments