-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add conv2d Implicit GEMM #15805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add conv2d Implicit GEMM #15805
Conversation
…es for 2D convolution
Why are you adding a new ggml op? |
Because of #15669 (comment) |
I think the implementation of implicit gemm can directly use ggml_conv2d_direct. There's really no need to provide so many conv2d functions. |
I can reuse ggml_conv2d_direct. TBH it is not a very good or intuitive name (the best one, ggml_conv_2d, is already occupied). I do wish it has an additional argument (ggml_conv_2d should carry in the beginning) for what method implemented. |
If the performance of implicit gemm is on par with or even better than that of im2col + gemm, I think ggml_conv_2d can also adopt the implementation of implicit gemm. |
What should be done regarding For kernel selection, please take a look at how e.g. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this PR, try removing the current conv2d kernel and replacing it with this one. Chances are it will be universally faster since it uses shared memory and has (unless I misread the code) coalesced memory accesses. I'll test the performance using a P40, RTX 3090, and RTX 4090 for NVIDIA and an RX 6800 and Mi 50 for AMD.
#include "convert.cuh" | ||
|
||
typedef struct{ | ||
unsigned int n; //batch szie |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned int n; //batch szie | |
unsigned int n; //batch size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
typedef struct{ | ||
unsigned int n; //batch szie | ||
unsigned int c; //channel number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to either "channel index" or "number of channels" depending on which this is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
int threadz = 1; // threadz number per block | ||
dim3 thblock(threadx, thready, threadz); | ||
dim3 grid(blockx, blocky, blockz); | ||
int smem_size = 24 * 1024; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On some CUDA architectures shared memory comes out of the L1 cache so it at all possible you should reserve only as much as will actually be used.
float * __restrict__ output, | ||
const param_t param) { | ||
|
||
extern __shared__ __align__(16 * 1024) char smem[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of __align__
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed, no difference in performance
for (int i = 0; i < 4; ++i) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (int i = 0; i < 4; ++i) | |
{ | |
for (int i = 0; i < 4; ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. corrected styles in all places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @JohannesGaessler, for taking time to review. I agree with your idea as to kernel selection behind the scenes. Indeed, no single kernel is optimal for input and filter shapes. That's why cudnn provide all kinds of them for user to choose. Previously I am not sure if selecting kernels is possible and I 'll look into FLASH_ATTN_EXT example (thanks again).
Now #15813 is adding tensor support with shared mem, I don't want to step over. This PR will be in hold for now. I may contribute to the current conv_2d_direct once tensor code is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if there is a kernel with tensor core support a good kernel without tensor cores would still be extremely useful. P40s and Mi50s are very cheap options for 24/32 GB VRAM but they lack tensor cores. And from a ggml perspective it's much easier to squeeze out more performance than it is to compress the weights (without affecting quality).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking of P40s, you should be careful with FP16 arithmetic since that is massively gimped on Pascal. You can use the macro FAST_FP16_AVAILABLE
to check whether FP16 would be fast and use FP32 as a workaround if not. You can look at e.g. mmvf.cu
for an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will look into it. Thanks.
…ncy; update parameter comments and remove unused code
…test for implicit convolution
erroneous numbers. old numbersTook it for a small test drive in sd.cpp for VAE decoding: 768x1024 sd1 fp16 vae:
The resulting images look correct. For some reason implicitgemm is as fast as the current direct implementation. im2col+mat_mul
direct
implicitgemm
|
@Green-Sky, thanks for giving it a try-out. I am a bit puzzled by the results.
|
diff --git a/ggml_extend.hpp b/ggml_extend.hpp
index 560d2861..36048bcc 100644
--- a/ggml_extend.hpp
+++ b/ggml_extend.hpp
@@ -851,7 +851,7 @@ __STATIC_INLINE__ struct ggml_tensor* ggml_nn_conv_2d_direct(struct ggml_context
int p1 = 0,
int d0 = 1,
int d1 = 1) {
- x = ggml_conv_2d_direct(ctx, w, x, s0, s1, p0, p1, d0, d1);
+ x = ggml_conv_2d_implicitgemm(ctx, w, x, s0, s1, p0, p1, d0, d1);
if (b != NULL) {
b = ggml_reshape_4d(ctx, b, 1, 1, b->ne[0], 1);
// b = ggml_repeat(ctx, b, x);
Yes, I first thought I did something wrong, but it is only one call in sd.cpp (see diff) and I looked at your code and it also looks like the OP is properly emitted ... Giving it another try, just to make sure I really did nothing wrong here (: . update: ok, getting (way) better values now. I do not know what I did wrong before, I made sure to recompile a couple of times with different settings, but it really did use the naive conv2d_direct impl. My mistake. |
Corrected test drive in sd.cpp for VAE decoding: 768x1024 sd1 fp16 vae:
Ignore previous numbers. Now looking more like it. Good job @bssrdf :) , sorry for the confusion. edit: added vulkan numbers from same device. (cm2) |
Thanks for the update, @Green-Sky. The sd.cpp result is consistent with what I tested in ggml backend op. I hope to improve implicit's performance, especially for Fp16. There should be plenty of low hanging fruits there. |
Hi, there are a few optimizations used in Vulkan: mininizing branch divergence, reducing the number of modulo/div computations with shuffles. Later @jeffbolznv further optimized it by using constant int divs and optimized the blocktile sizes to common inputs. Although the GLSL code is not too hard to read, I added the CUDA translation of the GLSL shader in a new PR: #16088. It also introduces some bank conflict reduction, and vectorized shmem loads so it is now faster than Vulkan on large inputs. |
@etasnadi, thanks for working on the cuda backend. Yes, with these optimizations in place, cuda should be on par with vulkan if not faster. Great job! |
Now #16088 is a better implementation. |
Actually, it is a somewhat better implementation you just forget to enable it in your code so the previous inefficient implementation was used, so I suggest to further merge on this PR. See my comment in: etasnadi#1 (comment) |
This PR added another CUDA conv_2d op using implicit GEMM approach. It is only optimized for cuda cores and its performance is up to 10x of that of direct method currently in llama.cpp.
On a RTX4090
Comparison with im2col+gemm
Fp16 filter, Fp32 activation
Fp32 filter, Fp32 activation