Token Classification
lora
AI-Detector-35BA3B / gpu /detector35_gpu /hopper_options.py
matthewagi's picture matt's picture
Initial model release
86599c0
Raw History Blame Contribute Delete
3.71 kB
"""Hopper options for the intact, native-attention EditLens worker."""
from types import MethodType
import numpy as np
import torch
def packed_layout(lengths):
from .stateless_contract import chunk_plan
from .packed import Layout
cu = np.empty(len(lengths) + 1, dtype=np.int32)
cu[0] = 0
np.cumsum(lengths, out=cu[1:])
chunks, chunk_offsets = chunk_plan(cu.tolist())
total = int(cu[-1])
positions = np.arange(total, dtype=np.int64) - np.repeat(cu[:-1], lengths)
def tensor(values, dtype):
return torch.as_tensor(np.asarray(values, dtype=dtype), device="cuda")
return Layout(
lengths,
tensor(cu, np.int32),
tensor(chunks, np.int32),
tensor(chunk_offsets, np.int32),
tensor(positions, np.int64),
total,
max(lengths),
)
@torch.no_grad()
def quantize_encoder(encoder):
from .precision import convert_dense_projections
return convert_dense_projections(
[encoder], {id(block.mlp.gate) for block in encoder}
)
def fused_projection(layer, x, z):
from .gdn_norm_quant import gdn_norm_quant
from vllm import _custom_ops as ops
linear = layer.out_proj
if (
layer.tp_size != 1
or not layer.norm.norm_before_gate
or layer.norm.group_size is not None
or layer.norm.activation not in ("silu", "swish")
or linear.bias is not None
):
raise ValueError("Unsupported gated normalization/projection configuration")
x = x.reshape_as(z)
activation, scale = gdn_norm_quant(x, z, layer.norm.weight, layer.norm.eps)
return ops.cutlass_scaled_mm(
activation, linear.weight.T, scale, linear.weight_scale, torch.bfloat16
)
def set_fusion(encoder, enabled):
for block in encoder:
if block.layer_type == "linear_attention":
layer = block.linear_attn
if enabled:
layer._output_projection = MethodType(fused_projection, layer)
elif "_output_projection" in vars(layer):
del layer._output_projection
def set_input_projection(encoder, mode):
from vllm import _custom_ops as ops
def make_project(qkvz, ba):
if qkvz.bias is not None or ba.bias is not None:
raise ValueError("Shared FP8 inputs require bias-free projections")
widths = (qkvz.original_width, ba.original_width)
if mode == "joined":
weight = torch.cat((qkvz.weight[: widths[0]], ba.weight[: widths[1]])).T
scale = torch.cat(
(qkvz.weight_scale[: widths[0]], ba.weight_scale[: widths[1]])
)
def project(x):
act, act_scale = ops.scaled_fp8_quant(
x.contiguous(), use_per_token_if_dynamic=True
)
if mode == "joined":
values = ops.cutlass_scaled_mm(
act, weight, act_scale, scale, torch.bfloat16
)
return values.split(widths, dim=-1)
return tuple(
ops.cutlass_scaled_mm(
act, lin.weight.T, act_scale, lin.weight_scale, torch.bfloat16
)[:, :w]
for lin, w in zip((qkvz, ba), widths, strict=True)
)
return project
if mode not in ("separate", "shared", "joined"):
raise ValueError(f"Unsupported GDN input projection: {mode}")
for block in encoder:
if block.layer_type == "linear_attention":
layer = block.linear_attn
layer._native_project_inputs = (
None
if mode == "separate"
else make_project(layer.in_proj_qkvz, layer.in_proj_ba)
)