Skip to content

[KDA] Add Paddle training compatibility - #2

Merged
SigureMo merged 3 commits into
paddle/kda-compactfrom
agent/paddle-kda-compact
Jul 30, 2026
Merged

[KDA] Add Paddle training compatibility#2
SigureMo merged 3 commits into
paddle/kda-compactfrom
agent/paddle-kda-compact

Conversation

@huangjiyi

Copy link
Copy Markdown
Collaborator

Summary

Add a compact Paddle compatibility path for KDA training while retaining the existing Triton kernels and shared FLA implementations. The change supports dense and variable-length chunk_kda forward/backward, synchronous context parallelism, and the ShortConvolution and FusedRMSNormGated dependencies used by the KimiDeltaAttention training data flow.

Paddle compatibility is enabled explicitly by the consuming process before importing FLA. The library itself does not call paddle.enable_compat, and fla.ops.kda exposes only chunk_kda.

Test plan

  • Added tests/paddle/test_kda.py with dense save/recompute, gate, beta, state, gradient, GVA, and variable-length coverage against a Paddle eager recurrence.
  • Added tests/paddle/test_cp_kda.py with two-GPU context-parallel forward/backward checks.
  • Added tests/paddle/test_kimi_delta_attention.py for ShortConvolution, gated RMSNorm, import boundaries, and dense/masked KimiDeltaAttention training data flow.
  • CUDA_VISIBLE_DEVICES=0,1 ../.venv/bin/python -m pytest --confcutdir=tests/paddle tests/paddle -q: 12 passed.
  • pre-commit run --files <changed files>: all hooks passed.
  • Environment: NVIDIA B30Z (SM103), CUDA 13.2, Paddle 3.5.0.dev20260721, Triton 3.7.1.

Benchmark / NCU

Not run. This is a framework compatibility change and does not alter the KDA Triton kernel algorithm, tiling, or scheduling.

Breaking changes

This target branch is Paddle-only and is not intended to preserve the complete upstream PyTorch/FLA API:

  • callers must enable Paddle compatibility for the fla and triton scopes before importing FLA;
  • fla.ops.kda exports chunk_kda only;
  • optional backend dispatch is not loaded for the supported CUDA training path;
  • asynchronous KDA context-parallel all-gather and KV-cache generation are not supported.
@huangjiyi
huangjiyi marked this pull request as ready for review July 30, 2026 03:02
@huangjiyi huangjiyi closed this Jul 30, 2026
@huangjiyi
huangjiyi deleted the agent/paddle-kda-compact branch July 30, 2026 03:04
@huangjiyi
huangjiyi restored the agent/paddle-kda-compact branch July 30, 2026 03:04
@huangjiyi huangjiyi reopened this Jul 30, 2026

@SigureMo SigureMo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

预期,如下代码是可以跑通的

import paddle

paddle.enable_compat(scope={'fla', 'triton'})

import fla

paddle.disable_compat()

from fla.modules import FusedRMSNormGated, ShortConvolution  # noqa: E402
from fla.ops.kda import chunk_kda  # noqa: E402
from fla.ops.utils.index import prepare_cu_seqlens_from_mask, prepare_lens_from_mask  # noqa: E402
from fla.utils import tensor_cache  # noqa: E402

# use FusedRMSNormGated, ShortConvolution, chunk_kda, prepare_cu_seqlens_from_mask, prepare_lens_from_mask, tensor_cache
Comment thread fla/ops/backends/__init__.py Outdated
Comment on lines +152 to +167
"""Return the default implementation without optional backend routing.

This branch supports KDA training on NVIDIA GPUs through the default Triton implementation.
The optional KDA backends are:

- TileLang is deferred for now.
TODO: consider supporting it if performance is better than the Triton implementation.
- FlashKDA, which is inference-only and has no training backward.
- Triton-Ascend, which only targets Huawei NPUs.

The module registry similarly contains only Triton-Ascend overrides.
None of these backends are currently required for the supported CUDA training paths.
Those paths include dense and variable-length forward/backward and context parallelism.
Loading them would only expand the unsupported dependency and import surface.

Iterates through all registered backends and selects the first one
that passes the verifier for the given function call.
The upstream dispatch wrapper depends on ``torch.compiler.disable``, which Paddle compat does not provide.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这里不改吧

Comment on lines -167 to -186
if _DISPATCH_DISABLED:
return func
func_name = func.__name__

@wraps(func)
def wrapper(*args, **kwargs) -> Any:
# Lazy initialization of backends
BackendRegistry.ensure_initialized(operation)

registry = BackendRegistry._registries.get(operation)
if registry is None:
return func(*args, **kwargs)

# Iterate through all registered backends sorted by priority
# to find one that can handle this call
backends_list = registry._get_sorted_backends()

for be in backends_list:
# Avoid be.can_use(): its @cache wrapper breaks torch.compile tracing.
if not (be.is_available() and be.is_enabled()):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这里代码别删除吧,直接加一个 early return 就可以了

Comment thread fla/modules/conv/causal_conv1d.py Outdated
return output, None

if backend == 'triton':
from fla.modules.conv.triton import CausalConv1dFunction

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这里的 lazy import 要注意下

@SigureMo
SigureMo merged commit a4fb2d7 into paddle/kda-compact Jul 30, 2026
1 check passed
@SigureMo
SigureMo deleted the agent/paddle-kda-compact branch July 30, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants