-
Notifications
You must be signed in to change notification settings - Fork 30
apply flagos te_groups_gemm op #55
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
Changes from 1 commit
28ad5f9
ec04e41
0c497bc
4ac418b
b072c6a
686d29f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| __all__ = [ | ||
| "generic_gemm_fl", | ||
| "te_general_grouped_gemm_fl", | ||
| ] | ||
|
|
||
| _DTYPE_TO_TORCH = { | ||
|
|
@@ -115,3 +116,107 @@ def generic_gemm_fl( | |
| return D, bias_grad, gelu_input, extra_output_ret | ||
| else: | ||
| return out1, bias_grad, gelu_input, extra_output_ret | ||
|
|
||
|
|
||
| def te_general_grouped_gemm_fl( | ||
| B: List[torch.Tensor], | ||
| transb: bool, | ||
| A: List[torch.Tensor], | ||
| transa: bool, | ||
| D: Optional[List[torch.Tensor]], | ||
| D_type: Any, | ||
| m_splits: List[int], | ||
| bias: List[torch.Tensor], | ||
| bias_type: Any, | ||
| single_output: bool, | ||
| pre_gelu_out: List[torch.Tensor], | ||
| grad: bool, | ||
| workspace: List[torch.Tensor], | ||
| workspaceSize: int, | ||
| accumulate: bool, | ||
| use_split_accumulator: bool, | ||
| math_sm_count: int, | ||
| ) -> Optional[List[torch.Tensor]]: | ||
| if single_output and D is None: | ||
| raise ValueError("not implemented, D should be allocated for single output case.") | ||
|
|
||
| num_gemms = len(A) | ||
| if D is None: | ||
| D = [] | ||
| for i in range(num_gemms): | ||
| m = A[i].shape[1] if transa else A[i].shape[0] | ||
| n = B[i].shape[0] if transb else B[i].shape[1] | ||
| D.append(torch.empty((m, n), dtype=D[i].dtype, device=A[0].device)) | ||
|
Collaborator
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. why not use flag_gems.zeros
Author
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. ok |
||
|
|
||
| def gelu_backward(grad_output, x): | ||
|
Collaborator
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. why not use flag_gems.gelu_backward
Author
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. OK,Will attempt to replace and test. |
||
| # Approximation of GELU derivative commonly used in Transformer Engine | ||
| cdf = 0.5 * (1.0 + flag_gems.erf(x / flag_gems.sqrt(2.0))) | ||
| pdf = flag_gems.exp(-0.5 * x * x) / flag_gems.sqrt(2.0 * math.pi) | ||
|
Collaborator
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. import math
Author
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. ok |
||
| return flag_gems.mul(grad_output, (cdf + x * pdf)) | ||
|
|
||
| temp_D = [] | ||
| for i in range(num_gemms): | ||
| # Handle the special case of zero-element inputs | ||
| if A[i].numel() == 0 or B[i].numel() == 0: | ||
| if not single_output: | ||
| if D[i].numel() != 0 and not accumulate: | ||
| D[i].zero_() | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| else: | ||
| out = torch.zeros(A[i].shape[0], B[i].shape[1]) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| if grad and len(bias) > i and bias[i] is not None and bias[i].numel() != 0: | ||
| bias[i].zero_() | ||
|
Collaborator
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. bias or bias_grad?
Author
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. Will add annotations |
||
| if ( | ||
| len(pre_gelu_out) > i | ||
| and pre_gelu_out[i] is not None | ||
| and pre_gelu_out[i].numel() != 0 | ||
| ): | ||
| pre_gelu_out[i].zero_() | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| continue | ||
| a = A[i].t() if transa else A[i] | ||
| b = B[i].t() if transb else B[i] | ||
| # Determine presence of epilogue tensors | ||
| has_bias = len(bias) > i and bias[i] is not None and bias[i].numel() > 0 | ||
| has_pre_gelu = ( | ||
| len(pre_gelu_out) > i and pre_gelu_out[i] is not None and pre_gelu_out[i].numel() > 0 | ||
| ) | ||
|
|
||
| # Forward Pass calculation | ||
| if not grad: | ||
| if has_bias: | ||
| # Fused matrix multiplication and bias addition | ||
| out = flag_gems.addmm(bias[i], a, b) | ||
| else: | ||
| out = flag_gems.mm(a, b) | ||
|
|
||
| # Apply GELU epilogue if pre_gelu_out is provided | ||
| if has_pre_gelu: | ||
| pre_gelu_out[i].copy_(out) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| out = flag_gems.gelu(out) | ||
| else: | ||
| out = flag_gems.mm(a, b) | ||
| if has_pre_gelu: | ||
| out = gelu_backward(out, pre_gelu_out[i]) | ||
|
|
||
| # Compute bias gradients if requested | ||
| if has_bias: | ||
| bias_grad = out.sum(dim=0) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| if accumulate: | ||
| bias[i].add_(bias_grad) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
| else: | ||
| bias[i].copy_(bias_grad) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
|
|
||
| if not single_output: | ||
| # Store output | ||
| if accumulate: | ||
| D[i].add_(out.to(D[i].dtype)) | ||
|
Collaborator
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. use
Author
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. OK,Will attempt to replace and test. |
||
| else: | ||
| D[i].copy_(out.to(D[i].dtype)) | ||
|
Collaborator
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. as above
Author
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. ok |
||
| else: | ||
| temp_D.append(out.to(D[0].dtype)) | ||
|
Collaborator
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. try to use
Author
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. ok |
||
|
|
||
| if single_output: | ||
| if temp_D: | ||
| temp = torch.cat(temp_D, dim=0) | ||
| D[0].copy_(temp) | ||
|
|
||
| return bias | ||
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.
Please add a comment indicating that this function can represent both forward computation and backward computation, distinguished by the
gradparameter.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.
OK