Skip to content

Commit

Permalink
RWLock for callbacks (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
cassanof authored Sep 21, 2023
1 parent 81d0802 commit 40c0d3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ int llama_predict(void* params_ptr, void* state_pr, char* result, bool debug) {

for (auto id : embd) {
const std::string token_str = llama_token_to_piece(ctx, id);
printf("%s", token_str.c_str());
if (debug) {
printf("%s", token_str.c_str());
}

if (embd.size() > 1) {
input_tokens.push_back(id);
Expand Down
9 changes: 5 additions & 4 deletions llama.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ func (l *LLama) Predict(text string, opts ...PredictOption) (string, error) {
return res, nil
}

// tokenize has an interesting return property: negative lengths (potentially) have meaning. Therefore, return the length seperate from the slice and error - all three can be used together
// tokenize has an interesting return property: negative lengths (potentially) have meaning.
// Therefore, return the length seperate from the slice and error - all three can be used together
func (l *LLama) TokenizeString(text string, opts ...PredictOption) (int32, []int32, error) {
po := NewPredictOptions(opts...)

Expand Down Expand Up @@ -396,14 +397,14 @@ func (l *LLama) SetTokenCallback(callback func(token string) bool) {
}

var (
m sync.Mutex
m sync.RWMutex
callbacks = map[uintptr]func(string) bool{}
)

//export tokenCallback
func tokenCallback(statePtr unsafe.Pointer, token *C.char) bool {
m.Lock()
defer m.Unlock()
m.RLock()
defer m.RUnlock()

if callback, ok := callbacks[uintptr(statePtr)]; ok {
return callback(C.GoString(token))
Expand Down

0 comments on commit 40c0d3d

Please sign in to comment.