MSL: Add cooperative matrix element-wise emulation - #2645
Conversation
|
just one comment I see this code has in some way bfloat16 support.. maybe are you interested in adding VK_KHR_shader_bfloat16 support for MoltenVK.. was part of my original cooperative matrix request: most work should be in SPIRV-Cross side (i.e. support SPV_KHR_bfloat16), right? there are tests, testing bfloat16 throughput in standard way and coop matrix way.. for example clpeak2 (vulkan mode) shows currently: |
|
comment #2: sadly testing 4 tests I know.. at least 3 don't check that 8x8 is the only supported dim and fail: vkpeak don't know because fails with spec-constant use.. so maybe it's time to start opening issues on these 3 projects on their github sites? |
|
Hi @oscarbg, it is definitely possible for me to add some bfloat16 support - but should we leave that for a future PR? It is pretty likely that they do check for dims, but maybe because of the error bug you mentioned in the MoltenVK PR (now fixed), the error appeared because the spec constant default was not 8 - maybe worth me checking quickly but I will also post issues on all 4 now. Thanks for your help! I also fixed the windows issue so hopefully that workflow should pass now. |
1)yep, bfloat16 for a future PR is OK.. was only trying to gather interest on implementing support for it.. posting here for visibilty in the future: jeffbolznv/vk_cooperative_matrix_perf#13 |
|
@oscarbg I was thinking about what 0cc4m was saying under the llama.cpp issue - and maybe it makes more sense that we do hardcode 8x8 + subgroup size and give a warning if the user tries to use spec constants. This way we are not actively punishing developers who check properly, and explicitly stating the issue. I'm curious to hear your thoughts on this? Only issue I can think of is if Apple ever decides to support more than 8x8, but still doesn't allow spec constants. |
|
i have updated the code to reflect this, if you wanted to test it out, but it should be easy to revert if this is unwanted behaviour. |
| }); | ||
| } | ||
|
|
||
| // Metal emulation of coop-mat ops use a shared fixed 16KiB threadgroup scratch buffer for element-wise ops. |
There was a problem hiding this comment.
This is far too naive to be shippable. 16k LDS block will dramatically lower occupancy and performance for any shader that happens to use element-wise operations. Also, there is real risk of generating a broken shader that exceeds limits for LDS if the shader otherwise uses scratch memory.
| if (rows_c && cols_c) | ||
| { | ||
| if (rows_c->specialization || cols_c->specialization) { | ||
| fprintf(stderr, "MSL does not support spec-constant dimensions for cooperative matrices. Defaulting to 8x8 size.\n"); |
There was a problem hiding this comment.
Don't use fprintf here. I think the other code just faults the shader if this happens.
| statement("threadgroup uchar _spvCoopMatScratch[", kCoopMatScratchTotalBytes, "];"); | ||
| } | ||
|
|
||
| void CompilerMSL::emit_coop_mat_binary_elem_op(uint32_t result_type, uint32_t result_id, |
There was a problem hiding this comment.
At a glance this looks extremely duplicated. Is there no way to collapse at least some of this code duplication?
|
Did anyone raise this issue with Apple? Not having basic operations like this in the language is kinda ridiculous, and adding yet more massive emulation workarounds that slow things down for basic operations is just not sustainable. ... |
This PR is designed to fix some of the errors in KhronosGroup/MoltenVK#2753.
Adds support for element-wise operations on CooperativeMatrixKHR types in the MSL backend. Since Metal's simdgroup_matrix API does not natively support element-wise arithmetic, this implementation emulates these operations by lowering them to threadgroup scratch memory, applying the requested operation per-thread, and reloading the result into the matrix.
Some unavoidable limitations: