[Feat][Conv2d] Add conv2d op with 1x1 optimization and benchmarks#464
[Feat][Conv2d] Add conv2d op with 1x1 optimization and benchmarks#464RMLYC wants to merge 2 commits intotile-ai:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new Conv2dOp with an optimized path for 1x1 convolutions, along with corresponding correctness tests and performance benchmarks. The implementation correctly dispatches to a faster GEMM-based kernel for 1x1 convolutions with stride 1 and no padding, while using a dedicated implicit-GEMM kernel for other 1x1 cases and an im2col approach for general convolutions. The changes are well-structured and include thorough testing. My feedback includes a suggestion to refactor some duplicated code in the new benchmark file to improve maintainability.
| def calculate_flops(self) -> Optional[float]: | ||
| test = self.test | ||
| kernel_h, kernel_w = test.kernel_size if isinstance(test.kernel_size, tuple) else ( | ||
| test.kernel_size, test.kernel_size) | ||
| stride_h, stride_w = test.stride if isinstance(test.stride, tuple) else ( | ||
| test.stride, test.stride) | ||
| pad_h, pad_w = test.padding if isinstance(test.padding, tuple) else ( | ||
| test.padding, test.padding) | ||
| out_h = (test.h + 2 * pad_h - kernel_h) // stride_h + 1 | ||
| out_w = (test.w + 2 * pad_w - kernel_w) // stride_w + 1 | ||
| return 2.0 * test.n * test.c_out * out_h * out_w * test.c_in * kernel_h * kernel_w | ||
|
|
||
| def calculate_memory(self) -> Optional[float]: | ||
| test = self.test | ||
| kernel_h, kernel_w = test.kernel_size if isinstance(test.kernel_size, tuple) else ( | ||
| test.kernel_size, test.kernel_size) | ||
| stride_h, stride_w = test.stride if isinstance(test.stride, tuple) else ( | ||
| test.stride, test.stride) | ||
| pad_h, pad_w = test.padding if isinstance(test.padding, tuple) else ( | ||
| test.padding, test.padding) | ||
| out_h = (test.h + 2 * pad_h - kernel_h) // stride_h + 1 | ||
| out_w = (test.w + 2 * pad_w - kernel_w) // stride_w + 1 | ||
| bias_elems = test.c_out if test.bias else 0 | ||
| total_elems = ( | ||
| test.n * test.c_in * test.h * test.w | ||
| + test.c_out * test.c_in * kernel_h * kernel_w | ||
| + bias_elems | ||
| + test.n * test.c_out * out_h * out_w | ||
| ) | ||
| return total_elems * test.dtype.itemsize |
There was a problem hiding this comment.
There's duplicated logic for calculating convolution parameters like kernel_h, kernel_w, out_h, and out_w in both calculate_flops and calculate_memory. This can be refactored into a helper method to improve maintainability and reduce redundancy.
def _get_derived_params(self) -> Tuple[int, int, int, int]:
test = self.test
kernel_h, kernel_w = test.kernel_size if isinstance(test.kernel_size, tuple) else (
test.kernel_size, test.kernel_size)
stride_h, stride_w = test.stride if isinstance(test.stride, tuple) else (
test.stride, test.stride)
pad_h, pad_w = test.padding if isinstance(test.padding, tuple) else (
test.padding, test.padding)
out_h = (test.h + 2 * pad_h - kernel_h) // stride_h + 1
out_w = (test.w + 2 * pad_w - kernel_w) // stride_w + 1
return kernel_h, kernel_w, out_h, out_w
def calculate_flops(self) -> Optional[float]:
test = self.test
kernel_h, kernel_w, out_h, out_w = self._get_derived_params()
return 2.0 * test.n * test.c_out * out_h * out_w * test.c_in * kernel_h * kernel_w
def calculate_memory(self) -> Optional[float]:
test = self.test
kernel_h, kernel_w, out_h, out_w = self._get_derived_params()
bias_elems = test.c_out if test.bias else 0
total_elems = (
test.n * test.c_in * test.h * test.w
+ test.c_out * test.c_in * kernel_h * kernel_w
+ bias_elems
+ test.n * test.c_out * out_h * out_w
)
return total_elems * test.dtype.itemsize
Closes #434
Summary
Test plan
python -m compileall tileops/ops/conv2d.py tileops/kernels/conv2d/pointwise.py tests/ops/test_conv2d.py tileops/kernels/conv2d/__init__.py tileops/kernels/__init__.pyPYTHONPATH="$PWD" python -m pytest -v tests/ops/test_conv2d.py -k 'dispatches_to_pointwise_kernel or dispatches_to_gemm_kernel or 1x1 and test_conv2d'BENCHMARK_REPORT_PATH=workdir_conv2d/profile_run.log python -m pytest -q benchmarks/ops/bench_conv2d.pyBenchmark
Measured on NVIDIA H200, torch 2.9.0+cu128, CUDA 12.8.
32x28x28 -> 64, k=1x1, s=2, p=1, bias, fp160.03 ms,0.03 TF,0.00 TB/s0.05 ms,0.02 TF,0.00 TB/s64x56x56 -> 256, k=1x1, s=1, p=0, fp160.02 ms,4.72 TF,0.09 TB/s0.00 ms,24.10 TF,0.48 TB/s64x56x56 -> 256, k=1x1, s=2, p=0, fp160.04 ms,0.65 TF,0.02 TB/s0.02 ms,1.24 TF,0.04 TB/s256x56x56 -> 512, k=1x1, s=1, p=0, bias, fp160.04 ms,18.57 TF,0.11 TB/s0.04 ms,19.52 TF,0.12 TB/s128x112x112 -> 512, k=1x1, s=1, p=0, bf160.08 ms,21.38 TF,0.21 TB/s0.03 ms,48.75 TF,0.48 TB/s64x56x56 -> 64, k=3x3, s=1, p=1, fp160.02 ms,9.43 TF,0.04 TB/s0.04 ms,5.38 TF,0.02 TB/s512x56x56 -> 512, k=3x3, s=1, p=1, bf160.12 ms,119.15 TF,0.09 TB/s0.23 ms,64.17 TF,0.05 TB/s128x112x112 -> 256, k=3x3, s=2, p=1, bf160.04 ms,43.01 TF,0.13 TB/s0.08 ms,24.59 TF,0.07 TB/s64x224x224 -> 128, k=5x5, s=2, p=2, fp160.09 ms,60.23 TF,0.12 TB/s0.16 ms,32.21 TF,0.06 TB/sAdditional context
workdir_conv2d/Error 304while trying to rerun GPU validation, so the benchmark table above reflects the last successful CUDA-visible runscripts/validate.shis not present in this checkout, so the repository PR validation script could not be run here