- 
                Notifications
    You must be signed in to change notification settings 
- Fork 58
Adding demo for running softmax kernel on Google colab #944
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3f6f6cd
              
                Adding Python notebook for Softmax kernel
              
              
                choijon5 bc2d932
              
                Add softmax notebook example to documentation and README with google …
              
              
                choijon5 63c1c76
              
                Add softmax notebook to be tested on CI. nbmake allows to run the not…
              
              
                choijon5 6c49957
              
                Ignore linting for Python notebook files under notebooks.
              
              
                choijon5 961c12b
              
                Update notebook example to use PyTorch 2.9. Create separate command t…
              
              
                choijon5 de6ac87
              
                Fixing test.yml script for notebook test.
              
              
                choijon5 ea3e3ae
              
                Fixing notebook test to work properly on CI.
              
              
                choijon5 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%pip install \"torch==2.9.*\" --index-url https://download.pytorch.org/whl/cu126\n", | ||
| "%pip install helion\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "\"\"\"\n", | ||
| "Helion Softmax Kernel Examples\n", | ||
| "==============================\n", | ||
| "This example demonstrates multiple Helion kernel implementations of the softmax function,\n", | ||
| "including a simple wrapper around PyTorch's softmax, and a numerically optimized two-pass version.\n", | ||
| "The example also includes a check function to compare these kernels against PyTorch's\n", | ||
| "built-in softmax for correctness.\n", | ||
| "\"\"\"\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "from __future__ import annotations\n", | ||
| "import torch\n", | ||
| "import helion\n", | ||
| "from helion._testing import run_example\n", | ||
| "import helion.language as hl\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "@helion.kernel(autotune_effort=\"quick\")\n", | ||
| "def softmax(x: torch.Tensor) -> torch.Tensor:\n", | ||
| " \"\"\"\n", | ||
| " Simple Helion kernel wrapping PyTorch's softmax function.\n", | ||
| " Args:\n", | ||
| " x (torch.Tensor): Input tensor of shape [n, m].\n", | ||
| " Returns:\n", | ||
| " torch.Tensor: Softmax output tensor of the same shape.\n", | ||
| " \"\"\"\n", | ||
| " n, _m = x.size()\n", | ||
| " out = torch.empty_like(x)\n", | ||
| " for tile_n in hl.tile(n):\n", | ||
| " out[tile_n, :] = torch.nn.functional.softmax(x[tile_n, :], dim=1)\n", | ||
| " return out\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "def check(m: int, n: int) -> None:\n", | ||
| " \"\"\"\n", | ||
| " Runs correctness checks comparing Helion softmax kernels against PyTorch's softmax.\n", | ||
| " Args:\n", | ||
| " m (int): Number of rows in input tensor.\n", | ||
| " n (int): Number of columns in input tensor.\n", | ||
| " \"\"\"\n", | ||
| " x = torch.randn([m, n], device=\"cuda\", dtype=torch.float16)\n", | ||
| " run_example(softmax, lambda x: torch.nn.functional.softmax(x, dim=1), (x,))\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "def main() -> None:\n", | ||
| " \"\"\"\n", | ||
| " Main function to run the softmax kernel correctness check with example input size.\n", | ||
| " \"\"\"\n", | ||
| " check(4096, 2560)\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "if __name__ == \"__main__\":\n", | ||
| " main()\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "\"\"\"\n", | ||
| "Helion Softmax Kernel Examples\n", | ||
| "==============================\n", | ||
| "This example demonstrates multiple Helion kernel implementations of the softmax function,\n", | ||
| "including a simple wrapper around PyTorch's softmax, and a numerically optimized two-pass version.\n", | ||
| "The example also includes a check function to compare these kernels against PyTorch's\n", | ||
| "built-in softmax for correctness.\n", | ||
| "\"\"\"\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "from __future__ import annotations\n", | ||
| "import torch\n", | ||
| "import helion\n", | ||
| "from helion._testing import run_example\n", | ||
| "import helion.language as hl\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "@helion.kernel(autotune_effort=\"quick\")\n", | ||
| "def softmax_two_pass(x: torch.Tensor) -> torch.Tensor:\n", | ||
| " \"\"\"\n", | ||
| " Numerically optimized Helion kernel performing softmax in two passes.\n", | ||
| " Args:\n", | ||
| " x (torch.Tensor): Input tensor of shape [m, n].\n", | ||
| " Returns:\n", | ||
| " torch.Tensor: Softmax output tensor of the same shape.\n", | ||
| " \"\"\"\n", | ||
| " m, n = x.size()\n", | ||
| " out = torch.empty_like(x)\n", | ||
| " block_size_m = hl.register_block_size(m)\n", | ||
| " block_size_n = hl.register_block_size(n)\n", | ||
| " for tile_m in hl.tile(m, block_size=block_size_m):\n", | ||
| " mi = hl.full([tile_m], float(\"-inf\"), dtype=torch.float32)\n", | ||
| " di = hl.zeros([tile_m], dtype=torch.float32)\n", | ||
| " for tile_n in hl.tile(n, block_size=block_size_n):\n", | ||
| " values = x[tile_m, tile_n]\n", | ||
| " local_amax = torch.amax(values, dim=1)\n", | ||
| " mi_next = torch.maximum(mi, local_amax)\n", | ||
| " di = di * torch.exp(mi - mi_next) + torch.exp(\n", | ||
| " values - mi_next[:, None]\n", | ||
| " ).sum(dim=1)\n", | ||
| " mi = mi_next\n", | ||
| " for tile_n in hl.tile(n, block_size=block_size_n):\n", | ||
| " values = x[tile_m, tile_n]\n", | ||
| " out[tile_m, tile_n] = torch.exp(values - mi[:, None]) / di[:, None]\n", | ||
| " return out\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "def check(m: int, n: int) -> None:\n", | ||
| " \"\"\"\n", | ||
| " Runs correctness checks comparing Helion softmax kernels against PyTorch's softmax.\n", | ||
| " Args:\n", | ||
| " m (int): Number of rows in input tensor.\n", | ||
| " n (int): Number of columns in input tensor.\n", | ||
| " \"\"\"\n", | ||
| " x = torch.randn([m, n], device=\"cuda\", dtype=torch.float16)\n", | ||
| " run_example(softmax_two_pass, lambda x: torch.nn.functional.softmax(x, dim=1), (x,))\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "def main() -> None:\n", | ||
| " \"\"\"\n", | ||
| " Main function to run the softmax kernel correctness check with example input size.\n", | ||
| " \"\"\"\n", | ||
| " check(4096, 2560)\n", | ||
| "\n", | ||
| "\n", | ||
| "# %%\n", | ||
| "if __name__ == \"__main__\":\n", | ||
| " main()\n" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "accelerator": "GPU", | ||
| "colab": { | ||
| "gpuType": "T4", | ||
| "provenance": [] | ||
| }, | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "name": "python" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 0 | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
you need to limit this to test folder now
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.
do you mean I should move this to the test folder?
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.
pytest -rf --timeout=60 test