File size: 7,574 Bytes
80603a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Compile PentaNet_NeurIPS_Draft.md + figures into a single publication PDF.
Uses WeasyPrint for HTML→PDF rendering.
"""
import markdown
import os
import re
from weasyprint import HTML

MD_FILE = 'PentaNet_NeurIPS_Draft.md'
OUTPUT  = 'paper/PentaNet_Technical_Report.pdf'

with open(MD_FILE, 'r') as f:
    md_text = f.read()

# --- Strip LaTeX math (WeasyPrint cannot render it) ---
def delatex(text):
    """Convert LaTeX math notation to readable Unicode text."""
    # Remove display math blocks $$ ... $$ first
    def replace_display(m):
        content = m.group(1).strip()
        # Common display equations
        content = content.replace('\\max', 'max')
        content = content.replace('\\min', 'min')
        content = content.replace('\\sum', 'Σ')
        content = content.replace('\\left(', '(').replace('\\right)', ')')
        content = content.replace('\\left\\{', '{').replace('\\right\\}', '}')
        content = content.replace('\\frac{1}{d}', '(1/d)')
        content = content.replace('\\frac{1}{n}', '(1/n)')
        content = content.replace('\\text{Round}', 'Round')
        content = content.replace('\\text{Clip}', 'Clip')
        content = content.replace('\\text{detach}', 'detach')
        content = content.replace('\\bar{W}', 'W̄')
        content = content.replace('\\gamma', 'γ')
        content = content.replace('\\epsilon', 'ε')
        content = content.replace('\\mathbb{R}', 'ℝ')
        content = content.replace('\\in', '∈')
        content = content.replace('\\times', '×')
        content = content.replace('\\cdot', '·')
        content = content.replace('\\pm', '±')
        content = content.replace('\\sigma', 'σ')
        content = content.replace('\\lambda', 'λ')
        content = content.replace('\\sim', '~')
        content = content.replace('\\approx', '≈')
        content = content.replace('\\log_2(3)', 'log₂(3)')
        content = content.replace('\\log_2(5)', 'log₂(5)')
        content = content.replace('\\leftrightarrow', '↔')
        content = content.replace('|', '|')
        content = re.sub(r'_\{([^}]+)\}', r'_\1', content)  # subscripts
        content = re.sub(r'\^T', 'ᵀ', content)
        content = re.sub(r'\\[a-zA-Z]+', '', content)  # remove remaining commands
        content = re.sub(r'[{}]', '', content)  # remove braces
        return content

    text = re.sub(r'\$\$(.*?)\$\$', replace_display, text, flags=re.DOTALL)

    # Inline math $...$
    def replace_inline(m):
        c = m.group(1)
        c = c.replace('\\{', '{').replace('\\}', '}')
        c = c.replace('\\pm', '±')
        c = c.replace('\\bar{W}', 'W̄')
        c = c.replace('\\gamma', 'γ')
        c = c.replace('\\epsilon', 'ε')
        c = c.replace('\\sigma', 'σ')
        c = c.replace('\\lambda', 'λ')
        c = c.replace('\\sim', '~')
        c = c.replace('\\approx', '≈')
        c = c.replace('\\log_2(3)', 'log₂(3)')
        c = c.replace('\\log_2(5)', 'log₂(5)')
        c = c.replace('\\leftrightarrow', '↔')
        c = c.replace('\\mathbb{R}', 'ℝ')
        c = c.replace('\\text{detach}', 'detach')
        c = c.replace('\\text{Round}', 'Round')
        c = c.replace('\\text{Clip}', 'Clip')
        c = c.replace('\\times', '×')
        c = c.replace('\\cdot', '·')
        c = re.sub(r'\^T', 'ᵀ', c)
        c = re.sub(r'_\{([^}]+)\}', r'_\1', c)
        c = re.sub(r'\\[a-zA-Z]+', '', c)
        c = re.sub(r'[{}]', '', c)
        return c

    text = re.sub(r'\$([^$]+?)\$', replace_inline, text)
    return text

md_text = delatex(md_text)

# Convert markdown to HTML
html_body = markdown.markdown(md_text, extensions=['tables', 'fenced_code', 'codehilite'])

# Inject figure references (replace placeholders or insert after relevant sections)
# We insert Figure 1 after "4.2 Convergence Dynamics" and Figure 2 after "4.3 Weight Distribution"
fig1_path = os.path.abspath('paper/figures/figure1_ppl_convergence.png')
fig2_path = os.path.abspath('paper/figures/figure2_weight_distribution.png')

fig1_html = f'''
<div class="figure">
  <img src="file://{fig1_path}" alt="Figure 1: PPL Convergence" />
  <p class="caption"><strong>Figure 1.</strong> Validation perplexity convergence on WikiText-103. 
  Solid lines: PentaNet (pentanary). Dashed lines: BitNet (ternary). 
  Three independent seeds per architecture. PentaNet consistently achieves lower PPL from iteration ~2,000 onward.</p>
</div>
'''

fig2_html = f'''
<div class="figure">
  <img src="file://{fig2_path}" alt="Figure 2: Weight Distribution" />
  <p class="caption"><strong>Figure 2.</strong> PentaNet quantized weight distribution over training (Seed 42).
  All five buckets maintain stable occupancy throughout 10,000 iterations, with ±2 states at ~11%.
  No collapse toward ternary is observed.</p>
</div>
'''

# Insert figures after the relevant sections
html_body = html_body.replace(
    '<h3>4.3 Weight Distribution Stability (Non-Collapse Analysis)</h3>',
    fig1_html + '<h3>4.3 Weight Distribution Stability (Non-Collapse Analysis)</h3>'
)
html_body = html_body.replace(
    '<h2>5. Discussion</h2>',
    fig2_html + '<h2>5. Discussion</h2>'
)

# Full HTML document with academic styling
full_html = f'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
  @import url('https://fonts.googleapis.com/css2?family=Source+Serif+4:wght@400;600;700&family=Source+Code+Pro:wght@400&display=swap');

  @page {{
    size: A4;
    margin: 2.5cm 2cm;
    @bottom-center {{ content: counter(page); font-size: 10pt; color: #666; }}
  }}

  body {{
    font-family: 'Source Serif 4', 'Georgia', serif;
    font-size: 11pt;
    line-height: 1.55;
    color: #1a1a1a;
    max-width: 100%;
  }}

  h1 {{
    font-size: 20pt;
    text-align: center;
    margin-bottom: 6pt;
    line-height: 1.2;
  }}

  /* Author block */
  h1 + p, h1 + p + p, h1 + p + p + p {{
    text-align: center;
    margin: 2pt 0;
  }}

  h2 {{
    font-size: 14pt;
    margin-top: 24pt;
    margin-bottom: 8pt;
    border-bottom: 1px solid #ccc;
    padding-bottom: 4pt;
  }}

  h3 {{
    font-size: 12pt;
    margin-top: 16pt;
    margin-bottom: 6pt;
  }}

  p {{
    text-align: justify;
    margin: 6pt 0;
  }}

  table {{
    border-collapse: collapse;
    width: 100%;
    margin: 12pt 0;
    font-size: 10pt;
  }}

  th, td {{
    border: 1px solid #ccc;
    padding: 6pt 10pt;
    text-align: center;
  }}

  th {{
    background-color: #f5f5f5;
    font-weight: 600;
  }}

  code {{
    font-family: 'Source Code Pro', monospace;
    font-size: 9.5pt;
    background: #f4f4f4;
    padding: 1pt 4pt;
    border-radius: 3pt;
  }}

  pre {{
    background: #f4f4f4;
    padding: 10pt;
    border-radius: 4pt;
    font-size: 9pt;
    overflow-x: auto;
  }}

  pre code {{
    background: none;
    padding: 0;
  }}

  .figure {{
    text-align: center;
    margin: 20pt 0;
    page-break-inside: avoid;
  }}

  .figure img {{
    max-width: 95%;
    height: auto;
  }}

  .caption {{
    font-size: 9.5pt;
    color: #444;
    margin-top: 6pt;
    text-align: center;
    font-style: italic;
  }}

  hr {{
    border: none;
    border-top: 1px solid #ddd;
    margin: 16pt 0;
  }}

  ul, ol {{
    margin: 6pt 0;
    padding-left: 20pt;
  }}

  li {{
    margin: 3pt 0;
  }}

  strong {{
    font-weight: 600;
  }}

  /* Abstract styling */
  h2:first-of-type + p {{
    font-style: italic;
  }}
</style>
</head>
<body>
{html_body}
</body>
</html>
'''

os.makedirs('paper', exist_ok=True)

print("📝 Compiling PDF...")
HTML(string=full_html).write_pdf(OUTPUT)
print(f"✅ PDF written to {OUTPUT}")