Skip to content
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

Adding rwkv_eval_array operation #49

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rwkv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,20 @@ bool rwkv_eval(struct rwkv_context * ctx, int32_t token, float * state_in, float
return true;
}

bool rwkv_eval_array(struct rwkv_context * ctx, int32_t * token_arr, int n_tokens, float * state_in, float * state_out, float * logits_out) {
bool success = true;
for (int i = 0; i < n_tokens; i++) {
success = success && rwkv_eval(ctx, token_arr[i], state_in, state_out, logits_out);
state_in = state_out; // switch to using state_out from here onwards

// If success fail, abort now
if( success == false ) {
return false;
}
}
return success;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose following implementation:

bool rwkv_eval_sequence(struct rwkv_context * ctx, int32_t * token_sequence, uint32_t n_tokens, float * state_in, float * state_out, float * logits_out) {
    RWKV_ASSERT_FALSE(n_tokens < 1, "Token count must be positive, got %d", n_tokens);

    for (uint32_t i = 0; i < n_tokens; i++) {
        if (!rwkv_eval(ctx, token_sequence[i], i == 0 ? state_in : state_out, state_out, logits_out)) {
            return false;
        }
    }

    return true;
}
  • it looks simpler to me
  • I would prefer to use term sequence instead of array
  • token count made unsigned and with specific size (IDK what exactly int is in C and try to avoid it lol)
  • added token count correctness check, so users don't try to process zero tokens

Function signature in rwkv.h should be changed accordingly.


void rwkv_free(struct rwkv_context * ctx) {
ctx->model->layers.~vector();
free(ctx->model);
Expand Down
10 changes: 10 additions & 0 deletions rwkv.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ extern "C" {
// - logits_out: FP32 buffer of size rwkv_get_logits_buffer_element_count. This buffer will be written to.
RWKV_API bool rwkv_eval(struct rwkv_context * ctx, int32_t token, float * state_in, float * state_out, float * logits_out);

// Evaluates the model for multiple token in an array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Evaluates the model for multiple token in an array
// Evaluates the model for a sequence of tokens.

// Returns false on any error. Error messages would be printed to stderr.
//
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//

// - tokens: next token array
// - n_tokens: number of tokens in the token array
// - state_in: FP32 buffer of size rwkv_get_state_buffer_element_count; or NULL, if this is a first pass.
// - state_out: FP32 buffer of size rwkv_get_state_buffer_element_count. This buffer will be written to.
// - logits_out: FP32 buffer of size rwkv_get_logits_buffer_element_count. This buffer will be written to.
RWKV_API bool rwkv_eval_array(struct rwkv_context * ctx, int32_t * token_arr, size_t tokens_count, float * state_in, float * state_out, float * logits_out);

// Returns count of FP32 elements in state buffer.
RWKV_API uint32_t rwkv_get_state_buffer_element_count(struct rwkv_context * ctx);

Expand Down