Skip to content
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

refactor python code #133

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions csrc/extensions.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023, DeepLink.

Check notice on line 1 in csrc/extensions.cpp

View workflow job for this annotation

GitHub Actions / clang-format

Run clang-format on csrc/extensions.cpp

File csrc/extensions.cpp does not conform to Custom style guidelines. (lines 65, 67)

#include <cstdint>
#include <string>
Expand Down Expand Up @@ -60,10 +60,11 @@
eps);
}

void extApplyRotary(at::Tensor& output, const at::Tensor& input,
void extApplyRotary(const at::Tensor& input1, const at::Tensor& input2,
const at::Tensor& cos, const at::Tensor& sin,
const bool conj, const bool interleaved) {
callDiopi(diopiRotaryEmbedding, output, input, cos, sin, conj, interleaved);
at::Tensor& output1, at::Tensor& output2,
const bool conj) {
callDiopi(diopiApplyRotary, output1, output2, input1, input2, cos, sin, conj, false);
}

auto extMultiHeadAttention(at::Tensor& q, at::Tensor& k, at::Tensor& v,
Expand Down Expand Up @@ -443,7 +444,7 @@
m.def("rms_norm_backward", &extRmsNormBackward,
"deeplink ext_rms_norm_backward");
}
if (&diopiRotaryEmbedding != nullptr) {
if (&diopiApplyRotary != nullptr) {
m.def("apply_rotary", &extApplyRotary, "deeplink ext_apply_rotary");
}
if (&diopiMultiHeadAttention != nullptr) {
Expand Down
3 changes: 0 additions & 3 deletions deeplink_ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ def _init():
platform_type = deeplink_ext_get_platform_type()
if platform_type == PlatformType.TORCH_DIPU:
import torch_dipu
elif platform_type == PlatformType.TORCH_NPU:
warnings.warn("DeepLinkExt using torch_npu ...", ImportWarning)
import torch_npu
else:
raise ImportError

Expand Down
1 change: 0 additions & 1 deletion deeplink_ext/ascend_speed/_flash_attention_dipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class FlashSelfAttention(torch.autograd.Function):

@staticmethod
def forward(
ctx, q, k, v, attention_mask, dropout_p, softmax_scale, head_num, input_layout
Expand Down
1 change: 0 additions & 1 deletion deeplink_ext/ascend_speed/_rms_norm_dipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class RMSNorm(torch.autograd.Function):

@staticmethod
def forward(ctx, hidden_states, weight, eps):
output = torch.empty_like(hidden_states)
Expand Down
1 change: 0 additions & 1 deletion deeplink_ext/ascend_speed/_scaled_masked_softmax_dipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class ScaledMaskedSoftmax(torch.autograd.Function):

@staticmethod
def forward(ctx, input, mask, scale, fixed_triu_mask):
out = torch.empty_like(input)
Expand Down
1 change: 0 additions & 1 deletion deeplink_ext/ascend_speed/_scaled_masked_softmax_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class ScaledMaskedSoftmax(torch.autograd.Function):

@staticmethod
def forward(ctx, input, mask, scale, fixed_triu_mask):
out = torch_npu.npu_scaled_masked_softmax(input, mask, scale, fixed_triu_mask)
Expand Down
40 changes: 11 additions & 29 deletions deeplink_ext/easyllm_ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,22 @@
_not_impl = "[deeplink_ext] {op_name} is not implemented in diopi. Falling back to the slower torch implementation."

try:
from .adamw import AdamW
from deeplink_ext.ops.adamw import AdamW
except Exception as e:
print(_not_impl.format(op_name="adamw"))
from torch.optim import AdamW

try:
from .flash_attention import (
flash_attn_qkvpacked_func,
flash_attn_kvpacked_func,
flash_attn_func,
flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func,
)
except Exception as e:
print(_not_impl.format(op_name="flash attention"))
from .flash_attention_fallback import (
flash_attn_qkvpacked_func_torch as flash_attn_qkvpacked_func,
flash_attn_kvpacked_func_torch as flash_attn_kvpacked_func,
flash_attn_func_torch as flash_attn_func,
flash_attn_varlen_qkvpacked_func_torch as flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func_torch as flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func_torch as flash_attn_varlen_func,
)

try:
from .rms_norm import rms_norm
except:
print(
_not_impl.format(op_name="RMSNorm"),
)
from .rms_norm_fallback import rms_norm_torch as rms_norm
from deeplink_ext.ops.flash_attention import (
flash_attn_qkvpacked_func,
flash_attn_kvpacked_func,
flash_attn_func,
flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func,
)

from .bert_padding import pad_input, unpad_input, index_first_axis
from deeplink_ext.ops.rms_norm import rms_norm
from deeplink_ext.ops.bert_padding import pad_input, unpad_input, index_first_axis

__all__ = [
"AdamW",
Expand Down
5 changes: 0 additions & 5 deletions deeplink_ext/easyllm_ops/adamw.py

This file was deleted.

19 changes: 0 additions & 19 deletions deeplink_ext/easyllm_ops/flash_attention.py

This file was deleted.

20 changes: 0 additions & 20 deletions deeplink_ext/easyllm_ops/flash_attention_fallback.py

This file was deleted.

49 changes: 15 additions & 34 deletions deeplink_ext/internevo_ops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
# Copyright (c) 2024, DeepLink.

_not_impl = "[deeplink_ext] {op_name} is not implemented in diopi. Falling back to the slower torch implementation."

try:
from .adamw import AdamW
from deeplink_ext.ops.adamw import AdamW
except Exception as e:
print(_not_impl.format(op_name="adamw"))
from torch.optim import AdamW

try:
from .flash_attention import (
flash_attn_qkvpacked_func,
flash_attn_kvpacked_func,
flash_attn_func,
flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func,
)
except Exception as e:
print(_not_impl.format(op_name="flash attention"))
from .flash_attention_fallback import (
flash_attn_qkvpacked_func_torch as flash_attn_qkvpacked_func,
flash_attn_kvpacked_func_torch as flash_attn_kvpacked_func,
flash_attn_func_torch as flash_attn_func,
flash_attn_varlen_qkvpacked_func_torch as flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func_torch as flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func_torch as flash_attn_varlen_func,
)
from deeplink_ext.ops.flash_attention import (
flash_attn_qkvpacked_func,
flash_attn_kvpacked_func,
flash_attn_func,
flash_attn_varlen_qkvpacked_func,
flash_attn_varlen_kvpacked_func,
flash_attn_varlen_func,
FlashCrossAttention,
FlashSelfAttention,
)

try:
from .rms_norm import MixedFusedRMSNorm
except:
print(
_not_impl.format(op_name="RMSNorm"),
)
from .rms_norm_fallback import MixedRMSNormTorch as MixedFusedRMSNorm
from deeplink_ext.ops.rms_norm import MixedFusedRMSNorm

try:
from .rotary_embedding import ApplyRotaryEmb
except:
print(_not_impl.format(op_name="rotary embedding"))
from .rotary_embedding_fallback import ApplyRotaryEmbTorch as ApplyRotaryEmb
from deeplink_ext.ops.rotary_embedding import ApplyRotaryEmb, ApplyRotaryEmbQKV_, apply_rotary

__all__ = [
"AdamW",
Expand All @@ -52,4 +31,6 @@
"flash_attn_varlen_func",
"MixedFusedRMSNorm",
"ApplyRotaryEmb",
"ApplyRotaryEmbQKV_",
"apply_rotary",
]
Loading
Loading