File size: 9,514 Bytes
c319cf6
87d8862
c319cf6
 
 
87d8862
 
c319cf6
 
 
 
87d8862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c319cf6
 
 
 
 
87d8862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""GraphLang interactive demo (Gradio) — DIDACTIC.

This is NOT the production GraphLang engine. It is a self-contained,
educational Python-AST reimplementation used only to illustrate the core
idea:
    source code  ->  semantic IR graph (12 kinds)  ->  hash-merge / dedup

The production engine (13 languages via tree-sitter, 238+296+242 CST node
types mapped to 12 IR kinds) is available under the MII Open License v1.1.
The benchmarks and datasets shipped alongside this demo were generated by
the REAL engine, not by this file.
"""

import ast
import hashlib
import json

import gradio as gr

from parallel_ir import detect_parallel_platform, normalize_thread_index

KINDS = {
    1: "function", 2: "if", 3: "for", 4: "while", 5: "return",
    6: "assign", 7: "call", 8: "binop", 9: "unary", 10: "var",
    11: "const", 12: "block",
}

_BINOPS = {
    ast.Add: "+", ast.Sub: "-", ast.Mult: "*", ast.Div: "/",
    ast.Eq: "==", ast.NotEq: "!=", ast.Lt: "<", ast.Gt: ">",
    ast.LtE: "<=", ast.GtE: ">=",
}


def _structural_hash(nid, nodes, memo):
    if nid in memo:
        return memo[nid]
    n = nodes[nid]
    children = tuple(_structural_hash(a, nodes, memo) for a in n["args"])
    content = json.dumps({
        "kind": n["kind"],
        "value": n.get("key", n.get("value")),
        "op": n["op"],
        "args": children,
    }, sort_keys=True)
    h = hashlib.sha256(content.encode()).hexdigest()[:12]
    memo[nid] = h
    return h


class Builder(ast.NodeVisitor):
    """Python AST -> GraphLang IR (didactic version)."""

    def __init__(self):
        self.nodes = {}
        self._n = 0
        self._names = {}

    def _canon(self, name):
        if name not in self._names:
            self._names[name] = f"v{len(self._names) + 1}"
        return self._names[name]

    def _var_node(self, name, args=None):
        return self._add("var", value=name, key=self._canon(name), args=args)

    def _add(self, kind, value=None, op="", args=None, key=None):
        self._n += 1
        nid = f"n{self._n}"
        node = {"kind": kind, "value": value, "op": op, "args": list(args or [])}
        if key is not None:
            node["key"] = key
        self.nodes[nid] = node
        return nid

    def build(self, code):
        self.nodes = {}
        self._n = 0
        self._names = {}
        tree = ast.parse(code)
        self.visit(tree)
        return self.nodes

    def visit_Module(self, node):
        return self._add("block", args=[self.visit(s) for s in node.body])

    def visit_FunctionDef(self, node):
        args = [self._var_node(a.arg) for a in node.args.args]
        body = [self.visit(s) for s in node.body]
        if len(body) == 1:
            body = body[0]
        else:
            body = self._add("block", args=body)
        return self._add("function", value=node.name, args=args + [body])

    def visit_Return(self, node):
        v = self.visit(node.value) if node.value else self._add("const", value=None)
        return self._add("return", args=[v])

    def visit_If(self, node):
        test = self.visit(node.test)
        then = self._add("block", args=[self.visit(s) for s in node.body])
        if node.orelse:
            orelse = self._add("block", args=[self.visit(s) for s in node.orelse])
            return self._add("if", args=[test, then, orelse])
        return self._add("if", args=[test, then])

    def visit_For(self, node):
        target = self._var_node(node.target.id)
        it = self.visit(node.iter)
        body = self._add("block", args=[self.visit(s) for s in node.body])
        return self._add("for", args=[target, it, body])

    def visit_While(self, node):
        test = self.visit(node.test)
        body = self._add("block", args=[self.visit(s) for s in node.body])
        return self._add("while", args=[test, body])

    def visit_Assign(self, node):
        val = self.visit(node.value)
        targets = [self._var_node(t.id) for t in node.targets
                   if isinstance(t, ast.Name)]
        return self._add("assign", args=targets + [val])

    def visit_Expr(self, node):
        return self.visit(node.value)

    def visit_Call(self, node):
        f = self.visit(node.func)
        return self._add("call", args=[f] + [self.visit(a) for a in node.args])

    def visit_Attribute(self, node):
        obj = self.visit(node.value)
        return self._var_node(node.attr, args=[obj])

    def visit_Name(self, node):
        return self._var_node(node.id)

    def visit_Constant(self, node):
        return self._add("const", value=node.value)

    def visit_BinOp(self, node):
        return self._add("binop", op=_BINOPS.get(type(node.op), "?"),
                         args=[self.visit(node.left), self.visit(node.right)])

    def visit_Compare(self, node):
        op = {ast.Eq: "==", ast.NotEq: "!=", ast.Lt: "<", ast.Gt: ">",
              ast.LtE: "<=", ast.GtE: ">="}.get(type(node.ops[0]), "?")
        return self._add("binop", op=op,
                         args=[self.visit(node.left), self.visit(node.comparators[0])])

    def visit_UnaryOp(self, node):
        op = {ast.USub: "-", ast.Not: "not", ast.UAdd: "+"}.get(type(node.op), "?")
        return self._add("unary", op=op, args=[self.visit(node.operand)])


def _graph_to_dot(nodes):
    lines = ["digraph G {", "  rankdir=TB;", '  node [shape=box, style=rounded];']
    for nid, n in nodes.items():
        label = n["kind"]
        if n.get("key"):
            label += f"\\n{n['value']}{n['key']}"
        elif n["value"] not in (None, ""):
            label += f"\\n{n['value']}"
        if n["op"]:
            label += f" [{n['op']}]"
        lines.append(f'  {nid} [label="{label}"];')
    for nid, n in nodes.items():
        for a in n["args"]:
            lines.append(f"  {nid} -> {a};")
    lines.append("}")
    return "\n".join(lines)


def _node_hashes(nodes):
    memo = {}
    return {_structural_hash(nid, nodes, memo) for nid in nodes}


def inspect(code):
    if not code.strip():
        return "_(paste code)_", ""
    try:
        nodes = Builder().build(code)
    except SyntaxError as e:
        return f"SyntaxError: {e}", ""
    kinds = {}
    for n in nodes.values():
        kinds[n["kind"]] = kinds.get(n["kind"], 0) + 1
    summary = f"{len(nodes)} nodes — " + ", ".join(
        f"{k}×{v}" for k, v in sorted(kinds.items()))
    return summary, _graph_to_dot(nodes)


def merge(code_a, code_b):
    try:
        na = Builder().build(code_a)
        nb = Builder().build(code_b)
    except SyntaxError as e:
        return f"SyntaxError: {e}"
    ha, hb = _node_hashes(na), _node_hashes(nb)
    shared = ha & hb
    union = ha | hb
    sim = len(shared) / len(union) if union else 0.0
    total = len(na) + len(nb)
    unique = len(union)
    comp = total / unique if unique else 0.0
    return (f"Graph A: {len(na)} nodes\nGraph B: {len(nb)} nodes\n"
            f"Union (unique): {unique}\n"
            f"Structural similarity: {sim*100:.1f}%\n"
            f"Compression (A+B -> merged): {comp:.1f}x")


def parallel(code):
    plat = detect_parallel_platform(code) or "none"
    norm = normalize_thread_index(code) if plat != "none" else code
    return f"Platform: {plat}\n\nNormalized:\n{norm}"


KINDS_TABLE = "\n".join(f"| {i} | `{k}` |" for i, k in KINDS.items())

with gr.Blocks(title="GraphLang demo") as demo:
    gr.Markdown("""
