-
Notifications
You must be signed in to change notification settings - Fork 16
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
Mac OS: Bringing metal support #14
Comments
that was fast! before it had problem haha
|
Some lldb (debug) info
|
The GGML Assert is from ggml.metal.m I think. I don't know if that causes the abort or something else EDIT: yep, the asserts cause the block. that means there is probably an issue somewhere, but I can at least proceed to the next stages for now by skipping over it |
@balisujohn so this is what you were referring to about the unsupported ggml operations? It's very fast at least up until the problems
|
This is actually quite suspicious, |
any thoughts on where to look? |
I believe that PAD is also supported
Update: I'm removing some of the GGML_ASSERTs to see whether it's just the detection of support that is broken |
It seems to me like these numbers are somehow wrong for the Metal implementation
I did some printouts and apparently the upscale one it wants is 49, which doesn't seem to be what GGML_OP_UPSCALE is defined as. I added 49 for debugging, but I'm gonna swap that op out for it to try and get by the error Update: Yeh it got past, but then got error for a different op that also exists, so I'm going to figure there's something wrong with these numbers somewhere |
Possibly i made a mistake adding other new ops. Though not sure why it would work in cuda and not metal. |
@balisujohn they're out by two.. which seems consistent with this kind of thing
but if they're only defined in one place, I don't understand how this would happen |
These are out of order with the OP enum, possibly leading to at a minimum the wrong op names being printed. All of the things I linked should reference the same ops in the same order. https://github.com/balisujohn/ggml/blob/e9ac38f82c0956cf6f4e86206d4503095c25d47c/src/ggml.c#L2701 |
(I think the op failing on metal actually isn't upscale) |
so what would need to be reordered to see what is actually failing? |
The other two things I linked need to be added to and reordered to have the same number of elements as https://github.com/balisujohn/ggml/blob/e9ac38f82c0956cf6f4e86206d4503095c25d47c/include/ggml/ggml.h#L424, and in the same order. A very silly mistake on my end to not have these matching already. |
Should be adding these two I think @balisujohn are they familiar? |
yeah that's right, assuming the order is correct those are the two new ops (yet to be upstreamed to ggml proper) |
and I've confirmed they are not a part of the metal code of course.. so these are the ones I need to add. Which other ones did you add? |
I added a cuda kernel for |
https://github.com/ggerganov/ggml/pulls/balisujohn Useful for reference, though missing the getrows change |
I've started a todo list at top of the issue. is there a better part of the repo to have such a thing? |
I think the top of the issue is fine. Here's a link to join the Discord; a while back someone in the discord expressed interest in metal so they might be willing to help https://discord.gg/q9zufuhc |
Conversion without understanding is an interesting task haha. Technically, I can run this through by CPU to get expected outputs for different inputs.. then I can translate code the best I can, and then test them one by one. Have you got a more refined approach you could recommend? @balisujohn given you've just done this for CUDA edit: I did find the tests finally! I guess that's something |
This is hardly going to get the desired result, but I do at least know where to make the modifications.
it ran through to the end and made the .wav file (albeit, static noises only haha)
I see these run a few times. I wonder how far off the time for this to run (with those parts of the code missing) is from the time it would take when it's done. It took ~26 seconds on M3 Max. If this were to run on a server, would some of this just need to load one? or do all these steps need to run each time? and is it possible to stream or is everything done all at once? |
You could avoid repeating the model loads if streaming, you will still need to run the autoregressive model, diffusion model, and vocoder for each generation. I am working on making tortoise.cpp faster. |
Wrt how to get the ops working, each op has tests in the tests folder, you should be able to define a flag at the top of a particular test file to make it build with metal, then you can run that test from the |
I'll get up to speed with those tests next time I work on the operations. What do you have planned to make tortoise faster? |
there are unnecessary ops which can be removed, in particular some copies from float32 to float16 that can be removed once |
Another a related note, how fast is it already on CUDA? |
I got to 98% of diffusion before I got the unsupported op on M3 Max in about 26 seconds for "./tortoise". How far from the end to go from that point do you think? |
About 5-8 more seconds probably. The vocoder usually is pretty fast. |
that's a weird place for it to fail, I'd recommend checking with print statements to see if it fails before or after the last call to the diffusion graph. |
Well I know the first unsupported op is pad reflect 1D, so which part of the code is that? |
oh i was getting "illegal instruction" and "unsupported OP" confused, |
It must surely have been very close the end then 😅. Do we really need those ops? 🤣 |
You can save any tensor you want to wav, but I think without those ops it won't sound very good lol |
interesting ablation to try replacing pad_reflect with a normal pad but with all the other ops left the same to see if it totally ruins the generation. |
lol. I'm the kind of programmer who would try that just out of curiosity. I'm currently using Piper for tts and it's very fast, but sounds so fake. Maybe it would still sound better 🤣 |
In all seriousness though, are these ops similar? That's potentially where I should start for seeing how to write metal (which I've zero experience with) |
lmao it actually still works fine on cuda if you replace it with the below and the tests also pass.
yeah I'd definitely look at the pad implementation for metal because pad reflect is just padding that mirrors the elements of the tensor whereas normal pad is just padding with a constant value. I'd start by trying to make pad reflect 1d a working clone of pad, then change the behavior to do a reflect pad instead. You can check https://pytorch.org/docs/stable/generated/torch.nn.ReflectionPad1d.html for a description of the intended behavior. |
these are the unsupported operations as per my earlier comments
Do the other two have evil twins (the reflection) or are they quite unique from the existing ggml ops? Regardless, I didn't think to look at PyTorch docs thanks as I've only ever used PyTorch to run repos |
No equivalent as far as I know. So those are totally unavoidable to get a working metal forward pass. Also the metal kernel of pad maybe be out of date ggernagov patched it in the pr but im not sure if thats in the tortoise branch. |
I'm doing a bit of a "can chatGPT write metal" experiment. At the very least I've now got kernel functions and things setup so I'm in a position to work the problem. Things like "stride" and "block_size".. I see that for CUDA you set block_size to 256? Is there a performance thing or is that the value required to get the right result. with reference to https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html of course PS: This code could be way way off. It's better than nothing though at this stage EDIT: my conv transpose 1d code currently never completes so I can't tell if unfold gets the right result (it's just static but when I skip conv transpose 1d that could cause that perhaps |
so the way cuda works, you get some number of blocks all with the same number of threads per block. Generally you will assign a thread to each index of the output tensor, so the computation can be massively parallelized. You will have some extra threads if your output tensor size is not a multiple of the cuda block size, for these threads you check to see if the output index is out of bounds, and if it is you do nothing. I don't know how much of that transfers to metal. |
You can just use whatever block size the other ggml metal kernels use. I think it can affect performance but that is pretty secondary here. |
thanks for the insights. I now see that various block sizes are defined as constants in the .metal code. I'll look into how CUDA translates to metal. At some point I can perhaps find someone who actually knows what they're doing (referring to myself being clueless) to have a look at my progress and provide some feedback |
there is some code in main.cpp for saving tensors to and loading them from disk, and also comparing tensors to one another. This code might be helpful for confirming that the ops are work the same way on metal as on cuda. You can fetch intermediate result tensors out of the computational graph after the vocoder computation using |
for conv transpose 1d you can make a new PR directly to ggml with the metal kernel to get feedback, for the ones which are still being added via pending PRs, you can make a PR into my PR branches and I will accept it which should give you credit in the final PR for each op, and you can then get feedback from ggml contributors via the open PR discussions. |
Work is underway to get metal working for this project. Keen to get help from anyone interested.
TODO:
Existing PRs from @balisujohn decent example to follow for the changes done to CUDA already:
https://github.com/ggerganov/ggml/pulls/balisujohn
The text was updated successfully, but these errors were encountered: