Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / impl / causal_conv1d.py: 80%

35 statements  

« 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"""Depthwise causal convolution followed by SiLU.""" 

16 

17from __future__ import annotations 

18 

19from functools import partial 

20from types import SimpleNamespace 

21from typing import Protocol 

22 

23import torch 

24from torch.nn import functional as F 

25 

26from hyper_parallel.components.functional.linear_attention_convolution import fused_causal_conv1d, packed_causal_conv1d 

27from hyper_parallel.components.modules.attention.linear_attention_utils.sequence_layout import ( 

28 PackedSequenceInfo, 

29) 

30 

31from .._dependency import import_fla_module, require_package, require_symbol 

32from ..registry import OpRegistry 

33from ..spec import OpSpec 

34 

35 

36class CausalConv1dFn(Protocol): 

37 """Apply causal depthwise convolution to dense or document-packed tokens.""" 

38 

39 def __call__( 

40 self, x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor | None, 

41 info: PackedSequenceInfo | None = None, *, dilation: int = 1, 

42 ) -> torch.Tensor: 

43 """Apply [C,1,W] weights and SiLU, resetting at packed document boundaries.""" 

44 

45 

46CAUSAL_CONV1D = OpSpec[CausalConv1dFn](config_key="causal_conv1d") 

47 

48 

49def _native( 

50 x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor | None, 

51 info: PackedSequenceInfo | None = None, *, dilation: int = 1, 

52) -> torch.Tensor: 

53 """Apply native causal depthwise convolution and SiLU with document resets.""" 

54 if info is not None: 

55 convolution = SimpleNamespace( 

56 groups=x.shape[-1], in_channels=x.shape[-1], out_channels=x.shape[-1], stride=(1,), 

57 kernel_size=(weight.shape[-1],), dilation=(dilation,), weight=weight, bias=bias, 

58 ) 

59 return packed_causal_conv1d(x, convolution, info, weight=weight, bias=bias) 

60 padding = (weight.shape[-1] - 1) * dilation 

61 output = torch.conv1d(x.transpose(1, 2), weight, bias, padding=padding, dilation=dilation, groups=x.shape[-1]) 

62 return F.silu(output[:, :, :x.shape[1]].transpose(1, 2)) 

63 

64 

65def _load_triton() -> CausalConv1dFn: 

66 require_package("triton.backends.ascend") 

67 require_symbol(require_package("fla.modules.conv", loader=import_fla_module), "causal_conv1d") 

68 return partial(fused_causal_conv1d, backend="triton") 

69 

70 

71def _load_ascendc() -> CausalConv1dFn: 

72 kernels = require_package("fla_npu.ops.ascendc") 

73 require_symbol(kernels, "causal_conv1d_fn") 

74 require_symbol(kernels, "causal_conv1d_bwd") 

75 return partial(fused_causal_conv1d, backend="ascendc") 

76 

77 

78def register_causal_conv1d(registry: OpRegistry) -> None: 

79 """Register dense and packed short convolution with a shared Torch reference.""" 

80 registry.register_family("causal_conv1d", specs=(CAUSAL_CONV1D,)) 

81 registry.register_impl(CAUSAL_CONV1D, name="native", devices=("cpu", "cuda", "npu"), load=lambda: _native) 

82 registry.register_impl(CAUSAL_CONV1D, name="triton_ascend", devices=("npu",), load=_load_triton) 

83 registry.register_impl(CAUSAL_CONV1D, name="ascendc", devices=("npu",), load=_load_ascendc)