<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aarav Aggarwal</title>
    <description>The latest articles on DEV Community by Aarav Aggarwal (@aarav_aggarwal).</description>
    <link>https://dev.to/aarav_aggarwal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4036188%2F0871e982-4fa5-4609-9db8-29e3781e2498.jpg</url>
      <title>DEV Community: Aarav Aggarwal</title>
      <link>https://dev.to/aarav_aggarwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aarav_aggarwal"/>
    <language>en</language>
    <item>
      <title>I Built a Deep Learning Framework from Scratch in C++ and CUDA (And Beat PyTorch's Speed Multiple Run)</title>
      <dc:creator>Aarav Aggarwal</dc:creator>
      <pubDate>Sun, 19 Jul 2026 07:24:15 +0000</pubDate>
      <link>https://dev.to/aarav_aggarwal/i-built-a-deep-learning-framework-from-scratch-in-c-and-cuda-and-beat-pytorchs-speed-multiple-1eh3</link>
      <guid>https://dev.to/aarav_aggarwal/i-built-a-deep-learning-framework-from-scratch-in-c-and-cuda-and-beat-pytorchs-speed-multiple-1eh3</guid>
      <description>&lt;p&gt;If you work in AI today, you are almost certainly using PyTorch or TensorFlow. They are incredible tools, but they are also massive black boxes. &lt;/p&gt;

&lt;p&gt;As an AI developer, I realized that relying solely on these frameworks meant I didn't truly understand the underlying hardware realities, memory mechanics, or how the math actually maps to GPU acceleration. &lt;/p&gt;

&lt;p&gt;So, I decided to strip away the abstractions. Over the last few months, I built &lt;strong&gt;Aakaar&lt;/strong&gt;—a deep learning framework developed completely from scratch using native C++ and CUDA, wrapped in Python for ease of use.&lt;/p&gt;

&lt;p&gt;Here is how I built it, the engineering challenges I faced, and how it surprisingly edged out PyTorch in a direct benchmark.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Architecture: Hand-Coding the Math
&lt;/h2&gt;

&lt;p&gt;Building a framework from scratch means you don't get autograd for free. &lt;/p&gt;

&lt;p&gt;The core architecture of Aakaar relies on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Backend:&lt;/strong&gt; A purely native C++ implementation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Components:&lt;/strong&gt; 18 hand-coded native loss modules and 11 custom optimizers built directly into the C++ backend.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Management:&lt;/strong&gt; Explicit contiguity management during tensor transpositions and custom backpropagation. Instead of relying on an automated computational graph, I had to manually track and allocate memory layouts during the backward passes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frontend:&lt;/strong&gt; A Python wrapper so models can be defined intuitively, similar to standard frameworks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Hardest Part: Memory on the GPU
&lt;/h2&gt;

&lt;p&gt;The primary engineering hurdle wasn't the forward pass—it was the backward pass. Mapping abstract mathematical shapes directly to physical GPU memory layouts during backpropagation is brutally unforgiving. &lt;/p&gt;

&lt;p&gt;If your memory isn't strictly contiguous when a tensor transposes during a CUDA kernel execution, the entire system bottleneck slows to a crawl or crashes. Managing that contiguity explicitly in C++ without standard autograd overhead was a massive exercise in systems engineering.&lt;/p&gt;

&lt;h2&gt;
  
  
  The EMNIST Benchmark: Aakaar vs. PyTorch
&lt;/h2&gt;

&lt;p&gt;To see if Aakaar was structurally viable, I didn't want to just run a toy matrix multiplication. I set up a 5-epoch training loop on the EMNIST dataset and benchmarked Aakaar directly against PyTorch on my local system (Intel i7, RTX 4060, 8GB VRAM).&lt;/p&gt;

&lt;p&gt;To ensure a fair test, the model architecture, hyperparameters, and weights were identical across both frameworks. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Results (Average of multiple test runs):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Aakaar:&lt;/strong&gt; 127.76 seconds(83.30% acc)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PyTorch:&lt;/strong&gt; 131.23 seconds(82.55% acc)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why was it faster?
&lt;/h3&gt;

&lt;p&gt;Aakaar consistently maintained absolute convergence parity while delivering a slight edge in runtime speed. This performance advantage stems directly from bypassing the Python runtime overhead during the low-level C++ optimizer steps. By keeping the optimizer operations strictly in the native backend, Aakaar shaved off the milliseconds of overhead that accumulate over thousands of batches.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next and How to Contribute
&lt;/h2&gt;

&lt;p&gt;Seeing the math align perfectly with the hardware performance made the struggle worth every line of code. &lt;/p&gt;

&lt;p&gt;Aakaar is fully open-source. I am currently looking for feedback from the systems engineering and AI infrastructure communities. Specifically, if you have experience with CUDA kernel optimization or C++ memory allocation strategies, I would love for you to audit the architecture. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Test Run (Source Code):&lt;/strong&gt; &lt;a href="https://github.com/aaravaggarwal3535/aakaar-wheels/blob/main/main.ipynb" rel="noopener noreferrer"&gt;https://github.com/aaravaggarwal3535/aakaar-wheels/blob/main/main.ipynb&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation:&lt;/strong&gt; &lt;a href="https://aakaar.readthedocs.io" rel="noopener noreferrer"&gt;https://aakaar.readthedocs.io&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you ever tried building ML components from scratch? Let me know your thoughts or optimization ideas in the comments below!&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>cpp</category>
      <category>python</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
