[fix] baseline4 rms_norm crash
This commit is contained in:
@@ -222,11 +222,16 @@ def fused_add_rms_norm(input: torch.Tensor, residual: torch.Tensor,
|
|||||||
"""Fused residual addition + RMSNorm.
|
"""Fused residual addition + RMSNorm.
|
||||||
|
|
||||||
Source: xllm/core/kernels/ilu/norm.cpp → infer::residual_rms_norm
|
Source: xllm/core/kernels/ilu/norm.cpp → infer::residual_rms_norm
|
||||||
output = rms_norm(input + residual, weight, eps)
|
The C++ function is in-place: modifies input → rms_norm(input+residual)*weight,
|
||||||
residual_output = input + residual
|
and residual → input+residual. We copy results to output/residual_output.
|
||||||
"""
|
"""
|
||||||
_bridge.fused_add_rms_norm(input, residual, weight, output,
|
# C++ signature: fused_add_rms_norm_forward(input, residual, weight, eps, alpha)
|
||||||
residual_output, eps)
|
# It modifies input and residual in-place.
|
||||||
|
inp_clone = input.clone()
|
||||||
|
res_clone = residual.clone()
|
||||||
|
_bridge.fused_add_rms_norm(inp_clone, res_clone, weight, eps)
|
||||||
|
output.copy_(inp_clone)
|
||||||
|
residual_output.copy_(res_clone)
|
||||||
|
|
||||||
|
|
||||||
def rotary_embedding(positions: torch.Tensor, query: torch.Tensor,
|
def rotary_embedding(positions: torch.Tensor, query: torch.Tensor,
|
||||||
|
|||||||
@@ -92,28 +92,26 @@ def _patch_layernorm() -> int:
|
|||||||
w = self.weight
|
w = self.weight
|
||||||
if _debug_count[0] < 20:
|
if _debug_count[0] < 20:
|
||||||
_debug_count[0] += 1
|
_debug_count[0] += 1
|
||||||
logger.info("DEBUG rms_norm #%d: w.shape=%s w.dim=%d x.shape=%s x.dim=%d "
|
logger.info("DEBUG rms_norm #%d: w.shape=%s w.dim=%d w.dtype=%s "
|
||||||
"class=%s residual=%s",
|
"x.shape=%s x.dim=%d x.dtype=%s class=%s residual=%s",
|
||||||
_debug_count[0], list(w.shape), w.dim(), list(x.shape), x.dim(),
|
_debug_count[0], list(w.shape), w.dim(), w.dtype,
|
||||||
|
list(x.shape), x.dim(), x.dtype,
|
||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
list(residual.shape) if residual is not None else None)
|
list(residual.shape) if residual is not None else None)
|
||||||
if w.dim() != 1 or w.shape[0] != x.shape[-1]:
|
if w.dim() != 1 or w.shape[0] != x.shape[-1]:
|
||||||
return _orig_forward(self, x, residual)
|
return _orig_forward(self, x, residual)
|
||||||
w_adjusted = 1.0 + w
|
# 1.0 + w promotes fp16→fp32; ixformer rms_norm requires weight
|
||||||
|
# to be 1-D AND same dtype as input, so cast back.
|
||||||
|
w_adjusted = (1.0 + w).to(w.dtype)
|
||||||
if residual is not None:
|
if residual is not None:
|
||||||
if ix_ops.has_fused_add_rms_norm():
|
# ixformer fused_add_rms_norm is in-place and has 4-arg C++
|
||||||
out = torch.empty_like(x)
|
# signature (input, residual, weight, eps). Safer to use
|
||||||
residual_out = torch.empty_like(x)
|
# the non-fused path which is explicit about outputs.
|
||||||
ix_ops.fused_add_rms_norm(
|
new_residual = x + residual
|
||||||
x, residual, w_adjusted, out, residual_out,
|
out = torch.empty_like(x)
|
||||||
self.variance_epsilon)
|
ix_ops.rms_norm(out, new_residual, w_adjusted,
|
||||||
return out, residual_out
|
self.variance_epsilon)
|
||||||
else:
|
return out, new_residual
|
||||||
new_residual = x + residual
|
|
||||||
out = torch.empty_like(x)
|
|
||||||
ix_ops.rms_norm(out, new_residual, w_adjusted,
|
|
||||||
self.variance_epsilon)
|
|
||||||
return out, new_residual
|
|
||||||
else:
|
else:
|
||||||
out = torch.empty_like(x)
|
out = torch.empty_like(x)
|
||||||
ix_ops.rms_norm(out, x, w_adjusted, self.variance_epsilon)
|
ix_ops.rms_norm(out, x, w_adjusted, self.variance_epsilon)
|
||||||
@@ -180,17 +178,17 @@ def _patch_custom_ops() -> int:
|
|||||||
logger.info("PATCHED: _custom_ops.rms_norm → ix_ops")
|
logger.info("PATCHED: _custom_ops.rms_norm → ix_ops")
|
||||||
|
|
||||||
# Patch fused_add_rms_norm
|
# Patch fused_add_rms_norm
|
||||||
if ix_ops.has_fused_add_rms_norm() and hasattr(ops, 'fused_add_rms_norm'):
|
if ix_ops.has_rms_norm() and hasattr(ops, 'fused_add_rms_norm'):
|
||||||
def _fused_add_rms_norm(input, residual, weight, eps):
|
def _fused_add_rms_norm(input, residual, weight, eps):
|
||||||
|
# C++ fused_add_rms_norm is in-place with 4-arg signature,
|
||||||
|
# doesn't match the 6-arg wrapper in ix_ops. Use non-fused path.
|
||||||
|
residual.add_(input)
|
||||||
out = torch.empty_like(input)
|
out = torch.empty_like(input)
|
||||||
residual_out = torch.empty_like(input)
|
ix_ops.rms_norm(out, residual, weight, eps)
|
||||||
ix_ops.fused_add_rms_norm(input, residual, weight,
|
|
||||||
out, residual_out, eps)
|
|
||||||
input.copy_(out)
|
input.copy_(out)
|
||||||
residual.copy_(residual_out)
|
|
||||||
ops.fused_add_rms_norm = _fused_add_rms_norm
|
ops.fused_add_rms_norm = _fused_add_rms_norm
|
||||||
count += 1
|
count += 1
|
||||||
logger.info("PATCHED: _custom_ops.fused_add_rms_norm → ix_ops")
|
logger.info("PATCHED: _custom_ops.fused_add_rms_norm → ix_ops (non-fused)")
|
||||||
|
|
||||||
# Patch rotary_embedding
|
# Patch rotary_embedding
|
||||||
if ix_ops.has_rotary_embedding() and hasattr(ops, 'rotary_embedding'):
|
if ix_ops.has_rotary_embedding() and hasattr(ops, 'rotary_embedding'):
|
||||||
|
|||||||
@@ -222,11 +222,16 @@ def fused_add_rms_norm(input: torch.Tensor, residual: torch.Tensor,
|
|||||||
"""Fused residual addition + RMSNorm.
|
"""Fused residual addition + RMSNorm.
|
||||||
|
|
||||||
Source: xllm/core/kernels/ilu/norm.cpp → infer::residual_rms_norm
|
Source: xllm/core/kernels/ilu/norm.cpp → infer::residual_rms_norm
|
||||||
output = rms_norm(input + residual, weight, eps)
|
The C++ function is in-place: modifies input → rms_norm(input+residual)*weight,
|
||||||
residual_output = input + residual
|
and residual → input+residual. We copy results to output/residual_output.
|
||||||
"""
|
"""
|
||||||
_bridge.fused_add_rms_norm(input, residual, weight, output,
|
# C++ signature: fused_add_rms_norm_forward(input, residual, weight, eps, alpha)
|
||||||
residual_output, eps)
|
# It modifies input and residual in-place.
|
||||||
|
inp_clone = input.clone()
|
||||||
|
res_clone = residual.clone()
|
||||||
|
_bridge.fused_add_rms_norm(inp_clone, res_clone, weight, eps)
|
||||||
|
output.copy_(inp_clone)
|
||||||
|
residual_output.copy_(res_clone)
|
||||||
|
|
||||||
|
|
||||||
def rotary_embedding(positions: torch.Tensor, query: torch.Tensor,
|
def rotary_embedding(positions: torch.Tensor, query: torch.Tensor,
|
||||||
|
|||||||
@@ -92,28 +92,26 @@ def _patch_layernorm() -> int:
|
|||||||
w = self.weight
|
w = self.weight
|
||||||
if _debug_count[0] < 20:
|
if _debug_count[0] < 20:
|
||||||
_debug_count[0] += 1
|
_debug_count[0] += 1
|
||||||
logger.info("DEBUG rms_norm #%d: w.shape=%s w.dim=%d x.shape=%s x.dim=%d "
|
logger.info("DEBUG rms_norm #%d: w.shape=%s w.dim=%d w.dtype=%s "
|
||||||
"class=%s residual=%s",
|
"x.shape=%s x.dim=%d x.dtype=%s class=%s residual=%s",
|
||||||
_debug_count[0], list(w.shape), w.dim(), list(x.shape), x.dim(),
|
_debug_count[0], list(w.shape), w.dim(), w.dtype,
|
||||||
|
list(x.shape), x.dim(), x.dtype,
|
||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
list(residual.shape) if residual is not None else None)
|
list(residual.shape) if residual is not None else None)
|
||||||
if w.dim() != 1 or w.shape[0] != x.shape[-1]:
|
if w.dim() != 1 or w.shape[0] != x.shape[-1]:
|
||||||
return _orig_forward(self, x, residual)
|
return _orig_forward(self, x, residual)
|
||||||
w_adjusted = 1.0 + w
|
# 1.0 + w promotes fp16→fp32; ixformer rms_norm requires weight
|
||||||
|
# to be 1-D AND same dtype as input, so cast back.
|
||||||
|
w_adjusted = (1.0 + w).to(w.dtype)
|
||||||
if residual is not None:
|
if residual is not None:
|
||||||
if ix_ops.has_fused_add_rms_norm():
|
# ixformer fused_add_rms_norm is in-place and has 4-arg C++
|
||||||
out = torch.empty_like(x)
|
# signature (input, residual, weight, eps). Safer to use
|
||||||
residual_out = torch.empty_like(x)
|
# the non-fused path which is explicit about outputs.
|
||||||
ix_ops.fused_add_rms_norm(
|
new_residual = x + residual
|
||||||
x, residual, w_adjusted, out, residual_out,
|
out = torch.empty_like(x)
|
||||||
self.variance_epsilon)
|
ix_ops.rms_norm(out, new_residual, w_adjusted,
|
||||||
return out, residual_out
|
self.variance_epsilon)
|
||||||
else:
|
return out, new_residual
|
||||||
new_residual = x + residual
|
|
||||||
out = torch.empty_like(x)
|
|
||||||
ix_ops.rms_norm(out, new_residual, w_adjusted,
|
|
||||||
self.variance_epsilon)
|
|
||||||
return out, new_residual
|
|
||||||
else:
|
else:
|
||||||
out = torch.empty_like(x)
|
out = torch.empty_like(x)
|
||||||
ix_ops.rms_norm(out, x, w_adjusted, self.variance_epsilon)
|
ix_ops.rms_norm(out, x, w_adjusted, self.variance_epsilon)
|
||||||
@@ -180,17 +178,17 @@ def _patch_custom_ops() -> int:
|
|||||||
logger.info("PATCHED: _custom_ops.rms_norm → ix_ops")
|
logger.info("PATCHED: _custom_ops.rms_norm → ix_ops")
|
||||||
|
|
||||||
# Patch fused_add_rms_norm
|
# Patch fused_add_rms_norm
|
||||||
if ix_ops.has_fused_add_rms_norm() and hasattr(ops, 'fused_add_rms_norm'):
|
if ix_ops.has_rms_norm() and hasattr(ops, 'fused_add_rms_norm'):
|
||||||
def _fused_add_rms_norm(input, residual, weight, eps):
|
def _fused_add_rms_norm(input, residual, weight, eps):
|
||||||
|
# C++ fused_add_rms_norm is in-place with 4-arg signature,
|
||||||
|
# doesn't match the 6-arg wrapper in ix_ops. Use non-fused path.
|
||||||
|
residual.add_(input)
|
||||||
out = torch.empty_like(input)
|
out = torch.empty_like(input)
|
||||||
residual_out = torch.empty_like(input)
|
ix_ops.rms_norm(out, residual, weight, eps)
|
||||||
ix_ops.fused_add_rms_norm(input, residual, weight,
|
|
||||||
out, residual_out, eps)
|
|
||||||
input.copy_(out)
|
input.copy_(out)
|
||||||
residual.copy_(residual_out)
|
|
||||||
ops.fused_add_rms_norm = _fused_add_rms_norm
|
ops.fused_add_rms_norm = _fused_add_rms_norm
|
||||||
count += 1
|
count += 1
|
||||||
logger.info("PATCHED: _custom_ops.fused_add_rms_norm → ix_ops")
|
logger.info("PATCHED: _custom_ops.fused_add_rms_norm → ix_ops (non-fused)")
|
||||||
|
|
||||||
# Patch rotary_embedding
|
# Patch rotary_embedding
|
||||||
if ix_ops.has_rotary_embedding() and hasattr(ops, 'rotary_embedding'):
|
if ix_ops.has_rotary_embedding() and hasattr(ops, 'rotary_embedding'):
|
||||||
|
|||||||
Reference in New Issue
Block a user