|
14 | 14 | <link href="./style.css" rel="stylesheet" type="text/css"> |
15 | 15 | <title>Refactoring Codebases through Library Design</title> |
16 | 16 |
|
| 17 | + <!-- MathJax Configuration --> |
| 18 | + <script> |
| 19 | + window.MathJax = { |
| 20 | + tex: { |
| 21 | + inlineMath: [['$', '$'], ['\\(', '\\)']], |
| 22 | + displayMath: [['$$', '$$'], ['\\[', '\\]']], |
| 23 | + processEscapes: true, |
| 24 | + processEnvironments: true, |
| 25 | + macros: { |
| 26 | + program: '\\ensuremath{\\rho}', |
| 27 | + library: '\\ensuremath{\\mathcal{L}}', |
| 28 | + loss: ['\\ensuremath{\\ell}\\left( #1 \\right)', 1], |
| 29 | + sample: ['\\ensuremath{\\textsc{Sample}}\\left( #1 \\right)', 1], |
| 30 | + instance: 'Source' |
| 31 | + } |
| 32 | + }, |
| 33 | + options: { |
| 34 | + ignoreHtmlClass: 'tex2jax_ignore', |
| 35 | + processHtmlClass: 'tex2jax_process' |
| 36 | + } |
| 37 | + }; |
| 38 | + </script> |
| 39 | + <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> |
| 40 | + <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> |
| 41 | + |
17 | 42 | <script> |
18 | 43 | function copyText() { |
19 | 44 | var text = document.getElementById("citation-content") |
@@ -81,6 +106,59 @@ <h2 id="contributions">Key Contributions</h2> |
81 | 106 | </div> |
82 | 107 | </section> |
83 | 108 |
|
| 109 | + <section> |
| 110 | + <h2 id="project-goal">Problem Statement</h2> |
| 111 | + <p>We're tackling the challenge of refactoring code to make it more organized and efficient. Imagine you have several pieces of code that do similar things. Our goal is to create a unified library that captures the common patterns, significantly reducing the total code size while still supporting all original functionalities. This not only makes the code base smaller but also helps uncover new ways to use the code.</p> |
| 112 | + |
| 113 | + <h3>How We Do It</h3> |
| 114 | + <p>Our approach focuses on finding refactorings that are both correct and simple.</p> |
| 115 | + <ul> |
| 116 | + <li><strong>Correctness</strong> is straightforward: Does the refactored code pass all the original tests?</li> |
| 117 | + <li><strong>Simplicity</strong> is more nuanced. We don't just count characters; we define simplicity using <strong>Minimum Description Length (MDL)</strong>. This means we're looking for code that is not only short but also natural, elegant, and extensible—like finding the most concise yet understandable way to express an idea, rather than just the shortest, potentially unreadable, version (think "Perl Golf" where the shortest code is often incomprehensible!).</li> |
| 118 | + </ul> |
| 119 | + |
| 120 | + |
| 121 | + <h3>The Math Behind It</h3> |
| 122 | + <p>Formally, given a set of original programs $\{\rho_n\}_{n=1}^N$, we want to find a new library $\mathcal{L}$ and refactored programs $\{\rho'_n\}_{n=1}^N$. We optimize the following objective:</p> |
| 123 | + |
| 124 | + <div class="math-display"> |
| 125 | + $$ |
| 126 | + \ell(\mathcal{L}, \{\rho'_n\}) = |
| 127 | + \begin{cases} |
| 128 | + -\log p_{\text{LM}}(\mathcal{L}) + \sum_n -\log p_{\text{LM}}(\rho'_n\mid\mathcal{L}) & \forall \rho_n, \, \tau(\rho_n) \leq \tau(\rho'_n) \\ |
| 129 | + \infty & \text{otherwise} |
| 130 | + \end{cases} |
| 131 | + $$ |
| 132 | + </div> |
| 133 | + <p style="margin-top: 1em;"> |
| 134 | + Here, $p_{\text{LM}}(\mathcal{L})$ is the probability of the library under a language model, and $p_{\text{LM}}(\rho'_n\mid\mathcal{L})$ is the probability of the refactored program $\rho'_n$ given the library $\mathcal{L}$. The constraint $\tau(\rho_n) \leq \tau(\rho'_n)$ ensures that the refactored programs pass at least as many tests as the originals. The loss function $\ell$ thus encourages solutions that are both correct and have minimal description length, as measured by the language model. |
| 135 | + </p> |
| 136 | + <p>In simpler terms, we're looking for a library and refactored programs that pass at least as many tests as the originals, and whose combined "description length" (how hard they are to describe using a language model) is minimized. This ensures our refactored code is not only correct but also intuitively simple and well-structured.</p> |
| 137 | + </section> |
| 138 | + |
| 139 | + <section> |
| 140 | + <h2 id="librarian-method">Librarian: Refactoring Code to Create Libraries</h2> |
| 141 | + <p> |
| 142 | + Librarian is our method for refactoring existing code into a more organized and reusable library. By identifying common patterns and abstracting them into shared building blocks, Librarian compresses collections of programs while migrating them to use these new components—reducing overall code size and often improving functionality. The method operates on a simple sample-and-rerank framework, progressively building a library of useful functions to maximize our refactoring objective. <strong>Figure 1</strong> illustrates the overall process. |
| 143 | + </p> |
| 144 | + <p> |
| 145 | + Librarian operates on a simple sample-and-rerank framework to maximize our refactoring objective described above. It maintains and grows a library of useful functions as part of this objective. |
| 146 | + </p> |
| 147 | + <p>Concretely, our framework follows:</p> |
| 148 | + <div class="math-display"> |
| 149 | + $$ |
| 150 | + \mathcal{L}^\star, \left\{ \rho^\star_n \right\} = \arg\min_{\mathcal{L}, \left\{ \rho'_n \right\} \in \mathrm{Sample}(\left\{ \rho_n \right\})} |
| 151 | + \ell(\mathcal{L}, \left\{ \rho'_n \right\}). |
| 152 | + $$ |
| 153 | + </div> |
| 154 | + <h3>How It Works:</h3> |
| 155 | + <ul> |
| 156 | + <li><strong>Clustering:</strong> We group related input programs into "tuples" by having a language model summarize the code, then clustering these summaries. This focuses the language model's attention on relevant code chunks.</li> |
| 157 | + <li><strong>Sampling Refactorings:</strong> For each tuple, Librarian retrieves relevant existing library functions. Then, using the original code and retrieved functions as context, a language model proposes K candidate refactorings.</li> |
| 158 | + <li><strong>Ranking with Compression:</strong> All K candidates are evaluated. We select the refactoring that scores highest on quality and maintains (or improves) test accuracy compared to the original code. New, useful library functions from the chosen refactoring are then added to the Librarian library for future use.</li> |
| 159 | + </ul> |
| 160 | + </section> |
| 161 | + |
84 | 162 | <!-- <section> |
85 | 163 | <h2 id="video">Video</h2> |
86 | 164 | <iframe height="528" src="#" title="Supplemental video" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> |
|
0 commit comments