e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import triton
|
|
import triton.language as tl
|
|
import torch
|
|
from .utils import calculate_settings, torch_gpu_device
|
|
|
|
# signed int32 max is 2**31-1 so num_elements cannot exceed 2**31
|
|
NUM_INT32_ELEMENTS = 2**31
|
|
SAFE_INT32_BUFFER_MULTIPLIER = 4
|
|
BLOCK_SIZE = 1024
|
|
INT32_SAFETY_BUFFER = NUM_INT32_ELEMENTS - BLOCK_SIZE * SAFE_INT32_BUFFER_MULTIPLIER
|
|
|
|
|
|
@triton.jit
|
|
def _fg_kernel(e, g, h, n_elements, BLOCK_SIZE: tl.constexpr, LONG_INDEXING: tl.constexpr):
|
|
block_idx = tl.program_id(0)
|
|
if LONG_INDEXING:
|
|
offsets = block_idx.to(tl.int64) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE).to(tl.int64)
|
|
n_elements = tl.cast(n_elements, tl.int64)
|
|
else:
|
|
offsets = block_idx * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
|
mask = offsets < n_elements
|
|
|
|
e_row = tl.load(e + offsets, mask = mask, other = 0).to(tl.float32)
|
|
g_row = tl.load(g + offsets, mask = mask, other = 0) # .to(tl.float32)
|
|
|
|
# f = e * sigmoid(e)
|
|
f_row = e_row * tl.sigmoid(e_row) # e_row / (1 + tl.exp(-e_row))
|
|
f_row = f_row.to(g_row.dtype) # Exact copy from HF
|
|
# h = f * g
|
|
h_row = f_row * g_row
|
|
|
|
# Store h
|
|
tl.store(h + offsets, h_row, mask = mask)
|
|
|
|
|
|
def swiglu_fg_kernel(e, g):
|
|
batch, seq_len, hd = e.shape
|
|
n_elements = e.numel()
|
|
h = torch.empty((batch, seq_len, hd), dtype = e.dtype, device = e.device)
|
|
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
|
|
with torch_gpu_device(e.device):
|
|
_fg_kernel[grid](
|
|
e,
|
|
g,
|
|
h,
|
|
n_elements,
|
|
BLOCK_SIZE = BLOCK_SIZE,
|
|
LONG_INDEXING = 0 if n_elements <= INT32_SAFETY_BUFFER else 1,
|
|
)
|
|
return h
|
|
|
|
|
|
@triton.jit
|
|
def _DWf_DW_dfg_kernel(DW, e, g, n_elements, BLOCK_SIZE: tl.constexpr, LONG_INDEXING: tl.constexpr):
|
|
"""
|
|
e = e.float()
|
|
se = 1.0 / (1.0 + torch.exp(-e))
|
|
f = (se * e).to(dtype)
|
|
h = f * g
|
|
df = DW * f
|
|
dg = DW * g
|
|
de = (dg.float() * se * (1.0 + e * (1.0 - se))).to(dtype)
|
|
"""
|
|
block_idx = tl.program_id(0)
|
|
if LONG_INDEXING:
|
|
offsets = block_idx.to(tl.int64) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE).to(tl.int64)
|
|
n_elements = tl.cast(n_elements, tl.int64)
|
|
else:
|
|
offsets = block_idx * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
|
mask = offsets < n_elements
|
|
|
|
DW_row = tl.load(DW + offsets, mask = mask, other = 0) # .to(tl.float32)
|
|
e_row = tl.load(e + offsets, mask = mask, other = 0).to(tl.float32)
|
|
g_row = tl.load(g + offsets, mask = mask, other = 0) # .to(tl.float32)
|
|
|
|
# e = e.float()
|
|
# se = 1.0 / (1.0 + torch.exp(-e))
|
|
se_row = tl.sigmoid(e_row) # 1.0 / (1.0 + tl.exp(-e_row))
|
|
# f = (se * e).to(dtype)
|
|
f_row = se_row * e_row
|
|
f_row = f_row.to(DW_row.dtype)
|
|
# h = f * g
|
|
h_row = f_row * g_row
|
|
# df = DW * f
|
|
df_row = DW_row * f_row
|
|
# dg = DW * g
|
|
dg_row = DW_row * g_row
|
|
# de = (dg.float() * se * (1.0 + e * (1.0 - se))).to(dtype)
|
|
de_row = dg_row.to(tl.float32) * se_row * (1.0 + e_row * (1.0 - se_row))
|
|
de_row = de_row.to(DW_row.dtype)
|
|
|
|
# Store derivatives in buffers
|
|
tl.store(DW + offsets, h_row, mask = mask) # h = f * g
|
|
tl.store(e + offsets, df_row, mask = mask) # df = DW * f
|
|
tl.store(g + offsets, de_row, mask = mask) # de
|
|
|
|
|
|
def swiglu_DWf_DW_dfg_kernel(DW, e, g):
|
|
batch_seq_len, hd = e.shape # Flattened to 2D, so 1st dim is bsz * seq_len
|
|
n_elements = e.numel()
|
|
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
|
|
with torch_gpu_device(e.device):
|
|
_DWf_DW_dfg_kernel[grid](
|
|
DW,
|
|
e,
|
|
g,
|
|
n_elements,
|
|
BLOCK_SIZE = BLOCK_SIZE,
|
|
LONG_INDEXING = 0 if n_elements <= INT32_SAFETY_BUFFER else 1,
|
|
)
|
|
return DW, e, g
|