-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Extend scratch buffer for long prompts #2212
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4844966
Extend scratch buffer for long prompts
cmikeh2 be4714d
Merge branch 'master' into cholmes/fix-long-seq-len-inference
cmikeh2 c6411d1
Fetch correct tail buffer for batched inputs.
cmikeh2 c074ed3
Style change
cmikeh2 3135774
Merge branch 'cholmes/fix-long-seq-len-inference' of https://github.c…
cmikeh2 777a36e
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi ec6b1ad
Fix variable rename
cmikeh2 d897f98
Merge branch 'cholmes/fix-long-seq-len-inference' of https://github.c…
cmikeh2 6da9234
Merge branch 'master' into cholmes/fix-long-seq-len-inference
cmikeh2 606d344
Reduce maximum sequence length
cmikeh2 5269ba1
Merge branch 'master' into cholmes/fix-long-seq-len-inference
cmikeh2 89f2ded
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi 8e29808
Merge branch 'master' into cholmes/fix-long-seq-len-inference
cmikeh2 c824330
Add debug print
cmikeh2 d9eb076
Merge branch 'master' into cholmes/fix-long-seq-len-inference
cmikeh2 aafba00
Multi-batch inference fix
cmikeh2 4abd455
add batch-size at the tranform launch for the half-precision implemen…
603cc5b
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi 508712a
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi 51a6371
no need to throw error when there is no mask passed
9effa9e
Merge branch 'cholmes/fix-long-seq-len-inference' of github.com:micro…
c9652ec
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi d8f5203
Increasing the token-length based on available memory for GPT models …
RezaYazdaniAminabadi b64c117
Merge branch 'master' into cholmes/fix-long-seq-len-inference
RezaYazdaniAminabadi 48a8b96
fix bert issue & remove some dead code
c1d83f9
fix formating
a5f4d31
Merge branch 'master' into cholmes/fix-long-seq-len-inference
jeffra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,13 +59,17 @@ at::Tensor ds_softmax(at::Tensor& attn_scores, | |
|
|
||
| template <typename T> | ||
| void allocate_workspace(size_t hidden_dim, | ||
| size_t prompt_len, | ||
| size_t max_seq_len, | ||
| size_t batch_size, | ||
| unsigned num_layers, | ||
| size_t head_size = 128) | ||
| { | ||
| size_t _workSpaceSize = 16 * (hidden_dim * batch_size * max_seq_len) + | ||
| (num_layers * batch_size * max_seq_len * hidden_dim * 2); // KV-cache | ||
| size_t prompt_output = batch_size * head_size * prompt_len * prompt_len; | ||
| _workSpaceSize = _workSpaceSize + prompt_output; | ||
|
|
||
| Context::Instance().GenWorkSpace(_workSpaceSize * sizeof(T)); | ||
| } | ||
|
|
||
|
|
@@ -81,10 +85,13 @@ at::Tensor einsum_sec_sm_ecm(at::Tensor& Q, at::Tensor& W) | |
| float alpha = 1; | ||
| float gemm_beta = 0.0; | ||
|
|
||
| if (!workspace) { | ||
| allocate_workspace<T>(W.size(1), MAX_OUT_TOKES, Q.size(0), 1); | ||
| /* | ||
| // Reallocate memory if we received a new prompt | ||
| if (!workspace || input.size(1) != 1) { | ||
| allocate_workspace<T>(W.size(1), MAX_OUT_TOKES, Q.size(0), 1, head_size); | ||
| workspace = (T*)Context::Instance().GetWorkSpace(); | ||
| } | ||
| */ | ||
|
|
||
| auto O = at::from_blob(workspace, {Q.size(1), Q.size(2), W.size(1)}, options); | ||
| unsigned m = W.size(1); | ||
|
|
@@ -305,7 +312,15 @@ void attention_unfused(T* prev_key_cont, | |
| float layer_scale = alibi.sizes().size() > 1 ? std::max(1, layer_id) : 1.0; | ||
| float alpha = norm_factor * norm_factor / layer_scale; | ||
| float gemm_beta = 0.0; | ||
| T* workspace = (T*)output + bsz * seq_len * heads * k; | ||
| T* workspace; | ||
| if (seq_len == 1) { | ||
| workspace = (T*)output + bsz * seq_len * heads * k; | ||
| } else { | ||
| // If we are doing the prompt, switch to the tail workspace | ||
| T* scratch = (T*)Context::Instance().GetWorkSpace(); | ||
| workspace = scratch + (Context::Instance().get_workspace_size() / sizeof(T)) - | ||
| heads * seq_len * seq_len; | ||
| } | ||
|
|
||
| cublasSetStream(Context::Instance().GetCublasHandle(), Context::Instance().GetCurrentStream()); | ||
| cublas_strided_batched_gemm(Context::Instance().GetCublasHandle(), | ||
|
|
@@ -591,14 +606,19 @@ std::vector<at::Tensor> ds_qkv_gemm(at::Tensor& input, | |
| at::Tensor& beta, | ||
| const float epsilon, | ||
| bool add_bias, | ||
| unsigned num_layers) | ||
| unsigned num_layers, | ||
| bool bloom_seq_len, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe change this to |
||
| unsigned head_size) | ||
| { | ||
| int bsz = input.size(0) * input.size(1); | ||
| T* workspace = (T*)Context::Instance().GetWorkSpace(); | ||
| if (!workspace) { | ||
| // Reallocate memory if we receive a new prompt | ||
| if (!workspace || input.size(1) != 1) { | ||
| cublasSetStream(Context::Instance().GetCublasHandle(), | ||
| Context::Instance().GetCurrentStream()); | ||
| allocate_workspace<T>(input.size(2), MAX_OUT_TOKES, input.size(0), num_layers); | ||
| const int max_seq_len = (bloom_seq_len) ? 128 : MAX_OUT_TOKES; | ||
| allocate_workspace<T>( | ||
| input.size(2), input.size(1), max_seq_len, input.size(0), num_layers, head_size); | ||
| workspace = (T*)Context::Instance().GetWorkSpace(); | ||
| } | ||
| auto options = at::TensorOptions() | ||
|
|
@@ -699,7 +719,8 @@ template <typename T> | |
| at::Tensor ds_linear_layer(at::Tensor& input, | ||
| at::Tensor& weight, | ||
| at::Tensor& bias, | ||
| unsigned num_layers) | ||
| unsigned num_layers, | ||
| unsigned head_size) | ||
| { | ||
| auto input_cont = input.contiguous(); | ||
| auto options = at::TensorOptions() | ||
|
|
@@ -710,10 +731,12 @@ at::Tensor ds_linear_layer(at::Tensor& input, | |
|
|
||
| int bsz = input.size(0) * input.size(1); | ||
| T* workspace = (T*)Context::Instance().GetWorkSpace(); | ||
| if (!workspace) { | ||
| // Reallocate memory if we received a new prompt | ||
| if (!workspace || input.size(1) != 1) { | ||
| cublasSetStream(Context::Instance().GetCublasHandle(), | ||
| Context::Instance().GetCurrentStream()); | ||
| allocate_workspace<T>(input.size(2), MAX_OUT_TOKES, input.size(0), num_layers); | ||
| allocate_workspace<T>( | ||
| input.size(2), input.size(1), MAX_OUT_TOKES, input.size(0), num_layers, head_size); | ||
| workspace = (T*)Context::Instance().GetWorkSpace(); | ||
| } | ||
| auto output = at::from_blob(workspace, {input.size(0), input.size(1), weight.size(1)}, options); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cmikeh2, do we want to add bsz here too, since we might get a batched input?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is primed to cause a segfault on any batched input.