File size: 2,295 Bytes
10e1b83
59506b5
36ab8c4
10e1b83
 
1ff818b
 
10e1b83
36ab8c4
 
1ff818b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36ab8c4
 
1ff818b
ed78f98
36ab8c4
 
1ff818b
 
 
 
 
 
 
 
36ab8c4
 
1ff818b
 
 
 
36ab8c4
1ff818b
 
36ab8c4
1ff818b
36ab8c4
 
1ff818b
 
10e1b83
 
1ff818b
 
 
 
 
 
 
 
 
36ab8c4
1ff818b
36ab8c4
 
 
1ff818b
 
36ab8c4
 
 
10e1b83
 
97cba10
36ab8c4
1ff818b
 
 
 
 
36ab8c4
10e1b83
36ab8c4
1ff818b
36ab8c4
1ff818b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, List
import json

from utils import (
    call_llm,
    safe_json_loads,
    validate_ir
)


# =====================================================
# FALLBACK
# =====================================================

def fallback_patterns(
    num_proposals: int
):

    return [
        {
            "name": f"Pattern {i+1}",
            "description": "Pattern détecté automatiquement"
        }
        for i in range(num_proposals)
    ]


# =====================================================
# PATTERN DETECTION
# =====================================================

def detect_patterns(
    ir: Dict[str, Any],
    num_proposals: int = 3,
    provider: str = "openai"
) -> List[Dict[str, str]]:

    if not validate_ir(ir):
        return fallback_patterns(num_proposals)

    ir_text = json.dumps(
        ir,
        indent=2,
        ensure_ascii=False
    )

    prompt = f"""
Tu es un expert en graphes mathématiques
et compilation scientifique.

Analyse cette IR.

Détecte EXACTEMENT {num_proposals}
patterns mathématiques importants.

Exemples possibles :
- produit scalaire
- convolution
- pipeline tensoriel
- réduction
- propagation
- matrice creuse
- calcul distribué
- accumulation
- normalisation
- opération SIMD

IMPORTANT :
- retourne UNIQUEMENT du JSON valide
- aucune balise markdown
- aucun texte hors JSON

FORMAT STRICT :

[
  {{
    "name": "Convolution",
    "description": "Détection d'un motif convolutionnel"
  }}
]

IR :
{ir_text}
"""

    response = call_llm(
        prompt,
        provider=provider,
        max_tokens=1800
    )

    parsed = safe_json_loads(response)

    valid_patterns = []

    if isinstance(parsed, list):

        for item in parsed:

            if not isinstance(item, dict):
                continue

            name = item.get(
                "name",
                "Unknown Pattern"
            )

            description = item.get(
                "description",
                "Description indisponible"
            )

            valid_patterns.append({
                "name": str(name),
                "description": str(description)
            })

    if len(valid_patterns) > 0:

        return valid_patterns[:num_proposals]

    return fallback_patterns(num_proposals)