Skip to content

Commit 36f852b

Browse files
committed
Initialize the sampler only once
1 parent be93ebb commit 36f852b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

libraries/YarpPlugins/LlamaGPT/DeviceDriverImpl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ bool LlamaGPT::open(yarp::os::Searchable & config)
9999
}
100100
}
101101

102+
// initialize the sampler
103+
smpl = llama_sampler_chain_init(llama_sampler_chain_default_params());
104+
llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
105+
102106
return true;
103107
}
104108

@@ -108,6 +112,12 @@ bool LlamaGPT::close()
108112
{
109113
bool ret = deleteConversation();
110114

115+
if (smpl)
116+
{
117+
llama_sampler_free(smpl);
118+
smpl = nullptr;
119+
}
120+
111121
if (model)
112122
{
113123
llama_model_free(model);

libraries/YarpPlugins/LlamaGPT/ILLMImpl.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,6 @@ bool LlamaGPT::ask(const std::string & question, yarp::dev::LLM_Message & answer
167167
#endif
168168
}
169169

170-
// initialize the sampler
171-
172-
auto sparams = llama_sampler_chain_default_params();
173-
sparams.no_perf = true;
174-
175-
llama_sampler * smpl = llama_sampler_chain_init(sparams);
176-
177-
llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
178-
179170
// prepare a batch for the prompt
180171
llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
181172

@@ -228,7 +219,6 @@ bool LlamaGPT::ask(const std::string & question, yarp::dev::LLM_Message & answer
228219
}
229220
}
230221

231-
llama_sampler_free(smpl);
232222
llama_free(ctx);
233223

234224
yCDebug(LLAMA) << "Generated:" << out;

libraries/YarpPlugins/LlamaGPT/LlamaGPT.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class LlamaGPT : public yarp::dev::DeviceDriver,
5353

5454
private:
5555
llama_model * model {nullptr};
56+
llama_sampler * smpl {nullptr};
57+
5658
std::vector<llama_chat_message> conversation;
5759
};
5860

0 commit comments

Comments
 (0)