Skip to content
Draft
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
16 changes: 7 additions & 9 deletions cookbook/transformers/ep_fsdp2_lora_qwen3_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from twinkle.model import TransformersModel
from twinkle.preprocessor import SelfCognitionProcessor
from twinkle.utils.framework import Torch
from twinkle.kernel import kernelize, liger_builtin, npu_builtin
from twinkle.kernel import kernelize

logger = get_logger()
args = CLI.from_args()
Expand Down Expand Up @@ -93,16 +93,14 @@ def train():
},
)
# Kernel mode: torch (TWINKLE_TORCH_BASELINE=1, no fusion) | npu (default,
# CANN + FLA) | npu+liger (--enable-liger, Liger per-layer on top of CANN).
# CANN + FLA via DEFAULT_KERNEL_CONFIG). `--enable-liger` gates the fused-CE
# loss below; per-layer kernels stay on CANN on NPU (the default config's
# npu-first chains). To force Liger per-layer kernels, pass a custom
# mapping with liger-first KernelChoice chains — see Kernel.md.
# Sharding/batch are unchanged across modes.
_torch_baseline = os.environ.get('TWINKLE_TORCH_BASELINE', '').lower() in ('1', 'true', 'yes')
kernel_mapping = {}
if Torch.is_npu_available() and not _torch_baseline:
kernel_mapping.update(npu_builtin(model))
if args.model.enable_liger and not _torch_baseline:
kernel_mapping.update(liger_builtin(model))
if kernel_mapping:
model = kernelize(model, kernel_mapping)
if not _torch_baseline:
model = kernelize(model)
_use_fused_ce = args.model.enable_liger and not _torch_baseline and args.model.enable_fused_ce
_task = 'fused_lm_ce' if _use_fused_ce else 'causal_lm'
lora_cfg = _build_lora_config()
Expand Down
20 changes: 8 additions & 12 deletions cookbook/transformers/fsdp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from twinkle.model import TransformersModel
from twinkle.preprocessor import SelfCognitionProcessor
from twinkle.utils.framework import Torch
from twinkle.kernel import kernelize, liger_builtin, npu_builtin
from twinkle.kernel import kernelize

logger = get_logger()
args = CLI.from_args()
Expand Down Expand Up @@ -66,19 +66,15 @@ def train():
discovered = {type(m).__name__ for m in model.model.modules()
if type(m).__name__.endswith('DecoderLayer')}
model.model._no_split_modules = list(discovered) or [model.model.config.model_type.title() + 'DecoderLayer']
# Compose the kernel mapping: NPU built-ins first, then Liger on top so
# `--enable-liger` opts into Liger's cross-device Triton/Ascend kernels
# (later keys win on overlap — see twinkle.kernel Kernel.md).
kernel_mapping = {}
# Kernel mode: torch (TWINKLE_TORCH_BASELINE=1, no fusion) | npu (default,
# CANN + FLA) | npu+liger (--enable-liger, Liger per-layer + fused-CE).
# CANN + FLA via DEFAULT_KERNEL_CONFIG). `--enable-liger` gates the fused-CE
# loss below; per-layer kernels stay on CANN on NPU (the default config's
# npu-first chains), matching the old npu+liger mode after CANN preference.
# To force Liger per-layer kernels, pass a custom mapping with liger-first
# KernelChoice chains — see Kernel.md.
_torch_baseline = os.environ.get('TWINKLE_TORCH_BASELINE', '').lower() in ('1', 'true', 'yes')
if Torch.is_npu_available() and not _torch_baseline:
kernel_mapping.update(npu_builtin(model))
if args.model.enable_liger and not _torch_baseline:
kernel_mapping.update(liger_builtin(model))
if kernel_mapping:
model = kernelize(model, kernel_mapping)
if not _torch_baseline:
model = kernelize(model)

# `--enable-liger` turns on BOTH the per-layer Liger/CANN kernels (above)
# AND, by default (`enable_fused_ce=True`), the LigerFusedLinearCrossEntropyLoss
Expand Down
16 changes: 7 additions & 9 deletions cookbook/transformers/sp_fsdp_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from twinkle.model import TransformersModel
from twinkle.preprocessor import SelfCognitionProcessor
from twinkle.utils.framework import Torch
from twinkle.kernel import kernelize, liger_builtin, npu_builtin
from twinkle.kernel import kernelize

logger = get_logger()
args = CLI.from_args()
Expand Down Expand Up @@ -70,16 +70,14 @@ def train():
strategy=args.model.strategy,
)
# Kernel mode: torch (TWINKLE_TORCH_BASELINE=1, no fusion) | npu (default,
# CANN + FLA) | npu+liger (--enable-liger, Liger per-layer on top of CANN).
# CANN + FLA via DEFAULT_KERNEL_CONFIG). `--enable-liger` gates the fused-CE
# loss below; per-layer kernels stay on CANN on NPU (the default config's
# npu-first chains). To force Liger per-layer kernels, pass a custom
# mapping with liger-first KernelChoice chains — see Kernel.md.
# Sharding/batch are unchanged across modes.
_torch_baseline = os.environ.get('TWINKLE_TORCH_BASELINE', '').lower() in ('1', 'true', 'yes')
kernel_mapping = {}
if Torch.is_npu_available() and not _torch_baseline:
kernel_mapping.update(npu_builtin(model))
if args.model.enable_liger and not _torch_baseline:
kernel_mapping.update(liger_builtin(model))
if kernel_mapping:
model = kernelize(model, kernel_mapping)
if not _torch_baseline:
model = kernelize(model)
_use_fused_ce = args.model.enable_liger and not _torch_baseline and args.model.enable_fused_ce
_task = 'fused_lm_ce' if _use_fused_ce else 'causal_lm'
lora_config = LoraConfig(**args.get_lora_args())
Expand Down
Loading