Skip to content

apply flagos te_groups_gemm op - #55

Merged
lxd-cumt merged 6 commits into
flagos-ai:mainfrom
chai-xiaonan:te_groups_gemm
Mar 30, 2026
Merged

apply flagos te_groups_gemm op#55
lxd-cumt merged 6 commits into
flagos-ai:mainfrom
chai-xiaonan:te_groups_gemm

Conversation

@chai-xiaonan

@chai-xiaonan chai-xiaonan commented Mar 26, 2026

Copy link
Copy Markdown
  • add te_general_grouped_gemm op for flagos backend, base on flag_gems
  • support both forward and backward computation, distinguished by grad

def gelu_backward(grad_output, x):
# 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

import math

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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])
if grad and len(bias) > i and bias[i] is not None and bias[i].numel() != 0:
bias[i].zero_()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

bias or bias_grad?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will add annotations

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))

def gelu_backward(grad_output, x):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why not use flag_gems.gelu_backward

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

OK,Will attempt to replace and test.

return out1, bias_grad, gelu_input, extra_output_ret


def te_general_grouped_gemm_fl(

Copy link
Copy Markdown
Collaborator

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 grad parameter.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use flag_gems.to_copy to replace xxx.to

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

OK,Will attempt to replace and test.

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))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why not use flag_gems.zeros

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

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_()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.zeros

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

if D[i].numel() != 0 and not accumulate:
D[i].zero_()
else:
out = torch.zeros(A[i].shape[0], B[i].shape[1])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.zeros

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

and pre_gelu_out[i] is not None
and pre_gelu_out[i].numel() != 0
):
pre_gelu_out[i].zero_()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.zeros

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok


# Apply GELU epilogue if pre_gelu_out is provided
if has_pre_gelu:
pre_gelu_out[i].copy_(out)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.copy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok


# Compute bias gradients if requested
if has_bias:
bias_grad = out.sum(dim=0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.sum_dim

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

if has_bias:
bias_grad = out.sum(dim=0)
if accumulate:
bias[i].add_(bias_grad)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.add

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

if accumulate:
bias[i].add_(bias_grad)
else:
bias[i].copy_(bias_grad)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.copy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

if accumulate:
D[i].add_(out.to(D[i].dtype))
else:
D[i].copy_(out.to(D[i].dtype))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

as above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

else:
D[i].copy_(out.to(D[i].dtype))
else:
temp_D.append(out.to(D[0].dtype))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

try to use flag_gems.to_copy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok


# Compute bias gradients if requested
if has_bias:
bias_grad = flag_gems.sum_dim(out, dim=0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please check dim argument of flag_gems.sum_dim, is it should be a list or not

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

In the flag_gems.sum_dim operator, the dim parameter supports multiple data types for input, including None, int, and List[int].

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please check

def sum_dim_comm(inp, dim=None, keepdim=False, *, dtype=None, out=None):
    if dtype is None:
        dtype = inp.dtype
        if dtype is torch.bool:
            dtype = torch.int64

    if dim == []:
        if not keepdim:
            return sum(inp, dtype=dtype)
        else:
            dim_num = inp.ndim
            return torch.reshape(sum(inp, dtype=dtype), [1] * dim_num)

    shape = list(inp.shape)
    dim = [d % inp.ndim for d in dim]

@lxd-cumt lxd-cumt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

print(f" ✓ {backend_name}")
except NotImplementedError:
self.skipped += 1
print(f" �~J~X {backend_name} (not implemented)")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please format

print(f" ✓ {backend_name}")
except NotImplementedError:
self.skipped += 1
print(f" �~J~X {backend_name} (not implemented)")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please format

@lxd-cumt
lxd-cumt merged commit 2188137 into flagos-ai:main Mar 30, 2026
10 checks passed
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