|
| 1 | +# Hugging Face kernel hub example |
| 2 | + |
| 3 | +This shows an example using the Hugging Face kernel hub to download a |
| 4 | +pre-compiled kernel. In this example, the |
| 5 | +[activation](https://huggingface.co/kernels/kernels-community/activation) |
| 6 | +gelu_and_mul kernel is demonstrated. At the time of this writing, this set |
| 7 | +of kernels has builds for Nvidia CUDA and Apple Metal. For the exact set of |
| 8 | +supported hardware, check the kernel card. |
| 9 | + |
| 10 | +## Build |
| 11 | + |
| 12 | +There is a Containerfile that encapsulates the environment needed to run a |
| 13 | +torch application that uses the kernels interface on Nvidia GPUs. |
| 14 | + |
| 15 | +```bash |
| 16 | +podman build . -t gelu:latest |
| 17 | +``` |
| 18 | + |
| 19 | +You can also see what the Containerfile does and recreate it locally in a |
| 20 | +python virtual environment if you prefer not to use containers, for example |
| 21 | +if you want to try it on Apple Metal. |
| 22 | + |
| 23 | +## Run |
| 24 | + |
| 25 | +Set the HF_TOKEN environment variable to your Hugging Face token. Then: |
| 26 | + |
| 27 | +```bash |
| 28 | +podman run -it --rm --device nvidia.com/gpu=all --security-opt=label=disable -e HF_TOKEN=${HF_TOKEN} gelu:latest python3.12 gelu-and-mul-test.py |
| 29 | +``` |
| 30 | + |
| 31 | +The output should look something like this if you have a supported Nvidia GPU |
| 32 | +and >=580 driver: |
| 33 | + |
| 34 | +```bash |
| 35 | +========== |
| 36 | +== CUDA == |
| 37 | +========== |
| 38 | + |
| 39 | +CUDA Version 13.0.3 |
| 40 | + |
| 41 | +Container image Copyright (c) 2016-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 42 | + |
| 43 | +This container image and its contents are governed by the NVIDIA Deep Learning Container License. |
| 44 | +By pulling and using the container, you accept the terms and conditions of this license: |
| 45 | +https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license |
| 46 | + |
| 47 | +A copy of this license is made available in this container at /NGC-DL-CONTAINER-LICENSE for your convenience. |
| 48 | + |
| 49 | +Fetching 6 files: 100%|███████████████████████████████████████████████████████████████| 6/6 [00:01<00:00, 3.99it/s] |
| 50 | +Download complete: : 4.18MB [00:01, 3.30MB/s] Success! | 2/6 [00:01<00:03, 1.17it/s] |
| 51 | +Download complete: : 4.18MB [00:01, 2.62MB/s] |
| 52 | +``` |
| 53 | + |
| 54 | +Note that the kernels package will output progress information about the |
| 55 | +download to stderr. You can filter that out. For example, you can |
| 56 | +redirect the stderr to /dev/null if you want cleaner output. Just be aware |
| 57 | +that debugging failures will be harder that way. |
| 58 | + |
| 59 | +```bash |
| 60 | +$ podman run -it --rm --device nvidia.com/gpu=all --security-opt=label=disable -e HF_TOKEN=${HF_TOKEN} gelu:latest bash |
| 61 | + |
| 62 | +========== |
| 63 | +== CUDA == |
| 64 | +========== |
| 65 | + |
| 66 | +CUDA Version 13.0.3 |
| 67 | + |
| 68 | +Container image Copyright (c) 2016-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 69 | + |
| 70 | +This container image and its contents are governed by the NVIDIA Deep Learning Container License. |
| 71 | +By pulling and using the container, you accept the terms and conditions of this license: |
| 72 | +https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license |
| 73 | + |
| 74 | +A copy of this license is made available in this container at /NGC-DL-CONTAINER-LICENSE for your convenience. |
| 75 | + |
| 76 | +[root@fc9f150b0e9e src]# python3.12 gelu-and-mul-test.py 2> /dev/null |
| 77 | +Success! |
| 78 | +``` |
0 commit comments