Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 809
[PyTorch] Custom kernel to compute reciprocal of a single float#1016
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b4ea12b865775fa5e53e71a589e223258bbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -117,10 +117,12 @@ def forward( | ||
| inputmat = cast_if_needed(inputmat, activation_dtype) | ||
| inputmat_t = None | ||
| inputmat_no_fp8 = inputmat | ||
| inputmat_fp8_scale_inv = None | ||
| if fp8: | ||
| fp8_dtype_forward = get_fp8_te_dtype(fp8_meta["recipe"], fprop_tensor=True) | ||
| if isinstance(inputmat, Float8Tensor): | ||
| inputmat_fp8_scale_inv = inputmat._scale_inv | ||
| if ( | ||
| not fp8_meta["recipe"].override_linear_precision.wgrad | ||
| and is_grad_enabled | ||
| @@ -152,6 +154,12 @@ def forward( | ||
| fp8_dtype_forward, | ||
| ) | ||
| # FP8 scale-inverse | ||
| inputmat_fp8_scale_inv = tex.scalar_reciprocal( | ||
| fp8_meta["scaling_fwd"].scale, | ||
| src_offset=int(tex.FP8FwdTensors.GEMM1_INPUT), | ||
| ) | ||
| # Column Parallel Linear | ||
| if parallel_mode == "column" and sequence_parallel: | ||
| inputmat_total, _ = gather_along_first_dim(inputmat, tp_group) | ||
| @@ -222,8 +230,8 @@ def forward( | ||
| if isinstance(inputmat_total, Float8Tensor) | ||
| else inputmat_total | ||
| ), | ||
| fp8_meta["scaling_fwd"].scale_inv, | ||
| tex.FP8FwdTensors.GEMM1_INPUT, | ||
| inputmat_fp8_scale_inv, | ||
| 0, | ||
| fp8_dtype_forward, | ||
| proj_out_pttype, | ||
| get_workspace(), | ||
| @@ -335,7 +343,7 @@ def forward( | ||
| weight, | ||
| weight_fp8, | ||
| weight.main_grad if cpu_offloading and fuse_wgrad_accumulation else None, | ||
| fp8_meta["scaling_fwd"].scale_inv.clone() if fp8 else None, | ||
| inputmat_fp8_scale_inv, | ||
| ) | ||
| ctx.activation_dtype = activation_dtype | ||
| @@ -388,7 +396,7 @@ def backward(ctx, grad_output: torch.Tensor) -> Tuple[Union[torch.Tensor, None], | ||
| weight, | ||
| weight_fp8, | ||
| main_grad, | ||
| fwd_scale_inverses, | ||
| inputmat_fp8_scale_inv, | ||
| ) = ctx.saved_tensors | ||
| # Gather intermediate/activation tensors if needed | ||
| @@ -545,8 +553,8 @@ def backward(ctx, grad_output: torch.Tensor) -> Tuple[Union[torch.Tensor, None], | ||
| if isinstance(inputmat_t_total, Float8Tensor) | ||
| else inputmat_t_total | ||
| ), | ||
| fwd_scale_inverses, | ||
| tex.FP8FwdTensors.GEMM1_INPUT, | ||
| inputmat_fp8_scale_inv, | ||
| 0, | ||
Member 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? MemberAuthor 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.
| ||
| fp8_dtype_forward, | ||
| grad_output_t, | ||
| ctx.fp8_meta["scaling_bwd"].scale_inv, | ||
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.
This is only set for float8tensor case
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.
We handle three cases:
NoneTransformerEngine/transformer_engine/pytorch/module/linear.py
Line 120 in 23258bb
Float8Tensorinput: scale-inv is taken fromFloat8TensorTransformerEngine/transformer_engine/pytorch/module/linear.py
Line 125 in 23258bb
Float8Tensorinput: scale-inv is computed with fast kernelTransformerEngine/transformer_engine/pytorch/module/linear.py
Line 158 in 23258bb