# GraphLang — Universal Semantic Kernel for Code

Same intent = same graph. Paste Python code and see its canonical IR graph;
merge two snippets and measure structural deduplication.

> **Didactic demo.** This Space runs a simplified Python-AST reimplementation
> to illustrate the concept. The production engine normalizes **13 languages**
> via tree-sitter and is licensed separately (MII Open License v1.1).
> Benchmarks and datasets in the companion model repo are from the real engine.
""")

    with gr.Tabs():
        with gr.Tab("IR inspector"):
            with gr.Row():
                inp = gr.Code(language="python", lines=8,
                              value="def add(a, b):\n    return a + b",
                              label="Python code")
                with gr.Column():
                    summary = gr.Textbox(label="IR summary", interactive=False)
                    dot = gr.Code(language="dot", lines=14, label="IR graph (DOT)")
            btn = gr.Button("Build IR")
            btn.click(inspect, inputs=inp, outputs=[summary, dot])

        with gr.Tab("Merge / dedup"):
            with gr.Row():
                a = gr.Code(language="python", lines=6,
                            value="def add(a, b):\n    return a + b", label="Graph A")
                b = gr.Code(language="python", lines=6,
                            value="def add(x, y):\n    return x + y", label="Graph B")
            out = gr.Textbox(label="Result", interactive=False)
            mbtn = gr.Button("Merge")
            mbtn.click(merge, inputs=[a, b], outputs=out)

        with gr.Tab("Parallel IR"):
            pc = gr.Code(language="cpp", lines=6,
                         value="int i = threadIdx.x + blockIdx.x * blockDim.x;",
                         label="GPU code")
            pout = gr.Textbox(label="Detection + normalization", interactive=False)
            pbtn = gr.Button("Analyze")
            pbtn.click(parallel, inputs=pc, outputs=pout)

        with gr.Tab("The 12 kinds"):
            gr.Markdown("| # | Kind |\n|---|------|\n" + KINDS_TABLE)

if __name__ == "__main__":
    demo.launch()