Skip to content

Use dynamic shared memory for CUDA #8317

Closed

Description

@ThomasRaoux

We currently use static shared memory which means the shared memory size is declared in the kernel.
Recent GPUs support a larger shared memory size when using dynamic shared memory. (https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared-memory-7-x)

As IREE controls both the compiler and runtime using static or dynamic shared memory doesn't make a big difference. Switching to dynamic shared memory all the time will allow us to allocate more shared memory to a given group and have a larger search space for code generation.

What needs to be done:

  • Change the logic that creates several global static shared memory in codegen and allocate a single dynamically sized global. Split the global between the different allocations. (either using subview op or by generating directly llvm pointer arithmetic). Current logic is here
  • Add a new attribute to the kernel to indicate the shared memory size
  • Add the shared memory size to the flat buffer definition.
  • Pass the dynamic shared memory size to the dispatch call in HAL (should support both cuda stream and cuda graph)

This shouldn't break ROCm backend, we should change it for CUDA only. If we think it may also help ROCm we could change it there as well.

Activity

  1. added
    help wanted
    hal/cuda
    codegen/nvvm
    good first issue 🌱
    on Feb 14, 2022
  2. benvanik commented on Feb 14, 2022

    @benvanik
    Collaborator

    That approach should be fine for now - if we need to compute a shared memory size at runtime based on device parameters and such we'll need to use the specialization constants approach instead (effectively runtime templating of executables), but that's bigger changes and easy to swap in place of this when we get there.

  3. ThomasRaoux commented on Feb 14, 2022

    @ThomasRaoux
    ContributorAuthor

    That approach should be fine for now - if we need to compute a shared memory size at runtime based on device parameters and such we'll need to use the specialization constants approach instead (effectively runtime templating of executables), but that's bigger changes and easy to swap in place of this when we get there.

    You mean have it explicitly in the hal dialect? That makes sense, I'm hoping we won't have to do that as in general the shared memory size should be only tied to the tile size that we should always know at compile time.

  4. benvanik commented on Feb 14, 2022

    @benvanik
    Collaborator

    Correct! When the executables are created we'll be able to pass in a table of key=value pairs that get substituted for values in the executables and those values can be derived from any VM code we want. This would result in a fixed parameter table available to all dispatches in CPU/CUDA (where we can't easily mutate compiled code) or in the case of a JIT (SPIR-V/WGSL/MSL/etc) will specialize the code in the driver compiler.

    The issue with shared memory in general is that we can't know the available amount at compile-time (https://vulkan.gpuinfo.org/displaydevicelimit.php?name=maxComputeSharedMemorySize&platform=all, Metal is 16-32KB, etc) and we also need to balance the local workgroup size that also changes. Today we're implicitly assuming these limits and that prevents us from generating portable binaries - this may matter less in the CUDA case because of the closed ecosystem and the architecture<->shared memory size association that usually makes it fine to assume, but something we'll need for other backends (CPU for cache sizes, Vulkan/Metal/etc for shared memory sizes, etc). The mid-term goal for codegen should be to get to the point where the workgroup size is expressed as a computation derived from device parameters (similar to the workgroup count) - once we do that it'll be easier to wire all this up so that if not available at compilation time we can emit the code to derive it all. Anything else we want to compute could be expressed in the same way (workgroup count limits, subgroup sizes, etc).

    I think going the pragmatic route here you mention is great as an early version of the specialization constants could have a special case for local workgroup size xyz and it'd be something that could wire right up in place of the flatbuffer fields!

  5. ThomasRaoux commented on Feb 14, 2022

    @ThomasRaoux
    ContributorAuthor

    ah good point, I didn't see it that way. Sounds like having real dynamic shared memory makes sense. I'll keep that in mind.

  6. nirvedhmeshram commented on Feb 16, 2022

    @nirvedhmeshram
    Contributor

    What needs to be done:

    • Change the logic that creates several global static shared memory in codegen and allocate a single dynamically sized global. Split the global between the different allocations. (either using subview op or by generating directly llvm pointer arithmetic). Current logic is here

    I am currently working on this part

  7. nirvedhmeshram commented on Feb 24, 2022

    @nirvedhmeshram
    Contributor

    I have made progress on this feature #8405

  8. nirvedhmeshram commented on Feb 26, 2022

    @nirvedhmeshram
    Contributor

    This is now implemented #8405

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen/nvvmgood first issue 🌱hal/cudahelp wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions