Files
project_6/upstream_ref/xllm/tools/compare_tensor.py
EX Engine 002f9879b2 ref(upstream): FULL TREE — Deep-Spark xllm (1470) + ds_vllm csrc/models (703)
Replaces cherry-picked upstream_ref with complete source trees.

xllm/ — Iluvatar official C++ inference engine (15MB, 1470 files)
  Complete: kernels → layers → models → runtime → scheduler → api
  Excluded: .git, binary images, third_party submodule checkouts

ds_vllm/ — Iluvatar official vllm fork (8MB, 703 files)
  Included: csrc/ (ALL CUDA kernels), fused_moe/, qwen3_5 model, _custom_ops
  Excluded: tests, benchmarks, docs, examples (not needed for reference)

Critical call chains now fully traceable:
  MoE: moe_topk_softmax_kernels.cuh → ixformer.h → fused_moe.cpp → layer
  GDN: qwen3_gated_delta_net_base.cpp → qwen3_5_gated_delta_net.cpp
  Attention: ixformer.h → xllm_paged_attention → attention.cpp
2026-08-10 02:54:03 +00:00

58 lines
1.7 KiB
Python

import torch
import numpy as np
def compare_tensors(
a: torch.Tensor,
b: torch.Tensor,
tol: float = 1e-6,
verbose: bool = False
) -> int:
"""
Compare two PyTorch tensors and count the number of elements whose absolute difference
exceeds the given tolerance.
Args:
a (torch.Tensor): The first tensor to compare.
b (torch.Tensor): The second tensor to compare.
tol (float, optional): The absolute tolerance threshold. Defaults to 1e-6.
verbose (bool, optional): If True, print the indices and values of differing elements. Defaults to False.
Returns:
int: The number of elements where abs(a - b) > tol.
Raises:
ValueError: If the shapes of the input tensors do not match.
"""
# Check if tensor shapes are the same
if a.shape != b.shape:
raise ValueError(f"Shape mismatch: {a.shape} vs {b.shape}")
# Create a boolean mask where differences exceed the tolerance
diff_mask = (a - b).abs() > tol
# Count the number of differing elements
diff_count = int(diff_mask.sum().item())
# If verbose, print details of differing elements
if verbose and diff_count > 0:
indices = torch.nonzero(diff_mask, as_tuple=False)
for idx in indices:
i, j = idx[0].item(), idx[1].item()
print(
f"diff at {i},{j}: "
f"{a[i, j].item():.6f} - {b[i, j].item():.6f} = "
f"{(a[i, j] - b[i, j]).item():.6f}"
)
return diff_count
if __name__ == "__main__":
# example:
# a = torch.load("/path/to/a.pt")
# b = torch.load("/path/to/b.pt")
# diff_count = compare_tensors(a, b)
# print(f"diff count: {diff_count}")
pass