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

Conversation

PicoCreator
Copy link
Contributor

Adding a varient of rwkv_eval as rwkv_eval_array for array operations.

This is useful for X language bindings, where we can eval a larger context, without switching back and forth between X language and C lang context. (I am currently working on a nodejs binding)

Subsequently, if you do add support for "transformer" mode, this should use the "transformer" mode (i dun see the point to doing so though, for CPU eval)

}
}
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.

@@ -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
// 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
//

@@ -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.

@saharNooby
Copy link
Collaborator

Please add test case for this new function into tests/test_tiny_rwkv.c. Probably, void test_model(...) can be extended with a boolean arg whether to use regular eval or eval_sequence.

@saharNooby
Copy link
Collaborator

It would also be useful to add corresponding method to rwkv/rwkv_cpp_shared_library.py, but this is not a blocker for merge -- until we have really optimized sequence processing mode, overhead when calling C from Python side in a loop seems insignificant.

@LoganDark
Copy link
Contributor

This is no longer necessary after #89

@saharNooby saharNooby closed this Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants