Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / impl / gdn.py: 79%
33 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Local zero-state Gated DeltaNet over dense or packed tokens."""
17from __future__ import annotations
19from typing import Protocol
21import torch
23from hyper_parallel.components.modules.attention.linear_attention_utils.sequence_layout import PackedSequenceInfo
24from hyper_parallel.components.functional.gated_delta_net import (
25 chunk_gated_delta_rule,
26 is_triton_gdn_available,
27)
29from .._dependency import require_package, require_symbol
30from ..registry import ImplementationUnavailable, OpRegistry
31from ..spec import OpSpec
34class GdnFn(Protocol):
35 """Compute GDN without reading or returning recurrent state."""
37 def __call__(
38 self, x: torch.Tensor, key: torch.Tensor, value: torch.Tensor, gate: torch.Tensor, beta: torch.Tensor,
39 *, chunk_size: int = 64, normalize_qk: bool = True, info: PackedSequenceInfo | None = None,
40 ) -> torch.Tensor:
41 """Apply local GDN to effective gate and beta inputs, resetting between documents."""
44GDN = OpSpec[GdnFn](config_key="gdn", variant="zero_state")
47def _implementation(backend: str) -> GdnFn:
48 """Bind the GDN backend and translate optional packed document boundaries."""
49 def run(
50 x: torch.Tensor, key: torch.Tensor, value: torch.Tensor, gate: torch.Tensor, beta: torch.Tensor,
51 *, chunk_size: int = 64, normalize_qk: bool = True, info: PackedSequenceInfo | None = None,
52 ) -> torch.Tensor:
53 """Run zero-state GDN with per-call document boundaries."""
54 offsets = None
55 if info is not None:
56 offsets = (info.materialize(x.device, chunk_size).cu_seqlens if backend == "triton" else info.local_offsets)
57 output, _ = chunk_gated_delta_rule(
58 x, key, value, gate, beta, chunk_size=chunk_size, use_qk_l2norm_in_kernel=normalize_qk,
59 backend=backend, cu_seqlens=offsets,
60 )
61 return output
62 return run
65def _load_triton() -> GdnFn:
66 if not is_triton_gdn_available():
67 raise ImplementationUnavailable("requires triton-ascend >= 3.2.1")
68 return _implementation("triton")
71def _load_ascendc() -> GdnFn:
72 kernels = require_package("fla_npu.ops.ascendc")
73 require_symbol(kernels, "chunk_gated_delta_rule_fwd")
74 require_symbol(kernels, "chunk_gated_delta_rule_bwd")
75 return _implementation("ascendc")
78def register_gdn(registry: OpRegistry) -> None:
79 """Register the shared GDN contract and its optional NPU kernels."""
80 registry.register_family("gdn", specs=(GDN,))
81 registry.register_impl(GDN, name="native", devices=("cpu", "cuda", "npu"), load=lambda: _implementation("eager"))
82 registry.register_impl(GDN, name="triton_ascend", devices=("npu",), load=_load_triton)
83 registry.register_impl(GDN, name="ascendc", devices=("npu",), load=_load_ascendc)