<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jiqun Tu on Medium]]></title>
        <description><![CDATA[Stories by Jiqun Tu on Medium]]></description>
        <link>https://medium.com/@jtu_55029?source=rss-4738cc7cfe47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GRSpCem2JKc8zQBYOTOXxA.png</url>
            <title>Stories by Jiqun Tu on Medium</title>
            <link>https://medium.com/@jtu_55029?source=rss-4738cc7cfe47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:57:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jtu_55029/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Run Your Python User Defined Functions in Native CUDA Kernels with RAPIDS cuDF]]></title>
            <link>https://medium.com/rapids-ai/run-your-python-user-defined-functions-in-native-cuda-kernels-with-rapids-cudf-57477dd94fb3?source=rss-4738cc7cfe47------2</link>
            <guid isPermaLink="false">https://medium.com/p/57477dd94fb3</guid>
            <category><![CDATA[dataframes]]></category>
            <category><![CDATA[user-defined-functions]]></category>
            <category><![CDATA[cuda]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Jiqun Tu]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 20:02:32 GMT</pubDate>
            <atom:updated>2020-07-09T23:14:22.052Z</atom:updated>
            <content:encoded><![CDATA[<h4>Combining Python/CUDA JIT Compilation for Flexible Acceleration in RAPIDS</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q5wpOwhHQjX05gZpmNTV2A.jpeg" /></figure><p>In this blog, we’ll introduce our design and implementation of a framework within <a href="https://github.com/rapidsai/cudf">RAPIDS cuDF</a> that enables compiling Python user-defined functions (UDF) and inlining them into native <a href="https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html">CUDA kernels</a>. Our framework uses the <a href="http://numba.pydata.org/numba-doc/latest/cuda/index.html">Numba Python compiler</a> and <a href="https://github.com/NVIDIA/jitify">Jitify CUDA</a> just-in-time (JIT) compilation library to provide cuDF users the flexibility of Python with the performance of CUDA as a compiled language. An essential part of the framework is a parser that parses a function in one of the CUDA intermediate representations stage, which is compiled from the Python UDF, into an equivalent CUDA device function that can be inlined into native CUDA C++ kernels. Our approach makes it possible for Python users without CUDA programming knowledge to extend optimized dataframe operations with their own Python UDFs, which enables more flexibility and generality for high-performance computations on dataframes in RAPIDS.</p><p>We start by giving examples on how to use the feature, followed by the goals we intend to achieve. Finally, we explain how things work in the background to make the function possible.</p><h3>How to Use the Feature</h3><p>The feature is built into the framework of RAPIDS cuDF and is easy to use. Once a dataframe is created, simply call the interfaces that support this feature with the user-defined Python function. Currently, the list of support includes:</p><ol><li>applymap, which applies a UDF to each of the elements.</li><li>rolling, which applies a range-based UDF to each of the windows.</li></ol><p>In the following, we give examples with applymap and rolling.</p><h4>The `applymap` example:</h4><pre>&gt;&gt;&gt; import cudf<br>&gt;&gt;&gt; import cudf.core<br>&gt;&gt;&gt; from cudf.core import Series<br>&gt;&gt;&gt; import numpy as np<br>&gt;&gt;&gt; a = Series([9, 16, 25, 36, 49], dtype=np.float64)<br>&gt;&gt;&gt; a.applymap(lambda x: x ** 2)<br>0      81.0<br>1     256.0<br>2     625.0<br>3    1296.0<br>4    2401.0<br>dtype: float64<br>&gt;&gt;&gt; a.applymap(lambda x: 1 if x in [9, 44] else 2)<br>0    1<br>1    2<br>2    2<br>3    2<br>4    2<br>dtype: int64</pre><h4>The `rolling` example:</h4><pre>&gt;&gt;&gt; def foo(A):<br>...     sum = 0<br>...     for a in A:<br>...         sum = sum + a<br>...     return sum<br>... <br>&gt;&gt;&gt; a.rolling(3, 1, False).apply(foo)<br>0      9.0<br>1     25.0<br>2     50.0<br>3     77.0<br>4    110.0<br>dtype: float64</pre><h3>What the Feature Intends to Achieve: Flexibility and Performance</h3><h4>Ahead-of-Time Compilation</h4><p>Traditionally, with ahead-of-time compilation, CUDA kernels are compiled into SASS machine-level code at compile-time and launched at run time. In cases where operator functions need to be called by kernels, the use of function pointers or stack frame, which usually jeopardizes performance, are avoided by <em>inlining</em> the operator function, as shown in the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzZ81X8n6joLTbeHI_ZRSQ.png" /><figcaption>Code after inlining. Note that this is just an illustration: the actual inlining happens at NVVM IR level.</figcaption></figure><p>Performance is achieved; however, at the price of flexibility. Often at compile-time, the operator function is not known. In most cases, the program does not reach the end-users until run time, and it is the users who are going to decide what operator function is needed. With ahead of time compilation, users cannot write their operator function without recompiling the whole program while still having the maximum performance.</p><h4>Just-in-Time Compilation</h4><p>Just-in-time (JIT) compilation, or run-time-compilation, helps. Utilizing <a href="https://docs.nvidia.com/cuda/nvrtc/index.html">CUDA runtime compilation (NVRTC)</a> and the <a href="https://github.com/NVIDIA/jitify">JITIFY</a> library, the code string of the operator function, written at run time, can be inlined into the code string of the kernel (before the combination is compiled at run time) and launched with the same performance of a corresponding traditional native CUDA kernel. Flexibility and performance are optimized, with the only overhead being the time needed to perform the run time compilation.</p><h3>Combine Python and CUDA</h3><p>Combining the flexibility of Python as an interpreted language and the performance of CUDA as a compiled language will have broader coverage. A Python UDF can be written, without the knowledge or even awareness of CUDA, compiled and inlined into carefully optimized pre-defined CUDA kernels and launched on GPUs with maximum performance, as shown in the usage examples.</p><blockquote><strong><em>For more information about how Python is added to the workflow on top of NVRTC/JITIFY framework, check out my </em></strong><a href="https://developer.nvidia.com/blog/running-python-udfs-in-native-cuda-kernels-with-rapids-cudf/"><strong><em>NVIDIA DevBlog</em></strong></a><strong><em> on the topic.</em></strong></blockquote><h3>A Performance Benchmark of `applymap`</h3><p>We compare the performance of pandas.apply with cudf.applymap for dataframes with large numbers of rows and the later one is able to achieve significant speedup over the former one. The following benchmark is measured on an Intel(R) Xeon(R) Gold 6128 CPU and an NVIDIA Quadro GV100 GPU. Note that these results do not include the overhead of JIT compilation. This overhead is a one-time cost paid only on the first execution of this feature using a specific UDF.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2dlEhux9WyQpm-G3QUNDMw.png" /><figcaption>Benchmark results.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kHm6dd_bB6cOeQbpiLllxA.png" /><figcaption>Python code used to produce the benchmark.</figcaption></figure><h3>Conclusion</h3><p>Utilizing the benefits of Python and CUDA, the combined Python/CUDA JIT compilation in RAPIDS cuDF allows users to apply their Python functions on dataframes on NVIDIA GPUs with great flexibility while achieving the maximum performance. The feature’s idea of combining Python and just-in-time compilation applies beyond the scope of dataframe extract, transform, load (ETL), and has potentially many more use cases.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57477dd94fb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rapids-ai/run-your-python-user-defined-functions-in-native-cuda-kernels-with-rapids-cudf-57477dd94fb3">Run Your Python User Defined Functions in Native CUDA Kernels with RAPIDS cuDF</a> was originally published in <a href="https://medium.com/rapids-ai">RAPIDS AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>