MCPcopy Create free account
hub / github.com/pytorch/executorch / embedding_q4gsw_impl

Function embedding_q4gsw_impl

backends/vulkan/custom_ops_lib.py:995–1020  ·  view source on GitHub ↗
(
    weight: torch.Tensor,
    weight_scales: torch.Tensor,
    group_size: int,
    indices: torch.Tensor,
    is_linear_weight: bool = False,
)

Source from the content-addressed store, hash-verified

993
994
995def embedding_q4gsw_impl(
996 weight: torch.Tensor,
997 weight_scales: torch.Tensor,
998 group_size: int,
999 indices: torch.Tensor,
1000 is_linear_weight: bool = False,
1001) -> torch.Tensor:
1002 # Unpack 4-bit values from packed uint8 tensor
1003 # Packing convention: packed_byte = (even_val + 8) << 4 | (odd_val + 8)
1004 high = (weight >> 4).to(torch.int8) - 8
1005 low = (weight & 0xF).to(torch.int8) - 8
1006 if is_linear_weight:
1007 unpacked = torch.stack([low, high], dim=-1).reshape(weight.shape[0], -1)
1008 else:
1009 unpacked = torch.stack([high, low], dim=-1).reshape(weight.shape[0], -1)
1010 # Dequantize using per-group scales
1011 num_groups = weight_scales.shape[1] if weight_scales.dim() > 1 else 1
1012 unpacked_groups = unpacked.reshape(weight.shape[0], num_groups, group_size)
1013 scales = (
1014 weight_scales.unsqueeze(-1)
1015 if weight_scales.dim() > 1
1016 else weight_scales.reshape(1, 1, 1)
1017 )
1018 dequantized = unpacked_groups.float() * scales.float()
1019 dequantized = dequantized.reshape(weight.shape[0], -1)
1020 return torch.nn.functional.embedding(indices, dequantized)
1021
1022
1023name = "embedding_q4gsw"

Callers

nothing calls this directly

Calls 2

embeddingMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected