Download kernels/sequential/L2/vision_patch_embed.py from Alexsssu/fastkernels-b200-codex-vanilla-20260923: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/datasets/Alexsssu/fastkernels-b200-codex-vanilla-20260923/resolve/main/kernels/sequential/L2/vision_patch_embed.py
- Command line
-
hf download hf://datasets/Alexsssu/fastkernels-b200-codex-vanilla-20260923/kernels/sequential/L2/vision_patch_embed.py
-
curl -L -o vision_patch_embed.py https://huggingface.co/datasets/Alexsssu/fastkernels-b200-codex-vanilla-20260923/resolve/main/kernels/sequential/L2/vision_patch_embed.py
1.11 kB
| """Vision patch embedding for Qwen VL models. | |
| Flattens 3D video/image patches via Conv3d weight reshaped into a linear projection. | |
| Unified across Qwen2-VL and Qwen3-VL: | |
| - bias: Qwen2-VL uses bias=False, Qwen3-VL uses bias=True. | |
| """ | |
| from __future__ import annotations | |
| import torch | |
| import torch.nn as nn | |
| from ..L1.conv3d import Conv3d | |
| from ..L1.linear import Matmul | |
| class VisionPatchEmbed(nn.Module): | |
| def __init__(self, patch_size: int, temporal_patch_size: int, | |
| in_channels: int, embed_dim: int, bias: bool = False): | |
| super().__init__() | |
| self.embed_dim = embed_dim | |
| self.input_size = in_channels * temporal_patch_size * patch_size * patch_size | |
| kernel = (temporal_patch_size, patch_size, patch_size) | |
| self.proj = Conv3d(in_channels, embed_dim, kernel, bias=bias) | |
| self.linear = Matmul() | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| x = x.view(x.shape[0], self.input_size) | |
| return self.linear( | |
| x, | |
| self.proj.weight.view(self.embed_dim, self.input_size), | |
| self.proj.bias, | |
| ) | |