Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -102,9 +102,7 @@ def make_rand_tensors(
|
||||
num_slices: int,
|
||||
device: str = "cuda",
|
||||
) -> tuple[torch.Tensor, list[torch.Tensor], torch.Tensor]:
|
||||
"""
|
||||
Make LoRA input/output matrices.
|
||||
"""
|
||||
"""Make LoRA input/output matrices."""
|
||||
A = torch.rand(a_shape, dtype=a_dtype).to(device)
|
||||
|
||||
# LoRA weights column major
|
||||
@@ -117,8 +115,7 @@ def make_rand_tensors(
|
||||
def make_prompt_lora_mapping(
|
||||
num_prompts: int, num_active_loras: int, sort_by_lora_id: bool, device: str
|
||||
) -> torch.Tensor:
|
||||
"""
|
||||
All prompts are mapped to a LoRA ID in range [0, num_active_loras).
|
||||
"""All prompts are mapped to a LoRA ID in range [0, num_active_loras).
|
||||
where 0 refers to first lora, 1 refers to second lora and so on.
|
||||
"""
|
||||
assert num_active_loras > 0
|
||||
@@ -147,9 +144,7 @@ def make_token_lora_mapping(
|
||||
seq_len_tensor: torch.Tensor,
|
||||
device: str,
|
||||
):
|
||||
"""
|
||||
Make token_lora_mapping from prompt_lora_mapping and seq_lens_tensor
|
||||
"""
|
||||
"""Make token_lora_mapping from prompt_lora_mapping and seq_lens_tensor."""
|
||||
assert prompt_lora_mapping.shape[0] == num_prompts
|
||||
|
||||
# token to lora index mapping
|
||||
@@ -174,8 +169,7 @@ def ref_group_gemm(
|
||||
scaling: float,
|
||||
add_inputs: bool | None,
|
||||
):
|
||||
"""
|
||||
Torch group gemm reference implementation to test correctness of
|
||||
"""Torch group gemm reference implementation to test correctness of
|
||||
benchmarking operations.
|
||||
"""
|
||||
batches = seq_lens_cpu.size(0)
|
||||
@@ -198,9 +192,7 @@ def ref_group_gemm(
|
||||
|
||||
|
||||
class OpType(Enum):
|
||||
"""
|
||||
LoRA Ops to benchmark and its properties.
|
||||
"""
|
||||
"""LoRA Ops to benchmark and its properties."""
|
||||
|
||||
LORA_SHRINK = auto()
|
||||
LORA_EXPAND = auto()
|
||||
@@ -291,9 +283,7 @@ class OpType(Enum):
|
||||
def matmul_dtypes(
|
||||
self, op_dtype: torch.dtype
|
||||
) -> tuple[torch.dtype, torch.dtype, torch.dtype]:
|
||||
"""
|
||||
return a type, b type and c type for A x B = C
|
||||
"""
|
||||
"""Return a type, b type and c type for A x B = C."""
|
||||
if self.is_shrink_fn():
|
||||
return op_dtype, op_dtype, torch.float32
|
||||
elif self.is_expand_fn():
|
||||
@@ -338,8 +328,7 @@ class OpType(Enum):
|
||||
top_k_num: int | None = None,
|
||||
num_experts: int | None = None,
|
||||
) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]:
|
||||
"""
|
||||
Given num_slices, return the shapes of the A, B, and C matrices
|
||||
"""Given num_slices, return the shapes of the A, B, and C matrices
|
||||
in A x B = C, for the op_type
|
||||
"""
|
||||
m, k, n = self.mkn(batch_size, seq_length, hidden_size, lora_rank)
|
||||
@@ -419,9 +408,7 @@ class OpType(Enum):
|
||||
|
||||
@dataclass
|
||||
class BenchmarkContext:
|
||||
"""
|
||||
LoRA benchmark context
|
||||
"""
|
||||
"""LoRA benchmark context."""
|
||||
|
||||
batch_size: int
|
||||
hidden_size: int
|
||||
@@ -467,9 +454,7 @@ class BenchmarkContext:
|
||||
|
||||
@dataclass
|
||||
class BenchmarkTensors:
|
||||
"""
|
||||
Input/Output tensors used for benchmarks
|
||||
"""
|
||||
"""Input/Output tensors used for benchmarks."""
|
||||
|
||||
# matmul tensors
|
||||
input: torch.Tensor
|
||||
@@ -555,9 +540,7 @@ class BenchmarkTensors:
|
||||
)
|
||||
|
||||
def sanity_check(self, ctx: BenchmarkContext, op_type: OpType) -> None:
|
||||
"""
|
||||
Fails asserts when non-conformality is detected.
|
||||
"""
|
||||
"""Fails asserts when non-conformality is detected."""
|
||||
num_tokens = (
|
||||
self.input.shape[1]
|
||||
if op_type.is_fused_moe_lora_expand_fn()
|
||||
@@ -577,9 +560,7 @@ class BenchmarkTensors:
|
||||
)
|
||||
|
||||
def to_device(self, device: str):
|
||||
"""
|
||||
Transfer tensors to device if the tensors aren't already on the device
|
||||
"""
|
||||
"""Transfer tensors to device if the tensors aren't already on the device."""
|
||||
|
||||
def to_device(tensor: torch.Tensor):
|
||||
if tensor.device != device:
|
||||
@@ -604,9 +585,7 @@ class BenchmarkTensors:
|
||||
)
|
||||
|
||||
def metadata(self, ctx: BenchmarkContext, op_type: OpType) -> tuple[int, int, int]:
|
||||
"""
|
||||
Return num_seqs, num_tokens and max_seq_len
|
||||
"""
|
||||
"""Return num_seqs, num_tokens and max_seq_len."""
|
||||
num_seqs = self.seq_lens.shape[0]
|
||||
num_tokens = self.get_num_tokens(
|
||||
self.lora_kernel_meta.token_lora_mapping.shape[0], ctx.top_k_num, op_type
|
||||
@@ -630,8 +609,7 @@ class BenchmarkTensors:
|
||||
expert_map: torch.Tensor | None = None,
|
||||
pad_sorted_ids: bool = False,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
Aligns tokens and experts into block-sized chunks for LoRA-based
|
||||
"""Aligns tokens and experts into block-sized chunks for LoRA-based
|
||||
mixture-of-experts (MoE) execution.
|
||||
"""
|
||||
max_num_tokens_padded = topk_ids.numel() + num_experts * (block_size - 1)
|
||||
@@ -959,8 +937,7 @@ class BenchmarkTensors:
|
||||
def test_correctness(
|
||||
self, op_type: OpType, expand_fn_add_inputs: bool | None
|
||||
) -> bool:
|
||||
"""
|
||||
Test correctness of op_type implementation against a grouped gemm
|
||||
"""Test correctness of op_type implementation against a grouped gemm
|
||||
reference implementation.
|
||||
"""
|
||||
seq_lens_cpu = self.seq_lens.to(device="cpu")
|
||||
@@ -1070,15 +1047,13 @@ def bench_torch_mm(
|
||||
op_type: OpType,
|
||||
cuda_graph_nops: int | None = None,
|
||||
) -> TMeasurement:
|
||||
"""
|
||||
Benchmark basic torch.mm as a roofline.
|
||||
"""Benchmark basic torch.mm as a roofline.
|
||||
|
||||
When all the input tokens have the same LoRA ID, the LoRA kernels are just
|
||||
a matmul. This torch.mm benchmark serves as a roofline for that case.
|
||||
|
||||
input op_type is used in determining the m, k, n dimensions for the matmul.
|
||||
"""
|
||||
|
||||
batch_size, hidden_size, lora_rank, seq_length, dtype = (
|
||||
ctx.batch_size,
|
||||
ctx.hidden_size,
|
||||
|
||||
Reference in New Issue
Block a user