Feature/opencl deserializer - #3435
SteveBronder wants to merge 8 commits into
Conversation
…rixCL> needs to wait for the full reverse pass stack to finish before writing the values back to the cpu
Construct serializer layouts from block sizes and alignment, use paired sizes and offsets in deserialization, and update buffer allocation calls. Remove unused serialization inputs and discarded-input casts. Add all OpenCL log-probability variants, template dispatch, and CRTP forwarding with explicit errors for unsupported models. Validated OpenCL serializer, deserializer, subbuffer, and model tests on the GPU; model headers also compile with OpenCL disabled.
In this alternative, would the I'm also curious how the usages of log_prob would need to be adjusted, e.g. to take a leapfrog step would now be something more than just some vector math, right? It almost sounds like Jax's PyTrees, which are super powerful in general but I imagine a bear to implement. |
The context class would mostly just be a wrapper around an So in the algorithm we start with a vector that we need to break up into chunks on the GPU. We can either break it up into chunks by the scheme here where we have one giant buffer and then correctly pad each parameter such that we can make sub buffers representing the parameters. That requires knowing the dimensions of each parameter such that we can do the sub buffer padding correctly. The sub buffer padding condition is kind of a bummer because it means that, even if the algorithm we used was totally on the gpu, we would still need to do this chunked copy over to the buffer with the correct padding in it so that we could then make sub buffers. So the flow is like Alternatively, we could have some sort of context which holds a
Now that I have typed it out I think we would need something like
So I think the actual impl of Either way, I think this would require the model making a new |
I don't hate that, but it would mean that the new log prob would be for a specific concrete |
|
Yeah but now that I'm golfing the context pattern I just don't really see what the pros would be relative to just the big buffer approach we have here. For instance, even if the algorithm was on the gpu and the input vector was already an opencl buffer I don't think the context here provides a lot of benefit over the buffer version. |
Submission Checklist
./runTests.py src/test/unitmake cpplintSummary
This PR adds a
deserializerthat works withmatrix_clandvar_value<matrix_cl>types. This will allow Stan models to havelog_probmethods which accept a gpu matrix type as the serialized input and then return back the gradient on the GPU.The main tricky part of this pull request is the gpu's min alignment of subbuffers for OpenCL. In OpenCL, if you have a buffer on the gpu then you are allowed to make sub buffers out of that buffer as long as the sub buffers are aligned on the correct boundary. This is usually 128 bytes, but can vary from device to device.
We call
log_probwith a vector that is a serialized representation of the parameters of the stan model. When we move that vector over to the GPU we need add padding to the GPU buffer such that when we make the sub buffers they have the correct alignment. So this PR is a mix of thedeserializerclass we need in the stan model the stan compiler generates and utilities for making sure the memory on the GPU is padded correctly.Another alternative design would be to just have a vector of matrix_cl buffers and have the
log_probsignature take in some sort ofcontexttype. Since the matrix_cl types will need their ownlog_probwe can totally rethink howlog_probworks. For instance, the signature for the gpu code could be something like the below where we just pass a map with the buffers we need stored in itThen internally we would not use a deserializer at all but have code like
I'm very open to this design. It would complicate things in the compiler but simplify the data passing which I think would be nice.
How to Verify
Copyright and Licensing
Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company): Simons Foundation
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses: